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, uint32_t algo); 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, cs->algo); 1920 1921 switch (TEE_ALG_GET_CLASS(cs->algo)) { 1922 case TEE_OPERATION_CIPHER: 1923 crypto_cipher_free_ctx(cs->ctx, cs->algo); 1924 break; 1925 case TEE_OPERATION_AE: 1926 crypto_authenc_free_ctx(cs->ctx, cs->algo); 1927 break; 1928 case TEE_OPERATION_DIGEST: 1929 crypto_hash_free_ctx(cs->ctx, cs->algo); 1930 break; 1931 case TEE_OPERATION_MAC: 1932 crypto_mac_free_ctx(cs->ctx, cs->algo); 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 cs_src->algo); 2174 break; 2175 case TEE_OPERATION_AE: 2176 crypto_authenc_copy_state(cs_dst->ctx, cs_src->ctx, 2177 cs_src->algo); 2178 break; 2179 case TEE_OPERATION_DIGEST: 2180 crypto_hash_copy_state(cs_dst->ctx, cs_src->ctx, cs_src->algo); 2181 break; 2182 case TEE_OPERATION_MAC: 2183 crypto_mac_copy_state(cs_dst->ctx, cs_src->ctx, cs_src->algo); 2184 break; 2185 default: 2186 return TEE_ERROR_BAD_STATE; 2187 } 2188 2189 cs_dst->state = cs_src->state; 2190 2191 return TEE_SUCCESS; 2192 } 2193 2194 void tee_svc_cryp_free_states(struct user_ta_ctx *utc) 2195 { 2196 struct tee_cryp_state_head *states = &utc->cryp_states; 2197 2198 while (!TAILQ_EMPTY(states)) 2199 cryp_state_free(utc, TAILQ_FIRST(states)); 2200 } 2201 2202 TEE_Result syscall_cryp_state_free(unsigned long state) 2203 { 2204 TEE_Result res; 2205 struct tee_cryp_state *cs; 2206 struct tee_ta_session *sess; 2207 2208 res = tee_ta_get_current_session(&sess); 2209 if (res != TEE_SUCCESS) 2210 return res; 2211 2212 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(state), &cs); 2213 if (res != TEE_SUCCESS) 2214 return res; 2215 cryp_state_free(to_user_ta_ctx(sess->ctx), cs); 2216 return TEE_SUCCESS; 2217 } 2218 2219 TEE_Result syscall_hash_init(unsigned long state, 2220 const void *iv __maybe_unused, 2221 size_t iv_len __maybe_unused) 2222 { 2223 TEE_Result res; 2224 struct tee_cryp_state *cs; 2225 struct tee_ta_session *sess; 2226 2227 res = tee_ta_get_current_session(&sess); 2228 if (res != TEE_SUCCESS) 2229 return res; 2230 2231 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(state), &cs); 2232 if (res != TEE_SUCCESS) 2233 return res; 2234 2235 switch (TEE_ALG_GET_CLASS(cs->algo)) { 2236 case TEE_OPERATION_DIGEST: 2237 res = crypto_hash_init(cs->ctx, cs->algo); 2238 if (res != TEE_SUCCESS) 2239 return res; 2240 break; 2241 case TEE_OPERATION_MAC: 2242 { 2243 struct tee_obj *o; 2244 struct tee_cryp_obj_secret *key; 2245 2246 res = tee_obj_get(to_user_ta_ctx(sess->ctx), 2247 cs->key1, &o); 2248 if (res != TEE_SUCCESS) 2249 return res; 2250 if ((o->info.handleFlags & 2251 TEE_HANDLE_FLAG_INITIALIZED) == 0) 2252 return TEE_ERROR_BAD_PARAMETERS; 2253 2254 key = (struct tee_cryp_obj_secret *)o->attr; 2255 res = crypto_mac_init(cs->ctx, cs->algo, 2256 (void *)(key + 1), key->key_size); 2257 if (res != TEE_SUCCESS) 2258 return res; 2259 break; 2260 } 2261 default: 2262 return TEE_ERROR_BAD_PARAMETERS; 2263 } 2264 2265 cs->state = CRYP_STATE_INITIALIZED; 2266 2267 return TEE_SUCCESS; 2268 } 2269 2270 TEE_Result syscall_hash_update(unsigned long state, const void *chunk, 2271 size_t chunk_size) 2272 { 2273 TEE_Result res; 2274 struct tee_cryp_state *cs; 2275 struct tee_ta_session *sess; 2276 2277 /* No data, but size provided isn't valid parameters. */ 2278 if (!chunk && chunk_size) 2279 return TEE_ERROR_BAD_PARAMETERS; 2280 2281 /* Zero length hash is valid, but nothing we need to do. */ 2282 if (!chunk_size) 2283 return TEE_SUCCESS; 2284 2285 res = tee_ta_get_current_session(&sess); 2286 if (res != TEE_SUCCESS) 2287 return res; 2288 2289 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 2290 TEE_MEMORY_ACCESS_READ | 2291 TEE_MEMORY_ACCESS_ANY_OWNER, 2292 (uaddr_t)chunk, chunk_size); 2293 if (res != TEE_SUCCESS) 2294 return res; 2295 2296 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(state), &cs); 2297 if (res != TEE_SUCCESS) 2298 return res; 2299 2300 if (cs->state != CRYP_STATE_INITIALIZED) 2301 return TEE_ERROR_BAD_STATE; 2302 2303 switch (TEE_ALG_GET_CLASS(cs->algo)) { 2304 case TEE_OPERATION_DIGEST: 2305 res = crypto_hash_update(cs->ctx, cs->algo, chunk, chunk_size); 2306 if (res != TEE_SUCCESS) 2307 return res; 2308 break; 2309 case TEE_OPERATION_MAC: 2310 res = crypto_mac_update(cs->ctx, cs->algo, chunk, chunk_size); 2311 if (res != TEE_SUCCESS) 2312 return res; 2313 break; 2314 default: 2315 return TEE_ERROR_BAD_PARAMETERS; 2316 } 2317 2318 return TEE_SUCCESS; 2319 } 2320 2321 TEE_Result syscall_hash_final(unsigned long state, const void *chunk, 2322 size_t chunk_size, void *hash, uint64_t *hash_len) 2323 { 2324 TEE_Result res, res2; 2325 size_t hash_size; 2326 size_t hlen = 0; 2327 struct tee_cryp_state *cs; 2328 struct tee_ta_session *sess; 2329 2330 /* No data, but size provided isn't valid parameters. */ 2331 if (!chunk && chunk_size) 2332 return TEE_ERROR_BAD_PARAMETERS; 2333 2334 res = tee_ta_get_current_session(&sess); 2335 if (res != TEE_SUCCESS) 2336 return res; 2337 2338 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 2339 TEE_MEMORY_ACCESS_READ | 2340 TEE_MEMORY_ACCESS_ANY_OWNER, 2341 (uaddr_t)chunk, chunk_size); 2342 if (res != TEE_SUCCESS) 2343 return res; 2344 2345 res = get_user_u64_as_size_t(&hlen, hash_len); 2346 if (res != TEE_SUCCESS) 2347 return res; 2348 2349 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 2350 TEE_MEMORY_ACCESS_READ | 2351 TEE_MEMORY_ACCESS_WRITE | 2352 TEE_MEMORY_ACCESS_ANY_OWNER, 2353 (uaddr_t)hash, hlen); 2354 if (res != TEE_SUCCESS) 2355 return res; 2356 2357 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(state), &cs); 2358 if (res != TEE_SUCCESS) 2359 return res; 2360 2361 if (cs->state != CRYP_STATE_INITIALIZED) 2362 return TEE_ERROR_BAD_STATE; 2363 2364 switch (TEE_ALG_GET_CLASS(cs->algo)) { 2365 case TEE_OPERATION_DIGEST: 2366 res = tee_hash_get_digest_size(cs->algo, &hash_size); 2367 if (res != TEE_SUCCESS) 2368 return res; 2369 if (hlen < hash_size) { 2370 res = TEE_ERROR_SHORT_BUFFER; 2371 goto out; 2372 } 2373 2374 if (chunk_size) { 2375 res = crypto_hash_update(cs->ctx, cs->algo, chunk, 2376 chunk_size); 2377 if (res != TEE_SUCCESS) 2378 return res; 2379 } 2380 2381 res = crypto_hash_final(cs->ctx, cs->algo, hash, hash_size); 2382 if (res != TEE_SUCCESS) 2383 return res; 2384 break; 2385 2386 case TEE_OPERATION_MAC: 2387 res = tee_mac_get_digest_size(cs->algo, &hash_size); 2388 if (res != TEE_SUCCESS) 2389 return res; 2390 if (hlen < hash_size) { 2391 res = TEE_ERROR_SHORT_BUFFER; 2392 goto out; 2393 } 2394 2395 if (chunk_size) { 2396 res = crypto_mac_update(cs->ctx, cs->algo, chunk, 2397 chunk_size); 2398 if (res != TEE_SUCCESS) 2399 return res; 2400 } 2401 2402 res = crypto_mac_final(cs->ctx, cs->algo, hash, hash_size); 2403 if (res != TEE_SUCCESS) 2404 return res; 2405 break; 2406 2407 default: 2408 return TEE_ERROR_BAD_PARAMETERS; 2409 } 2410 out: 2411 res2 = put_user_u64(hash_len, hash_size); 2412 if (res2 != TEE_SUCCESS) 2413 return res2; 2414 return res; 2415 } 2416 2417 TEE_Result syscall_cipher_init(unsigned long state, const void *iv, 2418 size_t iv_len) 2419 { 2420 TEE_Result res; 2421 struct tee_cryp_state *cs; 2422 struct tee_ta_session *sess; 2423 struct tee_obj *o; 2424 struct tee_cryp_obj_secret *key1; 2425 struct user_ta_ctx *utc; 2426 2427 res = tee_ta_get_current_session(&sess); 2428 if (res != TEE_SUCCESS) 2429 return res; 2430 utc = to_user_ta_ctx(sess->ctx); 2431 2432 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(state), &cs); 2433 if (res != TEE_SUCCESS) 2434 return res; 2435 2436 if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_CIPHER) 2437 return TEE_ERROR_BAD_STATE; 2438 2439 res = tee_mmu_check_access_rights(utc, 2440 TEE_MEMORY_ACCESS_READ | 2441 TEE_MEMORY_ACCESS_ANY_OWNER, 2442 (uaddr_t) iv, iv_len); 2443 if (res != TEE_SUCCESS) 2444 return res; 2445 2446 res = tee_obj_get(utc, cs->key1, &o); 2447 if (res != TEE_SUCCESS) 2448 return res; 2449 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 2450 return TEE_ERROR_BAD_PARAMETERS; 2451 2452 key1 = o->attr; 2453 2454 if (tee_obj_get(utc, cs->key2, &o) == TEE_SUCCESS) { 2455 struct tee_cryp_obj_secret *key2 = o->attr; 2456 2457 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 2458 return TEE_ERROR_BAD_PARAMETERS; 2459 2460 res = crypto_cipher_init(cs->ctx, cs->algo, cs->mode, 2461 (uint8_t *)(key1 + 1), key1->key_size, 2462 (uint8_t *)(key2 + 1), key2->key_size, 2463 iv, iv_len); 2464 } else { 2465 res = crypto_cipher_init(cs->ctx, cs->algo, cs->mode, 2466 (uint8_t *)(key1 + 1), key1->key_size, 2467 NULL, 0, iv, iv_len); 2468 } 2469 if (res != TEE_SUCCESS) 2470 return res; 2471 2472 cs->ctx_finalize = crypto_cipher_final; 2473 cs->state = CRYP_STATE_INITIALIZED; 2474 2475 return TEE_SUCCESS; 2476 } 2477 2478 static TEE_Result tee_svc_cipher_update_helper(unsigned long state, 2479 bool last_block, const void *src, size_t src_len, 2480 void *dst, uint64_t *dst_len) 2481 { 2482 TEE_Result res; 2483 struct tee_cryp_state *cs; 2484 struct tee_ta_session *sess; 2485 size_t dlen = 0; 2486 2487 res = tee_ta_get_current_session(&sess); 2488 if (res != TEE_SUCCESS) 2489 return res; 2490 2491 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(state), &cs); 2492 if (res != TEE_SUCCESS) 2493 return res; 2494 2495 if (cs->state != CRYP_STATE_INITIALIZED) 2496 return TEE_ERROR_BAD_STATE; 2497 2498 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 2499 TEE_MEMORY_ACCESS_READ | 2500 TEE_MEMORY_ACCESS_ANY_OWNER, 2501 (uaddr_t)src, src_len); 2502 if (res != TEE_SUCCESS) 2503 return res; 2504 2505 if (!dst_len) { 2506 dlen = 0; 2507 } else { 2508 res = get_user_u64_as_size_t(&dlen, dst_len); 2509 if (res != TEE_SUCCESS) 2510 return res; 2511 2512 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 2513 TEE_MEMORY_ACCESS_READ | 2514 TEE_MEMORY_ACCESS_WRITE | 2515 TEE_MEMORY_ACCESS_ANY_OWNER, 2516 (uaddr_t)dst, dlen); 2517 if (res != TEE_SUCCESS) 2518 return res; 2519 } 2520 2521 if (dlen < src_len) { 2522 res = TEE_ERROR_SHORT_BUFFER; 2523 goto out; 2524 } 2525 2526 if (src_len > 0) { 2527 /* Permit src_len == 0 to finalize the operation */ 2528 res = tee_do_cipher_update(cs->ctx, cs->algo, cs->mode, 2529 last_block, src, src_len, dst); 2530 } 2531 2532 if (last_block && cs->ctx_finalize != NULL) { 2533 cs->ctx_finalize(cs->ctx, cs->algo); 2534 cs->ctx_finalize = NULL; 2535 } 2536 2537 out: 2538 if ((res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) && 2539 dst_len != NULL) { 2540 TEE_Result res2; 2541 2542 res2 = put_user_u64(dst_len, src_len); 2543 if (res2 != TEE_SUCCESS) 2544 res = res2; 2545 } 2546 2547 return res; 2548 } 2549 2550 TEE_Result syscall_cipher_update(unsigned long state, const void *src, 2551 size_t src_len, void *dst, uint64_t *dst_len) 2552 { 2553 return tee_svc_cipher_update_helper(state, false /* last_block */, 2554 src, src_len, dst, dst_len); 2555 } 2556 2557 TEE_Result syscall_cipher_final(unsigned long state, const void *src, 2558 size_t src_len, void *dst, uint64_t *dst_len) 2559 { 2560 return tee_svc_cipher_update_helper(state, true /* last_block */, 2561 src, src_len, dst, dst_len); 2562 } 2563 2564 #if defined(CFG_CRYPTO_HKDF) 2565 static TEE_Result get_hkdf_params(const TEE_Attribute *params, 2566 uint32_t param_count, 2567 void **salt, size_t *salt_len, void **info, 2568 size_t *info_len, size_t *okm_len) 2569 { 2570 size_t n; 2571 enum { SALT = 0x1, LENGTH = 0x2, INFO = 0x4 }; 2572 uint8_t found = 0; 2573 2574 *salt = *info = NULL; 2575 *salt_len = *info_len = *okm_len = 0; 2576 2577 for (n = 0; n < param_count; n++) { 2578 switch (params[n].attributeID) { 2579 case TEE_ATTR_HKDF_SALT: 2580 if (!(found & SALT)) { 2581 *salt = params[n].content.ref.buffer; 2582 *salt_len = params[n].content.ref.length; 2583 found |= SALT; 2584 } 2585 break; 2586 case TEE_ATTR_HKDF_OKM_LENGTH: 2587 if (!(found & LENGTH)) { 2588 *okm_len = params[n].content.value.a; 2589 found |= LENGTH; 2590 } 2591 break; 2592 case TEE_ATTR_HKDF_INFO: 2593 if (!(found & INFO)) { 2594 *info = params[n].content.ref.buffer; 2595 *info_len = params[n].content.ref.length; 2596 found |= INFO; 2597 } 2598 break; 2599 default: 2600 /* Unexpected attribute */ 2601 return TEE_ERROR_BAD_PARAMETERS; 2602 } 2603 2604 } 2605 2606 if (!(found & LENGTH)) 2607 return TEE_ERROR_BAD_PARAMETERS; 2608 2609 return TEE_SUCCESS; 2610 } 2611 #endif 2612 2613 #if defined(CFG_CRYPTO_CONCAT_KDF) 2614 static TEE_Result get_concat_kdf_params(const TEE_Attribute *params, 2615 uint32_t param_count, 2616 void **other_info, 2617 size_t *other_info_len, 2618 size_t *derived_key_len) 2619 { 2620 size_t n; 2621 enum { LENGTH = 0x1, INFO = 0x2 }; 2622 uint8_t found = 0; 2623 2624 *other_info = NULL; 2625 *other_info_len = *derived_key_len = 0; 2626 2627 for (n = 0; n < param_count; n++) { 2628 switch (params[n].attributeID) { 2629 case TEE_ATTR_CONCAT_KDF_OTHER_INFO: 2630 if (!(found & INFO)) { 2631 *other_info = params[n].content.ref.buffer; 2632 *other_info_len = params[n].content.ref.length; 2633 found |= INFO; 2634 } 2635 break; 2636 case TEE_ATTR_CONCAT_KDF_DKM_LENGTH: 2637 if (!(found & LENGTH)) { 2638 *derived_key_len = params[n].content.value.a; 2639 found |= LENGTH; 2640 } 2641 break; 2642 default: 2643 /* Unexpected attribute */ 2644 return TEE_ERROR_BAD_PARAMETERS; 2645 } 2646 } 2647 2648 if (!(found & LENGTH)) 2649 return TEE_ERROR_BAD_PARAMETERS; 2650 2651 return TEE_SUCCESS; 2652 } 2653 #endif 2654 2655 #if defined(CFG_CRYPTO_PBKDF2) 2656 static TEE_Result get_pbkdf2_params(const TEE_Attribute *params, 2657 uint32_t param_count, void **salt, 2658 size_t *salt_len, size_t *derived_key_len, 2659 size_t *iteration_count) 2660 { 2661 size_t n; 2662 enum { SALT = 0x1, LENGTH = 0x2, COUNT = 0x4 }; 2663 uint8_t found = 0; 2664 2665 *salt = NULL; 2666 *salt_len = *derived_key_len = *iteration_count = 0; 2667 2668 for (n = 0; n < param_count; n++) { 2669 switch (params[n].attributeID) { 2670 case TEE_ATTR_PBKDF2_SALT: 2671 if (!(found & SALT)) { 2672 *salt = params[n].content.ref.buffer; 2673 *salt_len = params[n].content.ref.length; 2674 found |= SALT; 2675 } 2676 break; 2677 case TEE_ATTR_PBKDF2_DKM_LENGTH: 2678 if (!(found & LENGTH)) { 2679 *derived_key_len = params[n].content.value.a; 2680 found |= LENGTH; 2681 } 2682 break; 2683 case TEE_ATTR_PBKDF2_ITERATION_COUNT: 2684 if (!(found & COUNT)) { 2685 *iteration_count = params[n].content.value.a; 2686 found |= COUNT; 2687 } 2688 break; 2689 default: 2690 /* Unexpected attribute */ 2691 return TEE_ERROR_BAD_PARAMETERS; 2692 } 2693 } 2694 2695 if ((found & (LENGTH|COUNT)) != (LENGTH|COUNT)) 2696 return TEE_ERROR_BAD_PARAMETERS; 2697 2698 return TEE_SUCCESS; 2699 } 2700 #endif 2701 2702 TEE_Result syscall_cryp_derive_key(unsigned long state, 2703 const struct utee_attribute *usr_params, 2704 unsigned long param_count, unsigned long derived_key) 2705 { 2706 TEE_Result res = TEE_ERROR_NOT_SUPPORTED; 2707 struct tee_ta_session *sess; 2708 struct tee_obj *ko; 2709 struct tee_obj *so; 2710 struct tee_cryp_state *cs; 2711 struct tee_cryp_obj_secret *sk; 2712 const struct tee_cryp_obj_type_props *type_props; 2713 TEE_Attribute *params = NULL; 2714 struct user_ta_ctx *utc; 2715 2716 res = tee_ta_get_current_session(&sess); 2717 if (res != TEE_SUCCESS) 2718 return res; 2719 utc = to_user_ta_ctx(sess->ctx); 2720 2721 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(state), &cs); 2722 if (res != TEE_SUCCESS) 2723 return res; 2724 2725 size_t alloc_size = 0; 2726 2727 if (MUL_OVERFLOW(sizeof(TEE_Attribute), param_count, &alloc_size)) 2728 return TEE_ERROR_OVERFLOW; 2729 2730 params = malloc(alloc_size); 2731 if (!params) 2732 return TEE_ERROR_OUT_OF_MEMORY; 2733 res = copy_in_attrs(utc, usr_params, param_count, params); 2734 if (res != TEE_SUCCESS) 2735 goto out; 2736 2737 /* Get key set in operation */ 2738 res = tee_obj_get(utc, cs->key1, &ko); 2739 if (res != TEE_SUCCESS) 2740 goto out; 2741 2742 res = tee_obj_get(utc, tee_svc_uref_to_vaddr(derived_key), &so); 2743 if (res != TEE_SUCCESS) 2744 goto out; 2745 2746 /* Find information needed about the object to initialize */ 2747 sk = so->attr; 2748 2749 /* Find description of object */ 2750 type_props = tee_svc_find_type_props(so->info.objectType); 2751 if (!type_props) { 2752 res = TEE_ERROR_NOT_SUPPORTED; 2753 goto out; 2754 } 2755 2756 if (cs->algo == TEE_ALG_DH_DERIVE_SHARED_SECRET) { 2757 struct bignum *pub; 2758 struct bignum *ss; 2759 2760 if (param_count != 1 || 2761 params[0].attributeID != TEE_ATTR_DH_PUBLIC_VALUE) { 2762 res = TEE_ERROR_BAD_PARAMETERS; 2763 goto out; 2764 } 2765 2766 size_t bin_size = params[0].content.ref.length; 2767 2768 if (MUL_OVERFLOW(bin_size, 8, &alloc_size)) { 2769 res = TEE_ERROR_OVERFLOW; 2770 goto out; 2771 } 2772 2773 pub = crypto_bignum_allocate(alloc_size); 2774 ss = crypto_bignum_allocate(alloc_size); 2775 if (pub && ss) { 2776 crypto_bignum_bin2bn(params[0].content.ref.buffer, 2777 bin_size, pub); 2778 res = crypto_acipher_dh_shared_secret(ko->attr, 2779 pub, ss); 2780 if (res == TEE_SUCCESS) { 2781 sk->key_size = crypto_bignum_num_bytes(ss); 2782 crypto_bignum_bn2bin(ss, (uint8_t *)(sk + 1)); 2783 so->info.handleFlags |= 2784 TEE_HANDLE_FLAG_INITIALIZED; 2785 set_attribute(so, type_props, 2786 TEE_ATTR_SECRET_VALUE); 2787 } 2788 } else { 2789 res = TEE_ERROR_OUT_OF_MEMORY; 2790 } 2791 crypto_bignum_free(pub); 2792 crypto_bignum_free(ss); 2793 } else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_ECDH) { 2794 struct ecc_public_key key_public; 2795 uint8_t *pt_secret; 2796 unsigned long pt_secret_len; 2797 2798 if (param_count != 2 || 2799 params[0].attributeID != TEE_ATTR_ECC_PUBLIC_VALUE_X || 2800 params[1].attributeID != TEE_ATTR_ECC_PUBLIC_VALUE_Y) { 2801 res = TEE_ERROR_BAD_PARAMETERS; 2802 goto out; 2803 } 2804 2805 switch (cs->algo) { 2806 case TEE_ALG_ECDH_P192: 2807 alloc_size = 192; 2808 break; 2809 case TEE_ALG_ECDH_P224: 2810 alloc_size = 224; 2811 break; 2812 case TEE_ALG_ECDH_P256: 2813 alloc_size = 256; 2814 break; 2815 case TEE_ALG_ECDH_P384: 2816 alloc_size = 384; 2817 break; 2818 case TEE_ALG_ECDH_P521: 2819 alloc_size = 521; 2820 break; 2821 default: 2822 res = TEE_ERROR_NOT_IMPLEMENTED; 2823 goto out; 2824 } 2825 2826 /* Create the public key */ 2827 res = crypto_acipher_alloc_ecc_public_key(&key_public, 2828 alloc_size); 2829 if (res != TEE_SUCCESS) 2830 goto out; 2831 key_public.curve = ((struct ecc_keypair *)ko->attr)->curve; 2832 crypto_bignum_bin2bn(params[0].content.ref.buffer, 2833 params[0].content.ref.length, 2834 key_public.x); 2835 crypto_bignum_bin2bn(params[1].content.ref.buffer, 2836 params[1].content.ref.length, 2837 key_public.y); 2838 2839 pt_secret = (uint8_t *)(sk + 1); 2840 pt_secret_len = sk->alloc_size; 2841 res = crypto_acipher_ecc_shared_secret(ko->attr, &key_public, 2842 pt_secret, 2843 &pt_secret_len); 2844 2845 if (res == TEE_SUCCESS) { 2846 sk->key_size = pt_secret_len; 2847 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 2848 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 2849 } 2850 2851 /* free the public key */ 2852 crypto_acipher_free_ecc_public_key(&key_public); 2853 } 2854 #if defined(CFG_CRYPTO_HKDF) 2855 else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_HKDF) { 2856 void *salt, *info; 2857 size_t salt_len, info_len, okm_len; 2858 uint32_t hash_id = TEE_ALG_GET_DIGEST_HASH(cs->algo); 2859 struct tee_cryp_obj_secret *ik = ko->attr; 2860 const uint8_t *ikm = (const uint8_t *)(ik + 1); 2861 2862 res = get_hkdf_params(params, param_count, &salt, &salt_len, 2863 &info, &info_len, &okm_len); 2864 if (res != TEE_SUCCESS) 2865 goto out; 2866 2867 /* Requested size must fit into the output object's buffer */ 2868 if (okm_len > ik->alloc_size) { 2869 res = TEE_ERROR_BAD_PARAMETERS; 2870 goto out; 2871 } 2872 2873 res = tee_cryp_hkdf(hash_id, ikm, ik->key_size, salt, salt_len, 2874 info, info_len, (uint8_t *)(sk + 1), 2875 okm_len); 2876 if (res == TEE_SUCCESS) { 2877 sk->key_size = okm_len; 2878 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 2879 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 2880 } 2881 } 2882 #endif 2883 #if defined(CFG_CRYPTO_CONCAT_KDF) 2884 else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_CONCAT_KDF) { 2885 void *info; 2886 size_t info_len, derived_key_len; 2887 uint32_t hash_id = TEE_ALG_GET_DIGEST_HASH(cs->algo); 2888 struct tee_cryp_obj_secret *ss = ko->attr; 2889 const uint8_t *shared_secret = (const uint8_t *)(ss + 1); 2890 2891 res = get_concat_kdf_params(params, param_count, &info, 2892 &info_len, &derived_key_len); 2893 if (res != TEE_SUCCESS) 2894 goto out; 2895 2896 /* Requested size must fit into the output object's buffer */ 2897 if (derived_key_len > ss->alloc_size) { 2898 res = TEE_ERROR_BAD_PARAMETERS; 2899 goto out; 2900 } 2901 2902 res = tee_cryp_concat_kdf(hash_id, shared_secret, ss->key_size, 2903 info, info_len, (uint8_t *)(sk + 1), 2904 derived_key_len); 2905 if (res == TEE_SUCCESS) { 2906 sk->key_size = derived_key_len; 2907 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 2908 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 2909 } 2910 } 2911 #endif 2912 #if defined(CFG_CRYPTO_PBKDF2) 2913 else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_PBKDF2) { 2914 void *salt; 2915 size_t salt_len, iteration_count, derived_key_len; 2916 uint32_t hash_id = TEE_ALG_GET_DIGEST_HASH(cs->algo); 2917 struct tee_cryp_obj_secret *ss = ko->attr; 2918 const uint8_t *password = (const uint8_t *)(ss + 1); 2919 2920 res = get_pbkdf2_params(params, param_count, &salt, &salt_len, 2921 &derived_key_len, &iteration_count); 2922 if (res != TEE_SUCCESS) 2923 goto out; 2924 2925 /* Requested size must fit into the output object's buffer */ 2926 if (derived_key_len > ss->alloc_size) { 2927 res = TEE_ERROR_BAD_PARAMETERS; 2928 goto out; 2929 } 2930 2931 res = tee_cryp_pbkdf2(hash_id, password, ss->key_size, salt, 2932 salt_len, iteration_count, 2933 (uint8_t *)(sk + 1), derived_key_len); 2934 if (res == TEE_SUCCESS) { 2935 sk->key_size = derived_key_len; 2936 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 2937 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 2938 } 2939 } 2940 #endif 2941 else 2942 res = TEE_ERROR_NOT_SUPPORTED; 2943 2944 out: 2945 free_wipe(params); 2946 return res; 2947 } 2948 2949 TEE_Result syscall_cryp_random_number_generate(void *buf, size_t blen) 2950 { 2951 TEE_Result res; 2952 struct tee_ta_session *sess; 2953 2954 res = tee_ta_get_current_session(&sess); 2955 if (res != TEE_SUCCESS) 2956 return res; 2957 2958 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 2959 TEE_MEMORY_ACCESS_WRITE | 2960 TEE_MEMORY_ACCESS_ANY_OWNER, 2961 (uaddr_t)buf, blen); 2962 if (res != TEE_SUCCESS) 2963 return res; 2964 2965 res = crypto_rng_read(buf, blen); 2966 if (res != TEE_SUCCESS) 2967 return res; 2968 2969 return res; 2970 } 2971 2972 TEE_Result syscall_authenc_init(unsigned long state, const void *nonce, 2973 size_t nonce_len, size_t tag_len, 2974 size_t aad_len, size_t payload_len) 2975 { 2976 TEE_Result res; 2977 struct tee_cryp_state *cs; 2978 struct tee_ta_session *sess; 2979 struct tee_obj *o; 2980 struct tee_cryp_obj_secret *key; 2981 2982 res = tee_ta_get_current_session(&sess); 2983 if (res != TEE_SUCCESS) 2984 return res; 2985 2986 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 2987 TEE_MEMORY_ACCESS_READ | 2988 TEE_MEMORY_ACCESS_ANY_OWNER, 2989 (uaddr_t)nonce, nonce_len); 2990 if (res != TEE_SUCCESS) 2991 return res; 2992 2993 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(state), &cs); 2994 if (res != TEE_SUCCESS) 2995 return res; 2996 2997 res = tee_obj_get(to_user_ta_ctx(sess->ctx), cs->key1, &o); 2998 if (res != TEE_SUCCESS) 2999 return res; 3000 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 3001 return TEE_ERROR_BAD_PARAMETERS; 3002 3003 key = o->attr; 3004 res = crypto_authenc_init(cs->ctx, cs->algo, cs->mode, 3005 (uint8_t *)(key + 1), key->key_size, 3006 nonce, nonce_len, tag_len, aad_len, 3007 payload_len); 3008 if (res != TEE_SUCCESS) 3009 return res; 3010 3011 cs->ctx_finalize = (tee_cryp_ctx_finalize_func_t)crypto_authenc_final; 3012 cs->state = CRYP_STATE_INITIALIZED; 3013 3014 return TEE_SUCCESS; 3015 } 3016 3017 TEE_Result syscall_authenc_update_aad(unsigned long state, 3018 const void *aad_data, size_t aad_data_len) 3019 { 3020 TEE_Result res; 3021 struct tee_cryp_state *cs; 3022 struct tee_ta_session *sess; 3023 3024 res = tee_ta_get_current_session(&sess); 3025 if (res != TEE_SUCCESS) 3026 return res; 3027 3028 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 3029 TEE_MEMORY_ACCESS_READ | 3030 TEE_MEMORY_ACCESS_ANY_OWNER, 3031 (uaddr_t) aad_data, 3032 aad_data_len); 3033 if (res != TEE_SUCCESS) 3034 return res; 3035 3036 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(state), &cs); 3037 if (res != TEE_SUCCESS) 3038 return res; 3039 3040 if (cs->state != CRYP_STATE_INITIALIZED) 3041 return TEE_ERROR_BAD_STATE; 3042 3043 if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_AE) 3044 return TEE_ERROR_BAD_STATE; 3045 3046 res = crypto_authenc_update_aad(cs->ctx, cs->algo, cs->mode, 3047 aad_data, aad_data_len); 3048 if (res != TEE_SUCCESS) 3049 return res; 3050 3051 return TEE_SUCCESS; 3052 } 3053 3054 TEE_Result syscall_authenc_update_payload(unsigned long state, 3055 const void *src_data, size_t src_len, void *dst_data, 3056 uint64_t *dst_len) 3057 { 3058 TEE_Result res; 3059 struct tee_cryp_state *cs; 3060 struct tee_ta_session *sess; 3061 size_t dlen = 0; 3062 3063 res = tee_ta_get_current_session(&sess); 3064 if (res != TEE_SUCCESS) 3065 return res; 3066 3067 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(state), &cs); 3068 if (res != TEE_SUCCESS) 3069 return res; 3070 3071 if (cs->state != CRYP_STATE_INITIALIZED) 3072 return TEE_ERROR_BAD_STATE; 3073 3074 if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_AE) 3075 return TEE_ERROR_BAD_STATE; 3076 3077 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 3078 TEE_MEMORY_ACCESS_READ | 3079 TEE_MEMORY_ACCESS_ANY_OWNER, 3080 (uaddr_t) src_data, src_len); 3081 if (res != TEE_SUCCESS) 3082 return res; 3083 3084 res = get_user_u64_as_size_t(&dlen, dst_len); 3085 if (res != TEE_SUCCESS) 3086 return res; 3087 3088 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 3089 TEE_MEMORY_ACCESS_READ | 3090 TEE_MEMORY_ACCESS_WRITE | 3091 TEE_MEMORY_ACCESS_ANY_OWNER, 3092 (uaddr_t)dst_data, dlen); 3093 if (res != TEE_SUCCESS) 3094 return res; 3095 3096 if (dlen < src_len) { 3097 res = TEE_ERROR_SHORT_BUFFER; 3098 goto out; 3099 } 3100 3101 res = crypto_authenc_update_payload(cs->ctx, cs->algo, cs->mode, 3102 src_data, src_len, dst_data, 3103 &dlen); 3104 out: 3105 if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) { 3106 TEE_Result res2 = put_user_u64(dst_len, dlen); 3107 3108 if (res2 != TEE_SUCCESS) 3109 res = res2; 3110 } 3111 3112 return res; 3113 } 3114 3115 TEE_Result syscall_authenc_enc_final(unsigned long state, 3116 const void *src_data, size_t src_len, void *dst_data, 3117 uint64_t *dst_len, void *tag, uint64_t *tag_len) 3118 { 3119 TEE_Result res; 3120 struct tee_cryp_state *cs; 3121 struct tee_ta_session *sess; 3122 size_t dlen = 0; 3123 size_t tlen = 0; 3124 3125 res = tee_ta_get_current_session(&sess); 3126 if (res != TEE_SUCCESS) 3127 return res; 3128 3129 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(state), &cs); 3130 if (res != TEE_SUCCESS) 3131 return res; 3132 3133 if (cs->state != CRYP_STATE_INITIALIZED) 3134 return TEE_ERROR_BAD_STATE; 3135 3136 if (cs->mode != TEE_MODE_ENCRYPT) 3137 return TEE_ERROR_BAD_PARAMETERS; 3138 3139 if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_AE) 3140 return TEE_ERROR_BAD_STATE; 3141 3142 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 3143 TEE_MEMORY_ACCESS_READ | 3144 TEE_MEMORY_ACCESS_ANY_OWNER, 3145 (uaddr_t)src_data, src_len); 3146 if (res != TEE_SUCCESS) 3147 return res; 3148 3149 if (!dst_len) { 3150 dlen = 0; 3151 } else { 3152 res = get_user_u64_as_size_t(&dlen, dst_len); 3153 if (res != TEE_SUCCESS) 3154 return res; 3155 3156 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 3157 TEE_MEMORY_ACCESS_READ | 3158 TEE_MEMORY_ACCESS_WRITE | 3159 TEE_MEMORY_ACCESS_ANY_OWNER, 3160 (uaddr_t)dst_data, dlen); 3161 if (res != TEE_SUCCESS) 3162 return res; 3163 } 3164 3165 if (dlen < src_len) { 3166 res = TEE_ERROR_SHORT_BUFFER; 3167 goto out; 3168 } 3169 3170 res = get_user_u64_as_size_t(&tlen, tag_len); 3171 if (res != TEE_SUCCESS) 3172 return res; 3173 3174 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 3175 TEE_MEMORY_ACCESS_READ | 3176 TEE_MEMORY_ACCESS_WRITE | 3177 TEE_MEMORY_ACCESS_ANY_OWNER, 3178 (uaddr_t)tag, tlen); 3179 if (res != TEE_SUCCESS) 3180 return res; 3181 3182 res = crypto_authenc_enc_final(cs->ctx, cs->algo, src_data, 3183 src_len, dst_data, &dlen, tag, &tlen); 3184 3185 out: 3186 if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) { 3187 TEE_Result res2; 3188 3189 if (dst_len != NULL) { 3190 res2 = put_user_u64(dst_len, dlen); 3191 if (res2 != TEE_SUCCESS) 3192 return res2; 3193 } 3194 3195 res2 = put_user_u64(tag_len, tlen); 3196 if (res2 != TEE_SUCCESS) 3197 return res2; 3198 } 3199 3200 return res; 3201 } 3202 3203 TEE_Result syscall_authenc_dec_final(unsigned long state, 3204 const void *src_data, size_t src_len, void *dst_data, 3205 uint64_t *dst_len, const void *tag, size_t tag_len) 3206 { 3207 TEE_Result res; 3208 struct tee_cryp_state *cs; 3209 struct tee_ta_session *sess; 3210 size_t dlen = 0; 3211 3212 res = tee_ta_get_current_session(&sess); 3213 if (res != TEE_SUCCESS) 3214 return res; 3215 3216 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(state), &cs); 3217 if (res != TEE_SUCCESS) 3218 return res; 3219 3220 if (cs->state != CRYP_STATE_INITIALIZED) 3221 return TEE_ERROR_BAD_STATE; 3222 3223 if (cs->mode != TEE_MODE_DECRYPT) 3224 return TEE_ERROR_BAD_PARAMETERS; 3225 3226 if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_AE) 3227 return TEE_ERROR_BAD_STATE; 3228 3229 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 3230 TEE_MEMORY_ACCESS_READ | 3231 TEE_MEMORY_ACCESS_ANY_OWNER, 3232 (uaddr_t)src_data, src_len); 3233 if (res != TEE_SUCCESS) 3234 return res; 3235 3236 if (!dst_len) { 3237 dlen = 0; 3238 } else { 3239 res = get_user_u64_as_size_t(&dlen, dst_len); 3240 if (res != TEE_SUCCESS) 3241 return res; 3242 3243 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 3244 TEE_MEMORY_ACCESS_READ | 3245 TEE_MEMORY_ACCESS_WRITE | 3246 TEE_MEMORY_ACCESS_ANY_OWNER, 3247 (uaddr_t)dst_data, dlen); 3248 if (res != TEE_SUCCESS) 3249 return res; 3250 } 3251 3252 if (dlen < src_len) { 3253 res = TEE_ERROR_SHORT_BUFFER; 3254 goto out; 3255 } 3256 3257 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 3258 TEE_MEMORY_ACCESS_READ | 3259 TEE_MEMORY_ACCESS_ANY_OWNER, 3260 (uaddr_t)tag, tag_len); 3261 if (res != TEE_SUCCESS) 3262 return res; 3263 3264 res = crypto_authenc_dec_final(cs->ctx, cs->algo, src_data, src_len, 3265 dst_data, &dlen, tag, tag_len); 3266 3267 out: 3268 if ((res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) && 3269 dst_len != NULL) { 3270 TEE_Result res2 = put_user_u64(dst_len, dlen); 3271 3272 if (res2 != TEE_SUCCESS) 3273 return res2; 3274 } 3275 3276 return res; 3277 } 3278 3279 static int pkcs1_get_salt_len(const TEE_Attribute *params, uint32_t num_params, 3280 size_t default_len) 3281 { 3282 size_t n; 3283 3284 assert(default_len < INT_MAX); 3285 3286 for (n = 0; n < num_params; n++) { 3287 if (params[n].attributeID == TEE_ATTR_RSA_PSS_SALT_LENGTH) { 3288 if (params[n].content.value.a < INT_MAX) 3289 return params[n].content.value.a; 3290 break; 3291 } 3292 } 3293 /* 3294 * If salt length isn't provided use the default value which is 3295 * the length of the digest. 3296 */ 3297 return default_len; 3298 } 3299 3300 TEE_Result syscall_asymm_operate(unsigned long state, 3301 const struct utee_attribute *usr_params, 3302 size_t num_params, const void *src_data, size_t src_len, 3303 void *dst_data, uint64_t *dst_len) 3304 { 3305 TEE_Result res; 3306 struct tee_cryp_state *cs; 3307 struct tee_ta_session *sess; 3308 size_t dlen; 3309 struct tee_obj *o; 3310 void *label = NULL; 3311 size_t label_len = 0; 3312 size_t n; 3313 int salt_len; 3314 TEE_Attribute *params = NULL; 3315 struct user_ta_ctx *utc; 3316 3317 res = tee_ta_get_current_session(&sess); 3318 if (res != TEE_SUCCESS) 3319 return res; 3320 utc = to_user_ta_ctx(sess->ctx); 3321 3322 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(state), &cs); 3323 if (res != TEE_SUCCESS) 3324 return res; 3325 3326 res = tee_mmu_check_access_rights( 3327 utc, 3328 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_ANY_OWNER, 3329 (uaddr_t) src_data, src_len); 3330 if (res != TEE_SUCCESS) 3331 return res; 3332 3333 res = get_user_u64_as_size_t(&dlen, dst_len); 3334 if (res != TEE_SUCCESS) 3335 return res; 3336 3337 res = tee_mmu_check_access_rights( 3338 utc, 3339 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE | 3340 TEE_MEMORY_ACCESS_ANY_OWNER, 3341 (uaddr_t) dst_data, dlen); 3342 if (res != TEE_SUCCESS) 3343 return res; 3344 3345 size_t alloc_size = 0; 3346 3347 if (MUL_OVERFLOW(sizeof(TEE_Attribute), num_params, &alloc_size)) 3348 return TEE_ERROR_OVERFLOW; 3349 3350 params = malloc(alloc_size); 3351 if (!params) 3352 return TEE_ERROR_OUT_OF_MEMORY; 3353 res = copy_in_attrs(utc, usr_params, num_params, params); 3354 if (res != TEE_SUCCESS) 3355 goto out; 3356 3357 res = tee_obj_get(utc, cs->key1, &o); 3358 if (res != TEE_SUCCESS) 3359 goto out; 3360 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 3361 res = TEE_ERROR_GENERIC; 3362 goto out; 3363 } 3364 3365 switch (cs->algo) { 3366 case TEE_ALG_RSA_NOPAD: 3367 if (cs->mode == TEE_MODE_ENCRYPT) { 3368 res = crypto_acipher_rsanopad_encrypt(o->attr, src_data, 3369 src_len, dst_data, 3370 &dlen); 3371 } else if (cs->mode == TEE_MODE_DECRYPT) { 3372 res = crypto_acipher_rsanopad_decrypt(o->attr, src_data, 3373 src_len, dst_data, 3374 &dlen); 3375 } else { 3376 /* 3377 * We will panic because "the mode is not compatible 3378 * with the function" 3379 */ 3380 res = TEE_ERROR_GENERIC; 3381 } 3382 break; 3383 3384 case TEE_ALG_RSAES_PKCS1_V1_5: 3385 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1: 3386 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224: 3387 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256: 3388 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384: 3389 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512: 3390 for (n = 0; n < num_params; n++) { 3391 if (params[n].attributeID == TEE_ATTR_RSA_OAEP_LABEL) { 3392 label = params[n].content.ref.buffer; 3393 label_len = params[n].content.ref.length; 3394 break; 3395 } 3396 } 3397 3398 if (cs->mode == TEE_MODE_ENCRYPT) { 3399 res = crypto_acipher_rsaes_encrypt(cs->algo, o->attr, 3400 label, label_len, 3401 src_data, src_len, 3402 dst_data, &dlen); 3403 } else if (cs->mode == TEE_MODE_DECRYPT) { 3404 res = crypto_acipher_rsaes_decrypt( 3405 cs->algo, o->attr, label, label_len, 3406 src_data, src_len, dst_data, &dlen); 3407 } else { 3408 res = TEE_ERROR_BAD_PARAMETERS; 3409 } 3410 break; 3411 3412 #if defined(CFG_CRYPTO_RSASSA_NA1) 3413 case TEE_ALG_RSASSA_PKCS1_V1_5: 3414 #endif 3415 case TEE_ALG_RSASSA_PKCS1_V1_5_MD5: 3416 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1: 3417 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224: 3418 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256: 3419 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384: 3420 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512: 3421 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1: 3422 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224: 3423 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256: 3424 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384: 3425 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512: 3426 if (cs->mode != TEE_MODE_SIGN) { 3427 res = TEE_ERROR_BAD_PARAMETERS; 3428 break; 3429 } 3430 salt_len = pkcs1_get_salt_len(params, num_params, src_len); 3431 res = crypto_acipher_rsassa_sign(cs->algo, o->attr, salt_len, 3432 src_data, src_len, dst_data, 3433 &dlen); 3434 break; 3435 3436 case TEE_ALG_DSA_SHA1: 3437 case TEE_ALG_DSA_SHA224: 3438 case TEE_ALG_DSA_SHA256: 3439 res = crypto_acipher_dsa_sign(cs->algo, o->attr, src_data, 3440 src_len, dst_data, &dlen); 3441 break; 3442 case TEE_ALG_ECDSA_P192: 3443 case TEE_ALG_ECDSA_P224: 3444 case TEE_ALG_ECDSA_P256: 3445 case TEE_ALG_ECDSA_P384: 3446 case TEE_ALG_ECDSA_P521: 3447 res = crypto_acipher_ecc_sign(cs->algo, o->attr, src_data, 3448 src_len, dst_data, &dlen); 3449 break; 3450 3451 default: 3452 res = TEE_ERROR_BAD_PARAMETERS; 3453 break; 3454 } 3455 3456 out: 3457 free_wipe(params); 3458 3459 if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) { 3460 TEE_Result res2 = put_user_u64(dst_len, dlen); 3461 3462 if (res2 != TEE_SUCCESS) 3463 return res2; 3464 } 3465 3466 return res; 3467 } 3468 3469 TEE_Result syscall_asymm_verify(unsigned long state, 3470 const struct utee_attribute *usr_params, 3471 size_t num_params, const void *data, size_t data_len, 3472 const void *sig, size_t sig_len) 3473 { 3474 TEE_Result res; 3475 struct tee_cryp_state *cs; 3476 struct tee_ta_session *sess; 3477 struct tee_obj *o; 3478 size_t hash_size; 3479 int salt_len = 0; 3480 TEE_Attribute *params = NULL; 3481 uint32_t hash_algo; 3482 struct user_ta_ctx *utc; 3483 3484 res = tee_ta_get_current_session(&sess); 3485 if (res != TEE_SUCCESS) 3486 return res; 3487 utc = to_user_ta_ctx(sess->ctx); 3488 3489 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(state), &cs); 3490 if (res != TEE_SUCCESS) 3491 return res; 3492 3493 if (cs->mode != TEE_MODE_VERIFY) 3494 return TEE_ERROR_BAD_PARAMETERS; 3495 3496 res = tee_mmu_check_access_rights(utc, 3497 TEE_MEMORY_ACCESS_READ | 3498 TEE_MEMORY_ACCESS_ANY_OWNER, 3499 (uaddr_t)data, data_len); 3500 if (res != TEE_SUCCESS) 3501 return res; 3502 3503 res = tee_mmu_check_access_rights(utc, 3504 TEE_MEMORY_ACCESS_READ | 3505 TEE_MEMORY_ACCESS_ANY_OWNER, 3506 (uaddr_t)sig, sig_len); 3507 if (res != TEE_SUCCESS) 3508 return res; 3509 3510 size_t alloc_size = 0; 3511 3512 if (MUL_OVERFLOW(sizeof(TEE_Attribute), num_params, &alloc_size)) 3513 return TEE_ERROR_OVERFLOW; 3514 3515 params = malloc(alloc_size); 3516 if (!params) 3517 return TEE_ERROR_OUT_OF_MEMORY; 3518 res = copy_in_attrs(utc, usr_params, num_params, params); 3519 if (res != TEE_SUCCESS) 3520 goto out; 3521 3522 res = tee_obj_get(utc, cs->key1, &o); 3523 if (res != TEE_SUCCESS) 3524 goto out; 3525 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 3526 res = TEE_ERROR_BAD_PARAMETERS; 3527 goto out; 3528 } 3529 3530 switch (TEE_ALG_GET_MAIN_ALG(cs->algo)) { 3531 case TEE_MAIN_ALGO_RSA: 3532 if (cs->algo != TEE_ALG_RSASSA_PKCS1_V1_5) { 3533 hash_algo = TEE_DIGEST_HASH_TO_ALGO(cs->algo); 3534 res = tee_hash_get_digest_size(hash_algo, &hash_size); 3535 if (res != TEE_SUCCESS) 3536 break; 3537 if (data_len != hash_size) { 3538 res = TEE_ERROR_BAD_PARAMETERS; 3539 break; 3540 } 3541 salt_len = pkcs1_get_salt_len(params, num_params, 3542 hash_size); 3543 } 3544 res = crypto_acipher_rsassa_verify(cs->algo, o->attr, salt_len, 3545 data, data_len, sig, 3546 sig_len); 3547 break; 3548 3549 case TEE_MAIN_ALGO_DSA: 3550 hash_algo = TEE_DIGEST_HASH_TO_ALGO(cs->algo); 3551 res = tee_hash_get_digest_size(hash_algo, &hash_size); 3552 if (res != TEE_SUCCESS) 3553 break; 3554 /* 3555 * Depending on the DSA algorithm (NIST), the digital signature 3556 * output size may be truncated to the size of a key pair 3557 * (Q prime size). Q prime size must be less or equal than the 3558 * hash output length of the hash algorithm involved. 3559 */ 3560 if (data_len > hash_size) { 3561 res = TEE_ERROR_BAD_PARAMETERS; 3562 break; 3563 } 3564 res = crypto_acipher_dsa_verify(cs->algo, o->attr, data, 3565 data_len, sig, sig_len); 3566 break; 3567 3568 case TEE_MAIN_ALGO_ECDSA: 3569 res = crypto_acipher_ecc_verify(cs->algo, o->attr, data, 3570 data_len, sig, sig_len); 3571 break; 3572 3573 default: 3574 res = TEE_ERROR_NOT_SUPPORTED; 3575 } 3576 3577 out: 3578 free_wipe(params); 3579 return res; 3580 } 3581