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