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 uint32_t ae_tag_len; /* 28b0104773SPascal Brand * tag_len in bytes for AE operation else unused 29b0104773SPascal Brand */ 30b0104773SPascal Brand }; 31b0104773SPascal Brand 32b0104773SPascal Brand /* Cryptographic Operations API - Generic Operation Functions */ 33b0104773SPascal Brand 34b0104773SPascal Brand TEE_Result TEE_AllocateOperation(TEE_OperationHandle *operation, 35b0104773SPascal Brand uint32_t algorithm, uint32_t mode, 36b0104773SPascal Brand uint32_t maxKeySize) 37b0104773SPascal Brand { 38b0104773SPascal Brand TEE_Result res; 39b0104773SPascal Brand TEE_OperationHandle op = TEE_HANDLE_NULL; 40b0104773SPascal Brand uint32_t handle_state = 0; 41b0104773SPascal Brand size_t block_size = 1; 42b0104773SPascal Brand uint32_t req_key_usage; 43b0104773SPascal Brand bool with_private_key = false; 44b0104773SPascal Brand bool buffer_two_blocks = false; 45b0104773SPascal Brand 469b52c538SCedric Chaumont if (!operation) 47b0104773SPascal Brand TEE_Panic(0); 48b0104773SPascal Brand 495b385b3fSJerome Forissier if (algorithm == TEE_ALG_AES_XTS || algorithm == TEE_ALG_SM2_KEP) 50b0104773SPascal Brand handle_state = TEE_HANDLE_FLAG_EXPECT_TWO_KEYS; 51b0104773SPascal Brand 52218d9055SCedric Chaumont /* Check algorithm max key size */ 53218d9055SCedric Chaumont switch (algorithm) { 54218d9055SCedric Chaumont case TEE_ALG_DSA_SHA1: 55218d9055SCedric Chaumont if (maxKeySize < 512) 56218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 57218d9055SCedric Chaumont if (maxKeySize > 1024) 58218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 59218d9055SCedric Chaumont if (maxKeySize % 64 != 0) 60218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 61218d9055SCedric Chaumont break; 62218d9055SCedric Chaumont 63218d9055SCedric Chaumont case TEE_ALG_DSA_SHA224: 64218d9055SCedric Chaumont if (maxKeySize != 2048) 65218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 66218d9055SCedric Chaumont break; 67218d9055SCedric Chaumont 68218d9055SCedric Chaumont case TEE_ALG_DSA_SHA256: 69218d9055SCedric Chaumont if (maxKeySize != 2048 && maxKeySize != 3072) 70218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 71218d9055SCedric Chaumont break; 72218d9055SCedric Chaumont 731220586eSCedric Chaumont case TEE_ALG_ECDSA_P192: 741220586eSCedric Chaumont case TEE_ALG_ECDH_P192: 751220586eSCedric Chaumont if (maxKeySize != 192) 761220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 771220586eSCedric Chaumont break; 781220586eSCedric Chaumont 791220586eSCedric Chaumont case TEE_ALG_ECDSA_P224: 801220586eSCedric Chaumont case TEE_ALG_ECDH_P224: 811220586eSCedric Chaumont if (maxKeySize != 224) 821220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 831220586eSCedric Chaumont break; 841220586eSCedric Chaumont 851220586eSCedric Chaumont case TEE_ALG_ECDSA_P256: 861220586eSCedric Chaumont case TEE_ALG_ECDH_P256: 8791fc6bd8SJerome Forissier case TEE_ALG_SM2_PKE: 880f151943SJerome Forissier case TEE_ALG_SM2_DSA_SM3: 891220586eSCedric Chaumont if (maxKeySize != 256) 901220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 911220586eSCedric Chaumont break; 921220586eSCedric Chaumont 935b385b3fSJerome Forissier case TEE_ALG_SM2_KEP: 945b385b3fSJerome Forissier /* Two 256-bit keys */ 955b385b3fSJerome Forissier if (maxKeySize != 512) 965b385b3fSJerome Forissier return TEE_ERROR_NOT_SUPPORTED; 975b385b3fSJerome Forissier break; 985b385b3fSJerome Forissier 991220586eSCedric Chaumont case TEE_ALG_ECDSA_P384: 1001220586eSCedric Chaumont case TEE_ALG_ECDH_P384: 1011220586eSCedric Chaumont if (maxKeySize != 384) 1021220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 1031220586eSCedric Chaumont break; 1041220586eSCedric Chaumont 1051220586eSCedric Chaumont case TEE_ALG_ECDSA_P521: 1061220586eSCedric Chaumont case TEE_ALG_ECDH_P521: 1071220586eSCedric Chaumont if (maxKeySize != 521) 1081220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 1091220586eSCedric Chaumont break; 1101220586eSCedric Chaumont 111218d9055SCedric Chaumont default: 112218d9055SCedric Chaumont break; 113218d9055SCedric Chaumont } 114218d9055SCedric Chaumont 115218d9055SCedric Chaumont /* Check algorithm mode */ 116b0104773SPascal Brand switch (algorithm) { 117b0104773SPascal Brand case TEE_ALG_AES_CTS: 118b0104773SPascal Brand case TEE_ALG_AES_XTS: 119b0104773SPascal Brand buffer_two_blocks = true; 1204bd53c54SJerome Forissier /* FALLTHROUGH */ 1214bd53c54SJerome Forissier case TEE_ALG_AES_ECB_NOPAD: 122b0104773SPascal Brand case TEE_ALG_AES_CBC_NOPAD: 123b0104773SPascal Brand case TEE_ALG_AES_CCM: 124b0104773SPascal Brand case TEE_ALG_DES_ECB_NOPAD: 125b0104773SPascal Brand case TEE_ALG_DES_CBC_NOPAD: 126b0104773SPascal Brand case TEE_ALG_DES3_ECB_NOPAD: 127b0104773SPascal Brand case TEE_ALG_DES3_CBC_NOPAD: 128ade6f848SJerome Forissier case TEE_ALG_SM4_ECB_NOPAD: 129ade6f848SJerome Forissier case TEE_ALG_SM4_CBC_NOPAD: 130ade6f848SJerome Forissier case TEE_ALG_SM4_CTR: 131b0104773SPascal Brand if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES) 132b0104773SPascal Brand block_size = TEE_AES_BLOCK_SIZE; 133ade6f848SJerome Forissier else if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_SM4) 134ade6f848SJerome Forissier block_size = TEE_SM4_BLOCK_SIZE; 135b0104773SPascal Brand else 136b0104773SPascal Brand block_size = TEE_DES_BLOCK_SIZE; 137afc0c182SBogdan Liulko /* FALLTHROUGH */ 13857aabac5SBogdan Liulko case TEE_ALG_AES_CTR: 139afc0c182SBogdan Liulko case TEE_ALG_AES_GCM: 140b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) 141b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT; 142b0104773SPascal Brand else if (mode == TEE_MODE_DECRYPT) 143b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT; 144b0104773SPascal Brand else 145b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 146b0104773SPascal Brand break; 147b0104773SPascal Brand 1486a2e0a9fSGabor Szekely #if defined(CFG_CRYPTO_RSASSA_NA1) 1496a2e0a9fSGabor Szekely case TEE_ALG_RSASSA_PKCS1_V1_5: 1506a2e0a9fSGabor Szekely #endif 151b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_MD5: 152b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1: 153b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224: 154b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256: 155b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384: 156b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512: 157b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1: 158b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224: 159b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256: 160b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384: 161b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512: 162b0104773SPascal Brand case TEE_ALG_DSA_SHA1: 163218d9055SCedric Chaumont case TEE_ALG_DSA_SHA224: 164218d9055SCedric Chaumont case TEE_ALG_DSA_SHA256: 1651220586eSCedric Chaumont case TEE_ALG_ECDSA_P192: 1661220586eSCedric Chaumont case TEE_ALG_ECDSA_P224: 1671220586eSCedric Chaumont case TEE_ALG_ECDSA_P256: 1681220586eSCedric Chaumont case TEE_ALG_ECDSA_P384: 1691220586eSCedric Chaumont case TEE_ALG_ECDSA_P521: 1700f151943SJerome Forissier case TEE_ALG_SM2_DSA_SM3: 171b0104773SPascal Brand if (mode == TEE_MODE_SIGN) { 172b0104773SPascal Brand with_private_key = true; 173b0104773SPascal Brand req_key_usage = TEE_USAGE_SIGN; 174b0104773SPascal Brand } else if (mode == TEE_MODE_VERIFY) { 175b0104773SPascal Brand req_key_usage = TEE_USAGE_VERIFY; 176b0104773SPascal Brand } else { 177b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 178b0104773SPascal Brand } 179b0104773SPascal Brand break; 180b0104773SPascal Brand 181b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_V1_5: 182b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1: 183b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224: 184b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256: 185b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384: 186b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512: 18791fc6bd8SJerome Forissier case TEE_ALG_SM2_PKE: 188b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) { 189b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT; 190b0104773SPascal Brand } else if (mode == TEE_MODE_DECRYPT) { 191b0104773SPascal Brand with_private_key = true; 192b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT; 193b0104773SPascal Brand } else { 194b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 195b0104773SPascal Brand } 196b0104773SPascal Brand break; 197b0104773SPascal Brand 198b0104773SPascal Brand case TEE_ALG_RSA_NOPAD: 199b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) { 200b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY; 201b0104773SPascal Brand } else if (mode == TEE_MODE_DECRYPT) { 202b0104773SPascal Brand with_private_key = true; 203b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT | TEE_USAGE_SIGN; 204b0104773SPascal Brand } else { 205b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 206b0104773SPascal Brand } 207b0104773SPascal Brand break; 208b0104773SPascal Brand 209b0104773SPascal Brand case TEE_ALG_DH_DERIVE_SHARED_SECRET: 2101220586eSCedric Chaumont case TEE_ALG_ECDH_P192: 2111220586eSCedric Chaumont case TEE_ALG_ECDH_P224: 2121220586eSCedric Chaumont case TEE_ALG_ECDH_P256: 2131220586eSCedric Chaumont case TEE_ALG_ECDH_P384: 2141220586eSCedric Chaumont case TEE_ALG_ECDH_P521: 215cdb198a7SJerome Forissier case TEE_ALG_HKDF_MD5_DERIVE_KEY: 216cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA1_DERIVE_KEY: 217cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA224_DERIVE_KEY: 218cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA256_DERIVE_KEY: 219cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA384_DERIVE_KEY: 220cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA512_DERIVE_KEY: 2218854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA1_DERIVE_KEY: 2228854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA224_DERIVE_KEY: 2238854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA256_DERIVE_KEY: 2248854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA384_DERIVE_KEY: 2258854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA512_DERIVE_KEY: 2260f2293b7SJerome Forissier case TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY: 2275b385b3fSJerome Forissier case TEE_ALG_SM2_KEP: 228b0104773SPascal Brand if (mode != TEE_MODE_DERIVE) 229b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 230b0104773SPascal Brand with_private_key = true; 231b0104773SPascal Brand req_key_usage = TEE_USAGE_DERIVE; 232b0104773SPascal Brand break; 233b0104773SPascal Brand 234b0104773SPascal Brand case TEE_ALG_MD5: 235b0104773SPascal Brand case TEE_ALG_SHA1: 236b0104773SPascal Brand case TEE_ALG_SHA224: 237b0104773SPascal Brand case TEE_ALG_SHA256: 238b0104773SPascal Brand case TEE_ALG_SHA384: 239b0104773SPascal Brand case TEE_ALG_SHA512: 24047645577SJerome Forissier case TEE_ALG_SM3: 241b0104773SPascal Brand if (mode != TEE_MODE_DIGEST) 242b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 24305304565SCedric Chaumont /* v1.1: flags always set for digest operations */ 244b0104773SPascal Brand handle_state |= TEE_HANDLE_FLAG_KEY_SET; 245b0104773SPascal Brand req_key_usage = 0; 246b0104773SPascal Brand break; 247b0104773SPascal Brand 248b0104773SPascal Brand case TEE_ALG_DES_CBC_MAC_NOPAD: 249b0104773SPascal Brand case TEE_ALG_AES_CBC_MAC_NOPAD: 250b0104773SPascal Brand case TEE_ALG_AES_CBC_MAC_PKCS5: 251b0104773SPascal Brand case TEE_ALG_AES_CMAC: 252b0104773SPascal Brand case TEE_ALG_DES_CBC_MAC_PKCS5: 253b0104773SPascal Brand case TEE_ALG_DES3_CBC_MAC_NOPAD: 254b0104773SPascal Brand case TEE_ALG_DES3_CBC_MAC_PKCS5: 255b0104773SPascal Brand case TEE_ALG_HMAC_MD5: 256b0104773SPascal Brand case TEE_ALG_HMAC_SHA1: 257b0104773SPascal Brand case TEE_ALG_HMAC_SHA224: 258b0104773SPascal Brand case TEE_ALG_HMAC_SHA256: 259b0104773SPascal Brand case TEE_ALG_HMAC_SHA384: 260b0104773SPascal Brand case TEE_ALG_HMAC_SHA512: 26147645577SJerome Forissier case TEE_ALG_HMAC_SM3: 262b0104773SPascal Brand if (mode != TEE_MODE_MAC) 263b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 264b0104773SPascal Brand req_key_usage = TEE_USAGE_MAC; 265b0104773SPascal Brand break; 266b0104773SPascal Brand 267b0104773SPascal Brand default: 268b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 269b0104773SPascal Brand } 270b0104773SPascal Brand 271b66f219bSJens Wiklander op = TEE_Malloc(sizeof(*op), TEE_MALLOC_FILL_ZERO); 2729b52c538SCedric Chaumont if (!op) 273b0104773SPascal Brand return TEE_ERROR_OUT_OF_MEMORY; 274b0104773SPascal Brand 275b0104773SPascal Brand op->info.algorithm = algorithm; 276b0104773SPascal Brand op->info.operationClass = TEE_ALG_GET_CLASS(algorithm); 2776a2e0a9fSGabor Szekely #ifdef CFG_CRYPTO_RSASSA_NA1 2786a2e0a9fSGabor Szekely if (algorithm == TEE_ALG_RSASSA_PKCS1_V1_5) 2796a2e0a9fSGabor Szekely op->info.operationClass = TEE_OPERATION_ASYMMETRIC_SIGNATURE; 2806a2e0a9fSGabor Szekely #endif 281b0104773SPascal Brand op->info.mode = mode; 282*2e5e6460SAlbert Schwarzkopf op->info.digestLength = TEE_ALG_GET_DIGEST_SIZE(algorithm); 283b0104773SPascal Brand op->info.maxKeySize = maxKeySize; 284b0104773SPascal Brand op->info.requiredKeyUsage = req_key_usage; 285b0104773SPascal Brand op->info.handleState = handle_state; 286b0104773SPascal Brand 287b0104773SPascal Brand if (block_size > 1) { 288b0104773SPascal Brand size_t buffer_size = block_size; 289b0104773SPascal Brand 290b0104773SPascal Brand if (buffer_two_blocks) 291b0104773SPascal Brand buffer_size *= 2; 292b0104773SPascal Brand 2939b52c538SCedric Chaumont op->buffer = TEE_Malloc(buffer_size, 2949b52c538SCedric Chaumont TEE_USER_MEM_HINT_NO_FILL_ZERO); 295b0104773SPascal Brand if (op->buffer == NULL) { 296b0104773SPascal Brand res = TEE_ERROR_OUT_OF_MEMORY; 297b66f219bSJens Wiklander goto out; 298b0104773SPascal Brand } 299b0104773SPascal Brand } 300b0104773SPascal Brand op->block_size = block_size; 301b0104773SPascal Brand op->buffer_two_blocks = buffer_two_blocks; 302b0104773SPascal Brand 303b0104773SPascal Brand if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) { 304b0104773SPascal Brand uint32_t mks = maxKeySize; 305b0104773SPascal Brand TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm, 306b0104773SPascal Brand with_private_key); 307b0104773SPascal Brand 308b0104773SPascal Brand /* 309b0104773SPascal Brand * If two keys are expected the max key size is the sum of 310b0104773SPascal Brand * the size of both keys. 311b0104773SPascal Brand */ 312b0104773SPascal Brand if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) 313b0104773SPascal Brand mks /= 2; 314b0104773SPascal Brand 315b0104773SPascal Brand res = TEE_AllocateTransientObject(key_type, mks, &op->key1); 316b0104773SPascal Brand if (res != TEE_SUCCESS) 317b66f219bSJens Wiklander goto out; 318b0104773SPascal Brand 31905304565SCedric Chaumont if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) { 3209b52c538SCedric Chaumont res = TEE_AllocateTransientObject(key_type, mks, 321b0104773SPascal Brand &op->key2); 322b0104773SPascal Brand if (res != TEE_SUCCESS) 323b66f219bSJens Wiklander goto out; 324b0104773SPascal Brand } 325b0104773SPascal Brand } 326b0104773SPascal Brand 327e86f1266SJens Wiklander res = utee_cryp_state_alloc(algorithm, mode, (unsigned long)op->key1, 328e86f1266SJens Wiklander (unsigned long)op->key2, &op->state); 329b66f219bSJens Wiklander if (res != TEE_SUCCESS) 330b66f219bSJens Wiklander goto out; 331b0104773SPascal Brand 33205304565SCedric Chaumont /* 33305304565SCedric Chaumont * Initialize digest operations 33405304565SCedric Chaumont * Other multi-stage operations initialized w/ TEE_xxxInit functions 33505304565SCedric Chaumont * Non-applicable on asymmetric operations 33605304565SCedric Chaumont */ 33705304565SCedric Chaumont if (TEE_ALG_GET_CLASS(algorithm) == TEE_OPERATION_DIGEST) { 33805304565SCedric Chaumont res = utee_hash_init(op->state, NULL, 0); 33905304565SCedric Chaumont if (res != TEE_SUCCESS) 340b66f219bSJens Wiklander goto out; 34105304565SCedric Chaumont /* v1.1: flags always set for digest operations */ 34205304565SCedric Chaumont op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 34305304565SCedric Chaumont } 34405304565SCedric Chaumont 345642a1607SCedric Chaumont op->operationState = TEE_OPERATION_STATE_INITIAL; 346642a1607SCedric Chaumont 347b0104773SPascal Brand *operation = op; 348b0104773SPascal Brand 349b66f219bSJens Wiklander out: 350b66f219bSJens Wiklander if (res != TEE_SUCCESS) { 351b66f219bSJens Wiklander if (res != TEE_ERROR_OUT_OF_MEMORY && 3529b52c538SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 353b36311adSJerome Forissier TEE_Panic(res); 354b66f219bSJens Wiklander if (op) { 355b66f219bSJens Wiklander if (op->state) { 356b66f219bSJens Wiklander TEE_FreeOperation(op); 357b66f219bSJens Wiklander } else { 358b66f219bSJens Wiklander TEE_Free(op->buffer); 359b66f219bSJens Wiklander TEE_FreeTransientObject(op->key1); 360b66f219bSJens Wiklander TEE_FreeTransientObject(op->key2); 361b66f219bSJens Wiklander TEE_Free(op); 362b66f219bSJens Wiklander } 363b66f219bSJens Wiklander } 364b66f219bSJens Wiklander } 365b66f219bSJens Wiklander 366b0104773SPascal Brand return res; 367b0104773SPascal Brand } 368b0104773SPascal Brand 369b0104773SPascal Brand void TEE_FreeOperation(TEE_OperationHandle operation) 370b0104773SPascal Brand { 371e889e80bSCedric Chaumont TEE_Result res; 372e889e80bSCedric Chaumont 373e889e80bSCedric Chaumont if (operation == TEE_HANDLE_NULL) 374e889e80bSCedric Chaumont TEE_Panic(0); 375e889e80bSCedric Chaumont 376b0104773SPascal Brand /* 377b0104773SPascal Brand * Note that keys should not be freed here, since they are 378b0104773SPascal Brand * claimed by the operation they will be freed by 379b0104773SPascal Brand * utee_cryp_state_free(). 380b0104773SPascal Brand */ 381e889e80bSCedric Chaumont res = utee_cryp_state_free(operation->state); 382e889e80bSCedric Chaumont if (res != TEE_SUCCESS) 383b36311adSJerome Forissier TEE_Panic(res); 384e889e80bSCedric Chaumont 385b0104773SPascal Brand TEE_Free(operation->buffer); 386b0104773SPascal Brand TEE_Free(operation); 387b0104773SPascal Brand } 388b0104773SPascal Brand 389b0104773SPascal Brand void TEE_GetOperationInfo(TEE_OperationHandle operation, 390b0104773SPascal Brand TEE_OperationInfo *operationInfo) 391b0104773SPascal Brand { 392b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 393b0104773SPascal Brand TEE_Panic(0); 394b0104773SPascal Brand 39505304565SCedric Chaumont if (!operationInfo) 396b0104773SPascal Brand TEE_Panic(0); 397b0104773SPascal Brand 398b0104773SPascal Brand *operationInfo = operation->info; 399b0104773SPascal Brand } 400b0104773SPascal Brand 40105304565SCedric Chaumont TEE_Result TEE_GetOperationInfoMultiple(TEE_OperationHandle operation, 40205304565SCedric Chaumont TEE_OperationInfoMultiple *operationInfoMultiple, 40305304565SCedric Chaumont uint32_t *operationSize) 40405304565SCedric Chaumont { 40505304565SCedric Chaumont TEE_Result res = TEE_SUCCESS; 40605304565SCedric Chaumont TEE_ObjectInfo key_info1; 40705304565SCedric Chaumont TEE_ObjectInfo key_info2; 40805304565SCedric Chaumont uint32_t num_of_keys; 40905304565SCedric Chaumont size_t n; 41005304565SCedric Chaumont 41105304565SCedric Chaumont if (operation == TEE_HANDLE_NULL) { 41205304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 41305304565SCedric Chaumont goto out; 41405304565SCedric Chaumont } 41505304565SCedric Chaumont 41605304565SCedric Chaumont if (!operationInfoMultiple) { 41705304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 41805304565SCedric Chaumont goto out; 41905304565SCedric Chaumont } 42005304565SCedric Chaumont 42105304565SCedric Chaumont if (!operationSize) { 42205304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 42305304565SCedric Chaumont goto out; 42405304565SCedric Chaumont } 42505304565SCedric Chaumont 42605304565SCedric Chaumont num_of_keys = (*operationSize-sizeof(TEE_OperationInfoMultiple))/ 42705304565SCedric Chaumont sizeof(TEE_OperationInfoKey); 42805304565SCedric Chaumont 42905304565SCedric Chaumont if (num_of_keys > 2) { 43005304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 43105304565SCedric Chaumont goto out; 43205304565SCedric Chaumont } 43305304565SCedric Chaumont 43405304565SCedric Chaumont /* Two keys flag (TEE_ALG_AES_XTS only) */ 43505304565SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 43605304565SCedric Chaumont 0 && 43705304565SCedric Chaumont (num_of_keys != 2)) { 43805304565SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 43905304565SCedric Chaumont goto out; 44005304565SCedric Chaumont } 44105304565SCedric Chaumont 44205304565SCedric Chaumont /* Clear */ 44305304565SCedric Chaumont for (n = 0; n < num_of_keys; n++) { 44405304565SCedric Chaumont operationInfoMultiple->keyInformation[n].keySize = 0; 44505304565SCedric Chaumont operationInfoMultiple->keyInformation[n].requiredKeyUsage = 0; 44605304565SCedric Chaumont } 44705304565SCedric Chaumont 44805304565SCedric Chaumont if (num_of_keys == 2) { 44905304565SCedric Chaumont res = TEE_GetObjectInfo1(operation->key2, &key_info2); 45005304565SCedric Chaumont /* Key2 is not a valid handle */ 45105304565SCedric Chaumont if (res != TEE_SUCCESS) 45205304565SCedric Chaumont goto out; 45305304565SCedric Chaumont 45405304565SCedric Chaumont operationInfoMultiple->keyInformation[1].keySize = 45505304565SCedric Chaumont key_info2.keySize; 45605304565SCedric Chaumont operationInfoMultiple->keyInformation[1].requiredKeyUsage = 45705304565SCedric Chaumont operation->info.requiredKeyUsage; 45805304565SCedric Chaumont } 45905304565SCedric Chaumont 46005304565SCedric Chaumont if (num_of_keys >= 1) { 46105304565SCedric Chaumont res = TEE_GetObjectInfo1(operation->key1, &key_info1); 46205304565SCedric Chaumont /* Key1 is not a valid handle */ 46305304565SCedric Chaumont if (res != TEE_SUCCESS) { 46405304565SCedric Chaumont if (num_of_keys == 2) { 46505304565SCedric Chaumont operationInfoMultiple->keyInformation[1]. 46605304565SCedric Chaumont keySize = 0; 46705304565SCedric Chaumont operationInfoMultiple->keyInformation[1]. 46805304565SCedric Chaumont requiredKeyUsage = 0; 46905304565SCedric Chaumont } 47005304565SCedric Chaumont goto out; 47105304565SCedric Chaumont } 47205304565SCedric Chaumont 47305304565SCedric Chaumont operationInfoMultiple->keyInformation[0].keySize = 47405304565SCedric Chaumont key_info1.keySize; 47505304565SCedric Chaumont operationInfoMultiple->keyInformation[0].requiredKeyUsage = 47605304565SCedric Chaumont operation->info.requiredKeyUsage; 47705304565SCedric Chaumont } 47805304565SCedric Chaumont 47905304565SCedric Chaumont /* No key */ 48005304565SCedric Chaumont operationInfoMultiple->algorithm = operation->info.algorithm; 48105304565SCedric Chaumont operationInfoMultiple->operationClass = operation->info.operationClass; 48205304565SCedric Chaumont operationInfoMultiple->mode = operation->info.mode; 48305304565SCedric Chaumont operationInfoMultiple->digestLength = operation->info.digestLength; 48405304565SCedric Chaumont operationInfoMultiple->maxKeySize = operation->info.maxKeySize; 48505304565SCedric Chaumont operationInfoMultiple->handleState = operation->info.handleState; 48605304565SCedric Chaumont operationInfoMultiple->operationState = operation->operationState; 48705304565SCedric Chaumont operationInfoMultiple->numberOfKeys = num_of_keys; 48805304565SCedric Chaumont 48905304565SCedric Chaumont out: 49005304565SCedric Chaumont if (res != TEE_SUCCESS && 49105304565SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 492b36311adSJerome Forissier TEE_Panic(res); 49305304565SCedric Chaumont 49405304565SCedric Chaumont return res; 49505304565SCedric Chaumont } 49605304565SCedric Chaumont 497b0104773SPascal Brand void TEE_ResetOperation(TEE_OperationHandle operation) 498b0104773SPascal Brand { 499b0104773SPascal Brand TEE_Result res; 500b0104773SPascal Brand 501b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 502b0104773SPascal Brand TEE_Panic(0); 503bf80076aSCedric Chaumont 504642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET)) 505bf80076aSCedric Chaumont TEE_Panic(0); 506bf80076aSCedric Chaumont 507642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 508642a1607SCedric Chaumont 509b0104773SPascal Brand if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 510b0104773SPascal Brand res = utee_hash_init(operation->state, NULL, 0); 511b0104773SPascal Brand if (res != TEE_SUCCESS) 512b0104773SPascal Brand TEE_Panic(res); 51305304565SCedric Chaumont operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 51405304565SCedric Chaumont } else { 515b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 516b0104773SPascal Brand } 51705304565SCedric Chaumont } 518b0104773SPascal Brand 519b0104773SPascal Brand TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation, 520b0104773SPascal Brand TEE_ObjectHandle key) 521b0104773SPascal Brand { 5227583c59eSCedric Chaumont TEE_Result res; 523b0104773SPascal Brand uint32_t key_size = 0; 524b0104773SPascal Brand TEE_ObjectInfo key_info; 525b0104773SPascal Brand 526a57c1e2eSCedric Chaumont if (operation == TEE_HANDLE_NULL) { 527a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 528a57c1e2eSCedric Chaumont goto out; 529a57c1e2eSCedric Chaumont } 530a57c1e2eSCedric Chaumont 531642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 532642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 533642a1607SCedric Chaumont goto out; 534642a1607SCedric Chaumont } 535642a1607SCedric Chaumont 536a57c1e2eSCedric Chaumont if (key == TEE_HANDLE_NULL) { 537a57c1e2eSCedric Chaumont /* Operation key cleared */ 538a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key1); 539a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 540a57c1e2eSCedric Chaumont goto out; 541a57c1e2eSCedric Chaumont } 542a57c1e2eSCedric Chaumont 543a57c1e2eSCedric Chaumont /* No key for digest operation */ 544a57c1e2eSCedric Chaumont if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 545a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 546a57c1e2eSCedric Chaumont goto out; 547a57c1e2eSCedric Chaumont } 548a57c1e2eSCedric Chaumont 549a57c1e2eSCedric Chaumont /* Two keys flag not expected (TEE_ALG_AES_XTS excluded) */ 550a57c1e2eSCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 551a57c1e2eSCedric Chaumont 0) { 552a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 553a57c1e2eSCedric Chaumont goto out; 554a57c1e2eSCedric Chaumont } 555a57c1e2eSCedric Chaumont 5567583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key, &key_info); 557a57c1e2eSCedric Chaumont /* Key is not a valid handle */ 5587583c59eSCedric Chaumont if (res != TEE_SUCCESS) 559a57c1e2eSCedric Chaumont goto out; 5607583c59eSCedric Chaumont 561b0104773SPascal Brand /* Supplied key has to meet required usage */ 562b0104773SPascal Brand if ((key_info.objectUsage & operation->info.requiredKeyUsage) != 563b0104773SPascal Brand operation->info.requiredKeyUsage) { 564a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 565a57c1e2eSCedric Chaumont goto out; 566b0104773SPascal Brand } 567b0104773SPascal Brand 568a57c1e2eSCedric Chaumont if (operation->info.maxKeySize < key_info.keySize) { 569a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 570a57c1e2eSCedric Chaumont goto out; 571a57c1e2eSCedric Chaumont } 572b0104773SPascal Brand 5737583c59eSCedric Chaumont key_size = key_info.keySize; 574b0104773SPascal Brand 575b0104773SPascal Brand TEE_ResetTransientObject(operation->key1); 576b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 577b0104773SPascal Brand 5787583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key1, key); 5797583c59eSCedric Chaumont if (res != TEE_SUCCESS) 580a57c1e2eSCedric Chaumont goto out; 5817583c59eSCedric Chaumont 582b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 583b0104773SPascal Brand 584b0104773SPascal Brand operation->info.keySize = key_size; 585b0104773SPascal Brand 5867583c59eSCedric Chaumont out: 587a57c1e2eSCedric Chaumont if (res != TEE_SUCCESS && 588a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 589a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 590b36311adSJerome Forissier TEE_Panic(res); 591a57c1e2eSCedric Chaumont 592a57c1e2eSCedric Chaumont return res; 593b0104773SPascal Brand } 594b0104773SPascal Brand 595b0104773SPascal Brand TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation, 596b0104773SPascal Brand TEE_ObjectHandle key1, TEE_ObjectHandle key2) 597b0104773SPascal Brand { 5987583c59eSCedric Chaumont TEE_Result res; 599b0104773SPascal Brand uint32_t key_size = 0; 600b0104773SPascal Brand TEE_ObjectInfo key_info1; 601b0104773SPascal Brand TEE_ObjectInfo key_info2; 602b0104773SPascal Brand 603a57c1e2eSCedric Chaumont if (operation == TEE_HANDLE_NULL) { 604a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 605a57c1e2eSCedric Chaumont goto out; 606a57c1e2eSCedric Chaumont } 607a57c1e2eSCedric Chaumont 608642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 609642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 610642a1607SCedric Chaumont goto out; 611642a1607SCedric Chaumont } 612642a1607SCedric Chaumont 613a57c1e2eSCedric Chaumont /* 614a57c1e2eSCedric Chaumont * Key1/Key2 and/or are not initialized and 615a57c1e2eSCedric Chaumont * Either both keys are NULL or both are not NULL 616a57c1e2eSCedric Chaumont */ 617a57c1e2eSCedric Chaumont if (key1 == TEE_HANDLE_NULL || key2 == TEE_HANDLE_NULL) { 618a57c1e2eSCedric Chaumont /* Clear operation key1 (if needed) */ 619a57c1e2eSCedric Chaumont if (key1 == TEE_HANDLE_NULL) 620a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key1); 621a57c1e2eSCedric Chaumont /* Clear operation key2 (if needed) */ 622a57c1e2eSCedric Chaumont if (key2 == TEE_HANDLE_NULL) 623a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key2); 624a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 625a57c1e2eSCedric Chaumont goto out; 626a57c1e2eSCedric Chaumont } 627a57c1e2eSCedric Chaumont 628a57c1e2eSCedric Chaumont /* No key for digest operation */ 629a57c1e2eSCedric Chaumont if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 630a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 631a57c1e2eSCedric Chaumont goto out; 632a57c1e2eSCedric Chaumont } 633a57c1e2eSCedric Chaumont 6345b385b3fSJerome Forissier /* Two keys flag expected (TEE_ALG_AES_XTS and TEE_ALG_SM2_KEP only) */ 635a57c1e2eSCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 636a57c1e2eSCedric Chaumont 0) { 637a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 638a57c1e2eSCedric Chaumont goto out; 639a57c1e2eSCedric Chaumont } 640a57c1e2eSCedric Chaumont 6417583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key1, &key_info1); 642a57c1e2eSCedric Chaumont /* Key1 is not a valid handle */ 6437583c59eSCedric Chaumont if (res != TEE_SUCCESS) 644a57c1e2eSCedric Chaumont goto out; 6457583c59eSCedric Chaumont 646b0104773SPascal Brand /* Supplied key has to meet required usage */ 647b0104773SPascal Brand if ((key_info1.objectUsage & operation->info. 648b0104773SPascal Brand requiredKeyUsage) != operation->info.requiredKeyUsage) { 649a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 650a57c1e2eSCedric Chaumont goto out; 651b0104773SPascal Brand } 652b0104773SPascal Brand 6537583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key2, &key_info2); 654a57c1e2eSCedric Chaumont /* Key2 is not a valid handle */ 6557583c59eSCedric Chaumont if (res != TEE_SUCCESS) { 6567583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) 6577583c59eSCedric Chaumont res = TEE_ERROR_CORRUPT_OBJECT_2; 658a57c1e2eSCedric Chaumont goto out; 6597583c59eSCedric Chaumont } 6607583c59eSCedric Chaumont 661b0104773SPascal Brand /* Supplied key has to meet required usage */ 662b0104773SPascal Brand if ((key_info2.objectUsage & operation->info. 663b0104773SPascal Brand requiredKeyUsage) != operation->info.requiredKeyUsage) { 664a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 665a57c1e2eSCedric Chaumont goto out; 666b0104773SPascal Brand } 667b0104773SPascal Brand 668b0104773SPascal Brand /* 6695b385b3fSJerome Forissier * All the multi key algorithm currently supported requires the keys to 6705b385b3fSJerome Forissier * be of equal size. 671b0104773SPascal Brand */ 6725b385b3fSJerome Forissier if (key_info1.keySize != key_info2.keySize) { 673a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 674a57c1e2eSCedric Chaumont goto out; 675b0104773SPascal Brand 676a57c1e2eSCedric Chaumont } 677a57c1e2eSCedric Chaumont 678a57c1e2eSCedric Chaumont if (operation->info.maxKeySize < key_info1.keySize) { 679a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 680a57c1e2eSCedric Chaumont goto out; 681a57c1e2eSCedric Chaumont } 682b0104773SPascal Brand 683b0104773SPascal Brand /* 684b0104773SPascal Brand * Odd that only the size of one key should be reported while 685b0104773SPascal Brand * size of two key are used when allocating the operation. 686b0104773SPascal Brand */ 6877583c59eSCedric Chaumont key_size = key_info1.keySize; 688b0104773SPascal Brand 689b0104773SPascal Brand TEE_ResetTransientObject(operation->key1); 690b0104773SPascal Brand TEE_ResetTransientObject(operation->key2); 691b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 692b0104773SPascal Brand 6937583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key1, key1); 6947583c59eSCedric Chaumont if (res != TEE_SUCCESS) 695a57c1e2eSCedric Chaumont goto out; 6967583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key2, key2); 6977583c59eSCedric Chaumont if (res != TEE_SUCCESS) { 6987583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) 6997583c59eSCedric Chaumont res = TEE_ERROR_CORRUPT_OBJECT_2; 700a57c1e2eSCedric Chaumont goto out; 7017583c59eSCedric Chaumont } 7027583c59eSCedric Chaumont 703b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 704b0104773SPascal Brand 705b0104773SPascal Brand operation->info.keySize = key_size; 706b0104773SPascal Brand 7077583c59eSCedric Chaumont out: 708a57c1e2eSCedric Chaumont if (res != TEE_SUCCESS && 709a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 710a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT_2 && 711a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE && 712a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE_2) 713b36311adSJerome Forissier TEE_Panic(res); 714a57c1e2eSCedric Chaumont 715a57c1e2eSCedric Chaumont return res; 716b0104773SPascal Brand } 717b0104773SPascal Brand 718b0104773SPascal Brand void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op) 719b0104773SPascal Brand { 720b0104773SPascal Brand TEE_Result res; 721b0104773SPascal Brand 722b0104773SPascal Brand if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL) 723b0104773SPascal Brand TEE_Panic(0); 724b0104773SPascal Brand if (dst_op->info.algorithm != src_op->info.algorithm) 725b0104773SPascal Brand TEE_Panic(0); 726b0104773SPascal Brand if (src_op->info.operationClass != TEE_OPERATION_DIGEST) { 727b0104773SPascal Brand TEE_ObjectHandle key1 = TEE_HANDLE_NULL; 728b0104773SPascal Brand TEE_ObjectHandle key2 = TEE_HANDLE_NULL; 729b0104773SPascal Brand 730b0104773SPascal Brand if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) { 731b0104773SPascal Brand key1 = src_op->key1; 732b0104773SPascal Brand key2 = src_op->key2; 733b0104773SPascal Brand } 734b0104773SPascal Brand 735b0104773SPascal Brand if ((src_op->info.handleState & 736b0104773SPascal Brand TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) { 737b0104773SPascal Brand TEE_SetOperationKey(dst_op, key1); 738b0104773SPascal Brand } else { 739b0104773SPascal Brand TEE_SetOperationKey2(dst_op, key1, key2); 740b0104773SPascal Brand } 741b0104773SPascal Brand } 742b0104773SPascal Brand dst_op->info.handleState = src_op->info.handleState; 743b0104773SPascal Brand dst_op->info.keySize = src_op->info.keySize; 744642a1607SCedric Chaumont dst_op->operationState = src_op->operationState; 745b0104773SPascal Brand 746b0104773SPascal Brand if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks || 747b0104773SPascal Brand dst_op->block_size != src_op->block_size) 748b0104773SPascal Brand TEE_Panic(0); 749b0104773SPascal Brand 750b0104773SPascal Brand if (dst_op->buffer != NULL) { 751b0104773SPascal Brand if (src_op->buffer == NULL) 752b0104773SPascal Brand TEE_Panic(0); 753b0104773SPascal Brand 754b0104773SPascal Brand memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs); 755b0104773SPascal Brand dst_op->buffer_offs = src_op->buffer_offs; 756b0104773SPascal Brand } else if (src_op->buffer != NULL) { 757b0104773SPascal Brand TEE_Panic(0); 758b0104773SPascal Brand } 759b0104773SPascal Brand 760b0104773SPascal Brand res = utee_cryp_state_copy(dst_op->state, src_op->state); 761b0104773SPascal Brand if (res != TEE_SUCCESS) 762b0104773SPascal Brand TEE_Panic(res); 763b0104773SPascal Brand } 764b0104773SPascal Brand 765b0104773SPascal Brand /* Cryptographic Operations API - Message Digest Functions */ 766b0104773SPascal Brand 7678f07fe6fSJerome Forissier static void init_hash_operation(TEE_OperationHandle operation, const void *IV, 7686d15db08SJerome Forissier uint32_t IVLen) 7696d15db08SJerome Forissier { 7706d15db08SJerome Forissier TEE_Result res; 7716d15db08SJerome Forissier 7726d15db08SJerome Forissier /* 7736d15db08SJerome Forissier * Note : IV and IVLen are never used in current implementation 7746d15db08SJerome Forissier * This is why coherent values of IV and IVLen are not checked 7756d15db08SJerome Forissier */ 7766d15db08SJerome Forissier res = utee_hash_init(operation->state, IV, IVLen); 7776d15db08SJerome Forissier if (res != TEE_SUCCESS) 7786d15db08SJerome Forissier TEE_Panic(res); 7796d15db08SJerome Forissier operation->buffer_offs = 0; 7806d15db08SJerome Forissier operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 7816d15db08SJerome Forissier } 7826d15db08SJerome Forissier 783b0104773SPascal Brand void TEE_DigestUpdate(TEE_OperationHandle operation, 7848f07fe6fSJerome Forissier const void *chunk, uint32_t chunkSize) 785b0104773SPascal Brand { 78673d6c3baSJoakim Bech TEE_Result res = TEE_ERROR_GENERIC; 787b0104773SPascal Brand 78873d6c3baSJoakim Bech if (operation == TEE_HANDLE_NULL || 78973d6c3baSJoakim Bech operation->info.operationClass != TEE_OPERATION_DIGEST) 790b0104773SPascal Brand TEE_Panic(0); 79173d6c3baSJoakim Bech 792642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 793642a1607SCedric Chaumont 794b0104773SPascal Brand res = utee_hash_update(operation->state, chunk, chunkSize); 795b0104773SPascal Brand if (res != TEE_SUCCESS) 796b0104773SPascal Brand TEE_Panic(res); 797b0104773SPascal Brand } 798b0104773SPascal Brand 7998f07fe6fSJerome Forissier TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void *chunk, 80079a3c601SCedric Chaumont uint32_t chunkLen, void *hash, uint32_t *hashLen) 801b0104773SPascal Brand { 80287c2f6b6SCedric Chaumont TEE_Result res; 803e86f1266SJens Wiklander uint64_t hl; 80487c2f6b6SCedric Chaumont 80587c2f6b6SCedric Chaumont if ((operation == TEE_HANDLE_NULL) || 80687c2f6b6SCedric Chaumont (!chunk && chunkLen) || 80787c2f6b6SCedric Chaumont !hash || 80887c2f6b6SCedric Chaumont !hashLen || 80987c2f6b6SCedric Chaumont (operation->info.operationClass != TEE_OPERATION_DIGEST)) { 81087c2f6b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 81187c2f6b6SCedric Chaumont goto out; 81287c2f6b6SCedric Chaumont } 81387c2f6b6SCedric Chaumont 814e86f1266SJens Wiklander hl = *hashLen; 815e86f1266SJens Wiklander res = utee_hash_final(operation->state, chunk, chunkLen, hash, &hl); 816e86f1266SJens Wiklander *hashLen = hl; 8176d15db08SJerome Forissier if (res != TEE_SUCCESS) 8186d15db08SJerome Forissier goto out; 8196d15db08SJerome Forissier 8206d15db08SJerome Forissier /* Reset operation state */ 8216d15db08SJerome Forissier init_hash_operation(operation, NULL, 0); 82287c2f6b6SCedric Chaumont 823642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 824642a1607SCedric Chaumont 82587c2f6b6SCedric Chaumont out: 82687c2f6b6SCedric Chaumont if (res != TEE_SUCCESS && 82787c2f6b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 828b36311adSJerome Forissier TEE_Panic(res); 82973d6c3baSJoakim Bech 83087c2f6b6SCedric Chaumont return res; 831b0104773SPascal Brand } 832b0104773SPascal Brand 833b0104773SPascal Brand /* Cryptographic Operations API - Symmetric Cipher Functions */ 834b0104773SPascal Brand 8358f07fe6fSJerome Forissier void TEE_CipherInit(TEE_OperationHandle operation, const void *IV, 8368f07fe6fSJerome Forissier uint32_t IVLen) 837b0104773SPascal Brand { 838b0104773SPascal Brand TEE_Result res; 839b0104773SPascal Brand 840b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 841b0104773SPascal Brand TEE_Panic(0); 842642a1607SCedric Chaumont 843b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_CIPHER) 844b0104773SPascal Brand TEE_Panic(0); 845642a1607SCedric Chaumont 846642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 847642a1607SCedric Chaumont !(operation->key1)) 848642a1607SCedric Chaumont TEE_Panic(0); 849642a1607SCedric Chaumont 850642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 851642a1607SCedric Chaumont TEE_ResetOperation(operation); 852642a1607SCedric Chaumont 853642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 854642a1607SCedric Chaumont 855b0104773SPascal Brand res = utee_cipher_init(operation->state, IV, IVLen); 856b0104773SPascal Brand if (res != TEE_SUCCESS) 857b0104773SPascal Brand TEE_Panic(res); 858642a1607SCedric Chaumont 859b0104773SPascal Brand operation->buffer_offs = 0; 860b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 861b0104773SPascal Brand } 862b0104773SPascal Brand 863b0104773SPascal Brand static TEE_Result tee_buffer_update( 864b0104773SPascal Brand TEE_OperationHandle op, 865e86f1266SJens Wiklander TEE_Result(*update_func)(unsigned long state, const void *src, 866e86f1266SJens Wiklander size_t slen, void *dst, uint64_t *dlen), 867b0104773SPascal Brand const void *src_data, size_t src_len, 868e86f1266SJens Wiklander void *dest_data, uint64_t *dest_len) 869b0104773SPascal Brand { 870b0104773SPascal Brand TEE_Result res; 871b0104773SPascal Brand const uint8_t *src = src_data; 872b0104773SPascal Brand size_t slen = src_len; 873b0104773SPascal Brand uint8_t *dst = dest_data; 874b0104773SPascal Brand size_t dlen = *dest_len; 875b0104773SPascal Brand size_t acc_dlen = 0; 876e86f1266SJens Wiklander uint64_t tmp_dlen; 877b0104773SPascal Brand size_t l; 878b0104773SPascal Brand size_t buffer_size; 879d3588802SPascal Brand size_t buffer_left; 880b0104773SPascal Brand 881090268f5SJens Wiklander if (!src) { 882090268f5SJens Wiklander if (slen) 883090268f5SJens Wiklander TEE_Panic(0); 884090268f5SJens Wiklander goto out; 885090268f5SJens Wiklander } 886090268f5SJens Wiklander 887d3588802SPascal Brand if (op->buffer_two_blocks) { 888b0104773SPascal Brand buffer_size = op->block_size * 2; 889d3588802SPascal Brand buffer_left = 1; 890d3588802SPascal Brand } else { 891b0104773SPascal Brand buffer_size = op->block_size; 892d3588802SPascal Brand buffer_left = 0; 893d3588802SPascal Brand } 894b0104773SPascal Brand 895b0104773SPascal Brand if (op->buffer_offs > 0) { 896b0104773SPascal Brand /* Fill up complete block */ 897b0104773SPascal Brand if (op->buffer_offs < op->block_size) 898b0104773SPascal Brand l = MIN(slen, op->block_size - op->buffer_offs); 899b0104773SPascal Brand else 900b0104773SPascal Brand l = MIN(slen, buffer_size - op->buffer_offs); 901b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, l); 902b0104773SPascal Brand op->buffer_offs += l; 903b0104773SPascal Brand src += l; 904b0104773SPascal Brand slen -= l; 905b0104773SPascal Brand if ((op->buffer_offs % op->block_size) != 0) 906b0104773SPascal Brand goto out; /* Nothing left to do */ 907b0104773SPascal Brand } 908b0104773SPascal Brand 909b0104773SPascal Brand /* If we can feed from buffer */ 910d3588802SPascal Brand if ((op->buffer_offs > 0) && 911d3588802SPascal Brand ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) { 9122ff3fdbbSPascal Brand l = ROUNDUP(op->buffer_offs + slen - buffer_size, 913b0104773SPascal Brand op->block_size); 914b0104773SPascal Brand l = MIN(op->buffer_offs, l); 915b0104773SPascal Brand tmp_dlen = dlen; 916b0104773SPascal Brand res = update_func(op->state, op->buffer, l, dst, &tmp_dlen); 917b0104773SPascal Brand if (res != TEE_SUCCESS) 918b0104773SPascal Brand TEE_Panic(res); 919b0104773SPascal Brand dst += tmp_dlen; 920b0104773SPascal Brand dlen -= tmp_dlen; 921b0104773SPascal Brand acc_dlen += tmp_dlen; 922b0104773SPascal Brand op->buffer_offs -= l; 923b0104773SPascal Brand if (op->buffer_offs > 0) { 924b0104773SPascal Brand /* 925b0104773SPascal Brand * Slen is small enough to be contained in rest buffer. 926b0104773SPascal Brand */ 927b0104773SPascal Brand memcpy(op->buffer, op->buffer + l, buffer_size - l); 928b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 929b0104773SPascal Brand op->buffer_offs += slen; 930b0104773SPascal Brand goto out; /* Nothing left to do */ 931b0104773SPascal Brand } 932b0104773SPascal Brand } 933b0104773SPascal Brand 934d3588802SPascal Brand if (slen >= (buffer_size + buffer_left)) { 935b0104773SPascal Brand /* Buffer is empty, feed as much as possible from src */ 936bf7a587fSJerome Forissier if (op->info.algorithm == TEE_ALG_AES_CTS) 937b1ecda78SJerome Forissier l = ROUNDUP(slen - buffer_size, op->block_size); 938bf7a587fSJerome Forissier else 939bf7a587fSJerome Forissier l = ROUNDUP(slen - buffer_size + 1, op->block_size); 940b0104773SPascal Brand 941b0104773SPascal Brand tmp_dlen = dlen; 942b0104773SPascal Brand res = update_func(op->state, src, l, dst, &tmp_dlen); 943b0104773SPascal Brand if (res != TEE_SUCCESS) 944b0104773SPascal Brand TEE_Panic(res); 945b0104773SPascal Brand src += l; 946b0104773SPascal Brand slen -= l; 947b0104773SPascal Brand dst += tmp_dlen; 948b0104773SPascal Brand dlen -= tmp_dlen; 949b0104773SPascal Brand acc_dlen += tmp_dlen; 950b0104773SPascal Brand } 951b0104773SPascal Brand 952b0104773SPascal Brand /* Slen is small enough to be contained in buffer. */ 953b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 954b0104773SPascal Brand op->buffer_offs += slen; 955b0104773SPascal Brand 956b0104773SPascal Brand out: 957b0104773SPascal Brand *dest_len = acc_dlen; 958b0104773SPascal Brand return TEE_SUCCESS; 959b0104773SPascal Brand } 960b0104773SPascal Brand 9618f07fe6fSJerome Forissier TEE_Result TEE_CipherUpdate(TEE_OperationHandle operation, const void *srcData, 96279a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 963b0104773SPascal Brand { 964dea1f2b6SCedric Chaumont TEE_Result res; 965b0104773SPascal Brand size_t req_dlen; 966e86f1266SJens Wiklander uint64_t dl; 967b0104773SPascal Brand 968642a1607SCedric Chaumont if (operation == TEE_HANDLE_NULL || 969dea1f2b6SCedric Chaumont (srcData == NULL && srcLen != 0) || 970dea1f2b6SCedric Chaumont destLen == NULL || 971dea1f2b6SCedric Chaumont (destData == NULL && *destLen != 0)) { 972dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 973dea1f2b6SCedric Chaumont goto out; 974dea1f2b6SCedric Chaumont } 975dea1f2b6SCedric Chaumont 976642a1607SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 977dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 978dea1f2b6SCedric Chaumont goto out; 979dea1f2b6SCedric Chaumont } 980dea1f2b6SCedric Chaumont 981642a1607SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 982642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 983642a1607SCedric Chaumont goto out; 984642a1607SCedric Chaumont } 985642a1607SCedric Chaumont 986642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 987dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 988dea1f2b6SCedric Chaumont goto out; 989dea1f2b6SCedric Chaumont } 990b0104773SPascal Brand 991e32c5ddfSJerome Forissier if (!srcData && !srcLen) { 992090268f5SJens Wiklander *destLen = 0; 993e32c5ddfSJerome Forissier res = TEE_SUCCESS; 994e32c5ddfSJerome Forissier goto out; 995e32c5ddfSJerome Forissier } 996e32c5ddfSJerome Forissier 997b0104773SPascal Brand /* Calculate required dlen */ 99857aabac5SBogdan Liulko if (operation->block_size > 1) { 99957aabac5SBogdan Liulko req_dlen = ((operation->buffer_offs + srcLen) / 100057aabac5SBogdan Liulko operation->block_size) * operation->block_size; 100157aabac5SBogdan Liulko } else { 100257aabac5SBogdan Liulko req_dlen = srcLen; 100357aabac5SBogdan Liulko } 1004642a1607SCedric Chaumont if (operation->buffer_two_blocks) { 1005642a1607SCedric Chaumont if (req_dlen > operation->block_size * 2) 1006642a1607SCedric Chaumont req_dlen -= operation->block_size * 2; 1007b0104773SPascal Brand else 1008b0104773SPascal Brand req_dlen = 0; 1009b0104773SPascal Brand } 1010b0104773SPascal Brand /* 1011b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1012b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1013b0104773SPascal Brand * can't restore sync with this API. 1014b0104773SPascal Brand */ 1015b0104773SPascal Brand if (*destLen < req_dlen) { 1016b0104773SPascal Brand *destLen = req_dlen; 1017dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1018dea1f2b6SCedric Chaumont goto out; 1019b0104773SPascal Brand } 1020b0104773SPascal Brand 1021e86f1266SJens Wiklander dl = *destLen; 102257aabac5SBogdan Liulko if (operation->block_size > 1) { 102357aabac5SBogdan Liulko res = tee_buffer_update(operation, utee_cipher_update, srcData, 102457aabac5SBogdan Liulko srcLen, destData, &dl); 102557aabac5SBogdan Liulko } else { 102657aabac5SBogdan Liulko if (srcLen > 0) { 102757aabac5SBogdan Liulko res = utee_cipher_update(operation->state, srcData, 102857aabac5SBogdan Liulko srcLen, destData, &dl); 102957aabac5SBogdan Liulko } else { 103057aabac5SBogdan Liulko res = TEE_SUCCESS; 103157aabac5SBogdan Liulko dl = 0; 103257aabac5SBogdan Liulko } 103357aabac5SBogdan Liulko } 1034e86f1266SJens Wiklander *destLen = dl; 1035b0104773SPascal Brand 1036dea1f2b6SCedric Chaumont out: 1037dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 1038dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1039b36311adSJerome Forissier TEE_Panic(res); 1040dea1f2b6SCedric Chaumont 1041dea1f2b6SCedric Chaumont return res; 1042b0104773SPascal Brand } 1043b0104773SPascal Brand 1044642a1607SCedric Chaumont TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation, 10458f07fe6fSJerome Forissier const void *srcData, uint32_t srcLen, 10468f07fe6fSJerome Forissier void *destData, uint32_t *destLen) 1047b0104773SPascal Brand { 1048b0104773SPascal Brand TEE_Result res; 1049b0104773SPascal Brand uint8_t *dst = destData; 1050b0104773SPascal Brand size_t acc_dlen = 0; 1051e86f1266SJens Wiklander uint64_t tmp_dlen; 1052b0104773SPascal Brand size_t req_dlen; 1053b0104773SPascal Brand 1054642a1607SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1055dea1f2b6SCedric Chaumont (srcData == NULL && srcLen != 0) || 1056dea1f2b6SCedric Chaumont destLen == NULL || 1057dea1f2b6SCedric Chaumont (destData == NULL && *destLen != 0)) { 1058dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1059dea1f2b6SCedric Chaumont goto out; 1060dea1f2b6SCedric Chaumont } 1061dea1f2b6SCedric Chaumont 1062642a1607SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 1063dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1064dea1f2b6SCedric Chaumont goto out; 1065dea1f2b6SCedric Chaumont } 1066dea1f2b6SCedric Chaumont 1067642a1607SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1068642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1069642a1607SCedric Chaumont goto out; 1070642a1607SCedric Chaumont } 1071642a1607SCedric Chaumont 1072642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1073dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1074dea1f2b6SCedric Chaumont goto out; 1075dea1f2b6SCedric Chaumont } 1076b0104773SPascal Brand 1077b0104773SPascal Brand /* 1078b0104773SPascal Brand * Check that the final block doesn't require padding for those 1079b0104773SPascal Brand * algorithms that requires client to supply padding. 1080b0104773SPascal Brand */ 1081642a1607SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD || 1082642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD || 1083642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD || 1084642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD || 1085642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || 1086ade6f848SJerome Forissier operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD || 1087ade6f848SJerome Forissier operation->info.algorithm == TEE_ALG_SM4_ECB_NOPAD || 1088ade6f848SJerome Forissier operation->info.algorithm == TEE_ALG_SM4_CBC_NOPAD) { 1089642a1607SCedric Chaumont if (((operation->buffer_offs + srcLen) % operation->block_size) 1090642a1607SCedric Chaumont != 0) { 1091dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1092dea1f2b6SCedric Chaumont goto out; 1093dea1f2b6SCedric Chaumont } 1094b0104773SPascal Brand } 1095b0104773SPascal Brand 1096b0104773SPascal Brand /* 1097b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1098b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1099b0104773SPascal Brand * can't restore sync with this API. 1100b0104773SPascal Brand */ 110157aabac5SBogdan Liulko if (operation->block_size > 1) { 1102642a1607SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 110357aabac5SBogdan Liulko } else { 110457aabac5SBogdan Liulko req_dlen = srcLen; 110557aabac5SBogdan Liulko } 1106b0104773SPascal Brand if (*destLen < req_dlen) { 1107b0104773SPascal Brand *destLen = req_dlen; 1108dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1109dea1f2b6SCedric Chaumont goto out; 1110b0104773SPascal Brand } 1111b0104773SPascal Brand 1112b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 111357aabac5SBogdan Liulko if (operation->block_size > 1) { 111457aabac5SBogdan Liulko res = tee_buffer_update(operation, utee_cipher_update, 111557aabac5SBogdan Liulko srcData, srcLen, dst, &tmp_dlen); 1116dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS) 1117dea1f2b6SCedric Chaumont goto out; 1118dea1f2b6SCedric Chaumont 1119b0104773SPascal Brand dst += tmp_dlen; 1120b0104773SPascal Brand acc_dlen += tmp_dlen; 1121b0104773SPascal Brand 1122b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1123642a1607SCedric Chaumont res = utee_cipher_final(operation->state, operation->buffer, 1124642a1607SCedric Chaumont operation->buffer_offs, dst, &tmp_dlen); 112557aabac5SBogdan Liulko } else { 112657aabac5SBogdan Liulko res = utee_cipher_final(operation->state, srcData, 112757aabac5SBogdan Liulko srcLen, dst, &tmp_dlen); 112857aabac5SBogdan Liulko } 1129b0104773SPascal Brand if (res != TEE_SUCCESS) 1130dea1f2b6SCedric Chaumont goto out; 1131dea1f2b6SCedric Chaumont 1132b0104773SPascal Brand acc_dlen += tmp_dlen; 1133b0104773SPascal Brand *destLen = acc_dlen; 1134dea1f2b6SCedric Chaumont 1135642a1607SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1136642a1607SCedric Chaumont 1137642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1138642a1607SCedric Chaumont 1139dea1f2b6SCedric Chaumont out: 1140dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 1141dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1142b36311adSJerome Forissier TEE_Panic(res); 1143dea1f2b6SCedric Chaumont 1144dea1f2b6SCedric Chaumont return res; 1145b0104773SPascal Brand } 1146b0104773SPascal Brand 1147b0104773SPascal Brand /* Cryptographic Operations API - MAC Functions */ 1148b0104773SPascal Brand 11498f07fe6fSJerome Forissier void TEE_MACInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen) 1150b0104773SPascal Brand { 1151b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 1152b0104773SPascal Brand TEE_Panic(0); 1153642a1607SCedric Chaumont 1154b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_MAC) 1155b0104773SPascal Brand TEE_Panic(0); 1156642a1607SCedric Chaumont 1157642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 1158642a1607SCedric Chaumont !(operation->key1)) 1159642a1607SCedric Chaumont TEE_Panic(0); 1160642a1607SCedric Chaumont 1161642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 1162642a1607SCedric Chaumont TEE_ResetOperation(operation); 1163642a1607SCedric Chaumont 1164642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1165642a1607SCedric Chaumont 11666d15db08SJerome Forissier init_hash_operation(operation, IV, IVLen); 1167b0104773SPascal Brand } 1168b0104773SPascal Brand 11698f07fe6fSJerome Forissier void TEE_MACUpdate(TEE_OperationHandle operation, const void *chunk, 117028e0efc6SCedric Chaumont uint32_t chunkSize) 1171b0104773SPascal Brand { 1172b0104773SPascal Brand TEE_Result res; 1173b0104773SPascal Brand 117428e0efc6SCedric Chaumont if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0)) 1175b0104773SPascal Brand TEE_Panic(0); 1176642a1607SCedric Chaumont 117728e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) 1178b0104773SPascal Brand TEE_Panic(0); 1179642a1607SCedric Chaumont 118028e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1181b0104773SPascal Brand TEE_Panic(0); 1182b0104773SPascal Brand 1183642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) 1184642a1607SCedric Chaumont TEE_Panic(0); 1185642a1607SCedric Chaumont 118628e0efc6SCedric Chaumont res = utee_hash_update(operation->state, chunk, chunkSize); 1187b0104773SPascal Brand if (res != TEE_SUCCESS) 1188b0104773SPascal Brand TEE_Panic(res); 1189b0104773SPascal Brand } 1190b0104773SPascal Brand 119128e0efc6SCedric Chaumont TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation, 11928f07fe6fSJerome Forissier const void *message, uint32_t messageLen, 119379a3c601SCedric Chaumont void *mac, uint32_t *macLen) 1194b0104773SPascal Brand { 1195b0104773SPascal Brand TEE_Result res; 1196e86f1266SJens Wiklander uint64_t ml; 1197b0104773SPascal Brand 119828e0efc6SCedric Chaumont if (operation == TEE_HANDLE_NULL || 119928e0efc6SCedric Chaumont (message == NULL && messageLen != 0) || 120028e0efc6SCedric Chaumont mac == NULL || 120128e0efc6SCedric Chaumont macLen == NULL) { 120228e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 120328e0efc6SCedric Chaumont goto out; 120428e0efc6SCedric Chaumont } 1205b0104773SPascal Brand 120628e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 120728e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 120828e0efc6SCedric Chaumont goto out; 120928e0efc6SCedric Chaumont } 121028e0efc6SCedric Chaumont 121128e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 121228e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 121328e0efc6SCedric Chaumont goto out; 121428e0efc6SCedric Chaumont } 121528e0efc6SCedric Chaumont 1216642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1217642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1218642a1607SCedric Chaumont goto out; 1219642a1607SCedric Chaumont } 1220642a1607SCedric Chaumont 1221e86f1266SJens Wiklander ml = *macLen; 1222e86f1266SJens Wiklander res = utee_hash_final(operation->state, message, messageLen, mac, &ml); 1223e86f1266SJens Wiklander *macLen = ml; 122428e0efc6SCedric Chaumont if (res != TEE_SUCCESS) 122528e0efc6SCedric Chaumont goto out; 122628e0efc6SCedric Chaumont 122728e0efc6SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 122828e0efc6SCedric Chaumont 1229642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1230642a1607SCedric Chaumont 123128e0efc6SCedric Chaumont out: 123228e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 123328e0efc6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 123428e0efc6SCedric Chaumont TEE_Panic(res); 123528e0efc6SCedric Chaumont 1236b0104773SPascal Brand return res; 1237b0104773SPascal Brand } 1238b0104773SPascal Brand 1239b0104773SPascal Brand TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation, 12408f07fe6fSJerome Forissier const void *message, uint32_t messageLen, 12418f07fe6fSJerome Forissier const void *mac, uint32_t macLen) 1242b0104773SPascal Brand { 1243b0104773SPascal Brand TEE_Result res; 1244b0104773SPascal Brand uint8_t computed_mac[TEE_MAX_HASH_SIZE]; 12457f74c64aSPascal Brand uint32_t computed_mac_size = TEE_MAX_HASH_SIZE; 1246b0104773SPascal Brand 124728e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 124828e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 124928e0efc6SCedric Chaumont goto out; 125028e0efc6SCedric Chaumont } 125128e0efc6SCedric Chaumont 125228e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 125328e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 125428e0efc6SCedric Chaumont goto out; 125528e0efc6SCedric Chaumont } 125628e0efc6SCedric Chaumont 1257642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1258642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1259642a1607SCedric Chaumont goto out; 1260642a1607SCedric Chaumont } 1261642a1607SCedric Chaumont 1262b0104773SPascal Brand res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac, 1263b0104773SPascal Brand &computed_mac_size); 1264b0104773SPascal Brand if (res != TEE_SUCCESS) 126528e0efc6SCedric Chaumont goto out; 126628e0efc6SCedric Chaumont 126728e0efc6SCedric Chaumont if (computed_mac_size != macLen) { 126828e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 126928e0efc6SCedric Chaumont goto out; 127028e0efc6SCedric Chaumont } 127128e0efc6SCedric Chaumont 127248e10604SJerome Forissier if (consttime_memcmp(mac, computed_mac, computed_mac_size) != 0) { 127328e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 127428e0efc6SCedric Chaumont goto out; 127528e0efc6SCedric Chaumont } 127628e0efc6SCedric Chaumont 1277642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1278642a1607SCedric Chaumont 127928e0efc6SCedric Chaumont out: 128028e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 128128e0efc6SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 128228e0efc6SCedric Chaumont TEE_Panic(res); 128328e0efc6SCedric Chaumont 1284b0104773SPascal Brand return res; 1285b0104773SPascal Brand } 1286b0104773SPascal Brand 1287b0104773SPascal Brand /* Cryptographic Operations API - Authenticated Encryption Functions */ 1288b0104773SPascal Brand 12898f07fe6fSJerome Forissier TEE_Result TEE_AEInit(TEE_OperationHandle operation, const void *nonce, 129079a3c601SCedric Chaumont uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen, 1291b0104773SPascal Brand uint32_t payloadLen) 1292b0104773SPascal Brand { 1293b0104773SPascal Brand TEE_Result res; 1294b0104773SPascal Brand 1295b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || nonce == NULL) { 1296b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1297b5816c88SCedric Chaumont goto out; 1298b5816c88SCedric Chaumont } 1299b5816c88SCedric Chaumont 1300b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1301b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1302b5816c88SCedric Chaumont goto out; 1303b5816c88SCedric Chaumont } 1304b0104773SPascal Brand 1305642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 1306642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1307642a1607SCedric Chaumont goto out; 1308642a1607SCedric Chaumont } 1309642a1607SCedric Chaumont 1310b0104773SPascal Brand /* 1311b0104773SPascal Brand * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core 1312b0104773SPascal Brand * in the implementation. But AES-GCM spec doesn't specify the tag len 1313b0104773SPascal Brand * according to the same principle so we have to check here instead to 1314b0104773SPascal Brand * be GP compliant. 1315b0104773SPascal Brand */ 1316b5816c88SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_GCM) { 1317b0104773SPascal Brand /* 1318b0104773SPascal Brand * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96 1319b0104773SPascal Brand */ 1320b5816c88SCedric Chaumont if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) { 1321b5816c88SCedric Chaumont res = TEE_ERROR_NOT_SUPPORTED; 1322b5816c88SCedric Chaumont goto out; 1323b5816c88SCedric Chaumont } 1324b0104773SPascal Brand } 1325b0104773SPascal Brand 1326b5816c88SCedric Chaumont res = utee_authenc_init(operation->state, nonce, nonceLen, 1327b5816c88SCedric Chaumont tagLen / 8, AADLen, payloadLen); 1328b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1329b5816c88SCedric Chaumont goto out; 1330b5816c88SCedric Chaumont 1331b5816c88SCedric Chaumont operation->ae_tag_len = tagLen / 8; 1332*2e5e6460SAlbert Schwarzkopf operation->info.digestLength = operation->ae_tag_len; 1333b5816c88SCedric Chaumont operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 1334b5816c88SCedric Chaumont 1335b5816c88SCedric Chaumont out: 1336b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1337b5816c88SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 1338b0104773SPascal Brand TEE_Panic(res); 1339b5816c88SCedric Chaumont 1340b0104773SPascal Brand return res; 1341b0104773SPascal Brand } 1342b0104773SPascal Brand 13438f07fe6fSJerome Forissier void TEE_AEUpdateAAD(TEE_OperationHandle operation, const void *AADdata, 134479a3c601SCedric Chaumont uint32_t AADdataLen) 1345b0104773SPascal Brand { 1346b0104773SPascal Brand TEE_Result res; 1347b0104773SPascal Brand 1348b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1349b5816c88SCedric Chaumont (AADdata == NULL && AADdataLen != 0)) 1350b0104773SPascal Brand TEE_Panic(0); 1351642a1607SCedric Chaumont 1352b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) 1353b0104773SPascal Brand TEE_Panic(0); 1354642a1607SCedric Chaumont 1355b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1356b0104773SPascal Brand TEE_Panic(0); 1357b0104773SPascal Brand 1358b5816c88SCedric Chaumont res = utee_authenc_update_aad(operation->state, AADdata, AADdataLen); 1359642a1607SCedric Chaumont 1360642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1361642a1607SCedric Chaumont 1362b0104773SPascal Brand if (res != TEE_SUCCESS) 1363b0104773SPascal Brand TEE_Panic(res); 1364b0104773SPascal Brand } 1365b0104773SPascal Brand 13668f07fe6fSJerome Forissier TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, const void *srcData, 136779a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 1368b0104773SPascal Brand { 1369b5816c88SCedric Chaumont TEE_Result res; 1370b0104773SPascal Brand size_t req_dlen; 1371e86f1266SJens Wiklander uint64_t dl; 1372b0104773SPascal Brand 1373b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1374b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1375b5816c88SCedric Chaumont destLen == NULL || 1376b5816c88SCedric Chaumont (destData == NULL && *destLen != 0)) { 1377b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1378b5816c88SCedric Chaumont goto out; 1379b5816c88SCedric Chaumont } 1380b5816c88SCedric Chaumont 1381b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1382b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1383b5816c88SCedric Chaumont goto out; 1384b5816c88SCedric Chaumont } 1385b5816c88SCedric Chaumont 1386b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1387b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1388b5816c88SCedric Chaumont goto out; 1389b5816c88SCedric Chaumont } 1390b0104773SPascal Brand 1391827308b8SJerome Forissier if (!srcData && !srcLen) { 1392090268f5SJens Wiklander *destLen = 0; 1393827308b8SJerome Forissier res = TEE_SUCCESS; 1394827308b8SJerome Forissier goto out; 1395827308b8SJerome Forissier } 1396827308b8SJerome Forissier 1397b0104773SPascal Brand /* 1398b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1399b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1400b0104773SPascal Brand * can't restore sync with this API. 1401b0104773SPascal Brand */ 1402afc0c182SBogdan Liulko if (operation->block_size > 1) { 1403b5816c88SCedric Chaumont req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen, 1404b5816c88SCedric Chaumont operation->block_size); 1405afc0c182SBogdan Liulko } else { 1406afc0c182SBogdan Liulko req_dlen = srcLen; 1407afc0c182SBogdan Liulko } 1408afc0c182SBogdan Liulko 1409b0104773SPascal Brand if (*destLen < req_dlen) { 1410b0104773SPascal Brand *destLen = req_dlen; 1411b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1412b5816c88SCedric Chaumont goto out; 1413b0104773SPascal Brand } 1414b0104773SPascal Brand 1415e86f1266SJens Wiklander dl = *destLen; 1416afc0c182SBogdan Liulko if (operation->block_size > 1) { 1417afc0c182SBogdan Liulko res = tee_buffer_update(operation, utee_authenc_update_payload, 1418afc0c182SBogdan Liulko srcData, srcLen, destData, &dl); 1419afc0c182SBogdan Liulko } else { 1420afc0c182SBogdan Liulko if (srcLen > 0) { 1421afc0c182SBogdan Liulko res = utee_authenc_update_payload(operation->state, 1422afc0c182SBogdan Liulko srcData, srcLen, 1423afc0c182SBogdan Liulko destData, &dl); 1424afc0c182SBogdan Liulko } else { 1425afc0c182SBogdan Liulko dl = 0; 1426afc0c182SBogdan Liulko res = TEE_SUCCESS; 1427afc0c182SBogdan Liulko } 1428afc0c182SBogdan Liulko } 1429afc0c182SBogdan Liulko if (res != TEE_SUCCESS) 1430afc0c182SBogdan Liulko goto out; 1431afc0c182SBogdan Liulko 1432e86f1266SJens Wiklander *destLen = dl; 1433b0104773SPascal Brand 1434642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1435642a1607SCedric Chaumont 1436b5816c88SCedric Chaumont out: 1437b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1438b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1439b5816c88SCedric Chaumont TEE_Panic(res); 1440b5816c88SCedric Chaumont 1441b5816c88SCedric Chaumont return res; 1442b0104773SPascal Brand } 1443b0104773SPascal Brand 1444b5816c88SCedric Chaumont TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation, 14458f07fe6fSJerome Forissier const void *srcData, uint32_t srcLen, 144679a3c601SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 144779a3c601SCedric Chaumont uint32_t *tagLen) 1448b0104773SPascal Brand { 1449b0104773SPascal Brand TEE_Result res; 1450b0104773SPascal Brand uint8_t *dst = destData; 1451b0104773SPascal Brand size_t acc_dlen = 0; 1452e86f1266SJens Wiklander uint64_t tmp_dlen; 1453b0104773SPascal Brand size_t req_dlen; 1454e86f1266SJens Wiklander uint64_t tl; 1455b0104773SPascal Brand 1456b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1457b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1458b5816c88SCedric Chaumont destLen == NULL || 1459b5816c88SCedric Chaumont (destData == NULL && *destLen != 0) || 1460b5816c88SCedric Chaumont tag == NULL || tagLen == NULL) { 1461b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1462b5816c88SCedric Chaumont goto out; 1463b5816c88SCedric Chaumont } 1464b5816c88SCedric Chaumont 1465b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1466b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1467b5816c88SCedric Chaumont goto out; 1468b5816c88SCedric Chaumont } 1469b5816c88SCedric Chaumont 1470b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1471b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1472b5816c88SCedric Chaumont goto out; 1473b5816c88SCedric Chaumont } 1474b0104773SPascal Brand 1475b0104773SPascal Brand /* 1476b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1477b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1478b0104773SPascal Brand * can't restore sync with this API. 14792733280aSEtienne Carriere * 14802733280aSEtienne Carriere * Need to check this before update_payload since sync would be lost if 14812733280aSEtienne Carriere * we return short buffer after that. 1482b0104773SPascal Brand */ 14832733280aSEtienne Carriere res = TEE_ERROR_GENERIC; 14842733280aSEtienne Carriere 1485b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1486b0104773SPascal Brand if (*destLen < req_dlen) { 1487b0104773SPascal Brand *destLen = req_dlen; 1488b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1489b0104773SPascal Brand } 1490b0104773SPascal Brand 1491b5816c88SCedric Chaumont if (*tagLen < operation->ae_tag_len) { 1492b5816c88SCedric Chaumont *tagLen = operation->ae_tag_len; 1493b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1494b0104773SPascal Brand } 1495b0104773SPascal Brand 14962733280aSEtienne Carriere if (res == TEE_ERROR_SHORT_BUFFER) 14972733280aSEtienne Carriere goto out; 14982733280aSEtienne Carriere 1499afc0c182SBogdan Liulko tl = *tagLen; 1500b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1501afc0c182SBogdan Liulko if (operation->block_size > 1) { 1502afc0c182SBogdan Liulko res = tee_buffer_update(operation, utee_authenc_update_payload, 1503afc0c182SBogdan Liulko srcData, srcLen, dst, &tmp_dlen); 1504b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1505b5816c88SCedric Chaumont goto out; 1506b5816c88SCedric Chaumont 1507b0104773SPascal Brand dst += tmp_dlen; 1508b0104773SPascal Brand acc_dlen += tmp_dlen; 1509b0104773SPascal Brand 1510b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1511afc0c182SBogdan Liulko res = utee_authenc_enc_final(operation->state, 1512afc0c182SBogdan Liulko operation->buffer, 1513afc0c182SBogdan Liulko operation->buffer_offs, dst, 1514afc0c182SBogdan Liulko &tmp_dlen, tag, &tl); 1515afc0c182SBogdan Liulko } else { 1516afc0c182SBogdan Liulko res = utee_authenc_enc_final(operation->state, srcData, 1517afc0c182SBogdan Liulko srcLen, dst, &tmp_dlen, 1518e86f1266SJens Wiklander tag, &tl); 1519afc0c182SBogdan Liulko } 1520e86f1266SJens Wiklander *tagLen = tl; 1521b0104773SPascal Brand if (res != TEE_SUCCESS) 1522b5816c88SCedric Chaumont goto out; 1523b0104773SPascal Brand 1524b5816c88SCedric Chaumont acc_dlen += tmp_dlen; 1525b0104773SPascal Brand *destLen = acc_dlen; 1526642a1607SCedric Chaumont 1527b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1528b5816c88SCedric Chaumont 1529642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1530642a1607SCedric Chaumont 1531b5816c88SCedric Chaumont out: 1532b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1533b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1534b5816c88SCedric Chaumont TEE_Panic(res); 1535b0104773SPascal Brand 1536b0104773SPascal Brand return res; 1537b0104773SPascal Brand } 1538b0104773SPascal Brand 1539b5816c88SCedric Chaumont TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation, 15408f07fe6fSJerome Forissier const void *srcData, uint32_t srcLen, 1541b5816c88SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 154279a3c601SCedric Chaumont uint32_t tagLen) 1543b0104773SPascal Brand { 1544b0104773SPascal Brand TEE_Result res; 1545b0104773SPascal Brand uint8_t *dst = destData; 1546b0104773SPascal Brand size_t acc_dlen = 0; 1547e86f1266SJens Wiklander uint64_t tmp_dlen; 1548b0104773SPascal Brand size_t req_dlen; 1549b0104773SPascal Brand 1550b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1551b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1552b5816c88SCedric Chaumont destLen == NULL || 1553b5816c88SCedric Chaumont (destData == NULL && *destLen != 0) || 1554b5816c88SCedric Chaumont (tag == NULL && tagLen != 0)) { 1555b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1556b5816c88SCedric Chaumont goto out; 1557b5816c88SCedric Chaumont } 1558b5816c88SCedric Chaumont 1559b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1560b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1561b5816c88SCedric Chaumont goto out; 1562b5816c88SCedric Chaumont } 1563b5816c88SCedric Chaumont 1564b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1565b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1566b5816c88SCedric Chaumont goto out; 1567b5816c88SCedric Chaumont } 1568b0104773SPascal Brand 1569b0104773SPascal Brand /* 1570b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1571b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1572b0104773SPascal Brand * can't restore sync with this API. 1573b0104773SPascal Brand */ 1574b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1575b0104773SPascal Brand if (*destLen < req_dlen) { 1576b0104773SPascal Brand *destLen = req_dlen; 1577b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1578b5816c88SCedric Chaumont goto out; 1579b0104773SPascal Brand } 1580b0104773SPascal Brand 1581b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1582afc0c182SBogdan Liulko if (operation->block_size > 1) { 1583afc0c182SBogdan Liulko res = tee_buffer_update(operation, utee_authenc_update_payload, 1584afc0c182SBogdan Liulko srcData, srcLen, dst, &tmp_dlen); 1585b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1586b5816c88SCedric Chaumont goto out; 1587b5816c88SCedric Chaumont 1588b0104773SPascal Brand dst += tmp_dlen; 1589b0104773SPascal Brand acc_dlen += tmp_dlen; 1590b0104773SPascal Brand 1591b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1592afc0c182SBogdan Liulko res = utee_authenc_dec_final(operation->state, 1593afc0c182SBogdan Liulko operation->buffer, 1594afc0c182SBogdan Liulko operation->buffer_offs, dst, 1595afc0c182SBogdan Liulko &tmp_dlen, tag, tagLen); 1596afc0c182SBogdan Liulko } else { 1597afc0c182SBogdan Liulko res = utee_authenc_dec_final(operation->state, srcData, 1598afc0c182SBogdan Liulko srcLen, dst, &tmp_dlen, 1599b5816c88SCedric Chaumont tag, tagLen); 1600afc0c182SBogdan Liulko } 1601b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1602b5816c88SCedric Chaumont goto out; 1603b5816c88SCedric Chaumont 1604b0104773SPascal Brand /* Supplied tagLen should match what we initiated with */ 1605b5816c88SCedric Chaumont if (tagLen != operation->ae_tag_len) 1606b0104773SPascal Brand res = TEE_ERROR_MAC_INVALID; 1607b0104773SPascal Brand 1608b0104773SPascal Brand acc_dlen += tmp_dlen; 1609b0104773SPascal Brand *destLen = acc_dlen; 1610642a1607SCedric Chaumont 1611b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1612b5816c88SCedric Chaumont 1613642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1614642a1607SCedric Chaumont 1615b5816c88SCedric Chaumont out: 1616b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1617b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER && 1618b5816c88SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 1619b5816c88SCedric Chaumont TEE_Panic(res); 1620b0104773SPascal Brand 1621b0104773SPascal Brand return res; 1622b0104773SPascal Brand } 1623b0104773SPascal Brand 1624b0104773SPascal Brand /* Cryptographic Operations API - Asymmetric Functions */ 1625b0104773SPascal Brand 162612e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation, 16278f07fe6fSJerome Forissier const TEE_Attribute *params, 16288f07fe6fSJerome Forissier uint32_t paramCount, const void *srcData, 162979a3c601SCedric Chaumont uint32_t srcLen, void *destData, 163079a3c601SCedric Chaumont uint32_t *destLen) 1631b0104773SPascal Brand { 1632b0104773SPascal Brand TEE_Result res; 1633e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1634e86f1266SJens Wiklander uint64_t dl; 1635b0104773SPascal Brand 163612e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1637b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 1638b0104773SPascal Brand TEE_Panic(0); 163912e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1640b0104773SPascal Brand TEE_Panic(0); 164112e66b6fSCedric Chaumont if (!operation->key1) 1642b0104773SPascal Brand TEE_Panic(0); 164312e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 164412e66b6fSCedric Chaumont TEE_Panic(0); 164512e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_ENCRYPT) 1646b0104773SPascal Brand TEE_Panic(0); 1647b0104773SPascal Brand 1648e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1649e86f1266SJens Wiklander dl = *destLen; 1650e86f1266SJens Wiklander res = utee_asymm_operate(operation->state, ua, paramCount, srcData, 1651e86f1266SJens Wiklander srcLen, destData, &dl); 1652e86f1266SJens Wiklander *destLen = dl; 165312e66b6fSCedric Chaumont 16548844ebfcSPascal Brand if (res != TEE_SUCCESS && 16558844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 16568844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1657b0104773SPascal Brand TEE_Panic(res); 165812e66b6fSCedric Chaumont 1659b0104773SPascal Brand return res; 1660b0104773SPascal Brand } 1661b0104773SPascal Brand 166212e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation, 16638f07fe6fSJerome Forissier const TEE_Attribute *params, 16648f07fe6fSJerome Forissier uint32_t paramCount, const void *srcData, 166579a3c601SCedric Chaumont uint32_t srcLen, void *destData, 166679a3c601SCedric Chaumont uint32_t *destLen) 1667b0104773SPascal Brand { 1668b0104773SPascal Brand TEE_Result res; 1669e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1670e86f1266SJens Wiklander uint64_t dl; 1671b0104773SPascal Brand 167212e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1673b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 1674b0104773SPascal Brand TEE_Panic(0); 167512e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1676b0104773SPascal Brand TEE_Panic(0); 167712e66b6fSCedric Chaumont if (!operation->key1) 1678b0104773SPascal Brand TEE_Panic(0); 167912e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 168012e66b6fSCedric Chaumont TEE_Panic(0); 168112e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_DECRYPT) 1682b0104773SPascal Brand TEE_Panic(0); 1683b0104773SPascal Brand 1684e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1685e86f1266SJens Wiklander dl = *destLen; 1686e86f1266SJens Wiklander res = utee_asymm_operate(operation->state, ua, paramCount, srcData, 1687e86f1266SJens Wiklander srcLen, destData, &dl); 1688e86f1266SJens Wiklander *destLen = dl; 168912e66b6fSCedric Chaumont 16908844ebfcSPascal Brand if (res != TEE_SUCCESS && 16918844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 16928844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1693b0104773SPascal Brand TEE_Panic(res); 169412e66b6fSCedric Chaumont 1695b0104773SPascal Brand return res; 1696b0104773SPascal Brand } 1697b0104773SPascal Brand 169812e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation, 16998f07fe6fSJerome Forissier const TEE_Attribute *params, 17008f07fe6fSJerome Forissier uint32_t paramCount, const void *digest, 170179a3c601SCedric Chaumont uint32_t digestLen, void *signature, 170279a3c601SCedric Chaumont uint32_t *signatureLen) 1703b0104773SPascal Brand { 1704b0104773SPascal Brand TEE_Result res; 1705e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1706e86f1266SJens Wiklander uint64_t sl; 1707b0104773SPascal Brand 170812e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || 170912e66b6fSCedric Chaumont (digest == NULL && digestLen != 0) || 1710b0104773SPascal Brand signature == NULL || signatureLen == NULL) 1711b0104773SPascal Brand TEE_Panic(0); 171212e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1713b0104773SPascal Brand TEE_Panic(0); 171412e66b6fSCedric Chaumont if (!operation->key1) 1715b0104773SPascal Brand TEE_Panic(0); 171612e66b6fSCedric Chaumont if (operation->info.operationClass != 171712e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 171812e66b6fSCedric Chaumont TEE_Panic(0); 171912e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_SIGN) 1720b0104773SPascal Brand TEE_Panic(0); 1721b0104773SPascal Brand 1722e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1723e86f1266SJens Wiklander sl = *signatureLen; 1724e86f1266SJens Wiklander res = utee_asymm_operate(operation->state, ua, paramCount, digest, 1725e86f1266SJens Wiklander digestLen, signature, &sl); 1726e86f1266SJens Wiklander *signatureLen = sl; 172712e66b6fSCedric Chaumont 1728b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER) 1729b0104773SPascal Brand TEE_Panic(res); 173012e66b6fSCedric Chaumont 1731b0104773SPascal Brand return res; 1732b0104773SPascal Brand } 1733b0104773SPascal Brand 173412e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation, 17358f07fe6fSJerome Forissier const TEE_Attribute *params, 17368f07fe6fSJerome Forissier uint32_t paramCount, const void *digest, 17378f07fe6fSJerome Forissier uint32_t digestLen, 17388f07fe6fSJerome Forissier const void *signature, 173979a3c601SCedric Chaumont uint32_t signatureLen) 1740b0104773SPascal Brand { 1741b0104773SPascal Brand TEE_Result res; 1742e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1743b0104773SPascal Brand 174412e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || 174512e66b6fSCedric Chaumont (digest == NULL && digestLen != 0) || 1746b0104773SPascal Brand (signature == NULL && signatureLen != 0)) 1747b0104773SPascal Brand TEE_Panic(0); 174812e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1749b0104773SPascal Brand TEE_Panic(0); 175012e66b6fSCedric Chaumont if (!operation->key1) 1751b0104773SPascal Brand TEE_Panic(0); 175212e66b6fSCedric Chaumont if (operation->info.operationClass != 175312e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 175412e66b6fSCedric Chaumont TEE_Panic(0); 175512e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_VERIFY) 1756b0104773SPascal Brand TEE_Panic(0); 1757b0104773SPascal Brand 1758e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1759e86f1266SJens Wiklander res = utee_asymm_verify(operation->state, ua, paramCount, digest, 176012e66b6fSCedric Chaumont digestLen, signature, signatureLen); 176112e66b6fSCedric Chaumont 1762b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID) 1763b0104773SPascal Brand TEE_Panic(res); 176412e66b6fSCedric Chaumont 1765b0104773SPascal Brand return res; 1766b0104773SPascal Brand } 1767b0104773SPascal Brand 1768b0104773SPascal Brand /* Cryptographic Operations API - Key Derivation Functions */ 1769b0104773SPascal Brand 1770b0104773SPascal Brand void TEE_DeriveKey(TEE_OperationHandle operation, 1771b0104773SPascal Brand const TEE_Attribute *params, uint32_t paramCount, 1772b0104773SPascal Brand TEE_ObjectHandle derivedKey) 1773b0104773SPascal Brand { 1774b0104773SPascal Brand TEE_Result res; 1775b0104773SPascal Brand TEE_ObjectInfo key_info; 1776e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1777b0104773SPascal Brand 1778b0104773SPascal Brand if (operation == TEE_HANDLE_NULL || derivedKey == 0) 1779b0104773SPascal Brand TEE_Panic(0); 178084fa9467SCedric Chaumont if (params == NULL && paramCount != 0) 1781b0104773SPascal Brand TEE_Panic(0); 17828854d3c6SJerome Forissier if (TEE_ALG_GET_CLASS(operation->info.algorithm) != 17838854d3c6SJerome Forissier TEE_OPERATION_KEY_DERIVATION) 1784b0104773SPascal Brand TEE_Panic(0); 1785b0104773SPascal Brand 1786b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION) 1787b0104773SPascal Brand TEE_Panic(0); 178884fa9467SCedric Chaumont if (!operation->key1) 178984fa9467SCedric Chaumont TEE_Panic(0); 1790b0104773SPascal Brand if (operation->info.mode != TEE_MODE_DERIVE) 1791b0104773SPascal Brand TEE_Panic(0); 1792b0104773SPascal Brand if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0) 1793b0104773SPascal Brand TEE_Panic(0); 1794b0104773SPascal Brand 1795e86f1266SJens Wiklander res = utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info); 1796b0104773SPascal Brand if (res != TEE_SUCCESS) 1797b36311adSJerome Forissier TEE_Panic(res); 1798b0104773SPascal Brand 1799b0104773SPascal Brand if (key_info.objectType != TEE_TYPE_GENERIC_SECRET) 1800b0104773SPascal Brand TEE_Panic(0); 1801b0104773SPascal Brand if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1802b0104773SPascal Brand TEE_Panic(0); 1803b0104773SPascal Brand 1804e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1805e86f1266SJens Wiklander res = utee_cryp_derive_key(operation->state, ua, paramCount, 1806e86f1266SJens Wiklander (unsigned long)derivedKey); 1807b0104773SPascal Brand if (res != TEE_SUCCESS) 1808b0104773SPascal Brand TEE_Panic(res); 1809b0104773SPascal Brand } 1810b0104773SPascal Brand 1811b0104773SPascal Brand /* Cryptographic Operations API - Random Number Generation Functions */ 1812b0104773SPascal Brand 181379a3c601SCedric Chaumont void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen) 1814b0104773SPascal Brand { 1815b0104773SPascal Brand TEE_Result res; 1816b0104773SPascal Brand 1817b0104773SPascal Brand res = utee_cryp_random_number_generate(randomBuffer, randomBufferLen); 1818b0104773SPascal Brand if (res != TEE_SUCCESS) 1819b0104773SPascal Brand TEE_Panic(res); 1820b0104773SPascal Brand } 1821433c4257SJens Wiklander 1822433c4257SJens Wiklander int rand(void) 1823433c4257SJens Wiklander { 1824433c4257SJens Wiklander int rc; 1825433c4257SJens Wiklander 1826433c4257SJens Wiklander TEE_GenerateRandom(&rc, sizeof(rc)); 1827433c4257SJens Wiklander 1828433c4257SJens Wiklander /* 1829433c4257SJens Wiklander * RAND_MAX is the larges int, INT_MAX which is all bits but the 1830433c4257SJens Wiklander * highest bit set. 1831433c4257SJens Wiklander */ 1832433c4257SJens Wiklander return rc & RAND_MAX; 1833433c4257SJens Wiklander } 183479170ce0SJerome Forissier 183579170ce0SJerome Forissier TEE_Result TEE_IsAlgorithmSupported(uint32_t alg, uint32_t element) 183679170ce0SJerome Forissier { 183779170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_AES)) { 183879170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_ECB)) { 183979170ce0SJerome Forissier if (alg == TEE_ALG_AES_ECB_NOPAD) 184079170ce0SJerome Forissier goto check_element_none; 184179170ce0SJerome Forissier } 184279170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CBC)) { 184379170ce0SJerome Forissier if (alg == TEE_ALG_AES_CBC_NOPAD) 184479170ce0SJerome Forissier goto check_element_none; 184579170ce0SJerome Forissier } 184679170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CTR)) { 184779170ce0SJerome Forissier if (alg == TEE_ALG_AES_CTR) 184879170ce0SJerome Forissier goto check_element_none; 184979170ce0SJerome Forissier } 185079170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CTS)) { 185179170ce0SJerome Forissier if (alg == TEE_ALG_AES_CTS) 185279170ce0SJerome Forissier goto check_element_none; 185379170ce0SJerome Forissier } 185479170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_XTS)) { 185579170ce0SJerome Forissier if (alg == TEE_ALG_AES_XTS) 185679170ce0SJerome Forissier goto check_element_none; 185779170ce0SJerome Forissier } 185879170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) { 185979170ce0SJerome Forissier if (alg == TEE_ALG_AES_CBC_MAC_NOPAD || 186079170ce0SJerome Forissier alg == TEE_ALG_AES_CBC_MAC_PKCS5) 186179170ce0SJerome Forissier goto check_element_none; 186279170ce0SJerome Forissier } 186379170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CMAC)) { 186479170ce0SJerome Forissier if (alg == TEE_ALG_AES_CMAC) 186579170ce0SJerome Forissier goto check_element_none; 186679170ce0SJerome Forissier } 186779170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CCM)) { 186879170ce0SJerome Forissier if (alg == TEE_ALG_AES_CCM) 186979170ce0SJerome Forissier goto check_element_none; 187079170ce0SJerome Forissier } 187179170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_GCM)) { 187279170ce0SJerome Forissier if (alg == TEE_ALG_AES_GCM) 187379170ce0SJerome Forissier goto check_element_none; 187479170ce0SJerome Forissier } 187579170ce0SJerome Forissier } 187679170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_DES)) { 187779170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_ECB)) { 187879170ce0SJerome Forissier if (alg == TEE_ALG_DES_ECB_NOPAD || 187979170ce0SJerome Forissier alg == TEE_ALG_DES3_ECB_NOPAD) 188079170ce0SJerome Forissier goto check_element_none; 188179170ce0SJerome Forissier } 188279170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CBC)) { 188379170ce0SJerome Forissier if (alg == TEE_ALG_DES_CBC_NOPAD || 188479170ce0SJerome Forissier alg == TEE_ALG_DES3_CBC_NOPAD) 188579170ce0SJerome Forissier goto check_element_none; 188679170ce0SJerome Forissier } 188779170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) { 188879170ce0SJerome Forissier if (alg == TEE_ALG_DES_CBC_MAC_NOPAD || 188979170ce0SJerome Forissier alg == TEE_ALG_DES_CBC_MAC_PKCS5 || 189079170ce0SJerome Forissier alg == TEE_ALG_DES3_CBC_MAC_NOPAD || 189179170ce0SJerome Forissier alg == TEE_ALG_DES3_CBC_MAC_PKCS5) 189279170ce0SJerome Forissier goto check_element_none; 189379170ce0SJerome Forissier } 189479170ce0SJerome Forissier } 189579170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_MD5)) { 189679170ce0SJerome Forissier if (alg == TEE_ALG_MD5) 189779170ce0SJerome Forissier goto check_element_none; 189879170ce0SJerome Forissier } 189979170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 190079170ce0SJerome Forissier if (alg == TEE_ALG_SHA1) 190179170ce0SJerome Forissier goto check_element_none; 190279170ce0SJerome Forissier } 190379170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 190479170ce0SJerome Forissier if (alg == TEE_ALG_SHA224) 190579170ce0SJerome Forissier goto check_element_none; 190679170ce0SJerome Forissier } 190779170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 190879170ce0SJerome Forissier if (alg == TEE_ALG_SHA256) 190979170ce0SJerome Forissier goto check_element_none; 191079170ce0SJerome Forissier } 191179170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA384)) { 191279170ce0SJerome Forissier if (alg == TEE_ALG_SHA384) 191379170ce0SJerome Forissier goto check_element_none; 191479170ce0SJerome Forissier } 191579170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA512)) { 191679170ce0SJerome Forissier if (alg == TEE_ALG_SHA512) 191779170ce0SJerome Forissier goto check_element_none; 191879170ce0SJerome Forissier } 191979170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) { 192079170ce0SJerome Forissier if (alg == TEE_ALG_MD5SHA1) 192179170ce0SJerome Forissier goto check_element_none; 192279170ce0SJerome Forissier } 192379170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_HMAC)) { 192479170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_MD5)) { 192579170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_MD5) 192679170ce0SJerome Forissier goto check_element_none; 192779170ce0SJerome Forissier } 192879170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 192979170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_SHA1) 193079170ce0SJerome Forissier goto check_element_none; 193179170ce0SJerome Forissier } 193279170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 193379170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_SHA224) 193479170ce0SJerome Forissier goto check_element_none; 193579170ce0SJerome Forissier } 193679170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 193779170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_SHA256) 193879170ce0SJerome Forissier goto check_element_none; 193979170ce0SJerome Forissier } 194079170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA384)) { 194179170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_SHA384) 194279170ce0SJerome Forissier goto check_element_none; 194379170ce0SJerome Forissier } 194479170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA512)) { 194579170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_SHA512) 194679170ce0SJerome Forissier goto check_element_none; 194779170ce0SJerome Forissier } 194879170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SM3)) { 194979170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_SM3) 195079170ce0SJerome Forissier goto check_element_none; 195179170ce0SJerome Forissier } 195279170ce0SJerome Forissier } 195379170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SM3)) { 195479170ce0SJerome Forissier if (alg == TEE_ALG_SM3) 195579170ce0SJerome Forissier goto check_element_none; 195679170ce0SJerome Forissier } 195779170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SM4)) { 195879170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_ECB)) { 195979170ce0SJerome Forissier if (alg == TEE_ALG_SM4_ECB_NOPAD) 196079170ce0SJerome Forissier goto check_element_none; 196179170ce0SJerome Forissier } 196279170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CBC)) { 196379170ce0SJerome Forissier if (alg == TEE_ALG_SM4_CBC_NOPAD) 196479170ce0SJerome Forissier goto check_element_none; 196579170ce0SJerome Forissier } 196679170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CTR)) { 196779170ce0SJerome Forissier if (alg == TEE_ALG_SM4_CTR) 196879170ce0SJerome Forissier goto check_element_none; 196979170ce0SJerome Forissier } 197079170ce0SJerome Forissier } 197179170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_RSA)) { 197279170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_MD5)) { 197379170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5) 197479170ce0SJerome Forissier goto check_element_none; 197579170ce0SJerome Forissier } 197679170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 197779170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA1 || 197879170ce0SJerome Forissier alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1 || 197979170ce0SJerome Forissier alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1) 198079170ce0SJerome Forissier goto check_element_none; 198179170ce0SJerome Forissier } 198279170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) { 198379170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5SHA1) 198479170ce0SJerome Forissier goto check_element_none; 198579170ce0SJerome Forissier } 198679170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 198779170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA224 || 198879170ce0SJerome Forissier alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224 || 198979170ce0SJerome Forissier alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224) 199079170ce0SJerome Forissier goto check_element_none; 199179170ce0SJerome Forissier } 199279170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 199379170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA256 || 199479170ce0SJerome Forissier alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256 || 199579170ce0SJerome Forissier alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256) 199679170ce0SJerome Forissier goto check_element_none; 199779170ce0SJerome Forissier } 199879170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA384)) { 199979170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA384 || 200079170ce0SJerome Forissier alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384 || 200179170ce0SJerome Forissier alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384) 200279170ce0SJerome Forissier goto check_element_none; 200379170ce0SJerome Forissier } 200479170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA512)) { 200579170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA512 || 200679170ce0SJerome Forissier alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512 || 200779170ce0SJerome Forissier alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512) 200879170ce0SJerome Forissier goto check_element_none; 200979170ce0SJerome Forissier } 201079170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_RSASSA_NA1)) { 201179170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5) 201279170ce0SJerome Forissier goto check_element_none; 201379170ce0SJerome Forissier } 201479170ce0SJerome Forissier if (alg == TEE_ALG_RSA_NOPAD) 201579170ce0SJerome Forissier goto check_element_none; 201679170ce0SJerome Forissier } 201779170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_DSA)) { 201879170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 201979170ce0SJerome Forissier if (alg == TEE_ALG_DSA_SHA1) 202079170ce0SJerome Forissier goto check_element_none; 202179170ce0SJerome Forissier } 202279170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 202379170ce0SJerome Forissier if (alg == TEE_ALG_DSA_SHA224) 202479170ce0SJerome Forissier goto check_element_none; 202579170ce0SJerome Forissier } 202679170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 202779170ce0SJerome Forissier if (alg == TEE_ALG_DSA_SHA256) 202879170ce0SJerome Forissier goto check_element_none; 202979170ce0SJerome Forissier } 203079170ce0SJerome Forissier } 203179170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_DH)) { 203279170ce0SJerome Forissier if (alg == TEE_ALG_DH_DERIVE_SHARED_SECRET) 203379170ce0SJerome Forissier goto check_element_none; 203479170ce0SJerome Forissier } 203579170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_ECC)) { 203679170ce0SJerome Forissier if ((alg == TEE_ALG_ECDH_P192 || alg == TEE_ALG_ECDSA_P192) && 203779170ce0SJerome Forissier element == TEE_ECC_CURVE_NIST_P192) 203879170ce0SJerome Forissier return TEE_SUCCESS; 203979170ce0SJerome Forissier if ((alg == TEE_ALG_ECDH_P224 || alg == TEE_ALG_ECDSA_P224) && 204079170ce0SJerome Forissier element == TEE_ECC_CURVE_NIST_P224) 204179170ce0SJerome Forissier return TEE_SUCCESS; 204279170ce0SJerome Forissier if ((alg == TEE_ALG_ECDH_P256 || alg == TEE_ALG_ECDSA_P256) && 204379170ce0SJerome Forissier element == TEE_ECC_CURVE_NIST_P256) 204479170ce0SJerome Forissier return TEE_SUCCESS; 204579170ce0SJerome Forissier if ((alg == TEE_ALG_ECDH_P384 || alg == TEE_ALG_ECDSA_P384) && 204679170ce0SJerome Forissier element == TEE_ECC_CURVE_NIST_P384) 204779170ce0SJerome Forissier return TEE_SUCCESS; 204879170ce0SJerome Forissier if ((alg == TEE_ALG_ECDH_P521 || alg == TEE_ALG_ECDSA_P521) && 204979170ce0SJerome Forissier element == TEE_ECC_CURVE_NIST_P521) 205079170ce0SJerome Forissier return TEE_SUCCESS; 205179170ce0SJerome Forissier } 205279170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SM2_DSA)) { 205379170ce0SJerome Forissier if (alg == TEE_ALG_SM2_DSA_SM3 && element == TEE_ECC_CURVE_SM2) 205479170ce0SJerome Forissier return TEE_SUCCESS; 205579170ce0SJerome Forissier } 205679170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SM2_KEP)) { 205779170ce0SJerome Forissier if (alg == TEE_ALG_SM2_KEP && element == TEE_ECC_CURVE_SM2) 205879170ce0SJerome Forissier return TEE_SUCCESS; 205979170ce0SJerome Forissier } 206079170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SM2_PKE)) { 206179170ce0SJerome Forissier if (alg == TEE_ALG_SM2_PKE && element == TEE_ECC_CURVE_SM2) 206279170ce0SJerome Forissier return TEE_SUCCESS; 206379170ce0SJerome Forissier } 206479170ce0SJerome Forissier 206579170ce0SJerome Forissier return TEE_ERROR_NOT_SUPPORTED; 206679170ce0SJerome Forissier check_element_none: 206779170ce0SJerome Forissier if (element == TEE_CRYPTO_ELEMENT_NONE) 206879170ce0SJerome Forissier return TEE_SUCCESS; 206979170ce0SJerome Forissier return TEE_ERROR_NOT_SUPPORTED; 207079170ce0SJerome Forissier } 2071