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; 4205304565SCedric 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; 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 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 38705304565SCedric Chaumont if (!operationInfo) 388b0104773SPascal Brand TEE_Panic(0); 389b0104773SPascal Brand 390b0104773SPascal Brand *operationInfo = operation->info; 391b0104773SPascal Brand } 392b0104773SPascal Brand 39305304565SCedric Chaumont TEE_Result TEE_GetOperationInfoMultiple(TEE_OperationHandle operation, 39405304565SCedric Chaumont TEE_OperationInfoMultiple *operationInfoMultiple, 39505304565SCedric Chaumont uint32_t *operationSize) 39605304565SCedric Chaumont { 39705304565SCedric Chaumont TEE_Result res = TEE_SUCCESS; 39805304565SCedric Chaumont TEE_ObjectInfo key_info1; 39905304565SCedric Chaumont TEE_ObjectInfo key_info2; 40005304565SCedric Chaumont uint32_t num_of_keys; 40105304565SCedric Chaumont size_t n; 40205304565SCedric Chaumont 40305304565SCedric Chaumont if (operation == TEE_HANDLE_NULL) { 40405304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 40505304565SCedric Chaumont goto out; 40605304565SCedric Chaumont } 40705304565SCedric Chaumont 40805304565SCedric Chaumont if (!operationInfoMultiple) { 40905304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 41005304565SCedric Chaumont goto out; 41105304565SCedric Chaumont } 41205304565SCedric Chaumont 41305304565SCedric Chaumont if (!operationSize) { 41405304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 41505304565SCedric Chaumont goto out; 41605304565SCedric Chaumont } 41705304565SCedric Chaumont 41805304565SCedric Chaumont num_of_keys = (*operationSize-sizeof(TEE_OperationInfoMultiple))/ 41905304565SCedric Chaumont sizeof(TEE_OperationInfoKey); 42005304565SCedric Chaumont 42105304565SCedric Chaumont if (num_of_keys > 2) { 42205304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 42305304565SCedric Chaumont goto out; 42405304565SCedric Chaumont } 42505304565SCedric Chaumont 42605304565SCedric Chaumont /* Two keys flag (TEE_ALG_AES_XTS only) */ 42705304565SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 42805304565SCedric Chaumont 0 && 42905304565SCedric Chaumont (num_of_keys != 2)) { 43005304565SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 43105304565SCedric Chaumont goto out; 43205304565SCedric Chaumont } 43305304565SCedric Chaumont 43405304565SCedric Chaumont /* Clear */ 43505304565SCedric Chaumont for (n = 0; n < num_of_keys; n++) { 43605304565SCedric Chaumont operationInfoMultiple->keyInformation[n].keySize = 0; 43705304565SCedric Chaumont operationInfoMultiple->keyInformation[n].requiredKeyUsage = 0; 43805304565SCedric Chaumont } 43905304565SCedric Chaumont 44005304565SCedric Chaumont if (num_of_keys == 2) { 44105304565SCedric Chaumont res = TEE_GetObjectInfo1(operation->key2, &key_info2); 44205304565SCedric Chaumont /* Key2 is not a valid handle */ 44305304565SCedric Chaumont if (res != TEE_SUCCESS) 44405304565SCedric Chaumont goto out; 44505304565SCedric Chaumont 44605304565SCedric Chaumont operationInfoMultiple->keyInformation[1].keySize = 44705304565SCedric Chaumont key_info2.keySize; 44805304565SCedric Chaumont operationInfoMultiple->keyInformation[1].requiredKeyUsage = 44905304565SCedric Chaumont operation->info.requiredKeyUsage; 45005304565SCedric Chaumont } 45105304565SCedric Chaumont 45205304565SCedric Chaumont if (num_of_keys >= 1) { 45305304565SCedric Chaumont res = TEE_GetObjectInfo1(operation->key1, &key_info1); 45405304565SCedric Chaumont /* Key1 is not a valid handle */ 45505304565SCedric Chaumont if (res != TEE_SUCCESS) { 45605304565SCedric Chaumont if (num_of_keys == 2) { 45705304565SCedric Chaumont operationInfoMultiple->keyInformation[1]. 45805304565SCedric Chaumont keySize = 0; 45905304565SCedric Chaumont operationInfoMultiple->keyInformation[1]. 46005304565SCedric Chaumont requiredKeyUsage = 0; 46105304565SCedric Chaumont } 46205304565SCedric Chaumont goto out; 46305304565SCedric Chaumont } 46405304565SCedric Chaumont 46505304565SCedric Chaumont operationInfoMultiple->keyInformation[0].keySize = 46605304565SCedric Chaumont key_info1.keySize; 46705304565SCedric Chaumont operationInfoMultiple->keyInformation[0].requiredKeyUsage = 46805304565SCedric Chaumont operation->info.requiredKeyUsage; 46905304565SCedric Chaumont } 47005304565SCedric Chaumont 47105304565SCedric Chaumont /* No key */ 47205304565SCedric Chaumont operationInfoMultiple->algorithm = operation->info.algorithm; 47305304565SCedric Chaumont operationInfoMultiple->operationClass = operation->info.operationClass; 47405304565SCedric Chaumont operationInfoMultiple->mode = operation->info.mode; 47505304565SCedric Chaumont operationInfoMultiple->digestLength = operation->info.digestLength; 47605304565SCedric Chaumont operationInfoMultiple->maxKeySize = operation->info.maxKeySize; 47705304565SCedric Chaumont operationInfoMultiple->handleState = operation->info.handleState; 47805304565SCedric Chaumont operationInfoMultiple->operationState = operation->operationState; 47905304565SCedric Chaumont operationInfoMultiple->numberOfKeys = num_of_keys; 48005304565SCedric Chaumont 48105304565SCedric Chaumont out: 48205304565SCedric Chaumont if (res != TEE_SUCCESS && 48305304565SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 48405304565SCedric Chaumont TEE_Panic(0); 48505304565SCedric Chaumont 48605304565SCedric Chaumont return res; 48705304565SCedric Chaumont } 48805304565SCedric Chaumont 489b0104773SPascal Brand void TEE_ResetOperation(TEE_OperationHandle operation) 490b0104773SPascal Brand { 491b0104773SPascal Brand TEE_Result res; 492b0104773SPascal Brand 493b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 494b0104773SPascal Brand TEE_Panic(0); 495bf80076aSCedric Chaumont 49605304565SCedric 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); 50305304565SCedric Chaumont operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 50405304565SCedric Chaumont } else { 505b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 506b0104773SPascal Brand } 50705304565SCedric Chaumont } 508b0104773SPascal Brand 509b0104773SPascal Brand TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation, 510b0104773SPascal Brand TEE_ObjectHandle key) 511b0104773SPascal Brand { 5127583c59eSCedric Chaumont TEE_Result res; 513b0104773SPascal Brand uint32_t key_size = 0; 514b0104773SPascal Brand TEE_ObjectInfo key_info; 515b0104773SPascal Brand 516a57c1e2eSCedric Chaumont /* 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 751*6d15db08SJerome Forissier static void init_hash_operation(TEE_OperationHandle operation, void *IV, 752*6d15db08SJerome Forissier uint32_t IVLen) 753*6d15db08SJerome Forissier { 754*6d15db08SJerome Forissier TEE_Result res; 755*6d15db08SJerome Forissier 756*6d15db08SJerome Forissier /* 757*6d15db08SJerome Forissier * Note : IV and IVLen are never used in current implementation 758*6d15db08SJerome Forissier * This is why coherent values of IV and IVLen are not checked 759*6d15db08SJerome Forissier */ 760*6d15db08SJerome Forissier res = utee_hash_init(operation->state, IV, IVLen); 761*6d15db08SJerome Forissier if (res != TEE_SUCCESS) 762*6d15db08SJerome Forissier TEE_Panic(res); 763*6d15db08SJerome Forissier operation->buffer_offs = 0; 764*6d15db08SJerome Forissier operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 765*6d15db08SJerome Forissier } 766*6d15db08SJerome Forissier 767b0104773SPascal Brand void TEE_DigestUpdate(TEE_OperationHandle operation, 76879a3c601SCedric Chaumont void *chunk, uint32_t chunkSize) 769b0104773SPascal Brand { 77073d6c3baSJoakim Bech TEE_Result res = TEE_ERROR_GENERIC; 771b0104773SPascal Brand 77273d6c3baSJoakim Bech if (operation == TEE_HANDLE_NULL || 77373d6c3baSJoakim Bech operation->info.operationClass != TEE_OPERATION_DIGEST) 774b0104773SPascal Brand TEE_Panic(0); 77573d6c3baSJoakim Bech 776b0104773SPascal Brand res = utee_hash_update(operation->state, chunk, chunkSize); 777b0104773SPascal Brand if (res != TEE_SUCCESS) 778b0104773SPascal Brand TEE_Panic(res); 779b0104773SPascal Brand } 780b0104773SPascal Brand 781b0104773SPascal Brand TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void *chunk, 78279a3c601SCedric Chaumont uint32_t chunkLen, void *hash, uint32_t *hashLen) 783b0104773SPascal Brand { 78487c2f6b6SCedric Chaumont TEE_Result res; 78587c2f6b6SCedric Chaumont 78687c2f6b6SCedric Chaumont if ((operation == TEE_HANDLE_NULL) || 78787c2f6b6SCedric Chaumont (!chunk && chunkLen) || 78887c2f6b6SCedric Chaumont !hash || 78987c2f6b6SCedric Chaumont !hashLen || 79087c2f6b6SCedric Chaumont (operation->info.operationClass != TEE_OPERATION_DIGEST)) { 79187c2f6b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 79287c2f6b6SCedric Chaumont goto out; 79387c2f6b6SCedric Chaumont } 79487c2f6b6SCedric Chaumont 79587c2f6b6SCedric Chaumont res = utee_hash_final(operation->state, chunk, chunkLen, hash, 79687c2f6b6SCedric Chaumont hashLen); 797*6d15db08SJerome Forissier if (res != TEE_SUCCESS) 798*6d15db08SJerome Forissier goto out; 799*6d15db08SJerome Forissier 800*6d15db08SJerome Forissier /* Reset operation state */ 801*6d15db08SJerome Forissier init_hash_operation(operation, NULL, 0); 80287c2f6b6SCedric Chaumont 80387c2f6b6SCedric Chaumont out: 80487c2f6b6SCedric Chaumont if (res != TEE_SUCCESS && 80587c2f6b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 806b0104773SPascal Brand TEE_Panic(0); 80773d6c3baSJoakim Bech 80887c2f6b6SCedric Chaumont return res; 809b0104773SPascal Brand } 810b0104773SPascal Brand 811b0104773SPascal Brand /* Cryptographic Operations API - Symmetric Cipher Functions */ 812b0104773SPascal Brand 81379a3c601SCedric Chaumont void TEE_CipherInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen) 814b0104773SPascal Brand { 815b0104773SPascal Brand TEE_Result res; 816b0104773SPascal Brand 817b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 818b0104773SPascal Brand TEE_Panic(0); 819b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_CIPHER) 820b0104773SPascal Brand TEE_Panic(0); 821b0104773SPascal Brand res = utee_cipher_init(operation->state, IV, IVLen); 822b0104773SPascal Brand if (res != TEE_SUCCESS) 823b0104773SPascal Brand TEE_Panic(res); 824b0104773SPascal Brand operation->buffer_offs = 0; 825b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 826b0104773SPascal Brand } 827b0104773SPascal Brand 828b0104773SPascal Brand static TEE_Result tee_buffer_update( 829b0104773SPascal Brand TEE_OperationHandle op, 830b0104773SPascal Brand TEE_Result(*update_func) (uint32_t state, const void *src, 8317f74c64aSPascal Brand size_t slen, void *dst, uint32_t *dlen), 832b0104773SPascal Brand const void *src_data, size_t src_len, 8337f74c64aSPascal Brand void *dest_data, uint32_t *dest_len) 834b0104773SPascal Brand { 835b0104773SPascal Brand TEE_Result res; 836b0104773SPascal Brand const uint8_t *src = src_data; 837b0104773SPascal Brand size_t slen = src_len; 838b0104773SPascal Brand uint8_t *dst = dest_data; 839b0104773SPascal Brand size_t dlen = *dest_len; 840b0104773SPascal Brand size_t acc_dlen = 0; 8417f74c64aSPascal Brand uint32_t tmp_dlen; 842b0104773SPascal Brand size_t l; 843b0104773SPascal Brand size_t buffer_size; 844d3588802SPascal Brand size_t buffer_left; 845b0104773SPascal Brand 846d3588802SPascal Brand if (op->buffer_two_blocks) { 847b0104773SPascal Brand buffer_size = op->block_size * 2; 848d3588802SPascal Brand buffer_left = 1; 849d3588802SPascal Brand } else { 850b0104773SPascal Brand buffer_size = op->block_size; 851d3588802SPascal Brand buffer_left = 0; 852d3588802SPascal Brand } 853b0104773SPascal Brand 854b0104773SPascal Brand if (op->buffer_offs > 0) { 855b0104773SPascal Brand /* Fill up complete block */ 856b0104773SPascal Brand if (op->buffer_offs < op->block_size) 857b0104773SPascal Brand l = MIN(slen, op->block_size - op->buffer_offs); 858b0104773SPascal Brand else 859b0104773SPascal Brand l = MIN(slen, buffer_size - op->buffer_offs); 860b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, l); 861b0104773SPascal Brand op->buffer_offs += l; 862b0104773SPascal Brand src += l; 863b0104773SPascal Brand slen -= l; 864b0104773SPascal Brand if ((op->buffer_offs % op->block_size) != 0) 865b0104773SPascal Brand goto out; /* Nothing left to do */ 866b0104773SPascal Brand } 867b0104773SPascal Brand 868b0104773SPascal Brand /* If we can feed from buffer */ 869d3588802SPascal Brand if ((op->buffer_offs > 0) && 870d3588802SPascal Brand ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) { 8712ff3fdbbSPascal Brand l = ROUNDUP(op->buffer_offs + slen - buffer_size, 872b0104773SPascal Brand op->block_size); 873b0104773SPascal Brand l = MIN(op->buffer_offs, l); 874b0104773SPascal Brand tmp_dlen = dlen; 875b0104773SPascal Brand res = update_func(op->state, op->buffer, l, dst, &tmp_dlen); 876b0104773SPascal Brand if (res != TEE_SUCCESS) 877b0104773SPascal Brand TEE_Panic(res); 878b0104773SPascal Brand dst += tmp_dlen; 879b0104773SPascal Brand dlen -= tmp_dlen; 880b0104773SPascal Brand acc_dlen += tmp_dlen; 881b0104773SPascal Brand op->buffer_offs -= l; 882b0104773SPascal Brand if (op->buffer_offs > 0) { 883b0104773SPascal Brand /* 884b0104773SPascal Brand * Slen is small enough to be contained in rest buffer. 885b0104773SPascal Brand */ 886b0104773SPascal Brand memcpy(op->buffer, op->buffer + l, buffer_size - l); 887b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 888b0104773SPascal Brand op->buffer_offs += slen; 889b0104773SPascal Brand goto out; /* Nothing left to do */ 890b0104773SPascal Brand } 891b0104773SPascal Brand } 892b0104773SPascal Brand 893d3588802SPascal Brand if (slen >= (buffer_size + buffer_left)) { 894b0104773SPascal Brand /* Buffer is empty, feed as much as possible from src */ 895b0104773SPascal Brand if (TEE_ALIGNMENT_IS_OK(src, uint32_t)) { 8962ff3fdbbSPascal Brand l = ROUNDUP(slen - buffer_size + 1, op->block_size); 897b0104773SPascal Brand 898b0104773SPascal Brand tmp_dlen = dlen; 899b0104773SPascal Brand res = update_func(op->state, src, l, dst, &tmp_dlen); 900b0104773SPascal Brand if (res != TEE_SUCCESS) 901b0104773SPascal Brand TEE_Panic(res); 902b0104773SPascal Brand src += l; 903b0104773SPascal Brand slen -= l; 904b0104773SPascal Brand dst += tmp_dlen; 905b0104773SPascal Brand dlen -= tmp_dlen; 906b0104773SPascal Brand acc_dlen += tmp_dlen; 907b0104773SPascal Brand } else { 908b0104773SPascal Brand /* 909b0104773SPascal Brand * Supplied data isn't well aligned, we're forced to 910b0104773SPascal Brand * feed through the buffer. 911b0104773SPascal Brand */ 912b0104773SPascal Brand while (slen >= op->block_size) { 913b0104773SPascal Brand memcpy(op->buffer, src, op->block_size); 914b0104773SPascal Brand 915b0104773SPascal Brand tmp_dlen = dlen; 916b0104773SPascal Brand res = 917b0104773SPascal Brand update_func(op->state, op->buffer, 918b0104773SPascal Brand op->block_size, dst, &tmp_dlen); 919b0104773SPascal Brand if (res != TEE_SUCCESS) 920b0104773SPascal Brand TEE_Panic(res); 921b0104773SPascal Brand src += op->block_size; 922b0104773SPascal Brand slen -= op->block_size; 923b0104773SPascal Brand dst += tmp_dlen; 924b0104773SPascal Brand dlen -= tmp_dlen; 925b0104773SPascal Brand acc_dlen += tmp_dlen; 926b0104773SPascal Brand } 927b0104773SPascal Brand } 928b0104773SPascal Brand } 929b0104773SPascal Brand 930b0104773SPascal Brand /* Slen is small enough to be contained in buffer. */ 931b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 932b0104773SPascal Brand op->buffer_offs += slen; 933b0104773SPascal Brand 934b0104773SPascal Brand out: 935b0104773SPascal Brand *dest_len = acc_dlen; 936b0104773SPascal Brand return TEE_SUCCESS; 937b0104773SPascal Brand } 938b0104773SPascal Brand 939b0104773SPascal Brand TEE_Result TEE_CipherUpdate(TEE_OperationHandle op, const void *srcData, 94079a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 941b0104773SPascal Brand { 942dea1f2b6SCedric Chaumont TEE_Result res; 943b0104773SPascal Brand size_t req_dlen; 944b0104773SPascal Brand 945dea1f2b6SCedric Chaumont if (op == TEE_HANDLE_NULL || 946dea1f2b6SCedric Chaumont (srcData == NULL && srcLen != 0) || 947dea1f2b6SCedric Chaumont destLen == NULL || 948dea1f2b6SCedric Chaumont (destData == NULL && *destLen != 0)) { 949dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 950dea1f2b6SCedric Chaumont goto out; 951dea1f2b6SCedric Chaumont } 952dea1f2b6SCedric Chaumont 953dea1f2b6SCedric Chaumont if (op->info.operationClass != TEE_OPERATION_CIPHER) { 954dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 955dea1f2b6SCedric Chaumont goto out; 956dea1f2b6SCedric Chaumont } 957dea1f2b6SCedric Chaumont 958dea1f2b6SCedric Chaumont if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 959dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 960dea1f2b6SCedric Chaumont goto out; 961dea1f2b6SCedric Chaumont } 962b0104773SPascal Brand 963b0104773SPascal Brand /* Calculate required dlen */ 964b0104773SPascal Brand req_dlen = ((op->buffer_offs + srcLen) / op->block_size) * 965b0104773SPascal Brand op->block_size; 966b0104773SPascal Brand if (op->buffer_two_blocks) { 967b0104773SPascal Brand if (req_dlen > op->block_size * 2) 968b0104773SPascal Brand req_dlen -= op->block_size * 2; 969b0104773SPascal Brand else 970b0104773SPascal Brand req_dlen = 0; 971b0104773SPascal Brand } 972b0104773SPascal Brand /* 973b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 974b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 975b0104773SPascal Brand * can't restore sync with this API. 976b0104773SPascal Brand */ 977b0104773SPascal Brand if (*destLen < req_dlen) { 978b0104773SPascal Brand *destLen = req_dlen; 979dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 980dea1f2b6SCedric Chaumont goto out; 981b0104773SPascal Brand } 982b0104773SPascal Brand 983dea1f2b6SCedric Chaumont res = tee_buffer_update(op, utee_cipher_update, srcData, srcLen, 984dea1f2b6SCedric Chaumont destData, destLen); 985b0104773SPascal Brand 986dea1f2b6SCedric Chaumont out: 987dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 988dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 989dea1f2b6SCedric Chaumont TEE_Panic(0); 990dea1f2b6SCedric Chaumont 991dea1f2b6SCedric Chaumont return res; 992b0104773SPascal Brand } 993b0104773SPascal Brand 994b0104773SPascal Brand TEE_Result TEE_CipherDoFinal(TEE_OperationHandle op, 99579a3c601SCedric Chaumont const void *srcData, uint32_t srcLen, void *destData, 99679a3c601SCedric Chaumont uint32_t *destLen) 997b0104773SPascal Brand { 998b0104773SPascal Brand TEE_Result res; 999b0104773SPascal Brand uint8_t *dst = destData; 1000b0104773SPascal Brand size_t acc_dlen = 0; 10017f74c64aSPascal Brand uint32_t tmp_dlen; 1002b0104773SPascal Brand size_t req_dlen; 1003b0104773SPascal Brand 1004dea1f2b6SCedric Chaumont if (op == TEE_HANDLE_NULL || 1005dea1f2b6SCedric Chaumont (srcData == NULL && srcLen != 0) || 1006dea1f2b6SCedric Chaumont destLen == NULL || 1007dea1f2b6SCedric Chaumont (destData == NULL && *destLen != 0)) { 1008dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1009dea1f2b6SCedric Chaumont goto out; 1010dea1f2b6SCedric Chaumont } 1011dea1f2b6SCedric Chaumont 1012dea1f2b6SCedric Chaumont if (op->info.operationClass != TEE_OPERATION_CIPHER) { 1013dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1014dea1f2b6SCedric Chaumont goto out; 1015dea1f2b6SCedric Chaumont } 1016dea1f2b6SCedric Chaumont 1017dea1f2b6SCedric Chaumont if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1018dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1019dea1f2b6SCedric Chaumont goto out; 1020dea1f2b6SCedric Chaumont } 1021b0104773SPascal Brand 1022b0104773SPascal Brand /* 1023b0104773SPascal Brand * Check that the final block doesn't require padding for those 1024b0104773SPascal Brand * algorithms that requires client to supply padding. 1025b0104773SPascal Brand */ 1026b0104773SPascal Brand if (op->info.algorithm == TEE_ALG_AES_ECB_NOPAD || 1027b0104773SPascal Brand op->info.algorithm == TEE_ALG_AES_CBC_NOPAD || 1028b0104773SPascal Brand op->info.algorithm == TEE_ALG_DES_ECB_NOPAD || 1029b0104773SPascal Brand op->info.algorithm == TEE_ALG_DES_CBC_NOPAD || 1030b0104773SPascal Brand op->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || 1031b0104773SPascal Brand op->info.algorithm == TEE_ALG_DES3_CBC_NOPAD) { 1032dea1f2b6SCedric Chaumont if (((op->buffer_offs + srcLen) % op->block_size) != 0) { 1033dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1034dea1f2b6SCedric Chaumont goto out; 1035dea1f2b6SCedric Chaumont } 1036b0104773SPascal Brand } 1037b0104773SPascal Brand 1038b0104773SPascal Brand /* 1039b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1040b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1041b0104773SPascal Brand * can't restore sync with this API. 1042b0104773SPascal Brand */ 1043b0104773SPascal Brand req_dlen = op->buffer_offs + srcLen; 1044b0104773SPascal Brand if (*destLen < req_dlen) { 1045b0104773SPascal Brand *destLen = req_dlen; 1046dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1047dea1f2b6SCedric Chaumont goto out; 1048b0104773SPascal Brand } 1049b0104773SPascal Brand 1050b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1051dea1f2b6SCedric Chaumont res = tee_buffer_update(op, utee_cipher_update, srcData, srcLen, dst, 1052b0104773SPascal Brand &tmp_dlen); 1053dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS) 1054dea1f2b6SCedric Chaumont goto out; 1055dea1f2b6SCedric Chaumont 1056b0104773SPascal Brand dst += tmp_dlen; 1057b0104773SPascal Brand acc_dlen += tmp_dlen; 1058b0104773SPascal Brand 1059b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1060b0104773SPascal Brand res = utee_cipher_final(op->state, op->buffer, op->buffer_offs, 1061b0104773SPascal Brand dst, &tmp_dlen); 1062b0104773SPascal Brand if (res != TEE_SUCCESS) 1063dea1f2b6SCedric Chaumont goto out; 1064dea1f2b6SCedric Chaumont 1065b0104773SPascal Brand acc_dlen += tmp_dlen; 1066b0104773SPascal Brand 1067b0104773SPascal Brand op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1068b0104773SPascal Brand *destLen = acc_dlen; 1069dea1f2b6SCedric Chaumont 1070dea1f2b6SCedric Chaumont out: 1071dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 1072dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1073dea1f2b6SCedric Chaumont TEE_Panic(0); 1074dea1f2b6SCedric Chaumont 1075dea1f2b6SCedric Chaumont return res; 1076b0104773SPascal Brand } 1077b0104773SPascal Brand 1078b0104773SPascal Brand /* Cryptographic Operations API - MAC Functions */ 1079b0104773SPascal Brand 108028e0efc6SCedric Chaumont void TEE_MACInit(TEE_OperationHandle operation, void *IV, uint32_t IVLen) 1081b0104773SPascal Brand { 1082b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 1083b0104773SPascal Brand TEE_Panic(0); 108428e0efc6SCedric Chaumont if (!operation->key1) 1085b0104773SPascal Brand TEE_Panic(0); 1086b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_MAC) 1087b0104773SPascal Brand TEE_Panic(0); 1088*6d15db08SJerome Forissier init_hash_operation(operation, IV, IVLen); 1089b0104773SPascal Brand } 1090b0104773SPascal Brand 109128e0efc6SCedric Chaumont void TEE_MACUpdate(TEE_OperationHandle operation, void *chunk, 109228e0efc6SCedric Chaumont uint32_t chunkSize) 1093b0104773SPascal Brand { 1094b0104773SPascal Brand TEE_Result res; 1095b0104773SPascal Brand 109628e0efc6SCedric Chaumont if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0)) 1097b0104773SPascal Brand TEE_Panic(0); 109828e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) 1099b0104773SPascal Brand TEE_Panic(0); 110028e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1101b0104773SPascal Brand TEE_Panic(0); 1102b0104773SPascal Brand 110328e0efc6SCedric Chaumont res = utee_hash_update(operation->state, chunk, chunkSize); 1104b0104773SPascal Brand if (res != TEE_SUCCESS) 1105b0104773SPascal Brand TEE_Panic(res); 1106b0104773SPascal Brand } 1107b0104773SPascal Brand 110828e0efc6SCedric Chaumont TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation, 110928e0efc6SCedric Chaumont void *message, uint32_t messageLen, 111079a3c601SCedric Chaumont void *mac, uint32_t *macLen) 1111b0104773SPascal Brand { 1112b0104773SPascal Brand TEE_Result res; 1113b0104773SPascal Brand 111428e0efc6SCedric Chaumont if (operation == TEE_HANDLE_NULL || 111528e0efc6SCedric Chaumont (message == NULL && messageLen != 0) || 111628e0efc6SCedric Chaumont mac == NULL || 111728e0efc6SCedric Chaumont macLen == NULL) { 111828e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 111928e0efc6SCedric Chaumont goto out; 112028e0efc6SCedric Chaumont } 1121b0104773SPascal Brand 112228e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 112328e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 112428e0efc6SCedric Chaumont goto out; 112528e0efc6SCedric Chaumont } 112628e0efc6SCedric Chaumont 112728e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 112828e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 112928e0efc6SCedric Chaumont goto out; 113028e0efc6SCedric Chaumont } 113128e0efc6SCedric Chaumont 113228e0efc6SCedric Chaumont res = utee_hash_final(operation->state, message, messageLen, mac, 113328e0efc6SCedric Chaumont macLen); 113428e0efc6SCedric Chaumont if (res != TEE_SUCCESS) 113528e0efc6SCedric Chaumont goto out; 113628e0efc6SCedric Chaumont 113728e0efc6SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 113828e0efc6SCedric Chaumont 113928e0efc6SCedric Chaumont out: 114028e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 114128e0efc6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 114228e0efc6SCedric Chaumont TEE_Panic(res); 114328e0efc6SCedric Chaumont 1144b0104773SPascal Brand return res; 1145b0104773SPascal Brand } 1146b0104773SPascal Brand 1147b0104773SPascal Brand TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation, 114828e0efc6SCedric Chaumont void *message, uint32_t messageLen, 114928e0efc6SCedric Chaumont void *mac, uint32_t macLen) 1150b0104773SPascal Brand { 1151b0104773SPascal Brand TEE_Result res; 1152b0104773SPascal Brand uint8_t computed_mac[TEE_MAX_HASH_SIZE]; 11537f74c64aSPascal Brand uint32_t computed_mac_size = TEE_MAX_HASH_SIZE; 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 1165b0104773SPascal Brand res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac, 1166b0104773SPascal Brand &computed_mac_size); 1167b0104773SPascal Brand if (res != TEE_SUCCESS) 116828e0efc6SCedric Chaumont goto out; 116928e0efc6SCedric Chaumont 117028e0efc6SCedric Chaumont if (computed_mac_size != macLen) { 117128e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 117228e0efc6SCedric Chaumont goto out; 117328e0efc6SCedric Chaumont } 117428e0efc6SCedric Chaumont 117528e0efc6SCedric Chaumont if (buf_compare_ct(mac, computed_mac, computed_mac_size) != 0) { 117628e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 117728e0efc6SCedric Chaumont goto out; 117828e0efc6SCedric Chaumont } 117928e0efc6SCedric Chaumont 118028e0efc6SCedric Chaumont out: 118128e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 118228e0efc6SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 118328e0efc6SCedric Chaumont TEE_Panic(res); 118428e0efc6SCedric Chaumont 1185b0104773SPascal Brand return res; 1186b0104773SPascal Brand } 1187b0104773SPascal Brand 1188b0104773SPascal Brand /* Cryptographic Operations API - Authenticated Encryption Functions */ 1189b0104773SPascal Brand 1190b5816c88SCedric Chaumont TEE_Result TEE_AEInit(TEE_OperationHandle operation, void *nonce, 119179a3c601SCedric Chaumont uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen, 1192b0104773SPascal Brand uint32_t payloadLen) 1193b0104773SPascal Brand { 1194b0104773SPascal Brand TEE_Result res; 1195b0104773SPascal Brand 1196b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || nonce == NULL) { 1197b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1198b5816c88SCedric Chaumont goto out; 1199b5816c88SCedric Chaumont } 1200b5816c88SCedric Chaumont 1201b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1202b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1203b5816c88SCedric Chaumont goto out; 1204b5816c88SCedric Chaumont } 1205b0104773SPascal Brand 1206b0104773SPascal Brand /* 1207b0104773SPascal Brand * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core 1208b0104773SPascal Brand * in the implementation. But AES-GCM spec doesn't specify the tag len 1209b0104773SPascal Brand * according to the same principle so we have to check here instead to 1210b0104773SPascal Brand * be GP compliant. 1211b0104773SPascal Brand */ 1212b5816c88SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_GCM) { 1213b0104773SPascal Brand /* 1214b0104773SPascal Brand * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96 1215b0104773SPascal Brand */ 1216b5816c88SCedric Chaumont if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) { 1217b5816c88SCedric Chaumont res = TEE_ERROR_NOT_SUPPORTED; 1218b5816c88SCedric Chaumont goto out; 1219b5816c88SCedric Chaumont } 1220b0104773SPascal Brand } 1221b0104773SPascal Brand 1222b5816c88SCedric Chaumont res = utee_authenc_init(operation->state, nonce, nonceLen, 1223b5816c88SCedric Chaumont tagLen / 8, AADLen, payloadLen); 1224b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1225b5816c88SCedric Chaumont goto out; 1226b5816c88SCedric Chaumont 1227b5816c88SCedric Chaumont operation->ae_tag_len = tagLen / 8; 1228b5816c88SCedric Chaumont operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 1229b5816c88SCedric Chaumont 1230b5816c88SCedric Chaumont out: 1231b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1232b5816c88SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 1233b0104773SPascal Brand TEE_Panic(res); 1234b5816c88SCedric Chaumont 1235b0104773SPascal Brand return res; 1236b0104773SPascal Brand } 1237b0104773SPascal Brand 1238b5816c88SCedric Chaumont void TEE_AEUpdateAAD(TEE_OperationHandle operation, void *AADdata, 123979a3c601SCedric Chaumont uint32_t AADdataLen) 1240b0104773SPascal Brand { 1241b0104773SPascal Brand TEE_Result res; 1242b0104773SPascal Brand 1243b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1244b5816c88SCedric Chaumont (AADdata == NULL && AADdataLen != 0)) 1245b0104773SPascal Brand TEE_Panic(0); 1246b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) 1247b0104773SPascal Brand TEE_Panic(0); 1248b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1249b0104773SPascal Brand TEE_Panic(0); 1250b0104773SPascal Brand 1251b5816c88SCedric Chaumont res = utee_authenc_update_aad(operation->state, AADdata, AADdataLen); 1252b0104773SPascal Brand if (res != TEE_SUCCESS) 1253b0104773SPascal Brand TEE_Panic(res); 1254b0104773SPascal Brand } 1255b0104773SPascal Brand 1256b5816c88SCedric Chaumont TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, void *srcData, 125779a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 1258b0104773SPascal Brand { 1259b5816c88SCedric Chaumont TEE_Result res; 1260b0104773SPascal Brand size_t req_dlen; 1261b0104773SPascal Brand 1262b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1263b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1264b5816c88SCedric Chaumont destLen == NULL || 1265b5816c88SCedric Chaumont (destData == NULL && *destLen != 0)) { 1266b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1267b5816c88SCedric Chaumont goto out; 1268b5816c88SCedric Chaumont } 1269b5816c88SCedric Chaumont 1270b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1271b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1272b5816c88SCedric Chaumont goto out; 1273b5816c88SCedric Chaumont } 1274b5816c88SCedric Chaumont 1275b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1276b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1277b5816c88SCedric Chaumont goto out; 1278b5816c88SCedric Chaumont } 1279b0104773SPascal Brand 1280b0104773SPascal Brand /* 1281b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1282b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1283b0104773SPascal Brand * can't restore sync with this API. 1284b0104773SPascal Brand */ 1285b5816c88SCedric Chaumont req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen, 1286b5816c88SCedric Chaumont operation->block_size); 1287b0104773SPascal Brand if (*destLen < req_dlen) { 1288b0104773SPascal Brand *destLen = req_dlen; 1289b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1290b5816c88SCedric Chaumont goto out; 1291b0104773SPascal Brand } 1292b0104773SPascal Brand 1293b5816c88SCedric Chaumont res = tee_buffer_update(operation, utee_authenc_update_payload, srcData, 1294b5816c88SCedric Chaumont srcLen, destData, destLen); 1295b0104773SPascal Brand 1296b5816c88SCedric Chaumont out: 1297b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1298b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1299b5816c88SCedric Chaumont TEE_Panic(res); 1300b5816c88SCedric Chaumont 1301b5816c88SCedric Chaumont return res; 1302b0104773SPascal Brand } 1303b0104773SPascal Brand 1304b5816c88SCedric Chaumont TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation, 1305b5816c88SCedric Chaumont void *srcData, uint32_t srcLen, 130679a3c601SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 130779a3c601SCedric Chaumont uint32_t *tagLen) 1308b0104773SPascal Brand { 1309b0104773SPascal Brand TEE_Result res; 1310b0104773SPascal Brand uint8_t *dst = destData; 1311b0104773SPascal Brand size_t acc_dlen = 0; 13127f74c64aSPascal Brand uint32_t tmp_dlen; 1313b0104773SPascal Brand size_t req_dlen; 1314b0104773SPascal Brand 1315b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1316b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1317b5816c88SCedric Chaumont destLen == NULL || 1318b5816c88SCedric Chaumont (destData == NULL && *destLen != 0) || 1319b5816c88SCedric Chaumont tag == NULL || tagLen == NULL) { 1320b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1321b5816c88SCedric Chaumont goto out; 1322b5816c88SCedric Chaumont } 1323b5816c88SCedric Chaumont 1324b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1325b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1326b5816c88SCedric Chaumont goto out; 1327b5816c88SCedric Chaumont } 1328b5816c88SCedric Chaumont 1329b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1330b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1331b5816c88SCedric Chaumont goto out; 1332b5816c88SCedric Chaumont } 1333b0104773SPascal Brand 1334b0104773SPascal Brand /* 1335b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1336b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1337b0104773SPascal Brand * can't restore sync with this API. 1338b0104773SPascal Brand */ 1339b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1340b0104773SPascal Brand if (*destLen < req_dlen) { 1341b0104773SPascal Brand *destLen = req_dlen; 1342b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1343b5816c88SCedric Chaumont goto out; 1344b0104773SPascal Brand } 1345b0104773SPascal Brand 1346b0104773SPascal Brand /* 1347b0104773SPascal Brand * Need to check this before update_payload since sync would be lost if 1348b0104773SPascal Brand * we return short buffer after that. 1349b0104773SPascal Brand */ 1350b5816c88SCedric Chaumont if (*tagLen < operation->ae_tag_len) { 1351b5816c88SCedric Chaumont *tagLen = operation->ae_tag_len; 1352b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1353b5816c88SCedric Chaumont goto out; 1354b0104773SPascal Brand } 1355b0104773SPascal Brand 1356b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1357b5816c88SCedric Chaumont res = tee_buffer_update(operation, utee_authenc_update_payload, srcData, 1358b5816c88SCedric Chaumont srcLen, dst, &tmp_dlen); 1359b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1360b5816c88SCedric Chaumont goto out; 1361b5816c88SCedric Chaumont 1362b0104773SPascal Brand dst += tmp_dlen; 1363b0104773SPascal Brand acc_dlen += tmp_dlen; 1364b0104773SPascal Brand 1365b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1366b5816c88SCedric Chaumont res = utee_authenc_enc_final(operation->state, operation->buffer, 1367b5816c88SCedric Chaumont operation->buffer_offs, dst, &tmp_dlen, 1368b5816c88SCedric Chaumont tag, tagLen); 1369b0104773SPascal Brand if (res != TEE_SUCCESS) 1370b5816c88SCedric Chaumont goto out; 1371b0104773SPascal Brand 1372b5816c88SCedric Chaumont acc_dlen += tmp_dlen; 1373b0104773SPascal Brand *destLen = acc_dlen; 1374b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1375b5816c88SCedric Chaumont 1376b5816c88SCedric Chaumont out: 1377b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1378b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1379b5816c88SCedric Chaumont TEE_Panic(res); 1380b0104773SPascal Brand 1381b0104773SPascal Brand return res; 1382b0104773SPascal Brand } 1383b0104773SPascal Brand 1384b5816c88SCedric Chaumont TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation, 1385b5816c88SCedric Chaumont void *srcData, uint32_t srcLen, 1386b5816c88SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 138779a3c601SCedric Chaumont uint32_t tagLen) 1388b0104773SPascal Brand { 1389b0104773SPascal Brand TEE_Result res; 1390b0104773SPascal Brand uint8_t *dst = destData; 1391b0104773SPascal Brand size_t acc_dlen = 0; 13927f74c64aSPascal Brand uint32_t tmp_dlen; 1393b0104773SPascal Brand size_t req_dlen; 1394b0104773SPascal Brand 1395b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1396b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1397b5816c88SCedric Chaumont destLen == NULL || 1398b5816c88SCedric Chaumont (destData == NULL && *destLen != 0) || 1399b5816c88SCedric Chaumont (tag == NULL && tagLen != 0)) { 1400b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1401b5816c88SCedric Chaumont goto out; 1402b5816c88SCedric Chaumont } 1403b5816c88SCedric Chaumont 1404b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1405b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1406b5816c88SCedric Chaumont goto out; 1407b5816c88SCedric Chaumont } 1408b5816c88SCedric Chaumont 1409b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1410b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1411b5816c88SCedric Chaumont goto out; 1412b5816c88SCedric Chaumont } 1413b0104773SPascal Brand 1414b0104773SPascal Brand /* 1415b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1416b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1417b0104773SPascal Brand * can't restore sync with this API. 1418b0104773SPascal Brand */ 1419b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1420b0104773SPascal Brand if (*destLen < req_dlen) { 1421b0104773SPascal Brand *destLen = req_dlen; 1422b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1423b5816c88SCedric Chaumont goto out; 1424b0104773SPascal Brand } 1425b0104773SPascal Brand 1426b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1427b5816c88SCedric Chaumont res = tee_buffer_update(operation, utee_authenc_update_payload, srcData, 1428b5816c88SCedric Chaumont srcLen, dst, &tmp_dlen); 1429b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1430b5816c88SCedric Chaumont goto out; 1431b5816c88SCedric Chaumont 1432b0104773SPascal Brand dst += tmp_dlen; 1433b0104773SPascal Brand acc_dlen += tmp_dlen; 1434b0104773SPascal Brand 1435b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1436b5816c88SCedric Chaumont res = utee_authenc_dec_final(operation->state, operation->buffer, 1437b5816c88SCedric Chaumont operation->buffer_offs, dst, &tmp_dlen, 1438b5816c88SCedric Chaumont tag, tagLen); 1439b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1440b5816c88SCedric Chaumont goto out; 1441b5816c88SCedric Chaumont 1442b0104773SPascal Brand /* Supplied tagLen should match what we initiated with */ 1443b5816c88SCedric Chaumont if (tagLen != operation->ae_tag_len) 1444b0104773SPascal Brand res = TEE_ERROR_MAC_INVALID; 1445b0104773SPascal Brand 1446b0104773SPascal Brand acc_dlen += tmp_dlen; 1447b0104773SPascal Brand 1448b0104773SPascal Brand *destLen = acc_dlen; 1449b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1450b5816c88SCedric Chaumont 1451b5816c88SCedric Chaumont out: 1452b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1453b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER && 1454b5816c88SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 1455b5816c88SCedric Chaumont TEE_Panic(res); 1456b0104773SPascal Brand 1457b0104773SPascal Brand return res; 1458b0104773SPascal Brand } 1459b0104773SPascal Brand 1460b0104773SPascal Brand /* Cryptographic Operations API - Asymmetric Functions */ 1461b0104773SPascal Brand 146212e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation, 146312e66b6fSCedric Chaumont TEE_Attribute *params, 146412e66b6fSCedric Chaumont uint32_t paramCount, void *srcData, 146579a3c601SCedric Chaumont uint32_t srcLen, void *destData, 146679a3c601SCedric Chaumont uint32_t *destLen) 1467b0104773SPascal Brand { 1468b0104773SPascal Brand TEE_Result res; 1469b0104773SPascal Brand 147012e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1471b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 1472b0104773SPascal Brand TEE_Panic(0); 147312e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1474b0104773SPascal Brand TEE_Panic(0); 147512e66b6fSCedric Chaumont if (!operation->key1) 1476b0104773SPascal Brand TEE_Panic(0); 147712e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 147812e66b6fSCedric Chaumont TEE_Panic(0); 147912e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_ENCRYPT) 1480b0104773SPascal Brand TEE_Panic(0); 1481b0104773SPascal Brand 148212e66b6fSCedric Chaumont res = utee_asymm_operate(operation->state, params, paramCount, srcData, 148312e66b6fSCedric Chaumont srcLen, destData, destLen); 148412e66b6fSCedric Chaumont 14858844ebfcSPascal Brand if (res != TEE_SUCCESS && 14868844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 14878844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1488b0104773SPascal Brand TEE_Panic(res); 148912e66b6fSCedric Chaumont 1490b0104773SPascal Brand return res; 1491b0104773SPascal Brand } 1492b0104773SPascal Brand 149312e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation, 149412e66b6fSCedric Chaumont TEE_Attribute *params, 149512e66b6fSCedric Chaumont uint32_t paramCount, void *srcData, 149679a3c601SCedric Chaumont uint32_t srcLen, void *destData, 149779a3c601SCedric Chaumont uint32_t *destLen) 1498b0104773SPascal Brand { 1499b0104773SPascal Brand TEE_Result res; 1500b0104773SPascal Brand 150112e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1502b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 1503b0104773SPascal Brand TEE_Panic(0); 150412e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1505b0104773SPascal Brand TEE_Panic(0); 150612e66b6fSCedric Chaumont if (!operation->key1) 1507b0104773SPascal Brand TEE_Panic(0); 150812e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 150912e66b6fSCedric Chaumont TEE_Panic(0); 151012e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_DECRYPT) 1511b0104773SPascal Brand TEE_Panic(0); 1512b0104773SPascal Brand 151312e66b6fSCedric Chaumont res = utee_asymm_operate(operation->state, params, paramCount, srcData, 151412e66b6fSCedric Chaumont srcLen, destData, destLen); 151512e66b6fSCedric Chaumont 15168844ebfcSPascal Brand if (res != TEE_SUCCESS && 15178844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 15188844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1519b0104773SPascal Brand TEE_Panic(res); 152012e66b6fSCedric Chaumont 1521b0104773SPascal Brand return res; 1522b0104773SPascal Brand } 1523b0104773SPascal Brand 152412e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation, 152512e66b6fSCedric Chaumont TEE_Attribute *params, 152612e66b6fSCedric Chaumont uint32_t paramCount, void *digest, 152779a3c601SCedric Chaumont uint32_t digestLen, void *signature, 152879a3c601SCedric Chaumont uint32_t *signatureLen) 1529b0104773SPascal Brand { 1530b0104773SPascal Brand TEE_Result res; 1531b0104773SPascal Brand 153212e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || 153312e66b6fSCedric Chaumont (digest == NULL && digestLen != 0) || 1534b0104773SPascal Brand signature == NULL || signatureLen == NULL) 1535b0104773SPascal Brand TEE_Panic(0); 153612e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1537b0104773SPascal Brand TEE_Panic(0); 153812e66b6fSCedric Chaumont if (!operation->key1) 1539b0104773SPascal Brand TEE_Panic(0); 154012e66b6fSCedric Chaumont if (operation->info.operationClass != 154112e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 154212e66b6fSCedric Chaumont TEE_Panic(0); 154312e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_SIGN) 1544b0104773SPascal Brand TEE_Panic(0); 1545b0104773SPascal Brand 154612e66b6fSCedric Chaumont res = utee_asymm_operate(operation->state, params, paramCount, digest, 154712e66b6fSCedric Chaumont digestLen, signature, signatureLen); 154812e66b6fSCedric Chaumont 1549b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER) 1550b0104773SPascal Brand TEE_Panic(res); 155112e66b6fSCedric Chaumont 1552b0104773SPascal Brand return res; 1553b0104773SPascal Brand } 1554b0104773SPascal Brand 155512e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation, 155612e66b6fSCedric Chaumont TEE_Attribute *params, 155712e66b6fSCedric Chaumont uint32_t paramCount, void *digest, 155812e66b6fSCedric Chaumont uint32_t digestLen, void *signature, 155979a3c601SCedric Chaumont uint32_t signatureLen) 1560b0104773SPascal Brand { 1561b0104773SPascal Brand TEE_Result res; 1562b0104773SPascal Brand 156312e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || 156412e66b6fSCedric Chaumont (digest == NULL && digestLen != 0) || 1565b0104773SPascal Brand (signature == NULL && signatureLen != 0)) 1566b0104773SPascal Brand TEE_Panic(0); 156712e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1568b0104773SPascal Brand TEE_Panic(0); 156912e66b6fSCedric Chaumont if (!operation->key1) 1570b0104773SPascal Brand TEE_Panic(0); 157112e66b6fSCedric Chaumont if (operation->info.operationClass != 157212e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 157312e66b6fSCedric Chaumont TEE_Panic(0); 157412e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_VERIFY) 1575b0104773SPascal Brand TEE_Panic(0); 1576b0104773SPascal Brand 157712e66b6fSCedric Chaumont res = utee_asymm_verify(operation->state, params, paramCount, digest, 157812e66b6fSCedric Chaumont digestLen, signature, signatureLen); 157912e66b6fSCedric Chaumont 1580b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID) 1581b0104773SPascal Brand TEE_Panic(res); 158212e66b6fSCedric Chaumont 1583b0104773SPascal Brand return res; 1584b0104773SPascal Brand } 1585b0104773SPascal Brand 1586b0104773SPascal Brand /* Cryptographic Operations API - Key Derivation Functions */ 1587b0104773SPascal Brand 1588b0104773SPascal Brand void TEE_DeriveKey(TEE_OperationHandle operation, 1589b0104773SPascal Brand const TEE_Attribute *params, uint32_t paramCount, 1590b0104773SPascal Brand TEE_ObjectHandle derivedKey) 1591b0104773SPascal Brand { 1592b0104773SPascal Brand TEE_Result res; 1593b0104773SPascal Brand TEE_ObjectInfo key_info; 1594b0104773SPascal Brand 1595b0104773SPascal Brand if (operation == TEE_HANDLE_NULL || derivedKey == 0) 1596b0104773SPascal Brand TEE_Panic(0); 159784fa9467SCedric Chaumont if (params == NULL && paramCount != 0) 1598b0104773SPascal Brand TEE_Panic(0); 15998854d3c6SJerome Forissier if (TEE_ALG_GET_CLASS(operation->info.algorithm) != 16008854d3c6SJerome Forissier TEE_OPERATION_KEY_DERIVATION) 1601b0104773SPascal Brand TEE_Panic(0); 1602b0104773SPascal Brand 1603b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION) 1604b0104773SPascal Brand TEE_Panic(0); 160584fa9467SCedric Chaumont if (!operation->key1) 160684fa9467SCedric Chaumont TEE_Panic(0); 1607b0104773SPascal Brand if (operation->info.mode != TEE_MODE_DERIVE) 1608b0104773SPascal Brand TEE_Panic(0); 1609b0104773SPascal Brand if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0) 1610b0104773SPascal Brand TEE_Panic(0); 1611b0104773SPascal Brand 1612b0104773SPascal Brand res = utee_cryp_obj_get_info((uint32_t) derivedKey, &key_info); 1613b0104773SPascal Brand if (res != TEE_SUCCESS) 1614b0104773SPascal Brand TEE_Panic(0); 1615b0104773SPascal Brand 1616b0104773SPascal Brand if (key_info.objectType != TEE_TYPE_GENERIC_SECRET) 1617b0104773SPascal Brand TEE_Panic(0); 1618b0104773SPascal Brand if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1619b0104773SPascal Brand TEE_Panic(0); 1620b0104773SPascal Brand 1621b0104773SPascal Brand res = utee_cryp_derive_key(operation->state, params, paramCount, 1622b0104773SPascal Brand (uint32_t) derivedKey); 1623b0104773SPascal Brand if (res != TEE_SUCCESS) 1624b0104773SPascal Brand TEE_Panic(res); 1625b0104773SPascal Brand } 1626b0104773SPascal Brand 1627b0104773SPascal Brand /* Cryptographic Operations API - Random Number Generation Functions */ 1628b0104773SPascal Brand 162979a3c601SCedric Chaumont void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen) 1630b0104773SPascal Brand { 1631b0104773SPascal Brand TEE_Result res; 1632b0104773SPascal Brand 1633b0104773SPascal Brand res = utee_cryp_random_number_generate(randomBuffer, randomBufferLen); 1634b0104773SPascal Brand if (res != TEE_SUCCESS) 1635b0104773SPascal Brand TEE_Panic(res); 1636b0104773SPascal Brand } 1637