11bb92983SJerome Forissier // SPDX-License-Identifier: BSD-2-Clause 2b0104773SPascal Brand /* 3b0104773SPascal Brand * Copyright (c) 2014, STMicroelectronics International N.V. 4b0104773SPascal Brand */ 5b0104773SPascal Brand #include <stdlib.h> 6b0104773SPascal Brand #include <string.h> 7b796ebf3SJerome Forissier #include <string_ext.h> 8b0104773SPascal Brand 9b0104773SPascal Brand #include <tee_api.h> 108854d3c6SJerome Forissier #include <tee_api_defines_extensions.h> 11b0104773SPascal Brand #include <tee_internal_api_extensions.h> 12b0104773SPascal Brand #include <utee_syscalls.h> 13b0104773SPascal Brand #include <utee_defines.h> 14fc26c92aSJens Wiklander #include <util.h> 15e86f1266SJens Wiklander #include "tee_api_private.h" 16b0104773SPascal Brand 17b0104773SPascal Brand struct __TEE_OperationHandle { 18b0104773SPascal Brand TEE_OperationInfo info; 19b0104773SPascal Brand TEE_ObjectHandle key1; 20b0104773SPascal Brand TEE_ObjectHandle key2; 21642a1607SCedric Chaumont uint32_t operationState;/* Operation state : INITIAL or ACTIVE */ 22b0104773SPascal Brand uint8_t *buffer; /* buffer to collect complete blocks */ 23b0104773SPascal Brand bool buffer_two_blocks; /* True if two blocks need to be buffered */ 24b0104773SPascal Brand size_t block_size; /* Block size of cipher */ 25b0104773SPascal Brand size_t buffer_offs; /* Offset in buffer */ 26b0104773SPascal Brand uint32_t state; /* Handle to state in TEE Core */ 27b0104773SPascal Brand uint32_t ae_tag_len; /* 28b0104773SPascal Brand * tag_len in bytes for AE operation else unused 29b0104773SPascal Brand */ 30b0104773SPascal Brand }; 31b0104773SPascal Brand 32b0104773SPascal Brand /* Cryptographic Operations API - Generic Operation Functions */ 33b0104773SPascal Brand 34b0104773SPascal Brand TEE_Result TEE_AllocateOperation(TEE_OperationHandle *operation, 35b0104773SPascal Brand uint32_t algorithm, uint32_t mode, 36b0104773SPascal Brand uint32_t maxKeySize) 37b0104773SPascal Brand { 38b0104773SPascal Brand TEE_Result res; 39b0104773SPascal Brand TEE_OperationHandle op = TEE_HANDLE_NULL; 40b0104773SPascal Brand uint32_t handle_state = 0; 41b0104773SPascal Brand size_t block_size = 1; 42b0104773SPascal Brand uint32_t req_key_usage; 43b0104773SPascal Brand bool with_private_key = false; 44b0104773SPascal Brand bool buffer_two_blocks = false; 45b0104773SPascal Brand 469b52c538SCedric Chaumont if (!operation) 47b0104773SPascal Brand TEE_Panic(0); 48b0104773SPascal Brand 49b0104773SPascal Brand if (algorithm == TEE_ALG_AES_XTS) 50b0104773SPascal Brand handle_state = TEE_HANDLE_FLAG_EXPECT_TWO_KEYS; 51b0104773SPascal Brand 52218d9055SCedric Chaumont /* Check algorithm max key size */ 53218d9055SCedric Chaumont switch (algorithm) { 54218d9055SCedric Chaumont case TEE_ALG_DSA_SHA1: 55218d9055SCedric Chaumont if (maxKeySize < 512) 56218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 57218d9055SCedric Chaumont if (maxKeySize > 1024) 58218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 59218d9055SCedric Chaumont if (maxKeySize % 64 != 0) 60218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 61218d9055SCedric Chaumont break; 62218d9055SCedric Chaumont 63218d9055SCedric Chaumont case TEE_ALG_DSA_SHA224: 64218d9055SCedric Chaumont if (maxKeySize != 2048) 65218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 66218d9055SCedric Chaumont break; 67218d9055SCedric Chaumont 68218d9055SCedric Chaumont case TEE_ALG_DSA_SHA256: 69218d9055SCedric Chaumont if (maxKeySize != 2048 && maxKeySize != 3072) 70218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 71218d9055SCedric Chaumont break; 72218d9055SCedric Chaumont 731220586eSCedric Chaumont case TEE_ALG_ECDSA_P192: 741220586eSCedric Chaumont case TEE_ALG_ECDH_P192: 751220586eSCedric Chaumont if (maxKeySize != 192) 761220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 771220586eSCedric Chaumont break; 781220586eSCedric Chaumont 791220586eSCedric Chaumont case TEE_ALG_ECDSA_P224: 801220586eSCedric Chaumont case TEE_ALG_ECDH_P224: 811220586eSCedric Chaumont if (maxKeySize != 224) 821220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 831220586eSCedric Chaumont break; 841220586eSCedric Chaumont 851220586eSCedric Chaumont case TEE_ALG_ECDSA_P256: 861220586eSCedric Chaumont case TEE_ALG_ECDH_P256: 8791fc6bd8SJerome Forissier case TEE_ALG_SM2_PKE: 88*0f151943SJerome Forissier case TEE_ALG_SM2_DSA_SM3: 891220586eSCedric Chaumont if (maxKeySize != 256) 901220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 911220586eSCedric Chaumont break; 921220586eSCedric Chaumont 931220586eSCedric Chaumont case TEE_ALG_ECDSA_P384: 941220586eSCedric Chaumont case TEE_ALG_ECDH_P384: 951220586eSCedric Chaumont if (maxKeySize != 384) 961220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 971220586eSCedric Chaumont break; 981220586eSCedric Chaumont 991220586eSCedric Chaumont case TEE_ALG_ECDSA_P521: 1001220586eSCedric Chaumont case TEE_ALG_ECDH_P521: 1011220586eSCedric Chaumont if (maxKeySize != 521) 1021220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 1031220586eSCedric Chaumont break; 1041220586eSCedric Chaumont 105218d9055SCedric Chaumont default: 106218d9055SCedric Chaumont break; 107218d9055SCedric Chaumont } 108218d9055SCedric Chaumont 109218d9055SCedric Chaumont /* Check algorithm mode */ 110b0104773SPascal Brand switch (algorithm) { 111b0104773SPascal Brand case TEE_ALG_AES_CTS: 112b0104773SPascal Brand case TEE_ALG_AES_XTS: 113b0104773SPascal Brand buffer_two_blocks = true; 1144bd53c54SJerome Forissier /* FALLTHROUGH */ 1154bd53c54SJerome Forissier case TEE_ALG_AES_ECB_NOPAD: 116b0104773SPascal Brand case TEE_ALG_AES_CBC_NOPAD: 117b0104773SPascal Brand case TEE_ALG_AES_CCM: 118b0104773SPascal Brand case TEE_ALG_DES_ECB_NOPAD: 119b0104773SPascal Brand case TEE_ALG_DES_CBC_NOPAD: 120b0104773SPascal Brand case TEE_ALG_DES3_ECB_NOPAD: 121b0104773SPascal Brand case TEE_ALG_DES3_CBC_NOPAD: 122ade6f848SJerome Forissier case TEE_ALG_SM4_ECB_NOPAD: 123ade6f848SJerome Forissier case TEE_ALG_SM4_CBC_NOPAD: 124ade6f848SJerome Forissier case TEE_ALG_SM4_CTR: 125b0104773SPascal Brand if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES) 126b0104773SPascal Brand block_size = TEE_AES_BLOCK_SIZE; 127ade6f848SJerome Forissier else if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_SM4) 128ade6f848SJerome Forissier block_size = TEE_SM4_BLOCK_SIZE; 129b0104773SPascal Brand else 130b0104773SPascal Brand block_size = TEE_DES_BLOCK_SIZE; 131afc0c182SBogdan Liulko /* FALLTHROUGH */ 13257aabac5SBogdan Liulko case TEE_ALG_AES_CTR: 133afc0c182SBogdan Liulko case TEE_ALG_AES_GCM: 134b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) 135b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT; 136b0104773SPascal Brand else if (mode == TEE_MODE_DECRYPT) 137b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT; 138b0104773SPascal Brand else 139b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 140b0104773SPascal Brand break; 141b0104773SPascal Brand 1426a2e0a9fSGabor Szekely #if defined(CFG_CRYPTO_RSASSA_NA1) 1436a2e0a9fSGabor Szekely case TEE_ALG_RSASSA_PKCS1_V1_5: 1446a2e0a9fSGabor Szekely #endif 145b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_MD5: 146b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1: 147b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224: 148b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256: 149b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384: 150b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512: 151b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1: 152b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224: 153b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256: 154b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384: 155b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512: 156b0104773SPascal Brand case TEE_ALG_DSA_SHA1: 157218d9055SCedric Chaumont case TEE_ALG_DSA_SHA224: 158218d9055SCedric Chaumont case TEE_ALG_DSA_SHA256: 1591220586eSCedric Chaumont case TEE_ALG_ECDSA_P192: 1601220586eSCedric Chaumont case TEE_ALG_ECDSA_P224: 1611220586eSCedric Chaumont case TEE_ALG_ECDSA_P256: 1621220586eSCedric Chaumont case TEE_ALG_ECDSA_P384: 1631220586eSCedric Chaumont case TEE_ALG_ECDSA_P521: 164*0f151943SJerome Forissier case TEE_ALG_SM2_DSA_SM3: 165b0104773SPascal Brand if (mode == TEE_MODE_SIGN) { 166b0104773SPascal Brand with_private_key = true; 167b0104773SPascal Brand req_key_usage = TEE_USAGE_SIGN; 168b0104773SPascal Brand } else if (mode == TEE_MODE_VERIFY) { 169b0104773SPascal Brand req_key_usage = TEE_USAGE_VERIFY; 170b0104773SPascal Brand } else { 171b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 172b0104773SPascal Brand } 173b0104773SPascal Brand break; 174b0104773SPascal Brand 175b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_V1_5: 176b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1: 177b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224: 178b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256: 179b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384: 180b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512: 18191fc6bd8SJerome Forissier case TEE_ALG_SM2_PKE: 182b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) { 183b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT; 184b0104773SPascal Brand } else if (mode == TEE_MODE_DECRYPT) { 185b0104773SPascal Brand with_private_key = true; 186b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT; 187b0104773SPascal Brand } else { 188b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 189b0104773SPascal Brand } 190b0104773SPascal Brand break; 191b0104773SPascal Brand 192b0104773SPascal Brand case TEE_ALG_RSA_NOPAD: 193b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) { 194b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY; 195b0104773SPascal Brand } else if (mode == TEE_MODE_DECRYPT) { 196b0104773SPascal Brand with_private_key = true; 197b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT | TEE_USAGE_SIGN; 198b0104773SPascal Brand } else { 199b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 200b0104773SPascal Brand } 201b0104773SPascal Brand break; 202b0104773SPascal Brand 203b0104773SPascal Brand case TEE_ALG_DH_DERIVE_SHARED_SECRET: 2041220586eSCedric Chaumont case TEE_ALG_ECDH_P192: 2051220586eSCedric Chaumont case TEE_ALG_ECDH_P224: 2061220586eSCedric Chaumont case TEE_ALG_ECDH_P256: 2071220586eSCedric Chaumont case TEE_ALG_ECDH_P384: 2081220586eSCedric Chaumont case TEE_ALG_ECDH_P521: 209cdb198a7SJerome Forissier case TEE_ALG_HKDF_MD5_DERIVE_KEY: 210cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA1_DERIVE_KEY: 211cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA224_DERIVE_KEY: 212cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA256_DERIVE_KEY: 213cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA384_DERIVE_KEY: 214cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA512_DERIVE_KEY: 2158854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA1_DERIVE_KEY: 2168854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA224_DERIVE_KEY: 2178854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA256_DERIVE_KEY: 2188854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA384_DERIVE_KEY: 2198854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA512_DERIVE_KEY: 2200f2293b7SJerome Forissier case TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY: 221b0104773SPascal Brand if (mode != TEE_MODE_DERIVE) 222b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 223b0104773SPascal Brand with_private_key = true; 224b0104773SPascal Brand req_key_usage = TEE_USAGE_DERIVE; 225b0104773SPascal Brand break; 226b0104773SPascal Brand 227b0104773SPascal Brand case TEE_ALG_MD5: 228b0104773SPascal Brand case TEE_ALG_SHA1: 229b0104773SPascal Brand case TEE_ALG_SHA224: 230b0104773SPascal Brand case TEE_ALG_SHA256: 231b0104773SPascal Brand case TEE_ALG_SHA384: 232b0104773SPascal Brand case TEE_ALG_SHA512: 23347645577SJerome Forissier case TEE_ALG_SM3: 234b0104773SPascal Brand if (mode != TEE_MODE_DIGEST) 235b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 23605304565SCedric Chaumont /* v1.1: flags always set for digest operations */ 237b0104773SPascal Brand handle_state |= TEE_HANDLE_FLAG_KEY_SET; 238b0104773SPascal Brand req_key_usage = 0; 239b0104773SPascal Brand break; 240b0104773SPascal Brand 241b0104773SPascal Brand case TEE_ALG_DES_CBC_MAC_NOPAD: 242b0104773SPascal Brand case TEE_ALG_AES_CBC_MAC_NOPAD: 243b0104773SPascal Brand case TEE_ALG_AES_CBC_MAC_PKCS5: 244b0104773SPascal Brand case TEE_ALG_AES_CMAC: 245b0104773SPascal Brand case TEE_ALG_DES_CBC_MAC_PKCS5: 246b0104773SPascal Brand case TEE_ALG_DES3_CBC_MAC_NOPAD: 247b0104773SPascal Brand case TEE_ALG_DES3_CBC_MAC_PKCS5: 248b0104773SPascal Brand case TEE_ALG_HMAC_MD5: 249b0104773SPascal Brand case TEE_ALG_HMAC_SHA1: 250b0104773SPascal Brand case TEE_ALG_HMAC_SHA224: 251b0104773SPascal Brand case TEE_ALG_HMAC_SHA256: 252b0104773SPascal Brand case TEE_ALG_HMAC_SHA384: 253b0104773SPascal Brand case TEE_ALG_HMAC_SHA512: 25447645577SJerome Forissier case TEE_ALG_HMAC_SM3: 255b0104773SPascal Brand if (mode != TEE_MODE_MAC) 256b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 257b0104773SPascal Brand req_key_usage = TEE_USAGE_MAC; 258b0104773SPascal Brand break; 259b0104773SPascal Brand 260b0104773SPascal Brand default: 261b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 262b0104773SPascal Brand } 263b0104773SPascal Brand 264b66f219bSJens Wiklander op = TEE_Malloc(sizeof(*op), TEE_MALLOC_FILL_ZERO); 2659b52c538SCedric Chaumont if (!op) 266b0104773SPascal Brand return TEE_ERROR_OUT_OF_MEMORY; 267b0104773SPascal Brand 268b0104773SPascal Brand op->info.algorithm = algorithm; 269b0104773SPascal Brand op->info.operationClass = TEE_ALG_GET_CLASS(algorithm); 2706a2e0a9fSGabor Szekely #ifdef CFG_CRYPTO_RSASSA_NA1 2716a2e0a9fSGabor Szekely if (algorithm == TEE_ALG_RSASSA_PKCS1_V1_5) 2726a2e0a9fSGabor Szekely op->info.operationClass = TEE_OPERATION_ASYMMETRIC_SIGNATURE; 2736a2e0a9fSGabor Szekely #endif 274b0104773SPascal Brand op->info.mode = mode; 275b0104773SPascal Brand op->info.maxKeySize = maxKeySize; 276b0104773SPascal Brand op->info.requiredKeyUsage = req_key_usage; 277b0104773SPascal Brand op->info.handleState = handle_state; 278b0104773SPascal Brand 279b0104773SPascal Brand if (block_size > 1) { 280b0104773SPascal Brand size_t buffer_size = block_size; 281b0104773SPascal Brand 282b0104773SPascal Brand if (buffer_two_blocks) 283b0104773SPascal Brand buffer_size *= 2; 284b0104773SPascal Brand 2859b52c538SCedric Chaumont op->buffer = TEE_Malloc(buffer_size, 2869b52c538SCedric Chaumont TEE_USER_MEM_HINT_NO_FILL_ZERO); 287b0104773SPascal Brand if (op->buffer == NULL) { 288b0104773SPascal Brand res = TEE_ERROR_OUT_OF_MEMORY; 289b66f219bSJens Wiklander goto out; 290b0104773SPascal Brand } 291b0104773SPascal Brand } 292b0104773SPascal Brand op->block_size = block_size; 293b0104773SPascal Brand op->buffer_two_blocks = buffer_two_blocks; 294b0104773SPascal Brand 295b0104773SPascal Brand if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) { 296b0104773SPascal Brand uint32_t mks = maxKeySize; 297b0104773SPascal Brand TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm, 298b0104773SPascal Brand with_private_key); 299b0104773SPascal Brand 300b0104773SPascal Brand /* 301b0104773SPascal Brand * If two keys are expected the max key size is the sum of 302b0104773SPascal Brand * the size of both keys. 303b0104773SPascal Brand */ 304b0104773SPascal Brand if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) 305b0104773SPascal Brand mks /= 2; 306b0104773SPascal Brand 307b0104773SPascal Brand res = TEE_AllocateTransientObject(key_type, mks, &op->key1); 308b0104773SPascal Brand if (res != TEE_SUCCESS) 309b66f219bSJens Wiklander goto out; 310b0104773SPascal Brand 31105304565SCedric Chaumont if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) { 3129b52c538SCedric Chaumont res = TEE_AllocateTransientObject(key_type, mks, 313b0104773SPascal Brand &op->key2); 314b0104773SPascal Brand if (res != TEE_SUCCESS) 315b66f219bSJens Wiklander goto out; 316b0104773SPascal Brand } 317b0104773SPascal Brand } 318b0104773SPascal Brand 319e86f1266SJens Wiklander res = utee_cryp_state_alloc(algorithm, mode, (unsigned long)op->key1, 320e86f1266SJens Wiklander (unsigned long)op->key2, &op->state); 321b66f219bSJens Wiklander if (res != TEE_SUCCESS) 322b66f219bSJens Wiklander goto out; 323b0104773SPascal Brand 32405304565SCedric Chaumont /* 32505304565SCedric Chaumont * Initialize digest operations 32605304565SCedric Chaumont * Other multi-stage operations initialized w/ TEE_xxxInit functions 32705304565SCedric Chaumont * Non-applicable on asymmetric operations 32805304565SCedric Chaumont */ 32905304565SCedric Chaumont if (TEE_ALG_GET_CLASS(algorithm) == TEE_OPERATION_DIGEST) { 33005304565SCedric Chaumont res = utee_hash_init(op->state, NULL, 0); 33105304565SCedric Chaumont if (res != TEE_SUCCESS) 332b66f219bSJens Wiklander goto out; 33305304565SCedric Chaumont /* v1.1: flags always set for digest operations */ 33405304565SCedric Chaumont op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 33505304565SCedric Chaumont } 33605304565SCedric Chaumont 337642a1607SCedric Chaumont op->operationState = TEE_OPERATION_STATE_INITIAL; 338642a1607SCedric Chaumont 339b0104773SPascal Brand *operation = op; 340b0104773SPascal Brand 341b66f219bSJens Wiklander out: 342b66f219bSJens Wiklander if (res != TEE_SUCCESS) { 343b66f219bSJens Wiklander if (res != TEE_ERROR_OUT_OF_MEMORY && 3449b52c538SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 345b36311adSJerome Forissier TEE_Panic(res); 346b66f219bSJens Wiklander if (op) { 347b66f219bSJens Wiklander if (op->state) { 348b66f219bSJens Wiklander TEE_FreeOperation(op); 349b66f219bSJens Wiklander } else { 350b66f219bSJens Wiklander TEE_Free(op->buffer); 351b66f219bSJens Wiklander TEE_FreeTransientObject(op->key1); 352b66f219bSJens Wiklander TEE_FreeTransientObject(op->key2); 353b66f219bSJens Wiklander TEE_Free(op); 354b66f219bSJens Wiklander } 355b66f219bSJens Wiklander } 356b66f219bSJens Wiklander } 357b66f219bSJens Wiklander 358b0104773SPascal Brand return res; 359b0104773SPascal Brand } 360b0104773SPascal Brand 361b0104773SPascal Brand void TEE_FreeOperation(TEE_OperationHandle operation) 362b0104773SPascal Brand { 363e889e80bSCedric Chaumont TEE_Result res; 364e889e80bSCedric Chaumont 365e889e80bSCedric Chaumont if (operation == TEE_HANDLE_NULL) 366e889e80bSCedric Chaumont TEE_Panic(0); 367e889e80bSCedric Chaumont 368b0104773SPascal Brand /* 369b0104773SPascal Brand * Note that keys should not be freed here, since they are 370b0104773SPascal Brand * claimed by the operation they will be freed by 371b0104773SPascal Brand * utee_cryp_state_free(). 372b0104773SPascal Brand */ 373e889e80bSCedric Chaumont res = utee_cryp_state_free(operation->state); 374e889e80bSCedric Chaumont if (res != TEE_SUCCESS) 375b36311adSJerome Forissier TEE_Panic(res); 376e889e80bSCedric Chaumont 377b0104773SPascal Brand TEE_Free(operation->buffer); 378b0104773SPascal Brand TEE_Free(operation); 379b0104773SPascal Brand } 380b0104773SPascal Brand 381b0104773SPascal Brand void TEE_GetOperationInfo(TEE_OperationHandle operation, 382b0104773SPascal Brand TEE_OperationInfo *operationInfo) 383b0104773SPascal Brand { 384b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 385b0104773SPascal Brand TEE_Panic(0); 386b0104773SPascal Brand 38705304565SCedric Chaumont if (!operationInfo) 388b0104773SPascal Brand TEE_Panic(0); 389b0104773SPascal Brand 390b0104773SPascal Brand *operationInfo = operation->info; 391b0104773SPascal Brand } 392b0104773SPascal Brand 39305304565SCedric Chaumont TEE_Result TEE_GetOperationInfoMultiple(TEE_OperationHandle operation, 39405304565SCedric Chaumont TEE_OperationInfoMultiple *operationInfoMultiple, 39505304565SCedric Chaumont uint32_t *operationSize) 39605304565SCedric Chaumont { 39705304565SCedric Chaumont TEE_Result res = TEE_SUCCESS; 39805304565SCedric Chaumont TEE_ObjectInfo key_info1; 39905304565SCedric Chaumont TEE_ObjectInfo key_info2; 40005304565SCedric Chaumont uint32_t num_of_keys; 40105304565SCedric Chaumont size_t n; 40205304565SCedric Chaumont 40305304565SCedric Chaumont if (operation == TEE_HANDLE_NULL) { 40405304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 40505304565SCedric Chaumont goto out; 40605304565SCedric Chaumont } 40705304565SCedric Chaumont 40805304565SCedric Chaumont if (!operationInfoMultiple) { 40905304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 41005304565SCedric Chaumont goto out; 41105304565SCedric Chaumont } 41205304565SCedric Chaumont 41305304565SCedric Chaumont if (!operationSize) { 41405304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 41505304565SCedric Chaumont goto out; 41605304565SCedric Chaumont } 41705304565SCedric Chaumont 41805304565SCedric Chaumont num_of_keys = (*operationSize-sizeof(TEE_OperationInfoMultiple))/ 41905304565SCedric Chaumont sizeof(TEE_OperationInfoKey); 42005304565SCedric Chaumont 42105304565SCedric Chaumont if (num_of_keys > 2) { 42205304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 42305304565SCedric Chaumont goto out; 42405304565SCedric Chaumont } 42505304565SCedric Chaumont 42605304565SCedric Chaumont /* Two keys flag (TEE_ALG_AES_XTS only) */ 42705304565SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 42805304565SCedric Chaumont 0 && 42905304565SCedric Chaumont (num_of_keys != 2)) { 43005304565SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 43105304565SCedric Chaumont goto out; 43205304565SCedric Chaumont } 43305304565SCedric Chaumont 43405304565SCedric Chaumont /* Clear */ 43505304565SCedric Chaumont for (n = 0; n < num_of_keys; n++) { 43605304565SCedric Chaumont operationInfoMultiple->keyInformation[n].keySize = 0; 43705304565SCedric Chaumont operationInfoMultiple->keyInformation[n].requiredKeyUsage = 0; 43805304565SCedric Chaumont } 43905304565SCedric Chaumont 44005304565SCedric Chaumont if (num_of_keys == 2) { 44105304565SCedric Chaumont res = TEE_GetObjectInfo1(operation->key2, &key_info2); 44205304565SCedric Chaumont /* Key2 is not a valid handle */ 44305304565SCedric Chaumont if (res != TEE_SUCCESS) 44405304565SCedric Chaumont goto out; 44505304565SCedric Chaumont 44605304565SCedric Chaumont operationInfoMultiple->keyInformation[1].keySize = 44705304565SCedric Chaumont key_info2.keySize; 44805304565SCedric Chaumont operationInfoMultiple->keyInformation[1].requiredKeyUsage = 44905304565SCedric Chaumont operation->info.requiredKeyUsage; 45005304565SCedric Chaumont } 45105304565SCedric Chaumont 45205304565SCedric Chaumont if (num_of_keys >= 1) { 45305304565SCedric Chaumont res = TEE_GetObjectInfo1(operation->key1, &key_info1); 45405304565SCedric Chaumont /* Key1 is not a valid handle */ 45505304565SCedric Chaumont if (res != TEE_SUCCESS) { 45605304565SCedric Chaumont if (num_of_keys == 2) { 45705304565SCedric Chaumont operationInfoMultiple->keyInformation[1]. 45805304565SCedric Chaumont keySize = 0; 45905304565SCedric Chaumont operationInfoMultiple->keyInformation[1]. 46005304565SCedric Chaumont requiredKeyUsage = 0; 46105304565SCedric Chaumont } 46205304565SCedric Chaumont goto out; 46305304565SCedric Chaumont } 46405304565SCedric Chaumont 46505304565SCedric Chaumont operationInfoMultiple->keyInformation[0].keySize = 46605304565SCedric Chaumont key_info1.keySize; 46705304565SCedric Chaumont operationInfoMultiple->keyInformation[0].requiredKeyUsage = 46805304565SCedric Chaumont operation->info.requiredKeyUsage; 46905304565SCedric Chaumont } 47005304565SCedric Chaumont 47105304565SCedric Chaumont /* No key */ 47205304565SCedric Chaumont operationInfoMultiple->algorithm = operation->info.algorithm; 47305304565SCedric Chaumont operationInfoMultiple->operationClass = operation->info.operationClass; 47405304565SCedric Chaumont operationInfoMultiple->mode = operation->info.mode; 47505304565SCedric Chaumont operationInfoMultiple->digestLength = operation->info.digestLength; 47605304565SCedric Chaumont operationInfoMultiple->maxKeySize = operation->info.maxKeySize; 47705304565SCedric Chaumont operationInfoMultiple->handleState = operation->info.handleState; 47805304565SCedric Chaumont operationInfoMultiple->operationState = operation->operationState; 47905304565SCedric Chaumont operationInfoMultiple->numberOfKeys = num_of_keys; 48005304565SCedric Chaumont 48105304565SCedric Chaumont out: 48205304565SCedric Chaumont if (res != TEE_SUCCESS && 48305304565SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 484b36311adSJerome Forissier TEE_Panic(res); 48505304565SCedric Chaumont 48605304565SCedric Chaumont return res; 48705304565SCedric Chaumont } 48805304565SCedric Chaumont 489b0104773SPascal Brand void TEE_ResetOperation(TEE_OperationHandle operation) 490b0104773SPascal Brand { 491b0104773SPascal Brand TEE_Result res; 492b0104773SPascal Brand 493b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 494b0104773SPascal Brand TEE_Panic(0); 495bf80076aSCedric Chaumont 496642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET)) 497bf80076aSCedric Chaumont TEE_Panic(0); 498bf80076aSCedric Chaumont 499642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 500642a1607SCedric Chaumont 501b0104773SPascal Brand if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 502b0104773SPascal Brand res = utee_hash_init(operation->state, NULL, 0); 503b0104773SPascal Brand if (res != TEE_SUCCESS) 504b0104773SPascal Brand TEE_Panic(res); 50505304565SCedric Chaumont operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 50605304565SCedric Chaumont } else { 507b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 508b0104773SPascal Brand } 50905304565SCedric Chaumont } 510b0104773SPascal Brand 511b0104773SPascal Brand TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation, 512b0104773SPascal Brand TEE_ObjectHandle key) 513b0104773SPascal Brand { 5147583c59eSCedric Chaumont TEE_Result res; 515b0104773SPascal Brand uint32_t key_size = 0; 516b0104773SPascal Brand TEE_ObjectInfo key_info; 517b0104773SPascal Brand 518a57c1e2eSCedric Chaumont if (operation == TEE_HANDLE_NULL) { 519a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 520a57c1e2eSCedric Chaumont goto out; 521a57c1e2eSCedric Chaumont } 522a57c1e2eSCedric Chaumont 523642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 524642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 525642a1607SCedric Chaumont goto out; 526642a1607SCedric Chaumont } 527642a1607SCedric Chaumont 528a57c1e2eSCedric Chaumont if (key == TEE_HANDLE_NULL) { 529a57c1e2eSCedric Chaumont /* Operation key cleared */ 530a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key1); 531a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 532a57c1e2eSCedric Chaumont goto out; 533a57c1e2eSCedric Chaumont } 534a57c1e2eSCedric Chaumont 535a57c1e2eSCedric Chaumont /* No key for digest operation */ 536a57c1e2eSCedric Chaumont if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 537a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 538a57c1e2eSCedric Chaumont goto out; 539a57c1e2eSCedric Chaumont } 540a57c1e2eSCedric Chaumont 541a57c1e2eSCedric Chaumont /* Two keys flag not expected (TEE_ALG_AES_XTS excluded) */ 542a57c1e2eSCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 543a57c1e2eSCedric Chaumont 0) { 544a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 545a57c1e2eSCedric Chaumont goto out; 546a57c1e2eSCedric Chaumont } 547a57c1e2eSCedric Chaumont 5487583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key, &key_info); 549a57c1e2eSCedric Chaumont /* Key is not a valid handle */ 5507583c59eSCedric Chaumont if (res != TEE_SUCCESS) 551a57c1e2eSCedric Chaumont goto out; 5527583c59eSCedric Chaumont 553b0104773SPascal Brand /* Supplied key has to meet required usage */ 554b0104773SPascal Brand if ((key_info.objectUsage & operation->info.requiredKeyUsage) != 555b0104773SPascal Brand operation->info.requiredKeyUsage) { 556a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 557a57c1e2eSCedric Chaumont goto out; 558b0104773SPascal Brand } 559b0104773SPascal Brand 560a57c1e2eSCedric Chaumont if (operation->info.maxKeySize < key_info.keySize) { 561a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 562a57c1e2eSCedric Chaumont goto out; 563a57c1e2eSCedric Chaumont } 564b0104773SPascal Brand 5657583c59eSCedric Chaumont key_size = key_info.keySize; 566b0104773SPascal Brand 567b0104773SPascal Brand TEE_ResetTransientObject(operation->key1); 568b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 569b0104773SPascal Brand 5707583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key1, key); 5717583c59eSCedric Chaumont if (res != TEE_SUCCESS) 572a57c1e2eSCedric Chaumont goto out; 5737583c59eSCedric Chaumont 574b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 575b0104773SPascal Brand 576b0104773SPascal Brand operation->info.keySize = key_size; 577b0104773SPascal Brand 5787583c59eSCedric Chaumont out: 579a57c1e2eSCedric Chaumont if (res != TEE_SUCCESS && 580a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 581a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 582b36311adSJerome Forissier TEE_Panic(res); 583a57c1e2eSCedric Chaumont 584a57c1e2eSCedric Chaumont return res; 585b0104773SPascal Brand } 586b0104773SPascal Brand 587b0104773SPascal Brand TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation, 588b0104773SPascal Brand TEE_ObjectHandle key1, TEE_ObjectHandle key2) 589b0104773SPascal Brand { 5907583c59eSCedric Chaumont TEE_Result res; 591b0104773SPascal Brand uint32_t key_size = 0; 592b0104773SPascal Brand TEE_ObjectInfo key_info1; 593b0104773SPascal Brand TEE_ObjectInfo key_info2; 594b0104773SPascal Brand 595a57c1e2eSCedric Chaumont if (operation == TEE_HANDLE_NULL) { 596a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 597a57c1e2eSCedric Chaumont goto out; 598a57c1e2eSCedric Chaumont } 599a57c1e2eSCedric Chaumont 600642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 601642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 602642a1607SCedric Chaumont goto out; 603642a1607SCedric Chaumont } 604642a1607SCedric Chaumont 605a57c1e2eSCedric Chaumont /* 606a57c1e2eSCedric Chaumont * Key1/Key2 and/or are not initialized and 607a57c1e2eSCedric Chaumont * Either both keys are NULL or both are not NULL 608a57c1e2eSCedric Chaumont */ 609a57c1e2eSCedric Chaumont if (key1 == TEE_HANDLE_NULL || key2 == TEE_HANDLE_NULL) { 610a57c1e2eSCedric Chaumont /* Clear operation key1 (if needed) */ 611a57c1e2eSCedric Chaumont if (key1 == TEE_HANDLE_NULL) 612a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key1); 613a57c1e2eSCedric Chaumont /* Clear operation key2 (if needed) */ 614a57c1e2eSCedric Chaumont if (key2 == TEE_HANDLE_NULL) 615a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key2); 616a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 617a57c1e2eSCedric Chaumont goto out; 618a57c1e2eSCedric Chaumont } 619a57c1e2eSCedric Chaumont 620a57c1e2eSCedric Chaumont /* No key for digest operation */ 621a57c1e2eSCedric Chaumont if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 622a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 623a57c1e2eSCedric Chaumont goto out; 624a57c1e2eSCedric Chaumont } 625a57c1e2eSCedric Chaumont 626a57c1e2eSCedric Chaumont /* Two keys flag expected (TEE_ALG_AES_XTS only) */ 627a57c1e2eSCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 628a57c1e2eSCedric Chaumont 0) { 629a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 630a57c1e2eSCedric Chaumont goto out; 631a57c1e2eSCedric Chaumont } 632a57c1e2eSCedric Chaumont 6337583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key1, &key_info1); 634a57c1e2eSCedric Chaumont /* Key1 is not a valid handle */ 6357583c59eSCedric Chaumont if (res != TEE_SUCCESS) 636a57c1e2eSCedric Chaumont goto out; 6377583c59eSCedric Chaumont 638b0104773SPascal Brand /* Supplied key has to meet required usage */ 639b0104773SPascal Brand if ((key_info1.objectUsage & operation->info. 640b0104773SPascal Brand requiredKeyUsage) != operation->info.requiredKeyUsage) { 641a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 642a57c1e2eSCedric Chaumont goto out; 643b0104773SPascal Brand } 644b0104773SPascal Brand 6457583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key2, &key_info2); 646a57c1e2eSCedric Chaumont /* Key2 is not a valid handle */ 6477583c59eSCedric Chaumont if (res != TEE_SUCCESS) { 6487583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) 6497583c59eSCedric Chaumont res = TEE_ERROR_CORRUPT_OBJECT_2; 650a57c1e2eSCedric Chaumont goto out; 6517583c59eSCedric Chaumont } 6527583c59eSCedric Chaumont 653b0104773SPascal Brand /* Supplied key has to meet required usage */ 654b0104773SPascal Brand if ((key_info2.objectUsage & operation->info. 655b0104773SPascal Brand requiredKeyUsage) != operation->info.requiredKeyUsage) { 656a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 657a57c1e2eSCedric Chaumont goto out; 658b0104773SPascal Brand } 659b0104773SPascal Brand 660b0104773SPascal Brand /* 661b0104773SPascal Brand * AES-XTS (the only multi key algorithm supported, requires the 662b0104773SPascal Brand * keys to be of equal size. 663b0104773SPascal Brand */ 664b0104773SPascal Brand if (operation->info.algorithm == TEE_ALG_AES_XTS && 665a57c1e2eSCedric Chaumont key_info1.keySize != key_info2.keySize) { 666a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 667a57c1e2eSCedric Chaumont goto out; 668b0104773SPascal Brand 669a57c1e2eSCedric Chaumont } 670a57c1e2eSCedric Chaumont 671a57c1e2eSCedric Chaumont if (operation->info.maxKeySize < key_info1.keySize) { 672a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 673a57c1e2eSCedric Chaumont goto out; 674a57c1e2eSCedric Chaumont } 675b0104773SPascal Brand 676b0104773SPascal Brand /* 677b0104773SPascal Brand * Odd that only the size of one key should be reported while 678b0104773SPascal Brand * size of two key are used when allocating the operation. 679b0104773SPascal Brand */ 6807583c59eSCedric Chaumont key_size = key_info1.keySize; 681b0104773SPascal Brand 682b0104773SPascal Brand TEE_ResetTransientObject(operation->key1); 683b0104773SPascal Brand TEE_ResetTransientObject(operation->key2); 684b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 685b0104773SPascal Brand 6867583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key1, key1); 6877583c59eSCedric Chaumont if (res != TEE_SUCCESS) 688a57c1e2eSCedric Chaumont goto out; 6897583c59eSCedric Chaumont 6907583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key2, key2); 6917583c59eSCedric Chaumont if (res != TEE_SUCCESS) { 6927583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) 6937583c59eSCedric Chaumont res = TEE_ERROR_CORRUPT_OBJECT_2; 694a57c1e2eSCedric Chaumont goto out; 6957583c59eSCedric Chaumont } 6967583c59eSCedric Chaumont 697b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 698b0104773SPascal Brand 699b0104773SPascal Brand operation->info.keySize = key_size; 700b0104773SPascal Brand 7017583c59eSCedric Chaumont out: 702a57c1e2eSCedric Chaumont if (res != TEE_SUCCESS && 703a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 704a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT_2 && 705a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE && 706a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE_2) 707b36311adSJerome Forissier TEE_Panic(res); 708a57c1e2eSCedric Chaumont 709a57c1e2eSCedric Chaumont return res; 710b0104773SPascal Brand } 711b0104773SPascal Brand 712b0104773SPascal Brand void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op) 713b0104773SPascal Brand { 714b0104773SPascal Brand TEE_Result res; 715b0104773SPascal Brand 716b0104773SPascal Brand if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL) 717b0104773SPascal Brand TEE_Panic(0); 718b0104773SPascal Brand if (dst_op->info.algorithm != src_op->info.algorithm) 719b0104773SPascal Brand TEE_Panic(0); 720b0104773SPascal Brand if (src_op->info.operationClass != TEE_OPERATION_DIGEST) { 721b0104773SPascal Brand TEE_ObjectHandle key1 = TEE_HANDLE_NULL; 722b0104773SPascal Brand TEE_ObjectHandle key2 = TEE_HANDLE_NULL; 723b0104773SPascal Brand 724b0104773SPascal Brand if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) { 725b0104773SPascal Brand key1 = src_op->key1; 726b0104773SPascal Brand key2 = src_op->key2; 727b0104773SPascal Brand } 728b0104773SPascal Brand 729b0104773SPascal Brand if ((src_op->info.handleState & 730b0104773SPascal Brand TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) { 731b0104773SPascal Brand TEE_SetOperationKey(dst_op, key1); 732b0104773SPascal Brand } else { 733b0104773SPascal Brand TEE_SetOperationKey2(dst_op, key1, key2); 734b0104773SPascal Brand } 735b0104773SPascal Brand } 736b0104773SPascal Brand dst_op->info.handleState = src_op->info.handleState; 737b0104773SPascal Brand dst_op->info.keySize = src_op->info.keySize; 738642a1607SCedric Chaumont dst_op->operationState = src_op->operationState; 739b0104773SPascal Brand 740b0104773SPascal Brand if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks || 741b0104773SPascal Brand dst_op->block_size != src_op->block_size) 742b0104773SPascal Brand TEE_Panic(0); 743b0104773SPascal Brand 744b0104773SPascal Brand if (dst_op->buffer != NULL) { 745b0104773SPascal Brand if (src_op->buffer == NULL) 746b0104773SPascal Brand TEE_Panic(0); 747b0104773SPascal Brand 748b0104773SPascal Brand memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs); 749b0104773SPascal Brand dst_op->buffer_offs = src_op->buffer_offs; 750b0104773SPascal Brand } else if (src_op->buffer != NULL) { 751b0104773SPascal Brand TEE_Panic(0); 752b0104773SPascal Brand } 753b0104773SPascal Brand 754b0104773SPascal Brand res = utee_cryp_state_copy(dst_op->state, src_op->state); 755b0104773SPascal Brand if (res != TEE_SUCCESS) 756b0104773SPascal Brand TEE_Panic(res); 757b0104773SPascal Brand } 758b0104773SPascal Brand 759b0104773SPascal Brand /* Cryptographic Operations API - Message Digest Functions */ 760b0104773SPascal Brand 7618f07fe6fSJerome Forissier static void init_hash_operation(TEE_OperationHandle operation, const void *IV, 7626d15db08SJerome Forissier uint32_t IVLen) 7636d15db08SJerome Forissier { 7646d15db08SJerome Forissier TEE_Result res; 7656d15db08SJerome Forissier 7666d15db08SJerome Forissier /* 7676d15db08SJerome Forissier * Note : IV and IVLen are never used in current implementation 7686d15db08SJerome Forissier * This is why coherent values of IV and IVLen are not checked 7696d15db08SJerome Forissier */ 7706d15db08SJerome Forissier res = utee_hash_init(operation->state, IV, IVLen); 7716d15db08SJerome Forissier if (res != TEE_SUCCESS) 7726d15db08SJerome Forissier TEE_Panic(res); 7736d15db08SJerome Forissier operation->buffer_offs = 0; 7746d15db08SJerome Forissier operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 7756d15db08SJerome Forissier } 7766d15db08SJerome Forissier 777b0104773SPascal Brand void TEE_DigestUpdate(TEE_OperationHandle operation, 7788f07fe6fSJerome Forissier const void *chunk, uint32_t chunkSize) 779b0104773SPascal Brand { 78073d6c3baSJoakim Bech TEE_Result res = TEE_ERROR_GENERIC; 781b0104773SPascal Brand 78273d6c3baSJoakim Bech if (operation == TEE_HANDLE_NULL || 78373d6c3baSJoakim Bech operation->info.operationClass != TEE_OPERATION_DIGEST) 784b0104773SPascal Brand TEE_Panic(0); 78573d6c3baSJoakim Bech 786642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 787642a1607SCedric Chaumont 788b0104773SPascal Brand res = utee_hash_update(operation->state, chunk, chunkSize); 789b0104773SPascal Brand if (res != TEE_SUCCESS) 790b0104773SPascal Brand TEE_Panic(res); 791b0104773SPascal Brand } 792b0104773SPascal Brand 7938f07fe6fSJerome Forissier TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void *chunk, 79479a3c601SCedric Chaumont uint32_t chunkLen, void *hash, uint32_t *hashLen) 795b0104773SPascal Brand { 79687c2f6b6SCedric Chaumont TEE_Result res; 797e86f1266SJens Wiklander uint64_t hl; 79887c2f6b6SCedric Chaumont 79987c2f6b6SCedric Chaumont if ((operation == TEE_HANDLE_NULL) || 80087c2f6b6SCedric Chaumont (!chunk && chunkLen) || 80187c2f6b6SCedric Chaumont !hash || 80287c2f6b6SCedric Chaumont !hashLen || 80387c2f6b6SCedric Chaumont (operation->info.operationClass != TEE_OPERATION_DIGEST)) { 80487c2f6b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 80587c2f6b6SCedric Chaumont goto out; 80687c2f6b6SCedric Chaumont } 80787c2f6b6SCedric Chaumont 808e86f1266SJens Wiklander hl = *hashLen; 809e86f1266SJens Wiklander res = utee_hash_final(operation->state, chunk, chunkLen, hash, &hl); 810e86f1266SJens Wiklander *hashLen = hl; 8116d15db08SJerome Forissier if (res != TEE_SUCCESS) 8126d15db08SJerome Forissier goto out; 8136d15db08SJerome Forissier 8146d15db08SJerome Forissier /* Reset operation state */ 8156d15db08SJerome Forissier init_hash_operation(operation, NULL, 0); 81687c2f6b6SCedric Chaumont 817642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 818642a1607SCedric Chaumont 81987c2f6b6SCedric Chaumont out: 82087c2f6b6SCedric Chaumont if (res != TEE_SUCCESS && 82187c2f6b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 822b36311adSJerome Forissier TEE_Panic(res); 82373d6c3baSJoakim Bech 82487c2f6b6SCedric Chaumont return res; 825b0104773SPascal Brand } 826b0104773SPascal Brand 827b0104773SPascal Brand /* Cryptographic Operations API - Symmetric Cipher Functions */ 828b0104773SPascal Brand 8298f07fe6fSJerome Forissier void TEE_CipherInit(TEE_OperationHandle operation, const void *IV, 8308f07fe6fSJerome Forissier uint32_t IVLen) 831b0104773SPascal Brand { 832b0104773SPascal Brand TEE_Result res; 833b0104773SPascal Brand 834b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 835b0104773SPascal Brand TEE_Panic(0); 836642a1607SCedric Chaumont 837b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_CIPHER) 838b0104773SPascal Brand TEE_Panic(0); 839642a1607SCedric Chaumont 840642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 841642a1607SCedric Chaumont !(operation->key1)) 842642a1607SCedric Chaumont TEE_Panic(0); 843642a1607SCedric Chaumont 844642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 845642a1607SCedric Chaumont TEE_ResetOperation(operation); 846642a1607SCedric Chaumont 847642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 848642a1607SCedric Chaumont 849b0104773SPascal Brand res = utee_cipher_init(operation->state, IV, IVLen); 850b0104773SPascal Brand if (res != TEE_SUCCESS) 851b0104773SPascal Brand TEE_Panic(res); 852642a1607SCedric Chaumont 853b0104773SPascal Brand operation->buffer_offs = 0; 854b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 855b0104773SPascal Brand } 856b0104773SPascal Brand 857b0104773SPascal Brand static TEE_Result tee_buffer_update( 858b0104773SPascal Brand TEE_OperationHandle op, 859e86f1266SJens Wiklander TEE_Result(*update_func)(unsigned long state, const void *src, 860e86f1266SJens Wiklander size_t slen, void *dst, uint64_t *dlen), 861b0104773SPascal Brand const void *src_data, size_t src_len, 862e86f1266SJens Wiklander void *dest_data, uint64_t *dest_len) 863b0104773SPascal Brand { 864b0104773SPascal Brand TEE_Result res; 865b0104773SPascal Brand const uint8_t *src = src_data; 866b0104773SPascal Brand size_t slen = src_len; 867b0104773SPascal Brand uint8_t *dst = dest_data; 868b0104773SPascal Brand size_t dlen = *dest_len; 869b0104773SPascal Brand size_t acc_dlen = 0; 870e86f1266SJens Wiklander uint64_t tmp_dlen; 871b0104773SPascal Brand size_t l; 872b0104773SPascal Brand size_t buffer_size; 873d3588802SPascal Brand size_t buffer_left; 874b0104773SPascal Brand 875090268f5SJens Wiklander if (!src) { 876090268f5SJens Wiklander if (slen) 877090268f5SJens Wiklander TEE_Panic(0); 878090268f5SJens Wiklander goto out; 879090268f5SJens Wiklander } 880090268f5SJens Wiklander 881d3588802SPascal Brand if (op->buffer_two_blocks) { 882b0104773SPascal Brand buffer_size = op->block_size * 2; 883d3588802SPascal Brand buffer_left = 1; 884d3588802SPascal Brand } else { 885b0104773SPascal Brand buffer_size = op->block_size; 886d3588802SPascal Brand buffer_left = 0; 887d3588802SPascal Brand } 888b0104773SPascal Brand 889b0104773SPascal Brand if (op->buffer_offs > 0) { 890b0104773SPascal Brand /* Fill up complete block */ 891b0104773SPascal Brand if (op->buffer_offs < op->block_size) 892b0104773SPascal Brand l = MIN(slen, op->block_size - op->buffer_offs); 893b0104773SPascal Brand else 894b0104773SPascal Brand l = MIN(slen, buffer_size - op->buffer_offs); 895b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, l); 896b0104773SPascal Brand op->buffer_offs += l; 897b0104773SPascal Brand src += l; 898b0104773SPascal Brand slen -= l; 899b0104773SPascal Brand if ((op->buffer_offs % op->block_size) != 0) 900b0104773SPascal Brand goto out; /* Nothing left to do */ 901b0104773SPascal Brand } 902b0104773SPascal Brand 903b0104773SPascal Brand /* If we can feed from buffer */ 904d3588802SPascal Brand if ((op->buffer_offs > 0) && 905d3588802SPascal Brand ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) { 9062ff3fdbbSPascal Brand l = ROUNDUP(op->buffer_offs + slen - buffer_size, 907b0104773SPascal Brand op->block_size); 908b0104773SPascal Brand l = MIN(op->buffer_offs, l); 909b0104773SPascal Brand tmp_dlen = dlen; 910b0104773SPascal Brand res = update_func(op->state, op->buffer, l, dst, &tmp_dlen); 911b0104773SPascal Brand if (res != TEE_SUCCESS) 912b0104773SPascal Brand TEE_Panic(res); 913b0104773SPascal Brand dst += tmp_dlen; 914b0104773SPascal Brand dlen -= tmp_dlen; 915b0104773SPascal Brand acc_dlen += tmp_dlen; 916b0104773SPascal Brand op->buffer_offs -= l; 917b0104773SPascal Brand if (op->buffer_offs > 0) { 918b0104773SPascal Brand /* 919b0104773SPascal Brand * Slen is small enough to be contained in rest buffer. 920b0104773SPascal Brand */ 921b0104773SPascal Brand memcpy(op->buffer, op->buffer + l, buffer_size - l); 922b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 923b0104773SPascal Brand op->buffer_offs += slen; 924b0104773SPascal Brand goto out; /* Nothing left to do */ 925b0104773SPascal Brand } 926b0104773SPascal Brand } 927b0104773SPascal Brand 928d3588802SPascal Brand if (slen >= (buffer_size + buffer_left)) { 929b0104773SPascal Brand /* Buffer is empty, feed as much as possible from src */ 930bf7a587fSJerome Forissier if (op->info.algorithm == TEE_ALG_AES_CTS) 931b1ecda78SJerome Forissier l = ROUNDUP(slen - buffer_size, op->block_size); 932bf7a587fSJerome Forissier else 933bf7a587fSJerome Forissier l = ROUNDUP(slen - buffer_size + 1, op->block_size); 934b0104773SPascal Brand 935b0104773SPascal Brand tmp_dlen = dlen; 936b0104773SPascal Brand res = update_func(op->state, src, l, dst, &tmp_dlen); 937b0104773SPascal Brand if (res != TEE_SUCCESS) 938b0104773SPascal Brand TEE_Panic(res); 939b0104773SPascal Brand src += l; 940b0104773SPascal Brand slen -= l; 941b0104773SPascal Brand dst += tmp_dlen; 942b0104773SPascal Brand dlen -= tmp_dlen; 943b0104773SPascal Brand acc_dlen += tmp_dlen; 944b0104773SPascal Brand } 945b0104773SPascal Brand 946b0104773SPascal Brand /* Slen is small enough to be contained in buffer. */ 947b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 948b0104773SPascal Brand op->buffer_offs += slen; 949b0104773SPascal Brand 950b0104773SPascal Brand out: 951b0104773SPascal Brand *dest_len = acc_dlen; 952b0104773SPascal Brand return TEE_SUCCESS; 953b0104773SPascal Brand } 954b0104773SPascal Brand 9558f07fe6fSJerome Forissier TEE_Result TEE_CipherUpdate(TEE_OperationHandle operation, const void *srcData, 95679a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 957b0104773SPascal Brand { 958dea1f2b6SCedric Chaumont TEE_Result res; 959b0104773SPascal Brand size_t req_dlen; 960e86f1266SJens Wiklander uint64_t dl; 961b0104773SPascal Brand 962642a1607SCedric Chaumont if (operation == TEE_HANDLE_NULL || 963dea1f2b6SCedric Chaumont (srcData == NULL && srcLen != 0) || 964dea1f2b6SCedric Chaumont destLen == NULL || 965dea1f2b6SCedric Chaumont (destData == NULL && *destLen != 0)) { 966dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 967dea1f2b6SCedric Chaumont goto out; 968dea1f2b6SCedric Chaumont } 969dea1f2b6SCedric Chaumont 970642a1607SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 971dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 972dea1f2b6SCedric Chaumont goto out; 973dea1f2b6SCedric Chaumont } 974dea1f2b6SCedric Chaumont 975642a1607SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 976642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 977642a1607SCedric Chaumont goto out; 978642a1607SCedric Chaumont } 979642a1607SCedric Chaumont 980642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 981dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 982dea1f2b6SCedric Chaumont goto out; 983dea1f2b6SCedric Chaumont } 984b0104773SPascal Brand 985e32c5ddfSJerome Forissier if (!srcData && !srcLen) { 986090268f5SJens Wiklander *destLen = 0; 987e32c5ddfSJerome Forissier res = TEE_SUCCESS; 988e32c5ddfSJerome Forissier goto out; 989e32c5ddfSJerome Forissier } 990e32c5ddfSJerome Forissier 991b0104773SPascal Brand /* Calculate required dlen */ 99257aabac5SBogdan Liulko if (operation->block_size > 1) { 99357aabac5SBogdan Liulko req_dlen = ((operation->buffer_offs + srcLen) / 99457aabac5SBogdan Liulko operation->block_size) * operation->block_size; 99557aabac5SBogdan Liulko } else { 99657aabac5SBogdan Liulko req_dlen = srcLen; 99757aabac5SBogdan Liulko } 998642a1607SCedric Chaumont if (operation->buffer_two_blocks) { 999642a1607SCedric Chaumont if (req_dlen > operation->block_size * 2) 1000642a1607SCedric Chaumont req_dlen -= operation->block_size * 2; 1001b0104773SPascal Brand else 1002b0104773SPascal Brand req_dlen = 0; 1003b0104773SPascal Brand } 1004b0104773SPascal Brand /* 1005b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1006b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1007b0104773SPascal Brand * can't restore sync with this API. 1008b0104773SPascal Brand */ 1009b0104773SPascal Brand if (*destLen < req_dlen) { 1010b0104773SPascal Brand *destLen = req_dlen; 1011dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1012dea1f2b6SCedric Chaumont goto out; 1013b0104773SPascal Brand } 1014b0104773SPascal Brand 1015e86f1266SJens Wiklander dl = *destLen; 101657aabac5SBogdan Liulko if (operation->block_size > 1) { 101757aabac5SBogdan Liulko res = tee_buffer_update(operation, utee_cipher_update, srcData, 101857aabac5SBogdan Liulko srcLen, destData, &dl); 101957aabac5SBogdan Liulko } else { 102057aabac5SBogdan Liulko if (srcLen > 0) { 102157aabac5SBogdan Liulko res = utee_cipher_update(operation->state, srcData, 102257aabac5SBogdan Liulko srcLen, destData, &dl); 102357aabac5SBogdan Liulko } else { 102457aabac5SBogdan Liulko res = TEE_SUCCESS; 102557aabac5SBogdan Liulko dl = 0; 102657aabac5SBogdan Liulko } 102757aabac5SBogdan Liulko } 1028e86f1266SJens Wiklander *destLen = dl; 1029b0104773SPascal Brand 1030dea1f2b6SCedric Chaumont out: 1031dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 1032dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1033b36311adSJerome Forissier TEE_Panic(res); 1034dea1f2b6SCedric Chaumont 1035dea1f2b6SCedric Chaumont return res; 1036b0104773SPascal Brand } 1037b0104773SPascal Brand 1038642a1607SCedric Chaumont TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation, 10398f07fe6fSJerome Forissier const void *srcData, uint32_t srcLen, 10408f07fe6fSJerome Forissier void *destData, uint32_t *destLen) 1041b0104773SPascal Brand { 1042b0104773SPascal Brand TEE_Result res; 1043b0104773SPascal Brand uint8_t *dst = destData; 1044b0104773SPascal Brand size_t acc_dlen = 0; 1045e86f1266SJens Wiklander uint64_t tmp_dlen; 1046b0104773SPascal Brand size_t req_dlen; 1047b0104773SPascal Brand 1048642a1607SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1049dea1f2b6SCedric Chaumont (srcData == NULL && srcLen != 0) || 1050dea1f2b6SCedric Chaumont destLen == NULL || 1051dea1f2b6SCedric Chaumont (destData == NULL && *destLen != 0)) { 1052dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1053dea1f2b6SCedric Chaumont goto out; 1054dea1f2b6SCedric Chaumont } 1055dea1f2b6SCedric Chaumont 1056642a1607SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 1057dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1058dea1f2b6SCedric Chaumont goto out; 1059dea1f2b6SCedric Chaumont } 1060dea1f2b6SCedric Chaumont 1061642a1607SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1062642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1063642a1607SCedric Chaumont goto out; 1064642a1607SCedric Chaumont } 1065642a1607SCedric Chaumont 1066642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1067dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1068dea1f2b6SCedric Chaumont goto out; 1069dea1f2b6SCedric Chaumont } 1070b0104773SPascal Brand 1071b0104773SPascal Brand /* 1072b0104773SPascal Brand * Check that the final block doesn't require padding for those 1073b0104773SPascal Brand * algorithms that requires client to supply padding. 1074b0104773SPascal Brand */ 1075642a1607SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD || 1076642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD || 1077642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD || 1078642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD || 1079642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || 1080ade6f848SJerome Forissier operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD || 1081ade6f848SJerome Forissier operation->info.algorithm == TEE_ALG_SM4_ECB_NOPAD || 1082ade6f848SJerome Forissier operation->info.algorithm == TEE_ALG_SM4_CBC_NOPAD) { 1083642a1607SCedric Chaumont if (((operation->buffer_offs + srcLen) % operation->block_size) 1084642a1607SCedric Chaumont != 0) { 1085dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1086dea1f2b6SCedric Chaumont goto out; 1087dea1f2b6SCedric Chaumont } 1088b0104773SPascal Brand } 1089b0104773SPascal Brand 1090b0104773SPascal Brand /* 1091b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1092b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1093b0104773SPascal Brand * can't restore sync with this API. 1094b0104773SPascal Brand */ 109557aabac5SBogdan Liulko if (operation->block_size > 1) { 1096642a1607SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 109757aabac5SBogdan Liulko } else { 109857aabac5SBogdan Liulko req_dlen = srcLen; 109957aabac5SBogdan Liulko } 1100b0104773SPascal Brand if (*destLen < req_dlen) { 1101b0104773SPascal Brand *destLen = req_dlen; 1102dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1103dea1f2b6SCedric Chaumont goto out; 1104b0104773SPascal Brand } 1105b0104773SPascal Brand 1106b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 110757aabac5SBogdan Liulko if (operation->block_size > 1) { 110857aabac5SBogdan Liulko res = tee_buffer_update(operation, utee_cipher_update, 110957aabac5SBogdan Liulko srcData, srcLen, dst, &tmp_dlen); 1110dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS) 1111dea1f2b6SCedric Chaumont goto out; 1112dea1f2b6SCedric Chaumont 1113b0104773SPascal Brand dst += tmp_dlen; 1114b0104773SPascal Brand acc_dlen += tmp_dlen; 1115b0104773SPascal Brand 1116b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1117642a1607SCedric Chaumont res = utee_cipher_final(operation->state, operation->buffer, 1118642a1607SCedric Chaumont operation->buffer_offs, dst, &tmp_dlen); 111957aabac5SBogdan Liulko } else { 112057aabac5SBogdan Liulko res = utee_cipher_final(operation->state, srcData, 112157aabac5SBogdan Liulko srcLen, dst, &tmp_dlen); 112257aabac5SBogdan Liulko } 1123b0104773SPascal Brand if (res != TEE_SUCCESS) 1124dea1f2b6SCedric Chaumont goto out; 1125dea1f2b6SCedric Chaumont 1126b0104773SPascal Brand acc_dlen += tmp_dlen; 1127b0104773SPascal Brand *destLen = acc_dlen; 1128dea1f2b6SCedric Chaumont 1129642a1607SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1130642a1607SCedric Chaumont 1131642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1132642a1607SCedric Chaumont 1133dea1f2b6SCedric Chaumont out: 1134dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 1135dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1136b36311adSJerome Forissier TEE_Panic(res); 1137dea1f2b6SCedric Chaumont 1138dea1f2b6SCedric Chaumont return res; 1139b0104773SPascal Brand } 1140b0104773SPascal Brand 1141b0104773SPascal Brand /* Cryptographic Operations API - MAC Functions */ 1142b0104773SPascal Brand 11438f07fe6fSJerome Forissier void TEE_MACInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen) 1144b0104773SPascal Brand { 1145b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 1146b0104773SPascal Brand TEE_Panic(0); 1147642a1607SCedric Chaumont 1148b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_MAC) 1149b0104773SPascal Brand TEE_Panic(0); 1150642a1607SCedric Chaumont 1151642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 1152642a1607SCedric Chaumont !(operation->key1)) 1153642a1607SCedric Chaumont TEE_Panic(0); 1154642a1607SCedric Chaumont 1155642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 1156642a1607SCedric Chaumont TEE_ResetOperation(operation); 1157642a1607SCedric Chaumont 1158642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1159642a1607SCedric Chaumont 11606d15db08SJerome Forissier init_hash_operation(operation, IV, IVLen); 1161b0104773SPascal Brand } 1162b0104773SPascal Brand 11638f07fe6fSJerome Forissier void TEE_MACUpdate(TEE_OperationHandle operation, const void *chunk, 116428e0efc6SCedric Chaumont uint32_t chunkSize) 1165b0104773SPascal Brand { 1166b0104773SPascal Brand TEE_Result res; 1167b0104773SPascal Brand 116828e0efc6SCedric Chaumont if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0)) 1169b0104773SPascal Brand TEE_Panic(0); 1170642a1607SCedric Chaumont 117128e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) 1172b0104773SPascal Brand TEE_Panic(0); 1173642a1607SCedric Chaumont 117428e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1175b0104773SPascal Brand TEE_Panic(0); 1176b0104773SPascal Brand 1177642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) 1178642a1607SCedric Chaumont TEE_Panic(0); 1179642a1607SCedric Chaumont 118028e0efc6SCedric Chaumont res = utee_hash_update(operation->state, chunk, chunkSize); 1181b0104773SPascal Brand if (res != TEE_SUCCESS) 1182b0104773SPascal Brand TEE_Panic(res); 1183b0104773SPascal Brand } 1184b0104773SPascal Brand 118528e0efc6SCedric Chaumont TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation, 11868f07fe6fSJerome Forissier const void *message, uint32_t messageLen, 118779a3c601SCedric Chaumont void *mac, uint32_t *macLen) 1188b0104773SPascal Brand { 1189b0104773SPascal Brand TEE_Result res; 1190e86f1266SJens Wiklander uint64_t ml; 1191b0104773SPascal Brand 119228e0efc6SCedric Chaumont if (operation == TEE_HANDLE_NULL || 119328e0efc6SCedric Chaumont (message == NULL && messageLen != 0) || 119428e0efc6SCedric Chaumont mac == NULL || 119528e0efc6SCedric Chaumont macLen == NULL) { 119628e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 119728e0efc6SCedric Chaumont goto out; 119828e0efc6SCedric Chaumont } 1199b0104773SPascal Brand 120028e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 120128e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 120228e0efc6SCedric Chaumont goto out; 120328e0efc6SCedric Chaumont } 120428e0efc6SCedric Chaumont 120528e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 120628e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 120728e0efc6SCedric Chaumont goto out; 120828e0efc6SCedric Chaumont } 120928e0efc6SCedric Chaumont 1210642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1211642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1212642a1607SCedric Chaumont goto out; 1213642a1607SCedric Chaumont } 1214642a1607SCedric Chaumont 1215e86f1266SJens Wiklander ml = *macLen; 1216e86f1266SJens Wiklander res = utee_hash_final(operation->state, message, messageLen, mac, &ml); 1217e86f1266SJens Wiklander *macLen = ml; 121828e0efc6SCedric Chaumont if (res != TEE_SUCCESS) 121928e0efc6SCedric Chaumont goto out; 122028e0efc6SCedric Chaumont 122128e0efc6SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 122228e0efc6SCedric Chaumont 1223642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1224642a1607SCedric Chaumont 122528e0efc6SCedric Chaumont out: 122628e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 122728e0efc6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 122828e0efc6SCedric Chaumont TEE_Panic(res); 122928e0efc6SCedric Chaumont 1230b0104773SPascal Brand return res; 1231b0104773SPascal Brand } 1232b0104773SPascal Brand 1233b0104773SPascal Brand TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation, 12348f07fe6fSJerome Forissier const void *message, uint32_t messageLen, 12358f07fe6fSJerome Forissier const void *mac, uint32_t macLen) 1236b0104773SPascal Brand { 1237b0104773SPascal Brand TEE_Result res; 1238b0104773SPascal Brand uint8_t computed_mac[TEE_MAX_HASH_SIZE]; 12397f74c64aSPascal Brand uint32_t computed_mac_size = TEE_MAX_HASH_SIZE; 1240b0104773SPascal Brand 124128e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 124228e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 124328e0efc6SCedric Chaumont goto out; 124428e0efc6SCedric Chaumont } 124528e0efc6SCedric Chaumont 124628e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 124728e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 124828e0efc6SCedric Chaumont goto out; 124928e0efc6SCedric Chaumont } 125028e0efc6SCedric Chaumont 1251642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1252642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1253642a1607SCedric Chaumont goto out; 1254642a1607SCedric Chaumont } 1255642a1607SCedric Chaumont 1256b0104773SPascal Brand res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac, 1257b0104773SPascal Brand &computed_mac_size); 1258b0104773SPascal Brand if (res != TEE_SUCCESS) 125928e0efc6SCedric Chaumont goto out; 126028e0efc6SCedric Chaumont 126128e0efc6SCedric Chaumont if (computed_mac_size != macLen) { 126228e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 126328e0efc6SCedric Chaumont goto out; 126428e0efc6SCedric Chaumont } 126528e0efc6SCedric Chaumont 126648e10604SJerome Forissier if (consttime_memcmp(mac, computed_mac, computed_mac_size) != 0) { 126728e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 126828e0efc6SCedric Chaumont goto out; 126928e0efc6SCedric Chaumont } 127028e0efc6SCedric Chaumont 1271642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1272642a1607SCedric Chaumont 127328e0efc6SCedric Chaumont out: 127428e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 127528e0efc6SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 127628e0efc6SCedric Chaumont TEE_Panic(res); 127728e0efc6SCedric Chaumont 1278b0104773SPascal Brand return res; 1279b0104773SPascal Brand } 1280b0104773SPascal Brand 1281b0104773SPascal Brand /* Cryptographic Operations API - Authenticated Encryption Functions */ 1282b0104773SPascal Brand 12838f07fe6fSJerome Forissier TEE_Result TEE_AEInit(TEE_OperationHandle operation, const void *nonce, 128479a3c601SCedric Chaumont uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen, 1285b0104773SPascal Brand uint32_t payloadLen) 1286b0104773SPascal Brand { 1287b0104773SPascal Brand TEE_Result res; 1288b0104773SPascal Brand 1289b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || nonce == NULL) { 1290b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1291b5816c88SCedric Chaumont goto out; 1292b5816c88SCedric Chaumont } 1293b5816c88SCedric Chaumont 1294b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1295b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1296b5816c88SCedric Chaumont goto out; 1297b5816c88SCedric Chaumont } 1298b0104773SPascal Brand 1299642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 1300642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1301642a1607SCedric Chaumont goto out; 1302642a1607SCedric Chaumont } 1303642a1607SCedric Chaumont 1304b0104773SPascal Brand /* 1305b0104773SPascal Brand * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core 1306b0104773SPascal Brand * in the implementation. But AES-GCM spec doesn't specify the tag len 1307b0104773SPascal Brand * according to the same principle so we have to check here instead to 1308b0104773SPascal Brand * be GP compliant. 1309b0104773SPascal Brand */ 1310b5816c88SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_GCM) { 1311b0104773SPascal Brand /* 1312b0104773SPascal Brand * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96 1313b0104773SPascal Brand */ 1314b5816c88SCedric Chaumont if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) { 1315b5816c88SCedric Chaumont res = TEE_ERROR_NOT_SUPPORTED; 1316b5816c88SCedric Chaumont goto out; 1317b5816c88SCedric Chaumont } 1318b0104773SPascal Brand } 1319b0104773SPascal Brand 1320b5816c88SCedric Chaumont res = utee_authenc_init(operation->state, nonce, nonceLen, 1321b5816c88SCedric Chaumont tagLen / 8, AADLen, payloadLen); 1322b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1323b5816c88SCedric Chaumont goto out; 1324b5816c88SCedric Chaumont 1325b5816c88SCedric Chaumont operation->ae_tag_len = tagLen / 8; 1326b5816c88SCedric Chaumont operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 1327b5816c88SCedric Chaumont 1328b5816c88SCedric Chaumont out: 1329b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1330b5816c88SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 1331b0104773SPascal Brand TEE_Panic(res); 1332b5816c88SCedric Chaumont 1333b0104773SPascal Brand return res; 1334b0104773SPascal Brand } 1335b0104773SPascal Brand 13368f07fe6fSJerome Forissier void TEE_AEUpdateAAD(TEE_OperationHandle operation, const void *AADdata, 133779a3c601SCedric Chaumont uint32_t AADdataLen) 1338b0104773SPascal Brand { 1339b0104773SPascal Brand TEE_Result res; 1340b0104773SPascal Brand 1341b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1342b5816c88SCedric Chaumont (AADdata == NULL && AADdataLen != 0)) 1343b0104773SPascal Brand TEE_Panic(0); 1344642a1607SCedric Chaumont 1345b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) 1346b0104773SPascal Brand TEE_Panic(0); 1347642a1607SCedric Chaumont 1348b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1349b0104773SPascal Brand TEE_Panic(0); 1350b0104773SPascal Brand 1351b5816c88SCedric Chaumont res = utee_authenc_update_aad(operation->state, AADdata, AADdataLen); 1352642a1607SCedric Chaumont 1353642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1354642a1607SCedric Chaumont 1355b0104773SPascal Brand if (res != TEE_SUCCESS) 1356b0104773SPascal Brand TEE_Panic(res); 1357b0104773SPascal Brand } 1358b0104773SPascal Brand 13598f07fe6fSJerome Forissier TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, const void *srcData, 136079a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 1361b0104773SPascal Brand { 1362b5816c88SCedric Chaumont TEE_Result res; 1363b0104773SPascal Brand size_t req_dlen; 1364e86f1266SJens Wiklander uint64_t dl; 1365b0104773SPascal Brand 1366b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1367b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1368b5816c88SCedric Chaumont destLen == NULL || 1369b5816c88SCedric Chaumont (destData == NULL && *destLen != 0)) { 1370b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1371b5816c88SCedric Chaumont goto out; 1372b5816c88SCedric Chaumont } 1373b5816c88SCedric Chaumont 1374b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1375b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1376b5816c88SCedric Chaumont goto out; 1377b5816c88SCedric Chaumont } 1378b5816c88SCedric Chaumont 1379b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1380b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1381b5816c88SCedric Chaumont goto out; 1382b5816c88SCedric Chaumont } 1383b0104773SPascal Brand 1384827308b8SJerome Forissier if (!srcData && !srcLen) { 1385090268f5SJens Wiklander *destLen = 0; 1386827308b8SJerome Forissier res = TEE_SUCCESS; 1387827308b8SJerome Forissier goto out; 1388827308b8SJerome Forissier } 1389827308b8SJerome Forissier 1390b0104773SPascal Brand /* 1391b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1392b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1393b0104773SPascal Brand * can't restore sync with this API. 1394b0104773SPascal Brand */ 1395afc0c182SBogdan Liulko if (operation->block_size > 1) { 1396b5816c88SCedric Chaumont req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen, 1397b5816c88SCedric Chaumont operation->block_size); 1398afc0c182SBogdan Liulko } else { 1399afc0c182SBogdan Liulko req_dlen = srcLen; 1400afc0c182SBogdan Liulko } 1401afc0c182SBogdan Liulko 1402b0104773SPascal Brand if (*destLen < req_dlen) { 1403b0104773SPascal Brand *destLen = req_dlen; 1404b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1405b5816c88SCedric Chaumont goto out; 1406b0104773SPascal Brand } 1407b0104773SPascal Brand 1408e86f1266SJens Wiklander dl = *destLen; 1409afc0c182SBogdan Liulko if (operation->block_size > 1) { 1410afc0c182SBogdan Liulko res = tee_buffer_update(operation, utee_authenc_update_payload, 1411afc0c182SBogdan Liulko srcData, srcLen, destData, &dl); 1412afc0c182SBogdan Liulko } else { 1413afc0c182SBogdan Liulko if (srcLen > 0) { 1414afc0c182SBogdan Liulko res = utee_authenc_update_payload(operation->state, 1415afc0c182SBogdan Liulko srcData, srcLen, 1416afc0c182SBogdan Liulko destData, &dl); 1417afc0c182SBogdan Liulko } else { 1418afc0c182SBogdan Liulko dl = 0; 1419afc0c182SBogdan Liulko res = TEE_SUCCESS; 1420afc0c182SBogdan Liulko } 1421afc0c182SBogdan Liulko } 1422afc0c182SBogdan Liulko if (res != TEE_SUCCESS) 1423afc0c182SBogdan Liulko goto out; 1424afc0c182SBogdan Liulko 1425e86f1266SJens Wiklander *destLen = dl; 1426b0104773SPascal Brand 1427642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1428642a1607SCedric Chaumont 1429b5816c88SCedric Chaumont out: 1430b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1431b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1432b5816c88SCedric Chaumont TEE_Panic(res); 1433b5816c88SCedric Chaumont 1434b5816c88SCedric Chaumont return res; 1435b0104773SPascal Brand } 1436b0104773SPascal Brand 1437b5816c88SCedric Chaumont TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation, 14388f07fe6fSJerome Forissier const void *srcData, uint32_t srcLen, 143979a3c601SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 144079a3c601SCedric Chaumont uint32_t *tagLen) 1441b0104773SPascal Brand { 1442b0104773SPascal Brand TEE_Result res; 1443b0104773SPascal Brand uint8_t *dst = destData; 1444b0104773SPascal Brand size_t acc_dlen = 0; 1445e86f1266SJens Wiklander uint64_t tmp_dlen; 1446b0104773SPascal Brand size_t req_dlen; 1447e86f1266SJens Wiklander uint64_t tl; 1448b0104773SPascal Brand 1449b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1450b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1451b5816c88SCedric Chaumont destLen == NULL || 1452b5816c88SCedric Chaumont (destData == NULL && *destLen != 0) || 1453b5816c88SCedric Chaumont tag == NULL || tagLen == NULL) { 1454b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1455b5816c88SCedric Chaumont goto out; 1456b5816c88SCedric Chaumont } 1457b5816c88SCedric Chaumont 1458b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1459b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1460b5816c88SCedric Chaumont goto out; 1461b5816c88SCedric Chaumont } 1462b5816c88SCedric Chaumont 1463b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1464b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1465b5816c88SCedric Chaumont goto out; 1466b5816c88SCedric Chaumont } 1467b0104773SPascal Brand 1468b0104773SPascal Brand /* 1469b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1470b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1471b0104773SPascal Brand * can't restore sync with this API. 14722733280aSEtienne Carriere * 14732733280aSEtienne Carriere * Need to check this before update_payload since sync would be lost if 14742733280aSEtienne Carriere * we return short buffer after that. 1475b0104773SPascal Brand */ 14762733280aSEtienne Carriere res = TEE_ERROR_GENERIC; 14772733280aSEtienne Carriere 1478b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1479b0104773SPascal Brand if (*destLen < req_dlen) { 1480b0104773SPascal Brand *destLen = req_dlen; 1481b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1482b0104773SPascal Brand } 1483b0104773SPascal Brand 1484b5816c88SCedric Chaumont if (*tagLen < operation->ae_tag_len) { 1485b5816c88SCedric Chaumont *tagLen = operation->ae_tag_len; 1486b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1487b0104773SPascal Brand } 1488b0104773SPascal Brand 14892733280aSEtienne Carriere if (res == TEE_ERROR_SHORT_BUFFER) 14902733280aSEtienne Carriere goto out; 14912733280aSEtienne Carriere 1492afc0c182SBogdan Liulko tl = *tagLen; 1493b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1494afc0c182SBogdan Liulko if (operation->block_size > 1) { 1495afc0c182SBogdan Liulko res = tee_buffer_update(operation, utee_authenc_update_payload, 1496afc0c182SBogdan Liulko srcData, srcLen, dst, &tmp_dlen); 1497b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1498b5816c88SCedric Chaumont goto out; 1499b5816c88SCedric Chaumont 1500b0104773SPascal Brand dst += tmp_dlen; 1501b0104773SPascal Brand acc_dlen += tmp_dlen; 1502b0104773SPascal Brand 1503b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1504afc0c182SBogdan Liulko res = utee_authenc_enc_final(operation->state, 1505afc0c182SBogdan Liulko operation->buffer, 1506afc0c182SBogdan Liulko operation->buffer_offs, dst, 1507afc0c182SBogdan Liulko &tmp_dlen, tag, &tl); 1508afc0c182SBogdan Liulko } else { 1509afc0c182SBogdan Liulko res = utee_authenc_enc_final(operation->state, srcData, 1510afc0c182SBogdan Liulko srcLen, dst, &tmp_dlen, 1511e86f1266SJens Wiklander tag, &tl); 1512afc0c182SBogdan Liulko } 1513e86f1266SJens Wiklander *tagLen = tl; 1514b0104773SPascal Brand if (res != TEE_SUCCESS) 1515b5816c88SCedric Chaumont goto out; 1516b0104773SPascal Brand 1517b5816c88SCedric Chaumont acc_dlen += tmp_dlen; 1518b0104773SPascal Brand *destLen = acc_dlen; 1519642a1607SCedric Chaumont 1520b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1521b5816c88SCedric Chaumont 1522642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1523642a1607SCedric Chaumont 1524b5816c88SCedric Chaumont out: 1525b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1526b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1527b5816c88SCedric Chaumont TEE_Panic(res); 1528b0104773SPascal Brand 1529b0104773SPascal Brand return res; 1530b0104773SPascal Brand } 1531b0104773SPascal Brand 1532b5816c88SCedric Chaumont TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation, 15338f07fe6fSJerome Forissier const void *srcData, uint32_t srcLen, 1534b5816c88SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 153579a3c601SCedric Chaumont uint32_t tagLen) 1536b0104773SPascal Brand { 1537b0104773SPascal Brand TEE_Result res; 1538b0104773SPascal Brand uint8_t *dst = destData; 1539b0104773SPascal Brand size_t acc_dlen = 0; 1540e86f1266SJens Wiklander uint64_t tmp_dlen; 1541b0104773SPascal Brand size_t req_dlen; 1542b0104773SPascal Brand 1543b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1544b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1545b5816c88SCedric Chaumont destLen == NULL || 1546b5816c88SCedric Chaumont (destData == NULL && *destLen != 0) || 1547b5816c88SCedric Chaumont (tag == NULL && tagLen != 0)) { 1548b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1549b5816c88SCedric Chaumont goto out; 1550b5816c88SCedric Chaumont } 1551b5816c88SCedric Chaumont 1552b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1553b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1554b5816c88SCedric Chaumont goto out; 1555b5816c88SCedric Chaumont } 1556b5816c88SCedric Chaumont 1557b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1558b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1559b5816c88SCedric Chaumont goto out; 1560b5816c88SCedric Chaumont } 1561b0104773SPascal Brand 1562b0104773SPascal Brand /* 1563b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1564b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1565b0104773SPascal Brand * can't restore sync with this API. 1566b0104773SPascal Brand */ 1567b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1568b0104773SPascal Brand if (*destLen < req_dlen) { 1569b0104773SPascal Brand *destLen = req_dlen; 1570b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1571b5816c88SCedric Chaumont goto out; 1572b0104773SPascal Brand } 1573b0104773SPascal Brand 1574b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1575afc0c182SBogdan Liulko if (operation->block_size > 1) { 1576afc0c182SBogdan Liulko res = tee_buffer_update(operation, utee_authenc_update_payload, 1577afc0c182SBogdan Liulko srcData, srcLen, dst, &tmp_dlen); 1578b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1579b5816c88SCedric Chaumont goto out; 1580b5816c88SCedric Chaumont 1581b0104773SPascal Brand dst += tmp_dlen; 1582b0104773SPascal Brand acc_dlen += tmp_dlen; 1583b0104773SPascal Brand 1584b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1585afc0c182SBogdan Liulko res = utee_authenc_dec_final(operation->state, 1586afc0c182SBogdan Liulko operation->buffer, 1587afc0c182SBogdan Liulko operation->buffer_offs, dst, 1588afc0c182SBogdan Liulko &tmp_dlen, tag, tagLen); 1589afc0c182SBogdan Liulko } else { 1590afc0c182SBogdan Liulko res = utee_authenc_dec_final(operation->state, srcData, 1591afc0c182SBogdan Liulko srcLen, dst, &tmp_dlen, 1592b5816c88SCedric Chaumont tag, tagLen); 1593afc0c182SBogdan Liulko } 1594b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1595b5816c88SCedric Chaumont goto out; 1596b5816c88SCedric Chaumont 1597b0104773SPascal Brand /* Supplied tagLen should match what we initiated with */ 1598b5816c88SCedric Chaumont if (tagLen != operation->ae_tag_len) 1599b0104773SPascal Brand res = TEE_ERROR_MAC_INVALID; 1600b0104773SPascal Brand 1601b0104773SPascal Brand acc_dlen += tmp_dlen; 1602b0104773SPascal Brand *destLen = acc_dlen; 1603642a1607SCedric Chaumont 1604b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1605b5816c88SCedric Chaumont 1606642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1607642a1607SCedric Chaumont 1608b5816c88SCedric Chaumont out: 1609b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1610b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER && 1611b5816c88SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 1612b5816c88SCedric Chaumont TEE_Panic(res); 1613b0104773SPascal Brand 1614b0104773SPascal Brand return res; 1615b0104773SPascal Brand } 1616b0104773SPascal Brand 1617b0104773SPascal Brand /* Cryptographic Operations API - Asymmetric Functions */ 1618b0104773SPascal Brand 161912e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation, 16208f07fe6fSJerome Forissier const TEE_Attribute *params, 16218f07fe6fSJerome Forissier uint32_t paramCount, const void *srcData, 162279a3c601SCedric Chaumont uint32_t srcLen, void *destData, 162379a3c601SCedric Chaumont uint32_t *destLen) 1624b0104773SPascal Brand { 1625b0104773SPascal Brand TEE_Result res; 1626e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1627e86f1266SJens Wiklander uint64_t dl; 1628b0104773SPascal Brand 162912e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1630b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 1631b0104773SPascal Brand TEE_Panic(0); 163212e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1633b0104773SPascal Brand TEE_Panic(0); 163412e66b6fSCedric Chaumont if (!operation->key1) 1635b0104773SPascal Brand TEE_Panic(0); 163612e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 163712e66b6fSCedric Chaumont TEE_Panic(0); 163812e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_ENCRYPT) 1639b0104773SPascal Brand TEE_Panic(0); 1640b0104773SPascal Brand 1641e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1642e86f1266SJens Wiklander dl = *destLen; 1643e86f1266SJens Wiklander res = utee_asymm_operate(operation->state, ua, paramCount, srcData, 1644e86f1266SJens Wiklander srcLen, destData, &dl); 1645e86f1266SJens Wiklander *destLen = dl; 164612e66b6fSCedric Chaumont 16478844ebfcSPascal Brand if (res != TEE_SUCCESS && 16488844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 16498844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1650b0104773SPascal Brand TEE_Panic(res); 165112e66b6fSCedric Chaumont 1652b0104773SPascal Brand return res; 1653b0104773SPascal Brand } 1654b0104773SPascal Brand 165512e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation, 16568f07fe6fSJerome Forissier const TEE_Attribute *params, 16578f07fe6fSJerome Forissier uint32_t paramCount, const void *srcData, 165879a3c601SCedric Chaumont uint32_t srcLen, void *destData, 165979a3c601SCedric Chaumont uint32_t *destLen) 1660b0104773SPascal Brand { 1661b0104773SPascal Brand TEE_Result res; 1662e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1663e86f1266SJens Wiklander uint64_t dl; 1664b0104773SPascal Brand 166512e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1666b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 1667b0104773SPascal Brand TEE_Panic(0); 166812e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1669b0104773SPascal Brand TEE_Panic(0); 167012e66b6fSCedric Chaumont if (!operation->key1) 1671b0104773SPascal Brand TEE_Panic(0); 167212e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 167312e66b6fSCedric Chaumont TEE_Panic(0); 167412e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_DECRYPT) 1675b0104773SPascal Brand TEE_Panic(0); 1676b0104773SPascal Brand 1677e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1678e86f1266SJens Wiklander dl = *destLen; 1679e86f1266SJens Wiklander res = utee_asymm_operate(operation->state, ua, paramCount, srcData, 1680e86f1266SJens Wiklander srcLen, destData, &dl); 1681e86f1266SJens Wiklander *destLen = dl; 168212e66b6fSCedric Chaumont 16838844ebfcSPascal Brand if (res != TEE_SUCCESS && 16848844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 16858844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1686b0104773SPascal Brand TEE_Panic(res); 168712e66b6fSCedric Chaumont 1688b0104773SPascal Brand return res; 1689b0104773SPascal Brand } 1690b0104773SPascal Brand 169112e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation, 16928f07fe6fSJerome Forissier const TEE_Attribute *params, 16938f07fe6fSJerome Forissier uint32_t paramCount, const void *digest, 169479a3c601SCedric Chaumont uint32_t digestLen, void *signature, 169579a3c601SCedric Chaumont uint32_t *signatureLen) 1696b0104773SPascal Brand { 1697b0104773SPascal Brand TEE_Result res; 1698e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1699e86f1266SJens Wiklander uint64_t sl; 1700b0104773SPascal Brand 170112e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || 170212e66b6fSCedric Chaumont (digest == NULL && digestLen != 0) || 1703b0104773SPascal Brand signature == NULL || signatureLen == NULL) 1704b0104773SPascal Brand TEE_Panic(0); 170512e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1706b0104773SPascal Brand TEE_Panic(0); 170712e66b6fSCedric Chaumont if (!operation->key1) 1708b0104773SPascal Brand TEE_Panic(0); 170912e66b6fSCedric Chaumont if (operation->info.operationClass != 171012e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 171112e66b6fSCedric Chaumont TEE_Panic(0); 171212e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_SIGN) 1713b0104773SPascal Brand TEE_Panic(0); 1714b0104773SPascal Brand 1715e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1716e86f1266SJens Wiklander sl = *signatureLen; 1717e86f1266SJens Wiklander res = utee_asymm_operate(operation->state, ua, paramCount, digest, 1718e86f1266SJens Wiklander digestLen, signature, &sl); 1719e86f1266SJens Wiklander *signatureLen = sl; 172012e66b6fSCedric Chaumont 1721b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER) 1722b0104773SPascal Brand TEE_Panic(res); 172312e66b6fSCedric Chaumont 1724b0104773SPascal Brand return res; 1725b0104773SPascal Brand } 1726b0104773SPascal Brand 172712e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation, 17288f07fe6fSJerome Forissier const TEE_Attribute *params, 17298f07fe6fSJerome Forissier uint32_t paramCount, const void *digest, 17308f07fe6fSJerome Forissier uint32_t digestLen, 17318f07fe6fSJerome Forissier const void *signature, 173279a3c601SCedric Chaumont uint32_t signatureLen) 1733b0104773SPascal Brand { 1734b0104773SPascal Brand TEE_Result res; 1735e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1736b0104773SPascal Brand 173712e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || 173812e66b6fSCedric Chaumont (digest == NULL && digestLen != 0) || 1739b0104773SPascal Brand (signature == NULL && signatureLen != 0)) 1740b0104773SPascal Brand TEE_Panic(0); 174112e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1742b0104773SPascal Brand TEE_Panic(0); 174312e66b6fSCedric Chaumont if (!operation->key1) 1744b0104773SPascal Brand TEE_Panic(0); 174512e66b6fSCedric Chaumont if (operation->info.operationClass != 174612e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 174712e66b6fSCedric Chaumont TEE_Panic(0); 174812e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_VERIFY) 1749b0104773SPascal Brand TEE_Panic(0); 1750b0104773SPascal Brand 1751e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1752e86f1266SJens Wiklander res = utee_asymm_verify(operation->state, ua, paramCount, digest, 175312e66b6fSCedric Chaumont digestLen, signature, signatureLen); 175412e66b6fSCedric Chaumont 1755b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID) 1756b0104773SPascal Brand TEE_Panic(res); 175712e66b6fSCedric Chaumont 1758b0104773SPascal Brand return res; 1759b0104773SPascal Brand } 1760b0104773SPascal Brand 1761b0104773SPascal Brand /* Cryptographic Operations API - Key Derivation Functions */ 1762b0104773SPascal Brand 1763b0104773SPascal Brand void TEE_DeriveKey(TEE_OperationHandle operation, 1764b0104773SPascal Brand const TEE_Attribute *params, uint32_t paramCount, 1765b0104773SPascal Brand TEE_ObjectHandle derivedKey) 1766b0104773SPascal Brand { 1767b0104773SPascal Brand TEE_Result res; 1768b0104773SPascal Brand TEE_ObjectInfo key_info; 1769e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1770b0104773SPascal Brand 1771b0104773SPascal Brand if (operation == TEE_HANDLE_NULL || derivedKey == 0) 1772b0104773SPascal Brand TEE_Panic(0); 177384fa9467SCedric Chaumont if (params == NULL && paramCount != 0) 1774b0104773SPascal Brand TEE_Panic(0); 17758854d3c6SJerome Forissier if (TEE_ALG_GET_CLASS(operation->info.algorithm) != 17768854d3c6SJerome Forissier TEE_OPERATION_KEY_DERIVATION) 1777b0104773SPascal Brand TEE_Panic(0); 1778b0104773SPascal Brand 1779b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION) 1780b0104773SPascal Brand TEE_Panic(0); 178184fa9467SCedric Chaumont if (!operation->key1) 178284fa9467SCedric Chaumont TEE_Panic(0); 1783b0104773SPascal Brand if (operation->info.mode != TEE_MODE_DERIVE) 1784b0104773SPascal Brand TEE_Panic(0); 1785b0104773SPascal Brand if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0) 1786b0104773SPascal Brand TEE_Panic(0); 1787b0104773SPascal Brand 1788e86f1266SJens Wiklander res = utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info); 1789b0104773SPascal Brand if (res != TEE_SUCCESS) 1790b36311adSJerome Forissier TEE_Panic(res); 1791b0104773SPascal Brand 1792b0104773SPascal Brand if (key_info.objectType != TEE_TYPE_GENERIC_SECRET) 1793b0104773SPascal Brand TEE_Panic(0); 1794b0104773SPascal Brand if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1795b0104773SPascal Brand TEE_Panic(0); 1796b0104773SPascal Brand 1797e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1798e86f1266SJens Wiklander res = utee_cryp_derive_key(operation->state, ua, paramCount, 1799e86f1266SJens Wiklander (unsigned long)derivedKey); 1800b0104773SPascal Brand if (res != TEE_SUCCESS) 1801b0104773SPascal Brand TEE_Panic(res); 1802b0104773SPascal Brand } 1803b0104773SPascal Brand 1804b0104773SPascal Brand /* Cryptographic Operations API - Random Number Generation Functions */ 1805b0104773SPascal Brand 180679a3c601SCedric Chaumont void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen) 1807b0104773SPascal Brand { 1808b0104773SPascal Brand TEE_Result res; 1809b0104773SPascal Brand 1810b0104773SPascal Brand res = utee_cryp_random_number_generate(randomBuffer, randomBufferLen); 1811b0104773SPascal Brand if (res != TEE_SUCCESS) 1812b0104773SPascal Brand TEE_Panic(res); 1813b0104773SPascal Brand } 1814433c4257SJens Wiklander 1815433c4257SJens Wiklander int rand(void) 1816433c4257SJens Wiklander { 1817433c4257SJens Wiklander int rc; 1818433c4257SJens Wiklander 1819433c4257SJens Wiklander TEE_GenerateRandom(&rc, sizeof(rc)); 1820433c4257SJens Wiklander 1821433c4257SJens Wiklander /* 1822433c4257SJens Wiklander * RAND_MAX is the larges int, INT_MAX which is all bits but the 1823433c4257SJens Wiklander * highest bit set. 1824433c4257SJens Wiklander */ 1825433c4257SJens Wiklander return rc & RAND_MAX; 1826433c4257SJens Wiklander } 1827