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