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