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