1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 */ 5 #include <config.h> 6 #include <stdlib.h> 7 #include <string.h> 8 #include <string_ext.h> 9 #include <tee_api.h> 10 #include <tee_api_defines_extensions.h> 11 #include <tee_internal_api_extensions.h> 12 #include <utee_syscalls.h> 13 #include <utee_defines.h> 14 #include <util.h> 15 #include "tee_api_private.h" 16 17 struct __TEE_OperationHandle { 18 TEE_OperationInfo info; 19 TEE_ObjectHandle key1; 20 TEE_ObjectHandle key2; 21 uint32_t operationState;/* Operation state : INITIAL or ACTIVE */ 22 uint8_t *buffer; /* buffer to collect complete blocks */ 23 bool buffer_two_blocks; /* True if two blocks need to be buffered */ 24 size_t block_size; /* Block size of cipher */ 25 size_t buffer_offs; /* Offset in buffer */ 26 uint32_t state; /* Handle to state in TEE Core */ 27 }; 28 29 /* Cryptographic Operations API - Generic Operation Functions */ 30 31 TEE_Result TEE_AllocateOperation(TEE_OperationHandle *operation, 32 uint32_t algorithm, uint32_t mode, 33 uint32_t maxKeySize) 34 { 35 TEE_Result res; 36 TEE_OperationHandle op = TEE_HANDLE_NULL; 37 uint32_t handle_state = 0; 38 size_t block_size = 1; 39 uint32_t req_key_usage; 40 bool with_private_key = false; 41 bool buffer_two_blocks = false; 42 43 if (!operation) 44 TEE_Panic(0); 45 46 if (algorithm == TEE_ALG_AES_XTS || algorithm == TEE_ALG_SM2_KEP) 47 handle_state = TEE_HANDLE_FLAG_EXPECT_TWO_KEYS; 48 49 /* Check algorithm max key size */ 50 switch (algorithm) { 51 case TEE_ALG_DSA_SHA1: 52 if (maxKeySize < 512) 53 return TEE_ERROR_NOT_SUPPORTED; 54 if (maxKeySize > 1024) 55 return TEE_ERROR_NOT_SUPPORTED; 56 if (maxKeySize % 64 != 0) 57 return TEE_ERROR_NOT_SUPPORTED; 58 break; 59 60 case TEE_ALG_DSA_SHA224: 61 if (maxKeySize != 2048) 62 return TEE_ERROR_NOT_SUPPORTED; 63 break; 64 65 case TEE_ALG_DSA_SHA256: 66 if (maxKeySize != 2048 && maxKeySize != 3072) 67 return TEE_ERROR_NOT_SUPPORTED; 68 break; 69 70 case TEE_ALG_ECDSA_P192: 71 case TEE_ALG_ECDH_P192: 72 if (maxKeySize != 192) 73 return TEE_ERROR_NOT_SUPPORTED; 74 break; 75 76 case TEE_ALG_ECDSA_P224: 77 case TEE_ALG_ECDH_P224: 78 if (maxKeySize != 224) 79 return TEE_ERROR_NOT_SUPPORTED; 80 break; 81 82 case TEE_ALG_ECDSA_P256: 83 case TEE_ALG_ECDH_P256: 84 case TEE_ALG_SM2_PKE: 85 case TEE_ALG_SM2_DSA_SM3: 86 if (maxKeySize != 256) 87 return TEE_ERROR_NOT_SUPPORTED; 88 break; 89 90 case TEE_ALG_SM2_KEP: 91 /* Two 256-bit keys */ 92 if (maxKeySize != 512) 93 return TEE_ERROR_NOT_SUPPORTED; 94 break; 95 96 case TEE_ALG_ECDSA_P384: 97 case TEE_ALG_ECDH_P384: 98 if (maxKeySize != 384) 99 return TEE_ERROR_NOT_SUPPORTED; 100 break; 101 102 case TEE_ALG_ECDSA_P521: 103 case TEE_ALG_ECDH_P521: 104 if (maxKeySize != 521) 105 return TEE_ERROR_NOT_SUPPORTED; 106 break; 107 108 default: 109 break; 110 } 111 112 /* Check algorithm mode */ 113 switch (algorithm) { 114 case TEE_ALG_AES_CTS: 115 case TEE_ALG_AES_XTS: 116 buffer_two_blocks = true; 117 /* FALLTHROUGH */ 118 case TEE_ALG_AES_ECB_NOPAD: 119 case TEE_ALG_AES_CBC_NOPAD: 120 case TEE_ALG_AES_CCM: 121 case TEE_ALG_DES_ECB_NOPAD: 122 case TEE_ALG_DES_CBC_NOPAD: 123 case TEE_ALG_DES3_ECB_NOPAD: 124 case TEE_ALG_DES3_CBC_NOPAD: 125 case TEE_ALG_SM4_ECB_NOPAD: 126 case TEE_ALG_SM4_CBC_NOPAD: 127 case TEE_ALG_SM4_CTR: 128 if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES) 129 block_size = TEE_AES_BLOCK_SIZE; 130 else if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_SM4) 131 block_size = TEE_SM4_BLOCK_SIZE; 132 else 133 block_size = TEE_DES_BLOCK_SIZE; 134 /* FALLTHROUGH */ 135 case TEE_ALG_AES_CTR: 136 case TEE_ALG_AES_GCM: 137 if (mode == TEE_MODE_ENCRYPT) 138 req_key_usage = TEE_USAGE_ENCRYPT; 139 else if (mode == TEE_MODE_DECRYPT) 140 req_key_usage = TEE_USAGE_DECRYPT; 141 else 142 return TEE_ERROR_NOT_SUPPORTED; 143 break; 144 145 #if defined(CFG_CRYPTO_RSASSA_NA1) 146 case TEE_ALG_RSASSA_PKCS1_V1_5: 147 #endif 148 case TEE_ALG_RSASSA_PKCS1_V1_5_MD5: 149 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1: 150 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224: 151 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256: 152 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384: 153 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512: 154 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1: 155 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224: 156 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256: 157 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384: 158 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512: 159 case TEE_ALG_DSA_SHA1: 160 case TEE_ALG_DSA_SHA224: 161 case TEE_ALG_DSA_SHA256: 162 case TEE_ALG_ECDSA_P192: 163 case TEE_ALG_ECDSA_P224: 164 case TEE_ALG_ECDSA_P256: 165 case TEE_ALG_ECDSA_P384: 166 case TEE_ALG_ECDSA_P521: 167 case TEE_ALG_SM2_DSA_SM3: 168 if (mode == TEE_MODE_SIGN) { 169 with_private_key = true; 170 req_key_usage = TEE_USAGE_SIGN; 171 } else if (mode == TEE_MODE_VERIFY) { 172 req_key_usage = TEE_USAGE_VERIFY; 173 } else { 174 return TEE_ERROR_NOT_SUPPORTED; 175 } 176 break; 177 178 case TEE_ALG_RSAES_PKCS1_V1_5: 179 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1: 180 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224: 181 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256: 182 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384: 183 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512: 184 case TEE_ALG_SM2_PKE: 185 if (mode == TEE_MODE_ENCRYPT) { 186 req_key_usage = TEE_USAGE_ENCRYPT; 187 } else if (mode == TEE_MODE_DECRYPT) { 188 with_private_key = true; 189 req_key_usage = TEE_USAGE_DECRYPT; 190 } else { 191 return TEE_ERROR_NOT_SUPPORTED; 192 } 193 break; 194 195 case TEE_ALG_RSA_NOPAD: 196 if (mode == TEE_MODE_ENCRYPT) { 197 req_key_usage = TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY; 198 } else if (mode == TEE_MODE_DECRYPT) { 199 with_private_key = true; 200 req_key_usage = TEE_USAGE_DECRYPT | TEE_USAGE_SIGN; 201 } else { 202 return TEE_ERROR_NOT_SUPPORTED; 203 } 204 break; 205 206 case TEE_ALG_DH_DERIVE_SHARED_SECRET: 207 case TEE_ALG_ECDH_P192: 208 case TEE_ALG_ECDH_P224: 209 case TEE_ALG_ECDH_P256: 210 case TEE_ALG_ECDH_P384: 211 case TEE_ALG_ECDH_P521: 212 case TEE_ALG_HKDF_MD5_DERIVE_KEY: 213 case TEE_ALG_HKDF_SHA1_DERIVE_KEY: 214 case TEE_ALG_HKDF_SHA224_DERIVE_KEY: 215 case TEE_ALG_HKDF_SHA256_DERIVE_KEY: 216 case TEE_ALG_HKDF_SHA384_DERIVE_KEY: 217 case TEE_ALG_HKDF_SHA512_DERIVE_KEY: 218 case TEE_ALG_CONCAT_KDF_SHA1_DERIVE_KEY: 219 case TEE_ALG_CONCAT_KDF_SHA224_DERIVE_KEY: 220 case TEE_ALG_CONCAT_KDF_SHA256_DERIVE_KEY: 221 case TEE_ALG_CONCAT_KDF_SHA384_DERIVE_KEY: 222 case TEE_ALG_CONCAT_KDF_SHA512_DERIVE_KEY: 223 case TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY: 224 case TEE_ALG_SM2_KEP: 225 if (mode != TEE_MODE_DERIVE) 226 return TEE_ERROR_NOT_SUPPORTED; 227 with_private_key = true; 228 req_key_usage = TEE_USAGE_DERIVE; 229 break; 230 231 case TEE_ALG_MD5: 232 case TEE_ALG_SHA1: 233 case TEE_ALG_SHA224: 234 case TEE_ALG_SHA256: 235 case TEE_ALG_SHA384: 236 case TEE_ALG_SHA512: 237 case TEE_ALG_SM3: 238 if (mode != TEE_MODE_DIGEST) 239 return TEE_ERROR_NOT_SUPPORTED; 240 /* v1.1: flags always set for digest operations */ 241 handle_state |= TEE_HANDLE_FLAG_KEY_SET; 242 req_key_usage = 0; 243 break; 244 245 case TEE_ALG_DES_CBC_MAC_NOPAD: 246 case TEE_ALG_AES_CBC_MAC_NOPAD: 247 case TEE_ALG_AES_CBC_MAC_PKCS5: 248 case TEE_ALG_AES_CMAC: 249 case TEE_ALG_DES_CBC_MAC_PKCS5: 250 case TEE_ALG_DES3_CBC_MAC_NOPAD: 251 case TEE_ALG_DES3_CBC_MAC_PKCS5: 252 case TEE_ALG_HMAC_MD5: 253 case TEE_ALG_HMAC_SHA1: 254 case TEE_ALG_HMAC_SHA224: 255 case TEE_ALG_HMAC_SHA256: 256 case TEE_ALG_HMAC_SHA384: 257 case TEE_ALG_HMAC_SHA512: 258 case TEE_ALG_HMAC_SM3: 259 if (mode != TEE_MODE_MAC) 260 return TEE_ERROR_NOT_SUPPORTED; 261 req_key_usage = TEE_USAGE_MAC; 262 break; 263 264 default: 265 return TEE_ERROR_NOT_SUPPORTED; 266 } 267 268 op = TEE_Malloc(sizeof(*op), TEE_MALLOC_FILL_ZERO); 269 if (!op) 270 return TEE_ERROR_OUT_OF_MEMORY; 271 272 op->info.algorithm = algorithm; 273 op->info.operationClass = TEE_ALG_GET_CLASS(algorithm); 274 #ifdef CFG_CRYPTO_RSASSA_NA1 275 if (algorithm == TEE_ALG_RSASSA_PKCS1_V1_5) 276 op->info.operationClass = TEE_OPERATION_ASYMMETRIC_SIGNATURE; 277 #endif 278 op->info.mode = mode; 279 op->info.digestLength = TEE_ALG_GET_DIGEST_SIZE(algorithm); 280 op->info.maxKeySize = maxKeySize; 281 op->info.requiredKeyUsage = req_key_usage; 282 op->info.handleState = handle_state; 283 284 if (block_size > 1) { 285 size_t buffer_size = block_size; 286 287 if (buffer_two_blocks) 288 buffer_size *= 2; 289 290 op->buffer = TEE_Malloc(buffer_size, 291 TEE_USER_MEM_HINT_NO_FILL_ZERO); 292 if (op->buffer == NULL) { 293 res = TEE_ERROR_OUT_OF_MEMORY; 294 goto out; 295 } 296 } 297 op->block_size = block_size; 298 op->buffer_two_blocks = buffer_two_blocks; 299 300 if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) { 301 uint32_t mks = maxKeySize; 302 TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm, 303 with_private_key); 304 305 /* 306 * If two keys are expected the max key size is the sum of 307 * the size of both keys. 308 */ 309 if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) 310 mks /= 2; 311 312 res = TEE_AllocateTransientObject(key_type, mks, &op->key1); 313 if (res != TEE_SUCCESS) 314 goto out; 315 316 if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) { 317 res = TEE_AllocateTransientObject(key_type, mks, 318 &op->key2); 319 if (res != TEE_SUCCESS) 320 goto out; 321 } 322 } 323 324 res = utee_cryp_state_alloc(algorithm, mode, (unsigned long)op->key1, 325 (unsigned long)op->key2, &op->state); 326 if (res != TEE_SUCCESS) 327 goto out; 328 329 /* 330 * Initialize digest operations 331 * Other multi-stage operations initialized w/ TEE_xxxInit functions 332 * Non-applicable on asymmetric operations 333 */ 334 if (TEE_ALG_GET_CLASS(algorithm) == TEE_OPERATION_DIGEST) { 335 res = utee_hash_init(op->state, NULL, 0); 336 if (res != TEE_SUCCESS) 337 goto out; 338 /* v1.1: flags always set for digest operations */ 339 op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 340 } 341 342 op->operationState = TEE_OPERATION_STATE_INITIAL; 343 344 *operation = op; 345 346 out: 347 if (res != TEE_SUCCESS) { 348 if (res != TEE_ERROR_OUT_OF_MEMORY && 349 res != TEE_ERROR_NOT_SUPPORTED) 350 TEE_Panic(res); 351 if (op) { 352 if (op->state) { 353 TEE_FreeOperation(op); 354 } else { 355 TEE_Free(op->buffer); 356 TEE_FreeTransientObject(op->key1); 357 TEE_FreeTransientObject(op->key2); 358 TEE_Free(op); 359 } 360 } 361 } 362 363 return res; 364 } 365 366 void TEE_FreeOperation(TEE_OperationHandle operation) 367 { 368 TEE_Result res; 369 370 if (operation == TEE_HANDLE_NULL) 371 TEE_Panic(0); 372 373 /* 374 * Note that keys should not be freed here, since they are 375 * claimed by the operation they will be freed by 376 * utee_cryp_state_free(). 377 */ 378 res = utee_cryp_state_free(operation->state); 379 if (res != TEE_SUCCESS) 380 TEE_Panic(res); 381 382 TEE_Free(operation->buffer); 383 TEE_Free(operation); 384 } 385 386 void TEE_GetOperationInfo(TEE_OperationHandle operation, 387 TEE_OperationInfo *operationInfo) 388 { 389 if (operation == TEE_HANDLE_NULL) 390 TEE_Panic(0); 391 392 if (!operationInfo) 393 TEE_Panic(0); 394 395 *operationInfo = operation->info; 396 } 397 398 TEE_Result TEE_GetOperationInfoMultiple(TEE_OperationHandle operation, 399 TEE_OperationInfoMultiple *operationInfoMultiple, 400 uint32_t *operationSize) 401 { 402 TEE_Result res = TEE_SUCCESS; 403 TEE_ObjectInfo key_info1; 404 TEE_ObjectInfo key_info2; 405 uint32_t num_of_keys; 406 size_t n; 407 408 if (operation == TEE_HANDLE_NULL) { 409 res = TEE_ERROR_BAD_PARAMETERS; 410 goto out; 411 } 412 413 if (!operationInfoMultiple) { 414 res = TEE_ERROR_BAD_PARAMETERS; 415 goto out; 416 } 417 418 if (!operationSize) { 419 res = TEE_ERROR_BAD_PARAMETERS; 420 goto out; 421 } 422 423 num_of_keys = (*operationSize-sizeof(TEE_OperationInfoMultiple))/ 424 sizeof(TEE_OperationInfoKey); 425 426 if (num_of_keys > 2) { 427 res = TEE_ERROR_BAD_PARAMETERS; 428 goto out; 429 } 430 431 /* Two keys flag (TEE_ALG_AES_XTS only) */ 432 if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 433 0 && 434 (num_of_keys != 2)) { 435 res = TEE_ERROR_SHORT_BUFFER; 436 goto out; 437 } 438 439 /* Clear */ 440 for (n = 0; n < num_of_keys; n++) { 441 operationInfoMultiple->keyInformation[n].keySize = 0; 442 operationInfoMultiple->keyInformation[n].requiredKeyUsage = 0; 443 } 444 445 if (num_of_keys == 2) { 446 res = TEE_GetObjectInfo1(operation->key2, &key_info2); 447 /* Key2 is not a valid handle */ 448 if (res != TEE_SUCCESS) 449 goto out; 450 451 operationInfoMultiple->keyInformation[1].keySize = 452 key_info2.keySize; 453 operationInfoMultiple->keyInformation[1].requiredKeyUsage = 454 operation->info.requiredKeyUsage; 455 } 456 457 if (num_of_keys >= 1) { 458 res = TEE_GetObjectInfo1(operation->key1, &key_info1); 459 /* Key1 is not a valid handle */ 460 if (res != TEE_SUCCESS) { 461 if (num_of_keys == 2) { 462 operationInfoMultiple->keyInformation[1]. 463 keySize = 0; 464 operationInfoMultiple->keyInformation[1]. 465 requiredKeyUsage = 0; 466 } 467 goto out; 468 } 469 470 operationInfoMultiple->keyInformation[0].keySize = 471 key_info1.keySize; 472 operationInfoMultiple->keyInformation[0].requiredKeyUsage = 473 operation->info.requiredKeyUsage; 474 } 475 476 /* No key */ 477 operationInfoMultiple->algorithm = operation->info.algorithm; 478 operationInfoMultiple->operationClass = operation->info.operationClass; 479 operationInfoMultiple->mode = operation->info.mode; 480 operationInfoMultiple->digestLength = operation->info.digestLength; 481 operationInfoMultiple->maxKeySize = operation->info.maxKeySize; 482 operationInfoMultiple->handleState = operation->info.handleState; 483 operationInfoMultiple->operationState = operation->operationState; 484 operationInfoMultiple->numberOfKeys = num_of_keys; 485 486 out: 487 if (res != TEE_SUCCESS && 488 res != TEE_ERROR_SHORT_BUFFER) 489 TEE_Panic(res); 490 491 return res; 492 } 493 494 void TEE_ResetOperation(TEE_OperationHandle operation) 495 { 496 TEE_Result res; 497 498 if (operation == TEE_HANDLE_NULL) 499 TEE_Panic(0); 500 501 if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET)) 502 TEE_Panic(0); 503 504 operation->operationState = TEE_OPERATION_STATE_INITIAL; 505 506 if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 507 res = utee_hash_init(operation->state, NULL, 0); 508 if (res != TEE_SUCCESS) 509 TEE_Panic(res); 510 operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 511 } else { 512 operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 513 } 514 } 515 516 TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation, 517 TEE_ObjectHandle key) 518 { 519 TEE_Result res; 520 uint32_t key_size = 0; 521 TEE_ObjectInfo key_info; 522 523 if (operation == TEE_HANDLE_NULL) { 524 res = TEE_ERROR_BAD_PARAMETERS; 525 goto out; 526 } 527 528 if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 529 res = TEE_ERROR_BAD_PARAMETERS; 530 goto out; 531 } 532 533 if (key == TEE_HANDLE_NULL) { 534 /* Operation key cleared */ 535 TEE_ResetTransientObject(operation->key1); 536 res = TEE_ERROR_BAD_PARAMETERS; 537 goto out; 538 } 539 540 /* No key for digest operation */ 541 if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 542 res = TEE_ERROR_BAD_PARAMETERS; 543 goto out; 544 } 545 546 /* Two keys flag not expected (TEE_ALG_AES_XTS excluded) */ 547 if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 548 0) { 549 res = TEE_ERROR_BAD_PARAMETERS; 550 goto out; 551 } 552 553 res = TEE_GetObjectInfo1(key, &key_info); 554 /* Key is not a valid handle */ 555 if (res != TEE_SUCCESS) 556 goto out; 557 558 /* Supplied key has to meet required usage */ 559 if ((key_info.objectUsage & operation->info.requiredKeyUsage) != 560 operation->info.requiredKeyUsage) { 561 res = TEE_ERROR_BAD_PARAMETERS; 562 goto out; 563 } 564 565 if (operation->info.maxKeySize < key_info.keySize) { 566 res = TEE_ERROR_BAD_PARAMETERS; 567 goto out; 568 } 569 570 key_size = key_info.keySize; 571 572 TEE_ResetTransientObject(operation->key1); 573 operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 574 575 res = TEE_CopyObjectAttributes1(operation->key1, key); 576 if (res != TEE_SUCCESS) 577 goto out; 578 579 operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 580 581 operation->info.keySize = key_size; 582 583 out: 584 if (res != TEE_SUCCESS && 585 res != TEE_ERROR_CORRUPT_OBJECT && 586 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 587 TEE_Panic(res); 588 589 return res; 590 } 591 592 TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation, 593 TEE_ObjectHandle key1, TEE_ObjectHandle key2) 594 { 595 TEE_Result res; 596 uint32_t key_size = 0; 597 TEE_ObjectInfo key_info1; 598 TEE_ObjectInfo key_info2; 599 600 if (operation == TEE_HANDLE_NULL) { 601 res = TEE_ERROR_BAD_PARAMETERS; 602 goto out; 603 } 604 605 if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 606 res = TEE_ERROR_BAD_PARAMETERS; 607 goto out; 608 } 609 610 /* 611 * Key1/Key2 and/or are not initialized and 612 * Either both keys are NULL or both are not NULL 613 */ 614 if (key1 == TEE_HANDLE_NULL || key2 == TEE_HANDLE_NULL) { 615 /* Clear operation key1 (if needed) */ 616 if (key1 == TEE_HANDLE_NULL) 617 TEE_ResetTransientObject(operation->key1); 618 /* Clear operation key2 (if needed) */ 619 if (key2 == TEE_HANDLE_NULL) 620 TEE_ResetTransientObject(operation->key2); 621 res = TEE_ERROR_BAD_PARAMETERS; 622 goto out; 623 } 624 625 /* No key for digest operation */ 626 if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 627 res = TEE_ERROR_BAD_PARAMETERS; 628 goto out; 629 } 630 631 /* Two keys flag expected (TEE_ALG_AES_XTS and TEE_ALG_SM2_KEP only) */ 632 if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 633 0) { 634 res = TEE_ERROR_BAD_PARAMETERS; 635 goto out; 636 } 637 638 res = TEE_GetObjectInfo1(key1, &key_info1); 639 /* Key1 is not a valid handle */ 640 if (res != TEE_SUCCESS) 641 goto out; 642 643 /* Supplied key has to meet required usage */ 644 if ((key_info1.objectUsage & operation->info. 645 requiredKeyUsage) != operation->info.requiredKeyUsage) { 646 res = TEE_ERROR_BAD_PARAMETERS; 647 goto out; 648 } 649 650 res = TEE_GetObjectInfo1(key2, &key_info2); 651 /* Key2 is not a valid handle */ 652 if (res != TEE_SUCCESS) { 653 if (res == TEE_ERROR_CORRUPT_OBJECT) 654 res = TEE_ERROR_CORRUPT_OBJECT_2; 655 goto out; 656 } 657 658 /* Supplied key has to meet required usage */ 659 if ((key_info2.objectUsage & operation->info. 660 requiredKeyUsage) != operation->info.requiredKeyUsage) { 661 res = TEE_ERROR_BAD_PARAMETERS; 662 goto out; 663 } 664 665 /* 666 * All the multi key algorithm currently supported requires the keys to 667 * be of equal size. 668 */ 669 if (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 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(res); 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, const 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 const 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, const 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(res); 826 827 return res; 828 } 829 830 /* Cryptographic Operations API - Symmetric Cipher Functions */ 831 832 void TEE_CipherInit(TEE_OperationHandle operation, const void *IV, 833 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 if (op->info.algorithm == TEE_ALG_AES_CTS) 934 l = ROUNDUP(slen - buffer_size, op->block_size); 935 else 936 l = ROUNDUP(slen - buffer_size + 1, op->block_size); 937 938 tmp_dlen = dlen; 939 res = update_func(op->state, src, l, dst, &tmp_dlen); 940 if (res != TEE_SUCCESS) 941 TEE_Panic(res); 942 src += l; 943 slen -= l; 944 dst += tmp_dlen; 945 dlen -= tmp_dlen; 946 acc_dlen += tmp_dlen; 947 } 948 949 /* Slen is small enough to be contained in buffer. */ 950 memcpy(op->buffer + op->buffer_offs, src, slen); 951 op->buffer_offs += slen; 952 953 out: 954 *dest_len = acc_dlen; 955 return TEE_SUCCESS; 956 } 957 958 TEE_Result TEE_CipherUpdate(TEE_OperationHandle operation, const void *srcData, 959 uint32_t srcLen, void *destData, uint32_t *destLen) 960 { 961 TEE_Result res; 962 size_t req_dlen; 963 uint64_t dl; 964 965 if (operation == TEE_HANDLE_NULL || 966 (srcData == NULL && srcLen != 0) || 967 destLen == NULL || 968 (destData == NULL && *destLen != 0)) { 969 res = TEE_ERROR_BAD_PARAMETERS; 970 goto out; 971 } 972 973 if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 974 res = TEE_ERROR_BAD_PARAMETERS; 975 goto out; 976 } 977 978 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 979 res = TEE_ERROR_BAD_PARAMETERS; 980 goto out; 981 } 982 983 if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 984 res = TEE_ERROR_BAD_PARAMETERS; 985 goto out; 986 } 987 988 if (!srcData && !srcLen) { 989 *destLen = 0; 990 res = TEE_SUCCESS; 991 goto out; 992 } 993 994 /* Calculate required dlen */ 995 if (operation->block_size > 1) { 996 req_dlen = ((operation->buffer_offs + srcLen) / 997 operation->block_size) * operation->block_size; 998 } else { 999 req_dlen = srcLen; 1000 } 1001 if (operation->buffer_two_blocks) { 1002 if (req_dlen > operation->block_size * 2) 1003 req_dlen -= operation->block_size * 2; 1004 else 1005 req_dlen = 0; 1006 } 1007 /* 1008 * Check that required destLen is big enough before starting to feed 1009 * data to the algorithm. Errors during feeding of data are fatal as we 1010 * can't restore sync with this API. 1011 */ 1012 if (*destLen < req_dlen) { 1013 *destLen = req_dlen; 1014 res = TEE_ERROR_SHORT_BUFFER; 1015 goto out; 1016 } 1017 1018 dl = *destLen; 1019 if (operation->block_size > 1) { 1020 res = tee_buffer_update(operation, utee_cipher_update, srcData, 1021 srcLen, destData, &dl); 1022 } else { 1023 if (srcLen > 0) { 1024 res = utee_cipher_update(operation->state, srcData, 1025 srcLen, destData, &dl); 1026 } else { 1027 res = TEE_SUCCESS; 1028 dl = 0; 1029 } 1030 } 1031 *destLen = dl; 1032 1033 out: 1034 if (res != TEE_SUCCESS && 1035 res != TEE_ERROR_SHORT_BUFFER) 1036 TEE_Panic(res); 1037 1038 return res; 1039 } 1040 1041 TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation, 1042 const void *srcData, uint32_t srcLen, 1043 void *destData, uint32_t *destLen) 1044 { 1045 TEE_Result res; 1046 uint8_t *dst = destData; 1047 size_t acc_dlen = 0; 1048 uint64_t tmp_dlen; 1049 size_t req_dlen; 1050 1051 if (operation == TEE_HANDLE_NULL || 1052 (srcData == NULL && srcLen != 0) || 1053 destLen == NULL || 1054 (destData == NULL && *destLen != 0)) { 1055 res = TEE_ERROR_BAD_PARAMETERS; 1056 goto out; 1057 } 1058 1059 if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 1060 res = TEE_ERROR_BAD_PARAMETERS; 1061 goto out; 1062 } 1063 1064 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1065 res = TEE_ERROR_BAD_PARAMETERS; 1066 goto out; 1067 } 1068 1069 if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1070 res = TEE_ERROR_BAD_PARAMETERS; 1071 goto out; 1072 } 1073 1074 /* 1075 * Check that the final block doesn't require padding for those 1076 * algorithms that requires client to supply padding. 1077 */ 1078 if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD || 1079 operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD || 1080 operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD || 1081 operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD || 1082 operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || 1083 operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD || 1084 operation->info.algorithm == TEE_ALG_SM4_ECB_NOPAD || 1085 operation->info.algorithm == TEE_ALG_SM4_CBC_NOPAD) { 1086 if (((operation->buffer_offs + srcLen) % operation->block_size) 1087 != 0) { 1088 res = TEE_ERROR_BAD_PARAMETERS; 1089 goto out; 1090 } 1091 } 1092 1093 /* 1094 * Check that required destLen is big enough before starting to feed 1095 * data to the algorithm. Errors during feeding of data are fatal as we 1096 * can't restore sync with this API. 1097 */ 1098 if (operation->block_size > 1) { 1099 req_dlen = operation->buffer_offs + srcLen; 1100 } else { 1101 req_dlen = srcLen; 1102 } 1103 if (*destLen < req_dlen) { 1104 *destLen = req_dlen; 1105 res = TEE_ERROR_SHORT_BUFFER; 1106 goto out; 1107 } 1108 1109 tmp_dlen = *destLen - acc_dlen; 1110 if (operation->block_size > 1) { 1111 res = tee_buffer_update(operation, utee_cipher_update, 1112 srcData, srcLen, dst, &tmp_dlen); 1113 if (res != TEE_SUCCESS) 1114 goto out; 1115 1116 dst += tmp_dlen; 1117 acc_dlen += tmp_dlen; 1118 1119 tmp_dlen = *destLen - acc_dlen; 1120 res = utee_cipher_final(operation->state, operation->buffer, 1121 operation->buffer_offs, dst, &tmp_dlen); 1122 } else { 1123 res = utee_cipher_final(operation->state, srcData, 1124 srcLen, dst, &tmp_dlen); 1125 } 1126 if (res != TEE_SUCCESS) 1127 goto out; 1128 1129 acc_dlen += tmp_dlen; 1130 *destLen = acc_dlen; 1131 1132 operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1133 1134 operation->operationState = TEE_OPERATION_STATE_INITIAL; 1135 1136 out: 1137 if (res != TEE_SUCCESS && 1138 res != TEE_ERROR_SHORT_BUFFER) 1139 TEE_Panic(res); 1140 1141 return res; 1142 } 1143 1144 /* Cryptographic Operations API - MAC Functions */ 1145 1146 void TEE_MACInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen) 1147 { 1148 if (operation == TEE_HANDLE_NULL) 1149 TEE_Panic(0); 1150 1151 if (operation->info.operationClass != TEE_OPERATION_MAC) 1152 TEE_Panic(0); 1153 1154 if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 1155 !(operation->key1)) 1156 TEE_Panic(0); 1157 1158 if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 1159 TEE_ResetOperation(operation); 1160 1161 operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1162 1163 init_hash_operation(operation, IV, IVLen); 1164 } 1165 1166 void TEE_MACUpdate(TEE_OperationHandle operation, const void *chunk, 1167 uint32_t chunkSize) 1168 { 1169 TEE_Result res; 1170 1171 if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0)) 1172 TEE_Panic(0); 1173 1174 if (operation->info.operationClass != TEE_OPERATION_MAC) 1175 TEE_Panic(0); 1176 1177 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1178 TEE_Panic(0); 1179 1180 if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) 1181 TEE_Panic(0); 1182 1183 res = utee_hash_update(operation->state, chunk, chunkSize); 1184 if (res != TEE_SUCCESS) 1185 TEE_Panic(res); 1186 } 1187 1188 TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation, 1189 const void *message, uint32_t messageLen, 1190 void *mac, uint32_t *macLen) 1191 { 1192 TEE_Result res; 1193 uint64_t ml; 1194 1195 if (operation == TEE_HANDLE_NULL || 1196 (message == NULL && messageLen != 0) || 1197 mac == NULL || 1198 macLen == NULL) { 1199 res = TEE_ERROR_BAD_PARAMETERS; 1200 goto out; 1201 } 1202 1203 if (operation->info.operationClass != TEE_OPERATION_MAC) { 1204 res = TEE_ERROR_BAD_PARAMETERS; 1205 goto out; 1206 } 1207 1208 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1209 res = TEE_ERROR_BAD_PARAMETERS; 1210 goto out; 1211 } 1212 1213 if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1214 res = TEE_ERROR_BAD_PARAMETERS; 1215 goto out; 1216 } 1217 1218 ml = *macLen; 1219 res = utee_hash_final(operation->state, message, messageLen, mac, &ml); 1220 *macLen = ml; 1221 if (res != TEE_SUCCESS) 1222 goto out; 1223 1224 operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1225 1226 operation->operationState = TEE_OPERATION_STATE_INITIAL; 1227 1228 out: 1229 if (res != TEE_SUCCESS && 1230 res != TEE_ERROR_SHORT_BUFFER) 1231 TEE_Panic(res); 1232 1233 return res; 1234 } 1235 1236 TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation, 1237 const void *message, uint32_t messageLen, 1238 const void *mac, uint32_t macLen) 1239 { 1240 TEE_Result res; 1241 uint8_t computed_mac[TEE_MAX_HASH_SIZE]; 1242 uint32_t computed_mac_size = TEE_MAX_HASH_SIZE; 1243 1244 if (operation->info.operationClass != TEE_OPERATION_MAC) { 1245 res = TEE_ERROR_BAD_PARAMETERS; 1246 goto out; 1247 } 1248 1249 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1250 res = TEE_ERROR_BAD_PARAMETERS; 1251 goto out; 1252 } 1253 1254 if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1255 res = TEE_ERROR_BAD_PARAMETERS; 1256 goto out; 1257 } 1258 1259 res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac, 1260 &computed_mac_size); 1261 if (res != TEE_SUCCESS) 1262 goto out; 1263 1264 if (computed_mac_size != macLen) { 1265 res = TEE_ERROR_MAC_INVALID; 1266 goto out; 1267 } 1268 1269 if (consttime_memcmp(mac, computed_mac, computed_mac_size) != 0) { 1270 res = TEE_ERROR_MAC_INVALID; 1271 goto out; 1272 } 1273 1274 operation->operationState = TEE_OPERATION_STATE_INITIAL; 1275 1276 out: 1277 if (res != TEE_SUCCESS && 1278 res != TEE_ERROR_MAC_INVALID) 1279 TEE_Panic(res); 1280 1281 return res; 1282 } 1283 1284 /* Cryptographic Operations API - Authenticated Encryption Functions */ 1285 1286 TEE_Result TEE_AEInit(TEE_OperationHandle operation, const void *nonce, 1287 uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen, 1288 uint32_t payloadLen) 1289 { 1290 TEE_Result res; 1291 1292 if (operation == TEE_HANDLE_NULL || nonce == NULL) { 1293 res = TEE_ERROR_BAD_PARAMETERS; 1294 goto out; 1295 } 1296 1297 if (operation->info.operationClass != TEE_OPERATION_AE) { 1298 res = TEE_ERROR_BAD_PARAMETERS; 1299 goto out; 1300 } 1301 1302 if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 1303 res = TEE_ERROR_BAD_PARAMETERS; 1304 goto out; 1305 } 1306 1307 /* 1308 * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core 1309 * in the implementation. But AES-GCM spec doesn't specify the tag len 1310 * according to the same principle so we have to check here instead to 1311 * be GP compliant. 1312 */ 1313 if (operation->info.algorithm == TEE_ALG_AES_GCM) { 1314 /* 1315 * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96 1316 */ 1317 if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) { 1318 res = TEE_ERROR_NOT_SUPPORTED; 1319 goto out; 1320 } 1321 } 1322 1323 res = utee_authenc_init(operation->state, nonce, nonceLen, 1324 tagLen / 8, AADLen, payloadLen); 1325 if (res != TEE_SUCCESS) 1326 goto out; 1327 1328 operation->info.digestLength = tagLen / 8; 1329 operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 1330 1331 out: 1332 if (res != TEE_SUCCESS && 1333 res != TEE_ERROR_NOT_SUPPORTED) 1334 TEE_Panic(res); 1335 1336 return res; 1337 } 1338 1339 void TEE_AEUpdateAAD(TEE_OperationHandle operation, const void *AADdata, 1340 uint32_t AADdataLen) 1341 { 1342 TEE_Result res; 1343 1344 if (operation == TEE_HANDLE_NULL || 1345 (AADdata == NULL && AADdataLen != 0)) 1346 TEE_Panic(0); 1347 1348 if (operation->info.operationClass != TEE_OPERATION_AE) 1349 TEE_Panic(0); 1350 1351 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1352 TEE_Panic(0); 1353 1354 res = utee_authenc_update_aad(operation->state, AADdata, AADdataLen); 1355 1356 operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1357 1358 if (res != TEE_SUCCESS) 1359 TEE_Panic(res); 1360 } 1361 1362 TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, const void *srcData, 1363 uint32_t srcLen, void *destData, uint32_t *destLen) 1364 { 1365 TEE_Result res; 1366 size_t req_dlen; 1367 uint64_t dl; 1368 1369 if (operation == TEE_HANDLE_NULL || 1370 (srcData == NULL && srcLen != 0) || 1371 destLen == NULL || 1372 (destData == NULL && *destLen != 0)) { 1373 res = TEE_ERROR_BAD_PARAMETERS; 1374 goto out; 1375 } 1376 1377 if (operation->info.operationClass != TEE_OPERATION_AE) { 1378 res = TEE_ERROR_BAD_PARAMETERS; 1379 goto out; 1380 } 1381 1382 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1383 res = TEE_ERROR_BAD_PARAMETERS; 1384 goto out; 1385 } 1386 1387 if (!srcData && !srcLen) { 1388 *destLen = 0; 1389 res = TEE_SUCCESS; 1390 goto out; 1391 } 1392 1393 /* 1394 * Check that required destLen is big enough before starting to feed 1395 * data to the algorithm. Errors during feeding of data are fatal as we 1396 * can't restore sync with this API. 1397 */ 1398 if (operation->block_size > 1) { 1399 req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen, 1400 operation->block_size); 1401 } else { 1402 req_dlen = srcLen; 1403 } 1404 1405 if (*destLen < req_dlen) { 1406 *destLen = req_dlen; 1407 res = TEE_ERROR_SHORT_BUFFER; 1408 goto out; 1409 } 1410 1411 dl = *destLen; 1412 if (operation->block_size > 1) { 1413 res = tee_buffer_update(operation, utee_authenc_update_payload, 1414 srcData, srcLen, destData, &dl); 1415 } else { 1416 if (srcLen > 0) { 1417 res = utee_authenc_update_payload(operation->state, 1418 srcData, srcLen, 1419 destData, &dl); 1420 } else { 1421 dl = 0; 1422 res = TEE_SUCCESS; 1423 } 1424 } 1425 if (res != TEE_SUCCESS) 1426 goto out; 1427 1428 *destLen = dl; 1429 1430 operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1431 1432 out: 1433 if (res != TEE_SUCCESS && 1434 res != TEE_ERROR_SHORT_BUFFER) 1435 TEE_Panic(res); 1436 1437 return res; 1438 } 1439 1440 TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation, 1441 const void *srcData, uint32_t srcLen, 1442 void *destData, uint32_t *destLen, void *tag, 1443 uint32_t *tagLen) 1444 { 1445 TEE_Result res; 1446 uint8_t *dst = destData; 1447 size_t acc_dlen = 0; 1448 uint64_t tmp_dlen; 1449 size_t req_dlen; 1450 uint64_t tl; 1451 1452 if (operation == TEE_HANDLE_NULL || 1453 (srcData == NULL && srcLen != 0) || 1454 destLen == NULL || 1455 (destData == NULL && *destLen != 0) || 1456 tag == NULL || tagLen == NULL) { 1457 res = TEE_ERROR_BAD_PARAMETERS; 1458 goto out; 1459 } 1460 1461 if (operation->info.operationClass != TEE_OPERATION_AE) { 1462 res = TEE_ERROR_BAD_PARAMETERS; 1463 goto out; 1464 } 1465 1466 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1467 res = TEE_ERROR_BAD_PARAMETERS; 1468 goto out; 1469 } 1470 1471 /* 1472 * Check that required destLen is big enough before starting to feed 1473 * data to the algorithm. Errors during feeding of data are fatal as we 1474 * can't restore sync with this API. 1475 * 1476 * Need to check this before update_payload since sync would be lost if 1477 * we return short buffer after that. 1478 */ 1479 res = TEE_ERROR_GENERIC; 1480 1481 req_dlen = operation->buffer_offs + srcLen; 1482 if (*destLen < req_dlen) { 1483 *destLen = req_dlen; 1484 res = TEE_ERROR_SHORT_BUFFER; 1485 } 1486 1487 if (*tagLen < operation->info.digestLength) { 1488 *tagLen = operation->info.digestLength; 1489 res = TEE_ERROR_SHORT_BUFFER; 1490 } 1491 1492 if (res == TEE_ERROR_SHORT_BUFFER) 1493 goto out; 1494 1495 tl = *tagLen; 1496 tmp_dlen = *destLen - acc_dlen; 1497 if (operation->block_size > 1) { 1498 res = tee_buffer_update(operation, utee_authenc_update_payload, 1499 srcData, srcLen, dst, &tmp_dlen); 1500 if (res != TEE_SUCCESS) 1501 goto out; 1502 1503 dst += tmp_dlen; 1504 acc_dlen += tmp_dlen; 1505 1506 tmp_dlen = *destLen - acc_dlen; 1507 res = utee_authenc_enc_final(operation->state, 1508 operation->buffer, 1509 operation->buffer_offs, dst, 1510 &tmp_dlen, tag, &tl); 1511 } else { 1512 res = utee_authenc_enc_final(operation->state, srcData, 1513 srcLen, dst, &tmp_dlen, 1514 tag, &tl); 1515 } 1516 *tagLen = tl; 1517 if (res != TEE_SUCCESS) 1518 goto out; 1519 1520 acc_dlen += tmp_dlen; 1521 *destLen = acc_dlen; 1522 1523 operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1524 1525 operation->operationState = TEE_OPERATION_STATE_INITIAL; 1526 1527 out: 1528 if (res != TEE_SUCCESS && 1529 res != TEE_ERROR_SHORT_BUFFER) 1530 TEE_Panic(res); 1531 1532 return res; 1533 } 1534 1535 TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation, 1536 const void *srcData, uint32_t srcLen, 1537 void *destData, uint32_t *destLen, void *tag, 1538 uint32_t tagLen) 1539 { 1540 TEE_Result res; 1541 uint8_t *dst = destData; 1542 size_t acc_dlen = 0; 1543 uint64_t tmp_dlen; 1544 size_t req_dlen; 1545 1546 if (operation == TEE_HANDLE_NULL || 1547 (srcData == NULL && srcLen != 0) || 1548 destLen == NULL || 1549 (destData == NULL && *destLen != 0) || 1550 (tag == NULL && tagLen != 0)) { 1551 res = TEE_ERROR_BAD_PARAMETERS; 1552 goto out; 1553 } 1554 1555 if (operation->info.operationClass != TEE_OPERATION_AE) { 1556 res = TEE_ERROR_BAD_PARAMETERS; 1557 goto out; 1558 } 1559 1560 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1561 res = TEE_ERROR_BAD_PARAMETERS; 1562 goto out; 1563 } 1564 1565 /* 1566 * Check that required destLen is big enough before starting to feed 1567 * data to the algorithm. Errors during feeding of data are fatal as we 1568 * can't restore sync with this API. 1569 */ 1570 req_dlen = operation->buffer_offs + srcLen; 1571 if (*destLen < req_dlen) { 1572 *destLen = req_dlen; 1573 res = TEE_ERROR_SHORT_BUFFER; 1574 goto out; 1575 } 1576 1577 tmp_dlen = *destLen - acc_dlen; 1578 if (operation->block_size > 1) { 1579 res = tee_buffer_update(operation, utee_authenc_update_payload, 1580 srcData, srcLen, dst, &tmp_dlen); 1581 if (res != TEE_SUCCESS) 1582 goto out; 1583 1584 dst += tmp_dlen; 1585 acc_dlen += tmp_dlen; 1586 1587 tmp_dlen = *destLen - acc_dlen; 1588 res = utee_authenc_dec_final(operation->state, 1589 operation->buffer, 1590 operation->buffer_offs, dst, 1591 &tmp_dlen, tag, tagLen); 1592 } else { 1593 res = utee_authenc_dec_final(operation->state, srcData, 1594 srcLen, dst, &tmp_dlen, 1595 tag, tagLen); 1596 } 1597 if (res != TEE_SUCCESS) 1598 goto out; 1599 1600 /* Supplied tagLen should match what we initiated with */ 1601 if (tagLen != operation->info.digestLength) 1602 res = TEE_ERROR_MAC_INVALID; 1603 1604 acc_dlen += tmp_dlen; 1605 *destLen = acc_dlen; 1606 1607 operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1608 1609 operation->operationState = TEE_OPERATION_STATE_INITIAL; 1610 1611 out: 1612 if (res != TEE_SUCCESS && 1613 res != TEE_ERROR_SHORT_BUFFER && 1614 res != TEE_ERROR_MAC_INVALID) 1615 TEE_Panic(res); 1616 1617 return res; 1618 } 1619 1620 /* Cryptographic Operations API - Asymmetric Functions */ 1621 1622 TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation, 1623 const TEE_Attribute *params, 1624 uint32_t paramCount, const void *srcData, 1625 uint32_t srcLen, void *destData, 1626 uint32_t *destLen) 1627 { 1628 TEE_Result res; 1629 struct utee_attribute ua[paramCount]; 1630 uint64_t dl; 1631 1632 if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1633 destLen == NULL || (destData == NULL && *destLen != 0)) 1634 TEE_Panic(0); 1635 if (params == NULL && paramCount != 0) 1636 TEE_Panic(0); 1637 if (!operation->key1) 1638 TEE_Panic(0); 1639 if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 1640 TEE_Panic(0); 1641 if (operation->info.mode != TEE_MODE_ENCRYPT) 1642 TEE_Panic(0); 1643 1644 __utee_from_attr(ua, params, paramCount); 1645 dl = *destLen; 1646 res = utee_asymm_operate(operation->state, ua, paramCount, srcData, 1647 srcLen, destData, &dl); 1648 *destLen = dl; 1649 1650 if (res != TEE_SUCCESS && 1651 res != TEE_ERROR_SHORT_BUFFER && 1652 res != TEE_ERROR_BAD_PARAMETERS) 1653 TEE_Panic(res); 1654 1655 return res; 1656 } 1657 1658 TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation, 1659 const TEE_Attribute *params, 1660 uint32_t paramCount, const void *srcData, 1661 uint32_t srcLen, void *destData, 1662 uint32_t *destLen) 1663 { 1664 TEE_Result res; 1665 struct utee_attribute ua[paramCount]; 1666 uint64_t dl; 1667 1668 if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1669 destLen == NULL || (destData == NULL && *destLen != 0)) 1670 TEE_Panic(0); 1671 if (params == NULL && paramCount != 0) 1672 TEE_Panic(0); 1673 if (!operation->key1) 1674 TEE_Panic(0); 1675 if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 1676 TEE_Panic(0); 1677 if (operation->info.mode != TEE_MODE_DECRYPT) 1678 TEE_Panic(0); 1679 1680 __utee_from_attr(ua, params, paramCount); 1681 dl = *destLen; 1682 res = utee_asymm_operate(operation->state, ua, paramCount, srcData, 1683 srcLen, destData, &dl); 1684 *destLen = dl; 1685 1686 if (res != TEE_SUCCESS && 1687 res != TEE_ERROR_SHORT_BUFFER && 1688 res != TEE_ERROR_BAD_PARAMETERS) 1689 TEE_Panic(res); 1690 1691 return res; 1692 } 1693 1694 TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation, 1695 const TEE_Attribute *params, 1696 uint32_t paramCount, const void *digest, 1697 uint32_t digestLen, void *signature, 1698 uint32_t *signatureLen) 1699 { 1700 TEE_Result res; 1701 struct utee_attribute ua[paramCount]; 1702 uint64_t sl; 1703 1704 if (operation == TEE_HANDLE_NULL || 1705 (digest == NULL && digestLen != 0) || 1706 signature == NULL || signatureLen == NULL) 1707 TEE_Panic(0); 1708 if (params == NULL && paramCount != 0) 1709 TEE_Panic(0); 1710 if (!operation->key1) 1711 TEE_Panic(0); 1712 if (operation->info.operationClass != 1713 TEE_OPERATION_ASYMMETRIC_SIGNATURE) 1714 TEE_Panic(0); 1715 if (operation->info.mode != TEE_MODE_SIGN) 1716 TEE_Panic(0); 1717 1718 __utee_from_attr(ua, params, paramCount); 1719 sl = *signatureLen; 1720 res = utee_asymm_operate(operation->state, ua, paramCount, digest, 1721 digestLen, signature, &sl); 1722 *signatureLen = sl; 1723 1724 if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER) 1725 TEE_Panic(res); 1726 1727 return res; 1728 } 1729 1730 TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation, 1731 const TEE_Attribute *params, 1732 uint32_t paramCount, const void *digest, 1733 uint32_t digestLen, 1734 const void *signature, 1735 uint32_t signatureLen) 1736 { 1737 TEE_Result res; 1738 struct utee_attribute ua[paramCount]; 1739 1740 if (operation == TEE_HANDLE_NULL || 1741 (digest == NULL && digestLen != 0) || 1742 (signature == NULL && signatureLen != 0)) 1743 TEE_Panic(0); 1744 if (params == NULL && paramCount != 0) 1745 TEE_Panic(0); 1746 if (!operation->key1) 1747 TEE_Panic(0); 1748 if (operation->info.operationClass != 1749 TEE_OPERATION_ASYMMETRIC_SIGNATURE) 1750 TEE_Panic(0); 1751 if (operation->info.mode != TEE_MODE_VERIFY) 1752 TEE_Panic(0); 1753 1754 __utee_from_attr(ua, params, paramCount); 1755 res = utee_asymm_verify(operation->state, ua, paramCount, digest, 1756 digestLen, signature, signatureLen); 1757 1758 if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID) 1759 TEE_Panic(res); 1760 1761 return res; 1762 } 1763 1764 /* Cryptographic Operations API - Key Derivation Functions */ 1765 1766 void TEE_DeriveKey(TEE_OperationHandle operation, 1767 const TEE_Attribute *params, uint32_t paramCount, 1768 TEE_ObjectHandle derivedKey) 1769 { 1770 TEE_Result res; 1771 TEE_ObjectInfo key_info; 1772 struct utee_attribute ua[paramCount]; 1773 1774 if (operation == TEE_HANDLE_NULL || derivedKey == 0) 1775 TEE_Panic(0); 1776 if (params == NULL && paramCount != 0) 1777 TEE_Panic(0); 1778 if (TEE_ALG_GET_CLASS(operation->info.algorithm) != 1779 TEE_OPERATION_KEY_DERIVATION) 1780 TEE_Panic(0); 1781 1782 if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION) 1783 TEE_Panic(0); 1784 if (!operation->key1) 1785 TEE_Panic(0); 1786 if (operation->info.mode != TEE_MODE_DERIVE) 1787 TEE_Panic(0); 1788 if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0) 1789 TEE_Panic(0); 1790 1791 res = utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info); 1792 if (res != TEE_SUCCESS) 1793 TEE_Panic(res); 1794 1795 if (key_info.objectType != TEE_TYPE_GENERIC_SECRET) 1796 TEE_Panic(0); 1797 if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1798 TEE_Panic(0); 1799 1800 __utee_from_attr(ua, params, paramCount); 1801 res = utee_cryp_derive_key(operation->state, ua, paramCount, 1802 (unsigned long)derivedKey); 1803 if (res != TEE_SUCCESS) 1804 TEE_Panic(res); 1805 } 1806 1807 /* Cryptographic Operations API - Random Number Generation Functions */ 1808 1809 void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen) 1810 { 1811 TEE_Result res; 1812 1813 res = utee_cryp_random_number_generate(randomBuffer, randomBufferLen); 1814 if (res != TEE_SUCCESS) 1815 TEE_Panic(res); 1816 } 1817 1818 int rand(void) 1819 { 1820 int rc; 1821 1822 TEE_GenerateRandom(&rc, sizeof(rc)); 1823 1824 /* 1825 * RAND_MAX is the larges int, INT_MAX which is all bits but the 1826 * highest bit set. 1827 */ 1828 return rc & RAND_MAX; 1829 } 1830 1831 TEE_Result TEE_IsAlgorithmSupported(uint32_t alg, uint32_t element) 1832 { 1833 if (IS_ENABLED(CFG_CRYPTO_AES)) { 1834 if (IS_ENABLED(CFG_CRYPTO_ECB)) { 1835 if (alg == TEE_ALG_AES_ECB_NOPAD) 1836 goto check_element_none; 1837 } 1838 if (IS_ENABLED(CFG_CRYPTO_CBC)) { 1839 if (alg == TEE_ALG_AES_CBC_NOPAD) 1840 goto check_element_none; 1841 } 1842 if (IS_ENABLED(CFG_CRYPTO_CTR)) { 1843 if (alg == TEE_ALG_AES_CTR) 1844 goto check_element_none; 1845 } 1846 if (IS_ENABLED(CFG_CRYPTO_CTS)) { 1847 if (alg == TEE_ALG_AES_CTS) 1848 goto check_element_none; 1849 } 1850 if (IS_ENABLED(CFG_CRYPTO_XTS)) { 1851 if (alg == TEE_ALG_AES_XTS) 1852 goto check_element_none; 1853 } 1854 if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) { 1855 if (alg == TEE_ALG_AES_CBC_MAC_NOPAD || 1856 alg == TEE_ALG_AES_CBC_MAC_PKCS5) 1857 goto check_element_none; 1858 } 1859 if (IS_ENABLED(CFG_CRYPTO_CMAC)) { 1860 if (alg == TEE_ALG_AES_CMAC) 1861 goto check_element_none; 1862 } 1863 if (IS_ENABLED(CFG_CRYPTO_CCM)) { 1864 if (alg == TEE_ALG_AES_CCM) 1865 goto check_element_none; 1866 } 1867 if (IS_ENABLED(CFG_CRYPTO_GCM)) { 1868 if (alg == TEE_ALG_AES_GCM) 1869 goto check_element_none; 1870 } 1871 } 1872 if (IS_ENABLED(CFG_CRYPTO_DES)) { 1873 if (IS_ENABLED(CFG_CRYPTO_ECB)) { 1874 if (alg == TEE_ALG_DES_ECB_NOPAD || 1875 alg == TEE_ALG_DES3_ECB_NOPAD) 1876 goto check_element_none; 1877 } 1878 if (IS_ENABLED(CFG_CRYPTO_CBC)) { 1879 if (alg == TEE_ALG_DES_CBC_NOPAD || 1880 alg == TEE_ALG_DES3_CBC_NOPAD) 1881 goto check_element_none; 1882 } 1883 if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) { 1884 if (alg == TEE_ALG_DES_CBC_MAC_NOPAD || 1885 alg == TEE_ALG_DES_CBC_MAC_PKCS5 || 1886 alg == TEE_ALG_DES3_CBC_MAC_NOPAD || 1887 alg == TEE_ALG_DES3_CBC_MAC_PKCS5) 1888 goto check_element_none; 1889 } 1890 } 1891 if (IS_ENABLED(CFG_CRYPTO_MD5)) { 1892 if (alg == TEE_ALG_MD5) 1893 goto check_element_none; 1894 } 1895 if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 1896 if (alg == TEE_ALG_SHA1) 1897 goto check_element_none; 1898 } 1899 if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 1900 if (alg == TEE_ALG_SHA224) 1901 goto check_element_none; 1902 } 1903 if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 1904 if (alg == TEE_ALG_SHA256) 1905 goto check_element_none; 1906 } 1907 if (IS_ENABLED(CFG_CRYPTO_SHA384)) { 1908 if (alg == TEE_ALG_SHA384) 1909 goto check_element_none; 1910 } 1911 if (IS_ENABLED(CFG_CRYPTO_SHA512)) { 1912 if (alg == TEE_ALG_SHA512) 1913 goto check_element_none; 1914 } 1915 if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) { 1916 if (alg == TEE_ALG_MD5SHA1) 1917 goto check_element_none; 1918 } 1919 if (IS_ENABLED(CFG_CRYPTO_HMAC)) { 1920 if (IS_ENABLED(CFG_CRYPTO_MD5)) { 1921 if (alg == TEE_ALG_HMAC_MD5) 1922 goto check_element_none; 1923 } 1924 if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 1925 if (alg == TEE_ALG_HMAC_SHA1) 1926 goto check_element_none; 1927 } 1928 if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 1929 if (alg == TEE_ALG_HMAC_SHA224) 1930 goto check_element_none; 1931 } 1932 if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 1933 if (alg == TEE_ALG_HMAC_SHA256) 1934 goto check_element_none; 1935 } 1936 if (IS_ENABLED(CFG_CRYPTO_SHA384)) { 1937 if (alg == TEE_ALG_HMAC_SHA384) 1938 goto check_element_none; 1939 } 1940 if (IS_ENABLED(CFG_CRYPTO_SHA512)) { 1941 if (alg == TEE_ALG_HMAC_SHA512) 1942 goto check_element_none; 1943 } 1944 if (IS_ENABLED(CFG_CRYPTO_SM3)) { 1945 if (alg == TEE_ALG_HMAC_SM3) 1946 goto check_element_none; 1947 } 1948 } 1949 if (IS_ENABLED(CFG_CRYPTO_SM3)) { 1950 if (alg == TEE_ALG_SM3) 1951 goto check_element_none; 1952 } 1953 if (IS_ENABLED(CFG_CRYPTO_SM4)) { 1954 if (IS_ENABLED(CFG_CRYPTO_ECB)) { 1955 if (alg == TEE_ALG_SM4_ECB_NOPAD) 1956 goto check_element_none; 1957 } 1958 if (IS_ENABLED(CFG_CRYPTO_CBC)) { 1959 if (alg == TEE_ALG_SM4_CBC_NOPAD) 1960 goto check_element_none; 1961 } 1962 if (IS_ENABLED(CFG_CRYPTO_CTR)) { 1963 if (alg == TEE_ALG_SM4_CTR) 1964 goto check_element_none; 1965 } 1966 } 1967 if (IS_ENABLED(CFG_CRYPTO_RSA)) { 1968 if (IS_ENABLED(CFG_CRYPTO_MD5)) { 1969 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5) 1970 goto check_element_none; 1971 } 1972 if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 1973 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA1 || 1974 alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1 || 1975 alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1) 1976 goto check_element_none; 1977 } 1978 if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) { 1979 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5SHA1) 1980 goto check_element_none; 1981 } 1982 if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 1983 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA224 || 1984 alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224 || 1985 alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224) 1986 goto check_element_none; 1987 } 1988 if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 1989 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA256 || 1990 alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256 || 1991 alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256) 1992 goto check_element_none; 1993 } 1994 if (IS_ENABLED(CFG_CRYPTO_SHA384)) { 1995 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA384 || 1996 alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384 || 1997 alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384) 1998 goto check_element_none; 1999 } 2000 if (IS_ENABLED(CFG_CRYPTO_SHA512)) { 2001 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA512 || 2002 alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512 || 2003 alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512) 2004 goto check_element_none; 2005 } 2006 if (IS_ENABLED(CFG_CRYPTO_RSASSA_NA1)) { 2007 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5) 2008 goto check_element_none; 2009 } 2010 if (alg == TEE_ALG_RSA_NOPAD) 2011 goto check_element_none; 2012 } 2013 if (IS_ENABLED(CFG_CRYPTO_DSA)) { 2014 if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 2015 if (alg == TEE_ALG_DSA_SHA1) 2016 goto check_element_none; 2017 } 2018 if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 2019 if (alg == TEE_ALG_DSA_SHA224) 2020 goto check_element_none; 2021 } 2022 if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 2023 if (alg == TEE_ALG_DSA_SHA256) 2024 goto check_element_none; 2025 } 2026 } 2027 if (IS_ENABLED(CFG_CRYPTO_DH)) { 2028 if (alg == TEE_ALG_DH_DERIVE_SHARED_SECRET) 2029 goto check_element_none; 2030 } 2031 if (IS_ENABLED(CFG_CRYPTO_ECC)) { 2032 if ((alg == TEE_ALG_ECDH_P192 || alg == TEE_ALG_ECDSA_P192) && 2033 element == TEE_ECC_CURVE_NIST_P192) 2034 return TEE_SUCCESS; 2035 if ((alg == TEE_ALG_ECDH_P224 || alg == TEE_ALG_ECDSA_P224) && 2036 element == TEE_ECC_CURVE_NIST_P224) 2037 return TEE_SUCCESS; 2038 if ((alg == TEE_ALG_ECDH_P256 || alg == TEE_ALG_ECDSA_P256) && 2039 element == TEE_ECC_CURVE_NIST_P256) 2040 return TEE_SUCCESS; 2041 if ((alg == TEE_ALG_ECDH_P384 || alg == TEE_ALG_ECDSA_P384) && 2042 element == TEE_ECC_CURVE_NIST_P384) 2043 return TEE_SUCCESS; 2044 if ((alg == TEE_ALG_ECDH_P521 || alg == TEE_ALG_ECDSA_P521) && 2045 element == TEE_ECC_CURVE_NIST_P521) 2046 return TEE_SUCCESS; 2047 } 2048 if (IS_ENABLED(CFG_CRYPTO_SM2_DSA)) { 2049 if (alg == TEE_ALG_SM2_DSA_SM3 && element == TEE_ECC_CURVE_SM2) 2050 return TEE_SUCCESS; 2051 } 2052 if (IS_ENABLED(CFG_CRYPTO_SM2_KEP)) { 2053 if (alg == TEE_ALG_SM2_KEP && element == TEE_ECC_CURVE_SM2) 2054 return TEE_SUCCESS; 2055 } 2056 if (IS_ENABLED(CFG_CRYPTO_SM2_PKE)) { 2057 if (alg == TEE_ALG_SM2_PKE && element == TEE_ECC_CURVE_SM2) 2058 return TEE_SUCCESS; 2059 } 2060 2061 return TEE_ERROR_NOT_SUPPORTED; 2062 check_element_none: 2063 if (element == TEE_CRYPTO_ELEMENT_NONE) 2064 return TEE_SUCCESS; 2065 return TEE_ERROR_NOT_SUPPORTED; 2066 } 2067