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