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: 87*91fc6bd8SJerome Forissier case TEE_ALG_SM2_PKE: 881220586eSCedric Chaumont if (maxKeySize != 256) 891220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 901220586eSCedric Chaumont break; 911220586eSCedric Chaumont 921220586eSCedric Chaumont case TEE_ALG_ECDSA_P384: 931220586eSCedric Chaumont case TEE_ALG_ECDH_P384: 941220586eSCedric Chaumont if (maxKeySize != 384) 951220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 961220586eSCedric Chaumont break; 971220586eSCedric Chaumont 981220586eSCedric Chaumont case TEE_ALG_ECDSA_P521: 991220586eSCedric Chaumont case TEE_ALG_ECDH_P521: 1001220586eSCedric Chaumont if (maxKeySize != 521) 1011220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 1021220586eSCedric Chaumont break; 1031220586eSCedric Chaumont 104218d9055SCedric Chaumont default: 105218d9055SCedric Chaumont break; 106218d9055SCedric Chaumont } 107218d9055SCedric Chaumont 108218d9055SCedric Chaumont /* Check algorithm mode */ 109b0104773SPascal Brand switch (algorithm) { 110b0104773SPascal Brand case TEE_ALG_AES_CTS: 111b0104773SPascal Brand case TEE_ALG_AES_XTS: 112b0104773SPascal Brand buffer_two_blocks = true; 1134bd53c54SJerome Forissier /* FALLTHROUGH */ 1144bd53c54SJerome Forissier case TEE_ALG_AES_ECB_NOPAD: 115b0104773SPascal Brand case TEE_ALG_AES_CBC_NOPAD: 116b0104773SPascal Brand case TEE_ALG_AES_CCM: 117b0104773SPascal Brand case TEE_ALG_DES_ECB_NOPAD: 118b0104773SPascal Brand case TEE_ALG_DES_CBC_NOPAD: 119b0104773SPascal Brand case TEE_ALG_DES3_ECB_NOPAD: 120b0104773SPascal Brand case TEE_ALG_DES3_CBC_NOPAD: 121ade6f848SJerome Forissier case TEE_ALG_SM4_ECB_NOPAD: 122ade6f848SJerome Forissier case TEE_ALG_SM4_CBC_NOPAD: 123ade6f848SJerome Forissier case TEE_ALG_SM4_CTR: 124b0104773SPascal Brand if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES) 125b0104773SPascal Brand block_size = TEE_AES_BLOCK_SIZE; 126ade6f848SJerome Forissier else if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_SM4) 127ade6f848SJerome Forissier block_size = TEE_SM4_BLOCK_SIZE; 128b0104773SPascal Brand else 129b0104773SPascal Brand block_size = TEE_DES_BLOCK_SIZE; 130afc0c182SBogdan Liulko /* FALLTHROUGH */ 13157aabac5SBogdan Liulko case TEE_ALG_AES_CTR: 132afc0c182SBogdan Liulko case TEE_ALG_AES_GCM: 133b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) 134b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT; 135b0104773SPascal Brand else if (mode == TEE_MODE_DECRYPT) 136b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT; 137b0104773SPascal Brand else 138b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 139b0104773SPascal Brand break; 140b0104773SPascal Brand 1416a2e0a9fSGabor Szekely #if defined(CFG_CRYPTO_RSASSA_NA1) 1426a2e0a9fSGabor Szekely case TEE_ALG_RSASSA_PKCS1_V1_5: 1436a2e0a9fSGabor Szekely #endif 144b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_MD5: 145b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1: 146b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224: 147b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256: 148b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384: 149b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512: 150b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1: 151b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224: 152b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256: 153b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384: 154b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512: 155b0104773SPascal Brand case TEE_ALG_DSA_SHA1: 156218d9055SCedric Chaumont case TEE_ALG_DSA_SHA224: 157218d9055SCedric Chaumont case TEE_ALG_DSA_SHA256: 1581220586eSCedric Chaumont case TEE_ALG_ECDSA_P192: 1591220586eSCedric Chaumont case TEE_ALG_ECDSA_P224: 1601220586eSCedric Chaumont case TEE_ALG_ECDSA_P256: 1611220586eSCedric Chaumont case TEE_ALG_ECDSA_P384: 1621220586eSCedric Chaumont case TEE_ALG_ECDSA_P521: 163b0104773SPascal Brand if (mode == TEE_MODE_SIGN) { 164b0104773SPascal Brand with_private_key = true; 165b0104773SPascal Brand req_key_usage = TEE_USAGE_SIGN; 166b0104773SPascal Brand } else if (mode == TEE_MODE_VERIFY) { 167b0104773SPascal Brand req_key_usage = TEE_USAGE_VERIFY; 168b0104773SPascal Brand } else { 169b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 170b0104773SPascal Brand } 171b0104773SPascal Brand break; 172b0104773SPascal Brand 173b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_V1_5: 174b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1: 175b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224: 176b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256: 177b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384: 178b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512: 179*91fc6bd8SJerome Forissier case TEE_ALG_SM2_PKE: 180b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) { 181b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT; 182b0104773SPascal Brand } else if (mode == TEE_MODE_DECRYPT) { 183b0104773SPascal Brand with_private_key = true; 184b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT; 185b0104773SPascal Brand } else { 186b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 187b0104773SPascal Brand } 188b0104773SPascal Brand break; 189b0104773SPascal Brand 190b0104773SPascal Brand case TEE_ALG_RSA_NOPAD: 191b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) { 192b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY; 193b0104773SPascal Brand } else if (mode == TEE_MODE_DECRYPT) { 194b0104773SPascal Brand with_private_key = true; 195b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT | TEE_USAGE_SIGN; 196b0104773SPascal Brand } else { 197b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 198b0104773SPascal Brand } 199b0104773SPascal Brand break; 200b0104773SPascal Brand 201b0104773SPascal Brand case TEE_ALG_DH_DERIVE_SHARED_SECRET: 2021220586eSCedric Chaumont case TEE_ALG_ECDH_P192: 2031220586eSCedric Chaumont case TEE_ALG_ECDH_P224: 2041220586eSCedric Chaumont case TEE_ALG_ECDH_P256: 2051220586eSCedric Chaumont case TEE_ALG_ECDH_P384: 2061220586eSCedric Chaumont case TEE_ALG_ECDH_P521: 207cdb198a7SJerome Forissier case TEE_ALG_HKDF_MD5_DERIVE_KEY: 208cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA1_DERIVE_KEY: 209cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA224_DERIVE_KEY: 210cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA256_DERIVE_KEY: 211cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA384_DERIVE_KEY: 212cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA512_DERIVE_KEY: 2138854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA1_DERIVE_KEY: 2148854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA224_DERIVE_KEY: 2158854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA256_DERIVE_KEY: 2168854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA384_DERIVE_KEY: 2178854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA512_DERIVE_KEY: 2180f2293b7SJerome Forissier case TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY: 219b0104773SPascal Brand if (mode != TEE_MODE_DERIVE) 220b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 221b0104773SPascal Brand with_private_key = true; 222b0104773SPascal Brand req_key_usage = TEE_USAGE_DERIVE; 223b0104773SPascal Brand break; 224b0104773SPascal Brand 225b0104773SPascal Brand case TEE_ALG_MD5: 226b0104773SPascal Brand case TEE_ALG_SHA1: 227b0104773SPascal Brand case TEE_ALG_SHA224: 228b0104773SPascal Brand case TEE_ALG_SHA256: 229b0104773SPascal Brand case TEE_ALG_SHA384: 230b0104773SPascal Brand case TEE_ALG_SHA512: 23147645577SJerome Forissier case TEE_ALG_SM3: 232b0104773SPascal Brand if (mode != TEE_MODE_DIGEST) 233b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 23405304565SCedric Chaumont /* v1.1: flags always set for digest operations */ 235b0104773SPascal Brand handle_state |= TEE_HANDLE_FLAG_KEY_SET; 236b0104773SPascal Brand req_key_usage = 0; 237b0104773SPascal Brand break; 238b0104773SPascal Brand 239b0104773SPascal Brand case TEE_ALG_DES_CBC_MAC_NOPAD: 240b0104773SPascal Brand case TEE_ALG_AES_CBC_MAC_NOPAD: 241b0104773SPascal Brand case TEE_ALG_AES_CBC_MAC_PKCS5: 242b0104773SPascal Brand case TEE_ALG_AES_CMAC: 243b0104773SPascal Brand case TEE_ALG_DES_CBC_MAC_PKCS5: 244b0104773SPascal Brand case TEE_ALG_DES3_CBC_MAC_NOPAD: 245b0104773SPascal Brand case TEE_ALG_DES3_CBC_MAC_PKCS5: 246b0104773SPascal Brand case TEE_ALG_HMAC_MD5: 247b0104773SPascal Brand case TEE_ALG_HMAC_SHA1: 248b0104773SPascal Brand case TEE_ALG_HMAC_SHA224: 249b0104773SPascal Brand case TEE_ALG_HMAC_SHA256: 250b0104773SPascal Brand case TEE_ALG_HMAC_SHA384: 251b0104773SPascal Brand case TEE_ALG_HMAC_SHA512: 25247645577SJerome Forissier case TEE_ALG_HMAC_SM3: 253b0104773SPascal Brand if (mode != TEE_MODE_MAC) 254b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 255b0104773SPascal Brand req_key_usage = TEE_USAGE_MAC; 256b0104773SPascal Brand break; 257b0104773SPascal Brand 258b0104773SPascal Brand default: 259b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 260b0104773SPascal Brand } 261b0104773SPascal Brand 262b66f219bSJens Wiklander op = TEE_Malloc(sizeof(*op), TEE_MALLOC_FILL_ZERO); 2639b52c538SCedric Chaumont if (!op) 264b0104773SPascal Brand return TEE_ERROR_OUT_OF_MEMORY; 265b0104773SPascal Brand 266b0104773SPascal Brand op->info.algorithm = algorithm; 267b0104773SPascal Brand op->info.operationClass = TEE_ALG_GET_CLASS(algorithm); 2686a2e0a9fSGabor Szekely #ifdef CFG_CRYPTO_RSASSA_NA1 2696a2e0a9fSGabor Szekely if (algorithm == TEE_ALG_RSASSA_PKCS1_V1_5) 2706a2e0a9fSGabor Szekely op->info.operationClass = TEE_OPERATION_ASYMMETRIC_SIGNATURE; 2716a2e0a9fSGabor Szekely #endif 272b0104773SPascal Brand op->info.mode = mode; 273b0104773SPascal Brand op->info.maxKeySize = maxKeySize; 274b0104773SPascal Brand op->info.requiredKeyUsage = req_key_usage; 275b0104773SPascal Brand op->info.handleState = handle_state; 276b0104773SPascal Brand 277b0104773SPascal Brand if (block_size > 1) { 278b0104773SPascal Brand size_t buffer_size = block_size; 279b0104773SPascal Brand 280b0104773SPascal Brand if (buffer_two_blocks) 281b0104773SPascal Brand buffer_size *= 2; 282b0104773SPascal Brand 2839b52c538SCedric Chaumont op->buffer = TEE_Malloc(buffer_size, 2849b52c538SCedric Chaumont TEE_USER_MEM_HINT_NO_FILL_ZERO); 285b0104773SPascal Brand if (op->buffer == NULL) { 286b0104773SPascal Brand res = TEE_ERROR_OUT_OF_MEMORY; 287b66f219bSJens Wiklander goto out; 288b0104773SPascal Brand } 289b0104773SPascal Brand } 290b0104773SPascal Brand op->block_size = block_size; 291b0104773SPascal Brand op->buffer_two_blocks = buffer_two_blocks; 292b0104773SPascal Brand 293b0104773SPascal Brand if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) { 294b0104773SPascal Brand uint32_t mks = maxKeySize; 295b0104773SPascal Brand TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm, 296b0104773SPascal Brand with_private_key); 297b0104773SPascal Brand 298b0104773SPascal Brand /* 299b0104773SPascal Brand * If two keys are expected the max key size is the sum of 300b0104773SPascal Brand * the size of both keys. 301b0104773SPascal Brand */ 302b0104773SPascal Brand if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) 303b0104773SPascal Brand mks /= 2; 304b0104773SPascal Brand 305b0104773SPascal Brand res = TEE_AllocateTransientObject(key_type, mks, &op->key1); 306b0104773SPascal Brand if (res != TEE_SUCCESS) 307b66f219bSJens Wiklander goto out; 308b0104773SPascal Brand 30905304565SCedric Chaumont if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) { 3109b52c538SCedric Chaumont res = TEE_AllocateTransientObject(key_type, mks, 311b0104773SPascal Brand &op->key2); 312b0104773SPascal Brand if (res != TEE_SUCCESS) 313b66f219bSJens Wiklander goto out; 314b0104773SPascal Brand } 315b0104773SPascal Brand } 316b0104773SPascal Brand 317e86f1266SJens Wiklander res = utee_cryp_state_alloc(algorithm, mode, (unsigned long)op->key1, 318e86f1266SJens Wiklander (unsigned long)op->key2, &op->state); 319b66f219bSJens Wiklander if (res != TEE_SUCCESS) 320b66f219bSJens Wiklander goto out; 321b0104773SPascal Brand 32205304565SCedric Chaumont /* 32305304565SCedric Chaumont * Initialize digest operations 32405304565SCedric Chaumont * Other multi-stage operations initialized w/ TEE_xxxInit functions 32505304565SCedric Chaumont * Non-applicable on asymmetric operations 32605304565SCedric Chaumont */ 32705304565SCedric Chaumont if (TEE_ALG_GET_CLASS(algorithm) == TEE_OPERATION_DIGEST) { 32805304565SCedric Chaumont res = utee_hash_init(op->state, NULL, 0); 32905304565SCedric Chaumont if (res != TEE_SUCCESS) 330b66f219bSJens Wiklander goto out; 33105304565SCedric Chaumont /* v1.1: flags always set for digest operations */ 33205304565SCedric Chaumont op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 33305304565SCedric Chaumont } 33405304565SCedric Chaumont 335642a1607SCedric Chaumont op->operationState = TEE_OPERATION_STATE_INITIAL; 336642a1607SCedric Chaumont 337b0104773SPascal Brand *operation = op; 338b0104773SPascal Brand 339b66f219bSJens Wiklander out: 340b66f219bSJens Wiklander if (res != TEE_SUCCESS) { 341b66f219bSJens Wiklander if (res != TEE_ERROR_OUT_OF_MEMORY && 3429b52c538SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 343b36311adSJerome Forissier TEE_Panic(res); 344b66f219bSJens Wiklander if (op) { 345b66f219bSJens Wiklander if (op->state) { 346b66f219bSJens Wiklander TEE_FreeOperation(op); 347b66f219bSJens Wiklander } else { 348b66f219bSJens Wiklander TEE_Free(op->buffer); 349b66f219bSJens Wiklander TEE_FreeTransientObject(op->key1); 350b66f219bSJens Wiklander TEE_FreeTransientObject(op->key2); 351b66f219bSJens Wiklander TEE_Free(op); 352b66f219bSJens Wiklander } 353b66f219bSJens Wiklander } 354b66f219bSJens Wiklander } 355b66f219bSJens Wiklander 356b0104773SPascal Brand return res; 357b0104773SPascal Brand } 358b0104773SPascal Brand 359b0104773SPascal Brand void TEE_FreeOperation(TEE_OperationHandle operation) 360b0104773SPascal Brand { 361e889e80bSCedric Chaumont TEE_Result res; 362e889e80bSCedric Chaumont 363e889e80bSCedric Chaumont if (operation == TEE_HANDLE_NULL) 364e889e80bSCedric Chaumont TEE_Panic(0); 365e889e80bSCedric Chaumont 366b0104773SPascal Brand /* 367b0104773SPascal Brand * Note that keys should not be freed here, since they are 368b0104773SPascal Brand * claimed by the operation they will be freed by 369b0104773SPascal Brand * utee_cryp_state_free(). 370b0104773SPascal Brand */ 371e889e80bSCedric Chaumont res = utee_cryp_state_free(operation->state); 372e889e80bSCedric Chaumont if (res != TEE_SUCCESS) 373b36311adSJerome Forissier TEE_Panic(res); 374e889e80bSCedric Chaumont 375b0104773SPascal Brand TEE_Free(operation->buffer); 376b0104773SPascal Brand TEE_Free(operation); 377b0104773SPascal Brand } 378b0104773SPascal Brand 379b0104773SPascal Brand void TEE_GetOperationInfo(TEE_OperationHandle operation, 380b0104773SPascal Brand TEE_OperationInfo *operationInfo) 381b0104773SPascal Brand { 382b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 383b0104773SPascal Brand TEE_Panic(0); 384b0104773SPascal Brand 38505304565SCedric Chaumont if (!operationInfo) 386b0104773SPascal Brand TEE_Panic(0); 387b0104773SPascal Brand 388b0104773SPascal Brand *operationInfo = operation->info; 389b0104773SPascal Brand } 390b0104773SPascal Brand 39105304565SCedric Chaumont TEE_Result TEE_GetOperationInfoMultiple(TEE_OperationHandle operation, 39205304565SCedric Chaumont TEE_OperationInfoMultiple *operationInfoMultiple, 39305304565SCedric Chaumont uint32_t *operationSize) 39405304565SCedric Chaumont { 39505304565SCedric Chaumont TEE_Result res = TEE_SUCCESS; 39605304565SCedric Chaumont TEE_ObjectInfo key_info1; 39705304565SCedric Chaumont TEE_ObjectInfo key_info2; 39805304565SCedric Chaumont uint32_t num_of_keys; 39905304565SCedric Chaumont size_t n; 40005304565SCedric Chaumont 40105304565SCedric Chaumont if (operation == TEE_HANDLE_NULL) { 40205304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 40305304565SCedric Chaumont goto out; 40405304565SCedric Chaumont } 40505304565SCedric Chaumont 40605304565SCedric Chaumont if (!operationInfoMultiple) { 40705304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 40805304565SCedric Chaumont goto out; 40905304565SCedric Chaumont } 41005304565SCedric Chaumont 41105304565SCedric Chaumont if (!operationSize) { 41205304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 41305304565SCedric Chaumont goto out; 41405304565SCedric Chaumont } 41505304565SCedric Chaumont 41605304565SCedric Chaumont num_of_keys = (*operationSize-sizeof(TEE_OperationInfoMultiple))/ 41705304565SCedric Chaumont sizeof(TEE_OperationInfoKey); 41805304565SCedric Chaumont 41905304565SCedric Chaumont if (num_of_keys > 2) { 42005304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 42105304565SCedric Chaumont goto out; 42205304565SCedric Chaumont } 42305304565SCedric Chaumont 42405304565SCedric Chaumont /* Two keys flag (TEE_ALG_AES_XTS only) */ 42505304565SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 42605304565SCedric Chaumont 0 && 42705304565SCedric Chaumont (num_of_keys != 2)) { 42805304565SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 42905304565SCedric Chaumont goto out; 43005304565SCedric Chaumont } 43105304565SCedric Chaumont 43205304565SCedric Chaumont /* Clear */ 43305304565SCedric Chaumont for (n = 0; n < num_of_keys; n++) { 43405304565SCedric Chaumont operationInfoMultiple->keyInformation[n].keySize = 0; 43505304565SCedric Chaumont operationInfoMultiple->keyInformation[n].requiredKeyUsage = 0; 43605304565SCedric Chaumont } 43705304565SCedric Chaumont 43805304565SCedric Chaumont if (num_of_keys == 2) { 43905304565SCedric Chaumont res = TEE_GetObjectInfo1(operation->key2, &key_info2); 44005304565SCedric Chaumont /* Key2 is not a valid handle */ 44105304565SCedric Chaumont if (res != TEE_SUCCESS) 44205304565SCedric Chaumont goto out; 44305304565SCedric Chaumont 44405304565SCedric Chaumont operationInfoMultiple->keyInformation[1].keySize = 44505304565SCedric Chaumont key_info2.keySize; 44605304565SCedric Chaumont operationInfoMultiple->keyInformation[1].requiredKeyUsage = 44705304565SCedric Chaumont operation->info.requiredKeyUsage; 44805304565SCedric Chaumont } 44905304565SCedric Chaumont 45005304565SCedric Chaumont if (num_of_keys >= 1) { 45105304565SCedric Chaumont res = TEE_GetObjectInfo1(operation->key1, &key_info1); 45205304565SCedric Chaumont /* Key1 is not a valid handle */ 45305304565SCedric Chaumont if (res != TEE_SUCCESS) { 45405304565SCedric Chaumont if (num_of_keys == 2) { 45505304565SCedric Chaumont operationInfoMultiple->keyInformation[1]. 45605304565SCedric Chaumont keySize = 0; 45705304565SCedric Chaumont operationInfoMultiple->keyInformation[1]. 45805304565SCedric Chaumont requiredKeyUsage = 0; 45905304565SCedric Chaumont } 46005304565SCedric Chaumont goto out; 46105304565SCedric Chaumont } 46205304565SCedric Chaumont 46305304565SCedric Chaumont operationInfoMultiple->keyInformation[0].keySize = 46405304565SCedric Chaumont key_info1.keySize; 46505304565SCedric Chaumont operationInfoMultiple->keyInformation[0].requiredKeyUsage = 46605304565SCedric Chaumont operation->info.requiredKeyUsage; 46705304565SCedric Chaumont } 46805304565SCedric Chaumont 46905304565SCedric Chaumont /* No key */ 47005304565SCedric Chaumont operationInfoMultiple->algorithm = operation->info.algorithm; 47105304565SCedric Chaumont operationInfoMultiple->operationClass = operation->info.operationClass; 47205304565SCedric Chaumont operationInfoMultiple->mode = operation->info.mode; 47305304565SCedric Chaumont operationInfoMultiple->digestLength = operation->info.digestLength; 47405304565SCedric Chaumont operationInfoMultiple->maxKeySize = operation->info.maxKeySize; 47505304565SCedric Chaumont operationInfoMultiple->handleState = operation->info.handleState; 47605304565SCedric Chaumont operationInfoMultiple->operationState = operation->operationState; 47705304565SCedric Chaumont operationInfoMultiple->numberOfKeys = num_of_keys; 47805304565SCedric Chaumont 47905304565SCedric Chaumont out: 48005304565SCedric Chaumont if (res != TEE_SUCCESS && 48105304565SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 482b36311adSJerome Forissier TEE_Panic(res); 48305304565SCedric Chaumont 48405304565SCedric Chaumont return res; 48505304565SCedric Chaumont } 48605304565SCedric Chaumont 487b0104773SPascal Brand void TEE_ResetOperation(TEE_OperationHandle operation) 488b0104773SPascal Brand { 489b0104773SPascal Brand TEE_Result res; 490b0104773SPascal Brand 491b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 492b0104773SPascal Brand TEE_Panic(0); 493bf80076aSCedric Chaumont 494642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET)) 495bf80076aSCedric Chaumont TEE_Panic(0); 496bf80076aSCedric Chaumont 497642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 498642a1607SCedric Chaumont 499b0104773SPascal Brand if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 500b0104773SPascal Brand res = utee_hash_init(operation->state, NULL, 0); 501b0104773SPascal Brand if (res != TEE_SUCCESS) 502b0104773SPascal Brand TEE_Panic(res); 50305304565SCedric Chaumont operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 50405304565SCedric Chaumont } else { 505b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 506b0104773SPascal Brand } 50705304565SCedric Chaumont } 508b0104773SPascal Brand 509b0104773SPascal Brand TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation, 510b0104773SPascal Brand TEE_ObjectHandle key) 511b0104773SPascal Brand { 5127583c59eSCedric Chaumont TEE_Result res; 513b0104773SPascal Brand uint32_t key_size = 0; 514b0104773SPascal Brand TEE_ObjectInfo key_info; 515b0104773SPascal Brand 516a57c1e2eSCedric Chaumont if (operation == TEE_HANDLE_NULL) { 517a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 518a57c1e2eSCedric Chaumont goto out; 519a57c1e2eSCedric Chaumont } 520a57c1e2eSCedric Chaumont 521642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 522642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 523642a1607SCedric Chaumont goto out; 524642a1607SCedric Chaumont } 525642a1607SCedric Chaumont 526a57c1e2eSCedric Chaumont if (key == TEE_HANDLE_NULL) { 527a57c1e2eSCedric Chaumont /* Operation key cleared */ 528a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key1); 529a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 530a57c1e2eSCedric Chaumont goto out; 531a57c1e2eSCedric Chaumont } 532a57c1e2eSCedric Chaumont 533a57c1e2eSCedric Chaumont /* No key for digest operation */ 534a57c1e2eSCedric Chaumont if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 535a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 536a57c1e2eSCedric Chaumont goto out; 537a57c1e2eSCedric Chaumont } 538a57c1e2eSCedric Chaumont 539a57c1e2eSCedric Chaumont /* Two keys flag not expected (TEE_ALG_AES_XTS excluded) */ 540a57c1e2eSCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 541a57c1e2eSCedric Chaumont 0) { 542a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 543a57c1e2eSCedric Chaumont goto out; 544a57c1e2eSCedric Chaumont } 545a57c1e2eSCedric Chaumont 5467583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key, &key_info); 547a57c1e2eSCedric Chaumont /* Key is not a valid handle */ 5487583c59eSCedric Chaumont if (res != TEE_SUCCESS) 549a57c1e2eSCedric Chaumont goto out; 5507583c59eSCedric Chaumont 551b0104773SPascal Brand /* Supplied key has to meet required usage */ 552b0104773SPascal Brand if ((key_info.objectUsage & operation->info.requiredKeyUsage) != 553b0104773SPascal Brand operation->info.requiredKeyUsage) { 554a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 555a57c1e2eSCedric Chaumont goto out; 556b0104773SPascal Brand } 557b0104773SPascal Brand 558a57c1e2eSCedric Chaumont if (operation->info.maxKeySize < key_info.keySize) { 559a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 560a57c1e2eSCedric Chaumont goto out; 561a57c1e2eSCedric Chaumont } 562b0104773SPascal Brand 5637583c59eSCedric Chaumont key_size = key_info.keySize; 564b0104773SPascal Brand 565b0104773SPascal Brand TEE_ResetTransientObject(operation->key1); 566b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 567b0104773SPascal Brand 5687583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key1, key); 5697583c59eSCedric Chaumont if (res != TEE_SUCCESS) 570a57c1e2eSCedric Chaumont goto out; 5717583c59eSCedric Chaumont 572b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 573b0104773SPascal Brand 574b0104773SPascal Brand operation->info.keySize = key_size; 575b0104773SPascal Brand 5767583c59eSCedric Chaumont out: 577a57c1e2eSCedric Chaumont if (res != TEE_SUCCESS && 578a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 579a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 580b36311adSJerome Forissier TEE_Panic(res); 581a57c1e2eSCedric Chaumont 582a57c1e2eSCedric Chaumont return res; 583b0104773SPascal Brand } 584b0104773SPascal Brand 585b0104773SPascal Brand TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation, 586b0104773SPascal Brand TEE_ObjectHandle key1, TEE_ObjectHandle key2) 587b0104773SPascal Brand { 5887583c59eSCedric Chaumont TEE_Result res; 589b0104773SPascal Brand uint32_t key_size = 0; 590b0104773SPascal Brand TEE_ObjectInfo key_info1; 591b0104773SPascal Brand TEE_ObjectInfo key_info2; 592b0104773SPascal Brand 593a57c1e2eSCedric Chaumont if (operation == TEE_HANDLE_NULL) { 594a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 595a57c1e2eSCedric Chaumont goto out; 596a57c1e2eSCedric Chaumont } 597a57c1e2eSCedric Chaumont 598642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 599642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 600642a1607SCedric Chaumont goto out; 601642a1607SCedric Chaumont } 602642a1607SCedric Chaumont 603a57c1e2eSCedric Chaumont /* 604a57c1e2eSCedric Chaumont * Key1/Key2 and/or are not initialized and 605a57c1e2eSCedric Chaumont * Either both keys are NULL or both are not NULL 606a57c1e2eSCedric Chaumont */ 607a57c1e2eSCedric Chaumont if (key1 == TEE_HANDLE_NULL || key2 == TEE_HANDLE_NULL) { 608a57c1e2eSCedric Chaumont /* Clear operation key1 (if needed) */ 609a57c1e2eSCedric Chaumont if (key1 == TEE_HANDLE_NULL) 610a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key1); 611a57c1e2eSCedric Chaumont /* Clear operation key2 (if needed) */ 612a57c1e2eSCedric Chaumont if (key2 == TEE_HANDLE_NULL) 613a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key2); 614a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 615a57c1e2eSCedric Chaumont goto out; 616a57c1e2eSCedric Chaumont } 617a57c1e2eSCedric Chaumont 618a57c1e2eSCedric Chaumont /* No key for digest operation */ 619a57c1e2eSCedric Chaumont if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 620a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 621a57c1e2eSCedric Chaumont goto out; 622a57c1e2eSCedric Chaumont } 623a57c1e2eSCedric Chaumont 624a57c1e2eSCedric Chaumont /* Two keys flag expected (TEE_ALG_AES_XTS only) */ 625a57c1e2eSCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 626a57c1e2eSCedric Chaumont 0) { 627a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 628a57c1e2eSCedric Chaumont goto out; 629a57c1e2eSCedric Chaumont } 630a57c1e2eSCedric Chaumont 6317583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key1, &key_info1); 632a57c1e2eSCedric Chaumont /* Key1 is not a valid handle */ 6337583c59eSCedric Chaumont if (res != TEE_SUCCESS) 634a57c1e2eSCedric Chaumont goto out; 6357583c59eSCedric Chaumont 636b0104773SPascal Brand /* Supplied key has to meet required usage */ 637b0104773SPascal Brand if ((key_info1.objectUsage & operation->info. 638b0104773SPascal Brand requiredKeyUsage) != operation->info.requiredKeyUsage) { 639a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 640a57c1e2eSCedric Chaumont goto out; 641b0104773SPascal Brand } 642b0104773SPascal Brand 6437583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key2, &key_info2); 644a57c1e2eSCedric Chaumont /* Key2 is not a valid handle */ 6457583c59eSCedric Chaumont if (res != TEE_SUCCESS) { 6467583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) 6477583c59eSCedric Chaumont res = TEE_ERROR_CORRUPT_OBJECT_2; 648a57c1e2eSCedric Chaumont goto out; 6497583c59eSCedric Chaumont } 6507583c59eSCedric Chaumont 651b0104773SPascal Brand /* Supplied key has to meet required usage */ 652b0104773SPascal Brand if ((key_info2.objectUsage & operation->info. 653b0104773SPascal Brand requiredKeyUsage) != operation->info.requiredKeyUsage) { 654a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 655a57c1e2eSCedric Chaumont goto out; 656b0104773SPascal Brand } 657b0104773SPascal Brand 658b0104773SPascal Brand /* 659b0104773SPascal Brand * AES-XTS (the only multi key algorithm supported, requires the 660b0104773SPascal Brand * keys to be of equal size. 661b0104773SPascal Brand */ 662b0104773SPascal Brand if (operation->info.algorithm == TEE_ALG_AES_XTS && 663a57c1e2eSCedric Chaumont key_info1.keySize != key_info2.keySize) { 664a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 665a57c1e2eSCedric Chaumont goto out; 666b0104773SPascal Brand 667a57c1e2eSCedric Chaumont } 668a57c1e2eSCedric Chaumont 669a57c1e2eSCedric Chaumont if (operation->info.maxKeySize < key_info1.keySize) { 670a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 671a57c1e2eSCedric Chaumont goto out; 672a57c1e2eSCedric Chaumont } 673b0104773SPascal Brand 674b0104773SPascal Brand /* 675b0104773SPascal Brand * Odd that only the size of one key should be reported while 676b0104773SPascal Brand * size of two key are used when allocating the operation. 677b0104773SPascal Brand */ 6787583c59eSCedric Chaumont key_size = key_info1.keySize; 679b0104773SPascal Brand 680b0104773SPascal Brand TEE_ResetTransientObject(operation->key1); 681b0104773SPascal Brand TEE_ResetTransientObject(operation->key2); 682b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 683b0104773SPascal Brand 6847583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key1, key1); 6857583c59eSCedric Chaumont if (res != TEE_SUCCESS) 686a57c1e2eSCedric Chaumont goto out; 6877583c59eSCedric Chaumont 6887583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key2, key2); 6897583c59eSCedric Chaumont if (res != TEE_SUCCESS) { 6907583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) 6917583c59eSCedric Chaumont res = TEE_ERROR_CORRUPT_OBJECT_2; 692a57c1e2eSCedric Chaumont goto out; 6937583c59eSCedric Chaumont } 6947583c59eSCedric Chaumont 695b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 696b0104773SPascal Brand 697b0104773SPascal Brand operation->info.keySize = key_size; 698b0104773SPascal Brand 6997583c59eSCedric Chaumont out: 700a57c1e2eSCedric Chaumont if (res != TEE_SUCCESS && 701a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 702a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT_2 && 703a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE && 704a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE_2) 705b36311adSJerome Forissier TEE_Panic(res); 706a57c1e2eSCedric Chaumont 707a57c1e2eSCedric Chaumont return res; 708b0104773SPascal Brand } 709b0104773SPascal Brand 710b0104773SPascal Brand void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op) 711b0104773SPascal Brand { 712b0104773SPascal Brand TEE_Result res; 713b0104773SPascal Brand 714b0104773SPascal Brand if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL) 715b0104773SPascal Brand TEE_Panic(0); 716b0104773SPascal Brand if (dst_op->info.algorithm != src_op->info.algorithm) 717b0104773SPascal Brand TEE_Panic(0); 718b0104773SPascal Brand if (src_op->info.operationClass != TEE_OPERATION_DIGEST) { 719b0104773SPascal Brand TEE_ObjectHandle key1 = TEE_HANDLE_NULL; 720b0104773SPascal Brand TEE_ObjectHandle key2 = TEE_HANDLE_NULL; 721b0104773SPascal Brand 722b0104773SPascal Brand if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) { 723b0104773SPascal Brand key1 = src_op->key1; 724b0104773SPascal Brand key2 = src_op->key2; 725b0104773SPascal Brand } 726b0104773SPascal Brand 727b0104773SPascal Brand if ((src_op->info.handleState & 728b0104773SPascal Brand TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) { 729b0104773SPascal Brand TEE_SetOperationKey(dst_op, key1); 730b0104773SPascal Brand } else { 731b0104773SPascal Brand TEE_SetOperationKey2(dst_op, key1, key2); 732b0104773SPascal Brand } 733b0104773SPascal Brand } 734b0104773SPascal Brand dst_op->info.handleState = src_op->info.handleState; 735b0104773SPascal Brand dst_op->info.keySize = src_op->info.keySize; 736642a1607SCedric Chaumont dst_op->operationState = src_op->operationState; 737b0104773SPascal Brand 738b0104773SPascal Brand if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks || 739b0104773SPascal Brand dst_op->block_size != src_op->block_size) 740b0104773SPascal Brand TEE_Panic(0); 741b0104773SPascal Brand 742b0104773SPascal Brand if (dst_op->buffer != NULL) { 743b0104773SPascal Brand if (src_op->buffer == NULL) 744b0104773SPascal Brand TEE_Panic(0); 745b0104773SPascal Brand 746b0104773SPascal Brand memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs); 747b0104773SPascal Brand dst_op->buffer_offs = src_op->buffer_offs; 748b0104773SPascal Brand } else if (src_op->buffer != NULL) { 749b0104773SPascal Brand TEE_Panic(0); 750b0104773SPascal Brand } 751b0104773SPascal Brand 752b0104773SPascal Brand res = utee_cryp_state_copy(dst_op->state, src_op->state); 753b0104773SPascal Brand if (res != TEE_SUCCESS) 754b0104773SPascal Brand TEE_Panic(res); 755b0104773SPascal Brand } 756b0104773SPascal Brand 757b0104773SPascal Brand /* Cryptographic Operations API - Message Digest Functions */ 758b0104773SPascal Brand 7598f07fe6fSJerome Forissier static void init_hash_operation(TEE_OperationHandle operation, const void *IV, 7606d15db08SJerome Forissier uint32_t IVLen) 7616d15db08SJerome Forissier { 7626d15db08SJerome Forissier TEE_Result res; 7636d15db08SJerome Forissier 7646d15db08SJerome Forissier /* 7656d15db08SJerome Forissier * Note : IV and IVLen are never used in current implementation 7666d15db08SJerome Forissier * This is why coherent values of IV and IVLen are not checked 7676d15db08SJerome Forissier */ 7686d15db08SJerome Forissier res = utee_hash_init(operation->state, IV, IVLen); 7696d15db08SJerome Forissier if (res != TEE_SUCCESS) 7706d15db08SJerome Forissier TEE_Panic(res); 7716d15db08SJerome Forissier operation->buffer_offs = 0; 7726d15db08SJerome Forissier operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 7736d15db08SJerome Forissier } 7746d15db08SJerome Forissier 775b0104773SPascal Brand void TEE_DigestUpdate(TEE_OperationHandle operation, 7768f07fe6fSJerome Forissier const void *chunk, uint32_t chunkSize) 777b0104773SPascal Brand { 77873d6c3baSJoakim Bech TEE_Result res = TEE_ERROR_GENERIC; 779b0104773SPascal Brand 78073d6c3baSJoakim Bech if (operation == TEE_HANDLE_NULL || 78173d6c3baSJoakim Bech operation->info.operationClass != TEE_OPERATION_DIGEST) 782b0104773SPascal Brand TEE_Panic(0); 78373d6c3baSJoakim Bech 784642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 785642a1607SCedric Chaumont 786b0104773SPascal Brand res = utee_hash_update(operation->state, chunk, chunkSize); 787b0104773SPascal Brand if (res != TEE_SUCCESS) 788b0104773SPascal Brand TEE_Panic(res); 789b0104773SPascal Brand } 790b0104773SPascal Brand 7918f07fe6fSJerome Forissier TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void *chunk, 79279a3c601SCedric Chaumont uint32_t chunkLen, void *hash, uint32_t *hashLen) 793b0104773SPascal Brand { 79487c2f6b6SCedric Chaumont TEE_Result res; 795e86f1266SJens Wiklander uint64_t hl; 79687c2f6b6SCedric Chaumont 79787c2f6b6SCedric Chaumont if ((operation == TEE_HANDLE_NULL) || 79887c2f6b6SCedric Chaumont (!chunk && chunkLen) || 79987c2f6b6SCedric Chaumont !hash || 80087c2f6b6SCedric Chaumont !hashLen || 80187c2f6b6SCedric Chaumont (operation->info.operationClass != TEE_OPERATION_DIGEST)) { 80287c2f6b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 80387c2f6b6SCedric Chaumont goto out; 80487c2f6b6SCedric Chaumont } 80587c2f6b6SCedric Chaumont 806e86f1266SJens Wiklander hl = *hashLen; 807e86f1266SJens Wiklander res = utee_hash_final(operation->state, chunk, chunkLen, hash, &hl); 808e86f1266SJens Wiklander *hashLen = hl; 8096d15db08SJerome Forissier if (res != TEE_SUCCESS) 8106d15db08SJerome Forissier goto out; 8116d15db08SJerome Forissier 8126d15db08SJerome Forissier /* Reset operation state */ 8136d15db08SJerome Forissier init_hash_operation(operation, NULL, 0); 81487c2f6b6SCedric Chaumont 815642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 816642a1607SCedric Chaumont 81787c2f6b6SCedric Chaumont out: 81887c2f6b6SCedric Chaumont if (res != TEE_SUCCESS && 81987c2f6b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 820b36311adSJerome Forissier TEE_Panic(res); 82173d6c3baSJoakim Bech 82287c2f6b6SCedric Chaumont return res; 823b0104773SPascal Brand } 824b0104773SPascal Brand 825b0104773SPascal Brand /* Cryptographic Operations API - Symmetric Cipher Functions */ 826b0104773SPascal Brand 8278f07fe6fSJerome Forissier void TEE_CipherInit(TEE_OperationHandle operation, const void *IV, 8288f07fe6fSJerome Forissier uint32_t IVLen) 829b0104773SPascal Brand { 830b0104773SPascal Brand TEE_Result res; 831b0104773SPascal Brand 832b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 833b0104773SPascal Brand TEE_Panic(0); 834642a1607SCedric Chaumont 835b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_CIPHER) 836b0104773SPascal Brand TEE_Panic(0); 837642a1607SCedric Chaumont 838642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 839642a1607SCedric Chaumont !(operation->key1)) 840642a1607SCedric Chaumont TEE_Panic(0); 841642a1607SCedric Chaumont 842642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 843642a1607SCedric Chaumont TEE_ResetOperation(operation); 844642a1607SCedric Chaumont 845642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 846642a1607SCedric Chaumont 847b0104773SPascal Brand res = utee_cipher_init(operation->state, IV, IVLen); 848b0104773SPascal Brand if (res != TEE_SUCCESS) 849b0104773SPascal Brand TEE_Panic(res); 850642a1607SCedric Chaumont 851b0104773SPascal Brand operation->buffer_offs = 0; 852b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 853b0104773SPascal Brand } 854b0104773SPascal Brand 855b0104773SPascal Brand static TEE_Result tee_buffer_update( 856b0104773SPascal Brand TEE_OperationHandle op, 857e86f1266SJens Wiklander TEE_Result(*update_func)(unsigned long state, const void *src, 858e86f1266SJens Wiklander size_t slen, void *dst, uint64_t *dlen), 859b0104773SPascal Brand const void *src_data, size_t src_len, 860e86f1266SJens Wiklander void *dest_data, uint64_t *dest_len) 861b0104773SPascal Brand { 862b0104773SPascal Brand TEE_Result res; 863b0104773SPascal Brand const uint8_t *src = src_data; 864b0104773SPascal Brand size_t slen = src_len; 865b0104773SPascal Brand uint8_t *dst = dest_data; 866b0104773SPascal Brand size_t dlen = *dest_len; 867b0104773SPascal Brand size_t acc_dlen = 0; 868e86f1266SJens Wiklander uint64_t tmp_dlen; 869b0104773SPascal Brand size_t l; 870b0104773SPascal Brand size_t buffer_size; 871d3588802SPascal Brand size_t buffer_left; 872b0104773SPascal Brand 873090268f5SJens Wiklander if (!src) { 874090268f5SJens Wiklander if (slen) 875090268f5SJens Wiklander TEE_Panic(0); 876090268f5SJens Wiklander goto out; 877090268f5SJens Wiklander } 878090268f5SJens Wiklander 879d3588802SPascal Brand if (op->buffer_two_blocks) { 880b0104773SPascal Brand buffer_size = op->block_size * 2; 881d3588802SPascal Brand buffer_left = 1; 882d3588802SPascal Brand } else { 883b0104773SPascal Brand buffer_size = op->block_size; 884d3588802SPascal Brand buffer_left = 0; 885d3588802SPascal Brand } 886b0104773SPascal Brand 887b0104773SPascal Brand if (op->buffer_offs > 0) { 888b0104773SPascal Brand /* Fill up complete block */ 889b0104773SPascal Brand if (op->buffer_offs < op->block_size) 890b0104773SPascal Brand l = MIN(slen, op->block_size - op->buffer_offs); 891b0104773SPascal Brand else 892b0104773SPascal Brand l = MIN(slen, buffer_size - op->buffer_offs); 893b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, l); 894b0104773SPascal Brand op->buffer_offs += l; 895b0104773SPascal Brand src += l; 896b0104773SPascal Brand slen -= l; 897b0104773SPascal Brand if ((op->buffer_offs % op->block_size) != 0) 898b0104773SPascal Brand goto out; /* Nothing left to do */ 899b0104773SPascal Brand } 900b0104773SPascal Brand 901b0104773SPascal Brand /* If we can feed from buffer */ 902d3588802SPascal Brand if ((op->buffer_offs > 0) && 903d3588802SPascal Brand ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) { 9042ff3fdbbSPascal Brand l = ROUNDUP(op->buffer_offs + slen - buffer_size, 905b0104773SPascal Brand op->block_size); 906b0104773SPascal Brand l = MIN(op->buffer_offs, l); 907b0104773SPascal Brand tmp_dlen = dlen; 908b0104773SPascal Brand res = update_func(op->state, op->buffer, l, dst, &tmp_dlen); 909b0104773SPascal Brand if (res != TEE_SUCCESS) 910b0104773SPascal Brand TEE_Panic(res); 911b0104773SPascal Brand dst += tmp_dlen; 912b0104773SPascal Brand dlen -= tmp_dlen; 913b0104773SPascal Brand acc_dlen += tmp_dlen; 914b0104773SPascal Brand op->buffer_offs -= l; 915b0104773SPascal Brand if (op->buffer_offs > 0) { 916b0104773SPascal Brand /* 917b0104773SPascal Brand * Slen is small enough to be contained in rest buffer. 918b0104773SPascal Brand */ 919b0104773SPascal Brand memcpy(op->buffer, op->buffer + l, buffer_size - l); 920b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 921b0104773SPascal Brand op->buffer_offs += slen; 922b0104773SPascal Brand goto out; /* Nothing left to do */ 923b0104773SPascal Brand } 924b0104773SPascal Brand } 925b0104773SPascal Brand 926d3588802SPascal Brand if (slen >= (buffer_size + buffer_left)) { 927b0104773SPascal Brand /* Buffer is empty, feed as much as possible from src */ 928bf7a587fSJerome Forissier if (op->info.algorithm == TEE_ALG_AES_CTS) 929b1ecda78SJerome Forissier l = ROUNDUP(slen - buffer_size, op->block_size); 930bf7a587fSJerome Forissier else 931bf7a587fSJerome Forissier l = ROUNDUP(slen - buffer_size + 1, op->block_size); 932b0104773SPascal Brand 933b0104773SPascal Brand tmp_dlen = dlen; 934b0104773SPascal Brand res = update_func(op->state, src, l, dst, &tmp_dlen); 935b0104773SPascal Brand if (res != TEE_SUCCESS) 936b0104773SPascal Brand TEE_Panic(res); 937b0104773SPascal Brand src += l; 938b0104773SPascal Brand slen -= l; 939b0104773SPascal Brand dst += tmp_dlen; 940b0104773SPascal Brand dlen -= tmp_dlen; 941b0104773SPascal Brand acc_dlen += tmp_dlen; 942b0104773SPascal Brand } 943b0104773SPascal Brand 944b0104773SPascal Brand /* Slen is small enough to be contained in buffer. */ 945b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 946b0104773SPascal Brand op->buffer_offs += slen; 947b0104773SPascal Brand 948b0104773SPascal Brand out: 949b0104773SPascal Brand *dest_len = acc_dlen; 950b0104773SPascal Brand return TEE_SUCCESS; 951b0104773SPascal Brand } 952b0104773SPascal Brand 9538f07fe6fSJerome Forissier TEE_Result TEE_CipherUpdate(TEE_OperationHandle operation, const void *srcData, 95479a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 955b0104773SPascal Brand { 956dea1f2b6SCedric Chaumont TEE_Result res; 957b0104773SPascal Brand size_t req_dlen; 958e86f1266SJens Wiklander uint64_t dl; 959b0104773SPascal Brand 960642a1607SCedric Chaumont if (operation == TEE_HANDLE_NULL || 961dea1f2b6SCedric Chaumont (srcData == NULL && srcLen != 0) || 962dea1f2b6SCedric Chaumont destLen == NULL || 963dea1f2b6SCedric Chaumont (destData == NULL && *destLen != 0)) { 964dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 965dea1f2b6SCedric Chaumont goto out; 966dea1f2b6SCedric Chaumont } 967dea1f2b6SCedric Chaumont 968642a1607SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 969dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 970dea1f2b6SCedric Chaumont goto out; 971dea1f2b6SCedric Chaumont } 972dea1f2b6SCedric Chaumont 973642a1607SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 974642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 975642a1607SCedric Chaumont goto out; 976642a1607SCedric Chaumont } 977642a1607SCedric Chaumont 978642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 979dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 980dea1f2b6SCedric Chaumont goto out; 981dea1f2b6SCedric Chaumont } 982b0104773SPascal Brand 983e32c5ddfSJerome Forissier if (!srcData && !srcLen) { 984090268f5SJens Wiklander *destLen = 0; 985e32c5ddfSJerome Forissier res = TEE_SUCCESS; 986e32c5ddfSJerome Forissier goto out; 987e32c5ddfSJerome Forissier } 988e32c5ddfSJerome Forissier 989b0104773SPascal Brand /* Calculate required dlen */ 99057aabac5SBogdan Liulko if (operation->block_size > 1) { 99157aabac5SBogdan Liulko req_dlen = ((operation->buffer_offs + srcLen) / 99257aabac5SBogdan Liulko operation->block_size) * operation->block_size; 99357aabac5SBogdan Liulko } else { 99457aabac5SBogdan Liulko req_dlen = srcLen; 99557aabac5SBogdan Liulko } 996642a1607SCedric Chaumont if (operation->buffer_two_blocks) { 997642a1607SCedric Chaumont if (req_dlen > operation->block_size * 2) 998642a1607SCedric Chaumont req_dlen -= operation->block_size * 2; 999b0104773SPascal Brand else 1000b0104773SPascal Brand req_dlen = 0; 1001b0104773SPascal Brand } 1002b0104773SPascal Brand /* 1003b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1004b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1005b0104773SPascal Brand * can't restore sync with this API. 1006b0104773SPascal Brand */ 1007b0104773SPascal Brand if (*destLen < req_dlen) { 1008b0104773SPascal Brand *destLen = req_dlen; 1009dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1010dea1f2b6SCedric Chaumont goto out; 1011b0104773SPascal Brand } 1012b0104773SPascal Brand 1013e86f1266SJens Wiklander dl = *destLen; 101457aabac5SBogdan Liulko if (operation->block_size > 1) { 101557aabac5SBogdan Liulko res = tee_buffer_update(operation, utee_cipher_update, srcData, 101657aabac5SBogdan Liulko srcLen, destData, &dl); 101757aabac5SBogdan Liulko } else { 101857aabac5SBogdan Liulko if (srcLen > 0) { 101957aabac5SBogdan Liulko res = utee_cipher_update(operation->state, srcData, 102057aabac5SBogdan Liulko srcLen, destData, &dl); 102157aabac5SBogdan Liulko } else { 102257aabac5SBogdan Liulko res = TEE_SUCCESS; 102357aabac5SBogdan Liulko dl = 0; 102457aabac5SBogdan Liulko } 102557aabac5SBogdan Liulko } 1026e86f1266SJens Wiklander *destLen = dl; 1027b0104773SPascal Brand 1028dea1f2b6SCedric Chaumont out: 1029dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 1030dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1031b36311adSJerome Forissier TEE_Panic(res); 1032dea1f2b6SCedric Chaumont 1033dea1f2b6SCedric Chaumont return res; 1034b0104773SPascal Brand } 1035b0104773SPascal Brand 1036642a1607SCedric Chaumont TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation, 10378f07fe6fSJerome Forissier const void *srcData, uint32_t srcLen, 10388f07fe6fSJerome Forissier void *destData, uint32_t *destLen) 1039b0104773SPascal Brand { 1040b0104773SPascal Brand TEE_Result res; 1041b0104773SPascal Brand uint8_t *dst = destData; 1042b0104773SPascal Brand size_t acc_dlen = 0; 1043e86f1266SJens Wiklander uint64_t tmp_dlen; 1044b0104773SPascal Brand size_t req_dlen; 1045b0104773SPascal Brand 1046642a1607SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1047dea1f2b6SCedric Chaumont (srcData == NULL && srcLen != 0) || 1048dea1f2b6SCedric Chaumont destLen == NULL || 1049dea1f2b6SCedric Chaumont (destData == NULL && *destLen != 0)) { 1050dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1051dea1f2b6SCedric Chaumont goto out; 1052dea1f2b6SCedric Chaumont } 1053dea1f2b6SCedric Chaumont 1054642a1607SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 1055dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1056dea1f2b6SCedric Chaumont goto out; 1057dea1f2b6SCedric Chaumont } 1058dea1f2b6SCedric Chaumont 1059642a1607SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1060642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1061642a1607SCedric Chaumont goto out; 1062642a1607SCedric Chaumont } 1063642a1607SCedric Chaumont 1064642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1065dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1066dea1f2b6SCedric Chaumont goto out; 1067dea1f2b6SCedric Chaumont } 1068b0104773SPascal Brand 1069b0104773SPascal Brand /* 1070b0104773SPascal Brand * Check that the final block doesn't require padding for those 1071b0104773SPascal Brand * algorithms that requires client to supply padding. 1072b0104773SPascal Brand */ 1073642a1607SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD || 1074642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD || 1075642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD || 1076642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD || 1077642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || 1078ade6f848SJerome Forissier operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD || 1079ade6f848SJerome Forissier operation->info.algorithm == TEE_ALG_SM4_ECB_NOPAD || 1080ade6f848SJerome Forissier operation->info.algorithm == TEE_ALG_SM4_CBC_NOPAD) { 1081642a1607SCedric Chaumont if (((operation->buffer_offs + srcLen) % operation->block_size) 1082642a1607SCedric Chaumont != 0) { 1083dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1084dea1f2b6SCedric Chaumont goto out; 1085dea1f2b6SCedric Chaumont } 1086b0104773SPascal Brand } 1087b0104773SPascal Brand 1088b0104773SPascal Brand /* 1089b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1090b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1091b0104773SPascal Brand * can't restore sync with this API. 1092b0104773SPascal Brand */ 109357aabac5SBogdan Liulko if (operation->block_size > 1) { 1094642a1607SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 109557aabac5SBogdan Liulko } else { 109657aabac5SBogdan Liulko req_dlen = srcLen; 109757aabac5SBogdan Liulko } 1098b0104773SPascal Brand if (*destLen < req_dlen) { 1099b0104773SPascal Brand *destLen = req_dlen; 1100dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1101dea1f2b6SCedric Chaumont goto out; 1102b0104773SPascal Brand } 1103b0104773SPascal Brand 1104b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 110557aabac5SBogdan Liulko if (operation->block_size > 1) { 110657aabac5SBogdan Liulko res = tee_buffer_update(operation, utee_cipher_update, 110757aabac5SBogdan Liulko srcData, srcLen, dst, &tmp_dlen); 1108dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS) 1109dea1f2b6SCedric Chaumont goto out; 1110dea1f2b6SCedric Chaumont 1111b0104773SPascal Brand dst += tmp_dlen; 1112b0104773SPascal Brand acc_dlen += tmp_dlen; 1113b0104773SPascal Brand 1114b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1115642a1607SCedric Chaumont res = utee_cipher_final(operation->state, operation->buffer, 1116642a1607SCedric Chaumont operation->buffer_offs, dst, &tmp_dlen); 111757aabac5SBogdan Liulko } else { 111857aabac5SBogdan Liulko res = utee_cipher_final(operation->state, srcData, 111957aabac5SBogdan Liulko srcLen, dst, &tmp_dlen); 112057aabac5SBogdan Liulko } 1121b0104773SPascal Brand if (res != TEE_SUCCESS) 1122dea1f2b6SCedric Chaumont goto out; 1123dea1f2b6SCedric Chaumont 1124b0104773SPascal Brand acc_dlen += tmp_dlen; 1125b0104773SPascal Brand *destLen = acc_dlen; 1126dea1f2b6SCedric Chaumont 1127642a1607SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1128642a1607SCedric Chaumont 1129642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1130642a1607SCedric Chaumont 1131dea1f2b6SCedric Chaumont out: 1132dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 1133dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1134b36311adSJerome Forissier TEE_Panic(res); 1135dea1f2b6SCedric Chaumont 1136dea1f2b6SCedric Chaumont return res; 1137b0104773SPascal Brand } 1138b0104773SPascal Brand 1139b0104773SPascal Brand /* Cryptographic Operations API - MAC Functions */ 1140b0104773SPascal Brand 11418f07fe6fSJerome Forissier void TEE_MACInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen) 1142b0104773SPascal Brand { 1143b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 1144b0104773SPascal Brand TEE_Panic(0); 1145642a1607SCedric Chaumont 1146b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_MAC) 1147b0104773SPascal Brand TEE_Panic(0); 1148642a1607SCedric Chaumont 1149642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 1150642a1607SCedric Chaumont !(operation->key1)) 1151642a1607SCedric Chaumont TEE_Panic(0); 1152642a1607SCedric Chaumont 1153642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 1154642a1607SCedric Chaumont TEE_ResetOperation(operation); 1155642a1607SCedric Chaumont 1156642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1157642a1607SCedric Chaumont 11586d15db08SJerome Forissier init_hash_operation(operation, IV, IVLen); 1159b0104773SPascal Brand } 1160b0104773SPascal Brand 11618f07fe6fSJerome Forissier void TEE_MACUpdate(TEE_OperationHandle operation, const void *chunk, 116228e0efc6SCedric Chaumont uint32_t chunkSize) 1163b0104773SPascal Brand { 1164b0104773SPascal Brand TEE_Result res; 1165b0104773SPascal Brand 116628e0efc6SCedric Chaumont if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0)) 1167b0104773SPascal Brand TEE_Panic(0); 1168642a1607SCedric Chaumont 116928e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) 1170b0104773SPascal Brand TEE_Panic(0); 1171642a1607SCedric Chaumont 117228e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1173b0104773SPascal Brand TEE_Panic(0); 1174b0104773SPascal Brand 1175642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) 1176642a1607SCedric Chaumont TEE_Panic(0); 1177642a1607SCedric Chaumont 117828e0efc6SCedric Chaumont res = utee_hash_update(operation->state, chunk, chunkSize); 1179b0104773SPascal Brand if (res != TEE_SUCCESS) 1180b0104773SPascal Brand TEE_Panic(res); 1181b0104773SPascal Brand } 1182b0104773SPascal Brand 118328e0efc6SCedric Chaumont TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation, 11848f07fe6fSJerome Forissier const void *message, uint32_t messageLen, 118579a3c601SCedric Chaumont void *mac, uint32_t *macLen) 1186b0104773SPascal Brand { 1187b0104773SPascal Brand TEE_Result res; 1188e86f1266SJens Wiklander uint64_t ml; 1189b0104773SPascal Brand 119028e0efc6SCedric Chaumont if (operation == TEE_HANDLE_NULL || 119128e0efc6SCedric Chaumont (message == NULL && messageLen != 0) || 119228e0efc6SCedric Chaumont mac == NULL || 119328e0efc6SCedric Chaumont macLen == NULL) { 119428e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 119528e0efc6SCedric Chaumont goto out; 119628e0efc6SCedric Chaumont } 1197b0104773SPascal Brand 119828e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 119928e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 120028e0efc6SCedric Chaumont goto out; 120128e0efc6SCedric Chaumont } 120228e0efc6SCedric Chaumont 120328e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 120428e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 120528e0efc6SCedric Chaumont goto out; 120628e0efc6SCedric Chaumont } 120728e0efc6SCedric Chaumont 1208642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1209642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1210642a1607SCedric Chaumont goto out; 1211642a1607SCedric Chaumont } 1212642a1607SCedric Chaumont 1213e86f1266SJens Wiklander ml = *macLen; 1214e86f1266SJens Wiklander res = utee_hash_final(operation->state, message, messageLen, mac, &ml); 1215e86f1266SJens Wiklander *macLen = ml; 121628e0efc6SCedric Chaumont if (res != TEE_SUCCESS) 121728e0efc6SCedric Chaumont goto out; 121828e0efc6SCedric Chaumont 121928e0efc6SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 122028e0efc6SCedric Chaumont 1221642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1222642a1607SCedric Chaumont 122328e0efc6SCedric Chaumont out: 122428e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 122528e0efc6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 122628e0efc6SCedric Chaumont TEE_Panic(res); 122728e0efc6SCedric Chaumont 1228b0104773SPascal Brand return res; 1229b0104773SPascal Brand } 1230b0104773SPascal Brand 1231b0104773SPascal Brand TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation, 12328f07fe6fSJerome Forissier const void *message, uint32_t messageLen, 12338f07fe6fSJerome Forissier const void *mac, uint32_t macLen) 1234b0104773SPascal Brand { 1235b0104773SPascal Brand TEE_Result res; 1236b0104773SPascal Brand uint8_t computed_mac[TEE_MAX_HASH_SIZE]; 12377f74c64aSPascal Brand uint32_t computed_mac_size = TEE_MAX_HASH_SIZE; 1238b0104773SPascal Brand 123928e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 124028e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 124128e0efc6SCedric Chaumont goto out; 124228e0efc6SCedric Chaumont } 124328e0efc6SCedric Chaumont 124428e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 124528e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 124628e0efc6SCedric Chaumont goto out; 124728e0efc6SCedric Chaumont } 124828e0efc6SCedric Chaumont 1249642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1250642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1251642a1607SCedric Chaumont goto out; 1252642a1607SCedric Chaumont } 1253642a1607SCedric Chaumont 1254b0104773SPascal Brand res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac, 1255b0104773SPascal Brand &computed_mac_size); 1256b0104773SPascal Brand if (res != TEE_SUCCESS) 125728e0efc6SCedric Chaumont goto out; 125828e0efc6SCedric Chaumont 125928e0efc6SCedric Chaumont if (computed_mac_size != macLen) { 126028e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 126128e0efc6SCedric Chaumont goto out; 126228e0efc6SCedric Chaumont } 126328e0efc6SCedric Chaumont 126448e10604SJerome Forissier if (consttime_memcmp(mac, computed_mac, computed_mac_size) != 0) { 126528e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 126628e0efc6SCedric Chaumont goto out; 126728e0efc6SCedric Chaumont } 126828e0efc6SCedric Chaumont 1269642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1270642a1607SCedric Chaumont 127128e0efc6SCedric Chaumont out: 127228e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 127328e0efc6SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 127428e0efc6SCedric Chaumont TEE_Panic(res); 127528e0efc6SCedric Chaumont 1276b0104773SPascal Brand return res; 1277b0104773SPascal Brand } 1278b0104773SPascal Brand 1279b0104773SPascal Brand /* Cryptographic Operations API - Authenticated Encryption Functions */ 1280b0104773SPascal Brand 12818f07fe6fSJerome Forissier TEE_Result TEE_AEInit(TEE_OperationHandle operation, const void *nonce, 128279a3c601SCedric Chaumont uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen, 1283b0104773SPascal Brand uint32_t payloadLen) 1284b0104773SPascal Brand { 1285b0104773SPascal Brand TEE_Result res; 1286b0104773SPascal Brand 1287b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || nonce == NULL) { 1288b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1289b5816c88SCedric Chaumont goto out; 1290b5816c88SCedric Chaumont } 1291b5816c88SCedric Chaumont 1292b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1293b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1294b5816c88SCedric Chaumont goto out; 1295b5816c88SCedric Chaumont } 1296b0104773SPascal Brand 1297642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 1298642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1299642a1607SCedric Chaumont goto out; 1300642a1607SCedric Chaumont } 1301642a1607SCedric Chaumont 1302b0104773SPascal Brand /* 1303b0104773SPascal Brand * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core 1304b0104773SPascal Brand * in the implementation. But AES-GCM spec doesn't specify the tag len 1305b0104773SPascal Brand * according to the same principle so we have to check here instead to 1306b0104773SPascal Brand * be GP compliant. 1307b0104773SPascal Brand */ 1308b5816c88SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_GCM) { 1309b0104773SPascal Brand /* 1310b0104773SPascal Brand * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96 1311b0104773SPascal Brand */ 1312b5816c88SCedric Chaumont if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) { 1313b5816c88SCedric Chaumont res = TEE_ERROR_NOT_SUPPORTED; 1314b5816c88SCedric Chaumont goto out; 1315b5816c88SCedric Chaumont } 1316b0104773SPascal Brand } 1317b0104773SPascal Brand 1318b5816c88SCedric Chaumont res = utee_authenc_init(operation->state, nonce, nonceLen, 1319b5816c88SCedric Chaumont tagLen / 8, AADLen, payloadLen); 1320b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1321b5816c88SCedric Chaumont goto out; 1322b5816c88SCedric Chaumont 1323b5816c88SCedric Chaumont operation->ae_tag_len = tagLen / 8; 1324b5816c88SCedric Chaumont operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 1325b5816c88SCedric Chaumont 1326b5816c88SCedric Chaumont out: 1327b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1328b5816c88SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 1329b0104773SPascal Brand TEE_Panic(res); 1330b5816c88SCedric Chaumont 1331b0104773SPascal Brand return res; 1332b0104773SPascal Brand } 1333b0104773SPascal Brand 13348f07fe6fSJerome Forissier void TEE_AEUpdateAAD(TEE_OperationHandle operation, const void *AADdata, 133579a3c601SCedric Chaumont uint32_t AADdataLen) 1336b0104773SPascal Brand { 1337b0104773SPascal Brand TEE_Result res; 1338b0104773SPascal Brand 1339b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1340b5816c88SCedric Chaumont (AADdata == NULL && AADdataLen != 0)) 1341b0104773SPascal Brand TEE_Panic(0); 1342642a1607SCedric Chaumont 1343b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) 1344b0104773SPascal Brand TEE_Panic(0); 1345642a1607SCedric Chaumont 1346b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1347b0104773SPascal Brand TEE_Panic(0); 1348b0104773SPascal Brand 1349b5816c88SCedric Chaumont res = utee_authenc_update_aad(operation->state, AADdata, AADdataLen); 1350642a1607SCedric Chaumont 1351642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1352642a1607SCedric Chaumont 1353b0104773SPascal Brand if (res != TEE_SUCCESS) 1354b0104773SPascal Brand TEE_Panic(res); 1355b0104773SPascal Brand } 1356b0104773SPascal Brand 13578f07fe6fSJerome Forissier TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, const void *srcData, 135879a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 1359b0104773SPascal Brand { 1360b5816c88SCedric Chaumont TEE_Result res; 1361b0104773SPascal Brand size_t req_dlen; 1362e86f1266SJens Wiklander uint64_t dl; 1363b0104773SPascal Brand 1364b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1365b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1366b5816c88SCedric Chaumont destLen == NULL || 1367b5816c88SCedric Chaumont (destData == NULL && *destLen != 0)) { 1368b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1369b5816c88SCedric Chaumont goto out; 1370b5816c88SCedric Chaumont } 1371b5816c88SCedric Chaumont 1372b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1373b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1374b5816c88SCedric Chaumont goto out; 1375b5816c88SCedric Chaumont } 1376b5816c88SCedric Chaumont 1377b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1378b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1379b5816c88SCedric Chaumont goto out; 1380b5816c88SCedric Chaumont } 1381b0104773SPascal Brand 1382827308b8SJerome Forissier if (!srcData && !srcLen) { 1383090268f5SJens Wiklander *destLen = 0; 1384827308b8SJerome Forissier res = TEE_SUCCESS; 1385827308b8SJerome Forissier goto out; 1386827308b8SJerome Forissier } 1387827308b8SJerome Forissier 1388b0104773SPascal Brand /* 1389b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1390b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1391b0104773SPascal Brand * can't restore sync with this API. 1392b0104773SPascal Brand */ 1393afc0c182SBogdan Liulko if (operation->block_size > 1) { 1394b5816c88SCedric Chaumont req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen, 1395b5816c88SCedric Chaumont operation->block_size); 1396afc0c182SBogdan Liulko } else { 1397afc0c182SBogdan Liulko req_dlen = srcLen; 1398afc0c182SBogdan Liulko } 1399afc0c182SBogdan Liulko 1400b0104773SPascal Brand if (*destLen < req_dlen) { 1401b0104773SPascal Brand *destLen = req_dlen; 1402b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1403b5816c88SCedric Chaumont goto out; 1404b0104773SPascal Brand } 1405b0104773SPascal Brand 1406e86f1266SJens Wiklander dl = *destLen; 1407afc0c182SBogdan Liulko if (operation->block_size > 1) { 1408afc0c182SBogdan Liulko res = tee_buffer_update(operation, utee_authenc_update_payload, 1409afc0c182SBogdan Liulko srcData, srcLen, destData, &dl); 1410afc0c182SBogdan Liulko } else { 1411afc0c182SBogdan Liulko if (srcLen > 0) { 1412afc0c182SBogdan Liulko res = utee_authenc_update_payload(operation->state, 1413afc0c182SBogdan Liulko srcData, srcLen, 1414afc0c182SBogdan Liulko destData, &dl); 1415afc0c182SBogdan Liulko } else { 1416afc0c182SBogdan Liulko dl = 0; 1417afc0c182SBogdan Liulko res = TEE_SUCCESS; 1418afc0c182SBogdan Liulko } 1419afc0c182SBogdan Liulko } 1420afc0c182SBogdan Liulko if (res != TEE_SUCCESS) 1421afc0c182SBogdan Liulko goto out; 1422afc0c182SBogdan Liulko 1423e86f1266SJens Wiklander *destLen = dl; 1424b0104773SPascal Brand 1425642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1426642a1607SCedric Chaumont 1427b5816c88SCedric Chaumont out: 1428b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1429b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1430b5816c88SCedric Chaumont TEE_Panic(res); 1431b5816c88SCedric Chaumont 1432b5816c88SCedric Chaumont return res; 1433b0104773SPascal Brand } 1434b0104773SPascal Brand 1435b5816c88SCedric Chaumont TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation, 14368f07fe6fSJerome Forissier const void *srcData, uint32_t srcLen, 143779a3c601SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 143879a3c601SCedric Chaumont uint32_t *tagLen) 1439b0104773SPascal Brand { 1440b0104773SPascal Brand TEE_Result res; 1441b0104773SPascal Brand uint8_t *dst = destData; 1442b0104773SPascal Brand size_t acc_dlen = 0; 1443e86f1266SJens Wiklander uint64_t tmp_dlen; 1444b0104773SPascal Brand size_t req_dlen; 1445e86f1266SJens Wiklander uint64_t tl; 1446b0104773SPascal Brand 1447b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1448b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1449b5816c88SCedric Chaumont destLen == NULL || 1450b5816c88SCedric Chaumont (destData == NULL && *destLen != 0) || 1451b5816c88SCedric Chaumont tag == NULL || tagLen == NULL) { 1452b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1453b5816c88SCedric Chaumont goto out; 1454b5816c88SCedric Chaumont } 1455b5816c88SCedric Chaumont 1456b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1457b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1458b5816c88SCedric Chaumont goto out; 1459b5816c88SCedric Chaumont } 1460b5816c88SCedric Chaumont 1461b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1462b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1463b5816c88SCedric Chaumont goto out; 1464b5816c88SCedric Chaumont } 1465b0104773SPascal Brand 1466b0104773SPascal Brand /* 1467b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1468b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1469b0104773SPascal Brand * can't restore sync with this API. 14702733280aSEtienne Carriere * 14712733280aSEtienne Carriere * Need to check this before update_payload since sync would be lost if 14722733280aSEtienne Carriere * we return short buffer after that. 1473b0104773SPascal Brand */ 14742733280aSEtienne Carriere res = TEE_ERROR_GENERIC; 14752733280aSEtienne Carriere 1476b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1477b0104773SPascal Brand if (*destLen < req_dlen) { 1478b0104773SPascal Brand *destLen = req_dlen; 1479b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1480b0104773SPascal Brand } 1481b0104773SPascal Brand 1482b5816c88SCedric Chaumont if (*tagLen < operation->ae_tag_len) { 1483b5816c88SCedric Chaumont *tagLen = operation->ae_tag_len; 1484b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1485b0104773SPascal Brand } 1486b0104773SPascal Brand 14872733280aSEtienne Carriere if (res == TEE_ERROR_SHORT_BUFFER) 14882733280aSEtienne Carriere goto out; 14892733280aSEtienne Carriere 1490afc0c182SBogdan Liulko tl = *tagLen; 1491b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1492afc0c182SBogdan Liulko if (operation->block_size > 1) { 1493afc0c182SBogdan Liulko res = tee_buffer_update(operation, utee_authenc_update_payload, 1494afc0c182SBogdan Liulko srcData, srcLen, dst, &tmp_dlen); 1495b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1496b5816c88SCedric Chaumont goto out; 1497b5816c88SCedric Chaumont 1498b0104773SPascal Brand dst += tmp_dlen; 1499b0104773SPascal Brand acc_dlen += tmp_dlen; 1500b0104773SPascal Brand 1501b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1502afc0c182SBogdan Liulko res = utee_authenc_enc_final(operation->state, 1503afc0c182SBogdan Liulko operation->buffer, 1504afc0c182SBogdan Liulko operation->buffer_offs, dst, 1505afc0c182SBogdan Liulko &tmp_dlen, tag, &tl); 1506afc0c182SBogdan Liulko } else { 1507afc0c182SBogdan Liulko res = utee_authenc_enc_final(operation->state, srcData, 1508afc0c182SBogdan Liulko srcLen, dst, &tmp_dlen, 1509e86f1266SJens Wiklander tag, &tl); 1510afc0c182SBogdan Liulko } 1511e86f1266SJens Wiklander *tagLen = tl; 1512b0104773SPascal Brand if (res != TEE_SUCCESS) 1513b5816c88SCedric Chaumont goto out; 1514b0104773SPascal Brand 1515b5816c88SCedric Chaumont acc_dlen += tmp_dlen; 1516b0104773SPascal Brand *destLen = acc_dlen; 1517642a1607SCedric Chaumont 1518b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1519b5816c88SCedric Chaumont 1520642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1521642a1607SCedric Chaumont 1522b5816c88SCedric Chaumont out: 1523b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1524b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1525b5816c88SCedric Chaumont TEE_Panic(res); 1526b0104773SPascal Brand 1527b0104773SPascal Brand return res; 1528b0104773SPascal Brand } 1529b0104773SPascal Brand 1530b5816c88SCedric Chaumont TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation, 15318f07fe6fSJerome Forissier const void *srcData, uint32_t srcLen, 1532b5816c88SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 153379a3c601SCedric Chaumont uint32_t tagLen) 1534b0104773SPascal Brand { 1535b0104773SPascal Brand TEE_Result res; 1536b0104773SPascal Brand uint8_t *dst = destData; 1537b0104773SPascal Brand size_t acc_dlen = 0; 1538e86f1266SJens Wiklander uint64_t tmp_dlen; 1539b0104773SPascal Brand size_t req_dlen; 1540b0104773SPascal Brand 1541b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1542b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1543b5816c88SCedric Chaumont destLen == NULL || 1544b5816c88SCedric Chaumont (destData == NULL && *destLen != 0) || 1545b5816c88SCedric Chaumont (tag == NULL && tagLen != 0)) { 1546b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1547b5816c88SCedric Chaumont goto out; 1548b5816c88SCedric Chaumont } 1549b5816c88SCedric Chaumont 1550b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1551b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1552b5816c88SCedric Chaumont goto out; 1553b5816c88SCedric Chaumont } 1554b5816c88SCedric Chaumont 1555b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1556b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1557b5816c88SCedric Chaumont goto out; 1558b5816c88SCedric Chaumont } 1559b0104773SPascal Brand 1560b0104773SPascal Brand /* 1561b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1562b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1563b0104773SPascal Brand * can't restore sync with this API. 1564b0104773SPascal Brand */ 1565b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1566b0104773SPascal Brand if (*destLen < req_dlen) { 1567b0104773SPascal Brand *destLen = req_dlen; 1568b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1569b5816c88SCedric Chaumont goto out; 1570b0104773SPascal Brand } 1571b0104773SPascal Brand 1572b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1573afc0c182SBogdan Liulko if (operation->block_size > 1) { 1574afc0c182SBogdan Liulko res = tee_buffer_update(operation, utee_authenc_update_payload, 1575afc0c182SBogdan Liulko srcData, srcLen, dst, &tmp_dlen); 1576b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1577b5816c88SCedric Chaumont goto out; 1578b5816c88SCedric Chaumont 1579b0104773SPascal Brand dst += tmp_dlen; 1580b0104773SPascal Brand acc_dlen += tmp_dlen; 1581b0104773SPascal Brand 1582b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1583afc0c182SBogdan Liulko res = utee_authenc_dec_final(operation->state, 1584afc0c182SBogdan Liulko operation->buffer, 1585afc0c182SBogdan Liulko operation->buffer_offs, dst, 1586afc0c182SBogdan Liulko &tmp_dlen, tag, tagLen); 1587afc0c182SBogdan Liulko } else { 1588afc0c182SBogdan Liulko res = utee_authenc_dec_final(operation->state, srcData, 1589afc0c182SBogdan Liulko srcLen, dst, &tmp_dlen, 1590b5816c88SCedric Chaumont tag, tagLen); 1591afc0c182SBogdan Liulko } 1592b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1593b5816c88SCedric Chaumont goto out; 1594b5816c88SCedric Chaumont 1595b0104773SPascal Brand /* Supplied tagLen should match what we initiated with */ 1596b5816c88SCedric Chaumont if (tagLen != operation->ae_tag_len) 1597b0104773SPascal Brand res = TEE_ERROR_MAC_INVALID; 1598b0104773SPascal Brand 1599b0104773SPascal Brand acc_dlen += tmp_dlen; 1600b0104773SPascal Brand *destLen = acc_dlen; 1601642a1607SCedric Chaumont 1602b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1603b5816c88SCedric Chaumont 1604642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1605642a1607SCedric Chaumont 1606b5816c88SCedric Chaumont out: 1607b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1608b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER && 1609b5816c88SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 1610b5816c88SCedric Chaumont TEE_Panic(res); 1611b0104773SPascal Brand 1612b0104773SPascal Brand return res; 1613b0104773SPascal Brand } 1614b0104773SPascal Brand 1615b0104773SPascal Brand /* Cryptographic Operations API - Asymmetric Functions */ 1616b0104773SPascal Brand 161712e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation, 16188f07fe6fSJerome Forissier const TEE_Attribute *params, 16198f07fe6fSJerome Forissier uint32_t paramCount, const void *srcData, 162079a3c601SCedric Chaumont uint32_t srcLen, void *destData, 162179a3c601SCedric Chaumont uint32_t *destLen) 1622b0104773SPascal Brand { 1623b0104773SPascal Brand TEE_Result res; 1624e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1625e86f1266SJens Wiklander uint64_t dl; 1626b0104773SPascal Brand 162712e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1628b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 1629b0104773SPascal Brand TEE_Panic(0); 163012e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1631b0104773SPascal Brand TEE_Panic(0); 163212e66b6fSCedric Chaumont if (!operation->key1) 1633b0104773SPascal Brand TEE_Panic(0); 163412e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 163512e66b6fSCedric Chaumont TEE_Panic(0); 163612e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_ENCRYPT) 1637b0104773SPascal Brand TEE_Panic(0); 1638b0104773SPascal Brand 1639e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1640e86f1266SJens Wiklander dl = *destLen; 1641e86f1266SJens Wiklander res = utee_asymm_operate(operation->state, ua, paramCount, srcData, 1642e86f1266SJens Wiklander srcLen, destData, &dl); 1643e86f1266SJens Wiklander *destLen = dl; 164412e66b6fSCedric Chaumont 16458844ebfcSPascal Brand if (res != TEE_SUCCESS && 16468844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 16478844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1648b0104773SPascal Brand TEE_Panic(res); 164912e66b6fSCedric Chaumont 1650b0104773SPascal Brand return res; 1651b0104773SPascal Brand } 1652b0104773SPascal Brand 165312e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation, 16548f07fe6fSJerome Forissier const TEE_Attribute *params, 16558f07fe6fSJerome Forissier uint32_t paramCount, const void *srcData, 165679a3c601SCedric Chaumont uint32_t srcLen, void *destData, 165779a3c601SCedric Chaumont uint32_t *destLen) 1658b0104773SPascal Brand { 1659b0104773SPascal Brand TEE_Result res; 1660e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1661e86f1266SJens Wiklander uint64_t dl; 1662b0104773SPascal Brand 166312e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1664b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 1665b0104773SPascal Brand TEE_Panic(0); 166612e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1667b0104773SPascal Brand TEE_Panic(0); 166812e66b6fSCedric Chaumont if (!operation->key1) 1669b0104773SPascal Brand TEE_Panic(0); 167012e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 167112e66b6fSCedric Chaumont TEE_Panic(0); 167212e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_DECRYPT) 1673b0104773SPascal Brand TEE_Panic(0); 1674b0104773SPascal Brand 1675e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1676e86f1266SJens Wiklander dl = *destLen; 1677e86f1266SJens Wiklander res = utee_asymm_operate(operation->state, ua, paramCount, srcData, 1678e86f1266SJens Wiklander srcLen, destData, &dl); 1679e86f1266SJens Wiklander *destLen = dl; 168012e66b6fSCedric Chaumont 16818844ebfcSPascal Brand if (res != TEE_SUCCESS && 16828844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 16838844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1684b0104773SPascal Brand TEE_Panic(res); 168512e66b6fSCedric Chaumont 1686b0104773SPascal Brand return res; 1687b0104773SPascal Brand } 1688b0104773SPascal Brand 168912e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation, 16908f07fe6fSJerome Forissier const TEE_Attribute *params, 16918f07fe6fSJerome Forissier uint32_t paramCount, const void *digest, 169279a3c601SCedric Chaumont uint32_t digestLen, void *signature, 169379a3c601SCedric Chaumont uint32_t *signatureLen) 1694b0104773SPascal Brand { 1695b0104773SPascal Brand TEE_Result res; 1696e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1697e86f1266SJens Wiklander uint64_t sl; 1698b0104773SPascal Brand 169912e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || 170012e66b6fSCedric Chaumont (digest == NULL && digestLen != 0) || 1701b0104773SPascal Brand signature == NULL || signatureLen == NULL) 1702b0104773SPascal Brand TEE_Panic(0); 170312e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1704b0104773SPascal Brand TEE_Panic(0); 170512e66b6fSCedric Chaumont if (!operation->key1) 1706b0104773SPascal Brand TEE_Panic(0); 170712e66b6fSCedric Chaumont if (operation->info.operationClass != 170812e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 170912e66b6fSCedric Chaumont TEE_Panic(0); 171012e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_SIGN) 1711b0104773SPascal Brand TEE_Panic(0); 1712b0104773SPascal Brand 1713e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1714e86f1266SJens Wiklander sl = *signatureLen; 1715e86f1266SJens Wiklander res = utee_asymm_operate(operation->state, ua, paramCount, digest, 1716e86f1266SJens Wiklander digestLen, signature, &sl); 1717e86f1266SJens Wiklander *signatureLen = sl; 171812e66b6fSCedric Chaumont 1719b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER) 1720b0104773SPascal Brand TEE_Panic(res); 172112e66b6fSCedric Chaumont 1722b0104773SPascal Brand return res; 1723b0104773SPascal Brand } 1724b0104773SPascal Brand 172512e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation, 17268f07fe6fSJerome Forissier const TEE_Attribute *params, 17278f07fe6fSJerome Forissier uint32_t paramCount, const void *digest, 17288f07fe6fSJerome Forissier uint32_t digestLen, 17298f07fe6fSJerome Forissier const void *signature, 173079a3c601SCedric Chaumont uint32_t signatureLen) 1731b0104773SPascal Brand { 1732b0104773SPascal Brand TEE_Result res; 1733e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1734b0104773SPascal Brand 173512e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || 173612e66b6fSCedric Chaumont (digest == NULL && digestLen != 0) || 1737b0104773SPascal Brand (signature == NULL && signatureLen != 0)) 1738b0104773SPascal Brand TEE_Panic(0); 173912e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1740b0104773SPascal Brand TEE_Panic(0); 174112e66b6fSCedric Chaumont if (!operation->key1) 1742b0104773SPascal Brand TEE_Panic(0); 174312e66b6fSCedric Chaumont if (operation->info.operationClass != 174412e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 174512e66b6fSCedric Chaumont TEE_Panic(0); 174612e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_VERIFY) 1747b0104773SPascal Brand TEE_Panic(0); 1748b0104773SPascal Brand 1749e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1750e86f1266SJens Wiklander res = utee_asymm_verify(operation->state, ua, paramCount, digest, 175112e66b6fSCedric Chaumont digestLen, signature, signatureLen); 175212e66b6fSCedric Chaumont 1753b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID) 1754b0104773SPascal Brand TEE_Panic(res); 175512e66b6fSCedric Chaumont 1756b0104773SPascal Brand return res; 1757b0104773SPascal Brand } 1758b0104773SPascal Brand 1759b0104773SPascal Brand /* Cryptographic Operations API - Key Derivation Functions */ 1760b0104773SPascal Brand 1761b0104773SPascal Brand void TEE_DeriveKey(TEE_OperationHandle operation, 1762b0104773SPascal Brand const TEE_Attribute *params, uint32_t paramCount, 1763b0104773SPascal Brand TEE_ObjectHandle derivedKey) 1764b0104773SPascal Brand { 1765b0104773SPascal Brand TEE_Result res; 1766b0104773SPascal Brand TEE_ObjectInfo key_info; 1767e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1768b0104773SPascal Brand 1769b0104773SPascal Brand if (operation == TEE_HANDLE_NULL || derivedKey == 0) 1770b0104773SPascal Brand TEE_Panic(0); 177184fa9467SCedric Chaumont if (params == NULL && paramCount != 0) 1772b0104773SPascal Brand TEE_Panic(0); 17738854d3c6SJerome Forissier if (TEE_ALG_GET_CLASS(operation->info.algorithm) != 17748854d3c6SJerome Forissier TEE_OPERATION_KEY_DERIVATION) 1775b0104773SPascal Brand TEE_Panic(0); 1776b0104773SPascal Brand 1777b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION) 1778b0104773SPascal Brand TEE_Panic(0); 177984fa9467SCedric Chaumont if (!operation->key1) 178084fa9467SCedric Chaumont TEE_Panic(0); 1781b0104773SPascal Brand if (operation->info.mode != TEE_MODE_DERIVE) 1782b0104773SPascal Brand TEE_Panic(0); 1783b0104773SPascal Brand if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0) 1784b0104773SPascal Brand TEE_Panic(0); 1785b0104773SPascal Brand 1786e86f1266SJens Wiklander res = utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info); 1787b0104773SPascal Brand if (res != TEE_SUCCESS) 1788b36311adSJerome Forissier TEE_Panic(res); 1789b0104773SPascal Brand 1790b0104773SPascal Brand if (key_info.objectType != TEE_TYPE_GENERIC_SECRET) 1791b0104773SPascal Brand TEE_Panic(0); 1792b0104773SPascal Brand if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1793b0104773SPascal Brand TEE_Panic(0); 1794b0104773SPascal Brand 1795e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1796e86f1266SJens Wiklander res = utee_cryp_derive_key(operation->state, ua, paramCount, 1797e86f1266SJens Wiklander (unsigned long)derivedKey); 1798b0104773SPascal Brand if (res != TEE_SUCCESS) 1799b0104773SPascal Brand TEE_Panic(res); 1800b0104773SPascal Brand } 1801b0104773SPascal Brand 1802b0104773SPascal Brand /* Cryptographic Operations API - Random Number Generation Functions */ 1803b0104773SPascal Brand 180479a3c601SCedric Chaumont void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen) 1805b0104773SPascal Brand { 1806b0104773SPascal Brand TEE_Result res; 1807b0104773SPascal Brand 1808b0104773SPascal Brand res = utee_cryp_random_number_generate(randomBuffer, randomBufferLen); 1809b0104773SPascal Brand if (res != TEE_SUCCESS) 1810b0104773SPascal Brand TEE_Panic(res); 1811b0104773SPascal Brand } 1812433c4257SJens Wiklander 1813433c4257SJens Wiklander int rand(void) 1814433c4257SJens Wiklander { 1815433c4257SJens Wiklander int rc; 1816433c4257SJens Wiklander 1817433c4257SJens Wiklander TEE_GenerateRandom(&rc, sizeof(rc)); 1818433c4257SJens Wiklander 1819433c4257SJens Wiklander /* 1820433c4257SJens Wiklander * RAND_MAX is the larges int, INT_MAX which is all bits but the 1821433c4257SJens Wiklander * highest bit set. 1822433c4257SJens Wiklander */ 1823433c4257SJens Wiklander return rc & RAND_MAX; 1824433c4257SJens Wiklander } 1825