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