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 if (TEE_ALG_GET_CLASS(cs->algo) == TEE_OPERATION_DIGEST) 1867 crypto_hash_free_ctx(cs->ctx, cs->algo); 1868 else 1869 free(cs->ctx); 1870 free(cs); 1871 } 1872 1873 static TEE_Result tee_svc_cryp_check_key_type(const struct tee_obj *o, 1874 uint32_t algo, 1875 TEE_OperationMode mode) 1876 { 1877 uint32_t req_key_type; 1878 uint32_t req_key_type2 = 0; 1879 1880 switch (TEE_ALG_GET_MAIN_ALG(algo)) { 1881 case TEE_MAIN_ALGO_MD5: 1882 req_key_type = TEE_TYPE_HMAC_MD5; 1883 break; 1884 case TEE_MAIN_ALGO_SHA1: 1885 req_key_type = TEE_TYPE_HMAC_SHA1; 1886 break; 1887 case TEE_MAIN_ALGO_SHA224: 1888 req_key_type = TEE_TYPE_HMAC_SHA224; 1889 break; 1890 case TEE_MAIN_ALGO_SHA256: 1891 req_key_type = TEE_TYPE_HMAC_SHA256; 1892 break; 1893 case TEE_MAIN_ALGO_SHA384: 1894 req_key_type = TEE_TYPE_HMAC_SHA384; 1895 break; 1896 case TEE_MAIN_ALGO_SHA512: 1897 req_key_type = TEE_TYPE_HMAC_SHA512; 1898 break; 1899 case TEE_MAIN_ALGO_AES: 1900 req_key_type = TEE_TYPE_AES; 1901 break; 1902 case TEE_MAIN_ALGO_DES: 1903 req_key_type = TEE_TYPE_DES; 1904 break; 1905 case TEE_MAIN_ALGO_DES3: 1906 req_key_type = TEE_TYPE_DES3; 1907 break; 1908 case TEE_MAIN_ALGO_RSA: 1909 req_key_type = TEE_TYPE_RSA_KEYPAIR; 1910 if (mode == TEE_MODE_ENCRYPT || mode == TEE_MODE_VERIFY) 1911 req_key_type2 = TEE_TYPE_RSA_PUBLIC_KEY; 1912 break; 1913 case TEE_MAIN_ALGO_DSA: 1914 req_key_type = TEE_TYPE_DSA_KEYPAIR; 1915 if (mode == TEE_MODE_ENCRYPT || mode == TEE_MODE_VERIFY) 1916 req_key_type2 = TEE_TYPE_DSA_PUBLIC_KEY; 1917 break; 1918 case TEE_MAIN_ALGO_DH: 1919 req_key_type = TEE_TYPE_DH_KEYPAIR; 1920 break; 1921 case TEE_MAIN_ALGO_ECDSA: 1922 req_key_type = TEE_TYPE_ECDSA_KEYPAIR; 1923 if (mode == TEE_MODE_VERIFY) 1924 req_key_type2 = TEE_TYPE_ECDSA_PUBLIC_KEY; 1925 break; 1926 case TEE_MAIN_ALGO_ECDH: 1927 req_key_type = TEE_TYPE_ECDH_KEYPAIR; 1928 break; 1929 #if defined(CFG_CRYPTO_HKDF) 1930 case TEE_MAIN_ALGO_HKDF: 1931 req_key_type = TEE_TYPE_HKDF_IKM; 1932 break; 1933 #endif 1934 #if defined(CFG_CRYPTO_CONCAT_KDF) 1935 case TEE_MAIN_ALGO_CONCAT_KDF: 1936 req_key_type = TEE_TYPE_CONCAT_KDF_Z; 1937 break; 1938 #endif 1939 #if defined(CFG_CRYPTO_PBKDF2) 1940 case TEE_MAIN_ALGO_PBKDF2: 1941 req_key_type = TEE_TYPE_PBKDF2_PASSWORD; 1942 break; 1943 #endif 1944 default: 1945 return TEE_ERROR_BAD_PARAMETERS; 1946 } 1947 1948 if (req_key_type != o->info.objectType && 1949 req_key_type2 != o->info.objectType) 1950 return TEE_ERROR_BAD_PARAMETERS; 1951 return TEE_SUCCESS; 1952 } 1953 1954 TEE_Result syscall_cryp_state_alloc(unsigned long algo, unsigned long mode, 1955 unsigned long key1, unsigned long key2, 1956 uint32_t *state) 1957 { 1958 TEE_Result res; 1959 struct tee_cryp_state *cs; 1960 struct tee_ta_session *sess; 1961 struct tee_obj *o1 = NULL; 1962 struct tee_obj *o2 = NULL; 1963 struct user_ta_ctx *utc; 1964 1965 res = tee_ta_get_current_session(&sess); 1966 if (res != TEE_SUCCESS) 1967 return res; 1968 utc = to_user_ta_ctx(sess->ctx); 1969 1970 if (key1 != 0) { 1971 res = tee_obj_get(utc, tee_svc_uref_to_vaddr(key1), &o1); 1972 if (res != TEE_SUCCESS) 1973 return res; 1974 if (o1->busy) 1975 return TEE_ERROR_BAD_PARAMETERS; 1976 res = tee_svc_cryp_check_key_type(o1, algo, mode); 1977 if (res != TEE_SUCCESS) 1978 return res; 1979 } 1980 if (key2 != 0) { 1981 res = tee_obj_get(utc, tee_svc_uref_to_vaddr(key2), &o2); 1982 if (res != TEE_SUCCESS) 1983 return res; 1984 if (o2->busy) 1985 return TEE_ERROR_BAD_PARAMETERS; 1986 res = tee_svc_cryp_check_key_type(o2, algo, mode); 1987 if (res != TEE_SUCCESS) 1988 return res; 1989 } 1990 1991 cs = calloc(1, sizeof(struct tee_cryp_state)); 1992 if (!cs) 1993 return TEE_ERROR_OUT_OF_MEMORY; 1994 TAILQ_INSERT_TAIL(&utc->cryp_states, cs, link); 1995 cs->algo = algo; 1996 cs->mode = mode; 1997 1998 switch (TEE_ALG_GET_CLASS(algo)) { 1999 case TEE_OPERATION_CIPHER: 2000 if ((algo == TEE_ALG_AES_XTS && (key1 == 0 || key2 == 0)) || 2001 (algo != TEE_ALG_AES_XTS && (key1 == 0 || key2 != 0))) { 2002 res = TEE_ERROR_BAD_PARAMETERS; 2003 } else { 2004 res = crypto_cipher_get_ctx_size(algo, &cs->ctx_size); 2005 if (res != TEE_SUCCESS) 2006 break; 2007 cs->ctx = calloc(1, cs->ctx_size); 2008 if (!cs->ctx) 2009 res = TEE_ERROR_OUT_OF_MEMORY; 2010 } 2011 break; 2012 case TEE_OPERATION_AE: 2013 if (key1 == 0 || key2 != 0) { 2014 res = TEE_ERROR_BAD_PARAMETERS; 2015 } else { 2016 res = crypto_authenc_get_ctx_size(algo, &cs->ctx_size); 2017 if (res != TEE_SUCCESS) 2018 break; 2019 cs->ctx = calloc(1, cs->ctx_size); 2020 if (!cs->ctx) 2021 res = TEE_ERROR_OUT_OF_MEMORY; 2022 } 2023 break; 2024 case TEE_OPERATION_MAC: 2025 if (key1 == 0 || key2 != 0) { 2026 res = TEE_ERROR_BAD_PARAMETERS; 2027 } else { 2028 res = crypto_mac_get_ctx_size(algo, &cs->ctx_size); 2029 if (res != TEE_SUCCESS) 2030 break; 2031 cs->ctx = calloc(1, cs->ctx_size); 2032 if (!cs->ctx) 2033 res = TEE_ERROR_OUT_OF_MEMORY; 2034 } 2035 break; 2036 case TEE_OPERATION_DIGEST: 2037 if (key1 != 0 || key2 != 0) { 2038 res = TEE_ERROR_BAD_PARAMETERS; 2039 } else { 2040 res = crypto_hash_alloc_ctx(&cs->ctx, algo); 2041 if (res != TEE_SUCCESS) 2042 break; 2043 } 2044 break; 2045 case TEE_OPERATION_ASYMMETRIC_CIPHER: 2046 case TEE_OPERATION_ASYMMETRIC_SIGNATURE: 2047 if (key1 == 0 || key2 != 0) 2048 res = TEE_ERROR_BAD_PARAMETERS; 2049 break; 2050 case TEE_OPERATION_KEY_DERIVATION: 2051 if (key1 == 0 || key2 != 0) 2052 res = TEE_ERROR_BAD_PARAMETERS; 2053 break; 2054 default: 2055 res = TEE_ERROR_NOT_SUPPORTED; 2056 break; 2057 } 2058 if (res != TEE_SUCCESS) 2059 goto out; 2060 2061 res = tee_svc_copy_kaddr_to_uref(state, cs); 2062 if (res != TEE_SUCCESS) 2063 goto out; 2064 2065 /* Register keys */ 2066 if (o1 != NULL) { 2067 o1->busy = true; 2068 cs->key1 = (vaddr_t)o1; 2069 } 2070 if (o2 != NULL) { 2071 o2->busy = true; 2072 cs->key2 = (vaddr_t)o2; 2073 } 2074 2075 out: 2076 if (res != TEE_SUCCESS) 2077 cryp_state_free(utc, cs); 2078 return res; 2079 } 2080 2081 TEE_Result syscall_cryp_state_copy(unsigned long dst, unsigned long src) 2082 { 2083 TEE_Result res; 2084 struct tee_cryp_state *cs_dst; 2085 struct tee_cryp_state *cs_src; 2086 struct tee_ta_session *sess; 2087 2088 res = tee_ta_get_current_session(&sess); 2089 if (res != TEE_SUCCESS) 2090 return res; 2091 2092 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(dst), &cs_dst); 2093 if (res != TEE_SUCCESS) 2094 return res; 2095 2096 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(src), &cs_src); 2097 if (res != TEE_SUCCESS) 2098 return res; 2099 if (cs_dst->algo != cs_src->algo || cs_dst->mode != cs_src->mode) 2100 return TEE_ERROR_BAD_PARAMETERS; 2101 /* "Can't happen" */ 2102 if (cs_dst->ctx_size != cs_src->ctx_size) 2103 return TEE_ERROR_BAD_STATE; 2104 2105 if (TEE_ALG_GET_CLASS(cs_src->algo) == TEE_OPERATION_DIGEST) 2106 crypto_hash_copy_state(cs_dst->ctx, cs_src->ctx, cs_src->algo); 2107 else 2108 memcpy(cs_dst->ctx, cs_src->ctx, cs_src->ctx_size); 2109 2110 return TEE_SUCCESS; 2111 } 2112 2113 void tee_svc_cryp_free_states(struct user_ta_ctx *utc) 2114 { 2115 struct tee_cryp_state_head *states = &utc->cryp_states; 2116 2117 while (!TAILQ_EMPTY(states)) 2118 cryp_state_free(utc, TAILQ_FIRST(states)); 2119 } 2120 2121 TEE_Result syscall_cryp_state_free(unsigned long state) 2122 { 2123 TEE_Result res; 2124 struct tee_cryp_state *cs; 2125 struct tee_ta_session *sess; 2126 2127 res = tee_ta_get_current_session(&sess); 2128 if (res != TEE_SUCCESS) 2129 return res; 2130 2131 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(state), &cs); 2132 if (res != TEE_SUCCESS) 2133 return res; 2134 cryp_state_free(to_user_ta_ctx(sess->ctx), cs); 2135 return TEE_SUCCESS; 2136 } 2137 2138 TEE_Result syscall_hash_init(unsigned long state, 2139 const void *iv __maybe_unused, 2140 size_t iv_len __maybe_unused) 2141 { 2142 TEE_Result res; 2143 struct tee_cryp_state *cs; 2144 struct tee_ta_session *sess; 2145 2146 res = tee_ta_get_current_session(&sess); 2147 if (res != TEE_SUCCESS) 2148 return res; 2149 2150 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(state), &cs); 2151 if (res != TEE_SUCCESS) 2152 return res; 2153 2154 switch (TEE_ALG_GET_CLASS(cs->algo)) { 2155 case TEE_OPERATION_DIGEST: 2156 res = crypto_hash_init(cs->ctx, cs->algo); 2157 if (res != TEE_SUCCESS) 2158 return res; 2159 break; 2160 case TEE_OPERATION_MAC: 2161 { 2162 struct tee_obj *o; 2163 struct tee_cryp_obj_secret *key; 2164 2165 res = tee_obj_get(to_user_ta_ctx(sess->ctx), 2166 cs->key1, &o); 2167 if (res != TEE_SUCCESS) 2168 return res; 2169 if ((o->info.handleFlags & 2170 TEE_HANDLE_FLAG_INITIALIZED) == 0) 2171 return TEE_ERROR_BAD_PARAMETERS; 2172 2173 key = (struct tee_cryp_obj_secret *)o->attr; 2174 res = crypto_mac_init(cs->ctx, cs->algo, 2175 (void *)(key + 1), key->key_size); 2176 if (res != TEE_SUCCESS) 2177 return res; 2178 break; 2179 } 2180 default: 2181 return TEE_ERROR_BAD_PARAMETERS; 2182 } 2183 2184 return TEE_SUCCESS; 2185 } 2186 2187 TEE_Result syscall_hash_update(unsigned long state, const void *chunk, 2188 size_t chunk_size) 2189 { 2190 TEE_Result res; 2191 struct tee_cryp_state *cs; 2192 struct tee_ta_session *sess; 2193 2194 /* No data, but size provided isn't valid parameters. */ 2195 if (!chunk && chunk_size) 2196 return TEE_ERROR_BAD_PARAMETERS; 2197 2198 /* Zero length hash is valid, but nothing we need to do. */ 2199 if (!chunk_size) 2200 return TEE_SUCCESS; 2201 2202 res = tee_ta_get_current_session(&sess); 2203 if (res != TEE_SUCCESS) 2204 return res; 2205 2206 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 2207 TEE_MEMORY_ACCESS_READ | 2208 TEE_MEMORY_ACCESS_ANY_OWNER, 2209 (uaddr_t)chunk, chunk_size); 2210 if (res != TEE_SUCCESS) 2211 return res; 2212 2213 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(state), &cs); 2214 if (res != TEE_SUCCESS) 2215 return res; 2216 2217 switch (TEE_ALG_GET_CLASS(cs->algo)) { 2218 case TEE_OPERATION_DIGEST: 2219 res = crypto_hash_update(cs->ctx, cs->algo, chunk, chunk_size); 2220 if (res != TEE_SUCCESS) 2221 return res; 2222 break; 2223 case TEE_OPERATION_MAC: 2224 res = crypto_mac_update(cs->ctx, cs->algo, chunk, chunk_size); 2225 if (res != TEE_SUCCESS) 2226 return res; 2227 break; 2228 default: 2229 return TEE_ERROR_BAD_PARAMETERS; 2230 } 2231 2232 return TEE_SUCCESS; 2233 } 2234 2235 TEE_Result syscall_hash_final(unsigned long state, const void *chunk, 2236 size_t chunk_size, void *hash, uint64_t *hash_len) 2237 { 2238 TEE_Result res, res2; 2239 size_t hash_size; 2240 uint64_t hlen; 2241 struct tee_cryp_state *cs; 2242 struct tee_ta_session *sess; 2243 2244 /* No data, but size provided isn't valid parameters. */ 2245 if (!chunk && chunk_size) 2246 return TEE_ERROR_BAD_PARAMETERS; 2247 2248 res = tee_ta_get_current_session(&sess); 2249 if (res != TEE_SUCCESS) 2250 return res; 2251 2252 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 2253 TEE_MEMORY_ACCESS_READ | 2254 TEE_MEMORY_ACCESS_ANY_OWNER, 2255 (uaddr_t)chunk, chunk_size); 2256 if (res != TEE_SUCCESS) 2257 return res; 2258 2259 res = tee_svc_copy_from_user(&hlen, hash_len, sizeof(hlen)); 2260 if (res != TEE_SUCCESS) 2261 return res; 2262 2263 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 2264 TEE_MEMORY_ACCESS_READ | 2265 TEE_MEMORY_ACCESS_WRITE | 2266 TEE_MEMORY_ACCESS_ANY_OWNER, 2267 (uaddr_t)hash, hlen); 2268 if (res != TEE_SUCCESS) 2269 return res; 2270 2271 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(state), &cs); 2272 if (res != TEE_SUCCESS) 2273 return res; 2274 2275 switch (TEE_ALG_GET_CLASS(cs->algo)) { 2276 case TEE_OPERATION_DIGEST: 2277 res = tee_hash_get_digest_size(cs->algo, &hash_size); 2278 if (res != TEE_SUCCESS) 2279 return res; 2280 if (*hash_len < hash_size) { 2281 res = TEE_ERROR_SHORT_BUFFER; 2282 goto out; 2283 } 2284 2285 if (chunk_size) { 2286 res = crypto_hash_update(cs->ctx, cs->algo, chunk, 2287 chunk_size); 2288 if (res != TEE_SUCCESS) 2289 return res; 2290 } 2291 2292 res = crypto_hash_final(cs->ctx, cs->algo, hash, hash_size); 2293 if (res != TEE_SUCCESS) 2294 return res; 2295 break; 2296 2297 case TEE_OPERATION_MAC: 2298 res = tee_mac_get_digest_size(cs->algo, &hash_size); 2299 if (res != TEE_SUCCESS) 2300 return res; 2301 if (*hash_len < hash_size) { 2302 res = TEE_ERROR_SHORT_BUFFER; 2303 goto out; 2304 } 2305 2306 if (chunk_size) { 2307 res = crypto_mac_update(cs->ctx, cs->algo, chunk, 2308 chunk_size); 2309 if (res != TEE_SUCCESS) 2310 return res; 2311 } 2312 2313 res = crypto_mac_final(cs->ctx, cs->algo, hash, hash_size); 2314 if (res != TEE_SUCCESS) 2315 return res; 2316 break; 2317 2318 default: 2319 return TEE_ERROR_BAD_PARAMETERS; 2320 } 2321 out: 2322 hlen = hash_size; 2323 res2 = tee_svc_copy_to_user(hash_len, &hlen, sizeof(*hash_len)); 2324 if (res2 != TEE_SUCCESS) 2325 return res2; 2326 return res; 2327 } 2328 2329 TEE_Result syscall_cipher_init(unsigned long state, const void *iv, 2330 size_t iv_len) 2331 { 2332 TEE_Result res; 2333 struct tee_cryp_state *cs; 2334 struct tee_ta_session *sess; 2335 struct tee_obj *o; 2336 struct tee_cryp_obj_secret *key1; 2337 struct user_ta_ctx *utc; 2338 2339 res = tee_ta_get_current_session(&sess); 2340 if (res != TEE_SUCCESS) 2341 return res; 2342 utc = to_user_ta_ctx(sess->ctx); 2343 2344 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(state), &cs); 2345 if (res != TEE_SUCCESS) 2346 return res; 2347 2348 res = tee_mmu_check_access_rights(utc, 2349 TEE_MEMORY_ACCESS_READ | 2350 TEE_MEMORY_ACCESS_ANY_OWNER, 2351 (uaddr_t) iv, iv_len); 2352 if (res != TEE_SUCCESS) 2353 return res; 2354 2355 res = tee_obj_get(utc, cs->key1, &o); 2356 if (res != TEE_SUCCESS) 2357 return res; 2358 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 2359 return TEE_ERROR_BAD_PARAMETERS; 2360 2361 key1 = o->attr; 2362 2363 if (tee_obj_get(utc, cs->key2, &o) == TEE_SUCCESS) { 2364 struct tee_cryp_obj_secret *key2 = o->attr; 2365 2366 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 2367 return TEE_ERROR_BAD_PARAMETERS; 2368 2369 res = crypto_cipher_init(cs->ctx, cs->algo, cs->mode, 2370 (uint8_t *)(key1 + 1), key1->key_size, 2371 (uint8_t *)(key2 + 1), key2->key_size, 2372 iv, iv_len); 2373 } else { 2374 res = crypto_cipher_init(cs->ctx, cs->algo, cs->mode, 2375 (uint8_t *)(key1 + 1), key1->key_size, 2376 NULL, 0, iv, iv_len); 2377 } 2378 if (res != TEE_SUCCESS) 2379 return res; 2380 2381 cs->ctx_finalize = crypto_cipher_final; 2382 return TEE_SUCCESS; 2383 } 2384 2385 static TEE_Result tee_svc_cipher_update_helper(unsigned long state, 2386 bool last_block, const void *src, size_t src_len, 2387 void *dst, uint64_t *dst_len) 2388 { 2389 TEE_Result res; 2390 struct tee_cryp_state *cs; 2391 struct tee_ta_session *sess; 2392 uint64_t dlen; 2393 2394 res = tee_ta_get_current_session(&sess); 2395 if (res != TEE_SUCCESS) 2396 return res; 2397 2398 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(state), &cs); 2399 if (res != TEE_SUCCESS) 2400 return res; 2401 2402 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 2403 TEE_MEMORY_ACCESS_READ | 2404 TEE_MEMORY_ACCESS_ANY_OWNER, 2405 (uaddr_t)src, src_len); 2406 if (res != TEE_SUCCESS) 2407 return res; 2408 2409 if (!dst_len) { 2410 dlen = 0; 2411 } else { 2412 res = tee_svc_copy_from_user(&dlen, dst_len, sizeof(dlen)); 2413 if (res != TEE_SUCCESS) 2414 return res; 2415 2416 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 2417 TEE_MEMORY_ACCESS_READ | 2418 TEE_MEMORY_ACCESS_WRITE | 2419 TEE_MEMORY_ACCESS_ANY_OWNER, 2420 (uaddr_t)dst, dlen); 2421 if (res != TEE_SUCCESS) 2422 return res; 2423 } 2424 2425 if (dlen < src_len) { 2426 res = TEE_ERROR_SHORT_BUFFER; 2427 goto out; 2428 } 2429 2430 if (src_len > 0) { 2431 /* Permit src_len == 0 to finalize the operation */ 2432 res = tee_do_cipher_update(cs->ctx, cs->algo, cs->mode, 2433 last_block, src, src_len, dst); 2434 } 2435 2436 if (last_block && cs->ctx_finalize != NULL) { 2437 cs->ctx_finalize(cs->ctx, cs->algo); 2438 cs->ctx_finalize = NULL; 2439 } 2440 2441 out: 2442 if ((res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) && 2443 dst_len != NULL) { 2444 TEE_Result res2; 2445 2446 dlen = src_len; 2447 res2 = tee_svc_copy_to_user(dst_len, &dlen, sizeof(*dst_len)); 2448 if (res2 != TEE_SUCCESS) 2449 res = res2; 2450 } 2451 2452 return res; 2453 } 2454 2455 TEE_Result syscall_cipher_update(unsigned long state, const void *src, 2456 size_t src_len, void *dst, uint64_t *dst_len) 2457 { 2458 return tee_svc_cipher_update_helper(state, false /* last_block */, 2459 src, src_len, dst, dst_len); 2460 } 2461 2462 TEE_Result syscall_cipher_final(unsigned long state, const void *src, 2463 size_t src_len, void *dst, uint64_t *dst_len) 2464 { 2465 return tee_svc_cipher_update_helper(state, true /* last_block */, 2466 src, src_len, dst, dst_len); 2467 } 2468 2469 #if defined(CFG_CRYPTO_HKDF) 2470 static TEE_Result get_hkdf_params(const TEE_Attribute *params, 2471 uint32_t param_count, 2472 void **salt, size_t *salt_len, void **info, 2473 size_t *info_len, size_t *okm_len) 2474 { 2475 size_t n; 2476 enum { SALT = 0x1, LENGTH = 0x2, INFO = 0x4 }; 2477 uint8_t found = 0; 2478 2479 *salt = *info = NULL; 2480 *salt_len = *info_len = *okm_len = 0; 2481 2482 for (n = 0; n < param_count; n++) { 2483 switch (params[n].attributeID) { 2484 case TEE_ATTR_HKDF_SALT: 2485 if (!(found & SALT)) { 2486 *salt = params[n].content.ref.buffer; 2487 *salt_len = params[n].content.ref.length; 2488 found |= SALT; 2489 } 2490 break; 2491 case TEE_ATTR_HKDF_OKM_LENGTH: 2492 if (!(found & LENGTH)) { 2493 *okm_len = params[n].content.value.a; 2494 found |= LENGTH; 2495 } 2496 break; 2497 case TEE_ATTR_HKDF_INFO: 2498 if (!(found & INFO)) { 2499 *info = params[n].content.ref.buffer; 2500 *info_len = params[n].content.ref.length; 2501 found |= INFO; 2502 } 2503 break; 2504 default: 2505 /* Unexpected attribute */ 2506 return TEE_ERROR_BAD_PARAMETERS; 2507 } 2508 2509 } 2510 2511 if (!(found & LENGTH)) 2512 return TEE_ERROR_BAD_PARAMETERS; 2513 2514 return TEE_SUCCESS; 2515 } 2516 #endif 2517 2518 #if defined(CFG_CRYPTO_CONCAT_KDF) 2519 static TEE_Result get_concat_kdf_params(const TEE_Attribute *params, 2520 uint32_t param_count, 2521 void **other_info, 2522 size_t *other_info_len, 2523 size_t *derived_key_len) 2524 { 2525 size_t n; 2526 enum { LENGTH = 0x1, INFO = 0x2 }; 2527 uint8_t found = 0; 2528 2529 *other_info = NULL; 2530 *other_info_len = *derived_key_len = 0; 2531 2532 for (n = 0; n < param_count; n++) { 2533 switch (params[n].attributeID) { 2534 case TEE_ATTR_CONCAT_KDF_OTHER_INFO: 2535 if (!(found & INFO)) { 2536 *other_info = params[n].content.ref.buffer; 2537 *other_info_len = params[n].content.ref.length; 2538 found |= INFO; 2539 } 2540 break; 2541 case TEE_ATTR_CONCAT_KDF_DKM_LENGTH: 2542 if (!(found & LENGTH)) { 2543 *derived_key_len = params[n].content.value.a; 2544 found |= LENGTH; 2545 } 2546 break; 2547 default: 2548 /* Unexpected attribute */ 2549 return TEE_ERROR_BAD_PARAMETERS; 2550 } 2551 } 2552 2553 if (!(found & LENGTH)) 2554 return TEE_ERROR_BAD_PARAMETERS; 2555 2556 return TEE_SUCCESS; 2557 } 2558 #endif 2559 2560 #if defined(CFG_CRYPTO_PBKDF2) 2561 static TEE_Result get_pbkdf2_params(const TEE_Attribute *params, 2562 uint32_t param_count, void **salt, 2563 size_t *salt_len, size_t *derived_key_len, 2564 size_t *iteration_count) 2565 { 2566 size_t n; 2567 enum { SALT = 0x1, LENGTH = 0x2, COUNT = 0x4 }; 2568 uint8_t found = 0; 2569 2570 *salt = NULL; 2571 *salt_len = *derived_key_len = *iteration_count = 0; 2572 2573 for (n = 0; n < param_count; n++) { 2574 switch (params[n].attributeID) { 2575 case TEE_ATTR_PBKDF2_SALT: 2576 if (!(found & SALT)) { 2577 *salt = params[n].content.ref.buffer; 2578 *salt_len = params[n].content.ref.length; 2579 found |= SALT; 2580 } 2581 break; 2582 case TEE_ATTR_PBKDF2_DKM_LENGTH: 2583 if (!(found & LENGTH)) { 2584 *derived_key_len = params[n].content.value.a; 2585 found |= LENGTH; 2586 } 2587 break; 2588 case TEE_ATTR_PBKDF2_ITERATION_COUNT: 2589 if (!(found & COUNT)) { 2590 *iteration_count = params[n].content.value.a; 2591 found |= COUNT; 2592 } 2593 break; 2594 default: 2595 /* Unexpected attribute */ 2596 return TEE_ERROR_BAD_PARAMETERS; 2597 } 2598 } 2599 2600 if ((found & (LENGTH|COUNT)) != (LENGTH|COUNT)) 2601 return TEE_ERROR_BAD_PARAMETERS; 2602 2603 return TEE_SUCCESS; 2604 } 2605 #endif 2606 2607 TEE_Result syscall_cryp_derive_key(unsigned long state, 2608 const struct utee_attribute *usr_params, 2609 unsigned long param_count, unsigned long derived_key) 2610 { 2611 TEE_Result res = TEE_ERROR_NOT_SUPPORTED; 2612 struct tee_ta_session *sess; 2613 struct tee_obj *ko; 2614 struct tee_obj *so; 2615 struct tee_cryp_state *cs; 2616 struct tee_cryp_obj_secret *sk; 2617 const struct tee_cryp_obj_type_props *type_props; 2618 TEE_Attribute *params = NULL; 2619 struct user_ta_ctx *utc; 2620 2621 res = tee_ta_get_current_session(&sess); 2622 if (res != TEE_SUCCESS) 2623 return res; 2624 utc = to_user_ta_ctx(sess->ctx); 2625 2626 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(state), &cs); 2627 if (res != TEE_SUCCESS) 2628 return res; 2629 2630 params = malloc(sizeof(TEE_Attribute) * param_count); 2631 if (!params) 2632 return TEE_ERROR_OUT_OF_MEMORY; 2633 res = copy_in_attrs(utc, usr_params, param_count, params); 2634 if (res != TEE_SUCCESS) 2635 goto out; 2636 2637 /* Get key set in operation */ 2638 res = tee_obj_get(utc, cs->key1, &ko); 2639 if (res != TEE_SUCCESS) 2640 goto out; 2641 2642 res = tee_obj_get(utc, tee_svc_uref_to_vaddr(derived_key), &so); 2643 if (res != TEE_SUCCESS) 2644 goto out; 2645 2646 /* Find information needed about the object to initialize */ 2647 sk = so->attr; 2648 2649 /* Find description of object */ 2650 type_props = tee_svc_find_type_props(so->info.objectType); 2651 if (!type_props) { 2652 res = TEE_ERROR_NOT_SUPPORTED; 2653 goto out; 2654 } 2655 2656 if (cs->algo == TEE_ALG_DH_DERIVE_SHARED_SECRET) { 2657 size_t alloc_size; 2658 struct bignum *pub; 2659 struct bignum *ss; 2660 2661 if (param_count != 1 || 2662 params[0].attributeID != TEE_ATTR_DH_PUBLIC_VALUE) { 2663 res = TEE_ERROR_BAD_PARAMETERS; 2664 goto out; 2665 } 2666 2667 alloc_size = params[0].content.ref.length * 8; 2668 pub = crypto_bignum_allocate(alloc_size); 2669 ss = crypto_bignum_allocate(alloc_size); 2670 if (pub && ss) { 2671 crypto_bignum_bin2bn(params[0].content.ref.buffer, 2672 params[0].content.ref.length, pub); 2673 res = crypto_acipher_dh_shared_secret(ko->attr, 2674 pub, ss); 2675 if (res == TEE_SUCCESS) { 2676 sk->key_size = crypto_bignum_num_bytes(ss); 2677 crypto_bignum_bn2bin(ss, (uint8_t *)(sk + 1)); 2678 so->info.handleFlags |= 2679 TEE_HANDLE_FLAG_INITIALIZED; 2680 set_attribute(so, type_props, 2681 TEE_ATTR_SECRET_VALUE); 2682 } 2683 } else { 2684 res = TEE_ERROR_OUT_OF_MEMORY; 2685 } 2686 crypto_bignum_free(pub); 2687 crypto_bignum_free(ss); 2688 } else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_ECDH) { 2689 size_t alloc_size; 2690 struct ecc_public_key key_public; 2691 uint8_t *pt_secret; 2692 unsigned long pt_secret_len; 2693 2694 if (param_count != 2 || 2695 params[0].attributeID != TEE_ATTR_ECC_PUBLIC_VALUE_X || 2696 params[1].attributeID != TEE_ATTR_ECC_PUBLIC_VALUE_Y) { 2697 res = TEE_ERROR_BAD_PARAMETERS; 2698 goto out; 2699 } 2700 2701 switch (cs->algo) { 2702 case TEE_ALG_ECDH_P192: 2703 alloc_size = 192; 2704 break; 2705 case TEE_ALG_ECDH_P224: 2706 alloc_size = 224; 2707 break; 2708 case TEE_ALG_ECDH_P256: 2709 alloc_size = 256; 2710 break; 2711 case TEE_ALG_ECDH_P384: 2712 alloc_size = 384; 2713 break; 2714 case TEE_ALG_ECDH_P521: 2715 alloc_size = 521; 2716 break; 2717 default: 2718 res = TEE_ERROR_NOT_IMPLEMENTED; 2719 goto out; 2720 } 2721 2722 /* Create the public key */ 2723 res = crypto_acipher_alloc_ecc_public_key(&key_public, 2724 alloc_size); 2725 if (res != TEE_SUCCESS) 2726 goto out; 2727 key_public.curve = ((struct ecc_keypair *)ko->attr)->curve; 2728 crypto_bignum_bin2bn(params[0].content.ref.buffer, 2729 params[0].content.ref.length, 2730 key_public.x); 2731 crypto_bignum_bin2bn(params[1].content.ref.buffer, 2732 params[1].content.ref.length, 2733 key_public.y); 2734 2735 pt_secret = (uint8_t *)(sk + 1); 2736 pt_secret_len = sk->alloc_size; 2737 res = crypto_acipher_ecc_shared_secret(ko->attr, &key_public, 2738 pt_secret, 2739 &pt_secret_len); 2740 2741 if (res == TEE_SUCCESS) { 2742 sk->key_size = pt_secret_len; 2743 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 2744 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 2745 } 2746 2747 /* free the public key */ 2748 crypto_acipher_free_ecc_public_key(&key_public); 2749 } 2750 #if defined(CFG_CRYPTO_HKDF) 2751 else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_HKDF) { 2752 void *salt, *info; 2753 size_t salt_len, info_len, okm_len; 2754 uint32_t hash_id = TEE_ALG_GET_DIGEST_HASH(cs->algo); 2755 struct tee_cryp_obj_secret *ik = ko->attr; 2756 const uint8_t *ikm = (const uint8_t *)(ik + 1); 2757 2758 res = get_hkdf_params(params, param_count, &salt, &salt_len, 2759 &info, &info_len, &okm_len); 2760 if (res != TEE_SUCCESS) 2761 goto out; 2762 2763 /* Requested size must fit into the output object's buffer */ 2764 if (okm_len > ik->alloc_size) { 2765 res = TEE_ERROR_BAD_PARAMETERS; 2766 goto out; 2767 } 2768 2769 res = tee_cryp_hkdf(hash_id, ikm, ik->key_size, salt, salt_len, 2770 info, info_len, (uint8_t *)(sk + 1), 2771 okm_len); 2772 if (res == TEE_SUCCESS) { 2773 sk->key_size = okm_len; 2774 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 2775 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 2776 } 2777 } 2778 #endif 2779 #if defined(CFG_CRYPTO_CONCAT_KDF) 2780 else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_CONCAT_KDF) { 2781 void *info; 2782 size_t info_len, derived_key_len; 2783 uint32_t hash_id = TEE_ALG_GET_DIGEST_HASH(cs->algo); 2784 struct tee_cryp_obj_secret *ss = ko->attr; 2785 const uint8_t *shared_secret = (const uint8_t *)(ss + 1); 2786 2787 res = get_concat_kdf_params(params, param_count, &info, 2788 &info_len, &derived_key_len); 2789 if (res != TEE_SUCCESS) 2790 goto out; 2791 2792 /* Requested size must fit into the output object's buffer */ 2793 if (derived_key_len > ss->alloc_size) { 2794 res = TEE_ERROR_BAD_PARAMETERS; 2795 goto out; 2796 } 2797 2798 res = tee_cryp_concat_kdf(hash_id, shared_secret, ss->key_size, 2799 info, info_len, (uint8_t *)(sk + 1), 2800 derived_key_len); 2801 if (res == TEE_SUCCESS) { 2802 sk->key_size = derived_key_len; 2803 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 2804 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 2805 } 2806 } 2807 #endif 2808 #if defined(CFG_CRYPTO_PBKDF2) 2809 else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_PBKDF2) { 2810 void *salt; 2811 size_t salt_len, iteration_count, derived_key_len; 2812 uint32_t hash_id = TEE_ALG_GET_DIGEST_HASH(cs->algo); 2813 struct tee_cryp_obj_secret *ss = ko->attr; 2814 const uint8_t *password = (const uint8_t *)(ss + 1); 2815 2816 res = get_pbkdf2_params(params, param_count, &salt, &salt_len, 2817 &derived_key_len, &iteration_count); 2818 if (res != TEE_SUCCESS) 2819 goto out; 2820 2821 /* Requested size must fit into the output object's buffer */ 2822 if (derived_key_len > ss->alloc_size) { 2823 res = TEE_ERROR_BAD_PARAMETERS; 2824 goto out; 2825 } 2826 2827 res = tee_cryp_pbkdf2(hash_id, password, ss->key_size, salt, 2828 salt_len, iteration_count, 2829 (uint8_t *)(sk + 1), derived_key_len); 2830 if (res == TEE_SUCCESS) { 2831 sk->key_size = derived_key_len; 2832 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 2833 set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE); 2834 } 2835 } 2836 #endif 2837 else 2838 res = TEE_ERROR_NOT_SUPPORTED; 2839 2840 out: 2841 free(params); 2842 return res; 2843 } 2844 2845 TEE_Result syscall_cryp_random_number_generate(void *buf, size_t blen) 2846 { 2847 TEE_Result res; 2848 struct tee_ta_session *sess; 2849 2850 res = tee_ta_get_current_session(&sess); 2851 if (res != TEE_SUCCESS) 2852 return res; 2853 2854 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 2855 TEE_MEMORY_ACCESS_WRITE | 2856 TEE_MEMORY_ACCESS_ANY_OWNER, 2857 (uaddr_t)buf, blen); 2858 if (res != TEE_SUCCESS) 2859 return res; 2860 2861 res = crypto_rng_read(buf, blen); 2862 if (res != TEE_SUCCESS) 2863 return res; 2864 2865 return res; 2866 } 2867 2868 TEE_Result syscall_authenc_init(unsigned long state, const void *nonce, 2869 size_t nonce_len, size_t tag_len, 2870 size_t aad_len, size_t payload_len) 2871 { 2872 TEE_Result res; 2873 struct tee_cryp_state *cs; 2874 struct tee_ta_session *sess; 2875 struct tee_obj *o; 2876 struct tee_cryp_obj_secret *key; 2877 2878 res = tee_ta_get_current_session(&sess); 2879 if (res != TEE_SUCCESS) 2880 return res; 2881 2882 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(state), &cs); 2883 if (res != TEE_SUCCESS) 2884 return res; 2885 2886 res = tee_obj_get(to_user_ta_ctx(sess->ctx), cs->key1, &o); 2887 if (res != TEE_SUCCESS) 2888 return res; 2889 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 2890 return TEE_ERROR_BAD_PARAMETERS; 2891 2892 key = o->attr; 2893 res = crypto_authenc_init(cs->ctx, cs->algo, cs->mode, 2894 (uint8_t *)(key + 1), key->key_size, 2895 nonce, nonce_len, tag_len, aad_len, 2896 payload_len); 2897 if (res != TEE_SUCCESS) 2898 return res; 2899 2900 cs->ctx_finalize = (tee_cryp_ctx_finalize_func_t)crypto_authenc_final; 2901 return TEE_SUCCESS; 2902 } 2903 2904 TEE_Result syscall_authenc_update_aad(unsigned long state, 2905 const void *aad_data, size_t aad_data_len) 2906 { 2907 TEE_Result res; 2908 struct tee_cryp_state *cs; 2909 struct tee_ta_session *sess; 2910 2911 res = tee_ta_get_current_session(&sess); 2912 if (res != TEE_SUCCESS) 2913 return res; 2914 2915 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 2916 TEE_MEMORY_ACCESS_READ | 2917 TEE_MEMORY_ACCESS_ANY_OWNER, 2918 (uaddr_t) aad_data, 2919 aad_data_len); 2920 if (res != TEE_SUCCESS) 2921 return res; 2922 2923 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(state), &cs); 2924 if (res != TEE_SUCCESS) 2925 return res; 2926 2927 res = crypto_authenc_update_aad(cs->ctx, cs->algo, cs->mode, 2928 aad_data, aad_data_len); 2929 if (res != TEE_SUCCESS) 2930 return res; 2931 2932 return TEE_SUCCESS; 2933 } 2934 2935 TEE_Result syscall_authenc_update_payload(unsigned long state, 2936 const void *src_data, size_t src_len, void *dst_data, 2937 uint64_t *dst_len) 2938 { 2939 TEE_Result res; 2940 struct tee_cryp_state *cs; 2941 struct tee_ta_session *sess; 2942 uint64_t dlen; 2943 size_t tmp_dlen; 2944 2945 res = tee_ta_get_current_session(&sess); 2946 if (res != TEE_SUCCESS) 2947 return res; 2948 2949 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(state), &cs); 2950 if (res != TEE_SUCCESS) 2951 return res; 2952 2953 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 2954 TEE_MEMORY_ACCESS_READ | 2955 TEE_MEMORY_ACCESS_ANY_OWNER, 2956 (uaddr_t) src_data, src_len); 2957 if (res != TEE_SUCCESS) 2958 return res; 2959 2960 res = tee_svc_copy_from_user(&dlen, dst_len, sizeof(dlen)); 2961 if (res != TEE_SUCCESS) 2962 return res; 2963 2964 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 2965 TEE_MEMORY_ACCESS_READ | 2966 TEE_MEMORY_ACCESS_WRITE | 2967 TEE_MEMORY_ACCESS_ANY_OWNER, 2968 (uaddr_t)dst_data, dlen); 2969 if (res != TEE_SUCCESS) 2970 return res; 2971 2972 if (dlen < src_len) { 2973 res = TEE_ERROR_SHORT_BUFFER; 2974 goto out; 2975 } 2976 2977 tmp_dlen = dlen; 2978 res = crypto_authenc_update_payload(cs->ctx, cs->algo, cs->mode, 2979 src_data, src_len, dst_data, 2980 &tmp_dlen); 2981 dlen = tmp_dlen; 2982 2983 out: 2984 if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) { 2985 TEE_Result res2 = tee_svc_copy_to_user(dst_len, &dlen, 2986 sizeof(*dst_len)); 2987 if (res2 != TEE_SUCCESS) 2988 res = res2; 2989 } 2990 2991 return res; 2992 } 2993 2994 TEE_Result syscall_authenc_enc_final(unsigned long state, 2995 const void *src_data, size_t src_len, void *dst_data, 2996 uint64_t *dst_len, void *tag, uint64_t *tag_len) 2997 { 2998 TEE_Result res; 2999 struct tee_cryp_state *cs; 3000 struct tee_ta_session *sess; 3001 uint64_t dlen; 3002 uint64_t tlen; 3003 size_t tmp_dlen; 3004 size_t tmp_tlen; 3005 3006 res = tee_ta_get_current_session(&sess); 3007 if (res != TEE_SUCCESS) 3008 return res; 3009 3010 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(state), &cs); 3011 if (res != TEE_SUCCESS) 3012 return res; 3013 3014 if (cs->mode != TEE_MODE_ENCRYPT) 3015 return TEE_ERROR_BAD_PARAMETERS; 3016 3017 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 3018 TEE_MEMORY_ACCESS_READ | 3019 TEE_MEMORY_ACCESS_ANY_OWNER, 3020 (uaddr_t)src_data, src_len); 3021 if (res != TEE_SUCCESS) 3022 return res; 3023 3024 if (!dst_len) { 3025 dlen = 0; 3026 } else { 3027 res = tee_svc_copy_from_user(&dlen, dst_len, sizeof(dlen)); 3028 if (res != TEE_SUCCESS) 3029 return res; 3030 3031 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 3032 TEE_MEMORY_ACCESS_READ | 3033 TEE_MEMORY_ACCESS_WRITE | 3034 TEE_MEMORY_ACCESS_ANY_OWNER, 3035 (uaddr_t)dst_data, dlen); 3036 if (res != TEE_SUCCESS) 3037 return res; 3038 } 3039 3040 if (dlen < src_len) { 3041 res = TEE_ERROR_SHORT_BUFFER; 3042 goto out; 3043 } 3044 3045 res = tee_svc_copy_from_user(&tlen, tag_len, sizeof(tlen)); 3046 if (res != TEE_SUCCESS) 3047 return res; 3048 3049 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 3050 TEE_MEMORY_ACCESS_READ | 3051 TEE_MEMORY_ACCESS_WRITE | 3052 TEE_MEMORY_ACCESS_ANY_OWNER, 3053 (uaddr_t)tag, tlen); 3054 if (res != TEE_SUCCESS) 3055 return res; 3056 3057 tmp_dlen = dlen; 3058 tmp_tlen = tlen; 3059 res = crypto_authenc_enc_final(cs->ctx, cs->algo, src_data, 3060 src_len, dst_data, &tmp_dlen, tag, 3061 &tmp_tlen); 3062 dlen = tmp_dlen; 3063 tlen = tmp_tlen; 3064 3065 out: 3066 if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) { 3067 TEE_Result res2; 3068 3069 if (dst_len != NULL) { 3070 res2 = tee_svc_copy_to_user(dst_len, &dlen, 3071 sizeof(*dst_len)); 3072 if (res2 != TEE_SUCCESS) 3073 return res2; 3074 } 3075 3076 res2 = tee_svc_copy_to_user(tag_len, &tlen, sizeof(*tag_len)); 3077 if (res2 != TEE_SUCCESS) 3078 return res2; 3079 } 3080 3081 return res; 3082 } 3083 3084 TEE_Result syscall_authenc_dec_final(unsigned long state, 3085 const void *src_data, size_t src_len, void *dst_data, 3086 uint64_t *dst_len, const void *tag, size_t tag_len) 3087 { 3088 TEE_Result res; 3089 struct tee_cryp_state *cs; 3090 struct tee_ta_session *sess; 3091 uint64_t dlen; 3092 size_t tmp_dlen; 3093 3094 res = tee_ta_get_current_session(&sess); 3095 if (res != TEE_SUCCESS) 3096 return res; 3097 3098 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(state), &cs); 3099 if (res != TEE_SUCCESS) 3100 return res; 3101 3102 if (cs->mode != TEE_MODE_DECRYPT) 3103 return TEE_ERROR_BAD_PARAMETERS; 3104 3105 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 3106 TEE_MEMORY_ACCESS_READ | 3107 TEE_MEMORY_ACCESS_ANY_OWNER, 3108 (uaddr_t)src_data, src_len); 3109 if (res != TEE_SUCCESS) 3110 return res; 3111 3112 if (!dst_len) { 3113 dlen = 0; 3114 } else { 3115 res = tee_svc_copy_from_user(&dlen, dst_len, sizeof(dlen)); 3116 if (res != TEE_SUCCESS) 3117 return res; 3118 3119 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 3120 TEE_MEMORY_ACCESS_READ | 3121 TEE_MEMORY_ACCESS_WRITE | 3122 TEE_MEMORY_ACCESS_ANY_OWNER, 3123 (uaddr_t)dst_data, dlen); 3124 if (res != TEE_SUCCESS) 3125 return res; 3126 } 3127 3128 if (dlen < src_len) { 3129 res = TEE_ERROR_SHORT_BUFFER; 3130 goto out; 3131 } 3132 3133 res = tee_mmu_check_access_rights(to_user_ta_ctx(sess->ctx), 3134 TEE_MEMORY_ACCESS_READ | 3135 TEE_MEMORY_ACCESS_ANY_OWNER, 3136 (uaddr_t)tag, tag_len); 3137 if (res != TEE_SUCCESS) 3138 return res; 3139 3140 tmp_dlen = dlen; 3141 res = crypto_authenc_dec_final(cs->ctx, cs->algo, src_data, src_len, 3142 dst_data, &tmp_dlen, tag, tag_len); 3143 dlen = tmp_dlen; 3144 3145 out: 3146 if ((res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) && 3147 dst_len != NULL) { 3148 TEE_Result res2; 3149 3150 res2 = tee_svc_copy_to_user(dst_len, &dlen, sizeof(*dst_len)); 3151 if (res2 != TEE_SUCCESS) 3152 return res2; 3153 } 3154 3155 return res; 3156 } 3157 3158 static int pkcs1_get_salt_len(const TEE_Attribute *params, uint32_t num_params, 3159 size_t default_len) 3160 { 3161 size_t n; 3162 3163 assert(default_len < INT_MAX); 3164 3165 for (n = 0; n < num_params; n++) { 3166 if (params[n].attributeID == TEE_ATTR_RSA_PSS_SALT_LENGTH) { 3167 if (params[n].content.value.a < INT_MAX) 3168 return params[n].content.value.a; 3169 break; 3170 } 3171 } 3172 /* 3173 * If salt length isn't provided use the default value which is 3174 * the length of the digest. 3175 */ 3176 return default_len; 3177 } 3178 3179 TEE_Result syscall_asymm_operate(unsigned long state, 3180 const struct utee_attribute *usr_params, 3181 size_t num_params, const void *src_data, size_t src_len, 3182 void *dst_data, uint64_t *dst_len) 3183 { 3184 TEE_Result res; 3185 struct tee_cryp_state *cs; 3186 struct tee_ta_session *sess; 3187 uint64_t dlen64; 3188 size_t dlen; 3189 struct tee_obj *o; 3190 void *label = NULL; 3191 size_t label_len = 0; 3192 size_t n; 3193 int salt_len; 3194 TEE_Attribute *params = NULL; 3195 struct user_ta_ctx *utc; 3196 3197 res = tee_ta_get_current_session(&sess); 3198 if (res != TEE_SUCCESS) 3199 return res; 3200 utc = to_user_ta_ctx(sess->ctx); 3201 3202 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(state), &cs); 3203 if (res != TEE_SUCCESS) 3204 return res; 3205 3206 res = tee_mmu_check_access_rights( 3207 utc, 3208 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_ANY_OWNER, 3209 (uaddr_t) src_data, src_len); 3210 if (res != TEE_SUCCESS) 3211 return res; 3212 3213 res = tee_svc_copy_from_user(&dlen64, dst_len, sizeof(dlen64)); 3214 if (res != TEE_SUCCESS) 3215 return res; 3216 dlen = dlen64; 3217 3218 res = tee_mmu_check_access_rights( 3219 utc, 3220 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE | 3221 TEE_MEMORY_ACCESS_ANY_OWNER, 3222 (uaddr_t) dst_data, dlen); 3223 if (res != TEE_SUCCESS) 3224 return res; 3225 3226 params = malloc(sizeof(TEE_Attribute) * num_params); 3227 if (!params) 3228 return TEE_ERROR_OUT_OF_MEMORY; 3229 res = copy_in_attrs(utc, usr_params, num_params, params); 3230 if (res != TEE_SUCCESS) 3231 goto out; 3232 3233 res = tee_obj_get(utc, cs->key1, &o); 3234 if (res != TEE_SUCCESS) 3235 goto out; 3236 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 3237 res = TEE_ERROR_GENERIC; 3238 goto out; 3239 } 3240 3241 switch (cs->algo) { 3242 case TEE_ALG_RSA_NOPAD: 3243 if (cs->mode == TEE_MODE_ENCRYPT) { 3244 res = crypto_acipher_rsanopad_encrypt(o->attr, src_data, 3245 src_len, dst_data, 3246 &dlen); 3247 } else if (cs->mode == TEE_MODE_DECRYPT) { 3248 res = crypto_acipher_rsanopad_decrypt(o->attr, src_data, 3249 src_len, dst_data, 3250 &dlen); 3251 } else { 3252 /* 3253 * We will panic because "the mode is not compatible 3254 * with the function" 3255 */ 3256 res = TEE_ERROR_GENERIC; 3257 } 3258 break; 3259 3260 case TEE_ALG_RSAES_PKCS1_V1_5: 3261 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1: 3262 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224: 3263 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256: 3264 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384: 3265 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512: 3266 for (n = 0; n < num_params; n++) { 3267 if (params[n].attributeID == TEE_ATTR_RSA_OAEP_LABEL) { 3268 label = params[n].content.ref.buffer; 3269 label_len = params[n].content.ref.length; 3270 break; 3271 } 3272 } 3273 3274 if (cs->mode == TEE_MODE_ENCRYPT) { 3275 res = crypto_acipher_rsaes_encrypt(cs->algo, o->attr, 3276 label, label_len, 3277 src_data, src_len, 3278 dst_data, &dlen); 3279 } else if (cs->mode == TEE_MODE_DECRYPT) { 3280 res = crypto_acipher_rsaes_decrypt( 3281 cs->algo, o->attr, label, label_len, 3282 src_data, src_len, dst_data, &dlen); 3283 } else { 3284 res = TEE_ERROR_BAD_PARAMETERS; 3285 } 3286 break; 3287 3288 case TEE_ALG_RSASSA_PKCS1_V1_5_MD5: 3289 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1: 3290 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224: 3291 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256: 3292 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384: 3293 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512: 3294 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1: 3295 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224: 3296 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256: 3297 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384: 3298 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512: 3299 if (cs->mode != TEE_MODE_SIGN) { 3300 res = TEE_ERROR_BAD_PARAMETERS; 3301 break; 3302 } 3303 salt_len = pkcs1_get_salt_len(params, num_params, src_len); 3304 res = crypto_acipher_rsassa_sign(cs->algo, o->attr, salt_len, 3305 src_data, src_len, dst_data, 3306 &dlen); 3307 break; 3308 3309 case TEE_ALG_DSA_SHA1: 3310 case TEE_ALG_DSA_SHA224: 3311 case TEE_ALG_DSA_SHA256: 3312 res = crypto_acipher_dsa_sign(cs->algo, o->attr, src_data, 3313 src_len, dst_data, &dlen); 3314 break; 3315 case TEE_ALG_ECDSA_P192: 3316 case TEE_ALG_ECDSA_P224: 3317 case TEE_ALG_ECDSA_P256: 3318 case TEE_ALG_ECDSA_P384: 3319 case TEE_ALG_ECDSA_P521: 3320 res = crypto_acipher_ecc_sign(cs->algo, o->attr, src_data, 3321 src_len, dst_data, &dlen); 3322 break; 3323 3324 default: 3325 res = TEE_ERROR_BAD_PARAMETERS; 3326 break; 3327 } 3328 3329 out: 3330 free(params); 3331 3332 if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) { 3333 TEE_Result res2; 3334 3335 dlen64 = dlen; 3336 res2 = tee_svc_copy_to_user(dst_len, &dlen64, sizeof(*dst_len)); 3337 if (res2 != TEE_SUCCESS) 3338 return res2; 3339 } 3340 3341 return res; 3342 } 3343 3344 TEE_Result syscall_asymm_verify(unsigned long state, 3345 const struct utee_attribute *usr_params, 3346 size_t num_params, const void *data, size_t data_len, 3347 const void *sig, size_t sig_len) 3348 { 3349 TEE_Result res; 3350 struct tee_cryp_state *cs; 3351 struct tee_ta_session *sess; 3352 struct tee_obj *o; 3353 size_t hash_size; 3354 int salt_len; 3355 TEE_Attribute *params = NULL; 3356 uint32_t hash_algo; 3357 struct user_ta_ctx *utc; 3358 3359 res = tee_ta_get_current_session(&sess); 3360 if (res != TEE_SUCCESS) 3361 return res; 3362 utc = to_user_ta_ctx(sess->ctx); 3363 3364 res = tee_svc_cryp_get_state(sess, tee_svc_uref_to_vaddr(state), &cs); 3365 if (res != TEE_SUCCESS) 3366 return res; 3367 3368 if (cs->mode != TEE_MODE_VERIFY) 3369 return TEE_ERROR_BAD_PARAMETERS; 3370 3371 res = tee_mmu_check_access_rights(utc, 3372 TEE_MEMORY_ACCESS_READ | 3373 TEE_MEMORY_ACCESS_ANY_OWNER, 3374 (uaddr_t)data, data_len); 3375 if (res != TEE_SUCCESS) 3376 return res; 3377 3378 res = tee_mmu_check_access_rights(utc, 3379 TEE_MEMORY_ACCESS_READ | 3380 TEE_MEMORY_ACCESS_ANY_OWNER, 3381 (uaddr_t)sig, sig_len); 3382 if (res != TEE_SUCCESS) 3383 return res; 3384 3385 params = malloc(sizeof(TEE_Attribute) * num_params); 3386 if (!params) 3387 return TEE_ERROR_OUT_OF_MEMORY; 3388 res = copy_in_attrs(utc, usr_params, num_params, params); 3389 if (res != TEE_SUCCESS) 3390 goto out; 3391 3392 res = tee_obj_get(utc, cs->key1, &o); 3393 if (res != TEE_SUCCESS) 3394 goto out; 3395 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 3396 res = TEE_ERROR_BAD_PARAMETERS; 3397 goto out; 3398 } 3399 3400 switch (TEE_ALG_GET_MAIN_ALG(cs->algo)) { 3401 case TEE_MAIN_ALGO_RSA: 3402 hash_algo = TEE_DIGEST_HASH_TO_ALGO(cs->algo); 3403 res = tee_hash_get_digest_size(hash_algo, &hash_size); 3404 if (res != TEE_SUCCESS) 3405 break; 3406 if (data_len != hash_size) { 3407 res = TEE_ERROR_BAD_PARAMETERS; 3408 break; 3409 } 3410 salt_len = pkcs1_get_salt_len(params, num_params, hash_size); 3411 res = crypto_acipher_rsassa_verify(cs->algo, o->attr, salt_len, 3412 data, data_len, sig, 3413 sig_len); 3414 break; 3415 3416 case TEE_MAIN_ALGO_DSA: 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 /* 3422 * Depending on the DSA algorithm (NIST), the digital signature 3423 * output size may be truncated to the size of a key pair 3424 * (Q prime size). Q prime size must be less or equal than the 3425 * hash output length of the hash algorithm involved. 3426 */ 3427 if (data_len > hash_size) { 3428 res = TEE_ERROR_BAD_PARAMETERS; 3429 break; 3430 } 3431 res = crypto_acipher_dsa_verify(cs->algo, o->attr, data, 3432 data_len, sig, sig_len); 3433 break; 3434 3435 case TEE_MAIN_ALGO_ECDSA: 3436 res = crypto_acipher_ecc_verify(cs->algo, o->attr, data, 3437 data_len, sig, sig_len); 3438 break; 3439 3440 default: 3441 res = TEE_ERROR_NOT_SUPPORTED; 3442 } 3443 3444 out: 3445 free(params); 3446 return res; 3447 } 3448