11bb92983SJerome Forissier // SPDX-License-Identifier: BSD-2-Clause 2b0104773SPascal Brand /* 3b0104773SPascal Brand * Copyright (c) 2014, STMicroelectronics International N.V. 4b0104773SPascal Brand * All rights reserved. 5b0104773SPascal Brand * 6b0104773SPascal Brand * Redistribution and use in source and binary forms, with or without 7b0104773SPascal Brand * modification, are permitted provided that the following conditions are met: 8b0104773SPascal Brand * 9b0104773SPascal Brand * 1. Redistributions of source code must retain the above copyright notice, 10b0104773SPascal Brand * this list of conditions and the following disclaimer. 11b0104773SPascal Brand * 12b0104773SPascal Brand * 2. Redistributions in binary form must reproduce the above copyright notice, 13b0104773SPascal Brand * this list of conditions and the following disclaimer in the documentation 14b0104773SPascal Brand * and/or other materials provided with the distribution. 15b0104773SPascal Brand * 16b0104773SPascal Brand * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17b0104773SPascal Brand * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18b0104773SPascal Brand * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19b0104773SPascal Brand * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20b0104773SPascal Brand * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21b0104773SPascal Brand * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22b0104773SPascal Brand * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23b0104773SPascal Brand * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24b0104773SPascal Brand * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25b0104773SPascal Brand * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26b0104773SPascal Brand * POSSIBILITY OF SUCH DAMAGE. 27b0104773SPascal Brand */ 28b0104773SPascal Brand #include <stdlib.h> 29b0104773SPascal Brand #include <string.h> 30b796ebf3SJerome Forissier #include <string_ext.h> 31b0104773SPascal Brand 32b0104773SPascal Brand #include <tee_api.h> 338854d3c6SJerome Forissier #include <tee_api_defines_extensions.h> 34b0104773SPascal Brand #include <tee_internal_api_extensions.h> 35b0104773SPascal Brand #include <utee_syscalls.h> 36b0104773SPascal Brand #include <utee_defines.h> 37fc26c92aSJens Wiklander #include <util.h> 38e86f1266SJens Wiklander #include "tee_api_private.h" 39b0104773SPascal Brand 40b0104773SPascal Brand struct __TEE_OperationHandle { 41b0104773SPascal Brand TEE_OperationInfo info; 42b0104773SPascal Brand TEE_ObjectHandle key1; 43b0104773SPascal Brand TEE_ObjectHandle key2; 44642a1607SCedric Chaumont uint32_t operationState;/* Operation state : INITIAL or ACTIVE */ 45b0104773SPascal Brand uint8_t *buffer; /* buffer to collect complete blocks */ 46b0104773SPascal Brand bool buffer_two_blocks; /* True if two blocks need to be buffered */ 47b0104773SPascal Brand size_t block_size; /* Block size of cipher */ 48b0104773SPascal Brand size_t buffer_offs; /* Offset in buffer */ 49b0104773SPascal Brand uint32_t state; /* Handle to state in TEE Core */ 50b0104773SPascal Brand uint32_t ae_tag_len; /* 51b0104773SPascal Brand * tag_len in bytes for AE operation else unused 52b0104773SPascal Brand */ 53b0104773SPascal Brand }; 54b0104773SPascal Brand 55b0104773SPascal Brand /* Cryptographic Operations API - Generic Operation Functions */ 56b0104773SPascal Brand 57b0104773SPascal Brand TEE_Result TEE_AllocateOperation(TEE_OperationHandle *operation, 58b0104773SPascal Brand uint32_t algorithm, uint32_t mode, 59b0104773SPascal Brand uint32_t maxKeySize) 60b0104773SPascal Brand { 61b0104773SPascal Brand TEE_Result res; 62b0104773SPascal Brand TEE_OperationHandle op = TEE_HANDLE_NULL; 63b0104773SPascal Brand uint32_t handle_state = 0; 64b0104773SPascal Brand size_t block_size = 1; 65b0104773SPascal Brand uint32_t req_key_usage; 66b0104773SPascal Brand bool with_private_key = false; 67b0104773SPascal Brand bool buffer_two_blocks = false; 68b0104773SPascal Brand 699b52c538SCedric Chaumont if (!operation) 70b0104773SPascal Brand TEE_Panic(0); 71b0104773SPascal Brand 72b0104773SPascal Brand if (algorithm == TEE_ALG_AES_XTS) 73b0104773SPascal Brand handle_state = TEE_HANDLE_FLAG_EXPECT_TWO_KEYS; 74b0104773SPascal Brand 75218d9055SCedric Chaumont /* Check algorithm max key size */ 76218d9055SCedric Chaumont switch (algorithm) { 77218d9055SCedric Chaumont case TEE_ALG_DSA_SHA1: 78218d9055SCedric Chaumont if (maxKeySize < 512) 79218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 80218d9055SCedric Chaumont if (maxKeySize > 1024) 81218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 82218d9055SCedric Chaumont if (maxKeySize % 64 != 0) 83218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 84218d9055SCedric Chaumont break; 85218d9055SCedric Chaumont 86218d9055SCedric Chaumont case TEE_ALG_DSA_SHA224: 87218d9055SCedric Chaumont if (maxKeySize != 2048) 88218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 89218d9055SCedric Chaumont break; 90218d9055SCedric Chaumont 91218d9055SCedric Chaumont case TEE_ALG_DSA_SHA256: 92218d9055SCedric Chaumont if (maxKeySize != 2048 && maxKeySize != 3072) 93218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 94218d9055SCedric Chaumont break; 95218d9055SCedric Chaumont 961220586eSCedric Chaumont case TEE_ALG_ECDSA_P192: 971220586eSCedric Chaumont case TEE_ALG_ECDH_P192: 981220586eSCedric Chaumont if (maxKeySize != 192) 991220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 1001220586eSCedric Chaumont break; 1011220586eSCedric Chaumont 1021220586eSCedric Chaumont case TEE_ALG_ECDSA_P224: 1031220586eSCedric Chaumont case TEE_ALG_ECDH_P224: 1041220586eSCedric Chaumont if (maxKeySize != 224) 1051220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 1061220586eSCedric Chaumont break; 1071220586eSCedric Chaumont 1081220586eSCedric Chaumont case TEE_ALG_ECDSA_P256: 1091220586eSCedric Chaumont case TEE_ALG_ECDH_P256: 1101220586eSCedric Chaumont if (maxKeySize != 256) 1111220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 1121220586eSCedric Chaumont break; 1131220586eSCedric Chaumont 1141220586eSCedric Chaumont case TEE_ALG_ECDSA_P384: 1151220586eSCedric Chaumont case TEE_ALG_ECDH_P384: 1161220586eSCedric Chaumont if (maxKeySize != 384) 1171220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 1181220586eSCedric Chaumont break; 1191220586eSCedric Chaumont 1201220586eSCedric Chaumont case TEE_ALG_ECDSA_P521: 1211220586eSCedric Chaumont case TEE_ALG_ECDH_P521: 1221220586eSCedric Chaumont if (maxKeySize != 521) 1231220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 1241220586eSCedric Chaumont break; 1251220586eSCedric Chaumont 126218d9055SCedric Chaumont default: 127218d9055SCedric Chaumont break; 128218d9055SCedric Chaumont } 129218d9055SCedric Chaumont 130218d9055SCedric Chaumont /* Check algorithm mode */ 131b0104773SPascal Brand switch (algorithm) { 132b0104773SPascal Brand case TEE_ALG_AES_CTS: 133b0104773SPascal Brand case TEE_ALG_AES_XTS: 134b0104773SPascal Brand buffer_two_blocks = true; 1354bd53c54SJerome Forissier /* FALLTHROUGH */ 1364bd53c54SJerome Forissier case TEE_ALG_AES_ECB_NOPAD: 137b0104773SPascal Brand case TEE_ALG_AES_CBC_NOPAD: 138b0104773SPascal Brand case TEE_ALG_AES_CCM: 139b0104773SPascal Brand case TEE_ALG_DES_ECB_NOPAD: 140b0104773SPascal Brand case TEE_ALG_DES_CBC_NOPAD: 141b0104773SPascal Brand case TEE_ALG_DES3_ECB_NOPAD: 142b0104773SPascal Brand case TEE_ALG_DES3_CBC_NOPAD: 143b0104773SPascal Brand if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES) 144b0104773SPascal Brand block_size = TEE_AES_BLOCK_SIZE; 145b0104773SPascal Brand else 146b0104773SPascal Brand block_size = TEE_DES_BLOCK_SIZE; 147afc0c182SBogdan Liulko /* FALLTHROUGH */ 14857aabac5SBogdan Liulko case TEE_ALG_AES_CTR: 149afc0c182SBogdan Liulko case TEE_ALG_AES_GCM: 150b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) 151b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT; 152b0104773SPascal Brand else if (mode == TEE_MODE_DECRYPT) 153b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT; 154b0104773SPascal Brand else 155b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 156b0104773SPascal Brand break; 157b0104773SPascal Brand 158b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_MD5: 159b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1: 160b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224: 161b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256: 162b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384: 163b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512: 164b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1: 165b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224: 166b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256: 167b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384: 168b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512: 169b0104773SPascal Brand case TEE_ALG_DSA_SHA1: 170218d9055SCedric Chaumont case TEE_ALG_DSA_SHA224: 171218d9055SCedric Chaumont case TEE_ALG_DSA_SHA256: 1721220586eSCedric Chaumont case TEE_ALG_ECDSA_P192: 1731220586eSCedric Chaumont case TEE_ALG_ECDSA_P224: 1741220586eSCedric Chaumont case TEE_ALG_ECDSA_P256: 1751220586eSCedric Chaumont case TEE_ALG_ECDSA_P384: 1761220586eSCedric Chaumont case TEE_ALG_ECDSA_P521: 177b0104773SPascal Brand if (mode == TEE_MODE_SIGN) { 178b0104773SPascal Brand with_private_key = true; 179b0104773SPascal Brand req_key_usage = TEE_USAGE_SIGN; 180b0104773SPascal Brand } else if (mode == TEE_MODE_VERIFY) { 181b0104773SPascal Brand req_key_usage = TEE_USAGE_VERIFY; 182b0104773SPascal Brand } else { 183b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 184b0104773SPascal Brand } 185b0104773SPascal Brand break; 186b0104773SPascal Brand 187b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_V1_5: 188b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1: 189b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224: 190b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256: 191b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384: 192b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512: 193b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) { 194b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT; 195b0104773SPascal Brand } else if (mode == TEE_MODE_DECRYPT) { 196b0104773SPascal Brand with_private_key = true; 197b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT; 198b0104773SPascal Brand } else { 199b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 200b0104773SPascal Brand } 201b0104773SPascal Brand break; 202b0104773SPascal Brand 203b0104773SPascal Brand case TEE_ALG_RSA_NOPAD: 204b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) { 205b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY; 206b0104773SPascal Brand } else if (mode == TEE_MODE_DECRYPT) { 207b0104773SPascal Brand with_private_key = true; 208b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT | TEE_USAGE_SIGN; 209b0104773SPascal Brand } else { 210b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 211b0104773SPascal Brand } 212b0104773SPascal Brand break; 213b0104773SPascal Brand 214b0104773SPascal Brand case TEE_ALG_DH_DERIVE_SHARED_SECRET: 2151220586eSCedric Chaumont case TEE_ALG_ECDH_P192: 2161220586eSCedric Chaumont case TEE_ALG_ECDH_P224: 2171220586eSCedric Chaumont case TEE_ALG_ECDH_P256: 2181220586eSCedric Chaumont case TEE_ALG_ECDH_P384: 2191220586eSCedric Chaumont case TEE_ALG_ECDH_P521: 220cdb198a7SJerome Forissier case TEE_ALG_HKDF_MD5_DERIVE_KEY: 221cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA1_DERIVE_KEY: 222cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA224_DERIVE_KEY: 223cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA256_DERIVE_KEY: 224cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA384_DERIVE_KEY: 225cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA512_DERIVE_KEY: 2268854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA1_DERIVE_KEY: 2278854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA224_DERIVE_KEY: 2288854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA256_DERIVE_KEY: 2298854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA384_DERIVE_KEY: 2308854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA512_DERIVE_KEY: 2310f2293b7SJerome Forissier case TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY: 232b0104773SPascal Brand if (mode != TEE_MODE_DERIVE) 233b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 234b0104773SPascal Brand with_private_key = true; 235b0104773SPascal Brand req_key_usage = TEE_USAGE_DERIVE; 236b0104773SPascal Brand break; 237b0104773SPascal Brand 238b0104773SPascal Brand case TEE_ALG_MD5: 239b0104773SPascal Brand case TEE_ALG_SHA1: 240b0104773SPascal Brand case TEE_ALG_SHA224: 241b0104773SPascal Brand case TEE_ALG_SHA256: 242b0104773SPascal Brand case TEE_ALG_SHA384: 243b0104773SPascal Brand case TEE_ALG_SHA512: 244b0104773SPascal Brand if (mode != TEE_MODE_DIGEST) 245b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 24605304565SCedric Chaumont /* v1.1: flags always set for digest operations */ 247b0104773SPascal Brand handle_state |= TEE_HANDLE_FLAG_KEY_SET; 248b0104773SPascal Brand req_key_usage = 0; 249b0104773SPascal Brand break; 250b0104773SPascal Brand 251b0104773SPascal Brand case TEE_ALG_DES_CBC_MAC_NOPAD: 252b0104773SPascal Brand case TEE_ALG_AES_CBC_MAC_NOPAD: 253b0104773SPascal Brand case TEE_ALG_AES_CBC_MAC_PKCS5: 254b0104773SPascal Brand case TEE_ALG_AES_CMAC: 255b0104773SPascal Brand case TEE_ALG_DES_CBC_MAC_PKCS5: 256b0104773SPascal Brand case TEE_ALG_DES3_CBC_MAC_NOPAD: 257b0104773SPascal Brand case TEE_ALG_DES3_CBC_MAC_PKCS5: 258b0104773SPascal Brand case TEE_ALG_HMAC_MD5: 259b0104773SPascal Brand case TEE_ALG_HMAC_SHA1: 260b0104773SPascal Brand case TEE_ALG_HMAC_SHA224: 261b0104773SPascal Brand case TEE_ALG_HMAC_SHA256: 262b0104773SPascal Brand case TEE_ALG_HMAC_SHA384: 263b0104773SPascal Brand case TEE_ALG_HMAC_SHA512: 264b0104773SPascal Brand if (mode != TEE_MODE_MAC) 265b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 266b0104773SPascal Brand req_key_usage = TEE_USAGE_MAC; 267b0104773SPascal Brand break; 268b0104773SPascal Brand 269b0104773SPascal Brand default: 270b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 271b0104773SPascal Brand } 272b0104773SPascal Brand 273b66f219bSJens Wiklander op = TEE_Malloc(sizeof(*op), TEE_MALLOC_FILL_ZERO); 2749b52c538SCedric Chaumont if (!op) 275b0104773SPascal Brand return TEE_ERROR_OUT_OF_MEMORY; 276b0104773SPascal Brand 277b0104773SPascal Brand op->info.algorithm = algorithm; 278b0104773SPascal Brand op->info.operationClass = TEE_ALG_GET_CLASS(algorithm); 279b0104773SPascal Brand op->info.mode = mode; 280b0104773SPascal Brand op->info.maxKeySize = maxKeySize; 281b0104773SPascal Brand op->info.requiredKeyUsage = req_key_usage; 282b0104773SPascal Brand op->info.handleState = handle_state; 283b0104773SPascal Brand 284b0104773SPascal Brand if (block_size > 1) { 285b0104773SPascal Brand size_t buffer_size = block_size; 286b0104773SPascal Brand 287b0104773SPascal Brand if (buffer_two_blocks) 288b0104773SPascal Brand buffer_size *= 2; 289b0104773SPascal Brand 2909b52c538SCedric Chaumont op->buffer = TEE_Malloc(buffer_size, 2919b52c538SCedric Chaumont TEE_USER_MEM_HINT_NO_FILL_ZERO); 292b0104773SPascal Brand if (op->buffer == NULL) { 293b0104773SPascal Brand res = TEE_ERROR_OUT_OF_MEMORY; 294b66f219bSJens Wiklander goto out; 295b0104773SPascal Brand } 296b0104773SPascal Brand } 297b0104773SPascal Brand op->block_size = block_size; 298b0104773SPascal Brand op->buffer_two_blocks = buffer_two_blocks; 299b0104773SPascal Brand 300b0104773SPascal Brand if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) { 301b0104773SPascal Brand uint32_t mks = maxKeySize; 302b0104773SPascal Brand TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm, 303b0104773SPascal Brand with_private_key); 304b0104773SPascal Brand 305b0104773SPascal Brand /* 306b0104773SPascal Brand * If two keys are expected the max key size is the sum of 307b0104773SPascal Brand * the size of both keys. 308b0104773SPascal Brand */ 309b0104773SPascal Brand if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) 310b0104773SPascal Brand mks /= 2; 311b0104773SPascal Brand 312b0104773SPascal Brand res = TEE_AllocateTransientObject(key_type, mks, &op->key1); 313b0104773SPascal Brand if (res != TEE_SUCCESS) 314b66f219bSJens Wiklander goto out; 315b0104773SPascal Brand 31605304565SCedric Chaumont if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) { 3179b52c538SCedric Chaumont res = TEE_AllocateTransientObject(key_type, mks, 318b0104773SPascal Brand &op->key2); 319b0104773SPascal Brand if (res != TEE_SUCCESS) 320b66f219bSJens Wiklander goto out; 321b0104773SPascal Brand } 322b0104773SPascal Brand } 323b0104773SPascal Brand 324e86f1266SJens Wiklander res = utee_cryp_state_alloc(algorithm, mode, (unsigned long)op->key1, 325e86f1266SJens Wiklander (unsigned long)op->key2, &op->state); 326b66f219bSJens Wiklander if (res != TEE_SUCCESS) 327b66f219bSJens Wiklander goto out; 328b0104773SPascal Brand 32905304565SCedric Chaumont /* 33005304565SCedric Chaumont * Initialize digest operations 33105304565SCedric Chaumont * Other multi-stage operations initialized w/ TEE_xxxInit functions 33205304565SCedric Chaumont * Non-applicable on asymmetric operations 33305304565SCedric Chaumont */ 33405304565SCedric Chaumont if (TEE_ALG_GET_CLASS(algorithm) == TEE_OPERATION_DIGEST) { 33505304565SCedric Chaumont res = utee_hash_init(op->state, NULL, 0); 33605304565SCedric Chaumont if (res != TEE_SUCCESS) 337b66f219bSJens Wiklander goto out; 33805304565SCedric Chaumont /* v1.1: flags always set for digest operations */ 33905304565SCedric Chaumont op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 34005304565SCedric Chaumont } 34105304565SCedric Chaumont 342642a1607SCedric Chaumont op->operationState = TEE_OPERATION_STATE_INITIAL; 343642a1607SCedric Chaumont 344b0104773SPascal Brand *operation = op; 345b0104773SPascal Brand 346b66f219bSJens Wiklander out: 347b66f219bSJens Wiklander if (res != TEE_SUCCESS) { 348b66f219bSJens Wiklander if (res != TEE_ERROR_OUT_OF_MEMORY && 3499b52c538SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 350b36311adSJerome Forissier TEE_Panic(res); 351b66f219bSJens Wiklander if (op) { 352b66f219bSJens Wiklander if (op->state) { 353b66f219bSJens Wiklander TEE_FreeOperation(op); 354b66f219bSJens Wiklander } else { 355b66f219bSJens Wiklander TEE_Free(op->buffer); 356b66f219bSJens Wiklander TEE_FreeTransientObject(op->key1); 357b66f219bSJens Wiklander TEE_FreeTransientObject(op->key2); 358b66f219bSJens Wiklander TEE_Free(op); 359b66f219bSJens Wiklander } 360b66f219bSJens Wiklander } 361b66f219bSJens Wiklander } 362b66f219bSJens Wiklander 363b0104773SPascal Brand return res; 364b0104773SPascal Brand } 365b0104773SPascal Brand 366b0104773SPascal Brand void TEE_FreeOperation(TEE_OperationHandle operation) 367b0104773SPascal Brand { 368e889e80bSCedric Chaumont TEE_Result res; 369e889e80bSCedric Chaumont 370e889e80bSCedric Chaumont if (operation == TEE_HANDLE_NULL) 371e889e80bSCedric Chaumont TEE_Panic(0); 372e889e80bSCedric Chaumont 373b0104773SPascal Brand /* 374b0104773SPascal Brand * Note that keys should not be freed here, since they are 375b0104773SPascal Brand * claimed by the operation they will be freed by 376b0104773SPascal Brand * utee_cryp_state_free(). 377b0104773SPascal Brand */ 378e889e80bSCedric Chaumont res = utee_cryp_state_free(operation->state); 379e889e80bSCedric Chaumont if (res != TEE_SUCCESS) 380b36311adSJerome Forissier TEE_Panic(res); 381e889e80bSCedric Chaumont 382b0104773SPascal Brand TEE_Free(operation->buffer); 383b0104773SPascal Brand TEE_Free(operation); 384b0104773SPascal Brand } 385b0104773SPascal Brand 386b0104773SPascal Brand void TEE_GetOperationInfo(TEE_OperationHandle operation, 387b0104773SPascal Brand TEE_OperationInfo *operationInfo) 388b0104773SPascal Brand { 389b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 390b0104773SPascal Brand TEE_Panic(0); 391b0104773SPascal Brand 39205304565SCedric Chaumont if (!operationInfo) 393b0104773SPascal Brand TEE_Panic(0); 394b0104773SPascal Brand 395b0104773SPascal Brand *operationInfo = operation->info; 396b0104773SPascal Brand } 397b0104773SPascal Brand 39805304565SCedric Chaumont TEE_Result TEE_GetOperationInfoMultiple(TEE_OperationHandle operation, 39905304565SCedric Chaumont TEE_OperationInfoMultiple *operationInfoMultiple, 40005304565SCedric Chaumont uint32_t *operationSize) 40105304565SCedric Chaumont { 40205304565SCedric Chaumont TEE_Result res = TEE_SUCCESS; 40305304565SCedric Chaumont TEE_ObjectInfo key_info1; 40405304565SCedric Chaumont TEE_ObjectInfo key_info2; 40505304565SCedric Chaumont uint32_t num_of_keys; 40605304565SCedric Chaumont size_t n; 40705304565SCedric Chaumont 40805304565SCedric Chaumont if (operation == TEE_HANDLE_NULL) { 40905304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 41005304565SCedric Chaumont goto out; 41105304565SCedric Chaumont } 41205304565SCedric Chaumont 41305304565SCedric Chaumont if (!operationInfoMultiple) { 41405304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 41505304565SCedric Chaumont goto out; 41605304565SCedric Chaumont } 41705304565SCedric Chaumont 41805304565SCedric Chaumont if (!operationSize) { 41905304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 42005304565SCedric Chaumont goto out; 42105304565SCedric Chaumont } 42205304565SCedric Chaumont 42305304565SCedric Chaumont num_of_keys = (*operationSize-sizeof(TEE_OperationInfoMultiple))/ 42405304565SCedric Chaumont sizeof(TEE_OperationInfoKey); 42505304565SCedric Chaumont 42605304565SCedric Chaumont if (num_of_keys > 2) { 42705304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 42805304565SCedric Chaumont goto out; 42905304565SCedric Chaumont } 43005304565SCedric Chaumont 43105304565SCedric Chaumont /* Two keys flag (TEE_ALG_AES_XTS only) */ 43205304565SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 43305304565SCedric Chaumont 0 && 43405304565SCedric Chaumont (num_of_keys != 2)) { 43505304565SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 43605304565SCedric Chaumont goto out; 43705304565SCedric Chaumont } 43805304565SCedric Chaumont 43905304565SCedric Chaumont /* Clear */ 44005304565SCedric Chaumont for (n = 0; n < num_of_keys; n++) { 44105304565SCedric Chaumont operationInfoMultiple->keyInformation[n].keySize = 0; 44205304565SCedric Chaumont operationInfoMultiple->keyInformation[n].requiredKeyUsage = 0; 44305304565SCedric Chaumont } 44405304565SCedric Chaumont 44505304565SCedric Chaumont if (num_of_keys == 2) { 44605304565SCedric Chaumont res = TEE_GetObjectInfo1(operation->key2, &key_info2); 44705304565SCedric Chaumont /* Key2 is not a valid handle */ 44805304565SCedric Chaumont if (res != TEE_SUCCESS) 44905304565SCedric Chaumont goto out; 45005304565SCedric Chaumont 45105304565SCedric Chaumont operationInfoMultiple->keyInformation[1].keySize = 45205304565SCedric Chaumont key_info2.keySize; 45305304565SCedric Chaumont operationInfoMultiple->keyInformation[1].requiredKeyUsage = 45405304565SCedric Chaumont operation->info.requiredKeyUsage; 45505304565SCedric Chaumont } 45605304565SCedric Chaumont 45705304565SCedric Chaumont if (num_of_keys >= 1) { 45805304565SCedric Chaumont res = TEE_GetObjectInfo1(operation->key1, &key_info1); 45905304565SCedric Chaumont /* Key1 is not a valid handle */ 46005304565SCedric Chaumont if (res != TEE_SUCCESS) { 46105304565SCedric Chaumont if (num_of_keys == 2) { 46205304565SCedric Chaumont operationInfoMultiple->keyInformation[1]. 46305304565SCedric Chaumont keySize = 0; 46405304565SCedric Chaumont operationInfoMultiple->keyInformation[1]. 46505304565SCedric Chaumont requiredKeyUsage = 0; 46605304565SCedric Chaumont } 46705304565SCedric Chaumont goto out; 46805304565SCedric Chaumont } 46905304565SCedric Chaumont 47005304565SCedric Chaumont operationInfoMultiple->keyInformation[0].keySize = 47105304565SCedric Chaumont key_info1.keySize; 47205304565SCedric Chaumont operationInfoMultiple->keyInformation[0].requiredKeyUsage = 47305304565SCedric Chaumont operation->info.requiredKeyUsage; 47405304565SCedric Chaumont } 47505304565SCedric Chaumont 47605304565SCedric Chaumont /* No key */ 47705304565SCedric Chaumont operationInfoMultiple->algorithm = operation->info.algorithm; 47805304565SCedric Chaumont operationInfoMultiple->operationClass = operation->info.operationClass; 47905304565SCedric Chaumont operationInfoMultiple->mode = operation->info.mode; 48005304565SCedric Chaumont operationInfoMultiple->digestLength = operation->info.digestLength; 48105304565SCedric Chaumont operationInfoMultiple->maxKeySize = operation->info.maxKeySize; 48205304565SCedric Chaumont operationInfoMultiple->handleState = operation->info.handleState; 48305304565SCedric Chaumont operationInfoMultiple->operationState = operation->operationState; 48405304565SCedric Chaumont operationInfoMultiple->numberOfKeys = num_of_keys; 48505304565SCedric Chaumont 48605304565SCedric Chaumont out: 48705304565SCedric Chaumont if (res != TEE_SUCCESS && 48805304565SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 489b36311adSJerome Forissier TEE_Panic(res); 49005304565SCedric Chaumont 49105304565SCedric Chaumont return res; 49205304565SCedric Chaumont } 49305304565SCedric Chaumont 494b0104773SPascal Brand void TEE_ResetOperation(TEE_OperationHandle operation) 495b0104773SPascal Brand { 496b0104773SPascal Brand TEE_Result res; 497b0104773SPascal Brand 498b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 499b0104773SPascal Brand TEE_Panic(0); 500bf80076aSCedric Chaumont 501642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET)) 502bf80076aSCedric Chaumont TEE_Panic(0); 503bf80076aSCedric Chaumont 504642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 505642a1607SCedric Chaumont 506b0104773SPascal Brand if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 507b0104773SPascal Brand res = utee_hash_init(operation->state, NULL, 0); 508b0104773SPascal Brand if (res != TEE_SUCCESS) 509b0104773SPascal Brand TEE_Panic(res); 51005304565SCedric Chaumont operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 51105304565SCedric Chaumont } else { 512b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 513b0104773SPascal Brand } 51405304565SCedric Chaumont } 515b0104773SPascal Brand 516b0104773SPascal Brand TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation, 517b0104773SPascal Brand TEE_ObjectHandle key) 518b0104773SPascal Brand { 5197583c59eSCedric Chaumont TEE_Result res; 520b0104773SPascal Brand uint32_t key_size = 0; 521b0104773SPascal Brand TEE_ObjectInfo key_info; 522b0104773SPascal Brand 523a57c1e2eSCedric Chaumont if (operation == TEE_HANDLE_NULL) { 524a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 525a57c1e2eSCedric Chaumont goto out; 526a57c1e2eSCedric Chaumont } 527a57c1e2eSCedric Chaumont 528642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 529642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 530642a1607SCedric Chaumont goto out; 531642a1607SCedric Chaumont } 532642a1607SCedric Chaumont 533a57c1e2eSCedric Chaumont if (key == TEE_HANDLE_NULL) { 534a57c1e2eSCedric Chaumont /* Operation key cleared */ 535a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key1); 536a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 537a57c1e2eSCedric Chaumont goto out; 538a57c1e2eSCedric Chaumont } 539a57c1e2eSCedric Chaumont 540a57c1e2eSCedric Chaumont /* No key for digest operation */ 541a57c1e2eSCedric Chaumont if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 542a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 543a57c1e2eSCedric Chaumont goto out; 544a57c1e2eSCedric Chaumont } 545a57c1e2eSCedric Chaumont 546a57c1e2eSCedric Chaumont /* Two keys flag not expected (TEE_ALG_AES_XTS excluded) */ 547a57c1e2eSCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 548a57c1e2eSCedric Chaumont 0) { 549a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 550a57c1e2eSCedric Chaumont goto out; 551a57c1e2eSCedric Chaumont } 552a57c1e2eSCedric Chaumont 5537583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key, &key_info); 554a57c1e2eSCedric Chaumont /* Key is not a valid handle */ 5557583c59eSCedric Chaumont if (res != TEE_SUCCESS) 556a57c1e2eSCedric Chaumont goto out; 5577583c59eSCedric Chaumont 558b0104773SPascal Brand /* Supplied key has to meet required usage */ 559b0104773SPascal Brand if ((key_info.objectUsage & operation->info.requiredKeyUsage) != 560b0104773SPascal Brand operation->info.requiredKeyUsage) { 561a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 562a57c1e2eSCedric Chaumont goto out; 563b0104773SPascal Brand } 564b0104773SPascal Brand 565a57c1e2eSCedric Chaumont if (operation->info.maxKeySize < key_info.keySize) { 566a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 567a57c1e2eSCedric Chaumont goto out; 568a57c1e2eSCedric Chaumont } 569b0104773SPascal Brand 5707583c59eSCedric Chaumont key_size = key_info.keySize; 571b0104773SPascal Brand 572b0104773SPascal Brand TEE_ResetTransientObject(operation->key1); 573b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 574b0104773SPascal Brand 5757583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key1, key); 5767583c59eSCedric Chaumont if (res != TEE_SUCCESS) 577a57c1e2eSCedric Chaumont goto out; 5787583c59eSCedric Chaumont 579b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 580b0104773SPascal Brand 581b0104773SPascal Brand operation->info.keySize = key_size; 582b0104773SPascal Brand 5837583c59eSCedric Chaumont out: 584a57c1e2eSCedric Chaumont if (res != TEE_SUCCESS && 585a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 586a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 587b36311adSJerome Forissier TEE_Panic(res); 588a57c1e2eSCedric Chaumont 589a57c1e2eSCedric Chaumont return res; 590b0104773SPascal Brand } 591b0104773SPascal Brand 592b0104773SPascal Brand TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation, 593b0104773SPascal Brand TEE_ObjectHandle key1, TEE_ObjectHandle key2) 594b0104773SPascal Brand { 5957583c59eSCedric Chaumont TEE_Result res; 596b0104773SPascal Brand uint32_t key_size = 0; 597b0104773SPascal Brand TEE_ObjectInfo key_info1; 598b0104773SPascal Brand TEE_ObjectInfo key_info2; 599b0104773SPascal Brand 600a57c1e2eSCedric Chaumont if (operation == TEE_HANDLE_NULL) { 601a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 602a57c1e2eSCedric Chaumont goto out; 603a57c1e2eSCedric Chaumont } 604a57c1e2eSCedric Chaumont 605642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 606642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 607642a1607SCedric Chaumont goto out; 608642a1607SCedric Chaumont } 609642a1607SCedric Chaumont 610a57c1e2eSCedric Chaumont /* 611a57c1e2eSCedric Chaumont * Key1/Key2 and/or are not initialized and 612a57c1e2eSCedric Chaumont * Either both keys are NULL or both are not NULL 613a57c1e2eSCedric Chaumont */ 614a57c1e2eSCedric Chaumont if (key1 == TEE_HANDLE_NULL || key2 == TEE_HANDLE_NULL) { 615a57c1e2eSCedric Chaumont /* Clear operation key1 (if needed) */ 616a57c1e2eSCedric Chaumont if (key1 == TEE_HANDLE_NULL) 617a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key1); 618a57c1e2eSCedric Chaumont /* Clear operation key2 (if needed) */ 619a57c1e2eSCedric Chaumont if (key2 == TEE_HANDLE_NULL) 620a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key2); 621a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 622a57c1e2eSCedric Chaumont goto out; 623a57c1e2eSCedric Chaumont } 624a57c1e2eSCedric Chaumont 625a57c1e2eSCedric Chaumont /* No key for digest operation */ 626a57c1e2eSCedric Chaumont if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 627a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 628a57c1e2eSCedric Chaumont goto out; 629a57c1e2eSCedric Chaumont } 630a57c1e2eSCedric Chaumont 631a57c1e2eSCedric Chaumont /* Two keys flag expected (TEE_ALG_AES_XTS only) */ 632a57c1e2eSCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 633a57c1e2eSCedric Chaumont 0) { 634a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 635a57c1e2eSCedric Chaumont goto out; 636a57c1e2eSCedric Chaumont } 637a57c1e2eSCedric Chaumont 6387583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key1, &key_info1); 639a57c1e2eSCedric Chaumont /* Key1 is not a valid handle */ 6407583c59eSCedric Chaumont if (res != TEE_SUCCESS) 641a57c1e2eSCedric Chaumont goto out; 6427583c59eSCedric Chaumont 643b0104773SPascal Brand /* Supplied key has to meet required usage */ 644b0104773SPascal Brand if ((key_info1.objectUsage & operation->info. 645b0104773SPascal Brand requiredKeyUsage) != operation->info.requiredKeyUsage) { 646a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 647a57c1e2eSCedric Chaumont goto out; 648b0104773SPascal Brand } 649b0104773SPascal Brand 6507583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key2, &key_info2); 651a57c1e2eSCedric Chaumont /* Key2 is not a valid handle */ 6527583c59eSCedric Chaumont if (res != TEE_SUCCESS) { 6537583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) 6547583c59eSCedric Chaumont res = TEE_ERROR_CORRUPT_OBJECT_2; 655a57c1e2eSCedric Chaumont goto out; 6567583c59eSCedric Chaumont } 6577583c59eSCedric Chaumont 658b0104773SPascal Brand /* Supplied key has to meet required usage */ 659b0104773SPascal Brand if ((key_info2.objectUsage & operation->info. 660b0104773SPascal Brand requiredKeyUsage) != operation->info.requiredKeyUsage) { 661a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 662a57c1e2eSCedric Chaumont goto out; 663b0104773SPascal Brand } 664b0104773SPascal Brand 665b0104773SPascal Brand /* 666b0104773SPascal Brand * AES-XTS (the only multi key algorithm supported, requires the 667b0104773SPascal Brand * keys to be of equal size. 668b0104773SPascal Brand */ 669b0104773SPascal Brand if (operation->info.algorithm == TEE_ALG_AES_XTS && 670a57c1e2eSCedric Chaumont key_info1.keySize != key_info2.keySize) { 671a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 672a57c1e2eSCedric Chaumont goto out; 673b0104773SPascal Brand 674a57c1e2eSCedric Chaumont } 675a57c1e2eSCedric Chaumont 676a57c1e2eSCedric Chaumont if (operation->info.maxKeySize < key_info1.keySize) { 677a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 678a57c1e2eSCedric Chaumont goto out; 679a57c1e2eSCedric Chaumont } 680b0104773SPascal Brand 681b0104773SPascal Brand /* 682b0104773SPascal Brand * Odd that only the size of one key should be reported while 683b0104773SPascal Brand * size of two key are used when allocating the operation. 684b0104773SPascal Brand */ 6857583c59eSCedric Chaumont key_size = key_info1.keySize; 686b0104773SPascal Brand 687b0104773SPascal Brand TEE_ResetTransientObject(operation->key1); 688b0104773SPascal Brand TEE_ResetTransientObject(operation->key2); 689b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 690b0104773SPascal Brand 6917583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key1, key1); 6927583c59eSCedric Chaumont if (res != TEE_SUCCESS) 693a57c1e2eSCedric Chaumont goto out; 6947583c59eSCedric Chaumont 6957583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key2, key2); 6967583c59eSCedric Chaumont if (res != TEE_SUCCESS) { 6977583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) 6987583c59eSCedric Chaumont res = TEE_ERROR_CORRUPT_OBJECT_2; 699a57c1e2eSCedric Chaumont goto out; 7007583c59eSCedric Chaumont } 7017583c59eSCedric Chaumont 702b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 703b0104773SPascal Brand 704b0104773SPascal Brand operation->info.keySize = key_size; 705b0104773SPascal Brand 7067583c59eSCedric Chaumont out: 707a57c1e2eSCedric Chaumont if (res != TEE_SUCCESS && 708a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 709a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT_2 && 710a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE && 711a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE_2) 712b36311adSJerome Forissier TEE_Panic(res); 713a57c1e2eSCedric Chaumont 714a57c1e2eSCedric Chaumont return res; 715b0104773SPascal Brand } 716b0104773SPascal Brand 717b0104773SPascal Brand void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op) 718b0104773SPascal Brand { 719b0104773SPascal Brand TEE_Result res; 720b0104773SPascal Brand 721b0104773SPascal Brand if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL) 722b0104773SPascal Brand TEE_Panic(0); 723b0104773SPascal Brand if (dst_op->info.algorithm != src_op->info.algorithm) 724b0104773SPascal Brand TEE_Panic(0); 725b0104773SPascal Brand if (src_op->info.operationClass != TEE_OPERATION_DIGEST) { 726b0104773SPascal Brand TEE_ObjectHandle key1 = TEE_HANDLE_NULL; 727b0104773SPascal Brand TEE_ObjectHandle key2 = TEE_HANDLE_NULL; 728b0104773SPascal Brand 729b0104773SPascal Brand if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) { 730b0104773SPascal Brand key1 = src_op->key1; 731b0104773SPascal Brand key2 = src_op->key2; 732b0104773SPascal Brand } 733b0104773SPascal Brand 734b0104773SPascal Brand if ((src_op->info.handleState & 735b0104773SPascal Brand TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) { 736b0104773SPascal Brand TEE_SetOperationKey(dst_op, key1); 737b0104773SPascal Brand } else { 738b0104773SPascal Brand TEE_SetOperationKey2(dst_op, key1, key2); 739b0104773SPascal Brand } 740b0104773SPascal Brand } 741b0104773SPascal Brand dst_op->info.handleState = src_op->info.handleState; 742b0104773SPascal Brand dst_op->info.keySize = src_op->info.keySize; 743642a1607SCedric Chaumont dst_op->operationState = src_op->operationState; 744b0104773SPascal Brand 745b0104773SPascal Brand if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks || 746b0104773SPascal Brand dst_op->block_size != src_op->block_size) 747b0104773SPascal Brand TEE_Panic(0); 748b0104773SPascal Brand 749b0104773SPascal Brand if (dst_op->buffer != NULL) { 750b0104773SPascal Brand if (src_op->buffer == NULL) 751b0104773SPascal Brand TEE_Panic(0); 752b0104773SPascal Brand 753b0104773SPascal Brand memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs); 754b0104773SPascal Brand dst_op->buffer_offs = src_op->buffer_offs; 755b0104773SPascal Brand } else if (src_op->buffer != NULL) { 756b0104773SPascal Brand TEE_Panic(0); 757b0104773SPascal Brand } 758b0104773SPascal Brand 759b0104773SPascal Brand res = utee_cryp_state_copy(dst_op->state, src_op->state); 760b0104773SPascal Brand if (res != TEE_SUCCESS) 761b0104773SPascal Brand TEE_Panic(res); 762b0104773SPascal Brand } 763b0104773SPascal Brand 764b0104773SPascal Brand /* Cryptographic Operations API - Message Digest Functions */ 765b0104773SPascal Brand 7668f07fe6fSJerome Forissier static void init_hash_operation(TEE_OperationHandle operation, const void *IV, 7676d15db08SJerome Forissier uint32_t IVLen) 7686d15db08SJerome Forissier { 7696d15db08SJerome Forissier TEE_Result res; 7706d15db08SJerome Forissier 7716d15db08SJerome Forissier /* 7726d15db08SJerome Forissier * Note : IV and IVLen are never used in current implementation 7736d15db08SJerome Forissier * This is why coherent values of IV and IVLen are not checked 7746d15db08SJerome Forissier */ 7756d15db08SJerome Forissier res = utee_hash_init(operation->state, IV, IVLen); 7766d15db08SJerome Forissier if (res != TEE_SUCCESS) 7776d15db08SJerome Forissier TEE_Panic(res); 7786d15db08SJerome Forissier operation->buffer_offs = 0; 7796d15db08SJerome Forissier operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 7806d15db08SJerome Forissier } 7816d15db08SJerome Forissier 782b0104773SPascal Brand void TEE_DigestUpdate(TEE_OperationHandle operation, 7838f07fe6fSJerome Forissier const void *chunk, uint32_t chunkSize) 784b0104773SPascal Brand { 78573d6c3baSJoakim Bech TEE_Result res = TEE_ERROR_GENERIC; 786b0104773SPascal Brand 78773d6c3baSJoakim Bech if (operation == TEE_HANDLE_NULL || 78873d6c3baSJoakim Bech operation->info.operationClass != TEE_OPERATION_DIGEST) 789b0104773SPascal Brand TEE_Panic(0); 79073d6c3baSJoakim Bech 791642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 792642a1607SCedric Chaumont 793b0104773SPascal Brand res = utee_hash_update(operation->state, chunk, chunkSize); 794b0104773SPascal Brand if (res != TEE_SUCCESS) 795b0104773SPascal Brand TEE_Panic(res); 796b0104773SPascal Brand } 797b0104773SPascal Brand 7988f07fe6fSJerome Forissier TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void *chunk, 79979a3c601SCedric Chaumont uint32_t chunkLen, void *hash, uint32_t *hashLen) 800b0104773SPascal Brand { 80187c2f6b6SCedric Chaumont TEE_Result res; 802e86f1266SJens Wiklander uint64_t hl; 80387c2f6b6SCedric Chaumont 80487c2f6b6SCedric Chaumont if ((operation == TEE_HANDLE_NULL) || 80587c2f6b6SCedric Chaumont (!chunk && chunkLen) || 80687c2f6b6SCedric Chaumont !hash || 80787c2f6b6SCedric Chaumont !hashLen || 80887c2f6b6SCedric Chaumont (operation->info.operationClass != TEE_OPERATION_DIGEST)) { 80987c2f6b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 81087c2f6b6SCedric Chaumont goto out; 81187c2f6b6SCedric Chaumont } 81287c2f6b6SCedric Chaumont 813e86f1266SJens Wiklander hl = *hashLen; 814e86f1266SJens Wiklander res = utee_hash_final(operation->state, chunk, chunkLen, hash, &hl); 815e86f1266SJens Wiklander *hashLen = hl; 8166d15db08SJerome Forissier if (res != TEE_SUCCESS) 8176d15db08SJerome Forissier goto out; 8186d15db08SJerome Forissier 8196d15db08SJerome Forissier /* Reset operation state */ 8206d15db08SJerome Forissier init_hash_operation(operation, NULL, 0); 82187c2f6b6SCedric Chaumont 822642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 823642a1607SCedric Chaumont 82487c2f6b6SCedric Chaumont out: 82587c2f6b6SCedric Chaumont if (res != TEE_SUCCESS && 82687c2f6b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 827b36311adSJerome Forissier TEE_Panic(res); 82873d6c3baSJoakim Bech 82987c2f6b6SCedric Chaumont return res; 830b0104773SPascal Brand } 831b0104773SPascal Brand 832b0104773SPascal Brand /* Cryptographic Operations API - Symmetric Cipher Functions */ 833b0104773SPascal Brand 8348f07fe6fSJerome Forissier void TEE_CipherInit(TEE_OperationHandle operation, const void *IV, 8358f07fe6fSJerome Forissier uint32_t IVLen) 836b0104773SPascal Brand { 837b0104773SPascal Brand TEE_Result res; 838b0104773SPascal Brand 839b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 840b0104773SPascal Brand TEE_Panic(0); 841642a1607SCedric Chaumont 842b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_CIPHER) 843b0104773SPascal Brand TEE_Panic(0); 844642a1607SCedric Chaumont 845642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 846642a1607SCedric Chaumont !(operation->key1)) 847642a1607SCedric Chaumont TEE_Panic(0); 848642a1607SCedric Chaumont 849642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 850642a1607SCedric Chaumont TEE_ResetOperation(operation); 851642a1607SCedric Chaumont 852642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 853642a1607SCedric Chaumont 854b0104773SPascal Brand res = utee_cipher_init(operation->state, IV, IVLen); 855b0104773SPascal Brand if (res != TEE_SUCCESS) 856b0104773SPascal Brand TEE_Panic(res); 857642a1607SCedric Chaumont 858b0104773SPascal Brand operation->buffer_offs = 0; 859b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 860b0104773SPascal Brand } 861b0104773SPascal Brand 862b0104773SPascal Brand static TEE_Result tee_buffer_update( 863b0104773SPascal Brand TEE_OperationHandle op, 864e86f1266SJens Wiklander TEE_Result(*update_func)(unsigned long state, const void *src, 865e86f1266SJens Wiklander size_t slen, void *dst, uint64_t *dlen), 866b0104773SPascal Brand const void *src_data, size_t src_len, 867e86f1266SJens Wiklander void *dest_data, uint64_t *dest_len) 868b0104773SPascal Brand { 869b0104773SPascal Brand TEE_Result res; 870b0104773SPascal Brand const uint8_t *src = src_data; 871b0104773SPascal Brand size_t slen = src_len; 872b0104773SPascal Brand uint8_t *dst = dest_data; 873b0104773SPascal Brand size_t dlen = *dest_len; 874b0104773SPascal Brand size_t acc_dlen = 0; 875e86f1266SJens Wiklander uint64_t tmp_dlen; 876b0104773SPascal Brand size_t l; 877b0104773SPascal Brand size_t buffer_size; 878d3588802SPascal Brand size_t buffer_left; 879b0104773SPascal Brand 880090268f5SJens Wiklander if (!src) { 881090268f5SJens Wiklander if (slen) 882090268f5SJens Wiklander TEE_Panic(0); 883090268f5SJens Wiklander goto out; 884090268f5SJens Wiklander } 885090268f5SJens Wiklander 886d3588802SPascal Brand if (op->buffer_two_blocks) { 887b0104773SPascal Brand buffer_size = op->block_size * 2; 888d3588802SPascal Brand buffer_left = 1; 889d3588802SPascal Brand } else { 890b0104773SPascal Brand buffer_size = op->block_size; 891d3588802SPascal Brand buffer_left = 0; 892d3588802SPascal Brand } 893b0104773SPascal Brand 894b0104773SPascal Brand if (op->buffer_offs > 0) { 895b0104773SPascal Brand /* Fill up complete block */ 896b0104773SPascal Brand if (op->buffer_offs < op->block_size) 897b0104773SPascal Brand l = MIN(slen, op->block_size - op->buffer_offs); 898b0104773SPascal Brand else 899b0104773SPascal Brand l = MIN(slen, buffer_size - op->buffer_offs); 900b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, l); 901b0104773SPascal Brand op->buffer_offs += l; 902b0104773SPascal Brand src += l; 903b0104773SPascal Brand slen -= l; 904b0104773SPascal Brand if ((op->buffer_offs % op->block_size) != 0) 905b0104773SPascal Brand goto out; /* Nothing left to do */ 906b0104773SPascal Brand } 907b0104773SPascal Brand 908b0104773SPascal Brand /* If we can feed from buffer */ 909d3588802SPascal Brand if ((op->buffer_offs > 0) && 910d3588802SPascal Brand ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) { 9112ff3fdbbSPascal Brand l = ROUNDUP(op->buffer_offs + slen - buffer_size, 912b0104773SPascal Brand op->block_size); 913b0104773SPascal Brand l = MIN(op->buffer_offs, l); 914b0104773SPascal Brand tmp_dlen = dlen; 915b0104773SPascal Brand res = update_func(op->state, op->buffer, l, dst, &tmp_dlen); 916b0104773SPascal Brand if (res != TEE_SUCCESS) 917b0104773SPascal Brand TEE_Panic(res); 918b0104773SPascal Brand dst += tmp_dlen; 919b0104773SPascal Brand dlen -= tmp_dlen; 920b0104773SPascal Brand acc_dlen += tmp_dlen; 921b0104773SPascal Brand op->buffer_offs -= l; 922b0104773SPascal Brand if (op->buffer_offs > 0) { 923b0104773SPascal Brand /* 924b0104773SPascal Brand * Slen is small enough to be contained in rest buffer. 925b0104773SPascal Brand */ 926b0104773SPascal Brand memcpy(op->buffer, op->buffer + l, buffer_size - l); 927b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 928b0104773SPascal Brand op->buffer_offs += slen; 929b0104773SPascal Brand goto out; /* Nothing left to do */ 930b0104773SPascal Brand } 931b0104773SPascal Brand } 932b0104773SPascal Brand 933d3588802SPascal Brand if (slen >= (buffer_size + buffer_left)) { 934b0104773SPascal Brand /* Buffer is empty, feed as much as possible from src */ 935bf7a587fSJerome Forissier if (op->info.algorithm == TEE_ALG_AES_CTS) 936b1ecda78SJerome Forissier l = ROUNDUP(slen - buffer_size, op->block_size); 937bf7a587fSJerome Forissier else 938bf7a587fSJerome Forissier l = ROUNDUP(slen - buffer_size + 1, op->block_size); 939b0104773SPascal Brand 940b0104773SPascal Brand tmp_dlen = dlen; 941b0104773SPascal Brand res = update_func(op->state, src, l, dst, &tmp_dlen); 942b0104773SPascal Brand if (res != TEE_SUCCESS) 943b0104773SPascal Brand TEE_Panic(res); 944b0104773SPascal Brand src += l; 945b0104773SPascal Brand slen -= l; 946b0104773SPascal Brand dst += tmp_dlen; 947b0104773SPascal Brand dlen -= tmp_dlen; 948b0104773SPascal Brand acc_dlen += tmp_dlen; 949b0104773SPascal Brand } 950b0104773SPascal Brand 951b0104773SPascal Brand /* Slen is small enough to be contained in buffer. */ 952b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 953b0104773SPascal Brand op->buffer_offs += slen; 954b0104773SPascal Brand 955b0104773SPascal Brand out: 956b0104773SPascal Brand *dest_len = acc_dlen; 957b0104773SPascal Brand return TEE_SUCCESS; 958b0104773SPascal Brand } 959b0104773SPascal Brand 9608f07fe6fSJerome Forissier TEE_Result TEE_CipherUpdate(TEE_OperationHandle operation, const void *srcData, 96179a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 962b0104773SPascal Brand { 963dea1f2b6SCedric Chaumont TEE_Result res; 964b0104773SPascal Brand size_t req_dlen; 965e86f1266SJens Wiklander uint64_t dl; 966b0104773SPascal Brand 967642a1607SCedric Chaumont if (operation == TEE_HANDLE_NULL || 968dea1f2b6SCedric Chaumont (srcData == NULL && srcLen != 0) || 969dea1f2b6SCedric Chaumont destLen == NULL || 970dea1f2b6SCedric Chaumont (destData == NULL && *destLen != 0)) { 971dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 972dea1f2b6SCedric Chaumont goto out; 973dea1f2b6SCedric Chaumont } 974dea1f2b6SCedric Chaumont 975642a1607SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 976dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 977dea1f2b6SCedric Chaumont goto out; 978dea1f2b6SCedric Chaumont } 979dea1f2b6SCedric Chaumont 980642a1607SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 981642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 982642a1607SCedric Chaumont goto out; 983642a1607SCedric Chaumont } 984642a1607SCedric Chaumont 985642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 986dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 987dea1f2b6SCedric Chaumont goto out; 988dea1f2b6SCedric Chaumont } 989b0104773SPascal Brand 990e32c5ddfSJerome Forissier if (!srcData && !srcLen) { 991090268f5SJens Wiklander *destLen = 0; 992e32c5ddfSJerome Forissier res = TEE_SUCCESS; 993e32c5ddfSJerome Forissier goto out; 994e32c5ddfSJerome Forissier } 995e32c5ddfSJerome Forissier 996b0104773SPascal Brand /* Calculate required dlen */ 99757aabac5SBogdan Liulko if (operation->block_size > 1) { 99857aabac5SBogdan Liulko req_dlen = ((operation->buffer_offs + srcLen) / 99957aabac5SBogdan Liulko operation->block_size) * operation->block_size; 100057aabac5SBogdan Liulko } else { 100157aabac5SBogdan Liulko req_dlen = srcLen; 100257aabac5SBogdan Liulko } 1003642a1607SCedric Chaumont if (operation->buffer_two_blocks) { 1004642a1607SCedric Chaumont if (req_dlen > operation->block_size * 2) 1005642a1607SCedric Chaumont req_dlen -= operation->block_size * 2; 1006b0104773SPascal Brand else 1007b0104773SPascal Brand req_dlen = 0; 1008b0104773SPascal Brand } 1009b0104773SPascal Brand /* 1010b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1011b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1012b0104773SPascal Brand * can't restore sync with this API. 1013b0104773SPascal Brand */ 1014b0104773SPascal Brand if (*destLen < req_dlen) { 1015b0104773SPascal Brand *destLen = req_dlen; 1016dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1017dea1f2b6SCedric Chaumont goto out; 1018b0104773SPascal Brand } 1019b0104773SPascal Brand 1020e86f1266SJens Wiklander dl = *destLen; 102157aabac5SBogdan Liulko if (operation->block_size > 1) { 102257aabac5SBogdan Liulko res = tee_buffer_update(operation, utee_cipher_update, srcData, 102357aabac5SBogdan Liulko srcLen, destData, &dl); 102457aabac5SBogdan Liulko } else { 102557aabac5SBogdan Liulko if (srcLen > 0) { 102657aabac5SBogdan Liulko res = utee_cipher_update(operation->state, srcData, 102757aabac5SBogdan Liulko srcLen, destData, &dl); 102857aabac5SBogdan Liulko } else { 102957aabac5SBogdan Liulko res = TEE_SUCCESS; 103057aabac5SBogdan Liulko dl = 0; 103157aabac5SBogdan Liulko } 103257aabac5SBogdan Liulko } 1033e86f1266SJens Wiklander *destLen = dl; 1034b0104773SPascal Brand 1035dea1f2b6SCedric Chaumont out: 1036dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 1037dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1038b36311adSJerome Forissier TEE_Panic(res); 1039dea1f2b6SCedric Chaumont 1040dea1f2b6SCedric Chaumont return res; 1041b0104773SPascal Brand } 1042b0104773SPascal Brand 1043642a1607SCedric Chaumont TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation, 10448f07fe6fSJerome Forissier const void *srcData, uint32_t srcLen, 10458f07fe6fSJerome Forissier void *destData, uint32_t *destLen) 1046b0104773SPascal Brand { 1047b0104773SPascal Brand TEE_Result res; 1048b0104773SPascal Brand uint8_t *dst = destData; 1049b0104773SPascal Brand size_t acc_dlen = 0; 1050e86f1266SJens Wiklander uint64_t tmp_dlen; 1051b0104773SPascal Brand size_t req_dlen; 1052b0104773SPascal Brand 1053642a1607SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1054dea1f2b6SCedric Chaumont (srcData == NULL && srcLen != 0) || 1055dea1f2b6SCedric Chaumont destLen == NULL || 1056dea1f2b6SCedric Chaumont (destData == NULL && *destLen != 0)) { 1057dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1058dea1f2b6SCedric Chaumont goto out; 1059dea1f2b6SCedric Chaumont } 1060dea1f2b6SCedric Chaumont 1061642a1607SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 1062dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1063dea1f2b6SCedric Chaumont goto out; 1064dea1f2b6SCedric Chaumont } 1065dea1f2b6SCedric Chaumont 1066642a1607SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1067642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1068642a1607SCedric Chaumont goto out; 1069642a1607SCedric Chaumont } 1070642a1607SCedric Chaumont 1071642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1072dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1073dea1f2b6SCedric Chaumont goto out; 1074dea1f2b6SCedric Chaumont } 1075b0104773SPascal Brand 1076b0104773SPascal Brand /* 1077b0104773SPascal Brand * Check that the final block doesn't require padding for those 1078b0104773SPascal Brand * algorithms that requires client to supply padding. 1079b0104773SPascal Brand */ 1080642a1607SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD || 1081642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD || 1082642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD || 1083642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD || 1084642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || 1085642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD) { 1086642a1607SCedric Chaumont if (((operation->buffer_offs + srcLen) % operation->block_size) 1087642a1607SCedric Chaumont != 0) { 1088dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1089dea1f2b6SCedric Chaumont goto out; 1090dea1f2b6SCedric Chaumont } 1091b0104773SPascal Brand } 1092b0104773SPascal Brand 1093b0104773SPascal Brand /* 1094b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1095b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1096b0104773SPascal Brand * can't restore sync with this API. 1097b0104773SPascal Brand */ 109857aabac5SBogdan Liulko if (operation->block_size > 1) { 1099642a1607SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 110057aabac5SBogdan Liulko } else { 110157aabac5SBogdan Liulko req_dlen = srcLen; 110257aabac5SBogdan Liulko } 1103b0104773SPascal Brand if (*destLen < req_dlen) { 1104b0104773SPascal Brand *destLen = req_dlen; 1105dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1106dea1f2b6SCedric Chaumont goto out; 1107b0104773SPascal Brand } 1108b0104773SPascal Brand 1109b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 111057aabac5SBogdan Liulko if (operation->block_size > 1) { 111157aabac5SBogdan Liulko res = tee_buffer_update(operation, utee_cipher_update, 111257aabac5SBogdan Liulko srcData, srcLen, dst, &tmp_dlen); 1113dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS) 1114dea1f2b6SCedric Chaumont goto out; 1115dea1f2b6SCedric Chaumont 1116b0104773SPascal Brand dst += tmp_dlen; 1117b0104773SPascal Brand acc_dlen += tmp_dlen; 1118b0104773SPascal Brand 1119b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1120642a1607SCedric Chaumont res = utee_cipher_final(operation->state, operation->buffer, 1121642a1607SCedric Chaumont operation->buffer_offs, dst, &tmp_dlen); 112257aabac5SBogdan Liulko } else { 112357aabac5SBogdan Liulko res = utee_cipher_final(operation->state, srcData, 112457aabac5SBogdan Liulko srcLen, dst, &tmp_dlen); 112557aabac5SBogdan Liulko } 1126b0104773SPascal Brand if (res != TEE_SUCCESS) 1127dea1f2b6SCedric Chaumont goto out; 1128dea1f2b6SCedric Chaumont 1129b0104773SPascal Brand acc_dlen += tmp_dlen; 1130b0104773SPascal Brand *destLen = acc_dlen; 1131dea1f2b6SCedric Chaumont 1132642a1607SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1133642a1607SCedric Chaumont 1134642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1135642a1607SCedric Chaumont 1136dea1f2b6SCedric Chaumont out: 1137dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 1138dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1139b36311adSJerome Forissier TEE_Panic(res); 1140dea1f2b6SCedric Chaumont 1141dea1f2b6SCedric Chaumont return res; 1142b0104773SPascal Brand } 1143b0104773SPascal Brand 1144b0104773SPascal Brand /* Cryptographic Operations API - MAC Functions */ 1145b0104773SPascal Brand 11468f07fe6fSJerome Forissier void TEE_MACInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen) 1147b0104773SPascal Brand { 1148b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 1149b0104773SPascal Brand TEE_Panic(0); 1150642a1607SCedric Chaumont 1151b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_MAC) 1152b0104773SPascal Brand TEE_Panic(0); 1153642a1607SCedric Chaumont 1154642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 1155642a1607SCedric Chaumont !(operation->key1)) 1156642a1607SCedric Chaumont TEE_Panic(0); 1157642a1607SCedric Chaumont 1158642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 1159642a1607SCedric Chaumont TEE_ResetOperation(operation); 1160642a1607SCedric Chaumont 1161642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1162642a1607SCedric Chaumont 11636d15db08SJerome Forissier init_hash_operation(operation, IV, IVLen); 1164b0104773SPascal Brand } 1165b0104773SPascal Brand 11668f07fe6fSJerome Forissier void TEE_MACUpdate(TEE_OperationHandle operation, const void *chunk, 116728e0efc6SCedric Chaumont uint32_t chunkSize) 1168b0104773SPascal Brand { 1169b0104773SPascal Brand TEE_Result res; 1170b0104773SPascal Brand 117128e0efc6SCedric Chaumont if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0)) 1172b0104773SPascal Brand TEE_Panic(0); 1173642a1607SCedric Chaumont 117428e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) 1175b0104773SPascal Brand TEE_Panic(0); 1176642a1607SCedric Chaumont 117728e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1178b0104773SPascal Brand TEE_Panic(0); 1179b0104773SPascal Brand 1180642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) 1181642a1607SCedric Chaumont TEE_Panic(0); 1182642a1607SCedric Chaumont 118328e0efc6SCedric Chaumont res = utee_hash_update(operation->state, chunk, chunkSize); 1184b0104773SPascal Brand if (res != TEE_SUCCESS) 1185b0104773SPascal Brand TEE_Panic(res); 1186b0104773SPascal Brand } 1187b0104773SPascal Brand 118828e0efc6SCedric Chaumont TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation, 11898f07fe6fSJerome Forissier const void *message, uint32_t messageLen, 119079a3c601SCedric Chaumont void *mac, uint32_t *macLen) 1191b0104773SPascal Brand { 1192b0104773SPascal Brand TEE_Result res; 1193e86f1266SJens Wiklander uint64_t ml; 1194b0104773SPascal Brand 119528e0efc6SCedric Chaumont if (operation == TEE_HANDLE_NULL || 119628e0efc6SCedric Chaumont (message == NULL && messageLen != 0) || 119728e0efc6SCedric Chaumont mac == NULL || 119828e0efc6SCedric Chaumont macLen == NULL) { 119928e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 120028e0efc6SCedric Chaumont goto out; 120128e0efc6SCedric Chaumont } 1202b0104773SPascal Brand 120328e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 120428e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 120528e0efc6SCedric Chaumont goto out; 120628e0efc6SCedric Chaumont } 120728e0efc6SCedric Chaumont 120828e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 120928e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 121028e0efc6SCedric Chaumont goto out; 121128e0efc6SCedric Chaumont } 121228e0efc6SCedric Chaumont 1213642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1214642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1215642a1607SCedric Chaumont goto out; 1216642a1607SCedric Chaumont } 1217642a1607SCedric Chaumont 1218e86f1266SJens Wiklander ml = *macLen; 1219e86f1266SJens Wiklander res = utee_hash_final(operation->state, message, messageLen, mac, &ml); 1220e86f1266SJens Wiklander *macLen = ml; 122128e0efc6SCedric Chaumont if (res != TEE_SUCCESS) 122228e0efc6SCedric Chaumont goto out; 122328e0efc6SCedric Chaumont 122428e0efc6SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 122528e0efc6SCedric Chaumont 1226642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1227642a1607SCedric Chaumont 122828e0efc6SCedric Chaumont out: 122928e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 123028e0efc6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 123128e0efc6SCedric Chaumont TEE_Panic(res); 123228e0efc6SCedric Chaumont 1233b0104773SPascal Brand return res; 1234b0104773SPascal Brand } 1235b0104773SPascal Brand 1236b0104773SPascal Brand TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation, 12378f07fe6fSJerome Forissier const void *message, uint32_t messageLen, 12388f07fe6fSJerome Forissier const void *mac, uint32_t macLen) 1239b0104773SPascal Brand { 1240b0104773SPascal Brand TEE_Result res; 1241b0104773SPascal Brand uint8_t computed_mac[TEE_MAX_HASH_SIZE]; 12427f74c64aSPascal Brand uint32_t computed_mac_size = TEE_MAX_HASH_SIZE; 1243b0104773SPascal Brand 124428e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 124528e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 124628e0efc6SCedric Chaumont goto out; 124728e0efc6SCedric Chaumont } 124828e0efc6SCedric Chaumont 124928e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 125028e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 125128e0efc6SCedric Chaumont goto out; 125228e0efc6SCedric Chaumont } 125328e0efc6SCedric Chaumont 1254642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1255642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1256642a1607SCedric Chaumont goto out; 1257642a1607SCedric Chaumont } 1258642a1607SCedric Chaumont 1259b0104773SPascal Brand res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac, 1260b0104773SPascal Brand &computed_mac_size); 1261b0104773SPascal Brand if (res != TEE_SUCCESS) 126228e0efc6SCedric Chaumont goto out; 126328e0efc6SCedric Chaumont 126428e0efc6SCedric Chaumont if (computed_mac_size != macLen) { 126528e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 126628e0efc6SCedric Chaumont goto out; 126728e0efc6SCedric Chaumont } 126828e0efc6SCedric Chaumont 126928e0efc6SCedric Chaumont if (buf_compare_ct(mac, computed_mac, computed_mac_size) != 0) { 127028e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 127128e0efc6SCedric Chaumont goto out; 127228e0efc6SCedric Chaumont } 127328e0efc6SCedric Chaumont 1274642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1275642a1607SCedric Chaumont 127628e0efc6SCedric Chaumont out: 127728e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 127828e0efc6SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 127928e0efc6SCedric Chaumont TEE_Panic(res); 128028e0efc6SCedric Chaumont 1281b0104773SPascal Brand return res; 1282b0104773SPascal Brand } 1283b0104773SPascal Brand 1284b0104773SPascal Brand /* Cryptographic Operations API - Authenticated Encryption Functions */ 1285b0104773SPascal Brand 12868f07fe6fSJerome Forissier TEE_Result TEE_AEInit(TEE_OperationHandle operation, const void *nonce, 128779a3c601SCedric Chaumont uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen, 1288b0104773SPascal Brand uint32_t payloadLen) 1289b0104773SPascal Brand { 1290b0104773SPascal Brand TEE_Result res; 1291b0104773SPascal Brand 1292b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || nonce == NULL) { 1293b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1294b5816c88SCedric Chaumont goto out; 1295b5816c88SCedric Chaumont } 1296b5816c88SCedric Chaumont 1297b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1298b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1299b5816c88SCedric Chaumont goto out; 1300b5816c88SCedric Chaumont } 1301b0104773SPascal Brand 1302642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 1303642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1304642a1607SCedric Chaumont goto out; 1305642a1607SCedric Chaumont } 1306642a1607SCedric Chaumont 1307b0104773SPascal Brand /* 1308b0104773SPascal Brand * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core 1309b0104773SPascal Brand * in the implementation. But AES-GCM spec doesn't specify the tag len 1310b0104773SPascal Brand * according to the same principle so we have to check here instead to 1311b0104773SPascal Brand * be GP compliant. 1312b0104773SPascal Brand */ 1313b5816c88SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_GCM) { 1314b0104773SPascal Brand /* 1315b0104773SPascal Brand * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96 1316b0104773SPascal Brand */ 1317b5816c88SCedric Chaumont if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) { 1318b5816c88SCedric Chaumont res = TEE_ERROR_NOT_SUPPORTED; 1319b5816c88SCedric Chaumont goto out; 1320b5816c88SCedric Chaumont } 1321b0104773SPascal Brand } 1322b0104773SPascal Brand 1323b5816c88SCedric Chaumont res = utee_authenc_init(operation->state, nonce, nonceLen, 1324b5816c88SCedric Chaumont tagLen / 8, AADLen, payloadLen); 1325b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1326b5816c88SCedric Chaumont goto out; 1327b5816c88SCedric Chaumont 1328b5816c88SCedric Chaumont operation->ae_tag_len = tagLen / 8; 1329b5816c88SCedric Chaumont operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 1330b5816c88SCedric Chaumont 1331b5816c88SCedric Chaumont out: 1332b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1333b5816c88SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 1334b0104773SPascal Brand TEE_Panic(res); 1335b5816c88SCedric Chaumont 1336b0104773SPascal Brand return res; 1337b0104773SPascal Brand } 1338b0104773SPascal Brand 13398f07fe6fSJerome Forissier void TEE_AEUpdateAAD(TEE_OperationHandle operation, const void *AADdata, 134079a3c601SCedric Chaumont uint32_t AADdataLen) 1341b0104773SPascal Brand { 1342b0104773SPascal Brand TEE_Result res; 1343b0104773SPascal Brand 1344b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1345b5816c88SCedric Chaumont (AADdata == NULL && AADdataLen != 0)) 1346b0104773SPascal Brand TEE_Panic(0); 1347642a1607SCedric Chaumont 1348b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) 1349b0104773SPascal Brand TEE_Panic(0); 1350642a1607SCedric Chaumont 1351b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1352b0104773SPascal Brand TEE_Panic(0); 1353b0104773SPascal Brand 1354b5816c88SCedric Chaumont res = utee_authenc_update_aad(operation->state, AADdata, AADdataLen); 1355642a1607SCedric Chaumont 1356642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1357642a1607SCedric Chaumont 1358b0104773SPascal Brand if (res != TEE_SUCCESS) 1359b0104773SPascal Brand TEE_Panic(res); 1360b0104773SPascal Brand } 1361b0104773SPascal Brand 13628f07fe6fSJerome Forissier TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, const void *srcData, 136379a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 1364b0104773SPascal Brand { 1365b5816c88SCedric Chaumont TEE_Result res; 1366b0104773SPascal Brand size_t req_dlen; 1367e86f1266SJens Wiklander uint64_t dl; 1368b0104773SPascal Brand 1369b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1370b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1371b5816c88SCedric Chaumont destLen == NULL || 1372b5816c88SCedric Chaumont (destData == NULL && *destLen != 0)) { 1373b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1374b5816c88SCedric Chaumont goto out; 1375b5816c88SCedric Chaumont } 1376b5816c88SCedric Chaumont 1377b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1378b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1379b5816c88SCedric Chaumont goto out; 1380b5816c88SCedric Chaumont } 1381b5816c88SCedric Chaumont 1382b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1383b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1384b5816c88SCedric Chaumont goto out; 1385b5816c88SCedric Chaumont } 1386b0104773SPascal Brand 1387827308b8SJerome Forissier if (!srcData && !srcLen) { 1388090268f5SJens Wiklander *destLen = 0; 1389827308b8SJerome Forissier res = TEE_SUCCESS; 1390827308b8SJerome Forissier goto out; 1391827308b8SJerome Forissier } 1392827308b8SJerome Forissier 1393b0104773SPascal Brand /* 1394b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1395b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1396b0104773SPascal Brand * can't restore sync with this API. 1397b0104773SPascal Brand */ 1398afc0c182SBogdan Liulko if (operation->block_size > 1) { 1399b5816c88SCedric Chaumont req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen, 1400b5816c88SCedric Chaumont operation->block_size); 1401afc0c182SBogdan Liulko } else { 1402afc0c182SBogdan Liulko req_dlen = srcLen; 1403afc0c182SBogdan Liulko } 1404afc0c182SBogdan Liulko 1405b0104773SPascal Brand if (*destLen < req_dlen) { 1406b0104773SPascal Brand *destLen = req_dlen; 1407b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1408b5816c88SCedric Chaumont goto out; 1409b0104773SPascal Brand } 1410b0104773SPascal Brand 1411e86f1266SJens Wiklander dl = *destLen; 1412afc0c182SBogdan Liulko if (operation->block_size > 1) { 1413afc0c182SBogdan Liulko res = tee_buffer_update(operation, utee_authenc_update_payload, 1414afc0c182SBogdan Liulko srcData, srcLen, destData, &dl); 1415afc0c182SBogdan Liulko } else { 1416afc0c182SBogdan Liulko if (srcLen > 0) { 1417afc0c182SBogdan Liulko res = utee_authenc_update_payload(operation->state, 1418afc0c182SBogdan Liulko srcData, srcLen, 1419afc0c182SBogdan Liulko destData, &dl); 1420afc0c182SBogdan Liulko } else { 1421afc0c182SBogdan Liulko dl = 0; 1422afc0c182SBogdan Liulko res = TEE_SUCCESS; 1423afc0c182SBogdan Liulko } 1424afc0c182SBogdan Liulko } 1425afc0c182SBogdan Liulko if (res != TEE_SUCCESS) 1426afc0c182SBogdan Liulko goto out; 1427afc0c182SBogdan Liulko 1428e86f1266SJens Wiklander *destLen = dl; 1429b0104773SPascal Brand 1430642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1431642a1607SCedric Chaumont 1432b5816c88SCedric Chaumont out: 1433b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1434b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1435b5816c88SCedric Chaumont TEE_Panic(res); 1436b5816c88SCedric Chaumont 1437b5816c88SCedric Chaumont return res; 1438b0104773SPascal Brand } 1439b0104773SPascal Brand 1440b5816c88SCedric Chaumont TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation, 14418f07fe6fSJerome Forissier const void *srcData, uint32_t srcLen, 144279a3c601SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 144379a3c601SCedric Chaumont uint32_t *tagLen) 1444b0104773SPascal Brand { 1445b0104773SPascal Brand TEE_Result res; 1446b0104773SPascal Brand uint8_t *dst = destData; 1447b0104773SPascal Brand size_t acc_dlen = 0; 1448e86f1266SJens Wiklander uint64_t tmp_dlen; 1449b0104773SPascal Brand size_t req_dlen; 1450e86f1266SJens Wiklander uint64_t tl; 1451b0104773SPascal Brand 1452b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1453b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1454b5816c88SCedric Chaumont destLen == NULL || 1455b5816c88SCedric Chaumont (destData == NULL && *destLen != 0) || 1456b5816c88SCedric Chaumont tag == NULL || tagLen == NULL) { 1457b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1458b5816c88SCedric Chaumont goto out; 1459b5816c88SCedric Chaumont } 1460b5816c88SCedric Chaumont 1461b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1462b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1463b5816c88SCedric Chaumont goto out; 1464b5816c88SCedric Chaumont } 1465b5816c88SCedric Chaumont 1466b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1467b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1468b5816c88SCedric Chaumont goto out; 1469b5816c88SCedric Chaumont } 1470b0104773SPascal Brand 1471b0104773SPascal Brand /* 1472b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1473b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1474b0104773SPascal Brand * can't restore sync with this API. 1475*2733280aSEtienne Carriere * 1476*2733280aSEtienne Carriere * Need to check this before update_payload since sync would be lost if 1477*2733280aSEtienne Carriere * we return short buffer after that. 1478b0104773SPascal Brand */ 1479*2733280aSEtienne Carriere res = TEE_ERROR_GENERIC; 1480*2733280aSEtienne Carriere 1481b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1482b0104773SPascal Brand if (*destLen < req_dlen) { 1483b0104773SPascal Brand *destLen = req_dlen; 1484b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1485b0104773SPascal Brand } 1486b0104773SPascal Brand 1487b5816c88SCedric Chaumont if (*tagLen < operation->ae_tag_len) { 1488b5816c88SCedric Chaumont *tagLen = operation->ae_tag_len; 1489b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1490b0104773SPascal Brand } 1491b0104773SPascal Brand 1492*2733280aSEtienne Carriere if (res == TEE_ERROR_SHORT_BUFFER) 1493*2733280aSEtienne Carriere goto out; 1494*2733280aSEtienne Carriere 1495afc0c182SBogdan Liulko tl = *tagLen; 1496b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1497afc0c182SBogdan Liulko if (operation->block_size > 1) { 1498afc0c182SBogdan Liulko res = tee_buffer_update(operation, utee_authenc_update_payload, 1499afc0c182SBogdan Liulko srcData, srcLen, dst, &tmp_dlen); 1500b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1501b5816c88SCedric Chaumont goto out; 1502b5816c88SCedric Chaumont 1503b0104773SPascal Brand dst += tmp_dlen; 1504b0104773SPascal Brand acc_dlen += tmp_dlen; 1505b0104773SPascal Brand 1506b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1507afc0c182SBogdan Liulko res = utee_authenc_enc_final(operation->state, 1508afc0c182SBogdan Liulko operation->buffer, 1509afc0c182SBogdan Liulko operation->buffer_offs, dst, 1510afc0c182SBogdan Liulko &tmp_dlen, tag, &tl); 1511afc0c182SBogdan Liulko } else { 1512afc0c182SBogdan Liulko res = utee_authenc_enc_final(operation->state, srcData, 1513afc0c182SBogdan Liulko srcLen, dst, &tmp_dlen, 1514e86f1266SJens Wiklander tag, &tl); 1515afc0c182SBogdan Liulko } 1516e86f1266SJens Wiklander *tagLen = tl; 1517b0104773SPascal Brand if (res != TEE_SUCCESS) 1518b5816c88SCedric Chaumont goto out; 1519b0104773SPascal Brand 1520b5816c88SCedric Chaumont acc_dlen += tmp_dlen; 1521b0104773SPascal Brand *destLen = acc_dlen; 1522642a1607SCedric Chaumont 1523b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1524b5816c88SCedric Chaumont 1525642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1526642a1607SCedric Chaumont 1527b5816c88SCedric Chaumont out: 1528b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1529b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1530b5816c88SCedric Chaumont TEE_Panic(res); 1531b0104773SPascal Brand 1532b0104773SPascal Brand return res; 1533b0104773SPascal Brand } 1534b0104773SPascal Brand 1535b5816c88SCedric Chaumont TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation, 15368f07fe6fSJerome Forissier const void *srcData, uint32_t srcLen, 1537b5816c88SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 153879a3c601SCedric Chaumont uint32_t tagLen) 1539b0104773SPascal Brand { 1540b0104773SPascal Brand TEE_Result res; 1541b0104773SPascal Brand uint8_t *dst = destData; 1542b0104773SPascal Brand size_t acc_dlen = 0; 1543e86f1266SJens Wiklander uint64_t tmp_dlen; 1544b0104773SPascal Brand size_t req_dlen; 1545b0104773SPascal Brand 1546b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1547b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1548b5816c88SCedric Chaumont destLen == NULL || 1549b5816c88SCedric Chaumont (destData == NULL && *destLen != 0) || 1550b5816c88SCedric Chaumont (tag == NULL && tagLen != 0)) { 1551b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1552b5816c88SCedric Chaumont goto out; 1553b5816c88SCedric Chaumont } 1554b5816c88SCedric Chaumont 1555b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1556b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1557b5816c88SCedric Chaumont goto out; 1558b5816c88SCedric Chaumont } 1559b5816c88SCedric Chaumont 1560b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1561b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1562b5816c88SCedric Chaumont goto out; 1563b5816c88SCedric Chaumont } 1564b0104773SPascal Brand 1565b0104773SPascal Brand /* 1566b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1567b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1568b0104773SPascal Brand * can't restore sync with this API. 1569b0104773SPascal Brand */ 1570b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1571b0104773SPascal Brand if (*destLen < req_dlen) { 1572b0104773SPascal Brand *destLen = req_dlen; 1573b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1574b5816c88SCedric Chaumont goto out; 1575b0104773SPascal Brand } 1576b0104773SPascal Brand 1577b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1578afc0c182SBogdan Liulko if (operation->block_size > 1) { 1579afc0c182SBogdan Liulko res = tee_buffer_update(operation, utee_authenc_update_payload, 1580afc0c182SBogdan Liulko srcData, srcLen, dst, &tmp_dlen); 1581b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1582b5816c88SCedric Chaumont goto out; 1583b5816c88SCedric Chaumont 1584b0104773SPascal Brand dst += tmp_dlen; 1585b0104773SPascal Brand acc_dlen += tmp_dlen; 1586b0104773SPascal Brand 1587b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1588afc0c182SBogdan Liulko res = utee_authenc_dec_final(operation->state, 1589afc0c182SBogdan Liulko operation->buffer, 1590afc0c182SBogdan Liulko operation->buffer_offs, dst, 1591afc0c182SBogdan Liulko &tmp_dlen, tag, tagLen); 1592afc0c182SBogdan Liulko } else { 1593afc0c182SBogdan Liulko res = utee_authenc_dec_final(operation->state, srcData, 1594afc0c182SBogdan Liulko srcLen, dst, &tmp_dlen, 1595b5816c88SCedric Chaumont tag, tagLen); 1596afc0c182SBogdan Liulko } 1597b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1598b5816c88SCedric Chaumont goto out; 1599b5816c88SCedric Chaumont 1600b0104773SPascal Brand /* Supplied tagLen should match what we initiated with */ 1601b5816c88SCedric Chaumont if (tagLen != operation->ae_tag_len) 1602b0104773SPascal Brand res = TEE_ERROR_MAC_INVALID; 1603b0104773SPascal Brand 1604b0104773SPascal Brand acc_dlen += tmp_dlen; 1605b0104773SPascal Brand *destLen = acc_dlen; 1606642a1607SCedric Chaumont 1607b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1608b5816c88SCedric Chaumont 1609642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1610642a1607SCedric Chaumont 1611b5816c88SCedric Chaumont out: 1612b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1613b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER && 1614b5816c88SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 1615b5816c88SCedric Chaumont TEE_Panic(res); 1616b0104773SPascal Brand 1617b0104773SPascal Brand return res; 1618b0104773SPascal Brand } 1619b0104773SPascal Brand 1620b0104773SPascal Brand /* Cryptographic Operations API - Asymmetric Functions */ 1621b0104773SPascal Brand 162212e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation, 16238f07fe6fSJerome Forissier const TEE_Attribute *params, 16248f07fe6fSJerome Forissier uint32_t paramCount, const void *srcData, 162579a3c601SCedric Chaumont uint32_t srcLen, void *destData, 162679a3c601SCedric Chaumont uint32_t *destLen) 1627b0104773SPascal Brand { 1628b0104773SPascal Brand TEE_Result res; 1629e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1630e86f1266SJens Wiklander uint64_t dl; 1631b0104773SPascal Brand 163212e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1633b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 1634b0104773SPascal Brand TEE_Panic(0); 163512e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1636b0104773SPascal Brand TEE_Panic(0); 163712e66b6fSCedric Chaumont if (!operation->key1) 1638b0104773SPascal Brand TEE_Panic(0); 163912e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 164012e66b6fSCedric Chaumont TEE_Panic(0); 164112e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_ENCRYPT) 1642b0104773SPascal Brand TEE_Panic(0); 1643b0104773SPascal Brand 1644e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1645e86f1266SJens Wiklander dl = *destLen; 1646e86f1266SJens Wiklander res = utee_asymm_operate(operation->state, ua, paramCount, srcData, 1647e86f1266SJens Wiklander srcLen, destData, &dl); 1648e86f1266SJens Wiklander *destLen = dl; 164912e66b6fSCedric Chaumont 16508844ebfcSPascal Brand if (res != TEE_SUCCESS && 16518844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 16528844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1653b0104773SPascal Brand TEE_Panic(res); 165412e66b6fSCedric Chaumont 1655b0104773SPascal Brand return res; 1656b0104773SPascal Brand } 1657b0104773SPascal Brand 165812e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation, 16598f07fe6fSJerome Forissier const TEE_Attribute *params, 16608f07fe6fSJerome Forissier uint32_t paramCount, const void *srcData, 166179a3c601SCedric Chaumont uint32_t srcLen, void *destData, 166279a3c601SCedric Chaumont uint32_t *destLen) 1663b0104773SPascal Brand { 1664b0104773SPascal Brand TEE_Result res; 1665e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1666e86f1266SJens Wiklander uint64_t dl; 1667b0104773SPascal Brand 166812e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1669b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 1670b0104773SPascal Brand TEE_Panic(0); 167112e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1672b0104773SPascal Brand TEE_Panic(0); 167312e66b6fSCedric Chaumont if (!operation->key1) 1674b0104773SPascal Brand TEE_Panic(0); 167512e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 167612e66b6fSCedric Chaumont TEE_Panic(0); 167712e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_DECRYPT) 1678b0104773SPascal Brand TEE_Panic(0); 1679b0104773SPascal Brand 1680e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1681e86f1266SJens Wiklander dl = *destLen; 1682e86f1266SJens Wiklander res = utee_asymm_operate(operation->state, ua, paramCount, srcData, 1683e86f1266SJens Wiklander srcLen, destData, &dl); 1684e86f1266SJens Wiklander *destLen = dl; 168512e66b6fSCedric Chaumont 16868844ebfcSPascal Brand if (res != TEE_SUCCESS && 16878844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 16888844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1689b0104773SPascal Brand TEE_Panic(res); 169012e66b6fSCedric Chaumont 1691b0104773SPascal Brand return res; 1692b0104773SPascal Brand } 1693b0104773SPascal Brand 169412e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation, 16958f07fe6fSJerome Forissier const TEE_Attribute *params, 16968f07fe6fSJerome Forissier uint32_t paramCount, const void *digest, 169779a3c601SCedric Chaumont uint32_t digestLen, void *signature, 169879a3c601SCedric Chaumont uint32_t *signatureLen) 1699b0104773SPascal Brand { 1700b0104773SPascal Brand TEE_Result res; 1701e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1702e86f1266SJens Wiklander uint64_t sl; 1703b0104773SPascal Brand 170412e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || 170512e66b6fSCedric Chaumont (digest == NULL && digestLen != 0) || 1706b0104773SPascal Brand signature == NULL || signatureLen == NULL) 1707b0104773SPascal Brand TEE_Panic(0); 170812e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1709b0104773SPascal Brand TEE_Panic(0); 171012e66b6fSCedric Chaumont if (!operation->key1) 1711b0104773SPascal Brand TEE_Panic(0); 171212e66b6fSCedric Chaumont if (operation->info.operationClass != 171312e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 171412e66b6fSCedric Chaumont TEE_Panic(0); 171512e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_SIGN) 1716b0104773SPascal Brand TEE_Panic(0); 1717b0104773SPascal Brand 1718e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1719e86f1266SJens Wiklander sl = *signatureLen; 1720e86f1266SJens Wiklander res = utee_asymm_operate(operation->state, ua, paramCount, digest, 1721e86f1266SJens Wiklander digestLen, signature, &sl); 1722e86f1266SJens Wiklander *signatureLen = sl; 172312e66b6fSCedric Chaumont 1724b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER) 1725b0104773SPascal Brand TEE_Panic(res); 172612e66b6fSCedric Chaumont 1727b0104773SPascal Brand return res; 1728b0104773SPascal Brand } 1729b0104773SPascal Brand 173012e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation, 17318f07fe6fSJerome Forissier const TEE_Attribute *params, 17328f07fe6fSJerome Forissier uint32_t paramCount, const void *digest, 17338f07fe6fSJerome Forissier uint32_t digestLen, 17348f07fe6fSJerome Forissier const void *signature, 173579a3c601SCedric Chaumont uint32_t signatureLen) 1736b0104773SPascal Brand { 1737b0104773SPascal Brand TEE_Result res; 1738e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1739b0104773SPascal Brand 174012e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || 174112e66b6fSCedric Chaumont (digest == NULL && digestLen != 0) || 1742b0104773SPascal Brand (signature == NULL && signatureLen != 0)) 1743b0104773SPascal Brand TEE_Panic(0); 174412e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1745b0104773SPascal Brand TEE_Panic(0); 174612e66b6fSCedric Chaumont if (!operation->key1) 1747b0104773SPascal Brand TEE_Panic(0); 174812e66b6fSCedric Chaumont if (operation->info.operationClass != 174912e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 175012e66b6fSCedric Chaumont TEE_Panic(0); 175112e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_VERIFY) 1752b0104773SPascal Brand TEE_Panic(0); 1753b0104773SPascal Brand 1754e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1755e86f1266SJens Wiklander res = utee_asymm_verify(operation->state, ua, paramCount, digest, 175612e66b6fSCedric Chaumont digestLen, signature, signatureLen); 175712e66b6fSCedric Chaumont 1758b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID) 1759b0104773SPascal Brand TEE_Panic(res); 176012e66b6fSCedric Chaumont 1761b0104773SPascal Brand return res; 1762b0104773SPascal Brand } 1763b0104773SPascal Brand 1764b0104773SPascal Brand /* Cryptographic Operations API - Key Derivation Functions */ 1765b0104773SPascal Brand 1766b0104773SPascal Brand void TEE_DeriveKey(TEE_OperationHandle operation, 1767b0104773SPascal Brand const TEE_Attribute *params, uint32_t paramCount, 1768b0104773SPascal Brand TEE_ObjectHandle derivedKey) 1769b0104773SPascal Brand { 1770b0104773SPascal Brand TEE_Result res; 1771b0104773SPascal Brand TEE_ObjectInfo key_info; 1772e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1773b0104773SPascal Brand 1774b0104773SPascal Brand if (operation == TEE_HANDLE_NULL || derivedKey == 0) 1775b0104773SPascal Brand TEE_Panic(0); 177684fa9467SCedric Chaumont if (params == NULL && paramCount != 0) 1777b0104773SPascal Brand TEE_Panic(0); 17788854d3c6SJerome Forissier if (TEE_ALG_GET_CLASS(operation->info.algorithm) != 17798854d3c6SJerome Forissier TEE_OPERATION_KEY_DERIVATION) 1780b0104773SPascal Brand TEE_Panic(0); 1781b0104773SPascal Brand 1782b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION) 1783b0104773SPascal Brand TEE_Panic(0); 178484fa9467SCedric Chaumont if (!operation->key1) 178584fa9467SCedric Chaumont TEE_Panic(0); 1786b0104773SPascal Brand if (operation->info.mode != TEE_MODE_DERIVE) 1787b0104773SPascal Brand TEE_Panic(0); 1788b0104773SPascal Brand if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0) 1789b0104773SPascal Brand TEE_Panic(0); 1790b0104773SPascal Brand 1791e86f1266SJens Wiklander res = utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info); 1792b0104773SPascal Brand if (res != TEE_SUCCESS) 1793b36311adSJerome Forissier TEE_Panic(res); 1794b0104773SPascal Brand 1795b0104773SPascal Brand if (key_info.objectType != TEE_TYPE_GENERIC_SECRET) 1796b0104773SPascal Brand TEE_Panic(0); 1797b0104773SPascal Brand if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1798b0104773SPascal Brand TEE_Panic(0); 1799b0104773SPascal Brand 1800e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1801e86f1266SJens Wiklander res = utee_cryp_derive_key(operation->state, ua, paramCount, 1802e86f1266SJens Wiklander (unsigned long)derivedKey); 1803b0104773SPascal Brand if (res != TEE_SUCCESS) 1804b0104773SPascal Brand TEE_Panic(res); 1805b0104773SPascal Brand } 1806b0104773SPascal Brand 1807b0104773SPascal Brand /* Cryptographic Operations API - Random Number Generation Functions */ 1808b0104773SPascal Brand 180979a3c601SCedric Chaumont void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen) 1810b0104773SPascal Brand { 1811b0104773SPascal Brand TEE_Result res; 1812b0104773SPascal Brand 1813b0104773SPascal Brand res = utee_cryp_random_number_generate(randomBuffer, randomBufferLen); 1814b0104773SPascal Brand if (res != TEE_SUCCESS) 1815b0104773SPascal Brand TEE_Panic(res); 1816b0104773SPascal Brand } 1817