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