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