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