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 (and maxKeySize for digests) */ 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 if (maxKeySize) 241 return TEE_ERROR_NOT_SUPPORTED; 242 /* v1.1: flags always set for digest operations */ 243 handle_state |= TEE_HANDLE_FLAG_KEY_SET; 244 req_key_usage = 0; 245 break; 246 247 case TEE_ALG_DES_CBC_MAC_NOPAD: 248 case TEE_ALG_AES_CBC_MAC_NOPAD: 249 case TEE_ALG_AES_CBC_MAC_PKCS5: 250 case TEE_ALG_AES_CMAC: 251 case TEE_ALG_DES_CBC_MAC_PKCS5: 252 case TEE_ALG_DES3_CBC_MAC_NOPAD: 253 case TEE_ALG_DES3_CBC_MAC_PKCS5: 254 case TEE_ALG_HMAC_MD5: 255 case TEE_ALG_HMAC_SHA1: 256 case TEE_ALG_HMAC_SHA224: 257 case TEE_ALG_HMAC_SHA256: 258 case TEE_ALG_HMAC_SHA384: 259 case TEE_ALG_HMAC_SHA512: 260 case TEE_ALG_HMAC_SM3: 261 if (mode != TEE_MODE_MAC) 262 return TEE_ERROR_NOT_SUPPORTED; 263 req_key_usage = TEE_USAGE_MAC; 264 break; 265 266 default: 267 return TEE_ERROR_NOT_SUPPORTED; 268 } 269 270 op = TEE_Malloc(sizeof(*op), TEE_MALLOC_FILL_ZERO); 271 if (!op) 272 return TEE_ERROR_OUT_OF_MEMORY; 273 274 op->info.algorithm = algorithm; 275 op->info.operationClass = TEE_ALG_GET_CLASS(algorithm); 276 #ifdef CFG_CRYPTO_RSASSA_NA1 277 if (algorithm == TEE_ALG_RSASSA_PKCS1_V1_5) 278 op->info.operationClass = TEE_OPERATION_ASYMMETRIC_SIGNATURE; 279 #endif 280 op->info.mode = mode; 281 op->info.digestLength = TEE_ALG_GET_DIGEST_SIZE(algorithm); 282 op->info.maxKeySize = maxKeySize; 283 op->info.requiredKeyUsage = req_key_usage; 284 op->info.handleState = handle_state; 285 286 if (block_size > 1) { 287 size_t buffer_size = block_size; 288 289 if (buffer_two_blocks) 290 buffer_size *= 2; 291 292 op->buffer = TEE_Malloc(buffer_size, 293 TEE_USER_MEM_HINT_NO_FILL_ZERO); 294 if (op->buffer == NULL) { 295 res = TEE_ERROR_OUT_OF_MEMORY; 296 goto out; 297 } 298 } 299 op->block_size = block_size; 300 op->buffer_two_blocks = buffer_two_blocks; 301 302 if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) { 303 uint32_t mks = maxKeySize; 304 TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm, 305 with_private_key); 306 307 /* 308 * If two keys are expected the max key size is the sum of 309 * the size of both keys. 310 */ 311 if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) 312 mks /= 2; 313 314 res = TEE_AllocateTransientObject(key_type, mks, &op->key1); 315 if (res != TEE_SUCCESS) 316 goto out; 317 318 if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) { 319 res = TEE_AllocateTransientObject(key_type, mks, 320 &op->key2); 321 if (res != TEE_SUCCESS) 322 goto out; 323 } 324 } 325 326 res = _utee_cryp_state_alloc(algorithm, mode, (unsigned long)op->key1, 327 (unsigned long)op->key2, &op->state); 328 if (res != TEE_SUCCESS) 329 goto out; 330 331 /* 332 * Initialize digest operations 333 * Other multi-stage operations initialized w/ TEE_xxxInit functions 334 * Non-applicable on asymmetric operations 335 */ 336 if (TEE_ALG_GET_CLASS(algorithm) == TEE_OPERATION_DIGEST) { 337 res = _utee_hash_init(op->state, NULL, 0); 338 if (res != TEE_SUCCESS) 339 goto out; 340 /* v1.1: flags always set for digest operations */ 341 op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 342 } 343 344 op->operationState = TEE_OPERATION_STATE_INITIAL; 345 346 *operation = op; 347 348 out: 349 if (res != TEE_SUCCESS) { 350 if (res != TEE_ERROR_OUT_OF_MEMORY && 351 res != TEE_ERROR_NOT_SUPPORTED) 352 TEE_Panic(res); 353 if (op) { 354 if (op->state) { 355 TEE_FreeOperation(op); 356 } else { 357 TEE_Free(op->buffer); 358 TEE_FreeTransientObject(op->key1); 359 TEE_FreeTransientObject(op->key2); 360 TEE_Free(op); 361 } 362 } 363 } 364 365 return res; 366 } 367 368 void TEE_FreeOperation(TEE_OperationHandle operation) 369 { 370 TEE_Result res; 371 372 if (operation == TEE_HANDLE_NULL) 373 TEE_Panic(0); 374 375 /* 376 * Note that keys should not be freed here, since they are 377 * claimed by the operation they will be freed by 378 * utee_cryp_state_free(). 379 */ 380 res = _utee_cryp_state_free(operation->state); 381 if (res != TEE_SUCCESS) 382 TEE_Panic(res); 383 384 TEE_Free(operation->buffer); 385 TEE_Free(operation); 386 } 387 388 void TEE_GetOperationInfo(TEE_OperationHandle operation, 389 TEE_OperationInfo *operationInfo) 390 { 391 if (operation == TEE_HANDLE_NULL) 392 TEE_Panic(0); 393 394 __utee_check_out_annotation(operationInfo, sizeof(*operationInfo)); 395 396 *operationInfo = operation->info; 397 } 398 399 TEE_Result TEE_GetOperationInfoMultiple(TEE_OperationHandle operation, 400 TEE_OperationInfoMultiple *operationInfoMultiple, 401 uint32_t *operationSize) 402 { 403 TEE_Result res = TEE_SUCCESS; 404 TEE_ObjectInfo key_info1; 405 TEE_ObjectInfo key_info2; 406 uint32_t num_of_keys; 407 size_t n; 408 409 if (operation == TEE_HANDLE_NULL) { 410 res = TEE_ERROR_BAD_PARAMETERS; 411 goto out; 412 } 413 414 __utee_check_outbuf_annotation(operationInfoMultiple, operationSize); 415 416 num_of_keys = (*operationSize-sizeof(TEE_OperationInfoMultiple))/ 417 sizeof(TEE_OperationInfoKey); 418 419 if (num_of_keys > 2) { 420 res = TEE_ERROR_BAD_PARAMETERS; 421 goto out; 422 } 423 424 /* Two keys flag (TEE_ALG_AES_XTS only) */ 425 if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 426 0 && 427 (num_of_keys != 2)) { 428 res = TEE_ERROR_SHORT_BUFFER; 429 goto out; 430 } 431 432 /* Clear */ 433 for (n = 0; n < num_of_keys; n++) { 434 operationInfoMultiple->keyInformation[n].keySize = 0; 435 operationInfoMultiple->keyInformation[n].requiredKeyUsage = 0; 436 } 437 438 if (num_of_keys == 2) { 439 res = TEE_GetObjectInfo1(operation->key2, &key_info2); 440 /* Key2 is not a valid handle */ 441 if (res != TEE_SUCCESS) 442 goto out; 443 444 operationInfoMultiple->keyInformation[1].keySize = 445 key_info2.keySize; 446 operationInfoMultiple->keyInformation[1].requiredKeyUsage = 447 operation->info.requiredKeyUsage; 448 } 449 450 if (num_of_keys >= 1) { 451 res = TEE_GetObjectInfo1(operation->key1, &key_info1); 452 /* Key1 is not a valid handle */ 453 if (res != TEE_SUCCESS) { 454 if (num_of_keys == 2) { 455 operationInfoMultiple->keyInformation[1]. 456 keySize = 0; 457 operationInfoMultiple->keyInformation[1]. 458 requiredKeyUsage = 0; 459 } 460 goto out; 461 } 462 463 operationInfoMultiple->keyInformation[0].keySize = 464 key_info1.keySize; 465 operationInfoMultiple->keyInformation[0].requiredKeyUsage = 466 operation->info.requiredKeyUsage; 467 } 468 469 /* No key */ 470 operationInfoMultiple->algorithm = operation->info.algorithm; 471 operationInfoMultiple->operationClass = operation->info.operationClass; 472 operationInfoMultiple->mode = operation->info.mode; 473 operationInfoMultiple->digestLength = operation->info.digestLength; 474 operationInfoMultiple->maxKeySize = operation->info.maxKeySize; 475 operationInfoMultiple->handleState = operation->info.handleState; 476 operationInfoMultiple->operationState = operation->operationState; 477 operationInfoMultiple->numberOfKeys = num_of_keys; 478 479 out: 480 if (res != TEE_SUCCESS && 481 res != TEE_ERROR_SHORT_BUFFER) 482 TEE_Panic(res); 483 484 return res; 485 } 486 487 void TEE_ResetOperation(TEE_OperationHandle operation) 488 { 489 TEE_Result res; 490 491 if (operation == TEE_HANDLE_NULL) 492 TEE_Panic(0); 493 494 if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET)) 495 TEE_Panic(0); 496 497 operation->operationState = TEE_OPERATION_STATE_INITIAL; 498 499 if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 500 res = _utee_hash_init(operation->state, NULL, 0); 501 if (res != TEE_SUCCESS) 502 TEE_Panic(res); 503 operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 504 } else { 505 operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 506 } 507 } 508 509 TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation, 510 TEE_ObjectHandle key) 511 { 512 TEE_Result res; 513 uint32_t key_size = 0; 514 TEE_ObjectInfo key_info; 515 516 if (operation == TEE_HANDLE_NULL) { 517 res = TEE_ERROR_BAD_PARAMETERS; 518 goto out; 519 } 520 521 if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 522 res = TEE_ERROR_BAD_PARAMETERS; 523 goto out; 524 } 525 526 if (key == TEE_HANDLE_NULL) { 527 /* Operation key cleared */ 528 TEE_ResetTransientObject(operation->key1); 529 res = TEE_ERROR_BAD_PARAMETERS; 530 goto out; 531 } 532 533 /* No key for digest operation */ 534 if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 535 res = TEE_ERROR_BAD_PARAMETERS; 536 goto out; 537 } 538 539 /* Two keys flag not expected (TEE_ALG_AES_XTS excluded) */ 540 if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 541 0) { 542 res = TEE_ERROR_BAD_PARAMETERS; 543 goto out; 544 } 545 546 res = TEE_GetObjectInfo1(key, &key_info); 547 /* Key is not a valid handle */ 548 if (res != TEE_SUCCESS) 549 goto out; 550 551 /* Supplied key has to meet required usage */ 552 if ((key_info.objectUsage & operation->info.requiredKeyUsage) != 553 operation->info.requiredKeyUsage) { 554 res = TEE_ERROR_BAD_PARAMETERS; 555 goto out; 556 } 557 558 if (operation->info.maxKeySize < key_info.keySize) { 559 res = TEE_ERROR_BAD_PARAMETERS; 560 goto out; 561 } 562 563 key_size = key_info.keySize; 564 565 TEE_ResetTransientObject(operation->key1); 566 operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 567 568 res = TEE_CopyObjectAttributes1(operation->key1, key); 569 if (res != TEE_SUCCESS) 570 goto out; 571 572 operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 573 574 operation->info.keySize = key_size; 575 576 out: 577 if (res != TEE_SUCCESS && 578 res != TEE_ERROR_CORRUPT_OBJECT && 579 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 580 TEE_Panic(res); 581 582 return res; 583 } 584 585 TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation, 586 TEE_ObjectHandle key1, TEE_ObjectHandle key2) 587 { 588 TEE_Result res; 589 uint32_t key_size = 0; 590 TEE_ObjectInfo key_info1; 591 TEE_ObjectInfo key_info2; 592 593 if (operation == TEE_HANDLE_NULL) { 594 res = TEE_ERROR_BAD_PARAMETERS; 595 goto out; 596 } 597 598 if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 599 res = TEE_ERROR_BAD_PARAMETERS; 600 goto out; 601 } 602 603 /* 604 * Key1/Key2 and/or are not initialized and 605 * Either both keys are NULL or both are not NULL 606 */ 607 if (key1 == TEE_HANDLE_NULL || key2 == TEE_HANDLE_NULL) { 608 /* Clear operation key1 (if needed) */ 609 if (key1 == TEE_HANDLE_NULL) 610 TEE_ResetTransientObject(operation->key1); 611 /* Clear operation key2 (if needed) */ 612 if (key2 == TEE_HANDLE_NULL) 613 TEE_ResetTransientObject(operation->key2); 614 res = TEE_ERROR_BAD_PARAMETERS; 615 goto out; 616 } 617 618 /* No key for digest operation */ 619 if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 620 res = TEE_ERROR_BAD_PARAMETERS; 621 goto out; 622 } 623 624 /* Two keys flag expected (TEE_ALG_AES_XTS and TEE_ALG_SM2_KEP only) */ 625 if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 626 0) { 627 res = TEE_ERROR_BAD_PARAMETERS; 628 goto out; 629 } 630 631 res = TEE_GetObjectInfo1(key1, &key_info1); 632 /* Key1 is not a valid handle */ 633 if (res != TEE_SUCCESS) 634 goto out; 635 636 /* Supplied key has to meet required usage */ 637 if ((key_info1.objectUsage & operation->info. 638 requiredKeyUsage) != operation->info.requiredKeyUsage) { 639 res = TEE_ERROR_BAD_PARAMETERS; 640 goto out; 641 } 642 643 res = TEE_GetObjectInfo1(key2, &key_info2); 644 /* Key2 is not a valid handle */ 645 if (res != TEE_SUCCESS) { 646 if (res == TEE_ERROR_CORRUPT_OBJECT) 647 res = TEE_ERROR_CORRUPT_OBJECT_2; 648 goto out; 649 } 650 651 /* Supplied key has to meet required usage */ 652 if ((key_info2.objectUsage & operation->info. 653 requiredKeyUsage) != operation->info.requiredKeyUsage) { 654 res = TEE_ERROR_BAD_PARAMETERS; 655 goto out; 656 } 657 658 /* 659 * All the multi key algorithm currently supported requires the keys to 660 * be of equal size. 661 */ 662 if (key_info1.keySize != key_info2.keySize) { 663 res = TEE_ERROR_BAD_PARAMETERS; 664 goto out; 665 666 } 667 668 if (operation->info.maxKeySize < key_info1.keySize) { 669 res = TEE_ERROR_BAD_PARAMETERS; 670 goto out; 671 } 672 673 /* 674 * Odd that only the size of one key should be reported while 675 * size of two key are used when allocating the operation. 676 */ 677 key_size = key_info1.keySize; 678 679 TEE_ResetTransientObject(operation->key1); 680 TEE_ResetTransientObject(operation->key2); 681 operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 682 683 res = TEE_CopyObjectAttributes1(operation->key1, key1); 684 if (res != TEE_SUCCESS) 685 goto out; 686 res = TEE_CopyObjectAttributes1(operation->key2, key2); 687 if (res != TEE_SUCCESS) { 688 if (res == TEE_ERROR_CORRUPT_OBJECT) 689 res = TEE_ERROR_CORRUPT_OBJECT_2; 690 goto out; 691 } 692 693 operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 694 695 operation->info.keySize = key_size; 696 697 out: 698 if (res != TEE_SUCCESS && 699 res != TEE_ERROR_CORRUPT_OBJECT && 700 res != TEE_ERROR_CORRUPT_OBJECT_2 && 701 res != TEE_ERROR_STORAGE_NOT_AVAILABLE && 702 res != TEE_ERROR_STORAGE_NOT_AVAILABLE_2) 703 TEE_Panic(res); 704 705 return res; 706 } 707 708 void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op) 709 { 710 TEE_Result res; 711 712 if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL) 713 TEE_Panic(0); 714 if (dst_op->info.algorithm != src_op->info.algorithm) 715 TEE_Panic(0); 716 if (src_op->info.operationClass != TEE_OPERATION_DIGEST) { 717 TEE_ObjectHandle key1 = TEE_HANDLE_NULL; 718 TEE_ObjectHandle key2 = TEE_HANDLE_NULL; 719 720 if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) { 721 key1 = src_op->key1; 722 key2 = src_op->key2; 723 } 724 725 if ((src_op->info.handleState & 726 TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) { 727 TEE_SetOperationKey(dst_op, key1); 728 } else { 729 TEE_SetOperationKey2(dst_op, key1, key2); 730 } 731 } 732 dst_op->info.handleState = src_op->info.handleState; 733 dst_op->info.keySize = src_op->info.keySize; 734 dst_op->operationState = src_op->operationState; 735 736 if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks || 737 dst_op->block_size != src_op->block_size) 738 TEE_Panic(0); 739 740 if (dst_op->buffer != NULL) { 741 if (src_op->buffer == NULL) 742 TEE_Panic(0); 743 744 memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs); 745 dst_op->buffer_offs = src_op->buffer_offs; 746 } else if (src_op->buffer != NULL) { 747 TEE_Panic(0); 748 } 749 750 res = _utee_cryp_state_copy(dst_op->state, src_op->state); 751 if (res != TEE_SUCCESS) 752 TEE_Panic(res); 753 } 754 755 /* Cryptographic Operations API - Message Digest Functions */ 756 757 static void init_hash_operation(TEE_OperationHandle operation, const void *IV, 758 uint32_t IVLen) 759 { 760 TEE_Result res; 761 762 /* 763 * Note : IV and IVLen are never used in current implementation 764 * This is why coherent values of IV and IVLen are not checked 765 */ 766 res = _utee_hash_init(operation->state, IV, IVLen); 767 if (res != TEE_SUCCESS) 768 TEE_Panic(res); 769 operation->buffer_offs = 0; 770 operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 771 } 772 773 void TEE_DigestUpdate(TEE_OperationHandle operation, 774 const void *chunk, uint32_t chunkSize) 775 { 776 TEE_Result res = TEE_ERROR_GENERIC; 777 778 if (operation == TEE_HANDLE_NULL || 779 operation->info.operationClass != TEE_OPERATION_DIGEST) 780 TEE_Panic(0); 781 782 operation->operationState = TEE_OPERATION_STATE_ACTIVE; 783 784 res = _utee_hash_update(operation->state, chunk, chunkSize); 785 if (res != TEE_SUCCESS) 786 TEE_Panic(res); 787 } 788 789 TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void *chunk, 790 uint32_t chunkLen, void *hash, uint32_t *hashLen) 791 { 792 TEE_Result res; 793 uint64_t hl; 794 795 if ((operation == TEE_HANDLE_NULL) || 796 (!chunk && chunkLen) || 797 (operation->info.operationClass != TEE_OPERATION_DIGEST)) { 798 res = TEE_ERROR_BAD_PARAMETERS; 799 goto out; 800 } 801 __utee_check_inout_annotation(hashLen, sizeof(*hashLen)); 802 803 hl = *hashLen; 804 res = _utee_hash_final(operation->state, chunk, chunkLen, hash, &hl); 805 *hashLen = hl; 806 if (res != TEE_SUCCESS) 807 goto out; 808 809 /* Reset operation state */ 810 init_hash_operation(operation, NULL, 0); 811 812 operation->operationState = TEE_OPERATION_STATE_INITIAL; 813 814 out: 815 if (res != TEE_SUCCESS && 816 res != TEE_ERROR_SHORT_BUFFER) 817 TEE_Panic(res); 818 819 return res; 820 } 821 822 /* Cryptographic Operations API - Symmetric Cipher Functions */ 823 824 void TEE_CipherInit(TEE_OperationHandle operation, const void *IV, 825 uint32_t IVLen) 826 { 827 TEE_Result res; 828 829 if (operation == TEE_HANDLE_NULL) 830 TEE_Panic(0); 831 832 if (operation->info.operationClass != TEE_OPERATION_CIPHER) 833 TEE_Panic(0); 834 835 if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 836 !(operation->key1)) 837 TEE_Panic(0); 838 839 if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 840 TEE_ResetOperation(operation); 841 842 operation->operationState = TEE_OPERATION_STATE_ACTIVE; 843 844 res = _utee_cipher_init(operation->state, IV, IVLen); 845 if (res != TEE_SUCCESS) 846 TEE_Panic(res); 847 848 operation->buffer_offs = 0; 849 operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 850 } 851 852 static TEE_Result tee_buffer_update( 853 TEE_OperationHandle op, 854 TEE_Result(*update_func)(unsigned long state, const void *src, 855 size_t slen, void *dst, uint64_t *dlen), 856 const void *src_data, size_t src_len, 857 void *dest_data, uint64_t *dest_len) 858 { 859 TEE_Result res; 860 const uint8_t *src = src_data; 861 size_t slen = src_len; 862 uint8_t *dst = dest_data; 863 size_t dlen = *dest_len; 864 size_t acc_dlen = 0; 865 uint64_t tmp_dlen; 866 size_t l; 867 size_t buffer_size; 868 size_t buffer_left; 869 870 if (!src) { 871 if (slen) 872 TEE_Panic(0); 873 goto out; 874 } 875 876 if (op->buffer_two_blocks) { 877 buffer_size = op->block_size * 2; 878 buffer_left = 1; 879 } else { 880 buffer_size = op->block_size; 881 buffer_left = 0; 882 } 883 884 if (op->buffer_offs > 0) { 885 /* Fill up complete block */ 886 if (op->buffer_offs < op->block_size) 887 l = MIN(slen, op->block_size - op->buffer_offs); 888 else 889 l = MIN(slen, buffer_size - op->buffer_offs); 890 memcpy(op->buffer + op->buffer_offs, src, l); 891 op->buffer_offs += l; 892 src += l; 893 slen -= l; 894 if ((op->buffer_offs % op->block_size) != 0) 895 goto out; /* Nothing left to do */ 896 } 897 898 /* If we can feed from buffer */ 899 if ((op->buffer_offs > 0) && 900 ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) { 901 l = ROUNDUP(op->buffer_offs + slen - buffer_size, 902 op->block_size); 903 l = MIN(op->buffer_offs, l); 904 tmp_dlen = dlen; 905 res = update_func(op->state, op->buffer, l, dst, &tmp_dlen); 906 if (res != TEE_SUCCESS) 907 TEE_Panic(res); 908 dst += tmp_dlen; 909 dlen -= tmp_dlen; 910 acc_dlen += tmp_dlen; 911 op->buffer_offs -= l; 912 if (op->buffer_offs > 0) { 913 /* 914 * Slen is small enough to be contained in rest buffer. 915 */ 916 memcpy(op->buffer, op->buffer + l, buffer_size - l); 917 memcpy(op->buffer + op->buffer_offs, src, slen); 918 op->buffer_offs += slen; 919 goto out; /* Nothing left to do */ 920 } 921 } 922 923 if (slen >= (buffer_size + buffer_left)) { 924 /* Buffer is empty, feed as much as possible from src */ 925 if (op->info.algorithm == TEE_ALG_AES_CTS) 926 l = ROUNDUP(slen - buffer_size, op->block_size); 927 else 928 l = ROUNDUP(slen - buffer_size + 1, op->block_size); 929 930 tmp_dlen = dlen; 931 res = update_func(op->state, src, l, dst, &tmp_dlen); 932 if (res != TEE_SUCCESS) 933 TEE_Panic(res); 934 src += l; 935 slen -= l; 936 dst += tmp_dlen; 937 dlen -= tmp_dlen; 938 acc_dlen += tmp_dlen; 939 } 940 941 /* Slen is small enough to be contained in buffer. */ 942 memcpy(op->buffer + op->buffer_offs, src, slen); 943 op->buffer_offs += slen; 944 945 out: 946 *dest_len = acc_dlen; 947 return TEE_SUCCESS; 948 } 949 950 TEE_Result TEE_CipherUpdate(TEE_OperationHandle operation, const void *srcData, 951 uint32_t srcLen, void *destData, uint32_t *destLen) 952 { 953 TEE_Result res; 954 size_t req_dlen; 955 uint64_t dl; 956 957 if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 958 res = TEE_ERROR_BAD_PARAMETERS; 959 goto out; 960 } 961 __utee_check_inout_annotation(destLen, sizeof(*destLen)); 962 963 if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 964 res = TEE_ERROR_BAD_PARAMETERS; 965 goto out; 966 } 967 968 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 969 res = TEE_ERROR_BAD_PARAMETERS; 970 goto out; 971 } 972 973 if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 974 res = TEE_ERROR_BAD_PARAMETERS; 975 goto out; 976 } 977 978 if (!srcData && !srcLen) { 979 *destLen = 0; 980 res = TEE_SUCCESS; 981 goto out; 982 } 983 984 /* Calculate required dlen */ 985 if (operation->block_size > 1) { 986 req_dlen = ((operation->buffer_offs + srcLen) / 987 operation->block_size) * operation->block_size; 988 } else { 989 req_dlen = srcLen; 990 } 991 if (operation->buffer_two_blocks) { 992 if (req_dlen > operation->block_size * 2) 993 req_dlen -= operation->block_size * 2; 994 else 995 req_dlen = 0; 996 } 997 /* 998 * Check that required destLen is big enough before starting to feed 999 * data to the algorithm. Errors during feeding of data are fatal as we 1000 * can't restore sync with this API. 1001 */ 1002 if (*destLen < req_dlen) { 1003 *destLen = req_dlen; 1004 res = TEE_ERROR_SHORT_BUFFER; 1005 goto out; 1006 } 1007 1008 dl = *destLen; 1009 if (operation->block_size > 1) { 1010 res = tee_buffer_update(operation, _utee_cipher_update, srcData, 1011 srcLen, destData, &dl); 1012 } else { 1013 if (srcLen > 0) { 1014 res = _utee_cipher_update(operation->state, srcData, 1015 srcLen, destData, &dl); 1016 } else { 1017 res = TEE_SUCCESS; 1018 dl = 0; 1019 } 1020 } 1021 *destLen = dl; 1022 1023 out: 1024 if (res != TEE_SUCCESS && 1025 res != TEE_ERROR_SHORT_BUFFER) 1026 TEE_Panic(res); 1027 1028 return res; 1029 } 1030 1031 TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation, 1032 const void *srcData, uint32_t srcLen, 1033 void *destData, uint32_t *destLen) 1034 { 1035 TEE_Result res = TEE_SUCCESS; 1036 uint8_t *dst = destData; 1037 size_t acc_dlen = 0; 1038 uint64_t tmp_dlen = 0; 1039 size_t req_dlen = 0; 1040 1041 if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 1042 res = TEE_ERROR_BAD_PARAMETERS; 1043 goto out; 1044 } 1045 if (destLen) 1046 __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1047 1048 if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 1049 res = TEE_ERROR_BAD_PARAMETERS; 1050 goto out; 1051 } 1052 1053 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1054 res = TEE_ERROR_BAD_PARAMETERS; 1055 goto out; 1056 } 1057 1058 if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1059 res = TEE_ERROR_BAD_PARAMETERS; 1060 goto out; 1061 } 1062 1063 /* 1064 * Check that the final block doesn't require padding for those 1065 * algorithms that requires client to supply padding. 1066 */ 1067 if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD || 1068 operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD || 1069 operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD || 1070 operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD || 1071 operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || 1072 operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD || 1073 operation->info.algorithm == TEE_ALG_SM4_ECB_NOPAD || 1074 operation->info.algorithm == TEE_ALG_SM4_CBC_NOPAD) { 1075 if (((operation->buffer_offs + srcLen) % operation->block_size) 1076 != 0) { 1077 res = TEE_ERROR_BAD_PARAMETERS; 1078 goto out; 1079 } 1080 } 1081 1082 /* 1083 * Check that required destLen is big enough before starting to feed 1084 * data to the algorithm. Errors during feeding of data are fatal as we 1085 * can't restore sync with this API. 1086 */ 1087 if (operation->block_size > 1) { 1088 req_dlen = operation->buffer_offs + srcLen; 1089 } else { 1090 req_dlen = srcLen; 1091 } 1092 if (destLen) 1093 tmp_dlen = *destLen; 1094 if (tmp_dlen < req_dlen) { 1095 if (destLen) 1096 *destLen = req_dlen; 1097 res = TEE_ERROR_SHORT_BUFFER; 1098 goto out; 1099 } 1100 1101 if (operation->block_size > 1) { 1102 res = tee_buffer_update(operation, _utee_cipher_update, 1103 srcData, srcLen, dst, &tmp_dlen); 1104 if (res != TEE_SUCCESS) 1105 goto out; 1106 1107 dst += tmp_dlen; 1108 acc_dlen += tmp_dlen; 1109 1110 tmp_dlen = *destLen - acc_dlen; 1111 res = _utee_cipher_final(operation->state, operation->buffer, 1112 operation->buffer_offs, dst, 1113 &tmp_dlen); 1114 } else { 1115 res = _utee_cipher_final(operation->state, srcData, srcLen, dst, 1116 &tmp_dlen); 1117 } 1118 if (res != TEE_SUCCESS) 1119 goto out; 1120 1121 acc_dlen += tmp_dlen; 1122 if (destLen) 1123 *destLen = acc_dlen; 1124 1125 operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1126 1127 operation->operationState = TEE_OPERATION_STATE_INITIAL; 1128 1129 out: 1130 if (res != TEE_SUCCESS && 1131 res != TEE_ERROR_SHORT_BUFFER) 1132 TEE_Panic(res); 1133 1134 return res; 1135 } 1136 1137 /* Cryptographic Operations API - MAC Functions */ 1138 1139 void TEE_MACInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen) 1140 { 1141 if (operation == TEE_HANDLE_NULL) 1142 TEE_Panic(0); 1143 1144 if (operation->info.operationClass != TEE_OPERATION_MAC) 1145 TEE_Panic(0); 1146 1147 if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 1148 !(operation->key1)) 1149 TEE_Panic(0); 1150 1151 if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 1152 TEE_ResetOperation(operation); 1153 1154 operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1155 1156 init_hash_operation(operation, IV, IVLen); 1157 } 1158 1159 void TEE_MACUpdate(TEE_OperationHandle operation, const void *chunk, 1160 uint32_t chunkSize) 1161 { 1162 TEE_Result res; 1163 1164 if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0)) 1165 TEE_Panic(0); 1166 1167 if (operation->info.operationClass != TEE_OPERATION_MAC) 1168 TEE_Panic(0); 1169 1170 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1171 TEE_Panic(0); 1172 1173 if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) 1174 TEE_Panic(0); 1175 1176 res = _utee_hash_update(operation->state, chunk, chunkSize); 1177 if (res != TEE_SUCCESS) 1178 TEE_Panic(res); 1179 } 1180 1181 TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation, 1182 const void *message, uint32_t messageLen, 1183 void *mac, uint32_t *macLen) 1184 { 1185 TEE_Result res; 1186 uint64_t ml; 1187 1188 if (operation == TEE_HANDLE_NULL || (!message && messageLen)) { 1189 res = TEE_ERROR_BAD_PARAMETERS; 1190 goto out; 1191 } 1192 __utee_check_inout_annotation(macLen, sizeof(*macLen)); 1193 1194 if (operation->info.operationClass != TEE_OPERATION_MAC) { 1195 res = TEE_ERROR_BAD_PARAMETERS; 1196 goto out; 1197 } 1198 1199 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1200 res = TEE_ERROR_BAD_PARAMETERS; 1201 goto out; 1202 } 1203 1204 if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1205 res = TEE_ERROR_BAD_PARAMETERS; 1206 goto out; 1207 } 1208 1209 ml = *macLen; 1210 res = _utee_hash_final(operation->state, message, messageLen, mac, &ml); 1211 *macLen = ml; 1212 if (res != TEE_SUCCESS) 1213 goto out; 1214 1215 operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1216 1217 operation->operationState = TEE_OPERATION_STATE_INITIAL; 1218 1219 out: 1220 if (res != TEE_SUCCESS && 1221 res != TEE_ERROR_SHORT_BUFFER) 1222 TEE_Panic(res); 1223 1224 return res; 1225 } 1226 1227 TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation, 1228 const void *message, uint32_t messageLen, 1229 const void *mac, uint32_t macLen) 1230 { 1231 TEE_Result res; 1232 uint8_t computed_mac[TEE_MAX_HASH_SIZE]; 1233 uint32_t computed_mac_size = TEE_MAX_HASH_SIZE; 1234 1235 if (operation->info.operationClass != TEE_OPERATION_MAC) { 1236 res = TEE_ERROR_BAD_PARAMETERS; 1237 goto out; 1238 } 1239 1240 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1241 res = TEE_ERROR_BAD_PARAMETERS; 1242 goto out; 1243 } 1244 1245 if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1246 res = TEE_ERROR_BAD_PARAMETERS; 1247 goto out; 1248 } 1249 1250 res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac, 1251 &computed_mac_size); 1252 if (res != TEE_SUCCESS) 1253 goto out; 1254 1255 if (computed_mac_size != macLen) { 1256 res = TEE_ERROR_MAC_INVALID; 1257 goto out; 1258 } 1259 1260 if (consttime_memcmp(mac, computed_mac, computed_mac_size) != 0) { 1261 res = TEE_ERROR_MAC_INVALID; 1262 goto out; 1263 } 1264 1265 operation->operationState = TEE_OPERATION_STATE_INITIAL; 1266 1267 out: 1268 if (res != TEE_SUCCESS && 1269 res != TEE_ERROR_MAC_INVALID) 1270 TEE_Panic(res); 1271 1272 return res; 1273 } 1274 1275 /* Cryptographic Operations API - Authenticated Encryption Functions */ 1276 1277 TEE_Result TEE_AEInit(TEE_OperationHandle operation, const void *nonce, 1278 uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen, 1279 uint32_t payloadLen) 1280 { 1281 TEE_Result res; 1282 1283 if (operation == TEE_HANDLE_NULL || nonce == NULL) { 1284 res = TEE_ERROR_BAD_PARAMETERS; 1285 goto out; 1286 } 1287 1288 if (operation->info.operationClass != TEE_OPERATION_AE) { 1289 res = TEE_ERROR_BAD_PARAMETERS; 1290 goto out; 1291 } 1292 1293 if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 1294 res = TEE_ERROR_BAD_PARAMETERS; 1295 goto out; 1296 } 1297 1298 /* 1299 * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core 1300 * in the implementation. But AES-GCM spec doesn't specify the tag len 1301 * according to the same principle so we have to check here instead to 1302 * be GP compliant. 1303 */ 1304 if (operation->info.algorithm == TEE_ALG_AES_GCM) { 1305 /* 1306 * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96 1307 */ 1308 if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) { 1309 res = TEE_ERROR_NOT_SUPPORTED; 1310 goto out; 1311 } 1312 } 1313 1314 res = _utee_authenc_init(operation->state, nonce, nonceLen, tagLen / 8, 1315 AADLen, payloadLen); 1316 if (res != TEE_SUCCESS) 1317 goto out; 1318 1319 operation->info.digestLength = tagLen / 8; 1320 operation->buffer_offs = 0; 1321 operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 1322 1323 out: 1324 if (res != TEE_SUCCESS && 1325 res != TEE_ERROR_NOT_SUPPORTED) 1326 TEE_Panic(res); 1327 1328 return res; 1329 } 1330 1331 void TEE_AEUpdateAAD(TEE_OperationHandle operation, const void *AADdata, 1332 uint32_t AADdataLen) 1333 { 1334 TEE_Result res; 1335 1336 if (operation == TEE_HANDLE_NULL || 1337 (AADdata == NULL && AADdataLen != 0)) 1338 TEE_Panic(0); 1339 1340 if (operation->info.operationClass != TEE_OPERATION_AE) 1341 TEE_Panic(0); 1342 1343 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1344 TEE_Panic(0); 1345 1346 res = _utee_authenc_update_aad(operation->state, AADdata, AADdataLen); 1347 1348 operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1349 1350 if (res != TEE_SUCCESS) 1351 TEE_Panic(res); 1352 } 1353 1354 TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, const void *srcData, 1355 uint32_t srcLen, void *destData, uint32_t *destLen) 1356 { 1357 TEE_Result res = TEE_SUCCESS; 1358 size_t req_dlen = 0; 1359 uint64_t dl = 0; 1360 1361 if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 1362 res = TEE_ERROR_BAD_PARAMETERS; 1363 goto out; 1364 } 1365 __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1366 1367 if (operation->info.operationClass != TEE_OPERATION_AE) { 1368 res = TEE_ERROR_BAD_PARAMETERS; 1369 goto out; 1370 } 1371 1372 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1373 res = TEE_ERROR_BAD_PARAMETERS; 1374 goto out; 1375 } 1376 1377 if (!srcData && !srcLen) { 1378 *destLen = 0; 1379 res = TEE_SUCCESS; 1380 goto out; 1381 } 1382 1383 /* 1384 * Check that required destLen is big enough before starting to feed 1385 * data to the algorithm. Errors during feeding of data are fatal as we 1386 * can't restore sync with this API. 1387 */ 1388 if (operation->block_size > 1) { 1389 req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen, 1390 operation->block_size); 1391 } else { 1392 req_dlen = srcLen; 1393 } 1394 1395 dl = *destLen; 1396 if (dl < req_dlen) { 1397 *destLen = req_dlen; 1398 res = TEE_ERROR_SHORT_BUFFER; 1399 goto out; 1400 } 1401 1402 if (operation->block_size > 1) { 1403 res = tee_buffer_update(operation, _utee_authenc_update_payload, 1404 srcData, srcLen, destData, &dl); 1405 } else { 1406 if (srcLen > 0) { 1407 res = _utee_authenc_update_payload(operation->state, 1408 srcData, srcLen, 1409 destData, &dl); 1410 } else { 1411 dl = 0; 1412 res = TEE_SUCCESS; 1413 } 1414 } 1415 if (res != TEE_SUCCESS) 1416 goto out; 1417 1418 *destLen = dl; 1419 1420 operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1421 1422 out: 1423 if (res != TEE_SUCCESS && 1424 res != TEE_ERROR_SHORT_BUFFER) 1425 TEE_Panic(res); 1426 1427 return res; 1428 } 1429 1430 TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation, 1431 const void *srcData, uint32_t srcLen, 1432 void *destData, uint32_t *destLen, void *tag, 1433 uint32_t *tagLen) 1434 { 1435 TEE_Result res; 1436 uint8_t *dst = destData; 1437 size_t acc_dlen = 0; 1438 uint64_t tmp_dlen; 1439 size_t req_dlen; 1440 uint64_t tl; 1441 1442 if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 1443 res = TEE_ERROR_BAD_PARAMETERS; 1444 goto out; 1445 } 1446 __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1447 __utee_check_inout_annotation(tagLen, sizeof(*tagLen)); 1448 1449 if (operation->info.operationClass != TEE_OPERATION_AE) { 1450 res = TEE_ERROR_BAD_PARAMETERS; 1451 goto out; 1452 } 1453 1454 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1455 res = TEE_ERROR_BAD_PARAMETERS; 1456 goto out; 1457 } 1458 1459 /* 1460 * Check that required destLen is big enough before starting to feed 1461 * data to the algorithm. Errors during feeding of data are fatal as we 1462 * can't restore sync with this API. 1463 * 1464 * Need to check this before update_payload since sync would be lost if 1465 * we return short buffer after that. 1466 */ 1467 res = TEE_ERROR_GENERIC; 1468 1469 req_dlen = operation->buffer_offs + srcLen; 1470 if (*destLen < req_dlen) { 1471 *destLen = req_dlen; 1472 res = TEE_ERROR_SHORT_BUFFER; 1473 } 1474 1475 if (*tagLen < operation->info.digestLength) { 1476 *tagLen = operation->info.digestLength; 1477 res = TEE_ERROR_SHORT_BUFFER; 1478 } 1479 1480 if (res == TEE_ERROR_SHORT_BUFFER) 1481 goto out; 1482 1483 tl = *tagLen; 1484 tmp_dlen = *destLen - acc_dlen; 1485 if (operation->block_size > 1) { 1486 res = tee_buffer_update(operation, _utee_authenc_update_payload, 1487 srcData, srcLen, dst, &tmp_dlen); 1488 if (res != TEE_SUCCESS) 1489 goto out; 1490 1491 dst += tmp_dlen; 1492 acc_dlen += tmp_dlen; 1493 1494 tmp_dlen = *destLen - acc_dlen; 1495 res = _utee_authenc_enc_final(operation->state, 1496 operation->buffer, 1497 operation->buffer_offs, dst, 1498 &tmp_dlen, tag, &tl); 1499 } else { 1500 res = _utee_authenc_enc_final(operation->state, srcData, 1501 srcLen, dst, &tmp_dlen, 1502 tag, &tl); 1503 } 1504 *tagLen = tl; 1505 if (res != TEE_SUCCESS) 1506 goto out; 1507 1508 acc_dlen += tmp_dlen; 1509 *destLen = acc_dlen; 1510 1511 operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1512 1513 operation->operationState = TEE_OPERATION_STATE_INITIAL; 1514 1515 out: 1516 if (res != TEE_SUCCESS && 1517 res != TEE_ERROR_SHORT_BUFFER) 1518 TEE_Panic(res); 1519 1520 return res; 1521 } 1522 1523 TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation, 1524 const void *srcData, uint32_t srcLen, 1525 void *destData, uint32_t *destLen, void *tag, 1526 uint32_t tagLen) 1527 { 1528 TEE_Result res; 1529 uint8_t *dst = destData; 1530 size_t acc_dlen = 0; 1531 uint64_t tmp_dlen; 1532 size_t req_dlen; 1533 1534 if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 1535 res = TEE_ERROR_BAD_PARAMETERS; 1536 goto out; 1537 } 1538 __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1539 1540 if (operation->info.operationClass != TEE_OPERATION_AE) { 1541 res = TEE_ERROR_BAD_PARAMETERS; 1542 goto out; 1543 } 1544 1545 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1546 res = TEE_ERROR_BAD_PARAMETERS; 1547 goto out; 1548 } 1549 1550 /* 1551 * Check that required destLen is big enough before starting to feed 1552 * data to the algorithm. Errors during feeding of data are fatal as we 1553 * can't restore sync with this API. 1554 */ 1555 req_dlen = operation->buffer_offs + srcLen; 1556 if (*destLen < req_dlen) { 1557 *destLen = req_dlen; 1558 res = TEE_ERROR_SHORT_BUFFER; 1559 goto out; 1560 } 1561 1562 tmp_dlen = *destLen - acc_dlen; 1563 if (operation->block_size > 1) { 1564 res = tee_buffer_update(operation, _utee_authenc_update_payload, 1565 srcData, srcLen, dst, &tmp_dlen); 1566 if (res != TEE_SUCCESS) 1567 goto out; 1568 1569 dst += tmp_dlen; 1570 acc_dlen += tmp_dlen; 1571 1572 tmp_dlen = *destLen - acc_dlen; 1573 res = _utee_authenc_dec_final(operation->state, 1574 operation->buffer, 1575 operation->buffer_offs, dst, 1576 &tmp_dlen, tag, tagLen); 1577 } else { 1578 res = _utee_authenc_dec_final(operation->state, srcData, 1579 srcLen, dst, &tmp_dlen, 1580 tag, tagLen); 1581 } 1582 if (res != TEE_SUCCESS) 1583 goto out; 1584 1585 /* Supplied tagLen should match what we initiated with */ 1586 if (tagLen != operation->info.digestLength) 1587 res = TEE_ERROR_MAC_INVALID; 1588 1589 acc_dlen += tmp_dlen; 1590 *destLen = acc_dlen; 1591 1592 operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1593 1594 operation->operationState = TEE_OPERATION_STATE_INITIAL; 1595 1596 out: 1597 if (res != TEE_SUCCESS && 1598 res != TEE_ERROR_SHORT_BUFFER && 1599 res != TEE_ERROR_MAC_INVALID) 1600 TEE_Panic(res); 1601 1602 return res; 1603 } 1604 1605 /* Cryptographic Operations API - Asymmetric Functions */ 1606 1607 TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation, 1608 const TEE_Attribute *params, 1609 uint32_t paramCount, const void *srcData, 1610 uint32_t srcLen, void *destData, 1611 uint32_t *destLen) 1612 { 1613 TEE_Result res = TEE_SUCCESS; 1614 struct utee_attribute ua[paramCount]; 1615 uint64_t dl = 0; 1616 1617 if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) 1618 TEE_Panic(0); 1619 1620 __utee_check_attr_in_annotation(params, paramCount); 1621 __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1622 1623 if (!operation->key1) 1624 TEE_Panic(0); 1625 if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 1626 TEE_Panic(0); 1627 if (operation->info.mode != TEE_MODE_ENCRYPT) 1628 TEE_Panic(0); 1629 1630 __utee_from_attr(ua, params, paramCount); 1631 dl = *destLen; 1632 res = _utee_asymm_operate(operation->state, ua, paramCount, srcData, 1633 srcLen, destData, &dl); 1634 *destLen = dl; 1635 1636 if (res != TEE_SUCCESS && 1637 res != TEE_ERROR_SHORT_BUFFER && 1638 res != TEE_ERROR_BAD_PARAMETERS) 1639 TEE_Panic(res); 1640 1641 return res; 1642 } 1643 1644 TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation, 1645 const TEE_Attribute *params, 1646 uint32_t paramCount, const void *srcData, 1647 uint32_t srcLen, void *destData, 1648 uint32_t *destLen) 1649 { 1650 TEE_Result res = TEE_SUCCESS; 1651 struct utee_attribute ua[paramCount]; 1652 uint64_t dl = 0; 1653 1654 if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) 1655 TEE_Panic(0); 1656 1657 __utee_check_attr_in_annotation(params, paramCount); 1658 __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1659 1660 if (!operation->key1) 1661 TEE_Panic(0); 1662 if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 1663 TEE_Panic(0); 1664 if (operation->info.mode != TEE_MODE_DECRYPT) 1665 TEE_Panic(0); 1666 1667 __utee_from_attr(ua, params, paramCount); 1668 dl = *destLen; 1669 res = _utee_asymm_operate(operation->state, ua, paramCount, srcData, 1670 srcLen, destData, &dl); 1671 *destLen = dl; 1672 1673 if (res != TEE_SUCCESS && 1674 res != TEE_ERROR_SHORT_BUFFER && 1675 res != TEE_ERROR_BAD_PARAMETERS) 1676 TEE_Panic(res); 1677 1678 return res; 1679 } 1680 1681 TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation, 1682 const TEE_Attribute *params, 1683 uint32_t paramCount, const void *digest, 1684 uint32_t digestLen, void *signature, 1685 uint32_t *signatureLen) 1686 { 1687 TEE_Result res = TEE_SUCCESS; 1688 struct utee_attribute ua[paramCount]; 1689 uint64_t sl = 0; 1690 1691 if (operation == TEE_HANDLE_NULL || (!digest && digestLen)) 1692 TEE_Panic(0); 1693 1694 __utee_check_attr_in_annotation(params, paramCount); 1695 __utee_check_inout_annotation(signatureLen, sizeof(*signatureLen)); 1696 1697 if (!operation->key1) 1698 TEE_Panic(0); 1699 if (operation->info.operationClass != 1700 TEE_OPERATION_ASYMMETRIC_SIGNATURE) 1701 TEE_Panic(0); 1702 if (operation->info.mode != TEE_MODE_SIGN) 1703 TEE_Panic(0); 1704 1705 __utee_from_attr(ua, params, paramCount); 1706 sl = *signatureLen; 1707 res = _utee_asymm_operate(operation->state, ua, paramCount, digest, 1708 digestLen, signature, &sl); 1709 *signatureLen = sl; 1710 1711 if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER) 1712 TEE_Panic(res); 1713 1714 return res; 1715 } 1716 1717 TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation, 1718 const TEE_Attribute *params, 1719 uint32_t paramCount, const void *digest, 1720 uint32_t digestLen, 1721 const void *signature, 1722 uint32_t signatureLen) 1723 { 1724 TEE_Result res; 1725 struct utee_attribute ua[paramCount]; 1726 1727 if (operation == TEE_HANDLE_NULL || 1728 (digest == NULL && digestLen != 0) || 1729 (signature == NULL && signatureLen != 0)) 1730 TEE_Panic(0); 1731 1732 __utee_check_attr_in_annotation(params, paramCount); 1733 1734 if (!operation->key1) 1735 TEE_Panic(0); 1736 if (operation->info.operationClass != 1737 TEE_OPERATION_ASYMMETRIC_SIGNATURE) 1738 TEE_Panic(0); 1739 if (operation->info.mode != TEE_MODE_VERIFY) 1740 TEE_Panic(0); 1741 1742 __utee_from_attr(ua, params, paramCount); 1743 res = _utee_asymm_verify(operation->state, ua, paramCount, digest, 1744 digestLen, signature, signatureLen); 1745 1746 if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID) 1747 TEE_Panic(res); 1748 1749 return res; 1750 } 1751 1752 /* Cryptographic Operations API - Key Derivation Functions */ 1753 1754 void TEE_DeriveKey(TEE_OperationHandle operation, 1755 const TEE_Attribute *params, uint32_t paramCount, 1756 TEE_ObjectHandle derivedKey) 1757 { 1758 TEE_Result res; 1759 TEE_ObjectInfo key_info; 1760 struct utee_attribute ua[paramCount]; 1761 1762 if (operation == TEE_HANDLE_NULL || derivedKey == 0) 1763 TEE_Panic(0); 1764 1765 __utee_check_attr_in_annotation(params, paramCount); 1766 1767 if (TEE_ALG_GET_CLASS(operation->info.algorithm) != 1768 TEE_OPERATION_KEY_DERIVATION) 1769 TEE_Panic(0); 1770 1771 if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION) 1772 TEE_Panic(0); 1773 if (!operation->key1) 1774 TEE_Panic(0); 1775 if (operation->info.mode != TEE_MODE_DERIVE) 1776 TEE_Panic(0); 1777 if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0) 1778 TEE_Panic(0); 1779 1780 res = _utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info); 1781 if (res != TEE_SUCCESS) 1782 TEE_Panic(res); 1783 1784 if (key_info.objectType != TEE_TYPE_GENERIC_SECRET) 1785 TEE_Panic(0); 1786 if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1787 TEE_Panic(0); 1788 1789 __utee_from_attr(ua, params, paramCount); 1790 res = _utee_cryp_derive_key(operation->state, ua, paramCount, 1791 (unsigned long)derivedKey); 1792 if (res != TEE_SUCCESS) 1793 TEE_Panic(res); 1794 } 1795 1796 /* Cryptographic Operations API - Random Number Generation Functions */ 1797 1798 void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen) 1799 { 1800 TEE_Result res; 1801 1802 res = _utee_cryp_random_number_generate(randomBuffer, randomBufferLen); 1803 if (res != TEE_SUCCESS) 1804 TEE_Panic(res); 1805 } 1806 1807 int rand(void) 1808 { 1809 int rc; 1810 1811 TEE_GenerateRandom(&rc, sizeof(rc)); 1812 1813 /* 1814 * RAND_MAX is the larges int, INT_MAX which is all bits but the 1815 * highest bit set. 1816 */ 1817 return rc & RAND_MAX; 1818 } 1819 1820 TEE_Result TEE_IsAlgorithmSupported(uint32_t alg, uint32_t element) 1821 { 1822 if (IS_ENABLED(CFG_CRYPTO_AES)) { 1823 if (IS_ENABLED(CFG_CRYPTO_ECB)) { 1824 if (alg == TEE_ALG_AES_ECB_NOPAD) 1825 goto check_element_none; 1826 } 1827 if (IS_ENABLED(CFG_CRYPTO_CBC)) { 1828 if (alg == TEE_ALG_AES_CBC_NOPAD) 1829 goto check_element_none; 1830 } 1831 if (IS_ENABLED(CFG_CRYPTO_CTR)) { 1832 if (alg == TEE_ALG_AES_CTR) 1833 goto check_element_none; 1834 } 1835 if (IS_ENABLED(CFG_CRYPTO_CTS)) { 1836 if (alg == TEE_ALG_AES_CTS) 1837 goto check_element_none; 1838 } 1839 if (IS_ENABLED(CFG_CRYPTO_XTS)) { 1840 if (alg == TEE_ALG_AES_XTS) 1841 goto check_element_none; 1842 } 1843 if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) { 1844 if (alg == TEE_ALG_AES_CBC_MAC_NOPAD || 1845 alg == TEE_ALG_AES_CBC_MAC_PKCS5) 1846 goto check_element_none; 1847 } 1848 if (IS_ENABLED(CFG_CRYPTO_CMAC)) { 1849 if (alg == TEE_ALG_AES_CMAC) 1850 goto check_element_none; 1851 } 1852 if (IS_ENABLED(CFG_CRYPTO_CCM)) { 1853 if (alg == TEE_ALG_AES_CCM) 1854 goto check_element_none; 1855 } 1856 if (IS_ENABLED(CFG_CRYPTO_GCM)) { 1857 if (alg == TEE_ALG_AES_GCM) 1858 goto check_element_none; 1859 } 1860 } 1861 if (IS_ENABLED(CFG_CRYPTO_DES)) { 1862 if (IS_ENABLED(CFG_CRYPTO_ECB)) { 1863 if (alg == TEE_ALG_DES_ECB_NOPAD || 1864 alg == TEE_ALG_DES3_ECB_NOPAD) 1865 goto check_element_none; 1866 } 1867 if (IS_ENABLED(CFG_CRYPTO_CBC)) { 1868 if (alg == TEE_ALG_DES_CBC_NOPAD || 1869 alg == TEE_ALG_DES3_CBC_NOPAD) 1870 goto check_element_none; 1871 } 1872 if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) { 1873 if (alg == TEE_ALG_DES_CBC_MAC_NOPAD || 1874 alg == TEE_ALG_DES_CBC_MAC_PKCS5 || 1875 alg == TEE_ALG_DES3_CBC_MAC_NOPAD || 1876 alg == TEE_ALG_DES3_CBC_MAC_PKCS5) 1877 goto check_element_none; 1878 } 1879 } 1880 if (IS_ENABLED(CFG_CRYPTO_MD5)) { 1881 if (alg == TEE_ALG_MD5) 1882 goto check_element_none; 1883 } 1884 if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 1885 if (alg == TEE_ALG_SHA1) 1886 goto check_element_none; 1887 } 1888 if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 1889 if (alg == TEE_ALG_SHA224) 1890 goto check_element_none; 1891 } 1892 if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 1893 if (alg == TEE_ALG_SHA256) 1894 goto check_element_none; 1895 } 1896 if (IS_ENABLED(CFG_CRYPTO_SHA384)) { 1897 if (alg == TEE_ALG_SHA384) 1898 goto check_element_none; 1899 } 1900 if (IS_ENABLED(CFG_CRYPTO_SHA512)) { 1901 if (alg == TEE_ALG_SHA512) 1902 goto check_element_none; 1903 } 1904 if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) { 1905 if (alg == TEE_ALG_MD5SHA1) 1906 goto check_element_none; 1907 } 1908 if (IS_ENABLED(CFG_CRYPTO_HMAC)) { 1909 if (IS_ENABLED(CFG_CRYPTO_MD5)) { 1910 if (alg == TEE_ALG_HMAC_MD5) 1911 goto check_element_none; 1912 } 1913 if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 1914 if (alg == TEE_ALG_HMAC_SHA1) 1915 goto check_element_none; 1916 } 1917 if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 1918 if (alg == TEE_ALG_HMAC_SHA224) 1919 goto check_element_none; 1920 } 1921 if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 1922 if (alg == TEE_ALG_HMAC_SHA256) 1923 goto check_element_none; 1924 } 1925 if (IS_ENABLED(CFG_CRYPTO_SHA384)) { 1926 if (alg == TEE_ALG_HMAC_SHA384) 1927 goto check_element_none; 1928 } 1929 if (IS_ENABLED(CFG_CRYPTO_SHA512)) { 1930 if (alg == TEE_ALG_HMAC_SHA512) 1931 goto check_element_none; 1932 } 1933 if (IS_ENABLED(CFG_CRYPTO_SM3)) { 1934 if (alg == TEE_ALG_HMAC_SM3) 1935 goto check_element_none; 1936 } 1937 } 1938 if (IS_ENABLED(CFG_CRYPTO_SM3)) { 1939 if (alg == TEE_ALG_SM3) 1940 goto check_element_none; 1941 } 1942 if (IS_ENABLED(CFG_CRYPTO_SM4)) { 1943 if (IS_ENABLED(CFG_CRYPTO_ECB)) { 1944 if (alg == TEE_ALG_SM4_ECB_NOPAD) 1945 goto check_element_none; 1946 } 1947 if (IS_ENABLED(CFG_CRYPTO_CBC)) { 1948 if (alg == TEE_ALG_SM4_CBC_NOPAD) 1949 goto check_element_none; 1950 } 1951 if (IS_ENABLED(CFG_CRYPTO_CTR)) { 1952 if (alg == TEE_ALG_SM4_CTR) 1953 goto check_element_none; 1954 } 1955 } 1956 if (IS_ENABLED(CFG_CRYPTO_RSA)) { 1957 if (IS_ENABLED(CFG_CRYPTO_MD5)) { 1958 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5) 1959 goto check_element_none; 1960 } 1961 if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 1962 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA1 || 1963 alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1 || 1964 alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1) 1965 goto check_element_none; 1966 } 1967 if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) { 1968 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5SHA1) 1969 goto check_element_none; 1970 } 1971 if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 1972 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA224 || 1973 alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224 || 1974 alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224) 1975 goto check_element_none; 1976 } 1977 if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 1978 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA256 || 1979 alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256 || 1980 alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256) 1981 goto check_element_none; 1982 } 1983 if (IS_ENABLED(CFG_CRYPTO_SHA384)) { 1984 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA384 || 1985 alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384 || 1986 alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384) 1987 goto check_element_none; 1988 } 1989 if (IS_ENABLED(CFG_CRYPTO_SHA512)) { 1990 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA512 || 1991 alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512 || 1992 alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512) 1993 goto check_element_none; 1994 } 1995 if (IS_ENABLED(CFG_CRYPTO_RSASSA_NA1)) { 1996 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5) 1997 goto check_element_none; 1998 } 1999 if (alg == TEE_ALG_RSA_NOPAD) 2000 goto check_element_none; 2001 } 2002 if (IS_ENABLED(CFG_CRYPTO_DSA)) { 2003 if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 2004 if (alg == TEE_ALG_DSA_SHA1) 2005 goto check_element_none; 2006 } 2007 if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 2008 if (alg == TEE_ALG_DSA_SHA224) 2009 goto check_element_none; 2010 } 2011 if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 2012 if (alg == TEE_ALG_DSA_SHA256) 2013 goto check_element_none; 2014 } 2015 } 2016 if (IS_ENABLED(CFG_CRYPTO_DH)) { 2017 if (alg == TEE_ALG_DH_DERIVE_SHARED_SECRET) 2018 goto check_element_none; 2019 } 2020 if (IS_ENABLED(CFG_CRYPTO_ECC)) { 2021 if ((alg == TEE_ALG_ECDH_P192 || alg == TEE_ALG_ECDSA_P192) && 2022 element == TEE_ECC_CURVE_NIST_P192) 2023 return TEE_SUCCESS; 2024 if ((alg == TEE_ALG_ECDH_P224 || alg == TEE_ALG_ECDSA_P224) && 2025 element == TEE_ECC_CURVE_NIST_P224) 2026 return TEE_SUCCESS; 2027 if ((alg == TEE_ALG_ECDH_P256 || alg == TEE_ALG_ECDSA_P256) && 2028 element == TEE_ECC_CURVE_NIST_P256) 2029 return TEE_SUCCESS; 2030 if ((alg == TEE_ALG_ECDH_P384 || alg == TEE_ALG_ECDSA_P384) && 2031 element == TEE_ECC_CURVE_NIST_P384) 2032 return TEE_SUCCESS; 2033 if ((alg == TEE_ALG_ECDH_P521 || alg == TEE_ALG_ECDSA_P521) && 2034 element == TEE_ECC_CURVE_NIST_P521) 2035 return TEE_SUCCESS; 2036 } 2037 if (IS_ENABLED(CFG_CRYPTO_SM2_DSA)) { 2038 if (alg == TEE_ALG_SM2_DSA_SM3 && element == TEE_ECC_CURVE_SM2) 2039 return TEE_SUCCESS; 2040 } 2041 if (IS_ENABLED(CFG_CRYPTO_SM2_KEP)) { 2042 if (alg == TEE_ALG_SM2_KEP && element == TEE_ECC_CURVE_SM2) 2043 return TEE_SUCCESS; 2044 } 2045 if (IS_ENABLED(CFG_CRYPTO_SM2_PKE)) { 2046 if (alg == TEE_ALG_SM2_PKE && element == TEE_ECC_CURVE_SM2) 2047 return TEE_SUCCESS; 2048 } 2049 2050 return TEE_ERROR_NOT_SUPPORTED; 2051 check_element_none: 2052 if (element == TEE_CRYPTO_ELEMENT_NONE) 2053 return TEE_SUCCESS; 2054 return TEE_ERROR_NOT_SUPPORTED; 2055 } 2056