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