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