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