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