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 <stdlib.h> 28 #include <string.h> 29 #include <string_ext.h> 30 31 #include <tee_api.h> 32 #include <tee_api_defines_extensions.h> 33 #include <tee_internal_api_extensions.h> 34 #include <utee_syscalls.h> 35 #include <utee_defines.h> 36 #include <util.h> 37 #include "tee_api_private.h" 38 39 struct __TEE_OperationHandle { 40 TEE_OperationInfo info; 41 TEE_ObjectHandle key1; 42 TEE_ObjectHandle key2; 43 uint32_t operationState;/* Operation state : INITIAL or ACTIVE */ 44 uint8_t *buffer; /* buffer to collect complete blocks */ 45 bool buffer_two_blocks; /* True if two blocks need to be buffered */ 46 size_t block_size; /* Block size of cipher */ 47 size_t buffer_offs; /* Offset in buffer */ 48 uint32_t state; /* Handle to state in TEE Core */ 49 uint32_t ae_tag_len; /* 50 * tag_len in bytes for AE operation else unused 51 */ 52 }; 53 54 /* Cryptographic Operations API - Generic Operation Functions */ 55 56 TEE_Result TEE_AllocateOperation(TEE_OperationHandle *operation, 57 uint32_t algorithm, uint32_t mode, 58 uint32_t maxKeySize) 59 { 60 TEE_Result res; 61 TEE_OperationHandle op = TEE_HANDLE_NULL; 62 uint32_t handle_state = 0; 63 size_t block_size = 1; 64 uint32_t req_key_usage; 65 bool with_private_key = false; 66 bool buffer_two_blocks = false; 67 68 if (!operation) 69 TEE_Panic(0); 70 71 if (algorithm == TEE_ALG_AES_XTS) 72 handle_state = TEE_HANDLE_FLAG_EXPECT_TWO_KEYS; 73 74 /* Check algorithm max key size */ 75 switch (algorithm) { 76 case TEE_ALG_DSA_SHA1: 77 if (maxKeySize < 512) 78 return TEE_ERROR_NOT_SUPPORTED; 79 if (maxKeySize > 1024) 80 return TEE_ERROR_NOT_SUPPORTED; 81 if (maxKeySize % 64 != 0) 82 return TEE_ERROR_NOT_SUPPORTED; 83 break; 84 85 case TEE_ALG_DSA_SHA224: 86 if (maxKeySize != 2048) 87 return TEE_ERROR_NOT_SUPPORTED; 88 break; 89 90 case TEE_ALG_DSA_SHA256: 91 if (maxKeySize != 2048 && maxKeySize != 3072) 92 return TEE_ERROR_NOT_SUPPORTED; 93 break; 94 95 case TEE_ALG_ECDSA_P192: 96 case TEE_ALG_ECDH_P192: 97 if (maxKeySize != 192) 98 return TEE_ERROR_NOT_SUPPORTED; 99 break; 100 101 case TEE_ALG_ECDSA_P224: 102 case TEE_ALG_ECDH_P224: 103 if (maxKeySize != 224) 104 return TEE_ERROR_NOT_SUPPORTED; 105 break; 106 107 case TEE_ALG_ECDSA_P256: 108 case TEE_ALG_ECDH_P256: 109 if (maxKeySize != 256) 110 return TEE_ERROR_NOT_SUPPORTED; 111 break; 112 113 case TEE_ALG_ECDSA_P384: 114 case TEE_ALG_ECDH_P384: 115 if (maxKeySize != 384) 116 return TEE_ERROR_NOT_SUPPORTED; 117 break; 118 119 case TEE_ALG_ECDSA_P521: 120 case TEE_ALG_ECDH_P521: 121 if (maxKeySize != 521) 122 return TEE_ERROR_NOT_SUPPORTED; 123 break; 124 125 default: 126 break; 127 } 128 129 /* Check algorithm mode */ 130 switch (algorithm) { 131 case TEE_ALG_AES_CTS: 132 case TEE_ALG_AES_XTS: 133 buffer_two_blocks = true; 134 /*FALLTHROUGH*/ case TEE_ALG_AES_ECB_NOPAD: 135 case TEE_ALG_AES_CBC_NOPAD: 136 case TEE_ALG_AES_CTR: 137 case TEE_ALG_AES_CCM: 138 case TEE_ALG_AES_GCM: 139 case TEE_ALG_DES_ECB_NOPAD: 140 case TEE_ALG_DES_CBC_NOPAD: 141 case TEE_ALG_DES3_ECB_NOPAD: 142 case TEE_ALG_DES3_CBC_NOPAD: 143 if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES) 144 block_size = TEE_AES_BLOCK_SIZE; 145 else 146 block_size = TEE_DES_BLOCK_SIZE; 147 148 if (mode == TEE_MODE_ENCRYPT) 149 req_key_usage = TEE_USAGE_ENCRYPT; 150 else if (mode == TEE_MODE_DECRYPT) 151 req_key_usage = TEE_USAGE_DECRYPT; 152 else 153 return TEE_ERROR_NOT_SUPPORTED; 154 break; 155 156 case TEE_ALG_RSASSA_PKCS1_V1_5_MD5: 157 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1: 158 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224: 159 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256: 160 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384: 161 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512: 162 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1: 163 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224: 164 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256: 165 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384: 166 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512: 167 case TEE_ALG_DSA_SHA1: 168 case TEE_ALG_DSA_SHA224: 169 case TEE_ALG_DSA_SHA256: 170 case TEE_ALG_ECDSA_P192: 171 case TEE_ALG_ECDSA_P224: 172 case TEE_ALG_ECDSA_P256: 173 case TEE_ALG_ECDSA_P384: 174 case TEE_ALG_ECDSA_P521: 175 if (mode == TEE_MODE_SIGN) { 176 with_private_key = true; 177 req_key_usage = TEE_USAGE_SIGN; 178 } else if (mode == TEE_MODE_VERIFY) { 179 req_key_usage = TEE_USAGE_VERIFY; 180 } else { 181 return TEE_ERROR_NOT_SUPPORTED; 182 } 183 break; 184 185 case TEE_ALG_RSAES_PKCS1_V1_5: 186 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1: 187 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224: 188 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256: 189 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384: 190 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512: 191 if (mode == TEE_MODE_ENCRYPT) { 192 req_key_usage = TEE_USAGE_ENCRYPT; 193 } else if (mode == TEE_MODE_DECRYPT) { 194 with_private_key = true; 195 req_key_usage = TEE_USAGE_DECRYPT; 196 } else { 197 return TEE_ERROR_NOT_SUPPORTED; 198 } 199 break; 200 201 case TEE_ALG_RSA_NOPAD: 202 if (mode == TEE_MODE_ENCRYPT) { 203 req_key_usage = TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY; 204 } else if (mode == TEE_MODE_DECRYPT) { 205 with_private_key = true; 206 req_key_usage = TEE_USAGE_DECRYPT | TEE_USAGE_SIGN; 207 } else { 208 return TEE_ERROR_NOT_SUPPORTED; 209 } 210 break; 211 212 case TEE_ALG_DH_DERIVE_SHARED_SECRET: 213 case TEE_ALG_ECDH_P192: 214 case TEE_ALG_ECDH_P224: 215 case TEE_ALG_ECDH_P256: 216 case TEE_ALG_ECDH_P384: 217 case TEE_ALG_ECDH_P521: 218 case TEE_ALG_HKDF_MD5_DERIVE_KEY: 219 case TEE_ALG_HKDF_SHA1_DERIVE_KEY: 220 case TEE_ALG_HKDF_SHA224_DERIVE_KEY: 221 case TEE_ALG_HKDF_SHA256_DERIVE_KEY: 222 case TEE_ALG_HKDF_SHA384_DERIVE_KEY: 223 case TEE_ALG_HKDF_SHA512_DERIVE_KEY: 224 case TEE_ALG_CONCAT_KDF_SHA1_DERIVE_KEY: 225 case TEE_ALG_CONCAT_KDF_SHA224_DERIVE_KEY: 226 case TEE_ALG_CONCAT_KDF_SHA256_DERIVE_KEY: 227 case TEE_ALG_CONCAT_KDF_SHA384_DERIVE_KEY: 228 case TEE_ALG_CONCAT_KDF_SHA512_DERIVE_KEY: 229 case TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY: 230 if (mode != TEE_MODE_DERIVE) 231 return TEE_ERROR_NOT_SUPPORTED; 232 with_private_key = true; 233 req_key_usage = TEE_USAGE_DERIVE; 234 break; 235 236 case TEE_ALG_MD5: 237 case TEE_ALG_SHA1: 238 case TEE_ALG_SHA224: 239 case TEE_ALG_SHA256: 240 case TEE_ALG_SHA384: 241 case TEE_ALG_SHA512: 242 if (mode != TEE_MODE_DIGEST) 243 return TEE_ERROR_NOT_SUPPORTED; 244 /* v1.1: flags always set for digest operations */ 245 handle_state |= TEE_HANDLE_FLAG_KEY_SET; 246 req_key_usage = 0; 247 break; 248 249 case TEE_ALG_DES_CBC_MAC_NOPAD: 250 case TEE_ALG_AES_CBC_MAC_NOPAD: 251 case TEE_ALG_AES_CBC_MAC_PKCS5: 252 case TEE_ALG_AES_CMAC: 253 case TEE_ALG_DES_CBC_MAC_PKCS5: 254 case TEE_ALG_DES3_CBC_MAC_NOPAD: 255 case TEE_ALG_DES3_CBC_MAC_PKCS5: 256 case TEE_ALG_HMAC_MD5: 257 case TEE_ALG_HMAC_SHA1: 258 case TEE_ALG_HMAC_SHA224: 259 case TEE_ALG_HMAC_SHA256: 260 case TEE_ALG_HMAC_SHA384: 261 case TEE_ALG_HMAC_SHA512: 262 if (mode != TEE_MODE_MAC) 263 return TEE_ERROR_NOT_SUPPORTED; 264 req_key_usage = TEE_USAGE_MAC; 265 break; 266 267 default: 268 return TEE_ERROR_NOT_SUPPORTED; 269 } 270 271 op = TEE_Malloc(sizeof(*op), TEE_MALLOC_FILL_ZERO); 272 if (!op) 273 return TEE_ERROR_OUT_OF_MEMORY; 274 275 op->info.algorithm = algorithm; 276 op->info.operationClass = TEE_ALG_GET_CLASS(algorithm); 277 op->info.mode = mode; 278 op->info.maxKeySize = maxKeySize; 279 op->info.requiredKeyUsage = req_key_usage; 280 op->info.handleState = handle_state; 281 282 if (block_size > 1) { 283 size_t buffer_size = block_size; 284 285 if (buffer_two_blocks) 286 buffer_size *= 2; 287 288 op->buffer = TEE_Malloc(buffer_size, 289 TEE_USER_MEM_HINT_NO_FILL_ZERO); 290 if (op->buffer == NULL) { 291 res = TEE_ERROR_OUT_OF_MEMORY; 292 goto out; 293 } 294 } 295 op->block_size = block_size; 296 op->buffer_two_blocks = buffer_two_blocks; 297 298 if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) { 299 uint32_t mks = maxKeySize; 300 TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm, 301 with_private_key); 302 303 /* 304 * If two keys are expected the max key size is the sum of 305 * the size of both keys. 306 */ 307 if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) 308 mks /= 2; 309 310 res = TEE_AllocateTransientObject(key_type, mks, &op->key1); 311 if (res != TEE_SUCCESS) 312 goto out; 313 314 if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) { 315 res = TEE_AllocateTransientObject(key_type, mks, 316 &op->key2); 317 if (res != TEE_SUCCESS) 318 goto out; 319 } 320 } 321 322 res = utee_cryp_state_alloc(algorithm, mode, (unsigned long)op->key1, 323 (unsigned long)op->key2, &op->state); 324 if (res != TEE_SUCCESS) 325 goto out; 326 327 /* 328 * Initialize digest operations 329 * Other multi-stage operations initialized w/ TEE_xxxInit functions 330 * Non-applicable on asymmetric operations 331 */ 332 if (TEE_ALG_GET_CLASS(algorithm) == TEE_OPERATION_DIGEST) { 333 res = utee_hash_init(op->state, NULL, 0); 334 if (res != TEE_SUCCESS) 335 goto out; 336 /* v1.1: flags always set for digest operations */ 337 op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 338 } 339 340 op->operationState = TEE_OPERATION_STATE_INITIAL; 341 342 *operation = op; 343 344 out: 345 if (res != TEE_SUCCESS) { 346 if (res != TEE_ERROR_OUT_OF_MEMORY && 347 res != TEE_ERROR_NOT_SUPPORTED) 348 TEE_Panic(0); 349 if (op) { 350 if (op->state) { 351 TEE_FreeOperation(op); 352 } else { 353 TEE_Free(op->buffer); 354 TEE_FreeTransientObject(op->key1); 355 TEE_FreeTransientObject(op->key2); 356 TEE_Free(op); 357 } 358 } 359 } 360 361 return res; 362 } 363 364 void TEE_FreeOperation(TEE_OperationHandle operation) 365 { 366 TEE_Result res; 367 368 if (operation == TEE_HANDLE_NULL) 369 TEE_Panic(0); 370 371 /* 372 * Note that keys should not be freed here, since they are 373 * claimed by the operation they will be freed by 374 * utee_cryp_state_free(). 375 */ 376 res = utee_cryp_state_free(operation->state); 377 if (res != TEE_SUCCESS) 378 TEE_Panic(0); 379 380 TEE_Free(operation->buffer); 381 TEE_Free(operation); 382 } 383 384 void TEE_GetOperationInfo(TEE_OperationHandle operation, 385 TEE_OperationInfo *operationInfo) 386 { 387 if (operation == TEE_HANDLE_NULL) 388 TEE_Panic(0); 389 390 if (!operationInfo) 391 TEE_Panic(0); 392 393 *operationInfo = operation->info; 394 } 395 396 TEE_Result TEE_GetOperationInfoMultiple(TEE_OperationHandle operation, 397 TEE_OperationInfoMultiple *operationInfoMultiple, 398 uint32_t *operationSize) 399 { 400 TEE_Result res = TEE_SUCCESS; 401 TEE_ObjectInfo key_info1; 402 TEE_ObjectInfo key_info2; 403 uint32_t num_of_keys; 404 size_t n; 405 406 if (operation == TEE_HANDLE_NULL) { 407 res = TEE_ERROR_BAD_PARAMETERS; 408 goto out; 409 } 410 411 if (!operationInfoMultiple) { 412 res = TEE_ERROR_BAD_PARAMETERS; 413 goto out; 414 } 415 416 if (!operationSize) { 417 res = TEE_ERROR_BAD_PARAMETERS; 418 goto out; 419 } 420 421 num_of_keys = (*operationSize-sizeof(TEE_OperationInfoMultiple))/ 422 sizeof(TEE_OperationInfoKey); 423 424 if (num_of_keys > 2) { 425 res = TEE_ERROR_BAD_PARAMETERS; 426 goto out; 427 } 428 429 /* Two keys flag (TEE_ALG_AES_XTS only) */ 430 if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 431 0 && 432 (num_of_keys != 2)) { 433 res = TEE_ERROR_SHORT_BUFFER; 434 goto out; 435 } 436 437 /* Clear */ 438 for (n = 0; n < num_of_keys; n++) { 439 operationInfoMultiple->keyInformation[n].keySize = 0; 440 operationInfoMultiple->keyInformation[n].requiredKeyUsage = 0; 441 } 442 443 if (num_of_keys == 2) { 444 res = TEE_GetObjectInfo1(operation->key2, &key_info2); 445 /* Key2 is not a valid handle */ 446 if (res != TEE_SUCCESS) 447 goto out; 448 449 operationInfoMultiple->keyInformation[1].keySize = 450 key_info2.keySize; 451 operationInfoMultiple->keyInformation[1].requiredKeyUsage = 452 operation->info.requiredKeyUsage; 453 } 454 455 if (num_of_keys >= 1) { 456 res = TEE_GetObjectInfo1(operation->key1, &key_info1); 457 /* Key1 is not a valid handle */ 458 if (res != TEE_SUCCESS) { 459 if (num_of_keys == 2) { 460 operationInfoMultiple->keyInformation[1]. 461 keySize = 0; 462 operationInfoMultiple->keyInformation[1]. 463 requiredKeyUsage = 0; 464 } 465 goto out; 466 } 467 468 operationInfoMultiple->keyInformation[0].keySize = 469 key_info1.keySize; 470 operationInfoMultiple->keyInformation[0].requiredKeyUsage = 471 operation->info.requiredKeyUsage; 472 } 473 474 /* No key */ 475 operationInfoMultiple->algorithm = operation->info.algorithm; 476 operationInfoMultiple->operationClass = operation->info.operationClass; 477 operationInfoMultiple->mode = operation->info.mode; 478 operationInfoMultiple->digestLength = operation->info.digestLength; 479 operationInfoMultiple->maxKeySize = operation->info.maxKeySize; 480 operationInfoMultiple->handleState = operation->info.handleState; 481 operationInfoMultiple->operationState = operation->operationState; 482 operationInfoMultiple->numberOfKeys = num_of_keys; 483 484 out: 485 if (res != TEE_SUCCESS && 486 res != TEE_ERROR_SHORT_BUFFER) 487 TEE_Panic(0); 488 489 return res; 490 } 491 492 void TEE_ResetOperation(TEE_OperationHandle operation) 493 { 494 TEE_Result res; 495 496 if (operation == TEE_HANDLE_NULL) 497 TEE_Panic(0); 498 499 if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET)) 500 TEE_Panic(0); 501 502 operation->operationState = TEE_OPERATION_STATE_INITIAL; 503 504 if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 505 res = utee_hash_init(operation->state, NULL, 0); 506 if (res != TEE_SUCCESS) 507 TEE_Panic(res); 508 operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 509 } else { 510 operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 511 } 512 } 513 514 TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation, 515 TEE_ObjectHandle key) 516 { 517 TEE_Result res; 518 uint32_t key_size = 0; 519 TEE_ObjectInfo key_info; 520 521 if (operation == TEE_HANDLE_NULL) { 522 res = TEE_ERROR_BAD_PARAMETERS; 523 goto out; 524 } 525 526 if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 527 res = TEE_ERROR_BAD_PARAMETERS; 528 goto out; 529 } 530 531 if (key == TEE_HANDLE_NULL) { 532 /* Operation key cleared */ 533 TEE_ResetTransientObject(operation->key1); 534 res = TEE_ERROR_BAD_PARAMETERS; 535 goto out; 536 } 537 538 /* No key for digest operation */ 539 if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 540 res = TEE_ERROR_BAD_PARAMETERS; 541 goto out; 542 } 543 544 /* Two keys flag not expected (TEE_ALG_AES_XTS excluded) */ 545 if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 546 0) { 547 res = TEE_ERROR_BAD_PARAMETERS; 548 goto out; 549 } 550 551 res = TEE_GetObjectInfo1(key, &key_info); 552 /* Key is not a valid handle */ 553 if (res != TEE_SUCCESS) 554 goto out; 555 556 /* Supplied key has to meet required usage */ 557 if ((key_info.objectUsage & operation->info.requiredKeyUsage) != 558 operation->info.requiredKeyUsage) { 559 res = TEE_ERROR_BAD_PARAMETERS; 560 goto out; 561 } 562 563 if (operation->info.maxKeySize < key_info.keySize) { 564 res = TEE_ERROR_BAD_PARAMETERS; 565 goto out; 566 } 567 568 key_size = key_info.keySize; 569 570 TEE_ResetTransientObject(operation->key1); 571 operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 572 573 res = TEE_CopyObjectAttributes1(operation->key1, key); 574 if (res != TEE_SUCCESS) 575 goto out; 576 577 operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 578 579 operation->info.keySize = key_size; 580 581 out: 582 if (res != TEE_SUCCESS && 583 res != TEE_ERROR_CORRUPT_OBJECT && 584 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 585 TEE_Panic(0); 586 587 return res; 588 } 589 590 TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation, 591 TEE_ObjectHandle key1, TEE_ObjectHandle key2) 592 { 593 TEE_Result res; 594 uint32_t key_size = 0; 595 TEE_ObjectInfo key_info1; 596 TEE_ObjectInfo key_info2; 597 598 if (operation == TEE_HANDLE_NULL) { 599 res = TEE_ERROR_BAD_PARAMETERS; 600 goto out; 601 } 602 603 if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 604 res = TEE_ERROR_BAD_PARAMETERS; 605 goto out; 606 } 607 608 /* 609 * Key1/Key2 and/or are not initialized and 610 * Either both keys are NULL or both are not NULL 611 */ 612 if (key1 == TEE_HANDLE_NULL || key2 == TEE_HANDLE_NULL) { 613 /* Clear operation key1 (if needed) */ 614 if (key1 == TEE_HANDLE_NULL) 615 TEE_ResetTransientObject(operation->key1); 616 /* Clear operation key2 (if needed) */ 617 if (key2 == TEE_HANDLE_NULL) 618 TEE_ResetTransientObject(operation->key2); 619 res = TEE_ERROR_BAD_PARAMETERS; 620 goto out; 621 } 622 623 /* No key for digest operation */ 624 if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 625 res = TEE_ERROR_BAD_PARAMETERS; 626 goto out; 627 } 628 629 /* Two keys flag expected (TEE_ALG_AES_XTS only) */ 630 if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 631 0) { 632 res = TEE_ERROR_BAD_PARAMETERS; 633 goto out; 634 } 635 636 res = TEE_GetObjectInfo1(key1, &key_info1); 637 /* Key1 is not a valid handle */ 638 if (res != TEE_SUCCESS) 639 goto out; 640 641 /* Supplied key has to meet required usage */ 642 if ((key_info1.objectUsage & operation->info. 643 requiredKeyUsage) != operation->info.requiredKeyUsage) { 644 res = TEE_ERROR_BAD_PARAMETERS; 645 goto out; 646 } 647 648 res = TEE_GetObjectInfo1(key2, &key_info2); 649 /* Key2 is not a valid handle */ 650 if (res != TEE_SUCCESS) { 651 if (res == TEE_ERROR_CORRUPT_OBJECT) 652 res = TEE_ERROR_CORRUPT_OBJECT_2; 653 goto out; 654 } 655 656 /* Supplied key has to meet required usage */ 657 if ((key_info2.objectUsage & operation->info. 658 requiredKeyUsage) != operation->info.requiredKeyUsage) { 659 res = TEE_ERROR_BAD_PARAMETERS; 660 goto out; 661 } 662 663 /* 664 * AES-XTS (the only multi key algorithm supported, requires the 665 * keys to be of equal size. 666 */ 667 if (operation->info.algorithm == TEE_ALG_AES_XTS && 668 key_info1.keySize != key_info2.keySize) { 669 res = TEE_ERROR_BAD_PARAMETERS; 670 goto out; 671 672 } 673 674 if (operation->info.maxKeySize < key_info1.keySize) { 675 res = TEE_ERROR_BAD_PARAMETERS; 676 goto out; 677 } 678 679 /* 680 * Odd that only the size of one key should be reported while 681 * size of two key are used when allocating the operation. 682 */ 683 key_size = key_info1.keySize; 684 685 TEE_ResetTransientObject(operation->key1); 686 TEE_ResetTransientObject(operation->key2); 687 operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 688 689 res = TEE_CopyObjectAttributes1(operation->key1, key1); 690 if (res != TEE_SUCCESS) 691 goto out; 692 693 res = TEE_CopyObjectAttributes1(operation->key2, key2); 694 if (res != TEE_SUCCESS) { 695 if (res == TEE_ERROR_CORRUPT_OBJECT) 696 res = TEE_ERROR_CORRUPT_OBJECT_2; 697 goto out; 698 } 699 700 operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 701 702 operation->info.keySize = key_size; 703 704 out: 705 if (res != TEE_SUCCESS && 706 res != TEE_ERROR_CORRUPT_OBJECT && 707 res != TEE_ERROR_CORRUPT_OBJECT_2 && 708 res != TEE_ERROR_STORAGE_NOT_AVAILABLE && 709 res != TEE_ERROR_STORAGE_NOT_AVAILABLE_2) 710 TEE_Panic(0); 711 712 return res; 713 } 714 715 void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op) 716 { 717 TEE_Result res; 718 719 if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL) 720 TEE_Panic(0); 721 if (dst_op->info.algorithm != src_op->info.algorithm) 722 TEE_Panic(0); 723 if (src_op->info.operationClass != TEE_OPERATION_DIGEST) { 724 TEE_ObjectHandle key1 = TEE_HANDLE_NULL; 725 TEE_ObjectHandle key2 = TEE_HANDLE_NULL; 726 727 if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) { 728 key1 = src_op->key1; 729 key2 = src_op->key2; 730 } 731 732 if ((src_op->info.handleState & 733 TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) { 734 TEE_SetOperationKey(dst_op, key1); 735 } else { 736 TEE_SetOperationKey2(dst_op, key1, key2); 737 } 738 } 739 dst_op->info.handleState = src_op->info.handleState; 740 dst_op->info.keySize = src_op->info.keySize; 741 dst_op->operationState = src_op->operationState; 742 743 if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks || 744 dst_op->block_size != src_op->block_size) 745 TEE_Panic(0); 746 747 if (dst_op->buffer != NULL) { 748 if (src_op->buffer == NULL) 749 TEE_Panic(0); 750 751 memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs); 752 dst_op->buffer_offs = src_op->buffer_offs; 753 } else if (src_op->buffer != NULL) { 754 TEE_Panic(0); 755 } 756 757 res = utee_cryp_state_copy(dst_op->state, src_op->state); 758 if (res != TEE_SUCCESS) 759 TEE_Panic(res); 760 } 761 762 /* Cryptographic Operations API - Message Digest Functions */ 763 764 static void init_hash_operation(TEE_OperationHandle operation, void *IV, 765 uint32_t IVLen) 766 { 767 TEE_Result res; 768 769 /* 770 * Note : IV and IVLen are never used in current implementation 771 * This is why coherent values of IV and IVLen are not checked 772 */ 773 res = utee_hash_init(operation->state, IV, IVLen); 774 if (res != TEE_SUCCESS) 775 TEE_Panic(res); 776 operation->buffer_offs = 0; 777 operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 778 } 779 780 void TEE_DigestUpdate(TEE_OperationHandle operation, 781 void *chunk, uint32_t chunkSize) 782 { 783 TEE_Result res = TEE_ERROR_GENERIC; 784 785 if (operation == TEE_HANDLE_NULL || 786 operation->info.operationClass != TEE_OPERATION_DIGEST) 787 TEE_Panic(0); 788 789 operation->operationState = TEE_OPERATION_STATE_ACTIVE; 790 791 res = utee_hash_update(operation->state, chunk, chunkSize); 792 if (res != TEE_SUCCESS) 793 TEE_Panic(res); 794 } 795 796 TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, void *chunk, 797 uint32_t chunkLen, void *hash, uint32_t *hashLen) 798 { 799 TEE_Result res; 800 uint64_t hl; 801 802 if ((operation == TEE_HANDLE_NULL) || 803 (!chunk && chunkLen) || 804 !hash || 805 !hashLen || 806 (operation->info.operationClass != TEE_OPERATION_DIGEST)) { 807 res = TEE_ERROR_BAD_PARAMETERS; 808 goto out; 809 } 810 811 hl = *hashLen; 812 res = utee_hash_final(operation->state, chunk, chunkLen, hash, &hl); 813 *hashLen = hl; 814 if (res != TEE_SUCCESS) 815 goto out; 816 817 /* Reset operation state */ 818 init_hash_operation(operation, NULL, 0); 819 820 operation->operationState = TEE_OPERATION_STATE_INITIAL; 821 822 out: 823 if (res != TEE_SUCCESS && 824 res != TEE_ERROR_SHORT_BUFFER) 825 TEE_Panic(0); 826 827 return res; 828 } 829 830 /* Cryptographic Operations API - Symmetric Cipher Functions */ 831 832 void TEE_CipherInit(TEE_OperationHandle operation, void *IV, uint32_t IVLen) 833 { 834 TEE_Result res; 835 836 if (operation == TEE_HANDLE_NULL) 837 TEE_Panic(0); 838 839 if (operation->info.operationClass != TEE_OPERATION_CIPHER) 840 TEE_Panic(0); 841 842 if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 843 !(operation->key1)) 844 TEE_Panic(0); 845 846 if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 847 TEE_ResetOperation(operation); 848 849 operation->operationState = TEE_OPERATION_STATE_ACTIVE; 850 851 res = utee_cipher_init(operation->state, IV, IVLen); 852 if (res != TEE_SUCCESS) 853 TEE_Panic(res); 854 855 operation->buffer_offs = 0; 856 operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 857 } 858 859 static TEE_Result tee_buffer_update( 860 TEE_OperationHandle op, 861 TEE_Result(*update_func)(unsigned long state, const void *src, 862 size_t slen, void *dst, uint64_t *dlen), 863 const void *src_data, size_t src_len, 864 void *dest_data, uint64_t *dest_len) 865 { 866 TEE_Result res; 867 const uint8_t *src = src_data; 868 size_t slen = src_len; 869 uint8_t *dst = dest_data; 870 size_t dlen = *dest_len; 871 size_t acc_dlen = 0; 872 uint64_t tmp_dlen; 873 size_t l; 874 size_t buffer_size; 875 size_t buffer_left; 876 877 if (op->buffer_two_blocks) { 878 buffer_size = op->block_size * 2; 879 buffer_left = 1; 880 } else { 881 buffer_size = op->block_size; 882 buffer_left = 0; 883 } 884 885 if (op->buffer_offs > 0) { 886 /* Fill up complete block */ 887 if (op->buffer_offs < op->block_size) 888 l = MIN(slen, op->block_size - op->buffer_offs); 889 else 890 l = MIN(slen, buffer_size - op->buffer_offs); 891 memcpy(op->buffer + op->buffer_offs, src, l); 892 op->buffer_offs += l; 893 src += l; 894 slen -= l; 895 if ((op->buffer_offs % op->block_size) != 0) 896 goto out; /* Nothing left to do */ 897 } 898 899 /* If we can feed from buffer */ 900 if ((op->buffer_offs > 0) && 901 ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) { 902 l = ROUNDUP(op->buffer_offs + slen - buffer_size, 903 op->block_size); 904 l = MIN(op->buffer_offs, l); 905 tmp_dlen = dlen; 906 res = update_func(op->state, op->buffer, l, dst, &tmp_dlen); 907 if (res != TEE_SUCCESS) 908 TEE_Panic(res); 909 dst += tmp_dlen; 910 dlen -= tmp_dlen; 911 acc_dlen += tmp_dlen; 912 op->buffer_offs -= l; 913 if (op->buffer_offs > 0) { 914 /* 915 * Slen is small enough to be contained in rest buffer. 916 */ 917 memcpy(op->buffer, op->buffer + l, buffer_size - l); 918 memcpy(op->buffer + op->buffer_offs, src, slen); 919 op->buffer_offs += slen; 920 goto out; /* Nothing left to do */ 921 } 922 } 923 924 if (slen >= (buffer_size + buffer_left)) { 925 /* Buffer is empty, feed as much as possible from src */ 926 l = ROUNDUP(slen - buffer_size + 1, op->block_size); 927 928 tmp_dlen = dlen; 929 res = update_func(op->state, src, l, dst, &tmp_dlen); 930 if (res != TEE_SUCCESS) 931 TEE_Panic(res); 932 src += l; 933 slen -= l; 934 dst += tmp_dlen; 935 dlen -= tmp_dlen; 936 acc_dlen += tmp_dlen; 937 } 938 939 /* Slen is small enough to be contained in buffer. */ 940 memcpy(op->buffer + op->buffer_offs, src, slen); 941 op->buffer_offs += slen; 942 943 out: 944 *dest_len = acc_dlen; 945 return TEE_SUCCESS; 946 } 947 948 TEE_Result TEE_CipherUpdate(TEE_OperationHandle operation, void *srcData, 949 uint32_t srcLen, void *destData, uint32_t *destLen) 950 { 951 TEE_Result res; 952 size_t req_dlen; 953 uint64_t dl; 954 955 if (operation == TEE_HANDLE_NULL || 956 (srcData == NULL && srcLen != 0) || 957 destLen == NULL || 958 (destData == NULL && *destLen != 0)) { 959 res = TEE_ERROR_BAD_PARAMETERS; 960 goto out; 961 } 962 963 if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 964 res = TEE_ERROR_BAD_PARAMETERS; 965 goto out; 966 } 967 968 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 969 res = TEE_ERROR_BAD_PARAMETERS; 970 goto out; 971 } 972 973 if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 974 res = TEE_ERROR_BAD_PARAMETERS; 975 goto out; 976 } 977 978 if (!srcData && !srcLen) { 979 res = TEE_SUCCESS; 980 goto out; 981 } 982 983 /* Calculate required dlen */ 984 req_dlen = ((operation->buffer_offs + srcLen) / operation->block_size) * 985 operation->block_size; 986 if (operation->buffer_two_blocks) { 987 if (req_dlen > operation->block_size * 2) 988 req_dlen -= operation->block_size * 2; 989 else 990 req_dlen = 0; 991 } 992 /* 993 * Check that required destLen is big enough before starting to feed 994 * data to the algorithm. Errors during feeding of data are fatal as we 995 * can't restore sync with this API. 996 */ 997 if (*destLen < req_dlen) { 998 *destLen = req_dlen; 999 res = TEE_ERROR_SHORT_BUFFER; 1000 goto out; 1001 } 1002 1003 dl = *destLen; 1004 res = tee_buffer_update(operation, utee_cipher_update, srcData, srcLen, 1005 destData, &dl); 1006 *destLen = dl; 1007 1008 out: 1009 if (res != TEE_SUCCESS && 1010 res != TEE_ERROR_SHORT_BUFFER) 1011 TEE_Panic(0); 1012 1013 return res; 1014 } 1015 1016 TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation, 1017 void *srcData, uint32_t srcLen, void *destData, 1018 uint32_t *destLen) 1019 { 1020 TEE_Result res; 1021 uint8_t *dst = destData; 1022 size_t acc_dlen = 0; 1023 uint64_t tmp_dlen; 1024 size_t req_dlen; 1025 1026 if (operation == TEE_HANDLE_NULL || 1027 (srcData == NULL && srcLen != 0) || 1028 destLen == NULL || 1029 (destData == NULL && *destLen != 0)) { 1030 res = TEE_ERROR_BAD_PARAMETERS; 1031 goto out; 1032 } 1033 1034 if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 1035 res = TEE_ERROR_BAD_PARAMETERS; 1036 goto out; 1037 } 1038 1039 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1040 res = TEE_ERROR_BAD_PARAMETERS; 1041 goto out; 1042 } 1043 1044 if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1045 res = TEE_ERROR_BAD_PARAMETERS; 1046 goto out; 1047 } 1048 1049 /* 1050 * Check that the final block doesn't require padding for those 1051 * algorithms that requires client to supply padding. 1052 */ 1053 if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD || 1054 operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD || 1055 operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD || 1056 operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD || 1057 operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || 1058 operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD) { 1059 if (((operation->buffer_offs + srcLen) % operation->block_size) 1060 != 0) { 1061 res = TEE_ERROR_BAD_PARAMETERS; 1062 goto out; 1063 } 1064 } 1065 1066 /* 1067 * Check that required destLen is big enough before starting to feed 1068 * data to the algorithm. Errors during feeding of data are fatal as we 1069 * can't restore sync with this API. 1070 */ 1071 req_dlen = operation->buffer_offs + srcLen; 1072 if (*destLen < req_dlen) { 1073 *destLen = req_dlen; 1074 res = TEE_ERROR_SHORT_BUFFER; 1075 goto out; 1076 } 1077 1078 tmp_dlen = *destLen - acc_dlen; 1079 res = tee_buffer_update(operation, utee_cipher_update, srcData, srcLen, 1080 dst, &tmp_dlen); 1081 if (res != TEE_SUCCESS) 1082 goto out; 1083 1084 dst += tmp_dlen; 1085 acc_dlen += tmp_dlen; 1086 1087 tmp_dlen = *destLen - acc_dlen; 1088 res = utee_cipher_final(operation->state, operation->buffer, 1089 operation->buffer_offs, dst, &tmp_dlen); 1090 if (res != TEE_SUCCESS) 1091 goto out; 1092 1093 acc_dlen += tmp_dlen; 1094 *destLen = acc_dlen; 1095 1096 operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1097 1098 operation->operationState = TEE_OPERATION_STATE_INITIAL; 1099 1100 out: 1101 if (res != TEE_SUCCESS && 1102 res != TEE_ERROR_SHORT_BUFFER) 1103 TEE_Panic(0); 1104 1105 return res; 1106 } 1107 1108 /* Cryptographic Operations API - MAC Functions */ 1109 1110 void TEE_MACInit(TEE_OperationHandle operation, void *IV, uint32_t IVLen) 1111 { 1112 if (operation == TEE_HANDLE_NULL) 1113 TEE_Panic(0); 1114 1115 if (operation->info.operationClass != TEE_OPERATION_MAC) 1116 TEE_Panic(0); 1117 1118 if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 1119 !(operation->key1)) 1120 TEE_Panic(0); 1121 1122 if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 1123 TEE_ResetOperation(operation); 1124 1125 operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1126 1127 init_hash_operation(operation, IV, IVLen); 1128 } 1129 1130 void TEE_MACUpdate(TEE_OperationHandle operation, void *chunk, 1131 uint32_t chunkSize) 1132 { 1133 TEE_Result res; 1134 1135 if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0)) 1136 TEE_Panic(0); 1137 1138 if (operation->info.operationClass != TEE_OPERATION_MAC) 1139 TEE_Panic(0); 1140 1141 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1142 TEE_Panic(0); 1143 1144 if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) 1145 TEE_Panic(0); 1146 1147 res = utee_hash_update(operation->state, chunk, chunkSize); 1148 if (res != TEE_SUCCESS) 1149 TEE_Panic(res); 1150 } 1151 1152 TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation, 1153 void *message, uint32_t messageLen, 1154 void *mac, uint32_t *macLen) 1155 { 1156 TEE_Result res; 1157 uint64_t ml; 1158 1159 if (operation == TEE_HANDLE_NULL || 1160 (message == NULL && messageLen != 0) || 1161 mac == NULL || 1162 macLen == NULL) { 1163 res = TEE_ERROR_BAD_PARAMETERS; 1164 goto out; 1165 } 1166 1167 if (operation->info.operationClass != TEE_OPERATION_MAC) { 1168 res = TEE_ERROR_BAD_PARAMETERS; 1169 goto out; 1170 } 1171 1172 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1173 res = TEE_ERROR_BAD_PARAMETERS; 1174 goto out; 1175 } 1176 1177 if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1178 res = TEE_ERROR_BAD_PARAMETERS; 1179 goto out; 1180 } 1181 1182 ml = *macLen; 1183 res = utee_hash_final(operation->state, message, messageLen, mac, &ml); 1184 *macLen = ml; 1185 if (res != TEE_SUCCESS) 1186 goto out; 1187 1188 operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1189 1190 operation->operationState = TEE_OPERATION_STATE_INITIAL; 1191 1192 out: 1193 if (res != TEE_SUCCESS && 1194 res != TEE_ERROR_SHORT_BUFFER) 1195 TEE_Panic(res); 1196 1197 return res; 1198 } 1199 1200 TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation, 1201 void *message, uint32_t messageLen, 1202 void *mac, uint32_t macLen) 1203 { 1204 TEE_Result res; 1205 uint8_t computed_mac[TEE_MAX_HASH_SIZE]; 1206 uint32_t computed_mac_size = TEE_MAX_HASH_SIZE; 1207 1208 if (operation->info.operationClass != TEE_OPERATION_MAC) { 1209 res = TEE_ERROR_BAD_PARAMETERS; 1210 goto out; 1211 } 1212 1213 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1214 res = TEE_ERROR_BAD_PARAMETERS; 1215 goto out; 1216 } 1217 1218 if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1219 res = TEE_ERROR_BAD_PARAMETERS; 1220 goto out; 1221 } 1222 1223 res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac, 1224 &computed_mac_size); 1225 if (res != TEE_SUCCESS) 1226 goto out; 1227 1228 if (computed_mac_size != macLen) { 1229 res = TEE_ERROR_MAC_INVALID; 1230 goto out; 1231 } 1232 1233 if (buf_compare_ct(mac, computed_mac, computed_mac_size) != 0) { 1234 res = TEE_ERROR_MAC_INVALID; 1235 goto out; 1236 } 1237 1238 operation->operationState = TEE_OPERATION_STATE_INITIAL; 1239 1240 out: 1241 if (res != TEE_SUCCESS && 1242 res != TEE_ERROR_MAC_INVALID) 1243 TEE_Panic(res); 1244 1245 return res; 1246 } 1247 1248 /* Cryptographic Operations API - Authenticated Encryption Functions */ 1249 1250 TEE_Result TEE_AEInit(TEE_OperationHandle operation, void *nonce, 1251 uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen, 1252 uint32_t payloadLen) 1253 { 1254 TEE_Result res; 1255 1256 if (operation == TEE_HANDLE_NULL || nonce == NULL) { 1257 res = TEE_ERROR_BAD_PARAMETERS; 1258 goto out; 1259 } 1260 1261 if (operation->info.operationClass != TEE_OPERATION_AE) { 1262 res = TEE_ERROR_BAD_PARAMETERS; 1263 goto out; 1264 } 1265 1266 if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 1267 res = TEE_ERROR_BAD_PARAMETERS; 1268 goto out; 1269 } 1270 1271 /* 1272 * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core 1273 * in the implementation. But AES-GCM spec doesn't specify the tag len 1274 * according to the same principle so we have to check here instead to 1275 * be GP compliant. 1276 */ 1277 if (operation->info.algorithm == TEE_ALG_AES_GCM) { 1278 /* 1279 * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96 1280 */ 1281 if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) { 1282 res = TEE_ERROR_NOT_SUPPORTED; 1283 goto out; 1284 } 1285 } 1286 1287 res = utee_authenc_init(operation->state, nonce, nonceLen, 1288 tagLen / 8, AADLen, payloadLen); 1289 if (res != TEE_SUCCESS) 1290 goto out; 1291 1292 operation->ae_tag_len = tagLen / 8; 1293 operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 1294 1295 out: 1296 if (res != TEE_SUCCESS && 1297 res != TEE_ERROR_NOT_SUPPORTED) 1298 TEE_Panic(res); 1299 1300 return res; 1301 } 1302 1303 void TEE_AEUpdateAAD(TEE_OperationHandle operation, void *AADdata, 1304 uint32_t AADdataLen) 1305 { 1306 TEE_Result res; 1307 1308 if (operation == TEE_HANDLE_NULL || 1309 (AADdata == NULL && AADdataLen != 0)) 1310 TEE_Panic(0); 1311 1312 if (operation->info.operationClass != TEE_OPERATION_AE) 1313 TEE_Panic(0); 1314 1315 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1316 TEE_Panic(0); 1317 1318 res = utee_authenc_update_aad(operation->state, AADdata, AADdataLen); 1319 1320 operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1321 1322 if (res != TEE_SUCCESS) 1323 TEE_Panic(res); 1324 } 1325 1326 TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, void *srcData, 1327 uint32_t srcLen, void *destData, uint32_t *destLen) 1328 { 1329 TEE_Result res; 1330 size_t req_dlen; 1331 uint64_t dl; 1332 1333 if (operation == TEE_HANDLE_NULL || 1334 (srcData == NULL && srcLen != 0) || 1335 destLen == NULL || 1336 (destData == NULL && *destLen != 0)) { 1337 res = TEE_ERROR_BAD_PARAMETERS; 1338 goto out; 1339 } 1340 1341 if (operation->info.operationClass != TEE_OPERATION_AE) { 1342 res = TEE_ERROR_BAD_PARAMETERS; 1343 goto out; 1344 } 1345 1346 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1347 res = TEE_ERROR_BAD_PARAMETERS; 1348 goto out; 1349 } 1350 1351 /* 1352 * Check that required destLen is big enough before starting to feed 1353 * data to the algorithm. Errors during feeding of data are fatal as we 1354 * can't restore sync with this API. 1355 */ 1356 req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen, 1357 operation->block_size); 1358 if (*destLen < req_dlen) { 1359 *destLen = req_dlen; 1360 res = TEE_ERROR_SHORT_BUFFER; 1361 goto out; 1362 } 1363 1364 dl = *destLen; 1365 res = tee_buffer_update(operation, utee_authenc_update_payload, srcData, 1366 srcLen, destData, &dl); 1367 *destLen = dl; 1368 1369 operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1370 1371 out: 1372 if (res != TEE_SUCCESS && 1373 res != TEE_ERROR_SHORT_BUFFER) 1374 TEE_Panic(res); 1375 1376 return res; 1377 } 1378 1379 TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation, 1380 void *srcData, uint32_t srcLen, 1381 void *destData, uint32_t *destLen, void *tag, 1382 uint32_t *tagLen) 1383 { 1384 TEE_Result res; 1385 uint8_t *dst = destData; 1386 size_t acc_dlen = 0; 1387 uint64_t tmp_dlen; 1388 size_t req_dlen; 1389 uint64_t tl; 1390 1391 if (operation == TEE_HANDLE_NULL || 1392 (srcData == NULL && srcLen != 0) || 1393 destLen == NULL || 1394 (destData == NULL && *destLen != 0) || 1395 tag == NULL || tagLen == NULL) { 1396 res = TEE_ERROR_BAD_PARAMETERS; 1397 goto out; 1398 } 1399 1400 if (operation->info.operationClass != TEE_OPERATION_AE) { 1401 res = TEE_ERROR_BAD_PARAMETERS; 1402 goto out; 1403 } 1404 1405 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1406 res = TEE_ERROR_BAD_PARAMETERS; 1407 goto out; 1408 } 1409 1410 /* 1411 * Check that required destLen is big enough before starting to feed 1412 * data to the algorithm. Errors during feeding of data are fatal as we 1413 * can't restore sync with this API. 1414 */ 1415 req_dlen = operation->buffer_offs + srcLen; 1416 if (*destLen < req_dlen) { 1417 *destLen = req_dlen; 1418 res = TEE_ERROR_SHORT_BUFFER; 1419 goto out; 1420 } 1421 1422 /* 1423 * Need to check this before update_payload since sync would be lost if 1424 * we return short buffer after that. 1425 */ 1426 if (*tagLen < operation->ae_tag_len) { 1427 *tagLen = operation->ae_tag_len; 1428 res = TEE_ERROR_SHORT_BUFFER; 1429 goto out; 1430 } 1431 1432 tmp_dlen = *destLen - acc_dlen; 1433 res = tee_buffer_update(operation, utee_authenc_update_payload, srcData, 1434 srcLen, dst, &tmp_dlen); 1435 if (res != TEE_SUCCESS) 1436 goto out; 1437 1438 dst += tmp_dlen; 1439 acc_dlen += tmp_dlen; 1440 1441 tmp_dlen = *destLen - acc_dlen; 1442 tl = *tagLen; 1443 res = utee_authenc_enc_final(operation->state, operation->buffer, 1444 operation->buffer_offs, dst, &tmp_dlen, 1445 tag, &tl); 1446 *tagLen = tl; 1447 if (res != TEE_SUCCESS) 1448 goto out; 1449 1450 acc_dlen += tmp_dlen; 1451 *destLen = acc_dlen; 1452 1453 operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1454 1455 operation->operationState = TEE_OPERATION_STATE_INITIAL; 1456 1457 out: 1458 if (res != TEE_SUCCESS && 1459 res != TEE_ERROR_SHORT_BUFFER) 1460 TEE_Panic(res); 1461 1462 return res; 1463 } 1464 1465 TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation, 1466 void *srcData, uint32_t srcLen, 1467 void *destData, uint32_t *destLen, void *tag, 1468 uint32_t tagLen) 1469 { 1470 TEE_Result res; 1471 uint8_t *dst = destData; 1472 size_t acc_dlen = 0; 1473 uint64_t tmp_dlen; 1474 size_t req_dlen; 1475 1476 if (operation == TEE_HANDLE_NULL || 1477 (srcData == NULL && srcLen != 0) || 1478 destLen == NULL || 1479 (destData == NULL && *destLen != 0) || 1480 (tag == NULL && tagLen != 0)) { 1481 res = TEE_ERROR_BAD_PARAMETERS; 1482 goto out; 1483 } 1484 1485 if (operation->info.operationClass != TEE_OPERATION_AE) { 1486 res = TEE_ERROR_BAD_PARAMETERS; 1487 goto out; 1488 } 1489 1490 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1491 res = TEE_ERROR_BAD_PARAMETERS; 1492 goto out; 1493 } 1494 1495 /* 1496 * Check that required destLen is big enough before starting to feed 1497 * data to the algorithm. Errors during feeding of data are fatal as we 1498 * can't restore sync with this API. 1499 */ 1500 req_dlen = operation->buffer_offs + srcLen; 1501 if (*destLen < req_dlen) { 1502 *destLen = req_dlen; 1503 res = TEE_ERROR_SHORT_BUFFER; 1504 goto out; 1505 } 1506 1507 tmp_dlen = *destLen - acc_dlen; 1508 res = tee_buffer_update(operation, utee_authenc_update_payload, srcData, 1509 srcLen, dst, &tmp_dlen); 1510 if (res != TEE_SUCCESS) 1511 goto out; 1512 1513 dst += tmp_dlen; 1514 acc_dlen += tmp_dlen; 1515 1516 tmp_dlen = *destLen - acc_dlen; 1517 res = utee_authenc_dec_final(operation->state, operation->buffer, 1518 operation->buffer_offs, dst, &tmp_dlen, 1519 tag, tagLen); 1520 if (res != TEE_SUCCESS) 1521 goto out; 1522 1523 /* Supplied tagLen should match what we initiated with */ 1524 if (tagLen != operation->ae_tag_len) 1525 res = TEE_ERROR_MAC_INVALID; 1526 1527 acc_dlen += tmp_dlen; 1528 *destLen = acc_dlen; 1529 1530 operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1531 1532 operation->operationState = TEE_OPERATION_STATE_INITIAL; 1533 1534 out: 1535 if (res != TEE_SUCCESS && 1536 res != TEE_ERROR_SHORT_BUFFER && 1537 res != TEE_ERROR_MAC_INVALID) 1538 TEE_Panic(res); 1539 1540 return res; 1541 } 1542 1543 /* Cryptographic Operations API - Asymmetric Functions */ 1544 1545 TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation, 1546 TEE_Attribute *params, 1547 uint32_t paramCount, void *srcData, 1548 uint32_t srcLen, void *destData, 1549 uint32_t *destLen) 1550 { 1551 TEE_Result res; 1552 struct utee_attribute ua[paramCount]; 1553 uint64_t dl; 1554 1555 if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1556 destLen == NULL || (destData == NULL && *destLen != 0)) 1557 TEE_Panic(0); 1558 if (params == NULL && paramCount != 0) 1559 TEE_Panic(0); 1560 if (!operation->key1) 1561 TEE_Panic(0); 1562 if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 1563 TEE_Panic(0); 1564 if (operation->info.mode != TEE_MODE_ENCRYPT) 1565 TEE_Panic(0); 1566 1567 __utee_from_attr(ua, params, paramCount); 1568 dl = *destLen; 1569 res = utee_asymm_operate(operation->state, ua, paramCount, srcData, 1570 srcLen, destData, &dl); 1571 *destLen = dl; 1572 1573 if (res != TEE_SUCCESS && 1574 res != TEE_ERROR_SHORT_BUFFER && 1575 res != TEE_ERROR_BAD_PARAMETERS) 1576 TEE_Panic(res); 1577 1578 return res; 1579 } 1580 1581 TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation, 1582 TEE_Attribute *params, 1583 uint32_t paramCount, void *srcData, 1584 uint32_t srcLen, void *destData, 1585 uint32_t *destLen) 1586 { 1587 TEE_Result res; 1588 struct utee_attribute ua[paramCount]; 1589 uint64_t dl; 1590 1591 if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1592 destLen == NULL || (destData == NULL && *destLen != 0)) 1593 TEE_Panic(0); 1594 if (params == NULL && paramCount != 0) 1595 TEE_Panic(0); 1596 if (!operation->key1) 1597 TEE_Panic(0); 1598 if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 1599 TEE_Panic(0); 1600 if (operation->info.mode != TEE_MODE_DECRYPT) 1601 TEE_Panic(0); 1602 1603 __utee_from_attr(ua, params, paramCount); 1604 dl = *destLen; 1605 res = utee_asymm_operate(operation->state, ua, paramCount, srcData, 1606 srcLen, destData, &dl); 1607 *destLen = dl; 1608 1609 if (res != TEE_SUCCESS && 1610 res != TEE_ERROR_SHORT_BUFFER && 1611 res != TEE_ERROR_BAD_PARAMETERS) 1612 TEE_Panic(res); 1613 1614 return res; 1615 } 1616 1617 TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation, 1618 TEE_Attribute *params, 1619 uint32_t paramCount, void *digest, 1620 uint32_t digestLen, void *signature, 1621 uint32_t *signatureLen) 1622 { 1623 TEE_Result res; 1624 struct utee_attribute ua[paramCount]; 1625 uint64_t sl; 1626 1627 if (operation == TEE_HANDLE_NULL || 1628 (digest == NULL && digestLen != 0) || 1629 signature == NULL || signatureLen == NULL) 1630 TEE_Panic(0); 1631 if (params == NULL && paramCount != 0) 1632 TEE_Panic(0); 1633 if (!operation->key1) 1634 TEE_Panic(0); 1635 if (operation->info.operationClass != 1636 TEE_OPERATION_ASYMMETRIC_SIGNATURE) 1637 TEE_Panic(0); 1638 if (operation->info.mode != TEE_MODE_SIGN) 1639 TEE_Panic(0); 1640 1641 __utee_from_attr(ua, params, paramCount); 1642 sl = *signatureLen; 1643 res = utee_asymm_operate(operation->state, ua, paramCount, digest, 1644 digestLen, signature, &sl); 1645 *signatureLen = sl; 1646 1647 if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER) 1648 TEE_Panic(res); 1649 1650 return res; 1651 } 1652 1653 TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation, 1654 TEE_Attribute *params, 1655 uint32_t paramCount, void *digest, 1656 uint32_t digestLen, void *signature, 1657 uint32_t signatureLen) 1658 { 1659 TEE_Result res; 1660 struct utee_attribute ua[paramCount]; 1661 1662 if (operation == TEE_HANDLE_NULL || 1663 (digest == NULL && digestLen != 0) || 1664 (signature == NULL && signatureLen != 0)) 1665 TEE_Panic(0); 1666 if (params == NULL && paramCount != 0) 1667 TEE_Panic(0); 1668 if (!operation->key1) 1669 TEE_Panic(0); 1670 if (operation->info.operationClass != 1671 TEE_OPERATION_ASYMMETRIC_SIGNATURE) 1672 TEE_Panic(0); 1673 if (operation->info.mode != TEE_MODE_VERIFY) 1674 TEE_Panic(0); 1675 1676 __utee_from_attr(ua, params, paramCount); 1677 res = utee_asymm_verify(operation->state, ua, paramCount, digest, 1678 digestLen, signature, signatureLen); 1679 1680 if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID) 1681 TEE_Panic(res); 1682 1683 return res; 1684 } 1685 1686 /* Cryptographic Operations API - Key Derivation Functions */ 1687 1688 void TEE_DeriveKey(TEE_OperationHandle operation, 1689 const TEE_Attribute *params, uint32_t paramCount, 1690 TEE_ObjectHandle derivedKey) 1691 { 1692 TEE_Result res; 1693 TEE_ObjectInfo key_info; 1694 struct utee_attribute ua[paramCount]; 1695 1696 if (operation == TEE_HANDLE_NULL || derivedKey == 0) 1697 TEE_Panic(0); 1698 if (params == NULL && paramCount != 0) 1699 TEE_Panic(0); 1700 if (TEE_ALG_GET_CLASS(operation->info.algorithm) != 1701 TEE_OPERATION_KEY_DERIVATION) 1702 TEE_Panic(0); 1703 1704 if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION) 1705 TEE_Panic(0); 1706 if (!operation->key1) 1707 TEE_Panic(0); 1708 if (operation->info.mode != TEE_MODE_DERIVE) 1709 TEE_Panic(0); 1710 if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0) 1711 TEE_Panic(0); 1712 1713 res = utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info); 1714 if (res != TEE_SUCCESS) 1715 TEE_Panic(0); 1716 1717 if (key_info.objectType != TEE_TYPE_GENERIC_SECRET) 1718 TEE_Panic(0); 1719 if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1720 TEE_Panic(0); 1721 1722 __utee_from_attr(ua, params, paramCount); 1723 res = utee_cryp_derive_key(operation->state, ua, paramCount, 1724 (unsigned long)derivedKey); 1725 if (res != TEE_SUCCESS) 1726 TEE_Panic(res); 1727 } 1728 1729 /* Cryptographic Operations API - Random Number Generation Functions */ 1730 1731 void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen) 1732 { 1733 TEE_Result res; 1734 1735 res = utee_cryp_random_number_generate(randomBuffer, randomBufferLen); 1736 if (res != TEE_SUCCESS) 1737 TEE_Panic(res); 1738 } 1739