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