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