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