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