1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 * Copyright (c) 2021, SumUp Services GmbH 5 */ 6 #include <config.h> 7 #include <stdlib.h> 8 #include <string.h> 9 #include <string_ext.h> 10 #include <tee_api.h> 11 #include <tee_api_defines_extensions.h> 12 #include <tee_internal_api_extensions.h> 13 #include <utee_syscalls.h> 14 #include <utee_defines.h> 15 #include <util.h> 16 #include "tee_api_private.h" 17 18 struct __TEE_OperationHandle { 19 TEE_OperationInfo info; 20 TEE_ObjectHandle key1; 21 TEE_ObjectHandle key2; 22 uint32_t operationState;/* Operation state : INITIAL or ACTIVE */ 23 uint8_t *buffer; /* buffer to collect complete blocks */ 24 bool buffer_two_blocks; /* True if two blocks need to be buffered */ 25 size_t block_size; /* Block size of cipher */ 26 size_t buffer_offs; /* Offset in buffer */ 27 uint32_t state; /* Handle to state in TEE Core */ 28 }; 29 30 /* Cryptographic Operations API - Generic Operation Functions */ 31 32 TEE_Result TEE_AllocateOperation(TEE_OperationHandle *operation, 33 uint32_t algorithm, uint32_t mode, 34 uint32_t maxKeySize) 35 { 36 TEE_Result res; 37 TEE_OperationHandle op = TEE_HANDLE_NULL; 38 uint32_t handle_state = 0; 39 size_t block_size = 1; 40 uint32_t req_key_usage; 41 bool with_private_key = false; 42 bool buffer_two_blocks = false; 43 44 if (!operation) 45 TEE_Panic(0); 46 47 if (algorithm == TEE_ALG_AES_XTS || algorithm == TEE_ALG_SM2_KEP || 48 algorithm == TEE_ALG_SM4_XTS) 49 handle_state = TEE_HANDLE_FLAG_EXPECT_TWO_KEYS; 50 51 /* Check algorithm max key size */ 52 switch (algorithm) { 53 case TEE_ALG_DSA_SHA1: 54 if (maxKeySize < 512) 55 return TEE_ERROR_NOT_SUPPORTED; 56 if (maxKeySize > 1024) 57 return TEE_ERROR_NOT_SUPPORTED; 58 if (maxKeySize % 64 != 0) 59 return TEE_ERROR_NOT_SUPPORTED; 60 break; 61 62 case TEE_ALG_DSA_SHA224: 63 if (maxKeySize != 2048) 64 return TEE_ERROR_NOT_SUPPORTED; 65 break; 66 67 case TEE_ALG_DSA_SHA256: 68 if (maxKeySize != 2048 && maxKeySize != 3072) 69 return TEE_ERROR_NOT_SUPPORTED; 70 break; 71 72 case TEE_ALG_ECDSA_SHA1: 73 case __OPTEE_ALG_ECDSA_P192: 74 case __OPTEE_ALG_ECDH_P192: 75 if (maxKeySize != 192) 76 return TEE_ERROR_NOT_SUPPORTED; 77 break; 78 79 case TEE_ALG_ECDSA_SHA224: 80 case __OPTEE_ALG_ECDSA_P224: 81 case __OPTEE_ALG_ECDH_P224: 82 if (maxKeySize != 224) 83 return TEE_ERROR_NOT_SUPPORTED; 84 break; 85 86 case TEE_ALG_ECDSA_SHA256: 87 case __OPTEE_ALG_ECDSA_P256: 88 case __OPTEE_ALG_ECDH_P256: 89 case TEE_ALG_SM2_PKE: 90 case TEE_ALG_SM2_DSA_SM3: 91 if (maxKeySize != 256) 92 return TEE_ERROR_NOT_SUPPORTED; 93 break; 94 95 case TEE_ALG_SM2_KEP: 96 /* Two 256-bit keys */ 97 if (maxKeySize != 512) 98 return TEE_ERROR_NOT_SUPPORTED; 99 break; 100 101 case TEE_ALG_ECDSA_SHA384: 102 case __OPTEE_ALG_ECDSA_P384: 103 case __OPTEE_ALG_ECDH_P384: 104 if (maxKeySize != 384) 105 return TEE_ERROR_NOT_SUPPORTED; 106 break; 107 108 case TEE_ALG_ECDSA_SHA512: 109 case __OPTEE_ALG_ECDSA_P521: 110 case __OPTEE_ALG_ECDH_P521: 111 if (maxKeySize != 521) 112 return TEE_ERROR_NOT_SUPPORTED; 113 break; 114 115 case TEE_ALG_ECDH_DERIVE_SHARED_SECRET: 116 if (maxKeySize > 521) 117 return TEE_ERROR_NOT_SUPPORTED; 118 break; 119 120 case TEE_ALG_ED25519: 121 case TEE_ALG_X25519: 122 if (maxKeySize != 256) 123 return TEE_ERROR_NOT_SUPPORTED; 124 break; 125 default: 126 break; 127 } 128 129 /* Check algorithm mode (and maxKeySize for digests) */ 130 switch (algorithm) { 131 case TEE_ALG_AES_CTS: 132 case TEE_ALG_AES_XTS: 133 case TEE_ALG_SM4_XTS: 134 buffer_two_blocks = true; 135 fallthrough; 136 case TEE_ALG_AES_ECB_NOPAD: 137 case TEE_ALG_AES_CBC_NOPAD: 138 case TEE_ALG_AES_CCM: 139 case TEE_ALG_DES_ECB_NOPAD: 140 case TEE_ALG_DES_CBC_NOPAD: 141 case TEE_ALG_DES3_ECB_NOPAD: 142 case TEE_ALG_DES3_CBC_NOPAD: 143 case TEE_ALG_SM4_ECB_NOPAD: 144 case TEE_ALG_SM4_CBC_NOPAD: 145 case TEE_ALG_SM4_CTR: 146 if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES) 147 block_size = TEE_AES_BLOCK_SIZE; 148 else if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_SM4) 149 block_size = TEE_SM4_BLOCK_SIZE; 150 else 151 block_size = TEE_DES_BLOCK_SIZE; 152 fallthrough; 153 case TEE_ALG_AES_CTR: 154 case TEE_ALG_AES_GCM: 155 if (mode == TEE_MODE_ENCRYPT) 156 req_key_usage = TEE_USAGE_ENCRYPT; 157 else if (mode == TEE_MODE_DECRYPT) 158 req_key_usage = TEE_USAGE_DECRYPT; 159 else 160 return TEE_ERROR_NOT_SUPPORTED; 161 break; 162 163 #if defined(CFG_CRYPTO_RSASSA_NA1) 164 case TEE_ALG_RSASSA_PKCS1_V1_5: 165 #endif 166 case TEE_ALG_RSASSA_PKCS1_V1_5_MD5: 167 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1: 168 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224: 169 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256: 170 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384: 171 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512: 172 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1: 173 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224: 174 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256: 175 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384: 176 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512: 177 case TEE_ALG_DSA_SHA1: 178 case TEE_ALG_DSA_SHA224: 179 case TEE_ALG_DSA_SHA256: 180 case TEE_ALG_ECDSA_SHA1: 181 case TEE_ALG_ECDSA_SHA224: 182 case TEE_ALG_ECDSA_SHA256: 183 case TEE_ALG_ECDSA_SHA384: 184 case TEE_ALG_ECDSA_SHA512: 185 case __OPTEE_ALG_ECDSA_P192: 186 case __OPTEE_ALG_ECDSA_P224: 187 case __OPTEE_ALG_ECDSA_P256: 188 case __OPTEE_ALG_ECDSA_P384: 189 case __OPTEE_ALG_ECDSA_P521: 190 case TEE_ALG_SM2_DSA_SM3: 191 case TEE_ALG_ED25519: 192 if (mode == TEE_MODE_SIGN) { 193 with_private_key = true; 194 req_key_usage = TEE_USAGE_SIGN; 195 } else if (mode == TEE_MODE_VERIFY) { 196 req_key_usage = TEE_USAGE_VERIFY; 197 } else { 198 return TEE_ERROR_NOT_SUPPORTED; 199 } 200 break; 201 202 case TEE_ALG_RSAES_PKCS1_V1_5: 203 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1: 204 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224: 205 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256: 206 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384: 207 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512: 208 case TEE_ALG_SM2_PKE: 209 if (mode == TEE_MODE_ENCRYPT) { 210 req_key_usage = TEE_USAGE_ENCRYPT; 211 } else if (mode == TEE_MODE_DECRYPT) { 212 with_private_key = true; 213 req_key_usage = TEE_USAGE_DECRYPT; 214 } else { 215 return TEE_ERROR_NOT_SUPPORTED; 216 } 217 break; 218 219 case TEE_ALG_RSA_NOPAD: 220 if (mode == TEE_MODE_ENCRYPT) { 221 req_key_usage = TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY; 222 } else if (mode == TEE_MODE_DECRYPT) { 223 with_private_key = true; 224 req_key_usage = TEE_USAGE_DECRYPT | TEE_USAGE_SIGN; 225 } else { 226 return TEE_ERROR_NOT_SUPPORTED; 227 } 228 break; 229 230 case TEE_ALG_DH_DERIVE_SHARED_SECRET: 231 case TEE_ALG_ECDH_DERIVE_SHARED_SECRET: 232 case __OPTEE_ALG_ECDH_P192: 233 case __OPTEE_ALG_ECDH_P224: 234 case __OPTEE_ALG_ECDH_P256: 235 case __OPTEE_ALG_ECDH_P384: 236 case __OPTEE_ALG_ECDH_P521: 237 case TEE_ALG_HKDF_MD5_DERIVE_KEY: 238 case TEE_ALG_HKDF_SHA1_DERIVE_KEY: 239 case TEE_ALG_HKDF_SHA224_DERIVE_KEY: 240 case TEE_ALG_HKDF_SHA256_DERIVE_KEY: 241 case TEE_ALG_HKDF_SHA384_DERIVE_KEY: 242 case TEE_ALG_HKDF_SHA512_DERIVE_KEY: 243 case TEE_ALG_CONCAT_KDF_SHA1_DERIVE_KEY: 244 case TEE_ALG_CONCAT_KDF_SHA224_DERIVE_KEY: 245 case TEE_ALG_CONCAT_KDF_SHA256_DERIVE_KEY: 246 case TEE_ALG_CONCAT_KDF_SHA384_DERIVE_KEY: 247 case TEE_ALG_CONCAT_KDF_SHA512_DERIVE_KEY: 248 case TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY: 249 case TEE_ALG_SM2_KEP: 250 case TEE_ALG_X25519: 251 if (mode != TEE_MODE_DERIVE) 252 return TEE_ERROR_NOT_SUPPORTED; 253 with_private_key = true; 254 req_key_usage = TEE_USAGE_DERIVE; 255 break; 256 257 case TEE_ALG_MD5: 258 case TEE_ALG_SHA1: 259 case TEE_ALG_SHA224: 260 case TEE_ALG_SHA256: 261 case TEE_ALG_SHA384: 262 case TEE_ALG_SHA512: 263 case TEE_ALG_SM3: 264 if (mode != TEE_MODE_DIGEST) 265 return TEE_ERROR_NOT_SUPPORTED; 266 if (maxKeySize) 267 return TEE_ERROR_NOT_SUPPORTED; 268 /* v1.1: flags always set for digest operations */ 269 handle_state |= TEE_HANDLE_FLAG_KEY_SET; 270 req_key_usage = 0; 271 break; 272 273 case TEE_ALG_DES_CBC_MAC_NOPAD: 274 case TEE_ALG_AES_CBC_MAC_NOPAD: 275 case TEE_ALG_AES_CBC_MAC_PKCS5: 276 case TEE_ALG_AES_CMAC: 277 case TEE_ALG_DES_CBC_MAC_PKCS5: 278 case TEE_ALG_DES3_CBC_MAC_NOPAD: 279 case TEE_ALG_DES3_CBC_MAC_PKCS5: 280 case TEE_ALG_DES3_CMAC: 281 case TEE_ALG_HMAC_MD5: 282 case TEE_ALG_HMAC_SHA1: 283 case TEE_ALG_HMAC_SHA224: 284 case TEE_ALG_HMAC_SHA256: 285 case TEE_ALG_HMAC_SHA384: 286 case TEE_ALG_HMAC_SHA512: 287 case TEE_ALG_HMAC_SM3: 288 if (mode != TEE_MODE_MAC) 289 return TEE_ERROR_NOT_SUPPORTED; 290 req_key_usage = TEE_USAGE_MAC; 291 break; 292 293 default: 294 return TEE_ERROR_NOT_SUPPORTED; 295 } 296 297 op = TEE_Malloc(sizeof(*op), TEE_MALLOC_FILL_ZERO); 298 if (!op) 299 return TEE_ERROR_OUT_OF_MEMORY; 300 301 op->info.algorithm = algorithm; 302 op->info.operationClass = TEE_ALG_GET_CLASS(algorithm); 303 #ifdef CFG_CRYPTO_RSASSA_NA1 304 if (algorithm == TEE_ALG_RSASSA_PKCS1_V1_5) 305 op->info.operationClass = TEE_OPERATION_ASYMMETRIC_SIGNATURE; 306 #endif 307 op->info.mode = mode; 308 op->info.digestLength = TEE_ALG_GET_DIGEST_SIZE(algorithm); 309 op->info.maxKeySize = maxKeySize; 310 op->info.requiredKeyUsage = req_key_usage; 311 op->info.handleState = handle_state; 312 313 if (block_size > 1) { 314 size_t buffer_size = block_size; 315 316 if (buffer_two_blocks) 317 buffer_size *= 2; 318 319 op->buffer = TEE_Malloc(buffer_size, 320 TEE_USER_MEM_HINT_NO_FILL_ZERO); 321 if (op->buffer == NULL) { 322 res = TEE_ERROR_OUT_OF_MEMORY; 323 goto out; 324 } 325 } 326 op->block_size = block_size; 327 op->buffer_two_blocks = buffer_two_blocks; 328 329 if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) { 330 uint32_t mks = maxKeySize; 331 TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm, 332 with_private_key); 333 334 /* 335 * If two keys are expected the max key size is the sum of 336 * the size of both keys. 337 */ 338 if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) 339 mks /= 2; 340 341 res = TEE_AllocateTransientObject(key_type, mks, &op->key1); 342 if (res != TEE_SUCCESS) 343 goto out; 344 345 if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) { 346 res = TEE_AllocateTransientObject(key_type, mks, 347 &op->key2); 348 if (res != TEE_SUCCESS) 349 goto out; 350 } 351 } 352 353 res = _utee_cryp_state_alloc(algorithm, mode, (unsigned long)op->key1, 354 (unsigned long)op->key2, &op->state); 355 if (res != TEE_SUCCESS) 356 goto out; 357 358 /* 359 * Initialize digest operations 360 * Other multi-stage operations initialized w/ TEE_xxxInit functions 361 * Non-applicable on asymmetric operations 362 */ 363 if (TEE_ALG_GET_CLASS(algorithm) == TEE_OPERATION_DIGEST) { 364 res = _utee_hash_init(op->state, NULL, 0); 365 if (res != TEE_SUCCESS) 366 goto out; 367 /* v1.1: flags always set for digest operations */ 368 op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 369 } 370 371 op->operationState = TEE_OPERATION_STATE_INITIAL; 372 373 *operation = op; 374 375 out: 376 if (res != TEE_SUCCESS) { 377 if (res != TEE_ERROR_OUT_OF_MEMORY && 378 res != TEE_ERROR_NOT_SUPPORTED) 379 TEE_Panic(res); 380 if (op) { 381 if (op->state) { 382 TEE_FreeOperation(op); 383 } else { 384 TEE_Free(op->buffer); 385 TEE_FreeTransientObject(op->key1); 386 TEE_FreeTransientObject(op->key2); 387 TEE_Free(op); 388 } 389 } 390 } 391 392 return res; 393 } 394 395 void TEE_FreeOperation(TEE_OperationHandle operation) 396 { 397 TEE_Result res; 398 399 if (operation == TEE_HANDLE_NULL) 400 return; 401 402 /* 403 * Note that keys should not be freed here, since they are 404 * claimed by the operation they will be freed by 405 * utee_cryp_state_free(). 406 */ 407 res = _utee_cryp_state_free(operation->state); 408 if (res != TEE_SUCCESS) 409 TEE_Panic(res); 410 411 TEE_Free(operation->buffer); 412 TEE_Free(operation); 413 } 414 415 void __GP11_TEE_FreeOperation(TEE_OperationHandle operation) 416 { 417 if (operation == TEE_HANDLE_NULL) 418 TEE_Panic(0); 419 TEE_FreeOperation(operation); 420 } 421 422 void TEE_GetOperationInfo(TEE_OperationHandle operation, 423 TEE_OperationInfo *operationInfo) 424 { 425 if (operation == TEE_HANDLE_NULL) 426 TEE_Panic(0); 427 428 __utee_check_out_annotation(operationInfo, sizeof(*operationInfo)); 429 430 *operationInfo = operation->info; 431 if (operationInfo->handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) { 432 operationInfo->keySize = 0; 433 operationInfo->requiredKeyUsage = 0; 434 } 435 } 436 437 TEE_Result TEE_GetOperationInfoMultiple(TEE_OperationHandle op, 438 TEE_OperationInfoMultiple *op_info, 439 size_t *size) 440 { 441 TEE_Result res = TEE_SUCCESS; 442 TEE_ObjectInfo kinfo = { }; 443 size_t max_key_count = 0; 444 bool two_keys = false; 445 446 if (op == TEE_HANDLE_NULL) { 447 res = TEE_ERROR_BAD_PARAMETERS; 448 goto out; 449 } 450 451 __utee_check_outbuf_annotation(op_info, size); 452 453 if (*size < sizeof(*op_info)) { 454 res = TEE_ERROR_BAD_PARAMETERS; 455 goto out; 456 } 457 max_key_count = (*size - sizeof(*op_info)) / 458 sizeof(TEE_OperationInfoKey); 459 460 TEE_MemFill(op_info, 0, *size); 461 462 /* Two keys flag (TEE_ALG_AES_XTS only) */ 463 two_keys = op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS; 464 465 if (op->info.mode == TEE_MODE_DIGEST) { 466 op_info->numberOfKeys = 0; 467 } else if (!two_keys) { 468 if (max_key_count < 1) { 469 res = TEE_ERROR_SHORT_BUFFER; 470 goto out; 471 } 472 473 res = TEE_GetObjectInfo1(op->key1, &kinfo); 474 /* Key1 is not a valid handle, "can't happen". */ 475 if (res) 476 goto out; 477 478 op_info->keyInformation[0].keySize = kinfo.objectSize; 479 op_info->keyInformation[0].requiredKeyUsage = 480 op->info.requiredKeyUsage; 481 op_info->numberOfKeys = 1; 482 } else { 483 if (max_key_count < 2) { 484 res = TEE_ERROR_SHORT_BUFFER; 485 goto out; 486 } 487 488 res = TEE_GetObjectInfo1(op->key1, &kinfo); 489 /* Key1 is not a valid handle, "can't happen". */ 490 if (res) 491 goto out; 492 493 op_info->keyInformation[0].keySize = kinfo.objectSize; 494 op_info->keyInformation[0].requiredKeyUsage = 495 op->info.requiredKeyUsage; 496 497 res = TEE_GetObjectInfo1(op->key2, &kinfo); 498 /* Key2 is not a valid handle, "can't happen". */ 499 if (res) 500 goto out; 501 502 op_info->keyInformation[1].keySize = kinfo.objectSize; 503 op_info->keyInformation[1].requiredKeyUsage = 504 op->info.requiredKeyUsage; 505 506 op_info->numberOfKeys = 2; 507 } 508 509 op_info->algorithm = op->info.algorithm; 510 op_info->operationClass = op->info.operationClass; 511 op_info->mode = op->info.mode; 512 op_info->digestLength = op->info.digestLength; 513 op_info->maxKeySize = op->info.maxKeySize; 514 op_info->handleState = op->info.handleState; 515 op_info->operationState = op->operationState; 516 517 out: 518 if (res != TEE_SUCCESS && 519 res != TEE_ERROR_SHORT_BUFFER) 520 TEE_Panic(res); 521 522 return res; 523 } 524 525 TEE_Result 526 __GP11_TEE_GetOperationInfoMultiple(TEE_OperationHandle operation, 527 TEE_OperationInfoMultiple *info, 528 uint32_t *operationSize) 529 { 530 TEE_Result res = TEE_SUCCESS; 531 size_t s = 0; 532 533 __utee_check_gp11_outbuf_annotation(info, operationSize); 534 s = *operationSize; 535 res = TEE_GetOperationInfoMultiple(operation, info, &s); 536 *operationSize = s; 537 return res; 538 } 539 540 void TEE_ResetOperation(TEE_OperationHandle operation) 541 { 542 TEE_Result res; 543 544 if (operation == TEE_HANDLE_NULL) 545 TEE_Panic(0); 546 547 if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET)) 548 TEE_Panic(0); 549 550 operation->operationState = TEE_OPERATION_STATE_INITIAL; 551 552 if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 553 res = _utee_hash_init(operation->state, NULL, 0); 554 if (res != TEE_SUCCESS) 555 TEE_Panic(res); 556 operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 557 } else { 558 operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 559 } 560 } 561 562 TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation, 563 TEE_ObjectHandle key) 564 { 565 TEE_Result res; 566 uint32_t key_size = 0; 567 TEE_ObjectInfo key_info; 568 569 if (operation == TEE_HANDLE_NULL) { 570 res = TEE_ERROR_BAD_PARAMETERS; 571 goto out; 572 } 573 574 if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 575 res = TEE_ERROR_BAD_PARAMETERS; 576 goto out; 577 } 578 579 if (key == TEE_HANDLE_NULL) { 580 /* Operation key cleared */ 581 TEE_ResetTransientObject(operation->key1); 582 operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 583 return TEE_SUCCESS; 584 } 585 586 /* No key for digest operation */ 587 if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 588 res = TEE_ERROR_BAD_PARAMETERS; 589 goto out; 590 } 591 592 /* Two keys flag not expected (TEE_ALG_AES_XTS excluded) */ 593 if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 594 0) { 595 res = TEE_ERROR_BAD_PARAMETERS; 596 goto out; 597 } 598 599 res = TEE_GetObjectInfo1(key, &key_info); 600 /* Key is not a valid handle */ 601 if (res != TEE_SUCCESS) 602 goto out; 603 604 /* Supplied key has to meet required usage */ 605 if ((key_info.objectUsage & operation->info.requiredKeyUsage) != 606 operation->info.requiredKeyUsage) { 607 res = TEE_ERROR_BAD_PARAMETERS; 608 goto out; 609 } 610 611 if (operation->info.maxKeySize < key_info.objectSize) { 612 res = TEE_ERROR_BAD_PARAMETERS; 613 goto out; 614 } 615 616 key_size = key_info.objectSize; 617 618 TEE_ResetTransientObject(operation->key1); 619 operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 620 621 res = TEE_CopyObjectAttributes1(operation->key1, key); 622 if (res != TEE_SUCCESS) 623 goto out; 624 625 operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 626 627 operation->info.keySize = key_size; 628 629 out: 630 if (res != TEE_SUCCESS && 631 res != TEE_ERROR_CORRUPT_OBJECT && 632 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 633 TEE_Panic(res); 634 635 return res; 636 } 637 638 TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation, 639 TEE_ObjectHandle key1, TEE_ObjectHandle key2) 640 { 641 TEE_Result res; 642 uint32_t key_size = 0; 643 TEE_ObjectInfo key_info1; 644 TEE_ObjectInfo key_info2; 645 646 if (operation == TEE_HANDLE_NULL) { 647 res = TEE_ERROR_BAD_PARAMETERS; 648 goto out; 649 } 650 651 if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 652 res = TEE_ERROR_BAD_PARAMETERS; 653 goto out; 654 } 655 656 /* 657 * Key1/Key2 and/or are not initialized and 658 * Either both keys are NULL or both are not NULL 659 */ 660 if (!key1 && !key2) { 661 /* Clear the keys */ 662 TEE_ResetTransientObject(operation->key1); 663 TEE_ResetTransientObject(operation->key2); 664 operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 665 return TEE_SUCCESS; 666 } else if (!key1 || !key2) { 667 /* Both keys are obviously not valid. */ 668 res = TEE_ERROR_BAD_PARAMETERS; 669 goto out; 670 } 671 672 /* No key for digest operation */ 673 if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 674 res = TEE_ERROR_BAD_PARAMETERS; 675 goto out; 676 } 677 678 /* Two keys flag expected (TEE_ALG_AES_XTS and TEE_ALG_SM2_KEP only) */ 679 if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 680 0) { 681 res = TEE_ERROR_BAD_PARAMETERS; 682 goto out; 683 } 684 685 res = TEE_GetObjectInfo1(key1, &key_info1); 686 /* Key1 is not a valid handle */ 687 if (res != TEE_SUCCESS) 688 goto out; 689 690 /* Supplied key has to meet required usage */ 691 if ((key_info1.objectUsage & operation->info. 692 requiredKeyUsage) != operation->info.requiredKeyUsage) { 693 res = TEE_ERROR_BAD_PARAMETERS; 694 goto out; 695 } 696 697 res = TEE_GetObjectInfo1(key2, &key_info2); 698 /* Key2 is not a valid handle */ 699 if (res != TEE_SUCCESS) { 700 if (res == TEE_ERROR_CORRUPT_OBJECT) 701 res = TEE_ERROR_CORRUPT_OBJECT_2; 702 goto out; 703 } 704 705 /* Supplied key has to meet required usage */ 706 if ((key_info2.objectUsage & operation->info. 707 requiredKeyUsage) != operation->info.requiredKeyUsage) { 708 res = TEE_ERROR_BAD_PARAMETERS; 709 goto out; 710 } 711 712 /* 713 * All the multi key algorithm currently supported requires the keys to 714 * be of equal size. 715 */ 716 if (key_info1.objectSize != key_info2.objectSize) { 717 res = TEE_ERROR_BAD_PARAMETERS; 718 goto out; 719 720 } 721 722 if (operation->info.maxKeySize < key_info1.objectSize) { 723 res = TEE_ERROR_BAD_PARAMETERS; 724 goto out; 725 } 726 727 /* 728 * Odd that only the size of one key should be reported while 729 * size of two key are used when allocating the operation. 730 */ 731 key_size = key_info1.objectSize; 732 733 TEE_ResetTransientObject(operation->key1); 734 TEE_ResetTransientObject(operation->key2); 735 operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 736 737 res = TEE_CopyObjectAttributes1(operation->key1, key1); 738 if (res != TEE_SUCCESS) 739 goto out; 740 res = TEE_CopyObjectAttributes1(operation->key2, key2); 741 if (res != TEE_SUCCESS) { 742 if (res == TEE_ERROR_CORRUPT_OBJECT) 743 res = TEE_ERROR_CORRUPT_OBJECT_2; 744 goto out; 745 } 746 747 operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 748 749 operation->info.keySize = key_size; 750 751 out: 752 if (res != TEE_SUCCESS && 753 res != TEE_ERROR_CORRUPT_OBJECT && 754 res != TEE_ERROR_CORRUPT_OBJECT_2 && 755 res != TEE_ERROR_STORAGE_NOT_AVAILABLE && 756 res != TEE_ERROR_STORAGE_NOT_AVAILABLE_2) 757 TEE_Panic(res); 758 759 return res; 760 } 761 762 void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op) 763 { 764 TEE_Result res; 765 766 if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL) 767 TEE_Panic(0); 768 if (dst_op->info.algorithm != src_op->info.algorithm) 769 TEE_Panic(0); 770 if (dst_op->info.mode != src_op->info.mode) 771 TEE_Panic(0); 772 if (src_op->info.operationClass != TEE_OPERATION_DIGEST) { 773 TEE_ObjectHandle key1 = TEE_HANDLE_NULL; 774 TEE_ObjectHandle key2 = TEE_HANDLE_NULL; 775 776 if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) { 777 key1 = src_op->key1; 778 key2 = src_op->key2; 779 } 780 781 if ((src_op->info.handleState & 782 TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) { 783 TEE_SetOperationKey(dst_op, key1); 784 } else { 785 TEE_SetOperationKey2(dst_op, key1, key2); 786 } 787 } 788 dst_op->info.handleState = src_op->info.handleState; 789 dst_op->info.keySize = src_op->info.keySize; 790 dst_op->info.digestLength = src_op->info.digestLength; 791 dst_op->operationState = src_op->operationState; 792 793 if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks || 794 dst_op->block_size != src_op->block_size) 795 TEE_Panic(0); 796 797 if (dst_op->buffer != NULL) { 798 if (src_op->buffer == NULL) 799 TEE_Panic(0); 800 801 memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs); 802 dst_op->buffer_offs = src_op->buffer_offs; 803 } else if (src_op->buffer != NULL) { 804 TEE_Panic(0); 805 } 806 807 res = _utee_cryp_state_copy(dst_op->state, src_op->state); 808 if (res != TEE_SUCCESS) 809 TEE_Panic(res); 810 } 811 812 /* Cryptographic Operations API - Message Digest Functions */ 813 814 static void init_hash_operation(TEE_OperationHandle operation, const void *IV, 815 uint32_t IVLen) 816 { 817 TEE_Result res; 818 819 /* 820 * Note : IV and IVLen are never used in current implementation 821 * This is why coherent values of IV and IVLen are not checked 822 */ 823 res = _utee_hash_init(operation->state, IV, IVLen); 824 if (res != TEE_SUCCESS) 825 TEE_Panic(res); 826 operation->buffer_offs = 0; 827 operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 828 } 829 830 void TEE_DigestUpdate(TEE_OperationHandle operation, 831 const void *chunk, size_t chunkSize) 832 { 833 TEE_Result res = TEE_ERROR_GENERIC; 834 835 if (operation == TEE_HANDLE_NULL || 836 operation->info.operationClass != TEE_OPERATION_DIGEST) 837 TEE_Panic(0); 838 839 operation->operationState = TEE_OPERATION_STATE_ACTIVE; 840 841 res = _utee_hash_update(operation->state, chunk, chunkSize); 842 if (res != TEE_SUCCESS) 843 TEE_Panic(res); 844 } 845 846 void __GP11_TEE_DigestUpdate(TEE_OperationHandle operation, 847 const void *chunk, uint32_t chunkSize) 848 { 849 return TEE_DigestUpdate(operation, chunk, chunkSize); 850 } 851 852 TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void *chunk, 853 size_t chunkLen, void *hash, size_t *hashLen) 854 { 855 TEE_Result res; 856 uint64_t hl; 857 858 if ((operation == TEE_HANDLE_NULL) || 859 (!chunk && chunkLen) || 860 (operation->info.operationClass != TEE_OPERATION_DIGEST)) { 861 res = TEE_ERROR_BAD_PARAMETERS; 862 goto out; 863 } 864 __utee_check_inout_annotation(hashLen, sizeof(*hashLen)); 865 866 hl = *hashLen; 867 res = _utee_hash_final(operation->state, chunk, chunkLen, hash, &hl); 868 *hashLen = hl; 869 if (res != TEE_SUCCESS) 870 goto out; 871 872 /* Reset operation state */ 873 init_hash_operation(operation, NULL, 0); 874 875 operation->operationState = TEE_OPERATION_STATE_INITIAL; 876 877 out: 878 if (res != TEE_SUCCESS && 879 res != TEE_ERROR_SHORT_BUFFER) 880 TEE_Panic(res); 881 882 return res; 883 } 884 885 TEE_Result __GP11_TEE_DigestDoFinal(TEE_OperationHandle operation, 886 const void *chunk, uint32_t chunkLen, 887 void *hash, uint32_t *hashLen) 888 { 889 TEE_Result res = TEE_SUCCESS; 890 size_t l = 0; 891 892 __utee_check_inout_annotation(hashLen, sizeof(*hashLen)); 893 l = *hashLen; 894 res = TEE_DigestDoFinal(operation, chunk, chunkLen, hash, &l); 895 *hashLen = l; 896 return res; 897 } 898 899 /* Cryptographic Operations API - Symmetric Cipher Functions */ 900 901 void TEE_CipherInit(TEE_OperationHandle operation, const void *IV, 902 size_t IVLen) 903 { 904 TEE_Result res; 905 906 if (operation == TEE_HANDLE_NULL) 907 TEE_Panic(0); 908 909 if (operation->info.operationClass != TEE_OPERATION_CIPHER) 910 TEE_Panic(0); 911 912 if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 913 !(operation->key1)) 914 TEE_Panic(0); 915 916 if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 917 TEE_ResetOperation(operation); 918 919 if (IV && IVLen) { 920 if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD || 921 operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD || 922 operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || 923 operation->info.algorithm == TEE_ALG_SM4_ECB_NOPAD) 924 TEE_Panic(0); 925 } 926 927 operation->operationState = TEE_OPERATION_STATE_ACTIVE; 928 929 res = _utee_cipher_init(operation->state, IV, IVLen); 930 if (res != TEE_SUCCESS) 931 TEE_Panic(res); 932 933 operation->buffer_offs = 0; 934 operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 935 } 936 937 void __GP11_TEE_CipherInit(TEE_OperationHandle operation, const void *IV, 938 uint32_t IVLen) 939 { 940 return TEE_CipherInit(operation, IV, IVLen); 941 } 942 943 static TEE_Result tee_buffer_update( 944 TEE_OperationHandle op, 945 TEE_Result(*update_func)(unsigned long state, const void *src, 946 size_t slen, void *dst, uint64_t *dlen), 947 const void *src_data, size_t src_len, 948 void *dest_data, uint64_t *dest_len) 949 { 950 TEE_Result res; 951 const uint8_t *src = src_data; 952 size_t slen = src_len; 953 uint8_t *dst = dest_data; 954 size_t dlen = *dest_len; 955 size_t acc_dlen = 0; 956 uint64_t tmp_dlen; 957 size_t l; 958 size_t buffer_size; 959 size_t buffer_left; 960 961 if (!src) { 962 if (slen) 963 TEE_Panic(0); 964 goto out; 965 } 966 967 if (op->buffer_two_blocks) { 968 buffer_size = op->block_size * 2; 969 buffer_left = 1; 970 } else { 971 buffer_size = op->block_size; 972 buffer_left = 0; 973 } 974 975 if (op->buffer_offs > 0) { 976 /* Fill up complete block */ 977 if (op->buffer_offs < op->block_size) 978 l = MIN(slen, op->block_size - op->buffer_offs); 979 else 980 l = MIN(slen, buffer_size - op->buffer_offs); 981 memcpy(op->buffer + op->buffer_offs, src, l); 982 op->buffer_offs += l; 983 src += l; 984 slen -= l; 985 if ((op->buffer_offs % op->block_size) != 0) 986 goto out; /* Nothing left to do */ 987 } 988 989 /* If we can feed from buffer */ 990 if ((op->buffer_offs > 0) && 991 ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) { 992 l = ROUNDUP(op->buffer_offs + slen - buffer_size, 993 op->block_size); 994 l = MIN(op->buffer_offs, l); 995 tmp_dlen = dlen; 996 res = update_func(op->state, op->buffer, l, dst, &tmp_dlen); 997 if (res != TEE_SUCCESS) 998 TEE_Panic(res); 999 dst += tmp_dlen; 1000 dlen -= tmp_dlen; 1001 acc_dlen += tmp_dlen; 1002 op->buffer_offs -= l; 1003 if (op->buffer_offs > 0) { 1004 /* 1005 * Slen is small enough to be contained in rest buffer. 1006 */ 1007 memcpy(op->buffer, op->buffer + l, buffer_size - l); 1008 memcpy(op->buffer + op->buffer_offs, src, slen); 1009 op->buffer_offs += slen; 1010 goto out; /* Nothing left to do */ 1011 } 1012 } 1013 1014 if (slen >= (buffer_size + buffer_left)) { 1015 /* Buffer is empty, feed as much as possible from src */ 1016 if (op->info.algorithm == TEE_ALG_AES_CTS) 1017 l = ROUNDUP(slen - buffer_size, op->block_size); 1018 else 1019 l = ROUNDUP(slen - buffer_size + 1, op->block_size); 1020 1021 tmp_dlen = dlen; 1022 res = update_func(op->state, src, l, dst, &tmp_dlen); 1023 if (res != TEE_SUCCESS) 1024 TEE_Panic(res); 1025 src += l; 1026 slen -= l; 1027 dst += tmp_dlen; 1028 dlen -= tmp_dlen; 1029 acc_dlen += tmp_dlen; 1030 } 1031 1032 /* Slen is small enough to be contained in buffer. */ 1033 memcpy(op->buffer + op->buffer_offs, src, slen); 1034 op->buffer_offs += slen; 1035 1036 out: 1037 *dest_len = acc_dlen; 1038 return TEE_SUCCESS; 1039 } 1040 1041 TEE_Result TEE_CipherUpdate(TEE_OperationHandle operation, const void *srcData, 1042 size_t srcLen, void *destData, size_t *destLen) 1043 { 1044 TEE_Result res; 1045 size_t req_dlen; 1046 uint64_t dl; 1047 1048 if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 1049 res = TEE_ERROR_BAD_PARAMETERS; 1050 goto out; 1051 } 1052 __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1053 1054 if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 1055 res = TEE_ERROR_BAD_PARAMETERS; 1056 goto out; 1057 } 1058 1059 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1060 res = TEE_ERROR_BAD_PARAMETERS; 1061 goto out; 1062 } 1063 1064 if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1065 res = TEE_ERROR_BAD_PARAMETERS; 1066 goto out; 1067 } 1068 1069 if (!srcData && !srcLen) { 1070 *destLen = 0; 1071 res = TEE_SUCCESS; 1072 goto out; 1073 } 1074 1075 /* Calculate required dlen */ 1076 if (operation->block_size > 1) { 1077 req_dlen = ((operation->buffer_offs + srcLen) / 1078 operation->block_size) * operation->block_size; 1079 } else { 1080 req_dlen = srcLen; 1081 } 1082 if (operation->buffer_two_blocks) { 1083 if (req_dlen > operation->block_size * 2) 1084 req_dlen -= operation->block_size * 2; 1085 else 1086 req_dlen = 0; 1087 } 1088 /* 1089 * Check that required destLen is big enough before starting to feed 1090 * data to the algorithm. Errors during feeding of data are fatal as we 1091 * can't restore sync with this API. 1092 */ 1093 if (*destLen < req_dlen) { 1094 *destLen = req_dlen; 1095 res = TEE_ERROR_SHORT_BUFFER; 1096 goto out; 1097 } 1098 1099 dl = *destLen; 1100 if (operation->block_size > 1) { 1101 res = tee_buffer_update(operation, _utee_cipher_update, srcData, 1102 srcLen, destData, &dl); 1103 } else { 1104 if (srcLen > 0) { 1105 res = _utee_cipher_update(operation->state, srcData, 1106 srcLen, destData, &dl); 1107 } else { 1108 res = TEE_SUCCESS; 1109 dl = 0; 1110 } 1111 } 1112 *destLen = dl; 1113 1114 out: 1115 if (res != TEE_SUCCESS && 1116 res != TEE_ERROR_SHORT_BUFFER) 1117 TEE_Panic(res); 1118 1119 return res; 1120 } 1121 1122 TEE_Result __GP11_TEE_CipherUpdate(TEE_OperationHandle operation, 1123 const void *srcData, uint32_t srcLen, 1124 void *destData, uint32_t *destLen) 1125 { 1126 TEE_Result res = TEE_SUCCESS; 1127 size_t dl = 0; 1128 1129 __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1130 dl = *destLen; 1131 res = TEE_CipherUpdate(operation, srcData, srcLen, destData, &dl); 1132 *destLen = dl; 1133 return res; 1134 } 1135 1136 TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation, 1137 const void *srcData, size_t srcLen, 1138 void *destData, size_t *destLen) 1139 { 1140 TEE_Result res = TEE_SUCCESS; 1141 uint8_t *dst = destData; 1142 size_t acc_dlen = 0; 1143 uint64_t tmp_dlen = 0; 1144 size_t req_dlen = 0; 1145 1146 if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 1147 res = TEE_ERROR_BAD_PARAMETERS; 1148 goto out; 1149 } 1150 if (destLen) 1151 __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1152 1153 if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 1154 res = TEE_ERROR_BAD_PARAMETERS; 1155 goto out; 1156 } 1157 1158 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1159 res = TEE_ERROR_BAD_PARAMETERS; 1160 goto out; 1161 } 1162 1163 if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1164 res = TEE_ERROR_BAD_PARAMETERS; 1165 goto out; 1166 } 1167 1168 /* 1169 * Check that the final block doesn't require padding for those 1170 * algorithms that requires client to supply padding. 1171 */ 1172 if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD || 1173 operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD || 1174 operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD || 1175 operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD || 1176 operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || 1177 operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD || 1178 operation->info.algorithm == TEE_ALG_SM4_ECB_NOPAD || 1179 operation->info.algorithm == TEE_ALG_SM4_CBC_NOPAD) { 1180 if (((operation->buffer_offs + srcLen) % operation->block_size) 1181 != 0) { 1182 res = TEE_ERROR_BAD_PARAMETERS; 1183 goto out; 1184 } 1185 } 1186 1187 /* 1188 * Check that required destLen is big enough before starting to feed 1189 * data to the algorithm. Errors during feeding of data are fatal as we 1190 * can't restore sync with this API. 1191 */ 1192 if (operation->block_size > 1) { 1193 req_dlen = operation->buffer_offs + srcLen; 1194 } else { 1195 req_dlen = srcLen; 1196 } 1197 if (destLen) 1198 tmp_dlen = *destLen; 1199 if (tmp_dlen < req_dlen) { 1200 if (destLen) 1201 *destLen = req_dlen; 1202 res = TEE_ERROR_SHORT_BUFFER; 1203 goto out; 1204 } 1205 1206 if (operation->block_size > 1) { 1207 if (srcLen) { 1208 res = tee_buffer_update(operation, _utee_cipher_update, 1209 srcData, srcLen, dst, 1210 &tmp_dlen); 1211 if (res != TEE_SUCCESS) 1212 goto out; 1213 1214 dst += tmp_dlen; 1215 acc_dlen += tmp_dlen; 1216 1217 tmp_dlen = *destLen - acc_dlen; 1218 } 1219 res = _utee_cipher_final(operation->state, operation->buffer, 1220 operation->buffer_offs, dst, 1221 &tmp_dlen); 1222 } else { 1223 res = _utee_cipher_final(operation->state, srcData, srcLen, dst, 1224 &tmp_dlen); 1225 } 1226 if (res != TEE_SUCCESS) 1227 goto out; 1228 1229 acc_dlen += tmp_dlen; 1230 if (destLen) 1231 *destLen = acc_dlen; 1232 1233 operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1234 1235 operation->operationState = TEE_OPERATION_STATE_INITIAL; 1236 1237 out: 1238 if (res != TEE_SUCCESS && 1239 res != TEE_ERROR_SHORT_BUFFER) 1240 TEE_Panic(res); 1241 1242 return res; 1243 } 1244 1245 TEE_Result __GP11_TEE_CipherDoFinal(TEE_OperationHandle operation, 1246 const void *srcData, uint32_t srcLen, 1247 void *destData, uint32_t *destLen) 1248 { 1249 TEE_Result res = TEE_SUCCESS; 1250 size_t dl = 0; 1251 1252 if (destLen) { 1253 __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1254 dl = *destLen; 1255 } 1256 res = TEE_CipherDoFinal(operation, srcData, srcLen, destData, &dl); 1257 if (destLen) 1258 *destLen = dl; 1259 return res; 1260 } 1261 1262 /* Cryptographic Operations API - MAC Functions */ 1263 1264 void TEE_MACInit(TEE_OperationHandle operation, const void *IV, size_t IVLen) 1265 { 1266 if (operation == TEE_HANDLE_NULL) 1267 TEE_Panic(0); 1268 1269 if (operation->info.operationClass != TEE_OPERATION_MAC) 1270 TEE_Panic(0); 1271 1272 if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 1273 !(operation->key1)) 1274 TEE_Panic(0); 1275 1276 if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 1277 TEE_ResetOperation(operation); 1278 1279 operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1280 1281 init_hash_operation(operation, IV, IVLen); 1282 } 1283 1284 void __GP11_TEE_MACInit(TEE_OperationHandle operation, const void *IV, 1285 uint32_t IVLen) 1286 { 1287 return TEE_MACInit(operation, IV, IVLen); 1288 } 1289 1290 void TEE_MACUpdate(TEE_OperationHandle operation, const void *chunk, 1291 size_t chunkSize) 1292 { 1293 TEE_Result res; 1294 1295 if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0)) 1296 TEE_Panic(0); 1297 1298 if (operation->info.operationClass != TEE_OPERATION_MAC) 1299 TEE_Panic(0); 1300 1301 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1302 TEE_Panic(0); 1303 1304 if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) 1305 TEE_Panic(0); 1306 1307 res = _utee_hash_update(operation->state, chunk, chunkSize); 1308 if (res != TEE_SUCCESS) 1309 TEE_Panic(res); 1310 } 1311 1312 void __GP11_TEE_MACUpdate(TEE_OperationHandle operation, const void *chunk, 1313 uint32_t chunkSize) 1314 { 1315 return TEE_MACUpdate(operation, chunk, chunkSize); 1316 } 1317 1318 TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation, 1319 const void *message, size_t messageLen, 1320 void *mac, size_t *macLen) 1321 { 1322 TEE_Result res; 1323 uint64_t ml; 1324 1325 if (operation == TEE_HANDLE_NULL || (!message && messageLen)) { 1326 res = TEE_ERROR_BAD_PARAMETERS; 1327 goto out; 1328 } 1329 __utee_check_inout_annotation(macLen, sizeof(*macLen)); 1330 1331 if (operation->info.operationClass != TEE_OPERATION_MAC) { 1332 res = TEE_ERROR_BAD_PARAMETERS; 1333 goto out; 1334 } 1335 1336 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1337 res = TEE_ERROR_BAD_PARAMETERS; 1338 goto out; 1339 } 1340 1341 if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1342 res = TEE_ERROR_BAD_PARAMETERS; 1343 goto out; 1344 } 1345 1346 ml = *macLen; 1347 res = _utee_hash_final(operation->state, message, messageLen, mac, &ml); 1348 *macLen = ml; 1349 if (res != TEE_SUCCESS) 1350 goto out; 1351 1352 operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1353 1354 operation->operationState = TEE_OPERATION_STATE_INITIAL; 1355 1356 out: 1357 if (res != TEE_SUCCESS && 1358 res != TEE_ERROR_SHORT_BUFFER) 1359 TEE_Panic(res); 1360 1361 return res; 1362 } 1363 1364 TEE_Result __GP11_TEE_MACComputeFinal(TEE_OperationHandle operation, 1365 const void *message, uint32_t messageLen, 1366 void *mac, uint32_t *macLen) 1367 { 1368 TEE_Result res = TEE_SUCCESS; 1369 size_t ml = 0; 1370 1371 __utee_check_inout_annotation(macLen, sizeof(*macLen)); 1372 ml = *macLen; 1373 res = TEE_MACComputeFinal(operation, message, messageLen, mac, &ml); 1374 *macLen = ml; 1375 return res; 1376 } 1377 1378 TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation, 1379 const void *message, size_t messageLen, 1380 const void *mac, size_t macLen) 1381 { 1382 TEE_Result res; 1383 uint8_t computed_mac[TEE_MAX_HASH_SIZE] = { 0 }; 1384 size_t computed_mac_size = TEE_MAX_HASH_SIZE; 1385 1386 if (operation->info.operationClass != TEE_OPERATION_MAC) { 1387 res = TEE_ERROR_BAD_PARAMETERS; 1388 goto out; 1389 } 1390 1391 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1392 res = TEE_ERROR_BAD_PARAMETERS; 1393 goto out; 1394 } 1395 1396 if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1397 res = TEE_ERROR_BAD_PARAMETERS; 1398 goto out; 1399 } 1400 1401 res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac, 1402 &computed_mac_size); 1403 if (res != TEE_SUCCESS) 1404 goto out; 1405 1406 if (computed_mac_size != macLen) { 1407 res = TEE_ERROR_MAC_INVALID; 1408 goto out; 1409 } 1410 1411 if (consttime_memcmp(mac, computed_mac, computed_mac_size) != 0) { 1412 res = TEE_ERROR_MAC_INVALID; 1413 goto out; 1414 } 1415 1416 operation->operationState = TEE_OPERATION_STATE_INITIAL; 1417 1418 out: 1419 if (res != TEE_SUCCESS && 1420 res != TEE_ERROR_MAC_INVALID) 1421 TEE_Panic(res); 1422 1423 return res; 1424 } 1425 1426 TEE_Result __GP11_TEE_MACCompareFinal(TEE_OperationHandle operation, 1427 const void *message, uint32_t messageLen, 1428 const void *mac, uint32_t macLen) 1429 { 1430 return TEE_MACCompareFinal(operation, message, messageLen, mac, macLen); 1431 } 1432 1433 /* Cryptographic Operations API - Authenticated Encryption Functions */ 1434 1435 TEE_Result TEE_AEInit(TEE_OperationHandle operation, const void *nonce, 1436 size_t nonceLen, uint32_t tagLen, size_t AADLen, 1437 size_t payloadLen) 1438 { 1439 TEE_Result res; 1440 1441 if (operation == TEE_HANDLE_NULL || nonce == NULL) { 1442 res = TEE_ERROR_BAD_PARAMETERS; 1443 goto out; 1444 } 1445 1446 if (operation->info.operationClass != TEE_OPERATION_AE) { 1447 res = TEE_ERROR_BAD_PARAMETERS; 1448 goto out; 1449 } 1450 1451 if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 1452 res = TEE_ERROR_BAD_PARAMETERS; 1453 goto out; 1454 } 1455 1456 /* 1457 * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core 1458 * in the implementation. But AES-GCM spec doesn't specify the tag len 1459 * according to the same principle so we have to check here instead to 1460 * be GP compliant. 1461 */ 1462 if (operation->info.algorithm == TEE_ALG_AES_GCM) { 1463 /* 1464 * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96 1465 */ 1466 if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) { 1467 res = TEE_ERROR_NOT_SUPPORTED; 1468 goto out; 1469 } 1470 } 1471 1472 res = _utee_authenc_init(operation->state, nonce, nonceLen, tagLen / 8, 1473 AADLen, payloadLen); 1474 if (res != TEE_SUCCESS) 1475 goto out; 1476 1477 operation->info.digestLength = tagLen / 8; 1478 operation->buffer_offs = 0; 1479 operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 1480 1481 out: 1482 if (res != TEE_SUCCESS && 1483 res != TEE_ERROR_NOT_SUPPORTED) 1484 TEE_Panic(res); 1485 1486 return res; 1487 } 1488 1489 TEE_Result __GP11_TEE_AEInit(TEE_OperationHandle operation, const void *nonce, 1490 uint32_t nonceLen, uint32_t tagLen, 1491 uint32_t AADLen, uint32_t payloadLen) 1492 { 1493 return TEE_AEInit(operation, nonce, nonceLen, tagLen, AADLen, 1494 payloadLen); 1495 } 1496 1497 void TEE_AEUpdateAAD(TEE_OperationHandle operation, const void *AADdata, 1498 size_t AADdataLen) 1499 { 1500 TEE_Result res = TEE_SUCCESS; 1501 1502 if (operation == TEE_HANDLE_NULL || (!AADdata && AADdataLen)) 1503 TEE_Panic(0); 1504 1505 if (operation->info.operationClass != TEE_OPERATION_AE) 1506 TEE_Panic(0); 1507 1508 if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 1509 TEE_Panic(0); 1510 1511 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1512 TEE_Panic(0); 1513 1514 res = _utee_authenc_update_aad(operation->state, AADdata, AADdataLen); 1515 if (res != TEE_SUCCESS) 1516 TEE_Panic(res); 1517 } 1518 1519 void __GP11_TEE_AEUpdateAAD(TEE_OperationHandle operation, const void *AADdata, 1520 uint32_t AADdataLen) 1521 { 1522 TEE_Result res = TEE_SUCCESS; 1523 1524 if (operation == TEE_HANDLE_NULL || 1525 (AADdata == NULL && AADdataLen != 0)) 1526 TEE_Panic(0); 1527 1528 if (operation->info.operationClass != TEE_OPERATION_AE) 1529 TEE_Panic(0); 1530 1531 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1532 TEE_Panic(0); 1533 1534 res = _utee_authenc_update_aad(operation->state, AADdata, AADdataLen); 1535 1536 operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1537 1538 if (res != TEE_SUCCESS) 1539 TEE_Panic(res); 1540 } 1541 1542 static TEE_Result ae_update_helper(TEE_OperationHandle operation, 1543 const void *src, size_t slen, void *dst, 1544 size_t *dlen) 1545 { 1546 TEE_Result res = TEE_SUCCESS; 1547 size_t req_dlen = 0; 1548 uint64_t dl = 0; 1549 1550 if (!src && !slen) { 1551 *dlen = 0; 1552 return TEE_SUCCESS; 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 if (operation->block_size > 1) { 1561 req_dlen = ROUNDDOWN(operation->buffer_offs + slen, 1562 operation->block_size); 1563 } else { 1564 req_dlen = slen; 1565 } 1566 1567 dl = *dlen; 1568 if (dl < req_dlen) { 1569 *dlen = req_dlen; 1570 return TEE_ERROR_SHORT_BUFFER; 1571 } 1572 1573 if (operation->block_size > 1) { 1574 res = tee_buffer_update(operation, _utee_authenc_update_payload, 1575 src, slen, dst, &dl); 1576 } else { 1577 if (slen > 0) { 1578 res = _utee_authenc_update_payload(operation->state, 1579 src, slen, dst, &dl); 1580 } else { 1581 dl = 0; 1582 res = TEE_SUCCESS; 1583 } 1584 } 1585 1586 if (!res) 1587 *dlen = dl; 1588 1589 return res; 1590 } 1591 1592 TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, const void *srcData, 1593 size_t srcLen, void *destData, size_t *destLen) 1594 { 1595 TEE_Result res = TEE_SUCCESS; 1596 1597 if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 1598 res = TEE_ERROR_BAD_PARAMETERS; 1599 goto out; 1600 } 1601 __utee_check_outbuf_annotation(destData, destLen); 1602 1603 if (operation->info.operationClass != TEE_OPERATION_AE) { 1604 res = TEE_ERROR_BAD_PARAMETERS; 1605 goto out; 1606 } 1607 1608 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1609 res = TEE_ERROR_BAD_PARAMETERS; 1610 goto out; 1611 } 1612 1613 res = ae_update_helper(operation, srcData, srcLen, destData, destLen); 1614 if (res != TEE_ERROR_SHORT_BUFFER && srcLen) 1615 operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1616 1617 out: 1618 if (res != TEE_SUCCESS && 1619 res != TEE_ERROR_SHORT_BUFFER) 1620 TEE_Panic(res); 1621 1622 return res; 1623 } 1624 1625 TEE_Result __GP11_TEE_AEUpdate(TEE_OperationHandle operation, 1626 const void *srcData, uint32_t srcLen, 1627 void *destData, uint32_t *destLen) 1628 { 1629 TEE_Result res = TEE_SUCCESS; 1630 size_t dl = 0; 1631 1632 if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 1633 res = TEE_ERROR_BAD_PARAMETERS; 1634 goto out; 1635 } 1636 __utee_check_gp11_outbuf_annotation(destData, destLen); 1637 1638 if (operation->info.operationClass != TEE_OPERATION_AE) { 1639 res = TEE_ERROR_BAD_PARAMETERS; 1640 goto out; 1641 } 1642 1643 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1644 res = TEE_ERROR_BAD_PARAMETERS; 1645 goto out; 1646 } 1647 1648 dl = *destLen; 1649 res = ae_update_helper(operation, srcData, srcLen, destData, &dl); 1650 *destLen = dl; 1651 1652 if (res != TEE_SUCCESS) 1653 goto out; 1654 1655 operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1656 1657 out: 1658 if (res != TEE_SUCCESS && 1659 res != TEE_ERROR_SHORT_BUFFER) 1660 TEE_Panic(res); 1661 1662 return res; 1663 } 1664 1665 TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation, 1666 const void *srcData, size_t srcLen, 1667 void *destData, size_t *destLen, void *tag, 1668 size_t *tagLen) 1669 { 1670 TEE_Result res = TEE_SUCCESS; 1671 uint8_t *dst = destData; 1672 size_t acc_dlen = 0; 1673 uint64_t tmp_dlen = 0; 1674 size_t req_dlen = 0; 1675 uint64_t tl = 0; 1676 1677 if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 1678 res = TEE_ERROR_BAD_PARAMETERS; 1679 goto out; 1680 } 1681 __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1682 __utee_check_inout_annotation(tagLen, sizeof(*tagLen)); 1683 1684 if (operation->info.operationClass != TEE_OPERATION_AE) { 1685 res = TEE_ERROR_BAD_PARAMETERS; 1686 goto out; 1687 } 1688 1689 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1690 res = TEE_ERROR_BAD_PARAMETERS; 1691 goto out; 1692 } 1693 1694 /* 1695 * Check that required destLen is big enough before starting to feed 1696 * data to the algorithm. Errors during feeding of data are fatal as we 1697 * can't restore sync with this API. 1698 * 1699 * Need to check this before update_payload since sync would be lost if 1700 * we return short buffer after that. 1701 */ 1702 res = TEE_ERROR_GENERIC; 1703 1704 req_dlen = operation->buffer_offs + srcLen; 1705 if (*destLen < req_dlen) { 1706 *destLen = req_dlen; 1707 res = TEE_ERROR_SHORT_BUFFER; 1708 } 1709 1710 if (*tagLen < operation->info.digestLength) { 1711 *tagLen = operation->info.digestLength; 1712 res = TEE_ERROR_SHORT_BUFFER; 1713 } 1714 1715 if (res == TEE_ERROR_SHORT_BUFFER) 1716 goto out; 1717 1718 tl = *tagLen; 1719 tmp_dlen = *destLen - acc_dlen; 1720 if (operation->block_size > 1) { 1721 res = tee_buffer_update(operation, _utee_authenc_update_payload, 1722 srcData, srcLen, dst, &tmp_dlen); 1723 if (res != TEE_SUCCESS) 1724 goto out; 1725 1726 dst += tmp_dlen; 1727 acc_dlen += tmp_dlen; 1728 1729 tmp_dlen = *destLen - acc_dlen; 1730 res = _utee_authenc_enc_final(operation->state, 1731 operation->buffer, 1732 operation->buffer_offs, dst, 1733 &tmp_dlen, tag, &tl); 1734 } else { 1735 res = _utee_authenc_enc_final(operation->state, srcData, 1736 srcLen, dst, &tmp_dlen, 1737 tag, &tl); 1738 } 1739 *tagLen = tl; 1740 if (res != TEE_SUCCESS) 1741 goto out; 1742 1743 acc_dlen += tmp_dlen; 1744 *destLen = acc_dlen; 1745 1746 operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1747 1748 operation->operationState = TEE_OPERATION_STATE_INITIAL; 1749 1750 out: 1751 if (res != TEE_SUCCESS && 1752 res != TEE_ERROR_SHORT_BUFFER) 1753 TEE_Panic(res); 1754 1755 return res; 1756 } 1757 1758 TEE_Result __GP11_TEE_AEEncryptFinal(TEE_OperationHandle operation, 1759 const void *srcData, uint32_t srcLen, 1760 void *destData, uint32_t *destLen, 1761 void *tag, uint32_t *tagLen) 1762 { 1763 TEE_Result res = TEE_SUCCESS; 1764 size_t dl = 0; 1765 size_t tl = 0; 1766 1767 __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1768 __utee_check_inout_annotation(tagLen, sizeof(*tagLen)); 1769 dl = *destLen; 1770 tl = *tagLen; 1771 res = TEE_AEEncryptFinal(operation, srcData, srcLen, destData, &dl, 1772 tag, &tl); 1773 *destLen = dl; 1774 *tagLen = tl; 1775 return res; 1776 } 1777 1778 TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation, 1779 const void *srcData, size_t srcLen, 1780 void *destData, size_t *destLen, void *tag, 1781 size_t tagLen) 1782 { 1783 TEE_Result res = TEE_SUCCESS; 1784 uint8_t *dst = destData; 1785 size_t acc_dlen = 0; 1786 uint64_t tmp_dlen = 0; 1787 size_t req_dlen = 0; 1788 1789 if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 1790 res = TEE_ERROR_BAD_PARAMETERS; 1791 goto out; 1792 } 1793 __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1794 1795 if (operation->info.operationClass != TEE_OPERATION_AE) { 1796 res = TEE_ERROR_BAD_PARAMETERS; 1797 goto out; 1798 } 1799 1800 if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1801 res = TEE_ERROR_BAD_PARAMETERS; 1802 goto out; 1803 } 1804 1805 /* 1806 * Check that required destLen is big enough before starting to feed 1807 * data to the algorithm. Errors during feeding of data are fatal as we 1808 * can't restore sync with this API. 1809 */ 1810 req_dlen = operation->buffer_offs + srcLen; 1811 if (*destLen < req_dlen) { 1812 *destLen = req_dlen; 1813 res = TEE_ERROR_SHORT_BUFFER; 1814 goto out; 1815 } 1816 1817 tmp_dlen = *destLen - acc_dlen; 1818 if (operation->block_size > 1) { 1819 res = tee_buffer_update(operation, _utee_authenc_update_payload, 1820 srcData, srcLen, dst, &tmp_dlen); 1821 if (res != TEE_SUCCESS) 1822 goto out; 1823 1824 dst += tmp_dlen; 1825 acc_dlen += tmp_dlen; 1826 1827 tmp_dlen = *destLen - acc_dlen; 1828 res = _utee_authenc_dec_final(operation->state, 1829 operation->buffer, 1830 operation->buffer_offs, dst, 1831 &tmp_dlen, tag, tagLen); 1832 } else { 1833 res = _utee_authenc_dec_final(operation->state, srcData, 1834 srcLen, dst, &tmp_dlen, 1835 tag, tagLen); 1836 } 1837 if (res != TEE_SUCCESS) 1838 goto out; 1839 1840 /* Supplied tagLen should match what we initiated with */ 1841 if (tagLen != operation->info.digestLength) 1842 res = TEE_ERROR_MAC_INVALID; 1843 1844 acc_dlen += tmp_dlen; 1845 *destLen = acc_dlen; 1846 1847 operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1848 1849 operation->operationState = TEE_OPERATION_STATE_INITIAL; 1850 1851 out: 1852 if (res != TEE_SUCCESS && 1853 res != TEE_ERROR_SHORT_BUFFER && 1854 res != TEE_ERROR_MAC_INVALID) 1855 TEE_Panic(res); 1856 1857 return res; 1858 } 1859 1860 TEE_Result __GP11_TEE_AEDecryptFinal(TEE_OperationHandle operation, 1861 const void *srcData, uint32_t srcLen, 1862 void *destData, uint32_t *destLen, 1863 void *tag, uint32_t tagLen) 1864 { 1865 TEE_Result res = TEE_SUCCESS; 1866 size_t dl = 0; 1867 1868 __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1869 dl = *destLen; 1870 res = TEE_AEDecryptFinal(operation, srcData, srcLen, destData, &dl, 1871 tag, tagLen); 1872 *destLen = dl; 1873 return res; 1874 } 1875 1876 /* Cryptographic Operations API - Asymmetric Functions */ 1877 1878 TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation, 1879 const TEE_Attribute *params, 1880 uint32_t paramCount, const void *srcData, 1881 size_t srcLen, void *destData, 1882 size_t *destLen) 1883 { 1884 TEE_Result res = TEE_SUCCESS; 1885 struct utee_attribute ua[paramCount]; 1886 uint64_t dl = 0; 1887 1888 if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) 1889 TEE_Panic(0); 1890 1891 __utee_check_attr_in_annotation(params, paramCount); 1892 __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1893 1894 if (!operation->key1) 1895 TEE_Panic(0); 1896 if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 1897 TEE_Panic(0); 1898 if (operation->info.mode != TEE_MODE_ENCRYPT) 1899 TEE_Panic(0); 1900 1901 __utee_from_attr(ua, params, paramCount); 1902 dl = *destLen; 1903 res = _utee_asymm_operate(operation->state, ua, paramCount, srcData, 1904 srcLen, destData, &dl); 1905 *destLen = dl; 1906 1907 if (res != TEE_SUCCESS && 1908 res != TEE_ERROR_SHORT_BUFFER && 1909 res != TEE_ERROR_BAD_PARAMETERS) 1910 TEE_Panic(res); 1911 1912 return res; 1913 } 1914 1915 TEE_Result __GP11_TEE_AsymmetricEncrypt(TEE_OperationHandle operation, 1916 const __GP11_TEE_Attribute *params, 1917 uint32_t paramCount, 1918 const void *srcData, uint32_t srcLen, 1919 void *destData, uint32_t *destLen) 1920 { 1921 TEE_Result res = TEE_SUCCESS; 1922 struct utee_attribute ua[paramCount]; 1923 uint64_t dl = 0; 1924 1925 if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) 1926 TEE_Panic(0); 1927 1928 __utee_check_gp11_attr_in_annotation(params, paramCount); 1929 __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1930 1931 if (!operation->key1) 1932 TEE_Panic(0); 1933 if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 1934 TEE_Panic(0); 1935 if (operation->info.mode != TEE_MODE_ENCRYPT) 1936 TEE_Panic(0); 1937 1938 __utee_from_gp11_attr(ua, params, paramCount); 1939 dl = *destLen; 1940 res = _utee_asymm_operate(operation->state, ua, paramCount, srcData, 1941 srcLen, destData, &dl); 1942 *destLen = dl; 1943 1944 if (res != TEE_SUCCESS && 1945 res != TEE_ERROR_SHORT_BUFFER && 1946 res != TEE_ERROR_BAD_PARAMETERS) 1947 TEE_Panic(res); 1948 1949 return res; 1950 } 1951 1952 TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation, 1953 const TEE_Attribute *params, 1954 uint32_t paramCount, const void *srcData, 1955 size_t srcLen, void *destData, 1956 size_t *destLen) 1957 { 1958 TEE_Result res = TEE_SUCCESS; 1959 struct utee_attribute ua[paramCount]; 1960 uint64_t dl = 0; 1961 1962 if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) 1963 TEE_Panic(0); 1964 1965 __utee_check_attr_in_annotation(params, paramCount); 1966 __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1967 1968 if (!operation->key1) 1969 TEE_Panic(0); 1970 if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 1971 TEE_Panic(0); 1972 if (operation->info.mode != TEE_MODE_DECRYPT) 1973 TEE_Panic(0); 1974 1975 __utee_from_attr(ua, params, paramCount); 1976 dl = *destLen; 1977 res = _utee_asymm_operate(operation->state, ua, paramCount, srcData, 1978 srcLen, destData, &dl); 1979 *destLen = dl; 1980 1981 if (res != TEE_SUCCESS && 1982 res != TEE_ERROR_SHORT_BUFFER && 1983 res != TEE_ERROR_BAD_PARAMETERS) 1984 TEE_Panic(res); 1985 1986 return res; 1987 } 1988 1989 TEE_Result __GP11_TEE_AsymmetricDecrypt(TEE_OperationHandle operation, 1990 const __GP11_TEE_Attribute *params, 1991 uint32_t paramCount, 1992 const void *srcData, uint32_t srcLen, 1993 void *destData, uint32_t *destLen) 1994 { 1995 TEE_Result res = TEE_SUCCESS; 1996 struct utee_attribute ua[paramCount]; 1997 uint64_t dl = 0; 1998 1999 if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) 2000 TEE_Panic(0); 2001 2002 __utee_check_gp11_attr_in_annotation(params, paramCount); 2003 __utee_check_inout_annotation(destLen, sizeof(*destLen)); 2004 2005 if (!operation->key1) 2006 TEE_Panic(0); 2007 if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 2008 TEE_Panic(0); 2009 if (operation->info.mode != TEE_MODE_DECRYPT) 2010 TEE_Panic(0); 2011 2012 __utee_from_gp11_attr(ua, params, paramCount); 2013 dl = *destLen; 2014 res = _utee_asymm_operate(operation->state, ua, paramCount, srcData, 2015 srcLen, destData, &dl); 2016 *destLen = dl; 2017 2018 if (res != TEE_SUCCESS && 2019 res != TEE_ERROR_SHORT_BUFFER && 2020 res != TEE_ERROR_BAD_PARAMETERS) 2021 TEE_Panic(res); 2022 2023 return res; 2024 } 2025 2026 TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation, 2027 const TEE_Attribute *params, 2028 uint32_t paramCount, const void *digest, 2029 size_t digestLen, void *signature, 2030 size_t *signatureLen) 2031 { 2032 TEE_Result res = TEE_SUCCESS; 2033 struct utee_attribute ua[paramCount]; 2034 uint64_t sl = 0; 2035 2036 if (operation == TEE_HANDLE_NULL || (!digest && digestLen)) 2037 TEE_Panic(0); 2038 2039 __utee_check_attr_in_annotation(params, paramCount); 2040 __utee_check_inout_annotation(signatureLen, sizeof(*signatureLen)); 2041 2042 if (!operation->key1) 2043 TEE_Panic(0); 2044 if (operation->info.operationClass != 2045 TEE_OPERATION_ASYMMETRIC_SIGNATURE) 2046 TEE_Panic(0); 2047 if (operation->info.mode != TEE_MODE_SIGN) 2048 TEE_Panic(0); 2049 2050 __utee_from_attr(ua, params, paramCount); 2051 sl = *signatureLen; 2052 res = _utee_asymm_operate(operation->state, ua, paramCount, digest, 2053 digestLen, signature, &sl); 2054 *signatureLen = sl; 2055 2056 if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER) 2057 TEE_Panic(res); 2058 2059 return res; 2060 } 2061 2062 TEE_Result __GP11_TEE_AsymmetricSignDigest(TEE_OperationHandle operation, 2063 const __GP11_TEE_Attribute *params, 2064 uint32_t paramCount, 2065 const void *digest, 2066 uint32_t digestLen, void *signature, 2067 uint32_t *signatureLen) 2068 { 2069 TEE_Result res = TEE_SUCCESS; 2070 struct utee_attribute ua[paramCount]; 2071 uint64_t sl = 0; 2072 2073 if (operation == TEE_HANDLE_NULL || (!digest && digestLen)) 2074 TEE_Panic(0); 2075 2076 __utee_check_gp11_attr_in_annotation(params, paramCount); 2077 __utee_check_inout_annotation(signatureLen, sizeof(*signatureLen)); 2078 2079 if (!operation->key1) 2080 TEE_Panic(0); 2081 if (operation->info.operationClass != 2082 TEE_OPERATION_ASYMMETRIC_SIGNATURE) 2083 TEE_Panic(0); 2084 if (operation->info.mode != TEE_MODE_SIGN) 2085 TEE_Panic(0); 2086 2087 __utee_from_gp11_attr(ua, params, paramCount); 2088 sl = *signatureLen; 2089 res = _utee_asymm_operate(operation->state, ua, paramCount, digest, 2090 digestLen, signature, &sl); 2091 *signatureLen = sl; 2092 2093 if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER) 2094 TEE_Panic(res); 2095 2096 return res; 2097 } 2098 2099 TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation, 2100 const TEE_Attribute *params, 2101 uint32_t paramCount, const void *digest, 2102 size_t digestLen, 2103 const void *signature, 2104 size_t signatureLen) 2105 { 2106 TEE_Result res; 2107 struct utee_attribute ua[paramCount]; 2108 2109 if (operation == TEE_HANDLE_NULL || 2110 (digest == NULL && digestLen != 0) || 2111 (signature == NULL && signatureLen != 0)) 2112 TEE_Panic(0); 2113 2114 __utee_check_attr_in_annotation(params, paramCount); 2115 2116 if (!operation->key1) 2117 TEE_Panic(0); 2118 if (operation->info.operationClass != 2119 TEE_OPERATION_ASYMMETRIC_SIGNATURE) 2120 TEE_Panic(0); 2121 if (operation->info.mode != TEE_MODE_VERIFY) 2122 TEE_Panic(0); 2123 2124 __utee_from_attr(ua, params, paramCount); 2125 res = _utee_asymm_verify(operation->state, ua, paramCount, digest, 2126 digestLen, signature, signatureLen); 2127 2128 if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID) 2129 TEE_Panic(res); 2130 2131 return res; 2132 } 2133 2134 TEE_Result __GP11_TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation, 2135 const __GP11_TEE_Attribute *params, 2136 uint32_t paramCount, 2137 const void *digest, 2138 uint32_t digestLen, 2139 const void *signature, 2140 uint32_t signatureLen) 2141 { 2142 TEE_Result res = TEE_SUCCESS; 2143 struct utee_attribute ua[paramCount]; 2144 2145 if (operation == TEE_HANDLE_NULL || (!digest && digestLen) || 2146 (!signature && signatureLen)) 2147 TEE_Panic(0); 2148 2149 __utee_check_gp11_attr_in_annotation(params, paramCount); 2150 2151 if (!operation->key1) 2152 TEE_Panic(0); 2153 if (operation->info.operationClass != 2154 TEE_OPERATION_ASYMMETRIC_SIGNATURE) 2155 TEE_Panic(0); 2156 if (operation->info.mode != TEE_MODE_VERIFY) 2157 TEE_Panic(0); 2158 2159 __utee_from_gp11_attr(ua, params, paramCount); 2160 res = _utee_asymm_verify(operation->state, ua, paramCount, digest, 2161 digestLen, signature, signatureLen); 2162 2163 if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID) 2164 TEE_Panic(res); 2165 2166 return res; 2167 } 2168 2169 /* Cryptographic Operations API - Key Derivation Functions */ 2170 2171 void TEE_DeriveKey(TEE_OperationHandle operation, 2172 const TEE_Attribute *params, uint32_t paramCount, 2173 TEE_ObjectHandle derivedKey) 2174 { 2175 struct utee_attribute ua[paramCount]; 2176 struct utee_object_info key_info = { }; 2177 TEE_Result res = TEE_SUCCESS; 2178 2179 if (operation == TEE_HANDLE_NULL || derivedKey == 0) 2180 TEE_Panic(0); 2181 2182 __utee_check_attr_in_annotation(params, paramCount); 2183 2184 if (TEE_ALG_GET_CLASS(operation->info.algorithm) != 2185 TEE_OPERATION_KEY_DERIVATION) 2186 TEE_Panic(0); 2187 2188 if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION) 2189 TEE_Panic(0); 2190 if (!operation->key1) 2191 TEE_Panic(0); 2192 if (operation->info.mode != TEE_MODE_DERIVE) 2193 TEE_Panic(0); 2194 if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0) 2195 TEE_Panic(0); 2196 2197 res = _utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info); 2198 if (res != TEE_SUCCESS) 2199 TEE_Panic(res); 2200 2201 if (key_info.obj_type != TEE_TYPE_GENERIC_SECRET) 2202 TEE_Panic(0); 2203 if ((key_info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 2204 TEE_Panic(0); 2205 2206 __utee_from_attr(ua, params, paramCount); 2207 res = _utee_cryp_derive_key(operation->state, ua, paramCount, 2208 (unsigned long)derivedKey); 2209 if (res != TEE_SUCCESS) 2210 TEE_Panic(res); 2211 } 2212 2213 void __GP11_TEE_DeriveKey(TEE_OperationHandle operation, 2214 const __GP11_TEE_Attribute *params, 2215 uint32_t paramCount, TEE_ObjectHandle derivedKey) 2216 { 2217 struct utee_attribute ua[paramCount]; 2218 struct utee_object_info key_info = { }; 2219 TEE_Result res = TEE_SUCCESS; 2220 2221 if (operation == TEE_HANDLE_NULL || derivedKey == 0) 2222 TEE_Panic(0); 2223 2224 __utee_check_gp11_attr_in_annotation(params, paramCount); 2225 2226 if (TEE_ALG_GET_CLASS(operation->info.algorithm) != 2227 TEE_OPERATION_KEY_DERIVATION) 2228 TEE_Panic(0); 2229 2230 if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION) 2231 TEE_Panic(0); 2232 if (!operation->key1) 2233 TEE_Panic(0); 2234 if (operation->info.mode != TEE_MODE_DERIVE) 2235 TEE_Panic(0); 2236 if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0) 2237 TEE_Panic(0); 2238 2239 res = _utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info); 2240 if (res != TEE_SUCCESS) 2241 TEE_Panic(res); 2242 2243 if (key_info.obj_type != TEE_TYPE_GENERIC_SECRET) 2244 TEE_Panic(0); 2245 if ((key_info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 2246 TEE_Panic(0); 2247 2248 __utee_from_gp11_attr(ua, params, paramCount); 2249 res = _utee_cryp_derive_key(operation->state, ua, paramCount, 2250 (unsigned long)derivedKey); 2251 if (res != TEE_SUCCESS) 2252 TEE_Panic(res); 2253 } 2254 2255 /* Cryptographic Operations API - Random Number Generation Functions */ 2256 2257 void TEE_GenerateRandom(void *randomBuffer, size_t randomBufferLen) 2258 { 2259 TEE_Result res; 2260 2261 res = _utee_cryp_random_number_generate(randomBuffer, randomBufferLen); 2262 if (res != TEE_SUCCESS) 2263 TEE_Panic(res); 2264 } 2265 2266 void __GP11_TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen) 2267 { 2268 TEE_GenerateRandom(randomBuffer, randomBufferLen); 2269 } 2270 2271 int rand(void) 2272 { 2273 int rc; 2274 2275 TEE_GenerateRandom(&rc, sizeof(rc)); 2276 2277 /* 2278 * RAND_MAX is the larges int, INT_MAX which is all bits but the 2279 * highest bit set. 2280 */ 2281 return rc & RAND_MAX; 2282 } 2283 2284 TEE_Result TEE_IsAlgorithmSupported(uint32_t alg, uint32_t element) 2285 { 2286 if (IS_ENABLED(CFG_CRYPTO_AES)) { 2287 if (IS_ENABLED(CFG_CRYPTO_ECB)) { 2288 if (alg == TEE_ALG_AES_ECB_NOPAD) 2289 goto check_element_none; 2290 } 2291 if (IS_ENABLED(CFG_CRYPTO_CBC)) { 2292 if (alg == TEE_ALG_AES_CBC_NOPAD) 2293 goto check_element_none; 2294 } 2295 if (IS_ENABLED(CFG_CRYPTO_CTR)) { 2296 if (alg == TEE_ALG_AES_CTR) 2297 goto check_element_none; 2298 } 2299 if (IS_ENABLED(CFG_CRYPTO_CTS)) { 2300 if (alg == TEE_ALG_AES_CTS) 2301 goto check_element_none; 2302 } 2303 if (IS_ENABLED(CFG_CRYPTO_XTS)) { 2304 if (alg == TEE_ALG_AES_XTS) 2305 goto check_element_none; 2306 } 2307 if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) { 2308 if (alg == TEE_ALG_AES_CBC_MAC_NOPAD || 2309 alg == TEE_ALG_AES_CBC_MAC_PKCS5) 2310 goto check_element_none; 2311 } 2312 if (IS_ENABLED(CFG_CRYPTO_CMAC)) { 2313 if (alg == TEE_ALG_AES_CMAC) 2314 goto check_element_none; 2315 } 2316 if (IS_ENABLED(CFG_CRYPTO_CCM)) { 2317 if (alg == TEE_ALG_AES_CCM) 2318 goto check_element_none; 2319 } 2320 if (IS_ENABLED(CFG_CRYPTO_GCM)) { 2321 if (alg == TEE_ALG_AES_GCM) 2322 goto check_element_none; 2323 } 2324 } 2325 if (IS_ENABLED(CFG_CRYPTO_DES)) { 2326 if (IS_ENABLED(CFG_CRYPTO_ECB)) { 2327 if (alg == TEE_ALG_DES_ECB_NOPAD || 2328 alg == TEE_ALG_DES3_ECB_NOPAD) 2329 goto check_element_none; 2330 } 2331 if (IS_ENABLED(CFG_CRYPTO_CBC)) { 2332 if (alg == TEE_ALG_DES_CBC_NOPAD || 2333 alg == TEE_ALG_DES3_CBC_NOPAD) 2334 goto check_element_none; 2335 } 2336 if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) { 2337 if (alg == TEE_ALG_DES_CBC_MAC_NOPAD || 2338 alg == TEE_ALG_DES_CBC_MAC_PKCS5 || 2339 alg == TEE_ALG_DES3_CBC_MAC_NOPAD || 2340 alg == TEE_ALG_DES3_CBC_MAC_PKCS5) 2341 goto check_element_none; 2342 } 2343 } 2344 if (IS_ENABLED(CFG_CRYPTO_MD5)) { 2345 if (alg == TEE_ALG_MD5) 2346 goto check_element_none; 2347 } 2348 if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 2349 if (alg == TEE_ALG_SHA1) 2350 goto check_element_none; 2351 } 2352 if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 2353 if (alg == TEE_ALG_SHA224) 2354 goto check_element_none; 2355 } 2356 if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 2357 if (alg == TEE_ALG_SHA256) 2358 goto check_element_none; 2359 } 2360 if (IS_ENABLED(CFG_CRYPTO_SHA384)) { 2361 if (alg == TEE_ALG_SHA384) 2362 goto check_element_none; 2363 } 2364 if (IS_ENABLED(CFG_CRYPTO_SHA512)) { 2365 if (alg == TEE_ALG_SHA512) 2366 goto check_element_none; 2367 } 2368 if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) { 2369 if (alg == TEE_ALG_MD5SHA1) 2370 goto check_element_none; 2371 } 2372 if (IS_ENABLED(CFG_CRYPTO_HMAC)) { 2373 if (IS_ENABLED(CFG_CRYPTO_MD5)) { 2374 if (alg == TEE_ALG_HMAC_MD5) 2375 goto check_element_none; 2376 } 2377 if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 2378 if (alg == TEE_ALG_HMAC_SHA1) 2379 goto check_element_none; 2380 } 2381 if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 2382 if (alg == TEE_ALG_HMAC_SHA224) 2383 goto check_element_none; 2384 } 2385 if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 2386 if (alg == TEE_ALG_HMAC_SHA256) 2387 goto check_element_none; 2388 } 2389 if (IS_ENABLED(CFG_CRYPTO_SHA384)) { 2390 if (alg == TEE_ALG_HMAC_SHA384) 2391 goto check_element_none; 2392 } 2393 if (IS_ENABLED(CFG_CRYPTO_SHA512)) { 2394 if (alg == TEE_ALG_HMAC_SHA512) 2395 goto check_element_none; 2396 } 2397 if (IS_ENABLED(CFG_CRYPTO_SM3)) { 2398 if (alg == TEE_ALG_HMAC_SM3) 2399 goto check_element_none; 2400 } 2401 } 2402 if (IS_ENABLED(CFG_CRYPTO_SM3)) { 2403 if (alg == TEE_ALG_SM3) 2404 goto check_element_none; 2405 } 2406 if (IS_ENABLED(CFG_CRYPTO_SM4)) { 2407 if (IS_ENABLED(CFG_CRYPTO_ECB)) { 2408 if (alg == TEE_ALG_SM4_ECB_NOPAD) 2409 goto check_element_none; 2410 } 2411 if (IS_ENABLED(CFG_CRYPTO_CBC)) { 2412 if (alg == TEE_ALG_SM4_CBC_NOPAD) 2413 goto check_element_none; 2414 } 2415 if (IS_ENABLED(CFG_CRYPTO_CTR)) { 2416 if (alg == TEE_ALG_SM4_CTR) 2417 goto check_element_none; 2418 } 2419 if (IS_ENABLED(CFG_CRYPTO_XTS)) { 2420 if (alg == TEE_ALG_SM4_XTS) 2421 goto check_element_none; 2422 } 2423 } 2424 if (IS_ENABLED(CFG_CRYPTO_RSA)) { 2425 if (IS_ENABLED(CFG_CRYPTO_MD5)) { 2426 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5) 2427 goto check_element_none; 2428 } 2429 if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 2430 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA1 || 2431 alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1 || 2432 alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1) 2433 goto check_element_none; 2434 } 2435 if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) { 2436 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5SHA1) 2437 goto check_element_none; 2438 } 2439 if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 2440 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA224 || 2441 alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224 || 2442 alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224) 2443 goto check_element_none; 2444 } 2445 if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 2446 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA256 || 2447 alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256 || 2448 alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256) 2449 goto check_element_none; 2450 } 2451 if (IS_ENABLED(CFG_CRYPTO_SHA384)) { 2452 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA384 || 2453 alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384 || 2454 alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384) 2455 goto check_element_none; 2456 } 2457 if (IS_ENABLED(CFG_CRYPTO_SHA512)) { 2458 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA512 || 2459 alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512 || 2460 alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512) 2461 goto check_element_none; 2462 } 2463 if (IS_ENABLED(CFG_CRYPTO_RSASSA_NA1)) { 2464 if (alg == TEE_ALG_RSASSA_PKCS1_V1_5) 2465 goto check_element_none; 2466 } 2467 if (alg == TEE_ALG_RSA_NOPAD) 2468 goto check_element_none; 2469 } 2470 if (IS_ENABLED(CFG_CRYPTO_DSA)) { 2471 if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 2472 if (alg == TEE_ALG_DSA_SHA1) 2473 goto check_element_none; 2474 } 2475 if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 2476 if (alg == TEE_ALG_DSA_SHA224) 2477 goto check_element_none; 2478 } 2479 if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 2480 if (alg == TEE_ALG_DSA_SHA256) 2481 goto check_element_none; 2482 } 2483 } 2484 if (IS_ENABLED(CFG_CRYPTO_DH)) { 2485 if (alg == TEE_ALG_DH_DERIVE_SHARED_SECRET) 2486 goto check_element_none; 2487 } 2488 if (IS_ENABLED(CFG_CRYPTO_ECC)) { 2489 if ((alg == __OPTEE_ALG_ECDH_P192 || 2490 alg == __OPTEE_ALG_ECDSA_P192 || 2491 alg == TEE_ALG_ECDH_DERIVE_SHARED_SECRET || 2492 alg == TEE_ALG_ECDSA_SHA1) && 2493 element == TEE_ECC_CURVE_NIST_P192) 2494 return TEE_SUCCESS; 2495 if ((alg == __OPTEE_ALG_ECDH_P224 || 2496 alg == __OPTEE_ALG_ECDSA_P224 || 2497 alg == TEE_ALG_ECDH_DERIVE_SHARED_SECRET || 2498 alg == TEE_ALG_ECDSA_SHA224) && 2499 element == TEE_ECC_CURVE_NIST_P224) 2500 return TEE_SUCCESS; 2501 if ((alg == __OPTEE_ALG_ECDH_P256 || 2502 alg == __OPTEE_ALG_ECDSA_P256 || 2503 alg == TEE_ALG_ECDH_DERIVE_SHARED_SECRET || 2504 alg == TEE_ALG_ECDSA_SHA256) && 2505 element == TEE_ECC_CURVE_NIST_P256) 2506 return TEE_SUCCESS; 2507 if ((alg == __OPTEE_ALG_ECDH_P384 || 2508 alg == __OPTEE_ALG_ECDSA_P384 || 2509 alg == TEE_ALG_ECDH_DERIVE_SHARED_SECRET || 2510 alg == TEE_ALG_ECDSA_SHA384) && 2511 element == TEE_ECC_CURVE_NIST_P384) 2512 return TEE_SUCCESS; 2513 if ((alg == __OPTEE_ALG_ECDH_P521 || 2514 alg == __OPTEE_ALG_ECDSA_P521 || 2515 alg == TEE_ALG_ECDH_DERIVE_SHARED_SECRET || 2516 alg == TEE_ALG_ECDSA_SHA512) && 2517 element == TEE_ECC_CURVE_NIST_P521) 2518 return TEE_SUCCESS; 2519 } 2520 if (IS_ENABLED(CFG_CRYPTO_SM2_DSA)) { 2521 if (alg == TEE_ALG_SM2_DSA_SM3 && element == TEE_ECC_CURVE_SM2) 2522 return TEE_SUCCESS; 2523 } 2524 if (IS_ENABLED(CFG_CRYPTO_SM2_KEP)) { 2525 if (alg == TEE_ALG_SM2_KEP && element == TEE_ECC_CURVE_SM2) 2526 return TEE_SUCCESS; 2527 } 2528 if (IS_ENABLED(CFG_CRYPTO_SM2_PKE)) { 2529 if (alg == TEE_ALG_SM2_PKE && element == TEE_ECC_CURVE_SM2) 2530 return TEE_SUCCESS; 2531 } 2532 if (IS_ENABLED(CFG_CRYPTO_X25519)) { 2533 if (alg == TEE_ALG_X25519 && element == TEE_ECC_CURVE_25519) 2534 return TEE_SUCCESS; 2535 } 2536 if (IS_ENABLED(CFG_CRYPTO_ED25519)) { 2537 if (alg == TEE_ALG_ED25519 && element == TEE_ECC_CURVE_25519) 2538 return TEE_SUCCESS; 2539 } 2540 2541 return TEE_ERROR_NOT_SUPPORTED; 2542 check_element_none: 2543 if (element == TEE_CRYPTO_ELEMENT_NONE) 2544 return TEE_SUCCESS; 2545 return TEE_ERROR_NOT_SUPPORTED; 2546 } 2547