11bb92983SJerome Forissier // SPDX-License-Identifier: BSD-2-Clause 2b0104773SPascal Brand /* 3b0104773SPascal Brand * Copyright (c) 2014, STMicroelectronics International N.V. 4b0104773SPascal Brand */ 579170ce0SJerome Forissier #include <config.h> 6b0104773SPascal Brand #include <stdlib.h> 7b0104773SPascal Brand #include <string.h> 8b796ebf3SJerome Forissier #include <string_ext.h> 9b0104773SPascal Brand #include <tee_api.h> 108854d3c6SJerome Forissier #include <tee_api_defines_extensions.h> 11b0104773SPascal Brand #include <tee_internal_api_extensions.h> 12b0104773SPascal Brand #include <utee_syscalls.h> 13b0104773SPascal Brand #include <utee_defines.h> 14fc26c92aSJens Wiklander #include <util.h> 15e86f1266SJens Wiklander #include "tee_api_private.h" 16b0104773SPascal Brand 17b0104773SPascal Brand struct __TEE_OperationHandle { 18b0104773SPascal Brand TEE_OperationInfo info; 19b0104773SPascal Brand TEE_ObjectHandle key1; 20b0104773SPascal Brand TEE_ObjectHandle key2; 21642a1607SCedric Chaumont uint32_t operationState;/* Operation state : INITIAL or ACTIVE */ 22b0104773SPascal Brand uint8_t *buffer; /* buffer to collect complete blocks */ 23b0104773SPascal Brand bool buffer_two_blocks; /* True if two blocks need to be buffered */ 24b0104773SPascal Brand size_t block_size; /* Block size of cipher */ 25b0104773SPascal Brand size_t buffer_offs; /* Offset in buffer */ 26b0104773SPascal Brand uint32_t state; /* Handle to state in TEE Core */ 27b0104773SPascal Brand }; 28b0104773SPascal Brand 29b0104773SPascal Brand /* Cryptographic Operations API - Generic Operation Functions */ 30b0104773SPascal Brand 31b0104773SPascal Brand TEE_Result TEE_AllocateOperation(TEE_OperationHandle *operation, 32b0104773SPascal Brand uint32_t algorithm, uint32_t mode, 33b0104773SPascal Brand uint32_t maxKeySize) 34b0104773SPascal Brand { 35b0104773SPascal Brand TEE_Result res; 36b0104773SPascal Brand TEE_OperationHandle op = TEE_HANDLE_NULL; 37b0104773SPascal Brand uint32_t handle_state = 0; 38b0104773SPascal Brand size_t block_size = 1; 39b0104773SPascal Brand uint32_t req_key_usage; 40b0104773SPascal Brand bool with_private_key = false; 41b0104773SPascal Brand bool buffer_two_blocks = false; 42b0104773SPascal Brand 439b52c538SCedric Chaumont if (!operation) 44b0104773SPascal Brand TEE_Panic(0); 45b0104773SPascal Brand 465b385b3fSJerome Forissier if (algorithm == TEE_ALG_AES_XTS || algorithm == TEE_ALG_SM2_KEP) 47b0104773SPascal Brand handle_state = TEE_HANDLE_FLAG_EXPECT_TWO_KEYS; 48b0104773SPascal Brand 49218d9055SCedric Chaumont /* Check algorithm max key size */ 50218d9055SCedric Chaumont switch (algorithm) { 51218d9055SCedric Chaumont case TEE_ALG_DSA_SHA1: 52218d9055SCedric Chaumont if (maxKeySize < 512) 53218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 54218d9055SCedric Chaumont if (maxKeySize > 1024) 55218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 56218d9055SCedric Chaumont if (maxKeySize % 64 != 0) 57218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 58218d9055SCedric Chaumont break; 59218d9055SCedric Chaumont 60218d9055SCedric Chaumont case TEE_ALG_DSA_SHA224: 61218d9055SCedric Chaumont if (maxKeySize != 2048) 62218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 63218d9055SCedric Chaumont break; 64218d9055SCedric Chaumont 65218d9055SCedric Chaumont case TEE_ALG_DSA_SHA256: 66218d9055SCedric Chaumont if (maxKeySize != 2048 && maxKeySize != 3072) 67218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 68218d9055SCedric Chaumont break; 69218d9055SCedric Chaumont 701220586eSCedric Chaumont case TEE_ALG_ECDSA_P192: 711220586eSCedric Chaumont case TEE_ALG_ECDH_P192: 721220586eSCedric Chaumont if (maxKeySize != 192) 731220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 741220586eSCedric Chaumont break; 751220586eSCedric Chaumont 761220586eSCedric Chaumont case TEE_ALG_ECDSA_P224: 771220586eSCedric Chaumont case TEE_ALG_ECDH_P224: 781220586eSCedric Chaumont if (maxKeySize != 224) 791220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 801220586eSCedric Chaumont break; 811220586eSCedric Chaumont 821220586eSCedric Chaumont case TEE_ALG_ECDSA_P256: 831220586eSCedric Chaumont case TEE_ALG_ECDH_P256: 8491fc6bd8SJerome Forissier case TEE_ALG_SM2_PKE: 850f151943SJerome Forissier case TEE_ALG_SM2_DSA_SM3: 861220586eSCedric Chaumont if (maxKeySize != 256) 871220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 881220586eSCedric Chaumont break; 891220586eSCedric Chaumont 905b385b3fSJerome Forissier case TEE_ALG_SM2_KEP: 915b385b3fSJerome Forissier /* Two 256-bit keys */ 925b385b3fSJerome Forissier if (maxKeySize != 512) 935b385b3fSJerome Forissier return TEE_ERROR_NOT_SUPPORTED; 945b385b3fSJerome Forissier break; 955b385b3fSJerome Forissier 961220586eSCedric Chaumont case TEE_ALG_ECDSA_P384: 971220586eSCedric Chaumont case TEE_ALG_ECDH_P384: 981220586eSCedric Chaumont if (maxKeySize != 384) 991220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 1001220586eSCedric Chaumont break; 1011220586eSCedric Chaumont 1021220586eSCedric Chaumont case TEE_ALG_ECDSA_P521: 1031220586eSCedric Chaumont case TEE_ALG_ECDH_P521: 1041220586eSCedric Chaumont if (maxKeySize != 521) 1051220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 1061220586eSCedric Chaumont break; 1071220586eSCedric Chaumont 108218d9055SCedric Chaumont default: 109218d9055SCedric Chaumont break; 110218d9055SCedric Chaumont } 111218d9055SCedric Chaumont 112218d9055SCedric Chaumont /* Check algorithm mode */ 113b0104773SPascal Brand switch (algorithm) { 114b0104773SPascal Brand case TEE_ALG_AES_CTS: 115b0104773SPascal Brand case TEE_ALG_AES_XTS: 116b0104773SPascal Brand buffer_two_blocks = true; 1174bd53c54SJerome Forissier /* FALLTHROUGH */ 1184bd53c54SJerome Forissier case TEE_ALG_AES_ECB_NOPAD: 119b0104773SPascal Brand case TEE_ALG_AES_CBC_NOPAD: 120b0104773SPascal Brand case TEE_ALG_AES_CCM: 121b0104773SPascal Brand case TEE_ALG_DES_ECB_NOPAD: 122b0104773SPascal Brand case TEE_ALG_DES_CBC_NOPAD: 123b0104773SPascal Brand case TEE_ALG_DES3_ECB_NOPAD: 124b0104773SPascal Brand case TEE_ALG_DES3_CBC_NOPAD: 125ade6f848SJerome Forissier case TEE_ALG_SM4_ECB_NOPAD: 126ade6f848SJerome Forissier case TEE_ALG_SM4_CBC_NOPAD: 127ade6f848SJerome Forissier case TEE_ALG_SM4_CTR: 128b0104773SPascal Brand if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES) 129b0104773SPascal Brand block_size = TEE_AES_BLOCK_SIZE; 130ade6f848SJerome Forissier else if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_SM4) 131ade6f848SJerome Forissier block_size = TEE_SM4_BLOCK_SIZE; 132b0104773SPascal Brand else 133b0104773SPascal Brand block_size = TEE_DES_BLOCK_SIZE; 134afc0c182SBogdan Liulko /* FALLTHROUGH */ 13557aabac5SBogdan Liulko case TEE_ALG_AES_CTR: 136afc0c182SBogdan Liulko case TEE_ALG_AES_GCM: 137b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) 138b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT; 139b0104773SPascal Brand else if (mode == TEE_MODE_DECRYPT) 140b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT; 141b0104773SPascal Brand else 142b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 143b0104773SPascal Brand break; 144b0104773SPascal Brand 1456a2e0a9fSGabor Szekely #if defined(CFG_CRYPTO_RSASSA_NA1) 1466a2e0a9fSGabor Szekely case TEE_ALG_RSASSA_PKCS1_V1_5: 1476a2e0a9fSGabor Szekely #endif 148b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_MD5: 149b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1: 150b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224: 151b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256: 152b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384: 153b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512: 154b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1: 155b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224: 156b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256: 157b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384: 158b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512: 159b0104773SPascal Brand case TEE_ALG_DSA_SHA1: 160218d9055SCedric Chaumont case TEE_ALG_DSA_SHA224: 161218d9055SCedric Chaumont case TEE_ALG_DSA_SHA256: 1621220586eSCedric Chaumont case TEE_ALG_ECDSA_P192: 1631220586eSCedric Chaumont case TEE_ALG_ECDSA_P224: 1641220586eSCedric Chaumont case TEE_ALG_ECDSA_P256: 1651220586eSCedric Chaumont case TEE_ALG_ECDSA_P384: 1661220586eSCedric Chaumont case TEE_ALG_ECDSA_P521: 1670f151943SJerome Forissier case TEE_ALG_SM2_DSA_SM3: 168b0104773SPascal Brand if (mode == TEE_MODE_SIGN) { 169b0104773SPascal Brand with_private_key = true; 170b0104773SPascal Brand req_key_usage = TEE_USAGE_SIGN; 171b0104773SPascal Brand } else if (mode == TEE_MODE_VERIFY) { 172b0104773SPascal Brand req_key_usage = TEE_USAGE_VERIFY; 173b0104773SPascal Brand } else { 174b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 175b0104773SPascal Brand } 176b0104773SPascal Brand break; 177b0104773SPascal Brand 178b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_V1_5: 179b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1: 180b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224: 181b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256: 182b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384: 183b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512: 18491fc6bd8SJerome Forissier case TEE_ALG_SM2_PKE: 185b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) { 186b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT; 187b0104773SPascal Brand } else if (mode == TEE_MODE_DECRYPT) { 188b0104773SPascal Brand with_private_key = true; 189b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT; 190b0104773SPascal Brand } else { 191b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 192b0104773SPascal Brand } 193b0104773SPascal Brand break; 194b0104773SPascal Brand 195b0104773SPascal Brand case TEE_ALG_RSA_NOPAD: 196b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) { 197b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY; 198b0104773SPascal Brand } else if (mode == TEE_MODE_DECRYPT) { 199b0104773SPascal Brand with_private_key = true; 200b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT | TEE_USAGE_SIGN; 201b0104773SPascal Brand } else { 202b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 203b0104773SPascal Brand } 204b0104773SPascal Brand break; 205b0104773SPascal Brand 206b0104773SPascal Brand case TEE_ALG_DH_DERIVE_SHARED_SECRET: 2071220586eSCedric Chaumont case TEE_ALG_ECDH_P192: 2081220586eSCedric Chaumont case TEE_ALG_ECDH_P224: 2091220586eSCedric Chaumont case TEE_ALG_ECDH_P256: 2101220586eSCedric Chaumont case TEE_ALG_ECDH_P384: 2111220586eSCedric Chaumont case TEE_ALG_ECDH_P521: 212cdb198a7SJerome Forissier case TEE_ALG_HKDF_MD5_DERIVE_KEY: 213cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA1_DERIVE_KEY: 214cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA224_DERIVE_KEY: 215cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA256_DERIVE_KEY: 216cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA384_DERIVE_KEY: 217cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA512_DERIVE_KEY: 2188854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA1_DERIVE_KEY: 2198854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA224_DERIVE_KEY: 2208854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA256_DERIVE_KEY: 2218854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA384_DERIVE_KEY: 2228854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA512_DERIVE_KEY: 2230f2293b7SJerome Forissier case TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY: 2245b385b3fSJerome Forissier case TEE_ALG_SM2_KEP: 225b0104773SPascal Brand if (mode != TEE_MODE_DERIVE) 226b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 227b0104773SPascal Brand with_private_key = true; 228b0104773SPascal Brand req_key_usage = TEE_USAGE_DERIVE; 229b0104773SPascal Brand break; 230b0104773SPascal Brand 231b0104773SPascal Brand case TEE_ALG_MD5: 232b0104773SPascal Brand case TEE_ALG_SHA1: 233b0104773SPascal Brand case TEE_ALG_SHA224: 234b0104773SPascal Brand case TEE_ALG_SHA256: 235b0104773SPascal Brand case TEE_ALG_SHA384: 236b0104773SPascal Brand case TEE_ALG_SHA512: 23747645577SJerome Forissier case TEE_ALG_SM3: 238b0104773SPascal Brand if (mode != TEE_MODE_DIGEST) 239b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 24005304565SCedric Chaumont /* v1.1: flags always set for digest operations */ 241b0104773SPascal Brand handle_state |= TEE_HANDLE_FLAG_KEY_SET; 242b0104773SPascal Brand req_key_usage = 0; 243b0104773SPascal Brand break; 244b0104773SPascal Brand 245b0104773SPascal Brand case TEE_ALG_DES_CBC_MAC_NOPAD: 246b0104773SPascal Brand case TEE_ALG_AES_CBC_MAC_NOPAD: 247b0104773SPascal Brand case TEE_ALG_AES_CBC_MAC_PKCS5: 248b0104773SPascal Brand case TEE_ALG_AES_CMAC: 249b0104773SPascal Brand case TEE_ALG_DES_CBC_MAC_PKCS5: 250b0104773SPascal Brand case TEE_ALG_DES3_CBC_MAC_NOPAD: 251b0104773SPascal Brand case TEE_ALG_DES3_CBC_MAC_PKCS5: 252b0104773SPascal Brand case TEE_ALG_HMAC_MD5: 253b0104773SPascal Brand case TEE_ALG_HMAC_SHA1: 254b0104773SPascal Brand case TEE_ALG_HMAC_SHA224: 255b0104773SPascal Brand case TEE_ALG_HMAC_SHA256: 256b0104773SPascal Brand case TEE_ALG_HMAC_SHA384: 257b0104773SPascal Brand case TEE_ALG_HMAC_SHA512: 25847645577SJerome Forissier case TEE_ALG_HMAC_SM3: 259b0104773SPascal Brand if (mode != TEE_MODE_MAC) 260b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 261b0104773SPascal Brand req_key_usage = TEE_USAGE_MAC; 262b0104773SPascal Brand break; 263b0104773SPascal Brand 264b0104773SPascal Brand default: 265b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 266b0104773SPascal Brand } 267b0104773SPascal Brand 268b66f219bSJens Wiklander op = TEE_Malloc(sizeof(*op), TEE_MALLOC_FILL_ZERO); 2699b52c538SCedric Chaumont if (!op) 270b0104773SPascal Brand return TEE_ERROR_OUT_OF_MEMORY; 271b0104773SPascal Brand 272b0104773SPascal Brand op->info.algorithm = algorithm; 273b0104773SPascal Brand op->info.operationClass = TEE_ALG_GET_CLASS(algorithm); 2746a2e0a9fSGabor Szekely #ifdef CFG_CRYPTO_RSASSA_NA1 2756a2e0a9fSGabor Szekely if (algorithm == TEE_ALG_RSASSA_PKCS1_V1_5) 2766a2e0a9fSGabor Szekely op->info.operationClass = TEE_OPERATION_ASYMMETRIC_SIGNATURE; 2776a2e0a9fSGabor Szekely #endif 278b0104773SPascal Brand op->info.mode = mode; 2792e5e6460SAlbert Schwarzkopf op->info.digestLength = TEE_ALG_GET_DIGEST_SIZE(algorithm); 280b0104773SPascal Brand op->info.maxKeySize = maxKeySize; 281b0104773SPascal Brand op->info.requiredKeyUsage = req_key_usage; 282b0104773SPascal Brand op->info.handleState = handle_state; 283b0104773SPascal Brand 284b0104773SPascal Brand if (block_size > 1) { 285b0104773SPascal Brand size_t buffer_size = block_size; 286b0104773SPascal Brand 287b0104773SPascal Brand if (buffer_two_blocks) 288b0104773SPascal Brand buffer_size *= 2; 289b0104773SPascal Brand 2909b52c538SCedric Chaumont op->buffer = TEE_Malloc(buffer_size, 2919b52c538SCedric Chaumont TEE_USER_MEM_HINT_NO_FILL_ZERO); 292b0104773SPascal Brand if (op->buffer == NULL) { 293b0104773SPascal Brand res = TEE_ERROR_OUT_OF_MEMORY; 294b66f219bSJens Wiklander goto out; 295b0104773SPascal Brand } 296b0104773SPascal Brand } 297b0104773SPascal Brand op->block_size = block_size; 298b0104773SPascal Brand op->buffer_two_blocks = buffer_two_blocks; 299b0104773SPascal Brand 300b0104773SPascal Brand if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) { 301b0104773SPascal Brand uint32_t mks = maxKeySize; 302b0104773SPascal Brand TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm, 303b0104773SPascal Brand with_private_key); 304b0104773SPascal Brand 305b0104773SPascal Brand /* 306b0104773SPascal Brand * If two keys are expected the max key size is the sum of 307b0104773SPascal Brand * the size of both keys. 308b0104773SPascal Brand */ 309b0104773SPascal Brand if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) 310b0104773SPascal Brand mks /= 2; 311b0104773SPascal Brand 312b0104773SPascal Brand res = TEE_AllocateTransientObject(key_type, mks, &op->key1); 313b0104773SPascal Brand if (res != TEE_SUCCESS) 314b66f219bSJens Wiklander goto out; 315b0104773SPascal Brand 31605304565SCedric Chaumont if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) { 3179b52c538SCedric Chaumont res = TEE_AllocateTransientObject(key_type, mks, 318b0104773SPascal Brand &op->key2); 319b0104773SPascal Brand if (res != TEE_SUCCESS) 320b66f219bSJens Wiklander goto out; 321b0104773SPascal Brand } 322b0104773SPascal Brand } 323b0104773SPascal Brand 3242c028fdeSJerome Forissier res = _utee_cryp_state_alloc(algorithm, mode, (unsigned long)op->key1, 325e86f1266SJens Wiklander (unsigned long)op->key2, &op->state); 326b66f219bSJens Wiklander if (res != TEE_SUCCESS) 327b66f219bSJens Wiklander goto out; 328b0104773SPascal Brand 32905304565SCedric Chaumont /* 33005304565SCedric Chaumont * Initialize digest operations 33105304565SCedric Chaumont * Other multi-stage operations initialized w/ TEE_xxxInit functions 33205304565SCedric Chaumont * Non-applicable on asymmetric operations 33305304565SCedric Chaumont */ 33405304565SCedric Chaumont if (TEE_ALG_GET_CLASS(algorithm) == TEE_OPERATION_DIGEST) { 3352c028fdeSJerome Forissier res = _utee_hash_init(op->state, NULL, 0); 33605304565SCedric Chaumont if (res != TEE_SUCCESS) 337b66f219bSJens Wiklander goto out; 33805304565SCedric Chaumont /* v1.1: flags always set for digest operations */ 33905304565SCedric Chaumont op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 34005304565SCedric Chaumont } 34105304565SCedric Chaumont 342642a1607SCedric Chaumont op->operationState = TEE_OPERATION_STATE_INITIAL; 343642a1607SCedric Chaumont 344b0104773SPascal Brand *operation = op; 345b0104773SPascal Brand 346b66f219bSJens Wiklander out: 347b66f219bSJens Wiklander if (res != TEE_SUCCESS) { 348b66f219bSJens Wiklander if (res != TEE_ERROR_OUT_OF_MEMORY && 3499b52c538SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 350b36311adSJerome Forissier TEE_Panic(res); 351b66f219bSJens Wiklander if (op) { 352b66f219bSJens Wiklander if (op->state) { 353b66f219bSJens Wiklander TEE_FreeOperation(op); 354b66f219bSJens Wiklander } else { 355b66f219bSJens Wiklander TEE_Free(op->buffer); 356b66f219bSJens Wiklander TEE_FreeTransientObject(op->key1); 357b66f219bSJens Wiklander TEE_FreeTransientObject(op->key2); 358b66f219bSJens Wiklander TEE_Free(op); 359b66f219bSJens Wiklander } 360b66f219bSJens Wiklander } 361b66f219bSJens Wiklander } 362b66f219bSJens Wiklander 363b0104773SPascal Brand return res; 364b0104773SPascal Brand } 365b0104773SPascal Brand 366b0104773SPascal Brand void TEE_FreeOperation(TEE_OperationHandle operation) 367b0104773SPascal Brand { 368e889e80bSCedric Chaumont TEE_Result res; 369e889e80bSCedric Chaumont 370e889e80bSCedric Chaumont if (operation == TEE_HANDLE_NULL) 371e889e80bSCedric Chaumont TEE_Panic(0); 372e889e80bSCedric Chaumont 373b0104773SPascal Brand /* 374b0104773SPascal Brand * Note that keys should not be freed here, since they are 375b0104773SPascal Brand * claimed by the operation they will be freed by 376b0104773SPascal Brand * utee_cryp_state_free(). 377b0104773SPascal Brand */ 3782c028fdeSJerome Forissier res = _utee_cryp_state_free(operation->state); 379e889e80bSCedric Chaumont if (res != TEE_SUCCESS) 380b36311adSJerome Forissier TEE_Panic(res); 381e889e80bSCedric Chaumont 382b0104773SPascal Brand TEE_Free(operation->buffer); 383b0104773SPascal Brand TEE_Free(operation); 384b0104773SPascal Brand } 385b0104773SPascal Brand 386b0104773SPascal Brand void TEE_GetOperationInfo(TEE_OperationHandle operation, 387b0104773SPascal Brand TEE_OperationInfo *operationInfo) 388b0104773SPascal Brand { 389b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 390b0104773SPascal Brand TEE_Panic(0); 391b0104773SPascal Brand 392*6915bbbbSJens Wiklander __utee_check_out_annotation(operationInfo, sizeof(*operationInfo)); 393b0104773SPascal Brand 394b0104773SPascal Brand *operationInfo = operation->info; 395b0104773SPascal Brand } 396b0104773SPascal Brand 39705304565SCedric Chaumont TEE_Result TEE_GetOperationInfoMultiple(TEE_OperationHandle operation, 39805304565SCedric Chaumont TEE_OperationInfoMultiple *operationInfoMultiple, 39905304565SCedric Chaumont uint32_t *operationSize) 40005304565SCedric Chaumont { 40105304565SCedric Chaumont TEE_Result res = TEE_SUCCESS; 40205304565SCedric Chaumont TEE_ObjectInfo key_info1; 40305304565SCedric Chaumont TEE_ObjectInfo key_info2; 40405304565SCedric Chaumont uint32_t num_of_keys; 40505304565SCedric Chaumont size_t n; 40605304565SCedric Chaumont 40705304565SCedric Chaumont if (operation == TEE_HANDLE_NULL) { 40805304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 40905304565SCedric Chaumont goto out; 41005304565SCedric Chaumont } 41105304565SCedric Chaumont 412*6915bbbbSJens Wiklander __utee_check_outbuf_annotation(operationInfoMultiple, operationSize); 41305304565SCedric Chaumont 41405304565SCedric Chaumont num_of_keys = (*operationSize-sizeof(TEE_OperationInfoMultiple))/ 41505304565SCedric Chaumont sizeof(TEE_OperationInfoKey); 41605304565SCedric Chaumont 41705304565SCedric Chaumont if (num_of_keys > 2) { 41805304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 41905304565SCedric Chaumont goto out; 42005304565SCedric Chaumont } 42105304565SCedric Chaumont 42205304565SCedric Chaumont /* Two keys flag (TEE_ALG_AES_XTS only) */ 42305304565SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 42405304565SCedric Chaumont 0 && 42505304565SCedric Chaumont (num_of_keys != 2)) { 42605304565SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 42705304565SCedric Chaumont goto out; 42805304565SCedric Chaumont } 42905304565SCedric Chaumont 43005304565SCedric Chaumont /* Clear */ 43105304565SCedric Chaumont for (n = 0; n < num_of_keys; n++) { 43205304565SCedric Chaumont operationInfoMultiple->keyInformation[n].keySize = 0; 43305304565SCedric Chaumont operationInfoMultiple->keyInformation[n].requiredKeyUsage = 0; 43405304565SCedric Chaumont } 43505304565SCedric Chaumont 43605304565SCedric Chaumont if (num_of_keys == 2) { 43705304565SCedric Chaumont res = TEE_GetObjectInfo1(operation->key2, &key_info2); 43805304565SCedric Chaumont /* Key2 is not a valid handle */ 43905304565SCedric Chaumont if (res != TEE_SUCCESS) 44005304565SCedric Chaumont goto out; 44105304565SCedric Chaumont 44205304565SCedric Chaumont operationInfoMultiple->keyInformation[1].keySize = 44305304565SCedric Chaumont key_info2.keySize; 44405304565SCedric Chaumont operationInfoMultiple->keyInformation[1].requiredKeyUsage = 44505304565SCedric Chaumont operation->info.requiredKeyUsage; 44605304565SCedric Chaumont } 44705304565SCedric Chaumont 44805304565SCedric Chaumont if (num_of_keys >= 1) { 44905304565SCedric Chaumont res = TEE_GetObjectInfo1(operation->key1, &key_info1); 45005304565SCedric Chaumont /* Key1 is not a valid handle */ 45105304565SCedric Chaumont if (res != TEE_SUCCESS) { 45205304565SCedric Chaumont if (num_of_keys == 2) { 45305304565SCedric Chaumont operationInfoMultiple->keyInformation[1]. 45405304565SCedric Chaumont keySize = 0; 45505304565SCedric Chaumont operationInfoMultiple->keyInformation[1]. 45605304565SCedric Chaumont requiredKeyUsage = 0; 45705304565SCedric Chaumont } 45805304565SCedric Chaumont goto out; 45905304565SCedric Chaumont } 46005304565SCedric Chaumont 46105304565SCedric Chaumont operationInfoMultiple->keyInformation[0].keySize = 46205304565SCedric Chaumont key_info1.keySize; 46305304565SCedric Chaumont operationInfoMultiple->keyInformation[0].requiredKeyUsage = 46405304565SCedric Chaumont operation->info.requiredKeyUsage; 46505304565SCedric Chaumont } 46605304565SCedric Chaumont 46705304565SCedric Chaumont /* No key */ 46805304565SCedric Chaumont operationInfoMultiple->algorithm = operation->info.algorithm; 46905304565SCedric Chaumont operationInfoMultiple->operationClass = operation->info.operationClass; 47005304565SCedric Chaumont operationInfoMultiple->mode = operation->info.mode; 47105304565SCedric Chaumont operationInfoMultiple->digestLength = operation->info.digestLength; 47205304565SCedric Chaumont operationInfoMultiple->maxKeySize = operation->info.maxKeySize; 47305304565SCedric Chaumont operationInfoMultiple->handleState = operation->info.handleState; 47405304565SCedric Chaumont operationInfoMultiple->operationState = operation->operationState; 47505304565SCedric Chaumont operationInfoMultiple->numberOfKeys = num_of_keys; 47605304565SCedric Chaumont 47705304565SCedric Chaumont out: 47805304565SCedric Chaumont if (res != TEE_SUCCESS && 47905304565SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 480b36311adSJerome Forissier TEE_Panic(res); 48105304565SCedric Chaumont 48205304565SCedric Chaumont return res; 48305304565SCedric Chaumont } 48405304565SCedric Chaumont 485b0104773SPascal Brand void TEE_ResetOperation(TEE_OperationHandle operation) 486b0104773SPascal Brand { 487b0104773SPascal Brand TEE_Result res; 488b0104773SPascal Brand 489b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 490b0104773SPascal Brand TEE_Panic(0); 491bf80076aSCedric Chaumont 492642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET)) 493bf80076aSCedric Chaumont TEE_Panic(0); 494bf80076aSCedric Chaumont 495642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 496642a1607SCedric Chaumont 497b0104773SPascal Brand if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 4982c028fdeSJerome Forissier res = _utee_hash_init(operation->state, NULL, 0); 499b0104773SPascal Brand if (res != TEE_SUCCESS) 500b0104773SPascal Brand TEE_Panic(res); 50105304565SCedric Chaumont operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 50205304565SCedric Chaumont } else { 503b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 504b0104773SPascal Brand } 50505304565SCedric Chaumont } 506b0104773SPascal Brand 507b0104773SPascal Brand TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation, 508b0104773SPascal Brand TEE_ObjectHandle key) 509b0104773SPascal Brand { 5107583c59eSCedric Chaumont TEE_Result res; 511b0104773SPascal Brand uint32_t key_size = 0; 512b0104773SPascal Brand TEE_ObjectInfo key_info; 513b0104773SPascal Brand 514a57c1e2eSCedric Chaumont if (operation == TEE_HANDLE_NULL) { 515a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 516a57c1e2eSCedric Chaumont goto out; 517a57c1e2eSCedric Chaumont } 518a57c1e2eSCedric Chaumont 519642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 520642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 521642a1607SCedric Chaumont goto out; 522642a1607SCedric Chaumont } 523642a1607SCedric Chaumont 524a57c1e2eSCedric Chaumont if (key == TEE_HANDLE_NULL) { 525a57c1e2eSCedric Chaumont /* Operation key cleared */ 526a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key1); 527a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 528a57c1e2eSCedric Chaumont goto out; 529a57c1e2eSCedric Chaumont } 530a57c1e2eSCedric Chaumont 531a57c1e2eSCedric Chaumont /* No key for digest operation */ 532a57c1e2eSCedric Chaumont if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 533a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 534a57c1e2eSCedric Chaumont goto out; 535a57c1e2eSCedric Chaumont } 536a57c1e2eSCedric Chaumont 537a57c1e2eSCedric Chaumont /* Two keys flag not expected (TEE_ALG_AES_XTS excluded) */ 538a57c1e2eSCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 539a57c1e2eSCedric Chaumont 0) { 540a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 541a57c1e2eSCedric Chaumont goto out; 542a57c1e2eSCedric Chaumont } 543a57c1e2eSCedric Chaumont 5447583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key, &key_info); 545a57c1e2eSCedric Chaumont /* Key is not a valid handle */ 5467583c59eSCedric Chaumont if (res != TEE_SUCCESS) 547a57c1e2eSCedric Chaumont goto out; 5487583c59eSCedric Chaumont 549b0104773SPascal Brand /* Supplied key has to meet required usage */ 550b0104773SPascal Brand if ((key_info.objectUsage & operation->info.requiredKeyUsage) != 551b0104773SPascal Brand operation->info.requiredKeyUsage) { 552a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 553a57c1e2eSCedric Chaumont goto out; 554b0104773SPascal Brand } 555b0104773SPascal Brand 556a57c1e2eSCedric Chaumont if (operation->info.maxKeySize < key_info.keySize) { 557a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 558a57c1e2eSCedric Chaumont goto out; 559a57c1e2eSCedric Chaumont } 560b0104773SPascal Brand 5617583c59eSCedric Chaumont key_size = key_info.keySize; 562b0104773SPascal Brand 563b0104773SPascal Brand TEE_ResetTransientObject(operation->key1); 564b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 565b0104773SPascal Brand 5667583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key1, key); 5677583c59eSCedric Chaumont if (res != TEE_SUCCESS) 568a57c1e2eSCedric Chaumont goto out; 5697583c59eSCedric Chaumont 570b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 571b0104773SPascal Brand 572b0104773SPascal Brand operation->info.keySize = key_size; 573b0104773SPascal Brand 5747583c59eSCedric Chaumont out: 575a57c1e2eSCedric Chaumont if (res != TEE_SUCCESS && 576a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 577a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 578b36311adSJerome Forissier TEE_Panic(res); 579a57c1e2eSCedric Chaumont 580a57c1e2eSCedric Chaumont return res; 581b0104773SPascal Brand } 582b0104773SPascal Brand 583b0104773SPascal Brand TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation, 584b0104773SPascal Brand TEE_ObjectHandle key1, TEE_ObjectHandle key2) 585b0104773SPascal Brand { 5867583c59eSCedric Chaumont TEE_Result res; 587b0104773SPascal Brand uint32_t key_size = 0; 588b0104773SPascal Brand TEE_ObjectInfo key_info1; 589b0104773SPascal Brand TEE_ObjectInfo key_info2; 590b0104773SPascal Brand 591a57c1e2eSCedric Chaumont if (operation == TEE_HANDLE_NULL) { 592a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 593a57c1e2eSCedric Chaumont goto out; 594a57c1e2eSCedric Chaumont } 595a57c1e2eSCedric Chaumont 596642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 597642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 598642a1607SCedric Chaumont goto out; 599642a1607SCedric Chaumont } 600642a1607SCedric Chaumont 601a57c1e2eSCedric Chaumont /* 602a57c1e2eSCedric Chaumont * Key1/Key2 and/or are not initialized and 603a57c1e2eSCedric Chaumont * Either both keys are NULL or both are not NULL 604a57c1e2eSCedric Chaumont */ 605a57c1e2eSCedric Chaumont if (key1 == TEE_HANDLE_NULL || key2 == TEE_HANDLE_NULL) { 606a57c1e2eSCedric Chaumont /* Clear operation key1 (if needed) */ 607a57c1e2eSCedric Chaumont if (key1 == TEE_HANDLE_NULL) 608a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key1); 609a57c1e2eSCedric Chaumont /* Clear operation key2 (if needed) */ 610a57c1e2eSCedric Chaumont if (key2 == TEE_HANDLE_NULL) 611a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key2); 612a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 613a57c1e2eSCedric Chaumont goto out; 614a57c1e2eSCedric Chaumont } 615a57c1e2eSCedric Chaumont 616a57c1e2eSCedric Chaumont /* No key for digest operation */ 617a57c1e2eSCedric Chaumont if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 618a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 619a57c1e2eSCedric Chaumont goto out; 620a57c1e2eSCedric Chaumont } 621a57c1e2eSCedric Chaumont 6225b385b3fSJerome Forissier /* Two keys flag expected (TEE_ALG_AES_XTS and TEE_ALG_SM2_KEP only) */ 623a57c1e2eSCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 624a57c1e2eSCedric Chaumont 0) { 625a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 626a57c1e2eSCedric Chaumont goto out; 627a57c1e2eSCedric Chaumont } 628a57c1e2eSCedric Chaumont 6297583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key1, &key_info1); 630a57c1e2eSCedric Chaumont /* Key1 is not a valid handle */ 6317583c59eSCedric Chaumont if (res != TEE_SUCCESS) 632a57c1e2eSCedric Chaumont goto out; 6337583c59eSCedric Chaumont 634b0104773SPascal Brand /* Supplied key has to meet required usage */ 635b0104773SPascal Brand if ((key_info1.objectUsage & operation->info. 636b0104773SPascal Brand requiredKeyUsage) != operation->info.requiredKeyUsage) { 637a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 638a57c1e2eSCedric Chaumont goto out; 639b0104773SPascal Brand } 640b0104773SPascal Brand 6417583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key2, &key_info2); 642a57c1e2eSCedric Chaumont /* Key2 is not a valid handle */ 6437583c59eSCedric Chaumont if (res != TEE_SUCCESS) { 6447583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) 6457583c59eSCedric Chaumont res = TEE_ERROR_CORRUPT_OBJECT_2; 646a57c1e2eSCedric Chaumont goto out; 6477583c59eSCedric Chaumont } 6487583c59eSCedric Chaumont 649b0104773SPascal Brand /* Supplied key has to meet required usage */ 650b0104773SPascal Brand if ((key_info2.objectUsage & operation->info. 651b0104773SPascal Brand requiredKeyUsage) != operation->info.requiredKeyUsage) { 652a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 653a57c1e2eSCedric Chaumont goto out; 654b0104773SPascal Brand } 655b0104773SPascal Brand 656b0104773SPascal Brand /* 6575b385b3fSJerome Forissier * All the multi key algorithm currently supported requires the keys to 6585b385b3fSJerome Forissier * be of equal size. 659b0104773SPascal Brand */ 6605b385b3fSJerome Forissier if (key_info1.keySize != key_info2.keySize) { 661a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 662a57c1e2eSCedric Chaumont goto out; 663b0104773SPascal Brand 664a57c1e2eSCedric Chaumont } 665a57c1e2eSCedric Chaumont 666a57c1e2eSCedric Chaumont if (operation->info.maxKeySize < key_info1.keySize) { 667a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 668a57c1e2eSCedric Chaumont goto out; 669a57c1e2eSCedric Chaumont } 670b0104773SPascal Brand 671b0104773SPascal Brand /* 672b0104773SPascal Brand * Odd that only the size of one key should be reported while 673b0104773SPascal Brand * size of two key are used when allocating the operation. 674b0104773SPascal Brand */ 6757583c59eSCedric Chaumont key_size = key_info1.keySize; 676b0104773SPascal Brand 677b0104773SPascal Brand TEE_ResetTransientObject(operation->key1); 678b0104773SPascal Brand TEE_ResetTransientObject(operation->key2); 679b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 680b0104773SPascal Brand 6817583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key1, key1); 6827583c59eSCedric Chaumont if (res != TEE_SUCCESS) 683a57c1e2eSCedric Chaumont goto out; 6847583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key2, key2); 6857583c59eSCedric Chaumont if (res != TEE_SUCCESS) { 6867583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) 6877583c59eSCedric Chaumont res = TEE_ERROR_CORRUPT_OBJECT_2; 688a57c1e2eSCedric Chaumont goto out; 6897583c59eSCedric Chaumont } 6907583c59eSCedric Chaumont 691b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 692b0104773SPascal Brand 693b0104773SPascal Brand operation->info.keySize = key_size; 694b0104773SPascal Brand 6957583c59eSCedric Chaumont out: 696a57c1e2eSCedric Chaumont if (res != TEE_SUCCESS && 697a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 698a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT_2 && 699a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE && 700a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE_2) 701b36311adSJerome Forissier TEE_Panic(res); 702a57c1e2eSCedric Chaumont 703a57c1e2eSCedric Chaumont return res; 704b0104773SPascal Brand } 705b0104773SPascal Brand 706b0104773SPascal Brand void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op) 707b0104773SPascal Brand { 708b0104773SPascal Brand TEE_Result res; 709b0104773SPascal Brand 710b0104773SPascal Brand if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL) 711b0104773SPascal Brand TEE_Panic(0); 712b0104773SPascal Brand if (dst_op->info.algorithm != src_op->info.algorithm) 713b0104773SPascal Brand TEE_Panic(0); 714b0104773SPascal Brand if (src_op->info.operationClass != TEE_OPERATION_DIGEST) { 715b0104773SPascal Brand TEE_ObjectHandle key1 = TEE_HANDLE_NULL; 716b0104773SPascal Brand TEE_ObjectHandle key2 = TEE_HANDLE_NULL; 717b0104773SPascal Brand 718b0104773SPascal Brand if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) { 719b0104773SPascal Brand key1 = src_op->key1; 720b0104773SPascal Brand key2 = src_op->key2; 721b0104773SPascal Brand } 722b0104773SPascal Brand 723b0104773SPascal Brand if ((src_op->info.handleState & 724b0104773SPascal Brand TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) { 725b0104773SPascal Brand TEE_SetOperationKey(dst_op, key1); 726b0104773SPascal Brand } else { 727b0104773SPascal Brand TEE_SetOperationKey2(dst_op, key1, key2); 728b0104773SPascal Brand } 729b0104773SPascal Brand } 730b0104773SPascal Brand dst_op->info.handleState = src_op->info.handleState; 731b0104773SPascal Brand dst_op->info.keySize = src_op->info.keySize; 732642a1607SCedric Chaumont dst_op->operationState = src_op->operationState; 733b0104773SPascal Brand 734b0104773SPascal Brand if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks || 735b0104773SPascal Brand dst_op->block_size != src_op->block_size) 736b0104773SPascal Brand TEE_Panic(0); 737b0104773SPascal Brand 738b0104773SPascal Brand if (dst_op->buffer != NULL) { 739b0104773SPascal Brand if (src_op->buffer == NULL) 740b0104773SPascal Brand TEE_Panic(0); 741b0104773SPascal Brand 742b0104773SPascal Brand memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs); 743b0104773SPascal Brand dst_op->buffer_offs = src_op->buffer_offs; 744b0104773SPascal Brand } else if (src_op->buffer != NULL) { 745b0104773SPascal Brand TEE_Panic(0); 746b0104773SPascal Brand } 747b0104773SPascal Brand 7482c028fdeSJerome Forissier res = _utee_cryp_state_copy(dst_op->state, src_op->state); 749b0104773SPascal Brand if (res != TEE_SUCCESS) 750b0104773SPascal Brand TEE_Panic(res); 751b0104773SPascal Brand } 752b0104773SPascal Brand 753b0104773SPascal Brand /* Cryptographic Operations API - Message Digest Functions */ 754b0104773SPascal Brand 7558f07fe6fSJerome Forissier static void init_hash_operation(TEE_OperationHandle operation, const void *IV, 7566d15db08SJerome Forissier uint32_t IVLen) 7576d15db08SJerome Forissier { 7586d15db08SJerome Forissier TEE_Result res; 7596d15db08SJerome Forissier 7606d15db08SJerome Forissier /* 7616d15db08SJerome Forissier * Note : IV and IVLen are never used in current implementation 7626d15db08SJerome Forissier * This is why coherent values of IV and IVLen are not checked 7636d15db08SJerome Forissier */ 7642c028fdeSJerome Forissier res = _utee_hash_init(operation->state, IV, IVLen); 7656d15db08SJerome Forissier if (res != TEE_SUCCESS) 7666d15db08SJerome Forissier TEE_Panic(res); 7676d15db08SJerome Forissier operation->buffer_offs = 0; 7686d15db08SJerome Forissier operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 7696d15db08SJerome Forissier } 7706d15db08SJerome Forissier 771b0104773SPascal Brand void TEE_DigestUpdate(TEE_OperationHandle operation, 7728f07fe6fSJerome Forissier const void *chunk, uint32_t chunkSize) 773b0104773SPascal Brand { 77473d6c3baSJoakim Bech TEE_Result res = TEE_ERROR_GENERIC; 775b0104773SPascal Brand 77673d6c3baSJoakim Bech if (operation == TEE_HANDLE_NULL || 77773d6c3baSJoakim Bech operation->info.operationClass != TEE_OPERATION_DIGEST) 778b0104773SPascal Brand TEE_Panic(0); 77973d6c3baSJoakim Bech 780642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 781642a1607SCedric Chaumont 7822c028fdeSJerome Forissier res = _utee_hash_update(operation->state, chunk, chunkSize); 783b0104773SPascal Brand if (res != TEE_SUCCESS) 784b0104773SPascal Brand TEE_Panic(res); 785b0104773SPascal Brand } 786b0104773SPascal Brand 7878f07fe6fSJerome Forissier TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void *chunk, 78879a3c601SCedric Chaumont uint32_t chunkLen, void *hash, uint32_t *hashLen) 789b0104773SPascal Brand { 79087c2f6b6SCedric Chaumont TEE_Result res; 791e86f1266SJens Wiklander uint64_t hl; 79287c2f6b6SCedric Chaumont 79387c2f6b6SCedric Chaumont if ((operation == TEE_HANDLE_NULL) || 79487c2f6b6SCedric Chaumont (!chunk && chunkLen) || 79587c2f6b6SCedric Chaumont (operation->info.operationClass != TEE_OPERATION_DIGEST)) { 79687c2f6b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 79787c2f6b6SCedric Chaumont goto out; 79887c2f6b6SCedric Chaumont } 799*6915bbbbSJens Wiklander __utee_check_inout_annotation(hashLen, sizeof(*hashLen)); 80087c2f6b6SCedric Chaumont 801e86f1266SJens Wiklander hl = *hashLen; 8022c028fdeSJerome Forissier res = _utee_hash_final(operation->state, chunk, chunkLen, hash, &hl); 803e86f1266SJens Wiklander *hashLen = hl; 8046d15db08SJerome Forissier if (res != TEE_SUCCESS) 8056d15db08SJerome Forissier goto out; 8066d15db08SJerome Forissier 8076d15db08SJerome Forissier /* Reset operation state */ 8086d15db08SJerome Forissier init_hash_operation(operation, NULL, 0); 80987c2f6b6SCedric Chaumont 810642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 811642a1607SCedric Chaumont 81287c2f6b6SCedric Chaumont out: 81387c2f6b6SCedric Chaumont if (res != TEE_SUCCESS && 81487c2f6b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 815b36311adSJerome Forissier TEE_Panic(res); 81673d6c3baSJoakim Bech 81787c2f6b6SCedric Chaumont return res; 818b0104773SPascal Brand } 819b0104773SPascal Brand 820b0104773SPascal Brand /* Cryptographic Operations API - Symmetric Cipher Functions */ 821b0104773SPascal Brand 8228f07fe6fSJerome Forissier void TEE_CipherInit(TEE_OperationHandle operation, const void *IV, 8238f07fe6fSJerome Forissier uint32_t IVLen) 824b0104773SPascal Brand { 825b0104773SPascal Brand TEE_Result res; 826b0104773SPascal Brand 827b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 828b0104773SPascal Brand TEE_Panic(0); 829642a1607SCedric Chaumont 830b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_CIPHER) 831b0104773SPascal Brand TEE_Panic(0); 832642a1607SCedric Chaumont 833642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 834642a1607SCedric Chaumont !(operation->key1)) 835642a1607SCedric Chaumont TEE_Panic(0); 836642a1607SCedric Chaumont 837642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 838642a1607SCedric Chaumont TEE_ResetOperation(operation); 839642a1607SCedric Chaumont 840642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 841642a1607SCedric Chaumont 8422c028fdeSJerome Forissier res = _utee_cipher_init(operation->state, IV, IVLen); 843b0104773SPascal Brand if (res != TEE_SUCCESS) 844b0104773SPascal Brand TEE_Panic(res); 845642a1607SCedric Chaumont 846b0104773SPascal Brand operation->buffer_offs = 0; 847b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 848b0104773SPascal Brand } 849b0104773SPascal Brand 850b0104773SPascal Brand static TEE_Result tee_buffer_update( 851b0104773SPascal Brand TEE_OperationHandle op, 852e86f1266SJens Wiklander TEE_Result(*update_func)(unsigned long state, const void *src, 853e86f1266SJens Wiklander size_t slen, void *dst, uint64_t *dlen), 854b0104773SPascal Brand const void *src_data, size_t src_len, 855e86f1266SJens Wiklander void *dest_data, uint64_t *dest_len) 856b0104773SPascal Brand { 857b0104773SPascal Brand TEE_Result res; 858b0104773SPascal Brand const uint8_t *src = src_data; 859b0104773SPascal Brand size_t slen = src_len; 860b0104773SPascal Brand uint8_t *dst = dest_data; 861b0104773SPascal Brand size_t dlen = *dest_len; 862b0104773SPascal Brand size_t acc_dlen = 0; 863e86f1266SJens Wiklander uint64_t tmp_dlen; 864b0104773SPascal Brand size_t l; 865b0104773SPascal Brand size_t buffer_size; 866d3588802SPascal Brand size_t buffer_left; 867b0104773SPascal Brand 868090268f5SJens Wiklander if (!src) { 869090268f5SJens Wiklander if (slen) 870090268f5SJens Wiklander TEE_Panic(0); 871090268f5SJens Wiklander goto out; 872090268f5SJens Wiklander } 873090268f5SJens Wiklander 874d3588802SPascal Brand if (op->buffer_two_blocks) { 875b0104773SPascal Brand buffer_size = op->block_size * 2; 876d3588802SPascal Brand buffer_left = 1; 877d3588802SPascal Brand } else { 878b0104773SPascal Brand buffer_size = op->block_size; 879d3588802SPascal Brand buffer_left = 0; 880d3588802SPascal Brand } 881b0104773SPascal Brand 882b0104773SPascal Brand if (op->buffer_offs > 0) { 883b0104773SPascal Brand /* Fill up complete block */ 884b0104773SPascal Brand if (op->buffer_offs < op->block_size) 885b0104773SPascal Brand l = MIN(slen, op->block_size - op->buffer_offs); 886b0104773SPascal Brand else 887b0104773SPascal Brand l = MIN(slen, buffer_size - op->buffer_offs); 888b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, l); 889b0104773SPascal Brand op->buffer_offs += l; 890b0104773SPascal Brand src += l; 891b0104773SPascal Brand slen -= l; 892b0104773SPascal Brand if ((op->buffer_offs % op->block_size) != 0) 893b0104773SPascal Brand goto out; /* Nothing left to do */ 894b0104773SPascal Brand } 895b0104773SPascal Brand 896b0104773SPascal Brand /* If we can feed from buffer */ 897d3588802SPascal Brand if ((op->buffer_offs > 0) && 898d3588802SPascal Brand ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) { 8992ff3fdbbSPascal Brand l = ROUNDUP(op->buffer_offs + slen - buffer_size, 900b0104773SPascal Brand op->block_size); 901b0104773SPascal Brand l = MIN(op->buffer_offs, l); 902b0104773SPascal Brand tmp_dlen = dlen; 903b0104773SPascal Brand res = update_func(op->state, op->buffer, l, dst, &tmp_dlen); 904b0104773SPascal Brand if (res != TEE_SUCCESS) 905b0104773SPascal Brand TEE_Panic(res); 906b0104773SPascal Brand dst += tmp_dlen; 907b0104773SPascal Brand dlen -= tmp_dlen; 908b0104773SPascal Brand acc_dlen += tmp_dlen; 909b0104773SPascal Brand op->buffer_offs -= l; 910b0104773SPascal Brand if (op->buffer_offs > 0) { 911b0104773SPascal Brand /* 912b0104773SPascal Brand * Slen is small enough to be contained in rest buffer. 913b0104773SPascal Brand */ 914b0104773SPascal Brand memcpy(op->buffer, op->buffer + l, buffer_size - l); 915b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 916b0104773SPascal Brand op->buffer_offs += slen; 917b0104773SPascal Brand goto out; /* Nothing left to do */ 918b0104773SPascal Brand } 919b0104773SPascal Brand } 920b0104773SPascal Brand 921d3588802SPascal Brand if (slen >= (buffer_size + buffer_left)) { 922b0104773SPascal Brand /* Buffer is empty, feed as much as possible from src */ 923bf7a587fSJerome Forissier if (op->info.algorithm == TEE_ALG_AES_CTS) 924b1ecda78SJerome Forissier l = ROUNDUP(slen - buffer_size, op->block_size); 925bf7a587fSJerome Forissier else 926bf7a587fSJerome Forissier l = ROUNDUP(slen - buffer_size + 1, op->block_size); 927b0104773SPascal Brand 928b0104773SPascal Brand tmp_dlen = dlen; 929b0104773SPascal Brand res = update_func(op->state, src, l, dst, &tmp_dlen); 930b0104773SPascal Brand if (res != TEE_SUCCESS) 931b0104773SPascal Brand TEE_Panic(res); 932b0104773SPascal Brand src += l; 933b0104773SPascal Brand slen -= l; 934b0104773SPascal Brand dst += tmp_dlen; 935b0104773SPascal Brand dlen -= tmp_dlen; 936b0104773SPascal Brand acc_dlen += tmp_dlen; 937b0104773SPascal Brand } 938b0104773SPascal Brand 939b0104773SPascal Brand /* Slen is small enough to be contained in buffer. */ 940b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 941b0104773SPascal Brand op->buffer_offs += slen; 942b0104773SPascal Brand 943b0104773SPascal Brand out: 944b0104773SPascal Brand *dest_len = acc_dlen; 945b0104773SPascal Brand return TEE_SUCCESS; 946b0104773SPascal Brand } 947b0104773SPascal Brand 9488f07fe6fSJerome Forissier TEE_Result TEE_CipherUpdate(TEE_OperationHandle operation, const void *srcData, 94979a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 950b0104773SPascal Brand { 951dea1f2b6SCedric Chaumont TEE_Result res; 952b0104773SPascal Brand size_t req_dlen; 953e86f1266SJens Wiklander uint64_t dl; 954b0104773SPascal Brand 955*6915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 956dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 957dea1f2b6SCedric Chaumont goto out; 958dea1f2b6SCedric Chaumont } 959*6915bbbbSJens Wiklander __utee_check_inout_annotation(destLen, sizeof(*destLen)); 960dea1f2b6SCedric Chaumont 961642a1607SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 962dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 963dea1f2b6SCedric Chaumont goto out; 964dea1f2b6SCedric Chaumont } 965dea1f2b6SCedric Chaumont 966642a1607SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 967642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 968642a1607SCedric Chaumont goto out; 969642a1607SCedric Chaumont } 970642a1607SCedric Chaumont 971642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 972dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 973dea1f2b6SCedric Chaumont goto out; 974dea1f2b6SCedric Chaumont } 975b0104773SPascal Brand 976e32c5ddfSJerome Forissier if (!srcData && !srcLen) { 977090268f5SJens Wiklander *destLen = 0; 978e32c5ddfSJerome Forissier res = TEE_SUCCESS; 979e32c5ddfSJerome Forissier goto out; 980e32c5ddfSJerome Forissier } 981e32c5ddfSJerome Forissier 982b0104773SPascal Brand /* Calculate required dlen */ 98357aabac5SBogdan Liulko if (operation->block_size > 1) { 98457aabac5SBogdan Liulko req_dlen = ((operation->buffer_offs + srcLen) / 98557aabac5SBogdan Liulko operation->block_size) * operation->block_size; 98657aabac5SBogdan Liulko } else { 98757aabac5SBogdan Liulko req_dlen = srcLen; 98857aabac5SBogdan Liulko } 989642a1607SCedric Chaumont if (operation->buffer_two_blocks) { 990642a1607SCedric Chaumont if (req_dlen > operation->block_size * 2) 991642a1607SCedric Chaumont req_dlen -= operation->block_size * 2; 992b0104773SPascal Brand else 993b0104773SPascal Brand req_dlen = 0; 994b0104773SPascal Brand } 995b0104773SPascal Brand /* 996b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 997b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 998b0104773SPascal Brand * can't restore sync with this API. 999b0104773SPascal Brand */ 1000b0104773SPascal Brand if (*destLen < req_dlen) { 1001b0104773SPascal Brand *destLen = req_dlen; 1002dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1003dea1f2b6SCedric Chaumont goto out; 1004b0104773SPascal Brand } 1005b0104773SPascal Brand 1006e86f1266SJens Wiklander dl = *destLen; 100757aabac5SBogdan Liulko if (operation->block_size > 1) { 10082c028fdeSJerome Forissier res = tee_buffer_update(operation, _utee_cipher_update, srcData, 100957aabac5SBogdan Liulko srcLen, destData, &dl); 101057aabac5SBogdan Liulko } else { 101157aabac5SBogdan Liulko if (srcLen > 0) { 10122c028fdeSJerome Forissier res = _utee_cipher_update(operation->state, srcData, 101357aabac5SBogdan Liulko srcLen, destData, &dl); 101457aabac5SBogdan Liulko } else { 101557aabac5SBogdan Liulko res = TEE_SUCCESS; 101657aabac5SBogdan Liulko dl = 0; 101757aabac5SBogdan Liulko } 101857aabac5SBogdan Liulko } 1019e86f1266SJens Wiklander *destLen = dl; 1020b0104773SPascal Brand 1021dea1f2b6SCedric Chaumont out: 1022dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 1023dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1024b36311adSJerome Forissier TEE_Panic(res); 1025dea1f2b6SCedric Chaumont 1026dea1f2b6SCedric Chaumont return res; 1027b0104773SPascal Brand } 1028b0104773SPascal Brand 1029642a1607SCedric Chaumont TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation, 10308f07fe6fSJerome Forissier const void *srcData, uint32_t srcLen, 10318f07fe6fSJerome Forissier void *destData, uint32_t *destLen) 1032b0104773SPascal Brand { 1033*6915bbbbSJens Wiklander TEE_Result res = TEE_SUCCESS; 1034b0104773SPascal Brand uint8_t *dst = destData; 1035b0104773SPascal Brand size_t acc_dlen = 0; 1036*6915bbbbSJens Wiklander uint64_t tmp_dlen = 0; 1037*6915bbbbSJens Wiklander size_t req_dlen = 0; 1038b0104773SPascal Brand 1039*6915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 1040dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1041dea1f2b6SCedric Chaumont goto out; 1042dea1f2b6SCedric Chaumont } 1043*6915bbbbSJens Wiklander if (destLen) 1044*6915bbbbSJens Wiklander __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1045dea1f2b6SCedric Chaumont 1046642a1607SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 1047dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1048dea1f2b6SCedric Chaumont goto out; 1049dea1f2b6SCedric Chaumont } 1050dea1f2b6SCedric Chaumont 1051642a1607SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1052642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1053642a1607SCedric Chaumont goto out; 1054642a1607SCedric Chaumont } 1055642a1607SCedric Chaumont 1056642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1057dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1058dea1f2b6SCedric Chaumont goto out; 1059dea1f2b6SCedric Chaumont } 1060b0104773SPascal Brand 1061b0104773SPascal Brand /* 1062b0104773SPascal Brand * Check that the final block doesn't require padding for those 1063b0104773SPascal Brand * algorithms that requires client to supply padding. 1064b0104773SPascal Brand */ 1065642a1607SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD || 1066642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD || 1067642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD || 1068642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD || 1069642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || 1070ade6f848SJerome Forissier operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD || 1071ade6f848SJerome Forissier operation->info.algorithm == TEE_ALG_SM4_ECB_NOPAD || 1072ade6f848SJerome Forissier operation->info.algorithm == TEE_ALG_SM4_CBC_NOPAD) { 1073642a1607SCedric Chaumont if (((operation->buffer_offs + srcLen) % operation->block_size) 1074642a1607SCedric Chaumont != 0) { 1075dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1076dea1f2b6SCedric Chaumont goto out; 1077dea1f2b6SCedric Chaumont } 1078b0104773SPascal Brand } 1079b0104773SPascal Brand 1080b0104773SPascal Brand /* 1081b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1082b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1083b0104773SPascal Brand * can't restore sync with this API. 1084b0104773SPascal Brand */ 108557aabac5SBogdan Liulko if (operation->block_size > 1) { 1086642a1607SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 108757aabac5SBogdan Liulko } else { 108857aabac5SBogdan Liulko req_dlen = srcLen; 108957aabac5SBogdan Liulko } 1090*6915bbbbSJens Wiklander if (destLen) 1091*6915bbbbSJens Wiklander tmp_dlen = *destLen; 1092*6915bbbbSJens Wiklander if (tmp_dlen < req_dlen) { 1093*6915bbbbSJens Wiklander if (destLen) 1094b0104773SPascal Brand *destLen = req_dlen; 1095dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1096dea1f2b6SCedric Chaumont goto out; 1097b0104773SPascal Brand } 1098b0104773SPascal Brand 109957aabac5SBogdan Liulko if (operation->block_size > 1) { 11002c028fdeSJerome Forissier res = tee_buffer_update(operation, _utee_cipher_update, 110157aabac5SBogdan Liulko srcData, srcLen, dst, &tmp_dlen); 1102dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS) 1103dea1f2b6SCedric Chaumont goto out; 1104dea1f2b6SCedric Chaumont 1105b0104773SPascal Brand dst += tmp_dlen; 1106b0104773SPascal Brand acc_dlen += tmp_dlen; 1107b0104773SPascal Brand 1108b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 11092c028fdeSJerome Forissier res = _utee_cipher_final(operation->state, operation->buffer, 11102c028fdeSJerome Forissier operation->buffer_offs, dst, 11112c028fdeSJerome Forissier &tmp_dlen); 111257aabac5SBogdan Liulko } else { 11132c028fdeSJerome Forissier res = _utee_cipher_final(operation->state, srcData, srcLen, dst, 11142c028fdeSJerome Forissier &tmp_dlen); 111557aabac5SBogdan Liulko } 1116b0104773SPascal Brand if (res != TEE_SUCCESS) 1117dea1f2b6SCedric Chaumont goto out; 1118dea1f2b6SCedric Chaumont 1119b0104773SPascal Brand acc_dlen += tmp_dlen; 1120*6915bbbbSJens Wiklander if (destLen) 1121b0104773SPascal Brand *destLen = acc_dlen; 1122dea1f2b6SCedric Chaumont 1123642a1607SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1124642a1607SCedric Chaumont 1125642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1126642a1607SCedric Chaumont 1127dea1f2b6SCedric Chaumont out: 1128dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 1129dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1130b36311adSJerome Forissier TEE_Panic(res); 1131dea1f2b6SCedric Chaumont 1132dea1f2b6SCedric Chaumont return res; 1133b0104773SPascal Brand } 1134b0104773SPascal Brand 1135b0104773SPascal Brand /* Cryptographic Operations API - MAC Functions */ 1136b0104773SPascal Brand 11378f07fe6fSJerome Forissier void TEE_MACInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen) 1138b0104773SPascal Brand { 1139b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 1140b0104773SPascal Brand TEE_Panic(0); 1141642a1607SCedric Chaumont 1142b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_MAC) 1143b0104773SPascal Brand TEE_Panic(0); 1144642a1607SCedric Chaumont 1145642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 1146642a1607SCedric Chaumont !(operation->key1)) 1147642a1607SCedric Chaumont TEE_Panic(0); 1148642a1607SCedric Chaumont 1149642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 1150642a1607SCedric Chaumont TEE_ResetOperation(operation); 1151642a1607SCedric Chaumont 1152642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1153642a1607SCedric Chaumont 11546d15db08SJerome Forissier init_hash_operation(operation, IV, IVLen); 1155b0104773SPascal Brand } 1156b0104773SPascal Brand 11578f07fe6fSJerome Forissier void TEE_MACUpdate(TEE_OperationHandle operation, const void *chunk, 115828e0efc6SCedric Chaumont uint32_t chunkSize) 1159b0104773SPascal Brand { 1160b0104773SPascal Brand TEE_Result res; 1161b0104773SPascal Brand 116228e0efc6SCedric Chaumont if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0)) 1163b0104773SPascal Brand TEE_Panic(0); 1164642a1607SCedric Chaumont 116528e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) 1166b0104773SPascal Brand TEE_Panic(0); 1167642a1607SCedric Chaumont 116828e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1169b0104773SPascal Brand TEE_Panic(0); 1170b0104773SPascal Brand 1171642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) 1172642a1607SCedric Chaumont TEE_Panic(0); 1173642a1607SCedric Chaumont 11742c028fdeSJerome Forissier res = _utee_hash_update(operation->state, chunk, chunkSize); 1175b0104773SPascal Brand if (res != TEE_SUCCESS) 1176b0104773SPascal Brand TEE_Panic(res); 1177b0104773SPascal Brand } 1178b0104773SPascal Brand 117928e0efc6SCedric Chaumont TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation, 11808f07fe6fSJerome Forissier const void *message, uint32_t messageLen, 118179a3c601SCedric Chaumont void *mac, uint32_t *macLen) 1182b0104773SPascal Brand { 1183b0104773SPascal Brand TEE_Result res; 1184e86f1266SJens Wiklander uint64_t ml; 1185b0104773SPascal Brand 1186*6915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!message && messageLen)) { 118728e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 118828e0efc6SCedric Chaumont goto out; 118928e0efc6SCedric Chaumont } 1190*6915bbbbSJens Wiklander __utee_check_inout_annotation(macLen, sizeof(*macLen)); 1191b0104773SPascal Brand 119228e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 119328e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 119428e0efc6SCedric Chaumont goto out; 119528e0efc6SCedric Chaumont } 119628e0efc6SCedric Chaumont 119728e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 119828e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 119928e0efc6SCedric Chaumont goto out; 120028e0efc6SCedric Chaumont } 120128e0efc6SCedric Chaumont 1202642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1203642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1204642a1607SCedric Chaumont goto out; 1205642a1607SCedric Chaumont } 1206642a1607SCedric Chaumont 1207e86f1266SJens Wiklander ml = *macLen; 12082c028fdeSJerome Forissier res = _utee_hash_final(operation->state, message, messageLen, mac, &ml); 1209e86f1266SJens Wiklander *macLen = ml; 121028e0efc6SCedric Chaumont if (res != TEE_SUCCESS) 121128e0efc6SCedric Chaumont goto out; 121228e0efc6SCedric Chaumont 121328e0efc6SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 121428e0efc6SCedric Chaumont 1215642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1216642a1607SCedric Chaumont 121728e0efc6SCedric Chaumont out: 121828e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 121928e0efc6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 122028e0efc6SCedric Chaumont TEE_Panic(res); 122128e0efc6SCedric Chaumont 1222b0104773SPascal Brand return res; 1223b0104773SPascal Brand } 1224b0104773SPascal Brand 1225b0104773SPascal Brand TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation, 12268f07fe6fSJerome Forissier const void *message, uint32_t messageLen, 12278f07fe6fSJerome Forissier const void *mac, uint32_t macLen) 1228b0104773SPascal Brand { 1229b0104773SPascal Brand TEE_Result res; 1230b0104773SPascal Brand uint8_t computed_mac[TEE_MAX_HASH_SIZE]; 12317f74c64aSPascal Brand uint32_t computed_mac_size = TEE_MAX_HASH_SIZE; 1232b0104773SPascal Brand 123328e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 123428e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 123528e0efc6SCedric Chaumont goto out; 123628e0efc6SCedric Chaumont } 123728e0efc6SCedric Chaumont 123828e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 123928e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 124028e0efc6SCedric Chaumont goto out; 124128e0efc6SCedric Chaumont } 124228e0efc6SCedric Chaumont 1243642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1244642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1245642a1607SCedric Chaumont goto out; 1246642a1607SCedric Chaumont } 1247642a1607SCedric Chaumont 1248b0104773SPascal Brand res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac, 1249b0104773SPascal Brand &computed_mac_size); 1250b0104773SPascal Brand if (res != TEE_SUCCESS) 125128e0efc6SCedric Chaumont goto out; 125228e0efc6SCedric Chaumont 125328e0efc6SCedric Chaumont if (computed_mac_size != macLen) { 125428e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 125528e0efc6SCedric Chaumont goto out; 125628e0efc6SCedric Chaumont } 125728e0efc6SCedric Chaumont 125848e10604SJerome Forissier if (consttime_memcmp(mac, computed_mac, computed_mac_size) != 0) { 125928e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 126028e0efc6SCedric Chaumont goto out; 126128e0efc6SCedric Chaumont } 126228e0efc6SCedric Chaumont 1263642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1264642a1607SCedric Chaumont 126528e0efc6SCedric Chaumont out: 126628e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 126728e0efc6SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 126828e0efc6SCedric Chaumont TEE_Panic(res); 126928e0efc6SCedric Chaumont 1270b0104773SPascal Brand return res; 1271b0104773SPascal Brand } 1272b0104773SPascal Brand 1273b0104773SPascal Brand /* Cryptographic Operations API - Authenticated Encryption Functions */ 1274b0104773SPascal Brand 12758f07fe6fSJerome Forissier TEE_Result TEE_AEInit(TEE_OperationHandle operation, const void *nonce, 127679a3c601SCedric Chaumont uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen, 1277b0104773SPascal Brand uint32_t payloadLen) 1278b0104773SPascal Brand { 1279b0104773SPascal Brand TEE_Result res; 1280b0104773SPascal Brand 1281b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || nonce == NULL) { 1282b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1283b5816c88SCedric Chaumont goto out; 1284b5816c88SCedric Chaumont } 1285b5816c88SCedric Chaumont 1286b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1287b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1288b5816c88SCedric Chaumont goto out; 1289b5816c88SCedric Chaumont } 1290b0104773SPascal Brand 1291642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 1292642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1293642a1607SCedric Chaumont goto out; 1294642a1607SCedric Chaumont } 1295642a1607SCedric Chaumont 1296b0104773SPascal Brand /* 1297b0104773SPascal Brand * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core 1298b0104773SPascal Brand * in the implementation. But AES-GCM spec doesn't specify the tag len 1299b0104773SPascal Brand * according to the same principle so we have to check here instead to 1300b0104773SPascal Brand * be GP compliant. 1301b0104773SPascal Brand */ 1302b5816c88SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_GCM) { 1303b0104773SPascal Brand /* 1304b0104773SPascal Brand * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96 1305b0104773SPascal Brand */ 1306b5816c88SCedric Chaumont if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) { 1307b5816c88SCedric Chaumont res = TEE_ERROR_NOT_SUPPORTED; 1308b5816c88SCedric Chaumont goto out; 1309b5816c88SCedric Chaumont } 1310b0104773SPascal Brand } 1311b0104773SPascal Brand 13122c028fdeSJerome Forissier res = _utee_authenc_init(operation->state, nonce, nonceLen, tagLen / 8, 13132c028fdeSJerome Forissier AADLen, payloadLen); 1314b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1315b5816c88SCedric Chaumont goto out; 1316b5816c88SCedric Chaumont 13177acaf5adSAlbert Schwarzkopf operation->info.digestLength = tagLen / 8; 1318f2674567SSumit Garg operation->buffer_offs = 0; 1319b5816c88SCedric Chaumont operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 1320b5816c88SCedric Chaumont 1321b5816c88SCedric Chaumont out: 1322b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1323b5816c88SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 1324b0104773SPascal Brand TEE_Panic(res); 1325b5816c88SCedric Chaumont 1326b0104773SPascal Brand return res; 1327b0104773SPascal Brand } 1328b0104773SPascal Brand 13298f07fe6fSJerome Forissier void TEE_AEUpdateAAD(TEE_OperationHandle operation, const void *AADdata, 133079a3c601SCedric Chaumont uint32_t AADdataLen) 1331b0104773SPascal Brand { 1332b0104773SPascal Brand TEE_Result res; 1333b0104773SPascal Brand 1334b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1335b5816c88SCedric Chaumont (AADdata == NULL && AADdataLen != 0)) 1336b0104773SPascal Brand TEE_Panic(0); 1337642a1607SCedric Chaumont 1338b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) 1339b0104773SPascal Brand TEE_Panic(0); 1340642a1607SCedric Chaumont 1341b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1342b0104773SPascal Brand TEE_Panic(0); 1343b0104773SPascal Brand 13442c028fdeSJerome Forissier res = _utee_authenc_update_aad(operation->state, AADdata, AADdataLen); 1345642a1607SCedric Chaumont 1346642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1347642a1607SCedric Chaumont 1348b0104773SPascal Brand if (res != TEE_SUCCESS) 1349b0104773SPascal Brand TEE_Panic(res); 1350b0104773SPascal Brand } 1351b0104773SPascal Brand 13528f07fe6fSJerome Forissier TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, const void *srcData, 135379a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 1354b0104773SPascal Brand { 1355*6915bbbbSJens Wiklander TEE_Result res = TEE_SUCCESS; 1356*6915bbbbSJens Wiklander size_t req_dlen = 0; 1357*6915bbbbSJens Wiklander uint64_t dl = 0; 1358b0104773SPascal Brand 1359*6915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 1360b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1361b5816c88SCedric Chaumont goto out; 1362b5816c88SCedric Chaumont } 1363*6915bbbbSJens Wiklander __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1364b5816c88SCedric Chaumont 1365b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1366b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1367b5816c88SCedric Chaumont goto out; 1368b5816c88SCedric Chaumont } 1369b5816c88SCedric Chaumont 1370b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1371b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1372b5816c88SCedric Chaumont goto out; 1373b5816c88SCedric Chaumont } 1374b0104773SPascal Brand 1375827308b8SJerome Forissier if (!srcData && !srcLen) { 1376090268f5SJens Wiklander *destLen = 0; 1377827308b8SJerome Forissier res = TEE_SUCCESS; 1378827308b8SJerome Forissier goto out; 1379827308b8SJerome Forissier } 1380827308b8SJerome Forissier 1381b0104773SPascal Brand /* 1382b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1383b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1384b0104773SPascal Brand * can't restore sync with this API. 1385b0104773SPascal Brand */ 1386afc0c182SBogdan Liulko if (operation->block_size > 1) { 1387b5816c88SCedric Chaumont req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen, 1388b5816c88SCedric Chaumont operation->block_size); 1389afc0c182SBogdan Liulko } else { 1390afc0c182SBogdan Liulko req_dlen = srcLen; 1391afc0c182SBogdan Liulko } 1392afc0c182SBogdan Liulko 1393*6915bbbbSJens Wiklander dl = *destLen; 1394*6915bbbbSJens Wiklander if (dl < req_dlen) { 1395b0104773SPascal Brand *destLen = req_dlen; 1396b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1397b5816c88SCedric Chaumont goto out; 1398b0104773SPascal Brand } 1399b0104773SPascal Brand 1400afc0c182SBogdan Liulko if (operation->block_size > 1) { 14012c028fdeSJerome Forissier res = tee_buffer_update(operation, _utee_authenc_update_payload, 1402afc0c182SBogdan Liulko srcData, srcLen, destData, &dl); 1403afc0c182SBogdan Liulko } else { 1404afc0c182SBogdan Liulko if (srcLen > 0) { 14052c028fdeSJerome Forissier res = _utee_authenc_update_payload(operation->state, 1406afc0c182SBogdan Liulko srcData, srcLen, 1407afc0c182SBogdan Liulko destData, &dl); 1408afc0c182SBogdan Liulko } else { 1409afc0c182SBogdan Liulko dl = 0; 1410afc0c182SBogdan Liulko res = TEE_SUCCESS; 1411afc0c182SBogdan Liulko } 1412afc0c182SBogdan Liulko } 1413afc0c182SBogdan Liulko if (res != TEE_SUCCESS) 1414afc0c182SBogdan Liulko goto out; 1415afc0c182SBogdan Liulko 1416e86f1266SJens Wiklander *destLen = dl; 1417b0104773SPascal Brand 1418642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1419642a1607SCedric Chaumont 1420b5816c88SCedric Chaumont out: 1421b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1422b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1423b5816c88SCedric Chaumont TEE_Panic(res); 1424b5816c88SCedric Chaumont 1425b5816c88SCedric Chaumont return res; 1426b0104773SPascal Brand } 1427b0104773SPascal Brand 1428b5816c88SCedric Chaumont TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation, 14298f07fe6fSJerome Forissier const void *srcData, uint32_t srcLen, 143079a3c601SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 143179a3c601SCedric Chaumont uint32_t *tagLen) 1432b0104773SPascal Brand { 1433b0104773SPascal Brand TEE_Result res; 1434b0104773SPascal Brand uint8_t *dst = destData; 1435b0104773SPascal Brand size_t acc_dlen = 0; 1436e86f1266SJens Wiklander uint64_t tmp_dlen; 1437b0104773SPascal Brand size_t req_dlen; 1438e86f1266SJens Wiklander uint64_t tl; 1439b0104773SPascal Brand 1440*6915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 1441b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1442b5816c88SCedric Chaumont goto out; 1443b5816c88SCedric Chaumont } 1444*6915bbbbSJens Wiklander __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1445*6915bbbbSJens Wiklander __utee_check_inout_annotation(tagLen, sizeof(*tagLen)); 1446b5816c88SCedric Chaumont 1447b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1448b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1449b5816c88SCedric Chaumont goto out; 1450b5816c88SCedric Chaumont } 1451b5816c88SCedric Chaumont 1452b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1453b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1454b5816c88SCedric Chaumont goto out; 1455b5816c88SCedric Chaumont } 1456b0104773SPascal Brand 1457b0104773SPascal Brand /* 1458b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1459b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1460b0104773SPascal Brand * can't restore sync with this API. 14612733280aSEtienne Carriere * 14622733280aSEtienne Carriere * Need to check this before update_payload since sync would be lost if 14632733280aSEtienne Carriere * we return short buffer after that. 1464b0104773SPascal Brand */ 14652733280aSEtienne Carriere res = TEE_ERROR_GENERIC; 14662733280aSEtienne Carriere 1467b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1468b0104773SPascal Brand if (*destLen < req_dlen) { 1469b0104773SPascal Brand *destLen = req_dlen; 1470b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1471b0104773SPascal Brand } 1472b0104773SPascal Brand 14737acaf5adSAlbert Schwarzkopf if (*tagLen < operation->info.digestLength) { 14747acaf5adSAlbert Schwarzkopf *tagLen = operation->info.digestLength; 1475b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1476b0104773SPascal Brand } 1477b0104773SPascal Brand 14782733280aSEtienne Carriere if (res == TEE_ERROR_SHORT_BUFFER) 14792733280aSEtienne Carriere goto out; 14802733280aSEtienne Carriere 1481afc0c182SBogdan Liulko tl = *tagLen; 1482b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1483afc0c182SBogdan Liulko if (operation->block_size > 1) { 14842c028fdeSJerome Forissier res = tee_buffer_update(operation, _utee_authenc_update_payload, 1485afc0c182SBogdan Liulko srcData, srcLen, dst, &tmp_dlen); 1486b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1487b5816c88SCedric Chaumont goto out; 1488b5816c88SCedric Chaumont 1489b0104773SPascal Brand dst += tmp_dlen; 1490b0104773SPascal Brand acc_dlen += tmp_dlen; 1491b0104773SPascal Brand 1492b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 14932c028fdeSJerome Forissier res = _utee_authenc_enc_final(operation->state, 1494afc0c182SBogdan Liulko operation->buffer, 1495afc0c182SBogdan Liulko operation->buffer_offs, dst, 1496afc0c182SBogdan Liulko &tmp_dlen, tag, &tl); 1497afc0c182SBogdan Liulko } else { 14982c028fdeSJerome Forissier res = _utee_authenc_enc_final(operation->state, srcData, 1499afc0c182SBogdan Liulko srcLen, dst, &tmp_dlen, 1500e86f1266SJens Wiklander tag, &tl); 1501afc0c182SBogdan Liulko } 1502e86f1266SJens Wiklander *tagLen = tl; 1503b0104773SPascal Brand if (res != TEE_SUCCESS) 1504b5816c88SCedric Chaumont goto out; 1505b0104773SPascal Brand 1506b5816c88SCedric Chaumont acc_dlen += tmp_dlen; 1507b0104773SPascal Brand *destLen = acc_dlen; 1508642a1607SCedric Chaumont 1509b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1510b5816c88SCedric Chaumont 1511642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1512642a1607SCedric Chaumont 1513b5816c88SCedric Chaumont out: 1514b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1515b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1516b5816c88SCedric Chaumont TEE_Panic(res); 1517b0104773SPascal Brand 1518b0104773SPascal Brand return res; 1519b0104773SPascal Brand } 1520b0104773SPascal Brand 1521b5816c88SCedric Chaumont TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation, 15228f07fe6fSJerome Forissier const void *srcData, uint32_t srcLen, 1523b5816c88SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 152479a3c601SCedric Chaumont uint32_t tagLen) 1525b0104773SPascal Brand { 1526b0104773SPascal Brand TEE_Result res; 1527b0104773SPascal Brand uint8_t *dst = destData; 1528b0104773SPascal Brand size_t acc_dlen = 0; 1529e86f1266SJens Wiklander uint64_t tmp_dlen; 1530b0104773SPascal Brand size_t req_dlen; 1531b0104773SPascal Brand 1532*6915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 1533b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1534b5816c88SCedric Chaumont goto out; 1535b5816c88SCedric Chaumont } 1536*6915bbbbSJens Wiklander __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1537b5816c88SCedric Chaumont 1538b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1539b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1540b5816c88SCedric Chaumont goto out; 1541b5816c88SCedric Chaumont } 1542b5816c88SCedric Chaumont 1543b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1544b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1545b5816c88SCedric Chaumont goto out; 1546b5816c88SCedric Chaumont } 1547b0104773SPascal Brand 1548b0104773SPascal Brand /* 1549b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1550b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1551b0104773SPascal Brand * can't restore sync with this API. 1552b0104773SPascal Brand */ 1553b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1554b0104773SPascal Brand if (*destLen < req_dlen) { 1555b0104773SPascal Brand *destLen = req_dlen; 1556b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1557b5816c88SCedric Chaumont goto out; 1558b0104773SPascal Brand } 1559b0104773SPascal Brand 1560b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1561afc0c182SBogdan Liulko if (operation->block_size > 1) { 15622c028fdeSJerome Forissier res = tee_buffer_update(operation, _utee_authenc_update_payload, 1563afc0c182SBogdan Liulko srcData, srcLen, dst, &tmp_dlen); 1564b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1565b5816c88SCedric Chaumont goto out; 1566b5816c88SCedric Chaumont 1567b0104773SPascal Brand dst += tmp_dlen; 1568b0104773SPascal Brand acc_dlen += tmp_dlen; 1569b0104773SPascal Brand 1570b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 15712c028fdeSJerome Forissier res = _utee_authenc_dec_final(operation->state, 1572afc0c182SBogdan Liulko operation->buffer, 1573afc0c182SBogdan Liulko operation->buffer_offs, dst, 1574afc0c182SBogdan Liulko &tmp_dlen, tag, tagLen); 1575afc0c182SBogdan Liulko } else { 15762c028fdeSJerome Forissier res = _utee_authenc_dec_final(operation->state, srcData, 1577afc0c182SBogdan Liulko srcLen, dst, &tmp_dlen, 1578b5816c88SCedric Chaumont tag, tagLen); 1579afc0c182SBogdan Liulko } 1580b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1581b5816c88SCedric Chaumont goto out; 1582b5816c88SCedric Chaumont 1583b0104773SPascal Brand /* Supplied tagLen should match what we initiated with */ 15847acaf5adSAlbert Schwarzkopf if (tagLen != operation->info.digestLength) 1585b0104773SPascal Brand res = TEE_ERROR_MAC_INVALID; 1586b0104773SPascal Brand 1587b0104773SPascal Brand acc_dlen += tmp_dlen; 1588b0104773SPascal Brand *destLen = acc_dlen; 1589642a1607SCedric Chaumont 1590b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1591b5816c88SCedric Chaumont 1592642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1593642a1607SCedric Chaumont 1594b5816c88SCedric Chaumont out: 1595b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1596b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER && 1597b5816c88SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 1598b5816c88SCedric Chaumont TEE_Panic(res); 1599b0104773SPascal Brand 1600b0104773SPascal Brand return res; 1601b0104773SPascal Brand } 1602b0104773SPascal Brand 1603b0104773SPascal Brand /* Cryptographic Operations API - Asymmetric Functions */ 1604b0104773SPascal Brand 160512e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation, 16068f07fe6fSJerome Forissier const TEE_Attribute *params, 16078f07fe6fSJerome Forissier uint32_t paramCount, const void *srcData, 160879a3c601SCedric Chaumont uint32_t srcLen, void *destData, 160979a3c601SCedric Chaumont uint32_t *destLen) 1610b0104773SPascal Brand { 1611*6915bbbbSJens Wiklander TEE_Result res = TEE_SUCCESS; 1612e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1613*6915bbbbSJens Wiklander uint64_t dl = 0; 1614b0104773SPascal Brand 1615*6915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) 1616b0104773SPascal Brand TEE_Panic(0); 1617*6915bbbbSJens Wiklander 1618*6915bbbbSJens Wiklander __utee_check_attr_in_annotation(params, paramCount); 1619*6915bbbbSJens Wiklander __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1620*6915bbbbSJens Wiklander 162112e66b6fSCedric Chaumont if (!operation->key1) 1622b0104773SPascal Brand TEE_Panic(0); 162312e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 162412e66b6fSCedric Chaumont TEE_Panic(0); 162512e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_ENCRYPT) 1626b0104773SPascal Brand TEE_Panic(0); 1627b0104773SPascal Brand 1628e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1629e86f1266SJens Wiklander dl = *destLen; 16302c028fdeSJerome Forissier res = _utee_asymm_operate(operation->state, ua, paramCount, srcData, 1631e86f1266SJens Wiklander srcLen, destData, &dl); 1632e86f1266SJens Wiklander *destLen = dl; 163312e66b6fSCedric Chaumont 16348844ebfcSPascal Brand if (res != TEE_SUCCESS && 16358844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 16368844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1637b0104773SPascal Brand TEE_Panic(res); 163812e66b6fSCedric Chaumont 1639b0104773SPascal Brand return res; 1640b0104773SPascal Brand } 1641b0104773SPascal Brand 164212e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation, 16438f07fe6fSJerome Forissier const TEE_Attribute *params, 16448f07fe6fSJerome Forissier uint32_t paramCount, const void *srcData, 164579a3c601SCedric Chaumont uint32_t srcLen, void *destData, 164679a3c601SCedric Chaumont uint32_t *destLen) 1647b0104773SPascal Brand { 1648*6915bbbbSJens Wiklander TEE_Result res = TEE_SUCCESS; 1649e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1650*6915bbbbSJens Wiklander uint64_t dl = 0; 1651b0104773SPascal Brand 1652*6915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) 1653b0104773SPascal Brand TEE_Panic(0); 1654*6915bbbbSJens Wiklander 1655*6915bbbbSJens Wiklander __utee_check_attr_in_annotation(params, paramCount); 1656*6915bbbbSJens Wiklander __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1657*6915bbbbSJens Wiklander 165812e66b6fSCedric Chaumont if (!operation->key1) 1659b0104773SPascal Brand TEE_Panic(0); 166012e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 166112e66b6fSCedric Chaumont TEE_Panic(0); 166212e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_DECRYPT) 1663b0104773SPascal Brand TEE_Panic(0); 1664b0104773SPascal Brand 1665e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1666e86f1266SJens Wiklander dl = *destLen; 16672c028fdeSJerome Forissier res = _utee_asymm_operate(operation->state, ua, paramCount, srcData, 1668e86f1266SJens Wiklander srcLen, destData, &dl); 1669e86f1266SJens Wiklander *destLen = dl; 167012e66b6fSCedric Chaumont 16718844ebfcSPascal Brand if (res != TEE_SUCCESS && 16728844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 16738844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1674b0104773SPascal Brand TEE_Panic(res); 167512e66b6fSCedric Chaumont 1676b0104773SPascal Brand return res; 1677b0104773SPascal Brand } 1678b0104773SPascal Brand 167912e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation, 16808f07fe6fSJerome Forissier const TEE_Attribute *params, 16818f07fe6fSJerome Forissier uint32_t paramCount, const void *digest, 168279a3c601SCedric Chaumont uint32_t digestLen, void *signature, 168379a3c601SCedric Chaumont uint32_t *signatureLen) 1684b0104773SPascal Brand { 1685*6915bbbbSJens Wiklander TEE_Result res = TEE_SUCCESS; 1686e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1687*6915bbbbSJens Wiklander uint64_t sl = 0; 1688b0104773SPascal Brand 1689*6915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!digest && digestLen)) 1690b0104773SPascal Brand TEE_Panic(0); 1691*6915bbbbSJens Wiklander 1692*6915bbbbSJens Wiklander __utee_check_attr_in_annotation(params, paramCount); 1693*6915bbbbSJens Wiklander __utee_check_inout_annotation(signatureLen, sizeof(*signatureLen)); 1694*6915bbbbSJens Wiklander 169512e66b6fSCedric Chaumont if (!operation->key1) 1696b0104773SPascal Brand TEE_Panic(0); 169712e66b6fSCedric Chaumont if (operation->info.operationClass != 169812e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 169912e66b6fSCedric Chaumont TEE_Panic(0); 170012e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_SIGN) 1701b0104773SPascal Brand TEE_Panic(0); 1702b0104773SPascal Brand 1703e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1704e86f1266SJens Wiklander sl = *signatureLen; 17052c028fdeSJerome Forissier res = _utee_asymm_operate(operation->state, ua, paramCount, digest, 1706e86f1266SJens Wiklander digestLen, signature, &sl); 1707e86f1266SJens Wiklander *signatureLen = sl; 170812e66b6fSCedric Chaumont 1709b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER) 1710b0104773SPascal Brand TEE_Panic(res); 171112e66b6fSCedric Chaumont 1712b0104773SPascal Brand return res; 1713b0104773SPascal Brand } 1714b0104773SPascal Brand 171512e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation, 17168f07fe6fSJerome Forissier const TEE_Attribute *params, 17178f07fe6fSJerome Forissier uint32_t paramCount, const void *digest, 17188f07fe6fSJerome Forissier uint32_t digestLen, 17198f07fe6fSJerome Forissier const void *signature, 172079a3c601SCedric Chaumont uint32_t signatureLen) 1721b0104773SPascal Brand { 1722b0104773SPascal Brand TEE_Result res; 1723e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1724b0104773SPascal Brand 172512e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || 172612e66b6fSCedric Chaumont (digest == NULL && digestLen != 0) || 1727b0104773SPascal Brand (signature == NULL && signatureLen != 0)) 1728b0104773SPascal Brand TEE_Panic(0); 1729*6915bbbbSJens Wiklander 1730*6915bbbbSJens Wiklander __utee_check_attr_in_annotation(params, paramCount); 1731*6915bbbbSJens Wiklander 173212e66b6fSCedric Chaumont if (!operation->key1) 1733b0104773SPascal Brand TEE_Panic(0); 173412e66b6fSCedric Chaumont if (operation->info.operationClass != 173512e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 173612e66b6fSCedric Chaumont TEE_Panic(0); 173712e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_VERIFY) 1738b0104773SPascal Brand TEE_Panic(0); 1739b0104773SPascal Brand 1740e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 17412c028fdeSJerome Forissier res = _utee_asymm_verify(operation->state, ua, paramCount, digest, 174212e66b6fSCedric Chaumont digestLen, signature, signatureLen); 174312e66b6fSCedric Chaumont 1744b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID) 1745b0104773SPascal Brand TEE_Panic(res); 174612e66b6fSCedric Chaumont 1747b0104773SPascal Brand return res; 1748b0104773SPascal Brand } 1749b0104773SPascal Brand 1750b0104773SPascal Brand /* Cryptographic Operations API - Key Derivation Functions */ 1751b0104773SPascal Brand 1752b0104773SPascal Brand void TEE_DeriveKey(TEE_OperationHandle operation, 1753b0104773SPascal Brand const TEE_Attribute *params, uint32_t paramCount, 1754b0104773SPascal Brand TEE_ObjectHandle derivedKey) 1755b0104773SPascal Brand { 1756b0104773SPascal Brand TEE_Result res; 1757b0104773SPascal Brand TEE_ObjectInfo key_info; 1758e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1759b0104773SPascal Brand 1760b0104773SPascal Brand if (operation == TEE_HANDLE_NULL || derivedKey == 0) 1761b0104773SPascal Brand TEE_Panic(0); 1762*6915bbbbSJens Wiklander 1763*6915bbbbSJens Wiklander __utee_check_attr_in_annotation(params, paramCount); 1764*6915bbbbSJens Wiklander 17658854d3c6SJerome Forissier if (TEE_ALG_GET_CLASS(operation->info.algorithm) != 17668854d3c6SJerome Forissier TEE_OPERATION_KEY_DERIVATION) 1767b0104773SPascal Brand TEE_Panic(0); 1768b0104773SPascal Brand 1769b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION) 1770b0104773SPascal Brand TEE_Panic(0); 177184fa9467SCedric Chaumont if (!operation->key1) 177284fa9467SCedric Chaumont TEE_Panic(0); 1773b0104773SPascal Brand if (operation->info.mode != TEE_MODE_DERIVE) 1774b0104773SPascal Brand TEE_Panic(0); 1775b0104773SPascal Brand if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0) 1776b0104773SPascal Brand TEE_Panic(0); 1777b0104773SPascal Brand 17782c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info); 1779b0104773SPascal Brand if (res != TEE_SUCCESS) 1780b36311adSJerome Forissier TEE_Panic(res); 1781b0104773SPascal Brand 1782b0104773SPascal Brand if (key_info.objectType != TEE_TYPE_GENERIC_SECRET) 1783b0104773SPascal Brand TEE_Panic(0); 1784b0104773SPascal Brand if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1785b0104773SPascal Brand TEE_Panic(0); 1786b0104773SPascal Brand 1787e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 17882c028fdeSJerome Forissier res = _utee_cryp_derive_key(operation->state, ua, paramCount, 1789e86f1266SJens Wiklander (unsigned long)derivedKey); 1790b0104773SPascal Brand if (res != TEE_SUCCESS) 1791b0104773SPascal Brand TEE_Panic(res); 1792b0104773SPascal Brand } 1793b0104773SPascal Brand 1794b0104773SPascal Brand /* Cryptographic Operations API - Random Number Generation Functions */ 1795b0104773SPascal Brand 179679a3c601SCedric Chaumont void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen) 1797b0104773SPascal Brand { 1798b0104773SPascal Brand TEE_Result res; 1799b0104773SPascal Brand 18002c028fdeSJerome Forissier res = _utee_cryp_random_number_generate(randomBuffer, randomBufferLen); 1801b0104773SPascal Brand if (res != TEE_SUCCESS) 1802b0104773SPascal Brand TEE_Panic(res); 1803b0104773SPascal Brand } 1804433c4257SJens Wiklander 1805433c4257SJens Wiklander int rand(void) 1806433c4257SJens Wiklander { 1807433c4257SJens Wiklander int rc; 1808433c4257SJens Wiklander 1809433c4257SJens Wiklander TEE_GenerateRandom(&rc, sizeof(rc)); 1810433c4257SJens Wiklander 1811433c4257SJens Wiklander /* 1812433c4257SJens Wiklander * RAND_MAX is the larges int, INT_MAX which is all bits but the 1813433c4257SJens Wiklander * highest bit set. 1814433c4257SJens Wiklander */ 1815433c4257SJens Wiklander return rc & RAND_MAX; 1816433c4257SJens Wiklander } 181779170ce0SJerome Forissier 181879170ce0SJerome Forissier TEE_Result TEE_IsAlgorithmSupported(uint32_t alg, uint32_t element) 181979170ce0SJerome Forissier { 182079170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_AES)) { 182179170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_ECB)) { 182279170ce0SJerome Forissier if (alg == TEE_ALG_AES_ECB_NOPAD) 182379170ce0SJerome Forissier goto check_element_none; 182479170ce0SJerome Forissier } 182579170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CBC)) { 182679170ce0SJerome Forissier if (alg == TEE_ALG_AES_CBC_NOPAD) 182779170ce0SJerome Forissier goto check_element_none; 182879170ce0SJerome Forissier } 182979170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CTR)) { 183079170ce0SJerome Forissier if (alg == TEE_ALG_AES_CTR) 183179170ce0SJerome Forissier goto check_element_none; 183279170ce0SJerome Forissier } 183379170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CTS)) { 183479170ce0SJerome Forissier if (alg == TEE_ALG_AES_CTS) 183579170ce0SJerome Forissier goto check_element_none; 183679170ce0SJerome Forissier } 183779170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_XTS)) { 183879170ce0SJerome Forissier if (alg == TEE_ALG_AES_XTS) 183979170ce0SJerome Forissier goto check_element_none; 184079170ce0SJerome Forissier } 184179170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) { 184279170ce0SJerome Forissier if (alg == TEE_ALG_AES_CBC_MAC_NOPAD || 184379170ce0SJerome Forissier alg == TEE_ALG_AES_CBC_MAC_PKCS5) 184479170ce0SJerome Forissier goto check_element_none; 184579170ce0SJerome Forissier } 184679170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CMAC)) { 184779170ce0SJerome Forissier if (alg == TEE_ALG_AES_CMAC) 184879170ce0SJerome Forissier goto check_element_none; 184979170ce0SJerome Forissier } 185079170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CCM)) { 185179170ce0SJerome Forissier if (alg == TEE_ALG_AES_CCM) 185279170ce0SJerome Forissier goto check_element_none; 185379170ce0SJerome Forissier } 185479170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_GCM)) { 185579170ce0SJerome Forissier if (alg == TEE_ALG_AES_GCM) 185679170ce0SJerome Forissier goto check_element_none; 185779170ce0SJerome Forissier } 185879170ce0SJerome Forissier } 185979170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_DES)) { 186079170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_ECB)) { 186179170ce0SJerome Forissier if (alg == TEE_ALG_DES_ECB_NOPAD || 186279170ce0SJerome Forissier alg == TEE_ALG_DES3_ECB_NOPAD) 186379170ce0SJerome Forissier goto check_element_none; 186479170ce0SJerome Forissier } 186579170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CBC)) { 186679170ce0SJerome Forissier if (alg == TEE_ALG_DES_CBC_NOPAD || 186779170ce0SJerome Forissier alg == TEE_ALG_DES3_CBC_NOPAD) 186879170ce0SJerome Forissier goto check_element_none; 186979170ce0SJerome Forissier } 187079170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) { 187179170ce0SJerome Forissier if (alg == TEE_ALG_DES_CBC_MAC_NOPAD || 187279170ce0SJerome Forissier alg == TEE_ALG_DES_CBC_MAC_PKCS5 || 187379170ce0SJerome Forissier alg == TEE_ALG_DES3_CBC_MAC_NOPAD || 187479170ce0SJerome Forissier alg == TEE_ALG_DES3_CBC_MAC_PKCS5) 187579170ce0SJerome Forissier goto check_element_none; 187679170ce0SJerome Forissier } 187779170ce0SJerome Forissier } 187879170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_MD5)) { 187979170ce0SJerome Forissier if (alg == TEE_ALG_MD5) 188079170ce0SJerome Forissier goto check_element_none; 188179170ce0SJerome Forissier } 188279170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 188379170ce0SJerome Forissier if (alg == TEE_ALG_SHA1) 188479170ce0SJerome Forissier goto check_element_none; 188579170ce0SJerome Forissier } 188679170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 188779170ce0SJerome Forissier if (alg == TEE_ALG_SHA224) 188879170ce0SJerome Forissier goto check_element_none; 188979170ce0SJerome Forissier } 189079170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 189179170ce0SJerome Forissier if (alg == TEE_ALG_SHA256) 189279170ce0SJerome Forissier goto check_element_none; 189379170ce0SJerome Forissier } 189479170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA384)) { 189579170ce0SJerome Forissier if (alg == TEE_ALG_SHA384) 189679170ce0SJerome Forissier goto check_element_none; 189779170ce0SJerome Forissier } 189879170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA512)) { 189979170ce0SJerome Forissier if (alg == TEE_ALG_SHA512) 190079170ce0SJerome Forissier goto check_element_none; 190179170ce0SJerome Forissier } 190279170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) { 190379170ce0SJerome Forissier if (alg == TEE_ALG_MD5SHA1) 190479170ce0SJerome Forissier goto check_element_none; 190579170ce0SJerome Forissier } 190679170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_HMAC)) { 190779170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_MD5)) { 190879170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_MD5) 190979170ce0SJerome Forissier goto check_element_none; 191079170ce0SJerome Forissier } 191179170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 191279170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_SHA1) 191379170ce0SJerome Forissier goto check_element_none; 191479170ce0SJerome Forissier } 191579170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 191679170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_SHA224) 191779170ce0SJerome Forissier goto check_element_none; 191879170ce0SJerome Forissier } 191979170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 192079170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_SHA256) 192179170ce0SJerome Forissier goto check_element_none; 192279170ce0SJerome Forissier } 192379170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA384)) { 192479170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_SHA384) 192579170ce0SJerome Forissier goto check_element_none; 192679170ce0SJerome Forissier } 192779170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA512)) { 192879170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_SHA512) 192979170ce0SJerome Forissier goto check_element_none; 193079170ce0SJerome Forissier } 193179170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SM3)) { 193279170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_SM3) 193379170ce0SJerome Forissier goto check_element_none; 193479170ce0SJerome Forissier } 193579170ce0SJerome Forissier } 193679170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SM3)) { 193779170ce0SJerome Forissier if (alg == TEE_ALG_SM3) 193879170ce0SJerome Forissier goto check_element_none; 193979170ce0SJerome Forissier } 194079170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SM4)) { 194179170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_ECB)) { 194279170ce0SJerome Forissier if (alg == TEE_ALG_SM4_ECB_NOPAD) 194379170ce0SJerome Forissier goto check_element_none; 194479170ce0SJerome Forissier } 194579170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CBC)) { 194679170ce0SJerome Forissier if (alg == TEE_ALG_SM4_CBC_NOPAD) 194779170ce0SJerome Forissier goto check_element_none; 194879170ce0SJerome Forissier } 194979170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CTR)) { 195079170ce0SJerome Forissier if (alg == TEE_ALG_SM4_CTR) 195179170ce0SJerome Forissier goto check_element_none; 195279170ce0SJerome Forissier } 195379170ce0SJerome Forissier } 195479170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_RSA)) { 195579170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_MD5)) { 195679170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5) 195779170ce0SJerome Forissier goto check_element_none; 195879170ce0SJerome Forissier } 195979170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 196079170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA1 || 196179170ce0SJerome Forissier alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1 || 196279170ce0SJerome Forissier alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1) 196379170ce0SJerome Forissier goto check_element_none; 196479170ce0SJerome Forissier } 196579170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) { 196679170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5SHA1) 196779170ce0SJerome Forissier goto check_element_none; 196879170ce0SJerome Forissier } 196979170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 197079170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA224 || 197179170ce0SJerome Forissier alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224 || 197279170ce0SJerome Forissier alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224) 197379170ce0SJerome Forissier goto check_element_none; 197479170ce0SJerome Forissier } 197579170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 197679170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA256 || 197779170ce0SJerome Forissier alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256 || 197879170ce0SJerome Forissier alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256) 197979170ce0SJerome Forissier goto check_element_none; 198079170ce0SJerome Forissier } 198179170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA384)) { 198279170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA384 || 198379170ce0SJerome Forissier alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384 || 198479170ce0SJerome Forissier alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384) 198579170ce0SJerome Forissier goto check_element_none; 198679170ce0SJerome Forissier } 198779170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA512)) { 198879170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA512 || 198979170ce0SJerome Forissier alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512 || 199079170ce0SJerome Forissier alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512) 199179170ce0SJerome Forissier goto check_element_none; 199279170ce0SJerome Forissier } 199379170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_RSASSA_NA1)) { 199479170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5) 199579170ce0SJerome Forissier goto check_element_none; 199679170ce0SJerome Forissier } 199779170ce0SJerome Forissier if (alg == TEE_ALG_RSA_NOPAD) 199879170ce0SJerome Forissier goto check_element_none; 199979170ce0SJerome Forissier } 200079170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_DSA)) { 200179170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 200279170ce0SJerome Forissier if (alg == TEE_ALG_DSA_SHA1) 200379170ce0SJerome Forissier goto check_element_none; 200479170ce0SJerome Forissier } 200579170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 200679170ce0SJerome Forissier if (alg == TEE_ALG_DSA_SHA224) 200779170ce0SJerome Forissier goto check_element_none; 200879170ce0SJerome Forissier } 200979170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 201079170ce0SJerome Forissier if (alg == TEE_ALG_DSA_SHA256) 201179170ce0SJerome Forissier goto check_element_none; 201279170ce0SJerome Forissier } 201379170ce0SJerome Forissier } 201479170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_DH)) { 201579170ce0SJerome Forissier if (alg == TEE_ALG_DH_DERIVE_SHARED_SECRET) 201679170ce0SJerome Forissier goto check_element_none; 201779170ce0SJerome Forissier } 201879170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_ECC)) { 201979170ce0SJerome Forissier if ((alg == TEE_ALG_ECDH_P192 || alg == TEE_ALG_ECDSA_P192) && 202079170ce0SJerome Forissier element == TEE_ECC_CURVE_NIST_P192) 202179170ce0SJerome Forissier return TEE_SUCCESS; 202279170ce0SJerome Forissier if ((alg == TEE_ALG_ECDH_P224 || alg == TEE_ALG_ECDSA_P224) && 202379170ce0SJerome Forissier element == TEE_ECC_CURVE_NIST_P224) 202479170ce0SJerome Forissier return TEE_SUCCESS; 202579170ce0SJerome Forissier if ((alg == TEE_ALG_ECDH_P256 || alg == TEE_ALG_ECDSA_P256) && 202679170ce0SJerome Forissier element == TEE_ECC_CURVE_NIST_P256) 202779170ce0SJerome Forissier return TEE_SUCCESS; 202879170ce0SJerome Forissier if ((alg == TEE_ALG_ECDH_P384 || alg == TEE_ALG_ECDSA_P384) && 202979170ce0SJerome Forissier element == TEE_ECC_CURVE_NIST_P384) 203079170ce0SJerome Forissier return TEE_SUCCESS; 203179170ce0SJerome Forissier if ((alg == TEE_ALG_ECDH_P521 || alg == TEE_ALG_ECDSA_P521) && 203279170ce0SJerome Forissier element == TEE_ECC_CURVE_NIST_P521) 203379170ce0SJerome Forissier return TEE_SUCCESS; 203479170ce0SJerome Forissier } 203579170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SM2_DSA)) { 203679170ce0SJerome Forissier if (alg == TEE_ALG_SM2_DSA_SM3 && element == TEE_ECC_CURVE_SM2) 203779170ce0SJerome Forissier return TEE_SUCCESS; 203879170ce0SJerome Forissier } 203979170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SM2_KEP)) { 204079170ce0SJerome Forissier if (alg == TEE_ALG_SM2_KEP && element == TEE_ECC_CURVE_SM2) 204179170ce0SJerome Forissier return TEE_SUCCESS; 204279170ce0SJerome Forissier } 204379170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SM2_PKE)) { 204479170ce0SJerome Forissier if (alg == TEE_ALG_SM2_PKE && element == TEE_ECC_CURVE_SM2) 204579170ce0SJerome Forissier return TEE_SUCCESS; 204679170ce0SJerome Forissier } 204779170ce0SJerome Forissier 204879170ce0SJerome Forissier return TEE_ERROR_NOT_SUPPORTED; 204979170ce0SJerome Forissier check_element_none: 205079170ce0SJerome Forissier if (element == TEE_CRYPTO_ELEMENT_NONE) 205179170ce0SJerome Forissier return TEE_SUCCESS; 205279170ce0SJerome Forissier return TEE_ERROR_NOT_SUPPORTED; 205379170ce0SJerome Forissier } 2054