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_cleanup(struct bignum *bn, bool del) 604 { 605 if (del) 606 crypto_ops.bignum.free(bn); 607 else 608 crypto_ops.bignum.clear(bn); 609 } 610 611 static void cleanup_rsa_keypair(void *p, bool del) 612 { 613 struct rsa_keypair *s = (struct rsa_keypair *)p; 614 615 bn_cleanup(s->e, del); 616 bn_cleanup(s->d, del); 617 bn_cleanup(s->n, del); 618 bn_cleanup(s->p, del); 619 bn_cleanup(s->q, del); 620 bn_cleanup(s->qp, del); 621 bn_cleanup(s->dp, del); 622 bn_cleanup(s->dq, del); 623 } 624 625 static void cleanup_dsa_keypair(void *p, bool del) 626 { 627 struct dsa_keypair *s = (struct dsa_keypair *)p; 628 629 bn_cleanup(s->g, del); 630 bn_cleanup(s->p, del); 631 bn_cleanup(s->q, del); 632 bn_cleanup(s->y, del); 633 bn_cleanup(s->x, del); 634 } 635 636 static void cleanup_rsa_public_key(void *p, bool del) 637 { 638 struct rsa_public_key *s = (struct rsa_public_key *)p; 639 640 bn_cleanup(s->e, del); 641 bn_cleanup(s->n, del); 642 } 643 644 static void cleanup_dsa_public_key(void *p, bool del) 645 { 646 struct dsa_public_key *s = (struct dsa_public_key *)p; 647 648 bn_cleanup(s->g, del); 649 bn_cleanup(s->p, del); 650 bn_cleanup(s->q, del); 651 bn_cleanup(s->y, del); 652 } 653 654 static void cleanup_dh_keypair(void *p, bool del) 655 { 656 struct dh_keypair *s = (struct dh_keypair *)p; 657 658 bn_cleanup(s->g, del); 659 bn_cleanup(s->p, del); 660 bn_cleanup(s->x, del); 661 bn_cleanup(s->y, del); 662 bn_cleanup(s->q, del); 663 s->xbits = 0; 664 } 665 666 static void copy_rsa_public_key(struct rsa_public_key *to, 667 const struct rsa_public_key *from) 668 { 669 crypto_ops.bignum.copy(to->e, from->e); 670 crypto_ops.bignum.copy(to->n, from->n); 671 } 672 673 static void copy_rsa_keypair(struct rsa_keypair *to, 674 const struct rsa_keypair *from) 675 { 676 crypto_ops.bignum.copy(to->e, from->e); 677 crypto_ops.bignum.copy(to->d, from->d); 678 crypto_ops.bignum.copy(to->n, from->n); 679 crypto_ops.bignum.copy(to->p, from->p); 680 crypto_ops.bignum.copy(to->q, from->q); 681 crypto_ops.bignum.copy(to->qp, from->qp); 682 crypto_ops.bignum.copy(to->dp, from->dp); 683 crypto_ops.bignum.copy(to->dq, from->dq); 684 } 685 686 static void copy_dsa_public_key(struct dsa_public_key *to, 687 const struct dsa_public_key *from) 688 { 689 crypto_ops.bignum.copy(to->g, from->g); 690 crypto_ops.bignum.copy(to->p, from->p); 691 crypto_ops.bignum.copy(to->q, from->q); 692 crypto_ops.bignum.copy(to->y, from->y); 693 } 694 695 696 static void copy_dsa_keypair(struct dsa_keypair *to, 697 const struct dsa_keypair *from) 698 { 699 crypto_ops.bignum.copy(to->g, from->g); 700 crypto_ops.bignum.copy(to->p, from->p); 701 crypto_ops.bignum.copy(to->q, from->q); 702 crypto_ops.bignum.copy(to->y, from->y); 703 crypto_ops.bignum.copy(to->x, from->x); 704 } 705 706 static void copy_dh_keypair(struct dh_keypair *to, 707 const struct dh_keypair *from) 708 { 709 crypto_ops.bignum.copy(to->g, from->g); 710 crypto_ops.bignum.copy(to->p, from->p); 711 crypto_ops.bignum.copy(to->y, from->y); 712 crypto_ops.bignum.copy(to->x, from->x); 713 crypto_ops.bignum.copy(to->q, from->q); 714 to->xbits = from->xbits; 715 } 716 717 static void extract_rsa_public_key(struct rsa_public_key *to, 718 const struct rsa_keypair *from) 719 { 720 crypto_ops.bignum.copy(to->e, from->e); 721 crypto_ops.bignum.copy(to->n, from->n); 722 } 723 724 static void extract_dsa_public_key(struct dsa_public_key *to, 725 const struct dsa_keypair *from) 726 { 727 crypto_ops.bignum.copy(to->g, from->g); 728 crypto_ops.bignum.copy(to->p, from->p); 729 crypto_ops.bignum.copy(to->q, from->q); 730 crypto_ops.bignum.copy(to->y, from->y); 731 } 732 733 TEE_Result tee_svc_cryp_obj_alloc(TEE_ObjectType obj_type, 734 uint32_t max_obj_size, uint32_t *obj) 735 { 736 TEE_Result res; 737 struct tee_ta_session *sess; 738 const struct tee_cryp_obj_type_props *type_props; 739 struct tee_obj *o; 740 741 res = tee_ta_get_current_session(&sess); 742 if (res != TEE_SUCCESS) 743 return res; 744 745 /* 746 * Verify that maxObjectSize is supported and find out how 747 * much should be allocated. 748 */ 749 750 /* Find description of object */ 751 type_props = tee_svc_find_type_props(obj_type); 752 if (!type_props) 753 return TEE_ERROR_NOT_SUPPORTED; 754 755 /* Check that maxObjectSize follows restrictions */ 756 if (max_obj_size % type_props->quanta != 0) 757 return TEE_ERROR_NOT_SUPPORTED; 758 if (max_obj_size < type_props->min_size) 759 return TEE_ERROR_NOT_SUPPORTED; 760 if (max_obj_size > type_props->max_size) 761 return TEE_ERROR_NOT_SUPPORTED; 762 763 o = calloc(1, sizeof(*o)); 764 if (!o) 765 return TEE_ERROR_OUT_OF_MEMORY; 766 o->data = calloc(1, type_props->alloc_size); 767 if (!o->data) { 768 free(o); 769 return TEE_ERROR_OUT_OF_MEMORY; 770 } 771 o->data_size = type_props->alloc_size; 772 773 /* If we have a key structure, pre-allocate the bignums inside */ 774 switch (obj_type) { 775 case TEE_TYPE_RSA_PUBLIC_KEY: 776 if (!crypto_ops.acipher.alloc_rsa_public_key) 777 goto notimpl; 778 if (crypto_ops.acipher.alloc_rsa_public_key(o->data, 779 max_obj_size) 780 != TEE_SUCCESS) 781 goto alloc_err; 782 o->cleanup = cleanup_rsa_public_key; 783 break; 784 case TEE_TYPE_RSA_KEYPAIR: 785 if (!crypto_ops.acipher.alloc_rsa_keypair) 786 goto notimpl; 787 if (crypto_ops.acipher.alloc_rsa_keypair(o->data, 788 max_obj_size) 789 != TEE_SUCCESS) 790 goto alloc_err; 791 o->cleanup = cleanup_rsa_keypair; 792 break; 793 case TEE_TYPE_DSA_PUBLIC_KEY: 794 if (!crypto_ops.acipher.alloc_dsa_public_key) 795 goto notimpl; 796 if (crypto_ops.acipher.alloc_dsa_public_key(o->data, 797 max_obj_size) 798 != TEE_SUCCESS) 799 goto alloc_err; 800 o->cleanup = cleanup_dsa_public_key; 801 break; 802 case TEE_TYPE_DSA_KEYPAIR: 803 if (!crypto_ops.acipher.alloc_dsa_keypair) 804 goto notimpl; 805 if (crypto_ops.acipher.alloc_dsa_keypair(o->data, max_obj_size) 806 != TEE_SUCCESS) 807 goto alloc_err; 808 o->cleanup = cleanup_dsa_keypair; 809 break; 810 case TEE_TYPE_DH_KEYPAIR: 811 if (!crypto_ops.acipher.alloc_dh_keypair) 812 goto notimpl; 813 if (crypto_ops.acipher.alloc_dh_keypair(o->data, max_obj_size) 814 != TEE_SUCCESS) 815 goto alloc_err; 816 o->cleanup = cleanup_dh_keypair; 817 break; 818 default: 819 break; 820 } 821 822 o->info.objectType = obj_type; 823 o->info.maxObjectSize = max_obj_size; 824 o->info.objectUsage = TEE_USAGE_DEFAULT; 825 o->info.handleFlags = 0; 826 827 o->fd = -1; 828 829 tee_obj_add(sess->ctx, o); 830 831 res = tee_svc_copy_to_user(sess, obj, &o, sizeof(o)); 832 if (res != TEE_SUCCESS) 833 tee_obj_close(sess->ctx, o); 834 return res; 835 836 alloc_err: 837 free(o->data); 838 free(o); 839 return TEE_ERROR_OUT_OF_MEMORY; 840 notimpl: 841 free(o->data); 842 free(o); 843 return TEE_ERROR_NOT_IMPLEMENTED; 844 } 845 846 TEE_Result tee_svc_cryp_obj_close(uint32_t obj) 847 { 848 TEE_Result res; 849 struct tee_ta_session *sess; 850 struct tee_obj *o; 851 852 res = tee_ta_get_current_session(&sess); 853 if (res != TEE_SUCCESS) 854 return res; 855 856 res = tee_obj_get(sess->ctx, obj, &o); 857 if (res != TEE_SUCCESS) 858 return res; 859 860 /* 861 * If it's busy it's used by an operation, a client should never have 862 * this handle. 863 */ 864 if (o->busy) 865 return TEE_ERROR_ITEM_NOT_FOUND; 866 867 tee_obj_close(sess->ctx, o); 868 return TEE_SUCCESS; 869 } 870 871 TEE_Result tee_svc_cryp_obj_reset(uint32_t obj) 872 { 873 TEE_Result res; 874 struct tee_ta_session *sess; 875 struct tee_obj *o; 876 877 res = tee_ta_get_current_session(&sess); 878 if (res != TEE_SUCCESS) 879 return res; 880 881 res = tee_obj_get(sess->ctx, obj, &o); 882 if (res != TEE_SUCCESS) 883 return res; 884 885 if ((o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) == 0) { 886 if (o->cleanup) { 887 /* 888 * o->data contains pointers to key data. 889 * Clear key data, but keep the pointers. 890 */ 891 o->cleanup(o->data, false); 892 } else { 893 memset(o->data, 0, o->data_size); 894 } 895 o->info.objectSize = 0; 896 o->info.objectUsage = TEE_USAGE_DEFAULT; 897 } else { 898 return TEE_ERROR_BAD_PARAMETERS; 899 } 900 901 return TEE_SUCCESS; 902 } 903 904 static TEE_Result tee_svc_cryp_obj_store_attr_raw(struct tee_ta_session *sess, 905 uint16_t conv_func, 906 const TEE_Attribute *attr, 907 void *data, size_t data_size) 908 { 909 TEE_Result res; 910 struct tee_cryp_obj_secret *obj; 911 struct bignum *bn; 912 913 if (!attr) 914 return TEE_ERROR_BAD_STATE; 915 916 if (conv_func != TEE_TYPE_CONV_FUNC_VALUE && !attr->content.ref.buffer) 917 return TEE_ERROR_BAD_PARAMETERS; 918 919 switch (conv_func) { 920 case TEE_TYPE_CONV_FUNC_NONE: 921 /* No conversion data size has to match exactly */ 922 if (attr->content.ref.length != data_size) 923 return TEE_ERROR_BAD_PARAMETERS; 924 return tee_svc_copy_from_user(sess, data, 925 attr->content.ref.buffer, 926 data_size); 927 case TEE_TYPE_CONV_FUNC_SECRET: 928 if (!TEE_ALIGNMENT_IS_OK(data, struct tee_cryp_obj_secret)) 929 return TEE_ERROR_BAD_STATE; 930 obj = (struct tee_cryp_obj_secret *)(void *)data; 931 932 /* Data size has to fit in allocated buffer */ 933 if (attr->content.ref.length > 934 (data_size - sizeof(struct tee_cryp_obj_secret))) 935 return TEE_ERROR_BAD_PARAMETERS; 936 937 res = tee_svc_copy_from_user(sess, obj + 1, 938 attr->content.ref.buffer, 939 attr->content.ref.length); 940 if (res == TEE_SUCCESS) 941 obj->key_size = attr->content.ref.length; 942 return res; 943 944 case TEE_TYPE_CONV_FUNC_BIGNUM: 945 /* Check data can be accessed */ 946 res = tee_mmu_check_access_rights( 947 sess->ctx, 948 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_ANY_OWNER, 949 (tee_uaddr_t)attr->content.ref.buffer, 950 attr->content.ref.length); 951 if (res != TEE_SUCCESS) 952 return res; 953 954 /* 955 * Read the array of bytes (stored in attr->content.ref.buffer) 956 * and convert it to a bignum (pointed to by data) 957 */ 958 bn = *(struct bignum **)data; 959 if (!crypto_ops.bignum.bin2bn) 960 return TEE_ERROR_NOT_IMPLEMENTED; 961 res = crypto_ops.bignum.bin2bn(attr->content.ref.buffer, 962 attr->content.ref.length, 963 bn); 964 return res; 965 966 case TEE_TYPE_CONV_FUNC_VALUE: 967 /* 968 * a value attribute consists of two uint32 but have not 969 * seen anything that actaully would need that so this fills 970 * the data from the first value and discards the second value 971 */ 972 *(uint32_t *)data = attr->content.value.a; 973 974 return TEE_SUCCESS; 975 976 default: 977 return TEE_ERROR_BAD_STATE; 978 } 979 } 980 981 enum attr_usage { 982 ATTR_USAGE_POPULATE, 983 ATTR_USAGE_GENERATE_KEY 984 }; 985 986 static TEE_Result tee_svc_cryp_check_attr( 987 enum attr_usage usage, 988 const struct tee_cryp_obj_type_props *type_props, 989 TEE_Attribute *attrs, 990 uint32_t attr_count) 991 { 992 uint32_t required_flag; 993 uint32_t opt_flag; 994 bool all_opt_needed; 995 uint32_t req_attrs = 0; 996 uint32_t opt_grp_attrs = 0; 997 uint32_t attrs_found = 0; 998 size_t n; 999 1000 if (usage == ATTR_USAGE_POPULATE) { 1001 required_flag = TEE_TYPE_ATTR_REQUIRED; 1002 opt_flag = TEE_TYPE_ATTR_OPTIONAL_GROUP; 1003 all_opt_needed = true; 1004 } else { 1005 required_flag = TEE_TYPE_ATTR_GEN_KEY_REQ; 1006 opt_flag = TEE_TYPE_ATTR_GEN_KEY_OPT; 1007 all_opt_needed = false; 1008 } 1009 1010 /* 1011 * First find out which attributes are required and which belong to 1012 * the optional group 1013 */ 1014 for (n = 0; n < type_props->num_type_attrs; n++) { 1015 uint32_t bit = 1 << n; 1016 uint32_t flags = type_props->type_attrs[n].flags; 1017 1018 if (flags & required_flag) 1019 req_attrs |= bit; 1020 else if (flags & opt_flag) 1021 opt_grp_attrs |= bit; 1022 } 1023 1024 /* 1025 * Verify that all required attributes are in place and 1026 * that the same attribute isn't repeated. 1027 */ 1028 for (n = 0; n < attr_count; n++) { 1029 int idx = 1030 tee_svc_cryp_obj_find_type_attr_idx(attrs[n].attributeID, 1031 type_props); 1032 if (idx >= 0) { 1033 uint32_t bit = 1 << idx; 1034 1035 if ((attrs_found & bit) != 0) 1036 return TEE_ERROR_ITEM_NOT_FOUND; 1037 1038 attrs_found |= bit; 1039 } 1040 } 1041 /* Required attribute missing */ 1042 if ((attrs_found & req_attrs) != req_attrs) 1043 return TEE_ERROR_ITEM_NOT_FOUND; 1044 1045 /* 1046 * If the flag says that "if one of the optional attributes are included 1047 * all of them has to be included" this must be checked. 1048 */ 1049 if (all_opt_needed && (attrs_found & opt_grp_attrs) != 0 && 1050 (attrs_found & opt_grp_attrs) != opt_grp_attrs) 1051 return TEE_ERROR_ITEM_NOT_FOUND; 1052 1053 return TEE_SUCCESS; 1054 } 1055 1056 static TEE_Result tee_svc_cryp_obj_populate_type( 1057 struct tee_ta_session *sess, 1058 struct tee_obj *o, 1059 const struct tee_cryp_obj_type_props *type_props, 1060 const TEE_Attribute *attrs, 1061 uint32_t attr_count) 1062 { 1063 TEE_Result res; 1064 uint32_t have_attrs = 0; 1065 size_t obj_size = 0; 1066 size_t n; 1067 1068 for (n = 0; n < attr_count; n++) { 1069 size_t raw_size; 1070 void *raw_data; 1071 int idx = 1072 tee_svc_cryp_obj_find_type_attr_idx(attrs[n].attributeID, 1073 type_props); 1074 if (idx < 0) 1075 continue; 1076 1077 have_attrs |= 1 << idx; 1078 1079 res = tee_svc_cryp_obj_get_raw_data(o, type_props, idx, 1080 &raw_data, &raw_size); 1081 if (res != TEE_SUCCESS) 1082 return res; 1083 1084 res = 1085 tee_svc_cryp_obj_store_attr_raw( 1086 sess, type_props->type_attrs[idx].conv_func, 1087 attrs + n, raw_data, raw_size); 1088 if (res != TEE_SUCCESS) 1089 return res; 1090 1091 /* 1092 * First attr_idx signifies the attribute that gives the size 1093 * of the object 1094 */ 1095 if (type_props->type_attrs[idx].flags & 1096 TEE_TYPE_ATTR_SIZE_INDICATOR) { 1097 obj_size += attrs[n].content.ref.length * 8; 1098 } 1099 } 1100 1101 /* 1102 * We have to do it like this because the parity bits aren't counted 1103 * when telling the size of the key in bits. 1104 */ 1105 if (o->info.objectType == TEE_TYPE_DES || 1106 o->info.objectType == TEE_TYPE_DES3) 1107 obj_size -= obj_size / 8; /* Exclude parity in size of key */ 1108 1109 o->have_attrs = have_attrs; 1110 o->info.objectSize = obj_size; 1111 return TEE_SUCCESS; 1112 } 1113 1114 TEE_Result tee_svc_cryp_obj_populate(uint32_t obj, TEE_Attribute *attrs, 1115 uint32_t attr_count) 1116 { 1117 TEE_Result res; 1118 struct tee_ta_session *sess; 1119 struct tee_obj *o; 1120 const struct tee_cryp_obj_type_props *type_props; 1121 1122 res = tee_ta_get_current_session(&sess); 1123 if (res != TEE_SUCCESS) 1124 return res; 1125 1126 res = tee_obj_get(sess->ctx, obj, &o); 1127 if (res != TEE_SUCCESS) 1128 return res; 1129 1130 /* Must be a transient object */ 1131 if ((o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 1132 return TEE_ERROR_BAD_PARAMETERS; 1133 1134 /* Must not be initialized already */ 1135 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1136 return TEE_ERROR_BAD_PARAMETERS; 1137 1138 type_props = tee_svc_find_type_props(o->info.objectType); 1139 if (!type_props) 1140 return TEE_ERROR_NOT_IMPLEMENTED; 1141 1142 res = tee_svc_cryp_check_attr(ATTR_USAGE_POPULATE, type_props, attrs, 1143 attr_count); 1144 if (res != TEE_SUCCESS) 1145 return res; 1146 1147 res = tee_svc_cryp_obj_populate_type(sess, o, type_props, attrs, 1148 attr_count); 1149 if (res == TEE_SUCCESS) 1150 o->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 1151 1152 return res; 1153 } 1154 1155 TEE_Result tee_svc_cryp_obj_copy(uint32_t dst, uint32_t src) 1156 { 1157 TEE_Result res; 1158 struct tee_ta_session *sess; 1159 struct tee_obj *dst_o; 1160 struct tee_obj *src_o; 1161 1162 res = tee_ta_get_current_session(&sess); 1163 if (res != TEE_SUCCESS) 1164 return res; 1165 1166 res = tee_obj_get(sess->ctx, dst, &dst_o); 1167 if (res != TEE_SUCCESS) 1168 return res; 1169 1170 res = tee_obj_get(sess->ctx, src, &src_o); 1171 if (res != TEE_SUCCESS) 1172 return res; 1173 1174 if ((src_o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1175 return TEE_ERROR_BAD_PARAMETERS; 1176 if ((dst_o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 1177 return TEE_ERROR_BAD_PARAMETERS; 1178 if ((dst_o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1179 return TEE_ERROR_BAD_PARAMETERS; 1180 1181 if (dst_o->info.objectType == src_o->info.objectType) { 1182 /* Copy whole data */ 1183 1184 if (dst_o->data_size != src_o->data_size) 1185 return TEE_ERROR_BAD_STATE; 1186 if (dst_o->cleanup != src_o->cleanup) 1187 return TEE_ERROR_BAD_STATE; 1188 1189 dst_o->have_attrs = src_o->have_attrs; 1190 1191 switch (src_o->info.objectType) { 1192 case TEE_TYPE_RSA_PUBLIC_KEY: 1193 copy_rsa_public_key(dst_o->data, src_o->data); 1194 break; 1195 case TEE_TYPE_RSA_KEYPAIR: 1196 copy_rsa_keypair(dst_o->data, src_o->data); 1197 break; 1198 case TEE_TYPE_DSA_PUBLIC_KEY: 1199 copy_dsa_public_key(dst_o->data, src_o->data); 1200 break; 1201 case TEE_TYPE_DSA_KEYPAIR: 1202 copy_dsa_keypair(dst_o->data, src_o->data); 1203 break; 1204 case TEE_TYPE_DH_KEYPAIR: 1205 copy_dh_keypair(dst_o->data, src_o->data); 1206 break; 1207 default: 1208 /* Generic case */ 1209 memcpy(dst_o->data, src_o->data, src_o->data_size); 1210 } 1211 } else if (dst_o->info.objectType == TEE_TYPE_RSA_PUBLIC_KEY && 1212 src_o->info.objectType == TEE_TYPE_RSA_KEYPAIR) { 1213 /* Extract public key from RSA key pair */ 1214 size_t n; 1215 1216 extract_rsa_public_key(dst_o->data, src_o->data); 1217 dst_o->have_attrs = 0; 1218 for (n = 0; n < TEE_ARRAY_SIZE(tee_cryp_obj_rsa_pub_key_attrs); 1219 n++) 1220 dst_o->have_attrs |= 1 << n; 1221 1222 } else if (dst_o->info.objectType == TEE_TYPE_DSA_PUBLIC_KEY && 1223 src_o->info.objectType == TEE_TYPE_DSA_KEYPAIR) { 1224 /* Extract public key from DSA key pair */ 1225 size_t n; 1226 1227 extract_dsa_public_key(dst_o->data, src_o->data); 1228 dst_o->have_attrs = 0; 1229 for (n = 0; n < TEE_ARRAY_SIZE(tee_cryp_obj_dsa_pub_key_attrs); 1230 n++) 1231 dst_o->have_attrs |= 1 << n; 1232 1233 } else { 1234 return TEE_ERROR_BAD_PARAMETERS; 1235 } 1236 1237 dst_o->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 1238 dst_o->info.objectSize = src_o->info.objectSize; 1239 dst_o->info.objectUsage = src_o->info.objectUsage; 1240 return TEE_SUCCESS; 1241 } 1242 1243 static TEE_Result tee_svc_obj_generate_key_rsa( 1244 struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props, 1245 uint32_t key_size) 1246 { 1247 TEE_Result res; 1248 struct rsa_keypair *key = o->data; 1249 uint32_t e = TEE_U32_TO_BIG_ENDIAN(65537); 1250 1251 TEE_ASSERT(sizeof(struct rsa_keypair) == o->data_size); 1252 if (!crypto_ops.acipher.gen_rsa_key || !crypto_ops.bignum.bin2bn) 1253 return TEE_ERROR_NOT_IMPLEMENTED; 1254 if (!GET_ATTRIBUTE(o, type_props, TEE_ATTR_RSA_PUBLIC_EXPONENT)) 1255 crypto_ops.bignum.bin2bn((const uint8_t *)&e, sizeof(e), 1256 key->e); 1257 res = crypto_ops.acipher.gen_rsa_key(o->data, key_size); 1258 if (res != TEE_SUCCESS) 1259 return res; 1260 1261 /* Set bits for all known attributes for this object type */ 1262 o->have_attrs = (1 << type_props->num_type_attrs) - 1; 1263 1264 return TEE_SUCCESS; 1265 } 1266 1267 static TEE_Result tee_svc_obj_generate_key_dsa( 1268 struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props, 1269 uint32_t key_size) 1270 { 1271 TEE_Result res; 1272 1273 TEE_ASSERT(sizeof(struct dsa_keypair) == o->data_size); 1274 if (!crypto_ops.acipher.gen_dsa_key) 1275 return TEE_ERROR_NOT_IMPLEMENTED; 1276 res = crypto_ops.acipher.gen_dsa_key(o->data, key_size); 1277 if (res != TEE_SUCCESS) 1278 return res; 1279 1280 /* Set bits for all known attributes for this object type */ 1281 o->have_attrs = (1 << type_props->num_type_attrs) - 1; 1282 1283 return TEE_SUCCESS; 1284 } 1285 1286 static TEE_Result tee_svc_obj_generate_key_dh( 1287 struct tee_ta_session *sess, 1288 struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props, 1289 uint32_t key_size __unused, 1290 const TEE_Attribute *params, uint32_t param_count) 1291 { 1292 TEE_Result res; 1293 struct dh_keypair *tee_dh_key; 1294 struct bignum *dh_q = NULL; 1295 uint32_t dh_xbits = 0; 1296 1297 TEE_ASSERT(sizeof(struct dh_keypair) == o->data_size); 1298 1299 /* Copy the present attributes into the obj before starting */ 1300 res = tee_svc_cryp_obj_populate_type( 1301 sess, o, type_props, params, param_count); 1302 if (res != TEE_SUCCESS) 1303 return res; 1304 1305 tee_dh_key = (struct dh_keypair *)o->data; 1306 1307 if (GET_ATTRIBUTE(o, type_props, TEE_ATTR_DH_SUBPRIME)) 1308 dh_q = tee_dh_key->q; 1309 if (GET_ATTRIBUTE(o, type_props, TEE_ATTR_DH_X_BITS)) 1310 dh_xbits = tee_dh_key->xbits; 1311 if (!crypto_ops.acipher.gen_dh_key) 1312 return TEE_ERROR_NOT_IMPLEMENTED; 1313 res = crypto_ops.acipher.gen_dh_key(tee_dh_key, dh_q, dh_xbits); 1314 if (res != TEE_SUCCESS) 1315 return res; 1316 1317 /* Set bits for the generated public and private key */ 1318 SET_ATTRIBUTE(o, type_props, TEE_ATTR_DH_PUBLIC_VALUE); 1319 SET_ATTRIBUTE(o, type_props, TEE_ATTR_DH_PRIVATE_VALUE); 1320 SET_ATTRIBUTE(o, type_props, TEE_ATTR_DH_X_BITS); 1321 return TEE_SUCCESS; 1322 } 1323 1324 TEE_Result tee_svc_obj_generate_key( 1325 uint32_t obj, uint32_t key_size, 1326 const TEE_Attribute *params, uint32_t param_count) 1327 { 1328 TEE_Result res; 1329 struct tee_ta_session *sess; 1330 const struct tee_cryp_obj_type_props *type_props; 1331 struct tee_obj *o; 1332 struct tee_cryp_obj_secret *key; 1333 size_t byte_size; 1334 1335 res = tee_ta_get_current_session(&sess); 1336 if (res != TEE_SUCCESS) 1337 return res; 1338 1339 res = tee_obj_get(sess->ctx, obj, &o); 1340 if (res != TEE_SUCCESS) 1341 return res; 1342 1343 /* Must be a transient object */ 1344 if ((o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 1345 return TEE_ERROR_BAD_STATE; 1346 1347 /* Must not be initialized already */ 1348 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1349 return TEE_ERROR_BAD_STATE; 1350 1351 /* Find description of object */ 1352 type_props = tee_svc_find_type_props(o->info.objectType); 1353 if (!type_props) 1354 return TEE_ERROR_NOT_SUPPORTED; 1355 1356 /* Check that maxObjectSize follows restrictions */ 1357 if (key_size % type_props->quanta != 0) 1358 return TEE_ERROR_NOT_SUPPORTED; 1359 if (key_size < type_props->min_size) 1360 return TEE_ERROR_NOT_SUPPORTED; 1361 if (key_size > type_props->max_size) 1362 return TEE_ERROR_NOT_SUPPORTED; 1363 1364 res = tee_svc_cryp_check_attr(ATTR_USAGE_GENERATE_KEY, type_props, 1365 (TEE_Attribute *)params, param_count); 1366 if (res != TEE_SUCCESS) 1367 return res; 1368 1369 switch (o->info.objectType) { 1370 case TEE_TYPE_AES: 1371 case TEE_TYPE_DES: 1372 case TEE_TYPE_DES3: 1373 case TEE_TYPE_HMAC_MD5: 1374 case TEE_TYPE_HMAC_SHA1: 1375 case TEE_TYPE_HMAC_SHA224: 1376 case TEE_TYPE_HMAC_SHA256: 1377 case TEE_TYPE_HMAC_SHA384: 1378 case TEE_TYPE_HMAC_SHA512: 1379 case TEE_TYPE_GENERIC_SECRET: 1380 byte_size = key_size / 8; 1381 1382 /* 1383 * We have to do it like this because the parity bits aren't 1384 * counted when telling the size of the key in bits. 1385 */ 1386 if (o->info.objectType == TEE_TYPE_DES || 1387 o->info.objectType == TEE_TYPE_DES3) { 1388 byte_size = (key_size + key_size / 7) / 8; 1389 } 1390 1391 key = (struct tee_cryp_obj_secret *)o->data; 1392 if (byte_size > (o->data_size - sizeof(*key))) 1393 return TEE_ERROR_EXCESS_DATA; 1394 1395 res = get_rng_array((void *)(key + 1), byte_size); 1396 if (res != TEE_SUCCESS) 1397 return res; 1398 1399 /* Force the last bit to have exactly a value on byte_size */ 1400 ((char *)key)[sizeof(key->key_size) + byte_size - 1] |= 0x80; 1401 key->key_size = byte_size; 1402 1403 /* Set bits for all known attributes for this object type */ 1404 o->have_attrs = (1 << type_props->num_type_attrs) - 1; 1405 1406 break; 1407 1408 case TEE_TYPE_RSA_KEYPAIR: 1409 res = tee_svc_obj_generate_key_rsa(o, type_props, key_size); 1410 if (res != TEE_SUCCESS) 1411 return res; 1412 break; 1413 1414 case TEE_TYPE_DSA_KEYPAIR: 1415 res = tee_svc_obj_generate_key_dsa(o, type_props, key_size); 1416 if (res != TEE_SUCCESS) 1417 return res; 1418 break; 1419 1420 case TEE_TYPE_DH_KEYPAIR: 1421 res = tee_svc_obj_generate_key_dh( 1422 sess, o, type_props, key_size, params, param_count); 1423 if (res != TEE_SUCCESS) 1424 return res; 1425 break; 1426 1427 default: 1428 return TEE_ERROR_BAD_FORMAT; 1429 } 1430 1431 o->info.objectSize = key_size; 1432 o->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 1433 return TEE_SUCCESS; 1434 } 1435 1436 static TEE_Result tee_svc_cryp_get_state(struct tee_ta_session *sess, 1437 uint32_t state_id, 1438 struct tee_cryp_state **state) 1439 { 1440 struct tee_cryp_state *s; 1441 1442 TAILQ_FOREACH(s, &sess->ctx->cryp_states, link) { 1443 if (state_id == (uint32_t) s) { 1444 *state = s; 1445 return TEE_SUCCESS; 1446 } 1447 } 1448 return TEE_ERROR_BAD_PARAMETERS; 1449 } 1450 1451 static void cryp_state_free(struct tee_ta_ctx *ctx, struct tee_cryp_state *cs) 1452 { 1453 struct tee_obj *o; 1454 1455 if (tee_obj_get(ctx, cs->key1, &o) == TEE_SUCCESS) 1456 tee_obj_close(ctx, o); 1457 if (tee_obj_get(ctx, cs->key2, &o) == TEE_SUCCESS) 1458 tee_obj_close(ctx, o); 1459 1460 TAILQ_REMOVE(&ctx->cryp_states, cs, link); 1461 if (cs->ctx_finalize != NULL) 1462 cs->ctx_finalize(cs->ctx, cs->algo); 1463 free(cs->ctx); 1464 free(cs); 1465 } 1466 1467 static TEE_Result tee_svc_cryp_check_key_type(const struct tee_obj *o, 1468 uint32_t algo, 1469 TEE_OperationMode mode) 1470 { 1471 uint32_t req_key_type; 1472 1473 switch (TEE_ALG_GET_MAIN_ALG(algo)) { 1474 case TEE_MAIN_ALGO_MD5: 1475 req_key_type = TEE_TYPE_HMAC_MD5; 1476 break; 1477 case TEE_MAIN_ALGO_SHA1: 1478 req_key_type = TEE_TYPE_HMAC_SHA1; 1479 break; 1480 case TEE_MAIN_ALGO_SHA224: 1481 req_key_type = TEE_TYPE_HMAC_SHA224; 1482 break; 1483 case TEE_MAIN_ALGO_SHA256: 1484 req_key_type = TEE_TYPE_HMAC_SHA256; 1485 break; 1486 case TEE_MAIN_ALGO_SHA384: 1487 req_key_type = TEE_TYPE_HMAC_SHA384; 1488 break; 1489 case TEE_MAIN_ALGO_SHA512: 1490 req_key_type = TEE_TYPE_HMAC_SHA512; 1491 break; 1492 case TEE_MAIN_ALGO_AES: 1493 req_key_type = TEE_TYPE_AES; 1494 break; 1495 case TEE_MAIN_ALGO_DES: 1496 req_key_type = TEE_TYPE_DES; 1497 break; 1498 case TEE_MAIN_ALGO_DES3: 1499 req_key_type = TEE_TYPE_DES3; 1500 break; 1501 case TEE_MAIN_ALGO_RSA: 1502 if (mode == TEE_MODE_ENCRYPT || mode == TEE_MODE_VERIFY) 1503 req_key_type = TEE_TYPE_RSA_PUBLIC_KEY; 1504 else 1505 req_key_type = TEE_TYPE_RSA_KEYPAIR; 1506 break; 1507 case TEE_MAIN_ALGO_DSA: 1508 if (mode == TEE_MODE_ENCRYPT || mode == TEE_MODE_VERIFY) 1509 req_key_type = TEE_TYPE_DSA_PUBLIC_KEY; 1510 else 1511 req_key_type = TEE_TYPE_DSA_KEYPAIR; 1512 break; 1513 case TEE_MAIN_ALGO_DH: 1514 req_key_type = TEE_TYPE_DH_KEYPAIR; 1515 break; 1516 default: 1517 return TEE_ERROR_BAD_PARAMETERS; 1518 } 1519 1520 if (req_key_type != o->info.objectType) 1521 return TEE_ERROR_BAD_PARAMETERS; 1522 return TEE_SUCCESS; 1523 } 1524 1525 TEE_Result tee_svc_cryp_state_alloc(uint32_t algo, uint32_t mode, 1526 uint32_t key1, uint32_t key2, 1527 uint32_t *state) 1528 { 1529 TEE_Result res; 1530 struct tee_cryp_state *cs; 1531 struct tee_ta_session *sess; 1532 struct tee_obj *o1 = NULL; 1533 struct tee_obj *o2 = NULL; 1534 1535 res = tee_ta_get_current_session(&sess); 1536 if (res != TEE_SUCCESS) 1537 return res; 1538 1539 if (key1 != 0) { 1540 res = tee_obj_get(sess->ctx, key1, &o1); 1541 if (res != TEE_SUCCESS) 1542 return res; 1543 if (o1->busy) 1544 return TEE_ERROR_BAD_PARAMETERS; 1545 res = tee_svc_cryp_check_key_type(o1, algo, mode); 1546 if (res != TEE_SUCCESS) 1547 return res; 1548 } 1549 if (key2 != 0) { 1550 res = tee_obj_get(sess->ctx, key2, &o2); 1551 if (res != TEE_SUCCESS) 1552 return res; 1553 if (o2->busy) 1554 return TEE_ERROR_BAD_PARAMETERS; 1555 res = tee_svc_cryp_check_key_type(o2, algo, mode); 1556 if (res != TEE_SUCCESS) 1557 return res; 1558 } 1559 1560 cs = calloc(1, sizeof(struct tee_cryp_state)); 1561 if (!cs) 1562 return TEE_ERROR_OUT_OF_MEMORY; 1563 TAILQ_INSERT_TAIL(&sess->ctx->cryp_states, cs, link); 1564 cs->algo = algo; 1565 cs->mode = mode; 1566 1567 switch (TEE_ALG_GET_CLASS(algo)) { 1568 case TEE_OPERATION_CIPHER: 1569 if ((algo == TEE_ALG_AES_XTS && (key1 == 0 || key2 == 0)) || 1570 (algo != TEE_ALG_AES_XTS && (key1 == 0 || key2 != 0))) { 1571 res = TEE_ERROR_BAD_PARAMETERS; 1572 } else { 1573 if (crypto_ops.cipher.get_ctx_size) 1574 res = crypto_ops.cipher.get_ctx_size(algo, 1575 &cs->ctx_size); 1576 else 1577 res = TEE_ERROR_NOT_IMPLEMENTED; 1578 if (res != TEE_SUCCESS) 1579 break; 1580 cs->ctx = calloc(1, cs->ctx_size); 1581 if (!cs->ctx) 1582 res = TEE_ERROR_OUT_OF_MEMORY; 1583 } 1584 break; 1585 case TEE_OPERATION_AE: 1586 if (key1 == 0 || key2 != 0) { 1587 res = TEE_ERROR_BAD_PARAMETERS; 1588 } else { 1589 if (crypto_ops.authenc.get_ctx_size) 1590 res = crypto_ops.authenc.get_ctx_size(algo, 1591 &cs->ctx_size); 1592 else 1593 res = TEE_ERROR_NOT_IMPLEMENTED; 1594 if (res != TEE_SUCCESS) 1595 break; 1596 cs->ctx = calloc(1, cs->ctx_size); 1597 if (!cs->ctx) 1598 res = TEE_ERROR_OUT_OF_MEMORY; 1599 } 1600 break; 1601 case TEE_OPERATION_MAC: 1602 if (key1 == 0 || key2 != 0) { 1603 res = TEE_ERROR_BAD_PARAMETERS; 1604 } else { 1605 if (crypto_ops.mac.get_ctx_size) 1606 res = crypto_ops.mac.get_ctx_size(algo, 1607 &cs->ctx_size); 1608 else 1609 res = TEE_ERROR_NOT_IMPLEMENTED; 1610 if (res != TEE_SUCCESS) 1611 break; 1612 cs->ctx = calloc(1, cs->ctx_size); 1613 if (!cs->ctx) 1614 res = TEE_ERROR_OUT_OF_MEMORY; 1615 } 1616 break; 1617 case TEE_OPERATION_DIGEST: 1618 if (key1 != 0 || key2 != 0) { 1619 res = TEE_ERROR_BAD_PARAMETERS; 1620 } else { 1621 if (crypto_ops.hash.get_ctx_size) 1622 res = crypto_ops.hash.get_ctx_size(algo, 1623 &cs->ctx_size); 1624 else 1625 res = TEE_ERROR_NOT_IMPLEMENTED; 1626 if (res != TEE_SUCCESS) 1627 break; 1628 cs->ctx = calloc(1, cs->ctx_size); 1629 if (!cs->ctx) 1630 res = TEE_ERROR_OUT_OF_MEMORY; 1631 } 1632 break; 1633 case TEE_OPERATION_ASYMMETRIC_CIPHER: 1634 case TEE_OPERATION_ASYMMETRIC_SIGNATURE: 1635 if (key1 == 0 || key2 != 0) 1636 res = TEE_ERROR_BAD_PARAMETERS; 1637 break; 1638 case TEE_OPERATION_KEY_DERIVATION: 1639 if (key1 == 0 || key2 != 0) 1640 res = TEE_ERROR_BAD_PARAMETERS; 1641 break; 1642 default: 1643 res = TEE_ERROR_NOT_SUPPORTED; 1644 break; 1645 } 1646 if (res != TEE_SUCCESS) 1647 goto out; 1648 1649 res = tee_svc_copy_to_user(sess, state, &cs, sizeof(uint32_t)); 1650 if (res != TEE_SUCCESS) 1651 goto out; 1652 1653 /* Register keys */ 1654 if (o1 != NULL) { 1655 o1->busy = true; 1656 cs->key1 = key1; 1657 } 1658 if (o2 != NULL) { 1659 o2->busy = true; 1660 cs->key2 = key2; 1661 } 1662 1663 out: 1664 if (res != TEE_SUCCESS) 1665 cryp_state_free(sess->ctx, cs); 1666 return res; 1667 } 1668 1669 TEE_Result tee_svc_cryp_state_copy(uint32_t dst, uint32_t src) 1670 { 1671 TEE_Result res; 1672 struct tee_cryp_state *cs_dst; 1673 struct tee_cryp_state *cs_src; 1674 struct tee_ta_session *sess; 1675 1676 res = tee_ta_get_current_session(&sess); 1677 if (res != TEE_SUCCESS) 1678 return res; 1679 1680 res = tee_svc_cryp_get_state(sess, dst, &cs_dst); 1681 if (res != TEE_SUCCESS) 1682 return res; 1683 res = tee_svc_cryp_get_state(sess, src, &cs_src); 1684 if (res != TEE_SUCCESS) 1685 return res; 1686 if (cs_dst->algo != cs_src->algo || cs_dst->mode != cs_src->mode) 1687 return TEE_ERROR_BAD_PARAMETERS; 1688 /* "Can't happen" */ 1689 if (cs_dst->ctx_size != cs_src->ctx_size) 1690 return TEE_ERROR_BAD_STATE; 1691 1692 memcpy(cs_dst->ctx, cs_src->ctx, cs_src->ctx_size); 1693 return TEE_SUCCESS; 1694 } 1695 1696 void tee_svc_cryp_free_states(struct tee_ta_ctx *ctx) 1697 { 1698 struct tee_cryp_state_head *states = &ctx->cryp_states; 1699 1700 while (!TAILQ_EMPTY(states)) 1701 cryp_state_free(ctx, TAILQ_FIRST(states)); 1702 } 1703 1704 TEE_Result tee_svc_cryp_state_free(uint32_t state) 1705 { 1706 TEE_Result res; 1707 struct tee_cryp_state *cs; 1708 struct tee_ta_session *sess; 1709 1710 res = tee_ta_get_current_session(&sess); 1711 if (res != TEE_SUCCESS) 1712 return res; 1713 1714 res = tee_svc_cryp_get_state(sess, state, &cs); 1715 if (res != TEE_SUCCESS) 1716 return res; 1717 cryp_state_free(sess->ctx, cs); 1718 return TEE_SUCCESS; 1719 } 1720 1721 /* iv and iv_len are ignored for some algorithms */ 1722 TEE_Result tee_svc_hash_init(uint32_t state, const void *iv __unused, 1723 size_t iv_len __unused) 1724 { 1725 TEE_Result res; 1726 struct tee_cryp_state *cs; 1727 struct tee_ta_session *sess; 1728 1729 res = tee_ta_get_current_session(&sess); 1730 if (res != TEE_SUCCESS) 1731 return res; 1732 1733 res = tee_svc_cryp_get_state(sess, state, &cs); 1734 if (res != TEE_SUCCESS) 1735 return res; 1736 1737 switch (TEE_ALG_GET_CLASS(cs->algo)) { 1738 case TEE_OPERATION_DIGEST: 1739 if (!crypto_ops.hash.init) 1740 return TEE_ERROR_NOT_IMPLEMENTED; 1741 res = crypto_ops.hash.init(cs->ctx, cs->algo); 1742 if (res != TEE_SUCCESS) 1743 return res; 1744 break; 1745 case TEE_OPERATION_MAC: 1746 { 1747 struct tee_obj *o; 1748 struct tee_cryp_obj_secret *key; 1749 1750 res = tee_obj_get(sess->ctx, cs->key1, &o); 1751 if (res != TEE_SUCCESS) 1752 return res; 1753 if ((o->info.handleFlags & 1754 TEE_HANDLE_FLAG_INITIALIZED) == 0) 1755 return TEE_ERROR_BAD_PARAMETERS; 1756 1757 key = (struct tee_cryp_obj_secret *)o->data; 1758 if (!crypto_ops.mac.init) 1759 return TEE_ERROR_NOT_IMPLEMENTED; 1760 res = crypto_ops.mac.init(cs->ctx, cs->algo, 1761 (void *)(key + 1), 1762 key->key_size); 1763 if (res != TEE_SUCCESS) 1764 return res; 1765 break; 1766 } 1767 default: 1768 return TEE_ERROR_BAD_PARAMETERS; 1769 } 1770 1771 return TEE_SUCCESS; 1772 } 1773 1774 TEE_Result tee_svc_hash_update(uint32_t state, const void *chunk, 1775 size_t chunk_size) 1776 { 1777 TEE_Result res; 1778 struct tee_cryp_state *cs; 1779 struct tee_ta_session *sess; 1780 1781 /* No data, but size provided isn't valid parameters. */ 1782 if (!chunk && chunk_size) 1783 return TEE_ERROR_BAD_PARAMETERS; 1784 1785 /* Zero length hash is valid, but nothing we need to do. */ 1786 if (!chunk_size) 1787 return TEE_SUCCESS; 1788 1789 res = tee_ta_get_current_session(&sess); 1790 if (res != TEE_SUCCESS) 1791 return res; 1792 1793 res = tee_mmu_check_access_rights(sess->ctx, 1794 TEE_MEMORY_ACCESS_READ | 1795 TEE_MEMORY_ACCESS_ANY_OWNER, 1796 (tee_uaddr_t)chunk, chunk_size); 1797 if (res != TEE_SUCCESS) 1798 return res; 1799 1800 res = tee_svc_cryp_get_state(sess, state, &cs); 1801 if (res != TEE_SUCCESS) 1802 return res; 1803 1804 switch (TEE_ALG_GET_CLASS(cs->algo)) { 1805 case TEE_OPERATION_DIGEST: 1806 if (!crypto_ops.hash.update) 1807 return TEE_ERROR_NOT_IMPLEMENTED; 1808 res = crypto_ops.hash.update(cs->ctx, cs->algo, chunk, 1809 chunk_size); 1810 if (res != TEE_SUCCESS) 1811 return res; 1812 break; 1813 case TEE_OPERATION_MAC: 1814 if (!crypto_ops.mac.update) 1815 return TEE_ERROR_NOT_IMPLEMENTED; 1816 res = crypto_ops.mac.update(cs->ctx, cs->algo, chunk, 1817 chunk_size); 1818 if (res != TEE_SUCCESS) 1819 return res; 1820 break; 1821 default: 1822 return TEE_ERROR_BAD_PARAMETERS; 1823 } 1824 1825 return TEE_SUCCESS; 1826 } 1827 1828 TEE_Result tee_svc_hash_final(uint32_t state, const void *chunk, 1829 size_t chunk_size, void *hash, size_t *hash_len) 1830 { 1831 TEE_Result res, res2; 1832 size_t hash_size; 1833 size_t hlen; 1834 struct tee_cryp_state *cs; 1835 struct tee_ta_session *sess; 1836 1837 /* No data, but size provided isn't valid parameters. */ 1838 if (!chunk && chunk_size) 1839 return TEE_ERROR_BAD_PARAMETERS; 1840 1841 res = tee_ta_get_current_session(&sess); 1842 if (res != TEE_SUCCESS) 1843 return res; 1844 1845 res = tee_mmu_check_access_rights(sess->ctx, 1846 TEE_MEMORY_ACCESS_READ | 1847 TEE_MEMORY_ACCESS_ANY_OWNER, 1848 (tee_uaddr_t)chunk, chunk_size); 1849 if (res != TEE_SUCCESS) 1850 return res; 1851 1852 res = tee_svc_copy_from_user(sess, &hlen, hash_len, sizeof(size_t)); 1853 if (res != TEE_SUCCESS) 1854 return res; 1855 1856 res = tee_mmu_check_access_rights(sess->ctx, 1857 TEE_MEMORY_ACCESS_READ | 1858 TEE_MEMORY_ACCESS_WRITE | 1859 TEE_MEMORY_ACCESS_ANY_OWNER, 1860 (tee_uaddr_t)hash, hlen); 1861 if (res != TEE_SUCCESS) 1862 return res; 1863 1864 res = tee_svc_cryp_get_state(sess, state, &cs); 1865 if (res != TEE_SUCCESS) 1866 return res; 1867 1868 switch (TEE_ALG_GET_CLASS(cs->algo)) { 1869 case TEE_OPERATION_DIGEST: 1870 if (!crypto_ops.hash.update || !crypto_ops.hash.final) 1871 return TEE_ERROR_NOT_IMPLEMENTED; 1872 res = tee_hash_get_digest_size(cs->algo, &hash_size); 1873 if (res != TEE_SUCCESS) 1874 return res; 1875 if (*hash_len < hash_size) { 1876 res = TEE_ERROR_SHORT_BUFFER; 1877 goto out; 1878 } 1879 1880 if (chunk_size) { 1881 res = crypto_ops.hash.update(cs->ctx, cs->algo, chunk, 1882 chunk_size); 1883 if (res != TEE_SUCCESS) 1884 return res; 1885 } 1886 1887 res = crypto_ops.hash.final(cs->ctx, cs->algo, hash, 1888 hash_size); 1889 if (res != TEE_SUCCESS) 1890 return res; 1891 break; 1892 1893 case TEE_OPERATION_MAC: 1894 if (!crypto_ops.mac.update || !crypto_ops.mac.final) 1895 return TEE_ERROR_NOT_IMPLEMENTED; 1896 res = tee_mac_get_digest_size(cs->algo, &hash_size); 1897 if (res != TEE_SUCCESS) 1898 return res; 1899 if (*hash_len < hash_size) { 1900 res = TEE_ERROR_SHORT_BUFFER; 1901 goto out; 1902 } 1903 1904 if (chunk_size) { 1905 res = crypto_ops.mac.update(cs->ctx, cs->algo, chunk, 1906 chunk_size); 1907 if (res != TEE_SUCCESS) 1908 return res; 1909 } 1910 1911 res = crypto_ops.mac.final(cs->ctx, cs->algo, hash, hash_size); 1912 if (res != TEE_SUCCESS) 1913 return res; 1914 break; 1915 1916 default: 1917 return TEE_ERROR_BAD_PARAMETERS; 1918 } 1919 out: 1920 res2 = 1921 tee_svc_copy_to_user(sess, hash_len, &hash_size, sizeof(*hash_len)); 1922 if (res2 != TEE_SUCCESS) 1923 return res2; 1924 return res; 1925 } 1926 1927 TEE_Result tee_svc_cipher_init(uint32_t state, const void *iv, size_t iv_len) 1928 { 1929 TEE_Result res; 1930 struct tee_cryp_state *cs; 1931 struct tee_ta_session *sess; 1932 struct tee_obj *o; 1933 struct tee_cryp_obj_secret *key1; 1934 1935 res = tee_ta_get_current_session(&sess); 1936 if (res != TEE_SUCCESS) 1937 return res; 1938 1939 res = tee_svc_cryp_get_state(sess, state, &cs); 1940 if (res != TEE_SUCCESS) 1941 return res; 1942 1943 res = tee_mmu_check_access_rights(sess->ctx, 1944 TEE_MEMORY_ACCESS_READ | 1945 TEE_MEMORY_ACCESS_ANY_OWNER, 1946 (tee_uaddr_t) iv, iv_len); 1947 if (res != TEE_SUCCESS) 1948 return res; 1949 1950 res = tee_obj_get(sess->ctx, cs->key1, &o); 1951 if (res != TEE_SUCCESS) 1952 return res; 1953 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1954 return TEE_ERROR_BAD_PARAMETERS; 1955 1956 key1 = (struct tee_cryp_obj_secret *)o->data; 1957 1958 if (!crypto_ops.cipher.init) 1959 return TEE_ERROR_NOT_IMPLEMENTED; 1960 1961 if (tee_obj_get(sess->ctx, cs->key2, &o) == TEE_SUCCESS) { 1962 struct tee_cryp_obj_secret *key2 = 1963 (struct tee_cryp_obj_secret *)o->data; 1964 1965 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1966 return TEE_ERROR_BAD_PARAMETERS; 1967 1968 res = crypto_ops.cipher.init(cs->ctx, cs->algo, cs->mode, 1969 (uint8_t *)(key1 + 1), 1970 key1->key_size, 1971 (uint8_t *)(key2 + 1), 1972 key2->key_size, 1973 iv, iv_len); 1974 } else { 1975 res = crypto_ops.cipher.init(cs->ctx, cs->algo, cs->mode, 1976 (uint8_t *)(key1 + 1), 1977 key1->key_size, 1978 NULL, 1979 0, 1980 iv, iv_len); 1981 } 1982 if (res != TEE_SUCCESS) 1983 return res; 1984 1985 cs->ctx_finalize = crypto_ops.cipher.final; 1986 return TEE_SUCCESS; 1987 } 1988 1989 static TEE_Result tee_svc_cipher_update_helper(uint32_t state, bool last_block, 1990 const void *src, size_t src_len, 1991 void *dst, size_t *dst_len) 1992 { 1993 TEE_Result res; 1994 struct tee_cryp_state *cs; 1995 struct tee_ta_session *sess; 1996 size_t dlen; 1997 1998 res = tee_ta_get_current_session(&sess); 1999 if (res != TEE_SUCCESS) 2000 return res; 2001 2002 res = tee_svc_cryp_get_state(sess, state, &cs); 2003 if (res != TEE_SUCCESS) 2004 return res; 2005 2006 res = tee_mmu_check_access_rights(sess->ctx, 2007 TEE_MEMORY_ACCESS_READ | 2008 TEE_MEMORY_ACCESS_ANY_OWNER, 2009 (tee_uaddr_t)src, src_len); 2010 if (res != TEE_SUCCESS) 2011 return res; 2012 2013 if (!dst_len) { 2014 dlen = 0; 2015 } else { 2016 res = 2017 tee_svc_copy_from_user(sess, &dlen, dst_len, 2018 sizeof(size_t)); 2019 if (res != TEE_SUCCESS) 2020 return res; 2021 2022 res = tee_mmu_check_access_rights(sess->ctx, 2023 TEE_MEMORY_ACCESS_READ | 2024 TEE_MEMORY_ACCESS_WRITE | 2025 TEE_MEMORY_ACCESS_ANY_OWNER, 2026 (tee_uaddr_t)dst, dlen); 2027 if (res != TEE_SUCCESS) 2028 return res; 2029 } 2030 2031 if (dlen < src_len) { 2032 res = TEE_ERROR_SHORT_BUFFER; 2033 goto out; 2034 } 2035 2036 if (src_len > 0) { 2037 /* Permit src_len == 0 to finalize the operation */ 2038 res = tee_do_cipher_update(cs->ctx, cs->algo, cs->mode, 2039 last_block, src, src_len, dst); 2040 } 2041 2042 if (last_block && cs->ctx_finalize != NULL) { 2043 cs->ctx_finalize(cs->ctx, cs->mode); 2044 cs->ctx_finalize = NULL; 2045 } 2046 2047 out: 2048 if ((res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) && 2049 dst_len != NULL) { 2050 TEE_Result res2 = tee_svc_copy_to_user(sess, dst_len, &src_len, 2051 sizeof(size_t)); 2052 if (res2 != TEE_SUCCESS) 2053 res = res2; 2054 } 2055 2056 return res; 2057 } 2058 2059 TEE_Result tee_svc_cipher_update(uint32_t state, const void *src, 2060 size_t src_len, void *dst, size_t *dst_len) 2061 { 2062 return tee_svc_cipher_update_helper(state, false /* last_block */, 2063 src, src_len, dst, dst_len); 2064 } 2065 2066 TEE_Result tee_svc_cipher_final(uint32_t state, const void *src, 2067 size_t src_len, void *dst, size_t *dst_len) 2068 { 2069 return tee_svc_cipher_update_helper(state, true /* last_block */, 2070 src, src_len, dst, dst_len); 2071 } 2072 2073 TEE_Result tee_svc_cryp_derive_key(uint32_t state, const TEE_Attribute *params, 2074 uint32_t param_count, uint32_t derived_key) 2075 { 2076 TEE_Result res; 2077 struct tee_ta_session *sess; 2078 struct tee_obj *ko; 2079 struct tee_obj *so; 2080 struct tee_cryp_state *cs; 2081 struct tee_cryp_obj_secret *sk; 2082 const struct tee_cryp_obj_type_props *type_props; 2083 struct bignum *publicvalue = NULL; 2084 struct bignum *sharedsecret = NULL; 2085 size_t alloc_size; 2086 2087 res = tee_ta_get_current_session(&sess); 2088 if (res != TEE_SUCCESS) 2089 return res; 2090 2091 res = tee_svc_cryp_get_state(sess, state, &cs); 2092 if (res != TEE_SUCCESS) 2093 return res; 2094 2095 if ((param_count != 1) || 2096 (params[0].attributeID != TEE_ATTR_DH_PUBLIC_VALUE)) 2097 return TEE_ERROR_BAD_PARAMETERS; 2098 2099 /* get key set in operation */ 2100 res = tee_obj_get(sess->ctx, cs->key1, &ko); 2101 if (res != TEE_SUCCESS) 2102 return res; 2103 2104 res = tee_obj_get(sess->ctx, derived_key, &so); 2105 if (res != TEE_SUCCESS) 2106 return res; 2107 2108 /* find information needed about the object to initialize */ 2109 sk = (struct tee_cryp_obj_secret *)so->data; 2110 2111 /* Find description of object */ 2112 type_props = tee_svc_find_type_props(so->info.objectType); 2113 if (!type_props) 2114 return TEE_ERROR_NOT_SUPPORTED; 2115 2116 if (!crypto_ops.bignum.allocate || 2117 !crypto_ops.bignum.free || 2118 !crypto_ops.bignum.bin2bn || 2119 !crypto_ops.bignum.bn2bin || 2120 !crypto_ops.bignum.num_bytes || 2121 !crypto_ops.acipher.dh_shared_secret) 2122 return TEE_ERROR_NOT_IMPLEMENTED; 2123 2124 /* extract information from the attributes passed to the function */ 2125 alloc_size = params[0].content.ref.length * 8; 2126 publicvalue = crypto_ops.bignum.allocate(alloc_size); 2127 sharedsecret = crypto_ops.bignum.allocate(alloc_size); 2128 if (!publicvalue || !sharedsecret) { 2129 res = TEE_ERROR_OUT_OF_MEMORY; 2130 goto out; 2131 } 2132 crypto_ops.bignum.bin2bn(params[0].content.ref.buffer, 2133 params[0].content.ref.length, 2134 publicvalue); 2135 res = crypto_ops.acipher.dh_shared_secret(ko->data, publicvalue, 2136 sharedsecret); 2137 if (res == TEE_SUCCESS) { 2138 sk->key_size = crypto_ops.bignum.num_bytes(sharedsecret); 2139 crypto_ops.bignum.bn2bin(sharedsecret, (uint8_t *)(sk + 1)); 2140 so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED; 2141 SET_ATTRIBUTE(so, type_props, TEE_ATTR_SECRET_VALUE); 2142 } 2143 out: 2144 crypto_ops.bignum.free(publicvalue); 2145 crypto_ops.bignum.free(sharedsecret); 2146 return res; 2147 } 2148 2149 TEE_Result tee_svc_cryp_random_number_generate(void *buf, size_t blen) 2150 { 2151 TEE_Result res; 2152 struct tee_ta_session *sess; 2153 2154 res = tee_ta_get_current_session(&sess); 2155 if (res != TEE_SUCCESS) 2156 return res; 2157 2158 res = tee_mmu_check_access_rights(sess->ctx, 2159 TEE_MEMORY_ACCESS_WRITE | 2160 TEE_MEMORY_ACCESS_ANY_OWNER, 2161 (tee_uaddr_t)buf, blen); 2162 if (res != TEE_SUCCESS) 2163 return res; 2164 2165 res = get_rng_array(buf, blen); 2166 if (res != TEE_SUCCESS) 2167 return res; 2168 2169 return res; 2170 } 2171 2172 TEE_Result tee_svc_authenc_init(uint32_t state, const void *nonce, 2173 size_t nonce_len, size_t tag_len, 2174 size_t aad_len, size_t payload_len) 2175 { 2176 TEE_Result res; 2177 struct tee_cryp_state *cs; 2178 struct tee_ta_session *sess; 2179 struct tee_obj *o; 2180 struct tee_cryp_obj_secret *key; 2181 2182 res = tee_ta_get_current_session(&sess); 2183 if (res != TEE_SUCCESS) 2184 return res; 2185 2186 res = tee_svc_cryp_get_state(sess, state, &cs); 2187 if (res != TEE_SUCCESS) 2188 return res; 2189 2190 res = tee_obj_get(sess->ctx, cs->key1, &o); 2191 if (res != TEE_SUCCESS) 2192 return res; 2193 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 2194 return TEE_ERROR_BAD_PARAMETERS; 2195 2196 if (!crypto_ops.authenc.init) 2197 return TEE_ERROR_NOT_IMPLEMENTED; 2198 key = (struct tee_cryp_obj_secret *)o->data; 2199 res = crypto_ops.authenc.init(cs->ctx, cs->algo, cs->mode, 2200 (uint8_t *)(key + 1), key->key_size, 2201 nonce, nonce_len, tag_len, aad_len, 2202 payload_len); 2203 if (res != TEE_SUCCESS) 2204 return res; 2205 2206 cs->ctx_finalize = (tee_cryp_ctx_finalize_func_t) 2207 crypto_ops.authenc.final; 2208 return TEE_SUCCESS; 2209 } 2210 2211 TEE_Result tee_svc_authenc_update_aad(uint32_t state, const void *aad_data, 2212 size_t aad_data_len) 2213 { 2214 TEE_Result res; 2215 struct tee_cryp_state *cs; 2216 struct tee_ta_session *sess; 2217 2218 res = tee_ta_get_current_session(&sess); 2219 if (res != TEE_SUCCESS) 2220 return res; 2221 2222 res = tee_mmu_check_access_rights(sess->ctx, 2223 TEE_MEMORY_ACCESS_READ | 2224 TEE_MEMORY_ACCESS_ANY_OWNER, 2225 (tee_uaddr_t) aad_data, 2226 aad_data_len); 2227 if (res != TEE_SUCCESS) 2228 return res; 2229 2230 res = tee_svc_cryp_get_state(sess, state, &cs); 2231 if (res != TEE_SUCCESS) 2232 return res; 2233 2234 if (!crypto_ops.authenc.update_aad) 2235 return TEE_ERROR_NOT_IMPLEMENTED; 2236 res = crypto_ops.authenc.update_aad(cs->ctx, cs->algo, cs->mode, 2237 aad_data, aad_data_len); 2238 if (res != TEE_SUCCESS) 2239 return res; 2240 2241 return TEE_SUCCESS; 2242 } 2243 2244 TEE_Result tee_svc_authenc_update_payload(uint32_t state, const void *src_data, 2245 size_t src_len, void *dst_data, 2246 size_t *dst_len) 2247 { 2248 TEE_Result res; 2249 struct tee_cryp_state *cs; 2250 struct tee_ta_session *sess; 2251 size_t dlen; 2252 2253 res = tee_ta_get_current_session(&sess); 2254 if (res != TEE_SUCCESS) 2255 return res; 2256 2257 res = tee_svc_cryp_get_state(sess, state, &cs); 2258 if (res != TEE_SUCCESS) 2259 return res; 2260 2261 res = tee_mmu_check_access_rights(sess->ctx, 2262 TEE_MEMORY_ACCESS_READ | 2263 TEE_MEMORY_ACCESS_ANY_OWNER, 2264 (tee_uaddr_t) src_data, src_len); 2265 if (res != TEE_SUCCESS) 2266 return res; 2267 2268 res = tee_svc_copy_from_user(sess, &dlen, dst_len, sizeof(size_t)); 2269 if (res != TEE_SUCCESS) 2270 return res; 2271 2272 res = tee_mmu_check_access_rights(sess->ctx, 2273 TEE_MEMORY_ACCESS_READ | 2274 TEE_MEMORY_ACCESS_WRITE | 2275 TEE_MEMORY_ACCESS_ANY_OWNER, 2276 (tee_uaddr_t)dst_data, dlen); 2277 if (res != TEE_SUCCESS) 2278 return res; 2279 2280 if (dlen < src_len) { 2281 res = TEE_ERROR_SHORT_BUFFER; 2282 goto out; 2283 } 2284 2285 if (!crypto_ops.authenc.update_payload) 2286 return TEE_ERROR_NOT_IMPLEMENTED; 2287 res = crypto_ops.authenc.update_payload(cs->ctx, cs->algo, cs->mode, 2288 src_data, src_len, dst_data, 2289 &dlen); 2290 2291 out: 2292 if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) { 2293 TEE_Result res2 = tee_svc_copy_to_user(sess, dst_len, &dlen, 2294 sizeof(size_t)); 2295 if (res2 != TEE_SUCCESS) 2296 res = res2; 2297 } 2298 2299 return res; 2300 } 2301 2302 TEE_Result tee_svc_authenc_enc_final(uint32_t state, const void *src_data, 2303 size_t src_len, void *dst_data, 2304 size_t *dst_len, void *tag, 2305 size_t *tag_len) 2306 { 2307 TEE_Result res; 2308 struct tee_cryp_state *cs; 2309 struct tee_ta_session *sess; 2310 size_t dlen; 2311 size_t tlen; 2312 2313 res = tee_ta_get_current_session(&sess); 2314 if (res != TEE_SUCCESS) 2315 return res; 2316 2317 res = tee_svc_cryp_get_state(sess, state, &cs); 2318 if (res != TEE_SUCCESS) 2319 return res; 2320 2321 if (cs->mode != TEE_MODE_ENCRYPT) 2322 return TEE_ERROR_BAD_PARAMETERS; 2323 2324 res = tee_mmu_check_access_rights(sess->ctx, 2325 TEE_MEMORY_ACCESS_READ | 2326 TEE_MEMORY_ACCESS_ANY_OWNER, 2327 (tee_uaddr_t)src_data, src_len); 2328 if (res != TEE_SUCCESS) 2329 return res; 2330 2331 if (!dst_len) { 2332 dlen = 0; 2333 } else { 2334 res = 2335 tee_svc_copy_from_user(sess, &dlen, dst_len, 2336 sizeof(size_t)); 2337 if (res != TEE_SUCCESS) 2338 return res; 2339 2340 res = tee_mmu_check_access_rights(sess->ctx, 2341 TEE_MEMORY_ACCESS_READ | 2342 TEE_MEMORY_ACCESS_WRITE | 2343 TEE_MEMORY_ACCESS_ANY_OWNER, 2344 (tee_uaddr_t)dst_data, dlen); 2345 if (res != TEE_SUCCESS) 2346 return res; 2347 } 2348 2349 if (dlen < src_len) { 2350 res = TEE_ERROR_SHORT_BUFFER; 2351 goto out; 2352 } 2353 2354 res = tee_svc_copy_from_user(sess, &tlen, tag_len, sizeof(size_t)); 2355 if (res != TEE_SUCCESS) 2356 return res; 2357 2358 res = tee_mmu_check_access_rights(sess->ctx, 2359 TEE_MEMORY_ACCESS_READ | 2360 TEE_MEMORY_ACCESS_WRITE | 2361 TEE_MEMORY_ACCESS_ANY_OWNER, 2362 (tee_uaddr_t)tag, tlen); 2363 if (res != TEE_SUCCESS) 2364 return res; 2365 2366 if (!crypto_ops.authenc.enc_final) 2367 return TEE_ERROR_NOT_IMPLEMENTED; 2368 res = crypto_ops.authenc.enc_final(cs->ctx, cs->algo, src_data, 2369 src_len, dst_data, &dlen, tag, 2370 &tlen); 2371 2372 out: 2373 if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) { 2374 TEE_Result res2; 2375 2376 if (dst_len != NULL) { 2377 res2 = tee_svc_copy_to_user(sess, dst_len, &dlen, 2378 sizeof(size_t)); 2379 if (res2 != TEE_SUCCESS) 2380 return res2; 2381 } 2382 2383 res2 = 2384 tee_svc_copy_to_user(sess, tag_len, &tlen, sizeof(size_t)); 2385 if (res2 != TEE_SUCCESS) 2386 return res2; 2387 } 2388 2389 return res; 2390 } 2391 2392 TEE_Result tee_svc_authenc_dec_final(uint32_t state, const void *src_data, 2393 size_t src_len, void *dst_data, 2394 size_t *dst_len, const void *tag, 2395 size_t tag_len) 2396 { 2397 TEE_Result res; 2398 struct tee_cryp_state *cs; 2399 struct tee_ta_session *sess; 2400 size_t dlen; 2401 2402 res = tee_ta_get_current_session(&sess); 2403 if (res != TEE_SUCCESS) 2404 return res; 2405 2406 res = tee_svc_cryp_get_state(sess, state, &cs); 2407 if (res != TEE_SUCCESS) 2408 return res; 2409 2410 if (cs->mode != TEE_MODE_DECRYPT) 2411 return TEE_ERROR_BAD_PARAMETERS; 2412 2413 res = tee_mmu_check_access_rights(sess->ctx, 2414 TEE_MEMORY_ACCESS_READ | 2415 TEE_MEMORY_ACCESS_ANY_OWNER, 2416 (tee_uaddr_t)src_data, src_len); 2417 if (res != TEE_SUCCESS) 2418 return res; 2419 2420 if (!dst_len) { 2421 dlen = 0; 2422 } else { 2423 res = 2424 tee_svc_copy_from_user(sess, &dlen, dst_len, 2425 sizeof(size_t)); 2426 if (res != TEE_SUCCESS) 2427 return res; 2428 2429 res = tee_mmu_check_access_rights(sess->ctx, 2430 TEE_MEMORY_ACCESS_READ | 2431 TEE_MEMORY_ACCESS_WRITE | 2432 TEE_MEMORY_ACCESS_ANY_OWNER, 2433 (tee_uaddr_t)dst_data, dlen); 2434 if (res != TEE_SUCCESS) 2435 return res; 2436 } 2437 2438 if (dlen < src_len) { 2439 res = TEE_ERROR_SHORT_BUFFER; 2440 goto out; 2441 } 2442 2443 res = tee_mmu_check_access_rights(sess->ctx, 2444 TEE_MEMORY_ACCESS_READ | 2445 TEE_MEMORY_ACCESS_ANY_OWNER, 2446 (tee_uaddr_t)tag, tag_len); 2447 if (res != TEE_SUCCESS) 2448 return res; 2449 2450 if (!crypto_ops.authenc.dec_final) 2451 return TEE_ERROR_NOT_IMPLEMENTED; 2452 res = crypto_ops.authenc.dec_final(cs->ctx, cs->algo, src_data, 2453 src_len, dst_data, &dlen, tag, 2454 tag_len); 2455 2456 out: 2457 if ((res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) && 2458 dst_len != NULL) { 2459 TEE_Result res2; 2460 2461 res2 = 2462 tee_svc_copy_to_user(sess, dst_len, &dlen, 2463 sizeof(size_t)); 2464 if (res2 != TEE_SUCCESS) 2465 return res2; 2466 } 2467 2468 return res; 2469 } 2470 2471 static void tee_svc_asymm_pkcs1_get_salt_len(const TEE_Attribute *params, 2472 uint32_t num_params, int *salt_len) 2473 { 2474 size_t n; 2475 2476 for (n = 0; n < num_params; n++) { 2477 if (params[n].attributeID == TEE_ATTR_RSA_PSS_SALT_LENGTH) { 2478 *salt_len = params[n].content.value.a; 2479 return; 2480 } 2481 } 2482 *salt_len = -1; 2483 } 2484 2485 TEE_Result tee_svc_asymm_operate(uint32_t state, const TEE_Attribute *params, 2486 uint32_t num_params, const void *src_data, 2487 size_t src_len, void *dst_data, 2488 size_t *dst_len) 2489 { 2490 TEE_Result res; 2491 struct tee_cryp_state *cs; 2492 struct tee_ta_session *sess; 2493 size_t dlen; 2494 struct tee_obj *o; 2495 void *label = NULL; 2496 size_t label_len = 0; 2497 size_t n; 2498 int salt_len; 2499 2500 res = tee_ta_get_current_session(&sess); 2501 if (res != TEE_SUCCESS) 2502 return res; 2503 2504 res = tee_svc_cryp_get_state(sess, state, &cs); 2505 if (res != TEE_SUCCESS) 2506 return res; 2507 2508 res = tee_mmu_check_access_rights( 2509 sess->ctx, 2510 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_ANY_OWNER, 2511 (tee_uaddr_t) src_data, src_len); 2512 if (res != TEE_SUCCESS) 2513 return res; 2514 2515 res = tee_svc_copy_from_user(sess, &dlen, dst_len, sizeof(size_t)); 2516 if (res != TEE_SUCCESS) 2517 return res; 2518 2519 res = tee_mmu_check_access_rights( 2520 sess->ctx, 2521 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE | 2522 TEE_MEMORY_ACCESS_ANY_OWNER, 2523 (tee_uaddr_t) dst_data, dlen); 2524 if (res != TEE_SUCCESS) 2525 return res; 2526 2527 res = tee_obj_get(sess->ctx, cs->key1, &o); 2528 if (res != TEE_SUCCESS) 2529 return res; 2530 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 2531 return TEE_ERROR_GENERIC; 2532 2533 switch (cs->algo) { 2534 case TEE_ALG_RSA_NOPAD: 2535 if (cs->mode == TEE_MODE_ENCRYPT) { 2536 if (crypto_ops.acipher.rsanopad_encrypt) 2537 res = crypto_ops.acipher.rsanopad_encrypt( 2538 o->data, src_data, src_len, 2539 dst_data, &dlen); 2540 else 2541 res = TEE_ERROR_NOT_IMPLEMENTED; 2542 } else if (cs->mode == TEE_MODE_DECRYPT) { 2543 if (crypto_ops.acipher.rsanopad_decrypt) 2544 res = crypto_ops.acipher.rsanopad_decrypt( 2545 o->data, src_data, src_len, dst_data, 2546 &dlen); 2547 else 2548 res = TEE_ERROR_NOT_IMPLEMENTED; 2549 } else { 2550 /* 2551 * We will panic because "the mode is not compatible 2552 * with the function" 2553 */ 2554 return TEE_ERROR_GENERIC; 2555 } 2556 break; 2557 2558 case TEE_ALG_RSAES_PKCS1_V1_5: 2559 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1: 2560 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224: 2561 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256: 2562 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384: 2563 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512: 2564 for (n = 0; n < num_params; n++) { 2565 if (params[n].attributeID == TEE_ATTR_RSA_OAEP_LABEL) { 2566 label = params[n].content.ref.buffer; 2567 label_len = params[n].content.ref.length; 2568 break; 2569 } 2570 } 2571 2572 if (cs->mode == TEE_MODE_ENCRYPT) { 2573 if (crypto_ops.acipher.rsaes_encrypt) 2574 res = crypto_ops.acipher.rsaes_encrypt( 2575 cs->algo, o->data, label, label_len, 2576 src_data, src_len, dst_data, &dlen); 2577 else 2578 res = TEE_ERROR_NOT_IMPLEMENTED; 2579 } else if (cs->mode == TEE_MODE_DECRYPT) { 2580 if (crypto_ops.acipher.rsaes_decrypt) 2581 res = crypto_ops.acipher.rsaes_decrypt( 2582 cs->algo, o->data, 2583 label, label_len, 2584 src_data, src_len, dst_data, &dlen); 2585 else 2586 res = TEE_ERROR_NOT_IMPLEMENTED; 2587 } else { 2588 res = TEE_ERROR_BAD_PARAMETERS; 2589 } 2590 break; 2591 2592 case TEE_ALG_RSASSA_PKCS1_V1_5_MD5: 2593 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1: 2594 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224: 2595 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256: 2596 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384: 2597 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512: 2598 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1: 2599 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224: 2600 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256: 2601 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384: 2602 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512: 2603 if (cs->mode != TEE_MODE_SIGN) { 2604 res = TEE_ERROR_BAD_PARAMETERS; 2605 break; 2606 } 2607 tee_svc_asymm_pkcs1_get_salt_len(params, num_params, &salt_len); 2608 2609 if (!crypto_ops.acipher.rsassa_sign) { 2610 res = TEE_ERROR_NOT_IMPLEMENTED; 2611 break; 2612 } 2613 res = crypto_ops.acipher.rsassa_sign(cs->algo, o->data, 2614 salt_len, src_data, 2615 src_len, dst_data, &dlen); 2616 break; 2617 2618 case TEE_ALG_DSA_SHA1: 2619 if (!crypto_ops.acipher.dsa_sign) { 2620 res = TEE_ERROR_NOT_IMPLEMENTED; 2621 break; 2622 } 2623 res = crypto_ops.acipher.dsa_sign(cs->algo, o->data, src_data, 2624 src_len, dst_data, &dlen); 2625 break; 2626 2627 default: 2628 res = TEE_ERROR_BAD_PARAMETERS; 2629 break; 2630 } 2631 2632 if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) { 2633 TEE_Result res2; 2634 2635 res2 = 2636 tee_svc_copy_to_user(sess, dst_len, &dlen, sizeof(size_t)); 2637 if (res2 != TEE_SUCCESS) 2638 return res2; 2639 } 2640 2641 return res; 2642 } 2643 2644 TEE_Result tee_svc_asymm_verify(uint32_t state, const TEE_Attribute *params, 2645 uint32_t num_params, const void *data, 2646 size_t data_len, const void *sig, 2647 size_t sig_len) 2648 { 2649 TEE_Result res; 2650 struct tee_cryp_state *cs; 2651 struct tee_ta_session *sess; 2652 struct tee_obj *o; 2653 size_t hash_size; 2654 int salt_len; 2655 2656 res = tee_ta_get_current_session(&sess); 2657 if (res != TEE_SUCCESS) 2658 return res; 2659 2660 res = tee_svc_cryp_get_state(sess, state, &cs); 2661 if (res != TEE_SUCCESS) 2662 return res; 2663 2664 if (cs->mode != TEE_MODE_VERIFY) 2665 return TEE_ERROR_BAD_PARAMETERS; 2666 2667 res = tee_mmu_check_access_rights(sess->ctx, 2668 TEE_MEMORY_ACCESS_READ | 2669 TEE_MEMORY_ACCESS_ANY_OWNER, 2670 (tee_uaddr_t)data, data_len); 2671 if (res != TEE_SUCCESS) 2672 return res; 2673 2674 res = tee_mmu_check_access_rights(sess->ctx, 2675 TEE_MEMORY_ACCESS_READ | 2676 TEE_MEMORY_ACCESS_ANY_OWNER, 2677 (tee_uaddr_t)sig, sig_len); 2678 if (res != TEE_SUCCESS) 2679 return res; 2680 2681 res = tee_obj_get(sess->ctx, cs->key1, &o); 2682 if (res != TEE_SUCCESS) 2683 return res; 2684 if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 2685 return TEE_ERROR_BAD_PARAMETERS; 2686 2687 res = tee_hash_get_digest_size(TEE_DIGEST_HASH_TO_ALGO(cs->algo), 2688 &hash_size); 2689 if (res != TEE_SUCCESS) 2690 return res; 2691 2692 if (data_len != hash_size) 2693 return TEE_ERROR_BAD_PARAMETERS; 2694 2695 switch (TEE_ALG_GET_MAIN_ALG(cs->algo)) { 2696 case TEE_MAIN_ALGO_RSA: 2697 tee_svc_asymm_pkcs1_get_salt_len(params, num_params, &salt_len); 2698 if (!crypto_ops.acipher.rsassa_verify) { 2699 res = TEE_ERROR_NOT_IMPLEMENTED; 2700 break; 2701 } 2702 res = crypto_ops.acipher.rsassa_verify(cs->algo, o->data, 2703 salt_len, data, 2704 data_len, sig, sig_len); 2705 break; 2706 2707 case TEE_MAIN_ALGO_DSA: 2708 if (!crypto_ops.acipher.dsa_verify) { 2709 res = TEE_ERROR_NOT_IMPLEMENTED; 2710 break; 2711 } 2712 res = crypto_ops.acipher.dsa_verify(cs->algo, o->data, data, 2713 data_len, sig, sig_len); 2714 break; 2715 2716 default: 2717 res = TEE_ERROR_NOT_SUPPORTED; 2718 } 2719 2720 return res; 2721 } 2722