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 TEE_ObjectType obj_type = o->info.objectType; 1580 1581 obj_size += (attrs[n].content.ref.length * 8); 1582 1583 /* Drop the parity bits DES keys */ 1584 if (obj_type == TEE_TYPE_DES || 1585 obj_type == TEE_TYPE_DES3) 1586 obj_size -= obj_size / 8; 1587 } 1588 if (obj_size > o->info.maxKeySize) 1589 return TEE_ERROR_BAD_STATE; 1590 } 1591 } 1592 1593 /* 1594 * We have to do it like this because the parity bits aren't counted 1595 * when telling the size of the key in bits. 1596 */ 1597 if (o->info.objectType == TEE_TYPE_DES || 1598 o->info.objectType == TEE_TYPE_DES3) 1599 obj_size -= obj_size / 8; /* Exclude parity in size of key */ 1600 1601 o->have_attrs = have_attrs; 1602 o->info.keySize = obj_size; 1603 1604 return TEE_SUCCESS; 1605 } 1606 1607 TEE_Result syscall_cryp_obj_populate(unsigned long obj, 1608 struct utee_attribute *usr_attrs, 1609 unsigned long attr_count) 1610 { 1611 TEE_Result res; 1612 struct tee_ta_session *sess; 1613 struct tee_obj *o; 1614 const struct tee_cryp_obj_type_props *type_props; 1615 TEE_Attribute *attrs = NULL; 1616 1617 res = tee_ta_get_current_session(&sess); 1618 if (res != TEE_SUCCESS) 1619 return res; 1620 1621 res = tee_obj_get(to_user_ta_ctx(sess->ctx), uref_to_vaddr(obj), &o); 1622 if (res != TEE_SUCCESS) 1623 return res; 1624 1625 /* Must be a transient object */ 1626 if ((o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 1627 return TEE_ERROR_BAD_PARAMETERS; 1628 1629 /* Must not be initialized already */ 1630 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1631 return TEE_ERROR_BAD_PARAMETERS; 1632 1633 type_props = tee_svc_find_type_props(o->info.objectType); 1634 if (!type_props) 1635 return TEE_ERROR_NOT_IMPLEMENTED; 1636 1637 size_t alloc_size = 0; 1638 1639 if (MUL_OVERFLOW(sizeof(TEE_Attribute), attr_count, &alloc_size)) 1640 return TEE_ERROR_OVERFLOW; 1641 1642 attrs = malloc(alloc_size); 1643 if (!attrs) 1644 return TEE_ERROR_OUT_OF_MEMORY; 1645 1646 res = copy_in_attrs(to_user_ta_ctx(sess->ctx), usr_attrs, attr_count, 1647 attrs); 1648 if (res != TEE_SUCCESS) 1649 goto out; 1650 1651 res = tee_svc_cryp_check_attr(ATTR_USAGE_POPULATE, type_props, 1652 attrs, attr_count); 1653 if (res != TEE_SUCCESS) 1654 goto out; 1655 1656 res = tee_svc_cryp_obj_populate_type(o, type_props, attrs, attr_count); 1657 if (res == TEE_SUCCESS) 1658 o->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 1659 1660 out: 1661 free_wipe(attrs); 1662 return res; 1663 } 1664 1665 TEE_Result syscall_cryp_obj_copy(unsigned long dst, unsigned long src) 1666 { 1667 TEE_Result res; 1668 struct tee_ta_session *sess; 1669 struct tee_obj *dst_o; 1670 struct tee_obj *src_o; 1671 1672 res = tee_ta_get_current_session(&sess); 1673 if (res != TEE_SUCCESS) 1674 return res; 1675 1676 res = tee_obj_get(to_user_ta_ctx(sess->ctx), 1677 uref_to_vaddr(dst), &dst_o); 1678 if (res != TEE_SUCCESS) 1679 return res; 1680 1681 res = tee_obj_get(to_user_ta_ctx(sess->ctx), 1682 uref_to_vaddr(src), &src_o); 1683 if (res != TEE_SUCCESS) 1684 return res; 1685 1686 if ((src_o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1687 return TEE_ERROR_BAD_PARAMETERS; 1688 if ((dst_o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 1689 return TEE_ERROR_BAD_PARAMETERS; 1690 if ((dst_o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1691 return TEE_ERROR_BAD_PARAMETERS; 1692 1693 res = tee_obj_attr_copy_from(dst_o, src_o); 1694 if (res != TEE_SUCCESS) 1695 return res; 1696 1697 dst_o->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 1698 dst_o->info.keySize = src_o->info.keySize; 1699 dst_o->info.objectUsage = src_o->info.objectUsage; 1700 return TEE_SUCCESS; 1701 } 1702 1703 static TEE_Result check_pub_rsa_key(struct bignum *e) 1704 { 1705 size_t n = crypto_bignum_num_bytes(e); 1706 uint8_t bin_key[256 / 8] = { 0 }; 1707 1708 /* 1709 * NIST SP800-56B requires public RSA key to be an odd integer in 1710 * the range 65537 <= e < 2^256. 1711 */ 1712 1713 if (n > sizeof(bin_key) || n < 3) 1714 return TEE_ERROR_BAD_PARAMETERS; 1715 1716 crypto_bignum_bn2bin(e, bin_key); 1717 1718 if (!(bin_key[0] & 1)) /* key must be odd */ 1719 return TEE_ERROR_BAD_PARAMETERS; 1720 1721 if (n == 3) { 1722 uint32_t key = 0; 1723 1724 for (n = 0; n < 3; n++) { 1725 key <<= 8; 1726 key |= bin_key[n]; 1727 } 1728 1729 if (key < 65537) 1730 return TEE_ERROR_BAD_PARAMETERS; 1731 } 1732 1733 /* key is larger than 65537 */ 1734 return TEE_SUCCESS; 1735 } 1736 1737 static TEE_Result tee_svc_obj_generate_key_rsa( 1738 struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props, 1739 uint32_t key_size, 1740 const TEE_Attribute *params, uint32_t param_count) 1741 { 1742 TEE_Result res = TEE_SUCCESS; 1743 struct rsa_keypair *key = o->attr; 1744 uint32_t e = TEE_U32_TO_BIG_ENDIAN(65537); 1745 1746 /* Copy the present attributes into the obj before starting */ 1747 res = tee_svc_cryp_obj_populate_type(o, type_props, params, 1748 param_count); 1749 if (res != TEE_SUCCESS) 1750 return res; 1751 if (get_attribute(o, type_props, TEE_ATTR_RSA_PUBLIC_EXPONENT)) { 1752 res = check_pub_rsa_key(key->e); 1753 if (res) 1754 return res; 1755 } else { 1756 crypto_bignum_bin2bn((const uint8_t *)&e, sizeof(e), key->e); 1757 } 1758 res = crypto_acipher_gen_rsa_key(key, key_size); 1759 if (res != TEE_SUCCESS) 1760 return res; 1761 1762 /* Set bits for all known attributes for this object type */ 1763 o->have_attrs = (1 << type_props->num_type_attrs) - 1; 1764 1765 return TEE_SUCCESS; 1766 } 1767 1768 static TEE_Result tee_svc_obj_generate_key_dsa( 1769 struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props, 1770 uint32_t key_size, const TEE_Attribute *params, uint32_t param_count) 1771 { 1772 TEE_Result res; 1773 1774 /* Copy the present attributes into the obj before starting */ 1775 res = tee_svc_cryp_obj_populate_type(o, type_props, params, 1776 param_count); 1777 if (res != TEE_SUCCESS) 1778 return res; 1779 1780 res = crypto_acipher_gen_dsa_key(o->attr, key_size); 1781 if (res != TEE_SUCCESS) 1782 return res; 1783 1784 /* Set bits for all known attributes for this object type */ 1785 o->have_attrs = (1 << type_props->num_type_attrs) - 1; 1786 1787 return TEE_SUCCESS; 1788 } 1789 1790 static TEE_Result tee_svc_obj_generate_key_dh( 1791 struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props, 1792 uint32_t key_size, const TEE_Attribute *params, uint32_t param_count) 1793 { 1794 TEE_Result res; 1795 struct dh_keypair *tee_dh_key; 1796 struct bignum *dh_q = NULL; 1797 uint32_t dh_xbits = 0; 1798 1799 /* Copy the present attributes into the obj before starting */ 1800 res = tee_svc_cryp_obj_populate_type(o, type_props, params, 1801 param_count); 1802 if (res != TEE_SUCCESS) 1803 return res; 1804 1805 tee_dh_key = (struct dh_keypair *)o->attr; 1806 1807 if (get_attribute(o, type_props, TEE_ATTR_DH_SUBPRIME)) 1808 dh_q = tee_dh_key->q; 1809 if (get_attribute(o, type_props, TEE_ATTR_DH_X_BITS)) 1810 dh_xbits = tee_dh_key->xbits; 1811 res = crypto_acipher_gen_dh_key(tee_dh_key, dh_q, dh_xbits, key_size); 1812 if (res != TEE_SUCCESS) 1813 return res; 1814 1815 /* Set bits for the generated public and private key */ 1816 set_attribute(o, type_props, TEE_ATTR_DH_PUBLIC_VALUE); 1817 set_attribute(o, type_props, TEE_ATTR_DH_PRIVATE_VALUE); 1818 set_attribute(o, type_props, TEE_ATTR_DH_X_BITS); 1819 return TEE_SUCCESS; 1820 } 1821 1822 static TEE_Result tee_svc_obj_generate_key_ecc( 1823 struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props, 1824 uint32_t key_size, const TEE_Attribute *params, uint32_t param_count) 1825 { 1826 TEE_Result res; 1827 struct ecc_keypair *tee_ecc_key; 1828 1829 /* Copy the present attributes into the obj before starting */ 1830 res = tee_svc_cryp_obj_populate_type(o, type_props, params, 1831 param_count); 1832 if (res != TEE_SUCCESS) 1833 return res; 1834 1835 tee_ecc_key = (struct ecc_keypair *)o->attr; 1836 1837 res = crypto_acipher_gen_ecc_key(tee_ecc_key, key_size); 1838 if (res != TEE_SUCCESS) 1839 return res; 1840 1841 /* Set bits for the generated public and private key */ 1842 set_attribute(o, type_props, TEE_ATTR_ECC_PRIVATE_VALUE); 1843 set_attribute(o, type_props, TEE_ATTR_ECC_PUBLIC_VALUE_X); 1844 set_attribute(o, type_props, TEE_ATTR_ECC_PUBLIC_VALUE_Y); 1845 set_attribute(o, type_props, TEE_ATTR_ECC_CURVE); 1846 return TEE_SUCCESS; 1847 } 1848 1849 TEE_Result syscall_obj_generate_key(unsigned long obj, unsigned long key_size, 1850 const struct utee_attribute *usr_params, 1851 unsigned long param_count) 1852 { 1853 TEE_Result res; 1854 struct tee_ta_session *sess; 1855 const struct tee_cryp_obj_type_props *type_props; 1856 struct tee_obj *o; 1857 struct tee_cryp_obj_secret *key; 1858 size_t byte_size; 1859 TEE_Attribute *params = NULL; 1860 1861 res = tee_ta_get_current_session(&sess); 1862 if (res != TEE_SUCCESS) 1863 return res; 1864 1865 res = tee_obj_get(to_user_ta_ctx(sess->ctx), uref_to_vaddr(obj), &o); 1866 if (res != TEE_SUCCESS) 1867 return res; 1868 1869 /* Must be a transient object */ 1870 if ((o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 1871 return TEE_ERROR_BAD_STATE; 1872 1873 /* Must not be initialized already */ 1874 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1875 return TEE_ERROR_BAD_STATE; 1876 1877 /* Find description of object */ 1878 type_props = tee_svc_find_type_props(o->info.objectType); 1879 if (!type_props) 1880 return TEE_ERROR_NOT_SUPPORTED; 1881 1882 /* Check that maxKeySize follows restrictions */ 1883 if (key_size % type_props->quanta != 0) 1884 return TEE_ERROR_NOT_SUPPORTED; 1885 if (key_size < type_props->min_size) 1886 return TEE_ERROR_NOT_SUPPORTED; 1887 if (key_size > type_props->max_size) 1888 return TEE_ERROR_NOT_SUPPORTED; 1889 1890 size_t alloc_size = 0; 1891 1892 if (MUL_OVERFLOW(sizeof(TEE_Attribute), param_count, &alloc_size)) 1893 return TEE_ERROR_OVERFLOW; 1894 1895 params = malloc(alloc_size); 1896 if (!params) 1897 return TEE_ERROR_OUT_OF_MEMORY; 1898 res = copy_in_attrs(to_user_ta_ctx(sess->ctx), usr_params, param_count, 1899 params); 1900 if (res != TEE_SUCCESS) 1901 goto out; 1902 1903 res = tee_svc_cryp_check_attr(ATTR_USAGE_GENERATE_KEY, type_props, 1904 params, param_count); 1905 if (res != TEE_SUCCESS) 1906 goto out; 1907 1908 switch (o->info.objectType) { 1909 case TEE_TYPE_AES: 1910 case TEE_TYPE_DES: 1911 case TEE_TYPE_DES3: 1912 case TEE_TYPE_SM4: 1913 case TEE_TYPE_HMAC_MD5: 1914 case TEE_TYPE_HMAC_SHA1: 1915 case TEE_TYPE_HMAC_SHA224: 1916 case TEE_TYPE_HMAC_SHA256: 1917 case TEE_TYPE_HMAC_SHA384: 1918 case TEE_TYPE_HMAC_SHA512: 1919 case TEE_TYPE_HMAC_SM3: 1920 case TEE_TYPE_GENERIC_SECRET: 1921 byte_size = key_size / 8; 1922 1923 /* 1924 * We have to do it like this because the parity bits aren't 1925 * counted when telling the size of the key in bits. 1926 */ 1927 if (o->info.objectType == TEE_TYPE_DES || 1928 o->info.objectType == TEE_TYPE_DES3) { 1929 byte_size = (key_size + key_size / 7) / 8; 1930 } 1931 1932 key = (struct tee_cryp_obj_secret *)o->attr; 1933 if (byte_size > key->alloc_size) { 1934 res = TEE_ERROR_EXCESS_DATA; 1935 goto out; 1936 } 1937 1938 res = crypto_rng_read((void *)(key + 1), byte_size); 1939 if (res != TEE_SUCCESS) 1940 goto out; 1941 1942 key->key_size = byte_size; 1943 1944 /* Set bits for all known attributes for this object type */ 1945 o->have_attrs = (1 << type_props->num_type_attrs) - 1; 1946 1947 break; 1948 1949 case TEE_TYPE_RSA_KEYPAIR: 1950 res = tee_svc_obj_generate_key_rsa(o, type_props, key_size, 1951 params, param_count); 1952 if (res != TEE_SUCCESS) 1953 goto out; 1954 break; 1955 1956 case TEE_TYPE_DSA_KEYPAIR: 1957 res = tee_svc_obj_generate_key_dsa(o, type_props, key_size, 1958 params, param_count); 1959 if (res != TEE_SUCCESS) 1960 goto out; 1961 break; 1962 1963 case TEE_TYPE_DH_KEYPAIR: 1964 res = tee_svc_obj_generate_key_dh(o, type_props, key_size, 1965 params, param_count); 1966 if (res != TEE_SUCCESS) 1967 goto out; 1968 break; 1969 1970 case TEE_TYPE_ECDSA_KEYPAIR: 1971 case TEE_TYPE_ECDH_KEYPAIR: 1972 case TEE_TYPE_SM2_PKE_KEYPAIR: 1973 res = tee_svc_obj_generate_key_ecc(o, type_props, key_size, 1974 params, param_count); 1975 if (res != TEE_SUCCESS) 1976 goto out; 1977 break; 1978 1979 default: 1980 res = TEE_ERROR_BAD_FORMAT; 1981 } 1982 1983 out: 1984 free_wipe(params); 1985 if (res == TEE_SUCCESS) { 1986 o->info.keySize = key_size; 1987 o->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 1988 } 1989 return res; 1990 } 1991 1992 static TEE_Result tee_svc_cryp_get_state(struct tee_ta_session *sess, 1993 uint32_t state_id, 1994 struct tee_cryp_state **state) 1995 { 1996 struct tee_cryp_state *s; 1997 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 1998 1999 TAILQ_FOREACH(s, &utc->cryp_states, link) { 2000 if (state_id == (vaddr_t)s) { 2001 *state = s; 2002 return TEE_SUCCESS; 2003 } 2004 } 2005 return TEE_ERROR_BAD_PARAMETERS; 2006 } 2007 2008 static void cryp_state_free(struct user_ta_ctx *utc, struct tee_cryp_state *cs) 2009 { 2010 struct tee_obj *o; 2011 2012 if (tee_obj_get(utc, cs->key1, &o) == TEE_SUCCESS) 2013 tee_obj_close(utc, o); 2014 if (tee_obj_get(utc, cs->key2, &o) == TEE_SUCCESS) 2015 tee_obj_close(utc, o); 2016 2017 TAILQ_REMOVE(&utc->cryp_states, cs, link); 2018 if (cs->ctx_finalize != NULL) 2019 cs->ctx_finalize(cs->ctx); 2020 2021 switch (TEE_ALG_GET_CLASS(cs->algo)) { 2022 case TEE_OPERATION_CIPHER: 2023 crypto_cipher_free_ctx(cs->ctx); 2024 break; 2025 case TEE_OPERATION_AE: 2026 crypto_authenc_free_ctx(cs->ctx); 2027 break; 2028 case TEE_OPERATION_DIGEST: 2029 crypto_hash_free_ctx(cs->ctx); 2030 break; 2031 case TEE_OPERATION_MAC: 2032 crypto_mac_free_ctx(cs->ctx); 2033 break; 2034 default: 2035 assert(!cs->ctx); 2036 } 2037 2038 free(cs); 2039 } 2040 2041 static TEE_Result tee_svc_cryp_check_key_type(const struct tee_obj *o, 2042 uint32_t algo, 2043 TEE_OperationMode mode) 2044 { 2045 uint32_t req_key_type; 2046 uint32_t req_key_type2 = 0; 2047 2048 switch (TEE_ALG_GET_MAIN_ALG(algo)) { 2049 case TEE_MAIN_ALGO_MD5: 2050 req_key_type = TEE_TYPE_HMAC_MD5; 2051 break; 2052 case TEE_MAIN_ALGO_SHA1: 2053 req_key_type = TEE_TYPE_HMAC_SHA1; 2054 break; 2055 case TEE_MAIN_ALGO_SHA224: 2056 req_key_type = TEE_TYPE_HMAC_SHA224; 2057 break; 2058 case TEE_MAIN_ALGO_SHA256: 2059 req_key_type = TEE_TYPE_HMAC_SHA256; 2060 break; 2061 case TEE_MAIN_ALGO_SHA384: 2062 req_key_type = TEE_TYPE_HMAC_SHA384; 2063 break; 2064 case TEE_MAIN_ALGO_SHA512: 2065 req_key_type = TEE_TYPE_HMAC_SHA512; 2066 break; 2067 case TEE_MAIN_ALGO_SM3: 2068 req_key_type = TEE_TYPE_HMAC_SM3; 2069 break; 2070 case TEE_MAIN_ALGO_AES: 2071 req_key_type = TEE_TYPE_AES; 2072 break; 2073 case TEE_MAIN_ALGO_DES: 2074 req_key_type = TEE_TYPE_DES; 2075 break; 2076 case TEE_MAIN_ALGO_DES3: 2077 req_key_type = TEE_TYPE_DES3; 2078 break; 2079 case TEE_MAIN_ALGO_SM4: 2080 req_key_type = TEE_TYPE_SM4; 2081 break; 2082 case TEE_MAIN_ALGO_RSA: 2083 req_key_type = TEE_TYPE_RSA_KEYPAIR; 2084 if (mode == TEE_MODE_ENCRYPT || mode == TEE_MODE_VERIFY) 2085 req_key_type2 = TEE_TYPE_RSA_PUBLIC_KEY; 2086 break; 2087 case TEE_MAIN_ALGO_DSA: 2088 req_key_type = TEE_TYPE_DSA_KEYPAIR; 2089 if (mode == TEE_MODE_ENCRYPT || mode == TEE_MODE_VERIFY) 2090 req_key_type2 = TEE_TYPE_DSA_PUBLIC_KEY; 2091 break; 2092 case TEE_MAIN_ALGO_DH: 2093 req_key_type = TEE_TYPE_DH_KEYPAIR; 2094 break; 2095 case TEE_MAIN_ALGO_ECDSA: 2096 req_key_type = TEE_TYPE_ECDSA_KEYPAIR; 2097 if (mode == TEE_MODE_VERIFY) 2098 req_key_type2 = TEE_TYPE_ECDSA_PUBLIC_KEY; 2099 break; 2100 case TEE_MAIN_ALGO_ECDH: 2101 req_key_type = TEE_TYPE_ECDH_KEYPAIR; 2102 break; 2103 case TEE_MAIN_ALGO_SM2_PKE: 2104 if (mode == TEE_MODE_ENCRYPT) 2105 req_key_type = TEE_TYPE_SM2_PKE_PUBLIC_KEY; 2106 else 2107 req_key_type = TEE_TYPE_SM2_PKE_KEYPAIR; 2108 break; 2109 case TEE_MAIN_ALGO_SM2_DSA_SM3: 2110 if (mode == TEE_MODE_VERIFY) 2111 req_key_type = TEE_TYPE_SM2_DSA_PUBLIC_KEY; 2112 else 2113 req_key_type = TEE_TYPE_SM2_DSA_KEYPAIR; 2114 break; 2115 #if defined(CFG_CRYPTO_SM2_KEP) 2116 case TEE_MAIN_ALGO_SM2_KEP: 2117 req_key_type = TEE_TYPE_SM2_KEP_KEYPAIR; 2118 req_key_type2 = TEE_TYPE_SM2_KEP_PUBLIC_KEY; 2119 break; 2120 #endif 2121 #if defined(CFG_CRYPTO_HKDF) 2122 case TEE_MAIN_ALGO_HKDF: 2123 req_key_type = TEE_TYPE_HKDF_IKM; 2124 break; 2125 #endif 2126 #if defined(CFG_CRYPTO_CONCAT_KDF) 2127 case TEE_MAIN_ALGO_CONCAT_KDF: 2128 req_key_type = TEE_TYPE_CONCAT_KDF_Z; 2129 break; 2130 #endif 2131 #if defined(CFG_CRYPTO_PBKDF2) 2132 case TEE_MAIN_ALGO_PBKDF2: 2133 req_key_type = TEE_TYPE_PBKDF2_PASSWORD; 2134 break; 2135 #endif 2136 default: 2137 return TEE_ERROR_BAD_PARAMETERS; 2138 } 2139 2140 if (req_key_type != o->info.objectType && 2141 req_key_type2 != o->info.objectType) 2142 return TEE_ERROR_BAD_PARAMETERS; 2143 return TEE_SUCCESS; 2144 } 2145 2146 TEE_Result syscall_cryp_state_alloc(unsigned long algo, unsigned long mode, 2147 unsigned long key1, unsigned long key2, 2148 uint32_t *state) 2149 { 2150 TEE_Result res; 2151 struct tee_cryp_state *cs; 2152 struct tee_ta_session *sess; 2153 struct tee_obj *o1 = NULL; 2154 struct tee_obj *o2 = NULL; 2155 struct user_ta_ctx *utc; 2156 2157 res = tee_ta_get_current_session(&sess); 2158 if (res != TEE_SUCCESS) 2159 return res; 2160 utc = to_user_ta_ctx(sess->ctx); 2161 2162 if (key1 != 0) { 2163 res = tee_obj_get(utc, uref_to_vaddr(key1), &o1); 2164 if (res != TEE_SUCCESS) 2165 return res; 2166 if (o1->busy) 2167 return TEE_ERROR_BAD_PARAMETERS; 2168 res = tee_svc_cryp_check_key_type(o1, algo, mode); 2169 if (res != TEE_SUCCESS) 2170 return res; 2171 } 2172 if (key2 != 0) { 2173 res = tee_obj_get(utc, uref_to_vaddr(key2), &o2); 2174 if (res != TEE_SUCCESS) 2175 return res; 2176 if (o2->busy) 2177 return TEE_ERROR_BAD_PARAMETERS; 2178 res = tee_svc_cryp_check_key_type(o2, algo, mode); 2179 if (res != TEE_SUCCESS) 2180 return res; 2181 } 2182 2183 cs = calloc(1, sizeof(struct tee_cryp_state)); 2184 if (!cs) 2185 return TEE_ERROR_OUT_OF_MEMORY; 2186 TAILQ_INSERT_TAIL(&utc->cryp_states, cs, link); 2187 cs->algo = algo; 2188 cs->mode = mode; 2189 cs->state = CRYP_STATE_UNINITIALIZED; 2190 2191 switch (TEE_ALG_GET_CLASS(algo)) { 2192 case TEE_OPERATION_CIPHER: 2193 if ((algo == TEE_ALG_AES_XTS && (key1 == 0 || key2 == 0)) || 2194 (algo != TEE_ALG_AES_XTS && (key1 == 0 || key2 != 0))) { 2195 res = TEE_ERROR_BAD_PARAMETERS; 2196 } else { 2197 res = crypto_cipher_alloc_ctx(&cs->ctx, algo); 2198 if (res != TEE_SUCCESS) 2199 break; 2200 } 2201 break; 2202 case TEE_OPERATION_AE: 2203 if (key1 == 0 || key2 != 0) { 2204 res = TEE_ERROR_BAD_PARAMETERS; 2205 } else { 2206 res = crypto_authenc_alloc_ctx(&cs->ctx, algo); 2207 if (res != TEE_SUCCESS) 2208 break; 2209 } 2210 break; 2211 case TEE_OPERATION_MAC: 2212 if (key1 == 0 || key2 != 0) { 2213 res = TEE_ERROR_BAD_PARAMETERS; 2214 } else { 2215 res = crypto_mac_alloc_ctx(&cs->ctx, algo); 2216 if (res != TEE_SUCCESS) 2217 break; 2218 } 2219 break; 2220 case TEE_OPERATION_DIGEST: 2221 if (key1 != 0 || key2 != 0) { 2222 res = TEE_ERROR_BAD_PARAMETERS; 2223 } else { 2224 res = crypto_hash_alloc_ctx(&cs->ctx, algo); 2225 if (res != TEE_SUCCESS) 2226 break; 2227 } 2228 break; 2229 case TEE_OPERATION_ASYMMETRIC_CIPHER: 2230 case TEE_OPERATION_ASYMMETRIC_SIGNATURE: 2231 if (algo == TEE_ALG_RSASSA_PKCS1_V1_5 && 2232 !IS_ENABLED(CFG_CRYPTO_RSASSA_NA1)) { 2233 res = TEE_ERROR_NOT_SUPPORTED; 2234 break; 2235 } 2236 if (key1 == 0 || key2 != 0) 2237 res = TEE_ERROR_BAD_PARAMETERS; 2238 break; 2239 case TEE_OPERATION_KEY_DERIVATION: 2240 if (algo == TEE_ALG_SM2_KEP) { 2241 if (key1 == 0 || key2 == 0) 2242 res = TEE_ERROR_BAD_PARAMETERS; 2243 } else { 2244 if (key1 == 0 || key2 != 0) 2245 res = TEE_ERROR_BAD_PARAMETERS; 2246 } 2247 break; 2248 default: 2249 res = TEE_ERROR_NOT_SUPPORTED; 2250 break; 2251 } 2252 if (res != TEE_SUCCESS) 2253 goto out; 2254 2255 res = copy_kaddr_to_uref(state, cs); 2256 if (res != TEE_SUCCESS) 2257 goto out; 2258 2259 /* Register keys */ 2260 if (o1 != NULL) { 2261 o1->busy = true; 2262 cs->key1 = (vaddr_t)o1; 2263 } 2264 if (o2 != NULL) { 2265 o2->busy = true; 2266 cs->key2 = (vaddr_t)o2; 2267 } 2268 2269 out: 2270 if (res != TEE_SUCCESS) 2271 cryp_state_free(utc, cs); 2272 return res; 2273 } 2274 2275 TEE_Result syscall_cryp_state_copy(unsigned long dst, unsigned long src) 2276 { 2277 TEE_Result res; 2278 struct tee_cryp_state *cs_dst; 2279 struct tee_cryp_state *cs_src; 2280 struct tee_ta_session *sess; 2281 2282 res = tee_ta_get_current_session(&sess); 2283 if (res != TEE_SUCCESS) 2284 return res; 2285 2286 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(dst), &cs_dst); 2287 if (res != TEE_SUCCESS) 2288 return res; 2289 2290 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(src), &cs_src); 2291 if (res != TEE_SUCCESS) 2292 return res; 2293 if (cs_dst->algo != cs_src->algo || cs_dst->mode != cs_src->mode) 2294 return TEE_ERROR_BAD_PARAMETERS; 2295 2296 switch (TEE_ALG_GET_CLASS(cs_src->algo)) { 2297 case TEE_OPERATION_CIPHER: 2298 crypto_cipher_copy_state(cs_dst->ctx, cs_src->ctx); 2299 break; 2300 case TEE_OPERATION_AE: 2301 crypto_authenc_copy_state(cs_dst->ctx, cs_src->ctx); 2302 break; 2303 case TEE_OPERATION_DIGEST: 2304 crypto_hash_copy_state(cs_dst->ctx, cs_src->ctx); 2305 break; 2306 case TEE_OPERATION_MAC: 2307 crypto_mac_copy_state(cs_dst->ctx, cs_src->ctx); 2308 break; 2309 default: 2310 return TEE_ERROR_BAD_STATE; 2311 } 2312 2313 cs_dst->state = cs_src->state; 2314 2315 return TEE_SUCCESS; 2316 } 2317 2318 void tee_svc_cryp_free_states(struct user_ta_ctx *utc) 2319 { 2320 struct tee_cryp_state_head *states = &utc->cryp_states; 2321 2322 while (!TAILQ_EMPTY(states)) 2323 cryp_state_free(utc, TAILQ_FIRST(states)); 2324 } 2325 2326 TEE_Result syscall_cryp_state_free(unsigned long state) 2327 { 2328 TEE_Result res; 2329 struct tee_cryp_state *cs; 2330 struct tee_ta_session *sess; 2331 2332 res = tee_ta_get_current_session(&sess); 2333 if (res != TEE_SUCCESS) 2334 return res; 2335 2336 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 2337 if (res != TEE_SUCCESS) 2338 return res; 2339 cryp_state_free(to_user_ta_ctx(sess->ctx), cs); 2340 return TEE_SUCCESS; 2341 } 2342 2343 TEE_Result syscall_hash_init(unsigned long state, 2344 const void *iv __maybe_unused, 2345 size_t iv_len __maybe_unused) 2346 { 2347 TEE_Result res; 2348 struct tee_cryp_state *cs; 2349 struct tee_ta_session *sess; 2350 2351 res = tee_ta_get_current_session(&sess); 2352 if (res != TEE_SUCCESS) 2353 return res; 2354 2355 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 2356 if (res != TEE_SUCCESS) 2357 return res; 2358 2359 switch (TEE_ALG_GET_CLASS(cs->algo)) { 2360 case TEE_OPERATION_DIGEST: 2361 res = crypto_hash_init(cs->ctx); 2362 if (res != TEE_SUCCESS) 2363 return res; 2364 break; 2365 case TEE_OPERATION_MAC: 2366 { 2367 struct tee_obj *o; 2368 struct tee_cryp_obj_secret *key; 2369 2370 res = tee_obj_get(to_user_ta_ctx(sess->ctx), 2371 cs->key1, &o); 2372 if (res != TEE_SUCCESS) 2373 return res; 2374 if ((o->info.handleFlags & 2375 TEE_HANDLE_FLAG_INITIALIZED) == 0) 2376 return TEE_ERROR_BAD_PARAMETERS; 2377 2378 key = (struct tee_cryp_obj_secret *)o->attr; 2379 res = crypto_mac_init(cs->ctx, (void *)(key + 1), 2380 key->key_size); 2381 if (res != TEE_SUCCESS) 2382 return res; 2383 break; 2384 } 2385 default: 2386 return TEE_ERROR_BAD_PARAMETERS; 2387 } 2388 2389 cs->state = CRYP_STATE_INITIALIZED; 2390 2391 return TEE_SUCCESS; 2392 } 2393 2394 TEE_Result syscall_hash_update(unsigned long state, const void *chunk, 2395 size_t chunk_size) 2396 { 2397 struct tee_ta_session *sess = NULL; 2398 struct tee_cryp_state *cs = NULL; 2399 TEE_Result res = TEE_SUCCESS; 2400 2401 /* No data, but size provided isn't valid parameters. */ 2402 if (!chunk && chunk_size) 2403 return TEE_ERROR_BAD_PARAMETERS; 2404 2405 /* Zero length hash is valid, but nothing we need to do. */ 2406 if (!chunk_size) 2407 return TEE_SUCCESS; 2408 2409 res = tee_ta_get_current_session(&sess); 2410 if (res != TEE_SUCCESS) 2411 return res; 2412 2413 res = tee_mmu_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 2414 TEE_MEMORY_ACCESS_READ | 2415 TEE_MEMORY_ACCESS_ANY_OWNER, 2416 (uaddr_t)chunk, chunk_size); 2417 if (res != TEE_SUCCESS) 2418 return res; 2419 2420 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 2421 if (res != TEE_SUCCESS) 2422 return res; 2423 2424 if (cs->state != CRYP_STATE_INITIALIZED) 2425 return TEE_ERROR_BAD_STATE; 2426 2427 switch (TEE_ALG_GET_CLASS(cs->algo)) { 2428 case TEE_OPERATION_DIGEST: 2429 res = crypto_hash_update(cs->ctx, chunk, chunk_size); 2430 if (res != TEE_SUCCESS) 2431 return res; 2432 break; 2433 case TEE_OPERATION_MAC: 2434 res = crypto_mac_update(cs->ctx, chunk, chunk_size); 2435 if (res != TEE_SUCCESS) 2436 return res; 2437 break; 2438 default: 2439 return TEE_ERROR_BAD_PARAMETERS; 2440 } 2441 2442 return TEE_SUCCESS; 2443 } 2444 2445 TEE_Result syscall_hash_final(unsigned long state, const void *chunk, 2446 size_t chunk_size, void *hash, uint64_t *hash_len) 2447 { 2448 struct tee_ta_session *sess = NULL; 2449 struct tee_cryp_state *cs = NULL; 2450 TEE_Result res2 = TEE_SUCCESS; 2451 TEE_Result res = TEE_SUCCESS; 2452 size_t hash_size = 0; 2453 size_t hlen = 0; 2454 2455 /* No data, but size provided isn't valid parameters. */ 2456 if (!chunk && chunk_size) 2457 return TEE_ERROR_BAD_PARAMETERS; 2458 2459 res = tee_ta_get_current_session(&sess); 2460 if (res != TEE_SUCCESS) 2461 return res; 2462 2463 res = tee_mmu_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 2464 TEE_MEMORY_ACCESS_READ | 2465 TEE_MEMORY_ACCESS_ANY_OWNER, 2466 (uaddr_t)chunk, chunk_size); 2467 if (res != TEE_SUCCESS) 2468 return res; 2469 2470 res = get_user_u64_as_size_t(&hlen, hash_len); 2471 if (res != TEE_SUCCESS) 2472 return res; 2473 2474 res = tee_mmu_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 2475 TEE_MEMORY_ACCESS_READ | 2476 TEE_MEMORY_ACCESS_WRITE | 2477 TEE_MEMORY_ACCESS_ANY_OWNER, 2478 (uaddr_t)hash, hlen); 2479 if (res != TEE_SUCCESS) 2480 return res; 2481 2482 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 2483 if (res != TEE_SUCCESS) 2484 return res; 2485 2486 if (cs->state != CRYP_STATE_INITIALIZED) 2487 return TEE_ERROR_BAD_STATE; 2488 2489 switch (TEE_ALG_GET_CLASS(cs->algo)) { 2490 case TEE_OPERATION_DIGEST: 2491 res = tee_alg_get_digest_size(cs->algo, &hash_size); 2492 if (res != TEE_SUCCESS) 2493 return res; 2494 if (hlen < hash_size) { 2495 res = TEE_ERROR_SHORT_BUFFER; 2496 goto out; 2497 } 2498 2499 if (chunk_size) { 2500 res = crypto_hash_update(cs->ctx, chunk, chunk_size); 2501 if (res != TEE_SUCCESS) 2502 return res; 2503 } 2504 2505 res = crypto_hash_final(cs->ctx, hash, hash_size); 2506 if (res != TEE_SUCCESS) 2507 return res; 2508 break; 2509 2510 case TEE_OPERATION_MAC: 2511 res = tee_alg_get_digest_size(cs->algo, &hash_size); 2512 if (res != TEE_SUCCESS) 2513 return res; 2514 if (hlen < hash_size) { 2515 res = TEE_ERROR_SHORT_BUFFER; 2516 goto out; 2517 } 2518 2519 if (chunk_size) { 2520 res = crypto_mac_update(cs->ctx, chunk, chunk_size); 2521 if (res != TEE_SUCCESS) 2522 return res; 2523 } 2524 2525 res = crypto_mac_final(cs->ctx, hash, hash_size); 2526 if (res != TEE_SUCCESS) 2527 return res; 2528 break; 2529 2530 default: 2531 return TEE_ERROR_BAD_PARAMETERS; 2532 } 2533 out: 2534 res2 = put_user_u64(hash_len, hash_size); 2535 if (res2 != TEE_SUCCESS) 2536 return res2; 2537 return res; 2538 } 2539 2540 TEE_Result syscall_cipher_init(unsigned long state, const void *iv, 2541 size_t iv_len) 2542 { 2543 struct tee_cryp_obj_secret *key1 = NULL; 2544 struct tee_ta_session *sess = NULL; 2545 struct tee_cryp_state *cs = NULL; 2546 struct user_ta_ctx *utc = NULL; 2547 TEE_Result res = TEE_SUCCESS; 2548 struct tee_obj *o = NULL; 2549 2550 res = tee_ta_get_current_session(&sess); 2551 if (res != TEE_SUCCESS) 2552 return res; 2553 utc = to_user_ta_ctx(sess->ctx); 2554 2555 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 2556 if (res != TEE_SUCCESS) 2557 return res; 2558 2559 if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_CIPHER) 2560 return TEE_ERROR_BAD_STATE; 2561 2562 res = tee_mmu_check_access_rights(&utc->uctx, 2563 TEE_MEMORY_ACCESS_READ | 2564 TEE_MEMORY_ACCESS_ANY_OWNER, 2565 (uaddr_t)iv, iv_len); 2566 if (res != TEE_SUCCESS) 2567 return res; 2568 2569 res = tee_obj_get(utc, cs->key1, &o); 2570 if (res != TEE_SUCCESS) 2571 return res; 2572 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 2573 return TEE_ERROR_BAD_PARAMETERS; 2574 2575 key1 = o->attr; 2576 2577 if (tee_obj_get(utc, cs->key2, &o) == TEE_SUCCESS) { 2578 struct tee_cryp_obj_secret *key2 = o->attr; 2579 2580 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 2581 return TEE_ERROR_BAD_PARAMETERS; 2582 2583 res = crypto_cipher_init(cs->ctx, cs->mode, 2584 (uint8_t *)(key1 + 1), key1->key_size, 2585 (uint8_t *)(key2 + 1), key2->key_size, 2586 iv, iv_len); 2587 } else { 2588 res = crypto_cipher_init(cs->ctx, cs->mode, 2589 (uint8_t *)(key1 + 1), key1->key_size, 2590 NULL, 0, iv, iv_len); 2591 } 2592 if (res != TEE_SUCCESS) 2593 return res; 2594 2595 cs->ctx_finalize = crypto_cipher_final; 2596 cs->state = CRYP_STATE_INITIALIZED; 2597 2598 return TEE_SUCCESS; 2599 } 2600 2601 static TEE_Result tee_svc_cipher_update_helper(unsigned long state, 2602 bool last_block, const void *src, size_t src_len, 2603 void *dst, uint64_t *dst_len) 2604 { 2605 struct tee_ta_session *sess = NULL; 2606 struct tee_cryp_state *cs = NULL; 2607 TEE_Result res = TEE_SUCCESS; 2608 size_t dlen = 0; 2609 2610 res = tee_ta_get_current_session(&sess); 2611 if (res != TEE_SUCCESS) 2612 return res; 2613 2614 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 2615 if (res != TEE_SUCCESS) 2616 return res; 2617 2618 if (cs->state != CRYP_STATE_INITIALIZED) 2619 return TEE_ERROR_BAD_STATE; 2620 2621 res = tee_mmu_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 2622 TEE_MEMORY_ACCESS_READ | 2623 TEE_MEMORY_ACCESS_ANY_OWNER, 2624 (uaddr_t)src, src_len); 2625 if (res != TEE_SUCCESS) 2626 return res; 2627 2628 if (!dst_len) { 2629 dlen = 0; 2630 } else { 2631 struct user_mode_ctx *uctx = &to_user_ta_ctx(sess->ctx)->uctx; 2632 uint32_t flags = TEE_MEMORY_ACCESS_READ | 2633 TEE_MEMORY_ACCESS_WRITE | 2634 TEE_MEMORY_ACCESS_ANY_OWNER; 2635 2636 res = get_user_u64_as_size_t(&dlen, dst_len); 2637 if (res != TEE_SUCCESS) 2638 return res; 2639 2640 res = tee_mmu_check_access_rights(uctx, flags, (uaddr_t)dst, 2641 dlen); 2642 if (res != TEE_SUCCESS) 2643 return res; 2644 } 2645 2646 if (dlen < src_len) { 2647 res = TEE_ERROR_SHORT_BUFFER; 2648 goto out; 2649 } 2650 2651 if (src_len > 0) { 2652 /* Permit src_len == 0 to finalize the operation */ 2653 res = tee_do_cipher_update(cs->ctx, cs->algo, cs->mode, 2654 last_block, src, src_len, dst); 2655 } 2656 2657 if (last_block && cs->ctx_finalize != NULL) { 2658 cs->ctx_finalize(cs->ctx); 2659 cs->ctx_finalize = NULL; 2660 } 2661 2662 out: 2663 if ((res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) && 2664 dst_len != NULL) { 2665 TEE_Result res2; 2666 2667 res2 = put_user_u64(dst_len, src_len); 2668 if (res2 != TEE_SUCCESS) 2669 res = res2; 2670 } 2671 2672 return res; 2673 } 2674 2675 TEE_Result syscall_cipher_update(unsigned long state, const void *src, 2676 size_t src_len, void *dst, uint64_t *dst_len) 2677 { 2678 return tee_svc_cipher_update_helper(state, false /* last_block */, 2679 src, src_len, dst, dst_len); 2680 } 2681 2682 TEE_Result syscall_cipher_final(unsigned long state, const void *src, 2683 size_t src_len, void *dst, uint64_t *dst_len) 2684 { 2685 return tee_svc_cipher_update_helper(state, true /* last_block */, 2686 src, src_len, dst, dst_len); 2687 } 2688 2689 #if defined(CFG_CRYPTO_HKDF) 2690 static TEE_Result get_hkdf_params(const TEE_Attribute *params, 2691 uint32_t param_count, 2692 void **salt, size_t *salt_len, void **info, 2693 size_t *info_len, size_t *okm_len) 2694 { 2695 size_t n; 2696 enum { SALT = 0x1, LENGTH = 0x2, INFO = 0x4 }; 2697 uint8_t found = 0; 2698 2699 *salt = *info = NULL; 2700 *salt_len = *info_len = *okm_len = 0; 2701 2702 for (n = 0; n < param_count; n++) { 2703 switch (params[n].attributeID) { 2704 case TEE_ATTR_HKDF_SALT: 2705 if (!(found & SALT)) { 2706 *salt = params[n].content.ref.buffer; 2707 *salt_len = params[n].content.ref.length; 2708 found |= SALT; 2709 } 2710 break; 2711 case TEE_ATTR_HKDF_OKM_LENGTH: 2712 if (!(found & LENGTH)) { 2713 *okm_len = params[n].content.value.a; 2714 found |= LENGTH; 2715 } 2716 break; 2717 case TEE_ATTR_HKDF_INFO: 2718 if (!(found & INFO)) { 2719 *info = params[n].content.ref.buffer; 2720 *info_len = params[n].content.ref.length; 2721 found |= INFO; 2722 } 2723 break; 2724 default: 2725 /* Unexpected attribute */ 2726 return TEE_ERROR_BAD_PARAMETERS; 2727 } 2728 2729 } 2730 2731 if (!(found & LENGTH)) 2732 return TEE_ERROR_BAD_PARAMETERS; 2733 2734 return TEE_SUCCESS; 2735 } 2736 #endif 2737 2738 #if defined(CFG_CRYPTO_CONCAT_KDF) 2739 static TEE_Result get_concat_kdf_params(const TEE_Attribute *params, 2740 uint32_t param_count, 2741 void **other_info, 2742 size_t *other_info_len, 2743 size_t *derived_key_len) 2744 { 2745 size_t n; 2746 enum { LENGTH = 0x1, INFO = 0x2 }; 2747 uint8_t found = 0; 2748 2749 *other_info = NULL; 2750 *other_info_len = *derived_key_len = 0; 2751 2752 for (n = 0; n < param_count; n++) { 2753 switch (params[n].attributeID) { 2754 case TEE_ATTR_CONCAT_KDF_OTHER_INFO: 2755 if (!(found & INFO)) { 2756 *other_info = params[n].content.ref.buffer; 2757 *other_info_len = params[n].content.ref.length; 2758 found |= INFO; 2759 } 2760 break; 2761 case TEE_ATTR_CONCAT_KDF_DKM_LENGTH: 2762 if (!(found & LENGTH)) { 2763 *derived_key_len = params[n].content.value.a; 2764 found |= LENGTH; 2765 } 2766 break; 2767 default: 2768 /* Unexpected attribute */ 2769 return TEE_ERROR_BAD_PARAMETERS; 2770 } 2771 } 2772 2773 if (!(found & LENGTH)) 2774 return TEE_ERROR_BAD_PARAMETERS; 2775 2776 return TEE_SUCCESS; 2777 } 2778 #endif 2779 2780 #if defined(CFG_CRYPTO_PBKDF2) 2781 static TEE_Result get_pbkdf2_params(const TEE_Attribute *params, 2782 uint32_t param_count, void **salt, 2783 size_t *salt_len, size_t *derived_key_len, 2784 size_t *iteration_count) 2785 { 2786 size_t n; 2787 enum { SALT = 0x1, LENGTH = 0x2, COUNT = 0x4 }; 2788 uint8_t found = 0; 2789 2790 *salt = NULL; 2791 *salt_len = *derived_key_len = *iteration_count = 0; 2792 2793 for (n = 0; n < param_count; n++) { 2794 switch (params[n].attributeID) { 2795 case TEE_ATTR_PBKDF2_SALT: 2796 if (!(found & SALT)) { 2797 *salt = params[n].content.ref.buffer; 2798 *salt_len = params[n].content.ref.length; 2799 found |= SALT; 2800 } 2801 break; 2802 case TEE_ATTR_PBKDF2_DKM_LENGTH: 2803 if (!(found & LENGTH)) { 2804 *derived_key_len = params[n].content.value.a; 2805 found |= LENGTH; 2806 } 2807 break; 2808 case TEE_ATTR_PBKDF2_ITERATION_COUNT: 2809 if (!(found & COUNT)) { 2810 *iteration_count = params[n].content.value.a; 2811 found |= COUNT; 2812 } 2813 break; 2814 default: 2815 /* Unexpected attribute */ 2816 return TEE_ERROR_BAD_PARAMETERS; 2817 } 2818 } 2819 2820 if ((found & (LENGTH|COUNT)) != (LENGTH|COUNT)) 2821 return TEE_ERROR_BAD_PARAMETERS; 2822 2823 return TEE_SUCCESS; 2824 } 2825 #endif 2826 2827 #if defined(CFG_CRYPTO_SM2_KEP) 2828 static TEE_Result get_sm2_kep_params(const TEE_Attribute *params, 2829 uint32_t param_count, 2830 struct ecc_public_key *peer_key, 2831 struct ecc_public_key *peer_eph_key, 2832 struct sm2_kep_parms *kep_parms) 2833 { 2834 TEE_Result res = TEE_ERROR_GENERIC; 2835 size_t n; 2836 enum { 2837 IS_INITIATOR, 2838 PEER_KEY_X, 2839 PEER_KEY_Y, 2840 PEER_EPH_KEY_X, 2841 PEER_EPH_KEY_Y, 2842 INITIATOR_ID, 2843 RESPONDER_ID, 2844 }; 2845 uint8_t mandatory = BIT(IS_INITIATOR) | BIT(PEER_KEY_X) | 2846 BIT(PEER_KEY_Y) | BIT(PEER_EPH_KEY_X) | BIT(PEER_EPH_KEY_Y) | 2847 BIT(INITIATOR_ID) | BIT(RESPONDER_ID); 2848 uint8_t found = 0; 2849 2850 res = crypto_acipher_alloc_ecc_public_key(peer_key, 256); 2851 if (res) 2852 goto out; 2853 2854 res = crypto_acipher_alloc_ecc_public_key(peer_eph_key, 256); 2855 if (res) 2856 goto out; 2857 2858 peer_key->curve = TEE_ECC_CURVE_SM2; 2859 peer_eph_key->curve = TEE_ECC_CURVE_SM2; 2860 2861 for (n = 0; n < param_count; n++) { 2862 const TEE_Attribute *p = ¶ms[n]; 2863 2864 switch (p->attributeID) { 2865 case TEE_ATTR_SM2_KEP_USER: 2866 kep_parms->is_initiator = !p->content.value.a; 2867 found |= BIT(IS_INITIATOR); 2868 break; 2869 case TEE_ATTR_ECC_PUBLIC_VALUE_X: 2870 crypto_bignum_bin2bn(p->content.ref.buffer, 2871 p->content.ref.length, 2872 peer_key->x); 2873 found |= BIT(PEER_KEY_X); 2874 break; 2875 case TEE_ATTR_ECC_PUBLIC_VALUE_Y: 2876 crypto_bignum_bin2bn(p->content.ref.buffer, 2877 p->content.ref.length, 2878 peer_key->y); 2879 found |= BIT(PEER_KEY_Y); 2880 break; 2881 case TEE_ATTR_ECC_EPHEMERAL_PUBLIC_VALUE_X: 2882 crypto_bignum_bin2bn(p->content.ref.buffer, 2883 p->content.ref.length, 2884 peer_eph_key->x); 2885 found |= BIT(PEER_EPH_KEY_X); 2886 break; 2887 case TEE_ATTR_ECC_EPHEMERAL_PUBLIC_VALUE_Y: 2888 crypto_bignum_bin2bn(p->content.ref.buffer, 2889 p->content.ref.length, 2890 peer_eph_key->y); 2891 found |= BIT(PEER_EPH_KEY_Y); 2892 break; 2893 case TEE_ATTR_SM2_ID_INITIATOR: 2894 kep_parms->initiator_id = p->content.ref.buffer; 2895 kep_parms->initiator_id_len = p->content.ref.length; 2896 found |= BIT(INITIATOR_ID); 2897 break; 2898 case TEE_ATTR_SM2_ID_RESPONDER: 2899 kep_parms->responder_id = p->content.ref.buffer; 2900 kep_parms->responder_id_len = p->content.ref.length; 2901 found |= BIT(RESPONDER_ID); 2902 break; 2903 case TEE_ATTR_SM2_KEP_CONFIRMATION_IN: 2904 kep_parms->conf_in = p->content.ref.buffer; 2905 kep_parms->conf_in_len = p->content.ref.length; 2906 break; 2907 case TEE_ATTR_SM2_KEP_CONFIRMATION_OUT: 2908 kep_parms->conf_out = p->content.ref.buffer; 2909 kep_parms->conf_out_len = p->content.ref.length; 2910 break; 2911 default: 2912 /* Unexpected attribute */ 2913 res = TEE_ERROR_BAD_PARAMETERS; 2914 goto out; 2915 } 2916 } 2917 2918 if ((found & mandatory) != mandatory) { 2919 res = TEE_ERROR_BAD_PARAMETERS; 2920 goto out; 2921 } 2922 2923 return TEE_SUCCESS; 2924 out: 2925 crypto_acipher_free_ecc_public_key(peer_key); 2926 crypto_acipher_free_ecc_public_key(peer_eph_key); 2927 return res; 2928 } 2929 #endif 2930 2931 TEE_Result syscall_cryp_derive_key(unsigned long state, 2932 const struct utee_attribute *usr_params, 2933 unsigned long param_count, unsigned long derived_key) 2934 { 2935 TEE_Result res = TEE_ERROR_NOT_SUPPORTED; 2936 struct tee_ta_session *sess; 2937 struct tee_obj *ko; 2938 struct tee_obj *so; 2939 struct tee_cryp_state *cs; 2940 struct tee_cryp_obj_secret *sk; 2941 const struct tee_cryp_obj_type_props *type_props; 2942 TEE_Attribute *params = NULL; 2943 struct user_ta_ctx *utc; 2944 2945 res = tee_ta_get_current_session(&sess); 2946 if (res != TEE_SUCCESS) 2947 return res; 2948 utc = to_user_ta_ctx(sess->ctx); 2949 2950 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 2951 if (res != TEE_SUCCESS) 2952 return res; 2953 2954 size_t alloc_size = 0; 2955 2956 if (MUL_OVERFLOW(sizeof(TEE_Attribute), param_count, &alloc_size)) 2957 return TEE_ERROR_OVERFLOW; 2958 2959 params = malloc(alloc_size); 2960 if (!params) 2961 return TEE_ERROR_OUT_OF_MEMORY; 2962 res = copy_in_attrs(utc, usr_params, param_count, params); 2963 if (res != TEE_SUCCESS) 2964 goto out; 2965 2966 /* Get key set in operation */ 2967 res = tee_obj_get(utc, cs->key1, &ko); 2968 if (res != TEE_SUCCESS) 2969 goto out; 2970 2971 res = tee_obj_get(utc, uref_to_vaddr(derived_key), &so); 2972 if (res != TEE_SUCCESS) 2973 goto out; 2974 2975 /* Find information needed about the object to initialize */ 2976 sk = so->attr; 2977 2978 /* Find description of object */ 2979 type_props = tee_svc_find_type_props(so->info.objectType); 2980 if (!type_props) { 2981 res = TEE_ERROR_NOT_SUPPORTED; 2982 goto out; 2983 } 2984 2985 if (cs->algo == TEE_ALG_DH_DERIVE_SHARED_SECRET) { 2986 struct bignum *pub; 2987 struct bignum *ss; 2988 2989 if (param_count != 1 || 2990 params[0].attributeID != TEE_ATTR_DH_PUBLIC_VALUE) { 2991 res = TEE_ERROR_BAD_PARAMETERS; 2992 goto out; 2993 } 2994 2995 size_t bin_size = params[0].content.ref.length; 2996 2997 if (MUL_OVERFLOW(bin_size, 8, &alloc_size)) { 2998 res = TEE_ERROR_OVERFLOW; 2999 goto out; 3000 } 3001 3002 pub = crypto_bignum_allocate(alloc_size); 3003 ss = crypto_bignum_allocate(alloc_size); 3004 if (pub && ss) { 3005 crypto_bignum_bin2bn(params[0].content.ref.buffer, 3006 bin_size, pub); 3007 res = crypto_acipher_dh_shared_secret(ko->attr, 3008 pub, ss); 3009 if (res == TEE_SUCCESS) { 3010 sk->key_size = crypto_bignum_num_bytes(ss); 3011 crypto_bignum_bn2bin(ss, (uint8_t *)(sk + 1)); 3012 so->info.handleFlags |= 3013 TEE_HANDLE_FLAG_INITIALIZED; 3014 set_attribute(so, type_props, 3015 TEE_ATTR_SECRET_VALUE); 3016 } 3017 } else { 3018 res = TEE_ERROR_OUT_OF_MEMORY; 3019 } 3020 crypto_bignum_free(pub); 3021 crypto_bignum_free(ss); 3022 } else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_ECDH) { 3023 struct ecc_public_key key_public; 3024 uint8_t *pt_secret; 3025 unsigned long pt_secret_len; 3026 3027 if (param_count != 2 || 3028 params[0].attributeID != TEE_ATTR_ECC_PUBLIC_VALUE_X || 3029 params[1].attributeID != TEE_ATTR_ECC_PUBLIC_VALUE_Y) { 3030 res = TEE_ERROR_BAD_PARAMETERS; 3031 goto out; 3032 } 3033 3034 switch (cs->algo) { 3035 case TEE_ALG_ECDH_P192: 3036 alloc_size = 192; 3037 break; 3038 case TEE_ALG_ECDH_P224: 3039 alloc_size = 224; 3040 break; 3041 case TEE_ALG_ECDH_P256: 3042 alloc_size = 256; 3043 break; 3044 case TEE_ALG_ECDH_P384: 3045 alloc_size = 384; 3046 break; 3047 case TEE_ALG_ECDH_P521: 3048 alloc_size = 521; 3049 break; 3050 default: 3051 res = TEE_ERROR_NOT_IMPLEMENTED; 3052 goto out; 3053 } 3054 3055 /* Create the public key */ 3056 res = crypto_acipher_alloc_ecc_public_key(&key_public, 3057 alloc_size); 3058 if (res != TEE_SUCCESS) 3059 goto out; 3060 key_public.curve = ((struct ecc_keypair *)ko->attr)->curve; 3061 crypto_bignum_bin2bn(params[0].content.ref.buffer, 3062 params[0].content.ref.length, 3063 key_public.x); 3064 crypto_bignum_bin2bn(params[1].content.ref.buffer, 3065 params[1].content.ref.length, 3066 key_public.y); 3067 3068 pt_secret = (uint8_t *)(sk + 1); 3069 pt_secret_len = sk->alloc_size; 3070 res = crypto_acipher_ecc_shared_secret(ko->attr, &key_public, 3071 pt_secret, 3072 &pt_secret_len); 3073 3074 if (res == TEE_SUCCESS) { 3075 sk->key_size = pt_secret_len; 3076 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 3077 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 3078 } 3079 3080 /* free the public key */ 3081 crypto_acipher_free_ecc_public_key(&key_public); 3082 } 3083 #if defined(CFG_CRYPTO_HKDF) 3084 else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_HKDF) { 3085 void *salt, *info; 3086 size_t salt_len, info_len, okm_len; 3087 uint32_t hash_id = TEE_ALG_GET_DIGEST_HASH(cs->algo); 3088 struct tee_cryp_obj_secret *ik = ko->attr; 3089 const uint8_t *ikm = (const uint8_t *)(ik + 1); 3090 3091 res = get_hkdf_params(params, param_count, &salt, &salt_len, 3092 &info, &info_len, &okm_len); 3093 if (res != TEE_SUCCESS) 3094 goto out; 3095 3096 /* Requested size must fit into the output object's buffer */ 3097 if (okm_len > ik->alloc_size) { 3098 res = TEE_ERROR_BAD_PARAMETERS; 3099 goto out; 3100 } 3101 3102 res = tee_cryp_hkdf(hash_id, ikm, ik->key_size, salt, salt_len, 3103 info, info_len, (uint8_t *)(sk + 1), 3104 okm_len); 3105 if (res == TEE_SUCCESS) { 3106 sk->key_size = okm_len; 3107 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 3108 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 3109 } 3110 } 3111 #endif 3112 #if defined(CFG_CRYPTO_CONCAT_KDF) 3113 else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_CONCAT_KDF) { 3114 void *info; 3115 size_t info_len, derived_key_len; 3116 uint32_t hash_id = TEE_ALG_GET_DIGEST_HASH(cs->algo); 3117 struct tee_cryp_obj_secret *ss = ko->attr; 3118 const uint8_t *shared_secret = (const uint8_t *)(ss + 1); 3119 3120 res = get_concat_kdf_params(params, param_count, &info, 3121 &info_len, &derived_key_len); 3122 if (res != TEE_SUCCESS) 3123 goto out; 3124 3125 /* Requested size must fit into the output object's buffer */ 3126 if (derived_key_len > ss->alloc_size) { 3127 res = TEE_ERROR_BAD_PARAMETERS; 3128 goto out; 3129 } 3130 3131 res = tee_cryp_concat_kdf(hash_id, shared_secret, ss->key_size, 3132 info, info_len, (uint8_t *)(sk + 1), 3133 derived_key_len); 3134 if (res == TEE_SUCCESS) { 3135 sk->key_size = derived_key_len; 3136 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 3137 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 3138 } 3139 } 3140 #endif 3141 #if defined(CFG_CRYPTO_PBKDF2) 3142 else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_PBKDF2) { 3143 void *salt; 3144 size_t salt_len, iteration_count, derived_key_len; 3145 uint32_t hash_id = TEE_ALG_GET_DIGEST_HASH(cs->algo); 3146 struct tee_cryp_obj_secret *ss = ko->attr; 3147 const uint8_t *password = (const uint8_t *)(ss + 1); 3148 3149 res = get_pbkdf2_params(params, param_count, &salt, &salt_len, 3150 &derived_key_len, &iteration_count); 3151 if (res != TEE_SUCCESS) 3152 goto out; 3153 3154 /* Requested size must fit into the output object's buffer */ 3155 if (derived_key_len > ss->alloc_size) { 3156 res = TEE_ERROR_BAD_PARAMETERS; 3157 goto out; 3158 } 3159 3160 res = tee_cryp_pbkdf2(hash_id, password, ss->key_size, salt, 3161 salt_len, iteration_count, 3162 (uint8_t *)(sk + 1), derived_key_len); 3163 if (res == TEE_SUCCESS) { 3164 sk->key_size = derived_key_len; 3165 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 3166 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 3167 } 3168 } 3169 #endif 3170 #if defined(CFG_CRYPTO_SM2_KEP) 3171 else if (cs->algo == TEE_ALG_SM2_KEP) { 3172 struct ecc_public_key peer_eph_key = { }; 3173 struct ecc_public_key peer_key = { }; 3174 struct sm2_kep_parms kep_parms = { 3175 .out = (uint8_t *)(sk + 1), 3176 .out_len = so->info.maxKeySize, 3177 }; 3178 struct tee_obj *ko2 = NULL; 3179 3180 res = tee_obj_get(utc, cs->key2, &ko2); 3181 if (res != TEE_SUCCESS) 3182 goto out; 3183 3184 res = get_sm2_kep_params(params, param_count, &peer_key, 3185 &peer_eph_key, &kep_parms); 3186 if (res != TEE_SUCCESS) 3187 goto out; 3188 3189 /* 3190 * key1 is our private keypair, key2 is our ephemeral public key 3191 */ 3192 res = crypto_acipher_sm2_kep_derive(ko->attr, /* key1 */ 3193 ko2->attr, /* key2 */ 3194 &peer_key, &peer_eph_key, 3195 &kep_parms); 3196 3197 if (res == TEE_SUCCESS) { 3198 sk->key_size = kep_parms.out_len; 3199 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 3200 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 3201 } 3202 crypto_acipher_free_ecc_public_key(&peer_key); 3203 crypto_acipher_free_ecc_public_key(&peer_eph_key); 3204 } 3205 #endif 3206 else 3207 res = TEE_ERROR_NOT_SUPPORTED; 3208 3209 out: 3210 free_wipe(params); 3211 return res; 3212 } 3213 3214 TEE_Result syscall_cryp_random_number_generate(void *buf, size_t blen) 3215 { 3216 struct tee_ta_session *sess = NULL; 3217 TEE_Result res = TEE_SUCCESS; 3218 3219 res = tee_ta_get_current_session(&sess); 3220 if (res != TEE_SUCCESS) 3221 return res; 3222 3223 res = tee_mmu_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 3224 TEE_MEMORY_ACCESS_WRITE, 3225 (uaddr_t)buf, blen); 3226 if (res != TEE_SUCCESS) 3227 return res; 3228 3229 res = crypto_rng_read(buf, blen); 3230 if (res != TEE_SUCCESS) 3231 return res; 3232 3233 return res; 3234 } 3235 3236 TEE_Result syscall_authenc_init(unsigned long state, const void *nonce, 3237 size_t nonce_len, size_t tag_len, 3238 size_t aad_len, size_t payload_len) 3239 { 3240 struct tee_cryp_obj_secret *key = NULL; 3241 struct tee_ta_session *sess = NULL; 3242 struct tee_cryp_state *cs = NULL; 3243 TEE_Result res = TEE_SUCCESS; 3244 struct tee_obj *o = NULL; 3245 3246 res = tee_ta_get_current_session(&sess); 3247 if (res != TEE_SUCCESS) 3248 return res; 3249 3250 res = tee_mmu_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 3251 TEE_MEMORY_ACCESS_READ | 3252 TEE_MEMORY_ACCESS_ANY_OWNER, 3253 (uaddr_t)nonce, nonce_len); 3254 if (res != TEE_SUCCESS) 3255 return res; 3256 3257 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 3258 if (res != TEE_SUCCESS) 3259 return res; 3260 3261 res = tee_obj_get(to_user_ta_ctx(sess->ctx), cs->key1, &o); 3262 if (res != TEE_SUCCESS) 3263 return res; 3264 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 3265 return TEE_ERROR_BAD_PARAMETERS; 3266 3267 key = o->attr; 3268 res = crypto_authenc_init(cs->ctx, cs->mode, (uint8_t *)(key + 1), 3269 key->key_size, nonce, nonce_len, tag_len, 3270 aad_len, payload_len); 3271 if (res != TEE_SUCCESS) 3272 return res; 3273 3274 cs->ctx_finalize = crypto_authenc_final; 3275 cs->state = CRYP_STATE_INITIALIZED; 3276 3277 return TEE_SUCCESS; 3278 } 3279 3280 TEE_Result syscall_authenc_update_aad(unsigned long state, 3281 const void *aad_data, size_t aad_data_len) 3282 { 3283 TEE_Result res = TEE_SUCCESS; 3284 struct tee_cryp_state *cs; 3285 struct tee_ta_session *sess; 3286 3287 res = tee_ta_get_current_session(&sess); 3288 if (res != TEE_SUCCESS) 3289 return res; 3290 3291 res = tee_mmu_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 3292 TEE_MEMORY_ACCESS_READ | 3293 TEE_MEMORY_ACCESS_ANY_OWNER, 3294 (uaddr_t) aad_data, 3295 aad_data_len); 3296 if (res != TEE_SUCCESS) 3297 return res; 3298 3299 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 3300 if (res != TEE_SUCCESS) 3301 return res; 3302 3303 if (cs->state != CRYP_STATE_INITIALIZED) 3304 return TEE_ERROR_BAD_STATE; 3305 3306 if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_AE) 3307 return TEE_ERROR_BAD_STATE; 3308 3309 res = crypto_authenc_update_aad(cs->ctx, cs->mode, aad_data, 3310 aad_data_len); 3311 if (res != TEE_SUCCESS) 3312 return res; 3313 3314 return TEE_SUCCESS; 3315 } 3316 3317 TEE_Result syscall_authenc_update_payload(unsigned long state, 3318 const void *src_data, 3319 size_t src_len, void *dst_data, 3320 uint64_t *dst_len) 3321 { 3322 struct tee_ta_session *sess = NULL; 3323 struct tee_cryp_state *cs = NULL; 3324 TEE_Result res = TEE_SUCCESS; 3325 size_t dlen = 0; 3326 3327 res = tee_ta_get_current_session(&sess); 3328 if (res != TEE_SUCCESS) 3329 return res; 3330 3331 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 3332 if (res != TEE_SUCCESS) 3333 return res; 3334 3335 if (cs->state != CRYP_STATE_INITIALIZED) 3336 return TEE_ERROR_BAD_STATE; 3337 3338 if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_AE) 3339 return TEE_ERROR_BAD_STATE; 3340 3341 res = tee_mmu_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 3342 TEE_MEMORY_ACCESS_READ | 3343 TEE_MEMORY_ACCESS_ANY_OWNER, 3344 (uaddr_t)src_data, src_len); 3345 if (res != TEE_SUCCESS) 3346 return res; 3347 3348 res = get_user_u64_as_size_t(&dlen, dst_len); 3349 if (res != TEE_SUCCESS) 3350 return res; 3351 3352 res = tee_mmu_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx, 3353 TEE_MEMORY_ACCESS_READ | 3354 TEE_MEMORY_ACCESS_WRITE | 3355 TEE_MEMORY_ACCESS_ANY_OWNER, 3356 (uaddr_t)dst_data, dlen); 3357 if (res != TEE_SUCCESS) 3358 return res; 3359 3360 if (dlen < src_len) { 3361 res = TEE_ERROR_SHORT_BUFFER; 3362 goto out; 3363 } 3364 3365 res = crypto_authenc_update_payload(cs->ctx, cs->mode, src_data, 3366 src_len, dst_data, &dlen); 3367 out: 3368 if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) { 3369 TEE_Result res2 = put_user_u64(dst_len, dlen); 3370 3371 if (res2 != TEE_SUCCESS) 3372 res = res2; 3373 } 3374 3375 return res; 3376 } 3377 3378 TEE_Result syscall_authenc_enc_final(unsigned long state, const void *src_data, 3379 size_t src_len, void *dst_data, 3380 uint64_t *dst_len, void *tag, 3381 uint64_t *tag_len) 3382 { 3383 struct tee_ta_session *sess = NULL; 3384 struct user_mode_ctx *uctx = NULL; 3385 struct tee_cryp_state *cs = NULL; 3386 TEE_Result res = TEE_SUCCESS; 3387 size_t dlen = 0; 3388 size_t tlen = 0; 3389 3390 res = tee_ta_get_current_session(&sess); 3391 if (res != TEE_SUCCESS) 3392 return res; 3393 3394 uctx = &to_user_ta_ctx(sess->ctx)->uctx; 3395 3396 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 3397 if (res != TEE_SUCCESS) 3398 return res; 3399 3400 if (cs->state != CRYP_STATE_INITIALIZED) 3401 return TEE_ERROR_BAD_STATE; 3402 3403 if (cs->mode != TEE_MODE_ENCRYPT) 3404 return TEE_ERROR_BAD_PARAMETERS; 3405 3406 if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_AE) 3407 return TEE_ERROR_BAD_STATE; 3408 3409 res = tee_mmu_check_access_rights(uctx, 3410 TEE_MEMORY_ACCESS_READ | 3411 TEE_MEMORY_ACCESS_ANY_OWNER, 3412 (uaddr_t)src_data, src_len); 3413 if (res != TEE_SUCCESS) 3414 return res; 3415 3416 if (!dst_len) { 3417 dlen = 0; 3418 } else { 3419 res = get_user_u64_as_size_t(&dlen, dst_len); 3420 if (res != TEE_SUCCESS) 3421 return res; 3422 3423 res = tee_mmu_check_access_rights(uctx, 3424 TEE_MEMORY_ACCESS_READ | 3425 TEE_MEMORY_ACCESS_WRITE | 3426 TEE_MEMORY_ACCESS_ANY_OWNER, 3427 (uaddr_t)dst_data, dlen); 3428 if (res != TEE_SUCCESS) 3429 return res; 3430 } 3431 3432 if (dlen < src_len) { 3433 res = TEE_ERROR_SHORT_BUFFER; 3434 goto out; 3435 } 3436 3437 res = get_user_u64_as_size_t(&tlen, tag_len); 3438 if (res != TEE_SUCCESS) 3439 return res; 3440 3441 res = tee_mmu_check_access_rights(uctx, 3442 TEE_MEMORY_ACCESS_READ | 3443 TEE_MEMORY_ACCESS_WRITE | 3444 TEE_MEMORY_ACCESS_ANY_OWNER, 3445 (uaddr_t)tag, tlen); 3446 if (res != TEE_SUCCESS) 3447 return res; 3448 3449 res = crypto_authenc_enc_final(cs->ctx, src_data, src_len, dst_data, 3450 &dlen, tag, &tlen); 3451 3452 out: 3453 if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) { 3454 TEE_Result res2 = TEE_SUCCESS; 3455 3456 if (dst_len != NULL) { 3457 res2 = put_user_u64(dst_len, dlen); 3458 if (res2 != TEE_SUCCESS) 3459 return res2; 3460 } 3461 3462 res2 = put_user_u64(tag_len, tlen); 3463 if (res2 != TEE_SUCCESS) 3464 return res2; 3465 } 3466 3467 return res; 3468 } 3469 3470 TEE_Result syscall_authenc_dec_final(unsigned long state, 3471 const void *src_data, size_t src_len, void *dst_data, 3472 uint64_t *dst_len, const void *tag, size_t tag_len) 3473 { 3474 struct tee_ta_session *sess = NULL; 3475 struct user_mode_ctx *uctx = NULL; 3476 struct tee_cryp_state *cs = NULL; 3477 TEE_Result res = TEE_SUCCESS; 3478 size_t dlen = 0; 3479 3480 res = tee_ta_get_current_session(&sess); 3481 if (res != TEE_SUCCESS) 3482 return res; 3483 3484 uctx = &to_user_ta_ctx(sess->ctx)->uctx; 3485 3486 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 3487 if (res != TEE_SUCCESS) 3488 return res; 3489 3490 if (cs->state != CRYP_STATE_INITIALIZED) 3491 return TEE_ERROR_BAD_STATE; 3492 3493 if (cs->mode != TEE_MODE_DECRYPT) 3494 return TEE_ERROR_BAD_PARAMETERS; 3495 3496 if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_AE) 3497 return TEE_ERROR_BAD_STATE; 3498 3499 res = tee_mmu_check_access_rights(uctx, 3500 TEE_MEMORY_ACCESS_READ | 3501 TEE_MEMORY_ACCESS_ANY_OWNER, 3502 (uaddr_t)src_data, src_len); 3503 if (res != TEE_SUCCESS) 3504 return res; 3505 3506 if (!dst_len) { 3507 dlen = 0; 3508 } else { 3509 res = get_user_u64_as_size_t(&dlen, dst_len); 3510 if (res != TEE_SUCCESS) 3511 return res; 3512 3513 res = tee_mmu_check_access_rights(uctx, 3514 TEE_MEMORY_ACCESS_READ | 3515 TEE_MEMORY_ACCESS_WRITE | 3516 TEE_MEMORY_ACCESS_ANY_OWNER, 3517 (uaddr_t)dst_data, dlen); 3518 if (res != TEE_SUCCESS) 3519 return res; 3520 } 3521 3522 if (dlen < src_len) { 3523 res = TEE_ERROR_SHORT_BUFFER; 3524 goto out; 3525 } 3526 3527 res = tee_mmu_check_access_rights(uctx, TEE_MEMORY_ACCESS_READ, 3528 (uaddr_t)tag, tag_len); 3529 if (res != TEE_SUCCESS) 3530 return res; 3531 3532 res = crypto_authenc_dec_final(cs->ctx, src_data, src_len, dst_data, 3533 &dlen, tag, tag_len); 3534 3535 out: 3536 if ((res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) && 3537 dst_len != NULL) { 3538 TEE_Result res2 = put_user_u64(dst_len, dlen); 3539 3540 if (res2 != TEE_SUCCESS) 3541 return res2; 3542 } 3543 3544 return res; 3545 } 3546 3547 static int pkcs1_get_salt_len(const TEE_Attribute *params, uint32_t num_params, 3548 size_t default_len) 3549 { 3550 size_t n; 3551 3552 assert(default_len < INT_MAX); 3553 3554 for (n = 0; n < num_params; n++) { 3555 if (params[n].attributeID == TEE_ATTR_RSA_PSS_SALT_LENGTH) { 3556 if (params[n].content.value.a < INT_MAX) 3557 return params[n].content.value.a; 3558 break; 3559 } 3560 } 3561 /* 3562 * If salt length isn't provided use the default value which is 3563 * the length of the digest. 3564 */ 3565 return default_len; 3566 } 3567 3568 TEE_Result syscall_asymm_operate(unsigned long state, 3569 const struct utee_attribute *usr_params, 3570 size_t num_params, const void *src_data, size_t src_len, 3571 void *dst_data, uint64_t *dst_len) 3572 { 3573 TEE_Result res; 3574 struct tee_cryp_state *cs; 3575 struct tee_ta_session *sess; 3576 size_t dlen; 3577 struct tee_obj *o; 3578 void *label = NULL; 3579 size_t label_len = 0; 3580 size_t n; 3581 int salt_len; 3582 TEE_Attribute *params = NULL; 3583 struct user_ta_ctx *utc; 3584 3585 res = tee_ta_get_current_session(&sess); 3586 if (res != TEE_SUCCESS) 3587 return res; 3588 utc = to_user_ta_ctx(sess->ctx); 3589 3590 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 3591 if (res != TEE_SUCCESS) 3592 return res; 3593 3594 res = tee_mmu_check_access_rights(&utc->uctx, 3595 TEE_MEMORY_ACCESS_READ | 3596 TEE_MEMORY_ACCESS_ANY_OWNER, 3597 (uaddr_t)src_data, src_len); 3598 if (res != TEE_SUCCESS) 3599 return res; 3600 3601 res = get_user_u64_as_size_t(&dlen, dst_len); 3602 if (res != TEE_SUCCESS) 3603 return res; 3604 3605 res = tee_mmu_check_access_rights(&utc->uctx, 3606 TEE_MEMORY_ACCESS_READ | 3607 TEE_MEMORY_ACCESS_WRITE | 3608 TEE_MEMORY_ACCESS_ANY_OWNER, 3609 (uaddr_t)dst_data, dlen); 3610 if (res != TEE_SUCCESS) 3611 return res; 3612 3613 size_t alloc_size = 0; 3614 3615 if (MUL_OVERFLOW(sizeof(TEE_Attribute), num_params, &alloc_size)) 3616 return TEE_ERROR_OVERFLOW; 3617 3618 params = malloc(alloc_size); 3619 if (!params) 3620 return TEE_ERROR_OUT_OF_MEMORY; 3621 res = copy_in_attrs(utc, usr_params, num_params, params); 3622 if (res != TEE_SUCCESS) 3623 goto out; 3624 3625 res = tee_obj_get(utc, cs->key1, &o); 3626 if (res != TEE_SUCCESS) 3627 goto out; 3628 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 3629 res = TEE_ERROR_GENERIC; 3630 goto out; 3631 } 3632 3633 switch (cs->algo) { 3634 case TEE_ALG_RSA_NOPAD: 3635 if (cs->mode == TEE_MODE_ENCRYPT) { 3636 res = crypto_acipher_rsanopad_encrypt(o->attr, src_data, 3637 src_len, dst_data, 3638 &dlen); 3639 } else if (cs->mode == TEE_MODE_DECRYPT) { 3640 res = crypto_acipher_rsanopad_decrypt(o->attr, src_data, 3641 src_len, dst_data, 3642 &dlen); 3643 } else { 3644 /* 3645 * We will panic because "the mode is not compatible 3646 * with the function" 3647 */ 3648 res = TEE_ERROR_GENERIC; 3649 } 3650 break; 3651 3652 case TEE_ALG_SM2_PKE: 3653 if (cs->mode == TEE_MODE_ENCRYPT) { 3654 res = crypto_acipher_sm2_pke_encrypt(o->attr, src_data, 3655 src_len, dst_data, 3656 &dlen); 3657 } else if (cs->mode == TEE_MODE_DECRYPT) { 3658 res = crypto_acipher_sm2_pke_decrypt(o->attr, src_data, 3659 src_len, dst_data, 3660 &dlen); 3661 } else { 3662 res = TEE_ERROR_GENERIC; 3663 } 3664 break; 3665 3666 case TEE_ALG_RSAES_PKCS1_V1_5: 3667 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1: 3668 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224: 3669 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256: 3670 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384: 3671 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512: 3672 for (n = 0; n < num_params; n++) { 3673 if (params[n].attributeID == TEE_ATTR_RSA_OAEP_LABEL) { 3674 label = params[n].content.ref.buffer; 3675 label_len = params[n].content.ref.length; 3676 break; 3677 } 3678 } 3679 3680 if (cs->mode == TEE_MODE_ENCRYPT) { 3681 res = crypto_acipher_rsaes_encrypt(cs->algo, o->attr, 3682 label, label_len, 3683 src_data, src_len, 3684 dst_data, &dlen); 3685 } else if (cs->mode == TEE_MODE_DECRYPT) { 3686 res = crypto_acipher_rsaes_decrypt( 3687 cs->algo, o->attr, label, label_len, 3688 src_data, src_len, dst_data, &dlen); 3689 } else { 3690 res = TEE_ERROR_BAD_PARAMETERS; 3691 } 3692 break; 3693 3694 #if defined(CFG_CRYPTO_RSASSA_NA1) 3695 case TEE_ALG_RSASSA_PKCS1_V1_5: 3696 #endif 3697 case TEE_ALG_RSASSA_PKCS1_V1_5_MD5: 3698 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1: 3699 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224: 3700 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256: 3701 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384: 3702 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512: 3703 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1: 3704 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224: 3705 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256: 3706 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384: 3707 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512: 3708 if (cs->mode != TEE_MODE_SIGN) { 3709 res = TEE_ERROR_BAD_PARAMETERS; 3710 break; 3711 } 3712 salt_len = pkcs1_get_salt_len(params, num_params, src_len); 3713 res = crypto_acipher_rsassa_sign(cs->algo, o->attr, salt_len, 3714 src_data, src_len, dst_data, 3715 &dlen); 3716 break; 3717 3718 case TEE_ALG_DSA_SHA1: 3719 case TEE_ALG_DSA_SHA224: 3720 case TEE_ALG_DSA_SHA256: 3721 res = crypto_acipher_dsa_sign(cs->algo, o->attr, src_data, 3722 src_len, dst_data, &dlen); 3723 break; 3724 case TEE_ALG_ECDSA_P192: 3725 case TEE_ALG_ECDSA_P224: 3726 case TEE_ALG_ECDSA_P256: 3727 case TEE_ALG_ECDSA_P384: 3728 case TEE_ALG_ECDSA_P521: 3729 res = crypto_acipher_ecc_sign(cs->algo, o->attr, src_data, 3730 src_len, dst_data, &dlen); 3731 break; 3732 case TEE_ALG_SM2_DSA_SM3: 3733 res = crypto_acipher_sm2_dsa_sign(cs->algo, o->attr, src_data, 3734 src_len, dst_data, &dlen); 3735 break; 3736 3737 default: 3738 res = TEE_ERROR_BAD_PARAMETERS; 3739 break; 3740 } 3741 3742 out: 3743 free_wipe(params); 3744 3745 if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) { 3746 TEE_Result res2 = put_user_u64(dst_len, dlen); 3747 3748 if (res2 != TEE_SUCCESS) 3749 return res2; 3750 } 3751 3752 return res; 3753 } 3754 3755 TEE_Result syscall_asymm_verify(unsigned long state, 3756 const struct utee_attribute *usr_params, 3757 size_t num_params, const void *data, size_t data_len, 3758 const void *sig, size_t sig_len) 3759 { 3760 struct tee_ta_session *sess = NULL; 3761 struct tee_cryp_state *cs = NULL; 3762 struct user_ta_ctx *utc = NULL; 3763 TEE_Result res = TEE_SUCCESS; 3764 TEE_Attribute *params = NULL; 3765 struct tee_obj *o = NULL; 3766 size_t hash_size = 0; 3767 uint32_t hash_algo = 0; 3768 int salt_len = 0; 3769 3770 res = tee_ta_get_current_session(&sess); 3771 if (res != TEE_SUCCESS) 3772 return res; 3773 utc = to_user_ta_ctx(sess->ctx); 3774 3775 res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs); 3776 if (res != TEE_SUCCESS) 3777 return res; 3778 3779 if (cs->mode != TEE_MODE_VERIFY) 3780 return TEE_ERROR_BAD_PARAMETERS; 3781 3782 res = tee_mmu_check_access_rights(&utc->uctx, 3783 TEE_MEMORY_ACCESS_READ | 3784 TEE_MEMORY_ACCESS_ANY_OWNER, 3785 (uaddr_t)data, data_len); 3786 if (res != TEE_SUCCESS) 3787 return res; 3788 3789 res = tee_mmu_check_access_rights(&utc->uctx, 3790 TEE_MEMORY_ACCESS_READ | 3791 TEE_MEMORY_ACCESS_ANY_OWNER, 3792 (uaddr_t)sig, sig_len); 3793 if (res != TEE_SUCCESS) 3794 return res; 3795 3796 size_t alloc_size = 0; 3797 3798 if (MUL_OVERFLOW(sizeof(TEE_Attribute), num_params, &alloc_size)) 3799 return TEE_ERROR_OVERFLOW; 3800 3801 params = malloc(alloc_size); 3802 if (!params) 3803 return TEE_ERROR_OUT_OF_MEMORY; 3804 res = copy_in_attrs(utc, usr_params, num_params, params); 3805 if (res != TEE_SUCCESS) 3806 goto out; 3807 3808 res = tee_obj_get(utc, cs->key1, &o); 3809 if (res != TEE_SUCCESS) 3810 goto out; 3811 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 3812 res = TEE_ERROR_BAD_PARAMETERS; 3813 goto out; 3814 } 3815 3816 switch (TEE_ALG_GET_MAIN_ALG(cs->algo)) { 3817 case TEE_MAIN_ALGO_RSA: 3818 if (cs->algo != TEE_ALG_RSASSA_PKCS1_V1_5) { 3819 hash_algo = TEE_DIGEST_HASH_TO_ALGO(cs->algo); 3820 res = tee_alg_get_digest_size(hash_algo, &hash_size); 3821 if (res != TEE_SUCCESS) 3822 break; 3823 if (data_len != hash_size) { 3824 res = TEE_ERROR_BAD_PARAMETERS; 3825 break; 3826 } 3827 salt_len = pkcs1_get_salt_len(params, num_params, 3828 hash_size); 3829 } 3830 res = crypto_acipher_rsassa_verify(cs->algo, o->attr, salt_len, 3831 data, data_len, sig, 3832 sig_len); 3833 break; 3834 3835 case TEE_MAIN_ALGO_DSA: 3836 hash_algo = TEE_DIGEST_HASH_TO_ALGO(cs->algo); 3837 res = tee_alg_get_digest_size(hash_algo, &hash_size); 3838 if (res != TEE_SUCCESS) 3839 break; 3840 /* 3841 * Depending on the DSA algorithm (NIST), the digital signature 3842 * output size may be truncated to the size of a key pair 3843 * (Q prime size). Q prime size must be less or equal than the 3844 * hash output length of the hash algorithm involved. 3845 */ 3846 if (data_len > hash_size) { 3847 res = TEE_ERROR_BAD_PARAMETERS; 3848 break; 3849 } 3850 res = crypto_acipher_dsa_verify(cs->algo, o->attr, data, 3851 data_len, sig, sig_len); 3852 break; 3853 3854 case TEE_MAIN_ALGO_ECDSA: 3855 res = crypto_acipher_ecc_verify(cs->algo, o->attr, data, 3856 data_len, sig, sig_len); 3857 break; 3858 3859 case TEE_MAIN_ALGO_SM2_DSA_SM3: 3860 res = crypto_acipher_sm2_dsa_verify(cs->algo, o->attr, data, 3861 data_len, sig, sig_len); 3862 break; 3863 3864 default: 3865 res = TEE_ERROR_NOT_SUPPORTED; 3866 } 3867 3868 out: 3869 free_wipe(params); 3870 return res; 3871 } 3872