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