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