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