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