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