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 size_t ctx_size; 66 void *ctx; 67 tee_cryp_ctx_finalize_func_t ctx_finalize; 68 }; 69 70 struct tee_cryp_obj_secret { 71 uint32_t key_size; 72 uint32_t alloc_size; 73 74 /* 75 * Pseudo code visualize layout of structure 76 * Next follows data, such as: 77 * uint8_t data[alloc_size] 78 * key_size must never exceed alloc_size 79 */ 80 }; 81 82 #define TEE_TYPE_ATTR_OPTIONAL 0x0 83 #define TEE_TYPE_ATTR_REQUIRED 0x1 84 #define TEE_TYPE_ATTR_OPTIONAL_GROUP 0x2 85 #define TEE_TYPE_ATTR_SIZE_INDICATOR 0x4 86 #define TEE_TYPE_ATTR_GEN_KEY_OPT 0x8 87 #define TEE_TYPE_ATTR_GEN_KEY_REQ 0x10 88 89 /* Handle storing of generic secret keys of varying lengths */ 90 #define ATTR_OPS_INDEX_SECRET 0 91 /* Convert to/from big-endian byte array and provider-specific bignum */ 92 #define ATTR_OPS_INDEX_BIGNUM 1 93 /* Convert to/from value attribute depending on direction */ 94 #define ATTR_OPS_INDEX_VALUE 2 95 96 struct tee_cryp_obj_type_attrs { 97 uint32_t attr_id; 98 uint16_t flags; 99 uint16_t ops_index; 100 uint16_t raw_offs; 101 uint16_t raw_size; 102 }; 103 104 #define RAW_DATA(_x, _y) \ 105 .raw_offs = offsetof(_x, _y), .raw_size = MEMBER_SIZE(_x, _y) 106 107 static const struct tee_cryp_obj_type_attrs 108 tee_cryp_obj_secret_value_attrs[] = { 109 { 110 .attr_id = TEE_ATTR_SECRET_VALUE, 111 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR, 112 .ops_index = ATTR_OPS_INDEX_SECRET, 113 .raw_offs = 0, 114 .raw_size = 0 115 }, 116 }; 117 118 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_rsa_pub_key_attrs[] = { 119 { 120 .attr_id = TEE_ATTR_RSA_MODULUS, 121 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR, 122 .ops_index = ATTR_OPS_INDEX_BIGNUM, 123 RAW_DATA(struct rsa_public_key, n) 124 }, 125 126 { 127 .attr_id = TEE_ATTR_RSA_PUBLIC_EXPONENT, 128 .flags = TEE_TYPE_ATTR_REQUIRED, 129 .ops_index = ATTR_OPS_INDEX_BIGNUM, 130 RAW_DATA(struct rsa_public_key, e) 131 }, 132 }; 133 134 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_rsa_keypair_attrs[] = { 135 { 136 .attr_id = TEE_ATTR_RSA_MODULUS, 137 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR, 138 .ops_index = ATTR_OPS_INDEX_BIGNUM, 139 RAW_DATA(struct rsa_keypair, n) 140 }, 141 142 { 143 .attr_id = TEE_ATTR_RSA_PUBLIC_EXPONENT, 144 .flags = TEE_TYPE_ATTR_REQUIRED, 145 .ops_index = ATTR_OPS_INDEX_BIGNUM, 146 RAW_DATA(struct rsa_keypair, e) 147 }, 148 149 { 150 .attr_id = TEE_ATTR_RSA_PRIVATE_EXPONENT, 151 .flags = TEE_TYPE_ATTR_REQUIRED, 152 .ops_index = ATTR_OPS_INDEX_BIGNUM, 153 RAW_DATA(struct rsa_keypair, d) 154 }, 155 156 { 157 .attr_id = TEE_ATTR_RSA_PRIME1, 158 .flags = TEE_TYPE_ATTR_OPTIONAL_GROUP, 159 .ops_index = ATTR_OPS_INDEX_BIGNUM, 160 RAW_DATA(struct rsa_keypair, p) 161 }, 162 163 { 164 .attr_id = TEE_ATTR_RSA_PRIME2, 165 .flags = TEE_TYPE_ATTR_OPTIONAL_GROUP, 166 .ops_index = ATTR_OPS_INDEX_BIGNUM, 167 RAW_DATA(struct rsa_keypair, q) 168 }, 169 170 { 171 .attr_id = TEE_ATTR_RSA_EXPONENT1, 172 .flags = TEE_TYPE_ATTR_OPTIONAL_GROUP, 173 .ops_index = ATTR_OPS_INDEX_BIGNUM, 174 RAW_DATA(struct rsa_keypair, dp) 175 }, 176 177 { 178 .attr_id = TEE_ATTR_RSA_EXPONENT2, 179 .flags = TEE_TYPE_ATTR_OPTIONAL_GROUP, 180 .ops_index = ATTR_OPS_INDEX_BIGNUM, 181 RAW_DATA(struct rsa_keypair, dq) 182 }, 183 184 { 185 .attr_id = TEE_ATTR_RSA_COEFFICIENT, 186 .flags = TEE_TYPE_ATTR_OPTIONAL_GROUP, 187 .ops_index = ATTR_OPS_INDEX_BIGNUM, 188 RAW_DATA(struct rsa_keypair, qp) 189 }, 190 }; 191 192 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_dsa_pub_key_attrs[] = { 193 { 194 .attr_id = TEE_ATTR_DSA_PRIME, 195 .flags = TEE_TYPE_ATTR_REQUIRED, 196 .ops_index = ATTR_OPS_INDEX_BIGNUM, 197 RAW_DATA(struct dsa_public_key, p) 198 }, 199 200 { 201 .attr_id = TEE_ATTR_DSA_SUBPRIME, 202 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR, 203 .ops_index = ATTR_OPS_INDEX_BIGNUM, 204 RAW_DATA(struct dsa_public_key, q) 205 }, 206 207 { 208 .attr_id = TEE_ATTR_DSA_BASE, 209 .flags = TEE_TYPE_ATTR_REQUIRED, 210 .ops_index = ATTR_OPS_INDEX_BIGNUM, 211 RAW_DATA(struct dsa_public_key, g) 212 }, 213 214 { 215 .attr_id = TEE_ATTR_DSA_PUBLIC_VALUE, 216 .flags = TEE_TYPE_ATTR_REQUIRED, 217 .ops_index = ATTR_OPS_INDEX_BIGNUM, 218 RAW_DATA(struct dsa_public_key, y) 219 }, 220 }; 221 222 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_dsa_keypair_attrs[] = { 223 { 224 .attr_id = TEE_ATTR_DSA_PRIME, 225 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_GEN_KEY_REQ, 226 .ops_index = ATTR_OPS_INDEX_BIGNUM, 227 RAW_DATA(struct dsa_keypair, p) 228 }, 229 230 { 231 .attr_id = TEE_ATTR_DSA_SUBPRIME, 232 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR | 233 TEE_TYPE_ATTR_GEN_KEY_REQ, 234 .ops_index = ATTR_OPS_INDEX_BIGNUM, 235 RAW_DATA(struct dsa_keypair, q) 236 }, 237 238 { 239 .attr_id = TEE_ATTR_DSA_BASE, 240 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_GEN_KEY_REQ, 241 .ops_index = ATTR_OPS_INDEX_BIGNUM, 242 RAW_DATA(struct dsa_keypair, g) 243 }, 244 245 { 246 .attr_id = TEE_ATTR_DSA_PRIVATE_VALUE, 247 .flags = TEE_TYPE_ATTR_REQUIRED, 248 .ops_index = ATTR_OPS_INDEX_BIGNUM, 249 RAW_DATA(struct dsa_keypair, x) 250 }, 251 252 { 253 .attr_id = TEE_ATTR_DSA_PUBLIC_VALUE, 254 .flags = TEE_TYPE_ATTR_REQUIRED, 255 .ops_index = ATTR_OPS_INDEX_BIGNUM, 256 RAW_DATA(struct dsa_keypair, y) 257 }, 258 }; 259 260 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_dh_keypair_attrs[] = { 261 { 262 .attr_id = TEE_ATTR_DH_PRIME, 263 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR | 264 TEE_TYPE_ATTR_GEN_KEY_REQ, 265 .ops_index = ATTR_OPS_INDEX_BIGNUM, 266 RAW_DATA(struct dh_keypair, p) 267 }, 268 269 { 270 .attr_id = TEE_ATTR_DH_BASE, 271 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_GEN_KEY_REQ, 272 .ops_index = ATTR_OPS_INDEX_BIGNUM, 273 RAW_DATA(struct dh_keypair, g) 274 }, 275 276 { 277 .attr_id = TEE_ATTR_DH_PUBLIC_VALUE, 278 .flags = TEE_TYPE_ATTR_REQUIRED, 279 .ops_index = ATTR_OPS_INDEX_BIGNUM, 280 RAW_DATA(struct dh_keypair, y) 281 }, 282 283 { 284 .attr_id = TEE_ATTR_DH_PRIVATE_VALUE, 285 .flags = TEE_TYPE_ATTR_REQUIRED, 286 .ops_index = ATTR_OPS_INDEX_BIGNUM, 287 RAW_DATA(struct dh_keypair, x) 288 }, 289 290 { 291 .attr_id = TEE_ATTR_DH_SUBPRIME, 292 .flags = TEE_TYPE_ATTR_OPTIONAL_GROUP | TEE_TYPE_ATTR_GEN_KEY_OPT, 293 .ops_index = ATTR_OPS_INDEX_BIGNUM, 294 RAW_DATA(struct dh_keypair, q) 295 }, 296 297 { 298 .attr_id = TEE_ATTR_DH_X_BITS, 299 .flags = TEE_TYPE_ATTR_GEN_KEY_OPT, 300 .ops_index = ATTR_OPS_INDEX_VALUE, 301 RAW_DATA(struct dh_keypair, xbits) 302 }, 303 }; 304 305 #if defined(CFG_CRYPTO_HKDF) 306 static const struct tee_cryp_obj_type_attrs 307 tee_cryp_obj_hkdf_ikm_attrs[] = { 308 { 309 .attr_id = TEE_ATTR_HKDF_IKM, 310 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR, 311 .ops_index = ATTR_OPS_INDEX_SECRET, 312 .raw_offs = 0, 313 .raw_size = 0 314 }, 315 }; 316 #endif 317 318 #if defined(CFG_CRYPTO_CONCAT_KDF) 319 static const struct tee_cryp_obj_type_attrs 320 tee_cryp_obj_concat_kdf_z_attrs[] = { 321 { 322 .attr_id = TEE_ATTR_CONCAT_KDF_Z, 323 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR, 324 .ops_index = ATTR_OPS_INDEX_SECRET, 325 .raw_offs = 0, 326 .raw_size = 0 327 }, 328 }; 329 #endif 330 331 #if defined(CFG_CRYPTO_PBKDF2) 332 static const struct tee_cryp_obj_type_attrs 333 tee_cryp_obj_pbkdf2_passwd_attrs[] = { 334 { 335 .attr_id = TEE_ATTR_PBKDF2_PASSWORD, 336 .flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR, 337 .ops_index = ATTR_OPS_INDEX_SECRET, 338 .raw_offs = 0, 339 .raw_size = 0 340 }, 341 }; 342 #endif 343 344 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_ecc_pub_key_attrs[] = { 345 { 346 .attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_X, 347 .flags = TEE_TYPE_ATTR_REQUIRED, 348 .ops_index = ATTR_OPS_INDEX_BIGNUM, 349 RAW_DATA(struct ecc_public_key, x) 350 }, 351 352 { 353 .attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_Y, 354 .flags = TEE_TYPE_ATTR_REQUIRED, 355 .ops_index = ATTR_OPS_INDEX_BIGNUM, 356 RAW_DATA(struct ecc_public_key, y) 357 }, 358 359 { 360 .attr_id = TEE_ATTR_ECC_CURVE, 361 .flags = TEE_TYPE_ATTR_REQUIRED, 362 .ops_index = ATTR_OPS_INDEX_VALUE, 363 RAW_DATA(struct ecc_public_key, curve) 364 }, 365 }; 366 367 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_ecc_keypair_attrs[] = { 368 { 369 .attr_id = TEE_ATTR_ECC_PRIVATE_VALUE, 370 .flags = TEE_TYPE_ATTR_REQUIRED, 371 .ops_index = ATTR_OPS_INDEX_BIGNUM, 372 RAW_DATA(struct ecc_keypair, d) 373 }, 374 375 { 376 .attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_X, 377 .flags = TEE_TYPE_ATTR_REQUIRED, 378 .ops_index = ATTR_OPS_INDEX_BIGNUM, 379 RAW_DATA(struct ecc_keypair, x) 380 }, 381 382 { 383 .attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_Y, 384 .flags = TEE_TYPE_ATTR_REQUIRED, 385 .ops_index = ATTR_OPS_INDEX_BIGNUM, 386 RAW_DATA(struct ecc_keypair, y) 387 }, 388 389 { 390 .attr_id = TEE_ATTR_ECC_CURVE, 391 .flags = TEE_TYPE_ATTR_REQUIRED, 392 .ops_index = ATTR_OPS_INDEX_VALUE, 393 RAW_DATA(struct ecc_keypair, curve) 394 }, 395 }; 396 397 struct tee_cryp_obj_type_props { 398 TEE_ObjectType obj_type; 399 uint16_t min_size; /* may not be smaller than this */ 400 uint16_t max_size; /* may not be larger than this */ 401 uint16_t alloc_size; /* this many bytes are allocated to hold data */ 402 uint8_t quanta; /* may only be an multiple of this */ 403 404 uint8_t num_type_attrs; 405 const struct tee_cryp_obj_type_attrs *type_attrs; 406 }; 407 408 #define PROP(obj_type, quanta, min_size, max_size, alloc_size, type_attrs) \ 409 { (obj_type), (min_size), (max_size), (alloc_size), (quanta), \ 410 ARRAY_SIZE(type_attrs), (type_attrs) } 411 412 static const struct tee_cryp_obj_type_props tee_cryp_obj_props[] = { 413 PROP(TEE_TYPE_AES, 64, 128, 256, /* valid sizes 128, 192, 256 */ 414 256 / 8 + sizeof(struct tee_cryp_obj_secret), 415 tee_cryp_obj_secret_value_attrs), 416 PROP(TEE_TYPE_DES, 56, 56, 56, 417 /* 418 * Valid size 56 without parity, note that we still allocate 419 * for 64 bits since the key is supplied with parity. 420 */ 421 64 / 8 + sizeof(struct tee_cryp_obj_secret), 422 tee_cryp_obj_secret_value_attrs), 423 PROP(TEE_TYPE_DES3, 56, 112, 168, 424 /* 425 * Valid sizes 112, 168 without parity, note that we still 426 * allocate for with space for the parity since the key is 427 * supplied with parity. 428 */ 429 192 / 8 + sizeof(struct tee_cryp_obj_secret), 430 tee_cryp_obj_secret_value_attrs), 431 PROP(TEE_TYPE_HMAC_MD5, 8, 64, 512, 432 512 / 8 + sizeof(struct tee_cryp_obj_secret), 433 tee_cryp_obj_secret_value_attrs), 434 PROP(TEE_TYPE_HMAC_SHA1, 8, 80, 512, 435 512 / 8 + sizeof(struct tee_cryp_obj_secret), 436 tee_cryp_obj_secret_value_attrs), 437 PROP(TEE_TYPE_HMAC_SHA224, 8, 112, 512, 438 512 / 8 + sizeof(struct tee_cryp_obj_secret), 439 tee_cryp_obj_secret_value_attrs), 440 PROP(TEE_TYPE_HMAC_SHA256, 8, 192, 1024, 441 1024 / 8 + sizeof(struct tee_cryp_obj_secret), 442 tee_cryp_obj_secret_value_attrs), 443 PROP(TEE_TYPE_HMAC_SHA384, 8, 256, 1024, 444 1024 / 8 + sizeof(struct tee_cryp_obj_secret), 445 tee_cryp_obj_secret_value_attrs), 446 PROP(TEE_TYPE_HMAC_SHA512, 8, 256, 1024, 447 1024 / 8 + sizeof(struct tee_cryp_obj_secret), 448 tee_cryp_obj_secret_value_attrs), 449 PROP(TEE_TYPE_GENERIC_SECRET, 8, 0, 4096, 450 4096 / 8 + sizeof(struct tee_cryp_obj_secret), 451 tee_cryp_obj_secret_value_attrs), 452 #if defined(CFG_CRYPTO_HKDF) 453 PROP(TEE_TYPE_HKDF_IKM, 8, 0, 4096, 454 4096 / 8 + sizeof(struct tee_cryp_obj_secret), 455 tee_cryp_obj_hkdf_ikm_attrs), 456 #endif 457 #if defined(CFG_CRYPTO_CONCAT_KDF) 458 PROP(TEE_TYPE_CONCAT_KDF_Z, 8, 0, 4096, 459 4096 / 8 + sizeof(struct tee_cryp_obj_secret), 460 tee_cryp_obj_concat_kdf_z_attrs), 461 #endif 462 #if defined(CFG_CRYPTO_PBKDF2) 463 PROP(TEE_TYPE_PBKDF2_PASSWORD, 8, 0, 4096, 464 4096 / 8 + sizeof(struct tee_cryp_obj_secret), 465 tee_cryp_obj_pbkdf2_passwd_attrs), 466 #endif 467 PROP(TEE_TYPE_RSA_PUBLIC_KEY, 1, 256, 2048, 468 sizeof(struct rsa_public_key), 469 tee_cryp_obj_rsa_pub_key_attrs), 470 471 PROP(TEE_TYPE_RSA_KEYPAIR, 1, 256, 2048, 472 sizeof(struct rsa_keypair), 473 tee_cryp_obj_rsa_keypair_attrs), 474 475 PROP(TEE_TYPE_DSA_PUBLIC_KEY, 64, 512, 3072, 476 sizeof(struct dsa_public_key), 477 tee_cryp_obj_dsa_pub_key_attrs), 478 479 PROP(TEE_TYPE_DSA_KEYPAIR, 64, 512, 3072, 480 sizeof(struct dsa_keypair), 481 tee_cryp_obj_dsa_keypair_attrs), 482 483 PROP(TEE_TYPE_DH_KEYPAIR, 1, 256, 2048, 484 sizeof(struct dh_keypair), 485 tee_cryp_obj_dh_keypair_attrs), 486 487 PROP(TEE_TYPE_ECDSA_PUBLIC_KEY, 1, 192, 521, 488 sizeof(struct ecc_public_key), 489 tee_cryp_obj_ecc_pub_key_attrs), 490 491 PROP(TEE_TYPE_ECDSA_KEYPAIR, 1, 192, 521, 492 sizeof(struct ecc_keypair), 493 tee_cryp_obj_ecc_keypair_attrs), 494 495 PROP(TEE_TYPE_ECDH_PUBLIC_KEY, 1, 192, 521, 496 sizeof(struct ecc_public_key), 497 tee_cryp_obj_ecc_pub_key_attrs), 498 499 PROP(TEE_TYPE_ECDH_KEYPAIR, 1, 192, 521, 500 sizeof(struct ecc_keypair), 501 tee_cryp_obj_ecc_keypair_attrs), 502 }; 503 504 struct attr_ops { 505 TEE_Result (*from_user)(void *attr, const void *buffer, size_t size); 506 TEE_Result (*to_user)(void *attr, struct tee_ta_session *sess, 507 void *buffer, uint64_t *size); 508 TEE_Result (*to_binary)(void *attr, void *data, size_t data_len, 509 size_t *offs); 510 bool (*from_binary)(void *attr, const void *data, size_t data_len, 511 size_t *offs); 512 TEE_Result (*from_obj)(void *attr, void *src_attr); 513 void (*free)(void *attr); 514 void (*clear)(void *attr); 515 }; 516 517 static TEE_Result op_u32_to_binary_helper(uint32_t v, uint8_t *data, 518 size_t data_len, size_t *offs) 519 { 520 uint32_t field; 521 size_t next_offs; 522 523 if (ADD_OVERFLOW(*offs, sizeof(field), &next_offs)) 524 return TEE_ERROR_OVERFLOW; 525 526 if (data && next_offs <= data_len) { 527 field = TEE_U32_TO_BIG_ENDIAN(v); 528 memcpy(data + *offs, &field, sizeof(field)); 529 } 530 (*offs) = next_offs; 531 532 return TEE_SUCCESS; 533 } 534 535 static bool op_u32_from_binary_helper(uint32_t *v, const uint8_t *data, 536 size_t data_len, size_t *offs) 537 { 538 uint32_t field; 539 540 if (!data || (*offs + sizeof(field)) > data_len) 541 return false; 542 543 memcpy(&field, data + *offs, sizeof(field)); 544 *v = TEE_U32_FROM_BIG_ENDIAN(field); 545 (*offs) += sizeof(field); 546 return true; 547 } 548 549 static TEE_Result op_attr_secret_value_from_user(void *attr, const void *buffer, 550 size_t size) 551 { 552 struct tee_cryp_obj_secret *key = attr; 553 554 /* Data size has to fit in allocated buffer */ 555 if (size > key->alloc_size) 556 return TEE_ERROR_SECURITY; 557 memcpy(key + 1, buffer, size); 558 key->key_size = size; 559 return TEE_SUCCESS; 560 } 561 562 static TEE_Result op_attr_secret_value_to_user(void *attr, 563 struct tee_ta_session *sess __unused, 564 void *buffer, uint64_t *size) 565 { 566 TEE_Result res; 567 struct tee_cryp_obj_secret *key = attr; 568 uint64_t s; 569 uint64_t key_size; 570 571 res = tee_svc_copy_from_user(&s, size, sizeof(s)); 572 if (res != TEE_SUCCESS) 573 return res; 574 575 key_size = key->key_size; 576 res = tee_svc_copy_to_user(size, &key_size, sizeof(key_size)); 577 if (res != TEE_SUCCESS) 578 return res; 579 580 if (s < key->key_size) 581 return TEE_ERROR_SHORT_BUFFER; 582 583 return tee_svc_copy_to_user(buffer, key + 1, key->key_size); 584 } 585 586 static TEE_Result op_attr_secret_value_to_binary(void *attr, void *data, 587 size_t data_len, size_t *offs) 588 { 589 TEE_Result res; 590 struct tee_cryp_obj_secret *key = attr; 591 size_t next_offs; 592 593 res = op_u32_to_binary_helper(key->key_size, data, data_len, offs); 594 if (res != TEE_SUCCESS) 595 return res; 596 597 if (ADD_OVERFLOW(*offs, key->key_size, &next_offs)) 598 return TEE_ERROR_OVERFLOW; 599 600 if (data && next_offs <= data_len) 601 memcpy((uint8_t *)data + *offs, key + 1, key->key_size); 602 (*offs) = next_offs; 603 604 return TEE_SUCCESS; 605 } 606 607 static bool op_attr_secret_value_from_binary(void *attr, const void *data, 608 size_t data_len, size_t *offs) 609 { 610 struct tee_cryp_obj_secret *key = attr; 611 uint32_t s; 612 613 if (!op_u32_from_binary_helper(&s, data, data_len, offs)) 614 return false; 615 616 if ((*offs + s) > data_len) 617 return false; 618 619 /* Data size has to fit in allocated buffer */ 620 if (s > key->alloc_size) 621 return false; 622 key->key_size = s; 623 memcpy(key + 1, (const uint8_t *)data + *offs, s); 624 (*offs) += s; 625 return true; 626 } 627 628 629 static TEE_Result op_attr_secret_value_from_obj(void *attr, void *src_attr) 630 { 631 struct tee_cryp_obj_secret *key = attr; 632 struct tee_cryp_obj_secret *src_key = src_attr; 633 634 if (src_key->key_size > key->alloc_size) 635 return TEE_ERROR_BAD_STATE; 636 memcpy(key + 1, src_key + 1, src_key->key_size); 637 key->key_size = src_key->key_size; 638 return TEE_SUCCESS; 639 } 640 641 static void op_attr_secret_value_clear(void *attr) 642 { 643 struct tee_cryp_obj_secret *key = attr; 644 645 key->key_size = 0; 646 memset(key + 1, 0, key->alloc_size); 647 } 648 649 static TEE_Result op_attr_bignum_from_user(void *attr, const void *buffer, 650 size_t size) 651 { 652 struct bignum **bn = attr; 653 654 return crypto_bignum_bin2bn(buffer, size, *bn); 655 } 656 657 static TEE_Result op_attr_bignum_to_user(void *attr, 658 struct tee_ta_session *sess, 659 void *buffer, uint64_t *size) 660 { 661 TEE_Result res; 662 struct bignum **bn = attr; 663 uint64_t req_size; 664 uint64_t s; 665 666 res = tee_svc_copy_from_user(&s, size, sizeof(s)); 667 if (res != TEE_SUCCESS) 668 return res; 669 670 req_size = crypto_bignum_num_bytes(*bn); 671 res = tee_svc_copy_to_user(size, &req_size, sizeof(req_size)); 672 if (res != TEE_SUCCESS) 673 return res; 674 if (!req_size) 675 return TEE_SUCCESS; 676 if (s < req_size) 677 return TEE_ERROR_SHORT_BUFFER; 678 679 /* Check we can access data using supplied user mode pointer */ 680 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 681 TEE_MEMORY_ACCESS_READ | 682 TEE_MEMORY_ACCESS_WRITE | 683 TEE_MEMORY_ACCESS_ANY_OWNER, 684 (uaddr_t)buffer, req_size); 685 if (res != TEE_SUCCESS) 686 return res; 687 /* 688 * Write the bignum (wich raw data points to) into an array of 689 * bytes (stored in buffer) 690 */ 691 crypto_bignum_bn2bin(*bn, buffer); 692 return TEE_SUCCESS; 693 } 694 695 static TEE_Result op_attr_bignum_to_binary(void *attr, void *data, 696 size_t data_len, size_t *offs) 697 { 698 TEE_Result res; 699 struct bignum **bn = attr; 700 uint32_t n = crypto_bignum_num_bytes(*bn); 701 size_t next_offs; 702 703 res = op_u32_to_binary_helper(n, data, data_len, offs); 704 if (res != TEE_SUCCESS) 705 return res; 706 707 if (ADD_OVERFLOW(*offs, n, &next_offs)) 708 return TEE_ERROR_OVERFLOW; 709 710 if (data && next_offs <= data_len) 711 crypto_bignum_bn2bin(*bn, (uint8_t *)data + *offs); 712 (*offs) = next_offs; 713 714 return TEE_SUCCESS; 715 } 716 717 static bool op_attr_bignum_from_binary(void *attr, const void *data, 718 size_t data_len, size_t *offs) 719 { 720 struct bignum **bn = attr; 721 uint32_t n; 722 723 if (!op_u32_from_binary_helper(&n, data, data_len, offs)) 724 return false; 725 726 if ((*offs + n) > data_len) 727 return false; 728 if (crypto_bignum_bin2bn((const uint8_t *)data + *offs, n, *bn)) 729 return false; 730 (*offs) += n; 731 return true; 732 } 733 734 static TEE_Result op_attr_bignum_from_obj(void *attr, void *src_attr) 735 { 736 struct bignum **bn = attr; 737 struct bignum **src_bn = src_attr; 738 739 crypto_bignum_copy(*bn, *src_bn); 740 return TEE_SUCCESS; 741 } 742 743 static void op_attr_bignum_clear(void *attr) 744 { 745 struct bignum **bn = attr; 746 747 crypto_bignum_clear(*bn); 748 } 749 750 static void op_attr_bignum_free(void *attr) 751 { 752 struct bignum **bn = attr; 753 754 crypto_bignum_free(*bn); 755 *bn = NULL; 756 } 757 758 static TEE_Result op_attr_value_from_user(void *attr, const void *buffer, 759 size_t size) 760 { 761 uint32_t *v = attr; 762 763 if (size != sizeof(uint32_t) * 2) 764 return TEE_ERROR_GENERIC; /* "can't happen */ 765 766 /* Note that only the first value is copied */ 767 memcpy(v, buffer, sizeof(uint32_t)); 768 return TEE_SUCCESS; 769 } 770 771 static TEE_Result op_attr_value_to_user(void *attr, 772 struct tee_ta_session *sess __unused, 773 void *buffer, uint64_t *size) 774 { 775 TEE_Result res; 776 uint32_t *v = attr; 777 uint64_t s; 778 uint32_t value[2] = { *v }; 779 uint64_t req_size = sizeof(value); 780 781 res = tee_svc_copy_from_user(&s, size, sizeof(s)); 782 if (res != TEE_SUCCESS) 783 return res; 784 785 if (s < req_size) 786 return TEE_ERROR_SHORT_BUFFER; 787 788 return tee_svc_copy_to_user(buffer, value, req_size); 789 } 790 791 static TEE_Result op_attr_value_to_binary(void *attr, void *data, 792 size_t data_len, size_t *offs) 793 { 794 uint32_t *v = attr; 795 796 return op_u32_to_binary_helper(*v, data, data_len, offs); 797 } 798 799 static bool op_attr_value_from_binary(void *attr, const void *data, 800 size_t data_len, size_t *offs) 801 { 802 uint32_t *v = attr; 803 804 return op_u32_from_binary_helper(v, data, data_len, offs); 805 } 806 807 static TEE_Result op_attr_value_from_obj(void *attr, void *src_attr) 808 { 809 uint32_t *v = attr; 810 uint32_t *src_v = src_attr; 811 812 *v = *src_v; 813 return TEE_SUCCESS; 814 } 815 816 static void op_attr_value_clear(void *attr) 817 { 818 uint32_t *v = attr; 819 820 *v = 0; 821 } 822 823 static const struct attr_ops attr_ops[] = { 824 [ATTR_OPS_INDEX_SECRET] = { 825 .from_user = op_attr_secret_value_from_user, 826 .to_user = op_attr_secret_value_to_user, 827 .to_binary = op_attr_secret_value_to_binary, 828 .from_binary = op_attr_secret_value_from_binary, 829 .from_obj = op_attr_secret_value_from_obj, 830 .free = op_attr_secret_value_clear, /* not a typo */ 831 .clear = op_attr_secret_value_clear, 832 }, 833 [ATTR_OPS_INDEX_BIGNUM] = { 834 .from_user = op_attr_bignum_from_user, 835 .to_user = op_attr_bignum_to_user, 836 .to_binary = op_attr_bignum_to_binary, 837 .from_binary = op_attr_bignum_from_binary, 838 .from_obj = op_attr_bignum_from_obj, 839 .free = op_attr_bignum_free, 840 .clear = op_attr_bignum_clear, 841 }, 842 [ATTR_OPS_INDEX_VALUE] = { 843 .from_user = op_attr_value_from_user, 844 .to_user = op_attr_value_to_user, 845 .to_binary = op_attr_value_to_binary, 846 .from_binary = op_attr_value_from_binary, 847 .from_obj = op_attr_value_from_obj, 848 .free = op_attr_value_clear, /* not a typo */ 849 .clear = op_attr_value_clear, 850 }, 851 }; 852 853 TEE_Result syscall_cryp_obj_get_info(unsigned long obj, TEE_ObjectInfo *info) 854 { 855 TEE_Result res; 856 struct tee_ta_session *sess; 857 struct tee_obj *o; 858 859 res = tee_ta_get_current_session(&sess); 860 if (res != TEE_SUCCESS) 861 goto exit; 862 863 res = tee_obj_get(to_user_ta_ctx(sess->ctx), 864 tee_svc_uref_to_vaddr(obj), &o); 865 if (res != TEE_SUCCESS) 866 goto exit; 867 868 res = tee_svc_copy_to_user(info, &o->info, sizeof(o->info)); 869 870 exit: 871 return res; 872 } 873 874 TEE_Result syscall_cryp_obj_restrict_usage(unsigned long obj, 875 unsigned long usage) 876 { 877 TEE_Result res; 878 struct tee_ta_session *sess; 879 struct tee_obj *o; 880 881 res = tee_ta_get_current_session(&sess); 882 if (res != TEE_SUCCESS) 883 goto exit; 884 885 res = tee_obj_get(to_user_ta_ctx(sess->ctx), 886 tee_svc_uref_to_vaddr(obj), &o); 887 if (res != TEE_SUCCESS) 888 goto exit; 889 890 o->info.objectUsage &= usage; 891 892 exit: 893 return res; 894 } 895 896 static int tee_svc_cryp_obj_find_type_attr_idx( 897 uint32_t attr_id, 898 const struct tee_cryp_obj_type_props *type_props) 899 { 900 size_t n; 901 902 for (n = 0; n < type_props->num_type_attrs; n++) { 903 if (attr_id == type_props->type_attrs[n].attr_id) 904 return n; 905 } 906 return -1; 907 } 908 909 static const struct tee_cryp_obj_type_props *tee_svc_find_type_props( 910 TEE_ObjectType obj_type) 911 { 912 size_t n; 913 914 for (n = 0; n < ARRAY_SIZE(tee_cryp_obj_props); n++) { 915 if (tee_cryp_obj_props[n].obj_type == obj_type) 916 return tee_cryp_obj_props + n; 917 } 918 919 return NULL; 920 } 921 922 /* Set an attribute on an object */ 923 static void set_attribute(struct tee_obj *o, 924 const struct tee_cryp_obj_type_props *props, 925 uint32_t attr) 926 { 927 int idx = tee_svc_cryp_obj_find_type_attr_idx(attr, props); 928 929 if (idx < 0) 930 return; 931 o->have_attrs |= BIT(idx); 932 } 933 934 /* Get an attribute on an object */ 935 static uint32_t get_attribute(const struct tee_obj *o, 936 const struct tee_cryp_obj_type_props *props, 937 uint32_t attr) 938 { 939 int idx = tee_svc_cryp_obj_find_type_attr_idx(attr, props); 940 941 if (idx < 0) 942 return 0; 943 return o->have_attrs & BIT(idx); 944 } 945 946 TEE_Result syscall_cryp_obj_get_attr(unsigned long obj, unsigned long attr_id, 947 void *buffer, uint64_t *size) 948 { 949 TEE_Result res; 950 struct tee_ta_session *sess; 951 struct tee_obj *o; 952 const struct tee_cryp_obj_type_props *type_props; 953 int idx; 954 const struct attr_ops *ops; 955 void *attr; 956 957 res = tee_ta_get_current_session(&sess); 958 if (res != TEE_SUCCESS) 959 return res; 960 961 res = tee_obj_get(to_user_ta_ctx(sess->ctx), 962 tee_svc_uref_to_vaddr(obj), &o); 963 if (res != TEE_SUCCESS) 964 return TEE_ERROR_ITEM_NOT_FOUND; 965 966 /* Check that the object is initialized */ 967 if (!(o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED)) 968 return TEE_ERROR_BAD_PARAMETERS; 969 970 /* Check that getting the attribute is allowed */ 971 if (!(attr_id & TEE_ATTR_BIT_PROTECTED) && 972 !(o->info.objectUsage & TEE_USAGE_EXTRACTABLE)) 973 return TEE_ERROR_BAD_PARAMETERS; 974 975 type_props = tee_svc_find_type_props(o->info.objectType); 976 if (!type_props) { 977 /* Unknown object type, "can't happen" */ 978 return TEE_ERROR_BAD_STATE; 979 } 980 981 idx = tee_svc_cryp_obj_find_type_attr_idx(attr_id, type_props); 982 if ((idx < 0) || ((o->have_attrs & (1 << idx)) == 0)) 983 return TEE_ERROR_ITEM_NOT_FOUND; 984 985 ops = attr_ops + type_props->type_attrs[idx].ops_index; 986 attr = (uint8_t *)o->attr + type_props->type_attrs[idx].raw_offs; 987 return ops->to_user(attr, sess, buffer, size); 988 } 989 990 void tee_obj_attr_free(struct tee_obj *o) 991 { 992 const struct tee_cryp_obj_type_props *tp; 993 size_t n; 994 995 if (!o->attr) 996 return; 997 tp = tee_svc_find_type_props(o->info.objectType); 998 if (!tp) 999 return; 1000 1001 for (n = 0; n < tp->num_type_attrs; n++) { 1002 const struct tee_cryp_obj_type_attrs *ta = tp->type_attrs + n; 1003 1004 attr_ops[ta->ops_index].free((uint8_t *)o->attr + ta->raw_offs); 1005 } 1006 } 1007 1008 void tee_obj_attr_clear(struct tee_obj *o) 1009 { 1010 const struct tee_cryp_obj_type_props *tp; 1011 size_t n; 1012 1013 if (!o->attr) 1014 return; 1015 tp = tee_svc_find_type_props(o->info.objectType); 1016 if (!tp) 1017 return; 1018 1019 for (n = 0; n < tp->num_type_attrs; n++) { 1020 const struct tee_cryp_obj_type_attrs *ta = tp->type_attrs + n; 1021 1022 attr_ops[ta->ops_index].clear((uint8_t *)o->attr + 1023 ta->raw_offs); 1024 } 1025 } 1026 1027 TEE_Result tee_obj_attr_to_binary(struct tee_obj *o, void *data, 1028 size_t *data_len) 1029 { 1030 const struct tee_cryp_obj_type_props *tp; 1031 size_t n; 1032 size_t offs = 0; 1033 size_t len = data ? *data_len : 0; 1034 TEE_Result res; 1035 1036 if (o->info.objectType == TEE_TYPE_DATA) { 1037 *data_len = 0; 1038 return TEE_SUCCESS; /* pure data object */ 1039 } 1040 if (!o->attr) 1041 return TEE_ERROR_BAD_STATE; 1042 tp = tee_svc_find_type_props(o->info.objectType); 1043 if (!tp) 1044 return TEE_ERROR_BAD_STATE; 1045 1046 for (n = 0; n < tp->num_type_attrs; n++) { 1047 const struct tee_cryp_obj_type_attrs *ta = tp->type_attrs + n; 1048 void *attr = (uint8_t *)o->attr + ta->raw_offs; 1049 1050 res = attr_ops[ta->ops_index].to_binary(attr, data, len, &offs); 1051 if (res != TEE_SUCCESS) 1052 return res; 1053 } 1054 1055 *data_len = offs; 1056 if (data && offs > len) 1057 return TEE_ERROR_SHORT_BUFFER; 1058 return TEE_SUCCESS; 1059 } 1060 1061 TEE_Result tee_obj_attr_from_binary(struct tee_obj *o, const void *data, 1062 size_t data_len) 1063 { 1064 const struct tee_cryp_obj_type_props *tp; 1065 size_t n; 1066 size_t offs = 0; 1067 1068 if (o->info.objectType == TEE_TYPE_DATA) 1069 return TEE_SUCCESS; /* pure data object */ 1070 if (!o->attr) 1071 return TEE_ERROR_BAD_STATE; 1072 tp = tee_svc_find_type_props(o->info.objectType); 1073 if (!tp) 1074 return TEE_ERROR_BAD_STATE; 1075 1076 for (n = 0; n < tp->num_type_attrs; n++) { 1077 const struct tee_cryp_obj_type_attrs *ta = tp->type_attrs + n; 1078 void *attr = (uint8_t *)o->attr + ta->raw_offs; 1079 1080 if (!attr_ops[ta->ops_index].from_binary(attr, data, data_len, 1081 &offs)) 1082 return TEE_ERROR_CORRUPT_OBJECT; 1083 } 1084 return TEE_SUCCESS; 1085 } 1086 1087 TEE_Result tee_obj_attr_copy_from(struct tee_obj *o, const struct tee_obj *src) 1088 { 1089 TEE_Result res; 1090 const struct tee_cryp_obj_type_props *tp; 1091 const struct tee_cryp_obj_type_attrs *ta; 1092 size_t n; 1093 uint32_t have_attrs = 0; 1094 void *attr; 1095 void *src_attr; 1096 1097 if (o->info.objectType == TEE_TYPE_DATA) 1098 return TEE_SUCCESS; /* pure data object */ 1099 if (!o->attr) 1100 return TEE_ERROR_BAD_STATE; 1101 tp = tee_svc_find_type_props(o->info.objectType); 1102 if (!tp) 1103 return TEE_ERROR_BAD_STATE; 1104 1105 if (o->info.objectType == src->info.objectType) { 1106 have_attrs = src->have_attrs; 1107 for (n = 0; n < tp->num_type_attrs; n++) { 1108 ta = tp->type_attrs + n; 1109 attr = (uint8_t *)o->attr + ta->raw_offs; 1110 src_attr = (uint8_t *)src->attr + ta->raw_offs; 1111 res = attr_ops[ta->ops_index].from_obj(attr, src_attr); 1112 if (res != TEE_SUCCESS) 1113 return res; 1114 } 1115 } else { 1116 const struct tee_cryp_obj_type_props *tp_src; 1117 int idx; 1118 1119 if (o->info.objectType == TEE_TYPE_RSA_PUBLIC_KEY) { 1120 if (src->info.objectType != TEE_TYPE_RSA_KEYPAIR) 1121 return TEE_ERROR_BAD_PARAMETERS; 1122 } else if (o->info.objectType == TEE_TYPE_DSA_PUBLIC_KEY) { 1123 if (src->info.objectType != TEE_TYPE_DSA_KEYPAIR) 1124 return TEE_ERROR_BAD_PARAMETERS; 1125 } else if (o->info.objectType == TEE_TYPE_ECDSA_PUBLIC_KEY) { 1126 if (src->info.objectType != TEE_TYPE_ECDSA_KEYPAIR) 1127 return TEE_ERROR_BAD_PARAMETERS; 1128 } else if (o->info.objectType == TEE_TYPE_ECDH_PUBLIC_KEY) { 1129 if (src->info.objectType != TEE_TYPE_ECDH_KEYPAIR) 1130 return TEE_ERROR_BAD_PARAMETERS; 1131 } else { 1132 return TEE_ERROR_BAD_PARAMETERS; 1133 } 1134 1135 tp_src = tee_svc_find_type_props(src->info.objectType); 1136 if (!tp_src) 1137 return TEE_ERROR_BAD_STATE; 1138 1139 have_attrs = BIT32(tp->num_type_attrs) - 1; 1140 for (n = 0; n < tp->num_type_attrs; n++) { 1141 ta = tp->type_attrs + n; 1142 1143 idx = tee_svc_cryp_obj_find_type_attr_idx(ta->attr_id, 1144 tp_src); 1145 if (idx < 0) 1146 return TEE_ERROR_BAD_STATE; 1147 1148 attr = (uint8_t *)o->attr + ta->raw_offs; 1149 src_attr = (uint8_t *)src->attr + 1150 tp_src->type_attrs[idx].raw_offs; 1151 res = attr_ops[ta->ops_index].from_obj(attr, src_attr); 1152 if (res != TEE_SUCCESS) 1153 return res; 1154 } 1155 } 1156 1157 o->have_attrs = have_attrs; 1158 return TEE_SUCCESS; 1159 } 1160 1161 TEE_Result tee_obj_set_type(struct tee_obj *o, uint32_t obj_type, 1162 size_t max_key_size) 1163 { 1164 TEE_Result res = TEE_SUCCESS; 1165 const struct tee_cryp_obj_type_props *type_props; 1166 1167 /* Can only set type for newly allocated objs */ 1168 if (o->attr) 1169 return TEE_ERROR_BAD_STATE; 1170 1171 /* 1172 * Verify that maxKeySize is supported and find out how 1173 * much should be allocated. 1174 */ 1175 1176 if (obj_type == TEE_TYPE_DATA) { 1177 if (max_key_size) 1178 return TEE_ERROR_NOT_SUPPORTED; 1179 } else { 1180 /* Find description of object */ 1181 type_props = tee_svc_find_type_props(obj_type); 1182 if (!type_props) 1183 return TEE_ERROR_NOT_SUPPORTED; 1184 1185 /* Check that maxKeySize follows restrictions */ 1186 if (max_key_size % type_props->quanta != 0) 1187 return TEE_ERROR_NOT_SUPPORTED; 1188 if (max_key_size < type_props->min_size) 1189 return TEE_ERROR_NOT_SUPPORTED; 1190 if (max_key_size > type_props->max_size) 1191 return TEE_ERROR_NOT_SUPPORTED; 1192 1193 o->attr = calloc(1, type_props->alloc_size); 1194 if (!o->attr) 1195 return TEE_ERROR_OUT_OF_MEMORY; 1196 } 1197 1198 /* If we have a key structure, pre-allocate the bignums inside */ 1199 switch (obj_type) { 1200 case TEE_TYPE_RSA_PUBLIC_KEY: 1201 res = crypto_acipher_alloc_rsa_public_key(o->attr, 1202 max_key_size); 1203 break; 1204 case TEE_TYPE_RSA_KEYPAIR: 1205 res = crypto_acipher_alloc_rsa_keypair(o->attr, max_key_size); 1206 break; 1207 case TEE_TYPE_DSA_PUBLIC_KEY: 1208 res = crypto_acipher_alloc_dsa_public_key(o->attr, 1209 max_key_size); 1210 break; 1211 case TEE_TYPE_DSA_KEYPAIR: 1212 res = crypto_acipher_alloc_dsa_keypair(o->attr, max_key_size); 1213 break; 1214 case TEE_TYPE_DH_KEYPAIR: 1215 res = crypto_acipher_alloc_dh_keypair(o->attr, max_key_size); 1216 break; 1217 case TEE_TYPE_ECDSA_PUBLIC_KEY: 1218 case TEE_TYPE_ECDH_PUBLIC_KEY: 1219 res = crypto_acipher_alloc_ecc_public_key(o->attr, 1220 max_key_size); 1221 break; 1222 case TEE_TYPE_ECDSA_KEYPAIR: 1223 case TEE_TYPE_ECDH_KEYPAIR: 1224 res = crypto_acipher_alloc_ecc_keypair(o->attr, max_key_size); 1225 break; 1226 default: 1227 if (obj_type != TEE_TYPE_DATA) { 1228 struct tee_cryp_obj_secret *key = o->attr; 1229 1230 key->alloc_size = type_props->alloc_size - 1231 sizeof(*key); 1232 } 1233 break; 1234 } 1235 1236 if (res != TEE_SUCCESS) 1237 return res; 1238 1239 o->info.objectType = obj_type; 1240 o->info.maxKeySize = max_key_size; 1241 o->info.objectUsage = TEE_USAGE_DEFAULT; 1242 1243 return TEE_SUCCESS; 1244 } 1245 1246 TEE_Result syscall_cryp_obj_alloc(unsigned long obj_type, 1247 unsigned long max_key_size, uint32_t *obj) 1248 { 1249 TEE_Result res; 1250 struct tee_ta_session *sess; 1251 struct tee_obj *o; 1252 1253 if (obj_type == TEE_TYPE_DATA) 1254 return TEE_ERROR_NOT_SUPPORTED; 1255 1256 res = tee_ta_get_current_session(&sess); 1257 if (res != TEE_SUCCESS) 1258 return res; 1259 1260 o = tee_obj_alloc(); 1261 if (!o) 1262 return TEE_ERROR_OUT_OF_MEMORY; 1263 1264 res = tee_obj_set_type(o, obj_type, max_key_size); 1265 if (res != TEE_SUCCESS) { 1266 tee_obj_free(o); 1267 return res; 1268 } 1269 1270 tee_obj_add(to_user_ta_ctx(sess->ctx), o); 1271 1272 res = tee_svc_copy_kaddr_to_uref(obj, o); 1273 if (res != TEE_SUCCESS) 1274 tee_obj_close(to_user_ta_ctx(sess->ctx), o); 1275 return res; 1276 } 1277 1278 TEE_Result syscall_cryp_obj_close(unsigned long obj) 1279 { 1280 TEE_Result res; 1281 struct tee_ta_session *sess; 1282 struct tee_obj *o; 1283 1284 res = tee_ta_get_current_session(&sess); 1285 if (res != TEE_SUCCESS) 1286 return res; 1287 1288 res = tee_obj_get(to_user_ta_ctx(sess->ctx), 1289 tee_svc_uref_to_vaddr(obj), &o); 1290 if (res != TEE_SUCCESS) 1291 return res; 1292 1293 /* 1294 * If it's busy it's used by an operation, a client should never have 1295 * this handle. 1296 */ 1297 if (o->busy) 1298 return TEE_ERROR_ITEM_NOT_FOUND; 1299 1300 tee_obj_close(to_user_ta_ctx(sess->ctx), o); 1301 return TEE_SUCCESS; 1302 } 1303 1304 TEE_Result syscall_cryp_obj_reset(unsigned long obj) 1305 { 1306 TEE_Result res; 1307 struct tee_ta_session *sess; 1308 struct tee_obj *o; 1309 1310 res = tee_ta_get_current_session(&sess); 1311 if (res != TEE_SUCCESS) 1312 return res; 1313 1314 res = tee_obj_get(to_user_ta_ctx(sess->ctx), 1315 tee_svc_uref_to_vaddr(obj), &o); 1316 if (res != TEE_SUCCESS) 1317 return res; 1318 1319 if ((o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) == 0) { 1320 tee_obj_attr_clear(o); 1321 o->info.keySize = 0; 1322 o->info.objectUsage = TEE_USAGE_DEFAULT; 1323 } else { 1324 return TEE_ERROR_BAD_PARAMETERS; 1325 } 1326 1327 /* the object is no more initialized */ 1328 o->info.handleFlags &= ~TEE_HANDLE_FLAG_INITIALIZED; 1329 1330 return TEE_SUCCESS; 1331 } 1332 1333 static TEE_Result copy_in_attrs(struct user_ta_ctx *utc, 1334 const struct utee_attribute *usr_attrs, 1335 uint32_t attr_count, TEE_Attribute *attrs) 1336 { 1337 TEE_Result res; 1338 uint32_t n; 1339 1340 res = tee_mmu_check_access_rights(utc, 1341 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_ANY_OWNER, 1342 (uaddr_t)usr_attrs, 1343 attr_count * sizeof(struct utee_attribute)); 1344 if (res != TEE_SUCCESS) 1345 return res; 1346 1347 for (n = 0; n < attr_count; n++) { 1348 attrs[n].attributeID = usr_attrs[n].attribute_id; 1349 if (attrs[n].attributeID & TEE_ATTR_BIT_VALUE) { 1350 attrs[n].content.value.a = usr_attrs[n].a; 1351 attrs[n].content.value.b = usr_attrs[n].b; 1352 } else { 1353 uintptr_t buf = usr_attrs[n].a; 1354 size_t len = usr_attrs[n].b; 1355 1356 res = tee_mmu_check_access_rights(utc, 1357 TEE_MEMORY_ACCESS_READ | 1358 TEE_MEMORY_ACCESS_ANY_OWNER, buf, len); 1359 if (res != TEE_SUCCESS) 1360 return res; 1361 attrs[n].content.ref.buffer = (void *)buf; 1362 attrs[n].content.ref.length = len; 1363 } 1364 } 1365 1366 return TEE_SUCCESS; 1367 } 1368 1369 enum attr_usage { 1370 ATTR_USAGE_POPULATE, 1371 ATTR_USAGE_GENERATE_KEY 1372 }; 1373 1374 static TEE_Result tee_svc_cryp_check_attr(enum attr_usage usage, 1375 const struct tee_cryp_obj_type_props 1376 *type_props, 1377 const TEE_Attribute *attrs, 1378 uint32_t attr_count) 1379 { 1380 uint32_t required_flag; 1381 uint32_t opt_flag; 1382 bool all_opt_needed; 1383 uint32_t req_attrs = 0; 1384 uint32_t opt_grp_attrs = 0; 1385 uint32_t attrs_found = 0; 1386 size_t n; 1387 uint32_t bit; 1388 uint32_t flags; 1389 int idx; 1390 1391 if (usage == ATTR_USAGE_POPULATE) { 1392 required_flag = TEE_TYPE_ATTR_REQUIRED; 1393 opt_flag = TEE_TYPE_ATTR_OPTIONAL_GROUP; 1394 all_opt_needed = true; 1395 } else { 1396 required_flag = TEE_TYPE_ATTR_GEN_KEY_REQ; 1397 opt_flag = TEE_TYPE_ATTR_GEN_KEY_OPT; 1398 all_opt_needed = false; 1399 } 1400 1401 /* 1402 * First find out which attributes are required and which belong to 1403 * the optional group 1404 */ 1405 for (n = 0; n < type_props->num_type_attrs; n++) { 1406 bit = 1 << n; 1407 flags = type_props->type_attrs[n].flags; 1408 1409 if (flags & required_flag) 1410 req_attrs |= bit; 1411 else if (flags & opt_flag) 1412 opt_grp_attrs |= bit; 1413 } 1414 1415 /* 1416 * Verify that all required attributes are in place and 1417 * that the same attribute isn't repeated. 1418 */ 1419 for (n = 0; n < attr_count; n++) { 1420 idx = tee_svc_cryp_obj_find_type_attr_idx( 1421 attrs[n].attributeID, 1422 type_props); 1423 1424 /* attribute not defined in current object type */ 1425 if (idx < 0) 1426 return TEE_ERROR_ITEM_NOT_FOUND; 1427 1428 bit = 1 << idx; 1429 1430 /* attribute not repeated */ 1431 if ((attrs_found & bit) != 0) 1432 return TEE_ERROR_ITEM_NOT_FOUND; 1433 1434 attrs_found |= bit; 1435 } 1436 /* Required attribute missing */ 1437 if ((attrs_found & req_attrs) != req_attrs) 1438 return TEE_ERROR_ITEM_NOT_FOUND; 1439 1440 /* 1441 * If the flag says that "if one of the optional attributes are included 1442 * all of them has to be included" this must be checked. 1443 */ 1444 if (all_opt_needed && (attrs_found & opt_grp_attrs) != 0 && 1445 (attrs_found & opt_grp_attrs) != opt_grp_attrs) 1446 return TEE_ERROR_ITEM_NOT_FOUND; 1447 1448 return TEE_SUCCESS; 1449 } 1450 1451 static TEE_Result tee_svc_cryp_obj_populate_type( 1452 struct tee_obj *o, 1453 const struct tee_cryp_obj_type_props *type_props, 1454 const TEE_Attribute *attrs, 1455 uint32_t attr_count) 1456 { 1457 TEE_Result res; 1458 uint32_t have_attrs = 0; 1459 size_t obj_size = 0; 1460 size_t n; 1461 int idx; 1462 const struct attr_ops *ops; 1463 void *attr; 1464 1465 for (n = 0; n < attr_count; n++) { 1466 idx = tee_svc_cryp_obj_find_type_attr_idx( 1467 attrs[n].attributeID, 1468 type_props); 1469 /* attribute not defined in current object type */ 1470 if (idx < 0) 1471 return TEE_ERROR_ITEM_NOT_FOUND; 1472 1473 have_attrs |= BIT32(idx); 1474 ops = attr_ops + type_props->type_attrs[idx].ops_index; 1475 attr = (uint8_t *)o->attr + 1476 type_props->type_attrs[idx].raw_offs; 1477 if (attrs[n].attributeID & TEE_ATTR_BIT_VALUE) 1478 res = ops->from_user(attr, &attrs[n].content.value, 1479 sizeof(attrs[n].content.value)); 1480 else 1481 res = ops->from_user(attr, attrs[n].content.ref.buffer, 1482 attrs[n].content.ref.length); 1483 if (res != TEE_SUCCESS) 1484 return res; 1485 1486 /* 1487 * First attr_idx signifies the attribute that gives the size 1488 * of the object 1489 */ 1490 if (type_props->type_attrs[idx].flags & 1491 TEE_TYPE_ATTR_SIZE_INDICATOR) 1492 obj_size += attrs[n].content.ref.length * 8; 1493 } 1494 1495 /* 1496 * We have to do it like this because the parity bits aren't counted 1497 * when telling the size of the key in bits. 1498 */ 1499 if (o->info.objectType == TEE_TYPE_DES || 1500 o->info.objectType == TEE_TYPE_DES3) 1501 obj_size -= obj_size / 8; /* Exclude parity in size of key */ 1502 1503 o->have_attrs = have_attrs; 1504 o->info.keySize = obj_size; 1505 1506 return TEE_SUCCESS; 1507 } 1508 1509 TEE_Result syscall_cryp_obj_populate(unsigned long obj, 1510 struct utee_attribute *usr_attrs, 1511 unsigned long attr_count) 1512 { 1513 TEE_Result res; 1514 struct tee_ta_session *sess; 1515 struct tee_obj *o; 1516 const struct tee_cryp_obj_type_props *type_props; 1517 TEE_Attribute *attrs = NULL; 1518 1519 res = tee_ta_get_current_session(&sess); 1520 if (res != TEE_SUCCESS) 1521 return res; 1522 1523 res = tee_obj_get(to_user_ta_ctx(sess->ctx), 1524 tee_svc_uref_to_vaddr(obj), &o); 1525 if (res != TEE_SUCCESS) 1526 return res; 1527 1528 /* Must be a transient object */ 1529 if ((o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 1530 return TEE_ERROR_BAD_PARAMETERS; 1531 1532 /* Must not be initialized already */ 1533 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1534 return TEE_ERROR_BAD_PARAMETERS; 1535 1536 type_props = tee_svc_find_type_props(o->info.objectType); 1537 if (!type_props) 1538 return TEE_ERROR_NOT_IMPLEMENTED; 1539 1540 attrs = malloc(sizeof(TEE_Attribute) * attr_count); 1541 if (!attrs) 1542 return TEE_ERROR_OUT_OF_MEMORY; 1543 res = copy_in_attrs(to_user_ta_ctx(sess->ctx), usr_attrs, attr_count, 1544 attrs); 1545 if (res != TEE_SUCCESS) 1546 goto out; 1547 1548 res = tee_svc_cryp_check_attr(ATTR_USAGE_POPULATE, type_props, 1549 attrs, attr_count); 1550 if (res != TEE_SUCCESS) 1551 goto out; 1552 1553 res = tee_svc_cryp_obj_populate_type(o, type_props, attrs, attr_count); 1554 if (res == TEE_SUCCESS) 1555 o->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 1556 1557 out: 1558 free(attrs); 1559 return res; 1560 } 1561 1562 TEE_Result syscall_cryp_obj_copy(unsigned long dst, unsigned long src) 1563 { 1564 TEE_Result res; 1565 struct tee_ta_session *sess; 1566 struct tee_obj *dst_o; 1567 struct tee_obj *src_o; 1568 1569 res = tee_ta_get_current_session(&sess); 1570 if (res != TEE_SUCCESS) 1571 return res; 1572 1573 res = tee_obj_get(to_user_ta_ctx(sess->ctx), 1574 tee_svc_uref_to_vaddr(dst), &dst_o); 1575 if (res != TEE_SUCCESS) 1576 return res; 1577 1578 res = tee_obj_get(to_user_ta_ctx(sess->ctx), 1579 tee_svc_uref_to_vaddr(src), &src_o); 1580 if (res != TEE_SUCCESS) 1581 return res; 1582 1583 if ((src_o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1584 return TEE_ERROR_BAD_PARAMETERS; 1585 if ((dst_o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 1586 return TEE_ERROR_BAD_PARAMETERS; 1587 if ((dst_o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1588 return TEE_ERROR_BAD_PARAMETERS; 1589 1590 res = tee_obj_attr_copy_from(dst_o, src_o); 1591 if (res != TEE_SUCCESS) 1592 return res; 1593 1594 dst_o->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 1595 dst_o->info.keySize = src_o->info.keySize; 1596 dst_o->info.objectUsage = src_o->info.objectUsage; 1597 return TEE_SUCCESS; 1598 } 1599 1600 static TEE_Result tee_svc_obj_generate_key_rsa( 1601 struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props, 1602 uint32_t key_size, 1603 const TEE_Attribute *params, uint32_t param_count) 1604 { 1605 TEE_Result res; 1606 struct rsa_keypair *key = o->attr; 1607 uint32_t e = TEE_U32_TO_BIG_ENDIAN(65537); 1608 1609 /* Copy the present attributes into the obj before starting */ 1610 res = tee_svc_cryp_obj_populate_type(o, type_props, params, 1611 param_count); 1612 if (res != TEE_SUCCESS) 1613 return res; 1614 if (!get_attribute(o, type_props, TEE_ATTR_RSA_PUBLIC_EXPONENT)) 1615 crypto_bignum_bin2bn((const uint8_t *)&e, sizeof(e), key->e); 1616 res = crypto_acipher_gen_rsa_key(key, key_size); 1617 if (res != TEE_SUCCESS) 1618 return res; 1619 1620 /* Set bits for all known attributes for this object type */ 1621 o->have_attrs = (1 << type_props->num_type_attrs) - 1; 1622 1623 return TEE_SUCCESS; 1624 } 1625 1626 static TEE_Result tee_svc_obj_generate_key_dsa( 1627 struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props, 1628 uint32_t key_size) 1629 { 1630 TEE_Result res; 1631 1632 res = crypto_acipher_gen_dsa_key(o->attr, key_size); 1633 if (res != TEE_SUCCESS) 1634 return res; 1635 1636 /* Set bits for all known attributes for this object type */ 1637 o->have_attrs = (1 << type_props->num_type_attrs) - 1; 1638 1639 return TEE_SUCCESS; 1640 } 1641 1642 static TEE_Result tee_svc_obj_generate_key_dh( 1643 struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props, 1644 uint32_t key_size __unused, 1645 const TEE_Attribute *params, uint32_t param_count) 1646 { 1647 TEE_Result res; 1648 struct dh_keypair *tee_dh_key; 1649 struct bignum *dh_q = NULL; 1650 uint32_t dh_xbits = 0; 1651 1652 /* Copy the present attributes into the obj before starting */ 1653 res = tee_svc_cryp_obj_populate_type(o, type_props, params, 1654 param_count); 1655 if (res != TEE_SUCCESS) 1656 return res; 1657 1658 tee_dh_key = (struct dh_keypair *)o->attr; 1659 1660 if (get_attribute(o, type_props, TEE_ATTR_DH_SUBPRIME)) 1661 dh_q = tee_dh_key->q; 1662 if (get_attribute(o, type_props, TEE_ATTR_DH_X_BITS)) 1663 dh_xbits = tee_dh_key->xbits; 1664 res = crypto_acipher_gen_dh_key(tee_dh_key, dh_q, dh_xbits); 1665 if (res != TEE_SUCCESS) 1666 return res; 1667 1668 /* Set bits for the generated public and private key */ 1669 set_attribute(o, type_props, TEE_ATTR_DH_PUBLIC_VALUE); 1670 set_attribute(o, type_props, TEE_ATTR_DH_PRIVATE_VALUE); 1671 set_attribute(o, type_props, TEE_ATTR_DH_X_BITS); 1672 return TEE_SUCCESS; 1673 } 1674 1675 static TEE_Result tee_svc_obj_generate_key_ecc( 1676 struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props, 1677 uint32_t key_size __unused, 1678 const TEE_Attribute *params, uint32_t param_count) 1679 { 1680 TEE_Result res; 1681 struct ecc_keypair *tee_ecc_key; 1682 1683 /* Copy the present attributes into the obj before starting */ 1684 res = tee_svc_cryp_obj_populate_type(o, type_props, params, 1685 param_count); 1686 if (res != TEE_SUCCESS) 1687 return res; 1688 1689 tee_ecc_key = (struct ecc_keypair *)o->attr; 1690 1691 res = crypto_acipher_gen_ecc_key(tee_ecc_key); 1692 if (res != TEE_SUCCESS) 1693 return res; 1694 1695 /* Set bits for the generated public and private key */ 1696 set_attribute(o, type_props, TEE_ATTR_ECC_PRIVATE_VALUE); 1697 set_attribute(o, type_props, TEE_ATTR_ECC_PUBLIC_VALUE_X); 1698 set_attribute(o, type_props, TEE_ATTR_ECC_PUBLIC_VALUE_Y); 1699 set_attribute(o, type_props, TEE_ATTR_ECC_CURVE); 1700 return TEE_SUCCESS; 1701 } 1702 1703 TEE_Result syscall_obj_generate_key(unsigned long obj, unsigned long key_size, 1704 const struct utee_attribute *usr_params, 1705 unsigned long param_count) 1706 { 1707 TEE_Result res; 1708 struct tee_ta_session *sess; 1709 const struct tee_cryp_obj_type_props *type_props; 1710 struct tee_obj *o; 1711 struct tee_cryp_obj_secret *key; 1712 size_t byte_size; 1713 TEE_Attribute *params = NULL; 1714 1715 res = tee_ta_get_current_session(&sess); 1716 if (res != TEE_SUCCESS) 1717 return res; 1718 1719 res = tee_obj_get(to_user_ta_ctx(sess->ctx), 1720 tee_svc_uref_to_vaddr(obj), &o); 1721 if (res != TEE_SUCCESS) 1722 return res; 1723 1724 /* Must be a transient object */ 1725 if ((o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 1726 return TEE_ERROR_BAD_STATE; 1727 1728 /* Must not be initialized already */ 1729 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1730 return TEE_ERROR_BAD_STATE; 1731 1732 /* Find description of object */ 1733 type_props = tee_svc_find_type_props(o->info.objectType); 1734 if (!type_props) 1735 return TEE_ERROR_NOT_SUPPORTED; 1736 1737 /* Check that maxKeySize follows restrictions */ 1738 if (key_size % type_props->quanta != 0) 1739 return TEE_ERROR_NOT_SUPPORTED; 1740 if (key_size < type_props->min_size) 1741 return TEE_ERROR_NOT_SUPPORTED; 1742 if (key_size > type_props->max_size) 1743 return TEE_ERROR_NOT_SUPPORTED; 1744 1745 params = malloc(sizeof(TEE_Attribute) * param_count); 1746 if (!params) 1747 return TEE_ERROR_OUT_OF_MEMORY; 1748 res = copy_in_attrs(to_user_ta_ctx(sess->ctx), usr_params, param_count, 1749 params); 1750 if (res != TEE_SUCCESS) 1751 goto out; 1752 1753 res = tee_svc_cryp_check_attr(ATTR_USAGE_GENERATE_KEY, type_props, 1754 params, param_count); 1755 if (res != TEE_SUCCESS) 1756 goto out; 1757 1758 switch (o->info.objectType) { 1759 case TEE_TYPE_AES: 1760 case TEE_TYPE_DES: 1761 case TEE_TYPE_DES3: 1762 case TEE_TYPE_HMAC_MD5: 1763 case TEE_TYPE_HMAC_SHA1: 1764 case TEE_TYPE_HMAC_SHA224: 1765 case TEE_TYPE_HMAC_SHA256: 1766 case TEE_TYPE_HMAC_SHA384: 1767 case TEE_TYPE_HMAC_SHA512: 1768 case TEE_TYPE_GENERIC_SECRET: 1769 byte_size = key_size / 8; 1770 1771 /* 1772 * We have to do it like this because the parity bits aren't 1773 * counted when telling the size of the key in bits. 1774 */ 1775 if (o->info.objectType == TEE_TYPE_DES || 1776 o->info.objectType == TEE_TYPE_DES3) { 1777 byte_size = (key_size + key_size / 7) / 8; 1778 } 1779 1780 key = (struct tee_cryp_obj_secret *)o->attr; 1781 if (byte_size > key->alloc_size) { 1782 res = TEE_ERROR_EXCESS_DATA; 1783 goto out; 1784 } 1785 1786 res = crypto_rng_read((void *)(key + 1), byte_size); 1787 if (res != TEE_SUCCESS) 1788 goto out; 1789 1790 key->key_size = byte_size; 1791 1792 /* Set bits for all known attributes for this object type */ 1793 o->have_attrs = (1 << type_props->num_type_attrs) - 1; 1794 1795 break; 1796 1797 case TEE_TYPE_RSA_KEYPAIR: 1798 res = tee_svc_obj_generate_key_rsa(o, type_props, key_size, 1799 params, param_count); 1800 if (res != TEE_SUCCESS) 1801 goto out; 1802 break; 1803 1804 case TEE_TYPE_DSA_KEYPAIR: 1805 res = tee_svc_obj_generate_key_dsa(o, type_props, key_size); 1806 if (res != TEE_SUCCESS) 1807 goto out; 1808 break; 1809 1810 case TEE_TYPE_DH_KEYPAIR: 1811 res = tee_svc_obj_generate_key_dh(o, type_props, key_size, 1812 params, param_count); 1813 if (res != TEE_SUCCESS) 1814 goto out; 1815 break; 1816 1817 case TEE_TYPE_ECDSA_KEYPAIR: 1818 case TEE_TYPE_ECDH_KEYPAIR: 1819 res = tee_svc_obj_generate_key_ecc(o, type_props, key_size, 1820 params, param_count); 1821 if (res != TEE_SUCCESS) 1822 goto out; 1823 break; 1824 1825 default: 1826 res = TEE_ERROR_BAD_FORMAT; 1827 } 1828 1829 out: 1830 free(params); 1831 if (res == TEE_SUCCESS) { 1832 o->info.keySize = key_size; 1833 o->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 1834 } 1835 return res; 1836 } 1837 1838 static TEE_Result tee_svc_cryp_get_state(struct tee_ta_session *sess, 1839 uint32_t state_id, 1840 struct tee_cryp_state **state) 1841 { 1842 struct tee_cryp_state *s; 1843 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 1844 1845 TAILQ_FOREACH(s, &utc->cryp_states, link) { 1846 if (state_id == (vaddr_t)s) { 1847 *state = s; 1848 return TEE_SUCCESS; 1849 } 1850 } 1851 return TEE_ERROR_BAD_PARAMETERS; 1852 } 1853 1854 static void cryp_state_free(struct user_ta_ctx *utc, struct tee_cryp_state *cs) 1855 { 1856 struct tee_obj *o; 1857 1858 if (tee_obj_get(utc, cs->key1, &o) == TEE_SUCCESS) 1859 tee_obj_close(utc, o); 1860 if (tee_obj_get(utc, cs->key2, &o) == TEE_SUCCESS) 1861 tee_obj_close(utc, o); 1862 1863 TAILQ_REMOVE(&utc->cryp_states, cs, link); 1864 if (cs->ctx_finalize != NULL) 1865 cs->ctx_finalize(cs->ctx, cs->algo); 1866 1867 switch (TEE_ALG_GET_CLASS(cs->algo)) { 1868 case TEE_OPERATION_CIPHER: 1869 crypto_cipher_free_ctx(cs->ctx, cs->algo); 1870 break; 1871 case TEE_OPERATION_DIGEST: 1872 crypto_hash_free_ctx(cs->ctx, cs->algo); 1873 break; 1874 case TEE_OPERATION_MAC: 1875 crypto_mac_free_ctx(cs->ctx, cs->algo); 1876 break; 1877 default: 1878 free(cs->ctx); 1879 } 1880 1881 free(cs); 1882 } 1883 1884 static TEE_Result tee_svc_cryp_check_key_type(const struct tee_obj *o, 1885 uint32_t algo, 1886 TEE_OperationMode mode) 1887 { 1888 uint32_t req_key_type; 1889 uint32_t req_key_type2 = 0; 1890 1891 switch (TEE_ALG_GET_MAIN_ALG(algo)) { 1892 case TEE_MAIN_ALGO_MD5: 1893 req_key_type = TEE_TYPE_HMAC_MD5; 1894 break; 1895 case TEE_MAIN_ALGO_SHA1: 1896 req_key_type = TEE_TYPE_HMAC_SHA1; 1897 break; 1898 case TEE_MAIN_ALGO_SHA224: 1899 req_key_type = TEE_TYPE_HMAC_SHA224; 1900 break; 1901 case TEE_MAIN_ALGO_SHA256: 1902 req_key_type = TEE_TYPE_HMAC_SHA256; 1903 break; 1904 case TEE_MAIN_ALGO_SHA384: 1905 req_key_type = TEE_TYPE_HMAC_SHA384; 1906 break; 1907 case TEE_MAIN_ALGO_SHA512: 1908 req_key_type = TEE_TYPE_HMAC_SHA512; 1909 break; 1910 case TEE_MAIN_ALGO_AES: 1911 req_key_type = TEE_TYPE_AES; 1912 break; 1913 case TEE_MAIN_ALGO_DES: 1914 req_key_type = TEE_TYPE_DES; 1915 break; 1916 case TEE_MAIN_ALGO_DES3: 1917 req_key_type = TEE_TYPE_DES3; 1918 break; 1919 case TEE_MAIN_ALGO_RSA: 1920 req_key_type = TEE_TYPE_RSA_KEYPAIR; 1921 if (mode == TEE_MODE_ENCRYPT || mode == TEE_MODE_VERIFY) 1922 req_key_type2 = TEE_TYPE_RSA_PUBLIC_KEY; 1923 break; 1924 case TEE_MAIN_ALGO_DSA: 1925 req_key_type = TEE_TYPE_DSA_KEYPAIR; 1926 if (mode == TEE_MODE_ENCRYPT || mode == TEE_MODE_VERIFY) 1927 req_key_type2 = TEE_TYPE_DSA_PUBLIC_KEY; 1928 break; 1929 case TEE_MAIN_ALGO_DH: 1930 req_key_type = TEE_TYPE_DH_KEYPAIR; 1931 break; 1932 case TEE_MAIN_ALGO_ECDSA: 1933 req_key_type = TEE_TYPE_ECDSA_KEYPAIR; 1934 if (mode == TEE_MODE_VERIFY) 1935 req_key_type2 = TEE_TYPE_ECDSA_PUBLIC_KEY; 1936 break; 1937 case TEE_MAIN_ALGO_ECDH: 1938 req_key_type = TEE_TYPE_ECDH_KEYPAIR; 1939 break; 1940 #if defined(CFG_CRYPTO_HKDF) 1941 case TEE_MAIN_ALGO_HKDF: 1942 req_key_type = TEE_TYPE_HKDF_IKM; 1943 break; 1944 #endif 1945 #if defined(CFG_CRYPTO_CONCAT_KDF) 1946 case TEE_MAIN_ALGO_CONCAT_KDF: 1947 req_key_type = TEE_TYPE_CONCAT_KDF_Z; 1948 break; 1949 #endif 1950 #if defined(CFG_CRYPTO_PBKDF2) 1951 case TEE_MAIN_ALGO_PBKDF2: 1952 req_key_type = TEE_TYPE_PBKDF2_PASSWORD; 1953 break; 1954 #endif 1955 default: 1956 return TEE_ERROR_BAD_PARAMETERS; 1957 } 1958 1959 if (req_key_type != o->info.objectType && 1960 req_key_type2 != o->info.objectType) 1961 return TEE_ERROR_BAD_PARAMETERS; 1962 return TEE_SUCCESS; 1963 } 1964 1965 TEE_Result syscall_cryp_state_alloc(unsigned long algo, unsigned long mode, 1966 unsigned long key1, unsigned long key2, 1967 uint32_t *state) 1968 { 1969 TEE_Result res; 1970 struct tee_cryp_state *cs; 1971 struct tee_ta_session *sess; 1972 struct tee_obj *o1 = NULL; 1973 struct tee_obj *o2 = NULL; 1974 struct user_ta_ctx *utc; 1975 1976 res = tee_ta_get_current_session(&sess); 1977 if (res != TEE_SUCCESS) 1978 return res; 1979 utc = to_user_ta_ctx(sess->ctx); 1980 1981 if (key1 != 0) { 1982 res = tee_obj_get(utc, tee_svc_uref_to_vaddr(key1), &o1); 1983 if (res != TEE_SUCCESS) 1984 return res; 1985 if (o1->busy) 1986 return TEE_ERROR_BAD_PARAMETERS; 1987 res = tee_svc_cryp_check_key_type(o1, algo, mode); 1988 if (res != TEE_SUCCESS) 1989 return res; 1990 } 1991 if (key2 != 0) { 1992 res = tee_obj_get(utc, tee_svc_uref_to_vaddr(key2), &o2); 1993 if (res != TEE_SUCCESS) 1994 return res; 1995 if (o2->busy) 1996 return TEE_ERROR_BAD_PARAMETERS; 1997 res = tee_svc_cryp_check_key_type(o2, algo, mode); 1998 if (res != TEE_SUCCESS) 1999 return res; 2000 } 2001 2002 cs = calloc(1, sizeof(struct tee_cryp_state)); 2003 if (!cs) 2004 return TEE_ERROR_OUT_OF_MEMORY; 2005 TAILQ_INSERT_TAIL(&utc->cryp_states, cs, link); 2006 cs->algo = algo; 2007 cs->mode = mode; 2008 2009 switch (TEE_ALG_GET_CLASS(algo)) { 2010 case TEE_OPERATION_CIPHER: 2011 if ((algo == TEE_ALG_AES_XTS && (key1 == 0 || key2 == 0)) || 2012 (algo != TEE_ALG_AES_XTS && (key1 == 0 || key2 != 0))) { 2013 res = TEE_ERROR_BAD_PARAMETERS; 2014 } else { 2015 res = crypto_cipher_alloc_ctx(&cs->ctx, algo); 2016 if (res != TEE_SUCCESS) 2017 break; 2018 } 2019 break; 2020 case TEE_OPERATION_AE: 2021 if (key1 == 0 || key2 != 0) { 2022 res = TEE_ERROR_BAD_PARAMETERS; 2023 } else { 2024 res = crypto_authenc_get_ctx_size(algo, &cs->ctx_size); 2025 if (res != TEE_SUCCESS) 2026 break; 2027 cs->ctx = calloc(1, cs->ctx_size); 2028 if (!cs->ctx) 2029 res = TEE_ERROR_OUT_OF_MEMORY; 2030 } 2031 break; 2032 case TEE_OPERATION_MAC: 2033 if (key1 == 0 || key2 != 0) { 2034 res = TEE_ERROR_BAD_PARAMETERS; 2035 } else { 2036 res = crypto_mac_alloc_ctx(&cs->ctx, algo); 2037 if (res != TEE_SUCCESS) 2038 break; 2039 } 2040 break; 2041 case TEE_OPERATION_DIGEST: 2042 if (key1 != 0 || key2 != 0) { 2043 res = TEE_ERROR_BAD_PARAMETERS; 2044 } else { 2045 res = crypto_hash_alloc_ctx(&cs->ctx, algo); 2046 if (res != TEE_SUCCESS) 2047 break; 2048 } 2049 break; 2050 case TEE_OPERATION_ASYMMETRIC_CIPHER: 2051 case TEE_OPERATION_ASYMMETRIC_SIGNATURE: 2052 if (key1 == 0 || key2 != 0) 2053 res = TEE_ERROR_BAD_PARAMETERS; 2054 break; 2055 case TEE_OPERATION_KEY_DERIVATION: 2056 if (key1 == 0 || key2 != 0) 2057 res = TEE_ERROR_BAD_PARAMETERS; 2058 break; 2059 default: 2060 res = TEE_ERROR_NOT_SUPPORTED; 2061 break; 2062 } 2063 if (res != TEE_SUCCESS) 2064 goto out; 2065 2066 res = tee_svc_copy_kaddr_to_uref(state, cs); 2067 if (res != TEE_SUCCESS) 2068 goto out; 2069 2070 /* Register keys */ 2071 if (o1 != NULL) { 2072 o1->busy = true; 2073 cs->key1 = (vaddr_t)o1; 2074 } 2075 if (o2 != NULL) { 2076 o2->busy = true; 2077 cs->key2 = (vaddr_t)o2; 2078 } 2079 2080 out: 2081 if (res != TEE_SUCCESS) 2082 cryp_state_free(utc, cs); 2083 return res; 2084 } 2085 2086 TEE_Result syscall_cryp_state_copy(unsigned long dst, unsigned long src) 2087 { 2088 TEE_Result res; 2089 struct tee_cryp_state *cs_dst; 2090 struct tee_cryp_state *cs_src; 2091 struct tee_ta_session *sess; 2092 2093 res = tee_ta_get_current_session(&sess); 2094 if (res != TEE_SUCCESS) 2095 return res; 2096 2097 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(dst), &cs_dst); 2098 if (res != TEE_SUCCESS) 2099 return res; 2100 2101 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(src), &cs_src); 2102 if (res != TEE_SUCCESS) 2103 return res; 2104 if (cs_dst->algo != cs_src->algo || cs_dst->mode != cs_src->mode) 2105 return TEE_ERROR_BAD_PARAMETERS; 2106 /* "Can't happen" */ 2107 if (cs_dst->ctx_size != cs_src->ctx_size) 2108 return TEE_ERROR_BAD_STATE; 2109 2110 switch (TEE_ALG_GET_CLASS(cs_src->algo)) { 2111 case TEE_OPERATION_CIPHER: 2112 crypto_cipher_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 memcpy(cs_dst->ctx, cs_src->ctx, cs_src->ctx_size); 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; 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