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 1804 switch (TEE_ALG_GET_MAIN_ALG(algo)) { 1805 case TEE_MAIN_ALGO_MD5: 1806 req_key_type = TEE_TYPE_HMAC_MD5; 1807 break; 1808 case TEE_MAIN_ALGO_SHA1: 1809 req_key_type = TEE_TYPE_HMAC_SHA1; 1810 break; 1811 case TEE_MAIN_ALGO_SHA224: 1812 req_key_type = TEE_TYPE_HMAC_SHA224; 1813 break; 1814 case TEE_MAIN_ALGO_SHA256: 1815 req_key_type = TEE_TYPE_HMAC_SHA256; 1816 break; 1817 case TEE_MAIN_ALGO_SHA384: 1818 req_key_type = TEE_TYPE_HMAC_SHA384; 1819 break; 1820 case TEE_MAIN_ALGO_SHA512: 1821 req_key_type = TEE_TYPE_HMAC_SHA512; 1822 break; 1823 case TEE_MAIN_ALGO_AES: 1824 req_key_type = TEE_TYPE_AES; 1825 break; 1826 case TEE_MAIN_ALGO_DES: 1827 req_key_type = TEE_TYPE_DES; 1828 break; 1829 case TEE_MAIN_ALGO_DES3: 1830 req_key_type = TEE_TYPE_DES3; 1831 break; 1832 case TEE_MAIN_ALGO_RSA: 1833 if (mode == TEE_MODE_ENCRYPT || mode == TEE_MODE_VERIFY) 1834 req_key_type = TEE_TYPE_RSA_PUBLIC_KEY; 1835 else 1836 req_key_type = TEE_TYPE_RSA_KEYPAIR; 1837 break; 1838 case TEE_MAIN_ALGO_DSA: 1839 if (mode == TEE_MODE_ENCRYPT || mode == TEE_MODE_VERIFY) 1840 req_key_type = TEE_TYPE_DSA_PUBLIC_KEY; 1841 else 1842 req_key_type = TEE_TYPE_DSA_KEYPAIR; 1843 break; 1844 case TEE_MAIN_ALGO_DH: 1845 req_key_type = TEE_TYPE_DH_KEYPAIR; 1846 break; 1847 case TEE_MAIN_ALGO_ECDSA: 1848 if (mode == TEE_MODE_VERIFY) 1849 req_key_type = TEE_TYPE_ECDSA_PUBLIC_KEY; 1850 else 1851 req_key_type = TEE_TYPE_ECDSA_KEYPAIR; 1852 break; 1853 case TEE_MAIN_ALGO_ECDH: 1854 req_key_type = TEE_TYPE_ECDH_KEYPAIR; 1855 break; 1856 #if defined(CFG_CRYPTO_HKDF) 1857 case TEE_MAIN_ALGO_HKDF: 1858 req_key_type = TEE_TYPE_HKDF_IKM; 1859 break; 1860 #endif 1861 #if defined(CFG_CRYPTO_CONCAT_KDF) 1862 case TEE_MAIN_ALGO_CONCAT_KDF: 1863 req_key_type = TEE_TYPE_CONCAT_KDF_Z; 1864 break; 1865 #endif 1866 #if defined(CFG_CRYPTO_PBKDF2) 1867 case TEE_MAIN_ALGO_PBKDF2: 1868 req_key_type = TEE_TYPE_PBKDF2_PASSWORD; 1869 break; 1870 #endif 1871 default: 1872 return TEE_ERROR_BAD_PARAMETERS; 1873 } 1874 1875 if (req_key_type != o->info.objectType) 1876 return TEE_ERROR_BAD_PARAMETERS; 1877 return TEE_SUCCESS; 1878 } 1879 1880 TEE_Result tee_svc_cryp_state_alloc(uint32_t algo, uint32_t mode, 1881 uint32_t key1, uint32_t key2, 1882 uint32_t *state) 1883 { 1884 TEE_Result res; 1885 struct tee_cryp_state *cs; 1886 struct tee_ta_session *sess; 1887 struct tee_obj *o1 = NULL; 1888 struct tee_obj *o2 = NULL; 1889 1890 res = tee_ta_get_current_session(&sess); 1891 if (res != TEE_SUCCESS) 1892 return res; 1893 1894 if (key1 != 0) { 1895 res = tee_obj_get(sess->ctx, key1, &o1); 1896 if (res != TEE_SUCCESS) 1897 return res; 1898 if (o1->busy) 1899 return TEE_ERROR_BAD_PARAMETERS; 1900 res = tee_svc_cryp_check_key_type(o1, algo, mode); 1901 if (res != TEE_SUCCESS) 1902 return res; 1903 } 1904 if (key2 != 0) { 1905 res = tee_obj_get(sess->ctx, key2, &o2); 1906 if (res != TEE_SUCCESS) 1907 return res; 1908 if (o2->busy) 1909 return TEE_ERROR_BAD_PARAMETERS; 1910 res = tee_svc_cryp_check_key_type(o2, algo, mode); 1911 if (res != TEE_SUCCESS) 1912 return res; 1913 } 1914 1915 cs = calloc(1, sizeof(struct tee_cryp_state)); 1916 if (!cs) 1917 return TEE_ERROR_OUT_OF_MEMORY; 1918 TAILQ_INSERT_TAIL(&sess->ctx->cryp_states, cs, link); 1919 cs->algo = algo; 1920 cs->mode = mode; 1921 1922 switch (TEE_ALG_GET_CLASS(algo)) { 1923 case TEE_OPERATION_CIPHER: 1924 if ((algo == TEE_ALG_AES_XTS && (key1 == 0 || key2 == 0)) || 1925 (algo != TEE_ALG_AES_XTS && (key1 == 0 || key2 != 0))) { 1926 res = TEE_ERROR_BAD_PARAMETERS; 1927 } else { 1928 if (crypto_ops.cipher.get_ctx_size) 1929 res = crypto_ops.cipher.get_ctx_size(algo, 1930 &cs->ctx_size); 1931 else 1932 res = TEE_ERROR_NOT_IMPLEMENTED; 1933 if (res != TEE_SUCCESS) 1934 break; 1935 cs->ctx = calloc(1, cs->ctx_size); 1936 if (!cs->ctx) 1937 res = TEE_ERROR_OUT_OF_MEMORY; 1938 } 1939 break; 1940 case TEE_OPERATION_AE: 1941 if (key1 == 0 || key2 != 0) { 1942 res = TEE_ERROR_BAD_PARAMETERS; 1943 } else { 1944 if (crypto_ops.authenc.get_ctx_size) 1945 res = crypto_ops.authenc.get_ctx_size(algo, 1946 &cs->ctx_size); 1947 else 1948 res = TEE_ERROR_NOT_IMPLEMENTED; 1949 if (res != TEE_SUCCESS) 1950 break; 1951 cs->ctx = calloc(1, cs->ctx_size); 1952 if (!cs->ctx) 1953 res = TEE_ERROR_OUT_OF_MEMORY; 1954 } 1955 break; 1956 case TEE_OPERATION_MAC: 1957 if (key1 == 0 || key2 != 0) { 1958 res = TEE_ERROR_BAD_PARAMETERS; 1959 } else { 1960 if (crypto_ops.mac.get_ctx_size) 1961 res = crypto_ops.mac.get_ctx_size(algo, 1962 &cs->ctx_size); 1963 else 1964 res = TEE_ERROR_NOT_IMPLEMENTED; 1965 if (res != TEE_SUCCESS) 1966 break; 1967 cs->ctx = calloc(1, cs->ctx_size); 1968 if (!cs->ctx) 1969 res = TEE_ERROR_OUT_OF_MEMORY; 1970 } 1971 break; 1972 case TEE_OPERATION_DIGEST: 1973 if (key1 != 0 || key2 != 0) { 1974 res = TEE_ERROR_BAD_PARAMETERS; 1975 } else { 1976 if (crypto_ops.hash.get_ctx_size) 1977 res = crypto_ops.hash.get_ctx_size(algo, 1978 &cs->ctx_size); 1979 else 1980 res = TEE_ERROR_NOT_IMPLEMENTED; 1981 if (res != TEE_SUCCESS) 1982 break; 1983 cs->ctx = calloc(1, cs->ctx_size); 1984 if (!cs->ctx) 1985 res = TEE_ERROR_OUT_OF_MEMORY; 1986 } 1987 break; 1988 case TEE_OPERATION_ASYMMETRIC_CIPHER: 1989 case TEE_OPERATION_ASYMMETRIC_SIGNATURE: 1990 if (key1 == 0 || key2 != 0) 1991 res = TEE_ERROR_BAD_PARAMETERS; 1992 break; 1993 case TEE_OPERATION_KEY_DERIVATION: 1994 if (key1 == 0 || key2 != 0) 1995 res = TEE_ERROR_BAD_PARAMETERS; 1996 break; 1997 default: 1998 res = TEE_ERROR_NOT_SUPPORTED; 1999 break; 2000 } 2001 if (res != TEE_SUCCESS) 2002 goto out; 2003 2004 res = tee_svc_copy_to_user(sess, state, &cs, sizeof(uint32_t)); 2005 if (res != TEE_SUCCESS) 2006 goto out; 2007 2008 /* Register keys */ 2009 if (o1 != NULL) { 2010 o1->busy = true; 2011 cs->key1 = key1; 2012 } 2013 if (o2 != NULL) { 2014 o2->busy = true; 2015 cs->key2 = key2; 2016 } 2017 2018 out: 2019 if (res != TEE_SUCCESS) 2020 cryp_state_free(sess->ctx, cs); 2021 return res; 2022 } 2023 2024 TEE_Result tee_svc_cryp_state_copy(uint32_t dst, uint32_t src) 2025 { 2026 TEE_Result res; 2027 struct tee_cryp_state *cs_dst; 2028 struct tee_cryp_state *cs_src; 2029 struct tee_ta_session *sess; 2030 2031 res = tee_ta_get_current_session(&sess); 2032 if (res != TEE_SUCCESS) 2033 return res; 2034 2035 res = tee_svc_cryp_get_state(sess, dst, &cs_dst); 2036 if (res != TEE_SUCCESS) 2037 return res; 2038 res = tee_svc_cryp_get_state(sess, src, &cs_src); 2039 if (res != TEE_SUCCESS) 2040 return res; 2041 if (cs_dst->algo != cs_src->algo || cs_dst->mode != cs_src->mode) 2042 return TEE_ERROR_BAD_PARAMETERS; 2043 /* "Can't happen" */ 2044 if (cs_dst->ctx_size != cs_src->ctx_size) 2045 return TEE_ERROR_BAD_STATE; 2046 2047 memcpy(cs_dst->ctx, cs_src->ctx, cs_src->ctx_size); 2048 return TEE_SUCCESS; 2049 } 2050 2051 void tee_svc_cryp_free_states(struct tee_ta_ctx *ctx) 2052 { 2053 struct tee_cryp_state_head *states = &ctx->cryp_states; 2054 2055 while (!TAILQ_EMPTY(states)) 2056 cryp_state_free(ctx, TAILQ_FIRST(states)); 2057 } 2058 2059 TEE_Result tee_svc_cryp_state_free(uint32_t state) 2060 { 2061 TEE_Result res; 2062 struct tee_cryp_state *cs; 2063 struct tee_ta_session *sess; 2064 2065 res = tee_ta_get_current_session(&sess); 2066 if (res != TEE_SUCCESS) 2067 return res; 2068 2069 res = tee_svc_cryp_get_state(sess, state, &cs); 2070 if (res != TEE_SUCCESS) 2071 return res; 2072 cryp_state_free(sess->ctx, cs); 2073 return TEE_SUCCESS; 2074 } 2075 2076 /* iv and iv_len are ignored for some algorithms */ 2077 TEE_Result tee_svc_hash_init(uint32_t state, const void *iv __unused, 2078 size_t iv_len __unused) 2079 { 2080 TEE_Result res; 2081 struct tee_cryp_state *cs; 2082 struct tee_ta_session *sess; 2083 2084 res = tee_ta_get_current_session(&sess); 2085 if (res != TEE_SUCCESS) 2086 return res; 2087 2088 res = tee_svc_cryp_get_state(sess, state, &cs); 2089 if (res != TEE_SUCCESS) 2090 return res; 2091 2092 switch (TEE_ALG_GET_CLASS(cs->algo)) { 2093 case TEE_OPERATION_DIGEST: 2094 if (!crypto_ops.hash.init) 2095 return TEE_ERROR_NOT_IMPLEMENTED; 2096 res = crypto_ops.hash.init(cs->ctx, cs->algo); 2097 if (res != TEE_SUCCESS) 2098 return res; 2099 break; 2100 case TEE_OPERATION_MAC: 2101 { 2102 struct tee_obj *o; 2103 struct tee_cryp_obj_secret *key; 2104 2105 res = tee_obj_get(sess->ctx, cs->key1, &o); 2106 if (res != TEE_SUCCESS) 2107 return res; 2108 if ((o->info.handleFlags & 2109 TEE_HANDLE_FLAG_INITIALIZED) == 0) 2110 return TEE_ERROR_BAD_PARAMETERS; 2111 2112 key = (struct tee_cryp_obj_secret *)o->data; 2113 if (!crypto_ops.mac.init) 2114 return TEE_ERROR_NOT_IMPLEMENTED; 2115 res = crypto_ops.mac.init(cs->ctx, cs->algo, 2116 (void *)(key + 1), 2117 key->key_size); 2118 if (res != TEE_SUCCESS) 2119 return res; 2120 break; 2121 } 2122 default: 2123 return TEE_ERROR_BAD_PARAMETERS; 2124 } 2125 2126 return TEE_SUCCESS; 2127 } 2128 2129 TEE_Result tee_svc_hash_update(uint32_t state, const void *chunk, 2130 size_t chunk_size) 2131 { 2132 TEE_Result res; 2133 struct tee_cryp_state *cs; 2134 struct tee_ta_session *sess; 2135 2136 /* No data, but size provided isn't valid parameters. */ 2137 if (!chunk && chunk_size) 2138 return TEE_ERROR_BAD_PARAMETERS; 2139 2140 /* Zero length hash is valid, but nothing we need to do. */ 2141 if (!chunk_size) 2142 return TEE_SUCCESS; 2143 2144 res = tee_ta_get_current_session(&sess); 2145 if (res != TEE_SUCCESS) 2146 return res; 2147 2148 res = tee_mmu_check_access_rights(sess->ctx, 2149 TEE_MEMORY_ACCESS_READ | 2150 TEE_MEMORY_ACCESS_ANY_OWNER, 2151 (tee_uaddr_t)chunk, chunk_size); 2152 if (res != TEE_SUCCESS) 2153 return res; 2154 2155 res = tee_svc_cryp_get_state(sess, state, &cs); 2156 if (res != TEE_SUCCESS) 2157 return res; 2158 2159 switch (TEE_ALG_GET_CLASS(cs->algo)) { 2160 case TEE_OPERATION_DIGEST: 2161 if (!crypto_ops.hash.update) 2162 return TEE_ERROR_NOT_IMPLEMENTED; 2163 res = crypto_ops.hash.update(cs->ctx, cs->algo, chunk, 2164 chunk_size); 2165 if (res != TEE_SUCCESS) 2166 return res; 2167 break; 2168 case TEE_OPERATION_MAC: 2169 if (!crypto_ops.mac.update) 2170 return TEE_ERROR_NOT_IMPLEMENTED; 2171 res = crypto_ops.mac.update(cs->ctx, cs->algo, chunk, 2172 chunk_size); 2173 if (res != TEE_SUCCESS) 2174 return res; 2175 break; 2176 default: 2177 return TEE_ERROR_BAD_PARAMETERS; 2178 } 2179 2180 return TEE_SUCCESS; 2181 } 2182 2183 TEE_Result tee_svc_hash_final(uint32_t state, const void *chunk, 2184 size_t chunk_size, void *hash, uint32_t *hash_len) 2185 { 2186 TEE_Result res, res2; 2187 size_t hash_size; 2188 uint32_t hlen; 2189 struct tee_cryp_state *cs; 2190 struct tee_ta_session *sess; 2191 2192 /* No data, but size provided isn't valid parameters. */ 2193 if (!chunk && chunk_size) 2194 return TEE_ERROR_BAD_PARAMETERS; 2195 2196 res = tee_ta_get_current_session(&sess); 2197 if (res != TEE_SUCCESS) 2198 return res; 2199 2200 res = tee_mmu_check_access_rights(sess->ctx, 2201 TEE_MEMORY_ACCESS_READ | 2202 TEE_MEMORY_ACCESS_ANY_OWNER, 2203 (tee_uaddr_t)chunk, chunk_size); 2204 if (res != TEE_SUCCESS) 2205 return res; 2206 2207 res = tee_svc_copy_from_user(sess, &hlen, hash_len, sizeof(uint32_t)); 2208 if (res != TEE_SUCCESS) 2209 return res; 2210 2211 res = tee_mmu_check_access_rights(sess->ctx, 2212 TEE_MEMORY_ACCESS_READ | 2213 TEE_MEMORY_ACCESS_WRITE | 2214 TEE_MEMORY_ACCESS_ANY_OWNER, 2215 (tee_uaddr_t)hash, hlen); 2216 if (res != TEE_SUCCESS) 2217 return res; 2218 2219 res = tee_svc_cryp_get_state(sess, state, &cs); 2220 if (res != TEE_SUCCESS) 2221 return res; 2222 2223 switch (TEE_ALG_GET_CLASS(cs->algo)) { 2224 case TEE_OPERATION_DIGEST: 2225 if (!crypto_ops.hash.update || !crypto_ops.hash.final) 2226 return TEE_ERROR_NOT_IMPLEMENTED; 2227 res = tee_hash_get_digest_size(cs->algo, &hash_size); 2228 if (res != TEE_SUCCESS) 2229 return res; 2230 if (*hash_len < hash_size) { 2231 res = TEE_ERROR_SHORT_BUFFER; 2232 goto out; 2233 } 2234 2235 if (chunk_size) { 2236 res = crypto_ops.hash.update(cs->ctx, cs->algo, chunk, 2237 chunk_size); 2238 if (res != TEE_SUCCESS) 2239 return res; 2240 } 2241 2242 res = crypto_ops.hash.final(cs->ctx, cs->algo, hash, 2243 hash_size); 2244 if (res != TEE_SUCCESS) 2245 return res; 2246 break; 2247 2248 case TEE_OPERATION_MAC: 2249 if (!crypto_ops.mac.update || !crypto_ops.mac.final) 2250 return TEE_ERROR_NOT_IMPLEMENTED; 2251 res = tee_mac_get_digest_size(cs->algo, &hash_size); 2252 if (res != TEE_SUCCESS) 2253 return res; 2254 if (*hash_len < hash_size) { 2255 res = TEE_ERROR_SHORT_BUFFER; 2256 goto out; 2257 } 2258 2259 if (chunk_size) { 2260 res = crypto_ops.mac.update(cs->ctx, cs->algo, chunk, 2261 chunk_size); 2262 if (res != TEE_SUCCESS) 2263 return res; 2264 } 2265 2266 res = crypto_ops.mac.final(cs->ctx, cs->algo, hash, hash_size); 2267 if (res != TEE_SUCCESS) 2268 return res; 2269 break; 2270 2271 default: 2272 return TEE_ERROR_BAD_PARAMETERS; 2273 } 2274 out: 2275 hlen = hash_size; 2276 res2 = tee_svc_copy_to_user(sess, hash_len, &hlen, sizeof(uint32_t)); 2277 if (res2 != TEE_SUCCESS) 2278 return res2; 2279 return res; 2280 } 2281 2282 TEE_Result tee_svc_cipher_init(uint32_t state, const void *iv, size_t iv_len) 2283 { 2284 TEE_Result res; 2285 struct tee_cryp_state *cs; 2286 struct tee_ta_session *sess; 2287 struct tee_obj *o; 2288 struct tee_cryp_obj_secret *key1; 2289 2290 res = tee_ta_get_current_session(&sess); 2291 if (res != TEE_SUCCESS) 2292 return res; 2293 2294 res = tee_svc_cryp_get_state(sess, state, &cs); 2295 if (res != TEE_SUCCESS) 2296 return res; 2297 2298 res = tee_mmu_check_access_rights(sess->ctx, 2299 TEE_MEMORY_ACCESS_READ | 2300 TEE_MEMORY_ACCESS_ANY_OWNER, 2301 (tee_uaddr_t) iv, iv_len); 2302 if (res != TEE_SUCCESS) 2303 return res; 2304 2305 res = tee_obj_get(sess->ctx, cs->key1, &o); 2306 if (res != TEE_SUCCESS) 2307 return res; 2308 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 2309 return TEE_ERROR_BAD_PARAMETERS; 2310 2311 key1 = (struct tee_cryp_obj_secret *)o->data; 2312 2313 if (!crypto_ops.cipher.init) 2314 return TEE_ERROR_NOT_IMPLEMENTED; 2315 2316 if (tee_obj_get(sess->ctx, cs->key2, &o) == TEE_SUCCESS) { 2317 struct tee_cryp_obj_secret *key2 = 2318 (struct tee_cryp_obj_secret *)o->data; 2319 2320 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 2321 return TEE_ERROR_BAD_PARAMETERS; 2322 2323 res = crypto_ops.cipher.init(cs->ctx, cs->algo, cs->mode, 2324 (uint8_t *)(key1 + 1), 2325 key1->key_size, 2326 (uint8_t *)(key2 + 1), 2327 key2->key_size, 2328 iv, iv_len); 2329 } else { 2330 res = crypto_ops.cipher.init(cs->ctx, cs->algo, cs->mode, 2331 (uint8_t *)(key1 + 1), 2332 key1->key_size, 2333 NULL, 2334 0, 2335 iv, iv_len); 2336 } 2337 if (res != TEE_SUCCESS) 2338 return res; 2339 2340 cs->ctx_finalize = crypto_ops.cipher.final; 2341 return TEE_SUCCESS; 2342 } 2343 2344 static TEE_Result tee_svc_cipher_update_helper(uint32_t state, bool last_block, 2345 const void *src, size_t src_len, 2346 void *dst, uint32_t *dst_len) 2347 { 2348 TEE_Result res; 2349 struct tee_cryp_state *cs; 2350 struct tee_ta_session *sess; 2351 uint32_t dlen; 2352 2353 res = tee_ta_get_current_session(&sess); 2354 if (res != TEE_SUCCESS) 2355 return res; 2356 2357 res = tee_svc_cryp_get_state(sess, state, &cs); 2358 if (res != TEE_SUCCESS) 2359 return res; 2360 2361 res = tee_mmu_check_access_rights(sess->ctx, 2362 TEE_MEMORY_ACCESS_READ | 2363 TEE_MEMORY_ACCESS_ANY_OWNER, 2364 (tee_uaddr_t)src, src_len); 2365 if (res != TEE_SUCCESS) 2366 return res; 2367 2368 if (!dst_len) { 2369 dlen = 0; 2370 } else { 2371 res = 2372 tee_svc_copy_from_user(sess, &dlen, dst_len, 2373 sizeof(uint32_t)); 2374 if (res != TEE_SUCCESS) 2375 return res; 2376 2377 res = tee_mmu_check_access_rights(sess->ctx, 2378 TEE_MEMORY_ACCESS_READ | 2379 TEE_MEMORY_ACCESS_WRITE | 2380 TEE_MEMORY_ACCESS_ANY_OWNER, 2381 (tee_uaddr_t)dst, dlen); 2382 if (res != TEE_SUCCESS) 2383 return res; 2384 } 2385 2386 if (dlen < src_len) { 2387 res = TEE_ERROR_SHORT_BUFFER; 2388 goto out; 2389 } 2390 2391 if (src_len > 0) { 2392 /* Permit src_len == 0 to finalize the operation */ 2393 res = tee_do_cipher_update(cs->ctx, cs->algo, cs->mode, 2394 last_block, src, src_len, dst); 2395 } 2396 2397 if (last_block && cs->ctx_finalize != NULL) { 2398 cs->ctx_finalize(cs->ctx, cs->mode); 2399 cs->ctx_finalize = NULL; 2400 } 2401 2402 out: 2403 if ((res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) && 2404 dst_len != NULL) { 2405 TEE_Result res2; 2406 2407 dlen = src_len; 2408 res2 = tee_svc_copy_to_user(sess, dst_len, &dlen, 2409 sizeof(uint32_t)); 2410 if (res2 != TEE_SUCCESS) 2411 res = res2; 2412 } 2413 2414 return res; 2415 } 2416 2417 TEE_Result tee_svc_cipher_update(uint32_t state, const void *src, 2418 size_t src_len, void *dst, uint32_t *dst_len) 2419 { 2420 return tee_svc_cipher_update_helper(state, false /* last_block */, 2421 src, src_len, dst, dst_len); 2422 } 2423 2424 TEE_Result tee_svc_cipher_final(uint32_t state, const void *src, 2425 size_t src_len, void *dst, uint32_t *dst_len) 2426 { 2427 return tee_svc_cipher_update_helper(state, true /* last_block */, 2428 src, src_len, dst, dst_len); 2429 } 2430 2431 #if defined(CFG_CRYPTO_HKDF) 2432 static TEE_Result get_hkdf_params(const TEE_Attribute *params, 2433 uint32_t param_count, 2434 void **salt, size_t *salt_len, void **info, 2435 size_t *info_len, size_t *okm_len) 2436 { 2437 size_t n; 2438 enum { SALT = 0x1, LENGTH = 0x2, INFO = 0x4 }; 2439 uint8_t found = 0; 2440 2441 *salt = *info = NULL; 2442 *salt_len = *info_len = *okm_len = 0; 2443 2444 for (n = 0; n < param_count; n++) { 2445 switch (params[n].attributeID) { 2446 case TEE_ATTR_HKDF_SALT: 2447 if (!(found & SALT)) { 2448 *salt = params[n].content.ref.buffer; 2449 *salt_len = params[n].content.ref.length; 2450 found |= SALT; 2451 } 2452 break; 2453 case TEE_ATTR_HKDF_OKM_LENGTH: 2454 if (!(found & LENGTH)) { 2455 *okm_len = params[n].content.value.a; 2456 found |= LENGTH; 2457 } 2458 break; 2459 case TEE_ATTR_HKDF_INFO: 2460 if (!(found & INFO)) { 2461 *info = params[n].content.ref.buffer; 2462 *info_len = params[n].content.ref.length; 2463 found |= INFO; 2464 } 2465 break; 2466 default: 2467 /* Unexpected attribute */ 2468 return TEE_ERROR_BAD_PARAMETERS; 2469 } 2470 2471 } 2472 2473 if (!(found & LENGTH)) 2474 return TEE_ERROR_BAD_PARAMETERS; 2475 2476 return TEE_SUCCESS; 2477 } 2478 #endif 2479 2480 #if defined(CFG_CRYPTO_CONCAT_KDF) 2481 static TEE_Result get_concat_kdf_params(const TEE_Attribute *params, 2482 uint32_t param_count, 2483 void **other_info, 2484 size_t *other_info_len, 2485 size_t *derived_key_len) 2486 { 2487 size_t n; 2488 enum { LENGTH = 0x1, INFO = 0x2 }; 2489 uint8_t found = 0; 2490 2491 *other_info = NULL; 2492 *other_info_len = *derived_key_len = 0; 2493 2494 for (n = 0; n < param_count; n++) { 2495 switch (params[n].attributeID) { 2496 case TEE_ATTR_CONCAT_KDF_OTHER_INFO: 2497 if (!(found & INFO)) { 2498 *other_info = params[n].content.ref.buffer; 2499 *other_info_len = params[n].content.ref.length; 2500 found |= INFO; 2501 } 2502 break; 2503 case TEE_ATTR_CONCAT_KDF_DKM_LENGTH: 2504 if (!(found & LENGTH)) { 2505 *derived_key_len = params[n].content.value.a; 2506 found |= LENGTH; 2507 } 2508 break; 2509 default: 2510 /* Unexpected attribute */ 2511 return TEE_ERROR_BAD_PARAMETERS; 2512 } 2513 } 2514 2515 if (!(found & LENGTH)) 2516 return TEE_ERROR_BAD_PARAMETERS; 2517 2518 return TEE_SUCCESS; 2519 } 2520 #endif 2521 2522 #if defined(CFG_CRYPTO_PBKDF2) 2523 static TEE_Result get_pbkdf2_params(const TEE_Attribute *params, 2524 uint32_t param_count, void **salt, 2525 size_t *salt_len, size_t *derived_key_len, 2526 size_t *iteration_count) 2527 { 2528 size_t n; 2529 enum { SALT = 0x1, LENGTH = 0x2, COUNT = 0x4 }; 2530 uint8_t found = 0; 2531 2532 *salt = NULL; 2533 *salt_len = *derived_key_len = *iteration_count = 0; 2534 2535 for (n = 0; n < param_count; n++) { 2536 switch (params[n].attributeID) { 2537 case TEE_ATTR_PBKDF2_SALT: 2538 if (!(found & SALT)) { 2539 *salt = params[n].content.ref.buffer; 2540 *salt_len = params[n].content.ref.length; 2541 found |= SALT; 2542 } 2543 break; 2544 case TEE_ATTR_PBKDF2_DKM_LENGTH: 2545 if (!(found & LENGTH)) { 2546 *derived_key_len = params[n].content.value.a; 2547 found |= LENGTH; 2548 } 2549 break; 2550 case TEE_ATTR_PBKDF2_ITERATION_COUNT: 2551 if (!(found & COUNT)) { 2552 *iteration_count = params[n].content.value.a; 2553 found |= COUNT; 2554 } 2555 break; 2556 default: 2557 /* Unexpected attribute */ 2558 return TEE_ERROR_BAD_PARAMETERS; 2559 } 2560 } 2561 2562 if ((found & (LENGTH|COUNT)) != (LENGTH|COUNT)) 2563 return TEE_ERROR_BAD_PARAMETERS; 2564 2565 return TEE_SUCCESS; 2566 } 2567 #endif 2568 2569 TEE_Result tee_svc_cryp_derive_key(uint32_t state, 2570 const struct abi_user32_attribute *usr_params, 2571 uint32_t param_count, uint32_t derived_key) 2572 { 2573 TEE_Result res = TEE_ERROR_NOT_SUPPORTED; 2574 struct tee_ta_session *sess; 2575 struct tee_obj *ko; 2576 struct tee_obj *so; 2577 struct tee_cryp_state *cs; 2578 struct tee_cryp_obj_secret *sk; 2579 const struct tee_cryp_obj_type_props *type_props; 2580 TEE_Attribute *params = NULL; 2581 2582 res = tee_ta_get_current_session(&sess); 2583 if (res != TEE_SUCCESS) 2584 return res; 2585 2586 res = tee_svc_cryp_get_state(sess, state, &cs); 2587 if (res != TEE_SUCCESS) 2588 return res; 2589 2590 params = malloc(sizeof(TEE_Attribute) * param_count); 2591 if (!params) 2592 return TEE_ERROR_OUT_OF_MEMORY; 2593 res = copy_in_attrs(sess->ctx, usr_params, param_count, params); 2594 if (res != TEE_SUCCESS) 2595 goto out; 2596 2597 /* Get key set in operation */ 2598 res = tee_obj_get(sess->ctx, cs->key1, &ko); 2599 if (res != TEE_SUCCESS) 2600 goto out; 2601 2602 res = tee_obj_get(sess->ctx, derived_key, &so); 2603 if (res != TEE_SUCCESS) 2604 goto out; 2605 2606 /* Find information needed about the object to initialize */ 2607 sk = (struct tee_cryp_obj_secret *)so->data; 2608 2609 /* Find description of object */ 2610 type_props = tee_svc_find_type_props(so->info.objectType); 2611 if (!type_props) { 2612 res = TEE_ERROR_NOT_SUPPORTED; 2613 goto out; 2614 } 2615 2616 if (cs->algo == TEE_ALG_DH_DERIVE_SHARED_SECRET) { 2617 size_t alloc_size; 2618 struct bignum *pub; 2619 struct bignum *ss; 2620 2621 if (!crypto_ops.bignum.allocate || 2622 !crypto_ops.bignum.free || 2623 !crypto_ops.bignum.bin2bn || 2624 !crypto_ops.bignum.bn2bin || 2625 !crypto_ops.bignum.num_bytes || 2626 !crypto_ops.acipher.dh_shared_secret) { 2627 res = TEE_ERROR_NOT_IMPLEMENTED; 2628 goto out; 2629 } 2630 if (param_count != 1 || 2631 params[0].attributeID != TEE_ATTR_DH_PUBLIC_VALUE) { 2632 res = TEE_ERROR_BAD_PARAMETERS; 2633 goto out; 2634 } 2635 2636 alloc_size = params[0].content.ref.length * 8; 2637 pub = crypto_ops.bignum.allocate(alloc_size); 2638 ss = crypto_ops.bignum.allocate(alloc_size); 2639 if (pub && ss) { 2640 crypto_ops.bignum.bin2bn(params[0].content.ref.buffer, 2641 params[0].content.ref.length, pub); 2642 res = crypto_ops.acipher.dh_shared_secret(ko->data, 2643 pub, ss); 2644 if (res == TEE_SUCCESS) { 2645 sk->key_size = crypto_ops.bignum.num_bytes(ss); 2646 crypto_ops.bignum.bn2bin(ss, 2647 (uint8_t *)(sk + 1)); 2648 so->info.handleFlags |= 2649 TEE_HANDLE_FLAG_INITIALIZED; 2650 SET_ATTRIBUTE(so, type_props, 2651 TEE_ATTR_SECRET_VALUE); 2652 } 2653 } else { 2654 res = TEE_ERROR_OUT_OF_MEMORY; 2655 } 2656 crypto_ops.bignum.free(pub); 2657 crypto_ops.bignum.free(ss); 2658 } 2659 #if defined(CFG_CRYPTO_HKDF) 2660 else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_HKDF) { 2661 void *salt, *info; 2662 size_t salt_len, info_len, okm_len; 2663 uint32_t hash_id = TEE_ALG_GET_DIGEST_HASH(cs->algo); 2664 struct tee_cryp_obj_secret *ik = ko->data; 2665 const uint8_t *ikm = (const uint8_t *)(ik + 1); 2666 2667 res = get_hkdf_params(params, param_count, &salt, &salt_len, 2668 &info, &info_len, &okm_len); 2669 if (res != TEE_SUCCESS) 2670 goto out; 2671 2672 /* Requested size must fit into the output object's buffer */ 2673 if (okm_len > 2674 ko->data_size - sizeof(struct tee_cryp_obj_secret)) { 2675 res = TEE_ERROR_BAD_PARAMETERS; 2676 goto out; 2677 } 2678 2679 res = tee_cryp_hkdf(hash_id, ikm, ik->key_size, salt, salt_len, 2680 info, info_len, (uint8_t *)(sk + 1), 2681 okm_len); 2682 if (res == TEE_SUCCESS) { 2683 sk->key_size = okm_len; 2684 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 2685 SET_ATTRIBUTE(so, type_props, TEE_ATTR_SECRET_VALUE); 2686 } 2687 } 2688 #endif 2689 #if defined(CFG_CRYPTO_CONCAT_KDF) 2690 else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_CONCAT_KDF) { 2691 void *info; 2692 size_t info_len, derived_key_len; 2693 uint32_t hash_id = TEE_ALG_GET_DIGEST_HASH(cs->algo); 2694 struct tee_cryp_obj_secret *ss = ko->data; 2695 const uint8_t *shared_secret = (const uint8_t *)(ss + 1); 2696 2697 res = get_concat_kdf_params(params, param_count, &info, 2698 &info_len, &derived_key_len); 2699 if (res != TEE_SUCCESS) 2700 goto out; 2701 2702 /* Requested size must fit into the output object's buffer */ 2703 if (derived_key_len > 2704 ko->data_size - sizeof(struct tee_cryp_obj_secret)) { 2705 res = TEE_ERROR_BAD_PARAMETERS; 2706 goto out; 2707 } 2708 2709 res = tee_cryp_concat_kdf(hash_id, shared_secret, ss->key_size, 2710 info, info_len, (uint8_t *)(sk + 1), 2711 derived_key_len); 2712 if (res == TEE_SUCCESS) { 2713 sk->key_size = derived_key_len; 2714 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 2715 SET_ATTRIBUTE(so, type_props, TEE_ATTR_SECRET_VALUE); 2716 } 2717 } 2718 #endif 2719 #if defined(CFG_CRYPTO_PBKDF2) 2720 else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_PBKDF2) { 2721 void *salt; 2722 size_t salt_len, iteration_count, derived_key_len; 2723 uint32_t hash_id = TEE_ALG_GET_DIGEST_HASH(cs->algo); 2724 struct tee_cryp_obj_secret *ss = ko->data; 2725 const uint8_t *password = (const uint8_t *)(ss + 1); 2726 2727 res = get_pbkdf2_params(params, param_count, &salt, &salt_len, 2728 &derived_key_len, &iteration_count); 2729 if (res != TEE_SUCCESS) 2730 goto out; 2731 2732 /* Requested size must fit into the output object's buffer */ 2733 if (derived_key_len > 2734 ko->data_size - sizeof(struct tee_cryp_obj_secret)) { 2735 res = TEE_ERROR_BAD_PARAMETERS; 2736 goto out; 2737 } 2738 2739 res = tee_cryp_pbkdf2(hash_id, password, ss->key_size, salt, 2740 salt_len, iteration_count, 2741 (uint8_t *)(sk + 1), derived_key_len); 2742 if (res == TEE_SUCCESS) { 2743 sk->key_size = derived_key_len; 2744 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 2745 SET_ATTRIBUTE(so, type_props, TEE_ATTR_SECRET_VALUE); 2746 } 2747 } 2748 #endif 2749 else 2750 res = TEE_ERROR_NOT_SUPPORTED; 2751 2752 out: 2753 free(params); 2754 return res; 2755 } 2756 2757 TEE_Result tee_svc_cryp_random_number_generate(void *buf, size_t blen) 2758 { 2759 TEE_Result res; 2760 struct tee_ta_session *sess; 2761 2762 res = tee_ta_get_current_session(&sess); 2763 if (res != TEE_SUCCESS) 2764 return res; 2765 2766 res = tee_mmu_check_access_rights(sess->ctx, 2767 TEE_MEMORY_ACCESS_WRITE | 2768 TEE_MEMORY_ACCESS_ANY_OWNER, 2769 (tee_uaddr_t)buf, blen); 2770 if (res != TEE_SUCCESS) 2771 return res; 2772 2773 res = crypto_ops.prng.read(buf, blen); 2774 if (res != TEE_SUCCESS) 2775 return res; 2776 2777 return res; 2778 } 2779 2780 TEE_Result tee_svc_authenc_init(uint32_t state, const void *nonce, 2781 size_t nonce_len, size_t tag_len, 2782 size_t aad_len, size_t payload_len) 2783 { 2784 TEE_Result res; 2785 struct tee_cryp_state *cs; 2786 struct tee_ta_session *sess; 2787 struct tee_obj *o; 2788 struct tee_cryp_obj_secret *key; 2789 2790 res = tee_ta_get_current_session(&sess); 2791 if (res != TEE_SUCCESS) 2792 return res; 2793 2794 res = tee_svc_cryp_get_state(sess, state, &cs); 2795 if (res != TEE_SUCCESS) 2796 return res; 2797 2798 res = tee_obj_get(sess->ctx, cs->key1, &o); 2799 if (res != TEE_SUCCESS) 2800 return res; 2801 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 2802 return TEE_ERROR_BAD_PARAMETERS; 2803 2804 if (!crypto_ops.authenc.init) 2805 return TEE_ERROR_NOT_IMPLEMENTED; 2806 key = (struct tee_cryp_obj_secret *)o->data; 2807 res = crypto_ops.authenc.init(cs->ctx, cs->algo, cs->mode, 2808 (uint8_t *)(key + 1), key->key_size, 2809 nonce, nonce_len, tag_len, aad_len, 2810 payload_len); 2811 if (res != TEE_SUCCESS) 2812 return res; 2813 2814 cs->ctx_finalize = (tee_cryp_ctx_finalize_func_t) 2815 crypto_ops.authenc.final; 2816 return TEE_SUCCESS; 2817 } 2818 2819 TEE_Result tee_svc_authenc_update_aad(uint32_t state, const void *aad_data, 2820 size_t aad_data_len) 2821 { 2822 TEE_Result res; 2823 struct tee_cryp_state *cs; 2824 struct tee_ta_session *sess; 2825 2826 res = tee_ta_get_current_session(&sess); 2827 if (res != TEE_SUCCESS) 2828 return res; 2829 2830 res = tee_mmu_check_access_rights(sess->ctx, 2831 TEE_MEMORY_ACCESS_READ | 2832 TEE_MEMORY_ACCESS_ANY_OWNER, 2833 (tee_uaddr_t) aad_data, 2834 aad_data_len); 2835 if (res != TEE_SUCCESS) 2836 return res; 2837 2838 res = tee_svc_cryp_get_state(sess, state, &cs); 2839 if (res != TEE_SUCCESS) 2840 return res; 2841 2842 if (!crypto_ops.authenc.update_aad) 2843 return TEE_ERROR_NOT_IMPLEMENTED; 2844 res = crypto_ops.authenc.update_aad(cs->ctx, cs->algo, cs->mode, 2845 aad_data, aad_data_len); 2846 if (res != TEE_SUCCESS) 2847 return res; 2848 2849 return TEE_SUCCESS; 2850 } 2851 2852 TEE_Result tee_svc_authenc_update_payload(uint32_t state, const void *src_data, 2853 size_t src_len, void *dst_data, 2854 uint32_t *dst_len) 2855 { 2856 TEE_Result res; 2857 struct tee_cryp_state *cs; 2858 struct tee_ta_session *sess; 2859 uint32_t dlen; 2860 size_t tmp_dlen; 2861 2862 res = tee_ta_get_current_session(&sess); 2863 if (res != TEE_SUCCESS) 2864 return res; 2865 2866 res = tee_svc_cryp_get_state(sess, state, &cs); 2867 if (res != TEE_SUCCESS) 2868 return res; 2869 2870 res = tee_mmu_check_access_rights(sess->ctx, 2871 TEE_MEMORY_ACCESS_READ | 2872 TEE_MEMORY_ACCESS_ANY_OWNER, 2873 (tee_uaddr_t) src_data, src_len); 2874 if (res != TEE_SUCCESS) 2875 return res; 2876 2877 res = tee_svc_copy_from_user(sess, &dlen, dst_len, sizeof(uint32_t)); 2878 if (res != TEE_SUCCESS) 2879 return res; 2880 2881 res = tee_mmu_check_access_rights(sess->ctx, 2882 TEE_MEMORY_ACCESS_READ | 2883 TEE_MEMORY_ACCESS_WRITE | 2884 TEE_MEMORY_ACCESS_ANY_OWNER, 2885 (tee_uaddr_t)dst_data, dlen); 2886 if (res != TEE_SUCCESS) 2887 return res; 2888 2889 if (dlen < src_len) { 2890 res = TEE_ERROR_SHORT_BUFFER; 2891 goto out; 2892 } 2893 2894 if (!crypto_ops.authenc.update_payload) 2895 return TEE_ERROR_NOT_IMPLEMENTED; 2896 tmp_dlen = dlen; 2897 res = crypto_ops.authenc.update_payload(cs->ctx, cs->algo, cs->mode, 2898 src_data, src_len, dst_data, 2899 &tmp_dlen); 2900 dlen = tmp_dlen; 2901 2902 out: 2903 if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) { 2904 TEE_Result res2 = tee_svc_copy_to_user(sess, dst_len, &dlen, 2905 sizeof(uint32_t)); 2906 if (res2 != TEE_SUCCESS) 2907 res = res2; 2908 } 2909 2910 return res; 2911 } 2912 2913 TEE_Result tee_svc_authenc_enc_final(uint32_t state, const void *src_data, 2914 size_t src_len, void *dst_data, 2915 uint32_t *dst_len, void *tag, 2916 uint32_t *tag_len) 2917 { 2918 TEE_Result res; 2919 struct tee_cryp_state *cs; 2920 struct tee_ta_session *sess; 2921 uint32_t dlen; 2922 uint32_t tlen; 2923 size_t tmp_dlen; 2924 size_t tmp_tlen; 2925 2926 res = tee_ta_get_current_session(&sess); 2927 if (res != TEE_SUCCESS) 2928 return res; 2929 2930 res = tee_svc_cryp_get_state(sess, state, &cs); 2931 if (res != TEE_SUCCESS) 2932 return res; 2933 2934 if (cs->mode != TEE_MODE_ENCRYPT) 2935 return TEE_ERROR_BAD_PARAMETERS; 2936 2937 res = tee_mmu_check_access_rights(sess->ctx, 2938 TEE_MEMORY_ACCESS_READ | 2939 TEE_MEMORY_ACCESS_ANY_OWNER, 2940 (tee_uaddr_t)src_data, src_len); 2941 if (res != TEE_SUCCESS) 2942 return res; 2943 2944 if (!dst_len) { 2945 dlen = 0; 2946 } else { 2947 res = tee_svc_copy_from_user(sess, &dlen, dst_len, 2948 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 2961 if (dlen < src_len) { 2962 res = TEE_ERROR_SHORT_BUFFER; 2963 goto out; 2964 } 2965 2966 res = tee_svc_copy_from_user(sess, &tlen, tag_len, sizeof(uint32_t)); 2967 if (res != TEE_SUCCESS) 2968 return res; 2969 2970 res = tee_mmu_check_access_rights(sess->ctx, 2971 TEE_MEMORY_ACCESS_READ | 2972 TEE_MEMORY_ACCESS_WRITE | 2973 TEE_MEMORY_ACCESS_ANY_OWNER, 2974 (tee_uaddr_t)tag, tlen); 2975 if (res != TEE_SUCCESS) 2976 return res; 2977 2978 if (!crypto_ops.authenc.enc_final) 2979 return TEE_ERROR_NOT_IMPLEMENTED; 2980 tmp_dlen = dlen; 2981 tmp_tlen = tlen; 2982 res = crypto_ops.authenc.enc_final(cs->ctx, cs->algo, src_data, 2983 src_len, dst_data, &tmp_dlen, tag, 2984 &tmp_tlen); 2985 dlen = tmp_dlen; 2986 tlen = tmp_tlen; 2987 2988 out: 2989 if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) { 2990 TEE_Result res2; 2991 2992 if (dst_len != NULL) { 2993 res2 = tee_svc_copy_to_user(sess, dst_len, &dlen, 2994 sizeof(uint32_t)); 2995 if (res2 != TEE_SUCCESS) 2996 return res2; 2997 } 2998 2999 res2 = tee_svc_copy_to_user(sess, tag_len, &tlen, 3000 sizeof(uint32_t)); 3001 if (res2 != TEE_SUCCESS) 3002 return res2; 3003 } 3004 3005 return res; 3006 } 3007 3008 TEE_Result tee_svc_authenc_dec_final(uint32_t state, const void *src_data, 3009 size_t src_len, void *dst_data, 3010 uint32_t *dst_len, const void *tag, 3011 size_t tag_len) 3012 { 3013 TEE_Result res; 3014 struct tee_cryp_state *cs; 3015 struct tee_ta_session *sess; 3016 uint32_t dlen; 3017 size_t tmp_dlen; 3018 3019 res = tee_ta_get_current_session(&sess); 3020 if (res != TEE_SUCCESS) 3021 return res; 3022 3023 res = tee_svc_cryp_get_state(sess, state, &cs); 3024 if (res != TEE_SUCCESS) 3025 return res; 3026 3027 if (cs->mode != TEE_MODE_DECRYPT) 3028 return TEE_ERROR_BAD_PARAMETERS; 3029 3030 res = tee_mmu_check_access_rights(sess->ctx, 3031 TEE_MEMORY_ACCESS_READ | 3032 TEE_MEMORY_ACCESS_ANY_OWNER, 3033 (tee_uaddr_t)src_data, src_len); 3034 if (res != TEE_SUCCESS) 3035 return res; 3036 3037 if (!dst_len) { 3038 dlen = 0; 3039 } else { 3040 res = tee_svc_copy_from_user(sess, &dlen, dst_len, 3041 sizeof(uint32_t)); 3042 if (res != TEE_SUCCESS) 3043 return res; 3044 3045 res = tee_mmu_check_access_rights(sess->ctx, 3046 TEE_MEMORY_ACCESS_READ | 3047 TEE_MEMORY_ACCESS_WRITE | 3048 TEE_MEMORY_ACCESS_ANY_OWNER, 3049 (tee_uaddr_t)dst_data, dlen); 3050 if (res != TEE_SUCCESS) 3051 return res; 3052 } 3053 3054 if (dlen < src_len) { 3055 res = TEE_ERROR_SHORT_BUFFER; 3056 goto out; 3057 } 3058 3059 res = tee_mmu_check_access_rights(sess->ctx, 3060 TEE_MEMORY_ACCESS_READ | 3061 TEE_MEMORY_ACCESS_ANY_OWNER, 3062 (tee_uaddr_t)tag, tag_len); 3063 if (res != TEE_SUCCESS) 3064 return res; 3065 3066 if (!crypto_ops.authenc.dec_final) 3067 return TEE_ERROR_NOT_IMPLEMENTED; 3068 tmp_dlen = dlen; 3069 res = crypto_ops.authenc.dec_final(cs->ctx, cs->algo, src_data, 3070 src_len, dst_data, &tmp_dlen, tag, 3071 tag_len); 3072 dlen = tmp_dlen; 3073 3074 out: 3075 if ((res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) && 3076 dst_len != NULL) { 3077 TEE_Result res2; 3078 3079 res2 = tee_svc_copy_to_user(sess, dst_len, &dlen, 3080 sizeof(uint32_t)); 3081 if (res2 != TEE_SUCCESS) 3082 return res2; 3083 } 3084 3085 return res; 3086 } 3087 3088 static void tee_svc_asymm_pkcs1_get_salt_len(const TEE_Attribute *params, 3089 uint32_t num_params, int *salt_len) 3090 { 3091 size_t n; 3092 3093 for (n = 0; n < num_params; n++) { 3094 if (params[n].attributeID == TEE_ATTR_RSA_PSS_SALT_LENGTH) { 3095 *salt_len = params[n].content.value.a; 3096 return; 3097 } 3098 } 3099 *salt_len = -1; 3100 } 3101 3102 TEE_Result tee_svc_asymm_operate(uint32_t state, 3103 const struct abi_user32_attribute *usr_params, 3104 uint32_t num_params, const void *src_data, 3105 size_t src_len, void *dst_data, uint32_t *dst_len) 3106 { 3107 TEE_Result res; 3108 struct tee_cryp_state *cs; 3109 struct tee_ta_session *sess; 3110 uint32_t dlen32; 3111 size_t dlen; 3112 struct tee_obj *o; 3113 void *label = NULL; 3114 size_t label_len = 0; 3115 size_t n; 3116 int salt_len; 3117 TEE_Attribute *params = NULL; 3118 3119 res = tee_ta_get_current_session(&sess); 3120 if (res != TEE_SUCCESS) 3121 return res; 3122 3123 res = tee_svc_cryp_get_state(sess, state, &cs); 3124 if (res != TEE_SUCCESS) 3125 return res; 3126 3127 res = tee_mmu_check_access_rights( 3128 sess->ctx, 3129 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_ANY_OWNER, 3130 (tee_uaddr_t) src_data, src_len); 3131 if (res != TEE_SUCCESS) 3132 return res; 3133 3134 res = tee_svc_copy_from_user(sess, &dlen32, dst_len, sizeof(uint32_t)); 3135 if (res != TEE_SUCCESS) 3136 return res; 3137 dlen = dlen32; 3138 3139 res = tee_mmu_check_access_rights( 3140 sess->ctx, 3141 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE | 3142 TEE_MEMORY_ACCESS_ANY_OWNER, 3143 (tee_uaddr_t) dst_data, dlen); 3144 if (res != TEE_SUCCESS) 3145 return res; 3146 3147 params = malloc(sizeof(TEE_Attribute) * num_params); 3148 if (!params) 3149 return TEE_ERROR_OUT_OF_MEMORY; 3150 res = copy_in_attrs(sess->ctx, usr_params, num_params, params); 3151 if (res != TEE_SUCCESS) 3152 goto out; 3153 3154 res = tee_obj_get(sess->ctx, cs->key1, &o); 3155 if (res != TEE_SUCCESS) 3156 goto out; 3157 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 3158 res = TEE_ERROR_GENERIC; 3159 goto out; 3160 } 3161 3162 switch (cs->algo) { 3163 case TEE_ALG_RSA_NOPAD: 3164 if (cs->mode == TEE_MODE_ENCRYPT) { 3165 if (crypto_ops.acipher.rsanopad_encrypt) 3166 res = crypto_ops.acipher.rsanopad_encrypt( 3167 o->data, src_data, src_len, 3168 dst_data, &dlen); 3169 else 3170 res = TEE_ERROR_NOT_IMPLEMENTED; 3171 } else if (cs->mode == TEE_MODE_DECRYPT) { 3172 if (crypto_ops.acipher.rsanopad_decrypt) 3173 res = crypto_ops.acipher.rsanopad_decrypt( 3174 o->data, src_data, src_len, dst_data, 3175 &dlen); 3176 else 3177 res = TEE_ERROR_NOT_IMPLEMENTED; 3178 } else { 3179 /* 3180 * We will panic because "the mode is not compatible 3181 * with the function" 3182 */ 3183 res = TEE_ERROR_GENERIC; 3184 } 3185 break; 3186 3187 case TEE_ALG_RSAES_PKCS1_V1_5: 3188 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1: 3189 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224: 3190 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256: 3191 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384: 3192 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512: 3193 for (n = 0; n < num_params; n++) { 3194 if (params[n].attributeID == TEE_ATTR_RSA_OAEP_LABEL) { 3195 label = params[n].content.ref.buffer; 3196 label_len = params[n].content.ref.length; 3197 break; 3198 } 3199 } 3200 3201 if (cs->mode == TEE_MODE_ENCRYPT) { 3202 if (crypto_ops.acipher.rsaes_encrypt) 3203 res = crypto_ops.acipher.rsaes_encrypt( 3204 cs->algo, o->data, label, label_len, 3205 src_data, src_len, dst_data, &dlen); 3206 else 3207 res = TEE_ERROR_NOT_IMPLEMENTED; 3208 } else if (cs->mode == TEE_MODE_DECRYPT) { 3209 if (crypto_ops.acipher.rsaes_decrypt) 3210 res = crypto_ops.acipher.rsaes_decrypt( 3211 cs->algo, o->data, 3212 label, label_len, 3213 src_data, src_len, dst_data, &dlen); 3214 else 3215 res = TEE_ERROR_NOT_IMPLEMENTED; 3216 } else { 3217 res = TEE_ERROR_BAD_PARAMETERS; 3218 } 3219 break; 3220 3221 case TEE_ALG_RSASSA_PKCS1_V1_5_MD5: 3222 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1: 3223 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224: 3224 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256: 3225 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384: 3226 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512: 3227 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1: 3228 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224: 3229 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256: 3230 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384: 3231 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512: 3232 if (cs->mode != TEE_MODE_SIGN) { 3233 res = TEE_ERROR_BAD_PARAMETERS; 3234 break; 3235 } 3236 tee_svc_asymm_pkcs1_get_salt_len(params, num_params, &salt_len); 3237 3238 if (!crypto_ops.acipher.rsassa_sign) { 3239 res = TEE_ERROR_NOT_IMPLEMENTED; 3240 break; 3241 } 3242 res = crypto_ops.acipher.rsassa_sign(cs->algo, o->data, 3243 salt_len, src_data, 3244 src_len, dst_data, &dlen); 3245 break; 3246 3247 case TEE_ALG_DSA_SHA1: 3248 if (!crypto_ops.acipher.dsa_sign) { 3249 res = TEE_ERROR_NOT_IMPLEMENTED; 3250 break; 3251 } 3252 res = crypto_ops.acipher.dsa_sign(cs->algo, o->data, src_data, 3253 src_len, dst_data, &dlen); 3254 break; 3255 case TEE_ALG_ECDSA_P192: 3256 case TEE_ALG_ECDSA_P224: 3257 case TEE_ALG_ECDSA_P256: 3258 case TEE_ALG_ECDSA_P384: 3259 case TEE_ALG_ECDSA_P521: 3260 if (!crypto_ops.acipher.ecc_sign) { 3261 res = TEE_ERROR_NOT_IMPLEMENTED; 3262 break; 3263 } 3264 res = crypto_ops.acipher.ecc_sign(cs->algo, o->data, src_data, 3265 src_len, dst_data, &dlen); 3266 break; 3267 3268 default: 3269 res = TEE_ERROR_BAD_PARAMETERS; 3270 break; 3271 } 3272 3273 out: 3274 free(params); 3275 3276 if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) { 3277 TEE_Result res2; 3278 3279 dlen32 = dlen; 3280 res2 = tee_svc_copy_to_user(sess, dst_len, &dlen, 3281 sizeof(uint32_t)); 3282 if (res2 != TEE_SUCCESS) 3283 return res2; 3284 } 3285 3286 return res; 3287 } 3288 3289 TEE_Result tee_svc_asymm_verify(uint32_t state, 3290 const struct abi_user32_attribute *usr_params, 3291 uint32_t num_params, const void *data, 3292 size_t data_len, const void *sig, size_t sig_len) 3293 { 3294 TEE_Result res; 3295 struct tee_cryp_state *cs; 3296 struct tee_ta_session *sess; 3297 struct tee_obj *o; 3298 size_t hash_size; 3299 int salt_len; 3300 TEE_Attribute *params = NULL; 3301 uint32_t hash_algo; 3302 3303 res = tee_ta_get_current_session(&sess); 3304 if (res != TEE_SUCCESS) 3305 return res; 3306 3307 res = tee_svc_cryp_get_state(sess, state, &cs); 3308 if (res != TEE_SUCCESS) 3309 return res; 3310 3311 if (cs->mode != TEE_MODE_VERIFY) 3312 return TEE_ERROR_BAD_PARAMETERS; 3313 3314 res = tee_mmu_check_access_rights(sess->ctx, 3315 TEE_MEMORY_ACCESS_READ | 3316 TEE_MEMORY_ACCESS_ANY_OWNER, 3317 (tee_uaddr_t)data, data_len); 3318 if (res != TEE_SUCCESS) 3319 return res; 3320 3321 res = tee_mmu_check_access_rights(sess->ctx, 3322 TEE_MEMORY_ACCESS_READ | 3323 TEE_MEMORY_ACCESS_ANY_OWNER, 3324 (tee_uaddr_t)sig, sig_len); 3325 if (res != TEE_SUCCESS) 3326 return res; 3327 3328 params = malloc(sizeof(TEE_Attribute) * num_params); 3329 if (!params) 3330 return TEE_ERROR_OUT_OF_MEMORY; 3331 res = copy_in_attrs(sess->ctx, usr_params, num_params, params); 3332 if (res != TEE_SUCCESS) 3333 goto out; 3334 3335 res = tee_obj_get(sess->ctx, cs->key1, &o); 3336 if (res != TEE_SUCCESS) 3337 goto out; 3338 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 3339 res = TEE_ERROR_BAD_PARAMETERS; 3340 goto out; 3341 } 3342 3343 if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_ECDSA) 3344 hash_algo = TEE_ALG_SHA1; 3345 else 3346 hash_algo = TEE_DIGEST_HASH_TO_ALGO(cs->algo); 3347 3348 res = tee_hash_get_digest_size(hash_algo, &hash_size); 3349 if (res != TEE_SUCCESS) 3350 goto out; 3351 3352 if (data_len != hash_size) { 3353 res = TEE_ERROR_BAD_PARAMETERS; 3354 goto out; 3355 } 3356 3357 switch (TEE_ALG_GET_MAIN_ALG(cs->algo)) { 3358 case TEE_MAIN_ALGO_RSA: 3359 tee_svc_asymm_pkcs1_get_salt_len(params, num_params, &salt_len); 3360 if (!crypto_ops.acipher.rsassa_verify) { 3361 res = TEE_ERROR_NOT_IMPLEMENTED; 3362 break; 3363 } 3364 res = crypto_ops.acipher.rsassa_verify(cs->algo, o->data, 3365 salt_len, data, 3366 data_len, sig, sig_len); 3367 break; 3368 3369 case TEE_MAIN_ALGO_DSA: 3370 if (!crypto_ops.acipher.dsa_verify) { 3371 res = TEE_ERROR_NOT_IMPLEMENTED; 3372 break; 3373 } 3374 res = crypto_ops.acipher.dsa_verify(cs->algo, o->data, data, 3375 data_len, sig, sig_len); 3376 break; 3377 3378 case TEE_MAIN_ALGO_ECDSA: 3379 if (!crypto_ops.acipher.ecc_verify) { 3380 res = TEE_ERROR_NOT_IMPLEMENTED; 3381 break; 3382 } 3383 res = crypto_ops.acipher.ecc_verify(cs->algo, o->data, data, 3384 data_len, sig, sig_len); 3385 break; 3386 3387 default: 3388 res = TEE_ERROR_NOT_SUPPORTED; 3389 } 3390 3391 out: 3392 free(params); 3393 return res; 3394 } 3395