1b0104773SPascal Brand /* 2b0104773SPascal Brand * Copyright (c) 2014, STMicroelectronics International N.V. 3b0104773SPascal Brand * All rights reserved. 4b0104773SPascal Brand * 5b0104773SPascal Brand * Redistribution and use in source and binary forms, with or without 6b0104773SPascal Brand * modification, are permitted provided that the following conditions are met: 7b0104773SPascal Brand * 8b0104773SPascal Brand * 1. Redistributions of source code must retain the above copyright notice, 9b0104773SPascal Brand * this list of conditions and the following disclaimer. 10b0104773SPascal Brand * 11b0104773SPascal Brand * 2. Redistributions in binary form must reproduce the above copyright notice, 12b0104773SPascal Brand * this list of conditions and the following disclaimer in the documentation 13b0104773SPascal Brand * and/or other materials provided with the distribution. 14b0104773SPascal Brand * 15b0104773SPascal Brand * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16b0104773SPascal Brand * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17b0104773SPascal Brand * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18b0104773SPascal Brand * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19b0104773SPascal Brand * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20b0104773SPascal Brand * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21b0104773SPascal Brand * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22b0104773SPascal Brand * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23b0104773SPascal Brand * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24b0104773SPascal Brand * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25b0104773SPascal Brand * POSSIBILITY OF SUCH DAMAGE. 26b0104773SPascal Brand */ 27b0104773SPascal Brand #include <stdlib.h> 28b0104773SPascal Brand #include <string.h> 29b796ebf3SJerome Forissier #include <string_ext.h> 30b0104773SPascal Brand 31b0104773SPascal Brand #include <tee_api.h> 328854d3c6SJerome Forissier #include <tee_api_defines_extensions.h> 33b0104773SPascal Brand #include <tee_internal_api_extensions.h> 34b0104773SPascal Brand #include <utee_syscalls.h> 35b0104773SPascal Brand #include <utee_defines.h> 36fc26c92aSJens Wiklander #include <util.h> 37b0104773SPascal Brand 38b0104773SPascal Brand struct __TEE_OperationHandle { 39b0104773SPascal Brand TEE_OperationInfo info; 40b0104773SPascal Brand TEE_ObjectHandle key1; 41b0104773SPascal Brand TEE_ObjectHandle key2; 42*642a1607SCedric Chaumont uint32_t operationState;/* Operation state : INITIAL or ACTIVE */ 43b0104773SPascal Brand uint8_t *buffer; /* buffer to collect complete blocks */ 44b0104773SPascal Brand bool buffer_two_blocks; /* True if two blocks need to be buffered */ 45b0104773SPascal Brand size_t block_size; /* Block size of cipher */ 46b0104773SPascal Brand size_t buffer_offs; /* Offset in buffer */ 47b0104773SPascal Brand uint32_t state; /* Handle to state in TEE Core */ 48b0104773SPascal Brand uint32_t ae_tag_len; /* 49b0104773SPascal Brand * tag_len in bytes for AE operation else unused 50b0104773SPascal Brand */ 51b0104773SPascal Brand }; 52b0104773SPascal Brand 53b0104773SPascal Brand /* Cryptographic Operations API - Generic Operation Functions */ 54b0104773SPascal Brand 55b0104773SPascal Brand TEE_Result TEE_AllocateOperation(TEE_OperationHandle *operation, 56b0104773SPascal Brand uint32_t algorithm, uint32_t mode, 57b0104773SPascal Brand uint32_t maxKeySize) 58b0104773SPascal Brand { 59b0104773SPascal Brand TEE_Result res; 60b0104773SPascal Brand TEE_OperationHandle op = TEE_HANDLE_NULL; 61b0104773SPascal Brand uint32_t handle_state = 0; 62b0104773SPascal Brand size_t block_size = 1; 63b0104773SPascal Brand uint32_t req_key_usage; 64b0104773SPascal Brand bool with_private_key = false; 65b0104773SPascal Brand bool buffer_two_blocks = false; 66b0104773SPascal Brand 679b52c538SCedric Chaumont if (!operation) 68b0104773SPascal Brand TEE_Panic(0); 69b0104773SPascal Brand 70b0104773SPascal Brand if (algorithm == TEE_ALG_AES_XTS) 71b0104773SPascal Brand handle_state = TEE_HANDLE_FLAG_EXPECT_TWO_KEYS; 72b0104773SPascal Brand 73218d9055SCedric Chaumont /* Check algorithm max key size */ 74218d9055SCedric Chaumont switch (algorithm) { 75218d9055SCedric Chaumont case TEE_ALG_DSA_SHA1: 76218d9055SCedric Chaumont if (maxKeySize < 512) 77218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 78218d9055SCedric Chaumont if (maxKeySize > 1024) 79218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 80218d9055SCedric Chaumont if (maxKeySize % 64 != 0) 81218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 82218d9055SCedric Chaumont break; 83218d9055SCedric Chaumont 84218d9055SCedric Chaumont case TEE_ALG_DSA_SHA224: 85218d9055SCedric Chaumont if (maxKeySize != 2048) 86218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 87218d9055SCedric Chaumont break; 88218d9055SCedric Chaumont 89218d9055SCedric Chaumont case TEE_ALG_DSA_SHA256: 90218d9055SCedric Chaumont if (maxKeySize != 2048 && maxKeySize != 3072) 91218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 92218d9055SCedric Chaumont break; 93218d9055SCedric Chaumont 941220586eSCedric Chaumont case TEE_ALG_ECDSA_P192: 951220586eSCedric Chaumont case TEE_ALG_ECDH_P192: 961220586eSCedric Chaumont if (maxKeySize != 192) 971220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 981220586eSCedric Chaumont break; 991220586eSCedric Chaumont 1001220586eSCedric Chaumont case TEE_ALG_ECDSA_P224: 1011220586eSCedric Chaumont case TEE_ALG_ECDH_P224: 1021220586eSCedric Chaumont if (maxKeySize != 224) 1031220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 1041220586eSCedric Chaumont break; 1051220586eSCedric Chaumont 1061220586eSCedric Chaumont case TEE_ALG_ECDSA_P256: 1071220586eSCedric Chaumont case TEE_ALG_ECDH_P256: 1081220586eSCedric Chaumont if (maxKeySize != 256) 1091220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 1101220586eSCedric Chaumont break; 1111220586eSCedric Chaumont 1121220586eSCedric Chaumont case TEE_ALG_ECDSA_P384: 1131220586eSCedric Chaumont case TEE_ALG_ECDH_P384: 1141220586eSCedric Chaumont if (maxKeySize != 384) 1151220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 1161220586eSCedric Chaumont break; 1171220586eSCedric Chaumont 1181220586eSCedric Chaumont case TEE_ALG_ECDSA_P521: 1191220586eSCedric Chaumont case TEE_ALG_ECDH_P521: 1201220586eSCedric Chaumont if (maxKeySize != 521) 1211220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 1221220586eSCedric Chaumont break; 1231220586eSCedric Chaumont 124218d9055SCedric Chaumont default: 125218d9055SCedric Chaumont break; 126218d9055SCedric Chaumont } 127218d9055SCedric Chaumont 128218d9055SCedric Chaumont /* Check algorithm mode */ 129b0104773SPascal Brand switch (algorithm) { 130b0104773SPascal Brand case TEE_ALG_AES_CTS: 131b0104773SPascal Brand case TEE_ALG_AES_XTS: 132b0104773SPascal Brand buffer_two_blocks = true; 133b0104773SPascal Brand /*FALLTHROUGH*/ case TEE_ALG_AES_ECB_NOPAD: 134b0104773SPascal Brand case TEE_ALG_AES_CBC_NOPAD: 135b0104773SPascal Brand case TEE_ALG_AES_CTR: 136b0104773SPascal Brand case TEE_ALG_AES_CCM: 137b0104773SPascal Brand case TEE_ALG_AES_GCM: 138b0104773SPascal Brand case TEE_ALG_DES_ECB_NOPAD: 139b0104773SPascal Brand case TEE_ALG_DES_CBC_NOPAD: 140b0104773SPascal Brand case TEE_ALG_DES3_ECB_NOPAD: 141b0104773SPascal Brand case TEE_ALG_DES3_CBC_NOPAD: 142b0104773SPascal Brand if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES) 143b0104773SPascal Brand block_size = TEE_AES_BLOCK_SIZE; 144b0104773SPascal Brand else 145b0104773SPascal Brand block_size = TEE_DES_BLOCK_SIZE; 146b0104773SPascal Brand 147b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) 148b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT; 149b0104773SPascal Brand else if (mode == TEE_MODE_DECRYPT) 150b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT; 151b0104773SPascal Brand else 152b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 153b0104773SPascal Brand break; 154b0104773SPascal Brand 155b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_MD5: 156b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1: 157b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224: 158b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256: 159b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384: 160b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512: 161b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1: 162b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224: 163b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256: 164b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384: 165b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512: 166b0104773SPascal Brand case TEE_ALG_DSA_SHA1: 167218d9055SCedric Chaumont case TEE_ALG_DSA_SHA224: 168218d9055SCedric Chaumont case TEE_ALG_DSA_SHA256: 1691220586eSCedric Chaumont case TEE_ALG_ECDSA_P192: 1701220586eSCedric Chaumont case TEE_ALG_ECDSA_P224: 1711220586eSCedric Chaumont case TEE_ALG_ECDSA_P256: 1721220586eSCedric Chaumont case TEE_ALG_ECDSA_P384: 1731220586eSCedric Chaumont case TEE_ALG_ECDSA_P521: 174b0104773SPascal Brand if (mode == TEE_MODE_SIGN) { 175b0104773SPascal Brand with_private_key = true; 176b0104773SPascal Brand req_key_usage = TEE_USAGE_SIGN; 177b0104773SPascal Brand } else if (mode == TEE_MODE_VERIFY) { 178b0104773SPascal Brand req_key_usage = TEE_USAGE_VERIFY; 179b0104773SPascal Brand } else { 180b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 181b0104773SPascal Brand } 182b0104773SPascal Brand break; 183b0104773SPascal Brand 184b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_V1_5: 185b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1: 186b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224: 187b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256: 188b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384: 189b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512: 190b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) { 191b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT; 192b0104773SPascal Brand } else if (mode == TEE_MODE_DECRYPT) { 193b0104773SPascal Brand with_private_key = true; 194b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT; 195b0104773SPascal Brand } else { 196b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 197b0104773SPascal Brand } 198b0104773SPascal Brand break; 199b0104773SPascal Brand 200b0104773SPascal Brand case TEE_ALG_RSA_NOPAD: 201b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) { 202b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY; 203b0104773SPascal Brand } else if (mode == TEE_MODE_DECRYPT) { 204b0104773SPascal Brand with_private_key = true; 205b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT | TEE_USAGE_SIGN; 206b0104773SPascal Brand } else { 207b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 208b0104773SPascal Brand } 209b0104773SPascal Brand break; 210b0104773SPascal Brand 211b0104773SPascal Brand case TEE_ALG_DH_DERIVE_SHARED_SECRET: 2121220586eSCedric Chaumont case TEE_ALG_ECDH_P192: 2131220586eSCedric Chaumont case TEE_ALG_ECDH_P224: 2141220586eSCedric Chaumont case TEE_ALG_ECDH_P256: 2151220586eSCedric Chaumont case TEE_ALG_ECDH_P384: 2161220586eSCedric Chaumont case TEE_ALG_ECDH_P521: 217cdb198a7SJerome Forissier case TEE_ALG_HKDF_MD5_DERIVE_KEY: 218cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA1_DERIVE_KEY: 219cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA224_DERIVE_KEY: 220cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA256_DERIVE_KEY: 221cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA384_DERIVE_KEY: 222cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA512_DERIVE_KEY: 2238854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA1_DERIVE_KEY: 2248854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA224_DERIVE_KEY: 2258854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA256_DERIVE_KEY: 2268854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA384_DERIVE_KEY: 2278854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA512_DERIVE_KEY: 2280f2293b7SJerome Forissier case TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY: 229b0104773SPascal Brand if (mode != TEE_MODE_DERIVE) 230b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 231b0104773SPascal Brand with_private_key = true; 232b0104773SPascal Brand req_key_usage = TEE_USAGE_DERIVE; 233b0104773SPascal Brand break; 234b0104773SPascal Brand 235b0104773SPascal Brand case TEE_ALG_MD5: 236b0104773SPascal Brand case TEE_ALG_SHA1: 237b0104773SPascal Brand case TEE_ALG_SHA224: 238b0104773SPascal Brand case TEE_ALG_SHA256: 239b0104773SPascal Brand case TEE_ALG_SHA384: 240b0104773SPascal Brand case TEE_ALG_SHA512: 241b0104773SPascal Brand if (mode != TEE_MODE_DIGEST) 242b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 24305304565SCedric Chaumont /* v1.1: flags always set for digest operations */ 244b0104773SPascal Brand handle_state |= TEE_HANDLE_FLAG_KEY_SET; 245b0104773SPascal Brand req_key_usage = 0; 246b0104773SPascal Brand break; 247b0104773SPascal Brand 248b0104773SPascal Brand case TEE_ALG_DES_CBC_MAC_NOPAD: 249b0104773SPascal Brand case TEE_ALG_AES_CBC_MAC_NOPAD: 250b0104773SPascal Brand case TEE_ALG_AES_CBC_MAC_PKCS5: 251b0104773SPascal Brand case TEE_ALG_AES_CMAC: 252b0104773SPascal Brand case TEE_ALG_DES_CBC_MAC_PKCS5: 253b0104773SPascal Brand case TEE_ALG_DES3_CBC_MAC_NOPAD: 254b0104773SPascal Brand case TEE_ALG_DES3_CBC_MAC_PKCS5: 255b0104773SPascal Brand case TEE_ALG_HMAC_MD5: 256b0104773SPascal Brand case TEE_ALG_HMAC_SHA1: 257b0104773SPascal Brand case TEE_ALG_HMAC_SHA224: 258b0104773SPascal Brand case TEE_ALG_HMAC_SHA256: 259b0104773SPascal Brand case TEE_ALG_HMAC_SHA384: 260b0104773SPascal Brand case TEE_ALG_HMAC_SHA512: 261b0104773SPascal Brand if (mode != TEE_MODE_MAC) 262b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 263b0104773SPascal Brand req_key_usage = TEE_USAGE_MAC; 264b0104773SPascal Brand break; 265b0104773SPascal Brand 266b0104773SPascal Brand default: 267b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 268b0104773SPascal Brand } 269b0104773SPascal Brand 270b0104773SPascal Brand op = TEE_Malloc(sizeof(*op), 0); 2719b52c538SCedric Chaumont if (!op) 272b0104773SPascal Brand return TEE_ERROR_OUT_OF_MEMORY; 273b0104773SPascal Brand 274b0104773SPascal Brand op->info.algorithm = algorithm; 275b0104773SPascal Brand op->info.operationClass = TEE_ALG_GET_CLASS(algorithm); 276b0104773SPascal Brand op->info.mode = mode; 277b0104773SPascal Brand op->info.maxKeySize = maxKeySize; 278b0104773SPascal Brand op->info.requiredKeyUsage = req_key_usage; 279b0104773SPascal Brand op->info.handleState = handle_state; 280b0104773SPascal Brand 281b0104773SPascal Brand if (block_size > 1) { 282b0104773SPascal Brand size_t buffer_size = block_size; 283b0104773SPascal Brand 284b0104773SPascal Brand if (buffer_two_blocks) 285b0104773SPascal Brand buffer_size *= 2; 286b0104773SPascal Brand 2879b52c538SCedric Chaumont op->buffer = TEE_Malloc(buffer_size, 2889b52c538SCedric Chaumont TEE_USER_MEM_HINT_NO_FILL_ZERO); 289b0104773SPascal Brand if (op->buffer == NULL) { 290b0104773SPascal Brand res = TEE_ERROR_OUT_OF_MEMORY; 2919b52c538SCedric Chaumont goto err0; 292b0104773SPascal Brand } 293b0104773SPascal Brand } 294b0104773SPascal Brand op->block_size = block_size; 295b0104773SPascal Brand op->buffer_two_blocks = buffer_two_blocks; 296b0104773SPascal Brand 297b0104773SPascal Brand if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) { 298b0104773SPascal Brand uint32_t mks = maxKeySize; 299b0104773SPascal Brand TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm, 300b0104773SPascal Brand with_private_key); 301b0104773SPascal Brand 302b0104773SPascal Brand /* 303b0104773SPascal Brand * If two keys are expected the max key size is the sum of 304b0104773SPascal Brand * the size of both keys. 305b0104773SPascal Brand */ 306b0104773SPascal Brand if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) 307b0104773SPascal Brand mks /= 2; 308b0104773SPascal Brand 309b0104773SPascal Brand res = TEE_AllocateTransientObject(key_type, mks, &op->key1); 310b0104773SPascal Brand if (res != TEE_SUCCESS) 3119b52c538SCedric Chaumont goto err1; 312b0104773SPascal Brand 31305304565SCedric Chaumont if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) { 3149b52c538SCedric Chaumont res = TEE_AllocateTransientObject(key_type, mks, 315b0104773SPascal Brand &op->key2); 316b0104773SPascal Brand if (res != TEE_SUCCESS) 3179b52c538SCedric Chaumont goto err2; 318b0104773SPascal Brand } 319b0104773SPascal Brand } 320b0104773SPascal Brand 321b0104773SPascal Brand res = utee_cryp_state_alloc(algorithm, mode, (uint32_t) op->key1, 322b0104773SPascal Brand (uint32_t) op->key2, &op->state); 3239b52c538SCedric Chaumont if (res != TEE_SUCCESS) { 3249b52c538SCedric Chaumont if ((op->info.handleState & 3259b52c538SCedric Chaumont TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 0) 3269b52c538SCedric Chaumont goto err2; 3279b52c538SCedric Chaumont goto err1; 3289b52c538SCedric Chaumont } 329b0104773SPascal Brand 33005304565SCedric Chaumont /* 33105304565SCedric Chaumont * Initialize digest operations 33205304565SCedric Chaumont * Other multi-stage operations initialized w/ TEE_xxxInit functions 33305304565SCedric Chaumont * Non-applicable on asymmetric operations 33405304565SCedric Chaumont */ 33505304565SCedric Chaumont if (TEE_ALG_GET_CLASS(algorithm) == TEE_OPERATION_DIGEST) { 33605304565SCedric Chaumont res = utee_hash_init(op->state, NULL, 0); 33705304565SCedric Chaumont if (res != TEE_SUCCESS) 33805304565SCedric Chaumont goto err0; 33905304565SCedric Chaumont /* v1.1: flags always set for digest operations */ 34005304565SCedric Chaumont op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 34105304565SCedric Chaumont } 34205304565SCedric Chaumont 343*642a1607SCedric Chaumont op->operationState = TEE_OPERATION_STATE_INITIAL; 344*642a1607SCedric Chaumont 345b0104773SPascal Brand *operation = op; 3469b52c538SCedric Chaumont goto out; 347b0104773SPascal Brand 3489b52c538SCedric Chaumont err2: 349b0104773SPascal Brand TEE_FreeTransientObject(op->key2); 3509b52c538SCedric Chaumont err1: 3519b52c538SCedric Chaumont TEE_FreeTransientObject(op->key1); 3529b52c538SCedric Chaumont err0: 353b0104773SPascal Brand TEE_FreeOperation(op); 354b0104773SPascal Brand 3559b52c538SCedric Chaumont if (res != TEE_SUCCESS && 3569b52c538SCedric Chaumont res != TEE_ERROR_OUT_OF_MEMORY && 3579b52c538SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 3589b52c538SCedric Chaumont TEE_Panic(0); 3599b52c538SCedric Chaumont out: 360b0104773SPascal Brand return res; 361b0104773SPascal Brand } 362b0104773SPascal Brand 363b0104773SPascal Brand void TEE_FreeOperation(TEE_OperationHandle operation) 364b0104773SPascal Brand { 365e889e80bSCedric Chaumont TEE_Result res; 366e889e80bSCedric Chaumont 367e889e80bSCedric Chaumont if (operation == TEE_HANDLE_NULL) 368e889e80bSCedric Chaumont TEE_Panic(0); 369e889e80bSCedric Chaumont 370b0104773SPascal Brand /* 371b0104773SPascal Brand * Note that keys should not be freed here, since they are 372b0104773SPascal Brand * claimed by the operation they will be freed by 373b0104773SPascal Brand * utee_cryp_state_free(). 374b0104773SPascal Brand */ 375e889e80bSCedric Chaumont res = utee_cryp_state_free(operation->state); 376e889e80bSCedric Chaumont if (res != TEE_SUCCESS) 377e889e80bSCedric Chaumont TEE_Panic(0); 378e889e80bSCedric Chaumont 379b0104773SPascal Brand TEE_Free(operation->buffer); 380b0104773SPascal Brand TEE_Free(operation); 381b0104773SPascal Brand } 382b0104773SPascal Brand 383b0104773SPascal Brand void TEE_GetOperationInfo(TEE_OperationHandle operation, 384b0104773SPascal Brand TEE_OperationInfo *operationInfo) 385b0104773SPascal Brand { 386b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 387b0104773SPascal Brand TEE_Panic(0); 388b0104773SPascal Brand 38905304565SCedric Chaumont if (!operationInfo) 390b0104773SPascal Brand TEE_Panic(0); 391b0104773SPascal Brand 392b0104773SPascal Brand *operationInfo = operation->info; 393b0104773SPascal Brand } 394b0104773SPascal Brand 39505304565SCedric Chaumont TEE_Result TEE_GetOperationInfoMultiple(TEE_OperationHandle operation, 39605304565SCedric Chaumont TEE_OperationInfoMultiple *operationInfoMultiple, 39705304565SCedric Chaumont uint32_t *operationSize) 39805304565SCedric Chaumont { 39905304565SCedric Chaumont TEE_Result res = TEE_SUCCESS; 40005304565SCedric Chaumont TEE_ObjectInfo key_info1; 40105304565SCedric Chaumont TEE_ObjectInfo key_info2; 40205304565SCedric Chaumont uint32_t num_of_keys; 40305304565SCedric Chaumont size_t n; 40405304565SCedric Chaumont 40505304565SCedric Chaumont if (operation == TEE_HANDLE_NULL) { 40605304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 40705304565SCedric Chaumont goto out; 40805304565SCedric Chaumont } 40905304565SCedric Chaumont 41005304565SCedric Chaumont if (!operationInfoMultiple) { 41105304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 41205304565SCedric Chaumont goto out; 41305304565SCedric Chaumont } 41405304565SCedric Chaumont 41505304565SCedric Chaumont if (!operationSize) { 41605304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 41705304565SCedric Chaumont goto out; 41805304565SCedric Chaumont } 41905304565SCedric Chaumont 42005304565SCedric Chaumont num_of_keys = (*operationSize-sizeof(TEE_OperationInfoMultiple))/ 42105304565SCedric Chaumont sizeof(TEE_OperationInfoKey); 42205304565SCedric Chaumont 42305304565SCedric Chaumont if (num_of_keys > 2) { 42405304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 42505304565SCedric Chaumont goto out; 42605304565SCedric Chaumont } 42705304565SCedric Chaumont 42805304565SCedric Chaumont /* Two keys flag (TEE_ALG_AES_XTS only) */ 42905304565SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 43005304565SCedric Chaumont 0 && 43105304565SCedric Chaumont (num_of_keys != 2)) { 43205304565SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 43305304565SCedric Chaumont goto out; 43405304565SCedric Chaumont } 43505304565SCedric Chaumont 43605304565SCedric Chaumont /* Clear */ 43705304565SCedric Chaumont for (n = 0; n < num_of_keys; n++) { 43805304565SCedric Chaumont operationInfoMultiple->keyInformation[n].keySize = 0; 43905304565SCedric Chaumont operationInfoMultiple->keyInformation[n].requiredKeyUsage = 0; 44005304565SCedric Chaumont } 44105304565SCedric Chaumont 44205304565SCedric Chaumont if (num_of_keys == 2) { 44305304565SCedric Chaumont res = TEE_GetObjectInfo1(operation->key2, &key_info2); 44405304565SCedric Chaumont /* Key2 is not a valid handle */ 44505304565SCedric Chaumont if (res != TEE_SUCCESS) 44605304565SCedric Chaumont goto out; 44705304565SCedric Chaumont 44805304565SCedric Chaumont operationInfoMultiple->keyInformation[1].keySize = 44905304565SCedric Chaumont key_info2.keySize; 45005304565SCedric Chaumont operationInfoMultiple->keyInformation[1].requiredKeyUsage = 45105304565SCedric Chaumont operation->info.requiredKeyUsage; 45205304565SCedric Chaumont } 45305304565SCedric Chaumont 45405304565SCedric Chaumont if (num_of_keys >= 1) { 45505304565SCedric Chaumont res = TEE_GetObjectInfo1(operation->key1, &key_info1); 45605304565SCedric Chaumont /* Key1 is not a valid handle */ 45705304565SCedric Chaumont if (res != TEE_SUCCESS) { 45805304565SCedric Chaumont if (num_of_keys == 2) { 45905304565SCedric Chaumont operationInfoMultiple->keyInformation[1]. 46005304565SCedric Chaumont keySize = 0; 46105304565SCedric Chaumont operationInfoMultiple->keyInformation[1]. 46205304565SCedric Chaumont requiredKeyUsage = 0; 46305304565SCedric Chaumont } 46405304565SCedric Chaumont goto out; 46505304565SCedric Chaumont } 46605304565SCedric Chaumont 46705304565SCedric Chaumont operationInfoMultiple->keyInformation[0].keySize = 46805304565SCedric Chaumont key_info1.keySize; 46905304565SCedric Chaumont operationInfoMultiple->keyInformation[0].requiredKeyUsage = 47005304565SCedric Chaumont operation->info.requiredKeyUsage; 47105304565SCedric Chaumont } 47205304565SCedric Chaumont 47305304565SCedric Chaumont /* No key */ 47405304565SCedric Chaumont operationInfoMultiple->algorithm = operation->info.algorithm; 47505304565SCedric Chaumont operationInfoMultiple->operationClass = operation->info.operationClass; 47605304565SCedric Chaumont operationInfoMultiple->mode = operation->info.mode; 47705304565SCedric Chaumont operationInfoMultiple->digestLength = operation->info.digestLength; 47805304565SCedric Chaumont operationInfoMultiple->maxKeySize = operation->info.maxKeySize; 47905304565SCedric Chaumont operationInfoMultiple->handleState = operation->info.handleState; 48005304565SCedric Chaumont operationInfoMultiple->operationState = operation->operationState; 48105304565SCedric Chaumont operationInfoMultiple->numberOfKeys = num_of_keys; 48205304565SCedric Chaumont 48305304565SCedric Chaumont out: 48405304565SCedric Chaumont if (res != TEE_SUCCESS && 48505304565SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 48605304565SCedric Chaumont TEE_Panic(0); 48705304565SCedric Chaumont 48805304565SCedric Chaumont return res; 48905304565SCedric Chaumont } 49005304565SCedric Chaumont 491b0104773SPascal Brand void TEE_ResetOperation(TEE_OperationHandle operation) 492b0104773SPascal Brand { 493b0104773SPascal Brand TEE_Result res; 494b0104773SPascal Brand 495b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 496b0104773SPascal Brand TEE_Panic(0); 497bf80076aSCedric Chaumont 498*642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET)) 499bf80076aSCedric Chaumont TEE_Panic(0); 500bf80076aSCedric Chaumont 501*642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 502*642a1607SCedric Chaumont 503b0104773SPascal Brand if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 504b0104773SPascal Brand res = utee_hash_init(operation->state, NULL, 0); 505b0104773SPascal Brand if (res != TEE_SUCCESS) 506b0104773SPascal Brand TEE_Panic(res); 50705304565SCedric Chaumont operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 50805304565SCedric Chaumont } else { 509b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 510b0104773SPascal Brand } 51105304565SCedric Chaumont } 512b0104773SPascal Brand 513b0104773SPascal Brand TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation, 514b0104773SPascal Brand TEE_ObjectHandle key) 515b0104773SPascal Brand { 5167583c59eSCedric Chaumont TEE_Result res; 517b0104773SPascal Brand uint32_t key_size = 0; 518b0104773SPascal Brand TEE_ObjectInfo key_info; 519b0104773SPascal Brand 520a57c1e2eSCedric Chaumont if (operation == TEE_HANDLE_NULL) { 521a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 522a57c1e2eSCedric Chaumont goto out; 523a57c1e2eSCedric Chaumont } 524a57c1e2eSCedric Chaumont 525*642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 526*642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 527*642a1607SCedric Chaumont goto out; 528*642a1607SCedric Chaumont } 529*642a1607SCedric Chaumont 530a57c1e2eSCedric Chaumont if (key == TEE_HANDLE_NULL) { 531a57c1e2eSCedric Chaumont /* Operation key cleared */ 532a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key1); 533a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 534a57c1e2eSCedric Chaumont goto out; 535a57c1e2eSCedric Chaumont } 536a57c1e2eSCedric Chaumont 537a57c1e2eSCedric Chaumont /* No key for digest operation */ 538a57c1e2eSCedric Chaumont if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 539a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 540a57c1e2eSCedric Chaumont goto out; 541a57c1e2eSCedric Chaumont } 542a57c1e2eSCedric Chaumont 543a57c1e2eSCedric Chaumont /* Two keys flag not expected (TEE_ALG_AES_XTS excluded) */ 544a57c1e2eSCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 545a57c1e2eSCedric Chaumont 0) { 546a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 547a57c1e2eSCedric Chaumont goto out; 548a57c1e2eSCedric Chaumont } 549a57c1e2eSCedric Chaumont 5507583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key, &key_info); 551a57c1e2eSCedric Chaumont /* Key is not a valid handle */ 5527583c59eSCedric Chaumont if (res != TEE_SUCCESS) 553a57c1e2eSCedric Chaumont goto out; 5547583c59eSCedric Chaumont 555b0104773SPascal Brand /* Supplied key has to meet required usage */ 556b0104773SPascal Brand if ((key_info.objectUsage & operation->info.requiredKeyUsage) != 557b0104773SPascal Brand operation->info.requiredKeyUsage) { 558a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 559a57c1e2eSCedric Chaumont goto out; 560b0104773SPascal Brand } 561b0104773SPascal Brand 562a57c1e2eSCedric Chaumont if (operation->info.maxKeySize < key_info.keySize) { 563a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 564a57c1e2eSCedric Chaumont goto out; 565a57c1e2eSCedric Chaumont } 566b0104773SPascal Brand 5677583c59eSCedric Chaumont key_size = key_info.keySize; 568b0104773SPascal Brand 569b0104773SPascal Brand TEE_ResetTransientObject(operation->key1); 570b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 571b0104773SPascal Brand 5727583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key1, key); 5737583c59eSCedric Chaumont if (res != TEE_SUCCESS) 574a57c1e2eSCedric Chaumont goto out; 5757583c59eSCedric Chaumont 576b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 577b0104773SPascal Brand 578b0104773SPascal Brand operation->info.keySize = key_size; 579b0104773SPascal Brand 5807583c59eSCedric Chaumont out: 581a57c1e2eSCedric Chaumont if (res != TEE_SUCCESS && 582a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 583a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 584a57c1e2eSCedric Chaumont TEE_Panic(0); 585a57c1e2eSCedric Chaumont 586a57c1e2eSCedric Chaumont return res; 587b0104773SPascal Brand } 588b0104773SPascal Brand 589b0104773SPascal Brand TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation, 590b0104773SPascal Brand TEE_ObjectHandle key1, TEE_ObjectHandle key2) 591b0104773SPascal Brand { 5927583c59eSCedric Chaumont TEE_Result res; 593b0104773SPascal Brand uint32_t key_size = 0; 594b0104773SPascal Brand TEE_ObjectInfo key_info1; 595b0104773SPascal Brand TEE_ObjectInfo key_info2; 596b0104773SPascal Brand 597a57c1e2eSCedric Chaumont if (operation == TEE_HANDLE_NULL) { 598a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 599a57c1e2eSCedric Chaumont goto out; 600a57c1e2eSCedric Chaumont } 601a57c1e2eSCedric Chaumont 602*642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 603*642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 604*642a1607SCedric Chaumont goto out; 605*642a1607SCedric Chaumont } 606*642a1607SCedric Chaumont 607a57c1e2eSCedric Chaumont /* 608a57c1e2eSCedric Chaumont * Key1/Key2 and/or are not initialized and 609a57c1e2eSCedric Chaumont * Either both keys are NULL or both are not NULL 610a57c1e2eSCedric Chaumont */ 611a57c1e2eSCedric Chaumont if (key1 == TEE_HANDLE_NULL || key2 == TEE_HANDLE_NULL) { 612a57c1e2eSCedric Chaumont /* Clear operation key1 (if needed) */ 613a57c1e2eSCedric Chaumont if (key1 == TEE_HANDLE_NULL) 614a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key1); 615a57c1e2eSCedric Chaumont /* Clear operation key2 (if needed) */ 616a57c1e2eSCedric Chaumont if (key2 == TEE_HANDLE_NULL) 617a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key2); 618a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 619a57c1e2eSCedric Chaumont goto out; 620a57c1e2eSCedric Chaumont } 621a57c1e2eSCedric Chaumont 622a57c1e2eSCedric Chaumont /* No key for digest operation */ 623a57c1e2eSCedric Chaumont if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 624a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 625a57c1e2eSCedric Chaumont goto out; 626a57c1e2eSCedric Chaumont } 627a57c1e2eSCedric Chaumont 628a57c1e2eSCedric Chaumont /* Two keys flag expected (TEE_ALG_AES_XTS only) */ 629a57c1e2eSCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 630a57c1e2eSCedric Chaumont 0) { 631a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 632a57c1e2eSCedric Chaumont goto out; 633a57c1e2eSCedric Chaumont } 634a57c1e2eSCedric Chaumont 6357583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key1, &key_info1); 636a57c1e2eSCedric Chaumont /* Key1 is not a valid handle */ 6377583c59eSCedric Chaumont if (res != TEE_SUCCESS) 638a57c1e2eSCedric Chaumont goto out; 6397583c59eSCedric Chaumont 640b0104773SPascal Brand /* Supplied key has to meet required usage */ 641b0104773SPascal Brand if ((key_info1.objectUsage & operation->info. 642b0104773SPascal Brand requiredKeyUsage) != operation->info.requiredKeyUsage) { 643a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 644a57c1e2eSCedric Chaumont goto out; 645b0104773SPascal Brand } 646b0104773SPascal Brand 6477583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key2, &key_info2); 648a57c1e2eSCedric Chaumont /* Key2 is not a valid handle */ 6497583c59eSCedric Chaumont if (res != TEE_SUCCESS) { 6507583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) 6517583c59eSCedric Chaumont res = TEE_ERROR_CORRUPT_OBJECT_2; 652a57c1e2eSCedric Chaumont goto out; 6537583c59eSCedric Chaumont } 6547583c59eSCedric Chaumont 655b0104773SPascal Brand /* Supplied key has to meet required usage */ 656b0104773SPascal Brand if ((key_info2.objectUsage & operation->info. 657b0104773SPascal Brand requiredKeyUsage) != operation->info.requiredKeyUsage) { 658a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 659a57c1e2eSCedric Chaumont goto out; 660b0104773SPascal Brand } 661b0104773SPascal Brand 662b0104773SPascal Brand /* 663b0104773SPascal Brand * AES-XTS (the only multi key algorithm supported, requires the 664b0104773SPascal Brand * keys to be of equal size. 665b0104773SPascal Brand */ 666b0104773SPascal Brand if (operation->info.algorithm == TEE_ALG_AES_XTS && 667a57c1e2eSCedric Chaumont key_info1.keySize != key_info2.keySize) { 668a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 669a57c1e2eSCedric Chaumont goto out; 670b0104773SPascal Brand 671a57c1e2eSCedric Chaumont } 672a57c1e2eSCedric Chaumont 673a57c1e2eSCedric Chaumont if (operation->info.maxKeySize < key_info1.keySize) { 674a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 675a57c1e2eSCedric Chaumont goto out; 676a57c1e2eSCedric Chaumont } 677b0104773SPascal Brand 678b0104773SPascal Brand /* 679b0104773SPascal Brand * Odd that only the size of one key should be reported while 680b0104773SPascal Brand * size of two key are used when allocating the operation. 681b0104773SPascal Brand */ 6827583c59eSCedric Chaumont key_size = key_info1.keySize; 683b0104773SPascal Brand 684b0104773SPascal Brand TEE_ResetTransientObject(operation->key1); 685b0104773SPascal Brand TEE_ResetTransientObject(operation->key2); 686b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 687b0104773SPascal Brand 6887583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key1, key1); 6897583c59eSCedric Chaumont if (res != TEE_SUCCESS) 690a57c1e2eSCedric Chaumont goto out; 6917583c59eSCedric Chaumont 6927583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key2, key2); 6937583c59eSCedric Chaumont if (res != TEE_SUCCESS) { 6947583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) 6957583c59eSCedric Chaumont res = TEE_ERROR_CORRUPT_OBJECT_2; 696a57c1e2eSCedric Chaumont goto out; 6977583c59eSCedric Chaumont } 6987583c59eSCedric Chaumont 699b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 700b0104773SPascal Brand 701b0104773SPascal Brand operation->info.keySize = key_size; 702b0104773SPascal Brand 7037583c59eSCedric Chaumont out: 704a57c1e2eSCedric Chaumont if (res != TEE_SUCCESS && 705a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 706a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT_2 && 707a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE && 708a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE_2) 709a57c1e2eSCedric Chaumont TEE_Panic(0); 710a57c1e2eSCedric Chaumont 711a57c1e2eSCedric Chaumont return res; 712b0104773SPascal Brand } 713b0104773SPascal Brand 714b0104773SPascal Brand void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op) 715b0104773SPascal Brand { 716b0104773SPascal Brand TEE_Result res; 717b0104773SPascal Brand 718b0104773SPascal Brand if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL) 719b0104773SPascal Brand TEE_Panic(0); 720b0104773SPascal Brand if (dst_op->info.algorithm != src_op->info.algorithm) 721b0104773SPascal Brand TEE_Panic(0); 722b0104773SPascal Brand if (src_op->info.operationClass != TEE_OPERATION_DIGEST) { 723b0104773SPascal Brand TEE_ObjectHandle key1 = TEE_HANDLE_NULL; 724b0104773SPascal Brand TEE_ObjectHandle key2 = TEE_HANDLE_NULL; 725b0104773SPascal Brand 726b0104773SPascal Brand if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) { 727b0104773SPascal Brand key1 = src_op->key1; 728b0104773SPascal Brand key2 = src_op->key2; 729b0104773SPascal Brand } 730b0104773SPascal Brand 731b0104773SPascal Brand if ((src_op->info.handleState & 732b0104773SPascal Brand TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) { 733b0104773SPascal Brand TEE_SetOperationKey(dst_op, key1); 734b0104773SPascal Brand } else { 735b0104773SPascal Brand TEE_SetOperationKey2(dst_op, key1, key2); 736b0104773SPascal Brand } 737b0104773SPascal Brand } 738b0104773SPascal Brand dst_op->info.handleState = src_op->info.handleState; 739b0104773SPascal Brand dst_op->info.keySize = src_op->info.keySize; 740*642a1607SCedric Chaumont dst_op->operationState = src_op->operationState; 741b0104773SPascal Brand 742b0104773SPascal Brand if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks || 743b0104773SPascal Brand dst_op->block_size != src_op->block_size) 744b0104773SPascal Brand TEE_Panic(0); 745b0104773SPascal Brand 746b0104773SPascal Brand if (dst_op->buffer != NULL) { 747b0104773SPascal Brand if (src_op->buffer == NULL) 748b0104773SPascal Brand TEE_Panic(0); 749b0104773SPascal Brand 750b0104773SPascal Brand memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs); 751b0104773SPascal Brand dst_op->buffer_offs = src_op->buffer_offs; 752b0104773SPascal Brand } else if (src_op->buffer != NULL) { 753b0104773SPascal Brand TEE_Panic(0); 754b0104773SPascal Brand } 755b0104773SPascal Brand 756b0104773SPascal Brand res = utee_cryp_state_copy(dst_op->state, src_op->state); 757b0104773SPascal Brand if (res != TEE_SUCCESS) 758b0104773SPascal Brand TEE_Panic(res); 759b0104773SPascal Brand } 760b0104773SPascal Brand 761b0104773SPascal Brand /* Cryptographic Operations API - Message Digest Functions */ 762b0104773SPascal Brand 7636d15db08SJerome Forissier static void init_hash_operation(TEE_OperationHandle operation, void *IV, 7646d15db08SJerome Forissier uint32_t IVLen) 7656d15db08SJerome Forissier { 7666d15db08SJerome Forissier TEE_Result res; 7676d15db08SJerome Forissier 7686d15db08SJerome Forissier /* 7696d15db08SJerome Forissier * Note : IV and IVLen are never used in current implementation 7706d15db08SJerome Forissier * This is why coherent values of IV and IVLen are not checked 7716d15db08SJerome Forissier */ 7726d15db08SJerome Forissier res = utee_hash_init(operation->state, IV, IVLen); 7736d15db08SJerome Forissier if (res != TEE_SUCCESS) 7746d15db08SJerome Forissier TEE_Panic(res); 7756d15db08SJerome Forissier operation->buffer_offs = 0; 7766d15db08SJerome Forissier operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 7776d15db08SJerome Forissier } 7786d15db08SJerome Forissier 779b0104773SPascal Brand void TEE_DigestUpdate(TEE_OperationHandle operation, 78079a3c601SCedric Chaumont void *chunk, uint32_t chunkSize) 781b0104773SPascal Brand { 78273d6c3baSJoakim Bech TEE_Result res = TEE_ERROR_GENERIC; 783b0104773SPascal Brand 78473d6c3baSJoakim Bech if (operation == TEE_HANDLE_NULL || 78573d6c3baSJoakim Bech operation->info.operationClass != TEE_OPERATION_DIGEST) 786b0104773SPascal Brand TEE_Panic(0); 78773d6c3baSJoakim Bech 788*642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 789*642a1607SCedric Chaumont 790b0104773SPascal Brand res = utee_hash_update(operation->state, chunk, chunkSize); 791b0104773SPascal Brand if (res != TEE_SUCCESS) 792b0104773SPascal Brand TEE_Panic(res); 793b0104773SPascal Brand } 794b0104773SPascal Brand 795*642a1607SCedric Chaumont TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, void *chunk, 79679a3c601SCedric Chaumont uint32_t chunkLen, void *hash, uint32_t *hashLen) 797b0104773SPascal Brand { 79887c2f6b6SCedric Chaumont TEE_Result res; 79987c2f6b6SCedric Chaumont 80087c2f6b6SCedric Chaumont if ((operation == TEE_HANDLE_NULL) || 80187c2f6b6SCedric Chaumont (!chunk && chunkLen) || 80287c2f6b6SCedric Chaumont !hash || 80387c2f6b6SCedric Chaumont !hashLen || 80487c2f6b6SCedric Chaumont (operation->info.operationClass != TEE_OPERATION_DIGEST)) { 80587c2f6b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 80687c2f6b6SCedric Chaumont goto out; 80787c2f6b6SCedric Chaumont } 80887c2f6b6SCedric Chaumont 80987c2f6b6SCedric Chaumont res = utee_hash_final(operation->state, chunk, chunkLen, hash, 81087c2f6b6SCedric Chaumont hashLen); 8116d15db08SJerome Forissier if (res != TEE_SUCCESS) 8126d15db08SJerome Forissier goto out; 8136d15db08SJerome Forissier 8146d15db08SJerome Forissier /* Reset operation state */ 8156d15db08SJerome Forissier init_hash_operation(operation, NULL, 0); 81687c2f6b6SCedric Chaumont 817*642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 818*642a1607SCedric Chaumont 81987c2f6b6SCedric Chaumont out: 82087c2f6b6SCedric Chaumont if (res != TEE_SUCCESS && 82187c2f6b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 822b0104773SPascal Brand TEE_Panic(0); 82373d6c3baSJoakim Bech 82487c2f6b6SCedric Chaumont return res; 825b0104773SPascal Brand } 826b0104773SPascal Brand 827b0104773SPascal Brand /* Cryptographic Operations API - Symmetric Cipher Functions */ 828b0104773SPascal Brand 829*642a1607SCedric Chaumont void TEE_CipherInit(TEE_OperationHandle operation, void *IV, uint32_t IVLen) 830b0104773SPascal Brand { 831b0104773SPascal Brand TEE_Result res; 832b0104773SPascal Brand 833b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 834b0104773SPascal Brand TEE_Panic(0); 835*642a1607SCedric Chaumont 836b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_CIPHER) 837b0104773SPascal Brand TEE_Panic(0); 838*642a1607SCedric Chaumont 839*642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 840*642a1607SCedric Chaumont !(operation->key1)) 841*642a1607SCedric Chaumont TEE_Panic(0); 842*642a1607SCedric Chaumont 843*642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 844*642a1607SCedric Chaumont TEE_ResetOperation(operation); 845*642a1607SCedric Chaumont 846*642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 847*642a1607SCedric Chaumont 848b0104773SPascal Brand res = utee_cipher_init(operation->state, IV, IVLen); 849b0104773SPascal Brand if (res != TEE_SUCCESS) 850b0104773SPascal Brand TEE_Panic(res); 851*642a1607SCedric Chaumont 852b0104773SPascal Brand operation->buffer_offs = 0; 853b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 854b0104773SPascal Brand } 855b0104773SPascal Brand 856b0104773SPascal Brand static TEE_Result tee_buffer_update( 857b0104773SPascal Brand TEE_OperationHandle op, 858b0104773SPascal Brand TEE_Result(*update_func) (uint32_t state, const void *src, 8597f74c64aSPascal Brand size_t slen, void *dst, uint32_t *dlen), 860b0104773SPascal Brand const void *src_data, size_t src_len, 8617f74c64aSPascal Brand void *dest_data, uint32_t *dest_len) 862b0104773SPascal Brand { 863b0104773SPascal Brand TEE_Result res; 864b0104773SPascal Brand const uint8_t *src = src_data; 865b0104773SPascal Brand size_t slen = src_len; 866b0104773SPascal Brand uint8_t *dst = dest_data; 867b0104773SPascal Brand size_t dlen = *dest_len; 868b0104773SPascal Brand size_t acc_dlen = 0; 8697f74c64aSPascal Brand uint32_t tmp_dlen; 870b0104773SPascal Brand size_t l; 871b0104773SPascal Brand size_t buffer_size; 872d3588802SPascal Brand size_t buffer_left; 873b0104773SPascal Brand 874d3588802SPascal Brand if (op->buffer_two_blocks) { 875b0104773SPascal Brand buffer_size = op->block_size * 2; 876d3588802SPascal Brand buffer_left = 1; 877d3588802SPascal Brand } else { 878b0104773SPascal Brand buffer_size = op->block_size; 879d3588802SPascal Brand buffer_left = 0; 880d3588802SPascal Brand } 881b0104773SPascal Brand 882b0104773SPascal Brand if (op->buffer_offs > 0) { 883b0104773SPascal Brand /* Fill up complete block */ 884b0104773SPascal Brand if (op->buffer_offs < op->block_size) 885b0104773SPascal Brand l = MIN(slen, op->block_size - op->buffer_offs); 886b0104773SPascal Brand else 887b0104773SPascal Brand l = MIN(slen, buffer_size - op->buffer_offs); 888b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, l); 889b0104773SPascal Brand op->buffer_offs += l; 890b0104773SPascal Brand src += l; 891b0104773SPascal Brand slen -= l; 892b0104773SPascal Brand if ((op->buffer_offs % op->block_size) != 0) 893b0104773SPascal Brand goto out; /* Nothing left to do */ 894b0104773SPascal Brand } 895b0104773SPascal Brand 896b0104773SPascal Brand /* If we can feed from buffer */ 897d3588802SPascal Brand if ((op->buffer_offs > 0) && 898d3588802SPascal Brand ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) { 8992ff3fdbbSPascal Brand l = ROUNDUP(op->buffer_offs + slen - buffer_size, 900b0104773SPascal Brand op->block_size); 901b0104773SPascal Brand l = MIN(op->buffer_offs, l); 902b0104773SPascal Brand tmp_dlen = dlen; 903b0104773SPascal Brand res = update_func(op->state, op->buffer, l, dst, &tmp_dlen); 904b0104773SPascal Brand if (res != TEE_SUCCESS) 905b0104773SPascal Brand TEE_Panic(res); 906b0104773SPascal Brand dst += tmp_dlen; 907b0104773SPascal Brand dlen -= tmp_dlen; 908b0104773SPascal Brand acc_dlen += tmp_dlen; 909b0104773SPascal Brand op->buffer_offs -= l; 910b0104773SPascal Brand if (op->buffer_offs > 0) { 911b0104773SPascal Brand /* 912b0104773SPascal Brand * Slen is small enough to be contained in rest buffer. 913b0104773SPascal Brand */ 914b0104773SPascal Brand memcpy(op->buffer, op->buffer + l, buffer_size - l); 915b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 916b0104773SPascal Brand op->buffer_offs += slen; 917b0104773SPascal Brand goto out; /* Nothing left to do */ 918b0104773SPascal Brand } 919b0104773SPascal Brand } 920b0104773SPascal Brand 921d3588802SPascal Brand if (slen >= (buffer_size + buffer_left)) { 922b0104773SPascal Brand /* Buffer is empty, feed as much as possible from src */ 9232ff3fdbbSPascal Brand l = ROUNDUP(slen - buffer_size + 1, op->block_size); 924b0104773SPascal Brand 925b0104773SPascal Brand tmp_dlen = dlen; 926b0104773SPascal Brand res = update_func(op->state, src, l, dst, &tmp_dlen); 927b0104773SPascal Brand if (res != TEE_SUCCESS) 928b0104773SPascal Brand TEE_Panic(res); 929b0104773SPascal Brand src += l; 930b0104773SPascal Brand slen -= l; 931b0104773SPascal Brand dst += tmp_dlen; 932b0104773SPascal Brand dlen -= tmp_dlen; 933b0104773SPascal Brand acc_dlen += tmp_dlen; 934b0104773SPascal Brand } 935b0104773SPascal Brand 936b0104773SPascal Brand /* Slen is small enough to be contained in buffer. */ 937b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 938b0104773SPascal Brand op->buffer_offs += slen; 939b0104773SPascal Brand 940b0104773SPascal Brand out: 941b0104773SPascal Brand *dest_len = acc_dlen; 942b0104773SPascal Brand return TEE_SUCCESS; 943b0104773SPascal Brand } 944b0104773SPascal Brand 945*642a1607SCedric Chaumont TEE_Result TEE_CipherUpdate(TEE_OperationHandle operation, void *srcData, 94679a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 947b0104773SPascal Brand { 948dea1f2b6SCedric Chaumont TEE_Result res; 949b0104773SPascal Brand size_t req_dlen; 950b0104773SPascal Brand 951*642a1607SCedric Chaumont if (operation == TEE_HANDLE_NULL || 952dea1f2b6SCedric Chaumont (srcData == NULL && srcLen != 0) || 953dea1f2b6SCedric Chaumont destLen == NULL || 954dea1f2b6SCedric Chaumont (destData == NULL && *destLen != 0)) { 955dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 956dea1f2b6SCedric Chaumont goto out; 957dea1f2b6SCedric Chaumont } 958dea1f2b6SCedric Chaumont 959*642a1607SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 960dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 961dea1f2b6SCedric Chaumont goto out; 962dea1f2b6SCedric Chaumont } 963dea1f2b6SCedric Chaumont 964*642a1607SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 965*642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 966*642a1607SCedric Chaumont goto out; 967*642a1607SCedric Chaumont } 968*642a1607SCedric Chaumont 969*642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 970dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 971dea1f2b6SCedric Chaumont goto out; 972dea1f2b6SCedric Chaumont } 973b0104773SPascal Brand 974b0104773SPascal Brand /* Calculate required dlen */ 975*642a1607SCedric Chaumont req_dlen = ((operation->buffer_offs + srcLen) / operation->block_size) * 976*642a1607SCedric Chaumont operation->block_size; 977*642a1607SCedric Chaumont if (operation->buffer_two_blocks) { 978*642a1607SCedric Chaumont if (req_dlen > operation->block_size * 2) 979*642a1607SCedric Chaumont req_dlen -= operation->block_size * 2; 980b0104773SPascal Brand else 981b0104773SPascal Brand req_dlen = 0; 982b0104773SPascal Brand } 983b0104773SPascal Brand /* 984b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 985b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 986b0104773SPascal Brand * can't restore sync with this API. 987b0104773SPascal Brand */ 988b0104773SPascal Brand if (*destLen < req_dlen) { 989b0104773SPascal Brand *destLen = req_dlen; 990dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 991dea1f2b6SCedric Chaumont goto out; 992b0104773SPascal Brand } 993b0104773SPascal Brand 994*642a1607SCedric Chaumont res = tee_buffer_update(operation, utee_cipher_update, srcData, srcLen, 995dea1f2b6SCedric Chaumont destData, destLen); 996b0104773SPascal Brand 997dea1f2b6SCedric Chaumont out: 998dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 999dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1000dea1f2b6SCedric Chaumont TEE_Panic(0); 1001dea1f2b6SCedric Chaumont 1002dea1f2b6SCedric Chaumont return res; 1003b0104773SPascal Brand } 1004b0104773SPascal Brand 1005*642a1607SCedric Chaumont TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation, 1006*642a1607SCedric Chaumont void *srcData, uint32_t srcLen, void *destData, 100779a3c601SCedric Chaumont uint32_t *destLen) 1008b0104773SPascal Brand { 1009b0104773SPascal Brand TEE_Result res; 1010b0104773SPascal Brand uint8_t *dst = destData; 1011b0104773SPascal Brand size_t acc_dlen = 0; 10127f74c64aSPascal Brand uint32_t tmp_dlen; 1013b0104773SPascal Brand size_t req_dlen; 1014b0104773SPascal Brand 1015*642a1607SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1016dea1f2b6SCedric Chaumont (srcData == NULL && srcLen != 0) || 1017dea1f2b6SCedric Chaumont destLen == NULL || 1018dea1f2b6SCedric Chaumont (destData == NULL && *destLen != 0)) { 1019dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1020dea1f2b6SCedric Chaumont goto out; 1021dea1f2b6SCedric Chaumont } 1022dea1f2b6SCedric Chaumont 1023*642a1607SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 1024dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1025dea1f2b6SCedric Chaumont goto out; 1026dea1f2b6SCedric Chaumont } 1027dea1f2b6SCedric Chaumont 1028*642a1607SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1029*642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1030*642a1607SCedric Chaumont goto out; 1031*642a1607SCedric Chaumont } 1032*642a1607SCedric Chaumont 1033*642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1034dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1035dea1f2b6SCedric Chaumont goto out; 1036dea1f2b6SCedric Chaumont } 1037b0104773SPascal Brand 1038b0104773SPascal Brand /* 1039b0104773SPascal Brand * Check that the final block doesn't require padding for those 1040b0104773SPascal Brand * algorithms that requires client to supply padding. 1041b0104773SPascal Brand */ 1042*642a1607SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD || 1043*642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD || 1044*642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD || 1045*642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD || 1046*642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || 1047*642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD) { 1048*642a1607SCedric Chaumont if (((operation->buffer_offs + srcLen) % operation->block_size) 1049*642a1607SCedric Chaumont != 0) { 1050dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1051dea1f2b6SCedric Chaumont goto out; 1052dea1f2b6SCedric Chaumont } 1053b0104773SPascal Brand } 1054b0104773SPascal Brand 1055b0104773SPascal Brand /* 1056b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1057b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1058b0104773SPascal Brand * can't restore sync with this API. 1059b0104773SPascal Brand */ 1060*642a1607SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1061b0104773SPascal Brand if (*destLen < req_dlen) { 1062b0104773SPascal Brand *destLen = req_dlen; 1063dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1064dea1f2b6SCedric Chaumont goto out; 1065b0104773SPascal Brand } 1066b0104773SPascal Brand 1067b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1068*642a1607SCedric Chaumont res = tee_buffer_update(operation, utee_cipher_update, srcData, srcLen, 1069*642a1607SCedric Chaumont dst, &tmp_dlen); 1070dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS) 1071dea1f2b6SCedric Chaumont goto out; 1072dea1f2b6SCedric Chaumont 1073b0104773SPascal Brand dst += tmp_dlen; 1074b0104773SPascal Brand acc_dlen += tmp_dlen; 1075b0104773SPascal Brand 1076b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1077*642a1607SCedric Chaumont res = utee_cipher_final(operation->state, operation->buffer, 1078*642a1607SCedric Chaumont operation->buffer_offs, dst, &tmp_dlen); 1079b0104773SPascal Brand if (res != TEE_SUCCESS) 1080dea1f2b6SCedric Chaumont goto out; 1081dea1f2b6SCedric Chaumont 1082b0104773SPascal Brand acc_dlen += tmp_dlen; 1083b0104773SPascal Brand *destLen = acc_dlen; 1084dea1f2b6SCedric Chaumont 1085*642a1607SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1086*642a1607SCedric Chaumont 1087*642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1088*642a1607SCedric Chaumont 1089dea1f2b6SCedric Chaumont out: 1090dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 1091dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1092dea1f2b6SCedric Chaumont TEE_Panic(0); 1093dea1f2b6SCedric Chaumont 1094dea1f2b6SCedric Chaumont return res; 1095b0104773SPascal Brand } 1096b0104773SPascal Brand 1097b0104773SPascal Brand /* Cryptographic Operations API - MAC Functions */ 1098b0104773SPascal Brand 109928e0efc6SCedric Chaumont void TEE_MACInit(TEE_OperationHandle operation, void *IV, uint32_t IVLen) 1100b0104773SPascal Brand { 1101b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 1102b0104773SPascal Brand TEE_Panic(0); 1103*642a1607SCedric Chaumont 1104b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_MAC) 1105b0104773SPascal Brand TEE_Panic(0); 1106*642a1607SCedric Chaumont 1107*642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 1108*642a1607SCedric Chaumont !(operation->key1)) 1109*642a1607SCedric Chaumont TEE_Panic(0); 1110*642a1607SCedric Chaumont 1111*642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 1112*642a1607SCedric Chaumont TEE_ResetOperation(operation); 1113*642a1607SCedric Chaumont 1114*642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1115*642a1607SCedric Chaumont 11166d15db08SJerome Forissier init_hash_operation(operation, IV, IVLen); 1117b0104773SPascal Brand } 1118b0104773SPascal Brand 111928e0efc6SCedric Chaumont void TEE_MACUpdate(TEE_OperationHandle operation, void *chunk, 112028e0efc6SCedric Chaumont uint32_t chunkSize) 1121b0104773SPascal Brand { 1122b0104773SPascal Brand TEE_Result res; 1123b0104773SPascal Brand 112428e0efc6SCedric Chaumont if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0)) 1125b0104773SPascal Brand TEE_Panic(0); 1126*642a1607SCedric Chaumont 112728e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) 1128b0104773SPascal Brand TEE_Panic(0); 1129*642a1607SCedric Chaumont 113028e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1131b0104773SPascal Brand TEE_Panic(0); 1132b0104773SPascal Brand 1133*642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) 1134*642a1607SCedric Chaumont TEE_Panic(0); 1135*642a1607SCedric Chaumont 113628e0efc6SCedric Chaumont res = utee_hash_update(operation->state, chunk, chunkSize); 1137b0104773SPascal Brand if (res != TEE_SUCCESS) 1138b0104773SPascal Brand TEE_Panic(res); 1139b0104773SPascal Brand } 1140b0104773SPascal Brand 114128e0efc6SCedric Chaumont TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation, 114228e0efc6SCedric Chaumont void *message, uint32_t messageLen, 114379a3c601SCedric Chaumont void *mac, uint32_t *macLen) 1144b0104773SPascal Brand { 1145b0104773SPascal Brand TEE_Result res; 1146b0104773SPascal Brand 114728e0efc6SCedric Chaumont if (operation == TEE_HANDLE_NULL || 114828e0efc6SCedric Chaumont (message == NULL && messageLen != 0) || 114928e0efc6SCedric Chaumont mac == NULL || 115028e0efc6SCedric Chaumont macLen == NULL) { 115128e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 115228e0efc6SCedric Chaumont goto out; 115328e0efc6SCedric Chaumont } 1154b0104773SPascal Brand 115528e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 115628e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 115728e0efc6SCedric Chaumont goto out; 115828e0efc6SCedric Chaumont } 115928e0efc6SCedric Chaumont 116028e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 116128e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 116228e0efc6SCedric Chaumont goto out; 116328e0efc6SCedric Chaumont } 116428e0efc6SCedric Chaumont 1165*642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1166*642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1167*642a1607SCedric Chaumont goto out; 1168*642a1607SCedric Chaumont } 1169*642a1607SCedric Chaumont 117028e0efc6SCedric Chaumont res = utee_hash_final(operation->state, message, messageLen, mac, 117128e0efc6SCedric Chaumont macLen); 117228e0efc6SCedric Chaumont if (res != TEE_SUCCESS) 117328e0efc6SCedric Chaumont goto out; 117428e0efc6SCedric Chaumont 117528e0efc6SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 117628e0efc6SCedric Chaumont 1177*642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1178*642a1607SCedric Chaumont 117928e0efc6SCedric Chaumont out: 118028e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 118128e0efc6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 118228e0efc6SCedric Chaumont TEE_Panic(res); 118328e0efc6SCedric Chaumont 1184b0104773SPascal Brand return res; 1185b0104773SPascal Brand } 1186b0104773SPascal Brand 1187b0104773SPascal Brand TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation, 118828e0efc6SCedric Chaumont void *message, uint32_t messageLen, 118928e0efc6SCedric Chaumont void *mac, uint32_t macLen) 1190b0104773SPascal Brand { 1191b0104773SPascal Brand TEE_Result res; 1192b0104773SPascal Brand uint8_t computed_mac[TEE_MAX_HASH_SIZE]; 11937f74c64aSPascal Brand uint32_t computed_mac_size = TEE_MAX_HASH_SIZE; 1194b0104773SPascal Brand 119528e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 119628e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 119728e0efc6SCedric Chaumont goto out; 119828e0efc6SCedric Chaumont } 119928e0efc6SCedric Chaumont 120028e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 120128e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 120228e0efc6SCedric Chaumont goto out; 120328e0efc6SCedric Chaumont } 120428e0efc6SCedric Chaumont 1205*642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1206*642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1207*642a1607SCedric Chaumont goto out; 1208*642a1607SCedric Chaumont } 1209*642a1607SCedric Chaumont 1210b0104773SPascal Brand res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac, 1211b0104773SPascal Brand &computed_mac_size); 1212b0104773SPascal Brand if (res != TEE_SUCCESS) 121328e0efc6SCedric Chaumont goto out; 121428e0efc6SCedric Chaumont 121528e0efc6SCedric Chaumont if (computed_mac_size != macLen) { 121628e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 121728e0efc6SCedric Chaumont goto out; 121828e0efc6SCedric Chaumont } 121928e0efc6SCedric Chaumont 122028e0efc6SCedric Chaumont if (buf_compare_ct(mac, computed_mac, computed_mac_size) != 0) { 122128e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 122228e0efc6SCedric Chaumont goto out; 122328e0efc6SCedric Chaumont } 122428e0efc6SCedric Chaumont 1225*642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1226*642a1607SCedric Chaumont 122728e0efc6SCedric Chaumont out: 122828e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 122928e0efc6SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 123028e0efc6SCedric Chaumont TEE_Panic(res); 123128e0efc6SCedric Chaumont 1232b0104773SPascal Brand return res; 1233b0104773SPascal Brand } 1234b0104773SPascal Brand 1235b0104773SPascal Brand /* Cryptographic Operations API - Authenticated Encryption Functions */ 1236b0104773SPascal Brand 1237b5816c88SCedric Chaumont TEE_Result TEE_AEInit(TEE_OperationHandle operation, void *nonce, 123879a3c601SCedric Chaumont uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen, 1239b0104773SPascal Brand uint32_t payloadLen) 1240b0104773SPascal Brand { 1241b0104773SPascal Brand TEE_Result res; 1242b0104773SPascal Brand 1243b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || nonce == NULL) { 1244b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1245b5816c88SCedric Chaumont goto out; 1246b5816c88SCedric Chaumont } 1247b5816c88SCedric Chaumont 1248b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1249b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1250b5816c88SCedric Chaumont goto out; 1251b5816c88SCedric Chaumont } 1252b0104773SPascal Brand 1253*642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 1254*642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1255*642a1607SCedric Chaumont goto out; 1256*642a1607SCedric Chaumont } 1257*642a1607SCedric Chaumont 1258b0104773SPascal Brand /* 1259b0104773SPascal Brand * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core 1260b0104773SPascal Brand * in the implementation. But AES-GCM spec doesn't specify the tag len 1261b0104773SPascal Brand * according to the same principle so we have to check here instead to 1262b0104773SPascal Brand * be GP compliant. 1263b0104773SPascal Brand */ 1264b5816c88SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_GCM) { 1265b0104773SPascal Brand /* 1266b0104773SPascal Brand * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96 1267b0104773SPascal Brand */ 1268b5816c88SCedric Chaumont if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) { 1269b5816c88SCedric Chaumont res = TEE_ERROR_NOT_SUPPORTED; 1270b5816c88SCedric Chaumont goto out; 1271b5816c88SCedric Chaumont } 1272b0104773SPascal Brand } 1273b0104773SPascal Brand 1274b5816c88SCedric Chaumont res = utee_authenc_init(operation->state, nonce, nonceLen, 1275b5816c88SCedric Chaumont tagLen / 8, AADLen, payloadLen); 1276b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1277b5816c88SCedric Chaumont goto out; 1278b5816c88SCedric Chaumont 1279b5816c88SCedric Chaumont operation->ae_tag_len = tagLen / 8; 1280b5816c88SCedric Chaumont operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 1281b5816c88SCedric Chaumont 1282b5816c88SCedric Chaumont out: 1283b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1284b5816c88SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 1285b0104773SPascal Brand TEE_Panic(res); 1286b5816c88SCedric Chaumont 1287b0104773SPascal Brand return res; 1288b0104773SPascal Brand } 1289b0104773SPascal Brand 1290b5816c88SCedric Chaumont void TEE_AEUpdateAAD(TEE_OperationHandle operation, void *AADdata, 129179a3c601SCedric Chaumont uint32_t AADdataLen) 1292b0104773SPascal Brand { 1293b0104773SPascal Brand TEE_Result res; 1294b0104773SPascal Brand 1295b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1296b5816c88SCedric Chaumont (AADdata == NULL && AADdataLen != 0)) 1297b0104773SPascal Brand TEE_Panic(0); 1298*642a1607SCedric Chaumont 1299b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) 1300b0104773SPascal Brand TEE_Panic(0); 1301*642a1607SCedric Chaumont 1302b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1303b0104773SPascal Brand TEE_Panic(0); 1304b0104773SPascal Brand 1305b5816c88SCedric Chaumont res = utee_authenc_update_aad(operation->state, AADdata, AADdataLen); 1306*642a1607SCedric Chaumont 1307*642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1308*642a1607SCedric Chaumont 1309b0104773SPascal Brand if (res != TEE_SUCCESS) 1310b0104773SPascal Brand TEE_Panic(res); 1311b0104773SPascal Brand } 1312b0104773SPascal Brand 1313b5816c88SCedric Chaumont TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, void *srcData, 131479a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 1315b0104773SPascal Brand { 1316b5816c88SCedric Chaumont TEE_Result res; 1317b0104773SPascal Brand size_t req_dlen; 1318b0104773SPascal Brand 1319b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1320b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1321b5816c88SCedric Chaumont destLen == NULL || 1322b5816c88SCedric Chaumont (destData == NULL && *destLen != 0)) { 1323b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1324b5816c88SCedric Chaumont goto out; 1325b5816c88SCedric Chaumont } 1326b5816c88SCedric Chaumont 1327b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1328b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1329b5816c88SCedric Chaumont goto out; 1330b5816c88SCedric Chaumont } 1331b5816c88SCedric Chaumont 1332b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1333b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1334b5816c88SCedric Chaumont goto out; 1335b5816c88SCedric Chaumont } 1336b0104773SPascal Brand 1337b0104773SPascal Brand /* 1338b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1339b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1340b0104773SPascal Brand * can't restore sync with this API. 1341b0104773SPascal Brand */ 1342b5816c88SCedric Chaumont req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen, 1343b5816c88SCedric Chaumont operation->block_size); 1344b0104773SPascal Brand if (*destLen < req_dlen) { 1345b0104773SPascal Brand *destLen = req_dlen; 1346b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1347b5816c88SCedric Chaumont goto out; 1348b0104773SPascal Brand } 1349b0104773SPascal Brand 1350b5816c88SCedric Chaumont res = tee_buffer_update(operation, utee_authenc_update_payload, srcData, 1351b5816c88SCedric Chaumont srcLen, destData, destLen); 1352b0104773SPascal Brand 1353*642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1354*642a1607SCedric Chaumont 1355b5816c88SCedric Chaumont out: 1356b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1357b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1358b5816c88SCedric Chaumont TEE_Panic(res); 1359b5816c88SCedric Chaumont 1360b5816c88SCedric Chaumont return res; 1361b0104773SPascal Brand } 1362b0104773SPascal Brand 1363b5816c88SCedric Chaumont TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation, 1364b5816c88SCedric Chaumont void *srcData, uint32_t srcLen, 136579a3c601SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 136679a3c601SCedric Chaumont uint32_t *tagLen) 1367b0104773SPascal Brand { 1368b0104773SPascal Brand TEE_Result res; 1369b0104773SPascal Brand uint8_t *dst = destData; 1370b0104773SPascal Brand size_t acc_dlen = 0; 13717f74c64aSPascal Brand uint32_t tmp_dlen; 1372b0104773SPascal Brand size_t req_dlen; 1373b0104773SPascal Brand 1374b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1375b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1376b5816c88SCedric Chaumont destLen == NULL || 1377b5816c88SCedric Chaumont (destData == NULL && *destLen != 0) || 1378b5816c88SCedric Chaumont tag == NULL || tagLen == NULL) { 1379b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1380b5816c88SCedric Chaumont goto out; 1381b5816c88SCedric Chaumont } 1382b5816c88SCedric Chaumont 1383b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1384b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1385b5816c88SCedric Chaumont goto out; 1386b5816c88SCedric Chaumont } 1387b5816c88SCedric Chaumont 1388b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1389b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1390b5816c88SCedric Chaumont goto out; 1391b5816c88SCedric Chaumont } 1392b0104773SPascal Brand 1393b0104773SPascal Brand /* 1394b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1395b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1396b0104773SPascal Brand * can't restore sync with this API. 1397b0104773SPascal Brand */ 1398b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1399b0104773SPascal Brand if (*destLen < req_dlen) { 1400b0104773SPascal Brand *destLen = req_dlen; 1401b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1402b5816c88SCedric Chaumont goto out; 1403b0104773SPascal Brand } 1404b0104773SPascal Brand 1405b0104773SPascal Brand /* 1406b0104773SPascal Brand * Need to check this before update_payload since sync would be lost if 1407b0104773SPascal Brand * we return short buffer after that. 1408b0104773SPascal Brand */ 1409b5816c88SCedric Chaumont if (*tagLen < operation->ae_tag_len) { 1410b5816c88SCedric Chaumont *tagLen = operation->ae_tag_len; 1411b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1412b5816c88SCedric Chaumont goto out; 1413b0104773SPascal Brand } 1414b0104773SPascal Brand 1415b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1416b5816c88SCedric Chaumont res = tee_buffer_update(operation, utee_authenc_update_payload, srcData, 1417b5816c88SCedric Chaumont srcLen, dst, &tmp_dlen); 1418b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1419b5816c88SCedric Chaumont goto out; 1420b5816c88SCedric Chaumont 1421b0104773SPascal Brand dst += tmp_dlen; 1422b0104773SPascal Brand acc_dlen += tmp_dlen; 1423b0104773SPascal Brand 1424b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1425b5816c88SCedric Chaumont res = utee_authenc_enc_final(operation->state, operation->buffer, 1426b5816c88SCedric Chaumont operation->buffer_offs, dst, &tmp_dlen, 1427b5816c88SCedric Chaumont tag, tagLen); 1428b0104773SPascal Brand if (res != TEE_SUCCESS) 1429b5816c88SCedric Chaumont goto out; 1430b0104773SPascal Brand 1431b5816c88SCedric Chaumont acc_dlen += tmp_dlen; 1432b0104773SPascal Brand *destLen = acc_dlen; 1433*642a1607SCedric Chaumont 1434b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1435b5816c88SCedric Chaumont 1436*642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1437*642a1607SCedric Chaumont 1438b5816c88SCedric Chaumont out: 1439b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1440b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1441b5816c88SCedric Chaumont TEE_Panic(res); 1442b0104773SPascal Brand 1443b0104773SPascal Brand return res; 1444b0104773SPascal Brand } 1445b0104773SPascal Brand 1446b5816c88SCedric Chaumont TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation, 1447b5816c88SCedric Chaumont void *srcData, uint32_t srcLen, 1448b5816c88SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 144979a3c601SCedric Chaumont uint32_t tagLen) 1450b0104773SPascal Brand { 1451b0104773SPascal Brand TEE_Result res; 1452b0104773SPascal Brand uint8_t *dst = destData; 1453b0104773SPascal Brand size_t acc_dlen = 0; 14547f74c64aSPascal Brand uint32_t tmp_dlen; 1455b0104773SPascal Brand size_t req_dlen; 1456b0104773SPascal Brand 1457b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1458b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1459b5816c88SCedric Chaumont destLen == NULL || 1460b5816c88SCedric Chaumont (destData == NULL && *destLen != 0) || 1461b5816c88SCedric Chaumont (tag == NULL && tagLen != 0)) { 1462b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1463b5816c88SCedric Chaumont goto out; 1464b5816c88SCedric Chaumont } 1465b5816c88SCedric Chaumont 1466b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1467b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1468b5816c88SCedric Chaumont goto out; 1469b5816c88SCedric Chaumont } 1470b5816c88SCedric Chaumont 1471b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1472b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1473b5816c88SCedric Chaumont goto out; 1474b5816c88SCedric Chaumont } 1475b0104773SPascal Brand 1476b0104773SPascal Brand /* 1477b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1478b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1479b0104773SPascal Brand * can't restore sync with this API. 1480b0104773SPascal Brand */ 1481b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1482b0104773SPascal Brand if (*destLen < req_dlen) { 1483b0104773SPascal Brand *destLen = req_dlen; 1484b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1485b5816c88SCedric Chaumont goto out; 1486b0104773SPascal Brand } 1487b0104773SPascal Brand 1488b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1489b5816c88SCedric Chaumont res = tee_buffer_update(operation, utee_authenc_update_payload, srcData, 1490b5816c88SCedric Chaumont srcLen, dst, &tmp_dlen); 1491b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1492b5816c88SCedric Chaumont goto out; 1493b5816c88SCedric Chaumont 1494b0104773SPascal Brand dst += tmp_dlen; 1495b0104773SPascal Brand acc_dlen += tmp_dlen; 1496b0104773SPascal Brand 1497b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1498b5816c88SCedric Chaumont res = utee_authenc_dec_final(operation->state, operation->buffer, 1499b5816c88SCedric Chaumont operation->buffer_offs, dst, &tmp_dlen, 1500b5816c88SCedric Chaumont tag, tagLen); 1501b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1502b5816c88SCedric Chaumont goto out; 1503b5816c88SCedric Chaumont 1504b0104773SPascal Brand /* Supplied tagLen should match what we initiated with */ 1505b5816c88SCedric Chaumont if (tagLen != operation->ae_tag_len) 1506b0104773SPascal Brand res = TEE_ERROR_MAC_INVALID; 1507b0104773SPascal Brand 1508b0104773SPascal Brand acc_dlen += tmp_dlen; 1509b0104773SPascal Brand *destLen = acc_dlen; 1510*642a1607SCedric Chaumont 1511b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1512b5816c88SCedric Chaumont 1513*642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1514*642a1607SCedric Chaumont 1515b5816c88SCedric Chaumont out: 1516b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1517b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER && 1518b5816c88SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 1519b5816c88SCedric Chaumont TEE_Panic(res); 1520b0104773SPascal Brand 1521b0104773SPascal Brand return res; 1522b0104773SPascal Brand } 1523b0104773SPascal Brand 1524b0104773SPascal Brand /* Cryptographic Operations API - Asymmetric Functions */ 1525b0104773SPascal Brand 152612e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation, 152712e66b6fSCedric Chaumont TEE_Attribute *params, 152812e66b6fSCedric Chaumont uint32_t paramCount, void *srcData, 152979a3c601SCedric Chaumont uint32_t srcLen, void *destData, 153079a3c601SCedric Chaumont uint32_t *destLen) 1531b0104773SPascal Brand { 1532b0104773SPascal Brand TEE_Result res; 1533b0104773SPascal Brand 153412e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1535b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 1536b0104773SPascal Brand TEE_Panic(0); 153712e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1538b0104773SPascal Brand TEE_Panic(0); 153912e66b6fSCedric Chaumont if (!operation->key1) 1540b0104773SPascal Brand TEE_Panic(0); 154112e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 154212e66b6fSCedric Chaumont TEE_Panic(0); 154312e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_ENCRYPT) 1544b0104773SPascal Brand TEE_Panic(0); 1545b0104773SPascal Brand 154612e66b6fSCedric Chaumont res = utee_asymm_operate(operation->state, params, paramCount, srcData, 154712e66b6fSCedric Chaumont srcLen, destData, destLen); 154812e66b6fSCedric Chaumont 15498844ebfcSPascal Brand if (res != TEE_SUCCESS && 15508844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 15518844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1552b0104773SPascal Brand TEE_Panic(res); 155312e66b6fSCedric Chaumont 1554b0104773SPascal Brand return res; 1555b0104773SPascal Brand } 1556b0104773SPascal Brand 155712e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation, 155812e66b6fSCedric Chaumont TEE_Attribute *params, 155912e66b6fSCedric Chaumont uint32_t paramCount, void *srcData, 156079a3c601SCedric Chaumont uint32_t srcLen, void *destData, 156179a3c601SCedric Chaumont uint32_t *destLen) 1562b0104773SPascal Brand { 1563b0104773SPascal Brand TEE_Result res; 1564b0104773SPascal Brand 156512e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1566b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 1567b0104773SPascal Brand TEE_Panic(0); 156812e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1569b0104773SPascal Brand TEE_Panic(0); 157012e66b6fSCedric Chaumont if (!operation->key1) 1571b0104773SPascal Brand TEE_Panic(0); 157212e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 157312e66b6fSCedric Chaumont TEE_Panic(0); 157412e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_DECRYPT) 1575b0104773SPascal Brand TEE_Panic(0); 1576b0104773SPascal Brand 157712e66b6fSCedric Chaumont res = utee_asymm_operate(operation->state, params, paramCount, srcData, 157812e66b6fSCedric Chaumont srcLen, destData, destLen); 157912e66b6fSCedric Chaumont 15808844ebfcSPascal Brand if (res != TEE_SUCCESS && 15818844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 15828844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1583b0104773SPascal Brand TEE_Panic(res); 158412e66b6fSCedric Chaumont 1585b0104773SPascal Brand return res; 1586b0104773SPascal Brand } 1587b0104773SPascal Brand 158812e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation, 158912e66b6fSCedric Chaumont TEE_Attribute *params, 159012e66b6fSCedric Chaumont uint32_t paramCount, void *digest, 159179a3c601SCedric Chaumont uint32_t digestLen, void *signature, 159279a3c601SCedric Chaumont uint32_t *signatureLen) 1593b0104773SPascal Brand { 1594b0104773SPascal Brand TEE_Result res; 1595b0104773SPascal Brand 159612e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || 159712e66b6fSCedric Chaumont (digest == NULL && digestLen != 0) || 1598b0104773SPascal Brand signature == NULL || signatureLen == NULL) 1599b0104773SPascal Brand TEE_Panic(0); 160012e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1601b0104773SPascal Brand TEE_Panic(0); 160212e66b6fSCedric Chaumont if (!operation->key1) 1603b0104773SPascal Brand TEE_Panic(0); 160412e66b6fSCedric Chaumont if (operation->info.operationClass != 160512e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 160612e66b6fSCedric Chaumont TEE_Panic(0); 160712e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_SIGN) 1608b0104773SPascal Brand TEE_Panic(0); 1609b0104773SPascal Brand 161012e66b6fSCedric Chaumont res = utee_asymm_operate(operation->state, params, paramCount, digest, 161112e66b6fSCedric Chaumont digestLen, signature, signatureLen); 161212e66b6fSCedric Chaumont 1613b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER) 1614b0104773SPascal Brand TEE_Panic(res); 161512e66b6fSCedric Chaumont 1616b0104773SPascal Brand return res; 1617b0104773SPascal Brand } 1618b0104773SPascal Brand 161912e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation, 162012e66b6fSCedric Chaumont TEE_Attribute *params, 162112e66b6fSCedric Chaumont uint32_t paramCount, void *digest, 162212e66b6fSCedric Chaumont uint32_t digestLen, void *signature, 162379a3c601SCedric Chaumont uint32_t signatureLen) 1624b0104773SPascal Brand { 1625b0104773SPascal Brand TEE_Result res; 1626b0104773SPascal Brand 162712e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || 162812e66b6fSCedric Chaumont (digest == NULL && digestLen != 0) || 1629b0104773SPascal Brand (signature == NULL && signatureLen != 0)) 1630b0104773SPascal Brand TEE_Panic(0); 163112e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1632b0104773SPascal Brand TEE_Panic(0); 163312e66b6fSCedric Chaumont if (!operation->key1) 1634b0104773SPascal Brand TEE_Panic(0); 163512e66b6fSCedric Chaumont if (operation->info.operationClass != 163612e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 163712e66b6fSCedric Chaumont TEE_Panic(0); 163812e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_VERIFY) 1639b0104773SPascal Brand TEE_Panic(0); 1640b0104773SPascal Brand 164112e66b6fSCedric Chaumont res = utee_asymm_verify(operation->state, params, paramCount, digest, 164212e66b6fSCedric Chaumont digestLen, signature, signatureLen); 164312e66b6fSCedric Chaumont 1644b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID) 1645b0104773SPascal Brand TEE_Panic(res); 164612e66b6fSCedric Chaumont 1647b0104773SPascal Brand return res; 1648b0104773SPascal Brand } 1649b0104773SPascal Brand 1650b0104773SPascal Brand /* Cryptographic Operations API - Key Derivation Functions */ 1651b0104773SPascal Brand 1652b0104773SPascal Brand void TEE_DeriveKey(TEE_OperationHandle operation, 1653b0104773SPascal Brand const TEE_Attribute *params, uint32_t paramCount, 1654b0104773SPascal Brand TEE_ObjectHandle derivedKey) 1655b0104773SPascal Brand { 1656b0104773SPascal Brand TEE_Result res; 1657b0104773SPascal Brand TEE_ObjectInfo key_info; 1658b0104773SPascal Brand 1659b0104773SPascal Brand if (operation == TEE_HANDLE_NULL || derivedKey == 0) 1660b0104773SPascal Brand TEE_Panic(0); 166184fa9467SCedric Chaumont if (params == NULL && paramCount != 0) 1662b0104773SPascal Brand TEE_Panic(0); 16638854d3c6SJerome Forissier if (TEE_ALG_GET_CLASS(operation->info.algorithm) != 16648854d3c6SJerome Forissier TEE_OPERATION_KEY_DERIVATION) 1665b0104773SPascal Brand TEE_Panic(0); 1666b0104773SPascal Brand 1667b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION) 1668b0104773SPascal Brand TEE_Panic(0); 166984fa9467SCedric Chaumont if (!operation->key1) 167084fa9467SCedric Chaumont TEE_Panic(0); 1671b0104773SPascal Brand if (operation->info.mode != TEE_MODE_DERIVE) 1672b0104773SPascal Brand TEE_Panic(0); 1673b0104773SPascal Brand if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0) 1674b0104773SPascal Brand TEE_Panic(0); 1675b0104773SPascal Brand 1676b0104773SPascal Brand res = utee_cryp_obj_get_info((uint32_t) derivedKey, &key_info); 1677b0104773SPascal Brand if (res != TEE_SUCCESS) 1678b0104773SPascal Brand TEE_Panic(0); 1679b0104773SPascal Brand 1680b0104773SPascal Brand if (key_info.objectType != TEE_TYPE_GENERIC_SECRET) 1681b0104773SPascal Brand TEE_Panic(0); 1682b0104773SPascal Brand if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1683b0104773SPascal Brand TEE_Panic(0); 1684b0104773SPascal Brand 1685b0104773SPascal Brand res = utee_cryp_derive_key(operation->state, params, paramCount, 1686b0104773SPascal Brand (uint32_t) derivedKey); 1687b0104773SPascal Brand if (res != TEE_SUCCESS) 1688b0104773SPascal Brand TEE_Panic(res); 1689b0104773SPascal Brand } 1690b0104773SPascal Brand 1691b0104773SPascal Brand /* Cryptographic Operations API - Random Number Generation Functions */ 1692b0104773SPascal Brand 169379a3c601SCedric Chaumont void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen) 1694b0104773SPascal Brand { 1695b0104773SPascal Brand TEE_Result res; 1696b0104773SPascal Brand 1697b0104773SPascal Brand res = utee_cryp_random_number_generate(randomBuffer, randomBufferLen); 1698b0104773SPascal Brand if (res != TEE_SUCCESS) 1699b0104773SPascal Brand TEE_Panic(res); 1700b0104773SPascal Brand } 1701