1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 * Copyright (c) 2020, 2022 Linaro Limited 5 * Copyright (c) 2022, Technology Innovation Institute (TII) 6 */ 7 8 #include <assert.h> 9 #include <bitstring.h> 10 #include <compiler.h> 11 #include <config.h> 12 #include <crypto/crypto.h> 13 #include <kernel/tee_ta_manager.h> 14 #include <kernel/user_access.h> 15 #include <memtag.h> 16 #include <mm/vm.h> 17 #include <stdlib_ext.h> 18 #include <string_ext.h> 19 #include <string.h> 20 #include <sys/queue.h> 21 #include <tee_api_defines_extensions.h> 22 #include <tee_api_types.h> 23 #include <tee/tee_cryp_utl.h> 24 #include <tee/tee_obj.h> 25 #include <tee/tee_svc_cryp.h> 26 #include <tee/tee_svc.h> 27 #include <trace.h> 28 #include <utee_defines.h> 29 #include <util.h> 30 #if defined(CFG_CRYPTO_HKDF) 31 #include <tee/tee_cryp_hkdf.h> 32 #endif 33 #if defined(CFG_CRYPTO_CONCAT_KDF) 34 #include <tee/tee_cryp_concat_kdf.h> 35 #endif 36 #if defined(CFG_CRYPTO_PBKDF2) 37 #include <tee/tee_cryp_pbkdf2.h> 38 #endif 39 40 enum cryp_state { 41 CRYP_STATE_INITIALIZED = 0, 42 CRYP_STATE_UNINITIALIZED 43 }; 44 45 typedef void (*tee_cryp_ctx_finalize_func_t) (void *ctx); 46 struct tee_cryp_state { 47 TAILQ_ENTRY(tee_cryp_state) link; 48 uint32_t algo; 49 uint32_t mode; 50 vaddr_t key1; 51 vaddr_t key2; 52 void *ctx; 53 tee_cryp_ctx_finalize_func_t ctx_finalize; 54 enum cryp_state state; 55 }; 56 57 struct tee_cryp_obj_secret { 58 uint32_t key_size; 59 uint32_t alloc_size; 60 61 /* 62 * Pseudo code visualize layout of structure 63 * Next follows data, such as: 64 * uint8_t data[alloc_size] 65 * key_size must never exceed alloc_size 66 */ 67 }; 68 69 #define TEE_TYPE_ATTR_OPTIONAL BIT(0) 70 #define TEE_TYPE_ATTR_REQUIRED BIT(1) 71 #define TEE_TYPE_ATTR_OPTIONAL_GROUP BIT(2) 72 #define TEE_TYPE_ATTR_SIZE_INDICATOR BIT(3) 73 #define TEE_TYPE_ATTR_GEN_KEY_OPT BIT(4) 74 #define TEE_TYPE_ATTR_GEN_KEY_REQ BIT(5) 75 #define TEE_TYPE_ATTR_BIGNUM_MAXBITS BIT(6) 76 77 /* Handle storing of generic secret keys of varying lengths */ 78 #define ATTR_OPS_INDEX_SECRET 0 79 /* Convert to/from big-endian byte array and provider-specific bignum */ 80 #define ATTR_OPS_INDEX_BIGNUM 1 81 /* Convert to/from value attribute depending on direction */ 82 #define ATTR_OPS_INDEX_VALUE 2 83 /* Convert to/from curve25519 attribute depending on direction */ 84 #define ATTR_OPS_INDEX_25519 3 85 86 /* Curve25519 key bytes size is always 32 bytes*/ 87 #define KEY_SIZE_BYTES_25519 UL(32) 88 /* TEE Internal Core API v1.3.1, Table 6-8 */ 89 #define TEE_ED25519_CTX_MAX_LENGTH 255 90 91 struct tee_cryp_obj_type_attrs { 92 uint32_t attr_id; 93 uint16_t flags; 94 uint16_t ops_index; 95 uint16_t raw_offs; 96 uint16_t raw_size; 97 }; 98 99 #define RAW_DATA(_x, _y) \ 100 .raw_offs = offsetof(_x, _y), .raw_size = MEMBER_SIZE(_x, _y) 101 102 static const struct tee_cryp_obj_type_attrs 103 tee_cryp_obj_secret_value_attrs[] = { 104 { 105 .attr_id = TEE_ATTR_SECRET_VALUE, 106 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR, 107 .ops_index = ATTR_OPS_INDEX_SECRET, 108 .raw_offs = 0, 109 .raw_size = 0 110 }, 111 }; 112 113 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_rsa_pub_key_attrs[] = { 114 { 115 .attr_id = TEE_ATTR_RSA_MODULUS, 116 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR, 117 .ops_index = ATTR_OPS_INDEX_BIGNUM, 118 RAW_DATA(struct rsa_public_key, n) 119 }, 120 121 { 122 .attr_id = TEE_ATTR_RSA_PUBLIC_EXPONENT, 123 .flags = TEE_TYPE_ATTR_REQUIRED, 124 .ops_index = ATTR_OPS_INDEX_BIGNUM, 125 RAW_DATA(struct rsa_public_key, e) 126 }, 127 }; 128 129 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_rsa_keypair_attrs[] = { 130 { 131 .attr_id = TEE_ATTR_RSA_MODULUS, 132 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR, 133 .ops_index = ATTR_OPS_INDEX_BIGNUM, 134 RAW_DATA(struct rsa_keypair, n) 135 }, 136 137 { 138 .attr_id = TEE_ATTR_RSA_PUBLIC_EXPONENT, 139 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_GEN_KEY_OPT, 140 .ops_index = ATTR_OPS_INDEX_BIGNUM, 141 RAW_DATA(struct rsa_keypair, e) 142 }, 143 144 { 145 .attr_id = TEE_ATTR_RSA_PRIVATE_EXPONENT, 146 .flags = TEE_TYPE_ATTR_REQUIRED, 147 .ops_index = ATTR_OPS_INDEX_BIGNUM, 148 RAW_DATA(struct rsa_keypair, d) 149 }, 150 151 { 152 .attr_id = TEE_ATTR_RSA_PRIME1, 153 .flags = TEE_TYPE_ATTR_OPTIONAL_GROUP, 154 .ops_index = ATTR_OPS_INDEX_BIGNUM, 155 RAW_DATA(struct rsa_keypair, p) 156 }, 157 158 { 159 .attr_id = TEE_ATTR_RSA_PRIME2, 160 .flags = TEE_TYPE_ATTR_OPTIONAL_GROUP, 161 .ops_index = ATTR_OPS_INDEX_BIGNUM, 162 RAW_DATA(struct rsa_keypair, q) 163 }, 164 165 { 166 .attr_id = TEE_ATTR_RSA_EXPONENT1, 167 .flags = TEE_TYPE_ATTR_OPTIONAL_GROUP, 168 .ops_index = ATTR_OPS_INDEX_BIGNUM, 169 RAW_DATA(struct rsa_keypair, dp) 170 }, 171 172 { 173 .attr_id = TEE_ATTR_RSA_EXPONENT2, 174 .flags = TEE_TYPE_ATTR_OPTIONAL_GROUP, 175 .ops_index = ATTR_OPS_INDEX_BIGNUM, 176 RAW_DATA(struct rsa_keypair, dq) 177 }, 178 179 { 180 .attr_id = TEE_ATTR_RSA_COEFFICIENT, 181 .flags = TEE_TYPE_ATTR_OPTIONAL_GROUP, 182 .ops_index = ATTR_OPS_INDEX_BIGNUM, 183 RAW_DATA(struct rsa_keypair, qp) 184 }, 185 }; 186 187 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_dsa_pub_key_attrs[] = { 188 { 189 .attr_id = TEE_ATTR_DSA_PRIME, 190 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_BIGNUM_MAXBITS | 191 TEE_TYPE_ATTR_SIZE_INDICATOR, 192 .ops_index = ATTR_OPS_INDEX_BIGNUM, 193 RAW_DATA(struct dsa_public_key, p) 194 }, 195 196 { 197 .attr_id = TEE_ATTR_DSA_SUBPRIME, 198 .flags = TEE_TYPE_ATTR_REQUIRED, 199 .ops_index = ATTR_OPS_INDEX_BIGNUM, 200 RAW_DATA(struct dsa_public_key, q) 201 }, 202 203 { 204 .attr_id = TEE_ATTR_DSA_BASE, 205 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_BIGNUM_MAXBITS, 206 .ops_index = ATTR_OPS_INDEX_BIGNUM, 207 RAW_DATA(struct dsa_public_key, g) 208 }, 209 210 { 211 .attr_id = TEE_ATTR_DSA_PUBLIC_VALUE, 212 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_BIGNUM_MAXBITS, 213 .ops_index = ATTR_OPS_INDEX_BIGNUM, 214 RAW_DATA(struct dsa_public_key, y) 215 }, 216 }; 217 218 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_dsa_keypair_attrs[] = { 219 { 220 .attr_id = TEE_ATTR_DSA_PRIME, 221 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_GEN_KEY_REQ | 222 TEE_TYPE_ATTR_BIGNUM_MAXBITS | TEE_TYPE_ATTR_SIZE_INDICATOR, 223 .ops_index = ATTR_OPS_INDEX_BIGNUM, 224 RAW_DATA(struct dsa_keypair, p) 225 }, 226 227 { 228 .attr_id = TEE_ATTR_DSA_SUBPRIME, 229 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_GEN_KEY_REQ, 230 .ops_index = ATTR_OPS_INDEX_BIGNUM, 231 RAW_DATA(struct dsa_keypair, q) 232 }, 233 234 { 235 .attr_id = TEE_ATTR_DSA_BASE, 236 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_GEN_KEY_REQ | 237 TEE_TYPE_ATTR_BIGNUM_MAXBITS, 238 .ops_index = ATTR_OPS_INDEX_BIGNUM, 239 RAW_DATA(struct dsa_keypair, g) 240 }, 241 242 { 243 .attr_id = TEE_ATTR_DSA_PRIVATE_VALUE, 244 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_BIGNUM_MAXBITS, 245 .ops_index = ATTR_OPS_INDEX_BIGNUM, 246 RAW_DATA(struct dsa_keypair, x) 247 }, 248 249 { 250 .attr_id = TEE_ATTR_DSA_PUBLIC_VALUE, 251 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_BIGNUM_MAXBITS, 252 .ops_index = ATTR_OPS_INDEX_BIGNUM, 253 RAW_DATA(struct dsa_keypair, y) 254 }, 255 }; 256 257 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_dh_keypair_attrs[] = { 258 { 259 .attr_id = TEE_ATTR_DH_PRIME, 260 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR | 261 TEE_TYPE_ATTR_GEN_KEY_REQ, 262 .ops_index = ATTR_OPS_INDEX_BIGNUM, 263 RAW_DATA(struct dh_keypair, p) 264 }, 265 266 { 267 .attr_id = TEE_ATTR_DH_BASE, 268 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_GEN_KEY_REQ, 269 .ops_index = ATTR_OPS_INDEX_BIGNUM, 270 RAW_DATA(struct dh_keypair, g) 271 }, 272 273 { 274 .attr_id = TEE_ATTR_DH_PUBLIC_VALUE, 275 .flags = TEE_TYPE_ATTR_REQUIRED, 276 .ops_index = ATTR_OPS_INDEX_BIGNUM, 277 RAW_DATA(struct dh_keypair, y) 278 }, 279 280 { 281 .attr_id = TEE_ATTR_DH_PRIVATE_VALUE, 282 .flags = TEE_TYPE_ATTR_REQUIRED, 283 .ops_index = ATTR_OPS_INDEX_BIGNUM, 284 RAW_DATA(struct dh_keypair, x) 285 }, 286 287 { 288 .attr_id = TEE_ATTR_DH_SUBPRIME, 289 .flags = TEE_TYPE_ATTR_OPTIONAL_GROUP | TEE_TYPE_ATTR_GEN_KEY_OPT, 290 .ops_index = ATTR_OPS_INDEX_BIGNUM, 291 RAW_DATA(struct dh_keypair, q) 292 }, 293 294 { 295 .attr_id = TEE_ATTR_DH_X_BITS, 296 .flags = TEE_TYPE_ATTR_GEN_KEY_OPT, 297 .ops_index = ATTR_OPS_INDEX_VALUE, 298 RAW_DATA(struct dh_keypair, xbits) 299 }, 300 }; 301 302 #if defined(CFG_CRYPTO_HKDF) 303 static const struct tee_cryp_obj_type_attrs 304 tee_cryp_obj_hkdf_ikm_attrs[] = { 305 { 306 .attr_id = TEE_ATTR_HKDF_IKM, 307 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR, 308 .ops_index = ATTR_OPS_INDEX_SECRET, 309 .raw_offs = 0, 310 .raw_size = 0 311 }, 312 }; 313 #endif 314 315 #if defined(CFG_CRYPTO_CONCAT_KDF) 316 static const struct tee_cryp_obj_type_attrs 317 tee_cryp_obj_concat_kdf_z_attrs[] = { 318 { 319 .attr_id = TEE_ATTR_CONCAT_KDF_Z, 320 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR, 321 .ops_index = ATTR_OPS_INDEX_SECRET, 322 .raw_offs = 0, 323 .raw_size = 0 324 }, 325 }; 326 #endif 327 328 #if defined(CFG_CRYPTO_PBKDF2) 329 static const struct tee_cryp_obj_type_attrs 330 tee_cryp_obj_pbkdf2_passwd_attrs[] = { 331 { 332 .attr_id = TEE_ATTR_PBKDF2_PASSWORD, 333 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR, 334 .ops_index = ATTR_OPS_INDEX_SECRET, 335 .raw_offs = 0, 336 .raw_size = 0 337 }, 338 }; 339 #endif 340 341 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_ecc_pub_key_attrs[] = { 342 { 343 .attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_X, 344 .flags = TEE_TYPE_ATTR_REQUIRED, 345 .ops_index = ATTR_OPS_INDEX_BIGNUM, 346 RAW_DATA(struct ecc_public_key, x) 347 }, 348 349 { 350 .attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_Y, 351 .flags = TEE_TYPE_ATTR_REQUIRED, 352 .ops_index = ATTR_OPS_INDEX_BIGNUM, 353 RAW_DATA(struct ecc_public_key, y) 354 }, 355 356 { 357 .attr_id = TEE_ATTR_ECC_CURVE, 358 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR, 359 .ops_index = ATTR_OPS_INDEX_VALUE, 360 RAW_DATA(struct ecc_public_key, curve) 361 }, 362 }; 363 364 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_ecc_keypair_attrs[] = { 365 { 366 .attr_id = TEE_ATTR_ECC_PRIVATE_VALUE, 367 .flags = TEE_TYPE_ATTR_REQUIRED, 368 .ops_index = ATTR_OPS_INDEX_BIGNUM, 369 RAW_DATA(struct ecc_keypair, d) 370 }, 371 372 { 373 .attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_X, 374 .flags = TEE_TYPE_ATTR_REQUIRED, 375 .ops_index = ATTR_OPS_INDEX_BIGNUM, 376 RAW_DATA(struct ecc_keypair, x) 377 }, 378 379 { 380 .attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_Y, 381 .flags = TEE_TYPE_ATTR_REQUIRED, 382 .ops_index = ATTR_OPS_INDEX_BIGNUM, 383 RAW_DATA(struct ecc_keypair, y) 384 }, 385 386 { 387 .attr_id = TEE_ATTR_ECC_CURVE, 388 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR | 389 TEE_TYPE_ATTR_GEN_KEY_REQ, 390 .ops_index = ATTR_OPS_INDEX_VALUE, 391 RAW_DATA(struct ecc_keypair, curve) 392 }, 393 }; 394 395 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_sm2_pub_key_attrs[] = { 396 { 397 .attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_X, 398 .flags = TEE_TYPE_ATTR_REQUIRED, 399 .ops_index = ATTR_OPS_INDEX_BIGNUM, 400 RAW_DATA(struct ecc_public_key, x) 401 }, 402 403 { 404 .attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_Y, 405 .flags = TEE_TYPE_ATTR_REQUIRED, 406 .ops_index = ATTR_OPS_INDEX_BIGNUM, 407 RAW_DATA(struct ecc_public_key, y) 408 }, 409 }; 410 411 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_sm2_keypair_attrs[] = { 412 { 413 .attr_id = TEE_ATTR_ECC_PRIVATE_VALUE, 414 .flags = TEE_TYPE_ATTR_REQUIRED, 415 .ops_index = ATTR_OPS_INDEX_BIGNUM, 416 RAW_DATA(struct ecc_keypair, d) 417 }, 418 419 { 420 .attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_X, 421 .flags = TEE_TYPE_ATTR_REQUIRED, 422 .ops_index = ATTR_OPS_INDEX_BIGNUM, 423 RAW_DATA(struct ecc_keypair, x) 424 }, 425 426 { 427 .attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_Y, 428 .flags = TEE_TYPE_ATTR_REQUIRED, 429 .ops_index = ATTR_OPS_INDEX_BIGNUM, 430 RAW_DATA(struct ecc_keypair, y) 431 }, 432 }; 433 434 static 435 const struct tee_cryp_obj_type_attrs tee_cryp_obj_x25519_keypair_attrs[] = { 436 { 437 .attr_id = TEE_ATTR_X25519_PRIVATE_VALUE, 438 .flags = TEE_TYPE_ATTR_REQUIRED, 439 .ops_index = ATTR_OPS_INDEX_25519, 440 RAW_DATA(struct x25519_keypair, priv) 441 }, 442 443 { 444 .attr_id = TEE_ATTR_X25519_PUBLIC_VALUE, 445 .flags = TEE_TYPE_ATTR_REQUIRED, 446 .ops_index = ATTR_OPS_INDEX_25519, 447 RAW_DATA(struct x25519_keypair, pub) 448 }, 449 }; 450 451 static 452 const struct tee_cryp_obj_type_attrs tee_cryp_obj_ed25519_pub_key_attrs[] = { 453 { 454 .attr_id = TEE_ATTR_ED25519_PUBLIC_VALUE, 455 .flags = TEE_TYPE_ATTR_REQUIRED, 456 .ops_index = ATTR_OPS_INDEX_25519, 457 RAW_DATA(struct ed25519_keypair, pub) 458 }, 459 }; 460 461 static 462 const struct tee_cryp_obj_type_attrs tee_cryp_obj_ed25519_keypair_attrs[] = { 463 { 464 .attr_id = TEE_ATTR_ED25519_PRIVATE_VALUE, 465 .flags = TEE_TYPE_ATTR_REQUIRED, 466 .ops_index = ATTR_OPS_INDEX_25519, 467 RAW_DATA(struct ed25519_keypair, priv) 468 }, 469 470 { 471 .attr_id = TEE_ATTR_ED25519_PUBLIC_VALUE, 472 .flags = TEE_TYPE_ATTR_REQUIRED, 473 .ops_index = ATTR_OPS_INDEX_25519, 474 RAW_DATA(struct ed25519_keypair, pub) 475 }, 476 }; 477 478 struct tee_cryp_obj_type_props { 479 TEE_ObjectType obj_type; 480 uint16_t min_size; /* may not be smaller than this */ 481 uint16_t max_size; /* may not be larger than this */ 482 uint16_t alloc_size; /* this many bytes are allocated to hold data */ 483 uint8_t quanta; /* may only be an multiple of this */ 484 485 uint8_t num_type_attrs; 486 const struct tee_cryp_obj_type_attrs *type_attrs; 487 }; 488 489 #define PROP(obj_type, quanta, min_size, max_size, alloc_size, type_attrs) \ 490 { (obj_type), (min_size), (max_size), (alloc_size), (quanta), \ 491 ARRAY_SIZE(type_attrs), (type_attrs) } 492 493 static const struct tee_cryp_obj_type_props tee_cryp_obj_props[] = { 494 PROP(TEE_TYPE_AES, 64, 128, 256, /* valid sizes 128, 192, 256 */ 495 256 / 8 + sizeof(struct tee_cryp_obj_secret), 496 tee_cryp_obj_secret_value_attrs), 497 PROP(TEE_TYPE_DES, 64, 64, 64, 498 /* Valid size 64 with parity */ 499 64 / 8 + sizeof(struct tee_cryp_obj_secret), 500 tee_cryp_obj_secret_value_attrs), 501 PROP(TEE_TYPE_DES3, 64, 128, 192, 502 /* Valid sizes 128, 192 with parity */ 503 192 / 8 + sizeof(struct tee_cryp_obj_secret), 504 tee_cryp_obj_secret_value_attrs), 505 PROP(TEE_TYPE_SM4, 128, 128, 128, 506 128 / 8 + sizeof(struct tee_cryp_obj_secret), 507 tee_cryp_obj_secret_value_attrs), 508 PROP(TEE_TYPE_HMAC_MD5, 8, 64, 512, 509 512 / 8 + sizeof(struct tee_cryp_obj_secret), 510 tee_cryp_obj_secret_value_attrs), 511 PROP(TEE_TYPE_HMAC_SHA1, 8, 80, 512, 512 512 / 8 + sizeof(struct tee_cryp_obj_secret), 513 tee_cryp_obj_secret_value_attrs), 514 PROP(TEE_TYPE_HMAC_SHA224, 8, 112, 512, 515 512 / 8 + sizeof(struct tee_cryp_obj_secret), 516 tee_cryp_obj_secret_value_attrs), 517 PROP(TEE_TYPE_HMAC_SHA256, 8, 192, 1024, 518 1024 / 8 + sizeof(struct tee_cryp_obj_secret), 519 tee_cryp_obj_secret_value_attrs), 520 PROP(TEE_TYPE_HMAC_SHA384, 8, 256, 1024, 521 1024 / 8 + sizeof(struct tee_cryp_obj_secret), 522 tee_cryp_obj_secret_value_attrs), 523 PROP(TEE_TYPE_HMAC_SHA512, 8, 256, 1024, 524 1024 / 8 + sizeof(struct tee_cryp_obj_secret), 525 tee_cryp_obj_secret_value_attrs), 526 PROP(TEE_TYPE_HMAC_SM3, 8, 80, 1024, 527 512 / 8 + sizeof(struct tee_cryp_obj_secret), 528 tee_cryp_obj_secret_value_attrs), 529 PROP(TEE_TYPE_GENERIC_SECRET, 8, 0, 4096, 530 4096 / 8 + sizeof(struct tee_cryp_obj_secret), 531 tee_cryp_obj_secret_value_attrs), 532 #if defined(CFG_CRYPTO_HKDF) 533 PROP(TEE_TYPE_HKDF_IKM, 8, 0, 4096, 534 4096 / 8 + sizeof(struct tee_cryp_obj_secret), 535 tee_cryp_obj_hkdf_ikm_attrs), 536 #endif 537 #if defined(CFG_CRYPTO_CONCAT_KDF) 538 PROP(TEE_TYPE_CONCAT_KDF_Z, 8, 0, 4096, 539 4096 / 8 + sizeof(struct tee_cryp_obj_secret), 540 tee_cryp_obj_concat_kdf_z_attrs), 541 #endif 542 #if defined(CFG_CRYPTO_PBKDF2) 543 PROP(TEE_TYPE_PBKDF2_PASSWORD, 8, 0, 4096, 544 4096 / 8 + sizeof(struct tee_cryp_obj_secret), 545 tee_cryp_obj_pbkdf2_passwd_attrs), 546 #endif 547 PROP(TEE_TYPE_RSA_PUBLIC_KEY, 1, 256, CFG_CORE_BIGNUM_MAX_BITS, 548 sizeof(struct rsa_public_key), 549 tee_cryp_obj_rsa_pub_key_attrs), 550 551 PROP(TEE_TYPE_RSA_KEYPAIR, 1, 256, CFG_CORE_BIGNUM_MAX_BITS, 552 sizeof(struct rsa_keypair), 553 tee_cryp_obj_rsa_keypair_attrs), 554 555 PROP(TEE_TYPE_DSA_PUBLIC_KEY, 64, 512, 3072, 556 sizeof(struct dsa_public_key), 557 tee_cryp_obj_dsa_pub_key_attrs), 558 559 PROP(TEE_TYPE_DSA_KEYPAIR, 64, 512, 3072, 560 sizeof(struct dsa_keypair), 561 tee_cryp_obj_dsa_keypair_attrs), 562 563 PROP(TEE_TYPE_DH_KEYPAIR, 1, 256, 2048, 564 sizeof(struct dh_keypair), 565 tee_cryp_obj_dh_keypair_attrs), 566 567 PROP(TEE_TYPE_ECDSA_PUBLIC_KEY, 1, 192, 521, 568 sizeof(struct ecc_public_key), 569 tee_cryp_obj_ecc_pub_key_attrs), 570 571 PROP(TEE_TYPE_ECDSA_KEYPAIR, 1, 192, 521, 572 sizeof(struct ecc_keypair), 573 tee_cryp_obj_ecc_keypair_attrs), 574 575 PROP(TEE_TYPE_ECDH_PUBLIC_KEY, 1, 192, 521, 576 sizeof(struct ecc_public_key), 577 tee_cryp_obj_ecc_pub_key_attrs), 578 579 PROP(TEE_TYPE_ECDH_KEYPAIR, 1, 192, 521, 580 sizeof(struct ecc_keypair), 581 tee_cryp_obj_ecc_keypair_attrs), 582 583 PROP(TEE_TYPE_SM2_DSA_PUBLIC_KEY, 1, 256, 256, 584 sizeof(struct ecc_public_key), 585 tee_cryp_obj_sm2_pub_key_attrs), 586 587 PROP(TEE_TYPE_SM2_DSA_KEYPAIR, 1, 256, 256, 588 sizeof(struct ecc_keypair), 589 tee_cryp_obj_sm2_keypair_attrs), 590 591 PROP(TEE_TYPE_SM2_PKE_PUBLIC_KEY, 1, 256, 256, 592 sizeof(struct ecc_public_key), 593 tee_cryp_obj_sm2_pub_key_attrs), 594 595 PROP(TEE_TYPE_SM2_PKE_KEYPAIR, 1, 256, 256, 596 sizeof(struct ecc_keypair), 597 tee_cryp_obj_sm2_keypair_attrs), 598 599 PROP(TEE_TYPE_SM2_KEP_PUBLIC_KEY, 1, 256, 256, 600 sizeof(struct ecc_public_key), 601 tee_cryp_obj_sm2_pub_key_attrs), 602 603 PROP(TEE_TYPE_SM2_KEP_KEYPAIR, 1, 256, 256, 604 sizeof(struct ecc_keypair), 605 tee_cryp_obj_sm2_keypair_attrs), 606 607 PROP(TEE_TYPE_X25519_KEYPAIR, 1, 256, 256, 608 sizeof(struct x25519_keypair), 609 tee_cryp_obj_x25519_keypair_attrs), 610 611 PROP(TEE_TYPE_ED25519_PUBLIC_KEY, 1, 256, 256, 612 sizeof(struct ed25519_keypair), 613 tee_cryp_obj_ed25519_pub_key_attrs), 614 615 PROP(TEE_TYPE_ED25519_KEYPAIR, 1, 256, 256, 616 sizeof(struct ed25519_keypair), 617 tee_cryp_obj_ed25519_keypair_attrs), 618 }; 619 620 struct attr_ops { 621 TEE_Result (*from_user)(void *attr, const void *buffer, size_t size); 622 TEE_Result (*to_user)(void *attr, struct ts_session *sess, 623 void *buffer, uint64_t *size); 624 TEE_Result (*to_binary)(void *attr, void *data, size_t data_len, 625 size_t *offs); 626 bool (*from_binary)(void *attr, const void *data, size_t data_len, 627 size_t *offs); 628 TEE_Result (*from_obj)(void *attr, void *src_attr); 629 void (*free)(void *attr); 630 void (*clear)(void *attr); 631 }; 632 633 static TEE_Result op_u32_to_binary_helper(uint32_t v, uint8_t *data, 634 size_t data_len, size_t *offs) 635 { 636 uint32_t field; 637 size_t next_offs; 638 639 if (ADD_OVERFLOW(*offs, sizeof(field), &next_offs)) 640 return TEE_ERROR_OVERFLOW; 641 642 if (data && next_offs <= data_len) { 643 field = TEE_U32_TO_BIG_ENDIAN(v); 644 memcpy(data + *offs, &field, sizeof(field)); 645 } 646 (*offs) = next_offs; 647 648 return TEE_SUCCESS; 649 } 650 651 static bool op_u32_from_binary_helper(uint32_t *v, const uint8_t *data, 652 size_t data_len, size_t *offs) 653 { 654 uint32_t field; 655 656 if (!data || (*offs + sizeof(field)) > data_len) 657 return false; 658 659 memcpy(&field, data + *offs, sizeof(field)); 660 *v = TEE_U32_FROM_BIG_ENDIAN(field); 661 (*offs) += sizeof(field); 662 return true; 663 } 664 665 static TEE_Result op_attr_secret_value_from_user(void *attr, const void *buffer, 666 size_t size) 667 { 668 struct tee_cryp_obj_secret *key = attr; 669 670 /* Data size has to fit in allocated buffer */ 671 if (size > key->alloc_size) 672 return TEE_ERROR_SECURITY; 673 memcpy(key + 1, buffer, size); 674 key->key_size = size; 675 return TEE_SUCCESS; 676 } 677 678 static TEE_Result op_attr_secret_value_to_user(void *attr, 679 struct ts_session *sess __unused, 680 void *buffer, uint64_t *size) 681 { 682 TEE_Result res; 683 struct tee_cryp_obj_secret *key = attr; 684 uint64_t s; 685 uint64_t key_size; 686 687 res = copy_from_user(&s, size, sizeof(s)); 688 if (res != TEE_SUCCESS) 689 return res; 690 691 key_size = key->key_size; 692 res = copy_to_user(size, &key_size, sizeof(key_size)); 693 if (res != TEE_SUCCESS) 694 return res; 695 696 if (s < key->key_size || !buffer) 697 return TEE_ERROR_SHORT_BUFFER; 698 699 return copy_to_user(buffer, key + 1, key->key_size); 700 } 701 702 static TEE_Result op_attr_secret_value_to_binary(void *attr, void *data, 703 size_t data_len, size_t *offs) 704 { 705 TEE_Result res; 706 struct tee_cryp_obj_secret *key = attr; 707 size_t next_offs; 708 709 res = op_u32_to_binary_helper(key->key_size, data, data_len, offs); 710 if (res != TEE_SUCCESS) 711 return res; 712 713 if (ADD_OVERFLOW(*offs, key->key_size, &next_offs)) 714 return TEE_ERROR_OVERFLOW; 715 716 if (data && next_offs <= data_len) 717 memcpy((uint8_t *)data + *offs, key + 1, key->key_size); 718 (*offs) = next_offs; 719 720 return TEE_SUCCESS; 721 } 722 723 static bool op_attr_secret_value_from_binary(void *attr, const void *data, 724 size_t data_len, size_t *offs) 725 { 726 struct tee_cryp_obj_secret *key = attr; 727 uint32_t s; 728 729 if (!op_u32_from_binary_helper(&s, data, data_len, offs)) 730 return false; 731 732 if ((*offs + s) > data_len) 733 return false; 734 735 /* Data size has to fit in allocated buffer */ 736 if (s > key->alloc_size) 737 return false; 738 key->key_size = s; 739 memcpy(key + 1, (const uint8_t *)data + *offs, s); 740 (*offs) += s; 741 return true; 742 } 743 744 745 static TEE_Result op_attr_secret_value_from_obj(void *attr, void *src_attr) 746 { 747 struct tee_cryp_obj_secret *key = attr; 748 struct tee_cryp_obj_secret *src_key = src_attr; 749 750 if (src_key->key_size > key->alloc_size) 751 return TEE_ERROR_BAD_STATE; 752 memcpy(key + 1, src_key + 1, src_key->key_size); 753 key->key_size = src_key->key_size; 754 return TEE_SUCCESS; 755 } 756 757 static void op_attr_secret_value_clear(void *attr) 758 { 759 struct tee_cryp_obj_secret *key = attr; 760 761 key->key_size = 0; 762 memzero_explicit(key + 1, key->alloc_size); 763 } 764 765 static TEE_Result op_attr_bignum_from_user(void *attr, const void *buffer, 766 size_t size) 767 { 768 struct bignum **bn = attr; 769 770 return crypto_bignum_bin2bn(buffer, size, *bn); 771 } 772 773 static TEE_Result op_attr_bignum_to_user(void *attr, 774 struct ts_session *sess, 775 void *buffer, uint64_t *size) 776 { 777 TEE_Result res = TEE_SUCCESS; 778 struct bignum **bn = attr; 779 uint64_t req_size = 0; 780 uint64_t s = 0; 781 782 res = copy_from_user(&s, size, sizeof(s)); 783 if (res != TEE_SUCCESS) 784 return res; 785 786 req_size = crypto_bignum_num_bytes(*bn); 787 res = copy_to_user(size, &req_size, sizeof(req_size)); 788 if (res != TEE_SUCCESS) 789 return res; 790 if (!req_size) 791 return TEE_SUCCESS; 792 if (s < req_size || !buffer) 793 return TEE_ERROR_SHORT_BUFFER; 794 795 buffer = memtag_strip_tag(buffer); 796 797 /* Check we can access data using supplied user mode pointer */ 798 res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 799 TEE_MEMORY_ACCESS_READ | 800 TEE_MEMORY_ACCESS_WRITE | 801 TEE_MEMORY_ACCESS_ANY_OWNER, 802 (uaddr_t)buffer, req_size); 803 if (res != TEE_SUCCESS) 804 return res; 805 /* 806 * Write the bignum (wich raw data points to) into an array of 807 * bytes (stored in buffer) 808 */ 809 crypto_bignum_bn2bin(*bn, buffer); 810 return TEE_SUCCESS; 811 } 812 813 static TEE_Result op_attr_bignum_to_binary(void *attr, void *data, 814 size_t data_len, size_t *offs) 815 { 816 TEE_Result res; 817 struct bignum **bn = attr; 818 uint32_t n = crypto_bignum_num_bytes(*bn); 819 size_t next_offs; 820 821 res = op_u32_to_binary_helper(n, data, data_len, offs); 822 if (res != TEE_SUCCESS) 823 return res; 824 825 if (ADD_OVERFLOW(*offs, n, &next_offs)) 826 return TEE_ERROR_OVERFLOW; 827 828 if (data && next_offs <= data_len) 829 crypto_bignum_bn2bin(*bn, (uint8_t *)data + *offs); 830 (*offs) = next_offs; 831 832 return TEE_SUCCESS; 833 } 834 835 static bool op_attr_bignum_from_binary(void *attr, const void *data, 836 size_t data_len, size_t *offs) 837 { 838 struct bignum **bn = attr; 839 uint32_t n; 840 841 if (!op_u32_from_binary_helper(&n, data, data_len, offs)) 842 return false; 843 844 if ((*offs + n) > data_len) 845 return false; 846 if (crypto_bignum_bin2bn((const uint8_t *)data + *offs, n, *bn)) 847 return false; 848 (*offs) += n; 849 return true; 850 } 851 852 static TEE_Result op_attr_bignum_from_obj(void *attr, void *src_attr) 853 { 854 struct bignum **bn = attr; 855 struct bignum **src_bn = src_attr; 856 857 crypto_bignum_copy(*bn, *src_bn); 858 return TEE_SUCCESS; 859 } 860 861 static void op_attr_bignum_clear(void *attr) 862 { 863 struct bignum **bn = attr; 864 865 crypto_bignum_clear(*bn); 866 } 867 868 static void op_attr_bignum_free(void *attr) 869 { 870 struct bignum **bn = attr; 871 872 crypto_bignum_free(*bn); 873 *bn = NULL; 874 } 875 876 static TEE_Result op_attr_value_from_user(void *attr, const void *buffer, 877 size_t size) 878 { 879 uint32_t *v = attr; 880 881 if (size != sizeof(uint32_t) * 2) 882 return TEE_ERROR_GENERIC; /* "can't happen */ 883 884 /* Note that only the first value is copied */ 885 memcpy(v, buffer, sizeof(uint32_t)); 886 return TEE_SUCCESS; 887 } 888 889 static TEE_Result op_attr_value_to_user(void *attr, 890 struct ts_session *sess __unused, 891 void *buffer, uint64_t *size) 892 { 893 TEE_Result res; 894 uint32_t *v = attr; 895 uint64_t s; 896 uint32_t value[2] = { *v }; 897 uint64_t req_size = sizeof(value); 898 899 res = copy_from_user(&s, size, sizeof(s)); 900 if (res != TEE_SUCCESS) 901 return res; 902 903 if (s < req_size || !buffer) 904 return TEE_ERROR_SHORT_BUFFER; 905 906 return copy_to_user(buffer, value, req_size); 907 } 908 909 static TEE_Result op_attr_value_to_binary(void *attr, void *data, 910 size_t data_len, size_t *offs) 911 { 912 uint32_t *v = attr; 913 914 return op_u32_to_binary_helper(*v, data, data_len, offs); 915 } 916 917 static bool op_attr_value_from_binary(void *attr, const void *data, 918 size_t data_len, size_t *offs) 919 { 920 uint32_t *v = attr; 921 922 return op_u32_from_binary_helper(v, data, data_len, offs); 923 } 924 925 static TEE_Result op_attr_value_from_obj(void *attr, void *src_attr) 926 { 927 uint32_t *v = attr; 928 uint32_t *src_v = src_attr; 929 930 *v = *src_v; 931 return TEE_SUCCESS; 932 } 933 934 static void op_attr_value_clear(void *attr) 935 { 936 uint32_t *v = attr; 937 938 *v = 0; 939 } 940 941 static TEE_Result op_attr_25519_from_user(void *attr, const void *buffer, 942 size_t size) 943 { 944 uint8_t **key = attr; 945 946 if (size != KEY_SIZE_BYTES_25519 || !*key) 947 return TEE_ERROR_SECURITY; 948 949 memcpy(*key, buffer, size); 950 951 return TEE_SUCCESS; 952 } 953 954 static TEE_Result op_attr_25519_to_user(void *attr, 955 struct ts_session *sess __unused, 956 void *buffer, uint64_t *size) 957 { 958 TEE_Result res = TEE_ERROR_GENERIC; 959 uint8_t **key = attr; 960 uint64_t s = 0; 961 uint64_t key_size = (uint64_t)KEY_SIZE_BYTES_25519; 962 963 res = copy_from_user(&s, size, sizeof(s)); 964 if (res != TEE_SUCCESS) 965 return res; 966 967 res = copy_to_user(size, &key_size, sizeof(key_size)); 968 if (res != TEE_SUCCESS) 969 return res; 970 971 if (s < key_size || !buffer) 972 return TEE_ERROR_SHORT_BUFFER; 973 974 return copy_to_user(buffer, *key, key_size); 975 } 976 977 static TEE_Result op_attr_25519_to_binary(void *attr, void *data, 978 size_t data_len, size_t *offs) 979 { 980 TEE_Result res = TEE_ERROR_GENERIC; 981 uint8_t **key = attr; 982 size_t next_offs = 0; 983 uint64_t key_size = (uint64_t)KEY_SIZE_BYTES_25519; 984 985 res = op_u32_to_binary_helper(key_size, data, data_len, offs); 986 if (res != TEE_SUCCESS) 987 return res; 988 989 if (ADD_OVERFLOW(*offs, key_size, &next_offs)) 990 return TEE_ERROR_OVERFLOW; 991 992 if (data && next_offs <= data_len) 993 memcpy((uint8_t *)data + *offs, *key, key_size); 994 *offs = next_offs; 995 996 return TEE_SUCCESS; 997 } 998 999 static bool op_attr_25519_from_binary(void *attr, const void *data, 1000 size_t data_len, size_t *offs) 1001 { 1002 uint8_t **key = attr; 1003 uint32_t s = 0; 1004 1005 if (!op_u32_from_binary_helper(&s, data, data_len, offs)) 1006 return false; 1007 1008 if (*offs + s > data_len) 1009 return false; 1010 1011 if (s > (uint32_t)KEY_SIZE_BYTES_25519) 1012 return false; 1013 1014 memcpy(*key, (const uint8_t *)data + *offs, s); 1015 *offs += s; 1016 return true; 1017 } 1018 1019 static TEE_Result op_attr_25519_from_obj(void *attr, void *src_attr) 1020 { 1021 uint8_t **key = attr; 1022 uint8_t **src_key = src_attr; 1023 1024 if (!*key || !*src_key) 1025 return TEE_ERROR_SECURITY; 1026 1027 memcpy(*key, *src_key, KEY_SIZE_BYTES_25519); 1028 1029 return TEE_SUCCESS; 1030 } 1031 1032 static void op_attr_25519_clear(void *attr) 1033 { 1034 uint8_t **key = attr; 1035 1036 assert(*key); 1037 1038 memzero_explicit(*key, KEY_SIZE_BYTES_25519); 1039 } 1040 1041 static void op_attr_25519_free(void *attr) 1042 { 1043 uint8_t **key = attr; 1044 1045 op_attr_25519_clear(attr); 1046 free(*key); 1047 } 1048 1049 static const struct attr_ops attr_ops[] = { 1050 [ATTR_OPS_INDEX_SECRET] = { 1051 .from_user = op_attr_secret_value_from_user, 1052 .to_user = op_attr_secret_value_to_user, 1053 .to_binary = op_attr_secret_value_to_binary, 1054 .from_binary = op_attr_secret_value_from_binary, 1055 .from_obj = op_attr_secret_value_from_obj, 1056 .free = op_attr_secret_value_clear, /* not a typo */ 1057 .clear = op_attr_secret_value_clear, 1058 }, 1059 [ATTR_OPS_INDEX_BIGNUM] = { 1060 .from_user = op_attr_bignum_from_user, 1061 .to_user = op_attr_bignum_to_user, 1062 .to_binary = op_attr_bignum_to_binary, 1063 .from_binary = op_attr_bignum_from_binary, 1064 .from_obj = op_attr_bignum_from_obj, 1065 .free = op_attr_bignum_free, 1066 .clear = op_attr_bignum_clear, 1067 }, 1068 [ATTR_OPS_INDEX_VALUE] = { 1069 .from_user = op_attr_value_from_user, 1070 .to_user = op_attr_value_to_user, 1071 .to_binary = op_attr_value_to_binary, 1072 .from_binary = op_attr_value_from_binary, 1073 .from_obj = op_attr_value_from_obj, 1074 .free = op_attr_value_clear, /* not a typo */ 1075 .clear = op_attr_value_clear, 1076 }, 1077 [ATTR_OPS_INDEX_25519] = { 1078 .from_user = op_attr_25519_from_user, 1079 .to_user = op_attr_25519_to_user, 1080 .to_binary = op_attr_25519_to_binary, 1081 .from_binary = op_attr_25519_from_binary, 1082 .from_obj = op_attr_25519_from_obj, 1083 .free = op_attr_25519_free, 1084 .clear = op_attr_25519_clear, 1085 }, 1086 }; 1087 1088 static TEE_Result get_user_u64_as_size_t(size_t *dst, uint64_t *src) 1089 { 1090 uint64_t d = 0; 1091 TEE_Result res = copy_from_user(&d, src, sizeof(d)); 1092 1093 /* 1094 * On 32-bit systems a size_t can't hold a uint64_t so we need to 1095 * check that the value isn't too large. 1096 */ 1097 if (!res && ADD_OVERFLOW(0, d, dst)) 1098 return TEE_ERROR_OVERFLOW; 1099 1100 return res; 1101 } 1102 1103 static TEE_Result put_user_u64(uint64_t *dst, size_t value) 1104 { 1105 uint64_t v = value; 1106 1107 return copy_to_user(dst, &v, sizeof(v)); 1108 } 1109 1110 TEE_Result syscall_cryp_obj_get_info(unsigned long obj, TEE_ObjectInfo *info) 1111 { 1112 struct ts_session *sess = ts_get_current_session(); 1113 TEE_Result res = TEE_SUCCESS; 1114 struct tee_obj *o = NULL; 1115 1116 res = tee_obj_get(to_user_ta_ctx(sess->ctx), 1117 uref_to_vaddr(obj), &o); 1118 if (res != TEE_SUCCESS) 1119 goto exit; 1120 1121 res = copy_to_user_private(info, &o->info, sizeof(o->info)); 1122 1123 exit: 1124 return res; 1125 } 1126 1127 TEE_Result syscall_cryp_obj_restrict_usage(unsigned long obj, 1128 unsigned long usage) 1129 { 1130 struct ts_session *sess = ts_get_current_session(); 1131 TEE_Result res = TEE_SUCCESS; 1132 struct tee_obj *o = NULL; 1133 1134 res = tee_obj_get(to_user_ta_ctx(sess->ctx), uref_to_vaddr(obj), &o); 1135 if (res != TEE_SUCCESS) 1136 goto exit; 1137 1138 o->info.objectUsage &= usage; 1139 1140 exit: 1141 return res; 1142 } 1143 1144 static int tee_svc_cryp_obj_find_type_attr_idx( 1145 uint32_t attr_id, 1146 const struct tee_cryp_obj_type_props *type_props) 1147 { 1148 size_t n; 1149 1150 for (n = 0; n < type_props->num_type_attrs; n++) { 1151 if (attr_id == type_props->type_attrs[n].attr_id) 1152 return n; 1153 } 1154 return -1; 1155 } 1156 1157 static const struct tee_cryp_obj_type_props *tee_svc_find_type_props( 1158 TEE_ObjectType obj_type) 1159 { 1160 size_t n; 1161 1162 for (n = 0; n < ARRAY_SIZE(tee_cryp_obj_props); n++) { 1163 if (tee_cryp_obj_props[n].obj_type == obj_type) 1164 return tee_cryp_obj_props + n; 1165 } 1166 1167 return NULL; 1168 } 1169 1170 /* Set an attribute on an object */ 1171 static void set_attribute(struct tee_obj *o, 1172 const struct tee_cryp_obj_type_props *props, 1173 uint32_t attr) 1174 { 1175 int idx = tee_svc_cryp_obj_find_type_attr_idx(attr, props); 1176 1177 if (idx < 0) 1178 return; 1179 o->have_attrs |= BIT(idx); 1180 } 1181 1182 /* Get an attribute on an object */ 1183 static uint32_t get_attribute(const struct tee_obj *o, 1184 const struct tee_cryp_obj_type_props *props, 1185 uint32_t attr) 1186 { 1187 int idx = tee_svc_cryp_obj_find_type_attr_idx(attr, props); 1188 1189 if (idx < 0) 1190 return 0; 1191 return o->have_attrs & BIT(idx); 1192 } 1193 1194 TEE_Result syscall_cryp_obj_get_attr(unsigned long obj, unsigned long attr_id, 1195 void *buffer, uint64_t *size) 1196 { 1197 struct ts_session *sess = ts_get_current_session(); 1198 TEE_Result res = TEE_SUCCESS; 1199 struct tee_obj *o = NULL; 1200 const struct tee_cryp_obj_type_props *type_props = NULL; 1201 int idx = 0; 1202 const struct attr_ops *ops = NULL; 1203 void *attr = NULL; 1204 1205 res = tee_obj_get(to_user_ta_ctx(sess->ctx), uref_to_vaddr(obj), &o); 1206 if (res != TEE_SUCCESS) 1207 return TEE_ERROR_ITEM_NOT_FOUND; 1208 1209 /* Check that the object is initialized */ 1210 if (!(o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED)) 1211 return TEE_ERROR_BAD_PARAMETERS; 1212 1213 /* Check that getting the attribute is allowed */ 1214 if (!(attr_id & TEE_ATTR_FLAG_PUBLIC) && 1215 !(o->info.objectUsage & TEE_USAGE_EXTRACTABLE)) 1216 return TEE_ERROR_BAD_PARAMETERS; 1217 1218 type_props = tee_svc_find_type_props(o->info.objectType); 1219 if (!type_props) { 1220 /* Unknown object type, "can't happen" */ 1221 return TEE_ERROR_BAD_STATE; 1222 } 1223 1224 idx = tee_svc_cryp_obj_find_type_attr_idx(attr_id, type_props); 1225 if ((idx < 0) || ((o->have_attrs & (1 << idx)) == 0)) 1226 return TEE_ERROR_ITEM_NOT_FOUND; 1227 1228 ops = attr_ops + type_props->type_attrs[idx].ops_index; 1229 attr = (uint8_t *)o->attr + type_props->type_attrs[idx].raw_offs; 1230 return ops->to_user(attr, sess, buffer, size); 1231 } 1232 1233 void tee_obj_attr_free(struct tee_obj *o) 1234 { 1235 const struct tee_cryp_obj_type_props *tp; 1236 size_t n; 1237 1238 if (!o->attr) 1239 return; 1240 tp = tee_svc_find_type_props(o->info.objectType); 1241 if (!tp) 1242 return; 1243 1244 for (n = 0; n < tp->num_type_attrs; n++) { 1245 const struct tee_cryp_obj_type_attrs *ta = tp->type_attrs + n; 1246 1247 attr_ops[ta->ops_index].free((uint8_t *)o->attr + ta->raw_offs); 1248 } 1249 } 1250 1251 void tee_obj_attr_clear(struct tee_obj *o) 1252 { 1253 const struct tee_cryp_obj_type_props *tp; 1254 size_t n; 1255 1256 if (!o->attr) 1257 return; 1258 tp = tee_svc_find_type_props(o->info.objectType); 1259 if (!tp) 1260 return; 1261 1262 for (n = 0; n < tp->num_type_attrs; n++) { 1263 const struct tee_cryp_obj_type_attrs *ta = tp->type_attrs + n; 1264 1265 attr_ops[ta->ops_index].clear((uint8_t *)o->attr + 1266 ta->raw_offs); 1267 } 1268 } 1269 1270 TEE_Result tee_obj_attr_to_binary(struct tee_obj *o, void *data, 1271 size_t *data_len) 1272 { 1273 const struct tee_cryp_obj_type_props *tp; 1274 size_t n; 1275 size_t offs = 0; 1276 size_t len = data ? *data_len : 0; 1277 TEE_Result res; 1278 1279 if (o->info.objectType == TEE_TYPE_DATA) { 1280 *data_len = 0; 1281 return TEE_SUCCESS; /* pure data object */ 1282 } 1283 if (!o->attr) 1284 return TEE_ERROR_BAD_STATE; 1285 tp = tee_svc_find_type_props(o->info.objectType); 1286 if (!tp) 1287 return TEE_ERROR_BAD_STATE; 1288 1289 for (n = 0; n < tp->num_type_attrs; n++) { 1290 const struct tee_cryp_obj_type_attrs *ta = tp->type_attrs + n; 1291 void *attr = (uint8_t *)o->attr + ta->raw_offs; 1292 1293 res = attr_ops[ta->ops_index].to_binary(attr, data, len, &offs); 1294 if (res != TEE_SUCCESS) 1295 return res; 1296 } 1297 1298 *data_len = offs; 1299 if (data && offs > len) 1300 return TEE_ERROR_SHORT_BUFFER; 1301 return TEE_SUCCESS; 1302 } 1303 1304 TEE_Result tee_obj_attr_from_binary(struct tee_obj *o, const void *data, 1305 size_t data_len) 1306 { 1307 const struct tee_cryp_obj_type_props *tp; 1308 size_t n; 1309 size_t offs = 0; 1310 1311 if (o->info.objectType == TEE_TYPE_DATA) 1312 return TEE_SUCCESS; /* pure data object */ 1313 if (!o->attr) 1314 return TEE_ERROR_BAD_STATE; 1315 tp = tee_svc_find_type_props(o->info.objectType); 1316 if (!tp) 1317 return TEE_ERROR_BAD_STATE; 1318 1319 for (n = 0; n < tp->num_type_attrs; n++) { 1320 const struct tee_cryp_obj_type_attrs *ta = tp->type_attrs + n; 1321 void *attr = (uint8_t *)o->attr + ta->raw_offs; 1322 1323 if (!attr_ops[ta->ops_index].from_binary(attr, data, data_len, 1324 &offs)) 1325 return TEE_ERROR_CORRUPT_OBJECT; 1326 } 1327 return TEE_SUCCESS; 1328 } 1329 1330 TEE_Result tee_obj_attr_copy_from(struct tee_obj *o, const struct tee_obj *src) 1331 { 1332 TEE_Result res; 1333 const struct tee_cryp_obj_type_props *tp; 1334 const struct tee_cryp_obj_type_attrs *ta; 1335 size_t n; 1336 uint32_t have_attrs = 0; 1337 void *attr; 1338 void *src_attr; 1339 1340 if (o->info.objectType == TEE_TYPE_DATA) 1341 return TEE_SUCCESS; /* pure data object */ 1342 if (!o->attr) 1343 return TEE_ERROR_BAD_STATE; 1344 tp = tee_svc_find_type_props(o->info.objectType); 1345 if (!tp) 1346 return TEE_ERROR_BAD_STATE; 1347 1348 if (o->info.objectType == src->info.objectType) { 1349 have_attrs = src->have_attrs; 1350 for (n = 0; n < tp->num_type_attrs; n++) { 1351 ta = tp->type_attrs + n; 1352 attr = (uint8_t *)o->attr + ta->raw_offs; 1353 src_attr = (uint8_t *)src->attr + ta->raw_offs; 1354 res = attr_ops[ta->ops_index].from_obj(attr, src_attr); 1355 if (res != TEE_SUCCESS) 1356 return res; 1357 } 1358 } else { 1359 const struct tee_cryp_obj_type_props *tp_src; 1360 int idx; 1361 1362 if (o->info.objectType == TEE_TYPE_RSA_PUBLIC_KEY) { 1363 if (src->info.objectType != TEE_TYPE_RSA_KEYPAIR) 1364 return TEE_ERROR_BAD_PARAMETERS; 1365 } else if (o->info.objectType == TEE_TYPE_DSA_PUBLIC_KEY) { 1366 if (src->info.objectType != TEE_TYPE_DSA_KEYPAIR) 1367 return TEE_ERROR_BAD_PARAMETERS; 1368 } else if (o->info.objectType == TEE_TYPE_ECDSA_PUBLIC_KEY) { 1369 if (src->info.objectType != TEE_TYPE_ECDSA_KEYPAIR) 1370 return TEE_ERROR_BAD_PARAMETERS; 1371 } else if (o->info.objectType == TEE_TYPE_ECDH_PUBLIC_KEY) { 1372 if (src->info.objectType != TEE_TYPE_ECDH_KEYPAIR) 1373 return TEE_ERROR_BAD_PARAMETERS; 1374 } else if (o->info.objectType == TEE_TYPE_SM2_DSA_PUBLIC_KEY) { 1375 if (src->info.objectType != TEE_TYPE_SM2_DSA_KEYPAIR) 1376 return TEE_ERROR_BAD_PARAMETERS; 1377 } else if (o->info.objectType == TEE_TYPE_SM2_PKE_PUBLIC_KEY) { 1378 if (src->info.objectType != TEE_TYPE_SM2_PKE_KEYPAIR) 1379 return TEE_ERROR_BAD_PARAMETERS; 1380 } else if (o->info.objectType == TEE_TYPE_SM2_KEP_PUBLIC_KEY) { 1381 if (src->info.objectType != TEE_TYPE_SM2_KEP_KEYPAIR) 1382 return TEE_ERROR_BAD_PARAMETERS; 1383 } else if (o->info.objectType == TEE_TYPE_ED25519_PUBLIC_KEY) { 1384 if (src->info.objectType != TEE_TYPE_ED25519_KEYPAIR) 1385 return TEE_ERROR_BAD_PARAMETERS; 1386 } else { 1387 return TEE_ERROR_BAD_PARAMETERS; 1388 } 1389 1390 tp_src = tee_svc_find_type_props(src->info.objectType); 1391 if (!tp_src) 1392 return TEE_ERROR_BAD_STATE; 1393 1394 have_attrs = BIT32(tp->num_type_attrs) - 1; 1395 for (n = 0; n < tp->num_type_attrs; n++) { 1396 ta = tp->type_attrs + n; 1397 1398 idx = tee_svc_cryp_obj_find_type_attr_idx(ta->attr_id, 1399 tp_src); 1400 if (idx < 0) 1401 return TEE_ERROR_BAD_STATE; 1402 1403 attr = (uint8_t *)o->attr + ta->raw_offs; 1404 src_attr = (uint8_t *)src->attr + 1405 tp_src->type_attrs[idx].raw_offs; 1406 res = attr_ops[ta->ops_index].from_obj(attr, src_attr); 1407 if (res != TEE_SUCCESS) 1408 return res; 1409 } 1410 } 1411 1412 o->have_attrs = have_attrs; 1413 return TEE_SUCCESS; 1414 } 1415 1416 static bool is_gp_legacy_des_key_size(TEE_ObjectType type, size_t sz) 1417 { 1418 return IS_ENABLED(CFG_COMPAT_GP10_DES) && 1419 ((type == TEE_TYPE_DES && sz == 56) || 1420 (type == TEE_TYPE_DES3 && (sz == 112 || sz == 168))); 1421 } 1422 1423 static TEE_Result check_key_size(const struct tee_cryp_obj_type_props *props, 1424 size_t key_size) 1425 { 1426 size_t sz = key_size; 1427 1428 /* 1429 * In GP Internal API Specification 1.0 the partity bits aren't 1430 * counted when telling the size of the key in bits so add them 1431 * here if missing. 1432 */ 1433 if (is_gp_legacy_des_key_size(props->obj_type, sz)) 1434 sz += sz / 7; 1435 1436 if (sz % props->quanta != 0) 1437 return TEE_ERROR_NOT_SUPPORTED; 1438 if (sz < props->min_size) 1439 return TEE_ERROR_NOT_SUPPORTED; 1440 if (sz > props->max_size) 1441 return TEE_ERROR_NOT_SUPPORTED; 1442 1443 return TEE_SUCCESS; 1444 } 1445 1446 TEE_Result tee_obj_set_type(struct tee_obj *o, uint32_t obj_type, 1447 size_t max_key_size) 1448 { 1449 TEE_Result res = TEE_SUCCESS; 1450 const struct tee_cryp_obj_type_props *type_props; 1451 1452 /* Can only set type for newly allocated objs */ 1453 if (o->attr) 1454 return TEE_ERROR_BAD_STATE; 1455 1456 /* 1457 * Verify that maxObjectSize is supported and find out how 1458 * much should be allocated. 1459 */ 1460 1461 if (obj_type == TEE_TYPE_DATA) { 1462 if (max_key_size) 1463 return TEE_ERROR_NOT_SUPPORTED; 1464 } else { 1465 /* Find description of object */ 1466 type_props = tee_svc_find_type_props(obj_type); 1467 if (!type_props) 1468 return TEE_ERROR_NOT_SUPPORTED; 1469 1470 /* Check that max_key_size follows restrictions */ 1471 res = check_key_size(type_props, max_key_size); 1472 if (res) 1473 return res; 1474 1475 o->attr = calloc(1, type_props->alloc_size); 1476 if (!o->attr) 1477 return TEE_ERROR_OUT_OF_MEMORY; 1478 } 1479 1480 /* If we have a key structure, pre-allocate the bignums inside */ 1481 switch (obj_type) { 1482 case TEE_TYPE_RSA_PUBLIC_KEY: 1483 res = crypto_acipher_alloc_rsa_public_key(o->attr, 1484 max_key_size); 1485 break; 1486 case TEE_TYPE_RSA_KEYPAIR: 1487 res = crypto_acipher_alloc_rsa_keypair(o->attr, max_key_size); 1488 break; 1489 case TEE_TYPE_DSA_PUBLIC_KEY: 1490 res = crypto_acipher_alloc_dsa_public_key(o->attr, 1491 max_key_size); 1492 break; 1493 case TEE_TYPE_DSA_KEYPAIR: 1494 res = crypto_acipher_alloc_dsa_keypair(o->attr, max_key_size); 1495 break; 1496 case TEE_TYPE_DH_KEYPAIR: 1497 res = crypto_acipher_alloc_dh_keypair(o->attr, max_key_size); 1498 break; 1499 case TEE_TYPE_ECDSA_PUBLIC_KEY: 1500 case TEE_TYPE_ECDH_PUBLIC_KEY: 1501 case TEE_TYPE_SM2_DSA_PUBLIC_KEY: 1502 case TEE_TYPE_SM2_PKE_PUBLIC_KEY: 1503 case TEE_TYPE_SM2_KEP_PUBLIC_KEY: 1504 res = crypto_acipher_alloc_ecc_public_key(o->attr, obj_type, 1505 max_key_size); 1506 break; 1507 case TEE_TYPE_ECDSA_KEYPAIR: 1508 case TEE_TYPE_ECDH_KEYPAIR: 1509 case TEE_TYPE_SM2_DSA_KEYPAIR: 1510 case TEE_TYPE_SM2_PKE_KEYPAIR: 1511 case TEE_TYPE_SM2_KEP_KEYPAIR: 1512 res = crypto_acipher_alloc_ecc_keypair(o->attr, obj_type, 1513 max_key_size); 1514 break; 1515 case TEE_TYPE_X25519_KEYPAIR: 1516 res = crypto_acipher_alloc_x25519_keypair(o->attr, 1517 max_key_size); 1518 break; 1519 case TEE_TYPE_ED25519_KEYPAIR: 1520 case TEE_TYPE_ED25519_PUBLIC_KEY: 1521 res = crypto_acipher_alloc_ed25519_keypair(o->attr, 1522 max_key_size); 1523 break; 1524 default: 1525 if (obj_type != TEE_TYPE_DATA) { 1526 struct tee_cryp_obj_secret *key = o->attr; 1527 1528 key->alloc_size = type_props->alloc_size - 1529 sizeof(*key); 1530 } 1531 break; 1532 } 1533 1534 if (res != TEE_SUCCESS) 1535 return res; 1536 1537 o->info.objectType = obj_type; 1538 o->info.maxObjectSize = max_key_size; 1539 o->info.objectUsage = TEE_USAGE_DEFAULT; 1540 1541 return TEE_SUCCESS; 1542 } 1543 1544 TEE_Result syscall_cryp_obj_alloc(unsigned long obj_type, 1545 unsigned long max_key_size, uint32_t *obj) 1546 { 1547 struct ts_session *sess = ts_get_current_session(); 1548 TEE_Result res = TEE_SUCCESS; 1549 struct tee_obj *o = NULL; 1550 1551 1552 o = tee_obj_alloc(); 1553 if (!o) 1554 return TEE_ERROR_OUT_OF_MEMORY; 1555 1556 res = tee_obj_set_type(o, obj_type, max_key_size); 1557 if (res != TEE_SUCCESS) { 1558 tee_obj_free(o); 1559 return res; 1560 } 1561 1562 tee_obj_add(to_user_ta_ctx(sess->ctx), o); 1563 1564 res = copy_kaddr_to_uref(obj, o); 1565 if (res != TEE_SUCCESS) 1566 tee_obj_close(to_user_ta_ctx(sess->ctx), o); 1567 return res; 1568 } 1569 1570 TEE_Result syscall_cryp_obj_close(unsigned long obj) 1571 { 1572 struct ts_session *sess = ts_get_current_session(); 1573 TEE_Result res = TEE_SUCCESS; 1574 struct tee_obj *o = NULL; 1575 1576 res = tee_obj_get(to_user_ta_ctx(sess->ctx), uref_to_vaddr(obj), &o); 1577 if (res != TEE_SUCCESS) 1578 return res; 1579 1580 /* 1581 * If it's busy it's used by an operation, a client should never have 1582 * this handle. 1583 */ 1584 if (o->busy) 1585 return TEE_ERROR_ITEM_NOT_FOUND; 1586 1587 tee_obj_close(to_user_ta_ctx(sess->ctx), o); 1588 return TEE_SUCCESS; 1589 } 1590 1591 TEE_Result syscall_cryp_obj_reset(unsigned long obj) 1592 { 1593 struct ts_session *sess = ts_get_current_session(); 1594 TEE_Result res = TEE_SUCCESS; 1595 struct tee_obj *o = NULL; 1596 1597 res = tee_obj_get(to_user_ta_ctx(sess->ctx), uref_to_vaddr(obj), &o); 1598 if (res != TEE_SUCCESS) 1599 return res; 1600 1601 if ((o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) == 0) { 1602 tee_obj_attr_clear(o); 1603 o->info.objectSize = 0; 1604 o->info.objectUsage = TEE_USAGE_DEFAULT; 1605 } else { 1606 return TEE_ERROR_BAD_PARAMETERS; 1607 } 1608 1609 /* the object is no more initialized */ 1610 o->info.handleFlags &= ~TEE_HANDLE_FLAG_INITIALIZED; 1611 1612 return TEE_SUCCESS; 1613 } 1614 1615 static TEE_Result copy_in_attrs(struct user_ta_ctx *utc, 1616 const struct utee_attribute *usr_attrs, 1617 uint32_t attr_count, TEE_Attribute *attrs) 1618 { 1619 TEE_Result res = TEE_SUCCESS; 1620 size_t size = 0; 1621 uint32_t n = 0; 1622 1623 if (MUL_OVERFLOW(sizeof(struct utee_attribute), attr_count, &size)) 1624 return TEE_ERROR_OVERFLOW; 1625 1626 usr_attrs = memtag_strip_tag_const(usr_attrs); 1627 1628 res = vm_check_access_rights(&utc->uctx, 1629 TEE_MEMORY_ACCESS_READ | 1630 TEE_MEMORY_ACCESS_ANY_OWNER, 1631 (uaddr_t)usr_attrs, size); 1632 if (res != TEE_SUCCESS) 1633 return res; 1634 1635 for (n = 0; n < attr_count; n++) { 1636 attrs[n].attributeID = usr_attrs[n].attribute_id; 1637 if (attrs[n].attributeID & TEE_ATTR_FLAG_VALUE) { 1638 attrs[n].content.value.a = usr_attrs[n].a; 1639 attrs[n].content.value.b = usr_attrs[n].b; 1640 } else { 1641 uintptr_t buf = usr_attrs[n].a; 1642 size_t len = usr_attrs[n].b; 1643 uint32_t flags = TEE_MEMORY_ACCESS_READ | 1644 TEE_MEMORY_ACCESS_ANY_OWNER; 1645 1646 buf = memtag_strip_tag_vaddr((void *)buf); 1647 1648 res = vm_check_access_rights(&utc->uctx, flags, buf, 1649 len); 1650 if (res != TEE_SUCCESS) 1651 return res; 1652 attrs[n].content.ref.buffer = (void *)buf; 1653 attrs[n].content.ref.length = len; 1654 } 1655 } 1656 1657 return TEE_SUCCESS; 1658 } 1659 1660 enum attr_usage { 1661 ATTR_USAGE_POPULATE, 1662 ATTR_USAGE_GENERATE_KEY 1663 }; 1664 1665 static TEE_Result tee_svc_cryp_check_attr(enum attr_usage usage, 1666 const struct tee_cryp_obj_type_props 1667 *type_props, 1668 const TEE_Attribute *attrs, 1669 uint32_t attr_count) 1670 { 1671 uint32_t required_flag = 0; 1672 uint32_t opt_flag = 0; 1673 bool all_opt_needed = false; 1674 uint32_t req_attrs = 0; 1675 uint32_t opt_grp_attrs = 0; 1676 uint32_t attrs_found = 0; 1677 size_t n = 0; 1678 uint32_t bit = 0; 1679 uint32_t flags = 0; 1680 int idx = 0; 1681 1682 if (usage == ATTR_USAGE_POPULATE) { 1683 required_flag = TEE_TYPE_ATTR_REQUIRED; 1684 opt_flag = TEE_TYPE_ATTR_OPTIONAL_GROUP; 1685 all_opt_needed = true; 1686 } else { 1687 required_flag = TEE_TYPE_ATTR_GEN_KEY_REQ; 1688 opt_flag = TEE_TYPE_ATTR_GEN_KEY_OPT; 1689 all_opt_needed = false; 1690 } 1691 1692 /* 1693 * First find out which attributes are required and which belong to 1694 * the optional group 1695 */ 1696 for (n = 0; n < type_props->num_type_attrs; n++) { 1697 bit = 1 << n; 1698 flags = type_props->type_attrs[n].flags; 1699 1700 if (flags & required_flag) 1701 req_attrs |= bit; 1702 else if (flags & opt_flag) 1703 opt_grp_attrs |= bit; 1704 } 1705 1706 /* 1707 * Verify that all required attributes are in place and 1708 * that the same attribute isn't repeated. 1709 */ 1710 for (n = 0; n < attr_count; n++) { 1711 idx = tee_svc_cryp_obj_find_type_attr_idx( 1712 attrs[n].attributeID, 1713 type_props); 1714 1715 /* attribute not defined in current object type */ 1716 if (idx < 0) 1717 return TEE_ERROR_ITEM_NOT_FOUND; 1718 1719 bit = 1 << idx; 1720 1721 /* attribute not repeated */ 1722 if ((attrs_found & bit) != 0) 1723 return TEE_ERROR_ITEM_NOT_FOUND; 1724 1725 /* 1726 * Attribute not defined in current object type for this 1727 * usage. 1728 */ 1729 if (!(bit & (req_attrs | opt_grp_attrs))) 1730 return TEE_ERROR_ITEM_NOT_FOUND; 1731 1732 attrs_found |= bit; 1733 } 1734 /* Required attribute missing */ 1735 if ((attrs_found & req_attrs) != req_attrs) 1736 return TEE_ERROR_ITEM_NOT_FOUND; 1737 1738 /* 1739 * If the flag says that "if one of the optional attributes are included 1740 * all of them has to be included" this must be checked. 1741 */ 1742 if (all_opt_needed && (attrs_found & opt_grp_attrs) != 0 && 1743 (attrs_found & opt_grp_attrs) != opt_grp_attrs) 1744 return TEE_ERROR_ITEM_NOT_FOUND; 1745 1746 return TEE_SUCCESS; 1747 } 1748 1749 static TEE_Result get_ec_key_size(uint32_t curve, size_t *key_size) 1750 { 1751 switch (curve) { 1752 case TEE_ECC_CURVE_NIST_P192: 1753 *key_size = 192; 1754 break; 1755 case TEE_ECC_CURVE_NIST_P224: 1756 *key_size = 224; 1757 break; 1758 case TEE_ECC_CURVE_NIST_P256: 1759 *key_size = 256; 1760 break; 1761 case TEE_ECC_CURVE_NIST_P384: 1762 *key_size = 384; 1763 break; 1764 case TEE_ECC_CURVE_NIST_P521: 1765 *key_size = 521; 1766 break; 1767 case TEE_ECC_CURVE_SM2: 1768 case TEE_ECC_CURVE_25519: 1769 *key_size = 256; 1770 break; 1771 default: 1772 return TEE_ERROR_NOT_SUPPORTED; 1773 } 1774 1775 return TEE_SUCCESS; 1776 } 1777 1778 static size_t get_used_bits(const TEE_Attribute *a) 1779 { 1780 int nbits = a->content.ref.length * 8; 1781 int v = 0; 1782 1783 bit_ffs(a->content.ref.buffer, nbits, &v); 1784 return nbits - v; 1785 } 1786 1787 static TEE_Result tee_svc_cryp_obj_populate_type( 1788 struct tee_obj *o, 1789 const struct tee_cryp_obj_type_props *type_props, 1790 const TEE_Attribute *attrs, 1791 uint32_t attr_count) 1792 { 1793 TEE_Result res = TEE_SUCCESS; 1794 uint32_t have_attrs = 0; 1795 size_t obj_size = 0; 1796 size_t n = 0; 1797 int idx = 0; 1798 const struct attr_ops *ops = NULL; 1799 void *attr = NULL; 1800 1801 for (n = 0; n < attr_count; n++) { 1802 idx = tee_svc_cryp_obj_find_type_attr_idx( 1803 attrs[n].attributeID, 1804 type_props); 1805 /* attribute not defined in current object type */ 1806 if (idx < 0) 1807 return TEE_ERROR_ITEM_NOT_FOUND; 1808 1809 have_attrs |= BIT32(idx); 1810 ops = attr_ops + type_props->type_attrs[idx].ops_index; 1811 attr = (uint8_t *)o->attr + 1812 type_props->type_attrs[idx].raw_offs; 1813 if (attrs[n].attributeID & TEE_ATTR_FLAG_VALUE) 1814 res = ops->from_user(attr, &attrs[n].content.value, 1815 sizeof(attrs[n].content.value)); 1816 else 1817 res = ops->from_user(attr, attrs[n].content.ref.buffer, 1818 attrs[n].content.ref.length); 1819 if (res != TEE_SUCCESS) 1820 return res; 1821 1822 /* 1823 * The attribute that gives the size of the object is 1824 * flagged with TEE_TYPE_ATTR_SIZE_INDICATOR. 1825 */ 1826 if (type_props->type_attrs[idx].flags & 1827 TEE_TYPE_ATTR_SIZE_INDICATOR) { 1828 /* There should be only one */ 1829 if (obj_size) 1830 return TEE_ERROR_BAD_STATE; 1831 1832 /* 1833 * For ECDSA/ECDH we need to translate curve into 1834 * object size 1835 */ 1836 if (attrs[n].attributeID == TEE_ATTR_ECC_CURVE) { 1837 res = get_ec_key_size(attrs[n].content.value.a, 1838 &obj_size); 1839 if (res != TEE_SUCCESS) 1840 return res; 1841 } else { 1842 TEE_ObjectType obj_type = o->info.objectType; 1843 size_t sz = o->info.maxObjectSize; 1844 1845 obj_size = attrs[n].content.ref.length * 8; 1846 /* Drop the parity bits for legacy objects */ 1847 if (is_gp_legacy_des_key_size(obj_type, sz)) 1848 obj_size -= obj_size / 8; 1849 } 1850 if (obj_size > o->info.maxObjectSize) 1851 return TEE_ERROR_BAD_STATE; 1852 res = check_key_size(type_props, obj_size); 1853 if (res != TEE_SUCCESS) 1854 return TEE_ERROR_BAD_PARAMETERS; 1855 } 1856 1857 /* 1858 * Bignum attributes limited by the number of bits in 1859 * o->info.objectSize are flagged with 1860 * TEE_TYPE_ATTR_BIGNUM_MAXBITS. 1861 */ 1862 if (type_props->type_attrs[idx].flags & 1863 TEE_TYPE_ATTR_BIGNUM_MAXBITS) { 1864 if (get_used_bits(attrs + n) > o->info.maxObjectSize) 1865 return TEE_ERROR_BAD_STATE; 1866 } 1867 } 1868 1869 o->have_attrs = have_attrs; 1870 o->info.objectSize = obj_size; 1871 /* 1872 * In GP Internal API Specification 1.0 the partity bits aren't 1873 * counted when telling the size of the key in bits so remove the 1874 * parity bits here. 1875 */ 1876 if (is_gp_legacy_des_key_size(o->info.objectType, 1877 o->info.maxObjectSize)) 1878 o->info.objectSize -= o->info.objectSize / 8; 1879 1880 return TEE_SUCCESS; 1881 } 1882 1883 TEE_Result syscall_cryp_obj_populate(unsigned long obj, 1884 struct utee_attribute *usr_attrs, 1885 unsigned long attr_count) 1886 { 1887 struct ts_session *sess = ts_get_current_session(); 1888 TEE_Result res = TEE_SUCCESS; 1889 struct tee_obj *o = NULL; 1890 const struct tee_cryp_obj_type_props *type_props = NULL; 1891 TEE_Attribute *attrs = NULL; 1892 size_t alloc_size = 0; 1893 1894 res = tee_obj_get(to_user_ta_ctx(sess->ctx), uref_to_vaddr(obj), &o); 1895 if (res != TEE_SUCCESS) 1896 return res; 1897 1898 /* Must be a transient object */ 1899 if ((o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 1900 return TEE_ERROR_BAD_PARAMETERS; 1901 1902 /* Must not be initialized already */ 1903 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1904 return TEE_ERROR_BAD_PARAMETERS; 1905 1906 type_props = tee_svc_find_type_props(o->info.objectType); 1907 if (!type_props) 1908 return TEE_ERROR_NOT_IMPLEMENTED; 1909 1910 if (MUL_OVERFLOW(sizeof(TEE_Attribute), attr_count, &alloc_size)) 1911 return TEE_ERROR_OVERFLOW; 1912 1913 attrs = malloc(alloc_size); 1914 if (!attrs) 1915 return TEE_ERROR_OUT_OF_MEMORY; 1916 1917 res = copy_in_attrs(to_user_ta_ctx(sess->ctx), usr_attrs, attr_count, 1918 attrs); 1919 if (res != TEE_SUCCESS) 1920 goto out; 1921 1922 res = tee_svc_cryp_check_attr(ATTR_USAGE_POPULATE, type_props, 1923 attrs, attr_count); 1924 if (res != TEE_SUCCESS) 1925 goto out; 1926 1927 res = tee_svc_cryp_obj_populate_type(o, type_props, attrs, attr_count); 1928 if (res == TEE_SUCCESS) 1929 o->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 1930 1931 out: 1932 free_wipe(attrs); 1933 return res; 1934 } 1935 1936 TEE_Result syscall_cryp_obj_copy(unsigned long dst, unsigned long src) 1937 { 1938 struct ts_session *sess = ts_get_current_session(); 1939 TEE_Result res = TEE_SUCCESS; 1940 struct tee_obj *dst_o = NULL; 1941 struct tee_obj *src_o = NULL; 1942 1943 res = tee_obj_get(to_user_ta_ctx(sess->ctx), 1944 uref_to_vaddr(dst), &dst_o); 1945 if (res != TEE_SUCCESS) 1946 return res; 1947 1948 res = tee_obj_get(to_user_ta_ctx(sess->ctx), 1949 uref_to_vaddr(src), &src_o); 1950 if (res != TEE_SUCCESS) 1951 return res; 1952 1953 if ((src_o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1954 return TEE_ERROR_BAD_PARAMETERS; 1955 if ((dst_o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 1956 return TEE_ERROR_BAD_PARAMETERS; 1957 if ((dst_o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1958 return TEE_ERROR_BAD_PARAMETERS; 1959 1960 res = tee_obj_attr_copy_from(dst_o, src_o); 1961 if (res != TEE_SUCCESS) 1962 return res; 1963 1964 dst_o->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 1965 dst_o->info.objectSize = src_o->info.objectSize; 1966 dst_o->info.objectUsage = src_o->info.objectUsage; 1967 return TEE_SUCCESS; 1968 } 1969 1970 static TEE_Result check_pub_rsa_key(struct bignum *e) 1971 { 1972 size_t n = crypto_bignum_num_bytes(e); 1973 uint8_t bin_key[256 / 8] = { 0 }; 1974 1975 /* 1976 * NIST SP800-56B requires public RSA key to be an odd integer in 1977 * the range 65537 <= e < 2^256. 1978 */ 1979 1980 if (n > sizeof(bin_key) || n < 3) 1981 return TEE_ERROR_BAD_PARAMETERS; 1982 1983 crypto_bignum_bn2bin(e, bin_key); 1984 1985 if (!(bin_key[n - 1] & 1)) /* key must be odd */ 1986 return TEE_ERROR_BAD_PARAMETERS; 1987 1988 if (n == 3) { 1989 uint32_t key = 0; 1990 1991 for (n = 0; n < 3; n++) { 1992 key <<= 8; 1993 key |= bin_key[n]; 1994 } 1995 1996 if (key < 65537) 1997 return TEE_ERROR_BAD_PARAMETERS; 1998 } 1999 2000 /* key is larger than 65537 */ 2001 return TEE_SUCCESS; 2002 } 2003 2004 static TEE_Result tee_svc_obj_generate_key_rsa( 2005 struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props, 2006 uint32_t key_size, 2007 const TEE_Attribute *params, uint32_t param_count) 2008 { 2009 TEE_Result res = TEE_SUCCESS; 2010 struct rsa_keypair *key = o->attr; 2011 uint32_t e = TEE_U32_TO_BIG_ENDIAN(65537); 2012 2013 /* Copy the present attributes into the obj before starting */ 2014 res = tee_svc_cryp_obj_populate_type(o, type_props, params, 2015 param_count); 2016 if (res != TEE_SUCCESS) 2017 return res; 2018 if (get_attribute(o, type_props, TEE_ATTR_RSA_PUBLIC_EXPONENT)) { 2019 res = check_pub_rsa_key(key->e); 2020 if (res) 2021 return res; 2022 } else { 2023 crypto_bignum_bin2bn((const uint8_t *)&e, sizeof(e), key->e); 2024 } 2025 res = crypto_acipher_gen_rsa_key(key, key_size); 2026 if (res != TEE_SUCCESS) 2027 return res; 2028 2029 /* Set bits for all known attributes for this object type */ 2030 o->have_attrs = (1 << type_props->num_type_attrs) - 1; 2031 2032 return TEE_SUCCESS; 2033 } 2034 2035 static TEE_Result tee_svc_obj_generate_key_dsa( 2036 struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props, 2037 uint32_t key_size, const TEE_Attribute *params, uint32_t param_count) 2038 { 2039 TEE_Result res; 2040 2041 /* Copy the present attributes into the obj before starting */ 2042 res = tee_svc_cryp_obj_populate_type(o, type_props, params, 2043 param_count); 2044 if (res != TEE_SUCCESS) 2045 return res; 2046 2047 res = crypto_acipher_gen_dsa_key(o->attr, key_size); 2048 if (res != TEE_SUCCESS) 2049 return res; 2050 2051 /* Set bits for all known attributes for this object type */ 2052 o->have_attrs = (1 << type_props->num_type_attrs) - 1; 2053 2054 return TEE_SUCCESS; 2055 } 2056 2057 static TEE_Result tee_svc_obj_generate_key_dh( 2058 struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props, 2059 uint32_t key_size, const TEE_Attribute *params, uint32_t param_count) 2060 { 2061 TEE_Result res; 2062 struct dh_keypair *tee_dh_key; 2063 struct bignum *dh_q = NULL; 2064 uint32_t dh_xbits = 0; 2065 2066 /* Copy the present attributes into the obj before starting */ 2067 res = tee_svc_cryp_obj_populate_type(o, type_props, params, 2068 param_count); 2069 if (res != TEE_SUCCESS) 2070 return res; 2071 2072 tee_dh_key = (struct dh_keypair *)o->attr; 2073 2074 if (get_attribute(o, type_props, TEE_ATTR_DH_SUBPRIME)) 2075 dh_q = tee_dh_key->q; 2076 if (get_attribute(o, type_props, TEE_ATTR_DH_X_BITS)) 2077 dh_xbits = tee_dh_key->xbits; 2078 res = crypto_acipher_gen_dh_key(tee_dh_key, dh_q, dh_xbits, key_size); 2079 if (res != TEE_SUCCESS) 2080 return res; 2081 2082 /* Set bits for the generated public and private key */ 2083 set_attribute(o, type_props, TEE_ATTR_DH_PUBLIC_VALUE); 2084 set_attribute(o, type_props, TEE_ATTR_DH_PRIVATE_VALUE); 2085 set_attribute(o, type_props, TEE_ATTR_DH_X_BITS); 2086 return TEE_SUCCESS; 2087 } 2088 2089 static TEE_Result tee_svc_obj_generate_key_ecc( 2090 struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props, 2091 uint32_t key_size, const TEE_Attribute *params, uint32_t param_count) 2092 { 2093 TEE_Result res; 2094 struct ecc_keypair *tee_ecc_key; 2095 2096 /* Copy the present attributes into the obj before starting */ 2097 res = tee_svc_cryp_obj_populate_type(o, type_props, params, 2098 param_count); 2099 if (res != TEE_SUCCESS) 2100 return res; 2101 2102 tee_ecc_key = (struct ecc_keypair *)o->attr; 2103 2104 res = crypto_acipher_gen_ecc_key(tee_ecc_key, key_size); 2105 if (res != TEE_SUCCESS) 2106 return res; 2107 2108 /* Set bits for the generated public and private key */ 2109 set_attribute(o, type_props, TEE_ATTR_ECC_PRIVATE_VALUE); 2110 set_attribute(o, type_props, TEE_ATTR_ECC_PUBLIC_VALUE_X); 2111 set_attribute(o, type_props, TEE_ATTR_ECC_PUBLIC_VALUE_Y); 2112 set_attribute(o, type_props, TEE_ATTR_ECC_CURVE); 2113 return TEE_SUCCESS; 2114 } 2115 2116 static TEE_Result 2117 tee_svc_obj_generate_key_x25519(struct tee_obj *o, 2118 const struct tee_cryp_obj_type_props 2119 *type_props, 2120 uint32_t key_size, 2121 const TEE_Attribute *params, 2122 uint32_t param_count) 2123 { 2124 TEE_Result res = TEE_ERROR_GENERIC; 2125 struct x25519_keypair *tee_x25519_key = NULL; 2126 2127 /* Copy the present attributes into the obj before starting */ 2128 res = tee_svc_cryp_obj_populate_type(o, type_props, params, 2129 param_count); 2130 if (res != TEE_SUCCESS) 2131 return res; 2132 2133 tee_x25519_key = (struct x25519_keypair *)o->attr; 2134 2135 res = crypto_acipher_gen_x25519_key(tee_x25519_key, key_size); 2136 if (res != TEE_SUCCESS) 2137 return res; 2138 2139 /* Set bits for the generated public and private key */ 2140 set_attribute(o, type_props, TEE_ATTR_X25519_PRIVATE_VALUE); 2141 set_attribute(o, type_props, TEE_ATTR_X25519_PUBLIC_VALUE); 2142 return TEE_SUCCESS; 2143 } 2144 2145 static TEE_Result 2146 tee_svc_obj_generate_key_ed25519(struct tee_obj *o, 2147 const struct tee_cryp_obj_type_props 2148 *type_props, 2149 uint32_t key_size, 2150 const TEE_Attribute *params, 2151 uint32_t param_count) 2152 { 2153 TEE_Result res = TEE_ERROR_GENERIC; 2154 struct ed25519_keypair *key = NULL; 2155 2156 /* Copy the present attributes into the obj before starting */ 2157 res = tee_svc_cryp_obj_populate_type(o, type_props, params, 2158 param_count); 2159 if (res != TEE_SUCCESS) 2160 return res; 2161 2162 key = o->attr; 2163 2164 res = crypto_acipher_gen_ed25519_key(key, key_size); 2165 if (res != TEE_SUCCESS) 2166 return res; 2167 2168 /* Set bits for the generated public and private key */ 2169 set_attribute(o, type_props, TEE_ATTR_ED25519_PRIVATE_VALUE); 2170 set_attribute(o, type_props, TEE_ATTR_ED25519_PUBLIC_VALUE); 2171 return TEE_SUCCESS; 2172 } 2173 2174 static TEE_Result 2175 tee_svc_obj_ed25519_parse_params(const TEE_Attribute *params, size_t num_params, 2176 bool *ph_flag, const uint8_t **ctx, 2177 size_t *ctx_len) 2178 { 2179 size_t n = 0; 2180 2181 *ctx = NULL; 2182 2183 for (n = 0; n < num_params; n++) { 2184 switch (params[n].attributeID) { 2185 case TEE_ATTR_EDDSA_PREHASH: 2186 *ph_flag = true; 2187 break; 2188 2189 case TEE_ATTR_EDDSA_CTX: 2190 /* several provided contexts are treated as error */ 2191 if (*ctx) 2192 return TEE_ERROR_BAD_PARAMETERS; 2193 2194 *ctx_len = params[n].content.ref.length; 2195 if (*ctx_len > TEE_ED25519_CTX_MAX_LENGTH) 2196 return TEE_ERROR_BAD_PARAMETERS; 2197 2198 if (!*ctx_len) 2199 break; 2200 2201 *ctx = params[n].content.ref.buffer; 2202 if (!*ctx) 2203 return TEE_ERROR_BAD_PARAMETERS; 2204 break; 2205 2206 default: 2207 return TEE_ERROR_BAD_PARAMETERS; 2208 } 2209 } 2210 2211 return TEE_SUCCESS; 2212 } 2213 2214 static TEE_Result 2215 tee_svc_obj_ed25519_sign(struct ed25519_keypair *key, 2216 const uint8_t *msg, size_t msg_len, 2217 uint8_t *sig, size_t *sig_len, 2218 const TEE_Attribute *params, size_t num_params) 2219 { 2220 TEE_Result err = TEE_ERROR_GENERIC; 2221 size_t ctx_len = 0; 2222 const uint8_t *ctx = NULL; 2223 bool ph_flag = false; 2224 2225 err = tee_svc_obj_ed25519_parse_params(params, num_params, &ph_flag, 2226 &ctx, &ctx_len); 2227 if (err != TEE_SUCCESS) 2228 return err; 2229 2230 if (ph_flag || ctx) { 2231 return crypto_acipher_ed25519ctx_sign(key, msg, msg_len, sig, 2232 sig_len, ph_flag, 2233 ctx, ctx_len); 2234 } 2235 2236 return crypto_acipher_ed25519_sign(key, msg, msg_len, sig, sig_len); 2237 } 2238 2239 static TEE_Result 2240 tee_svc_obj_ed25519_verify(struct ed25519_keypair *key, 2241 const uint8_t *msg, size_t msg_len, 2242 const uint8_t *sig, size_t sig_len, 2243 const TEE_Attribute *params, size_t num_params) 2244 { 2245 TEE_Result err = TEE_ERROR_GENERIC; 2246 size_t ctx_len = 0; 2247 const uint8_t *ctx = NULL; 2248 bool ph_flag = false; 2249 2250 err = tee_svc_obj_ed25519_parse_params(params, num_params, &ph_flag, 2251 &ctx, &ctx_len); 2252 if (err) 2253 return err; 2254 2255 if (ph_flag || ctx) { 2256 return crypto_acipher_ed25519ctx_verify(key, msg, msg_len, sig, 2257 sig_len, ph_flag, 2258 ctx, ctx_len); 2259 } 2260 2261 return crypto_acipher_ed25519_verify(key, msg, msg_len, sig, sig_len); 2262 } 2263 2264 TEE_Result syscall_obj_generate_key(unsigned long obj, unsigned long key_size, 2265 const struct utee_attribute *usr_params, 2266 unsigned long param_count) 2267 { 2268 struct ts_session *sess = ts_get_current_session(); 2269 TEE_Result res = TEE_SUCCESS; 2270 const struct tee_cryp_obj_type_props *type_props = NULL; 2271 struct tee_obj *o = NULL; 2272 struct tee_cryp_obj_secret *key = NULL; 2273 size_t byte_size = 0; 2274 TEE_Attribute *params = NULL; 2275 size_t alloc_size = 0; 2276 2277 res = tee_obj_get(to_user_ta_ctx(sess->ctx), uref_to_vaddr(obj), &o); 2278 if (res != TEE_SUCCESS) 2279 return res; 2280 2281 /* Must be a transient object */ 2282 if ((o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 2283 return TEE_ERROR_BAD_STATE; 2284 2285 /* Must not be initialized already */ 2286 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 2287 return TEE_ERROR_BAD_STATE; 2288 2289 /* Find description of object */ 2290 type_props = tee_svc_find_type_props(o->info.objectType); 2291 if (!type_props) 2292 return TEE_ERROR_NOT_SUPPORTED; 2293 2294 /* Check that key_size follows restrictions */ 2295 res = check_key_size(type_props, key_size); 2296 if (res) 2297 return res; 2298 2299 if (MUL_OVERFLOW(sizeof(TEE_Attribute), param_count, &alloc_size)) 2300 return TEE_ERROR_OVERFLOW; 2301 2302 params = malloc(alloc_size); 2303 if (!params) 2304 return TEE_ERROR_OUT_OF_MEMORY; 2305 res = copy_in_attrs(to_user_ta_ctx(sess->ctx), usr_params, param_count, 2306 params); 2307 if (res != TEE_SUCCESS) 2308 goto out; 2309 2310 res = tee_svc_cryp_check_attr(ATTR_USAGE_GENERATE_KEY, type_props, 2311 params, param_count); 2312 if (res != TEE_SUCCESS) 2313 goto out; 2314 2315 switch (o->info.objectType) { 2316 case TEE_TYPE_AES: 2317 case TEE_TYPE_DES: 2318 case TEE_TYPE_DES3: 2319 case TEE_TYPE_SM4: 2320 case TEE_TYPE_HMAC_MD5: 2321 case TEE_TYPE_HMAC_SHA1: 2322 case TEE_TYPE_HMAC_SHA224: 2323 case TEE_TYPE_HMAC_SHA256: 2324 case TEE_TYPE_HMAC_SHA384: 2325 case TEE_TYPE_HMAC_SHA512: 2326 case TEE_TYPE_HMAC_SM3: 2327 case TEE_TYPE_GENERIC_SECRET: 2328 byte_size = key_size / 8; 2329 2330 /* 2331 * In GP Internal API Specification 1.0 the partity bits 2332 * aren't counted when telling the size of the key in bits. 2333 */ 2334 if (is_gp_legacy_des_key_size(o->info.objectType, key_size)) 2335 byte_size = (key_size + key_size / 7) / 8; 2336 2337 key = (struct tee_cryp_obj_secret *)o->attr; 2338 if (byte_size > key->alloc_size) { 2339 res = TEE_ERROR_EXCESS_DATA; 2340 goto out; 2341 } 2342 2343 res = crypto_rng_read((void *)(key + 1), byte_size); 2344 if (res != TEE_SUCCESS) 2345 goto out; 2346 2347 key->key_size = byte_size; 2348 2349 /* Set bits for all known attributes for this object type */ 2350 o->have_attrs = (1 << type_props->num_type_attrs) - 1; 2351 2352 break; 2353 2354 case TEE_TYPE_RSA_KEYPAIR: 2355 res = tee_svc_obj_generate_key_rsa(o, type_props, key_size, 2356 params, param_count); 2357 if (res != TEE_SUCCESS) 2358 goto out; 2359 break; 2360 2361 case TEE_TYPE_DSA_KEYPAIR: 2362 res = tee_svc_obj_generate_key_dsa(o, type_props, key_size, 2363 params, param_count); 2364 if (res != TEE_SUCCESS) 2365 goto out; 2366 break; 2367 2368 case TEE_TYPE_DH_KEYPAIR: 2369 res = tee_svc_obj_generate_key_dh(o, type_props, key_size, 2370 params, param_count); 2371 if (res != TEE_SUCCESS) 2372 goto out; 2373 break; 2374 2375 case TEE_TYPE_ECDSA_KEYPAIR: 2376 case TEE_TYPE_ECDH_KEYPAIR: 2377 case TEE_TYPE_SM2_DSA_KEYPAIR: 2378 case TEE_TYPE_SM2_KEP_KEYPAIR: 2379 case TEE_TYPE_SM2_PKE_KEYPAIR: 2380 res = tee_svc_obj_generate_key_ecc(o, type_props, key_size, 2381 params, param_count); 2382 if (res != TEE_SUCCESS) 2383 goto out; 2384 break; 2385 2386 case TEE_TYPE_X25519_KEYPAIR: 2387 res = tee_svc_obj_generate_key_x25519(o, type_props, key_size, 2388 params, param_count); 2389 if (res != TEE_SUCCESS) 2390 goto out; 2391 break; 2392 2393 case TEE_TYPE_ED25519_KEYPAIR: 2394 res = tee_svc_obj_generate_key_ed25519(o, type_props, key_size, 2395 params, param_count); 2396 if (res != TEE_SUCCESS) 2397 goto out; 2398 break; 2399 2400 default: 2401 res = TEE_ERROR_BAD_FORMAT; 2402 } 2403 2404 out: 2405 free_wipe(params); 2406 if (res == TEE_SUCCESS) { 2407 o->info.objectSize = key_size; 2408 o->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 2409 } 2410 return res; 2411 } 2412 2413 static TEE_Result tee_svc_cryp_get_state(struct ts_session *sess, 2414 vaddr_t state_id, 2415 struct tee_cryp_state **state) 2416 { 2417 struct tee_cryp_state *s; 2418 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 2419 2420 TAILQ_FOREACH(s, &utc->cryp_states, link) { 2421 if (state_id == (vaddr_t)s) { 2422 *state = s; 2423 return TEE_SUCCESS; 2424 } 2425 } 2426 return TEE_ERROR_BAD_PARAMETERS; 2427 } 2428 2429 static void cryp_state_free(struct user_ta_ctx *utc, struct tee_cryp_state *cs) 2430 { 2431 struct tee_obj *o; 2432 2433 if (tee_obj_get(utc, cs->key1, &o) == TEE_SUCCESS) 2434 tee_obj_close(utc, o); 2435 if (tee_obj_get(utc, cs->key2, &o) == TEE_SUCCESS) 2436 tee_obj_close(utc, o); 2437 2438 TAILQ_REMOVE(&utc->cryp_states, cs, link); 2439 if (cs->ctx_finalize != NULL) 2440 cs->ctx_finalize(cs->ctx); 2441 2442 switch (TEE_ALG_GET_CLASS(cs->algo)) { 2443 case TEE_OPERATION_CIPHER: 2444 crypto_cipher_free_ctx(cs->ctx); 2445 break; 2446 case TEE_OPERATION_AE: 2447 crypto_authenc_free_ctx(cs->ctx); 2448 break; 2449 case TEE_OPERATION_DIGEST: 2450 crypto_hash_free_ctx(cs->ctx); 2451 break; 2452 case TEE_OPERATION_MAC: 2453 crypto_mac_free_ctx(cs->ctx); 2454 break; 2455 default: 2456 assert(!cs->ctx); 2457 } 2458 2459 free(cs); 2460 } 2461 2462 static TEE_Result tee_svc_cryp_check_key_type(const struct tee_obj *o, 2463 uint32_t algo, 2464 TEE_OperationMode mode) 2465 { 2466 uint32_t req_key_type; 2467 uint32_t req_key_type2 = 0; 2468 2469 switch (TEE_ALG_GET_MAIN_ALG(algo)) { 2470 case TEE_MAIN_ALGO_MD5: 2471 req_key_type = TEE_TYPE_HMAC_MD5; 2472 break; 2473 case TEE_MAIN_ALGO_SHA1: 2474 req_key_type = TEE_TYPE_HMAC_SHA1; 2475 break; 2476 case TEE_MAIN_ALGO_SHA224: 2477 req_key_type = TEE_TYPE_HMAC_SHA224; 2478 break; 2479 case TEE_MAIN_ALGO_SHA256: 2480 req_key_type = TEE_TYPE_HMAC_SHA256; 2481 break; 2482 case TEE_MAIN_ALGO_SHA384: 2483 req_key_type = TEE_TYPE_HMAC_SHA384; 2484 break; 2485 case TEE_MAIN_ALGO_SHA512: 2486 req_key_type = TEE_TYPE_HMAC_SHA512; 2487 break; 2488 case TEE_MAIN_ALGO_SM3: 2489 req_key_type = TEE_TYPE_HMAC_SM3; 2490 break; 2491 case TEE_MAIN_ALGO_AES: 2492 req_key_type = TEE_TYPE_AES; 2493 break; 2494 case TEE_MAIN_ALGO_DES: 2495 req_key_type = TEE_TYPE_DES; 2496 break; 2497 case TEE_MAIN_ALGO_DES3: 2498 req_key_type = TEE_TYPE_DES3; 2499 break; 2500 case TEE_MAIN_ALGO_SM4: 2501 req_key_type = TEE_TYPE_SM4; 2502 break; 2503 case TEE_MAIN_ALGO_RSA: 2504 req_key_type = TEE_TYPE_RSA_KEYPAIR; 2505 if (mode == TEE_MODE_ENCRYPT || mode == TEE_MODE_VERIFY) 2506 req_key_type2 = TEE_TYPE_RSA_PUBLIC_KEY; 2507 break; 2508 case TEE_MAIN_ALGO_DSA: 2509 req_key_type = TEE_TYPE_DSA_KEYPAIR; 2510 if (mode == TEE_MODE_ENCRYPT || mode == TEE_MODE_VERIFY) 2511 req_key_type2 = TEE_TYPE_DSA_PUBLIC_KEY; 2512 break; 2513 case TEE_MAIN_ALGO_DH: 2514 req_key_type = TEE_TYPE_DH_KEYPAIR; 2515 break; 2516 case TEE_MAIN_ALGO_ECDSA: 2517 req_key_type = TEE_TYPE_ECDSA_KEYPAIR; 2518 if (mode == TEE_MODE_VERIFY) 2519 req_key_type2 = TEE_TYPE_ECDSA_PUBLIC_KEY; 2520 break; 2521 case TEE_MAIN_ALGO_ECDH: 2522 req_key_type = TEE_TYPE_ECDH_KEYPAIR; 2523 break; 2524 case TEE_MAIN_ALGO_ED25519: 2525 req_key_type = TEE_TYPE_ED25519_KEYPAIR; 2526 if (mode == TEE_MODE_VERIFY) 2527 req_key_type2 = TEE_TYPE_ED25519_PUBLIC_KEY; 2528 break; 2529 case TEE_MAIN_ALGO_SM2_PKE: 2530 if (mode == TEE_MODE_ENCRYPT) 2531 req_key_type = TEE_TYPE_SM2_PKE_PUBLIC_KEY; 2532 else 2533 req_key_type = TEE_TYPE_SM2_PKE_KEYPAIR; 2534 break; 2535 case TEE_MAIN_ALGO_SM2_DSA_SM3: 2536 if (mode == TEE_MODE_VERIFY) 2537 req_key_type = TEE_TYPE_SM2_DSA_PUBLIC_KEY; 2538 else 2539 req_key_type = TEE_TYPE_SM2_DSA_KEYPAIR; 2540 break; 2541 #if defined(CFG_CRYPTO_SM2_KEP) 2542 case TEE_MAIN_ALGO_SM2_KEP: 2543 req_key_type = TEE_TYPE_SM2_KEP_KEYPAIR; 2544 req_key_type2 = TEE_TYPE_SM2_KEP_PUBLIC_KEY; 2545 break; 2546 #endif 2547 #if defined(CFG_CRYPTO_HKDF) 2548 case TEE_MAIN_ALGO_HKDF: 2549 req_key_type = TEE_TYPE_HKDF_IKM; 2550 break; 2551 #endif 2552 #if defined(CFG_CRYPTO_CONCAT_KDF) 2553 case TEE_MAIN_ALGO_CONCAT_KDF: 2554 req_key_type = TEE_TYPE_CONCAT_KDF_Z; 2555 break; 2556 #endif 2557 #if defined(CFG_CRYPTO_PBKDF2) 2558 case TEE_MAIN_ALGO_PBKDF2: 2559 req_key_type = TEE_TYPE_PBKDF2_PASSWORD; 2560 break; 2561 #endif 2562 case TEE_MAIN_ALGO_X25519: 2563 req_key_type = TEE_TYPE_X25519_KEYPAIR; 2564 break; 2565 default: 2566 return TEE_ERROR_BAD_PARAMETERS; 2567 } 2568 2569 if (req_key_type != o->info.objectType && 2570 req_key_type2 != o->info.objectType) 2571 return TEE_ERROR_BAD_PARAMETERS; 2572 return TEE_SUCCESS; 2573 } 2574 2575 TEE_Result syscall_cryp_state_alloc(unsigned long algo, unsigned long mode, 2576 unsigned long key1, unsigned long key2, 2577 uint32_t *state) 2578 { 2579 struct ts_session *sess = ts_get_current_session(); 2580 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 2581 TEE_Result res = TEE_SUCCESS; 2582 struct tee_cryp_state *cs = NULL; 2583 struct tee_obj *o1 = NULL; 2584 struct tee_obj *o2 = NULL; 2585 2586 if (key1 != 0) { 2587 res = tee_obj_get(utc, uref_to_vaddr(key1), &o1); 2588 if (res != TEE_SUCCESS) 2589 return res; 2590 if (o1->busy) 2591 return TEE_ERROR_BAD_PARAMETERS; 2592 res = tee_svc_cryp_check_key_type(o1, algo, mode); 2593 if (res != TEE_SUCCESS) 2594 return res; 2595 } 2596 if (key2 != 0) { 2597 res = tee_obj_get(utc, uref_to_vaddr(key2), &o2); 2598 if (res != TEE_SUCCESS) 2599 return res; 2600 if (o2->busy) 2601 return TEE_ERROR_BAD_PARAMETERS; 2602 res = tee_svc_cryp_check_key_type(o2, algo, mode); 2603 if (res != TEE_SUCCESS) 2604 return res; 2605 } 2606 2607 cs = calloc(1, sizeof(struct tee_cryp_state)); 2608 if (!cs) 2609 return TEE_ERROR_OUT_OF_MEMORY; 2610 TAILQ_INSERT_TAIL(&utc->cryp_states, cs, link); 2611 cs->algo = algo; 2612 cs->mode = mode; 2613 cs->state = CRYP_STATE_UNINITIALIZED; 2614 2615 switch (TEE_ALG_GET_CLASS(algo)) { 2616 case TEE_OPERATION_CIPHER: 2617 if ((algo == TEE_ALG_AES_XTS && (key1 == 0 || key2 == 0)) || 2618 (algo != TEE_ALG_AES_XTS && (key1 == 0 || key2 != 0))) { 2619 res = TEE_ERROR_BAD_PARAMETERS; 2620 } else { 2621 res = crypto_cipher_alloc_ctx(&cs->ctx, algo); 2622 if (res != TEE_SUCCESS) 2623 break; 2624 } 2625 break; 2626 case TEE_OPERATION_AE: 2627 if (key1 == 0 || key2 != 0) { 2628 res = TEE_ERROR_BAD_PARAMETERS; 2629 } else { 2630 res = crypto_authenc_alloc_ctx(&cs->ctx, algo); 2631 if (res != TEE_SUCCESS) 2632 break; 2633 } 2634 break; 2635 case TEE_OPERATION_MAC: 2636 if (key1 == 0 || key2 != 0) { 2637 res = TEE_ERROR_BAD_PARAMETERS; 2638 } else { 2639 res = crypto_mac_alloc_ctx(&cs->ctx, algo); 2640 if (res != TEE_SUCCESS) 2641 break; 2642 } 2643 break; 2644 case TEE_OPERATION_DIGEST: 2645 if (key1 != 0 || key2 != 0) { 2646 res = TEE_ERROR_BAD_PARAMETERS; 2647 } else { 2648 res = crypto_hash_alloc_ctx(&cs->ctx, algo); 2649 if (res != TEE_SUCCESS) 2650 break; 2651 } 2652 break; 2653 case TEE_OPERATION_ASYMMETRIC_CIPHER: 2654 case TEE_OPERATION_ASYMMETRIC_SIGNATURE: 2655 if (algo == TEE_ALG_RSASSA_PKCS1_V1_5 && 2656 !IS_ENABLED(CFG_CRYPTO_RSASSA_NA1)) { 2657 res = TEE_ERROR_NOT_SUPPORTED; 2658 break; 2659 } 2660 if (key1 == 0 || key2 != 0) 2661 res = TEE_ERROR_BAD_PARAMETERS; 2662 break; 2663 case TEE_OPERATION_KEY_DERIVATION: 2664 if (algo == TEE_ALG_SM2_KEP) { 2665 if (key1 == 0 || key2 == 0) 2666 res = TEE_ERROR_BAD_PARAMETERS; 2667 } else { 2668 if (key1 == 0 || key2 != 0) 2669 res = TEE_ERROR_BAD_PARAMETERS; 2670 } 2671 break; 2672 default: 2673 res = TEE_ERROR_NOT_SUPPORTED; 2674 break; 2675 } 2676 if (res != TEE_SUCCESS) 2677 goto out; 2678 2679 res = copy_kaddr_to_uref(state, cs); 2680 if (res != TEE_SUCCESS) 2681 goto out; 2682 2683 /* Register keys */ 2684 if (o1 != NULL) { 2685 o1->busy = true; 2686 cs->key1 = (vaddr_t)o1; 2687 } 2688 if (o2 != NULL) { 2689 o2->busy = true; 2690 cs->key2 = (vaddr_t)o2; 2691 } 2692 2693 out: 2694 if (res != TEE_SUCCESS) 2695 cryp_state_free(utc, cs); 2696 return res; 2697 } 2698 2699 TEE_Result syscall_cryp_state_copy(unsigned long dst, unsigned long src) 2700 { 2701 struct ts_session *sess = ts_get_current_session(); 2702 TEE_Result res = TEE_SUCCESS; 2703 struct tee_cryp_state *cs_dst = NULL; 2704 struct tee_cryp_state *cs_src = NULL; 2705 2706 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(dst), &cs_dst); 2707 if (res != TEE_SUCCESS) 2708 return res; 2709 2710 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(src), &cs_src); 2711 if (res != TEE_SUCCESS) 2712 return res; 2713 if (cs_dst->algo != cs_src->algo || cs_dst->mode != cs_src->mode) 2714 return TEE_ERROR_BAD_PARAMETERS; 2715 2716 switch (TEE_ALG_GET_CLASS(cs_src->algo)) { 2717 case TEE_OPERATION_CIPHER: 2718 crypto_cipher_copy_state(cs_dst->ctx, cs_src->ctx); 2719 break; 2720 case TEE_OPERATION_AE: 2721 crypto_authenc_copy_state(cs_dst->ctx, cs_src->ctx); 2722 break; 2723 case TEE_OPERATION_DIGEST: 2724 crypto_hash_copy_state(cs_dst->ctx, cs_src->ctx); 2725 break; 2726 case TEE_OPERATION_MAC: 2727 crypto_mac_copy_state(cs_dst->ctx, cs_src->ctx); 2728 break; 2729 default: 2730 return TEE_ERROR_BAD_STATE; 2731 } 2732 2733 cs_dst->state = cs_src->state; 2734 cs_dst->ctx_finalize = cs_src->ctx_finalize; 2735 2736 return TEE_SUCCESS; 2737 } 2738 2739 void tee_svc_cryp_free_states(struct user_ta_ctx *utc) 2740 { 2741 struct tee_cryp_state_head *states = &utc->cryp_states; 2742 2743 while (!TAILQ_EMPTY(states)) 2744 cryp_state_free(utc, TAILQ_FIRST(states)); 2745 } 2746 2747 TEE_Result syscall_cryp_state_free(unsigned long state) 2748 { 2749 struct ts_session *sess = ts_get_current_session(); 2750 TEE_Result res = TEE_SUCCESS; 2751 struct tee_cryp_state *cs = NULL; 2752 2753 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 2754 if (res != TEE_SUCCESS) 2755 return res; 2756 cryp_state_free(to_user_ta_ctx(sess->ctx), cs); 2757 return TEE_SUCCESS; 2758 } 2759 2760 TEE_Result syscall_hash_init(unsigned long state, 2761 const void *iv __maybe_unused, 2762 size_t iv_len __maybe_unused) 2763 { 2764 struct ts_session *sess = ts_get_current_session(); 2765 TEE_Result res = TEE_SUCCESS; 2766 struct tee_cryp_state *cs = NULL; 2767 2768 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 2769 if (res != TEE_SUCCESS) 2770 return res; 2771 2772 switch (TEE_ALG_GET_CLASS(cs->algo)) { 2773 case TEE_OPERATION_DIGEST: 2774 res = crypto_hash_init(cs->ctx); 2775 if (res != TEE_SUCCESS) 2776 return res; 2777 break; 2778 case TEE_OPERATION_MAC: 2779 { 2780 struct tee_obj *o; 2781 struct tee_cryp_obj_secret *key; 2782 2783 res = tee_obj_get(to_user_ta_ctx(sess->ctx), 2784 cs->key1, &o); 2785 if (res != TEE_SUCCESS) 2786 return res; 2787 if ((o->info.handleFlags & 2788 TEE_HANDLE_FLAG_INITIALIZED) == 0) 2789 return TEE_ERROR_BAD_PARAMETERS; 2790 2791 key = (struct tee_cryp_obj_secret *)o->attr; 2792 res = crypto_mac_init(cs->ctx, (void *)(key + 1), 2793 key->key_size); 2794 if (res != TEE_SUCCESS) 2795 return res; 2796 break; 2797 } 2798 default: 2799 return TEE_ERROR_BAD_PARAMETERS; 2800 } 2801 2802 cs->state = CRYP_STATE_INITIALIZED; 2803 2804 return TEE_SUCCESS; 2805 } 2806 2807 TEE_Result syscall_hash_update(unsigned long state, const void *chunk, 2808 size_t chunk_size) 2809 { 2810 struct ts_session *sess = ts_get_current_session(); 2811 struct tee_cryp_state *cs = NULL; 2812 TEE_Result res = TEE_SUCCESS; 2813 2814 /* No data, but size provided isn't valid parameters. */ 2815 if (!chunk && chunk_size) 2816 return TEE_ERROR_BAD_PARAMETERS; 2817 2818 /* Zero length hash is valid, but nothing we need to do. */ 2819 if (!chunk_size) 2820 return TEE_SUCCESS; 2821 2822 chunk = memtag_strip_tag_const(chunk); 2823 2824 res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 2825 TEE_MEMORY_ACCESS_READ | 2826 TEE_MEMORY_ACCESS_ANY_OWNER, 2827 (uaddr_t)chunk, chunk_size); 2828 if (res != TEE_SUCCESS) 2829 return res; 2830 2831 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 2832 if (res != TEE_SUCCESS) 2833 return res; 2834 2835 if (cs->state != CRYP_STATE_INITIALIZED) 2836 return TEE_ERROR_BAD_STATE; 2837 2838 switch (TEE_ALG_GET_CLASS(cs->algo)) { 2839 case TEE_OPERATION_DIGEST: 2840 res = crypto_hash_update(cs->ctx, chunk, chunk_size); 2841 if (res != TEE_SUCCESS) 2842 return res; 2843 break; 2844 case TEE_OPERATION_MAC: 2845 res = crypto_mac_update(cs->ctx, chunk, chunk_size); 2846 if (res != TEE_SUCCESS) 2847 return res; 2848 break; 2849 default: 2850 return TEE_ERROR_BAD_PARAMETERS; 2851 } 2852 2853 return TEE_SUCCESS; 2854 } 2855 2856 TEE_Result syscall_hash_final(unsigned long state, const void *chunk, 2857 size_t chunk_size, void *hash, uint64_t *hash_len) 2858 { 2859 struct ts_session *sess = ts_get_current_session(); 2860 struct tee_cryp_state *cs = NULL; 2861 TEE_Result res2 = TEE_SUCCESS; 2862 TEE_Result res = TEE_SUCCESS; 2863 size_t hash_size = 0; 2864 size_t hlen = 0; 2865 2866 /* No data, but size provided isn't valid parameters. */ 2867 if (!chunk && chunk_size) 2868 return TEE_ERROR_BAD_PARAMETERS; 2869 2870 chunk = memtag_strip_tag_const(chunk); 2871 hash = memtag_strip_tag(hash); 2872 2873 res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 2874 TEE_MEMORY_ACCESS_READ | 2875 TEE_MEMORY_ACCESS_ANY_OWNER, 2876 (uaddr_t)chunk, chunk_size); 2877 if (res != TEE_SUCCESS) 2878 return res; 2879 2880 res = get_user_u64_as_size_t(&hlen, hash_len); 2881 if (res != TEE_SUCCESS) 2882 return res; 2883 2884 res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 2885 TEE_MEMORY_ACCESS_READ | 2886 TEE_MEMORY_ACCESS_WRITE | 2887 TEE_MEMORY_ACCESS_ANY_OWNER, 2888 (uaddr_t)hash, hlen); 2889 if (res != TEE_SUCCESS) 2890 return res; 2891 2892 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 2893 if (res != TEE_SUCCESS) 2894 return res; 2895 2896 if (cs->state != CRYP_STATE_INITIALIZED) 2897 return TEE_ERROR_BAD_STATE; 2898 2899 switch (TEE_ALG_GET_CLASS(cs->algo)) { 2900 case TEE_OPERATION_DIGEST: 2901 res = tee_alg_get_digest_size(cs->algo, &hash_size); 2902 if (res != TEE_SUCCESS) 2903 return res; 2904 if (hlen < hash_size) { 2905 res = TEE_ERROR_SHORT_BUFFER; 2906 goto out; 2907 } 2908 2909 if (chunk_size) { 2910 res = crypto_hash_update(cs->ctx, chunk, chunk_size); 2911 if (res != TEE_SUCCESS) 2912 return res; 2913 } 2914 2915 res = crypto_hash_final(cs->ctx, hash, hash_size); 2916 if (res != TEE_SUCCESS) 2917 return res; 2918 break; 2919 2920 case TEE_OPERATION_MAC: 2921 res = tee_alg_get_digest_size(cs->algo, &hash_size); 2922 if (res != TEE_SUCCESS) 2923 return res; 2924 if (hlen < hash_size) { 2925 res = TEE_ERROR_SHORT_BUFFER; 2926 goto out; 2927 } 2928 2929 if (chunk_size) { 2930 res = crypto_mac_update(cs->ctx, chunk, chunk_size); 2931 if (res != TEE_SUCCESS) 2932 return res; 2933 } 2934 2935 res = crypto_mac_final(cs->ctx, hash, hash_size); 2936 if (res != TEE_SUCCESS) 2937 return res; 2938 break; 2939 2940 default: 2941 return TEE_ERROR_BAD_PARAMETERS; 2942 } 2943 out: 2944 res2 = put_user_u64(hash_len, hash_size); 2945 if (res2 != TEE_SUCCESS) 2946 return res2; 2947 return res; 2948 } 2949 2950 TEE_Result syscall_cipher_init(unsigned long state, const void *iv, 2951 size_t iv_len) 2952 { 2953 struct ts_session *sess = ts_get_current_session(); 2954 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 2955 struct tee_cryp_obj_secret *key1 = NULL; 2956 struct tee_cryp_state *cs = NULL; 2957 TEE_Result res = TEE_SUCCESS; 2958 struct tee_obj *o = NULL; 2959 2960 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 2961 if (res != TEE_SUCCESS) 2962 return res; 2963 2964 if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_CIPHER) 2965 return TEE_ERROR_BAD_STATE; 2966 2967 iv = memtag_strip_tag_const(iv); 2968 2969 res = vm_check_access_rights(&utc->uctx, 2970 TEE_MEMORY_ACCESS_READ | 2971 TEE_MEMORY_ACCESS_ANY_OWNER, 2972 (uaddr_t)iv, iv_len); 2973 if (res != TEE_SUCCESS) 2974 return res; 2975 2976 res = tee_obj_get(utc, cs->key1, &o); 2977 if (res != TEE_SUCCESS) 2978 return res; 2979 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 2980 return TEE_ERROR_BAD_PARAMETERS; 2981 2982 key1 = o->attr; 2983 2984 if (tee_obj_get(utc, cs->key2, &o) == TEE_SUCCESS) { 2985 struct tee_cryp_obj_secret *key2 = o->attr; 2986 2987 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 2988 return TEE_ERROR_BAD_PARAMETERS; 2989 2990 res = crypto_cipher_init(cs->ctx, cs->mode, 2991 (uint8_t *)(key1 + 1), key1->key_size, 2992 (uint8_t *)(key2 + 1), key2->key_size, 2993 iv, iv_len); 2994 } else { 2995 res = crypto_cipher_init(cs->ctx, cs->mode, 2996 (uint8_t *)(key1 + 1), key1->key_size, 2997 NULL, 0, iv, iv_len); 2998 } 2999 if (res != TEE_SUCCESS) 3000 return res; 3001 3002 cs->ctx_finalize = crypto_cipher_final; 3003 cs->state = CRYP_STATE_INITIALIZED; 3004 3005 return TEE_SUCCESS; 3006 } 3007 3008 static TEE_Result tee_svc_cipher_update_helper(unsigned long state, 3009 bool last_block, const void *src, size_t src_len, 3010 void *dst, uint64_t *dst_len) 3011 { 3012 struct ts_session *sess = ts_get_current_session(); 3013 struct tee_cryp_state *cs = NULL; 3014 TEE_Result res = TEE_SUCCESS; 3015 size_t dlen = 0; 3016 3017 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 3018 if (res != TEE_SUCCESS) 3019 return res; 3020 3021 if (cs->state != CRYP_STATE_INITIALIZED) 3022 return TEE_ERROR_BAD_STATE; 3023 3024 src = memtag_strip_tag_const(src); 3025 dst = memtag_strip_tag(dst); 3026 3027 res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 3028 TEE_MEMORY_ACCESS_READ | 3029 TEE_MEMORY_ACCESS_ANY_OWNER, 3030 (uaddr_t)src, src_len); 3031 if (res != TEE_SUCCESS) 3032 return res; 3033 3034 if (!dst_len) { 3035 dlen = 0; 3036 } else { 3037 struct user_mode_ctx *uctx = &to_user_ta_ctx(sess->ctx)->uctx; 3038 uint32_t flags = TEE_MEMORY_ACCESS_READ | 3039 TEE_MEMORY_ACCESS_WRITE | 3040 TEE_MEMORY_ACCESS_ANY_OWNER; 3041 3042 res = get_user_u64_as_size_t(&dlen, dst_len); 3043 if (res != TEE_SUCCESS) 3044 return res; 3045 3046 res = vm_check_access_rights(uctx, flags, (uaddr_t)dst, dlen); 3047 if (res != TEE_SUCCESS) 3048 return res; 3049 } 3050 3051 if (dlen < src_len) { 3052 res = TEE_ERROR_SHORT_BUFFER; 3053 goto out; 3054 } 3055 3056 if (src_len > 0) { 3057 /* Permit src_len == 0 to finalize the operation */ 3058 res = tee_do_cipher_update(cs->ctx, cs->algo, cs->mode, 3059 last_block, src, src_len, dst); 3060 } 3061 3062 if (last_block && cs->ctx_finalize != NULL) { 3063 cs->ctx_finalize(cs->ctx); 3064 cs->ctx_finalize = NULL; 3065 } 3066 3067 out: 3068 if ((res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) && 3069 dst_len != NULL) { 3070 TEE_Result res2; 3071 3072 res2 = put_user_u64(dst_len, src_len); 3073 if (res2 != TEE_SUCCESS) 3074 res = res2; 3075 } 3076 3077 return res; 3078 } 3079 3080 TEE_Result syscall_cipher_update(unsigned long state, const void *src, 3081 size_t src_len, void *dst, uint64_t *dst_len) 3082 { 3083 return tee_svc_cipher_update_helper(state, false /* last_block */, 3084 src, src_len, dst, dst_len); 3085 } 3086 3087 TEE_Result syscall_cipher_final(unsigned long state, const void *src, 3088 size_t src_len, void *dst, uint64_t *dst_len) 3089 { 3090 return tee_svc_cipher_update_helper(state, true /* last_block */, 3091 src, src_len, dst, dst_len); 3092 } 3093 3094 #if defined(CFG_CRYPTO_HKDF) 3095 static TEE_Result get_hkdf_params(const TEE_Attribute *params, 3096 uint32_t param_count, 3097 void **salt, size_t *salt_len, void **info, 3098 size_t *info_len, size_t *okm_len) 3099 { 3100 size_t n; 3101 enum { SALT = 0x1, LENGTH = 0x2, INFO = 0x4 }; 3102 uint8_t found = 0; 3103 3104 *salt = *info = NULL; 3105 *salt_len = *info_len = *okm_len = 0; 3106 3107 for (n = 0; n < param_count; n++) { 3108 switch (params[n].attributeID) { 3109 case TEE_ATTR_HKDF_SALT: 3110 if (!(found & SALT)) { 3111 *salt = params[n].content.ref.buffer; 3112 *salt_len = params[n].content.ref.length; 3113 found |= SALT; 3114 } 3115 break; 3116 case TEE_ATTR_HKDF_OKM_LENGTH: 3117 if (!(found & LENGTH)) { 3118 *okm_len = params[n].content.value.a; 3119 found |= LENGTH; 3120 } 3121 break; 3122 case TEE_ATTR_HKDF_INFO: 3123 if (!(found & INFO)) { 3124 *info = params[n].content.ref.buffer; 3125 *info_len = params[n].content.ref.length; 3126 found |= INFO; 3127 } 3128 break; 3129 default: 3130 /* Unexpected attribute */ 3131 return TEE_ERROR_BAD_PARAMETERS; 3132 } 3133 3134 } 3135 3136 if (!(found & LENGTH)) 3137 return TEE_ERROR_BAD_PARAMETERS; 3138 3139 return TEE_SUCCESS; 3140 } 3141 #endif 3142 3143 #if defined(CFG_CRYPTO_CONCAT_KDF) 3144 static TEE_Result get_concat_kdf_params(const TEE_Attribute *params, 3145 uint32_t param_count, 3146 void **other_info, 3147 size_t *other_info_len, 3148 size_t *derived_key_len) 3149 { 3150 size_t n; 3151 enum { LENGTH = 0x1, INFO = 0x2 }; 3152 uint8_t found = 0; 3153 3154 *other_info = NULL; 3155 *other_info_len = *derived_key_len = 0; 3156 3157 for (n = 0; n < param_count; n++) { 3158 switch (params[n].attributeID) { 3159 case TEE_ATTR_CONCAT_KDF_OTHER_INFO: 3160 if (!(found & INFO)) { 3161 *other_info = params[n].content.ref.buffer; 3162 *other_info_len = params[n].content.ref.length; 3163 found |= INFO; 3164 } 3165 break; 3166 case TEE_ATTR_CONCAT_KDF_DKM_LENGTH: 3167 if (!(found & LENGTH)) { 3168 *derived_key_len = params[n].content.value.a; 3169 found |= LENGTH; 3170 } 3171 break; 3172 default: 3173 /* Unexpected attribute */ 3174 return TEE_ERROR_BAD_PARAMETERS; 3175 } 3176 } 3177 3178 if (!(found & LENGTH)) 3179 return TEE_ERROR_BAD_PARAMETERS; 3180 3181 return TEE_SUCCESS; 3182 } 3183 #endif 3184 3185 #if defined(CFG_CRYPTO_PBKDF2) 3186 static TEE_Result get_pbkdf2_params(const TEE_Attribute *params, 3187 uint32_t param_count, void **salt, 3188 size_t *salt_len, size_t *derived_key_len, 3189 size_t *iteration_count) 3190 { 3191 size_t n; 3192 enum { SALT = 0x1, LENGTH = 0x2, COUNT = 0x4 }; 3193 uint8_t found = 0; 3194 3195 *salt = NULL; 3196 *salt_len = *derived_key_len = *iteration_count = 0; 3197 3198 for (n = 0; n < param_count; n++) { 3199 switch (params[n].attributeID) { 3200 case TEE_ATTR_PBKDF2_SALT: 3201 if (!(found & SALT)) { 3202 *salt = params[n].content.ref.buffer; 3203 *salt_len = params[n].content.ref.length; 3204 found |= SALT; 3205 } 3206 break; 3207 case TEE_ATTR_PBKDF2_DKM_LENGTH: 3208 if (!(found & LENGTH)) { 3209 *derived_key_len = params[n].content.value.a; 3210 found |= LENGTH; 3211 } 3212 break; 3213 case TEE_ATTR_PBKDF2_ITERATION_COUNT: 3214 if (!(found & COUNT)) { 3215 *iteration_count = params[n].content.value.a; 3216 found |= COUNT; 3217 } 3218 break; 3219 default: 3220 /* Unexpected attribute */ 3221 return TEE_ERROR_BAD_PARAMETERS; 3222 } 3223 } 3224 3225 if ((found & (LENGTH|COUNT)) != (LENGTH|COUNT)) 3226 return TEE_ERROR_BAD_PARAMETERS; 3227 3228 return TEE_SUCCESS; 3229 } 3230 #endif 3231 3232 #if defined(CFG_CRYPTO_SM2_KEP) 3233 static TEE_Result get_sm2_kep_params(const TEE_Attribute *params, 3234 uint32_t param_count, 3235 struct ecc_public_key *peer_key, 3236 struct ecc_public_key *peer_eph_key, 3237 struct sm2_kep_parms *kep_parms) 3238 { 3239 TEE_Result res = TEE_ERROR_GENERIC; 3240 size_t n; 3241 enum { 3242 IS_INITIATOR, 3243 PEER_KEY_X, 3244 PEER_KEY_Y, 3245 PEER_EPH_KEY_X, 3246 PEER_EPH_KEY_Y, 3247 INITIATOR_ID, 3248 RESPONDER_ID, 3249 }; 3250 uint8_t mandatory = BIT(IS_INITIATOR) | BIT(PEER_KEY_X) | 3251 BIT(PEER_KEY_Y) | BIT(PEER_EPH_KEY_X) | BIT(PEER_EPH_KEY_Y) | 3252 BIT(INITIATOR_ID) | BIT(RESPONDER_ID); 3253 uint8_t found = 0; 3254 3255 res = crypto_acipher_alloc_ecc_public_key(peer_key, 3256 TEE_TYPE_SM2_KEP_PUBLIC_KEY, 3257 256); 3258 if (res) 3259 return res; 3260 3261 res = crypto_acipher_alloc_ecc_public_key(peer_eph_key, 3262 TEE_TYPE_SM2_KEP_PUBLIC_KEY, 3263 256); 3264 if (res) 3265 goto out_p; 3266 3267 peer_key->curve = TEE_ECC_CURVE_SM2; 3268 peer_eph_key->curve = TEE_ECC_CURVE_SM2; 3269 3270 for (n = 0; n < param_count; n++) { 3271 const TEE_Attribute *p = ¶ms[n]; 3272 3273 switch (p->attributeID) { 3274 case TEE_ATTR_SM2_KEP_USER: 3275 kep_parms->is_initiator = !p->content.value.a; 3276 found |= BIT(IS_INITIATOR); 3277 break; 3278 case TEE_ATTR_ECC_PUBLIC_VALUE_X: 3279 crypto_bignum_bin2bn(p->content.ref.buffer, 3280 p->content.ref.length, 3281 peer_key->x); 3282 found |= BIT(PEER_KEY_X); 3283 break; 3284 case TEE_ATTR_ECC_PUBLIC_VALUE_Y: 3285 crypto_bignum_bin2bn(p->content.ref.buffer, 3286 p->content.ref.length, 3287 peer_key->y); 3288 found |= BIT(PEER_KEY_Y); 3289 break; 3290 case TEE_ATTR_ECC_EPHEMERAL_PUBLIC_VALUE_X: 3291 crypto_bignum_bin2bn(p->content.ref.buffer, 3292 p->content.ref.length, 3293 peer_eph_key->x); 3294 found |= BIT(PEER_EPH_KEY_X); 3295 break; 3296 case TEE_ATTR_ECC_EPHEMERAL_PUBLIC_VALUE_Y: 3297 crypto_bignum_bin2bn(p->content.ref.buffer, 3298 p->content.ref.length, 3299 peer_eph_key->y); 3300 found |= BIT(PEER_EPH_KEY_Y); 3301 break; 3302 case TEE_ATTR_SM2_ID_INITIATOR: 3303 kep_parms->initiator_id = p->content.ref.buffer; 3304 kep_parms->initiator_id_len = p->content.ref.length; 3305 found |= BIT(INITIATOR_ID); 3306 break; 3307 case TEE_ATTR_SM2_ID_RESPONDER: 3308 kep_parms->responder_id = p->content.ref.buffer; 3309 kep_parms->responder_id_len = p->content.ref.length; 3310 found |= BIT(RESPONDER_ID); 3311 break; 3312 case TEE_ATTR_SM2_KEP_CONFIRMATION_IN: 3313 kep_parms->conf_in = p->content.ref.buffer; 3314 kep_parms->conf_in_len = p->content.ref.length; 3315 break; 3316 case TEE_ATTR_SM2_KEP_CONFIRMATION_OUT: 3317 kep_parms->conf_out = p->content.ref.buffer; 3318 kep_parms->conf_out_len = p->content.ref.length; 3319 break; 3320 default: 3321 /* Unexpected attribute */ 3322 res = TEE_ERROR_BAD_PARAMETERS; 3323 goto out; 3324 } 3325 } 3326 3327 if ((found & mandatory) != mandatory) { 3328 res = TEE_ERROR_BAD_PARAMETERS; 3329 goto out; 3330 } 3331 3332 return TEE_SUCCESS; 3333 out: 3334 crypto_acipher_free_ecc_public_key(peer_eph_key); 3335 out_p: 3336 crypto_acipher_free_ecc_public_key(peer_key); 3337 return res; 3338 } 3339 #endif 3340 3341 TEE_Result syscall_cryp_derive_key(unsigned long state, 3342 const struct utee_attribute *usr_params, 3343 unsigned long param_count, unsigned long derived_key) 3344 { 3345 struct ts_session *sess = ts_get_current_session(); 3346 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 3347 TEE_Result res = TEE_ERROR_NOT_SUPPORTED; 3348 struct tee_obj *ko = NULL; 3349 struct tee_obj *so = NULL; 3350 struct tee_cryp_state *cs = NULL; 3351 struct tee_cryp_obj_secret *sk = NULL; 3352 const struct tee_cryp_obj_type_props *type_props = NULL; 3353 TEE_Attribute *params = NULL; 3354 size_t alloc_size = 0; 3355 3356 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 3357 if (res != TEE_SUCCESS) 3358 return res; 3359 3360 if (MUL_OVERFLOW(sizeof(TEE_Attribute), param_count, &alloc_size)) 3361 return TEE_ERROR_OVERFLOW; 3362 3363 params = malloc(alloc_size); 3364 if (!params) 3365 return TEE_ERROR_OUT_OF_MEMORY; 3366 res = copy_in_attrs(utc, usr_params, param_count, params); 3367 if (res != TEE_SUCCESS) 3368 goto out; 3369 3370 /* Get key set in operation */ 3371 res = tee_obj_get(utc, cs->key1, &ko); 3372 if (res != TEE_SUCCESS) 3373 goto out; 3374 3375 res = tee_obj_get(utc, uref_to_vaddr(derived_key), &so); 3376 if (res != TEE_SUCCESS) 3377 goto out; 3378 3379 /* Find information needed about the object to initialize */ 3380 sk = so->attr; 3381 3382 /* Find description of object */ 3383 type_props = tee_svc_find_type_props(so->info.objectType); 3384 if (!type_props) { 3385 res = TEE_ERROR_NOT_SUPPORTED; 3386 goto out; 3387 } 3388 3389 if (cs->algo == TEE_ALG_DH_DERIVE_SHARED_SECRET) { 3390 struct bignum *pub = NULL; 3391 struct bignum *ss = NULL; 3392 size_t bin_size = 0; 3393 3394 if (param_count != 1 || 3395 params[0].attributeID != TEE_ATTR_DH_PUBLIC_VALUE) { 3396 res = TEE_ERROR_BAD_PARAMETERS; 3397 goto out; 3398 } 3399 3400 bin_size = params[0].content.ref.length; 3401 3402 if (MUL_OVERFLOW(bin_size, 8, &alloc_size)) { 3403 res = TEE_ERROR_OVERFLOW; 3404 goto out; 3405 } 3406 3407 pub = crypto_bignum_allocate(alloc_size); 3408 ss = crypto_bignum_allocate(alloc_size); 3409 if (pub && ss) { 3410 crypto_bignum_bin2bn(params[0].content.ref.buffer, 3411 bin_size, pub); 3412 res = crypto_acipher_dh_shared_secret(ko->attr, 3413 pub, ss); 3414 if (res == TEE_SUCCESS) { 3415 sk->key_size = crypto_bignum_num_bytes(ss); 3416 crypto_bignum_bn2bin(ss, (uint8_t *)(sk + 1)); 3417 so->info.handleFlags |= 3418 TEE_HANDLE_FLAG_INITIALIZED; 3419 set_attribute(so, type_props, 3420 TEE_ATTR_SECRET_VALUE); 3421 } 3422 } else { 3423 res = TEE_ERROR_OUT_OF_MEMORY; 3424 } 3425 crypto_bignum_free(pub); 3426 crypto_bignum_free(ss); 3427 } else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_ECDH) { 3428 struct ecc_public_key key_public; 3429 uint8_t *pt_secret; 3430 unsigned long pt_secret_len; 3431 uint32_t key_type = TEE_TYPE_ECDH_PUBLIC_KEY; 3432 3433 if (param_count != 2 || 3434 params[0].attributeID != TEE_ATTR_ECC_PUBLIC_VALUE_X || 3435 params[1].attributeID != TEE_ATTR_ECC_PUBLIC_VALUE_Y) { 3436 res = TEE_ERROR_BAD_PARAMETERS; 3437 goto out; 3438 } 3439 3440 switch (cs->algo) { 3441 case TEE_ALG_ECDH_P192: 3442 alloc_size = 192; 3443 break; 3444 case TEE_ALG_ECDH_P224: 3445 alloc_size = 224; 3446 break; 3447 case TEE_ALG_ECDH_P256: 3448 alloc_size = 256; 3449 break; 3450 case TEE_ALG_ECDH_P384: 3451 alloc_size = 384; 3452 break; 3453 case TEE_ALG_ECDH_P521: 3454 alloc_size = 521; 3455 break; 3456 default: 3457 res = TEE_ERROR_NOT_IMPLEMENTED; 3458 goto out; 3459 } 3460 3461 /* Create the public key */ 3462 res = crypto_acipher_alloc_ecc_public_key(&key_public, key_type, 3463 alloc_size); 3464 if (res != TEE_SUCCESS) 3465 goto out; 3466 key_public.curve = ((struct ecc_keypair *)ko->attr)->curve; 3467 crypto_bignum_bin2bn(params[0].content.ref.buffer, 3468 params[0].content.ref.length, 3469 key_public.x); 3470 crypto_bignum_bin2bn(params[1].content.ref.buffer, 3471 params[1].content.ref.length, 3472 key_public.y); 3473 3474 pt_secret = (uint8_t *)(sk + 1); 3475 pt_secret_len = sk->alloc_size; 3476 res = crypto_acipher_ecc_shared_secret(ko->attr, &key_public, 3477 pt_secret, 3478 &pt_secret_len); 3479 3480 if (res == TEE_SUCCESS) { 3481 sk->key_size = pt_secret_len; 3482 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 3483 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 3484 } 3485 3486 /* free the public key */ 3487 crypto_acipher_free_ecc_public_key(&key_public); 3488 } 3489 #if defined(CFG_CRYPTO_HKDF) 3490 else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_HKDF) { 3491 void *salt, *info; 3492 size_t salt_len, info_len, okm_len; 3493 uint32_t hash_id = TEE_ALG_GET_DIGEST_HASH(cs->algo); 3494 struct tee_cryp_obj_secret *ik = ko->attr; 3495 const uint8_t *ikm = (const uint8_t *)(ik + 1); 3496 3497 res = get_hkdf_params(params, param_count, &salt, &salt_len, 3498 &info, &info_len, &okm_len); 3499 if (res != TEE_SUCCESS) 3500 goto out; 3501 3502 /* Requested size must fit into the output object's buffer */ 3503 if (okm_len > ik->alloc_size) { 3504 res = TEE_ERROR_BAD_PARAMETERS; 3505 goto out; 3506 } 3507 3508 res = tee_cryp_hkdf(hash_id, ikm, ik->key_size, salt, salt_len, 3509 info, info_len, (uint8_t *)(sk + 1), 3510 okm_len); 3511 if (res == TEE_SUCCESS) { 3512 sk->key_size = okm_len; 3513 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 3514 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 3515 } 3516 } 3517 #endif 3518 #if defined(CFG_CRYPTO_CONCAT_KDF) 3519 else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_CONCAT_KDF) { 3520 void *info; 3521 size_t info_len, derived_key_len; 3522 uint32_t hash_id = TEE_ALG_GET_DIGEST_HASH(cs->algo); 3523 struct tee_cryp_obj_secret *ss = ko->attr; 3524 const uint8_t *shared_secret = (const uint8_t *)(ss + 1); 3525 3526 res = get_concat_kdf_params(params, param_count, &info, 3527 &info_len, &derived_key_len); 3528 if (res != TEE_SUCCESS) 3529 goto out; 3530 3531 /* Requested size must fit into the output object's buffer */ 3532 if (derived_key_len > ss->alloc_size) { 3533 res = TEE_ERROR_BAD_PARAMETERS; 3534 goto out; 3535 } 3536 3537 res = tee_cryp_concat_kdf(hash_id, shared_secret, ss->key_size, 3538 info, info_len, (uint8_t *)(sk + 1), 3539 derived_key_len); 3540 if (res == TEE_SUCCESS) { 3541 sk->key_size = derived_key_len; 3542 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 3543 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 3544 } 3545 } 3546 #endif 3547 #if defined(CFG_CRYPTO_PBKDF2) 3548 else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_PBKDF2) { 3549 void *salt; 3550 size_t salt_len, iteration_count, derived_key_len; 3551 uint32_t hash_id = TEE_ALG_GET_DIGEST_HASH(cs->algo); 3552 struct tee_cryp_obj_secret *ss = ko->attr; 3553 const uint8_t *password = (const uint8_t *)(ss + 1); 3554 3555 res = get_pbkdf2_params(params, param_count, &salt, &salt_len, 3556 &derived_key_len, &iteration_count); 3557 if (res != TEE_SUCCESS) 3558 goto out; 3559 3560 /* Requested size must fit into the output object's buffer */ 3561 if (derived_key_len > ss->alloc_size) { 3562 res = TEE_ERROR_BAD_PARAMETERS; 3563 goto out; 3564 } 3565 3566 res = tee_cryp_pbkdf2(hash_id, password, ss->key_size, salt, 3567 salt_len, iteration_count, 3568 (uint8_t *)(sk + 1), derived_key_len); 3569 if (res == TEE_SUCCESS) { 3570 sk->key_size = derived_key_len; 3571 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 3572 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 3573 } 3574 } 3575 #endif 3576 #if defined(CFG_CRYPTO_SM2_KEP) 3577 else if (cs->algo == TEE_ALG_SM2_KEP) { 3578 struct ecc_public_key peer_eph_key = { }; 3579 struct ecc_public_key peer_key = { }; 3580 struct sm2_kep_parms kep_parms = { 3581 .out = (uint8_t *)(sk + 1), 3582 .out_len = so->info.maxObjectSize, 3583 }; 3584 struct tee_obj *ko2 = NULL; 3585 3586 res = tee_obj_get(utc, cs->key2, &ko2); 3587 if (res != TEE_SUCCESS) 3588 goto out; 3589 3590 res = get_sm2_kep_params(params, param_count, &peer_key, 3591 &peer_eph_key, &kep_parms); 3592 if (res != TEE_SUCCESS) 3593 goto out; 3594 3595 /* 3596 * key1 is our private keypair, key2 is our ephemeral public key 3597 */ 3598 res = crypto_acipher_sm2_kep_derive(ko->attr, /* key1 */ 3599 ko2->attr, /* key2 */ 3600 &peer_key, &peer_eph_key, 3601 &kep_parms); 3602 3603 if (res == TEE_SUCCESS) { 3604 sk->key_size = kep_parms.out_len; 3605 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 3606 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 3607 } 3608 crypto_acipher_free_ecc_public_key(&peer_key); 3609 crypto_acipher_free_ecc_public_key(&peer_eph_key); 3610 } 3611 #endif 3612 #if defined(CFG_CRYPTO_X25519) 3613 else if (cs->algo == TEE_ALG_X25519) { 3614 uint8_t *x25519_pub_key = NULL; 3615 uint8_t *pt_secret = NULL; 3616 unsigned long pt_secret_len = 0; 3617 3618 if (param_count != 1 || 3619 params[0].attributeID != TEE_ATTR_X25519_PUBLIC_VALUE) { 3620 res = TEE_ERROR_BAD_PARAMETERS; 3621 goto out; 3622 } 3623 3624 /* X25519 public key size is 32 bytes */ 3625 if (params[0].content.ref.length != KEY_SIZE_BYTES_25519) { 3626 res = TEE_ERROR_BAD_PARAMETERS; 3627 goto out; 3628 } 3629 3630 /* Set the public key */ 3631 x25519_pub_key = params[0].content.ref.buffer; 3632 3633 pt_secret = (uint8_t *)(sk + 1); 3634 pt_secret_len = sk->alloc_size; 3635 res = crypto_acipher_x25519_shared_secret(ko->attr, 3636 x25519_pub_key, 3637 pt_secret, 3638 &pt_secret_len); 3639 3640 if (res == TEE_SUCCESS) { 3641 sk->key_size = pt_secret_len; 3642 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 3643 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 3644 } 3645 } 3646 #endif 3647 else 3648 res = TEE_ERROR_NOT_SUPPORTED; 3649 3650 out: 3651 free_wipe(params); 3652 return res; 3653 } 3654 3655 TEE_Result syscall_cryp_random_number_generate(void *buf, size_t blen) 3656 { 3657 struct ts_session *sess = ts_get_current_session(); 3658 TEE_Result res = TEE_SUCCESS; 3659 3660 buf = memtag_strip_tag(buf); 3661 3662 res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 3663 TEE_MEMORY_ACCESS_WRITE, 3664 (uaddr_t)buf, blen); 3665 if (res != TEE_SUCCESS) 3666 return res; 3667 3668 res = crypto_rng_read(buf, blen); 3669 if (res != TEE_SUCCESS) 3670 return res; 3671 3672 return res; 3673 } 3674 3675 TEE_Result syscall_authenc_init(unsigned long state, const void *nonce, 3676 size_t nonce_len, size_t tag_len, 3677 size_t aad_len, size_t payload_len) 3678 { 3679 struct ts_session *sess = ts_get_current_session(); 3680 struct tee_cryp_obj_secret *key = NULL; 3681 struct tee_cryp_state *cs = NULL; 3682 TEE_Result res = TEE_SUCCESS; 3683 struct tee_obj *o = NULL; 3684 3685 nonce = memtag_strip_tag_const(nonce); 3686 3687 res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 3688 TEE_MEMORY_ACCESS_READ | 3689 TEE_MEMORY_ACCESS_ANY_OWNER, 3690 (uaddr_t)nonce, nonce_len); 3691 if (res != TEE_SUCCESS) 3692 return res; 3693 3694 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 3695 if (res != TEE_SUCCESS) 3696 return res; 3697 3698 res = tee_obj_get(to_user_ta_ctx(sess->ctx), cs->key1, &o); 3699 if (res != TEE_SUCCESS) 3700 return res; 3701 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 3702 return TEE_ERROR_BAD_PARAMETERS; 3703 3704 key = o->attr; 3705 res = crypto_authenc_init(cs->ctx, cs->mode, (uint8_t *)(key + 1), 3706 key->key_size, nonce, nonce_len, tag_len, 3707 aad_len, payload_len); 3708 if (res != TEE_SUCCESS) 3709 return res; 3710 3711 cs->ctx_finalize = crypto_authenc_final; 3712 cs->state = CRYP_STATE_INITIALIZED; 3713 3714 return TEE_SUCCESS; 3715 } 3716 3717 TEE_Result syscall_authenc_update_aad(unsigned long state, 3718 const void *aad_data, size_t aad_data_len) 3719 { 3720 struct ts_session *sess = ts_get_current_session(); 3721 TEE_Result res = TEE_SUCCESS; 3722 struct tee_cryp_state *cs = NULL; 3723 3724 aad_data = memtag_strip_tag_const(aad_data); 3725 3726 res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 3727 TEE_MEMORY_ACCESS_READ | 3728 TEE_MEMORY_ACCESS_ANY_OWNER, 3729 (uaddr_t)aad_data, aad_data_len); 3730 if (res != TEE_SUCCESS) 3731 return res; 3732 3733 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 3734 if (res != TEE_SUCCESS) 3735 return res; 3736 3737 if (cs->state != CRYP_STATE_INITIALIZED) 3738 return TEE_ERROR_BAD_STATE; 3739 3740 if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_AE) 3741 return TEE_ERROR_BAD_STATE; 3742 3743 res = crypto_authenc_update_aad(cs->ctx, cs->mode, aad_data, 3744 aad_data_len); 3745 if (res != TEE_SUCCESS) 3746 return res; 3747 3748 return TEE_SUCCESS; 3749 } 3750 3751 TEE_Result syscall_authenc_update_payload(unsigned long state, 3752 const void *src_data, 3753 size_t src_len, void *dst_data, 3754 uint64_t *dst_len) 3755 { 3756 struct ts_session *sess = ts_get_current_session(); 3757 struct tee_cryp_state *cs = NULL; 3758 TEE_Result res = TEE_SUCCESS; 3759 size_t dlen = 0; 3760 3761 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 3762 if (res != TEE_SUCCESS) 3763 return res; 3764 3765 if (cs->state != CRYP_STATE_INITIALIZED) 3766 return TEE_ERROR_BAD_STATE; 3767 3768 if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_AE) 3769 return TEE_ERROR_BAD_STATE; 3770 3771 src_data = memtag_strip_tag_const(src_data); 3772 dst_data = memtag_strip_tag(dst_data); 3773 3774 res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 3775 TEE_MEMORY_ACCESS_READ | 3776 TEE_MEMORY_ACCESS_ANY_OWNER, 3777 (uaddr_t)src_data, src_len); 3778 if (res != TEE_SUCCESS) 3779 return res; 3780 3781 res = get_user_u64_as_size_t(&dlen, dst_len); 3782 if (res != TEE_SUCCESS) 3783 return res; 3784 3785 res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 3786 TEE_MEMORY_ACCESS_READ | 3787 TEE_MEMORY_ACCESS_WRITE | 3788 TEE_MEMORY_ACCESS_ANY_OWNER, 3789 (uaddr_t)dst_data, dlen); 3790 if (res != TEE_SUCCESS) 3791 return res; 3792 3793 if (dlen < src_len) { 3794 res = TEE_ERROR_SHORT_BUFFER; 3795 goto out; 3796 } 3797 3798 res = crypto_authenc_update_payload(cs->ctx, cs->mode, src_data, 3799 src_len, dst_data, &dlen); 3800 out: 3801 if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) { 3802 TEE_Result res2 = put_user_u64(dst_len, dlen); 3803 3804 if (res2 != TEE_SUCCESS) 3805 res = res2; 3806 } 3807 3808 return res; 3809 } 3810 3811 TEE_Result syscall_authenc_enc_final(unsigned long state, const void *src_data, 3812 size_t src_len, void *dst_data, 3813 uint64_t *dst_len, void *tag, 3814 uint64_t *tag_len) 3815 { 3816 struct ts_session *sess = ts_get_current_session(); 3817 struct user_mode_ctx *uctx = &to_user_ta_ctx(sess->ctx)->uctx; 3818 struct tee_cryp_state *cs = NULL; 3819 TEE_Result res = TEE_SUCCESS; 3820 size_t dlen = 0; 3821 size_t tlen = 0; 3822 3823 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 3824 if (res != TEE_SUCCESS) 3825 return res; 3826 3827 if (cs->state != CRYP_STATE_INITIALIZED) 3828 return TEE_ERROR_BAD_STATE; 3829 3830 if (cs->mode != TEE_MODE_ENCRYPT) 3831 return TEE_ERROR_BAD_PARAMETERS; 3832 3833 if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_AE) 3834 return TEE_ERROR_BAD_STATE; 3835 3836 src_data = memtag_strip_tag_const(src_data); 3837 dst_data = memtag_strip_tag(dst_data); 3838 tag = memtag_strip_tag(tag); 3839 3840 res = vm_check_access_rights(uctx, 3841 TEE_MEMORY_ACCESS_READ | 3842 TEE_MEMORY_ACCESS_ANY_OWNER, 3843 (uaddr_t)src_data, src_len); 3844 if (res != TEE_SUCCESS) 3845 return res; 3846 3847 if (!dst_len) { 3848 dlen = 0; 3849 } else { 3850 res = get_user_u64_as_size_t(&dlen, dst_len); 3851 if (res != TEE_SUCCESS) 3852 return res; 3853 3854 res = vm_check_access_rights(uctx, 3855 TEE_MEMORY_ACCESS_READ | 3856 TEE_MEMORY_ACCESS_WRITE | 3857 TEE_MEMORY_ACCESS_ANY_OWNER, 3858 (uaddr_t)dst_data, dlen); 3859 if (res != TEE_SUCCESS) 3860 return res; 3861 } 3862 3863 if (dlen < src_len) { 3864 res = TEE_ERROR_SHORT_BUFFER; 3865 goto out; 3866 } 3867 3868 res = get_user_u64_as_size_t(&tlen, tag_len); 3869 if (res != TEE_SUCCESS) 3870 return res; 3871 3872 res = vm_check_access_rights(uctx, 3873 TEE_MEMORY_ACCESS_READ | 3874 TEE_MEMORY_ACCESS_WRITE | 3875 TEE_MEMORY_ACCESS_ANY_OWNER, 3876 (uaddr_t)tag, tlen); 3877 if (res != TEE_SUCCESS) 3878 return res; 3879 3880 res = crypto_authenc_enc_final(cs->ctx, src_data, src_len, dst_data, 3881 &dlen, tag, &tlen); 3882 3883 out: 3884 if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) { 3885 TEE_Result res2 = TEE_SUCCESS; 3886 3887 if (dst_len != NULL) { 3888 res2 = put_user_u64(dst_len, dlen); 3889 if (res2 != TEE_SUCCESS) 3890 return res2; 3891 } 3892 3893 res2 = put_user_u64(tag_len, tlen); 3894 if (res2 != TEE_SUCCESS) 3895 return res2; 3896 } 3897 3898 return res; 3899 } 3900 3901 TEE_Result syscall_authenc_dec_final(unsigned long state, 3902 const void *src_data, size_t src_len, void *dst_data, 3903 uint64_t *dst_len, const void *tag, size_t tag_len) 3904 { 3905 struct ts_session *sess = ts_get_current_session(); 3906 struct user_mode_ctx *uctx = &to_user_ta_ctx(sess->ctx)->uctx; 3907 struct tee_cryp_state *cs = NULL; 3908 TEE_Result res = TEE_SUCCESS; 3909 size_t dlen = 0; 3910 3911 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 3912 if (res != TEE_SUCCESS) 3913 return res; 3914 3915 if (cs->state != CRYP_STATE_INITIALIZED) 3916 return TEE_ERROR_BAD_STATE; 3917 3918 if (cs->mode != TEE_MODE_DECRYPT) 3919 return TEE_ERROR_BAD_PARAMETERS; 3920 3921 if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_AE) 3922 return TEE_ERROR_BAD_STATE; 3923 3924 src_data = memtag_strip_tag_const(src_data); 3925 dst_data = memtag_strip_tag(dst_data); 3926 tag = memtag_strip_tag_const(tag); 3927 3928 res = vm_check_access_rights(uctx, 3929 TEE_MEMORY_ACCESS_READ | 3930 TEE_MEMORY_ACCESS_ANY_OWNER, 3931 (uaddr_t)src_data, src_len); 3932 if (res != TEE_SUCCESS) 3933 return res; 3934 3935 if (!dst_len) { 3936 dlen = 0; 3937 } else { 3938 res = get_user_u64_as_size_t(&dlen, dst_len); 3939 if (res != TEE_SUCCESS) 3940 return res; 3941 3942 res = vm_check_access_rights(uctx, 3943 TEE_MEMORY_ACCESS_READ | 3944 TEE_MEMORY_ACCESS_WRITE | 3945 TEE_MEMORY_ACCESS_ANY_OWNER, 3946 (uaddr_t)dst_data, dlen); 3947 if (res != TEE_SUCCESS) 3948 return res; 3949 } 3950 3951 if (dlen < src_len) { 3952 res = TEE_ERROR_SHORT_BUFFER; 3953 goto out; 3954 } 3955 3956 res = vm_check_access_rights(uctx, TEE_MEMORY_ACCESS_READ, 3957 (uaddr_t)tag, tag_len); 3958 if (res != TEE_SUCCESS) 3959 return res; 3960 3961 res = crypto_authenc_dec_final(cs->ctx, src_data, src_len, dst_data, 3962 &dlen, tag, tag_len); 3963 3964 out: 3965 if ((res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) && 3966 dst_len != NULL) { 3967 TEE_Result res2 = put_user_u64(dst_len, dlen); 3968 3969 if (res2 != TEE_SUCCESS) 3970 return res2; 3971 } 3972 3973 return res; 3974 } 3975 3976 static int pkcs1_get_salt_len(const TEE_Attribute *params, uint32_t num_params, 3977 size_t default_len) 3978 { 3979 size_t n; 3980 3981 assert(default_len < INT_MAX); 3982 3983 for (n = 0; n < num_params; n++) { 3984 if (params[n].attributeID == TEE_ATTR_RSA_PSS_SALT_LENGTH) { 3985 if (params[n].content.value.a < INT_MAX) 3986 return params[n].content.value.a; 3987 break; 3988 } 3989 } 3990 /* 3991 * If salt length isn't provided use the default value which is 3992 * the length of the digest. 3993 */ 3994 return default_len; 3995 } 3996 3997 TEE_Result syscall_asymm_operate(unsigned long state, 3998 const struct utee_attribute *usr_params, 3999 size_t num_params, const void *src_data, size_t src_len, 4000 void *dst_data, uint64_t *dst_len) 4001 { 4002 struct ts_session *sess = ts_get_current_session(); 4003 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 4004 TEE_Result res = TEE_SUCCESS; 4005 struct tee_cryp_state *cs = NULL; 4006 size_t dlen = 0; 4007 struct tee_obj *o = NULL; 4008 void *label = NULL; 4009 size_t label_len = 0; 4010 size_t n = 0; 4011 int salt_len = 0; 4012 TEE_Attribute *params = NULL; 4013 size_t alloc_size = 0; 4014 4015 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 4016 if (res != TEE_SUCCESS) 4017 return res; 4018 4019 src_data = memtag_strip_tag_const(src_data); 4020 dst_data = memtag_strip_tag(dst_data); 4021 4022 res = vm_check_access_rights(&utc->uctx, 4023 TEE_MEMORY_ACCESS_READ | 4024 TEE_MEMORY_ACCESS_ANY_OWNER, 4025 (uaddr_t)src_data, src_len); 4026 if (res != TEE_SUCCESS) 4027 return res; 4028 4029 res = get_user_u64_as_size_t(&dlen, dst_len); 4030 if (res != TEE_SUCCESS) 4031 return res; 4032 4033 res = vm_check_access_rights(&utc->uctx, 4034 TEE_MEMORY_ACCESS_READ | 4035 TEE_MEMORY_ACCESS_WRITE | 4036 TEE_MEMORY_ACCESS_ANY_OWNER, 4037 (uaddr_t)dst_data, dlen); 4038 if (res != TEE_SUCCESS) 4039 return res; 4040 4041 if (MUL_OVERFLOW(sizeof(TEE_Attribute), num_params, &alloc_size)) 4042 return TEE_ERROR_OVERFLOW; 4043 4044 params = malloc(alloc_size); 4045 if (!params) 4046 return TEE_ERROR_OUT_OF_MEMORY; 4047 res = copy_in_attrs(utc, usr_params, num_params, params); 4048 if (res != TEE_SUCCESS) 4049 goto out; 4050 4051 res = tee_obj_get(utc, cs->key1, &o); 4052 if (res != TEE_SUCCESS) 4053 goto out; 4054 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 4055 res = TEE_ERROR_GENERIC; 4056 goto out; 4057 } 4058 4059 switch (cs->algo) { 4060 case TEE_ALG_RSA_NOPAD: 4061 if (cs->mode == TEE_MODE_ENCRYPT) { 4062 res = crypto_acipher_rsanopad_encrypt(o->attr, src_data, 4063 src_len, dst_data, 4064 &dlen); 4065 } else if (cs->mode == TEE_MODE_DECRYPT) { 4066 res = crypto_acipher_rsanopad_decrypt(o->attr, src_data, 4067 src_len, dst_data, 4068 &dlen); 4069 } else { 4070 /* 4071 * We will panic because "the mode is not compatible 4072 * with the function" 4073 */ 4074 res = TEE_ERROR_GENERIC; 4075 } 4076 break; 4077 4078 case TEE_ALG_SM2_PKE: 4079 if (cs->mode == TEE_MODE_ENCRYPT) { 4080 res = crypto_acipher_sm2_pke_encrypt(o->attr, src_data, 4081 src_len, dst_data, 4082 &dlen); 4083 } else if (cs->mode == TEE_MODE_DECRYPT) { 4084 res = crypto_acipher_sm2_pke_decrypt(o->attr, src_data, 4085 src_len, dst_data, 4086 &dlen); 4087 } else { 4088 res = TEE_ERROR_GENERIC; 4089 } 4090 break; 4091 4092 case TEE_ALG_RSAES_PKCS1_V1_5: 4093 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1: 4094 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224: 4095 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256: 4096 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384: 4097 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512: 4098 for (n = 0; n < num_params; n++) { 4099 if (params[n].attributeID == TEE_ATTR_RSA_OAEP_LABEL) { 4100 label = params[n].content.ref.buffer; 4101 label_len = params[n].content.ref.length; 4102 break; 4103 } 4104 } 4105 4106 if (cs->mode == TEE_MODE_ENCRYPT) { 4107 res = crypto_acipher_rsaes_encrypt(cs->algo, o->attr, 4108 label, label_len, 4109 src_data, src_len, 4110 dst_data, &dlen); 4111 } else if (cs->mode == TEE_MODE_DECRYPT) { 4112 res = crypto_acipher_rsaes_decrypt( 4113 cs->algo, o->attr, label, label_len, 4114 src_data, src_len, dst_data, &dlen); 4115 } else { 4116 res = TEE_ERROR_BAD_PARAMETERS; 4117 } 4118 break; 4119 4120 #if defined(CFG_CRYPTO_RSASSA_NA1) 4121 case TEE_ALG_RSASSA_PKCS1_V1_5: 4122 #endif 4123 case TEE_ALG_RSASSA_PKCS1_V1_5_MD5: 4124 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1: 4125 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224: 4126 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256: 4127 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384: 4128 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512: 4129 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1: 4130 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224: 4131 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256: 4132 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384: 4133 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512: 4134 if (cs->mode != TEE_MODE_SIGN) { 4135 res = TEE_ERROR_BAD_PARAMETERS; 4136 break; 4137 } 4138 salt_len = pkcs1_get_salt_len(params, num_params, src_len); 4139 res = crypto_acipher_rsassa_sign(cs->algo, o->attr, salt_len, 4140 src_data, src_len, dst_data, 4141 &dlen); 4142 break; 4143 4144 case TEE_ALG_DSA_SHA1: 4145 case TEE_ALG_DSA_SHA224: 4146 case TEE_ALG_DSA_SHA256: 4147 res = crypto_acipher_dsa_sign(cs->algo, o->attr, src_data, 4148 src_len, dst_data, &dlen); 4149 break; 4150 4151 case TEE_ALG_ED25519: 4152 res = tee_svc_obj_ed25519_sign(o->attr, src_data, src_len, 4153 dst_data, &dlen, params, 4154 num_params); 4155 break; 4156 4157 case TEE_ALG_ECDSA_P192: 4158 case TEE_ALG_ECDSA_P224: 4159 case TEE_ALG_ECDSA_P256: 4160 case TEE_ALG_ECDSA_P384: 4161 case TEE_ALG_ECDSA_P521: 4162 case TEE_ALG_SM2_DSA_SM3: 4163 res = crypto_acipher_ecc_sign(cs->algo, o->attr, src_data, 4164 src_len, dst_data, &dlen); 4165 break; 4166 default: 4167 res = TEE_ERROR_BAD_PARAMETERS; 4168 break; 4169 } 4170 4171 out: 4172 free_wipe(params); 4173 4174 if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) { 4175 TEE_Result res2 = put_user_u64(dst_len, dlen); 4176 4177 if (res2 != TEE_SUCCESS) 4178 return res2; 4179 } 4180 4181 return res; 4182 } 4183 4184 TEE_Result syscall_asymm_verify(unsigned long state, 4185 const struct utee_attribute *usr_params, 4186 size_t num_params, const void *data, size_t data_len, 4187 const void *sig, size_t sig_len) 4188 { 4189 struct ts_session *sess = ts_get_current_session(); 4190 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 4191 struct tee_cryp_state *cs = NULL; 4192 TEE_Result res = TEE_SUCCESS; 4193 TEE_Attribute *params = NULL; 4194 struct tee_obj *o = NULL; 4195 size_t hash_size = 0; 4196 uint32_t hash_algo = 0; 4197 int salt_len = 0; 4198 size_t alloc_size = 0; 4199 4200 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 4201 if (res != TEE_SUCCESS) 4202 return res; 4203 4204 if (cs->mode != TEE_MODE_VERIFY) 4205 return TEE_ERROR_BAD_PARAMETERS; 4206 4207 data = memtag_strip_tag_const(data); 4208 sig = memtag_strip_tag_const(sig); 4209 4210 res = vm_check_access_rights(&utc->uctx, 4211 TEE_MEMORY_ACCESS_READ | 4212 TEE_MEMORY_ACCESS_ANY_OWNER, 4213 (uaddr_t)data, data_len); 4214 if (res != TEE_SUCCESS) 4215 return res; 4216 4217 res = vm_check_access_rights(&utc->uctx, 4218 TEE_MEMORY_ACCESS_READ | 4219 TEE_MEMORY_ACCESS_ANY_OWNER, 4220 (uaddr_t)sig, sig_len); 4221 if (res != TEE_SUCCESS) 4222 return res; 4223 4224 if (MUL_OVERFLOW(sizeof(TEE_Attribute), num_params, &alloc_size)) 4225 return TEE_ERROR_OVERFLOW; 4226 4227 params = malloc(alloc_size); 4228 if (!params) 4229 return TEE_ERROR_OUT_OF_MEMORY; 4230 res = copy_in_attrs(utc, usr_params, num_params, params); 4231 if (res != TEE_SUCCESS) 4232 goto out; 4233 4234 res = tee_obj_get(utc, cs->key1, &o); 4235 if (res != TEE_SUCCESS) 4236 goto out; 4237 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 4238 res = TEE_ERROR_BAD_PARAMETERS; 4239 goto out; 4240 } 4241 4242 switch (TEE_ALG_GET_MAIN_ALG(cs->algo)) { 4243 case TEE_MAIN_ALGO_RSA: 4244 if (cs->algo != TEE_ALG_RSASSA_PKCS1_V1_5) { 4245 hash_algo = TEE_DIGEST_HASH_TO_ALGO(cs->algo); 4246 res = tee_alg_get_digest_size(hash_algo, &hash_size); 4247 if (res != TEE_SUCCESS) 4248 break; 4249 if (data_len != hash_size) { 4250 res = TEE_ERROR_BAD_PARAMETERS; 4251 break; 4252 } 4253 salt_len = pkcs1_get_salt_len(params, num_params, 4254 hash_size); 4255 } 4256 res = crypto_acipher_rsassa_verify(cs->algo, o->attr, salt_len, 4257 data, data_len, sig, 4258 sig_len); 4259 break; 4260 4261 case TEE_MAIN_ALGO_DSA: 4262 hash_algo = TEE_DIGEST_HASH_TO_ALGO(cs->algo); 4263 res = tee_alg_get_digest_size(hash_algo, &hash_size); 4264 if (res != TEE_SUCCESS) 4265 break; 4266 4267 if (data_len != hash_size) { 4268 struct dsa_public_key *key = o->attr; 4269 4270 /* 4271 * Depending on the DSA algorithm (NIST), the 4272 * digital signature output size may be truncated 4273 * to the size of a key pair (Q prime size). Q 4274 * prime size must be less or equal than the hash 4275 * output length of the hash algorithm involved. 4276 * 4277 * We're checking here in order to be able to 4278 * return this particular error code, which will 4279 * cause TEE_AsymmetricVerifyDigest() to panic as 4280 * required by GP. crypto_acipher_dsa_verify() is 4281 * implemented in the glue layer of the crypto 4282 * library and it might be a bit harder to catch 4283 * this particular case there or lead to duplicated 4284 * code in different crypto glue layers. 4285 * 4286 * The GP spec says that we SHOULD panic if 4287 * data_len != hash_size, but that would break a 4288 * few of the DSA tests in xtest where the 4289 * hash_size is larger than possible data_len. So 4290 * the compromise is in case data_len != hash_size 4291 * check that it's not smaller than what makes 4292 * sense. 4293 */ 4294 if (data_len != crypto_bignum_num_bytes(key->q)) { 4295 res = TEE_ERROR_BAD_PARAMETERS; 4296 break; 4297 } 4298 } 4299 res = crypto_acipher_dsa_verify(cs->algo, o->attr, data, 4300 data_len, sig, sig_len); 4301 break; 4302 4303 case TEE_MAIN_ALGO_ED25519: 4304 res = tee_svc_obj_ed25519_verify(o->attr, data, 4305 data_len, sig, sig_len, 4306 params, num_params); 4307 break; 4308 4309 case TEE_MAIN_ALGO_ECDSA: 4310 case TEE_MAIN_ALGO_SM2_DSA_SM3: 4311 res = crypto_acipher_ecc_verify(cs->algo, o->attr, data, 4312 data_len, sig, sig_len); 4313 break; 4314 4315 default: 4316 res = TEE_ERROR_NOT_SUPPORTED; 4317 } 4318 4319 out: 4320 free_wipe(params); 4321 return res; 4322 } 4323