11bb92983SJerome Forissier // SPDX-License-Identifier: BSD-2-Clause 2b0104773SPascal Brand /* 3b0104773SPascal Brand * Copyright (c) 2014, STMicroelectronics International N.V. 4eee637e7SAlexander Zakharov * Copyright (c) 2021, SumUp Services GmbH 5b0104773SPascal Brand */ 679170ce0SJerome Forissier #include <config.h> 7b0104773SPascal Brand #include <stdlib.h> 8b0104773SPascal Brand #include <string.h> 9b796ebf3SJerome Forissier #include <string_ext.h> 10b0104773SPascal Brand #include <tee_api.h> 118854d3c6SJerome Forissier #include <tee_api_defines_extensions.h> 12b0104773SPascal Brand #include <tee_internal_api_extensions.h> 13b0104773SPascal Brand #include <utee_syscalls.h> 14b0104773SPascal Brand #include <utee_defines.h> 15fc26c92aSJens Wiklander #include <util.h> 16e86f1266SJens Wiklander #include "tee_api_private.h" 17b0104773SPascal Brand 18b0104773SPascal Brand struct __TEE_OperationHandle { 19b0104773SPascal Brand TEE_OperationInfo info; 20b0104773SPascal Brand TEE_ObjectHandle key1; 21b0104773SPascal Brand TEE_ObjectHandle key2; 22642a1607SCedric Chaumont uint32_t operationState;/* Operation state : INITIAL or ACTIVE */ 23b0104773SPascal Brand uint8_t *buffer; /* buffer to collect complete blocks */ 24b0104773SPascal Brand bool buffer_two_blocks; /* True if two blocks need to be buffered */ 25b0104773SPascal Brand size_t block_size; /* Block size of cipher */ 26b0104773SPascal Brand size_t buffer_offs; /* Offset in buffer */ 27b0104773SPascal Brand uint32_t state; /* Handle to state in TEE Core */ 28b0104773SPascal Brand }; 29b0104773SPascal Brand 30b0104773SPascal Brand /* Cryptographic Operations API - Generic Operation Functions */ 31b0104773SPascal Brand 32b0104773SPascal Brand TEE_Result TEE_AllocateOperation(TEE_OperationHandle *operation, 33b0104773SPascal Brand uint32_t algorithm, uint32_t mode, 34b0104773SPascal Brand uint32_t maxKeySize) 35b0104773SPascal Brand { 36b0104773SPascal Brand TEE_Result res; 37b0104773SPascal Brand TEE_OperationHandle op = TEE_HANDLE_NULL; 38b0104773SPascal Brand uint32_t handle_state = 0; 39b0104773SPascal Brand size_t block_size = 1; 40b0104773SPascal Brand uint32_t req_key_usage; 41b0104773SPascal Brand bool with_private_key = false; 42b0104773SPascal Brand bool buffer_two_blocks = false; 43b0104773SPascal Brand 449b52c538SCedric Chaumont if (!operation) 45b0104773SPascal Brand TEE_Panic(0); 46b0104773SPascal Brand 475b385b3fSJerome Forissier if (algorithm == TEE_ALG_AES_XTS || algorithm == TEE_ALG_SM2_KEP) 48b0104773SPascal Brand handle_state = TEE_HANDLE_FLAG_EXPECT_TWO_KEYS; 49b0104773SPascal Brand 50218d9055SCedric Chaumont /* Check algorithm max key size */ 51218d9055SCedric Chaumont switch (algorithm) { 52218d9055SCedric Chaumont case TEE_ALG_DSA_SHA1: 53218d9055SCedric Chaumont if (maxKeySize < 512) 54218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 55218d9055SCedric Chaumont if (maxKeySize > 1024) 56218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 57218d9055SCedric Chaumont if (maxKeySize % 64 != 0) 58218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 59218d9055SCedric Chaumont break; 60218d9055SCedric Chaumont 61218d9055SCedric Chaumont case TEE_ALG_DSA_SHA224: 62218d9055SCedric Chaumont if (maxKeySize != 2048) 63218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 64218d9055SCedric Chaumont break; 65218d9055SCedric Chaumont 66218d9055SCedric Chaumont case TEE_ALG_DSA_SHA256: 67218d9055SCedric Chaumont if (maxKeySize != 2048 && maxKeySize != 3072) 68218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 69218d9055SCedric Chaumont break; 70218d9055SCedric Chaumont 711220586eSCedric Chaumont case TEE_ALG_ECDSA_P192: 721220586eSCedric Chaumont case TEE_ALG_ECDH_P192: 731220586eSCedric Chaumont if (maxKeySize != 192) 741220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 751220586eSCedric Chaumont break; 761220586eSCedric Chaumont 771220586eSCedric Chaumont case TEE_ALG_ECDSA_P224: 781220586eSCedric Chaumont case TEE_ALG_ECDH_P224: 791220586eSCedric Chaumont if (maxKeySize != 224) 801220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 811220586eSCedric Chaumont break; 821220586eSCedric Chaumont 831220586eSCedric Chaumont case TEE_ALG_ECDSA_P256: 841220586eSCedric Chaumont case TEE_ALG_ECDH_P256: 8591fc6bd8SJerome Forissier case TEE_ALG_SM2_PKE: 860f151943SJerome Forissier case TEE_ALG_SM2_DSA_SM3: 871220586eSCedric Chaumont if (maxKeySize != 256) 881220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 891220586eSCedric Chaumont break; 901220586eSCedric Chaumont 915b385b3fSJerome Forissier case TEE_ALG_SM2_KEP: 925b385b3fSJerome Forissier /* Two 256-bit keys */ 935b385b3fSJerome Forissier if (maxKeySize != 512) 945b385b3fSJerome Forissier return TEE_ERROR_NOT_SUPPORTED; 955b385b3fSJerome Forissier break; 965b385b3fSJerome Forissier 971220586eSCedric Chaumont case TEE_ALG_ECDSA_P384: 981220586eSCedric Chaumont case TEE_ALG_ECDH_P384: 991220586eSCedric Chaumont if (maxKeySize != 384) 1001220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 1011220586eSCedric Chaumont break; 1021220586eSCedric Chaumont 1031220586eSCedric Chaumont case TEE_ALG_ECDSA_P521: 1041220586eSCedric Chaumont case TEE_ALG_ECDH_P521: 1051220586eSCedric Chaumont if (maxKeySize != 521) 1061220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 1071220586eSCedric Chaumont break; 108*3f61056dSSohaib ul Hassan case TEE_ALG_X25519: 109*3f61056dSSohaib ul Hassan if (maxKeySize != 256) 110*3f61056dSSohaib ul Hassan return TEE_ERROR_NOT_SUPPORTED; 111*3f61056dSSohaib ul Hassan break; 112218d9055SCedric Chaumont default: 113218d9055SCedric Chaumont break; 114218d9055SCedric Chaumont } 115218d9055SCedric Chaumont 116cf5c060cSJens Wiklander /* Check algorithm mode (and maxKeySize for digests) */ 117b0104773SPascal Brand switch (algorithm) { 118b0104773SPascal Brand case TEE_ALG_AES_CTS: 119b0104773SPascal Brand case TEE_ALG_AES_XTS: 120b0104773SPascal Brand buffer_two_blocks = true; 121919a5a68SJerome Forissier fallthrough; 1224bd53c54SJerome Forissier case TEE_ALG_AES_ECB_NOPAD: 123b0104773SPascal Brand case TEE_ALG_AES_CBC_NOPAD: 124b0104773SPascal Brand case TEE_ALG_AES_CCM: 125b0104773SPascal Brand case TEE_ALG_DES_ECB_NOPAD: 126b0104773SPascal Brand case TEE_ALG_DES_CBC_NOPAD: 127b0104773SPascal Brand case TEE_ALG_DES3_ECB_NOPAD: 128b0104773SPascal Brand case TEE_ALG_DES3_CBC_NOPAD: 129ade6f848SJerome Forissier case TEE_ALG_SM4_ECB_NOPAD: 130ade6f848SJerome Forissier case TEE_ALG_SM4_CBC_NOPAD: 131ade6f848SJerome Forissier case TEE_ALG_SM4_CTR: 132b0104773SPascal Brand if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES) 133b0104773SPascal Brand block_size = TEE_AES_BLOCK_SIZE; 134ade6f848SJerome Forissier else if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_SM4) 135ade6f848SJerome Forissier block_size = TEE_SM4_BLOCK_SIZE; 136b0104773SPascal Brand else 137b0104773SPascal Brand block_size = TEE_DES_BLOCK_SIZE; 138919a5a68SJerome Forissier fallthrough; 13957aabac5SBogdan Liulko case TEE_ALG_AES_CTR: 140afc0c182SBogdan Liulko case TEE_ALG_AES_GCM: 141b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) 142b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT; 143b0104773SPascal Brand else if (mode == TEE_MODE_DECRYPT) 144b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT; 145b0104773SPascal Brand else 146b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 147b0104773SPascal Brand break; 148b0104773SPascal Brand 1496a2e0a9fSGabor Szekely #if defined(CFG_CRYPTO_RSASSA_NA1) 1506a2e0a9fSGabor Szekely case TEE_ALG_RSASSA_PKCS1_V1_5: 1516a2e0a9fSGabor Szekely #endif 152b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_MD5: 153b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1: 154b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224: 155b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256: 156b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384: 157b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512: 158b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1: 159b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224: 160b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256: 161b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384: 162b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512: 163b0104773SPascal Brand case TEE_ALG_DSA_SHA1: 164218d9055SCedric Chaumont case TEE_ALG_DSA_SHA224: 165218d9055SCedric Chaumont case TEE_ALG_DSA_SHA256: 1661220586eSCedric Chaumont case TEE_ALG_ECDSA_P192: 1671220586eSCedric Chaumont case TEE_ALG_ECDSA_P224: 1681220586eSCedric Chaumont case TEE_ALG_ECDSA_P256: 1691220586eSCedric Chaumont case TEE_ALG_ECDSA_P384: 1701220586eSCedric Chaumont case TEE_ALG_ECDSA_P521: 1710f151943SJerome Forissier case TEE_ALG_SM2_DSA_SM3: 172b0104773SPascal Brand if (mode == TEE_MODE_SIGN) { 173b0104773SPascal Brand with_private_key = true; 174b0104773SPascal Brand req_key_usage = TEE_USAGE_SIGN; 175b0104773SPascal Brand } else if (mode == TEE_MODE_VERIFY) { 176b0104773SPascal Brand req_key_usage = TEE_USAGE_VERIFY; 177b0104773SPascal Brand } else { 178b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 179b0104773SPascal Brand } 180b0104773SPascal Brand break; 181b0104773SPascal Brand 182b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_V1_5: 183b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1: 184b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224: 185b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256: 186b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384: 187b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512: 18891fc6bd8SJerome Forissier case TEE_ALG_SM2_PKE: 189b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) { 190b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT; 191b0104773SPascal Brand } else if (mode == TEE_MODE_DECRYPT) { 192b0104773SPascal Brand with_private_key = true; 193b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT; 194b0104773SPascal Brand } else { 195b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 196b0104773SPascal Brand } 197b0104773SPascal Brand break; 198b0104773SPascal Brand 199b0104773SPascal Brand case TEE_ALG_RSA_NOPAD: 200b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) { 201b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY; 202b0104773SPascal Brand } else if (mode == TEE_MODE_DECRYPT) { 203b0104773SPascal Brand with_private_key = true; 204b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT | TEE_USAGE_SIGN; 205b0104773SPascal Brand } else { 206b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 207b0104773SPascal Brand } 208b0104773SPascal Brand break; 209b0104773SPascal Brand 210b0104773SPascal Brand case TEE_ALG_DH_DERIVE_SHARED_SECRET: 2111220586eSCedric Chaumont case TEE_ALG_ECDH_P192: 2121220586eSCedric Chaumont case TEE_ALG_ECDH_P224: 2131220586eSCedric Chaumont case TEE_ALG_ECDH_P256: 2141220586eSCedric Chaumont case TEE_ALG_ECDH_P384: 2151220586eSCedric Chaumont case TEE_ALG_ECDH_P521: 216cdb198a7SJerome Forissier case TEE_ALG_HKDF_MD5_DERIVE_KEY: 217cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA1_DERIVE_KEY: 218cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA224_DERIVE_KEY: 219cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA256_DERIVE_KEY: 220cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA384_DERIVE_KEY: 221cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA512_DERIVE_KEY: 2228854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA1_DERIVE_KEY: 2238854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA224_DERIVE_KEY: 2248854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA256_DERIVE_KEY: 2258854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA384_DERIVE_KEY: 2268854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA512_DERIVE_KEY: 2270f2293b7SJerome Forissier case TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY: 2285b385b3fSJerome Forissier case TEE_ALG_SM2_KEP: 229*3f61056dSSohaib ul Hassan case TEE_ALG_X25519: 230b0104773SPascal Brand if (mode != TEE_MODE_DERIVE) 231b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 232b0104773SPascal Brand with_private_key = true; 233b0104773SPascal Brand req_key_usage = TEE_USAGE_DERIVE; 234b0104773SPascal Brand break; 235b0104773SPascal Brand 236b0104773SPascal Brand case TEE_ALG_MD5: 237b0104773SPascal Brand case TEE_ALG_SHA1: 238b0104773SPascal Brand case TEE_ALG_SHA224: 239b0104773SPascal Brand case TEE_ALG_SHA256: 240b0104773SPascal Brand case TEE_ALG_SHA384: 241b0104773SPascal Brand case TEE_ALG_SHA512: 24247645577SJerome Forissier case TEE_ALG_SM3: 243b0104773SPascal Brand if (mode != TEE_MODE_DIGEST) 244b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 245cf5c060cSJens Wiklander if (maxKeySize) 246cf5c060cSJens Wiklander return TEE_ERROR_NOT_SUPPORTED; 24705304565SCedric Chaumont /* v1.1: flags always set for digest operations */ 248b0104773SPascal Brand handle_state |= TEE_HANDLE_FLAG_KEY_SET; 249b0104773SPascal Brand req_key_usage = 0; 250b0104773SPascal Brand break; 251b0104773SPascal Brand 252b0104773SPascal Brand case TEE_ALG_DES_CBC_MAC_NOPAD: 253b0104773SPascal Brand case TEE_ALG_AES_CBC_MAC_NOPAD: 254b0104773SPascal Brand case TEE_ALG_AES_CBC_MAC_PKCS5: 255b0104773SPascal Brand case TEE_ALG_AES_CMAC: 256b0104773SPascal Brand case TEE_ALG_DES_CBC_MAC_PKCS5: 257b0104773SPascal Brand case TEE_ALG_DES3_CBC_MAC_NOPAD: 258b0104773SPascal Brand case TEE_ALG_DES3_CBC_MAC_PKCS5: 259eee637e7SAlexander Zakharov case TEE_ALG_DES3_CMAC: 260b0104773SPascal Brand case TEE_ALG_HMAC_MD5: 261b0104773SPascal Brand case TEE_ALG_HMAC_SHA1: 262b0104773SPascal Brand case TEE_ALG_HMAC_SHA224: 263b0104773SPascal Brand case TEE_ALG_HMAC_SHA256: 264b0104773SPascal Brand case TEE_ALG_HMAC_SHA384: 265b0104773SPascal Brand case TEE_ALG_HMAC_SHA512: 26647645577SJerome Forissier case TEE_ALG_HMAC_SM3: 267b0104773SPascal Brand if (mode != TEE_MODE_MAC) 268b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 269b0104773SPascal Brand req_key_usage = TEE_USAGE_MAC; 270b0104773SPascal Brand break; 271b0104773SPascal Brand 272b0104773SPascal Brand default: 273b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 274b0104773SPascal Brand } 275b0104773SPascal Brand 276b66f219bSJens Wiklander op = TEE_Malloc(sizeof(*op), TEE_MALLOC_FILL_ZERO); 2779b52c538SCedric Chaumont if (!op) 278b0104773SPascal Brand return TEE_ERROR_OUT_OF_MEMORY; 279b0104773SPascal Brand 280b0104773SPascal Brand op->info.algorithm = algorithm; 281b0104773SPascal Brand op->info.operationClass = TEE_ALG_GET_CLASS(algorithm); 2826a2e0a9fSGabor Szekely #ifdef CFG_CRYPTO_RSASSA_NA1 2836a2e0a9fSGabor Szekely if (algorithm == TEE_ALG_RSASSA_PKCS1_V1_5) 2846a2e0a9fSGabor Szekely op->info.operationClass = TEE_OPERATION_ASYMMETRIC_SIGNATURE; 2856a2e0a9fSGabor Szekely #endif 286b0104773SPascal Brand op->info.mode = mode; 2872e5e6460SAlbert Schwarzkopf op->info.digestLength = TEE_ALG_GET_DIGEST_SIZE(algorithm); 288b0104773SPascal Brand op->info.maxKeySize = maxKeySize; 289b0104773SPascal Brand op->info.requiredKeyUsage = req_key_usage; 290b0104773SPascal Brand op->info.handleState = handle_state; 291b0104773SPascal Brand 292b0104773SPascal Brand if (block_size > 1) { 293b0104773SPascal Brand size_t buffer_size = block_size; 294b0104773SPascal Brand 295b0104773SPascal Brand if (buffer_two_blocks) 296b0104773SPascal Brand buffer_size *= 2; 297b0104773SPascal Brand 2989b52c538SCedric Chaumont op->buffer = TEE_Malloc(buffer_size, 2999b52c538SCedric Chaumont TEE_USER_MEM_HINT_NO_FILL_ZERO); 300b0104773SPascal Brand if (op->buffer == NULL) { 301b0104773SPascal Brand res = TEE_ERROR_OUT_OF_MEMORY; 302b66f219bSJens Wiklander goto out; 303b0104773SPascal Brand } 304b0104773SPascal Brand } 305b0104773SPascal Brand op->block_size = block_size; 306b0104773SPascal Brand op->buffer_two_blocks = buffer_two_blocks; 307b0104773SPascal Brand 308b0104773SPascal Brand if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) { 309b0104773SPascal Brand uint32_t mks = maxKeySize; 310b0104773SPascal Brand TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm, 311b0104773SPascal Brand with_private_key); 312b0104773SPascal Brand 313b0104773SPascal Brand /* 314b0104773SPascal Brand * If two keys are expected the max key size is the sum of 315b0104773SPascal Brand * the size of both keys. 316b0104773SPascal Brand */ 317b0104773SPascal Brand if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) 318b0104773SPascal Brand mks /= 2; 319b0104773SPascal Brand 320b0104773SPascal Brand res = TEE_AllocateTransientObject(key_type, mks, &op->key1); 321b0104773SPascal Brand if (res != TEE_SUCCESS) 322b66f219bSJens Wiklander goto out; 323b0104773SPascal Brand 32405304565SCedric Chaumont if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) { 3259b52c538SCedric Chaumont res = TEE_AllocateTransientObject(key_type, mks, 326b0104773SPascal Brand &op->key2); 327b0104773SPascal Brand if (res != TEE_SUCCESS) 328b66f219bSJens Wiklander goto out; 329b0104773SPascal Brand } 330b0104773SPascal Brand } 331b0104773SPascal Brand 3322c028fdeSJerome Forissier res = _utee_cryp_state_alloc(algorithm, mode, (unsigned long)op->key1, 333e86f1266SJens Wiklander (unsigned long)op->key2, &op->state); 334b66f219bSJens Wiklander if (res != TEE_SUCCESS) 335b66f219bSJens Wiklander goto out; 336b0104773SPascal Brand 33705304565SCedric Chaumont /* 33805304565SCedric Chaumont * Initialize digest operations 33905304565SCedric Chaumont * Other multi-stage operations initialized w/ TEE_xxxInit functions 34005304565SCedric Chaumont * Non-applicable on asymmetric operations 34105304565SCedric Chaumont */ 34205304565SCedric Chaumont if (TEE_ALG_GET_CLASS(algorithm) == TEE_OPERATION_DIGEST) { 3432c028fdeSJerome Forissier res = _utee_hash_init(op->state, NULL, 0); 34405304565SCedric Chaumont if (res != TEE_SUCCESS) 345b66f219bSJens Wiklander goto out; 34605304565SCedric Chaumont /* v1.1: flags always set for digest operations */ 34705304565SCedric Chaumont op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 34805304565SCedric Chaumont } 34905304565SCedric Chaumont 350642a1607SCedric Chaumont op->operationState = TEE_OPERATION_STATE_INITIAL; 351642a1607SCedric Chaumont 352b0104773SPascal Brand *operation = op; 353b0104773SPascal Brand 354b66f219bSJens Wiklander out: 355b66f219bSJens Wiklander if (res != TEE_SUCCESS) { 356b66f219bSJens Wiklander if (res != TEE_ERROR_OUT_OF_MEMORY && 3579b52c538SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 358b36311adSJerome Forissier TEE_Panic(res); 359b66f219bSJens Wiklander if (op) { 360b66f219bSJens Wiklander if (op->state) { 361b66f219bSJens Wiklander TEE_FreeOperation(op); 362b66f219bSJens Wiklander } else { 363b66f219bSJens Wiklander TEE_Free(op->buffer); 364b66f219bSJens Wiklander TEE_FreeTransientObject(op->key1); 365b66f219bSJens Wiklander TEE_FreeTransientObject(op->key2); 366b66f219bSJens Wiklander TEE_Free(op); 367b66f219bSJens Wiklander } 368b66f219bSJens Wiklander } 369b66f219bSJens Wiklander } 370b66f219bSJens Wiklander 371b0104773SPascal Brand return res; 372b0104773SPascal Brand } 373b0104773SPascal Brand 374b0104773SPascal Brand void TEE_FreeOperation(TEE_OperationHandle operation) 375b0104773SPascal Brand { 376e889e80bSCedric Chaumont TEE_Result res; 377e889e80bSCedric Chaumont 378e889e80bSCedric Chaumont if (operation == TEE_HANDLE_NULL) 379e889e80bSCedric Chaumont TEE_Panic(0); 380e889e80bSCedric Chaumont 381b0104773SPascal Brand /* 382b0104773SPascal Brand * Note that keys should not be freed here, since they are 383b0104773SPascal Brand * claimed by the operation they will be freed by 384b0104773SPascal Brand * utee_cryp_state_free(). 385b0104773SPascal Brand */ 3862c028fdeSJerome Forissier res = _utee_cryp_state_free(operation->state); 387e889e80bSCedric Chaumont if (res != TEE_SUCCESS) 388b36311adSJerome Forissier TEE_Panic(res); 389e889e80bSCedric Chaumont 390b0104773SPascal Brand TEE_Free(operation->buffer); 391b0104773SPascal Brand TEE_Free(operation); 392b0104773SPascal Brand } 393b0104773SPascal Brand 394b0104773SPascal Brand void TEE_GetOperationInfo(TEE_OperationHandle operation, 395b0104773SPascal Brand TEE_OperationInfo *operationInfo) 396b0104773SPascal Brand { 397b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 398b0104773SPascal Brand TEE_Panic(0); 399b0104773SPascal Brand 4006915bbbbSJens Wiklander __utee_check_out_annotation(operationInfo, sizeof(*operationInfo)); 401b0104773SPascal Brand 402b0104773SPascal Brand *operationInfo = operation->info; 403bac3a8a7SJens Wiklander if (operationInfo->handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) { 404bac3a8a7SJens Wiklander operationInfo->keySize = 0; 405bac3a8a7SJens Wiklander operationInfo->requiredKeyUsage = 0; 406bac3a8a7SJens Wiklander } 407b0104773SPascal Brand } 408b0104773SPascal Brand 409ee2f75afSJens Wiklander TEE_Result TEE_GetOperationInfoMultiple(TEE_OperationHandle op, 410ee2f75afSJens Wiklander TEE_OperationInfoMultiple *op_info, 411ee2f75afSJens Wiklander uint32_t *size) 41205304565SCedric Chaumont { 41305304565SCedric Chaumont TEE_Result res = TEE_SUCCESS; 414ee2f75afSJens Wiklander TEE_ObjectInfo kinfo = { }; 415ee2f75afSJens Wiklander size_t max_key_count = 0; 416ee2f75afSJens Wiklander bool two_keys = false; 41705304565SCedric Chaumont 418ee2f75afSJens Wiklander if (op == TEE_HANDLE_NULL) { 41905304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 42005304565SCedric Chaumont goto out; 42105304565SCedric Chaumont } 42205304565SCedric Chaumont 423ee2f75afSJens Wiklander __utee_check_outbuf_annotation(op_info, size); 42405304565SCedric Chaumont 425ee2f75afSJens Wiklander if (*size < sizeof(*op_info)) { 426ee2f75afSJens Wiklander res = TEE_ERROR_BAD_PARAMETERS; 427ee2f75afSJens Wiklander goto out; 428ee2f75afSJens Wiklander } 429ee2f75afSJens Wiklander max_key_count = (*size - sizeof(*op_info)) / 43005304565SCedric Chaumont sizeof(TEE_OperationInfoKey); 43105304565SCedric Chaumont 432ee2f75afSJens Wiklander TEE_MemFill(op_info, 0, *size); 43305304565SCedric Chaumont 43405304565SCedric Chaumont /* Two keys flag (TEE_ALG_AES_XTS only) */ 435ee2f75afSJens Wiklander two_keys = op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS; 436ee2f75afSJens Wiklander 437ee2f75afSJens Wiklander if (op->info.mode == TEE_MODE_DIGEST) { 438ee2f75afSJens Wiklander op_info->numberOfKeys = 0; 439ee2f75afSJens Wiklander } else if (!two_keys) { 440ee2f75afSJens Wiklander if (max_key_count < 1) { 44105304565SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 44205304565SCedric Chaumont goto out; 44305304565SCedric Chaumont } 44405304565SCedric Chaumont 445ee2f75afSJens Wiklander res = TEE_GetObjectInfo1(op->key1, &kinfo); 446ee2f75afSJens Wiklander /* Key1 is not a valid handle, "can't happen". */ 447ee2f75afSJens Wiklander if (res) 44805304565SCedric Chaumont goto out; 44905304565SCedric Chaumont 450ee2f75afSJens Wiklander op_info->keyInformation[0].keySize = kinfo.keySize; 451ee2f75afSJens Wiklander op_info->keyInformation[0].requiredKeyUsage = 452ee2f75afSJens Wiklander op->info.requiredKeyUsage; 453ee2f75afSJens Wiklander op_info->numberOfKeys = 1; 454ee2f75afSJens Wiklander } else { 455ee2f75afSJens Wiklander if (max_key_count < 2) { 456ee2f75afSJens Wiklander res = TEE_ERROR_SHORT_BUFFER; 45705304565SCedric Chaumont goto out; 45805304565SCedric Chaumont } 45905304565SCedric Chaumont 460ee2f75afSJens Wiklander res = TEE_GetObjectInfo1(op->key1, &kinfo); 461ee2f75afSJens Wiklander /* Key1 is not a valid handle, "can't happen". */ 462ee2f75afSJens Wiklander if (res) 463ee2f75afSJens Wiklander goto out; 464ee2f75afSJens Wiklander 465ee2f75afSJens Wiklander op_info->keyInformation[0].keySize = kinfo.keySize; 466ee2f75afSJens Wiklander op_info->keyInformation[0].requiredKeyUsage = 467ee2f75afSJens Wiklander op->info.requiredKeyUsage; 468ee2f75afSJens Wiklander 469ee2f75afSJens Wiklander res = TEE_GetObjectInfo1(op->key2, &kinfo); 470ee2f75afSJens Wiklander /* Key2 is not a valid handle, "can't happen". */ 471ee2f75afSJens Wiklander if (res) 472ee2f75afSJens Wiklander goto out; 473ee2f75afSJens Wiklander 474ee2f75afSJens Wiklander op_info->keyInformation[1].keySize = kinfo.keySize; 475ee2f75afSJens Wiklander op_info->keyInformation[1].requiredKeyUsage = 476ee2f75afSJens Wiklander op->info.requiredKeyUsage; 477ee2f75afSJens Wiklander 478ee2f75afSJens Wiklander op_info->numberOfKeys = 2; 47905304565SCedric Chaumont } 48005304565SCedric Chaumont 481ee2f75afSJens Wiklander op_info->algorithm = op->info.algorithm; 482ee2f75afSJens Wiklander op_info->operationClass = op->info.operationClass; 483ee2f75afSJens Wiklander op_info->mode = op->info.mode; 484ee2f75afSJens Wiklander op_info->digestLength = op->info.digestLength; 485ee2f75afSJens Wiklander op_info->maxKeySize = op->info.maxKeySize; 486ee2f75afSJens Wiklander op_info->handleState = op->info.handleState; 487ee2f75afSJens Wiklander op_info->operationState = op->operationState; 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) { 5102c028fdeSJerome Forissier 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); 5396c4ea258SJens Wiklander operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 5406c4ea258SJens Wiklander return TEE_SUCCESS; 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 */ 6176c4ea258SJens Wiklander if (!key1 && !key2) { 6186c4ea258SJens Wiklander /* Clear the keys */ 619a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key1); 620a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key2); 6216c4ea258SJens Wiklander operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 6226c4ea258SJens Wiklander return TEE_SUCCESS; 6236c4ea258SJens Wiklander } else if (!key1 || !key2) { 6246c4ea258SJens Wiklander /* Both keys are obviously not valid. */ 625a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 626a57c1e2eSCedric Chaumont goto out; 627a57c1e2eSCedric Chaumont } 628a57c1e2eSCedric Chaumont 629a57c1e2eSCedric Chaumont /* No key for digest operation */ 630a57c1e2eSCedric Chaumont if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 631a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 632a57c1e2eSCedric Chaumont goto out; 633a57c1e2eSCedric Chaumont } 634a57c1e2eSCedric Chaumont 6355b385b3fSJerome Forissier /* Two keys flag expected (TEE_ALG_AES_XTS and TEE_ALG_SM2_KEP only) */ 636a57c1e2eSCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 637a57c1e2eSCedric Chaumont 0) { 638a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 639a57c1e2eSCedric Chaumont goto out; 640a57c1e2eSCedric Chaumont } 641a57c1e2eSCedric Chaumont 6427583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key1, &key_info1); 643a57c1e2eSCedric Chaumont /* Key1 is not a valid handle */ 6447583c59eSCedric Chaumont if (res != TEE_SUCCESS) 645a57c1e2eSCedric Chaumont goto out; 6467583c59eSCedric Chaumont 647b0104773SPascal Brand /* Supplied key has to meet required usage */ 648b0104773SPascal Brand if ((key_info1.objectUsage & operation->info. 649b0104773SPascal Brand requiredKeyUsage) != operation->info.requiredKeyUsage) { 650a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 651a57c1e2eSCedric Chaumont goto out; 652b0104773SPascal Brand } 653b0104773SPascal Brand 6547583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key2, &key_info2); 655a57c1e2eSCedric Chaumont /* Key2 is not a valid handle */ 6567583c59eSCedric Chaumont if (res != TEE_SUCCESS) { 6577583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) 6587583c59eSCedric Chaumont res = TEE_ERROR_CORRUPT_OBJECT_2; 659a57c1e2eSCedric Chaumont goto out; 6607583c59eSCedric Chaumont } 6617583c59eSCedric Chaumont 662b0104773SPascal Brand /* Supplied key has to meet required usage */ 663b0104773SPascal Brand if ((key_info2.objectUsage & operation->info. 664b0104773SPascal Brand requiredKeyUsage) != operation->info.requiredKeyUsage) { 665a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 666a57c1e2eSCedric Chaumont goto out; 667b0104773SPascal Brand } 668b0104773SPascal Brand 669b0104773SPascal Brand /* 6705b385b3fSJerome Forissier * All the multi key algorithm currently supported requires the keys to 6715b385b3fSJerome Forissier * be of equal size. 672b0104773SPascal Brand */ 6735b385b3fSJerome Forissier if (key_info1.keySize != key_info2.keySize) { 674a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 675a57c1e2eSCedric Chaumont goto out; 676b0104773SPascal Brand 677a57c1e2eSCedric Chaumont } 678a57c1e2eSCedric Chaumont 679a57c1e2eSCedric Chaumont if (operation->info.maxKeySize < key_info1.keySize) { 680a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 681a57c1e2eSCedric Chaumont goto out; 682a57c1e2eSCedric Chaumont } 683b0104773SPascal Brand 684b0104773SPascal Brand /* 685b0104773SPascal Brand * Odd that only the size of one key should be reported while 686b0104773SPascal Brand * size of two key are used when allocating the operation. 687b0104773SPascal Brand */ 6887583c59eSCedric Chaumont key_size = key_info1.keySize; 689b0104773SPascal Brand 690b0104773SPascal Brand TEE_ResetTransientObject(operation->key1); 691b0104773SPascal Brand TEE_ResetTransientObject(operation->key2); 692b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 693b0104773SPascal Brand 6947583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key1, key1); 6957583c59eSCedric Chaumont if (res != TEE_SUCCESS) 696a57c1e2eSCedric Chaumont goto out; 6977583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key2, key2); 6987583c59eSCedric Chaumont if (res != TEE_SUCCESS) { 6997583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) 7007583c59eSCedric Chaumont res = TEE_ERROR_CORRUPT_OBJECT_2; 701a57c1e2eSCedric Chaumont goto out; 7027583c59eSCedric Chaumont } 7037583c59eSCedric Chaumont 704b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 705b0104773SPascal Brand 706b0104773SPascal Brand operation->info.keySize = key_size; 707b0104773SPascal Brand 7087583c59eSCedric Chaumont out: 709a57c1e2eSCedric Chaumont if (res != TEE_SUCCESS && 710a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 711a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT_2 && 712a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE && 713a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE_2) 714b36311adSJerome Forissier TEE_Panic(res); 715a57c1e2eSCedric Chaumont 716a57c1e2eSCedric Chaumont return res; 717b0104773SPascal Brand } 718b0104773SPascal Brand 719b0104773SPascal Brand void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op) 720b0104773SPascal Brand { 721b0104773SPascal Brand TEE_Result res; 722b0104773SPascal Brand 723b0104773SPascal Brand if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL) 724b0104773SPascal Brand TEE_Panic(0); 725b0104773SPascal Brand if (dst_op->info.algorithm != src_op->info.algorithm) 726b0104773SPascal Brand TEE_Panic(0); 7278734de30SJens Wiklander if (dst_op->info.mode != src_op->info.mode) 7288734de30SJens Wiklander TEE_Panic(0); 729b0104773SPascal Brand if (src_op->info.operationClass != TEE_OPERATION_DIGEST) { 730b0104773SPascal Brand TEE_ObjectHandle key1 = TEE_HANDLE_NULL; 731b0104773SPascal Brand TEE_ObjectHandle key2 = TEE_HANDLE_NULL; 732b0104773SPascal Brand 733b0104773SPascal Brand if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) { 734b0104773SPascal Brand key1 = src_op->key1; 735b0104773SPascal Brand key2 = src_op->key2; 736b0104773SPascal Brand } 737b0104773SPascal Brand 738b0104773SPascal Brand if ((src_op->info.handleState & 739b0104773SPascal Brand TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) { 740b0104773SPascal Brand TEE_SetOperationKey(dst_op, key1); 741b0104773SPascal Brand } else { 742b0104773SPascal Brand TEE_SetOperationKey2(dst_op, key1, key2); 743b0104773SPascal Brand } 744b0104773SPascal Brand } 745b0104773SPascal Brand dst_op->info.handleState = src_op->info.handleState; 746b0104773SPascal Brand dst_op->info.keySize = src_op->info.keySize; 7478e07702eSJens Wiklander dst_op->info.digestLength = src_op->info.digestLength; 748642a1607SCedric Chaumont dst_op->operationState = src_op->operationState; 749b0104773SPascal Brand 750b0104773SPascal Brand if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks || 751b0104773SPascal Brand dst_op->block_size != src_op->block_size) 752b0104773SPascal Brand TEE_Panic(0); 753b0104773SPascal Brand 754b0104773SPascal Brand if (dst_op->buffer != NULL) { 755b0104773SPascal Brand if (src_op->buffer == NULL) 756b0104773SPascal Brand TEE_Panic(0); 757b0104773SPascal Brand 758b0104773SPascal Brand memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs); 759b0104773SPascal Brand dst_op->buffer_offs = src_op->buffer_offs; 760b0104773SPascal Brand } else if (src_op->buffer != NULL) { 761b0104773SPascal Brand TEE_Panic(0); 762b0104773SPascal Brand } 763b0104773SPascal Brand 7642c028fdeSJerome Forissier res = _utee_cryp_state_copy(dst_op->state, src_op->state); 765b0104773SPascal Brand if (res != TEE_SUCCESS) 766b0104773SPascal Brand TEE_Panic(res); 767b0104773SPascal Brand } 768b0104773SPascal Brand 769b0104773SPascal Brand /* Cryptographic Operations API - Message Digest Functions */ 770b0104773SPascal Brand 7718f07fe6fSJerome Forissier static void init_hash_operation(TEE_OperationHandle operation, const void *IV, 7726d15db08SJerome Forissier uint32_t IVLen) 7736d15db08SJerome Forissier { 7746d15db08SJerome Forissier TEE_Result res; 7756d15db08SJerome Forissier 7766d15db08SJerome Forissier /* 7776d15db08SJerome Forissier * Note : IV and IVLen are never used in current implementation 7786d15db08SJerome Forissier * This is why coherent values of IV and IVLen are not checked 7796d15db08SJerome Forissier */ 7802c028fdeSJerome Forissier res = _utee_hash_init(operation->state, IV, IVLen); 7816d15db08SJerome Forissier if (res != TEE_SUCCESS) 7826d15db08SJerome Forissier TEE_Panic(res); 7836d15db08SJerome Forissier operation->buffer_offs = 0; 7846d15db08SJerome Forissier operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 7856d15db08SJerome Forissier } 7866d15db08SJerome Forissier 787b0104773SPascal Brand void TEE_DigestUpdate(TEE_OperationHandle operation, 7888f07fe6fSJerome Forissier const void *chunk, uint32_t chunkSize) 789b0104773SPascal Brand { 79073d6c3baSJoakim Bech TEE_Result res = TEE_ERROR_GENERIC; 791b0104773SPascal Brand 79273d6c3baSJoakim Bech if (operation == TEE_HANDLE_NULL || 79373d6c3baSJoakim Bech operation->info.operationClass != TEE_OPERATION_DIGEST) 794b0104773SPascal Brand TEE_Panic(0); 79573d6c3baSJoakim Bech 796642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 797642a1607SCedric Chaumont 7982c028fdeSJerome Forissier res = _utee_hash_update(operation->state, chunk, chunkSize); 799b0104773SPascal Brand if (res != TEE_SUCCESS) 800b0104773SPascal Brand TEE_Panic(res); 801b0104773SPascal Brand } 802b0104773SPascal Brand 8038f07fe6fSJerome Forissier TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void *chunk, 80479a3c601SCedric Chaumont uint32_t chunkLen, void *hash, uint32_t *hashLen) 805b0104773SPascal Brand { 80687c2f6b6SCedric Chaumont TEE_Result res; 807e86f1266SJens Wiklander uint64_t hl; 80887c2f6b6SCedric Chaumont 80987c2f6b6SCedric Chaumont if ((operation == TEE_HANDLE_NULL) || 81087c2f6b6SCedric Chaumont (!chunk && chunkLen) || 81187c2f6b6SCedric Chaumont (operation->info.operationClass != TEE_OPERATION_DIGEST)) { 81287c2f6b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 81387c2f6b6SCedric Chaumont goto out; 81487c2f6b6SCedric Chaumont } 8156915bbbbSJens Wiklander __utee_check_inout_annotation(hashLen, sizeof(*hashLen)); 81687c2f6b6SCedric Chaumont 817e86f1266SJens Wiklander hl = *hashLen; 8182c028fdeSJerome Forissier res = _utee_hash_final(operation->state, chunk, chunkLen, hash, &hl); 819e86f1266SJens Wiklander *hashLen = hl; 8206d15db08SJerome Forissier if (res != TEE_SUCCESS) 8216d15db08SJerome Forissier goto out; 8226d15db08SJerome Forissier 8236d15db08SJerome Forissier /* Reset operation state */ 8246d15db08SJerome Forissier init_hash_operation(operation, NULL, 0); 82587c2f6b6SCedric Chaumont 826642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 827642a1607SCedric Chaumont 82887c2f6b6SCedric Chaumont out: 82987c2f6b6SCedric Chaumont if (res != TEE_SUCCESS && 83087c2f6b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 831b36311adSJerome Forissier TEE_Panic(res); 83273d6c3baSJoakim Bech 83387c2f6b6SCedric Chaumont return res; 834b0104773SPascal Brand } 835b0104773SPascal Brand 836b0104773SPascal Brand /* Cryptographic Operations API - Symmetric Cipher Functions */ 837b0104773SPascal Brand 8388f07fe6fSJerome Forissier void TEE_CipherInit(TEE_OperationHandle operation, const void *IV, 8398f07fe6fSJerome Forissier uint32_t IVLen) 840b0104773SPascal Brand { 841b0104773SPascal Brand TEE_Result res; 842b0104773SPascal Brand 843b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 844b0104773SPascal Brand TEE_Panic(0); 845642a1607SCedric Chaumont 846b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_CIPHER) 847b0104773SPascal Brand TEE_Panic(0); 848642a1607SCedric Chaumont 849642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 850642a1607SCedric Chaumont !(operation->key1)) 851642a1607SCedric Chaumont TEE_Panic(0); 852642a1607SCedric Chaumont 853642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 854642a1607SCedric Chaumont TEE_ResetOperation(operation); 855642a1607SCedric Chaumont 856ad7aa2a5SSadiq Hussain if (IV && IVLen) { 857ad7aa2a5SSadiq Hussain if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD || 858ad7aa2a5SSadiq Hussain operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD || 859ad7aa2a5SSadiq Hussain operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || 860ad7aa2a5SSadiq Hussain operation->info.algorithm == TEE_ALG_SM4_ECB_NOPAD) 861ad7aa2a5SSadiq Hussain TEE_Panic(0); 862ad7aa2a5SSadiq Hussain } 863ad7aa2a5SSadiq Hussain 864642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 865642a1607SCedric Chaumont 8662c028fdeSJerome Forissier res = _utee_cipher_init(operation->state, IV, IVLen); 867b0104773SPascal Brand if (res != TEE_SUCCESS) 868b0104773SPascal Brand TEE_Panic(res); 869642a1607SCedric Chaumont 870b0104773SPascal Brand operation->buffer_offs = 0; 871b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 872b0104773SPascal Brand } 873b0104773SPascal Brand 874b0104773SPascal Brand static TEE_Result tee_buffer_update( 875b0104773SPascal Brand TEE_OperationHandle op, 876e86f1266SJens Wiklander TEE_Result(*update_func)(unsigned long state, const void *src, 877e86f1266SJens Wiklander size_t slen, void *dst, uint64_t *dlen), 878b0104773SPascal Brand const void *src_data, size_t src_len, 879e86f1266SJens Wiklander void *dest_data, uint64_t *dest_len) 880b0104773SPascal Brand { 881b0104773SPascal Brand TEE_Result res; 882b0104773SPascal Brand const uint8_t *src = src_data; 883b0104773SPascal Brand size_t slen = src_len; 884b0104773SPascal Brand uint8_t *dst = dest_data; 885b0104773SPascal Brand size_t dlen = *dest_len; 886b0104773SPascal Brand size_t acc_dlen = 0; 887e86f1266SJens Wiklander uint64_t tmp_dlen; 888b0104773SPascal Brand size_t l; 889b0104773SPascal Brand size_t buffer_size; 890d3588802SPascal Brand size_t buffer_left; 891b0104773SPascal Brand 892090268f5SJens Wiklander if (!src) { 893090268f5SJens Wiklander if (slen) 894090268f5SJens Wiklander TEE_Panic(0); 895090268f5SJens Wiklander goto out; 896090268f5SJens Wiklander } 897090268f5SJens Wiklander 898d3588802SPascal Brand if (op->buffer_two_blocks) { 899b0104773SPascal Brand buffer_size = op->block_size * 2; 900d3588802SPascal Brand buffer_left = 1; 901d3588802SPascal Brand } else { 902b0104773SPascal Brand buffer_size = op->block_size; 903d3588802SPascal Brand buffer_left = 0; 904d3588802SPascal Brand } 905b0104773SPascal Brand 906b0104773SPascal Brand if (op->buffer_offs > 0) { 907b0104773SPascal Brand /* Fill up complete block */ 908b0104773SPascal Brand if (op->buffer_offs < op->block_size) 909b0104773SPascal Brand l = MIN(slen, op->block_size - op->buffer_offs); 910b0104773SPascal Brand else 911b0104773SPascal Brand l = MIN(slen, buffer_size - op->buffer_offs); 912b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, l); 913b0104773SPascal Brand op->buffer_offs += l; 914b0104773SPascal Brand src += l; 915b0104773SPascal Brand slen -= l; 916b0104773SPascal Brand if ((op->buffer_offs % op->block_size) != 0) 917b0104773SPascal Brand goto out; /* Nothing left to do */ 918b0104773SPascal Brand } 919b0104773SPascal Brand 920b0104773SPascal Brand /* If we can feed from buffer */ 921d3588802SPascal Brand if ((op->buffer_offs > 0) && 922d3588802SPascal Brand ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) { 9232ff3fdbbSPascal Brand l = ROUNDUP(op->buffer_offs + slen - buffer_size, 924b0104773SPascal Brand op->block_size); 925b0104773SPascal Brand l = MIN(op->buffer_offs, l); 926b0104773SPascal Brand tmp_dlen = dlen; 927b0104773SPascal Brand res = update_func(op->state, op->buffer, l, dst, &tmp_dlen); 928b0104773SPascal Brand if (res != TEE_SUCCESS) 929b0104773SPascal Brand TEE_Panic(res); 930b0104773SPascal Brand dst += tmp_dlen; 931b0104773SPascal Brand dlen -= tmp_dlen; 932b0104773SPascal Brand acc_dlen += tmp_dlen; 933b0104773SPascal Brand op->buffer_offs -= l; 934b0104773SPascal Brand if (op->buffer_offs > 0) { 935b0104773SPascal Brand /* 936b0104773SPascal Brand * Slen is small enough to be contained in rest buffer. 937b0104773SPascal Brand */ 938b0104773SPascal Brand memcpy(op->buffer, op->buffer + l, buffer_size - l); 939b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 940b0104773SPascal Brand op->buffer_offs += slen; 941b0104773SPascal Brand goto out; /* Nothing left to do */ 942b0104773SPascal Brand } 943b0104773SPascal Brand } 944b0104773SPascal Brand 945d3588802SPascal Brand if (slen >= (buffer_size + buffer_left)) { 946b0104773SPascal Brand /* Buffer is empty, feed as much as possible from src */ 947bf7a587fSJerome Forissier if (op->info.algorithm == TEE_ALG_AES_CTS) 948b1ecda78SJerome Forissier l = ROUNDUP(slen - buffer_size, op->block_size); 949bf7a587fSJerome Forissier else 950bf7a587fSJerome Forissier l = ROUNDUP(slen - buffer_size + 1, op->block_size); 951b0104773SPascal Brand 952b0104773SPascal Brand tmp_dlen = dlen; 953b0104773SPascal Brand res = update_func(op->state, src, l, dst, &tmp_dlen); 954b0104773SPascal Brand if (res != TEE_SUCCESS) 955b0104773SPascal Brand TEE_Panic(res); 956b0104773SPascal Brand src += l; 957b0104773SPascal Brand slen -= l; 958b0104773SPascal Brand dst += tmp_dlen; 959b0104773SPascal Brand dlen -= tmp_dlen; 960b0104773SPascal Brand acc_dlen += tmp_dlen; 961b0104773SPascal Brand } 962b0104773SPascal Brand 963b0104773SPascal Brand /* Slen is small enough to be contained in buffer. */ 964b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 965b0104773SPascal Brand op->buffer_offs += slen; 966b0104773SPascal Brand 967b0104773SPascal Brand out: 968b0104773SPascal Brand *dest_len = acc_dlen; 969b0104773SPascal Brand return TEE_SUCCESS; 970b0104773SPascal Brand } 971b0104773SPascal Brand 9728f07fe6fSJerome Forissier TEE_Result TEE_CipherUpdate(TEE_OperationHandle operation, const void *srcData, 97379a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 974b0104773SPascal Brand { 975dea1f2b6SCedric Chaumont TEE_Result res; 976b0104773SPascal Brand size_t req_dlen; 977e86f1266SJens Wiklander uint64_t dl; 978b0104773SPascal Brand 9796915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 980dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 981dea1f2b6SCedric Chaumont goto out; 982dea1f2b6SCedric Chaumont } 9836915bbbbSJens Wiklander __utee_check_inout_annotation(destLen, sizeof(*destLen)); 984dea1f2b6SCedric Chaumont 985642a1607SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 986dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 987dea1f2b6SCedric Chaumont goto out; 988dea1f2b6SCedric Chaumont } 989dea1f2b6SCedric Chaumont 990642a1607SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 991642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 992642a1607SCedric Chaumont goto out; 993642a1607SCedric Chaumont } 994642a1607SCedric Chaumont 995642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 996dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 997dea1f2b6SCedric Chaumont goto out; 998dea1f2b6SCedric Chaumont } 999b0104773SPascal Brand 1000e32c5ddfSJerome Forissier if (!srcData && !srcLen) { 1001090268f5SJens Wiklander *destLen = 0; 1002e32c5ddfSJerome Forissier res = TEE_SUCCESS; 1003e32c5ddfSJerome Forissier goto out; 1004e32c5ddfSJerome Forissier } 1005e32c5ddfSJerome Forissier 1006b0104773SPascal Brand /* Calculate required dlen */ 100757aabac5SBogdan Liulko if (operation->block_size > 1) { 100857aabac5SBogdan Liulko req_dlen = ((operation->buffer_offs + srcLen) / 100957aabac5SBogdan Liulko operation->block_size) * operation->block_size; 101057aabac5SBogdan Liulko } else { 101157aabac5SBogdan Liulko req_dlen = srcLen; 101257aabac5SBogdan Liulko } 1013642a1607SCedric Chaumont if (operation->buffer_two_blocks) { 1014642a1607SCedric Chaumont if (req_dlen > operation->block_size * 2) 1015642a1607SCedric Chaumont req_dlen -= operation->block_size * 2; 1016b0104773SPascal Brand else 1017b0104773SPascal Brand req_dlen = 0; 1018b0104773SPascal Brand } 1019b0104773SPascal Brand /* 1020b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1021b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1022b0104773SPascal Brand * can't restore sync with this API. 1023b0104773SPascal Brand */ 1024b0104773SPascal Brand if (*destLen < req_dlen) { 1025b0104773SPascal Brand *destLen = req_dlen; 1026dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1027dea1f2b6SCedric Chaumont goto out; 1028b0104773SPascal Brand } 1029b0104773SPascal Brand 1030e86f1266SJens Wiklander dl = *destLen; 103157aabac5SBogdan Liulko if (operation->block_size > 1) { 10322c028fdeSJerome Forissier res = tee_buffer_update(operation, _utee_cipher_update, srcData, 103357aabac5SBogdan Liulko srcLen, destData, &dl); 103457aabac5SBogdan Liulko } else { 103557aabac5SBogdan Liulko if (srcLen > 0) { 10362c028fdeSJerome Forissier res = _utee_cipher_update(operation->state, srcData, 103757aabac5SBogdan Liulko srcLen, destData, &dl); 103857aabac5SBogdan Liulko } else { 103957aabac5SBogdan Liulko res = TEE_SUCCESS; 104057aabac5SBogdan Liulko dl = 0; 104157aabac5SBogdan Liulko } 104257aabac5SBogdan Liulko } 1043e86f1266SJens Wiklander *destLen = dl; 1044b0104773SPascal Brand 1045dea1f2b6SCedric Chaumont out: 1046dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 1047dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1048b36311adSJerome Forissier TEE_Panic(res); 1049dea1f2b6SCedric Chaumont 1050dea1f2b6SCedric Chaumont return res; 1051b0104773SPascal Brand } 1052b0104773SPascal Brand 1053642a1607SCedric Chaumont TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation, 10548f07fe6fSJerome Forissier const void *srcData, uint32_t srcLen, 10558f07fe6fSJerome Forissier void *destData, uint32_t *destLen) 1056b0104773SPascal Brand { 10576915bbbbSJens Wiklander TEE_Result res = TEE_SUCCESS; 1058b0104773SPascal Brand uint8_t *dst = destData; 1059b0104773SPascal Brand size_t acc_dlen = 0; 10606915bbbbSJens Wiklander uint64_t tmp_dlen = 0; 10616915bbbbSJens Wiklander size_t req_dlen = 0; 1062b0104773SPascal Brand 10636915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 1064dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1065dea1f2b6SCedric Chaumont goto out; 1066dea1f2b6SCedric Chaumont } 10676915bbbbSJens Wiklander if (destLen) 10686915bbbbSJens Wiklander __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1069dea1f2b6SCedric Chaumont 1070642a1607SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 1071dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1072dea1f2b6SCedric Chaumont goto out; 1073dea1f2b6SCedric Chaumont } 1074dea1f2b6SCedric Chaumont 1075642a1607SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1076642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1077642a1607SCedric Chaumont goto out; 1078642a1607SCedric Chaumont } 1079642a1607SCedric Chaumont 1080642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1081dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1082dea1f2b6SCedric Chaumont goto out; 1083dea1f2b6SCedric Chaumont } 1084b0104773SPascal Brand 1085b0104773SPascal Brand /* 1086b0104773SPascal Brand * Check that the final block doesn't require padding for those 1087b0104773SPascal Brand * algorithms that requires client to supply padding. 1088b0104773SPascal Brand */ 1089642a1607SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD || 1090642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD || 1091642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD || 1092642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD || 1093642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || 1094ade6f848SJerome Forissier operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD || 1095ade6f848SJerome Forissier operation->info.algorithm == TEE_ALG_SM4_ECB_NOPAD || 1096ade6f848SJerome Forissier operation->info.algorithm == TEE_ALG_SM4_CBC_NOPAD) { 1097642a1607SCedric Chaumont if (((operation->buffer_offs + srcLen) % operation->block_size) 1098642a1607SCedric Chaumont != 0) { 1099dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1100dea1f2b6SCedric Chaumont goto out; 1101dea1f2b6SCedric Chaumont } 1102b0104773SPascal Brand } 1103b0104773SPascal Brand 1104b0104773SPascal Brand /* 1105b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1106b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1107b0104773SPascal Brand * can't restore sync with this API. 1108b0104773SPascal Brand */ 110957aabac5SBogdan Liulko if (operation->block_size > 1) { 1110642a1607SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 111157aabac5SBogdan Liulko } else { 111257aabac5SBogdan Liulko req_dlen = srcLen; 111357aabac5SBogdan Liulko } 11146915bbbbSJens Wiklander if (destLen) 11156915bbbbSJens Wiklander tmp_dlen = *destLen; 11166915bbbbSJens Wiklander if (tmp_dlen < req_dlen) { 11176915bbbbSJens Wiklander if (destLen) 1118b0104773SPascal Brand *destLen = req_dlen; 1119dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1120dea1f2b6SCedric Chaumont goto out; 1121b0104773SPascal Brand } 1122b0104773SPascal Brand 112357aabac5SBogdan Liulko if (operation->block_size > 1) { 1124dea9063eSJens Wiklander if (srcLen) { 11252c028fdeSJerome Forissier res = tee_buffer_update(operation, _utee_cipher_update, 1126dea9063eSJens Wiklander srcData, srcLen, dst, 1127dea9063eSJens Wiklander &tmp_dlen); 1128dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS) 1129dea1f2b6SCedric Chaumont goto out; 1130dea1f2b6SCedric Chaumont 1131b0104773SPascal Brand dst += tmp_dlen; 1132b0104773SPascal Brand acc_dlen += tmp_dlen; 1133b0104773SPascal Brand 1134b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1135dea9063eSJens Wiklander } 11362c028fdeSJerome Forissier res = _utee_cipher_final(operation->state, operation->buffer, 11372c028fdeSJerome Forissier operation->buffer_offs, dst, 11382c028fdeSJerome Forissier &tmp_dlen); 113957aabac5SBogdan Liulko } else { 11402c028fdeSJerome Forissier res = _utee_cipher_final(operation->state, srcData, srcLen, dst, 11412c028fdeSJerome Forissier &tmp_dlen); 114257aabac5SBogdan Liulko } 1143b0104773SPascal Brand if (res != TEE_SUCCESS) 1144dea1f2b6SCedric Chaumont goto out; 1145dea1f2b6SCedric Chaumont 1146b0104773SPascal Brand acc_dlen += tmp_dlen; 11476915bbbbSJens Wiklander if (destLen) 1148b0104773SPascal Brand *destLen = acc_dlen; 1149dea1f2b6SCedric Chaumont 1150642a1607SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1151642a1607SCedric Chaumont 1152642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1153642a1607SCedric Chaumont 1154dea1f2b6SCedric Chaumont out: 1155dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 1156dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1157b36311adSJerome Forissier TEE_Panic(res); 1158dea1f2b6SCedric Chaumont 1159dea1f2b6SCedric Chaumont return res; 1160b0104773SPascal Brand } 1161b0104773SPascal Brand 1162b0104773SPascal Brand /* Cryptographic Operations API - MAC Functions */ 1163b0104773SPascal Brand 11648f07fe6fSJerome Forissier void TEE_MACInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen) 1165b0104773SPascal Brand { 1166b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 1167b0104773SPascal Brand TEE_Panic(0); 1168642a1607SCedric Chaumont 1169b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_MAC) 1170b0104773SPascal Brand TEE_Panic(0); 1171642a1607SCedric Chaumont 1172642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 1173642a1607SCedric Chaumont !(operation->key1)) 1174642a1607SCedric Chaumont TEE_Panic(0); 1175642a1607SCedric Chaumont 1176642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 1177642a1607SCedric Chaumont TEE_ResetOperation(operation); 1178642a1607SCedric Chaumont 1179642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1180642a1607SCedric Chaumont 11816d15db08SJerome Forissier init_hash_operation(operation, IV, IVLen); 1182b0104773SPascal Brand } 1183b0104773SPascal Brand 11848f07fe6fSJerome Forissier void TEE_MACUpdate(TEE_OperationHandle operation, const void *chunk, 118528e0efc6SCedric Chaumont uint32_t chunkSize) 1186b0104773SPascal Brand { 1187b0104773SPascal Brand TEE_Result res; 1188b0104773SPascal Brand 118928e0efc6SCedric Chaumont if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0)) 1190b0104773SPascal Brand TEE_Panic(0); 1191642a1607SCedric Chaumont 119228e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) 1193b0104773SPascal Brand TEE_Panic(0); 1194642a1607SCedric Chaumont 119528e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1196b0104773SPascal Brand TEE_Panic(0); 1197b0104773SPascal Brand 1198642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) 1199642a1607SCedric Chaumont TEE_Panic(0); 1200642a1607SCedric Chaumont 12012c028fdeSJerome Forissier res = _utee_hash_update(operation->state, chunk, chunkSize); 1202b0104773SPascal Brand if (res != TEE_SUCCESS) 1203b0104773SPascal Brand TEE_Panic(res); 1204b0104773SPascal Brand } 1205b0104773SPascal Brand 120628e0efc6SCedric Chaumont TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation, 12078f07fe6fSJerome Forissier const void *message, uint32_t messageLen, 120879a3c601SCedric Chaumont void *mac, uint32_t *macLen) 1209b0104773SPascal Brand { 1210b0104773SPascal Brand TEE_Result res; 1211e86f1266SJens Wiklander uint64_t ml; 1212b0104773SPascal Brand 12136915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!message && messageLen)) { 121428e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 121528e0efc6SCedric Chaumont goto out; 121628e0efc6SCedric Chaumont } 12176915bbbbSJens Wiklander __utee_check_inout_annotation(macLen, sizeof(*macLen)); 1218b0104773SPascal Brand 121928e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 122028e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 122128e0efc6SCedric Chaumont goto out; 122228e0efc6SCedric Chaumont } 122328e0efc6SCedric Chaumont 122428e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 122528e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 122628e0efc6SCedric Chaumont goto out; 122728e0efc6SCedric Chaumont } 122828e0efc6SCedric Chaumont 1229642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1230642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1231642a1607SCedric Chaumont goto out; 1232642a1607SCedric Chaumont } 1233642a1607SCedric Chaumont 1234e86f1266SJens Wiklander ml = *macLen; 12352c028fdeSJerome Forissier res = _utee_hash_final(operation->state, message, messageLen, mac, &ml); 1236e86f1266SJens Wiklander *macLen = ml; 123728e0efc6SCedric Chaumont if (res != TEE_SUCCESS) 123828e0efc6SCedric Chaumont goto out; 123928e0efc6SCedric Chaumont 124028e0efc6SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 124128e0efc6SCedric Chaumont 1242642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1243642a1607SCedric Chaumont 124428e0efc6SCedric Chaumont out: 124528e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 124628e0efc6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 124728e0efc6SCedric Chaumont TEE_Panic(res); 124828e0efc6SCedric Chaumont 1249b0104773SPascal Brand return res; 1250b0104773SPascal Brand } 1251b0104773SPascal Brand 1252b0104773SPascal Brand TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation, 12538f07fe6fSJerome Forissier const void *message, uint32_t messageLen, 12548f07fe6fSJerome Forissier const void *mac, uint32_t macLen) 1255b0104773SPascal Brand { 1256b0104773SPascal Brand TEE_Result res; 1257ee4ba3d1SVictor Chong uint8_t computed_mac[TEE_MAX_HASH_SIZE] = { 0 }; 12587f74c64aSPascal Brand uint32_t computed_mac_size = TEE_MAX_HASH_SIZE; 1259b0104773SPascal Brand 126028e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 126128e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 126228e0efc6SCedric Chaumont goto out; 126328e0efc6SCedric Chaumont } 126428e0efc6SCedric Chaumont 126528e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 126628e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 126728e0efc6SCedric Chaumont goto out; 126828e0efc6SCedric Chaumont } 126928e0efc6SCedric Chaumont 1270642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1271642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1272642a1607SCedric Chaumont goto out; 1273642a1607SCedric Chaumont } 1274642a1607SCedric Chaumont 1275b0104773SPascal Brand res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac, 1276b0104773SPascal Brand &computed_mac_size); 1277b0104773SPascal Brand if (res != TEE_SUCCESS) 127828e0efc6SCedric Chaumont goto out; 127928e0efc6SCedric Chaumont 128028e0efc6SCedric Chaumont if (computed_mac_size != macLen) { 128128e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 128228e0efc6SCedric Chaumont goto out; 128328e0efc6SCedric Chaumont } 128428e0efc6SCedric Chaumont 128548e10604SJerome Forissier if (consttime_memcmp(mac, computed_mac, computed_mac_size) != 0) { 128628e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 128728e0efc6SCedric Chaumont goto out; 128828e0efc6SCedric Chaumont } 128928e0efc6SCedric Chaumont 1290642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1291642a1607SCedric Chaumont 129228e0efc6SCedric Chaumont out: 129328e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 129428e0efc6SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 129528e0efc6SCedric Chaumont TEE_Panic(res); 129628e0efc6SCedric Chaumont 1297b0104773SPascal Brand return res; 1298b0104773SPascal Brand } 1299b0104773SPascal Brand 1300b0104773SPascal Brand /* Cryptographic Operations API - Authenticated Encryption Functions */ 1301b0104773SPascal Brand 13028f07fe6fSJerome Forissier TEE_Result TEE_AEInit(TEE_OperationHandle operation, const void *nonce, 130379a3c601SCedric Chaumont uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen, 1304b0104773SPascal Brand uint32_t payloadLen) 1305b0104773SPascal Brand { 1306b0104773SPascal Brand TEE_Result res; 1307b0104773SPascal Brand 1308b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || nonce == NULL) { 1309b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1310b5816c88SCedric Chaumont goto out; 1311b5816c88SCedric Chaumont } 1312b5816c88SCedric Chaumont 1313b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1314b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1315b5816c88SCedric Chaumont goto out; 1316b5816c88SCedric Chaumont } 1317b0104773SPascal Brand 1318642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 1319642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1320642a1607SCedric Chaumont goto out; 1321642a1607SCedric Chaumont } 1322642a1607SCedric Chaumont 1323b0104773SPascal Brand /* 1324b0104773SPascal Brand * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core 1325b0104773SPascal Brand * in the implementation. But AES-GCM spec doesn't specify the tag len 1326b0104773SPascal Brand * according to the same principle so we have to check here instead to 1327b0104773SPascal Brand * be GP compliant. 1328b0104773SPascal Brand */ 1329b5816c88SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_GCM) { 1330b0104773SPascal Brand /* 1331b0104773SPascal Brand * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96 1332b0104773SPascal Brand */ 1333b5816c88SCedric Chaumont if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) { 1334b5816c88SCedric Chaumont res = TEE_ERROR_NOT_SUPPORTED; 1335b5816c88SCedric Chaumont goto out; 1336b5816c88SCedric Chaumont } 1337b0104773SPascal Brand } 1338b0104773SPascal Brand 13392c028fdeSJerome Forissier res = _utee_authenc_init(operation->state, nonce, nonceLen, tagLen / 8, 13402c028fdeSJerome Forissier AADLen, payloadLen); 1341b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1342b5816c88SCedric Chaumont goto out; 1343b5816c88SCedric Chaumont 13447acaf5adSAlbert Schwarzkopf operation->info.digestLength = tagLen / 8; 1345f2674567SSumit Garg operation->buffer_offs = 0; 1346b5816c88SCedric Chaumont operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 1347b5816c88SCedric Chaumont 1348b5816c88SCedric Chaumont out: 1349b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1350b5816c88SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 1351b0104773SPascal Brand TEE_Panic(res); 1352b5816c88SCedric Chaumont 1353b0104773SPascal Brand return res; 1354b0104773SPascal Brand } 1355b0104773SPascal Brand 13568f07fe6fSJerome Forissier void TEE_AEUpdateAAD(TEE_OperationHandle operation, const void *AADdata, 135779a3c601SCedric Chaumont uint32_t AADdataLen) 1358b0104773SPascal Brand { 1359b0104773SPascal Brand TEE_Result res; 1360b0104773SPascal Brand 1361b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1362b5816c88SCedric Chaumont (AADdata == NULL && AADdataLen != 0)) 1363b0104773SPascal Brand TEE_Panic(0); 1364642a1607SCedric Chaumont 1365b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) 1366b0104773SPascal Brand TEE_Panic(0); 1367642a1607SCedric Chaumont 1368b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1369b0104773SPascal Brand TEE_Panic(0); 1370b0104773SPascal Brand 13712c028fdeSJerome Forissier res = _utee_authenc_update_aad(operation->state, AADdata, AADdataLen); 1372642a1607SCedric Chaumont 1373642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1374642a1607SCedric Chaumont 1375b0104773SPascal Brand if (res != TEE_SUCCESS) 1376b0104773SPascal Brand TEE_Panic(res); 1377b0104773SPascal Brand } 1378b0104773SPascal Brand 13798f07fe6fSJerome Forissier TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, const void *srcData, 138079a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 1381b0104773SPascal Brand { 13826915bbbbSJens Wiklander TEE_Result res = TEE_SUCCESS; 13836915bbbbSJens Wiklander size_t req_dlen = 0; 13846915bbbbSJens Wiklander uint64_t dl = 0; 1385b0104773SPascal Brand 13866915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 1387b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1388b5816c88SCedric Chaumont goto out; 1389b5816c88SCedric Chaumont } 13906915bbbbSJens Wiklander __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1391b5816c88SCedric Chaumont 1392b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1393b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1394b5816c88SCedric Chaumont goto out; 1395b5816c88SCedric Chaumont } 1396b5816c88SCedric Chaumont 1397b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1398b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1399b5816c88SCedric Chaumont goto out; 1400b5816c88SCedric Chaumont } 1401b0104773SPascal Brand 1402827308b8SJerome Forissier if (!srcData && !srcLen) { 1403090268f5SJens Wiklander *destLen = 0; 1404827308b8SJerome Forissier res = TEE_SUCCESS; 1405827308b8SJerome Forissier goto out; 1406827308b8SJerome Forissier } 1407827308b8SJerome Forissier 1408b0104773SPascal Brand /* 1409b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1410b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1411b0104773SPascal Brand * can't restore sync with this API. 1412b0104773SPascal Brand */ 1413afc0c182SBogdan Liulko if (operation->block_size > 1) { 1414b5816c88SCedric Chaumont req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen, 1415b5816c88SCedric Chaumont operation->block_size); 1416afc0c182SBogdan Liulko } else { 1417afc0c182SBogdan Liulko req_dlen = srcLen; 1418afc0c182SBogdan Liulko } 1419afc0c182SBogdan Liulko 14206915bbbbSJens Wiklander dl = *destLen; 14216915bbbbSJens Wiklander if (dl < req_dlen) { 1422b0104773SPascal Brand *destLen = req_dlen; 1423b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1424b5816c88SCedric Chaumont goto out; 1425b0104773SPascal Brand } 1426b0104773SPascal Brand 1427afc0c182SBogdan Liulko if (operation->block_size > 1) { 14282c028fdeSJerome Forissier res = tee_buffer_update(operation, _utee_authenc_update_payload, 1429afc0c182SBogdan Liulko srcData, srcLen, destData, &dl); 1430afc0c182SBogdan Liulko } else { 1431afc0c182SBogdan Liulko if (srcLen > 0) { 14322c028fdeSJerome Forissier res = _utee_authenc_update_payload(operation->state, 1433afc0c182SBogdan Liulko srcData, srcLen, 1434afc0c182SBogdan Liulko destData, &dl); 1435afc0c182SBogdan Liulko } else { 1436afc0c182SBogdan Liulko dl = 0; 1437afc0c182SBogdan Liulko res = TEE_SUCCESS; 1438afc0c182SBogdan Liulko } 1439afc0c182SBogdan Liulko } 1440afc0c182SBogdan Liulko if (res != TEE_SUCCESS) 1441afc0c182SBogdan Liulko goto out; 1442afc0c182SBogdan Liulko 1443e86f1266SJens Wiklander *destLen = dl; 1444b0104773SPascal Brand 1445642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1446642a1607SCedric Chaumont 1447b5816c88SCedric Chaumont out: 1448b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1449b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1450b5816c88SCedric Chaumont TEE_Panic(res); 1451b5816c88SCedric Chaumont 1452b5816c88SCedric Chaumont return res; 1453b0104773SPascal Brand } 1454b0104773SPascal Brand 1455b5816c88SCedric Chaumont TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation, 14568f07fe6fSJerome Forissier const void *srcData, uint32_t srcLen, 145779a3c601SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 145879a3c601SCedric Chaumont uint32_t *tagLen) 1459b0104773SPascal Brand { 1460b0104773SPascal Brand TEE_Result res; 1461b0104773SPascal Brand uint8_t *dst = destData; 1462b0104773SPascal Brand size_t acc_dlen = 0; 1463e86f1266SJens Wiklander uint64_t tmp_dlen; 1464b0104773SPascal Brand size_t req_dlen; 1465e86f1266SJens Wiklander uint64_t tl; 1466b0104773SPascal Brand 14676915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 1468b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1469b5816c88SCedric Chaumont goto out; 1470b5816c88SCedric Chaumont } 14716915bbbbSJens Wiklander __utee_check_inout_annotation(destLen, sizeof(*destLen)); 14726915bbbbSJens Wiklander __utee_check_inout_annotation(tagLen, sizeof(*tagLen)); 1473b5816c88SCedric Chaumont 1474b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1475b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1476b5816c88SCedric Chaumont goto out; 1477b5816c88SCedric Chaumont } 1478b5816c88SCedric Chaumont 1479b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1480b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1481b5816c88SCedric Chaumont goto out; 1482b5816c88SCedric Chaumont } 1483b0104773SPascal Brand 1484b0104773SPascal Brand /* 1485b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1486b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1487b0104773SPascal Brand * can't restore sync with this API. 14882733280aSEtienne Carriere * 14892733280aSEtienne Carriere * Need to check this before update_payload since sync would be lost if 14902733280aSEtienne Carriere * we return short buffer after that. 1491b0104773SPascal Brand */ 14922733280aSEtienne Carriere res = TEE_ERROR_GENERIC; 14932733280aSEtienne Carriere 1494b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1495b0104773SPascal Brand if (*destLen < req_dlen) { 1496b0104773SPascal Brand *destLen = req_dlen; 1497b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1498b0104773SPascal Brand } 1499b0104773SPascal Brand 15007acaf5adSAlbert Schwarzkopf if (*tagLen < operation->info.digestLength) { 15017acaf5adSAlbert Schwarzkopf *tagLen = operation->info.digestLength; 1502b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1503b0104773SPascal Brand } 1504b0104773SPascal Brand 15052733280aSEtienne Carriere if (res == TEE_ERROR_SHORT_BUFFER) 15062733280aSEtienne Carriere goto out; 15072733280aSEtienne Carriere 1508afc0c182SBogdan Liulko tl = *tagLen; 1509b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1510afc0c182SBogdan Liulko if (operation->block_size > 1) { 15112c028fdeSJerome Forissier res = tee_buffer_update(operation, _utee_authenc_update_payload, 1512afc0c182SBogdan Liulko srcData, srcLen, dst, &tmp_dlen); 1513b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1514b5816c88SCedric Chaumont goto out; 1515b5816c88SCedric Chaumont 1516b0104773SPascal Brand dst += tmp_dlen; 1517b0104773SPascal Brand acc_dlen += tmp_dlen; 1518b0104773SPascal Brand 1519b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 15202c028fdeSJerome Forissier res = _utee_authenc_enc_final(operation->state, 1521afc0c182SBogdan Liulko operation->buffer, 1522afc0c182SBogdan Liulko operation->buffer_offs, dst, 1523afc0c182SBogdan Liulko &tmp_dlen, tag, &tl); 1524afc0c182SBogdan Liulko } else { 15252c028fdeSJerome Forissier res = _utee_authenc_enc_final(operation->state, srcData, 1526afc0c182SBogdan Liulko srcLen, dst, &tmp_dlen, 1527e86f1266SJens Wiklander tag, &tl); 1528afc0c182SBogdan Liulko } 1529e86f1266SJens Wiklander *tagLen = tl; 1530b0104773SPascal Brand if (res != TEE_SUCCESS) 1531b5816c88SCedric Chaumont goto out; 1532b0104773SPascal Brand 1533b5816c88SCedric Chaumont acc_dlen += tmp_dlen; 1534b0104773SPascal Brand *destLen = acc_dlen; 1535642a1607SCedric Chaumont 1536b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1537b5816c88SCedric Chaumont 1538642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1539642a1607SCedric Chaumont 1540b5816c88SCedric Chaumont out: 1541b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1542b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1543b5816c88SCedric Chaumont TEE_Panic(res); 1544b0104773SPascal Brand 1545b0104773SPascal Brand return res; 1546b0104773SPascal Brand } 1547b0104773SPascal Brand 1548b5816c88SCedric Chaumont TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation, 15498f07fe6fSJerome Forissier const void *srcData, uint32_t srcLen, 1550b5816c88SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 155179a3c601SCedric Chaumont uint32_t tagLen) 1552b0104773SPascal Brand { 1553b0104773SPascal Brand TEE_Result res; 1554b0104773SPascal Brand uint8_t *dst = destData; 1555b0104773SPascal Brand size_t acc_dlen = 0; 1556e86f1266SJens Wiklander uint64_t tmp_dlen; 1557b0104773SPascal Brand size_t req_dlen; 1558b0104773SPascal Brand 15596915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 1560b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1561b5816c88SCedric Chaumont goto out; 1562b5816c88SCedric Chaumont } 15636915bbbbSJens Wiklander __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1564b5816c88SCedric Chaumont 1565b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1566b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1567b5816c88SCedric Chaumont goto out; 1568b5816c88SCedric Chaumont } 1569b5816c88SCedric Chaumont 1570b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1571b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1572b5816c88SCedric Chaumont goto out; 1573b5816c88SCedric Chaumont } 1574b0104773SPascal Brand 1575b0104773SPascal Brand /* 1576b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1577b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1578b0104773SPascal Brand * can't restore sync with this API. 1579b0104773SPascal Brand */ 1580b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1581b0104773SPascal Brand if (*destLen < req_dlen) { 1582b0104773SPascal Brand *destLen = req_dlen; 1583b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1584b5816c88SCedric Chaumont goto out; 1585b0104773SPascal Brand } 1586b0104773SPascal Brand 1587b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1588afc0c182SBogdan Liulko if (operation->block_size > 1) { 15892c028fdeSJerome Forissier res = tee_buffer_update(operation, _utee_authenc_update_payload, 1590afc0c182SBogdan Liulko srcData, srcLen, dst, &tmp_dlen); 1591b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1592b5816c88SCedric Chaumont goto out; 1593b5816c88SCedric Chaumont 1594b0104773SPascal Brand dst += tmp_dlen; 1595b0104773SPascal Brand acc_dlen += tmp_dlen; 1596b0104773SPascal Brand 1597b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 15982c028fdeSJerome Forissier res = _utee_authenc_dec_final(operation->state, 1599afc0c182SBogdan Liulko operation->buffer, 1600afc0c182SBogdan Liulko operation->buffer_offs, dst, 1601afc0c182SBogdan Liulko &tmp_dlen, tag, tagLen); 1602afc0c182SBogdan Liulko } else { 16032c028fdeSJerome Forissier res = _utee_authenc_dec_final(operation->state, srcData, 1604afc0c182SBogdan Liulko srcLen, dst, &tmp_dlen, 1605b5816c88SCedric Chaumont tag, tagLen); 1606afc0c182SBogdan Liulko } 1607b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1608b5816c88SCedric Chaumont goto out; 1609b5816c88SCedric Chaumont 1610b0104773SPascal Brand /* Supplied tagLen should match what we initiated with */ 16117acaf5adSAlbert Schwarzkopf if (tagLen != operation->info.digestLength) 1612b0104773SPascal Brand res = TEE_ERROR_MAC_INVALID; 1613b0104773SPascal Brand 1614b0104773SPascal Brand acc_dlen += tmp_dlen; 1615b0104773SPascal Brand *destLen = acc_dlen; 1616642a1607SCedric Chaumont 1617b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1618b5816c88SCedric Chaumont 1619642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1620642a1607SCedric Chaumont 1621b5816c88SCedric Chaumont out: 1622b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1623b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER && 1624b5816c88SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 1625b5816c88SCedric Chaumont TEE_Panic(res); 1626b0104773SPascal Brand 1627b0104773SPascal Brand return res; 1628b0104773SPascal Brand } 1629b0104773SPascal Brand 1630b0104773SPascal Brand /* Cryptographic Operations API - Asymmetric Functions */ 1631b0104773SPascal Brand 163212e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation, 16338f07fe6fSJerome Forissier const TEE_Attribute *params, 16348f07fe6fSJerome Forissier uint32_t paramCount, const void *srcData, 163579a3c601SCedric Chaumont uint32_t srcLen, void *destData, 163679a3c601SCedric Chaumont uint32_t *destLen) 1637b0104773SPascal Brand { 16386915bbbbSJens Wiklander TEE_Result res = TEE_SUCCESS; 1639e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 16406915bbbbSJens Wiklander uint64_t dl = 0; 1641b0104773SPascal Brand 16426915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) 1643b0104773SPascal Brand TEE_Panic(0); 16446915bbbbSJens Wiklander 16456915bbbbSJens Wiklander __utee_check_attr_in_annotation(params, paramCount); 16466915bbbbSJens Wiklander __utee_check_inout_annotation(destLen, sizeof(*destLen)); 16476915bbbbSJens Wiklander 164812e66b6fSCedric Chaumont if (!operation->key1) 1649b0104773SPascal Brand TEE_Panic(0); 165012e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 165112e66b6fSCedric Chaumont TEE_Panic(0); 165212e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_ENCRYPT) 1653b0104773SPascal Brand TEE_Panic(0); 1654b0104773SPascal Brand 1655e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1656e86f1266SJens Wiklander dl = *destLen; 16572c028fdeSJerome Forissier res = _utee_asymm_operate(operation->state, ua, paramCount, srcData, 1658e86f1266SJens Wiklander srcLen, destData, &dl); 1659e86f1266SJens Wiklander *destLen = dl; 166012e66b6fSCedric Chaumont 16618844ebfcSPascal Brand if (res != TEE_SUCCESS && 16628844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 16638844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1664b0104773SPascal Brand TEE_Panic(res); 166512e66b6fSCedric Chaumont 1666b0104773SPascal Brand return res; 1667b0104773SPascal Brand } 1668b0104773SPascal Brand 166912e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation, 16708f07fe6fSJerome Forissier const TEE_Attribute *params, 16718f07fe6fSJerome Forissier uint32_t paramCount, const void *srcData, 167279a3c601SCedric Chaumont uint32_t srcLen, void *destData, 167379a3c601SCedric Chaumont uint32_t *destLen) 1674b0104773SPascal Brand { 16756915bbbbSJens Wiklander TEE_Result res = TEE_SUCCESS; 1676e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 16776915bbbbSJens Wiklander uint64_t dl = 0; 1678b0104773SPascal Brand 16796915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) 1680b0104773SPascal Brand TEE_Panic(0); 16816915bbbbSJens Wiklander 16826915bbbbSJens Wiklander __utee_check_attr_in_annotation(params, paramCount); 16836915bbbbSJens Wiklander __utee_check_inout_annotation(destLen, sizeof(*destLen)); 16846915bbbbSJens Wiklander 168512e66b6fSCedric Chaumont if (!operation->key1) 1686b0104773SPascal Brand TEE_Panic(0); 168712e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 168812e66b6fSCedric Chaumont TEE_Panic(0); 168912e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_DECRYPT) 1690b0104773SPascal Brand TEE_Panic(0); 1691b0104773SPascal Brand 1692e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1693e86f1266SJens Wiklander dl = *destLen; 16942c028fdeSJerome Forissier res = _utee_asymm_operate(operation->state, ua, paramCount, srcData, 1695e86f1266SJens Wiklander srcLen, destData, &dl); 1696e86f1266SJens Wiklander *destLen = dl; 169712e66b6fSCedric Chaumont 16988844ebfcSPascal Brand if (res != TEE_SUCCESS && 16998844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 17008844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1701b0104773SPascal Brand TEE_Panic(res); 170212e66b6fSCedric Chaumont 1703b0104773SPascal Brand return res; 1704b0104773SPascal Brand } 1705b0104773SPascal Brand 170612e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation, 17078f07fe6fSJerome Forissier const TEE_Attribute *params, 17088f07fe6fSJerome Forissier uint32_t paramCount, const void *digest, 170979a3c601SCedric Chaumont uint32_t digestLen, void *signature, 171079a3c601SCedric Chaumont uint32_t *signatureLen) 1711b0104773SPascal Brand { 17126915bbbbSJens Wiklander TEE_Result res = TEE_SUCCESS; 1713e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 17146915bbbbSJens Wiklander uint64_t sl = 0; 1715b0104773SPascal Brand 17166915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!digest && digestLen)) 1717b0104773SPascal Brand TEE_Panic(0); 17186915bbbbSJens Wiklander 17196915bbbbSJens Wiklander __utee_check_attr_in_annotation(params, paramCount); 17206915bbbbSJens Wiklander __utee_check_inout_annotation(signatureLen, sizeof(*signatureLen)); 17216915bbbbSJens Wiklander 172212e66b6fSCedric Chaumont if (!operation->key1) 1723b0104773SPascal Brand TEE_Panic(0); 172412e66b6fSCedric Chaumont if (operation->info.operationClass != 172512e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 172612e66b6fSCedric Chaumont TEE_Panic(0); 172712e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_SIGN) 1728b0104773SPascal Brand TEE_Panic(0); 1729b0104773SPascal Brand 1730e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1731e86f1266SJens Wiklander sl = *signatureLen; 17322c028fdeSJerome Forissier res = _utee_asymm_operate(operation->state, ua, paramCount, digest, 1733e86f1266SJens Wiklander digestLen, signature, &sl); 1734e86f1266SJens Wiklander *signatureLen = sl; 173512e66b6fSCedric Chaumont 1736b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER) 1737b0104773SPascal Brand TEE_Panic(res); 173812e66b6fSCedric Chaumont 1739b0104773SPascal Brand return res; 1740b0104773SPascal Brand } 1741b0104773SPascal Brand 174212e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation, 17438f07fe6fSJerome Forissier const TEE_Attribute *params, 17448f07fe6fSJerome Forissier uint32_t paramCount, const void *digest, 17458f07fe6fSJerome Forissier uint32_t digestLen, 17468f07fe6fSJerome Forissier const void *signature, 174779a3c601SCedric Chaumont uint32_t signatureLen) 1748b0104773SPascal Brand { 1749b0104773SPascal Brand TEE_Result res; 1750e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1751b0104773SPascal Brand 175212e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || 175312e66b6fSCedric Chaumont (digest == NULL && digestLen != 0) || 1754b0104773SPascal Brand (signature == NULL && signatureLen != 0)) 1755b0104773SPascal Brand TEE_Panic(0); 17566915bbbbSJens Wiklander 17576915bbbbSJens Wiklander __utee_check_attr_in_annotation(params, paramCount); 17586915bbbbSJens Wiklander 175912e66b6fSCedric Chaumont if (!operation->key1) 1760b0104773SPascal Brand TEE_Panic(0); 176112e66b6fSCedric Chaumont if (operation->info.operationClass != 176212e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 176312e66b6fSCedric Chaumont TEE_Panic(0); 176412e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_VERIFY) 1765b0104773SPascal Brand TEE_Panic(0); 1766b0104773SPascal Brand 1767e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 17682c028fdeSJerome Forissier res = _utee_asymm_verify(operation->state, ua, paramCount, digest, 176912e66b6fSCedric Chaumont digestLen, signature, signatureLen); 177012e66b6fSCedric Chaumont 1771b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID) 1772b0104773SPascal Brand TEE_Panic(res); 177312e66b6fSCedric Chaumont 1774b0104773SPascal Brand return res; 1775b0104773SPascal Brand } 1776b0104773SPascal Brand 1777b0104773SPascal Brand /* Cryptographic Operations API - Key Derivation Functions */ 1778b0104773SPascal Brand 1779b0104773SPascal Brand void TEE_DeriveKey(TEE_OperationHandle operation, 1780b0104773SPascal Brand const TEE_Attribute *params, uint32_t paramCount, 1781b0104773SPascal Brand TEE_ObjectHandle derivedKey) 1782b0104773SPascal Brand { 1783b0104773SPascal Brand TEE_Result res; 1784b0104773SPascal Brand TEE_ObjectInfo key_info; 1785e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1786b0104773SPascal Brand 1787b0104773SPascal Brand if (operation == TEE_HANDLE_NULL || derivedKey == 0) 1788b0104773SPascal Brand TEE_Panic(0); 17896915bbbbSJens Wiklander 17906915bbbbSJens Wiklander __utee_check_attr_in_annotation(params, paramCount); 17916915bbbbSJens Wiklander 17928854d3c6SJerome Forissier if (TEE_ALG_GET_CLASS(operation->info.algorithm) != 17938854d3c6SJerome Forissier TEE_OPERATION_KEY_DERIVATION) 1794b0104773SPascal Brand TEE_Panic(0); 1795b0104773SPascal Brand 1796b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION) 1797b0104773SPascal Brand TEE_Panic(0); 179884fa9467SCedric Chaumont if (!operation->key1) 179984fa9467SCedric Chaumont TEE_Panic(0); 1800b0104773SPascal Brand if (operation->info.mode != TEE_MODE_DERIVE) 1801b0104773SPascal Brand TEE_Panic(0); 1802b0104773SPascal Brand if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0) 1803b0104773SPascal Brand TEE_Panic(0); 1804b0104773SPascal Brand 18052c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info); 1806b0104773SPascal Brand if (res != TEE_SUCCESS) 1807b36311adSJerome Forissier TEE_Panic(res); 1808b0104773SPascal Brand 1809b0104773SPascal Brand if (key_info.objectType != TEE_TYPE_GENERIC_SECRET) 1810b0104773SPascal Brand TEE_Panic(0); 1811b0104773SPascal Brand if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1812b0104773SPascal Brand TEE_Panic(0); 1813b0104773SPascal Brand 1814e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 18152c028fdeSJerome Forissier res = _utee_cryp_derive_key(operation->state, ua, paramCount, 1816e86f1266SJens Wiklander (unsigned long)derivedKey); 1817b0104773SPascal Brand if (res != TEE_SUCCESS) 1818b0104773SPascal Brand TEE_Panic(res); 1819b0104773SPascal Brand } 1820b0104773SPascal Brand 1821b0104773SPascal Brand /* Cryptographic Operations API - Random Number Generation Functions */ 1822b0104773SPascal Brand 182379a3c601SCedric Chaumont void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen) 1824b0104773SPascal Brand { 1825b0104773SPascal Brand TEE_Result res; 1826b0104773SPascal Brand 18272c028fdeSJerome Forissier res = _utee_cryp_random_number_generate(randomBuffer, randomBufferLen); 1828b0104773SPascal Brand if (res != TEE_SUCCESS) 1829b0104773SPascal Brand TEE_Panic(res); 1830b0104773SPascal Brand } 1831433c4257SJens Wiklander 1832433c4257SJens Wiklander int rand(void) 1833433c4257SJens Wiklander { 1834433c4257SJens Wiklander int rc; 1835433c4257SJens Wiklander 1836433c4257SJens Wiklander TEE_GenerateRandom(&rc, sizeof(rc)); 1837433c4257SJens Wiklander 1838433c4257SJens Wiklander /* 1839433c4257SJens Wiklander * RAND_MAX is the larges int, INT_MAX which is all bits but the 1840433c4257SJens Wiklander * highest bit set. 1841433c4257SJens Wiklander */ 1842433c4257SJens Wiklander return rc & RAND_MAX; 1843433c4257SJens Wiklander } 184479170ce0SJerome Forissier 184579170ce0SJerome Forissier TEE_Result TEE_IsAlgorithmSupported(uint32_t alg, uint32_t element) 184679170ce0SJerome Forissier { 184779170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_AES)) { 184879170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_ECB)) { 184979170ce0SJerome Forissier if (alg == TEE_ALG_AES_ECB_NOPAD) 185079170ce0SJerome Forissier goto check_element_none; 185179170ce0SJerome Forissier } 185279170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CBC)) { 185379170ce0SJerome Forissier if (alg == TEE_ALG_AES_CBC_NOPAD) 185479170ce0SJerome Forissier goto check_element_none; 185579170ce0SJerome Forissier } 185679170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CTR)) { 185779170ce0SJerome Forissier if (alg == TEE_ALG_AES_CTR) 185879170ce0SJerome Forissier goto check_element_none; 185979170ce0SJerome Forissier } 186079170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CTS)) { 186179170ce0SJerome Forissier if (alg == TEE_ALG_AES_CTS) 186279170ce0SJerome Forissier goto check_element_none; 186379170ce0SJerome Forissier } 186479170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_XTS)) { 186579170ce0SJerome Forissier if (alg == TEE_ALG_AES_XTS) 186679170ce0SJerome Forissier goto check_element_none; 186779170ce0SJerome Forissier } 186879170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) { 186979170ce0SJerome Forissier if (alg == TEE_ALG_AES_CBC_MAC_NOPAD || 187079170ce0SJerome Forissier alg == TEE_ALG_AES_CBC_MAC_PKCS5) 187179170ce0SJerome Forissier goto check_element_none; 187279170ce0SJerome Forissier } 187379170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CMAC)) { 187479170ce0SJerome Forissier if (alg == TEE_ALG_AES_CMAC) 187579170ce0SJerome Forissier goto check_element_none; 187679170ce0SJerome Forissier } 187779170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CCM)) { 187879170ce0SJerome Forissier if (alg == TEE_ALG_AES_CCM) 187979170ce0SJerome Forissier goto check_element_none; 188079170ce0SJerome Forissier } 188179170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_GCM)) { 188279170ce0SJerome Forissier if (alg == TEE_ALG_AES_GCM) 188379170ce0SJerome Forissier goto check_element_none; 188479170ce0SJerome Forissier } 188579170ce0SJerome Forissier } 188679170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_DES)) { 188779170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_ECB)) { 188879170ce0SJerome Forissier if (alg == TEE_ALG_DES_ECB_NOPAD || 188979170ce0SJerome Forissier alg == TEE_ALG_DES3_ECB_NOPAD) 189079170ce0SJerome Forissier goto check_element_none; 189179170ce0SJerome Forissier } 189279170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CBC)) { 189379170ce0SJerome Forissier if (alg == TEE_ALG_DES_CBC_NOPAD || 189479170ce0SJerome Forissier alg == TEE_ALG_DES3_CBC_NOPAD) 189579170ce0SJerome Forissier goto check_element_none; 189679170ce0SJerome Forissier } 189779170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) { 189879170ce0SJerome Forissier if (alg == TEE_ALG_DES_CBC_MAC_NOPAD || 189979170ce0SJerome Forissier alg == TEE_ALG_DES_CBC_MAC_PKCS5 || 190079170ce0SJerome Forissier alg == TEE_ALG_DES3_CBC_MAC_NOPAD || 190179170ce0SJerome Forissier alg == TEE_ALG_DES3_CBC_MAC_PKCS5) 190279170ce0SJerome Forissier goto check_element_none; 190379170ce0SJerome Forissier } 190479170ce0SJerome Forissier } 190579170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_MD5)) { 190679170ce0SJerome Forissier if (alg == TEE_ALG_MD5) 190779170ce0SJerome Forissier goto check_element_none; 190879170ce0SJerome Forissier } 190979170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 191079170ce0SJerome Forissier if (alg == TEE_ALG_SHA1) 191179170ce0SJerome Forissier goto check_element_none; 191279170ce0SJerome Forissier } 191379170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 191479170ce0SJerome Forissier if (alg == TEE_ALG_SHA224) 191579170ce0SJerome Forissier goto check_element_none; 191679170ce0SJerome Forissier } 191779170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 191879170ce0SJerome Forissier if (alg == TEE_ALG_SHA256) 191979170ce0SJerome Forissier goto check_element_none; 192079170ce0SJerome Forissier } 192179170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA384)) { 192279170ce0SJerome Forissier if (alg == TEE_ALG_SHA384) 192379170ce0SJerome Forissier goto check_element_none; 192479170ce0SJerome Forissier } 192579170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA512)) { 192679170ce0SJerome Forissier if (alg == TEE_ALG_SHA512) 192779170ce0SJerome Forissier goto check_element_none; 192879170ce0SJerome Forissier } 192979170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) { 193079170ce0SJerome Forissier if (alg == TEE_ALG_MD5SHA1) 193179170ce0SJerome Forissier goto check_element_none; 193279170ce0SJerome Forissier } 193379170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_HMAC)) { 193479170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_MD5)) { 193579170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_MD5) 193679170ce0SJerome Forissier goto check_element_none; 193779170ce0SJerome Forissier } 193879170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 193979170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_SHA1) 194079170ce0SJerome Forissier goto check_element_none; 194179170ce0SJerome Forissier } 194279170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 194379170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_SHA224) 194479170ce0SJerome Forissier goto check_element_none; 194579170ce0SJerome Forissier } 194679170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 194779170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_SHA256) 194879170ce0SJerome Forissier goto check_element_none; 194979170ce0SJerome Forissier } 195079170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA384)) { 195179170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_SHA384) 195279170ce0SJerome Forissier goto check_element_none; 195379170ce0SJerome Forissier } 195479170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA512)) { 195579170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_SHA512) 195679170ce0SJerome Forissier goto check_element_none; 195779170ce0SJerome Forissier } 195879170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SM3)) { 195979170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_SM3) 196079170ce0SJerome Forissier goto check_element_none; 196179170ce0SJerome Forissier } 196279170ce0SJerome Forissier } 196379170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SM3)) { 196479170ce0SJerome Forissier if (alg == TEE_ALG_SM3) 196579170ce0SJerome Forissier goto check_element_none; 196679170ce0SJerome Forissier } 196779170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SM4)) { 196879170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_ECB)) { 196979170ce0SJerome Forissier if (alg == TEE_ALG_SM4_ECB_NOPAD) 197079170ce0SJerome Forissier goto check_element_none; 197179170ce0SJerome Forissier } 197279170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CBC)) { 197379170ce0SJerome Forissier if (alg == TEE_ALG_SM4_CBC_NOPAD) 197479170ce0SJerome Forissier goto check_element_none; 197579170ce0SJerome Forissier } 197679170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CTR)) { 197779170ce0SJerome Forissier if (alg == TEE_ALG_SM4_CTR) 197879170ce0SJerome Forissier goto check_element_none; 197979170ce0SJerome Forissier } 198079170ce0SJerome Forissier } 198179170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_RSA)) { 198279170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_MD5)) { 198379170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5) 198479170ce0SJerome Forissier goto check_element_none; 198579170ce0SJerome Forissier } 198679170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 198779170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA1 || 198879170ce0SJerome Forissier alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1 || 198979170ce0SJerome Forissier alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1) 199079170ce0SJerome Forissier goto check_element_none; 199179170ce0SJerome Forissier } 199279170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) { 199379170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5SHA1) 199479170ce0SJerome Forissier goto check_element_none; 199579170ce0SJerome Forissier } 199679170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 199779170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA224 || 199879170ce0SJerome Forissier alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224 || 199979170ce0SJerome Forissier alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224) 200079170ce0SJerome Forissier goto check_element_none; 200179170ce0SJerome Forissier } 200279170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 200379170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA256 || 200479170ce0SJerome Forissier alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256 || 200579170ce0SJerome Forissier alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256) 200679170ce0SJerome Forissier goto check_element_none; 200779170ce0SJerome Forissier } 200879170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA384)) { 200979170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA384 || 201079170ce0SJerome Forissier alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384 || 201179170ce0SJerome Forissier alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384) 201279170ce0SJerome Forissier goto check_element_none; 201379170ce0SJerome Forissier } 201479170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA512)) { 201579170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA512 || 201679170ce0SJerome Forissier alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512 || 201779170ce0SJerome Forissier alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512) 201879170ce0SJerome Forissier goto check_element_none; 201979170ce0SJerome Forissier } 202079170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_RSASSA_NA1)) { 202179170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5) 202279170ce0SJerome Forissier goto check_element_none; 202379170ce0SJerome Forissier } 202479170ce0SJerome Forissier if (alg == TEE_ALG_RSA_NOPAD) 202579170ce0SJerome Forissier goto check_element_none; 202679170ce0SJerome Forissier } 202779170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_DSA)) { 202879170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 202979170ce0SJerome Forissier if (alg == TEE_ALG_DSA_SHA1) 203079170ce0SJerome Forissier goto check_element_none; 203179170ce0SJerome Forissier } 203279170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 203379170ce0SJerome Forissier if (alg == TEE_ALG_DSA_SHA224) 203479170ce0SJerome Forissier goto check_element_none; 203579170ce0SJerome Forissier } 203679170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 203779170ce0SJerome Forissier if (alg == TEE_ALG_DSA_SHA256) 203879170ce0SJerome Forissier goto check_element_none; 203979170ce0SJerome Forissier } 204079170ce0SJerome Forissier } 204179170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_DH)) { 204279170ce0SJerome Forissier if (alg == TEE_ALG_DH_DERIVE_SHARED_SECRET) 204379170ce0SJerome Forissier goto check_element_none; 204479170ce0SJerome Forissier } 204579170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_ECC)) { 204679170ce0SJerome Forissier if ((alg == TEE_ALG_ECDH_P192 || alg == TEE_ALG_ECDSA_P192) && 204779170ce0SJerome Forissier element == TEE_ECC_CURVE_NIST_P192) 204879170ce0SJerome Forissier return TEE_SUCCESS; 204979170ce0SJerome Forissier if ((alg == TEE_ALG_ECDH_P224 || alg == TEE_ALG_ECDSA_P224) && 205079170ce0SJerome Forissier element == TEE_ECC_CURVE_NIST_P224) 205179170ce0SJerome Forissier return TEE_SUCCESS; 205279170ce0SJerome Forissier if ((alg == TEE_ALG_ECDH_P256 || alg == TEE_ALG_ECDSA_P256) && 205379170ce0SJerome Forissier element == TEE_ECC_CURVE_NIST_P256) 205479170ce0SJerome Forissier return TEE_SUCCESS; 205579170ce0SJerome Forissier if ((alg == TEE_ALG_ECDH_P384 || alg == TEE_ALG_ECDSA_P384) && 205679170ce0SJerome Forissier element == TEE_ECC_CURVE_NIST_P384) 205779170ce0SJerome Forissier return TEE_SUCCESS; 205879170ce0SJerome Forissier if ((alg == TEE_ALG_ECDH_P521 || alg == TEE_ALG_ECDSA_P521) && 205979170ce0SJerome Forissier element == TEE_ECC_CURVE_NIST_P521) 206079170ce0SJerome Forissier return TEE_SUCCESS; 206179170ce0SJerome Forissier } 206279170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SM2_DSA)) { 206379170ce0SJerome Forissier if (alg == TEE_ALG_SM2_DSA_SM3 && element == TEE_ECC_CURVE_SM2) 206479170ce0SJerome Forissier return TEE_SUCCESS; 206579170ce0SJerome Forissier } 206679170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SM2_KEP)) { 206779170ce0SJerome Forissier if (alg == TEE_ALG_SM2_KEP && element == TEE_ECC_CURVE_SM2) 206879170ce0SJerome Forissier return TEE_SUCCESS; 206979170ce0SJerome Forissier } 207079170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SM2_PKE)) { 207179170ce0SJerome Forissier if (alg == TEE_ALG_SM2_PKE && element == TEE_ECC_CURVE_SM2) 207279170ce0SJerome Forissier return TEE_SUCCESS; 207379170ce0SJerome Forissier } 2074*3f61056dSSohaib ul Hassan if (IS_ENABLED(CFG_CRYPTO_X25519)) { 2075*3f61056dSSohaib ul Hassan if (alg == TEE_ALG_X25519 && element == TEE_ECC_CURVE_25519) 2076*3f61056dSSohaib ul Hassan return TEE_SUCCESS; 2077*3f61056dSSohaib ul Hassan } 207879170ce0SJerome Forissier 207979170ce0SJerome Forissier return TEE_ERROR_NOT_SUPPORTED; 208079170ce0SJerome Forissier check_element_none: 208179170ce0SJerome Forissier if (element == TEE_CRYPTO_ELEMENT_NONE) 208279170ce0SJerome Forissier return TEE_SUCCESS; 208379170ce0SJerome Forissier return TEE_ERROR_NOT_SUPPORTED; 208479170ce0SJerome Forissier } 2085