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