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 112*cf5c060cSJens Wiklander /* Check algorithm mode (and maxKeySize for digests) */ 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; 240*cf5c060cSJens Wiklander if (maxKeySize) 241*cf5c060cSJens Wiklander return TEE_ERROR_NOT_SUPPORTED; 24205304565SCedric Chaumont /* v1.1: flags always set for digest operations */ 243b0104773SPascal Brand handle_state |= TEE_HANDLE_FLAG_KEY_SET; 244b0104773SPascal Brand req_key_usage = 0; 245b0104773SPascal Brand break; 246b0104773SPascal Brand 247b0104773SPascal Brand case TEE_ALG_DES_CBC_MAC_NOPAD: 248b0104773SPascal Brand case TEE_ALG_AES_CBC_MAC_NOPAD: 249b0104773SPascal Brand case TEE_ALG_AES_CBC_MAC_PKCS5: 250b0104773SPascal Brand case TEE_ALG_AES_CMAC: 251b0104773SPascal Brand case TEE_ALG_DES_CBC_MAC_PKCS5: 252b0104773SPascal Brand case TEE_ALG_DES3_CBC_MAC_NOPAD: 253b0104773SPascal Brand case TEE_ALG_DES3_CBC_MAC_PKCS5: 254b0104773SPascal Brand case TEE_ALG_HMAC_MD5: 255b0104773SPascal Brand case TEE_ALG_HMAC_SHA1: 256b0104773SPascal Brand case TEE_ALG_HMAC_SHA224: 257b0104773SPascal Brand case TEE_ALG_HMAC_SHA256: 258b0104773SPascal Brand case TEE_ALG_HMAC_SHA384: 259b0104773SPascal Brand case TEE_ALG_HMAC_SHA512: 26047645577SJerome Forissier case TEE_ALG_HMAC_SM3: 261b0104773SPascal Brand if (mode != TEE_MODE_MAC) 262b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 263b0104773SPascal Brand req_key_usage = TEE_USAGE_MAC; 264b0104773SPascal Brand break; 265b0104773SPascal Brand 266b0104773SPascal Brand default: 267b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 268b0104773SPascal Brand } 269b0104773SPascal Brand 270b66f219bSJens Wiklander op = TEE_Malloc(sizeof(*op), TEE_MALLOC_FILL_ZERO); 2719b52c538SCedric Chaumont if (!op) 272b0104773SPascal Brand return TEE_ERROR_OUT_OF_MEMORY; 273b0104773SPascal Brand 274b0104773SPascal Brand op->info.algorithm = algorithm; 275b0104773SPascal Brand op->info.operationClass = TEE_ALG_GET_CLASS(algorithm); 2766a2e0a9fSGabor Szekely #ifdef CFG_CRYPTO_RSASSA_NA1 2776a2e0a9fSGabor Szekely if (algorithm == TEE_ALG_RSASSA_PKCS1_V1_5) 2786a2e0a9fSGabor Szekely op->info.operationClass = TEE_OPERATION_ASYMMETRIC_SIGNATURE; 2796a2e0a9fSGabor Szekely #endif 280b0104773SPascal Brand op->info.mode = mode; 2812e5e6460SAlbert Schwarzkopf op->info.digestLength = TEE_ALG_GET_DIGEST_SIZE(algorithm); 282b0104773SPascal Brand op->info.maxKeySize = maxKeySize; 283b0104773SPascal Brand op->info.requiredKeyUsage = req_key_usage; 284b0104773SPascal Brand op->info.handleState = handle_state; 285b0104773SPascal Brand 286b0104773SPascal Brand if (block_size > 1) { 287b0104773SPascal Brand size_t buffer_size = block_size; 288b0104773SPascal Brand 289b0104773SPascal Brand if (buffer_two_blocks) 290b0104773SPascal Brand buffer_size *= 2; 291b0104773SPascal Brand 2929b52c538SCedric Chaumont op->buffer = TEE_Malloc(buffer_size, 2939b52c538SCedric Chaumont TEE_USER_MEM_HINT_NO_FILL_ZERO); 294b0104773SPascal Brand if (op->buffer == NULL) { 295b0104773SPascal Brand res = TEE_ERROR_OUT_OF_MEMORY; 296b66f219bSJens Wiklander goto out; 297b0104773SPascal Brand } 298b0104773SPascal Brand } 299b0104773SPascal Brand op->block_size = block_size; 300b0104773SPascal Brand op->buffer_two_blocks = buffer_two_blocks; 301b0104773SPascal Brand 302b0104773SPascal Brand if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) { 303b0104773SPascal Brand uint32_t mks = maxKeySize; 304b0104773SPascal Brand TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm, 305b0104773SPascal Brand with_private_key); 306b0104773SPascal Brand 307b0104773SPascal Brand /* 308b0104773SPascal Brand * If two keys are expected the max key size is the sum of 309b0104773SPascal Brand * the size of both keys. 310b0104773SPascal Brand */ 311b0104773SPascal Brand if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) 312b0104773SPascal Brand mks /= 2; 313b0104773SPascal Brand 314b0104773SPascal Brand res = TEE_AllocateTransientObject(key_type, mks, &op->key1); 315b0104773SPascal Brand if (res != TEE_SUCCESS) 316b66f219bSJens Wiklander goto out; 317b0104773SPascal Brand 31805304565SCedric Chaumont if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) { 3199b52c538SCedric Chaumont res = TEE_AllocateTransientObject(key_type, mks, 320b0104773SPascal Brand &op->key2); 321b0104773SPascal Brand if (res != TEE_SUCCESS) 322b66f219bSJens Wiklander goto out; 323b0104773SPascal Brand } 324b0104773SPascal Brand } 325b0104773SPascal Brand 3262c028fdeSJerome Forissier res = _utee_cryp_state_alloc(algorithm, mode, (unsigned long)op->key1, 327e86f1266SJens Wiklander (unsigned long)op->key2, &op->state); 328b66f219bSJens Wiklander if (res != TEE_SUCCESS) 329b66f219bSJens Wiklander goto out; 330b0104773SPascal Brand 33105304565SCedric Chaumont /* 33205304565SCedric Chaumont * Initialize digest operations 33305304565SCedric Chaumont * Other multi-stage operations initialized w/ TEE_xxxInit functions 33405304565SCedric Chaumont * Non-applicable on asymmetric operations 33505304565SCedric Chaumont */ 33605304565SCedric Chaumont if (TEE_ALG_GET_CLASS(algorithm) == TEE_OPERATION_DIGEST) { 3372c028fdeSJerome Forissier res = _utee_hash_init(op->state, NULL, 0); 33805304565SCedric Chaumont if (res != TEE_SUCCESS) 339b66f219bSJens Wiklander goto out; 34005304565SCedric Chaumont /* v1.1: flags always set for digest operations */ 34105304565SCedric Chaumont op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 34205304565SCedric Chaumont } 34305304565SCedric Chaumont 344642a1607SCedric Chaumont op->operationState = TEE_OPERATION_STATE_INITIAL; 345642a1607SCedric Chaumont 346b0104773SPascal Brand *operation = op; 347b0104773SPascal Brand 348b66f219bSJens Wiklander out: 349b66f219bSJens Wiklander if (res != TEE_SUCCESS) { 350b66f219bSJens Wiklander if (res != TEE_ERROR_OUT_OF_MEMORY && 3519b52c538SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 352b36311adSJerome Forissier TEE_Panic(res); 353b66f219bSJens Wiklander if (op) { 354b66f219bSJens Wiklander if (op->state) { 355b66f219bSJens Wiklander TEE_FreeOperation(op); 356b66f219bSJens Wiklander } else { 357b66f219bSJens Wiklander TEE_Free(op->buffer); 358b66f219bSJens Wiklander TEE_FreeTransientObject(op->key1); 359b66f219bSJens Wiklander TEE_FreeTransientObject(op->key2); 360b66f219bSJens Wiklander TEE_Free(op); 361b66f219bSJens Wiklander } 362b66f219bSJens Wiklander } 363b66f219bSJens Wiklander } 364b66f219bSJens Wiklander 365b0104773SPascal Brand return res; 366b0104773SPascal Brand } 367b0104773SPascal Brand 368b0104773SPascal Brand void TEE_FreeOperation(TEE_OperationHandle operation) 369b0104773SPascal Brand { 370e889e80bSCedric Chaumont TEE_Result res; 371e889e80bSCedric Chaumont 372e889e80bSCedric Chaumont if (operation == TEE_HANDLE_NULL) 373e889e80bSCedric Chaumont TEE_Panic(0); 374e889e80bSCedric Chaumont 375b0104773SPascal Brand /* 376b0104773SPascal Brand * Note that keys should not be freed here, since they are 377b0104773SPascal Brand * claimed by the operation they will be freed by 378b0104773SPascal Brand * utee_cryp_state_free(). 379b0104773SPascal Brand */ 3802c028fdeSJerome Forissier res = _utee_cryp_state_free(operation->state); 381e889e80bSCedric Chaumont if (res != TEE_SUCCESS) 382b36311adSJerome Forissier TEE_Panic(res); 383e889e80bSCedric Chaumont 384b0104773SPascal Brand TEE_Free(operation->buffer); 385b0104773SPascal Brand TEE_Free(operation); 386b0104773SPascal Brand } 387b0104773SPascal Brand 388b0104773SPascal Brand void TEE_GetOperationInfo(TEE_OperationHandle operation, 389b0104773SPascal Brand TEE_OperationInfo *operationInfo) 390b0104773SPascal Brand { 391b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 392b0104773SPascal Brand TEE_Panic(0); 393b0104773SPascal Brand 3946915bbbbSJens Wiklander __utee_check_out_annotation(operationInfo, sizeof(*operationInfo)); 395b0104773SPascal Brand 396b0104773SPascal Brand *operationInfo = operation->info; 397b0104773SPascal Brand } 398b0104773SPascal Brand 39905304565SCedric Chaumont TEE_Result TEE_GetOperationInfoMultiple(TEE_OperationHandle operation, 40005304565SCedric Chaumont TEE_OperationInfoMultiple *operationInfoMultiple, 40105304565SCedric Chaumont uint32_t *operationSize) 40205304565SCedric Chaumont { 40305304565SCedric Chaumont TEE_Result res = TEE_SUCCESS; 40405304565SCedric Chaumont TEE_ObjectInfo key_info1; 40505304565SCedric Chaumont TEE_ObjectInfo key_info2; 40605304565SCedric Chaumont uint32_t num_of_keys; 40705304565SCedric Chaumont size_t n; 40805304565SCedric Chaumont 40905304565SCedric Chaumont if (operation == TEE_HANDLE_NULL) { 41005304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 41105304565SCedric Chaumont goto out; 41205304565SCedric Chaumont } 41305304565SCedric Chaumont 4146915bbbbSJens Wiklander __utee_check_outbuf_annotation(operationInfoMultiple, operationSize); 41505304565SCedric Chaumont 41605304565SCedric Chaumont num_of_keys = (*operationSize-sizeof(TEE_OperationInfoMultiple))/ 41705304565SCedric Chaumont sizeof(TEE_OperationInfoKey); 41805304565SCedric Chaumont 41905304565SCedric Chaumont if (num_of_keys > 2) { 42005304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 42105304565SCedric Chaumont goto out; 42205304565SCedric Chaumont } 42305304565SCedric Chaumont 42405304565SCedric Chaumont /* Two keys flag (TEE_ALG_AES_XTS only) */ 42505304565SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 42605304565SCedric Chaumont 0 && 42705304565SCedric Chaumont (num_of_keys != 2)) { 42805304565SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 42905304565SCedric Chaumont goto out; 43005304565SCedric Chaumont } 43105304565SCedric Chaumont 43205304565SCedric Chaumont /* Clear */ 43305304565SCedric Chaumont for (n = 0; n < num_of_keys; n++) { 43405304565SCedric Chaumont operationInfoMultiple->keyInformation[n].keySize = 0; 43505304565SCedric Chaumont operationInfoMultiple->keyInformation[n].requiredKeyUsage = 0; 43605304565SCedric Chaumont } 43705304565SCedric Chaumont 43805304565SCedric Chaumont if (num_of_keys == 2) { 43905304565SCedric Chaumont res = TEE_GetObjectInfo1(operation->key2, &key_info2); 44005304565SCedric Chaumont /* Key2 is not a valid handle */ 44105304565SCedric Chaumont if (res != TEE_SUCCESS) 44205304565SCedric Chaumont goto out; 44305304565SCedric Chaumont 44405304565SCedric Chaumont operationInfoMultiple->keyInformation[1].keySize = 44505304565SCedric Chaumont key_info2.keySize; 44605304565SCedric Chaumont operationInfoMultiple->keyInformation[1].requiredKeyUsage = 44705304565SCedric Chaumont operation->info.requiredKeyUsage; 44805304565SCedric Chaumont } 44905304565SCedric Chaumont 45005304565SCedric Chaumont if (num_of_keys >= 1) { 45105304565SCedric Chaumont res = TEE_GetObjectInfo1(operation->key1, &key_info1); 45205304565SCedric Chaumont /* Key1 is not a valid handle */ 45305304565SCedric Chaumont if (res != TEE_SUCCESS) { 45405304565SCedric Chaumont if (num_of_keys == 2) { 45505304565SCedric Chaumont operationInfoMultiple->keyInformation[1]. 45605304565SCedric Chaumont keySize = 0; 45705304565SCedric Chaumont operationInfoMultiple->keyInformation[1]. 45805304565SCedric Chaumont requiredKeyUsage = 0; 45905304565SCedric Chaumont } 46005304565SCedric Chaumont goto out; 46105304565SCedric Chaumont } 46205304565SCedric Chaumont 46305304565SCedric Chaumont operationInfoMultiple->keyInformation[0].keySize = 46405304565SCedric Chaumont key_info1.keySize; 46505304565SCedric Chaumont operationInfoMultiple->keyInformation[0].requiredKeyUsage = 46605304565SCedric Chaumont operation->info.requiredKeyUsage; 46705304565SCedric Chaumont } 46805304565SCedric Chaumont 46905304565SCedric Chaumont /* No key */ 47005304565SCedric Chaumont operationInfoMultiple->algorithm = operation->info.algorithm; 47105304565SCedric Chaumont operationInfoMultiple->operationClass = operation->info.operationClass; 47205304565SCedric Chaumont operationInfoMultiple->mode = operation->info.mode; 47305304565SCedric Chaumont operationInfoMultiple->digestLength = operation->info.digestLength; 47405304565SCedric Chaumont operationInfoMultiple->maxKeySize = operation->info.maxKeySize; 47505304565SCedric Chaumont operationInfoMultiple->handleState = operation->info.handleState; 47605304565SCedric Chaumont operationInfoMultiple->operationState = operation->operationState; 47705304565SCedric Chaumont operationInfoMultiple->numberOfKeys = num_of_keys; 47805304565SCedric Chaumont 47905304565SCedric Chaumont out: 48005304565SCedric Chaumont if (res != TEE_SUCCESS && 48105304565SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 482b36311adSJerome Forissier TEE_Panic(res); 48305304565SCedric Chaumont 48405304565SCedric Chaumont return res; 48505304565SCedric Chaumont } 48605304565SCedric Chaumont 487b0104773SPascal Brand void TEE_ResetOperation(TEE_OperationHandle operation) 488b0104773SPascal Brand { 489b0104773SPascal Brand TEE_Result res; 490b0104773SPascal Brand 491b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 492b0104773SPascal Brand TEE_Panic(0); 493bf80076aSCedric Chaumont 494642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET)) 495bf80076aSCedric Chaumont TEE_Panic(0); 496bf80076aSCedric Chaumont 497642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 498642a1607SCedric Chaumont 499b0104773SPascal Brand if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 5002c028fdeSJerome Forissier res = _utee_hash_init(operation->state, NULL, 0); 501b0104773SPascal Brand if (res != TEE_SUCCESS) 502b0104773SPascal Brand TEE_Panic(res); 50305304565SCedric Chaumont operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 50405304565SCedric Chaumont } else { 505b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 506b0104773SPascal Brand } 50705304565SCedric Chaumont } 508b0104773SPascal Brand 509b0104773SPascal Brand TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation, 510b0104773SPascal Brand TEE_ObjectHandle key) 511b0104773SPascal Brand { 5127583c59eSCedric Chaumont TEE_Result res; 513b0104773SPascal Brand uint32_t key_size = 0; 514b0104773SPascal Brand TEE_ObjectInfo key_info; 515b0104773SPascal Brand 516a57c1e2eSCedric Chaumont if (operation == TEE_HANDLE_NULL) { 517a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 518a57c1e2eSCedric Chaumont goto out; 519a57c1e2eSCedric Chaumont } 520a57c1e2eSCedric Chaumont 521642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 522642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 523642a1607SCedric Chaumont goto out; 524642a1607SCedric Chaumont } 525642a1607SCedric Chaumont 526a57c1e2eSCedric Chaumont if (key == TEE_HANDLE_NULL) { 527a57c1e2eSCedric Chaumont /* Operation key cleared */ 528a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key1); 529a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 530a57c1e2eSCedric Chaumont goto out; 531a57c1e2eSCedric Chaumont } 532a57c1e2eSCedric Chaumont 533a57c1e2eSCedric Chaumont /* No key for digest operation */ 534a57c1e2eSCedric Chaumont if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 535a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 536a57c1e2eSCedric Chaumont goto out; 537a57c1e2eSCedric Chaumont } 538a57c1e2eSCedric Chaumont 539a57c1e2eSCedric Chaumont /* Two keys flag not expected (TEE_ALG_AES_XTS excluded) */ 540a57c1e2eSCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 541a57c1e2eSCedric Chaumont 0) { 542a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 543a57c1e2eSCedric Chaumont goto out; 544a57c1e2eSCedric Chaumont } 545a57c1e2eSCedric Chaumont 5467583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key, &key_info); 547a57c1e2eSCedric Chaumont /* Key is not a valid handle */ 5487583c59eSCedric Chaumont if (res != TEE_SUCCESS) 549a57c1e2eSCedric Chaumont goto out; 5507583c59eSCedric Chaumont 551b0104773SPascal Brand /* Supplied key has to meet required usage */ 552b0104773SPascal Brand if ((key_info.objectUsage & operation->info.requiredKeyUsage) != 553b0104773SPascal Brand operation->info.requiredKeyUsage) { 554a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 555a57c1e2eSCedric Chaumont goto out; 556b0104773SPascal Brand } 557b0104773SPascal Brand 558a57c1e2eSCedric Chaumont if (operation->info.maxKeySize < key_info.keySize) { 559a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 560a57c1e2eSCedric Chaumont goto out; 561a57c1e2eSCedric Chaumont } 562b0104773SPascal Brand 5637583c59eSCedric Chaumont key_size = key_info.keySize; 564b0104773SPascal Brand 565b0104773SPascal Brand TEE_ResetTransientObject(operation->key1); 566b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 567b0104773SPascal Brand 5687583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key1, key); 5697583c59eSCedric Chaumont if (res != TEE_SUCCESS) 570a57c1e2eSCedric Chaumont goto out; 5717583c59eSCedric Chaumont 572b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 573b0104773SPascal Brand 574b0104773SPascal Brand operation->info.keySize = key_size; 575b0104773SPascal Brand 5767583c59eSCedric Chaumont out: 577a57c1e2eSCedric Chaumont if (res != TEE_SUCCESS && 578a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 579a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 580b36311adSJerome Forissier TEE_Panic(res); 581a57c1e2eSCedric Chaumont 582a57c1e2eSCedric Chaumont return res; 583b0104773SPascal Brand } 584b0104773SPascal Brand 585b0104773SPascal Brand TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation, 586b0104773SPascal Brand TEE_ObjectHandle key1, TEE_ObjectHandle key2) 587b0104773SPascal Brand { 5887583c59eSCedric Chaumont TEE_Result res; 589b0104773SPascal Brand uint32_t key_size = 0; 590b0104773SPascal Brand TEE_ObjectInfo key_info1; 591b0104773SPascal Brand TEE_ObjectInfo key_info2; 592b0104773SPascal Brand 593a57c1e2eSCedric Chaumont if (operation == TEE_HANDLE_NULL) { 594a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 595a57c1e2eSCedric Chaumont goto out; 596a57c1e2eSCedric Chaumont } 597a57c1e2eSCedric Chaumont 598642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 599642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 600642a1607SCedric Chaumont goto out; 601642a1607SCedric Chaumont } 602642a1607SCedric Chaumont 603a57c1e2eSCedric Chaumont /* 604a57c1e2eSCedric Chaumont * Key1/Key2 and/or are not initialized and 605a57c1e2eSCedric Chaumont * Either both keys are NULL or both are not NULL 606a57c1e2eSCedric Chaumont */ 607a57c1e2eSCedric Chaumont if (key1 == TEE_HANDLE_NULL || key2 == TEE_HANDLE_NULL) { 608a57c1e2eSCedric Chaumont /* Clear operation key1 (if needed) */ 609a57c1e2eSCedric Chaumont if (key1 == TEE_HANDLE_NULL) 610a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key1); 611a57c1e2eSCedric Chaumont /* Clear operation key2 (if needed) */ 612a57c1e2eSCedric Chaumont if (key2 == TEE_HANDLE_NULL) 613a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key2); 614a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 615a57c1e2eSCedric Chaumont goto out; 616a57c1e2eSCedric Chaumont } 617a57c1e2eSCedric Chaumont 618a57c1e2eSCedric Chaumont /* No key for digest operation */ 619a57c1e2eSCedric Chaumont if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 620a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 621a57c1e2eSCedric Chaumont goto out; 622a57c1e2eSCedric Chaumont } 623a57c1e2eSCedric Chaumont 6245b385b3fSJerome Forissier /* Two keys flag expected (TEE_ALG_AES_XTS and TEE_ALG_SM2_KEP only) */ 625a57c1e2eSCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 626a57c1e2eSCedric Chaumont 0) { 627a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 628a57c1e2eSCedric Chaumont goto out; 629a57c1e2eSCedric Chaumont } 630a57c1e2eSCedric Chaumont 6317583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key1, &key_info1); 632a57c1e2eSCedric Chaumont /* Key1 is not a valid handle */ 6337583c59eSCedric Chaumont if (res != TEE_SUCCESS) 634a57c1e2eSCedric Chaumont goto out; 6357583c59eSCedric Chaumont 636b0104773SPascal Brand /* Supplied key has to meet required usage */ 637b0104773SPascal Brand if ((key_info1.objectUsage & operation->info. 638b0104773SPascal Brand requiredKeyUsage) != operation->info.requiredKeyUsage) { 639a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 640a57c1e2eSCedric Chaumont goto out; 641b0104773SPascal Brand } 642b0104773SPascal Brand 6437583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key2, &key_info2); 644a57c1e2eSCedric Chaumont /* Key2 is not a valid handle */ 6457583c59eSCedric Chaumont if (res != TEE_SUCCESS) { 6467583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) 6477583c59eSCedric Chaumont res = TEE_ERROR_CORRUPT_OBJECT_2; 648a57c1e2eSCedric Chaumont goto out; 6497583c59eSCedric Chaumont } 6507583c59eSCedric Chaumont 651b0104773SPascal Brand /* Supplied key has to meet required usage */ 652b0104773SPascal Brand if ((key_info2.objectUsage & operation->info. 653b0104773SPascal Brand requiredKeyUsage) != operation->info.requiredKeyUsage) { 654a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 655a57c1e2eSCedric Chaumont goto out; 656b0104773SPascal Brand } 657b0104773SPascal Brand 658b0104773SPascal Brand /* 6595b385b3fSJerome Forissier * All the multi key algorithm currently supported requires the keys to 6605b385b3fSJerome Forissier * be of equal size. 661b0104773SPascal Brand */ 6625b385b3fSJerome Forissier if (key_info1.keySize != key_info2.keySize) { 663a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 664a57c1e2eSCedric Chaumont goto out; 665b0104773SPascal Brand 666a57c1e2eSCedric Chaumont } 667a57c1e2eSCedric Chaumont 668a57c1e2eSCedric Chaumont if (operation->info.maxKeySize < key_info1.keySize) { 669a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 670a57c1e2eSCedric Chaumont goto out; 671a57c1e2eSCedric Chaumont } 672b0104773SPascal Brand 673b0104773SPascal Brand /* 674b0104773SPascal Brand * Odd that only the size of one key should be reported while 675b0104773SPascal Brand * size of two key are used when allocating the operation. 676b0104773SPascal Brand */ 6777583c59eSCedric Chaumont key_size = key_info1.keySize; 678b0104773SPascal Brand 679b0104773SPascal Brand TEE_ResetTransientObject(operation->key1); 680b0104773SPascal Brand TEE_ResetTransientObject(operation->key2); 681b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 682b0104773SPascal Brand 6837583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key1, key1); 6847583c59eSCedric Chaumont if (res != TEE_SUCCESS) 685a57c1e2eSCedric Chaumont goto out; 6867583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key2, key2); 6877583c59eSCedric Chaumont if (res != TEE_SUCCESS) { 6887583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) 6897583c59eSCedric Chaumont res = TEE_ERROR_CORRUPT_OBJECT_2; 690a57c1e2eSCedric Chaumont goto out; 6917583c59eSCedric Chaumont } 6927583c59eSCedric Chaumont 693b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 694b0104773SPascal Brand 695b0104773SPascal Brand operation->info.keySize = key_size; 696b0104773SPascal Brand 6977583c59eSCedric Chaumont out: 698a57c1e2eSCedric Chaumont if (res != TEE_SUCCESS && 699a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 700a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT_2 && 701a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE && 702a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE_2) 703b36311adSJerome Forissier TEE_Panic(res); 704a57c1e2eSCedric Chaumont 705a57c1e2eSCedric Chaumont return res; 706b0104773SPascal Brand } 707b0104773SPascal Brand 708b0104773SPascal Brand void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op) 709b0104773SPascal Brand { 710b0104773SPascal Brand TEE_Result res; 711b0104773SPascal Brand 712b0104773SPascal Brand if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL) 713b0104773SPascal Brand TEE_Panic(0); 714b0104773SPascal Brand if (dst_op->info.algorithm != src_op->info.algorithm) 715b0104773SPascal Brand TEE_Panic(0); 716b0104773SPascal Brand if (src_op->info.operationClass != TEE_OPERATION_DIGEST) { 717b0104773SPascal Brand TEE_ObjectHandle key1 = TEE_HANDLE_NULL; 718b0104773SPascal Brand TEE_ObjectHandle key2 = TEE_HANDLE_NULL; 719b0104773SPascal Brand 720b0104773SPascal Brand if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) { 721b0104773SPascal Brand key1 = src_op->key1; 722b0104773SPascal Brand key2 = src_op->key2; 723b0104773SPascal Brand } 724b0104773SPascal Brand 725b0104773SPascal Brand if ((src_op->info.handleState & 726b0104773SPascal Brand TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) { 727b0104773SPascal Brand TEE_SetOperationKey(dst_op, key1); 728b0104773SPascal Brand } else { 729b0104773SPascal Brand TEE_SetOperationKey2(dst_op, key1, key2); 730b0104773SPascal Brand } 731b0104773SPascal Brand } 732b0104773SPascal Brand dst_op->info.handleState = src_op->info.handleState; 733b0104773SPascal Brand dst_op->info.keySize = src_op->info.keySize; 734642a1607SCedric Chaumont dst_op->operationState = src_op->operationState; 735b0104773SPascal Brand 736b0104773SPascal Brand if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks || 737b0104773SPascal Brand dst_op->block_size != src_op->block_size) 738b0104773SPascal Brand TEE_Panic(0); 739b0104773SPascal Brand 740b0104773SPascal Brand if (dst_op->buffer != NULL) { 741b0104773SPascal Brand if (src_op->buffer == NULL) 742b0104773SPascal Brand TEE_Panic(0); 743b0104773SPascal Brand 744b0104773SPascal Brand memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs); 745b0104773SPascal Brand dst_op->buffer_offs = src_op->buffer_offs; 746b0104773SPascal Brand } else if (src_op->buffer != NULL) { 747b0104773SPascal Brand TEE_Panic(0); 748b0104773SPascal Brand } 749b0104773SPascal Brand 7502c028fdeSJerome Forissier res = _utee_cryp_state_copy(dst_op->state, src_op->state); 751b0104773SPascal Brand if (res != TEE_SUCCESS) 752b0104773SPascal Brand TEE_Panic(res); 753b0104773SPascal Brand } 754b0104773SPascal Brand 755b0104773SPascal Brand /* Cryptographic Operations API - Message Digest Functions */ 756b0104773SPascal Brand 7578f07fe6fSJerome Forissier static void init_hash_operation(TEE_OperationHandle operation, const void *IV, 7586d15db08SJerome Forissier uint32_t IVLen) 7596d15db08SJerome Forissier { 7606d15db08SJerome Forissier TEE_Result res; 7616d15db08SJerome Forissier 7626d15db08SJerome Forissier /* 7636d15db08SJerome Forissier * Note : IV and IVLen are never used in current implementation 7646d15db08SJerome Forissier * This is why coherent values of IV and IVLen are not checked 7656d15db08SJerome Forissier */ 7662c028fdeSJerome Forissier res = _utee_hash_init(operation->state, IV, IVLen); 7676d15db08SJerome Forissier if (res != TEE_SUCCESS) 7686d15db08SJerome Forissier TEE_Panic(res); 7696d15db08SJerome Forissier operation->buffer_offs = 0; 7706d15db08SJerome Forissier operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 7716d15db08SJerome Forissier } 7726d15db08SJerome Forissier 773b0104773SPascal Brand void TEE_DigestUpdate(TEE_OperationHandle operation, 7748f07fe6fSJerome Forissier const void *chunk, uint32_t chunkSize) 775b0104773SPascal Brand { 77673d6c3baSJoakim Bech TEE_Result res = TEE_ERROR_GENERIC; 777b0104773SPascal Brand 77873d6c3baSJoakim Bech if (operation == TEE_HANDLE_NULL || 77973d6c3baSJoakim Bech operation->info.operationClass != TEE_OPERATION_DIGEST) 780b0104773SPascal Brand TEE_Panic(0); 78173d6c3baSJoakim Bech 782642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 783642a1607SCedric Chaumont 7842c028fdeSJerome Forissier res = _utee_hash_update(operation->state, chunk, chunkSize); 785b0104773SPascal Brand if (res != TEE_SUCCESS) 786b0104773SPascal Brand TEE_Panic(res); 787b0104773SPascal Brand } 788b0104773SPascal Brand 7898f07fe6fSJerome Forissier TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void *chunk, 79079a3c601SCedric Chaumont uint32_t chunkLen, void *hash, uint32_t *hashLen) 791b0104773SPascal Brand { 79287c2f6b6SCedric Chaumont TEE_Result res; 793e86f1266SJens Wiklander uint64_t hl; 79487c2f6b6SCedric Chaumont 79587c2f6b6SCedric Chaumont if ((operation == TEE_HANDLE_NULL) || 79687c2f6b6SCedric Chaumont (!chunk && chunkLen) || 79787c2f6b6SCedric Chaumont (operation->info.operationClass != TEE_OPERATION_DIGEST)) { 79887c2f6b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 79987c2f6b6SCedric Chaumont goto out; 80087c2f6b6SCedric Chaumont } 8016915bbbbSJens Wiklander __utee_check_inout_annotation(hashLen, sizeof(*hashLen)); 80287c2f6b6SCedric Chaumont 803e86f1266SJens Wiklander hl = *hashLen; 8042c028fdeSJerome Forissier res = _utee_hash_final(operation->state, chunk, chunkLen, hash, &hl); 805e86f1266SJens Wiklander *hashLen = hl; 8066d15db08SJerome Forissier if (res != TEE_SUCCESS) 8076d15db08SJerome Forissier goto out; 8086d15db08SJerome Forissier 8096d15db08SJerome Forissier /* Reset operation state */ 8106d15db08SJerome Forissier init_hash_operation(operation, NULL, 0); 81187c2f6b6SCedric Chaumont 812642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 813642a1607SCedric Chaumont 81487c2f6b6SCedric Chaumont out: 81587c2f6b6SCedric Chaumont if (res != TEE_SUCCESS && 81687c2f6b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 817b36311adSJerome Forissier TEE_Panic(res); 81873d6c3baSJoakim Bech 81987c2f6b6SCedric Chaumont return res; 820b0104773SPascal Brand } 821b0104773SPascal Brand 822b0104773SPascal Brand /* Cryptographic Operations API - Symmetric Cipher Functions */ 823b0104773SPascal Brand 8248f07fe6fSJerome Forissier void TEE_CipherInit(TEE_OperationHandle operation, const void *IV, 8258f07fe6fSJerome Forissier uint32_t IVLen) 826b0104773SPascal Brand { 827b0104773SPascal Brand TEE_Result res; 828b0104773SPascal Brand 829b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 830b0104773SPascal Brand TEE_Panic(0); 831642a1607SCedric Chaumont 832b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_CIPHER) 833b0104773SPascal Brand TEE_Panic(0); 834642a1607SCedric Chaumont 835642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 836642a1607SCedric Chaumont !(operation->key1)) 837642a1607SCedric Chaumont TEE_Panic(0); 838642a1607SCedric Chaumont 839642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 840642a1607SCedric Chaumont TEE_ResetOperation(operation); 841642a1607SCedric Chaumont 842642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 843642a1607SCedric Chaumont 8442c028fdeSJerome Forissier res = _utee_cipher_init(operation->state, IV, IVLen); 845b0104773SPascal Brand if (res != TEE_SUCCESS) 846b0104773SPascal Brand TEE_Panic(res); 847642a1607SCedric Chaumont 848b0104773SPascal Brand operation->buffer_offs = 0; 849b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 850b0104773SPascal Brand } 851b0104773SPascal Brand 852b0104773SPascal Brand static TEE_Result tee_buffer_update( 853b0104773SPascal Brand TEE_OperationHandle op, 854e86f1266SJens Wiklander TEE_Result(*update_func)(unsigned long state, const void *src, 855e86f1266SJens Wiklander size_t slen, void *dst, uint64_t *dlen), 856b0104773SPascal Brand const void *src_data, size_t src_len, 857e86f1266SJens Wiklander void *dest_data, uint64_t *dest_len) 858b0104773SPascal Brand { 859b0104773SPascal Brand TEE_Result res; 860b0104773SPascal Brand const uint8_t *src = src_data; 861b0104773SPascal Brand size_t slen = src_len; 862b0104773SPascal Brand uint8_t *dst = dest_data; 863b0104773SPascal Brand size_t dlen = *dest_len; 864b0104773SPascal Brand size_t acc_dlen = 0; 865e86f1266SJens Wiklander uint64_t tmp_dlen; 866b0104773SPascal Brand size_t l; 867b0104773SPascal Brand size_t buffer_size; 868d3588802SPascal Brand size_t buffer_left; 869b0104773SPascal Brand 870090268f5SJens Wiklander if (!src) { 871090268f5SJens Wiklander if (slen) 872090268f5SJens Wiklander TEE_Panic(0); 873090268f5SJens Wiklander goto out; 874090268f5SJens Wiklander } 875090268f5SJens Wiklander 876d3588802SPascal Brand if (op->buffer_two_blocks) { 877b0104773SPascal Brand buffer_size = op->block_size * 2; 878d3588802SPascal Brand buffer_left = 1; 879d3588802SPascal Brand } else { 880b0104773SPascal Brand buffer_size = op->block_size; 881d3588802SPascal Brand buffer_left = 0; 882d3588802SPascal Brand } 883b0104773SPascal Brand 884b0104773SPascal Brand if (op->buffer_offs > 0) { 885b0104773SPascal Brand /* Fill up complete block */ 886b0104773SPascal Brand if (op->buffer_offs < op->block_size) 887b0104773SPascal Brand l = MIN(slen, op->block_size - op->buffer_offs); 888b0104773SPascal Brand else 889b0104773SPascal Brand l = MIN(slen, buffer_size - op->buffer_offs); 890b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, l); 891b0104773SPascal Brand op->buffer_offs += l; 892b0104773SPascal Brand src += l; 893b0104773SPascal Brand slen -= l; 894b0104773SPascal Brand if ((op->buffer_offs % op->block_size) != 0) 895b0104773SPascal Brand goto out; /* Nothing left to do */ 896b0104773SPascal Brand } 897b0104773SPascal Brand 898b0104773SPascal Brand /* If we can feed from buffer */ 899d3588802SPascal Brand if ((op->buffer_offs > 0) && 900d3588802SPascal Brand ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) { 9012ff3fdbbSPascal Brand l = ROUNDUP(op->buffer_offs + slen - buffer_size, 902b0104773SPascal Brand op->block_size); 903b0104773SPascal Brand l = MIN(op->buffer_offs, l); 904b0104773SPascal Brand tmp_dlen = dlen; 905b0104773SPascal Brand res = update_func(op->state, op->buffer, l, dst, &tmp_dlen); 906b0104773SPascal Brand if (res != TEE_SUCCESS) 907b0104773SPascal Brand TEE_Panic(res); 908b0104773SPascal Brand dst += tmp_dlen; 909b0104773SPascal Brand dlen -= tmp_dlen; 910b0104773SPascal Brand acc_dlen += tmp_dlen; 911b0104773SPascal Brand op->buffer_offs -= l; 912b0104773SPascal Brand if (op->buffer_offs > 0) { 913b0104773SPascal Brand /* 914b0104773SPascal Brand * Slen is small enough to be contained in rest buffer. 915b0104773SPascal Brand */ 916b0104773SPascal Brand memcpy(op->buffer, op->buffer + l, buffer_size - l); 917b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 918b0104773SPascal Brand op->buffer_offs += slen; 919b0104773SPascal Brand goto out; /* Nothing left to do */ 920b0104773SPascal Brand } 921b0104773SPascal Brand } 922b0104773SPascal Brand 923d3588802SPascal Brand if (slen >= (buffer_size + buffer_left)) { 924b0104773SPascal Brand /* Buffer is empty, feed as much as possible from src */ 925bf7a587fSJerome Forissier if (op->info.algorithm == TEE_ALG_AES_CTS) 926b1ecda78SJerome Forissier l = ROUNDUP(slen - buffer_size, op->block_size); 927bf7a587fSJerome Forissier else 928bf7a587fSJerome Forissier l = ROUNDUP(slen - buffer_size + 1, op->block_size); 929b0104773SPascal Brand 930b0104773SPascal Brand tmp_dlen = dlen; 931b0104773SPascal Brand res = update_func(op->state, src, l, dst, &tmp_dlen); 932b0104773SPascal Brand if (res != TEE_SUCCESS) 933b0104773SPascal Brand TEE_Panic(res); 934b0104773SPascal Brand src += l; 935b0104773SPascal Brand slen -= l; 936b0104773SPascal Brand dst += tmp_dlen; 937b0104773SPascal Brand dlen -= tmp_dlen; 938b0104773SPascal Brand acc_dlen += tmp_dlen; 939b0104773SPascal Brand } 940b0104773SPascal Brand 941b0104773SPascal Brand /* Slen is small enough to be contained in buffer. */ 942b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 943b0104773SPascal Brand op->buffer_offs += slen; 944b0104773SPascal Brand 945b0104773SPascal Brand out: 946b0104773SPascal Brand *dest_len = acc_dlen; 947b0104773SPascal Brand return TEE_SUCCESS; 948b0104773SPascal Brand } 949b0104773SPascal Brand 9508f07fe6fSJerome Forissier TEE_Result TEE_CipherUpdate(TEE_OperationHandle operation, const void *srcData, 95179a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 952b0104773SPascal Brand { 953dea1f2b6SCedric Chaumont TEE_Result res; 954b0104773SPascal Brand size_t req_dlen; 955e86f1266SJens Wiklander uint64_t dl; 956b0104773SPascal Brand 9576915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 958dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 959dea1f2b6SCedric Chaumont goto out; 960dea1f2b6SCedric Chaumont } 9616915bbbbSJens Wiklander __utee_check_inout_annotation(destLen, sizeof(*destLen)); 962dea1f2b6SCedric Chaumont 963642a1607SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 964dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 965dea1f2b6SCedric Chaumont goto out; 966dea1f2b6SCedric Chaumont } 967dea1f2b6SCedric Chaumont 968642a1607SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 969642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 970642a1607SCedric Chaumont goto out; 971642a1607SCedric Chaumont } 972642a1607SCedric Chaumont 973642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 974dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 975dea1f2b6SCedric Chaumont goto out; 976dea1f2b6SCedric Chaumont } 977b0104773SPascal Brand 978e32c5ddfSJerome Forissier if (!srcData && !srcLen) { 979090268f5SJens Wiklander *destLen = 0; 980e32c5ddfSJerome Forissier res = TEE_SUCCESS; 981e32c5ddfSJerome Forissier goto out; 982e32c5ddfSJerome Forissier } 983e32c5ddfSJerome Forissier 984b0104773SPascal Brand /* Calculate required dlen */ 98557aabac5SBogdan Liulko if (operation->block_size > 1) { 98657aabac5SBogdan Liulko req_dlen = ((operation->buffer_offs + srcLen) / 98757aabac5SBogdan Liulko operation->block_size) * operation->block_size; 98857aabac5SBogdan Liulko } else { 98957aabac5SBogdan Liulko req_dlen = srcLen; 99057aabac5SBogdan Liulko } 991642a1607SCedric Chaumont if (operation->buffer_two_blocks) { 992642a1607SCedric Chaumont if (req_dlen > operation->block_size * 2) 993642a1607SCedric Chaumont req_dlen -= operation->block_size * 2; 994b0104773SPascal Brand else 995b0104773SPascal Brand req_dlen = 0; 996b0104773SPascal Brand } 997b0104773SPascal Brand /* 998b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 999b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1000b0104773SPascal Brand * can't restore sync with this API. 1001b0104773SPascal Brand */ 1002b0104773SPascal Brand if (*destLen < req_dlen) { 1003b0104773SPascal Brand *destLen = req_dlen; 1004dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1005dea1f2b6SCedric Chaumont goto out; 1006b0104773SPascal Brand } 1007b0104773SPascal Brand 1008e86f1266SJens Wiklander dl = *destLen; 100957aabac5SBogdan Liulko if (operation->block_size > 1) { 10102c028fdeSJerome Forissier res = tee_buffer_update(operation, _utee_cipher_update, srcData, 101157aabac5SBogdan Liulko srcLen, destData, &dl); 101257aabac5SBogdan Liulko } else { 101357aabac5SBogdan Liulko if (srcLen > 0) { 10142c028fdeSJerome Forissier res = _utee_cipher_update(operation->state, srcData, 101557aabac5SBogdan Liulko srcLen, destData, &dl); 101657aabac5SBogdan Liulko } else { 101757aabac5SBogdan Liulko res = TEE_SUCCESS; 101857aabac5SBogdan Liulko dl = 0; 101957aabac5SBogdan Liulko } 102057aabac5SBogdan Liulko } 1021e86f1266SJens Wiklander *destLen = dl; 1022b0104773SPascal Brand 1023dea1f2b6SCedric Chaumont out: 1024dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 1025dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1026b36311adSJerome Forissier TEE_Panic(res); 1027dea1f2b6SCedric Chaumont 1028dea1f2b6SCedric Chaumont return res; 1029b0104773SPascal Brand } 1030b0104773SPascal Brand 1031642a1607SCedric Chaumont TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation, 10328f07fe6fSJerome Forissier const void *srcData, uint32_t srcLen, 10338f07fe6fSJerome Forissier void *destData, uint32_t *destLen) 1034b0104773SPascal Brand { 10356915bbbbSJens Wiklander TEE_Result res = TEE_SUCCESS; 1036b0104773SPascal Brand uint8_t *dst = destData; 1037b0104773SPascal Brand size_t acc_dlen = 0; 10386915bbbbSJens Wiklander uint64_t tmp_dlen = 0; 10396915bbbbSJens Wiklander size_t req_dlen = 0; 1040b0104773SPascal Brand 10416915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 1042dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1043dea1f2b6SCedric Chaumont goto out; 1044dea1f2b6SCedric Chaumont } 10456915bbbbSJens Wiklander if (destLen) 10466915bbbbSJens Wiklander __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1047dea1f2b6SCedric Chaumont 1048642a1607SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 1049dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1050dea1f2b6SCedric Chaumont goto out; 1051dea1f2b6SCedric Chaumont } 1052dea1f2b6SCedric Chaumont 1053642a1607SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1054642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1055642a1607SCedric Chaumont goto out; 1056642a1607SCedric Chaumont } 1057642a1607SCedric Chaumont 1058642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1059dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1060dea1f2b6SCedric Chaumont goto out; 1061dea1f2b6SCedric Chaumont } 1062b0104773SPascal Brand 1063b0104773SPascal Brand /* 1064b0104773SPascal Brand * Check that the final block doesn't require padding for those 1065b0104773SPascal Brand * algorithms that requires client to supply padding. 1066b0104773SPascal Brand */ 1067642a1607SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD || 1068642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD || 1069642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD || 1070642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD || 1071642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || 1072ade6f848SJerome Forissier operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD || 1073ade6f848SJerome Forissier operation->info.algorithm == TEE_ALG_SM4_ECB_NOPAD || 1074ade6f848SJerome Forissier operation->info.algorithm == TEE_ALG_SM4_CBC_NOPAD) { 1075642a1607SCedric Chaumont if (((operation->buffer_offs + srcLen) % operation->block_size) 1076642a1607SCedric Chaumont != 0) { 1077dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1078dea1f2b6SCedric Chaumont goto out; 1079dea1f2b6SCedric Chaumont } 1080b0104773SPascal Brand } 1081b0104773SPascal Brand 1082b0104773SPascal Brand /* 1083b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1084b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1085b0104773SPascal Brand * can't restore sync with this API. 1086b0104773SPascal Brand */ 108757aabac5SBogdan Liulko if (operation->block_size > 1) { 1088642a1607SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 108957aabac5SBogdan Liulko } else { 109057aabac5SBogdan Liulko req_dlen = srcLen; 109157aabac5SBogdan Liulko } 10926915bbbbSJens Wiklander if (destLen) 10936915bbbbSJens Wiklander tmp_dlen = *destLen; 10946915bbbbSJens Wiklander if (tmp_dlen < req_dlen) { 10956915bbbbSJens Wiklander if (destLen) 1096b0104773SPascal Brand *destLen = req_dlen; 1097dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1098dea1f2b6SCedric Chaumont goto out; 1099b0104773SPascal Brand } 1100b0104773SPascal Brand 110157aabac5SBogdan Liulko if (operation->block_size > 1) { 11022c028fdeSJerome Forissier res = tee_buffer_update(operation, _utee_cipher_update, 110357aabac5SBogdan Liulko srcData, srcLen, dst, &tmp_dlen); 1104dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS) 1105dea1f2b6SCedric Chaumont goto out; 1106dea1f2b6SCedric Chaumont 1107b0104773SPascal Brand dst += tmp_dlen; 1108b0104773SPascal Brand acc_dlen += tmp_dlen; 1109b0104773SPascal Brand 1110b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 11112c028fdeSJerome Forissier res = _utee_cipher_final(operation->state, operation->buffer, 11122c028fdeSJerome Forissier operation->buffer_offs, dst, 11132c028fdeSJerome Forissier &tmp_dlen); 111457aabac5SBogdan Liulko } else { 11152c028fdeSJerome Forissier res = _utee_cipher_final(operation->state, srcData, srcLen, dst, 11162c028fdeSJerome Forissier &tmp_dlen); 111757aabac5SBogdan Liulko } 1118b0104773SPascal Brand if (res != TEE_SUCCESS) 1119dea1f2b6SCedric Chaumont goto out; 1120dea1f2b6SCedric Chaumont 1121b0104773SPascal Brand acc_dlen += tmp_dlen; 11226915bbbbSJens Wiklander if (destLen) 1123b0104773SPascal Brand *destLen = acc_dlen; 1124dea1f2b6SCedric Chaumont 1125642a1607SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1126642a1607SCedric Chaumont 1127642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1128642a1607SCedric Chaumont 1129dea1f2b6SCedric Chaumont out: 1130dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 1131dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1132b36311adSJerome Forissier TEE_Panic(res); 1133dea1f2b6SCedric Chaumont 1134dea1f2b6SCedric Chaumont return res; 1135b0104773SPascal Brand } 1136b0104773SPascal Brand 1137b0104773SPascal Brand /* Cryptographic Operations API - MAC Functions */ 1138b0104773SPascal Brand 11398f07fe6fSJerome Forissier void TEE_MACInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen) 1140b0104773SPascal Brand { 1141b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 1142b0104773SPascal Brand TEE_Panic(0); 1143642a1607SCedric Chaumont 1144b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_MAC) 1145b0104773SPascal Brand TEE_Panic(0); 1146642a1607SCedric Chaumont 1147642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 1148642a1607SCedric Chaumont !(operation->key1)) 1149642a1607SCedric Chaumont TEE_Panic(0); 1150642a1607SCedric Chaumont 1151642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 1152642a1607SCedric Chaumont TEE_ResetOperation(operation); 1153642a1607SCedric Chaumont 1154642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1155642a1607SCedric Chaumont 11566d15db08SJerome Forissier init_hash_operation(operation, IV, IVLen); 1157b0104773SPascal Brand } 1158b0104773SPascal Brand 11598f07fe6fSJerome Forissier void TEE_MACUpdate(TEE_OperationHandle operation, const void *chunk, 116028e0efc6SCedric Chaumont uint32_t chunkSize) 1161b0104773SPascal Brand { 1162b0104773SPascal Brand TEE_Result res; 1163b0104773SPascal Brand 116428e0efc6SCedric Chaumont if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0)) 1165b0104773SPascal Brand TEE_Panic(0); 1166642a1607SCedric Chaumont 116728e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) 1168b0104773SPascal Brand TEE_Panic(0); 1169642a1607SCedric Chaumont 117028e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1171b0104773SPascal Brand TEE_Panic(0); 1172b0104773SPascal Brand 1173642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) 1174642a1607SCedric Chaumont TEE_Panic(0); 1175642a1607SCedric Chaumont 11762c028fdeSJerome Forissier res = _utee_hash_update(operation->state, chunk, chunkSize); 1177b0104773SPascal Brand if (res != TEE_SUCCESS) 1178b0104773SPascal Brand TEE_Panic(res); 1179b0104773SPascal Brand } 1180b0104773SPascal Brand 118128e0efc6SCedric Chaumont TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation, 11828f07fe6fSJerome Forissier const void *message, uint32_t messageLen, 118379a3c601SCedric Chaumont void *mac, uint32_t *macLen) 1184b0104773SPascal Brand { 1185b0104773SPascal Brand TEE_Result res; 1186e86f1266SJens Wiklander uint64_t ml; 1187b0104773SPascal Brand 11886915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!message && messageLen)) { 118928e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 119028e0efc6SCedric Chaumont goto out; 119128e0efc6SCedric Chaumont } 11926915bbbbSJens Wiklander __utee_check_inout_annotation(macLen, sizeof(*macLen)); 1193b0104773SPascal Brand 119428e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 119528e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 119628e0efc6SCedric Chaumont goto out; 119728e0efc6SCedric Chaumont } 119828e0efc6SCedric Chaumont 119928e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 120028e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 120128e0efc6SCedric Chaumont goto out; 120228e0efc6SCedric Chaumont } 120328e0efc6SCedric Chaumont 1204642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1205642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1206642a1607SCedric Chaumont goto out; 1207642a1607SCedric Chaumont } 1208642a1607SCedric Chaumont 1209e86f1266SJens Wiklander ml = *macLen; 12102c028fdeSJerome Forissier res = _utee_hash_final(operation->state, message, messageLen, mac, &ml); 1211e86f1266SJens Wiklander *macLen = ml; 121228e0efc6SCedric Chaumont if (res != TEE_SUCCESS) 121328e0efc6SCedric Chaumont goto out; 121428e0efc6SCedric Chaumont 121528e0efc6SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 121628e0efc6SCedric Chaumont 1217642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1218642a1607SCedric Chaumont 121928e0efc6SCedric Chaumont out: 122028e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 122128e0efc6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 122228e0efc6SCedric Chaumont TEE_Panic(res); 122328e0efc6SCedric Chaumont 1224b0104773SPascal Brand return res; 1225b0104773SPascal Brand } 1226b0104773SPascal Brand 1227b0104773SPascal Brand TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation, 12288f07fe6fSJerome Forissier const void *message, uint32_t messageLen, 12298f07fe6fSJerome Forissier const void *mac, uint32_t macLen) 1230b0104773SPascal Brand { 1231b0104773SPascal Brand TEE_Result res; 1232b0104773SPascal Brand uint8_t computed_mac[TEE_MAX_HASH_SIZE]; 12337f74c64aSPascal Brand uint32_t computed_mac_size = TEE_MAX_HASH_SIZE; 1234b0104773SPascal Brand 123528e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 123628e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 123728e0efc6SCedric Chaumont goto out; 123828e0efc6SCedric Chaumont } 123928e0efc6SCedric Chaumont 124028e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 124128e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 124228e0efc6SCedric Chaumont goto out; 124328e0efc6SCedric Chaumont } 124428e0efc6SCedric Chaumont 1245642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1246642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1247642a1607SCedric Chaumont goto out; 1248642a1607SCedric Chaumont } 1249642a1607SCedric Chaumont 1250b0104773SPascal Brand res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac, 1251b0104773SPascal Brand &computed_mac_size); 1252b0104773SPascal Brand if (res != TEE_SUCCESS) 125328e0efc6SCedric Chaumont goto out; 125428e0efc6SCedric Chaumont 125528e0efc6SCedric Chaumont if (computed_mac_size != macLen) { 125628e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 125728e0efc6SCedric Chaumont goto out; 125828e0efc6SCedric Chaumont } 125928e0efc6SCedric Chaumont 126048e10604SJerome Forissier if (consttime_memcmp(mac, computed_mac, computed_mac_size) != 0) { 126128e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 126228e0efc6SCedric Chaumont goto out; 126328e0efc6SCedric Chaumont } 126428e0efc6SCedric Chaumont 1265642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1266642a1607SCedric Chaumont 126728e0efc6SCedric Chaumont out: 126828e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 126928e0efc6SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 127028e0efc6SCedric Chaumont TEE_Panic(res); 127128e0efc6SCedric Chaumont 1272b0104773SPascal Brand return res; 1273b0104773SPascal Brand } 1274b0104773SPascal Brand 1275b0104773SPascal Brand /* Cryptographic Operations API - Authenticated Encryption Functions */ 1276b0104773SPascal Brand 12778f07fe6fSJerome Forissier TEE_Result TEE_AEInit(TEE_OperationHandle operation, const void *nonce, 127879a3c601SCedric Chaumont uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen, 1279b0104773SPascal Brand uint32_t payloadLen) 1280b0104773SPascal Brand { 1281b0104773SPascal Brand TEE_Result res; 1282b0104773SPascal Brand 1283b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || nonce == NULL) { 1284b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1285b5816c88SCedric Chaumont goto out; 1286b5816c88SCedric Chaumont } 1287b5816c88SCedric Chaumont 1288b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1289b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1290b5816c88SCedric Chaumont goto out; 1291b5816c88SCedric Chaumont } 1292b0104773SPascal Brand 1293642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 1294642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1295642a1607SCedric Chaumont goto out; 1296642a1607SCedric Chaumont } 1297642a1607SCedric Chaumont 1298b0104773SPascal Brand /* 1299b0104773SPascal Brand * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core 1300b0104773SPascal Brand * in the implementation. But AES-GCM spec doesn't specify the tag len 1301b0104773SPascal Brand * according to the same principle so we have to check here instead to 1302b0104773SPascal Brand * be GP compliant. 1303b0104773SPascal Brand */ 1304b5816c88SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_GCM) { 1305b0104773SPascal Brand /* 1306b0104773SPascal Brand * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96 1307b0104773SPascal Brand */ 1308b5816c88SCedric Chaumont if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) { 1309b5816c88SCedric Chaumont res = TEE_ERROR_NOT_SUPPORTED; 1310b5816c88SCedric Chaumont goto out; 1311b5816c88SCedric Chaumont } 1312b0104773SPascal Brand } 1313b0104773SPascal Brand 13142c028fdeSJerome Forissier res = _utee_authenc_init(operation->state, nonce, nonceLen, tagLen / 8, 13152c028fdeSJerome Forissier AADLen, payloadLen); 1316b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1317b5816c88SCedric Chaumont goto out; 1318b5816c88SCedric Chaumont 13197acaf5adSAlbert Schwarzkopf operation->info.digestLength = tagLen / 8; 1320f2674567SSumit Garg operation->buffer_offs = 0; 1321b5816c88SCedric Chaumont operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 1322b5816c88SCedric Chaumont 1323b5816c88SCedric Chaumont out: 1324b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1325b5816c88SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 1326b0104773SPascal Brand TEE_Panic(res); 1327b5816c88SCedric Chaumont 1328b0104773SPascal Brand return res; 1329b0104773SPascal Brand } 1330b0104773SPascal Brand 13318f07fe6fSJerome Forissier void TEE_AEUpdateAAD(TEE_OperationHandle operation, const void *AADdata, 133279a3c601SCedric Chaumont uint32_t AADdataLen) 1333b0104773SPascal Brand { 1334b0104773SPascal Brand TEE_Result res; 1335b0104773SPascal Brand 1336b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1337b5816c88SCedric Chaumont (AADdata == NULL && AADdataLen != 0)) 1338b0104773SPascal Brand TEE_Panic(0); 1339642a1607SCedric Chaumont 1340b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) 1341b0104773SPascal Brand TEE_Panic(0); 1342642a1607SCedric Chaumont 1343b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1344b0104773SPascal Brand TEE_Panic(0); 1345b0104773SPascal Brand 13462c028fdeSJerome Forissier res = _utee_authenc_update_aad(operation->state, AADdata, AADdataLen); 1347642a1607SCedric Chaumont 1348642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1349642a1607SCedric Chaumont 1350b0104773SPascal Brand if (res != TEE_SUCCESS) 1351b0104773SPascal Brand TEE_Panic(res); 1352b0104773SPascal Brand } 1353b0104773SPascal Brand 13548f07fe6fSJerome Forissier TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, const void *srcData, 135579a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 1356b0104773SPascal Brand { 13576915bbbbSJens Wiklander TEE_Result res = TEE_SUCCESS; 13586915bbbbSJens Wiklander size_t req_dlen = 0; 13596915bbbbSJens Wiklander uint64_t dl = 0; 1360b0104773SPascal Brand 13616915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 1362b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1363b5816c88SCedric Chaumont goto out; 1364b5816c88SCedric Chaumont } 13656915bbbbSJens Wiklander __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1366b5816c88SCedric Chaumont 1367b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1368b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1369b5816c88SCedric Chaumont goto out; 1370b5816c88SCedric Chaumont } 1371b5816c88SCedric Chaumont 1372b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1373b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1374b5816c88SCedric Chaumont goto out; 1375b5816c88SCedric Chaumont } 1376b0104773SPascal Brand 1377827308b8SJerome Forissier if (!srcData && !srcLen) { 1378090268f5SJens Wiklander *destLen = 0; 1379827308b8SJerome Forissier res = TEE_SUCCESS; 1380827308b8SJerome Forissier goto out; 1381827308b8SJerome Forissier } 1382827308b8SJerome Forissier 1383b0104773SPascal Brand /* 1384b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1385b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1386b0104773SPascal Brand * can't restore sync with this API. 1387b0104773SPascal Brand */ 1388afc0c182SBogdan Liulko if (operation->block_size > 1) { 1389b5816c88SCedric Chaumont req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen, 1390b5816c88SCedric Chaumont operation->block_size); 1391afc0c182SBogdan Liulko } else { 1392afc0c182SBogdan Liulko req_dlen = srcLen; 1393afc0c182SBogdan Liulko } 1394afc0c182SBogdan Liulko 13956915bbbbSJens Wiklander dl = *destLen; 13966915bbbbSJens Wiklander if (dl < req_dlen) { 1397b0104773SPascal Brand *destLen = req_dlen; 1398b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1399b5816c88SCedric Chaumont goto out; 1400b0104773SPascal Brand } 1401b0104773SPascal Brand 1402afc0c182SBogdan Liulko if (operation->block_size > 1) { 14032c028fdeSJerome Forissier res = tee_buffer_update(operation, _utee_authenc_update_payload, 1404afc0c182SBogdan Liulko srcData, srcLen, destData, &dl); 1405afc0c182SBogdan Liulko } else { 1406afc0c182SBogdan Liulko if (srcLen > 0) { 14072c028fdeSJerome Forissier res = _utee_authenc_update_payload(operation->state, 1408afc0c182SBogdan Liulko srcData, srcLen, 1409afc0c182SBogdan Liulko destData, &dl); 1410afc0c182SBogdan Liulko } else { 1411afc0c182SBogdan Liulko dl = 0; 1412afc0c182SBogdan Liulko res = TEE_SUCCESS; 1413afc0c182SBogdan Liulko } 1414afc0c182SBogdan Liulko } 1415afc0c182SBogdan Liulko if (res != TEE_SUCCESS) 1416afc0c182SBogdan Liulko goto out; 1417afc0c182SBogdan Liulko 1418e86f1266SJens Wiklander *destLen = dl; 1419b0104773SPascal Brand 1420642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1421642a1607SCedric Chaumont 1422b5816c88SCedric Chaumont out: 1423b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1424b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1425b5816c88SCedric Chaumont TEE_Panic(res); 1426b5816c88SCedric Chaumont 1427b5816c88SCedric Chaumont return res; 1428b0104773SPascal Brand } 1429b0104773SPascal Brand 1430b5816c88SCedric Chaumont TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation, 14318f07fe6fSJerome Forissier const void *srcData, uint32_t srcLen, 143279a3c601SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 143379a3c601SCedric Chaumont uint32_t *tagLen) 1434b0104773SPascal Brand { 1435b0104773SPascal Brand TEE_Result res; 1436b0104773SPascal Brand uint8_t *dst = destData; 1437b0104773SPascal Brand size_t acc_dlen = 0; 1438e86f1266SJens Wiklander uint64_t tmp_dlen; 1439b0104773SPascal Brand size_t req_dlen; 1440e86f1266SJens Wiklander uint64_t tl; 1441b0104773SPascal Brand 14426915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 1443b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1444b5816c88SCedric Chaumont goto out; 1445b5816c88SCedric Chaumont } 14466915bbbbSJens Wiklander __utee_check_inout_annotation(destLen, sizeof(*destLen)); 14476915bbbbSJens Wiklander __utee_check_inout_annotation(tagLen, sizeof(*tagLen)); 1448b5816c88SCedric Chaumont 1449b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1450b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1451b5816c88SCedric Chaumont goto out; 1452b5816c88SCedric Chaumont } 1453b5816c88SCedric Chaumont 1454b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1455b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1456b5816c88SCedric Chaumont goto out; 1457b5816c88SCedric Chaumont } 1458b0104773SPascal Brand 1459b0104773SPascal Brand /* 1460b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1461b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1462b0104773SPascal Brand * can't restore sync with this API. 14632733280aSEtienne Carriere * 14642733280aSEtienne Carriere * Need to check this before update_payload since sync would be lost if 14652733280aSEtienne Carriere * we return short buffer after that. 1466b0104773SPascal Brand */ 14672733280aSEtienne Carriere res = TEE_ERROR_GENERIC; 14682733280aSEtienne Carriere 1469b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1470b0104773SPascal Brand if (*destLen < req_dlen) { 1471b0104773SPascal Brand *destLen = req_dlen; 1472b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1473b0104773SPascal Brand } 1474b0104773SPascal Brand 14757acaf5adSAlbert Schwarzkopf if (*tagLen < operation->info.digestLength) { 14767acaf5adSAlbert Schwarzkopf *tagLen = operation->info.digestLength; 1477b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1478b0104773SPascal Brand } 1479b0104773SPascal Brand 14802733280aSEtienne Carriere if (res == TEE_ERROR_SHORT_BUFFER) 14812733280aSEtienne Carriere goto out; 14822733280aSEtienne Carriere 1483afc0c182SBogdan Liulko tl = *tagLen; 1484b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1485afc0c182SBogdan Liulko if (operation->block_size > 1) { 14862c028fdeSJerome Forissier res = tee_buffer_update(operation, _utee_authenc_update_payload, 1487afc0c182SBogdan Liulko srcData, srcLen, dst, &tmp_dlen); 1488b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1489b5816c88SCedric Chaumont goto out; 1490b5816c88SCedric Chaumont 1491b0104773SPascal Brand dst += tmp_dlen; 1492b0104773SPascal Brand acc_dlen += tmp_dlen; 1493b0104773SPascal Brand 1494b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 14952c028fdeSJerome Forissier res = _utee_authenc_enc_final(operation->state, 1496afc0c182SBogdan Liulko operation->buffer, 1497afc0c182SBogdan Liulko operation->buffer_offs, dst, 1498afc0c182SBogdan Liulko &tmp_dlen, tag, &tl); 1499afc0c182SBogdan Liulko } else { 15002c028fdeSJerome Forissier res = _utee_authenc_enc_final(operation->state, srcData, 1501afc0c182SBogdan Liulko srcLen, dst, &tmp_dlen, 1502e86f1266SJens Wiklander tag, &tl); 1503afc0c182SBogdan Liulko } 1504e86f1266SJens Wiklander *tagLen = tl; 1505b0104773SPascal Brand if (res != TEE_SUCCESS) 1506b5816c88SCedric Chaumont goto out; 1507b0104773SPascal Brand 1508b5816c88SCedric Chaumont acc_dlen += tmp_dlen; 1509b0104773SPascal Brand *destLen = acc_dlen; 1510642a1607SCedric Chaumont 1511b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1512b5816c88SCedric Chaumont 1513642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1514642a1607SCedric Chaumont 1515b5816c88SCedric Chaumont out: 1516b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1517b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1518b5816c88SCedric Chaumont TEE_Panic(res); 1519b0104773SPascal Brand 1520b0104773SPascal Brand return res; 1521b0104773SPascal Brand } 1522b0104773SPascal Brand 1523b5816c88SCedric Chaumont TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation, 15248f07fe6fSJerome Forissier const void *srcData, uint32_t srcLen, 1525b5816c88SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 152679a3c601SCedric Chaumont uint32_t tagLen) 1527b0104773SPascal Brand { 1528b0104773SPascal Brand TEE_Result res; 1529b0104773SPascal Brand uint8_t *dst = destData; 1530b0104773SPascal Brand size_t acc_dlen = 0; 1531e86f1266SJens Wiklander uint64_t tmp_dlen; 1532b0104773SPascal Brand size_t req_dlen; 1533b0104773SPascal Brand 15346915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 1535b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1536b5816c88SCedric Chaumont goto out; 1537b5816c88SCedric Chaumont } 15386915bbbbSJens Wiklander __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1539b5816c88SCedric Chaumont 1540b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1541b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1542b5816c88SCedric Chaumont goto out; 1543b5816c88SCedric Chaumont } 1544b5816c88SCedric Chaumont 1545b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1546b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1547b5816c88SCedric Chaumont goto out; 1548b5816c88SCedric Chaumont } 1549b0104773SPascal Brand 1550b0104773SPascal Brand /* 1551b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1552b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1553b0104773SPascal Brand * can't restore sync with this API. 1554b0104773SPascal Brand */ 1555b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1556b0104773SPascal Brand if (*destLen < req_dlen) { 1557b0104773SPascal Brand *destLen = req_dlen; 1558b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1559b5816c88SCedric Chaumont goto out; 1560b0104773SPascal Brand } 1561b0104773SPascal Brand 1562b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1563afc0c182SBogdan Liulko if (operation->block_size > 1) { 15642c028fdeSJerome Forissier res = tee_buffer_update(operation, _utee_authenc_update_payload, 1565afc0c182SBogdan Liulko srcData, srcLen, dst, &tmp_dlen); 1566b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1567b5816c88SCedric Chaumont goto out; 1568b5816c88SCedric Chaumont 1569b0104773SPascal Brand dst += tmp_dlen; 1570b0104773SPascal Brand acc_dlen += tmp_dlen; 1571b0104773SPascal Brand 1572b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 15732c028fdeSJerome Forissier res = _utee_authenc_dec_final(operation->state, 1574afc0c182SBogdan Liulko operation->buffer, 1575afc0c182SBogdan Liulko operation->buffer_offs, dst, 1576afc0c182SBogdan Liulko &tmp_dlen, tag, tagLen); 1577afc0c182SBogdan Liulko } else { 15782c028fdeSJerome Forissier res = _utee_authenc_dec_final(operation->state, srcData, 1579afc0c182SBogdan Liulko srcLen, dst, &tmp_dlen, 1580b5816c88SCedric Chaumont tag, tagLen); 1581afc0c182SBogdan Liulko } 1582b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1583b5816c88SCedric Chaumont goto out; 1584b5816c88SCedric Chaumont 1585b0104773SPascal Brand /* Supplied tagLen should match what we initiated with */ 15867acaf5adSAlbert Schwarzkopf if (tagLen != operation->info.digestLength) 1587b0104773SPascal Brand res = TEE_ERROR_MAC_INVALID; 1588b0104773SPascal Brand 1589b0104773SPascal Brand acc_dlen += tmp_dlen; 1590b0104773SPascal Brand *destLen = acc_dlen; 1591642a1607SCedric Chaumont 1592b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1593b5816c88SCedric Chaumont 1594642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1595642a1607SCedric Chaumont 1596b5816c88SCedric Chaumont out: 1597b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1598b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER && 1599b5816c88SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 1600b5816c88SCedric Chaumont TEE_Panic(res); 1601b0104773SPascal Brand 1602b0104773SPascal Brand return res; 1603b0104773SPascal Brand } 1604b0104773SPascal Brand 1605b0104773SPascal Brand /* Cryptographic Operations API - Asymmetric Functions */ 1606b0104773SPascal Brand 160712e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation, 16088f07fe6fSJerome Forissier const TEE_Attribute *params, 16098f07fe6fSJerome Forissier uint32_t paramCount, const void *srcData, 161079a3c601SCedric Chaumont uint32_t srcLen, void *destData, 161179a3c601SCedric Chaumont uint32_t *destLen) 1612b0104773SPascal Brand { 16136915bbbbSJens Wiklander TEE_Result res = TEE_SUCCESS; 1614e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 16156915bbbbSJens Wiklander uint64_t dl = 0; 1616b0104773SPascal Brand 16176915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) 1618b0104773SPascal Brand TEE_Panic(0); 16196915bbbbSJens Wiklander 16206915bbbbSJens Wiklander __utee_check_attr_in_annotation(params, paramCount); 16216915bbbbSJens Wiklander __utee_check_inout_annotation(destLen, sizeof(*destLen)); 16226915bbbbSJens Wiklander 162312e66b6fSCedric Chaumont if (!operation->key1) 1624b0104773SPascal Brand TEE_Panic(0); 162512e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 162612e66b6fSCedric Chaumont TEE_Panic(0); 162712e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_ENCRYPT) 1628b0104773SPascal Brand TEE_Panic(0); 1629b0104773SPascal Brand 1630e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1631e86f1266SJens Wiklander dl = *destLen; 16322c028fdeSJerome Forissier res = _utee_asymm_operate(operation->state, ua, paramCount, srcData, 1633e86f1266SJens Wiklander srcLen, destData, &dl); 1634e86f1266SJens Wiklander *destLen = dl; 163512e66b6fSCedric Chaumont 16368844ebfcSPascal Brand if (res != TEE_SUCCESS && 16378844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 16388844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1639b0104773SPascal Brand TEE_Panic(res); 164012e66b6fSCedric Chaumont 1641b0104773SPascal Brand return res; 1642b0104773SPascal Brand } 1643b0104773SPascal Brand 164412e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation, 16458f07fe6fSJerome Forissier const TEE_Attribute *params, 16468f07fe6fSJerome Forissier uint32_t paramCount, const void *srcData, 164779a3c601SCedric Chaumont uint32_t srcLen, void *destData, 164879a3c601SCedric Chaumont uint32_t *destLen) 1649b0104773SPascal Brand { 16506915bbbbSJens Wiklander TEE_Result res = TEE_SUCCESS; 1651e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 16526915bbbbSJens Wiklander uint64_t dl = 0; 1653b0104773SPascal Brand 16546915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) 1655b0104773SPascal Brand TEE_Panic(0); 16566915bbbbSJens Wiklander 16576915bbbbSJens Wiklander __utee_check_attr_in_annotation(params, paramCount); 16586915bbbbSJens Wiklander __utee_check_inout_annotation(destLen, sizeof(*destLen)); 16596915bbbbSJens Wiklander 166012e66b6fSCedric Chaumont if (!operation->key1) 1661b0104773SPascal Brand TEE_Panic(0); 166212e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 166312e66b6fSCedric Chaumont TEE_Panic(0); 166412e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_DECRYPT) 1665b0104773SPascal Brand TEE_Panic(0); 1666b0104773SPascal Brand 1667e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1668e86f1266SJens Wiklander dl = *destLen; 16692c028fdeSJerome Forissier res = _utee_asymm_operate(operation->state, ua, paramCount, srcData, 1670e86f1266SJens Wiklander srcLen, destData, &dl); 1671e86f1266SJens Wiklander *destLen = dl; 167212e66b6fSCedric Chaumont 16738844ebfcSPascal Brand if (res != TEE_SUCCESS && 16748844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 16758844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1676b0104773SPascal Brand TEE_Panic(res); 167712e66b6fSCedric Chaumont 1678b0104773SPascal Brand return res; 1679b0104773SPascal Brand } 1680b0104773SPascal Brand 168112e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation, 16828f07fe6fSJerome Forissier const TEE_Attribute *params, 16838f07fe6fSJerome Forissier uint32_t paramCount, const void *digest, 168479a3c601SCedric Chaumont uint32_t digestLen, void *signature, 168579a3c601SCedric Chaumont uint32_t *signatureLen) 1686b0104773SPascal Brand { 16876915bbbbSJens Wiklander TEE_Result res = TEE_SUCCESS; 1688e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 16896915bbbbSJens Wiklander uint64_t sl = 0; 1690b0104773SPascal Brand 16916915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!digest && digestLen)) 1692b0104773SPascal Brand TEE_Panic(0); 16936915bbbbSJens Wiklander 16946915bbbbSJens Wiklander __utee_check_attr_in_annotation(params, paramCount); 16956915bbbbSJens Wiklander __utee_check_inout_annotation(signatureLen, sizeof(*signatureLen)); 16966915bbbbSJens Wiklander 169712e66b6fSCedric Chaumont if (!operation->key1) 1698b0104773SPascal Brand TEE_Panic(0); 169912e66b6fSCedric Chaumont if (operation->info.operationClass != 170012e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 170112e66b6fSCedric Chaumont TEE_Panic(0); 170212e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_SIGN) 1703b0104773SPascal Brand TEE_Panic(0); 1704b0104773SPascal Brand 1705e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1706e86f1266SJens Wiklander sl = *signatureLen; 17072c028fdeSJerome Forissier res = _utee_asymm_operate(operation->state, ua, paramCount, digest, 1708e86f1266SJens Wiklander digestLen, signature, &sl); 1709e86f1266SJens Wiklander *signatureLen = sl; 171012e66b6fSCedric Chaumont 1711b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER) 1712b0104773SPascal Brand TEE_Panic(res); 171312e66b6fSCedric Chaumont 1714b0104773SPascal Brand return res; 1715b0104773SPascal Brand } 1716b0104773SPascal Brand 171712e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation, 17188f07fe6fSJerome Forissier const TEE_Attribute *params, 17198f07fe6fSJerome Forissier uint32_t paramCount, const void *digest, 17208f07fe6fSJerome Forissier uint32_t digestLen, 17218f07fe6fSJerome Forissier const void *signature, 172279a3c601SCedric Chaumont uint32_t signatureLen) 1723b0104773SPascal Brand { 1724b0104773SPascal Brand TEE_Result res; 1725e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1726b0104773SPascal Brand 172712e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || 172812e66b6fSCedric Chaumont (digest == NULL && digestLen != 0) || 1729b0104773SPascal Brand (signature == NULL && signatureLen != 0)) 1730b0104773SPascal Brand TEE_Panic(0); 17316915bbbbSJens Wiklander 17326915bbbbSJens Wiklander __utee_check_attr_in_annotation(params, paramCount); 17336915bbbbSJens Wiklander 173412e66b6fSCedric Chaumont if (!operation->key1) 1735b0104773SPascal Brand TEE_Panic(0); 173612e66b6fSCedric Chaumont if (operation->info.operationClass != 173712e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 173812e66b6fSCedric Chaumont TEE_Panic(0); 173912e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_VERIFY) 1740b0104773SPascal Brand TEE_Panic(0); 1741b0104773SPascal Brand 1742e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 17432c028fdeSJerome Forissier res = _utee_asymm_verify(operation->state, ua, paramCount, digest, 174412e66b6fSCedric Chaumont digestLen, signature, signatureLen); 174512e66b6fSCedric Chaumont 1746b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID) 1747b0104773SPascal Brand TEE_Panic(res); 174812e66b6fSCedric Chaumont 1749b0104773SPascal Brand return res; 1750b0104773SPascal Brand } 1751b0104773SPascal Brand 1752b0104773SPascal Brand /* Cryptographic Operations API - Key Derivation Functions */ 1753b0104773SPascal Brand 1754b0104773SPascal Brand void TEE_DeriveKey(TEE_OperationHandle operation, 1755b0104773SPascal Brand const TEE_Attribute *params, uint32_t paramCount, 1756b0104773SPascal Brand TEE_ObjectHandle derivedKey) 1757b0104773SPascal Brand { 1758b0104773SPascal Brand TEE_Result res; 1759b0104773SPascal Brand TEE_ObjectInfo key_info; 1760e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1761b0104773SPascal Brand 1762b0104773SPascal Brand if (operation == TEE_HANDLE_NULL || derivedKey == 0) 1763b0104773SPascal Brand TEE_Panic(0); 17646915bbbbSJens Wiklander 17656915bbbbSJens Wiklander __utee_check_attr_in_annotation(params, paramCount); 17666915bbbbSJens Wiklander 17678854d3c6SJerome Forissier if (TEE_ALG_GET_CLASS(operation->info.algorithm) != 17688854d3c6SJerome Forissier TEE_OPERATION_KEY_DERIVATION) 1769b0104773SPascal Brand TEE_Panic(0); 1770b0104773SPascal Brand 1771b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION) 1772b0104773SPascal Brand TEE_Panic(0); 177384fa9467SCedric Chaumont if (!operation->key1) 177484fa9467SCedric Chaumont TEE_Panic(0); 1775b0104773SPascal Brand if (operation->info.mode != TEE_MODE_DERIVE) 1776b0104773SPascal Brand TEE_Panic(0); 1777b0104773SPascal Brand if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0) 1778b0104773SPascal Brand TEE_Panic(0); 1779b0104773SPascal Brand 17802c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info); 1781b0104773SPascal Brand if (res != TEE_SUCCESS) 1782b36311adSJerome Forissier TEE_Panic(res); 1783b0104773SPascal Brand 1784b0104773SPascal Brand if (key_info.objectType != TEE_TYPE_GENERIC_SECRET) 1785b0104773SPascal Brand TEE_Panic(0); 1786b0104773SPascal Brand if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1787b0104773SPascal Brand TEE_Panic(0); 1788b0104773SPascal Brand 1789e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 17902c028fdeSJerome Forissier res = _utee_cryp_derive_key(operation->state, ua, paramCount, 1791e86f1266SJens Wiklander (unsigned long)derivedKey); 1792b0104773SPascal Brand if (res != TEE_SUCCESS) 1793b0104773SPascal Brand TEE_Panic(res); 1794b0104773SPascal Brand } 1795b0104773SPascal Brand 1796b0104773SPascal Brand /* Cryptographic Operations API - Random Number Generation Functions */ 1797b0104773SPascal Brand 179879a3c601SCedric Chaumont void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen) 1799b0104773SPascal Brand { 1800b0104773SPascal Brand TEE_Result res; 1801b0104773SPascal Brand 18022c028fdeSJerome Forissier res = _utee_cryp_random_number_generate(randomBuffer, randomBufferLen); 1803b0104773SPascal Brand if (res != TEE_SUCCESS) 1804b0104773SPascal Brand TEE_Panic(res); 1805b0104773SPascal Brand } 1806433c4257SJens Wiklander 1807433c4257SJens Wiklander int rand(void) 1808433c4257SJens Wiklander { 1809433c4257SJens Wiklander int rc; 1810433c4257SJens Wiklander 1811433c4257SJens Wiklander TEE_GenerateRandom(&rc, sizeof(rc)); 1812433c4257SJens Wiklander 1813433c4257SJens Wiklander /* 1814433c4257SJens Wiklander * RAND_MAX is the larges int, INT_MAX which is all bits but the 1815433c4257SJens Wiklander * highest bit set. 1816433c4257SJens Wiklander */ 1817433c4257SJens Wiklander return rc & RAND_MAX; 1818433c4257SJens Wiklander } 181979170ce0SJerome Forissier 182079170ce0SJerome Forissier TEE_Result TEE_IsAlgorithmSupported(uint32_t alg, uint32_t element) 182179170ce0SJerome Forissier { 182279170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_AES)) { 182379170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_ECB)) { 182479170ce0SJerome Forissier if (alg == TEE_ALG_AES_ECB_NOPAD) 182579170ce0SJerome Forissier goto check_element_none; 182679170ce0SJerome Forissier } 182779170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CBC)) { 182879170ce0SJerome Forissier if (alg == TEE_ALG_AES_CBC_NOPAD) 182979170ce0SJerome Forissier goto check_element_none; 183079170ce0SJerome Forissier } 183179170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CTR)) { 183279170ce0SJerome Forissier if (alg == TEE_ALG_AES_CTR) 183379170ce0SJerome Forissier goto check_element_none; 183479170ce0SJerome Forissier } 183579170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CTS)) { 183679170ce0SJerome Forissier if (alg == TEE_ALG_AES_CTS) 183779170ce0SJerome Forissier goto check_element_none; 183879170ce0SJerome Forissier } 183979170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_XTS)) { 184079170ce0SJerome Forissier if (alg == TEE_ALG_AES_XTS) 184179170ce0SJerome Forissier goto check_element_none; 184279170ce0SJerome Forissier } 184379170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) { 184479170ce0SJerome Forissier if (alg == TEE_ALG_AES_CBC_MAC_NOPAD || 184579170ce0SJerome Forissier alg == TEE_ALG_AES_CBC_MAC_PKCS5) 184679170ce0SJerome Forissier goto check_element_none; 184779170ce0SJerome Forissier } 184879170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CMAC)) { 184979170ce0SJerome Forissier if (alg == TEE_ALG_AES_CMAC) 185079170ce0SJerome Forissier goto check_element_none; 185179170ce0SJerome Forissier } 185279170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CCM)) { 185379170ce0SJerome Forissier if (alg == TEE_ALG_AES_CCM) 185479170ce0SJerome Forissier goto check_element_none; 185579170ce0SJerome Forissier } 185679170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_GCM)) { 185779170ce0SJerome Forissier if (alg == TEE_ALG_AES_GCM) 185879170ce0SJerome Forissier goto check_element_none; 185979170ce0SJerome Forissier } 186079170ce0SJerome Forissier } 186179170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_DES)) { 186279170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_ECB)) { 186379170ce0SJerome Forissier if (alg == TEE_ALG_DES_ECB_NOPAD || 186479170ce0SJerome Forissier alg == TEE_ALG_DES3_ECB_NOPAD) 186579170ce0SJerome Forissier goto check_element_none; 186679170ce0SJerome Forissier } 186779170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CBC)) { 186879170ce0SJerome Forissier if (alg == TEE_ALG_DES_CBC_NOPAD || 186979170ce0SJerome Forissier alg == TEE_ALG_DES3_CBC_NOPAD) 187079170ce0SJerome Forissier goto check_element_none; 187179170ce0SJerome Forissier } 187279170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) { 187379170ce0SJerome Forissier if (alg == TEE_ALG_DES_CBC_MAC_NOPAD || 187479170ce0SJerome Forissier alg == TEE_ALG_DES_CBC_MAC_PKCS5 || 187579170ce0SJerome Forissier alg == TEE_ALG_DES3_CBC_MAC_NOPAD || 187679170ce0SJerome Forissier alg == TEE_ALG_DES3_CBC_MAC_PKCS5) 187779170ce0SJerome Forissier goto check_element_none; 187879170ce0SJerome Forissier } 187979170ce0SJerome Forissier } 188079170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_MD5)) { 188179170ce0SJerome Forissier if (alg == TEE_ALG_MD5) 188279170ce0SJerome Forissier goto check_element_none; 188379170ce0SJerome Forissier } 188479170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 188579170ce0SJerome Forissier if (alg == TEE_ALG_SHA1) 188679170ce0SJerome Forissier goto check_element_none; 188779170ce0SJerome Forissier } 188879170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 188979170ce0SJerome Forissier if (alg == TEE_ALG_SHA224) 189079170ce0SJerome Forissier goto check_element_none; 189179170ce0SJerome Forissier } 189279170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 189379170ce0SJerome Forissier if (alg == TEE_ALG_SHA256) 189479170ce0SJerome Forissier goto check_element_none; 189579170ce0SJerome Forissier } 189679170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA384)) { 189779170ce0SJerome Forissier if (alg == TEE_ALG_SHA384) 189879170ce0SJerome Forissier goto check_element_none; 189979170ce0SJerome Forissier } 190079170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA512)) { 190179170ce0SJerome Forissier if (alg == TEE_ALG_SHA512) 190279170ce0SJerome Forissier goto check_element_none; 190379170ce0SJerome Forissier } 190479170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) { 190579170ce0SJerome Forissier if (alg == TEE_ALG_MD5SHA1) 190679170ce0SJerome Forissier goto check_element_none; 190779170ce0SJerome Forissier } 190879170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_HMAC)) { 190979170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_MD5)) { 191079170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_MD5) 191179170ce0SJerome Forissier goto check_element_none; 191279170ce0SJerome Forissier } 191379170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 191479170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_SHA1) 191579170ce0SJerome Forissier goto check_element_none; 191679170ce0SJerome Forissier } 191779170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 191879170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_SHA224) 191979170ce0SJerome Forissier goto check_element_none; 192079170ce0SJerome Forissier } 192179170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 192279170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_SHA256) 192379170ce0SJerome Forissier goto check_element_none; 192479170ce0SJerome Forissier } 192579170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA384)) { 192679170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_SHA384) 192779170ce0SJerome Forissier goto check_element_none; 192879170ce0SJerome Forissier } 192979170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA512)) { 193079170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_SHA512) 193179170ce0SJerome Forissier goto check_element_none; 193279170ce0SJerome Forissier } 193379170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SM3)) { 193479170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_SM3) 193579170ce0SJerome Forissier goto check_element_none; 193679170ce0SJerome Forissier } 193779170ce0SJerome Forissier } 193879170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SM3)) { 193979170ce0SJerome Forissier if (alg == TEE_ALG_SM3) 194079170ce0SJerome Forissier goto check_element_none; 194179170ce0SJerome Forissier } 194279170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SM4)) { 194379170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_ECB)) { 194479170ce0SJerome Forissier if (alg == TEE_ALG_SM4_ECB_NOPAD) 194579170ce0SJerome Forissier goto check_element_none; 194679170ce0SJerome Forissier } 194779170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CBC)) { 194879170ce0SJerome Forissier if (alg == TEE_ALG_SM4_CBC_NOPAD) 194979170ce0SJerome Forissier goto check_element_none; 195079170ce0SJerome Forissier } 195179170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CTR)) { 195279170ce0SJerome Forissier if (alg == TEE_ALG_SM4_CTR) 195379170ce0SJerome Forissier goto check_element_none; 195479170ce0SJerome Forissier } 195579170ce0SJerome Forissier } 195679170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_RSA)) { 195779170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_MD5)) { 195879170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5) 195979170ce0SJerome Forissier goto check_element_none; 196079170ce0SJerome Forissier } 196179170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 196279170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA1 || 196379170ce0SJerome Forissier alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1 || 196479170ce0SJerome Forissier alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1) 196579170ce0SJerome Forissier goto check_element_none; 196679170ce0SJerome Forissier } 196779170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) { 196879170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5SHA1) 196979170ce0SJerome Forissier goto check_element_none; 197079170ce0SJerome Forissier } 197179170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 197279170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA224 || 197379170ce0SJerome Forissier alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224 || 197479170ce0SJerome Forissier alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224) 197579170ce0SJerome Forissier goto check_element_none; 197679170ce0SJerome Forissier } 197779170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 197879170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA256 || 197979170ce0SJerome Forissier alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256 || 198079170ce0SJerome Forissier alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256) 198179170ce0SJerome Forissier goto check_element_none; 198279170ce0SJerome Forissier } 198379170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA384)) { 198479170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA384 || 198579170ce0SJerome Forissier alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384 || 198679170ce0SJerome Forissier alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384) 198779170ce0SJerome Forissier goto check_element_none; 198879170ce0SJerome Forissier } 198979170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA512)) { 199079170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA512 || 199179170ce0SJerome Forissier alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512 || 199279170ce0SJerome Forissier alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512) 199379170ce0SJerome Forissier goto check_element_none; 199479170ce0SJerome Forissier } 199579170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_RSASSA_NA1)) { 199679170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5) 199779170ce0SJerome Forissier goto check_element_none; 199879170ce0SJerome Forissier } 199979170ce0SJerome Forissier if (alg == TEE_ALG_RSA_NOPAD) 200079170ce0SJerome Forissier goto check_element_none; 200179170ce0SJerome Forissier } 200279170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_DSA)) { 200379170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 200479170ce0SJerome Forissier if (alg == TEE_ALG_DSA_SHA1) 200579170ce0SJerome Forissier goto check_element_none; 200679170ce0SJerome Forissier } 200779170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 200879170ce0SJerome Forissier if (alg == TEE_ALG_DSA_SHA224) 200979170ce0SJerome Forissier goto check_element_none; 201079170ce0SJerome Forissier } 201179170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 201279170ce0SJerome Forissier if (alg == TEE_ALG_DSA_SHA256) 201379170ce0SJerome Forissier goto check_element_none; 201479170ce0SJerome Forissier } 201579170ce0SJerome Forissier } 201679170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_DH)) { 201779170ce0SJerome Forissier if (alg == TEE_ALG_DH_DERIVE_SHARED_SECRET) 201879170ce0SJerome Forissier goto check_element_none; 201979170ce0SJerome Forissier } 202079170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_ECC)) { 202179170ce0SJerome Forissier if ((alg == TEE_ALG_ECDH_P192 || alg == TEE_ALG_ECDSA_P192) && 202279170ce0SJerome Forissier element == TEE_ECC_CURVE_NIST_P192) 202379170ce0SJerome Forissier return TEE_SUCCESS; 202479170ce0SJerome Forissier if ((alg == TEE_ALG_ECDH_P224 || alg == TEE_ALG_ECDSA_P224) && 202579170ce0SJerome Forissier element == TEE_ECC_CURVE_NIST_P224) 202679170ce0SJerome Forissier return TEE_SUCCESS; 202779170ce0SJerome Forissier if ((alg == TEE_ALG_ECDH_P256 || alg == TEE_ALG_ECDSA_P256) && 202879170ce0SJerome Forissier element == TEE_ECC_CURVE_NIST_P256) 202979170ce0SJerome Forissier return TEE_SUCCESS; 203079170ce0SJerome Forissier if ((alg == TEE_ALG_ECDH_P384 || alg == TEE_ALG_ECDSA_P384) && 203179170ce0SJerome Forissier element == TEE_ECC_CURVE_NIST_P384) 203279170ce0SJerome Forissier return TEE_SUCCESS; 203379170ce0SJerome Forissier if ((alg == TEE_ALG_ECDH_P521 || alg == TEE_ALG_ECDSA_P521) && 203479170ce0SJerome Forissier element == TEE_ECC_CURVE_NIST_P521) 203579170ce0SJerome Forissier return TEE_SUCCESS; 203679170ce0SJerome Forissier } 203779170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SM2_DSA)) { 203879170ce0SJerome Forissier if (alg == TEE_ALG_SM2_DSA_SM3 && element == TEE_ECC_CURVE_SM2) 203979170ce0SJerome Forissier return TEE_SUCCESS; 204079170ce0SJerome Forissier } 204179170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SM2_KEP)) { 204279170ce0SJerome Forissier if (alg == TEE_ALG_SM2_KEP && element == TEE_ECC_CURVE_SM2) 204379170ce0SJerome Forissier return TEE_SUCCESS; 204479170ce0SJerome Forissier } 204579170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SM2_PKE)) { 204679170ce0SJerome Forissier if (alg == TEE_ALG_SM2_PKE && element == TEE_ECC_CURVE_SM2) 204779170ce0SJerome Forissier return TEE_SUCCESS; 204879170ce0SJerome Forissier } 204979170ce0SJerome Forissier 205079170ce0SJerome Forissier return TEE_ERROR_NOT_SUPPORTED; 205179170ce0SJerome Forissier check_element_none: 205279170ce0SJerome Forissier if (element == TEE_CRYPTO_ELEMENT_NONE) 205379170ce0SJerome Forissier return TEE_SUCCESS; 205479170ce0SJerome Forissier return TEE_ERROR_NOT_SUPPORTED; 205579170ce0SJerome Forissier } 2056