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