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