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; 108e1f9cee7SSergiy Kibrik case TEE_ALG_ED25519: 1093f61056dSSohaib ul Hassan case TEE_ALG_X25519: 1103f61056dSSohaib ul Hassan if (maxKeySize != 256) 1113f61056dSSohaib ul Hassan return TEE_ERROR_NOT_SUPPORTED; 1123f61056dSSohaib ul Hassan break; 113218d9055SCedric Chaumont default: 114218d9055SCedric Chaumont break; 115218d9055SCedric Chaumont } 116218d9055SCedric Chaumont 117cf5c060cSJens Wiklander /* Check algorithm mode (and maxKeySize for digests) */ 118b0104773SPascal Brand switch (algorithm) { 119b0104773SPascal Brand case TEE_ALG_AES_CTS: 120b0104773SPascal Brand case TEE_ALG_AES_XTS: 121b0104773SPascal Brand buffer_two_blocks = true; 122919a5a68SJerome Forissier fallthrough; 1234bd53c54SJerome Forissier case TEE_ALG_AES_ECB_NOPAD: 124b0104773SPascal Brand case TEE_ALG_AES_CBC_NOPAD: 125b0104773SPascal Brand case TEE_ALG_AES_CCM: 126b0104773SPascal Brand case TEE_ALG_DES_ECB_NOPAD: 127b0104773SPascal Brand case TEE_ALG_DES_CBC_NOPAD: 128b0104773SPascal Brand case TEE_ALG_DES3_ECB_NOPAD: 129b0104773SPascal Brand case TEE_ALG_DES3_CBC_NOPAD: 130ade6f848SJerome Forissier case TEE_ALG_SM4_ECB_NOPAD: 131ade6f848SJerome Forissier case TEE_ALG_SM4_CBC_NOPAD: 132ade6f848SJerome Forissier case TEE_ALG_SM4_CTR: 133b0104773SPascal Brand if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES) 134b0104773SPascal Brand block_size = TEE_AES_BLOCK_SIZE; 135ade6f848SJerome Forissier else if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_SM4) 136ade6f848SJerome Forissier block_size = TEE_SM4_BLOCK_SIZE; 137b0104773SPascal Brand else 138b0104773SPascal Brand block_size = TEE_DES_BLOCK_SIZE; 139919a5a68SJerome Forissier fallthrough; 14057aabac5SBogdan Liulko case TEE_ALG_AES_CTR: 141afc0c182SBogdan Liulko case TEE_ALG_AES_GCM: 142b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) 143b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT; 144b0104773SPascal Brand else if (mode == TEE_MODE_DECRYPT) 145b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT; 146b0104773SPascal Brand else 147b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 148b0104773SPascal Brand break; 149b0104773SPascal Brand 1506a2e0a9fSGabor Szekely #if defined(CFG_CRYPTO_RSASSA_NA1) 1516a2e0a9fSGabor Szekely case TEE_ALG_RSASSA_PKCS1_V1_5: 1526a2e0a9fSGabor Szekely #endif 153b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_MD5: 154b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1: 155b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224: 156b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256: 157b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384: 158b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512: 159b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1: 160b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224: 161b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256: 162b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384: 163b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512: 164b0104773SPascal Brand case TEE_ALG_DSA_SHA1: 165218d9055SCedric Chaumont case TEE_ALG_DSA_SHA224: 166218d9055SCedric Chaumont case TEE_ALG_DSA_SHA256: 1671220586eSCedric Chaumont case TEE_ALG_ECDSA_P192: 1681220586eSCedric Chaumont case TEE_ALG_ECDSA_P224: 1691220586eSCedric Chaumont case TEE_ALG_ECDSA_P256: 1701220586eSCedric Chaumont case TEE_ALG_ECDSA_P384: 1711220586eSCedric Chaumont case TEE_ALG_ECDSA_P521: 1720f151943SJerome Forissier case TEE_ALG_SM2_DSA_SM3: 173e1f9cee7SSergiy Kibrik case TEE_ALG_ED25519: 174b0104773SPascal Brand if (mode == TEE_MODE_SIGN) { 175b0104773SPascal Brand with_private_key = true; 176b0104773SPascal Brand req_key_usage = TEE_USAGE_SIGN; 177b0104773SPascal Brand } else if (mode == TEE_MODE_VERIFY) { 178b0104773SPascal Brand req_key_usage = TEE_USAGE_VERIFY; 179b0104773SPascal Brand } else { 180b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 181b0104773SPascal Brand } 182b0104773SPascal Brand break; 183b0104773SPascal Brand 184b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_V1_5: 185b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1: 186b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224: 187b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256: 188b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384: 189b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512: 19091fc6bd8SJerome Forissier case TEE_ALG_SM2_PKE: 191b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) { 192b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT; 193b0104773SPascal Brand } else if (mode == TEE_MODE_DECRYPT) { 194b0104773SPascal Brand with_private_key = true; 195b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT; 196b0104773SPascal Brand } else { 197b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 198b0104773SPascal Brand } 199b0104773SPascal Brand break; 200b0104773SPascal Brand 201b0104773SPascal Brand case TEE_ALG_RSA_NOPAD: 202b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) { 203b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY; 204b0104773SPascal Brand } else if (mode == TEE_MODE_DECRYPT) { 205b0104773SPascal Brand with_private_key = true; 206b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT | TEE_USAGE_SIGN; 207b0104773SPascal Brand } else { 208b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 209b0104773SPascal Brand } 210b0104773SPascal Brand break; 211b0104773SPascal Brand 212b0104773SPascal Brand case TEE_ALG_DH_DERIVE_SHARED_SECRET: 2131220586eSCedric Chaumont case TEE_ALG_ECDH_P192: 2141220586eSCedric Chaumont case TEE_ALG_ECDH_P224: 2151220586eSCedric Chaumont case TEE_ALG_ECDH_P256: 2161220586eSCedric Chaumont case TEE_ALG_ECDH_P384: 2171220586eSCedric Chaumont case TEE_ALG_ECDH_P521: 218cdb198a7SJerome Forissier case TEE_ALG_HKDF_MD5_DERIVE_KEY: 219cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA1_DERIVE_KEY: 220cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA224_DERIVE_KEY: 221cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA256_DERIVE_KEY: 222cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA384_DERIVE_KEY: 223cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA512_DERIVE_KEY: 2248854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA1_DERIVE_KEY: 2258854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA224_DERIVE_KEY: 2268854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA256_DERIVE_KEY: 2278854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA384_DERIVE_KEY: 2288854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA512_DERIVE_KEY: 2290f2293b7SJerome Forissier case TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY: 2305b385b3fSJerome Forissier case TEE_ALG_SM2_KEP: 2313f61056dSSohaib ul Hassan case TEE_ALG_X25519: 232b0104773SPascal Brand if (mode != TEE_MODE_DERIVE) 233b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 234b0104773SPascal Brand with_private_key = true; 235b0104773SPascal Brand req_key_usage = TEE_USAGE_DERIVE; 236b0104773SPascal Brand break; 237b0104773SPascal Brand 238b0104773SPascal Brand case TEE_ALG_MD5: 239b0104773SPascal Brand case TEE_ALG_SHA1: 240b0104773SPascal Brand case TEE_ALG_SHA224: 241b0104773SPascal Brand case TEE_ALG_SHA256: 242b0104773SPascal Brand case TEE_ALG_SHA384: 243b0104773SPascal Brand case TEE_ALG_SHA512: 24447645577SJerome Forissier case TEE_ALG_SM3: 245b0104773SPascal Brand if (mode != TEE_MODE_DIGEST) 246b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 247cf5c060cSJens Wiklander if (maxKeySize) 248cf5c060cSJens Wiklander return TEE_ERROR_NOT_SUPPORTED; 24905304565SCedric Chaumont /* v1.1: flags always set for digest operations */ 250b0104773SPascal Brand handle_state |= TEE_HANDLE_FLAG_KEY_SET; 251b0104773SPascal Brand req_key_usage = 0; 252b0104773SPascal Brand break; 253b0104773SPascal Brand 254b0104773SPascal Brand case TEE_ALG_DES_CBC_MAC_NOPAD: 255b0104773SPascal Brand case TEE_ALG_AES_CBC_MAC_NOPAD: 256b0104773SPascal Brand case TEE_ALG_AES_CBC_MAC_PKCS5: 257b0104773SPascal Brand case TEE_ALG_AES_CMAC: 258b0104773SPascal Brand case TEE_ALG_DES_CBC_MAC_PKCS5: 259b0104773SPascal Brand case TEE_ALG_DES3_CBC_MAC_NOPAD: 260b0104773SPascal Brand case TEE_ALG_DES3_CBC_MAC_PKCS5: 261eee637e7SAlexander Zakharov case TEE_ALG_DES3_CMAC: 262b0104773SPascal Brand case TEE_ALG_HMAC_MD5: 263b0104773SPascal Brand case TEE_ALG_HMAC_SHA1: 264b0104773SPascal Brand case TEE_ALG_HMAC_SHA224: 265b0104773SPascal Brand case TEE_ALG_HMAC_SHA256: 266b0104773SPascal Brand case TEE_ALG_HMAC_SHA384: 267b0104773SPascal Brand case TEE_ALG_HMAC_SHA512: 26847645577SJerome Forissier case TEE_ALG_HMAC_SM3: 269b0104773SPascal Brand if (mode != TEE_MODE_MAC) 270b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 271b0104773SPascal Brand req_key_usage = TEE_USAGE_MAC; 272b0104773SPascal Brand break; 273b0104773SPascal Brand 274b0104773SPascal Brand default: 275b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 276b0104773SPascal Brand } 277b0104773SPascal Brand 278b66f219bSJens Wiklander op = TEE_Malloc(sizeof(*op), TEE_MALLOC_FILL_ZERO); 2799b52c538SCedric Chaumont if (!op) 280b0104773SPascal Brand return TEE_ERROR_OUT_OF_MEMORY; 281b0104773SPascal Brand 282b0104773SPascal Brand op->info.algorithm = algorithm; 283b0104773SPascal Brand op->info.operationClass = TEE_ALG_GET_CLASS(algorithm); 2846a2e0a9fSGabor Szekely #ifdef CFG_CRYPTO_RSASSA_NA1 2856a2e0a9fSGabor Szekely if (algorithm == TEE_ALG_RSASSA_PKCS1_V1_5) 2866a2e0a9fSGabor Szekely op->info.operationClass = TEE_OPERATION_ASYMMETRIC_SIGNATURE; 2876a2e0a9fSGabor Szekely #endif 288b0104773SPascal Brand op->info.mode = mode; 2892e5e6460SAlbert Schwarzkopf op->info.digestLength = TEE_ALG_GET_DIGEST_SIZE(algorithm); 290b0104773SPascal Brand op->info.maxKeySize = maxKeySize; 291b0104773SPascal Brand op->info.requiredKeyUsage = req_key_usage; 292b0104773SPascal Brand op->info.handleState = handle_state; 293b0104773SPascal Brand 294b0104773SPascal Brand if (block_size > 1) { 295b0104773SPascal Brand size_t buffer_size = block_size; 296b0104773SPascal Brand 297b0104773SPascal Brand if (buffer_two_blocks) 298b0104773SPascal Brand buffer_size *= 2; 299b0104773SPascal Brand 3009b52c538SCedric Chaumont op->buffer = TEE_Malloc(buffer_size, 3019b52c538SCedric Chaumont TEE_USER_MEM_HINT_NO_FILL_ZERO); 302b0104773SPascal Brand if (op->buffer == NULL) { 303b0104773SPascal Brand res = TEE_ERROR_OUT_OF_MEMORY; 304b66f219bSJens Wiklander goto out; 305b0104773SPascal Brand } 306b0104773SPascal Brand } 307b0104773SPascal Brand op->block_size = block_size; 308b0104773SPascal Brand op->buffer_two_blocks = buffer_two_blocks; 309b0104773SPascal Brand 310b0104773SPascal Brand if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) { 311b0104773SPascal Brand uint32_t mks = maxKeySize; 312b0104773SPascal Brand TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm, 313b0104773SPascal Brand with_private_key); 314b0104773SPascal Brand 315b0104773SPascal Brand /* 316b0104773SPascal Brand * If two keys are expected the max key size is the sum of 317b0104773SPascal Brand * the size of both keys. 318b0104773SPascal Brand */ 319b0104773SPascal Brand if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) 320b0104773SPascal Brand mks /= 2; 321b0104773SPascal Brand 322b0104773SPascal Brand res = TEE_AllocateTransientObject(key_type, mks, &op->key1); 323b0104773SPascal Brand if (res != TEE_SUCCESS) 324b66f219bSJens Wiklander goto out; 325b0104773SPascal Brand 32605304565SCedric Chaumont if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) { 3279b52c538SCedric Chaumont res = TEE_AllocateTransientObject(key_type, mks, 328b0104773SPascal Brand &op->key2); 329b0104773SPascal Brand if (res != TEE_SUCCESS) 330b66f219bSJens Wiklander goto out; 331b0104773SPascal Brand } 332b0104773SPascal Brand } 333b0104773SPascal Brand 3342c028fdeSJerome Forissier res = _utee_cryp_state_alloc(algorithm, mode, (unsigned long)op->key1, 335e86f1266SJens Wiklander (unsigned long)op->key2, &op->state); 336b66f219bSJens Wiklander if (res != TEE_SUCCESS) 337b66f219bSJens Wiklander goto out; 338b0104773SPascal Brand 33905304565SCedric Chaumont /* 34005304565SCedric Chaumont * Initialize digest operations 34105304565SCedric Chaumont * Other multi-stage operations initialized w/ TEE_xxxInit functions 34205304565SCedric Chaumont * Non-applicable on asymmetric operations 34305304565SCedric Chaumont */ 34405304565SCedric Chaumont if (TEE_ALG_GET_CLASS(algorithm) == TEE_OPERATION_DIGEST) { 3452c028fdeSJerome Forissier res = _utee_hash_init(op->state, NULL, 0); 34605304565SCedric Chaumont if (res != TEE_SUCCESS) 347b66f219bSJens Wiklander goto out; 34805304565SCedric Chaumont /* v1.1: flags always set for digest operations */ 34905304565SCedric Chaumont op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 35005304565SCedric Chaumont } 35105304565SCedric Chaumont 352642a1607SCedric Chaumont op->operationState = TEE_OPERATION_STATE_INITIAL; 353642a1607SCedric Chaumont 354b0104773SPascal Brand *operation = op; 355b0104773SPascal Brand 356b66f219bSJens Wiklander out: 357b66f219bSJens Wiklander if (res != TEE_SUCCESS) { 358b66f219bSJens Wiklander if (res != TEE_ERROR_OUT_OF_MEMORY && 3599b52c538SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 360b36311adSJerome Forissier TEE_Panic(res); 361b66f219bSJens Wiklander if (op) { 362b66f219bSJens Wiklander if (op->state) { 363b66f219bSJens Wiklander TEE_FreeOperation(op); 364b66f219bSJens Wiklander } else { 365b66f219bSJens Wiklander TEE_Free(op->buffer); 366b66f219bSJens Wiklander TEE_FreeTransientObject(op->key1); 367b66f219bSJens Wiklander TEE_FreeTransientObject(op->key2); 368b66f219bSJens Wiklander TEE_Free(op); 369b66f219bSJens Wiklander } 370b66f219bSJens Wiklander } 371b66f219bSJens Wiklander } 372b66f219bSJens Wiklander 373b0104773SPascal Brand return res; 374b0104773SPascal Brand } 375b0104773SPascal Brand 376b0104773SPascal Brand void TEE_FreeOperation(TEE_OperationHandle operation) 377b0104773SPascal Brand { 378e889e80bSCedric Chaumont TEE_Result res; 379e889e80bSCedric Chaumont 380e889e80bSCedric Chaumont if (operation == TEE_HANDLE_NULL) 381e889e80bSCedric Chaumont TEE_Panic(0); 382e889e80bSCedric Chaumont 383b0104773SPascal Brand /* 384b0104773SPascal Brand * Note that keys should not be freed here, since they are 385b0104773SPascal Brand * claimed by the operation they will be freed by 386b0104773SPascal Brand * utee_cryp_state_free(). 387b0104773SPascal Brand */ 3882c028fdeSJerome Forissier res = _utee_cryp_state_free(operation->state); 389e889e80bSCedric Chaumont if (res != TEE_SUCCESS) 390b36311adSJerome Forissier TEE_Panic(res); 391e889e80bSCedric Chaumont 392b0104773SPascal Brand TEE_Free(operation->buffer); 393b0104773SPascal Brand TEE_Free(operation); 394b0104773SPascal Brand } 395b0104773SPascal Brand 396b0104773SPascal Brand void TEE_GetOperationInfo(TEE_OperationHandle operation, 397b0104773SPascal Brand TEE_OperationInfo *operationInfo) 398b0104773SPascal Brand { 399b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 400b0104773SPascal Brand TEE_Panic(0); 401b0104773SPascal Brand 4026915bbbbSJens Wiklander __utee_check_out_annotation(operationInfo, sizeof(*operationInfo)); 403b0104773SPascal Brand 404b0104773SPascal Brand *operationInfo = operation->info; 405bac3a8a7SJens Wiklander if (operationInfo->handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) { 406bac3a8a7SJens Wiklander operationInfo->keySize = 0; 407bac3a8a7SJens Wiklander operationInfo->requiredKeyUsage = 0; 408bac3a8a7SJens Wiklander } 409b0104773SPascal Brand } 410b0104773SPascal Brand 411ee2f75afSJens Wiklander TEE_Result TEE_GetOperationInfoMultiple(TEE_OperationHandle op, 412ee2f75afSJens Wiklander TEE_OperationInfoMultiple *op_info, 413ee2f75afSJens Wiklander uint32_t *size) 41405304565SCedric Chaumont { 41505304565SCedric Chaumont TEE_Result res = TEE_SUCCESS; 416ee2f75afSJens Wiklander TEE_ObjectInfo kinfo = { }; 417ee2f75afSJens Wiklander size_t max_key_count = 0; 418ee2f75afSJens Wiklander bool two_keys = false; 41905304565SCedric Chaumont 420ee2f75afSJens Wiklander if (op == TEE_HANDLE_NULL) { 42105304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 42205304565SCedric Chaumont goto out; 42305304565SCedric Chaumont } 42405304565SCedric Chaumont 425ee2f75afSJens Wiklander __utee_check_outbuf_annotation(op_info, size); 42605304565SCedric Chaumont 427ee2f75afSJens Wiklander if (*size < sizeof(*op_info)) { 428ee2f75afSJens Wiklander res = TEE_ERROR_BAD_PARAMETERS; 429ee2f75afSJens Wiklander goto out; 430ee2f75afSJens Wiklander } 431ee2f75afSJens Wiklander max_key_count = (*size - sizeof(*op_info)) / 43205304565SCedric Chaumont sizeof(TEE_OperationInfoKey); 43305304565SCedric Chaumont 434ee2f75afSJens Wiklander TEE_MemFill(op_info, 0, *size); 43505304565SCedric Chaumont 43605304565SCedric Chaumont /* Two keys flag (TEE_ALG_AES_XTS only) */ 437ee2f75afSJens Wiklander two_keys = op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS; 438ee2f75afSJens Wiklander 439ee2f75afSJens Wiklander if (op->info.mode == TEE_MODE_DIGEST) { 440ee2f75afSJens Wiklander op_info->numberOfKeys = 0; 441ee2f75afSJens Wiklander } else if (!two_keys) { 442ee2f75afSJens Wiklander if (max_key_count < 1) { 44305304565SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 44405304565SCedric Chaumont goto out; 44505304565SCedric Chaumont } 44605304565SCedric Chaumont 447ee2f75afSJens Wiklander res = TEE_GetObjectInfo1(op->key1, &kinfo); 448ee2f75afSJens Wiklander /* Key1 is not a valid handle, "can't happen". */ 449ee2f75afSJens Wiklander if (res) 45005304565SCedric Chaumont goto out; 45105304565SCedric Chaumont 452ee2f75afSJens Wiklander op_info->keyInformation[0].keySize = kinfo.keySize; 453ee2f75afSJens Wiklander op_info->keyInformation[0].requiredKeyUsage = 454ee2f75afSJens Wiklander op->info.requiredKeyUsage; 455ee2f75afSJens Wiklander op_info->numberOfKeys = 1; 456ee2f75afSJens Wiklander } else { 457ee2f75afSJens Wiklander if (max_key_count < 2) { 458ee2f75afSJens Wiklander res = TEE_ERROR_SHORT_BUFFER; 45905304565SCedric Chaumont goto out; 46005304565SCedric Chaumont } 46105304565SCedric Chaumont 462ee2f75afSJens Wiklander res = TEE_GetObjectInfo1(op->key1, &kinfo); 463ee2f75afSJens Wiklander /* Key1 is not a valid handle, "can't happen". */ 464ee2f75afSJens Wiklander if (res) 465ee2f75afSJens Wiklander goto out; 466ee2f75afSJens Wiklander 467ee2f75afSJens Wiklander op_info->keyInformation[0].keySize = kinfo.keySize; 468ee2f75afSJens Wiklander op_info->keyInformation[0].requiredKeyUsage = 469ee2f75afSJens Wiklander op->info.requiredKeyUsage; 470ee2f75afSJens Wiklander 471ee2f75afSJens Wiklander res = TEE_GetObjectInfo1(op->key2, &kinfo); 472ee2f75afSJens Wiklander /* Key2 is not a valid handle, "can't happen". */ 473ee2f75afSJens Wiklander if (res) 474ee2f75afSJens Wiklander goto out; 475ee2f75afSJens Wiklander 476ee2f75afSJens Wiklander op_info->keyInformation[1].keySize = kinfo.keySize; 477ee2f75afSJens Wiklander op_info->keyInformation[1].requiredKeyUsage = 478ee2f75afSJens Wiklander op->info.requiredKeyUsage; 479ee2f75afSJens Wiklander 480ee2f75afSJens Wiklander op_info->numberOfKeys = 2; 48105304565SCedric Chaumont } 48205304565SCedric Chaumont 483ee2f75afSJens Wiklander op_info->algorithm = op->info.algorithm; 484ee2f75afSJens Wiklander op_info->operationClass = op->info.operationClass; 485ee2f75afSJens Wiklander op_info->mode = op->info.mode; 486ee2f75afSJens Wiklander op_info->digestLength = op->info.digestLength; 487ee2f75afSJens Wiklander op_info->maxKeySize = op->info.maxKeySize; 488ee2f75afSJens Wiklander op_info->handleState = op->info.handleState; 489ee2f75afSJens Wiklander op_info->operationState = op->operationState; 49005304565SCedric Chaumont 49105304565SCedric Chaumont out: 49205304565SCedric Chaumont if (res != TEE_SUCCESS && 49305304565SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 494b36311adSJerome Forissier TEE_Panic(res); 49505304565SCedric Chaumont 49605304565SCedric Chaumont return res; 49705304565SCedric Chaumont } 49805304565SCedric Chaumont 499b0104773SPascal Brand void TEE_ResetOperation(TEE_OperationHandle operation) 500b0104773SPascal Brand { 501b0104773SPascal Brand TEE_Result res; 502b0104773SPascal Brand 503b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 504b0104773SPascal Brand TEE_Panic(0); 505bf80076aSCedric Chaumont 506642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET)) 507bf80076aSCedric Chaumont TEE_Panic(0); 508bf80076aSCedric Chaumont 509642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 510642a1607SCedric Chaumont 511b0104773SPascal Brand if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 5122c028fdeSJerome Forissier res = _utee_hash_init(operation->state, NULL, 0); 513b0104773SPascal Brand if (res != TEE_SUCCESS) 514b0104773SPascal Brand TEE_Panic(res); 51505304565SCedric Chaumont operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 51605304565SCedric Chaumont } else { 517b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 518b0104773SPascal Brand } 51905304565SCedric Chaumont } 520b0104773SPascal Brand 521b0104773SPascal Brand TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation, 522b0104773SPascal Brand TEE_ObjectHandle key) 523b0104773SPascal Brand { 5247583c59eSCedric Chaumont TEE_Result res; 525b0104773SPascal Brand uint32_t key_size = 0; 526b0104773SPascal Brand TEE_ObjectInfo key_info; 527b0104773SPascal Brand 528a57c1e2eSCedric Chaumont if (operation == TEE_HANDLE_NULL) { 529a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 530a57c1e2eSCedric Chaumont goto out; 531a57c1e2eSCedric Chaumont } 532a57c1e2eSCedric Chaumont 533642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 534642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 535642a1607SCedric Chaumont goto out; 536642a1607SCedric Chaumont } 537642a1607SCedric Chaumont 538a57c1e2eSCedric Chaumont if (key == TEE_HANDLE_NULL) { 539a57c1e2eSCedric Chaumont /* Operation key cleared */ 540a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key1); 5416c4ea258SJens Wiklander operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 5426c4ea258SJens Wiklander return TEE_SUCCESS; 543a57c1e2eSCedric Chaumont } 544a57c1e2eSCedric Chaumont 545a57c1e2eSCedric Chaumont /* No key for digest operation */ 546a57c1e2eSCedric Chaumont if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 547a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 548a57c1e2eSCedric Chaumont goto out; 549a57c1e2eSCedric Chaumont } 550a57c1e2eSCedric Chaumont 551a57c1e2eSCedric Chaumont /* Two keys flag not expected (TEE_ALG_AES_XTS excluded) */ 552a57c1e2eSCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 553a57c1e2eSCedric Chaumont 0) { 554a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 555a57c1e2eSCedric Chaumont goto out; 556a57c1e2eSCedric Chaumont } 557a57c1e2eSCedric Chaumont 5587583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key, &key_info); 559a57c1e2eSCedric Chaumont /* Key is not a valid handle */ 5607583c59eSCedric Chaumont if (res != TEE_SUCCESS) 561a57c1e2eSCedric Chaumont goto out; 5627583c59eSCedric Chaumont 563b0104773SPascal Brand /* Supplied key has to meet required usage */ 564b0104773SPascal Brand if ((key_info.objectUsage & operation->info.requiredKeyUsage) != 565b0104773SPascal Brand operation->info.requiredKeyUsage) { 566a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 567a57c1e2eSCedric Chaumont goto out; 568b0104773SPascal Brand } 569b0104773SPascal Brand 570a57c1e2eSCedric Chaumont if (operation->info.maxKeySize < key_info.keySize) { 571a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 572a57c1e2eSCedric Chaumont goto out; 573a57c1e2eSCedric Chaumont } 574b0104773SPascal Brand 5757583c59eSCedric Chaumont key_size = key_info.keySize; 576b0104773SPascal Brand 577b0104773SPascal Brand TEE_ResetTransientObject(operation->key1); 578b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 579b0104773SPascal Brand 5807583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key1, key); 5817583c59eSCedric Chaumont if (res != TEE_SUCCESS) 582a57c1e2eSCedric Chaumont goto out; 5837583c59eSCedric Chaumont 584b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 585b0104773SPascal Brand 586b0104773SPascal Brand operation->info.keySize = key_size; 587b0104773SPascal Brand 5887583c59eSCedric Chaumont out: 589a57c1e2eSCedric Chaumont if (res != TEE_SUCCESS && 590a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 591a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 592b36311adSJerome Forissier TEE_Panic(res); 593a57c1e2eSCedric Chaumont 594a57c1e2eSCedric Chaumont return res; 595b0104773SPascal Brand } 596b0104773SPascal Brand 597b0104773SPascal Brand TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation, 598b0104773SPascal Brand TEE_ObjectHandle key1, TEE_ObjectHandle key2) 599b0104773SPascal Brand { 6007583c59eSCedric Chaumont TEE_Result res; 601b0104773SPascal Brand uint32_t key_size = 0; 602b0104773SPascal Brand TEE_ObjectInfo key_info1; 603b0104773SPascal Brand TEE_ObjectInfo key_info2; 604b0104773SPascal Brand 605a57c1e2eSCedric Chaumont if (operation == TEE_HANDLE_NULL) { 606a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 607a57c1e2eSCedric Chaumont goto out; 608a57c1e2eSCedric Chaumont } 609a57c1e2eSCedric Chaumont 610642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 611642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 612642a1607SCedric Chaumont goto out; 613642a1607SCedric Chaumont } 614642a1607SCedric Chaumont 615a57c1e2eSCedric Chaumont /* 616a57c1e2eSCedric Chaumont * Key1/Key2 and/or are not initialized and 617a57c1e2eSCedric Chaumont * Either both keys are NULL or both are not NULL 618a57c1e2eSCedric Chaumont */ 6196c4ea258SJens Wiklander if (!key1 && !key2) { 6206c4ea258SJens Wiklander /* Clear the keys */ 621a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key1); 622a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key2); 6236c4ea258SJens Wiklander operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 6246c4ea258SJens Wiklander return TEE_SUCCESS; 6256c4ea258SJens Wiklander } else if (!key1 || !key2) { 6266c4ea258SJens Wiklander /* Both keys are obviously not valid. */ 627a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 628a57c1e2eSCedric Chaumont goto out; 629a57c1e2eSCedric Chaumont } 630a57c1e2eSCedric Chaumont 631a57c1e2eSCedric Chaumont /* No key for digest operation */ 632a57c1e2eSCedric Chaumont if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 633a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 634a57c1e2eSCedric Chaumont goto out; 635a57c1e2eSCedric Chaumont } 636a57c1e2eSCedric Chaumont 6375b385b3fSJerome Forissier /* Two keys flag expected (TEE_ALG_AES_XTS and TEE_ALG_SM2_KEP only) */ 638a57c1e2eSCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 639a57c1e2eSCedric Chaumont 0) { 640a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 641a57c1e2eSCedric Chaumont goto out; 642a57c1e2eSCedric Chaumont } 643a57c1e2eSCedric Chaumont 6447583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key1, &key_info1); 645a57c1e2eSCedric Chaumont /* Key1 is not a valid handle */ 6467583c59eSCedric Chaumont if (res != TEE_SUCCESS) 647a57c1e2eSCedric Chaumont goto out; 6487583c59eSCedric Chaumont 649b0104773SPascal Brand /* Supplied key has to meet required usage */ 650b0104773SPascal Brand if ((key_info1.objectUsage & operation->info. 651b0104773SPascal Brand requiredKeyUsage) != operation->info.requiredKeyUsage) { 652a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 653a57c1e2eSCedric Chaumont goto out; 654b0104773SPascal Brand } 655b0104773SPascal Brand 6567583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key2, &key_info2); 657a57c1e2eSCedric Chaumont /* Key2 is not a valid handle */ 6587583c59eSCedric Chaumont if (res != TEE_SUCCESS) { 6597583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) 6607583c59eSCedric Chaumont res = TEE_ERROR_CORRUPT_OBJECT_2; 661a57c1e2eSCedric Chaumont goto out; 6627583c59eSCedric Chaumont } 6637583c59eSCedric Chaumont 664b0104773SPascal Brand /* Supplied key has to meet required usage */ 665b0104773SPascal Brand if ((key_info2.objectUsage & operation->info. 666b0104773SPascal Brand requiredKeyUsage) != operation->info.requiredKeyUsage) { 667a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 668a57c1e2eSCedric Chaumont goto out; 669b0104773SPascal Brand } 670b0104773SPascal Brand 671b0104773SPascal Brand /* 6725b385b3fSJerome Forissier * All the multi key algorithm currently supported requires the keys to 6735b385b3fSJerome Forissier * be of equal size. 674b0104773SPascal Brand */ 6755b385b3fSJerome Forissier if (key_info1.keySize != key_info2.keySize) { 676a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 677a57c1e2eSCedric Chaumont goto out; 678b0104773SPascal Brand 679a57c1e2eSCedric Chaumont } 680a57c1e2eSCedric Chaumont 681a57c1e2eSCedric Chaumont if (operation->info.maxKeySize < key_info1.keySize) { 682a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 683a57c1e2eSCedric Chaumont goto out; 684a57c1e2eSCedric Chaumont } 685b0104773SPascal Brand 686b0104773SPascal Brand /* 687b0104773SPascal Brand * Odd that only the size of one key should be reported while 688b0104773SPascal Brand * size of two key are used when allocating the operation. 689b0104773SPascal Brand */ 6907583c59eSCedric Chaumont key_size = key_info1.keySize; 691b0104773SPascal Brand 692b0104773SPascal Brand TEE_ResetTransientObject(operation->key1); 693b0104773SPascal Brand TEE_ResetTransientObject(operation->key2); 694b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 695b0104773SPascal Brand 6967583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key1, key1); 6977583c59eSCedric Chaumont if (res != TEE_SUCCESS) 698a57c1e2eSCedric Chaumont goto out; 6997583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key2, key2); 7007583c59eSCedric Chaumont if (res != TEE_SUCCESS) { 7017583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) 7027583c59eSCedric Chaumont res = TEE_ERROR_CORRUPT_OBJECT_2; 703a57c1e2eSCedric Chaumont goto out; 7047583c59eSCedric Chaumont } 7057583c59eSCedric Chaumont 706b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 707b0104773SPascal Brand 708b0104773SPascal Brand operation->info.keySize = key_size; 709b0104773SPascal Brand 7107583c59eSCedric Chaumont out: 711a57c1e2eSCedric Chaumont if (res != TEE_SUCCESS && 712a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 713a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT_2 && 714a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE && 715a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE_2) 716b36311adSJerome Forissier TEE_Panic(res); 717a57c1e2eSCedric Chaumont 718a57c1e2eSCedric Chaumont return res; 719b0104773SPascal Brand } 720b0104773SPascal Brand 721b0104773SPascal Brand void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op) 722b0104773SPascal Brand { 723b0104773SPascal Brand TEE_Result res; 724b0104773SPascal Brand 725b0104773SPascal Brand if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL) 726b0104773SPascal Brand TEE_Panic(0); 727b0104773SPascal Brand if (dst_op->info.algorithm != src_op->info.algorithm) 728b0104773SPascal Brand TEE_Panic(0); 7298734de30SJens Wiklander if (dst_op->info.mode != src_op->info.mode) 7308734de30SJens Wiklander TEE_Panic(0); 731b0104773SPascal Brand if (src_op->info.operationClass != TEE_OPERATION_DIGEST) { 732b0104773SPascal Brand TEE_ObjectHandle key1 = TEE_HANDLE_NULL; 733b0104773SPascal Brand TEE_ObjectHandle key2 = TEE_HANDLE_NULL; 734b0104773SPascal Brand 735b0104773SPascal Brand if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) { 736b0104773SPascal Brand key1 = src_op->key1; 737b0104773SPascal Brand key2 = src_op->key2; 738b0104773SPascal Brand } 739b0104773SPascal Brand 740b0104773SPascal Brand if ((src_op->info.handleState & 741b0104773SPascal Brand TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) { 742b0104773SPascal Brand TEE_SetOperationKey(dst_op, key1); 743b0104773SPascal Brand } else { 744b0104773SPascal Brand TEE_SetOperationKey2(dst_op, key1, key2); 745b0104773SPascal Brand } 746b0104773SPascal Brand } 747b0104773SPascal Brand dst_op->info.handleState = src_op->info.handleState; 748b0104773SPascal Brand dst_op->info.keySize = src_op->info.keySize; 7498e07702eSJens Wiklander dst_op->info.digestLength = src_op->info.digestLength; 750642a1607SCedric Chaumont dst_op->operationState = src_op->operationState; 751b0104773SPascal Brand 752b0104773SPascal Brand if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks || 753b0104773SPascal Brand dst_op->block_size != src_op->block_size) 754b0104773SPascal Brand TEE_Panic(0); 755b0104773SPascal Brand 756b0104773SPascal Brand if (dst_op->buffer != NULL) { 757b0104773SPascal Brand if (src_op->buffer == NULL) 758b0104773SPascal Brand TEE_Panic(0); 759b0104773SPascal Brand 760b0104773SPascal Brand memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs); 761b0104773SPascal Brand dst_op->buffer_offs = src_op->buffer_offs; 762b0104773SPascal Brand } else if (src_op->buffer != NULL) { 763b0104773SPascal Brand TEE_Panic(0); 764b0104773SPascal Brand } 765b0104773SPascal Brand 7662c028fdeSJerome Forissier res = _utee_cryp_state_copy(dst_op->state, src_op->state); 767b0104773SPascal Brand if (res != TEE_SUCCESS) 768b0104773SPascal Brand TEE_Panic(res); 769b0104773SPascal Brand } 770b0104773SPascal Brand 771b0104773SPascal Brand /* Cryptographic Operations API - Message Digest Functions */ 772b0104773SPascal Brand 7738f07fe6fSJerome Forissier static void init_hash_operation(TEE_OperationHandle operation, const void *IV, 7746d15db08SJerome Forissier uint32_t IVLen) 7756d15db08SJerome Forissier { 7766d15db08SJerome Forissier TEE_Result res; 7776d15db08SJerome Forissier 7786d15db08SJerome Forissier /* 7796d15db08SJerome Forissier * Note : IV and IVLen are never used in current implementation 7806d15db08SJerome Forissier * This is why coherent values of IV and IVLen are not checked 7816d15db08SJerome Forissier */ 7822c028fdeSJerome Forissier res = _utee_hash_init(operation->state, IV, IVLen); 7836d15db08SJerome Forissier if (res != TEE_SUCCESS) 7846d15db08SJerome Forissier TEE_Panic(res); 7856d15db08SJerome Forissier operation->buffer_offs = 0; 7866d15db08SJerome Forissier operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 7876d15db08SJerome Forissier } 7886d15db08SJerome Forissier 789b0104773SPascal Brand void TEE_DigestUpdate(TEE_OperationHandle operation, 7908f07fe6fSJerome Forissier const void *chunk, uint32_t chunkSize) 791b0104773SPascal Brand { 79273d6c3baSJoakim Bech TEE_Result res = TEE_ERROR_GENERIC; 793b0104773SPascal Brand 79473d6c3baSJoakim Bech if (operation == TEE_HANDLE_NULL || 79573d6c3baSJoakim Bech operation->info.operationClass != TEE_OPERATION_DIGEST) 796b0104773SPascal Brand TEE_Panic(0); 79773d6c3baSJoakim Bech 798642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 799642a1607SCedric Chaumont 8002c028fdeSJerome Forissier res = _utee_hash_update(operation->state, chunk, chunkSize); 801b0104773SPascal Brand if (res != TEE_SUCCESS) 802b0104773SPascal Brand TEE_Panic(res); 803b0104773SPascal Brand } 804b0104773SPascal Brand 8058f07fe6fSJerome Forissier TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void *chunk, 80679a3c601SCedric Chaumont uint32_t chunkLen, void *hash, uint32_t *hashLen) 807b0104773SPascal Brand { 80887c2f6b6SCedric Chaumont TEE_Result res; 809e86f1266SJens Wiklander uint64_t hl; 81087c2f6b6SCedric Chaumont 81187c2f6b6SCedric Chaumont if ((operation == TEE_HANDLE_NULL) || 81287c2f6b6SCedric Chaumont (!chunk && chunkLen) || 81387c2f6b6SCedric Chaumont (operation->info.operationClass != TEE_OPERATION_DIGEST)) { 81487c2f6b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 81587c2f6b6SCedric Chaumont goto out; 81687c2f6b6SCedric Chaumont } 8176915bbbbSJens Wiklander __utee_check_inout_annotation(hashLen, sizeof(*hashLen)); 81887c2f6b6SCedric Chaumont 819e86f1266SJens Wiklander hl = *hashLen; 8202c028fdeSJerome Forissier res = _utee_hash_final(operation->state, chunk, chunkLen, hash, &hl); 821e86f1266SJens Wiklander *hashLen = hl; 8226d15db08SJerome Forissier if (res != TEE_SUCCESS) 8236d15db08SJerome Forissier goto out; 8246d15db08SJerome Forissier 8256d15db08SJerome Forissier /* Reset operation state */ 8266d15db08SJerome Forissier init_hash_operation(operation, NULL, 0); 82787c2f6b6SCedric Chaumont 828642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 829642a1607SCedric Chaumont 83087c2f6b6SCedric Chaumont out: 83187c2f6b6SCedric Chaumont if (res != TEE_SUCCESS && 83287c2f6b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 833b36311adSJerome Forissier TEE_Panic(res); 83473d6c3baSJoakim Bech 83587c2f6b6SCedric Chaumont return res; 836b0104773SPascal Brand } 837b0104773SPascal Brand 838b0104773SPascal Brand /* Cryptographic Operations API - Symmetric Cipher Functions */ 839b0104773SPascal Brand 8408f07fe6fSJerome Forissier void TEE_CipherInit(TEE_OperationHandle operation, const void *IV, 8418f07fe6fSJerome Forissier uint32_t IVLen) 842b0104773SPascal Brand { 843b0104773SPascal Brand TEE_Result res; 844b0104773SPascal Brand 845b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 846b0104773SPascal Brand TEE_Panic(0); 847642a1607SCedric Chaumont 848b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_CIPHER) 849b0104773SPascal Brand TEE_Panic(0); 850642a1607SCedric Chaumont 851642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 852642a1607SCedric Chaumont !(operation->key1)) 853642a1607SCedric Chaumont TEE_Panic(0); 854642a1607SCedric Chaumont 855642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 856642a1607SCedric Chaumont TEE_ResetOperation(operation); 857642a1607SCedric Chaumont 858ad7aa2a5SSadiq Hussain if (IV && IVLen) { 859ad7aa2a5SSadiq Hussain if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD || 860ad7aa2a5SSadiq Hussain operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD || 861ad7aa2a5SSadiq Hussain operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || 862ad7aa2a5SSadiq Hussain operation->info.algorithm == TEE_ALG_SM4_ECB_NOPAD) 863ad7aa2a5SSadiq Hussain TEE_Panic(0); 864ad7aa2a5SSadiq Hussain } 865ad7aa2a5SSadiq Hussain 866642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 867642a1607SCedric Chaumont 8682c028fdeSJerome Forissier res = _utee_cipher_init(operation->state, IV, IVLen); 869b0104773SPascal Brand if (res != TEE_SUCCESS) 870b0104773SPascal Brand TEE_Panic(res); 871642a1607SCedric Chaumont 872b0104773SPascal Brand operation->buffer_offs = 0; 873b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 874b0104773SPascal Brand } 875b0104773SPascal Brand 876b0104773SPascal Brand static TEE_Result tee_buffer_update( 877b0104773SPascal Brand TEE_OperationHandle op, 878e86f1266SJens Wiklander TEE_Result(*update_func)(unsigned long state, const void *src, 879e86f1266SJens Wiklander size_t slen, void *dst, uint64_t *dlen), 880b0104773SPascal Brand const void *src_data, size_t src_len, 881e86f1266SJens Wiklander void *dest_data, uint64_t *dest_len) 882b0104773SPascal Brand { 883b0104773SPascal Brand TEE_Result res; 884b0104773SPascal Brand const uint8_t *src = src_data; 885b0104773SPascal Brand size_t slen = src_len; 886b0104773SPascal Brand uint8_t *dst = dest_data; 887b0104773SPascal Brand size_t dlen = *dest_len; 888b0104773SPascal Brand size_t acc_dlen = 0; 889e86f1266SJens Wiklander uint64_t tmp_dlen; 890b0104773SPascal Brand size_t l; 891b0104773SPascal Brand size_t buffer_size; 892d3588802SPascal Brand size_t buffer_left; 893b0104773SPascal Brand 894090268f5SJens Wiklander if (!src) { 895090268f5SJens Wiklander if (slen) 896090268f5SJens Wiklander TEE_Panic(0); 897090268f5SJens Wiklander goto out; 898090268f5SJens Wiklander } 899090268f5SJens Wiklander 900d3588802SPascal Brand if (op->buffer_two_blocks) { 901b0104773SPascal Brand buffer_size = op->block_size * 2; 902d3588802SPascal Brand buffer_left = 1; 903d3588802SPascal Brand } else { 904b0104773SPascal Brand buffer_size = op->block_size; 905d3588802SPascal Brand buffer_left = 0; 906d3588802SPascal Brand } 907b0104773SPascal Brand 908b0104773SPascal Brand if (op->buffer_offs > 0) { 909b0104773SPascal Brand /* Fill up complete block */ 910b0104773SPascal Brand if (op->buffer_offs < op->block_size) 911b0104773SPascal Brand l = MIN(slen, op->block_size - op->buffer_offs); 912b0104773SPascal Brand else 913b0104773SPascal Brand l = MIN(slen, buffer_size - op->buffer_offs); 914b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, l); 915b0104773SPascal Brand op->buffer_offs += l; 916b0104773SPascal Brand src += l; 917b0104773SPascal Brand slen -= l; 918b0104773SPascal Brand if ((op->buffer_offs % op->block_size) != 0) 919b0104773SPascal Brand goto out; /* Nothing left to do */ 920b0104773SPascal Brand } 921b0104773SPascal Brand 922b0104773SPascal Brand /* If we can feed from buffer */ 923d3588802SPascal Brand if ((op->buffer_offs > 0) && 924d3588802SPascal Brand ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) { 9252ff3fdbbSPascal Brand l = ROUNDUP(op->buffer_offs + slen - buffer_size, 926b0104773SPascal Brand op->block_size); 927b0104773SPascal Brand l = MIN(op->buffer_offs, l); 928b0104773SPascal Brand tmp_dlen = dlen; 929b0104773SPascal Brand res = update_func(op->state, op->buffer, l, dst, &tmp_dlen); 930b0104773SPascal Brand if (res != TEE_SUCCESS) 931b0104773SPascal Brand TEE_Panic(res); 932b0104773SPascal Brand dst += tmp_dlen; 933b0104773SPascal Brand dlen -= tmp_dlen; 934b0104773SPascal Brand acc_dlen += tmp_dlen; 935b0104773SPascal Brand op->buffer_offs -= l; 936b0104773SPascal Brand if (op->buffer_offs > 0) { 937b0104773SPascal Brand /* 938b0104773SPascal Brand * Slen is small enough to be contained in rest buffer. 939b0104773SPascal Brand */ 940b0104773SPascal Brand memcpy(op->buffer, op->buffer + l, buffer_size - l); 941b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 942b0104773SPascal Brand op->buffer_offs += slen; 943b0104773SPascal Brand goto out; /* Nothing left to do */ 944b0104773SPascal Brand } 945b0104773SPascal Brand } 946b0104773SPascal Brand 947d3588802SPascal Brand if (slen >= (buffer_size + buffer_left)) { 948b0104773SPascal Brand /* Buffer is empty, feed as much as possible from src */ 949bf7a587fSJerome Forissier if (op->info.algorithm == TEE_ALG_AES_CTS) 950b1ecda78SJerome Forissier l = ROUNDUP(slen - buffer_size, op->block_size); 951bf7a587fSJerome Forissier else 952bf7a587fSJerome Forissier l = ROUNDUP(slen - buffer_size + 1, op->block_size); 953b0104773SPascal Brand 954b0104773SPascal Brand tmp_dlen = dlen; 955b0104773SPascal Brand res = update_func(op->state, src, l, dst, &tmp_dlen); 956b0104773SPascal Brand if (res != TEE_SUCCESS) 957b0104773SPascal Brand TEE_Panic(res); 958b0104773SPascal Brand src += l; 959b0104773SPascal Brand slen -= l; 960b0104773SPascal Brand dst += tmp_dlen; 961b0104773SPascal Brand dlen -= tmp_dlen; 962b0104773SPascal Brand acc_dlen += tmp_dlen; 963b0104773SPascal Brand } 964b0104773SPascal Brand 965b0104773SPascal Brand /* Slen is small enough to be contained in buffer. */ 966b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 967b0104773SPascal Brand op->buffer_offs += slen; 968b0104773SPascal Brand 969b0104773SPascal Brand out: 970b0104773SPascal Brand *dest_len = acc_dlen; 971b0104773SPascal Brand return TEE_SUCCESS; 972b0104773SPascal Brand } 973b0104773SPascal Brand 9748f07fe6fSJerome Forissier TEE_Result TEE_CipherUpdate(TEE_OperationHandle operation, const void *srcData, 97579a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 976b0104773SPascal Brand { 977dea1f2b6SCedric Chaumont TEE_Result res; 978b0104773SPascal Brand size_t req_dlen; 979e86f1266SJens Wiklander uint64_t dl; 980b0104773SPascal Brand 9816915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 982dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 983dea1f2b6SCedric Chaumont goto out; 984dea1f2b6SCedric Chaumont } 9856915bbbbSJens Wiklander __utee_check_inout_annotation(destLen, sizeof(*destLen)); 986dea1f2b6SCedric Chaumont 987642a1607SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 988dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 989dea1f2b6SCedric Chaumont goto out; 990dea1f2b6SCedric Chaumont } 991dea1f2b6SCedric Chaumont 992642a1607SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 993642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 994642a1607SCedric Chaumont goto out; 995642a1607SCedric Chaumont } 996642a1607SCedric Chaumont 997642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 998dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 999dea1f2b6SCedric Chaumont goto out; 1000dea1f2b6SCedric Chaumont } 1001b0104773SPascal Brand 1002e32c5ddfSJerome Forissier if (!srcData && !srcLen) { 1003090268f5SJens Wiklander *destLen = 0; 1004e32c5ddfSJerome Forissier res = TEE_SUCCESS; 1005e32c5ddfSJerome Forissier goto out; 1006e32c5ddfSJerome Forissier } 1007e32c5ddfSJerome Forissier 1008b0104773SPascal Brand /* Calculate required dlen */ 100957aabac5SBogdan Liulko if (operation->block_size > 1) { 101057aabac5SBogdan Liulko req_dlen = ((operation->buffer_offs + srcLen) / 101157aabac5SBogdan Liulko operation->block_size) * operation->block_size; 101257aabac5SBogdan Liulko } else { 101357aabac5SBogdan Liulko req_dlen = srcLen; 101457aabac5SBogdan Liulko } 1015642a1607SCedric Chaumont if (operation->buffer_two_blocks) { 1016642a1607SCedric Chaumont if (req_dlen > operation->block_size * 2) 1017642a1607SCedric Chaumont req_dlen -= operation->block_size * 2; 1018b0104773SPascal Brand else 1019b0104773SPascal Brand req_dlen = 0; 1020b0104773SPascal Brand } 1021b0104773SPascal Brand /* 1022b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1023b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1024b0104773SPascal Brand * can't restore sync with this API. 1025b0104773SPascal Brand */ 1026b0104773SPascal Brand if (*destLen < req_dlen) { 1027b0104773SPascal Brand *destLen = req_dlen; 1028dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1029dea1f2b6SCedric Chaumont goto out; 1030b0104773SPascal Brand } 1031b0104773SPascal Brand 1032e86f1266SJens Wiklander dl = *destLen; 103357aabac5SBogdan Liulko if (operation->block_size > 1) { 10342c028fdeSJerome Forissier res = tee_buffer_update(operation, _utee_cipher_update, srcData, 103557aabac5SBogdan Liulko srcLen, destData, &dl); 103657aabac5SBogdan Liulko } else { 103757aabac5SBogdan Liulko if (srcLen > 0) { 10382c028fdeSJerome Forissier res = _utee_cipher_update(operation->state, srcData, 103957aabac5SBogdan Liulko srcLen, destData, &dl); 104057aabac5SBogdan Liulko } else { 104157aabac5SBogdan Liulko res = TEE_SUCCESS; 104257aabac5SBogdan Liulko dl = 0; 104357aabac5SBogdan Liulko } 104457aabac5SBogdan Liulko } 1045e86f1266SJens Wiklander *destLen = dl; 1046b0104773SPascal Brand 1047dea1f2b6SCedric Chaumont out: 1048dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 1049dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1050b36311adSJerome Forissier TEE_Panic(res); 1051dea1f2b6SCedric Chaumont 1052dea1f2b6SCedric Chaumont return res; 1053b0104773SPascal Brand } 1054b0104773SPascal Brand 1055642a1607SCedric Chaumont TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation, 10568f07fe6fSJerome Forissier const void *srcData, uint32_t srcLen, 10578f07fe6fSJerome Forissier void *destData, uint32_t *destLen) 1058b0104773SPascal Brand { 10596915bbbbSJens Wiklander TEE_Result res = TEE_SUCCESS; 1060b0104773SPascal Brand uint8_t *dst = destData; 1061b0104773SPascal Brand size_t acc_dlen = 0; 10626915bbbbSJens Wiklander uint64_t tmp_dlen = 0; 10636915bbbbSJens Wiklander size_t req_dlen = 0; 1064b0104773SPascal Brand 10656915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 1066dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1067dea1f2b6SCedric Chaumont goto out; 1068dea1f2b6SCedric Chaumont } 10696915bbbbSJens Wiklander if (destLen) 10706915bbbbSJens Wiklander __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1071dea1f2b6SCedric Chaumont 1072642a1607SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 1073dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1074dea1f2b6SCedric Chaumont goto out; 1075dea1f2b6SCedric Chaumont } 1076dea1f2b6SCedric Chaumont 1077642a1607SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1078642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1079642a1607SCedric Chaumont goto out; 1080642a1607SCedric Chaumont } 1081642a1607SCedric Chaumont 1082642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1083dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1084dea1f2b6SCedric Chaumont goto out; 1085dea1f2b6SCedric Chaumont } 1086b0104773SPascal Brand 1087b0104773SPascal Brand /* 1088b0104773SPascal Brand * Check that the final block doesn't require padding for those 1089b0104773SPascal Brand * algorithms that requires client to supply padding. 1090b0104773SPascal Brand */ 1091642a1607SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD || 1092642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD || 1093642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD || 1094642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD || 1095642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || 1096ade6f848SJerome Forissier operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD || 1097ade6f848SJerome Forissier operation->info.algorithm == TEE_ALG_SM4_ECB_NOPAD || 1098ade6f848SJerome Forissier operation->info.algorithm == TEE_ALG_SM4_CBC_NOPAD) { 1099642a1607SCedric Chaumont if (((operation->buffer_offs + srcLen) % operation->block_size) 1100642a1607SCedric Chaumont != 0) { 1101dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1102dea1f2b6SCedric Chaumont goto out; 1103dea1f2b6SCedric Chaumont } 1104b0104773SPascal Brand } 1105b0104773SPascal Brand 1106b0104773SPascal Brand /* 1107b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1108b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1109b0104773SPascal Brand * can't restore sync with this API. 1110b0104773SPascal Brand */ 111157aabac5SBogdan Liulko if (operation->block_size > 1) { 1112642a1607SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 111357aabac5SBogdan Liulko } else { 111457aabac5SBogdan Liulko req_dlen = srcLen; 111557aabac5SBogdan Liulko } 11166915bbbbSJens Wiklander if (destLen) 11176915bbbbSJens Wiklander tmp_dlen = *destLen; 11186915bbbbSJens Wiklander if (tmp_dlen < req_dlen) { 11196915bbbbSJens Wiklander if (destLen) 1120b0104773SPascal Brand *destLen = req_dlen; 1121dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1122dea1f2b6SCedric Chaumont goto out; 1123b0104773SPascal Brand } 1124b0104773SPascal Brand 112557aabac5SBogdan Liulko if (operation->block_size > 1) { 1126dea9063eSJens Wiklander if (srcLen) { 11272c028fdeSJerome Forissier res = tee_buffer_update(operation, _utee_cipher_update, 1128dea9063eSJens Wiklander srcData, srcLen, dst, 1129dea9063eSJens Wiklander &tmp_dlen); 1130dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS) 1131dea1f2b6SCedric Chaumont goto out; 1132dea1f2b6SCedric Chaumont 1133b0104773SPascal Brand dst += tmp_dlen; 1134b0104773SPascal Brand acc_dlen += tmp_dlen; 1135b0104773SPascal Brand 1136b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1137dea9063eSJens Wiklander } 11382c028fdeSJerome Forissier res = _utee_cipher_final(operation->state, operation->buffer, 11392c028fdeSJerome Forissier operation->buffer_offs, dst, 11402c028fdeSJerome Forissier &tmp_dlen); 114157aabac5SBogdan Liulko } else { 11422c028fdeSJerome Forissier res = _utee_cipher_final(operation->state, srcData, srcLen, dst, 11432c028fdeSJerome Forissier &tmp_dlen); 114457aabac5SBogdan Liulko } 1145b0104773SPascal Brand if (res != TEE_SUCCESS) 1146dea1f2b6SCedric Chaumont goto out; 1147dea1f2b6SCedric Chaumont 1148b0104773SPascal Brand acc_dlen += tmp_dlen; 11496915bbbbSJens Wiklander if (destLen) 1150b0104773SPascal Brand *destLen = acc_dlen; 1151dea1f2b6SCedric Chaumont 1152642a1607SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1153642a1607SCedric Chaumont 1154642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1155642a1607SCedric Chaumont 1156dea1f2b6SCedric Chaumont out: 1157dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 1158dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1159b36311adSJerome Forissier TEE_Panic(res); 1160dea1f2b6SCedric Chaumont 1161dea1f2b6SCedric Chaumont return res; 1162b0104773SPascal Brand } 1163b0104773SPascal Brand 1164b0104773SPascal Brand /* Cryptographic Operations API - MAC Functions */ 1165b0104773SPascal Brand 11668f07fe6fSJerome Forissier void TEE_MACInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen) 1167b0104773SPascal Brand { 1168b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 1169b0104773SPascal Brand TEE_Panic(0); 1170642a1607SCedric Chaumont 1171b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_MAC) 1172b0104773SPascal Brand TEE_Panic(0); 1173642a1607SCedric Chaumont 1174642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 1175642a1607SCedric Chaumont !(operation->key1)) 1176642a1607SCedric Chaumont TEE_Panic(0); 1177642a1607SCedric Chaumont 1178642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 1179642a1607SCedric Chaumont TEE_ResetOperation(operation); 1180642a1607SCedric Chaumont 1181642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1182642a1607SCedric Chaumont 11836d15db08SJerome Forissier init_hash_operation(operation, IV, IVLen); 1184b0104773SPascal Brand } 1185b0104773SPascal Brand 11868f07fe6fSJerome Forissier void TEE_MACUpdate(TEE_OperationHandle operation, const void *chunk, 118728e0efc6SCedric Chaumont uint32_t chunkSize) 1188b0104773SPascal Brand { 1189b0104773SPascal Brand TEE_Result res; 1190b0104773SPascal Brand 119128e0efc6SCedric Chaumont if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0)) 1192b0104773SPascal Brand TEE_Panic(0); 1193642a1607SCedric Chaumont 119428e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) 1195b0104773SPascal Brand TEE_Panic(0); 1196642a1607SCedric Chaumont 119728e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1198b0104773SPascal Brand TEE_Panic(0); 1199b0104773SPascal Brand 1200642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) 1201642a1607SCedric Chaumont TEE_Panic(0); 1202642a1607SCedric Chaumont 12032c028fdeSJerome Forissier res = _utee_hash_update(operation->state, chunk, chunkSize); 1204b0104773SPascal Brand if (res != TEE_SUCCESS) 1205b0104773SPascal Brand TEE_Panic(res); 1206b0104773SPascal Brand } 1207b0104773SPascal Brand 120828e0efc6SCedric Chaumont TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation, 12098f07fe6fSJerome Forissier const void *message, uint32_t messageLen, 121079a3c601SCedric Chaumont void *mac, uint32_t *macLen) 1211b0104773SPascal Brand { 1212b0104773SPascal Brand TEE_Result res; 1213e86f1266SJens Wiklander uint64_t ml; 1214b0104773SPascal Brand 12156915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!message && messageLen)) { 121628e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 121728e0efc6SCedric Chaumont goto out; 121828e0efc6SCedric Chaumont } 12196915bbbbSJens Wiklander __utee_check_inout_annotation(macLen, sizeof(*macLen)); 1220b0104773SPascal Brand 122128e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 122228e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 122328e0efc6SCedric Chaumont goto out; 122428e0efc6SCedric Chaumont } 122528e0efc6SCedric Chaumont 122628e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 122728e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 122828e0efc6SCedric Chaumont goto out; 122928e0efc6SCedric Chaumont } 123028e0efc6SCedric Chaumont 1231642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1232642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1233642a1607SCedric Chaumont goto out; 1234642a1607SCedric Chaumont } 1235642a1607SCedric Chaumont 1236e86f1266SJens Wiklander ml = *macLen; 12372c028fdeSJerome Forissier res = _utee_hash_final(operation->state, message, messageLen, mac, &ml); 1238e86f1266SJens Wiklander *macLen = ml; 123928e0efc6SCedric Chaumont if (res != TEE_SUCCESS) 124028e0efc6SCedric Chaumont goto out; 124128e0efc6SCedric Chaumont 124228e0efc6SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 124328e0efc6SCedric Chaumont 1244642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1245642a1607SCedric Chaumont 124628e0efc6SCedric Chaumont out: 124728e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 124828e0efc6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 124928e0efc6SCedric Chaumont TEE_Panic(res); 125028e0efc6SCedric Chaumont 1251b0104773SPascal Brand return res; 1252b0104773SPascal Brand } 1253b0104773SPascal Brand 1254b0104773SPascal Brand TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation, 12558f07fe6fSJerome Forissier const void *message, uint32_t messageLen, 12568f07fe6fSJerome Forissier const void *mac, uint32_t macLen) 1257b0104773SPascal Brand { 1258b0104773SPascal Brand TEE_Result res; 1259ee4ba3d1SVictor Chong uint8_t computed_mac[TEE_MAX_HASH_SIZE] = { 0 }; 12607f74c64aSPascal Brand uint32_t computed_mac_size = TEE_MAX_HASH_SIZE; 1261b0104773SPascal Brand 126228e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 126328e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 126428e0efc6SCedric Chaumont goto out; 126528e0efc6SCedric Chaumont } 126628e0efc6SCedric Chaumont 126728e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 126828e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 126928e0efc6SCedric Chaumont goto out; 127028e0efc6SCedric Chaumont } 127128e0efc6SCedric Chaumont 1272642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1273642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1274642a1607SCedric Chaumont goto out; 1275642a1607SCedric Chaumont } 1276642a1607SCedric Chaumont 1277b0104773SPascal Brand res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac, 1278b0104773SPascal Brand &computed_mac_size); 1279b0104773SPascal Brand if (res != TEE_SUCCESS) 128028e0efc6SCedric Chaumont goto out; 128128e0efc6SCedric Chaumont 128228e0efc6SCedric Chaumont if (computed_mac_size != macLen) { 128328e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 128428e0efc6SCedric Chaumont goto out; 128528e0efc6SCedric Chaumont } 128628e0efc6SCedric Chaumont 128748e10604SJerome Forissier if (consttime_memcmp(mac, computed_mac, computed_mac_size) != 0) { 128828e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 128928e0efc6SCedric Chaumont goto out; 129028e0efc6SCedric Chaumont } 129128e0efc6SCedric Chaumont 1292642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1293642a1607SCedric Chaumont 129428e0efc6SCedric Chaumont out: 129528e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 129628e0efc6SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 129728e0efc6SCedric Chaumont TEE_Panic(res); 129828e0efc6SCedric Chaumont 1299b0104773SPascal Brand return res; 1300b0104773SPascal Brand } 1301b0104773SPascal Brand 1302b0104773SPascal Brand /* Cryptographic Operations API - Authenticated Encryption Functions */ 1303b0104773SPascal Brand 13048f07fe6fSJerome Forissier TEE_Result TEE_AEInit(TEE_OperationHandle operation, const void *nonce, 130579a3c601SCedric Chaumont uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen, 1306b0104773SPascal Brand uint32_t payloadLen) 1307b0104773SPascal Brand { 1308b0104773SPascal Brand TEE_Result res; 1309b0104773SPascal Brand 1310b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || nonce == NULL) { 1311b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1312b5816c88SCedric Chaumont goto out; 1313b5816c88SCedric Chaumont } 1314b5816c88SCedric Chaumont 1315b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1316b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1317b5816c88SCedric Chaumont goto out; 1318b5816c88SCedric Chaumont } 1319b0104773SPascal Brand 1320642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 1321642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1322642a1607SCedric Chaumont goto out; 1323642a1607SCedric Chaumont } 1324642a1607SCedric Chaumont 1325b0104773SPascal Brand /* 1326b0104773SPascal Brand * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core 1327b0104773SPascal Brand * in the implementation. But AES-GCM spec doesn't specify the tag len 1328b0104773SPascal Brand * according to the same principle so we have to check here instead to 1329b0104773SPascal Brand * be GP compliant. 1330b0104773SPascal Brand */ 1331b5816c88SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_GCM) { 1332b0104773SPascal Brand /* 1333b0104773SPascal Brand * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96 1334b0104773SPascal Brand */ 1335b5816c88SCedric Chaumont if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) { 1336b5816c88SCedric Chaumont res = TEE_ERROR_NOT_SUPPORTED; 1337b5816c88SCedric Chaumont goto out; 1338b5816c88SCedric Chaumont } 1339b0104773SPascal Brand } 1340b0104773SPascal Brand 13412c028fdeSJerome Forissier res = _utee_authenc_init(operation->state, nonce, nonceLen, tagLen / 8, 13422c028fdeSJerome Forissier AADLen, payloadLen); 1343b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1344b5816c88SCedric Chaumont goto out; 1345b5816c88SCedric Chaumont 13467acaf5adSAlbert Schwarzkopf operation->info.digestLength = tagLen / 8; 1347f2674567SSumit Garg operation->buffer_offs = 0; 1348b5816c88SCedric Chaumont operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 1349b5816c88SCedric Chaumont 1350b5816c88SCedric Chaumont out: 1351b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1352b5816c88SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 1353b0104773SPascal Brand TEE_Panic(res); 1354b5816c88SCedric Chaumont 1355b0104773SPascal Brand return res; 1356b0104773SPascal Brand } 1357b0104773SPascal Brand 13588f07fe6fSJerome Forissier void TEE_AEUpdateAAD(TEE_OperationHandle operation, const void *AADdata, 135979a3c601SCedric Chaumont uint32_t AADdataLen) 1360b0104773SPascal Brand { 1361b0104773SPascal Brand TEE_Result res; 1362b0104773SPascal Brand 1363b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1364b5816c88SCedric Chaumont (AADdata == NULL && AADdataLen != 0)) 1365b0104773SPascal Brand TEE_Panic(0); 1366642a1607SCedric Chaumont 1367b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) 1368b0104773SPascal Brand TEE_Panic(0); 1369642a1607SCedric Chaumont 1370b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1371b0104773SPascal Brand TEE_Panic(0); 1372b0104773SPascal Brand 13732c028fdeSJerome Forissier res = _utee_authenc_update_aad(operation->state, AADdata, AADdataLen); 1374642a1607SCedric Chaumont 1375642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1376642a1607SCedric Chaumont 1377b0104773SPascal Brand if (res != TEE_SUCCESS) 1378b0104773SPascal Brand TEE_Panic(res); 1379b0104773SPascal Brand } 1380b0104773SPascal Brand 13818f07fe6fSJerome Forissier TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, const void *srcData, 138279a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 1383b0104773SPascal Brand { 13846915bbbbSJens Wiklander TEE_Result res = TEE_SUCCESS; 13856915bbbbSJens Wiklander size_t req_dlen = 0; 13866915bbbbSJens Wiklander uint64_t dl = 0; 1387b0104773SPascal Brand 13886915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 1389b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1390b5816c88SCedric Chaumont goto out; 1391b5816c88SCedric Chaumont } 13926915bbbbSJens Wiklander __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1393b5816c88SCedric Chaumont 1394b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1395b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1396b5816c88SCedric Chaumont goto out; 1397b5816c88SCedric Chaumont } 1398b5816c88SCedric Chaumont 1399b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1400b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1401b5816c88SCedric Chaumont goto out; 1402b5816c88SCedric Chaumont } 1403b0104773SPascal Brand 1404827308b8SJerome Forissier if (!srcData && !srcLen) { 1405090268f5SJens Wiklander *destLen = 0; 1406827308b8SJerome Forissier res = TEE_SUCCESS; 1407827308b8SJerome Forissier goto out; 1408827308b8SJerome Forissier } 1409827308b8SJerome Forissier 1410b0104773SPascal Brand /* 1411b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1412b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1413b0104773SPascal Brand * can't restore sync with this API. 1414b0104773SPascal Brand */ 1415afc0c182SBogdan Liulko if (operation->block_size > 1) { 1416b5816c88SCedric Chaumont req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen, 1417b5816c88SCedric Chaumont operation->block_size); 1418afc0c182SBogdan Liulko } else { 1419afc0c182SBogdan Liulko req_dlen = srcLen; 1420afc0c182SBogdan Liulko } 1421afc0c182SBogdan Liulko 14226915bbbbSJens Wiklander dl = *destLen; 14236915bbbbSJens Wiklander if (dl < req_dlen) { 1424b0104773SPascal Brand *destLen = req_dlen; 1425b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1426b5816c88SCedric Chaumont goto out; 1427b0104773SPascal Brand } 1428b0104773SPascal Brand 1429afc0c182SBogdan Liulko if (operation->block_size > 1) { 14302c028fdeSJerome Forissier res = tee_buffer_update(operation, _utee_authenc_update_payload, 1431afc0c182SBogdan Liulko srcData, srcLen, destData, &dl); 1432afc0c182SBogdan Liulko } else { 1433afc0c182SBogdan Liulko if (srcLen > 0) { 14342c028fdeSJerome Forissier res = _utee_authenc_update_payload(operation->state, 1435afc0c182SBogdan Liulko srcData, srcLen, 1436afc0c182SBogdan Liulko destData, &dl); 1437afc0c182SBogdan Liulko } else { 1438afc0c182SBogdan Liulko dl = 0; 1439afc0c182SBogdan Liulko res = TEE_SUCCESS; 1440afc0c182SBogdan Liulko } 1441afc0c182SBogdan Liulko } 1442afc0c182SBogdan Liulko if (res != TEE_SUCCESS) 1443afc0c182SBogdan Liulko goto out; 1444afc0c182SBogdan Liulko 1445e86f1266SJens Wiklander *destLen = dl; 1446b0104773SPascal Brand 1447642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1448642a1607SCedric Chaumont 1449b5816c88SCedric Chaumont out: 1450b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1451b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1452b5816c88SCedric Chaumont TEE_Panic(res); 1453b5816c88SCedric Chaumont 1454b5816c88SCedric Chaumont return res; 1455b0104773SPascal Brand } 1456b0104773SPascal Brand 1457b5816c88SCedric Chaumont TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation, 14588f07fe6fSJerome Forissier const void *srcData, uint32_t srcLen, 145979a3c601SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 146079a3c601SCedric Chaumont uint32_t *tagLen) 1461b0104773SPascal Brand { 1462b0104773SPascal Brand TEE_Result res; 1463b0104773SPascal Brand uint8_t *dst = destData; 1464b0104773SPascal Brand size_t acc_dlen = 0; 1465e86f1266SJens Wiklander uint64_t tmp_dlen; 1466b0104773SPascal Brand size_t req_dlen; 1467e86f1266SJens Wiklander uint64_t tl; 1468b0104773SPascal Brand 14696915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 1470b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1471b5816c88SCedric Chaumont goto out; 1472b5816c88SCedric Chaumont } 14736915bbbbSJens Wiklander __utee_check_inout_annotation(destLen, sizeof(*destLen)); 14746915bbbbSJens Wiklander __utee_check_inout_annotation(tagLen, sizeof(*tagLen)); 1475b5816c88SCedric Chaumont 1476b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1477b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1478b5816c88SCedric Chaumont goto out; 1479b5816c88SCedric Chaumont } 1480b5816c88SCedric Chaumont 1481b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1482b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1483b5816c88SCedric Chaumont goto out; 1484b5816c88SCedric Chaumont } 1485b0104773SPascal Brand 1486b0104773SPascal Brand /* 1487b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1488b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1489b0104773SPascal Brand * can't restore sync with this API. 14902733280aSEtienne Carriere * 14912733280aSEtienne Carriere * Need to check this before update_payload since sync would be lost if 14922733280aSEtienne Carriere * we return short buffer after that. 1493b0104773SPascal Brand */ 14942733280aSEtienne Carriere res = TEE_ERROR_GENERIC; 14952733280aSEtienne Carriere 1496b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1497b0104773SPascal Brand if (*destLen < req_dlen) { 1498b0104773SPascal Brand *destLen = req_dlen; 1499b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1500b0104773SPascal Brand } 1501b0104773SPascal Brand 15027acaf5adSAlbert Schwarzkopf if (*tagLen < operation->info.digestLength) { 15037acaf5adSAlbert Schwarzkopf *tagLen = operation->info.digestLength; 1504b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1505b0104773SPascal Brand } 1506b0104773SPascal Brand 15072733280aSEtienne Carriere if (res == TEE_ERROR_SHORT_BUFFER) 15082733280aSEtienne Carriere goto out; 15092733280aSEtienne Carriere 1510afc0c182SBogdan Liulko tl = *tagLen; 1511b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1512afc0c182SBogdan Liulko if (operation->block_size > 1) { 15132c028fdeSJerome Forissier res = tee_buffer_update(operation, _utee_authenc_update_payload, 1514afc0c182SBogdan Liulko srcData, srcLen, dst, &tmp_dlen); 1515b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1516b5816c88SCedric Chaumont goto out; 1517b5816c88SCedric Chaumont 1518b0104773SPascal Brand dst += tmp_dlen; 1519b0104773SPascal Brand acc_dlen += tmp_dlen; 1520b0104773SPascal Brand 1521b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 15222c028fdeSJerome Forissier res = _utee_authenc_enc_final(operation->state, 1523afc0c182SBogdan Liulko operation->buffer, 1524afc0c182SBogdan Liulko operation->buffer_offs, dst, 1525afc0c182SBogdan Liulko &tmp_dlen, tag, &tl); 1526afc0c182SBogdan Liulko } else { 15272c028fdeSJerome Forissier res = _utee_authenc_enc_final(operation->state, srcData, 1528afc0c182SBogdan Liulko srcLen, dst, &tmp_dlen, 1529e86f1266SJens Wiklander tag, &tl); 1530afc0c182SBogdan Liulko } 1531e86f1266SJens Wiklander *tagLen = tl; 1532b0104773SPascal Brand if (res != TEE_SUCCESS) 1533b5816c88SCedric Chaumont goto out; 1534b0104773SPascal Brand 1535b5816c88SCedric Chaumont acc_dlen += tmp_dlen; 1536b0104773SPascal Brand *destLen = acc_dlen; 1537642a1607SCedric Chaumont 1538b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1539b5816c88SCedric Chaumont 1540642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1541642a1607SCedric Chaumont 1542b5816c88SCedric Chaumont out: 1543b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1544b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1545b5816c88SCedric Chaumont TEE_Panic(res); 1546b0104773SPascal Brand 1547b0104773SPascal Brand return res; 1548b0104773SPascal Brand } 1549b0104773SPascal Brand 1550b5816c88SCedric Chaumont TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation, 15518f07fe6fSJerome Forissier const void *srcData, uint32_t srcLen, 1552b5816c88SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 155379a3c601SCedric Chaumont uint32_t tagLen) 1554b0104773SPascal Brand { 1555b0104773SPascal Brand TEE_Result res; 1556b0104773SPascal Brand uint8_t *dst = destData; 1557b0104773SPascal Brand size_t acc_dlen = 0; 1558e86f1266SJens Wiklander uint64_t tmp_dlen; 1559b0104773SPascal Brand size_t req_dlen; 1560b0104773SPascal Brand 15616915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) { 1562b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1563b5816c88SCedric Chaumont goto out; 1564b5816c88SCedric Chaumont } 15656915bbbbSJens Wiklander __utee_check_inout_annotation(destLen, sizeof(*destLen)); 1566b5816c88SCedric Chaumont 1567b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1568b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1569b5816c88SCedric Chaumont goto out; 1570b5816c88SCedric Chaumont } 1571b5816c88SCedric Chaumont 1572b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1573b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1574b5816c88SCedric Chaumont goto out; 1575b5816c88SCedric Chaumont } 1576b0104773SPascal Brand 1577b0104773SPascal Brand /* 1578b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1579b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1580b0104773SPascal Brand * can't restore sync with this API. 1581b0104773SPascal Brand */ 1582b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1583b0104773SPascal Brand if (*destLen < req_dlen) { 1584b0104773SPascal Brand *destLen = req_dlen; 1585b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1586b5816c88SCedric Chaumont goto out; 1587b0104773SPascal Brand } 1588b0104773SPascal Brand 1589b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1590afc0c182SBogdan Liulko if (operation->block_size > 1) { 15912c028fdeSJerome Forissier res = tee_buffer_update(operation, _utee_authenc_update_payload, 1592afc0c182SBogdan Liulko srcData, srcLen, dst, &tmp_dlen); 1593b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1594b5816c88SCedric Chaumont goto out; 1595b5816c88SCedric Chaumont 1596b0104773SPascal Brand dst += tmp_dlen; 1597b0104773SPascal Brand acc_dlen += tmp_dlen; 1598b0104773SPascal Brand 1599b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 16002c028fdeSJerome Forissier res = _utee_authenc_dec_final(operation->state, 1601afc0c182SBogdan Liulko operation->buffer, 1602afc0c182SBogdan Liulko operation->buffer_offs, dst, 1603afc0c182SBogdan Liulko &tmp_dlen, tag, tagLen); 1604afc0c182SBogdan Liulko } else { 16052c028fdeSJerome Forissier res = _utee_authenc_dec_final(operation->state, srcData, 1606afc0c182SBogdan Liulko srcLen, dst, &tmp_dlen, 1607b5816c88SCedric Chaumont tag, tagLen); 1608afc0c182SBogdan Liulko } 1609b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1610b5816c88SCedric Chaumont goto out; 1611b5816c88SCedric Chaumont 1612b0104773SPascal Brand /* Supplied tagLen should match what we initiated with */ 16137acaf5adSAlbert Schwarzkopf if (tagLen != operation->info.digestLength) 1614b0104773SPascal Brand res = TEE_ERROR_MAC_INVALID; 1615b0104773SPascal Brand 1616b0104773SPascal Brand acc_dlen += tmp_dlen; 1617b0104773SPascal Brand *destLen = acc_dlen; 1618642a1607SCedric Chaumont 1619b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1620b5816c88SCedric Chaumont 1621642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1622642a1607SCedric Chaumont 1623b5816c88SCedric Chaumont out: 1624b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1625b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER && 1626b5816c88SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 1627b5816c88SCedric Chaumont TEE_Panic(res); 1628b0104773SPascal Brand 1629b0104773SPascal Brand return res; 1630b0104773SPascal Brand } 1631b0104773SPascal Brand 1632b0104773SPascal Brand /* Cryptographic Operations API - Asymmetric Functions */ 1633b0104773SPascal Brand 163412e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation, 16358f07fe6fSJerome Forissier const TEE_Attribute *params, 16368f07fe6fSJerome Forissier uint32_t paramCount, const void *srcData, 163779a3c601SCedric Chaumont uint32_t srcLen, void *destData, 163879a3c601SCedric Chaumont uint32_t *destLen) 1639b0104773SPascal Brand { 16406915bbbbSJens Wiklander TEE_Result res = TEE_SUCCESS; 1641e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 16426915bbbbSJens Wiklander uint64_t dl = 0; 1643b0104773SPascal Brand 16446915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) 1645b0104773SPascal Brand TEE_Panic(0); 16466915bbbbSJens Wiklander 16476915bbbbSJens Wiklander __utee_check_attr_in_annotation(params, paramCount); 16486915bbbbSJens Wiklander __utee_check_inout_annotation(destLen, sizeof(*destLen)); 16496915bbbbSJens Wiklander 165012e66b6fSCedric Chaumont if (!operation->key1) 1651b0104773SPascal Brand TEE_Panic(0); 165212e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 165312e66b6fSCedric Chaumont TEE_Panic(0); 165412e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_ENCRYPT) 1655b0104773SPascal Brand TEE_Panic(0); 1656b0104773SPascal Brand 1657e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1658e86f1266SJens Wiklander dl = *destLen; 16592c028fdeSJerome Forissier res = _utee_asymm_operate(operation->state, ua, paramCount, srcData, 1660e86f1266SJens Wiklander srcLen, destData, &dl); 1661e86f1266SJens Wiklander *destLen = dl; 166212e66b6fSCedric Chaumont 16638844ebfcSPascal Brand if (res != TEE_SUCCESS && 16648844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 16658844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1666b0104773SPascal Brand TEE_Panic(res); 166712e66b6fSCedric Chaumont 1668b0104773SPascal Brand return res; 1669b0104773SPascal Brand } 1670b0104773SPascal Brand 167112e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation, 16728f07fe6fSJerome Forissier const TEE_Attribute *params, 16738f07fe6fSJerome Forissier uint32_t paramCount, const void *srcData, 167479a3c601SCedric Chaumont uint32_t srcLen, void *destData, 167579a3c601SCedric Chaumont uint32_t *destLen) 1676b0104773SPascal Brand { 16776915bbbbSJens Wiklander TEE_Result res = TEE_SUCCESS; 1678e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 16796915bbbbSJens Wiklander uint64_t dl = 0; 1680b0104773SPascal Brand 16816915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) 1682b0104773SPascal Brand TEE_Panic(0); 16836915bbbbSJens Wiklander 16846915bbbbSJens Wiklander __utee_check_attr_in_annotation(params, paramCount); 16856915bbbbSJens Wiklander __utee_check_inout_annotation(destLen, sizeof(*destLen)); 16866915bbbbSJens Wiklander 168712e66b6fSCedric Chaumont if (!operation->key1) 1688b0104773SPascal Brand TEE_Panic(0); 168912e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 169012e66b6fSCedric Chaumont TEE_Panic(0); 169112e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_DECRYPT) 1692b0104773SPascal Brand TEE_Panic(0); 1693b0104773SPascal Brand 1694e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1695e86f1266SJens Wiklander dl = *destLen; 16962c028fdeSJerome Forissier res = _utee_asymm_operate(operation->state, ua, paramCount, srcData, 1697e86f1266SJens Wiklander srcLen, destData, &dl); 1698e86f1266SJens Wiklander *destLen = dl; 169912e66b6fSCedric Chaumont 17008844ebfcSPascal Brand if (res != TEE_SUCCESS && 17018844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 17028844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1703b0104773SPascal Brand TEE_Panic(res); 170412e66b6fSCedric Chaumont 1705b0104773SPascal Brand return res; 1706b0104773SPascal Brand } 1707b0104773SPascal Brand 170812e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation, 17098f07fe6fSJerome Forissier const TEE_Attribute *params, 17108f07fe6fSJerome Forissier uint32_t paramCount, const void *digest, 171179a3c601SCedric Chaumont uint32_t digestLen, void *signature, 171279a3c601SCedric Chaumont uint32_t *signatureLen) 1713b0104773SPascal Brand { 17146915bbbbSJens Wiklander TEE_Result res = TEE_SUCCESS; 1715e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 17166915bbbbSJens Wiklander uint64_t sl = 0; 1717b0104773SPascal Brand 17186915bbbbSJens Wiklander if (operation == TEE_HANDLE_NULL || (!digest && digestLen)) 1719b0104773SPascal Brand TEE_Panic(0); 17206915bbbbSJens Wiklander 17216915bbbbSJens Wiklander __utee_check_attr_in_annotation(params, paramCount); 17226915bbbbSJens Wiklander __utee_check_inout_annotation(signatureLen, sizeof(*signatureLen)); 17236915bbbbSJens Wiklander 172412e66b6fSCedric Chaumont if (!operation->key1) 1725b0104773SPascal Brand TEE_Panic(0); 172612e66b6fSCedric Chaumont if (operation->info.operationClass != 172712e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 172812e66b6fSCedric Chaumont TEE_Panic(0); 172912e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_SIGN) 1730b0104773SPascal Brand TEE_Panic(0); 1731b0104773SPascal Brand 1732e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1733e86f1266SJens Wiklander sl = *signatureLen; 17342c028fdeSJerome Forissier res = _utee_asymm_operate(operation->state, ua, paramCount, digest, 1735e86f1266SJens Wiklander digestLen, signature, &sl); 1736e86f1266SJens Wiklander *signatureLen = sl; 173712e66b6fSCedric Chaumont 1738b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER) 1739b0104773SPascal Brand TEE_Panic(res); 174012e66b6fSCedric Chaumont 1741b0104773SPascal Brand return res; 1742b0104773SPascal Brand } 1743b0104773SPascal Brand 174412e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation, 17458f07fe6fSJerome Forissier const TEE_Attribute *params, 17468f07fe6fSJerome Forissier uint32_t paramCount, const void *digest, 17478f07fe6fSJerome Forissier uint32_t digestLen, 17488f07fe6fSJerome Forissier const void *signature, 174979a3c601SCedric Chaumont uint32_t signatureLen) 1750b0104773SPascal Brand { 1751b0104773SPascal Brand TEE_Result res; 1752e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1753b0104773SPascal Brand 175412e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || 175512e66b6fSCedric Chaumont (digest == NULL && digestLen != 0) || 1756b0104773SPascal Brand (signature == NULL && signatureLen != 0)) 1757b0104773SPascal Brand TEE_Panic(0); 17586915bbbbSJens Wiklander 17596915bbbbSJens Wiklander __utee_check_attr_in_annotation(params, paramCount); 17606915bbbbSJens Wiklander 176112e66b6fSCedric Chaumont if (!operation->key1) 1762b0104773SPascal Brand TEE_Panic(0); 176312e66b6fSCedric Chaumont if (operation->info.operationClass != 176412e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 176512e66b6fSCedric Chaumont TEE_Panic(0); 176612e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_VERIFY) 1767b0104773SPascal Brand TEE_Panic(0); 1768b0104773SPascal Brand 1769e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 17702c028fdeSJerome Forissier res = _utee_asymm_verify(operation->state, ua, paramCount, digest, 177112e66b6fSCedric Chaumont digestLen, signature, signatureLen); 177212e66b6fSCedric Chaumont 1773b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID) 1774b0104773SPascal Brand TEE_Panic(res); 177512e66b6fSCedric Chaumont 1776b0104773SPascal Brand return res; 1777b0104773SPascal Brand } 1778b0104773SPascal Brand 1779b0104773SPascal Brand /* Cryptographic Operations API - Key Derivation Functions */ 1780b0104773SPascal Brand 1781b0104773SPascal Brand void TEE_DeriveKey(TEE_OperationHandle operation, 1782b0104773SPascal Brand const TEE_Attribute *params, uint32_t paramCount, 1783b0104773SPascal Brand TEE_ObjectHandle derivedKey) 1784b0104773SPascal Brand { 1785e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1786*75d6a373SJens Wiklander struct utee_object_info key_info = { }; 1787*75d6a373SJens Wiklander TEE_Result res = TEE_SUCCESS; 1788b0104773SPascal Brand 1789b0104773SPascal Brand if (operation == TEE_HANDLE_NULL || derivedKey == 0) 1790b0104773SPascal Brand TEE_Panic(0); 17916915bbbbSJens Wiklander 17926915bbbbSJens Wiklander __utee_check_attr_in_annotation(params, paramCount); 17936915bbbbSJens Wiklander 17948854d3c6SJerome Forissier if (TEE_ALG_GET_CLASS(operation->info.algorithm) != 17958854d3c6SJerome Forissier TEE_OPERATION_KEY_DERIVATION) 1796b0104773SPascal Brand TEE_Panic(0); 1797b0104773SPascal Brand 1798b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION) 1799b0104773SPascal Brand TEE_Panic(0); 180084fa9467SCedric Chaumont if (!operation->key1) 180184fa9467SCedric Chaumont TEE_Panic(0); 1802b0104773SPascal Brand if (operation->info.mode != TEE_MODE_DERIVE) 1803b0104773SPascal Brand TEE_Panic(0); 1804b0104773SPascal Brand if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0) 1805b0104773SPascal Brand TEE_Panic(0); 1806b0104773SPascal Brand 18072c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info); 1808b0104773SPascal Brand if (res != TEE_SUCCESS) 1809b36311adSJerome Forissier TEE_Panic(res); 1810b0104773SPascal Brand 1811*75d6a373SJens Wiklander if (key_info.obj_type != TEE_TYPE_GENERIC_SECRET) 1812b0104773SPascal Brand TEE_Panic(0); 1813*75d6a373SJens Wiklander if ((key_info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1814b0104773SPascal Brand TEE_Panic(0); 1815b0104773SPascal Brand 1816e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 18172c028fdeSJerome Forissier res = _utee_cryp_derive_key(operation->state, ua, paramCount, 1818e86f1266SJens Wiklander (unsigned long)derivedKey); 1819b0104773SPascal Brand if (res != TEE_SUCCESS) 1820b0104773SPascal Brand TEE_Panic(res); 1821b0104773SPascal Brand } 1822b0104773SPascal Brand 1823b0104773SPascal Brand /* Cryptographic Operations API - Random Number Generation Functions */ 1824b0104773SPascal Brand 182579a3c601SCedric Chaumont void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen) 1826b0104773SPascal Brand { 1827b0104773SPascal Brand TEE_Result res; 1828b0104773SPascal Brand 18292c028fdeSJerome Forissier res = _utee_cryp_random_number_generate(randomBuffer, randomBufferLen); 1830b0104773SPascal Brand if (res != TEE_SUCCESS) 1831b0104773SPascal Brand TEE_Panic(res); 1832b0104773SPascal Brand } 1833433c4257SJens Wiklander 1834433c4257SJens Wiklander int rand(void) 1835433c4257SJens Wiklander { 1836433c4257SJens Wiklander int rc; 1837433c4257SJens Wiklander 1838433c4257SJens Wiklander TEE_GenerateRandom(&rc, sizeof(rc)); 1839433c4257SJens Wiklander 1840433c4257SJens Wiklander /* 1841433c4257SJens Wiklander * RAND_MAX is the larges int, INT_MAX which is all bits but the 1842433c4257SJens Wiklander * highest bit set. 1843433c4257SJens Wiklander */ 1844433c4257SJens Wiklander return rc & RAND_MAX; 1845433c4257SJens Wiklander } 184679170ce0SJerome Forissier 184779170ce0SJerome Forissier TEE_Result TEE_IsAlgorithmSupported(uint32_t alg, uint32_t element) 184879170ce0SJerome Forissier { 184979170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_AES)) { 185079170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_ECB)) { 185179170ce0SJerome Forissier if (alg == TEE_ALG_AES_ECB_NOPAD) 185279170ce0SJerome Forissier goto check_element_none; 185379170ce0SJerome Forissier } 185479170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CBC)) { 185579170ce0SJerome Forissier if (alg == TEE_ALG_AES_CBC_NOPAD) 185679170ce0SJerome Forissier goto check_element_none; 185779170ce0SJerome Forissier } 185879170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CTR)) { 185979170ce0SJerome Forissier if (alg == TEE_ALG_AES_CTR) 186079170ce0SJerome Forissier goto check_element_none; 186179170ce0SJerome Forissier } 186279170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CTS)) { 186379170ce0SJerome Forissier if (alg == TEE_ALG_AES_CTS) 186479170ce0SJerome Forissier goto check_element_none; 186579170ce0SJerome Forissier } 186679170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_XTS)) { 186779170ce0SJerome Forissier if (alg == TEE_ALG_AES_XTS) 186879170ce0SJerome Forissier goto check_element_none; 186979170ce0SJerome Forissier } 187079170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) { 187179170ce0SJerome Forissier if (alg == TEE_ALG_AES_CBC_MAC_NOPAD || 187279170ce0SJerome Forissier alg == TEE_ALG_AES_CBC_MAC_PKCS5) 187379170ce0SJerome Forissier goto check_element_none; 187479170ce0SJerome Forissier } 187579170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CMAC)) { 187679170ce0SJerome Forissier if (alg == TEE_ALG_AES_CMAC) 187779170ce0SJerome Forissier goto check_element_none; 187879170ce0SJerome Forissier } 187979170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CCM)) { 188079170ce0SJerome Forissier if (alg == TEE_ALG_AES_CCM) 188179170ce0SJerome Forissier goto check_element_none; 188279170ce0SJerome Forissier } 188379170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_GCM)) { 188479170ce0SJerome Forissier if (alg == TEE_ALG_AES_GCM) 188579170ce0SJerome Forissier goto check_element_none; 188679170ce0SJerome Forissier } 188779170ce0SJerome Forissier } 188879170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_DES)) { 188979170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_ECB)) { 189079170ce0SJerome Forissier if (alg == TEE_ALG_DES_ECB_NOPAD || 189179170ce0SJerome Forissier alg == TEE_ALG_DES3_ECB_NOPAD) 189279170ce0SJerome Forissier goto check_element_none; 189379170ce0SJerome Forissier } 189479170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CBC)) { 189579170ce0SJerome Forissier if (alg == TEE_ALG_DES_CBC_NOPAD || 189679170ce0SJerome Forissier alg == TEE_ALG_DES3_CBC_NOPAD) 189779170ce0SJerome Forissier goto check_element_none; 189879170ce0SJerome Forissier } 189979170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) { 190079170ce0SJerome Forissier if (alg == TEE_ALG_DES_CBC_MAC_NOPAD || 190179170ce0SJerome Forissier alg == TEE_ALG_DES_CBC_MAC_PKCS5 || 190279170ce0SJerome Forissier alg == TEE_ALG_DES3_CBC_MAC_NOPAD || 190379170ce0SJerome Forissier alg == TEE_ALG_DES3_CBC_MAC_PKCS5) 190479170ce0SJerome Forissier goto check_element_none; 190579170ce0SJerome Forissier } 190679170ce0SJerome Forissier } 190779170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_MD5)) { 190879170ce0SJerome Forissier if (alg == TEE_ALG_MD5) 190979170ce0SJerome Forissier goto check_element_none; 191079170ce0SJerome Forissier } 191179170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 191279170ce0SJerome Forissier if (alg == TEE_ALG_SHA1) 191379170ce0SJerome Forissier goto check_element_none; 191479170ce0SJerome Forissier } 191579170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 191679170ce0SJerome Forissier if (alg == TEE_ALG_SHA224) 191779170ce0SJerome Forissier goto check_element_none; 191879170ce0SJerome Forissier } 191979170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 192079170ce0SJerome Forissier if (alg == TEE_ALG_SHA256) 192179170ce0SJerome Forissier goto check_element_none; 192279170ce0SJerome Forissier } 192379170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA384)) { 192479170ce0SJerome Forissier if (alg == TEE_ALG_SHA384) 192579170ce0SJerome Forissier goto check_element_none; 192679170ce0SJerome Forissier } 192779170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA512)) { 192879170ce0SJerome Forissier if (alg == TEE_ALG_SHA512) 192979170ce0SJerome Forissier goto check_element_none; 193079170ce0SJerome Forissier } 193179170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) { 193279170ce0SJerome Forissier if (alg == TEE_ALG_MD5SHA1) 193379170ce0SJerome Forissier goto check_element_none; 193479170ce0SJerome Forissier } 193579170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_HMAC)) { 193679170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_MD5)) { 193779170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_MD5) 193879170ce0SJerome Forissier goto check_element_none; 193979170ce0SJerome Forissier } 194079170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 194179170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_SHA1) 194279170ce0SJerome Forissier goto check_element_none; 194379170ce0SJerome Forissier } 194479170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 194579170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_SHA224) 194679170ce0SJerome Forissier goto check_element_none; 194779170ce0SJerome Forissier } 194879170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 194979170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_SHA256) 195079170ce0SJerome Forissier goto check_element_none; 195179170ce0SJerome Forissier } 195279170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA384)) { 195379170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_SHA384) 195479170ce0SJerome Forissier goto check_element_none; 195579170ce0SJerome Forissier } 195679170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA512)) { 195779170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_SHA512) 195879170ce0SJerome Forissier goto check_element_none; 195979170ce0SJerome Forissier } 196079170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SM3)) { 196179170ce0SJerome Forissier if (alg == TEE_ALG_HMAC_SM3) 196279170ce0SJerome Forissier goto check_element_none; 196379170ce0SJerome Forissier } 196479170ce0SJerome Forissier } 196579170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SM3)) { 196679170ce0SJerome Forissier if (alg == TEE_ALG_SM3) 196779170ce0SJerome Forissier goto check_element_none; 196879170ce0SJerome Forissier } 196979170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SM4)) { 197079170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_ECB)) { 197179170ce0SJerome Forissier if (alg == TEE_ALG_SM4_ECB_NOPAD) 197279170ce0SJerome Forissier goto check_element_none; 197379170ce0SJerome Forissier } 197479170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CBC)) { 197579170ce0SJerome Forissier if (alg == TEE_ALG_SM4_CBC_NOPAD) 197679170ce0SJerome Forissier goto check_element_none; 197779170ce0SJerome Forissier } 197879170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_CTR)) { 197979170ce0SJerome Forissier if (alg == TEE_ALG_SM4_CTR) 198079170ce0SJerome Forissier goto check_element_none; 198179170ce0SJerome Forissier } 198279170ce0SJerome Forissier } 198379170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_RSA)) { 198479170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_MD5)) { 198579170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5) 198679170ce0SJerome Forissier goto check_element_none; 198779170ce0SJerome Forissier } 198879170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 198979170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA1 || 199079170ce0SJerome Forissier alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1 || 199179170ce0SJerome Forissier alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1) 199279170ce0SJerome Forissier goto check_element_none; 199379170ce0SJerome Forissier } 199479170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) { 199579170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5SHA1) 199679170ce0SJerome Forissier goto check_element_none; 199779170ce0SJerome Forissier } 199879170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 199979170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA224 || 200079170ce0SJerome Forissier alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224 || 200179170ce0SJerome Forissier alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224) 200279170ce0SJerome Forissier goto check_element_none; 200379170ce0SJerome Forissier } 200479170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 200579170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA256 || 200679170ce0SJerome Forissier alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256 || 200779170ce0SJerome Forissier alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256) 200879170ce0SJerome Forissier goto check_element_none; 200979170ce0SJerome Forissier } 201079170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA384)) { 201179170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA384 || 201279170ce0SJerome Forissier alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384 || 201379170ce0SJerome Forissier alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384) 201479170ce0SJerome Forissier goto check_element_none; 201579170ce0SJerome Forissier } 201679170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA512)) { 201779170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA512 || 201879170ce0SJerome Forissier alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512 || 201979170ce0SJerome Forissier alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512) 202079170ce0SJerome Forissier goto check_element_none; 202179170ce0SJerome Forissier } 202279170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_RSASSA_NA1)) { 202379170ce0SJerome Forissier if (alg == TEE_ALG_RSASSA_PKCS1_V1_5) 202479170ce0SJerome Forissier goto check_element_none; 202579170ce0SJerome Forissier } 202679170ce0SJerome Forissier if (alg == TEE_ALG_RSA_NOPAD) 202779170ce0SJerome Forissier goto check_element_none; 202879170ce0SJerome Forissier } 202979170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_DSA)) { 203079170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA1)) { 203179170ce0SJerome Forissier if (alg == TEE_ALG_DSA_SHA1) 203279170ce0SJerome Forissier goto check_element_none; 203379170ce0SJerome Forissier } 203479170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA224)) { 203579170ce0SJerome Forissier if (alg == TEE_ALG_DSA_SHA224) 203679170ce0SJerome Forissier goto check_element_none; 203779170ce0SJerome Forissier } 203879170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SHA256)) { 203979170ce0SJerome Forissier if (alg == TEE_ALG_DSA_SHA256) 204079170ce0SJerome Forissier goto check_element_none; 204179170ce0SJerome Forissier } 204279170ce0SJerome Forissier } 204379170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_DH)) { 204479170ce0SJerome Forissier if (alg == TEE_ALG_DH_DERIVE_SHARED_SECRET) 204579170ce0SJerome Forissier goto check_element_none; 204679170ce0SJerome Forissier } 204779170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_ECC)) { 204879170ce0SJerome Forissier if ((alg == TEE_ALG_ECDH_P192 || alg == TEE_ALG_ECDSA_P192) && 204979170ce0SJerome Forissier element == TEE_ECC_CURVE_NIST_P192) 205079170ce0SJerome Forissier return TEE_SUCCESS; 205179170ce0SJerome Forissier if ((alg == TEE_ALG_ECDH_P224 || alg == TEE_ALG_ECDSA_P224) && 205279170ce0SJerome Forissier element == TEE_ECC_CURVE_NIST_P224) 205379170ce0SJerome Forissier return TEE_SUCCESS; 205479170ce0SJerome Forissier if ((alg == TEE_ALG_ECDH_P256 || alg == TEE_ALG_ECDSA_P256) && 205579170ce0SJerome Forissier element == TEE_ECC_CURVE_NIST_P256) 205679170ce0SJerome Forissier return TEE_SUCCESS; 205779170ce0SJerome Forissier if ((alg == TEE_ALG_ECDH_P384 || alg == TEE_ALG_ECDSA_P384) && 205879170ce0SJerome Forissier element == TEE_ECC_CURVE_NIST_P384) 205979170ce0SJerome Forissier return TEE_SUCCESS; 206079170ce0SJerome Forissier if ((alg == TEE_ALG_ECDH_P521 || alg == TEE_ALG_ECDSA_P521) && 206179170ce0SJerome Forissier element == TEE_ECC_CURVE_NIST_P521) 206279170ce0SJerome Forissier return TEE_SUCCESS; 206379170ce0SJerome Forissier } 206479170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SM2_DSA)) { 206579170ce0SJerome Forissier if (alg == TEE_ALG_SM2_DSA_SM3 && element == TEE_ECC_CURVE_SM2) 206679170ce0SJerome Forissier return TEE_SUCCESS; 206779170ce0SJerome Forissier } 206879170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SM2_KEP)) { 206979170ce0SJerome Forissier if (alg == TEE_ALG_SM2_KEP && element == TEE_ECC_CURVE_SM2) 207079170ce0SJerome Forissier return TEE_SUCCESS; 207179170ce0SJerome Forissier } 207279170ce0SJerome Forissier if (IS_ENABLED(CFG_CRYPTO_SM2_PKE)) { 207379170ce0SJerome Forissier if (alg == TEE_ALG_SM2_PKE && element == TEE_ECC_CURVE_SM2) 207479170ce0SJerome Forissier return TEE_SUCCESS; 207579170ce0SJerome Forissier } 20763f61056dSSohaib ul Hassan if (IS_ENABLED(CFG_CRYPTO_X25519)) { 20773f61056dSSohaib ul Hassan if (alg == TEE_ALG_X25519 && element == TEE_ECC_CURVE_25519) 20783f61056dSSohaib ul Hassan return TEE_SUCCESS; 20793f61056dSSohaib ul Hassan } 2080e1f9cee7SSergiy Kibrik if (IS_ENABLED(CFG_CRYPTO_ED25519)) { 2081e1f9cee7SSergiy Kibrik if (alg == TEE_ALG_ED25519 && element == TEE_ECC_CURVE_25519) 2082e1f9cee7SSergiy Kibrik return TEE_SUCCESS; 2083e1f9cee7SSergiy Kibrik } 208479170ce0SJerome Forissier 208579170ce0SJerome Forissier return TEE_ERROR_NOT_SUPPORTED; 208679170ce0SJerome Forissier check_element_none: 208779170ce0SJerome Forissier if (element == TEE_CRYPTO_ELEMENT_NONE) 208879170ce0SJerome Forissier return TEE_SUCCESS; 208979170ce0SJerome Forissier return TEE_ERROR_NOT_SUPPORTED; 209079170ce0SJerome Forissier } 2091