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