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