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