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