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*05304565SCedric Chaumont uint32_t operationState; 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; 243*05304565SCedric 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 313*05304565SCedric 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 330*05304565SCedric Chaumont /* 331*05304565SCedric Chaumont * Initialize digest operations 332*05304565SCedric Chaumont * Other multi-stage operations initialized w/ TEE_xxxInit functions 333*05304565SCedric Chaumont * Non-applicable on asymmetric operations 334*05304565SCedric Chaumont */ 335*05304565SCedric Chaumont if (TEE_ALG_GET_CLASS(algorithm) == TEE_OPERATION_DIGEST) { 336*05304565SCedric Chaumont res = utee_hash_init(op->state, NULL, 0); 337*05304565SCedric Chaumont if (res != TEE_SUCCESS) 338*05304565SCedric Chaumont goto err0; 339*05304565SCedric Chaumont /* v1.1: flags always set for digest operations */ 340*05304565SCedric Chaumont op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 341*05304565SCedric Chaumont } 342*05304565SCedric Chaumont 343b0104773SPascal Brand *operation = op; 3449b52c538SCedric Chaumont goto out; 345b0104773SPascal Brand 3469b52c538SCedric Chaumont err2: 347b0104773SPascal Brand TEE_FreeTransientObject(op->key2); 3489b52c538SCedric Chaumont err1: 3499b52c538SCedric Chaumont TEE_FreeTransientObject(op->key1); 3509b52c538SCedric Chaumont err0: 351b0104773SPascal Brand TEE_FreeOperation(op); 352b0104773SPascal Brand 3539b52c538SCedric Chaumont if (res != TEE_SUCCESS && 3549b52c538SCedric Chaumont res != TEE_ERROR_OUT_OF_MEMORY && 3559b52c538SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 3569b52c538SCedric Chaumont TEE_Panic(0); 3579b52c538SCedric Chaumont out: 358b0104773SPascal Brand return res; 359b0104773SPascal Brand } 360b0104773SPascal Brand 361b0104773SPascal Brand void TEE_FreeOperation(TEE_OperationHandle operation) 362b0104773SPascal Brand { 363e889e80bSCedric Chaumont TEE_Result res; 364e889e80bSCedric Chaumont 365e889e80bSCedric Chaumont if (operation == TEE_HANDLE_NULL) 366e889e80bSCedric Chaumont TEE_Panic(0); 367e889e80bSCedric Chaumont 368b0104773SPascal Brand /* 369b0104773SPascal Brand * Note that keys should not be freed here, since they are 370b0104773SPascal Brand * claimed by the operation they will be freed by 371b0104773SPascal Brand * utee_cryp_state_free(). 372b0104773SPascal Brand */ 373e889e80bSCedric Chaumont res = utee_cryp_state_free(operation->state); 374e889e80bSCedric Chaumont if (res != TEE_SUCCESS) 375e889e80bSCedric Chaumont TEE_Panic(0); 376e889e80bSCedric Chaumont 377b0104773SPascal Brand TEE_Free(operation->buffer); 378b0104773SPascal Brand TEE_Free(operation); 379b0104773SPascal Brand } 380b0104773SPascal Brand 381b0104773SPascal Brand void TEE_GetOperationInfo(TEE_OperationHandle operation, 382b0104773SPascal Brand TEE_OperationInfo *operationInfo) 383b0104773SPascal Brand { 384b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 385b0104773SPascal Brand TEE_Panic(0); 386b0104773SPascal Brand 387*05304565SCedric Chaumont if (!operationInfo) 388b0104773SPascal Brand TEE_Panic(0); 389b0104773SPascal Brand 390b0104773SPascal Brand *operationInfo = operation->info; 391b0104773SPascal Brand } 392b0104773SPascal Brand 393*05304565SCedric Chaumont TEE_Result TEE_GetOperationInfoMultiple(TEE_OperationHandle operation, 394*05304565SCedric Chaumont TEE_OperationInfoMultiple *operationInfoMultiple, 395*05304565SCedric Chaumont uint32_t *operationSize) 396*05304565SCedric Chaumont { 397*05304565SCedric Chaumont TEE_Result res = TEE_SUCCESS; 398*05304565SCedric Chaumont TEE_ObjectInfo key_info1; 399*05304565SCedric Chaumont TEE_ObjectInfo key_info2; 400*05304565SCedric Chaumont uint32_t num_of_keys; 401*05304565SCedric Chaumont size_t n; 402*05304565SCedric Chaumont 403*05304565SCedric Chaumont if (operation == TEE_HANDLE_NULL) { 404*05304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 405*05304565SCedric Chaumont goto out; 406*05304565SCedric Chaumont } 407*05304565SCedric Chaumont 408*05304565SCedric Chaumont if (!operationInfoMultiple) { 409*05304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 410*05304565SCedric Chaumont goto out; 411*05304565SCedric Chaumont } 412*05304565SCedric Chaumont 413*05304565SCedric Chaumont if (!operationSize) { 414*05304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 415*05304565SCedric Chaumont goto out; 416*05304565SCedric Chaumont } 417*05304565SCedric Chaumont 418*05304565SCedric Chaumont num_of_keys = (*operationSize-sizeof(TEE_OperationInfoMultiple))/ 419*05304565SCedric Chaumont sizeof(TEE_OperationInfoKey); 420*05304565SCedric Chaumont 421*05304565SCedric Chaumont if (num_of_keys > 2) { 422*05304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 423*05304565SCedric Chaumont goto out; 424*05304565SCedric Chaumont } 425*05304565SCedric Chaumont 426*05304565SCedric Chaumont /* Two keys flag (TEE_ALG_AES_XTS only) */ 427*05304565SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 428*05304565SCedric Chaumont 0 && 429*05304565SCedric Chaumont (num_of_keys != 2)) { 430*05304565SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 431*05304565SCedric Chaumont goto out; 432*05304565SCedric Chaumont } 433*05304565SCedric Chaumont 434*05304565SCedric Chaumont /* Clear */ 435*05304565SCedric Chaumont for (n = 0; n < num_of_keys; n++) { 436*05304565SCedric Chaumont operationInfoMultiple->keyInformation[n].keySize = 0; 437*05304565SCedric Chaumont operationInfoMultiple->keyInformation[n].requiredKeyUsage = 0; 438*05304565SCedric Chaumont } 439*05304565SCedric Chaumont 440*05304565SCedric Chaumont if (num_of_keys == 2) { 441*05304565SCedric Chaumont res = TEE_GetObjectInfo1(operation->key2, &key_info2); 442*05304565SCedric Chaumont /* Key2 is not a valid handle */ 443*05304565SCedric Chaumont if (res != TEE_SUCCESS) 444*05304565SCedric Chaumont goto out; 445*05304565SCedric Chaumont 446*05304565SCedric Chaumont operationInfoMultiple->keyInformation[1].keySize = 447*05304565SCedric Chaumont key_info2.keySize; 448*05304565SCedric Chaumont operationInfoMultiple->keyInformation[1].requiredKeyUsage = 449*05304565SCedric Chaumont operation->info.requiredKeyUsage; 450*05304565SCedric Chaumont } 451*05304565SCedric Chaumont 452*05304565SCedric Chaumont if (num_of_keys >= 1) { 453*05304565SCedric Chaumont res = TEE_GetObjectInfo1(operation->key1, &key_info1); 454*05304565SCedric Chaumont /* Key1 is not a valid handle */ 455*05304565SCedric Chaumont if (res != TEE_SUCCESS) { 456*05304565SCedric Chaumont if (num_of_keys == 2) { 457*05304565SCedric Chaumont operationInfoMultiple->keyInformation[1]. 458*05304565SCedric Chaumont keySize = 0; 459*05304565SCedric Chaumont operationInfoMultiple->keyInformation[1]. 460*05304565SCedric Chaumont requiredKeyUsage = 0; 461*05304565SCedric Chaumont } 462*05304565SCedric Chaumont goto out; 463*05304565SCedric Chaumont } 464*05304565SCedric Chaumont 465*05304565SCedric Chaumont operationInfoMultiple->keyInformation[0].keySize = 466*05304565SCedric Chaumont key_info1.keySize; 467*05304565SCedric Chaumont operationInfoMultiple->keyInformation[0].requiredKeyUsage = 468*05304565SCedric Chaumont operation->info.requiredKeyUsage; 469*05304565SCedric Chaumont } 470*05304565SCedric Chaumont 471*05304565SCedric Chaumont /* No key */ 472*05304565SCedric Chaumont operationInfoMultiple->algorithm = operation->info.algorithm; 473*05304565SCedric Chaumont operationInfoMultiple->operationClass = operation->info.operationClass; 474*05304565SCedric Chaumont operationInfoMultiple->mode = operation->info.mode; 475*05304565SCedric Chaumont operationInfoMultiple->digestLength = operation->info.digestLength; 476*05304565SCedric Chaumont operationInfoMultiple->maxKeySize = operation->info.maxKeySize; 477*05304565SCedric Chaumont operationInfoMultiple->handleState = operation->info.handleState; 478*05304565SCedric Chaumont operationInfoMultiple->operationState = operation->operationState; 479*05304565SCedric Chaumont operationInfoMultiple->numberOfKeys = num_of_keys; 480*05304565SCedric Chaumont 481*05304565SCedric Chaumont out: 482*05304565SCedric Chaumont if (res != TEE_SUCCESS && 483*05304565SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 484*05304565SCedric Chaumont TEE_Panic(0); 485*05304565SCedric Chaumont 486*05304565SCedric Chaumont return res; 487*05304565SCedric Chaumont } 488*05304565SCedric Chaumont 489b0104773SPascal Brand void TEE_ResetOperation(TEE_OperationHandle operation) 490b0104773SPascal Brand { 491b0104773SPascal Brand TEE_Result res; 492b0104773SPascal Brand 493b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 494b0104773SPascal Brand TEE_Panic(0); 495bf80076aSCedric Chaumont 496*05304565SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0) 497bf80076aSCedric Chaumont TEE_Panic(0); 498bf80076aSCedric 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); 503*05304565SCedric Chaumont operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 504*05304565SCedric Chaumont } else { 505b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 506b0104773SPascal Brand } 507*05304565SCedric 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 /* Operation is not a valid handle */ 517a57c1e2eSCedric Chaumont if (operation == TEE_HANDLE_NULL) { 518a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 519a57c1e2eSCedric Chaumont goto out; 520a57c1e2eSCedric Chaumont } 521a57c1e2eSCedric Chaumont 522a57c1e2eSCedric Chaumont /* Key is not initialized */ 523a57c1e2eSCedric Chaumont if (key == TEE_HANDLE_NULL) { 524a57c1e2eSCedric Chaumont /* Operation key cleared */ 525a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key1); 526a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 527a57c1e2eSCedric Chaumont goto out; 528a57c1e2eSCedric Chaumont } 529a57c1e2eSCedric Chaumont 530a57c1e2eSCedric Chaumont /* No key for digest operation */ 531a57c1e2eSCedric Chaumont if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 532a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 533a57c1e2eSCedric Chaumont goto out; 534a57c1e2eSCedric Chaumont } 535a57c1e2eSCedric Chaumont 536a57c1e2eSCedric Chaumont /* Two keys flag not expected (TEE_ALG_AES_XTS excluded) */ 537a57c1e2eSCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 538a57c1e2eSCedric Chaumont 0) { 539a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 540a57c1e2eSCedric Chaumont goto out; 541a57c1e2eSCedric Chaumont } 542a57c1e2eSCedric Chaumont 5437583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key, &key_info); 544a57c1e2eSCedric Chaumont /* Key is not a valid handle */ 5457583c59eSCedric Chaumont if (res != TEE_SUCCESS) 546a57c1e2eSCedric Chaumont goto out; 5477583c59eSCedric Chaumont 548b0104773SPascal Brand /* Supplied key has to meet required usage */ 549b0104773SPascal Brand if ((key_info.objectUsage & operation->info.requiredKeyUsage) != 550b0104773SPascal Brand operation->info.requiredKeyUsage) { 551a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 552a57c1e2eSCedric Chaumont goto out; 553b0104773SPascal Brand } 554b0104773SPascal Brand 555a57c1e2eSCedric Chaumont if (operation->info.maxKeySize < key_info.keySize) { 556a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 557a57c1e2eSCedric Chaumont goto out; 558a57c1e2eSCedric Chaumont } 559b0104773SPascal Brand 5607583c59eSCedric Chaumont key_size = key_info.keySize; 561b0104773SPascal Brand 562b0104773SPascal Brand TEE_ResetTransientObject(operation->key1); 563b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 564b0104773SPascal Brand 5657583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key1, key); 5667583c59eSCedric Chaumont if (res != TEE_SUCCESS) 567a57c1e2eSCedric Chaumont goto out; 5687583c59eSCedric Chaumont 569b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 570b0104773SPascal Brand 571b0104773SPascal Brand operation->info.keySize = key_size; 572b0104773SPascal Brand 5737583c59eSCedric Chaumont out: 574a57c1e2eSCedric Chaumont if (res != TEE_SUCCESS && 575a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 576a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 577a57c1e2eSCedric Chaumont TEE_Panic(0); 578a57c1e2eSCedric Chaumont 579a57c1e2eSCedric Chaumont return res; 580b0104773SPascal Brand } 581b0104773SPascal Brand 582b0104773SPascal Brand TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation, 583b0104773SPascal Brand TEE_ObjectHandle key1, TEE_ObjectHandle key2) 584b0104773SPascal Brand { 5857583c59eSCedric Chaumont TEE_Result res; 586b0104773SPascal Brand uint32_t key_size = 0; 587b0104773SPascal Brand TEE_ObjectInfo key_info1; 588b0104773SPascal Brand TEE_ObjectInfo key_info2; 589b0104773SPascal Brand 590a57c1e2eSCedric Chaumont /* Operation is not a valid handle */ 591a57c1e2eSCedric Chaumont if (operation == TEE_HANDLE_NULL) { 592a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 593a57c1e2eSCedric Chaumont goto out; 594a57c1e2eSCedric Chaumont } 595a57c1e2eSCedric Chaumont 596a57c1e2eSCedric Chaumont /* 597a57c1e2eSCedric Chaumont * Key1/Key2 and/or are not initialized and 598a57c1e2eSCedric Chaumont * Either both keys are NULL or both are not NULL 599a57c1e2eSCedric Chaumont */ 600a57c1e2eSCedric Chaumont if (key1 == TEE_HANDLE_NULL || key2 == TEE_HANDLE_NULL) { 601a57c1e2eSCedric Chaumont /* Clear operation key1 (if needed) */ 602a57c1e2eSCedric Chaumont if (key1 == TEE_HANDLE_NULL) 603a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key1); 604a57c1e2eSCedric Chaumont /* Clear operation key2 (if needed) */ 605a57c1e2eSCedric Chaumont if (key2 == TEE_HANDLE_NULL) 606a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key2); 607a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 608a57c1e2eSCedric Chaumont goto out; 609a57c1e2eSCedric Chaumont } 610a57c1e2eSCedric Chaumont 611a57c1e2eSCedric Chaumont /* No key for digest operation */ 612a57c1e2eSCedric Chaumont if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 613a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 614a57c1e2eSCedric Chaumont goto out; 615a57c1e2eSCedric Chaumont } 616a57c1e2eSCedric Chaumont 617a57c1e2eSCedric Chaumont /* Two keys flag expected (TEE_ALG_AES_XTS only) */ 618a57c1e2eSCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 619a57c1e2eSCedric Chaumont 0) { 620a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 621a57c1e2eSCedric Chaumont goto out; 622a57c1e2eSCedric Chaumont } 623a57c1e2eSCedric Chaumont 6247583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key1, &key_info1); 625a57c1e2eSCedric Chaumont /* Key1 is not a valid handle */ 6267583c59eSCedric Chaumont if (res != TEE_SUCCESS) 627a57c1e2eSCedric Chaumont goto out; 6287583c59eSCedric Chaumont 629b0104773SPascal Brand /* Supplied key has to meet required usage */ 630b0104773SPascal Brand if ((key_info1.objectUsage & operation->info. 631b0104773SPascal Brand requiredKeyUsage) != operation->info.requiredKeyUsage) { 632a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 633a57c1e2eSCedric Chaumont goto out; 634b0104773SPascal Brand } 635b0104773SPascal Brand 6367583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key2, &key_info2); 637a57c1e2eSCedric Chaumont /* Key2 is not a valid handle */ 6387583c59eSCedric Chaumont if (res != TEE_SUCCESS) { 6397583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) 6407583c59eSCedric Chaumont res = TEE_ERROR_CORRUPT_OBJECT_2; 641a57c1e2eSCedric Chaumont goto out; 6427583c59eSCedric Chaumont } 6437583c59eSCedric Chaumont 644b0104773SPascal Brand /* Supplied key has to meet required usage */ 645b0104773SPascal Brand if ((key_info2.objectUsage & operation->info. 646b0104773SPascal Brand requiredKeyUsage) != operation->info.requiredKeyUsage) { 647a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 648a57c1e2eSCedric Chaumont goto out; 649b0104773SPascal Brand } 650b0104773SPascal Brand 651b0104773SPascal Brand /* 652b0104773SPascal Brand * AES-XTS (the only multi key algorithm supported, requires the 653b0104773SPascal Brand * keys to be of equal size. 654b0104773SPascal Brand */ 655b0104773SPascal Brand if (operation->info.algorithm == TEE_ALG_AES_XTS && 656a57c1e2eSCedric Chaumont key_info1.keySize != key_info2.keySize) { 657a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 658a57c1e2eSCedric Chaumont goto out; 659b0104773SPascal Brand 660a57c1e2eSCedric Chaumont } 661a57c1e2eSCedric Chaumont 662a57c1e2eSCedric Chaumont if (operation->info.maxKeySize < key_info1.keySize) { 663a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 664a57c1e2eSCedric Chaumont goto out; 665a57c1e2eSCedric Chaumont } 666b0104773SPascal Brand 667b0104773SPascal Brand /* 668b0104773SPascal Brand * Odd that only the size of one key should be reported while 669b0104773SPascal Brand * size of two key are used when allocating the operation. 670b0104773SPascal Brand */ 6717583c59eSCedric Chaumont key_size = key_info1.keySize; 672b0104773SPascal Brand 673b0104773SPascal Brand TEE_ResetTransientObject(operation->key1); 674b0104773SPascal Brand TEE_ResetTransientObject(operation->key2); 675b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 676b0104773SPascal Brand 6777583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key1, key1); 6787583c59eSCedric Chaumont if (res != TEE_SUCCESS) 679a57c1e2eSCedric Chaumont goto out; 6807583c59eSCedric Chaumont 6817583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key2, key2); 6827583c59eSCedric Chaumont if (res != TEE_SUCCESS) { 6837583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) 6847583c59eSCedric Chaumont res = TEE_ERROR_CORRUPT_OBJECT_2; 685a57c1e2eSCedric Chaumont goto out; 6867583c59eSCedric Chaumont } 6877583c59eSCedric Chaumont 688b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 689b0104773SPascal Brand 690b0104773SPascal Brand operation->info.keySize = key_size; 691b0104773SPascal Brand 6927583c59eSCedric Chaumont out: 693a57c1e2eSCedric Chaumont if (res != TEE_SUCCESS && 694a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 695a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT_2 && 696a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE && 697a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE_2) 698a57c1e2eSCedric Chaumont TEE_Panic(0); 699a57c1e2eSCedric Chaumont 700a57c1e2eSCedric Chaumont return res; 701b0104773SPascal Brand } 702b0104773SPascal Brand 703b0104773SPascal Brand void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op) 704b0104773SPascal Brand { 705b0104773SPascal Brand TEE_Result res; 706b0104773SPascal Brand 707b0104773SPascal Brand if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL) 708b0104773SPascal Brand TEE_Panic(0); 709b0104773SPascal Brand if (dst_op->info.algorithm != src_op->info.algorithm) 710b0104773SPascal Brand TEE_Panic(0); 711b0104773SPascal Brand if (src_op->info.operationClass != TEE_OPERATION_DIGEST) { 712b0104773SPascal Brand TEE_ObjectHandle key1 = TEE_HANDLE_NULL; 713b0104773SPascal Brand TEE_ObjectHandle key2 = TEE_HANDLE_NULL; 714b0104773SPascal Brand 715b0104773SPascal Brand if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) { 716b0104773SPascal Brand key1 = src_op->key1; 717b0104773SPascal Brand key2 = src_op->key2; 718b0104773SPascal Brand } 719b0104773SPascal Brand 720b0104773SPascal Brand if ((src_op->info.handleState & 721b0104773SPascal Brand TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) { 722b0104773SPascal Brand TEE_SetOperationKey(dst_op, key1); 723b0104773SPascal Brand } else { 724b0104773SPascal Brand TEE_SetOperationKey2(dst_op, key1, key2); 725b0104773SPascal Brand } 726b0104773SPascal Brand } 727b0104773SPascal Brand dst_op->info.handleState = src_op->info.handleState; 728b0104773SPascal Brand dst_op->info.keySize = src_op->info.keySize; 729b0104773SPascal Brand 730b0104773SPascal Brand if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks || 731b0104773SPascal Brand dst_op->block_size != src_op->block_size) 732b0104773SPascal Brand TEE_Panic(0); 733b0104773SPascal Brand 734b0104773SPascal Brand if (dst_op->buffer != NULL) { 735b0104773SPascal Brand if (src_op->buffer == NULL) 736b0104773SPascal Brand TEE_Panic(0); 737b0104773SPascal Brand 738b0104773SPascal Brand memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs); 739b0104773SPascal Brand dst_op->buffer_offs = src_op->buffer_offs; 740b0104773SPascal Brand } else if (src_op->buffer != NULL) { 741b0104773SPascal Brand TEE_Panic(0); 742b0104773SPascal Brand } 743b0104773SPascal Brand 744b0104773SPascal Brand res = utee_cryp_state_copy(dst_op->state, src_op->state); 745b0104773SPascal Brand if (res != TEE_SUCCESS) 746b0104773SPascal Brand TEE_Panic(res); 747b0104773SPascal Brand } 748b0104773SPascal Brand 749b0104773SPascal Brand /* Cryptographic Operations API - Message Digest Functions */ 750b0104773SPascal Brand 751b0104773SPascal Brand void TEE_DigestUpdate(TEE_OperationHandle operation, 75279a3c601SCedric Chaumont void *chunk, uint32_t chunkSize) 753b0104773SPascal Brand { 75473d6c3baSJoakim Bech TEE_Result res = TEE_ERROR_GENERIC; 755b0104773SPascal Brand 75673d6c3baSJoakim Bech if (operation == TEE_HANDLE_NULL || 75773d6c3baSJoakim Bech operation->info.operationClass != TEE_OPERATION_DIGEST) 758b0104773SPascal Brand TEE_Panic(0); 75973d6c3baSJoakim Bech 760b0104773SPascal Brand res = utee_hash_update(operation->state, chunk, chunkSize); 761b0104773SPascal Brand if (res != TEE_SUCCESS) 762b0104773SPascal Brand TEE_Panic(res); 763b0104773SPascal Brand } 764b0104773SPascal Brand 765b0104773SPascal Brand TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void *chunk, 76679a3c601SCedric Chaumont uint32_t chunkLen, void *hash, uint32_t *hashLen) 767b0104773SPascal Brand { 76887c2f6b6SCedric Chaumont TEE_Result res; 76987c2f6b6SCedric Chaumont 77087c2f6b6SCedric Chaumont if ((operation == TEE_HANDLE_NULL) || 77187c2f6b6SCedric Chaumont (!chunk && chunkLen) || 77287c2f6b6SCedric Chaumont !hash || 77387c2f6b6SCedric Chaumont !hashLen || 77487c2f6b6SCedric Chaumont (operation->info.operationClass != TEE_OPERATION_DIGEST)) { 77587c2f6b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 77687c2f6b6SCedric Chaumont goto out; 77787c2f6b6SCedric Chaumont } 77887c2f6b6SCedric Chaumont 77987c2f6b6SCedric Chaumont res = utee_hash_final(operation->state, chunk, chunkLen, hash, 78087c2f6b6SCedric Chaumont hashLen); 78187c2f6b6SCedric Chaumont 78287c2f6b6SCedric Chaumont out: 78387c2f6b6SCedric Chaumont if (res != TEE_SUCCESS && 78487c2f6b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 785b0104773SPascal Brand TEE_Panic(0); 78673d6c3baSJoakim Bech 78787c2f6b6SCedric Chaumont return res; 788b0104773SPascal Brand } 789b0104773SPascal Brand 790b0104773SPascal Brand /* Cryptographic Operations API - Symmetric Cipher Functions */ 791b0104773SPascal Brand 79279a3c601SCedric Chaumont void TEE_CipherInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen) 793b0104773SPascal Brand { 794b0104773SPascal Brand TEE_Result res; 795b0104773SPascal Brand 796b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 797b0104773SPascal Brand TEE_Panic(0); 798b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_CIPHER) 799b0104773SPascal Brand TEE_Panic(0); 800b0104773SPascal Brand res = utee_cipher_init(operation->state, IV, IVLen); 801b0104773SPascal Brand if (res != TEE_SUCCESS) 802b0104773SPascal Brand TEE_Panic(res); 803b0104773SPascal Brand operation->buffer_offs = 0; 804b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 805b0104773SPascal Brand } 806b0104773SPascal Brand 807b0104773SPascal Brand static TEE_Result tee_buffer_update( 808b0104773SPascal Brand TEE_OperationHandle op, 809b0104773SPascal Brand TEE_Result(*update_func) (uint32_t state, const void *src, 8107f74c64aSPascal Brand size_t slen, void *dst, uint32_t *dlen), 811b0104773SPascal Brand const void *src_data, size_t src_len, 8127f74c64aSPascal Brand void *dest_data, uint32_t *dest_len) 813b0104773SPascal Brand { 814b0104773SPascal Brand TEE_Result res; 815b0104773SPascal Brand const uint8_t *src = src_data; 816b0104773SPascal Brand size_t slen = src_len; 817b0104773SPascal Brand uint8_t *dst = dest_data; 818b0104773SPascal Brand size_t dlen = *dest_len; 819b0104773SPascal Brand size_t acc_dlen = 0; 8207f74c64aSPascal Brand uint32_t tmp_dlen; 821b0104773SPascal Brand size_t l; 822b0104773SPascal Brand size_t buffer_size; 823d3588802SPascal Brand size_t buffer_left; 824b0104773SPascal Brand 825d3588802SPascal Brand if (op->buffer_two_blocks) { 826b0104773SPascal Brand buffer_size = op->block_size * 2; 827d3588802SPascal Brand buffer_left = 1; 828d3588802SPascal Brand } else { 829b0104773SPascal Brand buffer_size = op->block_size; 830d3588802SPascal Brand buffer_left = 0; 831d3588802SPascal Brand } 832b0104773SPascal Brand 833b0104773SPascal Brand if (op->buffer_offs > 0) { 834b0104773SPascal Brand /* Fill up complete block */ 835b0104773SPascal Brand if (op->buffer_offs < op->block_size) 836b0104773SPascal Brand l = MIN(slen, op->block_size - op->buffer_offs); 837b0104773SPascal Brand else 838b0104773SPascal Brand l = MIN(slen, buffer_size - op->buffer_offs); 839b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, l); 840b0104773SPascal Brand op->buffer_offs += l; 841b0104773SPascal Brand src += l; 842b0104773SPascal Brand slen -= l; 843b0104773SPascal Brand if ((op->buffer_offs % op->block_size) != 0) 844b0104773SPascal Brand goto out; /* Nothing left to do */ 845b0104773SPascal Brand } 846b0104773SPascal Brand 847b0104773SPascal Brand /* If we can feed from buffer */ 848d3588802SPascal Brand if ((op->buffer_offs > 0) && 849d3588802SPascal Brand ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) { 8502ff3fdbbSPascal Brand l = ROUNDUP(op->buffer_offs + slen - buffer_size, 851b0104773SPascal Brand op->block_size); 852b0104773SPascal Brand l = MIN(op->buffer_offs, l); 853b0104773SPascal Brand tmp_dlen = dlen; 854b0104773SPascal Brand res = update_func(op->state, op->buffer, l, dst, &tmp_dlen); 855b0104773SPascal Brand if (res != TEE_SUCCESS) 856b0104773SPascal Brand TEE_Panic(res); 857b0104773SPascal Brand dst += tmp_dlen; 858b0104773SPascal Brand dlen -= tmp_dlen; 859b0104773SPascal Brand acc_dlen += tmp_dlen; 860b0104773SPascal Brand op->buffer_offs -= l; 861b0104773SPascal Brand if (op->buffer_offs > 0) { 862b0104773SPascal Brand /* 863b0104773SPascal Brand * Slen is small enough to be contained in rest buffer. 864b0104773SPascal Brand */ 865b0104773SPascal Brand memcpy(op->buffer, op->buffer + l, buffer_size - l); 866b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 867b0104773SPascal Brand op->buffer_offs += slen; 868b0104773SPascal Brand goto out; /* Nothing left to do */ 869b0104773SPascal Brand } 870b0104773SPascal Brand } 871b0104773SPascal Brand 872d3588802SPascal Brand if (slen >= (buffer_size + buffer_left)) { 873b0104773SPascal Brand /* Buffer is empty, feed as much as possible from src */ 874b0104773SPascal Brand if (TEE_ALIGNMENT_IS_OK(src, uint32_t)) { 8752ff3fdbbSPascal Brand l = ROUNDUP(slen - buffer_size + 1, op->block_size); 876b0104773SPascal Brand 877b0104773SPascal Brand tmp_dlen = dlen; 878b0104773SPascal Brand res = update_func(op->state, src, l, dst, &tmp_dlen); 879b0104773SPascal Brand if (res != TEE_SUCCESS) 880b0104773SPascal Brand TEE_Panic(res); 881b0104773SPascal Brand src += l; 882b0104773SPascal Brand slen -= l; 883b0104773SPascal Brand dst += tmp_dlen; 884b0104773SPascal Brand dlen -= tmp_dlen; 885b0104773SPascal Brand acc_dlen += tmp_dlen; 886b0104773SPascal Brand } else { 887b0104773SPascal Brand /* 888b0104773SPascal Brand * Supplied data isn't well aligned, we're forced to 889b0104773SPascal Brand * feed through the buffer. 890b0104773SPascal Brand */ 891b0104773SPascal Brand while (slen >= op->block_size) { 892b0104773SPascal Brand memcpy(op->buffer, src, op->block_size); 893b0104773SPascal Brand 894b0104773SPascal Brand tmp_dlen = dlen; 895b0104773SPascal Brand res = 896b0104773SPascal Brand update_func(op->state, op->buffer, 897b0104773SPascal Brand op->block_size, dst, &tmp_dlen); 898b0104773SPascal Brand if (res != TEE_SUCCESS) 899b0104773SPascal Brand TEE_Panic(res); 900b0104773SPascal Brand src += op->block_size; 901b0104773SPascal Brand slen -= op->block_size; 902b0104773SPascal Brand dst += tmp_dlen; 903b0104773SPascal Brand dlen -= tmp_dlen; 904b0104773SPascal Brand acc_dlen += tmp_dlen; 905b0104773SPascal Brand } 906b0104773SPascal Brand } 907b0104773SPascal Brand } 908b0104773SPascal Brand 909b0104773SPascal Brand /* Slen is small enough to be contained in buffer. */ 910b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 911b0104773SPascal Brand op->buffer_offs += slen; 912b0104773SPascal Brand 913b0104773SPascal Brand out: 914b0104773SPascal Brand *dest_len = acc_dlen; 915b0104773SPascal Brand return TEE_SUCCESS; 916b0104773SPascal Brand } 917b0104773SPascal Brand 918b0104773SPascal Brand TEE_Result TEE_CipherUpdate(TEE_OperationHandle op, const void *srcData, 91979a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 920b0104773SPascal Brand { 921dea1f2b6SCedric Chaumont TEE_Result res; 922b0104773SPascal Brand size_t req_dlen; 923b0104773SPascal Brand 924dea1f2b6SCedric Chaumont if (op == TEE_HANDLE_NULL || 925dea1f2b6SCedric Chaumont (srcData == NULL && srcLen != 0) || 926dea1f2b6SCedric Chaumont destLen == NULL || 927dea1f2b6SCedric Chaumont (destData == NULL && *destLen != 0)) { 928dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 929dea1f2b6SCedric Chaumont goto out; 930dea1f2b6SCedric Chaumont } 931dea1f2b6SCedric Chaumont 932dea1f2b6SCedric Chaumont if (op->info.operationClass != TEE_OPERATION_CIPHER) { 933dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 934dea1f2b6SCedric Chaumont goto out; 935dea1f2b6SCedric Chaumont } 936dea1f2b6SCedric Chaumont 937dea1f2b6SCedric Chaumont if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 938dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 939dea1f2b6SCedric Chaumont goto out; 940dea1f2b6SCedric Chaumont } 941b0104773SPascal Brand 942b0104773SPascal Brand /* Calculate required dlen */ 943b0104773SPascal Brand req_dlen = ((op->buffer_offs + srcLen) / op->block_size) * 944b0104773SPascal Brand op->block_size; 945b0104773SPascal Brand if (op->buffer_two_blocks) { 946b0104773SPascal Brand if (req_dlen > op->block_size * 2) 947b0104773SPascal Brand req_dlen -= op->block_size * 2; 948b0104773SPascal Brand else 949b0104773SPascal Brand req_dlen = 0; 950b0104773SPascal Brand } 951b0104773SPascal Brand /* 952b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 953b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 954b0104773SPascal Brand * can't restore sync with this API. 955b0104773SPascal Brand */ 956b0104773SPascal Brand if (*destLen < req_dlen) { 957b0104773SPascal Brand *destLen = req_dlen; 958dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 959dea1f2b6SCedric Chaumont goto out; 960b0104773SPascal Brand } 961b0104773SPascal Brand 962dea1f2b6SCedric Chaumont res = tee_buffer_update(op, utee_cipher_update, srcData, srcLen, 963dea1f2b6SCedric Chaumont destData, destLen); 964b0104773SPascal Brand 965dea1f2b6SCedric Chaumont out: 966dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 967dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 968dea1f2b6SCedric Chaumont TEE_Panic(0); 969dea1f2b6SCedric Chaumont 970dea1f2b6SCedric Chaumont return res; 971b0104773SPascal Brand } 972b0104773SPascal Brand 973b0104773SPascal Brand TEE_Result TEE_CipherDoFinal(TEE_OperationHandle op, 97479a3c601SCedric Chaumont const void *srcData, uint32_t srcLen, void *destData, 97579a3c601SCedric Chaumont uint32_t *destLen) 976b0104773SPascal Brand { 977b0104773SPascal Brand TEE_Result res; 978b0104773SPascal Brand uint8_t *dst = destData; 979b0104773SPascal Brand size_t acc_dlen = 0; 9807f74c64aSPascal Brand uint32_t tmp_dlen; 981b0104773SPascal Brand size_t req_dlen; 982b0104773SPascal Brand 983dea1f2b6SCedric Chaumont if (op == TEE_HANDLE_NULL || 984dea1f2b6SCedric Chaumont (srcData == NULL && srcLen != 0) || 985dea1f2b6SCedric Chaumont destLen == NULL || 986dea1f2b6SCedric Chaumont (destData == NULL && *destLen != 0)) { 987dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 988dea1f2b6SCedric Chaumont goto out; 989dea1f2b6SCedric Chaumont } 990dea1f2b6SCedric Chaumont 991dea1f2b6SCedric Chaumont if (op->info.operationClass != TEE_OPERATION_CIPHER) { 992dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 993dea1f2b6SCedric Chaumont goto out; 994dea1f2b6SCedric Chaumont } 995dea1f2b6SCedric Chaumont 996dea1f2b6SCedric Chaumont if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 997dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 998dea1f2b6SCedric Chaumont goto out; 999dea1f2b6SCedric Chaumont } 1000b0104773SPascal Brand 1001b0104773SPascal Brand /* 1002b0104773SPascal Brand * Check that the final block doesn't require padding for those 1003b0104773SPascal Brand * algorithms that requires client to supply padding. 1004b0104773SPascal Brand */ 1005b0104773SPascal Brand if (op->info.algorithm == TEE_ALG_AES_ECB_NOPAD || 1006b0104773SPascal Brand op->info.algorithm == TEE_ALG_AES_CBC_NOPAD || 1007b0104773SPascal Brand op->info.algorithm == TEE_ALG_DES_ECB_NOPAD || 1008b0104773SPascal Brand op->info.algorithm == TEE_ALG_DES_CBC_NOPAD || 1009b0104773SPascal Brand op->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || 1010b0104773SPascal Brand op->info.algorithm == TEE_ALG_DES3_CBC_NOPAD) { 1011dea1f2b6SCedric Chaumont if (((op->buffer_offs + srcLen) % op->block_size) != 0) { 1012dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1013dea1f2b6SCedric Chaumont goto out; 1014dea1f2b6SCedric Chaumont } 1015b0104773SPascal Brand } 1016b0104773SPascal Brand 1017b0104773SPascal Brand /* 1018b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1019b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1020b0104773SPascal Brand * can't restore sync with this API. 1021b0104773SPascal Brand */ 1022b0104773SPascal Brand req_dlen = op->buffer_offs + srcLen; 1023b0104773SPascal Brand if (*destLen < req_dlen) { 1024b0104773SPascal Brand *destLen = req_dlen; 1025dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1026dea1f2b6SCedric Chaumont goto out; 1027b0104773SPascal Brand } 1028b0104773SPascal Brand 1029b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1030dea1f2b6SCedric Chaumont res = tee_buffer_update(op, utee_cipher_update, srcData, srcLen, dst, 1031b0104773SPascal Brand &tmp_dlen); 1032dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS) 1033dea1f2b6SCedric Chaumont goto out; 1034dea1f2b6SCedric Chaumont 1035b0104773SPascal Brand dst += tmp_dlen; 1036b0104773SPascal Brand acc_dlen += tmp_dlen; 1037b0104773SPascal Brand 1038b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1039b0104773SPascal Brand res = utee_cipher_final(op->state, op->buffer, op->buffer_offs, 1040b0104773SPascal Brand dst, &tmp_dlen); 1041b0104773SPascal Brand if (res != TEE_SUCCESS) 1042dea1f2b6SCedric Chaumont goto out; 1043dea1f2b6SCedric Chaumont 1044b0104773SPascal Brand acc_dlen += tmp_dlen; 1045b0104773SPascal Brand 1046b0104773SPascal Brand op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1047b0104773SPascal Brand *destLen = acc_dlen; 1048dea1f2b6SCedric Chaumont 1049dea1f2b6SCedric Chaumont out: 1050dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 1051dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1052dea1f2b6SCedric Chaumont TEE_Panic(0); 1053dea1f2b6SCedric Chaumont 1054dea1f2b6SCedric Chaumont return res; 1055b0104773SPascal Brand } 1056b0104773SPascal Brand 1057b0104773SPascal Brand /* Cryptographic Operations API - MAC Functions */ 1058b0104773SPascal Brand 105928e0efc6SCedric Chaumont void TEE_MACInit(TEE_OperationHandle operation, void *IV, uint32_t IVLen) 1060b0104773SPascal Brand { 1061b0104773SPascal Brand TEE_Result res; 1062b0104773SPascal Brand 1063b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 1064b0104773SPascal Brand TEE_Panic(0); 106528e0efc6SCedric Chaumont if (!operation->key1) 1066b0104773SPascal Brand TEE_Panic(0); 1067b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_MAC) 1068b0104773SPascal Brand TEE_Panic(0); 106928e0efc6SCedric Chaumont /* 107028e0efc6SCedric Chaumont * Note : IV and IVLen are never used in current implementation 107128e0efc6SCedric Chaumont * This is why coherent values of IV and IVLen are not checked 107228e0efc6SCedric Chaumont */ 1073b0104773SPascal Brand res = utee_hash_init(operation->state, IV, IVLen); 1074b0104773SPascal Brand if (res != TEE_SUCCESS) 1075b0104773SPascal Brand TEE_Panic(res); 1076b0104773SPascal Brand operation->buffer_offs = 0; 1077b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 1078b0104773SPascal Brand } 1079b0104773SPascal Brand 108028e0efc6SCedric Chaumont void TEE_MACUpdate(TEE_OperationHandle operation, void *chunk, 108128e0efc6SCedric Chaumont uint32_t chunkSize) 1082b0104773SPascal Brand { 1083b0104773SPascal Brand TEE_Result res; 1084b0104773SPascal Brand 108528e0efc6SCedric Chaumont if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0)) 1086b0104773SPascal Brand TEE_Panic(0); 108728e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) 1088b0104773SPascal Brand TEE_Panic(0); 108928e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1090b0104773SPascal Brand TEE_Panic(0); 1091b0104773SPascal Brand 109228e0efc6SCedric Chaumont res = utee_hash_update(operation->state, chunk, chunkSize); 1093b0104773SPascal Brand if (res != TEE_SUCCESS) 1094b0104773SPascal Brand TEE_Panic(res); 1095b0104773SPascal Brand } 1096b0104773SPascal Brand 109728e0efc6SCedric Chaumont TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation, 109828e0efc6SCedric Chaumont void *message, uint32_t messageLen, 109979a3c601SCedric Chaumont void *mac, uint32_t *macLen) 1100b0104773SPascal Brand { 1101b0104773SPascal Brand TEE_Result res; 1102b0104773SPascal Brand 110328e0efc6SCedric Chaumont if (operation == TEE_HANDLE_NULL || 110428e0efc6SCedric Chaumont (message == NULL && messageLen != 0) || 110528e0efc6SCedric Chaumont mac == NULL || 110628e0efc6SCedric Chaumont macLen == NULL) { 110728e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 110828e0efc6SCedric Chaumont goto out; 110928e0efc6SCedric Chaumont } 1110b0104773SPascal Brand 111128e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 111228e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 111328e0efc6SCedric Chaumont goto out; 111428e0efc6SCedric Chaumont } 111528e0efc6SCedric Chaumont 111628e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 111728e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 111828e0efc6SCedric Chaumont goto out; 111928e0efc6SCedric Chaumont } 112028e0efc6SCedric Chaumont 112128e0efc6SCedric Chaumont res = utee_hash_final(operation->state, message, messageLen, mac, 112228e0efc6SCedric Chaumont macLen); 112328e0efc6SCedric Chaumont if (res != TEE_SUCCESS) 112428e0efc6SCedric Chaumont goto out; 112528e0efc6SCedric Chaumont 112628e0efc6SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 112728e0efc6SCedric Chaumont 112828e0efc6SCedric Chaumont out: 112928e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 113028e0efc6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 113128e0efc6SCedric Chaumont TEE_Panic(res); 113228e0efc6SCedric Chaumont 1133b0104773SPascal Brand return res; 1134b0104773SPascal Brand } 1135b0104773SPascal Brand 1136b0104773SPascal Brand TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation, 113728e0efc6SCedric Chaumont void *message, uint32_t messageLen, 113828e0efc6SCedric Chaumont void *mac, uint32_t macLen) 1139b0104773SPascal Brand { 1140b0104773SPascal Brand TEE_Result res; 1141b0104773SPascal Brand uint8_t computed_mac[TEE_MAX_HASH_SIZE]; 11427f74c64aSPascal Brand uint32_t computed_mac_size = TEE_MAX_HASH_SIZE; 1143b0104773SPascal Brand 114428e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 114528e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 114628e0efc6SCedric Chaumont goto out; 114728e0efc6SCedric Chaumont } 114828e0efc6SCedric Chaumont 114928e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 115028e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 115128e0efc6SCedric Chaumont goto out; 115228e0efc6SCedric Chaumont } 115328e0efc6SCedric Chaumont 1154b0104773SPascal Brand res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac, 1155b0104773SPascal Brand &computed_mac_size); 1156b0104773SPascal Brand if (res != TEE_SUCCESS) 115728e0efc6SCedric Chaumont goto out; 115828e0efc6SCedric Chaumont 115928e0efc6SCedric Chaumont if (computed_mac_size != macLen) { 116028e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 116128e0efc6SCedric Chaumont goto out; 116228e0efc6SCedric Chaumont } 116328e0efc6SCedric Chaumont 116428e0efc6SCedric Chaumont if (buf_compare_ct(mac, computed_mac, computed_mac_size) != 0) { 116528e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 116628e0efc6SCedric Chaumont goto out; 116728e0efc6SCedric Chaumont } 116828e0efc6SCedric Chaumont 116928e0efc6SCedric Chaumont out: 117028e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 117128e0efc6SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 117228e0efc6SCedric Chaumont TEE_Panic(res); 117328e0efc6SCedric Chaumont 1174b0104773SPascal Brand return res; 1175b0104773SPascal Brand } 1176b0104773SPascal Brand 1177b0104773SPascal Brand /* Cryptographic Operations API - Authenticated Encryption Functions */ 1178b0104773SPascal Brand 1179b5816c88SCedric Chaumont TEE_Result TEE_AEInit(TEE_OperationHandle operation, void *nonce, 118079a3c601SCedric Chaumont uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen, 1181b0104773SPascal Brand uint32_t payloadLen) 1182b0104773SPascal Brand { 1183b0104773SPascal Brand TEE_Result res; 1184b0104773SPascal Brand 1185b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || nonce == NULL) { 1186b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1187b5816c88SCedric Chaumont goto out; 1188b5816c88SCedric Chaumont } 1189b5816c88SCedric Chaumont 1190b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1191b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1192b5816c88SCedric Chaumont goto out; 1193b5816c88SCedric Chaumont } 1194b0104773SPascal Brand 1195b0104773SPascal Brand /* 1196b0104773SPascal Brand * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core 1197b0104773SPascal Brand * in the implementation. But AES-GCM spec doesn't specify the tag len 1198b0104773SPascal Brand * according to the same principle so we have to check here instead to 1199b0104773SPascal Brand * be GP compliant. 1200b0104773SPascal Brand */ 1201b5816c88SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_GCM) { 1202b0104773SPascal Brand /* 1203b0104773SPascal Brand * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96 1204b0104773SPascal Brand */ 1205b5816c88SCedric Chaumont if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) { 1206b5816c88SCedric Chaumont res = TEE_ERROR_NOT_SUPPORTED; 1207b5816c88SCedric Chaumont goto out; 1208b5816c88SCedric Chaumont } 1209b0104773SPascal Brand } 1210b0104773SPascal Brand 1211b5816c88SCedric Chaumont res = utee_authenc_init(operation->state, nonce, nonceLen, 1212b5816c88SCedric Chaumont tagLen / 8, AADLen, payloadLen); 1213b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1214b5816c88SCedric Chaumont goto out; 1215b5816c88SCedric Chaumont 1216b5816c88SCedric Chaumont operation->ae_tag_len = tagLen / 8; 1217b5816c88SCedric Chaumont operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 1218b5816c88SCedric Chaumont 1219b5816c88SCedric Chaumont out: 1220b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1221b5816c88SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 1222b0104773SPascal Brand TEE_Panic(res); 1223b5816c88SCedric Chaumont 1224b0104773SPascal Brand return res; 1225b0104773SPascal Brand } 1226b0104773SPascal Brand 1227b5816c88SCedric Chaumont void TEE_AEUpdateAAD(TEE_OperationHandle operation, void *AADdata, 122879a3c601SCedric Chaumont uint32_t AADdataLen) 1229b0104773SPascal Brand { 1230b0104773SPascal Brand TEE_Result res; 1231b0104773SPascal Brand 1232b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1233b5816c88SCedric Chaumont (AADdata == NULL && AADdataLen != 0)) 1234b0104773SPascal Brand TEE_Panic(0); 1235b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) 1236b0104773SPascal Brand TEE_Panic(0); 1237b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1238b0104773SPascal Brand TEE_Panic(0); 1239b0104773SPascal Brand 1240b5816c88SCedric Chaumont res = utee_authenc_update_aad(operation->state, AADdata, AADdataLen); 1241b0104773SPascal Brand if (res != TEE_SUCCESS) 1242b0104773SPascal Brand TEE_Panic(res); 1243b0104773SPascal Brand } 1244b0104773SPascal Brand 1245b5816c88SCedric Chaumont TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, void *srcData, 124679a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 1247b0104773SPascal Brand { 1248b5816c88SCedric Chaumont TEE_Result res; 1249b0104773SPascal Brand size_t req_dlen; 1250b0104773SPascal Brand 1251b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1252b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1253b5816c88SCedric Chaumont destLen == NULL || 1254b5816c88SCedric Chaumont (destData == NULL && *destLen != 0)) { 1255b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1256b5816c88SCedric Chaumont goto out; 1257b5816c88SCedric Chaumont } 1258b5816c88SCedric Chaumont 1259b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1260b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1261b5816c88SCedric Chaumont goto out; 1262b5816c88SCedric Chaumont } 1263b5816c88SCedric Chaumont 1264b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1265b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1266b5816c88SCedric Chaumont goto out; 1267b5816c88SCedric Chaumont } 1268b0104773SPascal Brand 1269b0104773SPascal Brand /* 1270b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1271b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1272b0104773SPascal Brand * can't restore sync with this API. 1273b0104773SPascal Brand */ 1274b5816c88SCedric Chaumont req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen, 1275b5816c88SCedric Chaumont operation->block_size); 1276b0104773SPascal Brand if (*destLen < req_dlen) { 1277b0104773SPascal Brand *destLen = req_dlen; 1278b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1279b5816c88SCedric Chaumont goto out; 1280b0104773SPascal Brand } 1281b0104773SPascal Brand 1282b5816c88SCedric Chaumont res = tee_buffer_update(operation, utee_authenc_update_payload, srcData, 1283b5816c88SCedric Chaumont srcLen, destData, destLen); 1284b0104773SPascal Brand 1285b5816c88SCedric Chaumont out: 1286b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1287b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1288b5816c88SCedric Chaumont TEE_Panic(res); 1289b5816c88SCedric Chaumont 1290b5816c88SCedric Chaumont return res; 1291b0104773SPascal Brand } 1292b0104773SPascal Brand 1293b5816c88SCedric Chaumont TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation, 1294b5816c88SCedric Chaumont void *srcData, uint32_t srcLen, 129579a3c601SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 129679a3c601SCedric Chaumont uint32_t *tagLen) 1297b0104773SPascal Brand { 1298b0104773SPascal Brand TEE_Result res; 1299b0104773SPascal Brand uint8_t *dst = destData; 1300b0104773SPascal Brand size_t acc_dlen = 0; 13017f74c64aSPascal Brand uint32_t tmp_dlen; 1302b0104773SPascal Brand size_t req_dlen; 1303b0104773SPascal Brand 1304b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1305b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1306b5816c88SCedric Chaumont destLen == NULL || 1307b5816c88SCedric Chaumont (destData == NULL && *destLen != 0) || 1308b5816c88SCedric Chaumont tag == NULL || tagLen == NULL) { 1309b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1310b5816c88SCedric Chaumont goto out; 1311b5816c88SCedric Chaumont } 1312b5816c88SCedric Chaumont 1313b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1314b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1315b5816c88SCedric Chaumont goto out; 1316b5816c88SCedric Chaumont } 1317b5816c88SCedric Chaumont 1318b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1319b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1320b5816c88SCedric Chaumont goto out; 1321b5816c88SCedric Chaumont } 1322b0104773SPascal Brand 1323b0104773SPascal Brand /* 1324b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1325b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1326b0104773SPascal Brand * can't restore sync with this API. 1327b0104773SPascal Brand */ 1328b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1329b0104773SPascal Brand if (*destLen < req_dlen) { 1330b0104773SPascal Brand *destLen = req_dlen; 1331b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1332b5816c88SCedric Chaumont goto out; 1333b0104773SPascal Brand } 1334b0104773SPascal Brand 1335b0104773SPascal Brand /* 1336b0104773SPascal Brand * Need to check this before update_payload since sync would be lost if 1337b0104773SPascal Brand * we return short buffer after that. 1338b0104773SPascal Brand */ 1339b5816c88SCedric Chaumont if (*tagLen < operation->ae_tag_len) { 1340b5816c88SCedric Chaumont *tagLen = operation->ae_tag_len; 1341b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1342b5816c88SCedric Chaumont goto out; 1343b0104773SPascal Brand } 1344b0104773SPascal Brand 1345b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1346b5816c88SCedric Chaumont res = tee_buffer_update(operation, utee_authenc_update_payload, srcData, 1347b5816c88SCedric Chaumont srcLen, dst, &tmp_dlen); 1348b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1349b5816c88SCedric Chaumont goto out; 1350b5816c88SCedric Chaumont 1351b0104773SPascal Brand dst += tmp_dlen; 1352b0104773SPascal Brand acc_dlen += tmp_dlen; 1353b0104773SPascal Brand 1354b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1355b5816c88SCedric Chaumont res = utee_authenc_enc_final(operation->state, operation->buffer, 1356b5816c88SCedric Chaumont operation->buffer_offs, dst, &tmp_dlen, 1357b5816c88SCedric Chaumont tag, tagLen); 1358b0104773SPascal Brand if (res != TEE_SUCCESS) 1359b5816c88SCedric Chaumont goto out; 1360b0104773SPascal Brand 1361b5816c88SCedric Chaumont acc_dlen += tmp_dlen; 1362b0104773SPascal Brand *destLen = acc_dlen; 1363b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1364b5816c88SCedric Chaumont 1365b5816c88SCedric Chaumont out: 1366b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1367b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1368b5816c88SCedric Chaumont TEE_Panic(res); 1369b0104773SPascal Brand 1370b0104773SPascal Brand return res; 1371b0104773SPascal Brand } 1372b0104773SPascal Brand 1373b5816c88SCedric Chaumont TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation, 1374b5816c88SCedric Chaumont void *srcData, uint32_t srcLen, 1375b5816c88SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 137679a3c601SCedric Chaumont uint32_t tagLen) 1377b0104773SPascal Brand { 1378b0104773SPascal Brand TEE_Result res; 1379b0104773SPascal Brand uint8_t *dst = destData; 1380b0104773SPascal Brand size_t acc_dlen = 0; 13817f74c64aSPascal Brand uint32_t tmp_dlen; 1382b0104773SPascal Brand size_t req_dlen; 1383b0104773SPascal Brand 1384b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1385b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1386b5816c88SCedric Chaumont destLen == NULL || 1387b5816c88SCedric Chaumont (destData == NULL && *destLen != 0) || 1388b5816c88SCedric Chaumont (tag == NULL && tagLen != 0)) { 1389b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1390b5816c88SCedric Chaumont goto out; 1391b5816c88SCedric Chaumont } 1392b5816c88SCedric Chaumont 1393b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1394b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1395b5816c88SCedric Chaumont goto out; 1396b5816c88SCedric Chaumont } 1397b5816c88SCedric Chaumont 1398b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1399b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1400b5816c88SCedric Chaumont goto out; 1401b5816c88SCedric Chaumont } 1402b0104773SPascal Brand 1403b0104773SPascal Brand /* 1404b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1405b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1406b0104773SPascal Brand * can't restore sync with this API. 1407b0104773SPascal Brand */ 1408b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1409b0104773SPascal Brand if (*destLen < req_dlen) { 1410b0104773SPascal Brand *destLen = req_dlen; 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_dec_final(operation->state, operation->buffer, 1426b5816c88SCedric Chaumont operation->buffer_offs, dst, &tmp_dlen, 1427b5816c88SCedric Chaumont tag, tagLen); 1428b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1429b5816c88SCedric Chaumont goto out; 1430b5816c88SCedric Chaumont 1431b0104773SPascal Brand /* Supplied tagLen should match what we initiated with */ 1432b5816c88SCedric Chaumont if (tagLen != operation->ae_tag_len) 1433b0104773SPascal Brand res = TEE_ERROR_MAC_INVALID; 1434b0104773SPascal Brand 1435b0104773SPascal Brand acc_dlen += tmp_dlen; 1436b0104773SPascal Brand 1437b0104773SPascal Brand *destLen = acc_dlen; 1438b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1439b5816c88SCedric Chaumont 1440b5816c88SCedric Chaumont out: 1441b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1442b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER && 1443b5816c88SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 1444b5816c88SCedric Chaumont TEE_Panic(res); 1445b0104773SPascal Brand 1446b0104773SPascal Brand return res; 1447b0104773SPascal Brand } 1448b0104773SPascal Brand 1449b0104773SPascal Brand /* Cryptographic Operations API - Asymmetric Functions */ 1450b0104773SPascal Brand 145112e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation, 145212e66b6fSCedric Chaumont TEE_Attribute *params, 145312e66b6fSCedric Chaumont uint32_t paramCount, void *srcData, 145479a3c601SCedric Chaumont uint32_t srcLen, void *destData, 145579a3c601SCedric Chaumont uint32_t *destLen) 1456b0104773SPascal Brand { 1457b0104773SPascal Brand TEE_Result res; 1458b0104773SPascal Brand 145912e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1460b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 1461b0104773SPascal Brand TEE_Panic(0); 146212e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1463b0104773SPascal Brand TEE_Panic(0); 146412e66b6fSCedric Chaumont if (!operation->key1) 1465b0104773SPascal Brand TEE_Panic(0); 146612e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 146712e66b6fSCedric Chaumont TEE_Panic(0); 146812e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_ENCRYPT) 1469b0104773SPascal Brand TEE_Panic(0); 1470b0104773SPascal Brand 147112e66b6fSCedric Chaumont res = utee_asymm_operate(operation->state, params, paramCount, srcData, 147212e66b6fSCedric Chaumont srcLen, destData, destLen); 147312e66b6fSCedric Chaumont 14748844ebfcSPascal Brand if (res != TEE_SUCCESS && 14758844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 14768844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1477b0104773SPascal Brand TEE_Panic(res); 147812e66b6fSCedric Chaumont 1479b0104773SPascal Brand return res; 1480b0104773SPascal Brand } 1481b0104773SPascal Brand 148212e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation, 148312e66b6fSCedric Chaumont TEE_Attribute *params, 148412e66b6fSCedric Chaumont uint32_t paramCount, void *srcData, 148579a3c601SCedric Chaumont uint32_t srcLen, void *destData, 148679a3c601SCedric Chaumont uint32_t *destLen) 1487b0104773SPascal Brand { 1488b0104773SPascal Brand TEE_Result res; 1489b0104773SPascal Brand 149012e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1491b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 1492b0104773SPascal Brand TEE_Panic(0); 149312e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1494b0104773SPascal Brand TEE_Panic(0); 149512e66b6fSCedric Chaumont if (!operation->key1) 1496b0104773SPascal Brand TEE_Panic(0); 149712e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 149812e66b6fSCedric Chaumont TEE_Panic(0); 149912e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_DECRYPT) 1500b0104773SPascal Brand TEE_Panic(0); 1501b0104773SPascal Brand 150212e66b6fSCedric Chaumont res = utee_asymm_operate(operation->state, params, paramCount, srcData, 150312e66b6fSCedric Chaumont srcLen, destData, destLen); 150412e66b6fSCedric Chaumont 15058844ebfcSPascal Brand if (res != TEE_SUCCESS && 15068844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 15078844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1508b0104773SPascal Brand TEE_Panic(res); 150912e66b6fSCedric Chaumont 1510b0104773SPascal Brand return res; 1511b0104773SPascal Brand } 1512b0104773SPascal Brand 151312e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation, 151412e66b6fSCedric Chaumont TEE_Attribute *params, 151512e66b6fSCedric Chaumont uint32_t paramCount, void *digest, 151679a3c601SCedric Chaumont uint32_t digestLen, void *signature, 151779a3c601SCedric Chaumont uint32_t *signatureLen) 1518b0104773SPascal Brand { 1519b0104773SPascal Brand TEE_Result res; 1520b0104773SPascal Brand 152112e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || 152212e66b6fSCedric Chaumont (digest == NULL && digestLen != 0) || 1523b0104773SPascal Brand signature == NULL || signatureLen == NULL) 1524b0104773SPascal Brand TEE_Panic(0); 152512e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1526b0104773SPascal Brand TEE_Panic(0); 152712e66b6fSCedric Chaumont if (!operation->key1) 1528b0104773SPascal Brand TEE_Panic(0); 152912e66b6fSCedric Chaumont if (operation->info.operationClass != 153012e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 153112e66b6fSCedric Chaumont TEE_Panic(0); 153212e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_SIGN) 1533b0104773SPascal Brand TEE_Panic(0); 1534b0104773SPascal Brand 153512e66b6fSCedric Chaumont res = utee_asymm_operate(operation->state, params, paramCount, digest, 153612e66b6fSCedric Chaumont digestLen, signature, signatureLen); 153712e66b6fSCedric Chaumont 1538b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER) 1539b0104773SPascal Brand TEE_Panic(res); 154012e66b6fSCedric Chaumont 1541b0104773SPascal Brand return res; 1542b0104773SPascal Brand } 1543b0104773SPascal Brand 154412e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation, 154512e66b6fSCedric Chaumont TEE_Attribute *params, 154612e66b6fSCedric Chaumont uint32_t paramCount, void *digest, 154712e66b6fSCedric Chaumont uint32_t digestLen, void *signature, 154879a3c601SCedric Chaumont uint32_t signatureLen) 1549b0104773SPascal Brand { 1550b0104773SPascal Brand TEE_Result res; 1551b0104773SPascal Brand 155212e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || 155312e66b6fSCedric Chaumont (digest == NULL && digestLen != 0) || 1554b0104773SPascal Brand (signature == NULL && signatureLen != 0)) 1555b0104773SPascal Brand TEE_Panic(0); 155612e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1557b0104773SPascal Brand TEE_Panic(0); 155812e66b6fSCedric Chaumont if (!operation->key1) 1559b0104773SPascal Brand TEE_Panic(0); 156012e66b6fSCedric Chaumont if (operation->info.operationClass != 156112e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 156212e66b6fSCedric Chaumont TEE_Panic(0); 156312e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_VERIFY) 1564b0104773SPascal Brand TEE_Panic(0); 1565b0104773SPascal Brand 156612e66b6fSCedric Chaumont res = utee_asymm_verify(operation->state, params, paramCount, digest, 156712e66b6fSCedric Chaumont digestLen, signature, signatureLen); 156812e66b6fSCedric Chaumont 1569b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID) 1570b0104773SPascal Brand TEE_Panic(res); 157112e66b6fSCedric Chaumont 1572b0104773SPascal Brand return res; 1573b0104773SPascal Brand } 1574b0104773SPascal Brand 1575b0104773SPascal Brand /* Cryptographic Operations API - Key Derivation Functions */ 1576b0104773SPascal Brand 1577b0104773SPascal Brand void TEE_DeriveKey(TEE_OperationHandle operation, 1578b0104773SPascal Brand const TEE_Attribute *params, uint32_t paramCount, 1579b0104773SPascal Brand TEE_ObjectHandle derivedKey) 1580b0104773SPascal Brand { 1581b0104773SPascal Brand TEE_Result res; 1582b0104773SPascal Brand TEE_ObjectInfo key_info; 1583b0104773SPascal Brand 1584b0104773SPascal Brand if (operation == TEE_HANDLE_NULL || derivedKey == 0) 1585b0104773SPascal Brand TEE_Panic(0); 158684fa9467SCedric Chaumont if (params == NULL && paramCount != 0) 1587b0104773SPascal Brand TEE_Panic(0); 15888854d3c6SJerome Forissier if (TEE_ALG_GET_CLASS(operation->info.algorithm) != 15898854d3c6SJerome Forissier TEE_OPERATION_KEY_DERIVATION) 1590b0104773SPascal Brand TEE_Panic(0); 1591b0104773SPascal Brand 1592b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION) 1593b0104773SPascal Brand TEE_Panic(0); 159484fa9467SCedric Chaumont if (!operation->key1) 159584fa9467SCedric Chaumont TEE_Panic(0); 1596b0104773SPascal Brand if (operation->info.mode != TEE_MODE_DERIVE) 1597b0104773SPascal Brand TEE_Panic(0); 1598b0104773SPascal Brand if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0) 1599b0104773SPascal Brand TEE_Panic(0); 1600b0104773SPascal Brand 1601b0104773SPascal Brand res = utee_cryp_obj_get_info((uint32_t) derivedKey, &key_info); 1602b0104773SPascal Brand if (res != TEE_SUCCESS) 1603b0104773SPascal Brand TEE_Panic(0); 1604b0104773SPascal Brand 1605b0104773SPascal Brand if (key_info.objectType != TEE_TYPE_GENERIC_SECRET) 1606b0104773SPascal Brand TEE_Panic(0); 1607b0104773SPascal Brand if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1608b0104773SPascal Brand TEE_Panic(0); 1609b0104773SPascal Brand 1610b0104773SPascal Brand res = utee_cryp_derive_key(operation->state, params, paramCount, 1611b0104773SPascal Brand (uint32_t) derivedKey); 1612b0104773SPascal Brand if (res != TEE_SUCCESS) 1613b0104773SPascal Brand TEE_Panic(res); 1614b0104773SPascal Brand } 1615b0104773SPascal Brand 1616b0104773SPascal Brand /* Cryptographic Operations API - Random Number Generation Functions */ 1617b0104773SPascal Brand 161879a3c601SCedric Chaumont void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen) 1619b0104773SPascal Brand { 1620b0104773SPascal Brand TEE_Result res; 1621b0104773SPascal Brand 1622b0104773SPascal Brand res = utee_cryp_random_number_generate(randomBuffer, randomBufferLen); 1623b0104773SPascal Brand if (res != TEE_SUCCESS) 1624b0104773SPascal Brand TEE_Panic(res); 1625b0104773SPascal Brand } 1626