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