1b0104773SPascal Brand /* 2b0104773SPascal Brand * Copyright (c) 2014, STMicroelectronics International N.V. 3b0104773SPascal Brand * All rights reserved. 4b0104773SPascal Brand * 5b0104773SPascal Brand * Redistribution and use in source and binary forms, with or without 6b0104773SPascal Brand * modification, are permitted provided that the following conditions are met: 7b0104773SPascal Brand * 8b0104773SPascal Brand * 1. Redistributions of source code must retain the above copyright notice, 9b0104773SPascal Brand * this list of conditions and the following disclaimer. 10b0104773SPascal Brand * 11b0104773SPascal Brand * 2. Redistributions in binary form must reproduce the above copyright notice, 12b0104773SPascal Brand * this list of conditions and the following disclaimer in the documentation 13b0104773SPascal Brand * and/or other materials provided with the distribution. 14b0104773SPascal Brand * 15b0104773SPascal Brand * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16b0104773SPascal Brand * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17b0104773SPascal Brand * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18b0104773SPascal Brand * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19b0104773SPascal Brand * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20b0104773SPascal Brand * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21b0104773SPascal Brand * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22b0104773SPascal Brand * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23b0104773SPascal Brand * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24b0104773SPascal Brand * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25b0104773SPascal Brand * POSSIBILITY OF SUCH DAMAGE. 26b0104773SPascal Brand */ 27b0104773SPascal Brand #include <stdlib.h> 28b0104773SPascal Brand #include <string.h> 29b796ebf3SJerome Forissier #include <string_ext.h> 30b0104773SPascal Brand 31b0104773SPascal Brand #include <tee_api.h> 328854d3c6SJerome Forissier #include <tee_api_defines_extensions.h> 33b0104773SPascal Brand #include <tee_internal_api_extensions.h> 34b0104773SPascal Brand #include <utee_syscalls.h> 35b0104773SPascal Brand #include <utee_defines.h> 36fc26c92aSJens Wiklander #include <util.h> 37e86f1266SJens Wiklander #include "tee_api_private.h" 38b0104773SPascal Brand 39b0104773SPascal Brand struct __TEE_OperationHandle { 40b0104773SPascal Brand TEE_OperationInfo info; 41b0104773SPascal Brand TEE_ObjectHandle key1; 42b0104773SPascal Brand TEE_ObjectHandle key2; 43642a1607SCedric Chaumont uint32_t operationState;/* Operation state : INITIAL or ACTIVE */ 44b0104773SPascal Brand uint8_t *buffer; /* buffer to collect complete blocks */ 45b0104773SPascal Brand bool buffer_two_blocks; /* True if two blocks need to be buffered */ 46b0104773SPascal Brand size_t block_size; /* Block size of cipher */ 47b0104773SPascal Brand size_t buffer_offs; /* Offset in buffer */ 48b0104773SPascal Brand uint32_t state; /* Handle to state in TEE Core */ 49b0104773SPascal Brand uint32_t ae_tag_len; /* 50b0104773SPascal Brand * tag_len in bytes for AE operation else unused 51b0104773SPascal Brand */ 52b0104773SPascal Brand }; 53b0104773SPascal Brand 54b0104773SPascal Brand /* Cryptographic Operations API - Generic Operation Functions */ 55b0104773SPascal Brand 56b0104773SPascal Brand TEE_Result TEE_AllocateOperation(TEE_OperationHandle *operation, 57b0104773SPascal Brand uint32_t algorithm, uint32_t mode, 58b0104773SPascal Brand uint32_t maxKeySize) 59b0104773SPascal Brand { 60b0104773SPascal Brand TEE_Result res; 61b0104773SPascal Brand TEE_OperationHandle op = TEE_HANDLE_NULL; 62b0104773SPascal Brand uint32_t handle_state = 0; 63b0104773SPascal Brand size_t block_size = 1; 64b0104773SPascal Brand uint32_t req_key_usage; 65b0104773SPascal Brand bool with_private_key = false; 66b0104773SPascal Brand bool buffer_two_blocks = false; 67b0104773SPascal Brand 689b52c538SCedric Chaumont if (!operation) 69b0104773SPascal Brand TEE_Panic(0); 70b0104773SPascal Brand 71b0104773SPascal Brand if (algorithm == TEE_ALG_AES_XTS) 72b0104773SPascal Brand handle_state = TEE_HANDLE_FLAG_EXPECT_TWO_KEYS; 73b0104773SPascal Brand 74218d9055SCedric Chaumont /* Check algorithm max key size */ 75218d9055SCedric Chaumont switch (algorithm) { 76218d9055SCedric Chaumont case TEE_ALG_DSA_SHA1: 77218d9055SCedric Chaumont if (maxKeySize < 512) 78218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 79218d9055SCedric Chaumont if (maxKeySize > 1024) 80218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 81218d9055SCedric Chaumont if (maxKeySize % 64 != 0) 82218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 83218d9055SCedric Chaumont break; 84218d9055SCedric Chaumont 85218d9055SCedric Chaumont case TEE_ALG_DSA_SHA224: 86218d9055SCedric Chaumont if (maxKeySize != 2048) 87218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 88218d9055SCedric Chaumont break; 89218d9055SCedric Chaumont 90218d9055SCedric Chaumont case TEE_ALG_DSA_SHA256: 91218d9055SCedric Chaumont if (maxKeySize != 2048 && maxKeySize != 3072) 92218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 93218d9055SCedric Chaumont break; 94218d9055SCedric Chaumont 951220586eSCedric Chaumont case TEE_ALG_ECDSA_P192: 961220586eSCedric Chaumont case TEE_ALG_ECDH_P192: 971220586eSCedric Chaumont if (maxKeySize != 192) 981220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 991220586eSCedric Chaumont break; 1001220586eSCedric Chaumont 1011220586eSCedric Chaumont case TEE_ALG_ECDSA_P224: 1021220586eSCedric Chaumont case TEE_ALG_ECDH_P224: 1031220586eSCedric Chaumont if (maxKeySize != 224) 1041220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 1051220586eSCedric Chaumont break; 1061220586eSCedric Chaumont 1071220586eSCedric Chaumont case TEE_ALG_ECDSA_P256: 1081220586eSCedric Chaumont case TEE_ALG_ECDH_P256: 1091220586eSCedric Chaumont if (maxKeySize != 256) 1101220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 1111220586eSCedric Chaumont break; 1121220586eSCedric Chaumont 1131220586eSCedric Chaumont case TEE_ALG_ECDSA_P384: 1141220586eSCedric Chaumont case TEE_ALG_ECDH_P384: 1151220586eSCedric Chaumont if (maxKeySize != 384) 1161220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 1171220586eSCedric Chaumont break; 1181220586eSCedric Chaumont 1191220586eSCedric Chaumont case TEE_ALG_ECDSA_P521: 1201220586eSCedric Chaumont case TEE_ALG_ECDH_P521: 1211220586eSCedric Chaumont if (maxKeySize != 521) 1221220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 1231220586eSCedric Chaumont break; 1241220586eSCedric Chaumont 125218d9055SCedric Chaumont default: 126218d9055SCedric Chaumont break; 127218d9055SCedric Chaumont } 128218d9055SCedric Chaumont 129218d9055SCedric Chaumont /* Check algorithm mode */ 130b0104773SPascal Brand switch (algorithm) { 131b0104773SPascal Brand case TEE_ALG_AES_CTS: 132b0104773SPascal Brand case TEE_ALG_AES_XTS: 133b0104773SPascal Brand buffer_two_blocks = true; 1344bd53c54SJerome Forissier /* FALLTHROUGH */ 1354bd53c54SJerome Forissier case TEE_ALG_AES_ECB_NOPAD: 136b0104773SPascal Brand case TEE_ALG_AES_CBC_NOPAD: 137b0104773SPascal Brand case TEE_ALG_AES_CTR: 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; 147*afc0c182SBogdan Liulko /* FALLTHROUGH */ 148*afc0c182SBogdan Liulko case TEE_ALG_AES_GCM: 149b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) 150b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT; 151b0104773SPascal Brand else if (mode == TEE_MODE_DECRYPT) 152b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT; 153b0104773SPascal Brand else 154b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 155b0104773SPascal Brand break; 156b0104773SPascal Brand 157b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_MD5: 158b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1: 159b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224: 160b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256: 161b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384: 162b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512: 163b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1: 164b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224: 165b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256: 166b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384: 167b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512: 168b0104773SPascal Brand case TEE_ALG_DSA_SHA1: 169218d9055SCedric Chaumont case TEE_ALG_DSA_SHA224: 170218d9055SCedric Chaumont case TEE_ALG_DSA_SHA256: 1711220586eSCedric Chaumont case TEE_ALG_ECDSA_P192: 1721220586eSCedric Chaumont case TEE_ALG_ECDSA_P224: 1731220586eSCedric Chaumont case TEE_ALG_ECDSA_P256: 1741220586eSCedric Chaumont case TEE_ALG_ECDSA_P384: 1751220586eSCedric Chaumont case TEE_ALG_ECDSA_P521: 176b0104773SPascal Brand if (mode == TEE_MODE_SIGN) { 177b0104773SPascal Brand with_private_key = true; 178b0104773SPascal Brand req_key_usage = TEE_USAGE_SIGN; 179b0104773SPascal Brand } else if (mode == TEE_MODE_VERIFY) { 180b0104773SPascal Brand req_key_usage = TEE_USAGE_VERIFY; 181b0104773SPascal Brand } else { 182b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 183b0104773SPascal Brand } 184b0104773SPascal Brand break; 185b0104773SPascal Brand 186b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_V1_5: 187b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1: 188b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224: 189b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256: 190b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384: 191b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512: 192b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) { 193b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT; 194b0104773SPascal Brand } else if (mode == TEE_MODE_DECRYPT) { 195b0104773SPascal Brand with_private_key = true; 196b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT; 197b0104773SPascal Brand } else { 198b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 199b0104773SPascal Brand } 200b0104773SPascal Brand break; 201b0104773SPascal Brand 202b0104773SPascal Brand case TEE_ALG_RSA_NOPAD: 203b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) { 204b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY; 205b0104773SPascal Brand } else if (mode == TEE_MODE_DECRYPT) { 206b0104773SPascal Brand with_private_key = true; 207b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT | TEE_USAGE_SIGN; 208b0104773SPascal Brand } else { 209b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 210b0104773SPascal Brand } 211b0104773SPascal Brand break; 212b0104773SPascal Brand 213b0104773SPascal Brand case TEE_ALG_DH_DERIVE_SHARED_SECRET: 2141220586eSCedric Chaumont case TEE_ALG_ECDH_P192: 2151220586eSCedric Chaumont case TEE_ALG_ECDH_P224: 2161220586eSCedric Chaumont case TEE_ALG_ECDH_P256: 2171220586eSCedric Chaumont case TEE_ALG_ECDH_P384: 2181220586eSCedric Chaumont case TEE_ALG_ECDH_P521: 219cdb198a7SJerome Forissier case TEE_ALG_HKDF_MD5_DERIVE_KEY: 220cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA1_DERIVE_KEY: 221cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA224_DERIVE_KEY: 222cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA256_DERIVE_KEY: 223cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA384_DERIVE_KEY: 224cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA512_DERIVE_KEY: 2258854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA1_DERIVE_KEY: 2268854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA224_DERIVE_KEY: 2278854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA256_DERIVE_KEY: 2288854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA384_DERIVE_KEY: 2298854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA512_DERIVE_KEY: 2300f2293b7SJerome Forissier case TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY: 231b0104773SPascal Brand if (mode != TEE_MODE_DERIVE) 232b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 233b0104773SPascal Brand with_private_key = true; 234b0104773SPascal Brand req_key_usage = TEE_USAGE_DERIVE; 235b0104773SPascal Brand break; 236b0104773SPascal Brand 237b0104773SPascal Brand case TEE_ALG_MD5: 238b0104773SPascal Brand case TEE_ALG_SHA1: 239b0104773SPascal Brand case TEE_ALG_SHA224: 240b0104773SPascal Brand case TEE_ALG_SHA256: 241b0104773SPascal Brand case TEE_ALG_SHA384: 242b0104773SPascal Brand case TEE_ALG_SHA512: 243b0104773SPascal Brand if (mode != TEE_MODE_DIGEST) 244b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 24505304565SCedric Chaumont /* v1.1: flags always set for digest operations */ 246b0104773SPascal Brand handle_state |= TEE_HANDLE_FLAG_KEY_SET; 247b0104773SPascal Brand req_key_usage = 0; 248b0104773SPascal Brand break; 249b0104773SPascal Brand 250b0104773SPascal Brand case TEE_ALG_DES_CBC_MAC_NOPAD: 251b0104773SPascal Brand case TEE_ALG_AES_CBC_MAC_NOPAD: 252b0104773SPascal Brand case TEE_ALG_AES_CBC_MAC_PKCS5: 253b0104773SPascal Brand case TEE_ALG_AES_CMAC: 254b0104773SPascal Brand case TEE_ALG_DES_CBC_MAC_PKCS5: 255b0104773SPascal Brand case TEE_ALG_DES3_CBC_MAC_NOPAD: 256b0104773SPascal Brand case TEE_ALG_DES3_CBC_MAC_PKCS5: 257b0104773SPascal Brand case TEE_ALG_HMAC_MD5: 258b0104773SPascal Brand case TEE_ALG_HMAC_SHA1: 259b0104773SPascal Brand case TEE_ALG_HMAC_SHA224: 260b0104773SPascal Brand case TEE_ALG_HMAC_SHA256: 261b0104773SPascal Brand case TEE_ALG_HMAC_SHA384: 262b0104773SPascal Brand case TEE_ALG_HMAC_SHA512: 263b0104773SPascal Brand if (mode != TEE_MODE_MAC) 264b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 265b0104773SPascal Brand req_key_usage = TEE_USAGE_MAC; 266b0104773SPascal Brand break; 267b0104773SPascal Brand 268b0104773SPascal Brand default: 269b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 270b0104773SPascal Brand } 271b0104773SPascal Brand 272b66f219bSJens Wiklander op = TEE_Malloc(sizeof(*op), TEE_MALLOC_FILL_ZERO); 2739b52c538SCedric Chaumont if (!op) 274b0104773SPascal Brand return TEE_ERROR_OUT_OF_MEMORY; 275b0104773SPascal Brand 276b0104773SPascal Brand op->info.algorithm = algorithm; 277b0104773SPascal Brand op->info.operationClass = TEE_ALG_GET_CLASS(algorithm); 278b0104773SPascal Brand op->info.mode = mode; 279b0104773SPascal Brand op->info.maxKeySize = maxKeySize; 280b0104773SPascal Brand op->info.requiredKeyUsage = req_key_usage; 281b0104773SPascal Brand op->info.handleState = handle_state; 282b0104773SPascal Brand 283b0104773SPascal Brand if (block_size > 1) { 284b0104773SPascal Brand size_t buffer_size = block_size; 285b0104773SPascal Brand 286b0104773SPascal Brand if (buffer_two_blocks) 287b0104773SPascal Brand buffer_size *= 2; 288b0104773SPascal Brand 2899b52c538SCedric Chaumont op->buffer = TEE_Malloc(buffer_size, 2909b52c538SCedric Chaumont TEE_USER_MEM_HINT_NO_FILL_ZERO); 291b0104773SPascal Brand if (op->buffer == NULL) { 292b0104773SPascal Brand res = TEE_ERROR_OUT_OF_MEMORY; 293b66f219bSJens Wiklander goto out; 294b0104773SPascal Brand } 295b0104773SPascal Brand } 296b0104773SPascal Brand op->block_size = block_size; 297b0104773SPascal Brand op->buffer_two_blocks = buffer_two_blocks; 298b0104773SPascal Brand 299b0104773SPascal Brand if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) { 300b0104773SPascal Brand uint32_t mks = maxKeySize; 301b0104773SPascal Brand TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm, 302b0104773SPascal Brand with_private_key); 303b0104773SPascal Brand 304b0104773SPascal Brand /* 305b0104773SPascal Brand * If two keys are expected the max key size is the sum of 306b0104773SPascal Brand * the size of both keys. 307b0104773SPascal Brand */ 308b0104773SPascal Brand if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) 309b0104773SPascal Brand mks /= 2; 310b0104773SPascal Brand 311b0104773SPascal Brand res = TEE_AllocateTransientObject(key_type, mks, &op->key1); 312b0104773SPascal Brand if (res != TEE_SUCCESS) 313b66f219bSJens Wiklander goto out; 314b0104773SPascal Brand 31505304565SCedric Chaumont if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) { 3169b52c538SCedric Chaumont res = TEE_AllocateTransientObject(key_type, mks, 317b0104773SPascal Brand &op->key2); 318b0104773SPascal Brand if (res != TEE_SUCCESS) 319b66f219bSJens Wiklander goto out; 320b0104773SPascal Brand } 321b0104773SPascal Brand } 322b0104773SPascal Brand 323e86f1266SJens Wiklander res = utee_cryp_state_alloc(algorithm, mode, (unsigned long)op->key1, 324e86f1266SJens Wiklander (unsigned long)op->key2, &op->state); 325b66f219bSJens Wiklander if (res != TEE_SUCCESS) 326b66f219bSJens Wiklander goto out; 327b0104773SPascal Brand 32805304565SCedric Chaumont /* 32905304565SCedric Chaumont * Initialize digest operations 33005304565SCedric Chaumont * Other multi-stage operations initialized w/ TEE_xxxInit functions 33105304565SCedric Chaumont * Non-applicable on asymmetric operations 33205304565SCedric Chaumont */ 33305304565SCedric Chaumont if (TEE_ALG_GET_CLASS(algorithm) == TEE_OPERATION_DIGEST) { 33405304565SCedric Chaumont res = utee_hash_init(op->state, NULL, 0); 33505304565SCedric Chaumont if (res != TEE_SUCCESS) 336b66f219bSJens Wiklander goto out; 33705304565SCedric Chaumont /* v1.1: flags always set for digest operations */ 33805304565SCedric Chaumont op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 33905304565SCedric Chaumont } 34005304565SCedric Chaumont 341642a1607SCedric Chaumont op->operationState = TEE_OPERATION_STATE_INITIAL; 342642a1607SCedric Chaumont 343b0104773SPascal Brand *operation = op; 344b0104773SPascal Brand 345b66f219bSJens Wiklander out: 346b66f219bSJens Wiklander if (res != TEE_SUCCESS) { 347b66f219bSJens Wiklander if (res != TEE_ERROR_OUT_OF_MEMORY && 3489b52c538SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 349b36311adSJerome Forissier TEE_Panic(res); 350b66f219bSJens Wiklander if (op) { 351b66f219bSJens Wiklander if (op->state) { 352b66f219bSJens Wiklander TEE_FreeOperation(op); 353b66f219bSJens Wiklander } else { 354b66f219bSJens Wiklander TEE_Free(op->buffer); 355b66f219bSJens Wiklander TEE_FreeTransientObject(op->key1); 356b66f219bSJens Wiklander TEE_FreeTransientObject(op->key2); 357b66f219bSJens Wiklander TEE_Free(op); 358b66f219bSJens Wiklander } 359b66f219bSJens Wiklander } 360b66f219bSJens Wiklander } 361b66f219bSJens Wiklander 362b0104773SPascal Brand return res; 363b0104773SPascal Brand } 364b0104773SPascal Brand 365b0104773SPascal Brand void TEE_FreeOperation(TEE_OperationHandle operation) 366b0104773SPascal Brand { 367e889e80bSCedric Chaumont TEE_Result res; 368e889e80bSCedric Chaumont 369e889e80bSCedric Chaumont if (operation == TEE_HANDLE_NULL) 370e889e80bSCedric Chaumont TEE_Panic(0); 371e889e80bSCedric Chaumont 372b0104773SPascal Brand /* 373b0104773SPascal Brand * Note that keys should not be freed here, since they are 374b0104773SPascal Brand * claimed by the operation they will be freed by 375b0104773SPascal Brand * utee_cryp_state_free(). 376b0104773SPascal Brand */ 377e889e80bSCedric Chaumont res = utee_cryp_state_free(operation->state); 378e889e80bSCedric Chaumont if (res != TEE_SUCCESS) 379b36311adSJerome Forissier TEE_Panic(res); 380e889e80bSCedric Chaumont 381b0104773SPascal Brand TEE_Free(operation->buffer); 382b0104773SPascal Brand TEE_Free(operation); 383b0104773SPascal Brand } 384b0104773SPascal Brand 385b0104773SPascal Brand void TEE_GetOperationInfo(TEE_OperationHandle operation, 386b0104773SPascal Brand TEE_OperationInfo *operationInfo) 387b0104773SPascal Brand { 388b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 389b0104773SPascal Brand TEE_Panic(0); 390b0104773SPascal Brand 39105304565SCedric Chaumont if (!operationInfo) 392b0104773SPascal Brand TEE_Panic(0); 393b0104773SPascal Brand 394b0104773SPascal Brand *operationInfo = operation->info; 395b0104773SPascal Brand } 396b0104773SPascal Brand 39705304565SCedric Chaumont TEE_Result TEE_GetOperationInfoMultiple(TEE_OperationHandle operation, 39805304565SCedric Chaumont TEE_OperationInfoMultiple *operationInfoMultiple, 39905304565SCedric Chaumont uint32_t *operationSize) 40005304565SCedric Chaumont { 40105304565SCedric Chaumont TEE_Result res = TEE_SUCCESS; 40205304565SCedric Chaumont TEE_ObjectInfo key_info1; 40305304565SCedric Chaumont TEE_ObjectInfo key_info2; 40405304565SCedric Chaumont uint32_t num_of_keys; 40505304565SCedric Chaumont size_t n; 40605304565SCedric Chaumont 40705304565SCedric Chaumont if (operation == TEE_HANDLE_NULL) { 40805304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 40905304565SCedric Chaumont goto out; 41005304565SCedric Chaumont } 41105304565SCedric Chaumont 41205304565SCedric Chaumont if (!operationInfoMultiple) { 41305304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 41405304565SCedric Chaumont goto out; 41505304565SCedric Chaumont } 41605304565SCedric Chaumont 41705304565SCedric Chaumont if (!operationSize) { 41805304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 41905304565SCedric Chaumont goto out; 42005304565SCedric Chaumont } 42105304565SCedric Chaumont 42205304565SCedric Chaumont num_of_keys = (*operationSize-sizeof(TEE_OperationInfoMultiple))/ 42305304565SCedric Chaumont sizeof(TEE_OperationInfoKey); 42405304565SCedric Chaumont 42505304565SCedric Chaumont if (num_of_keys > 2) { 42605304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 42705304565SCedric Chaumont goto out; 42805304565SCedric Chaumont } 42905304565SCedric Chaumont 43005304565SCedric Chaumont /* Two keys flag (TEE_ALG_AES_XTS only) */ 43105304565SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 43205304565SCedric Chaumont 0 && 43305304565SCedric Chaumont (num_of_keys != 2)) { 43405304565SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 43505304565SCedric Chaumont goto out; 43605304565SCedric Chaumont } 43705304565SCedric Chaumont 43805304565SCedric Chaumont /* Clear */ 43905304565SCedric Chaumont for (n = 0; n < num_of_keys; n++) { 44005304565SCedric Chaumont operationInfoMultiple->keyInformation[n].keySize = 0; 44105304565SCedric Chaumont operationInfoMultiple->keyInformation[n].requiredKeyUsage = 0; 44205304565SCedric Chaumont } 44305304565SCedric Chaumont 44405304565SCedric Chaumont if (num_of_keys == 2) { 44505304565SCedric Chaumont res = TEE_GetObjectInfo1(operation->key2, &key_info2); 44605304565SCedric Chaumont /* Key2 is not a valid handle */ 44705304565SCedric Chaumont if (res != TEE_SUCCESS) 44805304565SCedric Chaumont goto out; 44905304565SCedric Chaumont 45005304565SCedric Chaumont operationInfoMultiple->keyInformation[1].keySize = 45105304565SCedric Chaumont key_info2.keySize; 45205304565SCedric Chaumont operationInfoMultiple->keyInformation[1].requiredKeyUsage = 45305304565SCedric Chaumont operation->info.requiredKeyUsage; 45405304565SCedric Chaumont } 45505304565SCedric Chaumont 45605304565SCedric Chaumont if (num_of_keys >= 1) { 45705304565SCedric Chaumont res = TEE_GetObjectInfo1(operation->key1, &key_info1); 45805304565SCedric Chaumont /* Key1 is not a valid handle */ 45905304565SCedric Chaumont if (res != TEE_SUCCESS) { 46005304565SCedric Chaumont if (num_of_keys == 2) { 46105304565SCedric Chaumont operationInfoMultiple->keyInformation[1]. 46205304565SCedric Chaumont keySize = 0; 46305304565SCedric Chaumont operationInfoMultiple->keyInformation[1]. 46405304565SCedric Chaumont requiredKeyUsage = 0; 46505304565SCedric Chaumont } 46605304565SCedric Chaumont goto out; 46705304565SCedric Chaumont } 46805304565SCedric Chaumont 46905304565SCedric Chaumont operationInfoMultiple->keyInformation[0].keySize = 47005304565SCedric Chaumont key_info1.keySize; 47105304565SCedric Chaumont operationInfoMultiple->keyInformation[0].requiredKeyUsage = 47205304565SCedric Chaumont operation->info.requiredKeyUsage; 47305304565SCedric Chaumont } 47405304565SCedric Chaumont 47505304565SCedric Chaumont /* No key */ 47605304565SCedric Chaumont operationInfoMultiple->algorithm = operation->info.algorithm; 47705304565SCedric Chaumont operationInfoMultiple->operationClass = operation->info.operationClass; 47805304565SCedric Chaumont operationInfoMultiple->mode = operation->info.mode; 47905304565SCedric Chaumont operationInfoMultiple->digestLength = operation->info.digestLength; 48005304565SCedric Chaumont operationInfoMultiple->maxKeySize = operation->info.maxKeySize; 48105304565SCedric Chaumont operationInfoMultiple->handleState = operation->info.handleState; 48205304565SCedric Chaumont operationInfoMultiple->operationState = operation->operationState; 48305304565SCedric Chaumont operationInfoMultiple->numberOfKeys = num_of_keys; 48405304565SCedric Chaumont 48505304565SCedric Chaumont out: 48605304565SCedric Chaumont if (res != TEE_SUCCESS && 48705304565SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 488b36311adSJerome Forissier TEE_Panic(res); 48905304565SCedric Chaumont 49005304565SCedric Chaumont return res; 49105304565SCedric Chaumont } 49205304565SCedric Chaumont 493b0104773SPascal Brand void TEE_ResetOperation(TEE_OperationHandle operation) 494b0104773SPascal Brand { 495b0104773SPascal Brand TEE_Result res; 496b0104773SPascal Brand 497b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 498b0104773SPascal Brand TEE_Panic(0); 499bf80076aSCedric Chaumont 500642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET)) 501bf80076aSCedric Chaumont TEE_Panic(0); 502bf80076aSCedric Chaumont 503642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 504642a1607SCedric Chaumont 505b0104773SPascal Brand if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 506b0104773SPascal Brand res = utee_hash_init(operation->state, NULL, 0); 507b0104773SPascal Brand if (res != TEE_SUCCESS) 508b0104773SPascal Brand TEE_Panic(res); 50905304565SCedric Chaumont operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 51005304565SCedric Chaumont } else { 511b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 512b0104773SPascal Brand } 51305304565SCedric Chaumont } 514b0104773SPascal Brand 515b0104773SPascal Brand TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation, 516b0104773SPascal Brand TEE_ObjectHandle key) 517b0104773SPascal Brand { 5187583c59eSCedric Chaumont TEE_Result res; 519b0104773SPascal Brand uint32_t key_size = 0; 520b0104773SPascal Brand TEE_ObjectInfo key_info; 521b0104773SPascal Brand 522a57c1e2eSCedric Chaumont if (operation == TEE_HANDLE_NULL) { 523a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 524a57c1e2eSCedric Chaumont goto out; 525a57c1e2eSCedric Chaumont } 526a57c1e2eSCedric Chaumont 527642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 528642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 529642a1607SCedric Chaumont goto out; 530642a1607SCedric Chaumont } 531642a1607SCedric Chaumont 532a57c1e2eSCedric Chaumont if (key == TEE_HANDLE_NULL) { 533a57c1e2eSCedric Chaumont /* Operation key cleared */ 534a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key1); 535a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 536a57c1e2eSCedric Chaumont goto out; 537a57c1e2eSCedric Chaumont } 538a57c1e2eSCedric Chaumont 539a57c1e2eSCedric Chaumont /* No key for digest operation */ 540a57c1e2eSCedric Chaumont if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 541a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 542a57c1e2eSCedric Chaumont goto out; 543a57c1e2eSCedric Chaumont } 544a57c1e2eSCedric Chaumont 545a57c1e2eSCedric Chaumont /* Two keys flag not expected (TEE_ALG_AES_XTS excluded) */ 546a57c1e2eSCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 547a57c1e2eSCedric Chaumont 0) { 548a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 549a57c1e2eSCedric Chaumont goto out; 550a57c1e2eSCedric Chaumont } 551a57c1e2eSCedric Chaumont 5527583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key, &key_info); 553a57c1e2eSCedric Chaumont /* Key is not a valid handle */ 5547583c59eSCedric Chaumont if (res != TEE_SUCCESS) 555a57c1e2eSCedric Chaumont goto out; 5567583c59eSCedric Chaumont 557b0104773SPascal Brand /* Supplied key has to meet required usage */ 558b0104773SPascal Brand if ((key_info.objectUsage & operation->info.requiredKeyUsage) != 559b0104773SPascal Brand operation->info.requiredKeyUsage) { 560a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 561a57c1e2eSCedric Chaumont goto out; 562b0104773SPascal Brand } 563b0104773SPascal Brand 564a57c1e2eSCedric Chaumont if (operation->info.maxKeySize < key_info.keySize) { 565a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 566a57c1e2eSCedric Chaumont goto out; 567a57c1e2eSCedric Chaumont } 568b0104773SPascal Brand 5697583c59eSCedric Chaumont key_size = key_info.keySize; 570b0104773SPascal Brand 571b0104773SPascal Brand TEE_ResetTransientObject(operation->key1); 572b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 573b0104773SPascal Brand 5747583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key1, key); 5757583c59eSCedric Chaumont if (res != TEE_SUCCESS) 576a57c1e2eSCedric Chaumont goto out; 5777583c59eSCedric Chaumont 578b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 579b0104773SPascal Brand 580b0104773SPascal Brand operation->info.keySize = key_size; 581b0104773SPascal Brand 5827583c59eSCedric Chaumont out: 583a57c1e2eSCedric Chaumont if (res != TEE_SUCCESS && 584a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 585a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 586b36311adSJerome Forissier TEE_Panic(res); 587a57c1e2eSCedric Chaumont 588a57c1e2eSCedric Chaumont return res; 589b0104773SPascal Brand } 590b0104773SPascal Brand 591b0104773SPascal Brand TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation, 592b0104773SPascal Brand TEE_ObjectHandle key1, TEE_ObjectHandle key2) 593b0104773SPascal Brand { 5947583c59eSCedric Chaumont TEE_Result res; 595b0104773SPascal Brand uint32_t key_size = 0; 596b0104773SPascal Brand TEE_ObjectInfo key_info1; 597b0104773SPascal Brand TEE_ObjectInfo key_info2; 598b0104773SPascal Brand 599a57c1e2eSCedric Chaumont if (operation == TEE_HANDLE_NULL) { 600a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 601a57c1e2eSCedric Chaumont goto out; 602a57c1e2eSCedric Chaumont } 603a57c1e2eSCedric Chaumont 604642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 605642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 606642a1607SCedric Chaumont goto out; 607642a1607SCedric Chaumont } 608642a1607SCedric Chaumont 609a57c1e2eSCedric Chaumont /* 610a57c1e2eSCedric Chaumont * Key1/Key2 and/or are not initialized and 611a57c1e2eSCedric Chaumont * Either both keys are NULL or both are not NULL 612a57c1e2eSCedric Chaumont */ 613a57c1e2eSCedric Chaumont if (key1 == TEE_HANDLE_NULL || key2 == TEE_HANDLE_NULL) { 614a57c1e2eSCedric Chaumont /* Clear operation key1 (if needed) */ 615a57c1e2eSCedric Chaumont if (key1 == TEE_HANDLE_NULL) 616a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key1); 617a57c1e2eSCedric Chaumont /* Clear operation key2 (if needed) */ 618a57c1e2eSCedric Chaumont if (key2 == TEE_HANDLE_NULL) 619a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key2); 620a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 621a57c1e2eSCedric Chaumont goto out; 622a57c1e2eSCedric Chaumont } 623a57c1e2eSCedric Chaumont 624a57c1e2eSCedric Chaumont /* No key for digest operation */ 625a57c1e2eSCedric Chaumont if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 626a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 627a57c1e2eSCedric Chaumont goto out; 628a57c1e2eSCedric Chaumont } 629a57c1e2eSCedric Chaumont 630a57c1e2eSCedric Chaumont /* Two keys flag expected (TEE_ALG_AES_XTS only) */ 631a57c1e2eSCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 632a57c1e2eSCedric Chaumont 0) { 633a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 634a57c1e2eSCedric Chaumont goto out; 635a57c1e2eSCedric Chaumont } 636a57c1e2eSCedric Chaumont 6377583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key1, &key_info1); 638a57c1e2eSCedric Chaumont /* Key1 is not a valid handle */ 6397583c59eSCedric Chaumont if (res != TEE_SUCCESS) 640a57c1e2eSCedric Chaumont goto out; 6417583c59eSCedric Chaumont 642b0104773SPascal Brand /* Supplied key has to meet required usage */ 643b0104773SPascal Brand if ((key_info1.objectUsage & operation->info. 644b0104773SPascal Brand requiredKeyUsage) != operation->info.requiredKeyUsage) { 645a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 646a57c1e2eSCedric Chaumont goto out; 647b0104773SPascal Brand } 648b0104773SPascal Brand 6497583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key2, &key_info2); 650a57c1e2eSCedric Chaumont /* Key2 is not a valid handle */ 6517583c59eSCedric Chaumont if (res != TEE_SUCCESS) { 6527583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) 6537583c59eSCedric Chaumont res = TEE_ERROR_CORRUPT_OBJECT_2; 654a57c1e2eSCedric Chaumont goto out; 6557583c59eSCedric Chaumont } 6567583c59eSCedric Chaumont 657b0104773SPascal Brand /* Supplied key has to meet required usage */ 658b0104773SPascal Brand if ((key_info2.objectUsage & operation->info. 659b0104773SPascal Brand requiredKeyUsage) != operation->info.requiredKeyUsage) { 660a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 661a57c1e2eSCedric Chaumont goto out; 662b0104773SPascal Brand } 663b0104773SPascal Brand 664b0104773SPascal Brand /* 665b0104773SPascal Brand * AES-XTS (the only multi key algorithm supported, requires the 666b0104773SPascal Brand * keys to be of equal size. 667b0104773SPascal Brand */ 668b0104773SPascal Brand if (operation->info.algorithm == TEE_ALG_AES_XTS && 669a57c1e2eSCedric Chaumont key_info1.keySize != key_info2.keySize) { 670a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 671a57c1e2eSCedric Chaumont goto out; 672b0104773SPascal Brand 673a57c1e2eSCedric Chaumont } 674a57c1e2eSCedric Chaumont 675a57c1e2eSCedric Chaumont if (operation->info.maxKeySize < key_info1.keySize) { 676a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 677a57c1e2eSCedric Chaumont goto out; 678a57c1e2eSCedric Chaumont } 679b0104773SPascal Brand 680b0104773SPascal Brand /* 681b0104773SPascal Brand * Odd that only the size of one key should be reported while 682b0104773SPascal Brand * size of two key are used when allocating the operation. 683b0104773SPascal Brand */ 6847583c59eSCedric Chaumont key_size = key_info1.keySize; 685b0104773SPascal Brand 686b0104773SPascal Brand TEE_ResetTransientObject(operation->key1); 687b0104773SPascal Brand TEE_ResetTransientObject(operation->key2); 688b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 689b0104773SPascal Brand 6907583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key1, key1); 6917583c59eSCedric Chaumont if (res != TEE_SUCCESS) 692a57c1e2eSCedric Chaumont goto out; 6937583c59eSCedric Chaumont 6947583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key2, key2); 6957583c59eSCedric Chaumont if (res != TEE_SUCCESS) { 6967583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) 6977583c59eSCedric Chaumont res = TEE_ERROR_CORRUPT_OBJECT_2; 698a57c1e2eSCedric Chaumont goto out; 6997583c59eSCedric Chaumont } 7007583c59eSCedric Chaumont 701b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 702b0104773SPascal Brand 703b0104773SPascal Brand operation->info.keySize = key_size; 704b0104773SPascal Brand 7057583c59eSCedric Chaumont out: 706a57c1e2eSCedric Chaumont if (res != TEE_SUCCESS && 707a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 708a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT_2 && 709a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE && 710a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE_2) 711b36311adSJerome Forissier TEE_Panic(res); 712a57c1e2eSCedric Chaumont 713a57c1e2eSCedric Chaumont return res; 714b0104773SPascal Brand } 715b0104773SPascal Brand 716b0104773SPascal Brand void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op) 717b0104773SPascal Brand { 718b0104773SPascal Brand TEE_Result res; 719b0104773SPascal Brand 720b0104773SPascal Brand if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL) 721b0104773SPascal Brand TEE_Panic(0); 722b0104773SPascal Brand if (dst_op->info.algorithm != src_op->info.algorithm) 723b0104773SPascal Brand TEE_Panic(0); 724b0104773SPascal Brand if (src_op->info.operationClass != TEE_OPERATION_DIGEST) { 725b0104773SPascal Brand TEE_ObjectHandle key1 = TEE_HANDLE_NULL; 726b0104773SPascal Brand TEE_ObjectHandle key2 = TEE_HANDLE_NULL; 727b0104773SPascal Brand 728b0104773SPascal Brand if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) { 729b0104773SPascal Brand key1 = src_op->key1; 730b0104773SPascal Brand key2 = src_op->key2; 731b0104773SPascal Brand } 732b0104773SPascal Brand 733b0104773SPascal Brand if ((src_op->info.handleState & 734b0104773SPascal Brand TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) { 735b0104773SPascal Brand TEE_SetOperationKey(dst_op, key1); 736b0104773SPascal Brand } else { 737b0104773SPascal Brand TEE_SetOperationKey2(dst_op, key1, key2); 738b0104773SPascal Brand } 739b0104773SPascal Brand } 740b0104773SPascal Brand dst_op->info.handleState = src_op->info.handleState; 741b0104773SPascal Brand dst_op->info.keySize = src_op->info.keySize; 742642a1607SCedric Chaumont dst_op->operationState = src_op->operationState; 743b0104773SPascal Brand 744b0104773SPascal Brand if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks || 745b0104773SPascal Brand dst_op->block_size != src_op->block_size) 746b0104773SPascal Brand TEE_Panic(0); 747b0104773SPascal Brand 748b0104773SPascal Brand if (dst_op->buffer != NULL) { 749b0104773SPascal Brand if (src_op->buffer == NULL) 750b0104773SPascal Brand TEE_Panic(0); 751b0104773SPascal Brand 752b0104773SPascal Brand memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs); 753b0104773SPascal Brand dst_op->buffer_offs = src_op->buffer_offs; 754b0104773SPascal Brand } else if (src_op->buffer != NULL) { 755b0104773SPascal Brand TEE_Panic(0); 756b0104773SPascal Brand } 757b0104773SPascal Brand 758b0104773SPascal Brand res = utee_cryp_state_copy(dst_op->state, src_op->state); 759b0104773SPascal Brand if (res != TEE_SUCCESS) 760b0104773SPascal Brand TEE_Panic(res); 761b0104773SPascal Brand } 762b0104773SPascal Brand 763b0104773SPascal Brand /* Cryptographic Operations API - Message Digest Functions */ 764b0104773SPascal Brand 7658f07fe6fSJerome Forissier static void init_hash_operation(TEE_OperationHandle operation, const void *IV, 7666d15db08SJerome Forissier uint32_t IVLen) 7676d15db08SJerome Forissier { 7686d15db08SJerome Forissier TEE_Result res; 7696d15db08SJerome Forissier 7706d15db08SJerome Forissier /* 7716d15db08SJerome Forissier * Note : IV and IVLen are never used in current implementation 7726d15db08SJerome Forissier * This is why coherent values of IV and IVLen are not checked 7736d15db08SJerome Forissier */ 7746d15db08SJerome Forissier res = utee_hash_init(operation->state, IV, IVLen); 7756d15db08SJerome Forissier if (res != TEE_SUCCESS) 7766d15db08SJerome Forissier TEE_Panic(res); 7776d15db08SJerome Forissier operation->buffer_offs = 0; 7786d15db08SJerome Forissier operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 7796d15db08SJerome Forissier } 7806d15db08SJerome Forissier 781b0104773SPascal Brand void TEE_DigestUpdate(TEE_OperationHandle operation, 7828f07fe6fSJerome Forissier const void *chunk, uint32_t chunkSize) 783b0104773SPascal Brand { 78473d6c3baSJoakim Bech TEE_Result res = TEE_ERROR_GENERIC; 785b0104773SPascal Brand 78673d6c3baSJoakim Bech if (operation == TEE_HANDLE_NULL || 78773d6c3baSJoakim Bech operation->info.operationClass != TEE_OPERATION_DIGEST) 788b0104773SPascal Brand TEE_Panic(0); 78973d6c3baSJoakim Bech 790642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 791642a1607SCedric Chaumont 792b0104773SPascal Brand res = utee_hash_update(operation->state, chunk, chunkSize); 793b0104773SPascal Brand if (res != TEE_SUCCESS) 794b0104773SPascal Brand TEE_Panic(res); 795b0104773SPascal Brand } 796b0104773SPascal Brand 7978f07fe6fSJerome Forissier TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void *chunk, 79879a3c601SCedric Chaumont uint32_t chunkLen, void *hash, uint32_t *hashLen) 799b0104773SPascal Brand { 80087c2f6b6SCedric Chaumont TEE_Result res; 801e86f1266SJens Wiklander uint64_t hl; 80287c2f6b6SCedric Chaumont 80387c2f6b6SCedric Chaumont if ((operation == TEE_HANDLE_NULL) || 80487c2f6b6SCedric Chaumont (!chunk && chunkLen) || 80587c2f6b6SCedric Chaumont !hash || 80687c2f6b6SCedric Chaumont !hashLen || 80787c2f6b6SCedric Chaumont (operation->info.operationClass != TEE_OPERATION_DIGEST)) { 80887c2f6b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 80987c2f6b6SCedric Chaumont goto out; 81087c2f6b6SCedric Chaumont } 81187c2f6b6SCedric Chaumont 812e86f1266SJens Wiklander hl = *hashLen; 813e86f1266SJens Wiklander res = utee_hash_final(operation->state, chunk, chunkLen, hash, &hl); 814e86f1266SJens Wiklander *hashLen = hl; 8156d15db08SJerome Forissier if (res != TEE_SUCCESS) 8166d15db08SJerome Forissier goto out; 8176d15db08SJerome Forissier 8186d15db08SJerome Forissier /* Reset operation state */ 8196d15db08SJerome Forissier init_hash_operation(operation, NULL, 0); 82087c2f6b6SCedric Chaumont 821642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 822642a1607SCedric Chaumont 82387c2f6b6SCedric Chaumont out: 82487c2f6b6SCedric Chaumont if (res != TEE_SUCCESS && 82587c2f6b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 826b36311adSJerome Forissier TEE_Panic(res); 82773d6c3baSJoakim Bech 82887c2f6b6SCedric Chaumont return res; 829b0104773SPascal Brand } 830b0104773SPascal Brand 831b0104773SPascal Brand /* Cryptographic Operations API - Symmetric Cipher Functions */ 832b0104773SPascal Brand 8338f07fe6fSJerome Forissier void TEE_CipherInit(TEE_OperationHandle operation, const void *IV, 8348f07fe6fSJerome Forissier uint32_t IVLen) 835b0104773SPascal Brand { 836b0104773SPascal Brand TEE_Result res; 837b0104773SPascal Brand 838b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 839b0104773SPascal Brand TEE_Panic(0); 840642a1607SCedric Chaumont 841b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_CIPHER) 842b0104773SPascal Brand TEE_Panic(0); 843642a1607SCedric Chaumont 844642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 845642a1607SCedric Chaumont !(operation->key1)) 846642a1607SCedric Chaumont TEE_Panic(0); 847642a1607SCedric Chaumont 848642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 849642a1607SCedric Chaumont TEE_ResetOperation(operation); 850642a1607SCedric Chaumont 851642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 852642a1607SCedric Chaumont 853b0104773SPascal Brand res = utee_cipher_init(operation->state, IV, IVLen); 854b0104773SPascal Brand if (res != TEE_SUCCESS) 855b0104773SPascal Brand TEE_Panic(res); 856642a1607SCedric Chaumont 857b0104773SPascal Brand operation->buffer_offs = 0; 858b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 859b0104773SPascal Brand } 860b0104773SPascal Brand 861b0104773SPascal Brand static TEE_Result tee_buffer_update( 862b0104773SPascal Brand TEE_OperationHandle op, 863e86f1266SJens Wiklander TEE_Result(*update_func)(unsigned long state, const void *src, 864e86f1266SJens Wiklander size_t slen, void *dst, uint64_t *dlen), 865b0104773SPascal Brand const void *src_data, size_t src_len, 866e86f1266SJens Wiklander void *dest_data, uint64_t *dest_len) 867b0104773SPascal Brand { 868b0104773SPascal Brand TEE_Result res; 869b0104773SPascal Brand const uint8_t *src = src_data; 870b0104773SPascal Brand size_t slen = src_len; 871b0104773SPascal Brand uint8_t *dst = dest_data; 872b0104773SPascal Brand size_t dlen = *dest_len; 873b0104773SPascal Brand size_t acc_dlen = 0; 874e86f1266SJens Wiklander uint64_t tmp_dlen; 875b0104773SPascal Brand size_t l; 876b0104773SPascal Brand size_t buffer_size; 877d3588802SPascal Brand size_t buffer_left; 878b0104773SPascal Brand 879090268f5SJens Wiklander if (!src) { 880090268f5SJens Wiklander if (slen) 881090268f5SJens Wiklander TEE_Panic(0); 882090268f5SJens Wiklander goto out; 883090268f5SJens Wiklander } 884090268f5SJens Wiklander 885d3588802SPascal Brand if (op->buffer_two_blocks) { 886b0104773SPascal Brand buffer_size = op->block_size * 2; 887d3588802SPascal Brand buffer_left = 1; 888d3588802SPascal Brand } else { 889b0104773SPascal Brand buffer_size = op->block_size; 890d3588802SPascal Brand buffer_left = 0; 891d3588802SPascal Brand } 892b0104773SPascal Brand 893b0104773SPascal Brand if (op->buffer_offs > 0) { 894b0104773SPascal Brand /* Fill up complete block */ 895b0104773SPascal Brand if (op->buffer_offs < op->block_size) 896b0104773SPascal Brand l = MIN(slen, op->block_size - op->buffer_offs); 897b0104773SPascal Brand else 898b0104773SPascal Brand l = MIN(slen, buffer_size - op->buffer_offs); 899b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, l); 900b0104773SPascal Brand op->buffer_offs += l; 901b0104773SPascal Brand src += l; 902b0104773SPascal Brand slen -= l; 903b0104773SPascal Brand if ((op->buffer_offs % op->block_size) != 0) 904b0104773SPascal Brand goto out; /* Nothing left to do */ 905b0104773SPascal Brand } 906b0104773SPascal Brand 907b0104773SPascal Brand /* If we can feed from buffer */ 908d3588802SPascal Brand if ((op->buffer_offs > 0) && 909d3588802SPascal Brand ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) { 9102ff3fdbbSPascal Brand l = ROUNDUP(op->buffer_offs + slen - buffer_size, 911b0104773SPascal Brand op->block_size); 912b0104773SPascal Brand l = MIN(op->buffer_offs, l); 913b0104773SPascal Brand tmp_dlen = dlen; 914b0104773SPascal Brand res = update_func(op->state, op->buffer, l, dst, &tmp_dlen); 915b0104773SPascal Brand if (res != TEE_SUCCESS) 916b0104773SPascal Brand TEE_Panic(res); 917b0104773SPascal Brand dst += tmp_dlen; 918b0104773SPascal Brand dlen -= tmp_dlen; 919b0104773SPascal Brand acc_dlen += tmp_dlen; 920b0104773SPascal Brand op->buffer_offs -= l; 921b0104773SPascal Brand if (op->buffer_offs > 0) { 922b0104773SPascal Brand /* 923b0104773SPascal Brand * Slen is small enough to be contained in rest buffer. 924b0104773SPascal Brand */ 925b0104773SPascal Brand memcpy(op->buffer, op->buffer + l, buffer_size - l); 926b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 927b0104773SPascal Brand op->buffer_offs += slen; 928b0104773SPascal Brand goto out; /* Nothing left to do */ 929b0104773SPascal Brand } 930b0104773SPascal Brand } 931b0104773SPascal Brand 932d3588802SPascal Brand if (slen >= (buffer_size + buffer_left)) { 933b0104773SPascal Brand /* Buffer is empty, feed as much as possible from src */ 934bf7a587fSJerome Forissier if (op->info.algorithm == TEE_ALG_AES_CTS) 935b1ecda78SJerome Forissier l = ROUNDUP(slen - buffer_size, op->block_size); 936bf7a587fSJerome Forissier else 937bf7a587fSJerome Forissier l = ROUNDUP(slen - buffer_size + 1, op->block_size); 938b0104773SPascal Brand 939b0104773SPascal Brand tmp_dlen = dlen; 940b0104773SPascal Brand res = update_func(op->state, src, l, dst, &tmp_dlen); 941b0104773SPascal Brand if (res != TEE_SUCCESS) 942b0104773SPascal Brand TEE_Panic(res); 943b0104773SPascal Brand src += l; 944b0104773SPascal Brand slen -= l; 945b0104773SPascal Brand dst += tmp_dlen; 946b0104773SPascal Brand dlen -= tmp_dlen; 947b0104773SPascal Brand acc_dlen += tmp_dlen; 948b0104773SPascal Brand } 949b0104773SPascal Brand 950b0104773SPascal Brand /* Slen is small enough to be contained in buffer. */ 951b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 952b0104773SPascal Brand op->buffer_offs += slen; 953b0104773SPascal Brand 954b0104773SPascal Brand out: 955b0104773SPascal Brand *dest_len = acc_dlen; 956b0104773SPascal Brand return TEE_SUCCESS; 957b0104773SPascal Brand } 958b0104773SPascal Brand 9598f07fe6fSJerome Forissier TEE_Result TEE_CipherUpdate(TEE_OperationHandle operation, const void *srcData, 96079a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 961b0104773SPascal Brand { 962dea1f2b6SCedric Chaumont TEE_Result res; 963b0104773SPascal Brand size_t req_dlen; 964e86f1266SJens Wiklander uint64_t dl; 965b0104773SPascal Brand 966642a1607SCedric Chaumont if (operation == TEE_HANDLE_NULL || 967dea1f2b6SCedric Chaumont (srcData == NULL && srcLen != 0) || 968dea1f2b6SCedric Chaumont destLen == NULL || 969dea1f2b6SCedric Chaumont (destData == NULL && *destLen != 0)) { 970dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 971dea1f2b6SCedric Chaumont goto out; 972dea1f2b6SCedric Chaumont } 973dea1f2b6SCedric Chaumont 974642a1607SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 975dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 976dea1f2b6SCedric Chaumont goto out; 977dea1f2b6SCedric Chaumont } 978dea1f2b6SCedric Chaumont 979642a1607SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 980642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 981642a1607SCedric Chaumont goto out; 982642a1607SCedric Chaumont } 983642a1607SCedric Chaumont 984642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 985dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 986dea1f2b6SCedric Chaumont goto out; 987dea1f2b6SCedric Chaumont } 988b0104773SPascal Brand 989e32c5ddfSJerome Forissier if (!srcData && !srcLen) { 990090268f5SJens Wiklander *destLen = 0; 991e32c5ddfSJerome Forissier res = TEE_SUCCESS; 992e32c5ddfSJerome Forissier goto out; 993e32c5ddfSJerome Forissier } 994e32c5ddfSJerome Forissier 995b0104773SPascal Brand /* Calculate required dlen */ 996642a1607SCedric Chaumont req_dlen = ((operation->buffer_offs + srcLen) / operation->block_size) * 997642a1607SCedric Chaumont operation->block_size; 998642a1607SCedric Chaumont if (operation->buffer_two_blocks) { 999642a1607SCedric Chaumont if (req_dlen > operation->block_size * 2) 1000642a1607SCedric Chaumont req_dlen -= operation->block_size * 2; 1001b0104773SPascal Brand else 1002b0104773SPascal Brand req_dlen = 0; 1003b0104773SPascal Brand } 1004b0104773SPascal Brand /* 1005b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1006b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1007b0104773SPascal Brand * can't restore sync with this API. 1008b0104773SPascal Brand */ 1009b0104773SPascal Brand if (*destLen < req_dlen) { 1010b0104773SPascal Brand *destLen = req_dlen; 1011dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1012dea1f2b6SCedric Chaumont goto out; 1013b0104773SPascal Brand } 1014b0104773SPascal Brand 1015e86f1266SJens Wiklander dl = *destLen; 1016642a1607SCedric Chaumont res = tee_buffer_update(operation, utee_cipher_update, srcData, srcLen, 1017e86f1266SJens Wiklander destData, &dl); 1018e86f1266SJens Wiklander *destLen = dl; 1019b0104773SPascal Brand 1020dea1f2b6SCedric Chaumont out: 1021dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 1022dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1023b36311adSJerome Forissier TEE_Panic(res); 1024dea1f2b6SCedric Chaumont 1025dea1f2b6SCedric Chaumont return res; 1026b0104773SPascal Brand } 1027b0104773SPascal Brand 1028642a1607SCedric Chaumont TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation, 10298f07fe6fSJerome Forissier const void *srcData, uint32_t srcLen, 10308f07fe6fSJerome Forissier void *destData, uint32_t *destLen) 1031b0104773SPascal Brand { 1032b0104773SPascal Brand TEE_Result res; 1033b0104773SPascal Brand uint8_t *dst = destData; 1034b0104773SPascal Brand size_t acc_dlen = 0; 1035e86f1266SJens Wiklander uint64_t tmp_dlen; 1036b0104773SPascal Brand size_t req_dlen; 1037b0104773SPascal Brand 1038642a1607SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1039dea1f2b6SCedric Chaumont (srcData == NULL && srcLen != 0) || 1040dea1f2b6SCedric Chaumont destLen == NULL || 1041dea1f2b6SCedric Chaumont (destData == NULL && *destLen != 0)) { 1042dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1043dea1f2b6SCedric Chaumont goto out; 1044dea1f2b6SCedric Chaumont } 1045dea1f2b6SCedric Chaumont 1046642a1607SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 1047dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1048dea1f2b6SCedric Chaumont goto out; 1049dea1f2b6SCedric Chaumont } 1050dea1f2b6SCedric Chaumont 1051642a1607SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1052642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1053642a1607SCedric Chaumont goto out; 1054642a1607SCedric Chaumont } 1055642a1607SCedric Chaumont 1056642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1057dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1058dea1f2b6SCedric Chaumont goto out; 1059dea1f2b6SCedric Chaumont } 1060b0104773SPascal Brand 1061b0104773SPascal Brand /* 1062b0104773SPascal Brand * Check that the final block doesn't require padding for those 1063b0104773SPascal Brand * algorithms that requires client to supply padding. 1064b0104773SPascal Brand */ 1065642a1607SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD || 1066642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD || 1067642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD || 1068642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD || 1069642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || 1070642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD) { 1071642a1607SCedric Chaumont if (((operation->buffer_offs + srcLen) % operation->block_size) 1072642a1607SCedric Chaumont != 0) { 1073dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1074dea1f2b6SCedric Chaumont goto out; 1075dea1f2b6SCedric Chaumont } 1076b0104773SPascal Brand } 1077b0104773SPascal Brand 1078b0104773SPascal Brand /* 1079b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1080b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1081b0104773SPascal Brand * can't restore sync with this API. 1082b0104773SPascal Brand */ 1083642a1607SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1084b0104773SPascal Brand if (*destLen < req_dlen) { 1085b0104773SPascal Brand *destLen = req_dlen; 1086dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1087dea1f2b6SCedric Chaumont goto out; 1088b0104773SPascal Brand } 1089b0104773SPascal Brand 1090b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1091642a1607SCedric Chaumont res = tee_buffer_update(operation, utee_cipher_update, srcData, srcLen, 1092642a1607SCedric Chaumont dst, &tmp_dlen); 1093dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS) 1094dea1f2b6SCedric Chaumont goto out; 1095dea1f2b6SCedric Chaumont 1096b0104773SPascal Brand dst += tmp_dlen; 1097b0104773SPascal Brand acc_dlen += tmp_dlen; 1098b0104773SPascal Brand 1099b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1100642a1607SCedric Chaumont res = utee_cipher_final(operation->state, operation->buffer, 1101642a1607SCedric Chaumont operation->buffer_offs, dst, &tmp_dlen); 1102b0104773SPascal Brand if (res != TEE_SUCCESS) 1103dea1f2b6SCedric Chaumont goto out; 1104dea1f2b6SCedric Chaumont 1105b0104773SPascal Brand acc_dlen += tmp_dlen; 1106b0104773SPascal Brand *destLen = acc_dlen; 1107dea1f2b6SCedric Chaumont 1108642a1607SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1109642a1607SCedric Chaumont 1110642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1111642a1607SCedric Chaumont 1112dea1f2b6SCedric Chaumont out: 1113dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 1114dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1115b36311adSJerome Forissier TEE_Panic(res); 1116dea1f2b6SCedric Chaumont 1117dea1f2b6SCedric Chaumont return res; 1118b0104773SPascal Brand } 1119b0104773SPascal Brand 1120b0104773SPascal Brand /* Cryptographic Operations API - MAC Functions */ 1121b0104773SPascal Brand 11228f07fe6fSJerome Forissier void TEE_MACInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen) 1123b0104773SPascal Brand { 1124b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 1125b0104773SPascal Brand TEE_Panic(0); 1126642a1607SCedric Chaumont 1127b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_MAC) 1128b0104773SPascal Brand TEE_Panic(0); 1129642a1607SCedric Chaumont 1130642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 1131642a1607SCedric Chaumont !(operation->key1)) 1132642a1607SCedric Chaumont TEE_Panic(0); 1133642a1607SCedric Chaumont 1134642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 1135642a1607SCedric Chaumont TEE_ResetOperation(operation); 1136642a1607SCedric Chaumont 1137642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1138642a1607SCedric Chaumont 11396d15db08SJerome Forissier init_hash_operation(operation, IV, IVLen); 1140b0104773SPascal Brand } 1141b0104773SPascal Brand 11428f07fe6fSJerome Forissier void TEE_MACUpdate(TEE_OperationHandle operation, const void *chunk, 114328e0efc6SCedric Chaumont uint32_t chunkSize) 1144b0104773SPascal Brand { 1145b0104773SPascal Brand TEE_Result res; 1146b0104773SPascal Brand 114728e0efc6SCedric Chaumont if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0)) 1148b0104773SPascal Brand TEE_Panic(0); 1149642a1607SCedric Chaumont 115028e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) 1151b0104773SPascal Brand TEE_Panic(0); 1152642a1607SCedric Chaumont 115328e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1154b0104773SPascal Brand TEE_Panic(0); 1155b0104773SPascal Brand 1156642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) 1157642a1607SCedric Chaumont TEE_Panic(0); 1158642a1607SCedric Chaumont 115928e0efc6SCedric Chaumont res = utee_hash_update(operation->state, chunk, chunkSize); 1160b0104773SPascal Brand if (res != TEE_SUCCESS) 1161b0104773SPascal Brand TEE_Panic(res); 1162b0104773SPascal Brand } 1163b0104773SPascal Brand 116428e0efc6SCedric Chaumont TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation, 11658f07fe6fSJerome Forissier const void *message, uint32_t messageLen, 116679a3c601SCedric Chaumont void *mac, uint32_t *macLen) 1167b0104773SPascal Brand { 1168b0104773SPascal Brand TEE_Result res; 1169e86f1266SJens Wiklander uint64_t ml; 1170b0104773SPascal Brand 117128e0efc6SCedric Chaumont if (operation == TEE_HANDLE_NULL || 117228e0efc6SCedric Chaumont (message == NULL && messageLen != 0) || 117328e0efc6SCedric Chaumont mac == NULL || 117428e0efc6SCedric Chaumont macLen == NULL) { 117528e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 117628e0efc6SCedric Chaumont goto out; 117728e0efc6SCedric Chaumont } 1178b0104773SPascal Brand 117928e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 118028e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 118128e0efc6SCedric Chaumont goto out; 118228e0efc6SCedric Chaumont } 118328e0efc6SCedric Chaumont 118428e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 118528e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 118628e0efc6SCedric Chaumont goto out; 118728e0efc6SCedric Chaumont } 118828e0efc6SCedric Chaumont 1189642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1190642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1191642a1607SCedric Chaumont goto out; 1192642a1607SCedric Chaumont } 1193642a1607SCedric Chaumont 1194e86f1266SJens Wiklander ml = *macLen; 1195e86f1266SJens Wiklander res = utee_hash_final(operation->state, message, messageLen, mac, &ml); 1196e86f1266SJens Wiklander *macLen = ml; 119728e0efc6SCedric Chaumont if (res != TEE_SUCCESS) 119828e0efc6SCedric Chaumont goto out; 119928e0efc6SCedric Chaumont 120028e0efc6SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 120128e0efc6SCedric Chaumont 1202642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1203642a1607SCedric Chaumont 120428e0efc6SCedric Chaumont out: 120528e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 120628e0efc6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 120728e0efc6SCedric Chaumont TEE_Panic(res); 120828e0efc6SCedric Chaumont 1209b0104773SPascal Brand return res; 1210b0104773SPascal Brand } 1211b0104773SPascal Brand 1212b0104773SPascal Brand TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation, 12138f07fe6fSJerome Forissier const void *message, uint32_t messageLen, 12148f07fe6fSJerome Forissier const void *mac, uint32_t macLen) 1215b0104773SPascal Brand { 1216b0104773SPascal Brand TEE_Result res; 1217b0104773SPascal Brand uint8_t computed_mac[TEE_MAX_HASH_SIZE]; 12187f74c64aSPascal Brand uint32_t computed_mac_size = TEE_MAX_HASH_SIZE; 1219b0104773SPascal Brand 122028e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 122128e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 122228e0efc6SCedric Chaumont goto out; 122328e0efc6SCedric Chaumont } 122428e0efc6SCedric Chaumont 122528e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 122628e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 122728e0efc6SCedric Chaumont goto out; 122828e0efc6SCedric Chaumont } 122928e0efc6SCedric Chaumont 1230642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1231642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1232642a1607SCedric Chaumont goto out; 1233642a1607SCedric Chaumont } 1234642a1607SCedric Chaumont 1235b0104773SPascal Brand res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac, 1236b0104773SPascal Brand &computed_mac_size); 1237b0104773SPascal Brand if (res != TEE_SUCCESS) 123828e0efc6SCedric Chaumont goto out; 123928e0efc6SCedric Chaumont 124028e0efc6SCedric Chaumont if (computed_mac_size != macLen) { 124128e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 124228e0efc6SCedric Chaumont goto out; 124328e0efc6SCedric Chaumont } 124428e0efc6SCedric Chaumont 124528e0efc6SCedric Chaumont if (buf_compare_ct(mac, computed_mac, computed_mac_size) != 0) { 124628e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 124728e0efc6SCedric Chaumont goto out; 124828e0efc6SCedric Chaumont } 124928e0efc6SCedric Chaumont 1250642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1251642a1607SCedric Chaumont 125228e0efc6SCedric Chaumont out: 125328e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 125428e0efc6SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 125528e0efc6SCedric Chaumont TEE_Panic(res); 125628e0efc6SCedric Chaumont 1257b0104773SPascal Brand return res; 1258b0104773SPascal Brand } 1259b0104773SPascal Brand 1260b0104773SPascal Brand /* Cryptographic Operations API - Authenticated Encryption Functions */ 1261b0104773SPascal Brand 12628f07fe6fSJerome Forissier TEE_Result TEE_AEInit(TEE_OperationHandle operation, const void *nonce, 126379a3c601SCedric Chaumont uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen, 1264b0104773SPascal Brand uint32_t payloadLen) 1265b0104773SPascal Brand { 1266b0104773SPascal Brand TEE_Result res; 1267b0104773SPascal Brand 1268b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || nonce == NULL) { 1269b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1270b5816c88SCedric Chaumont goto out; 1271b5816c88SCedric Chaumont } 1272b5816c88SCedric Chaumont 1273b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1274b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1275b5816c88SCedric Chaumont goto out; 1276b5816c88SCedric Chaumont } 1277b0104773SPascal Brand 1278642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 1279642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1280642a1607SCedric Chaumont goto out; 1281642a1607SCedric Chaumont } 1282642a1607SCedric Chaumont 1283b0104773SPascal Brand /* 1284b0104773SPascal Brand * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core 1285b0104773SPascal Brand * in the implementation. But AES-GCM spec doesn't specify the tag len 1286b0104773SPascal Brand * according to the same principle so we have to check here instead to 1287b0104773SPascal Brand * be GP compliant. 1288b0104773SPascal Brand */ 1289b5816c88SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_GCM) { 1290b0104773SPascal Brand /* 1291b0104773SPascal Brand * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96 1292b0104773SPascal Brand */ 1293b5816c88SCedric Chaumont if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) { 1294b5816c88SCedric Chaumont res = TEE_ERROR_NOT_SUPPORTED; 1295b5816c88SCedric Chaumont goto out; 1296b5816c88SCedric Chaumont } 1297b0104773SPascal Brand } 1298b0104773SPascal Brand 1299b5816c88SCedric Chaumont res = utee_authenc_init(operation->state, nonce, nonceLen, 1300b5816c88SCedric Chaumont tagLen / 8, AADLen, payloadLen); 1301b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1302b5816c88SCedric Chaumont goto out; 1303b5816c88SCedric Chaumont 1304b5816c88SCedric Chaumont operation->ae_tag_len = tagLen / 8; 1305b5816c88SCedric Chaumont operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 1306b5816c88SCedric Chaumont 1307b5816c88SCedric Chaumont out: 1308b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1309b5816c88SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 1310b0104773SPascal Brand TEE_Panic(res); 1311b5816c88SCedric Chaumont 1312b0104773SPascal Brand return res; 1313b0104773SPascal Brand } 1314b0104773SPascal Brand 13158f07fe6fSJerome Forissier void TEE_AEUpdateAAD(TEE_OperationHandle operation, const void *AADdata, 131679a3c601SCedric Chaumont uint32_t AADdataLen) 1317b0104773SPascal Brand { 1318b0104773SPascal Brand TEE_Result res; 1319b0104773SPascal Brand 1320b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1321b5816c88SCedric Chaumont (AADdata == NULL && AADdataLen != 0)) 1322b0104773SPascal Brand TEE_Panic(0); 1323642a1607SCedric Chaumont 1324b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) 1325b0104773SPascal Brand TEE_Panic(0); 1326642a1607SCedric Chaumont 1327b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1328b0104773SPascal Brand TEE_Panic(0); 1329b0104773SPascal Brand 1330b5816c88SCedric Chaumont res = utee_authenc_update_aad(operation->state, AADdata, AADdataLen); 1331642a1607SCedric Chaumont 1332642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1333642a1607SCedric Chaumont 1334b0104773SPascal Brand if (res != TEE_SUCCESS) 1335b0104773SPascal Brand TEE_Panic(res); 1336b0104773SPascal Brand } 1337b0104773SPascal Brand 13388f07fe6fSJerome Forissier TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, const void *srcData, 133979a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 1340b0104773SPascal Brand { 1341b5816c88SCedric Chaumont TEE_Result res; 1342b0104773SPascal Brand size_t req_dlen; 1343e86f1266SJens Wiklander uint64_t dl; 1344b0104773SPascal Brand 1345b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1346b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1347b5816c88SCedric Chaumont destLen == NULL || 1348b5816c88SCedric Chaumont (destData == NULL && *destLen != 0)) { 1349b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1350b5816c88SCedric Chaumont goto out; 1351b5816c88SCedric Chaumont } 1352b5816c88SCedric Chaumont 1353b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1354b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1355b5816c88SCedric Chaumont goto out; 1356b5816c88SCedric Chaumont } 1357b5816c88SCedric Chaumont 1358b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1359b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1360b5816c88SCedric Chaumont goto out; 1361b5816c88SCedric Chaumont } 1362b0104773SPascal Brand 1363827308b8SJerome Forissier if (!srcData && !srcLen) { 1364090268f5SJens Wiklander *destLen = 0; 1365827308b8SJerome Forissier res = TEE_SUCCESS; 1366827308b8SJerome Forissier goto out; 1367827308b8SJerome Forissier } 1368827308b8SJerome Forissier 1369b0104773SPascal Brand /* 1370b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1371b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1372b0104773SPascal Brand * can't restore sync with this API. 1373b0104773SPascal Brand */ 1374*afc0c182SBogdan Liulko if (operation->block_size > 1) { 1375b5816c88SCedric Chaumont req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen, 1376b5816c88SCedric Chaumont operation->block_size); 1377*afc0c182SBogdan Liulko } else { 1378*afc0c182SBogdan Liulko req_dlen = srcLen; 1379*afc0c182SBogdan Liulko } 1380*afc0c182SBogdan Liulko 1381b0104773SPascal Brand if (*destLen < req_dlen) { 1382b0104773SPascal Brand *destLen = req_dlen; 1383b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1384b5816c88SCedric Chaumont goto out; 1385b0104773SPascal Brand } 1386b0104773SPascal Brand 1387e86f1266SJens Wiklander dl = *destLen; 1388*afc0c182SBogdan Liulko if (operation->block_size > 1) { 1389*afc0c182SBogdan Liulko res = tee_buffer_update(operation, utee_authenc_update_payload, 1390*afc0c182SBogdan Liulko srcData, srcLen, destData, &dl); 1391*afc0c182SBogdan Liulko } else { 1392*afc0c182SBogdan Liulko if (srcLen > 0) { 1393*afc0c182SBogdan Liulko res = utee_authenc_update_payload(operation->state, 1394*afc0c182SBogdan Liulko srcData, srcLen, 1395*afc0c182SBogdan Liulko destData, &dl); 1396*afc0c182SBogdan Liulko } else { 1397*afc0c182SBogdan Liulko dl = 0; 1398*afc0c182SBogdan Liulko res = TEE_SUCCESS; 1399*afc0c182SBogdan Liulko } 1400*afc0c182SBogdan Liulko } 1401*afc0c182SBogdan Liulko if (res != TEE_SUCCESS) 1402*afc0c182SBogdan Liulko goto out; 1403*afc0c182SBogdan Liulko 1404e86f1266SJens Wiklander *destLen = dl; 1405b0104773SPascal Brand 1406642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1407642a1607SCedric Chaumont 1408b5816c88SCedric Chaumont out: 1409b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1410b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1411b5816c88SCedric Chaumont TEE_Panic(res); 1412b5816c88SCedric Chaumont 1413b5816c88SCedric Chaumont return res; 1414b0104773SPascal Brand } 1415b0104773SPascal Brand 1416b5816c88SCedric Chaumont TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation, 14178f07fe6fSJerome Forissier const void *srcData, uint32_t srcLen, 141879a3c601SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 141979a3c601SCedric Chaumont uint32_t *tagLen) 1420b0104773SPascal Brand { 1421b0104773SPascal Brand TEE_Result res; 1422b0104773SPascal Brand uint8_t *dst = destData; 1423b0104773SPascal Brand size_t acc_dlen = 0; 1424e86f1266SJens Wiklander uint64_t tmp_dlen; 1425b0104773SPascal Brand size_t req_dlen; 1426e86f1266SJens Wiklander uint64_t tl; 1427b0104773SPascal Brand 1428b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1429b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1430b5816c88SCedric Chaumont destLen == NULL || 1431b5816c88SCedric Chaumont (destData == NULL && *destLen != 0) || 1432b5816c88SCedric Chaumont tag == NULL || tagLen == NULL) { 1433b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1434b5816c88SCedric Chaumont goto out; 1435b5816c88SCedric Chaumont } 1436b5816c88SCedric Chaumont 1437b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1438b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1439b5816c88SCedric Chaumont goto out; 1440b5816c88SCedric Chaumont } 1441b5816c88SCedric Chaumont 1442b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1443b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1444b5816c88SCedric Chaumont goto out; 1445b5816c88SCedric Chaumont } 1446b0104773SPascal Brand 1447b0104773SPascal Brand /* 1448b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1449b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1450b0104773SPascal Brand * can't restore sync with this API. 1451b0104773SPascal Brand */ 1452b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1453b0104773SPascal Brand if (*destLen < req_dlen) { 1454b0104773SPascal Brand *destLen = req_dlen; 1455b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1456b5816c88SCedric Chaumont goto out; 1457b0104773SPascal Brand } 1458b0104773SPascal Brand 1459b0104773SPascal Brand /* 1460b0104773SPascal Brand * Need to check this before update_payload since sync would be lost if 1461b0104773SPascal Brand * we return short buffer after that. 1462b0104773SPascal Brand */ 1463b5816c88SCedric Chaumont if (*tagLen < operation->ae_tag_len) { 1464b5816c88SCedric Chaumont *tagLen = operation->ae_tag_len; 1465b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1466b5816c88SCedric Chaumont goto out; 1467b0104773SPascal Brand } 1468b0104773SPascal Brand 1469*afc0c182SBogdan Liulko tl = *tagLen; 1470b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1471*afc0c182SBogdan Liulko if (operation->block_size > 1) { 1472*afc0c182SBogdan Liulko res = tee_buffer_update(operation, utee_authenc_update_payload, 1473*afc0c182SBogdan Liulko srcData, srcLen, dst, &tmp_dlen); 1474b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1475b5816c88SCedric Chaumont goto out; 1476b5816c88SCedric Chaumont 1477b0104773SPascal Brand dst += tmp_dlen; 1478b0104773SPascal Brand acc_dlen += tmp_dlen; 1479b0104773SPascal Brand 1480b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1481*afc0c182SBogdan Liulko res = utee_authenc_enc_final(operation->state, 1482*afc0c182SBogdan Liulko operation->buffer, 1483*afc0c182SBogdan Liulko operation->buffer_offs, dst, 1484*afc0c182SBogdan Liulko &tmp_dlen, tag, &tl); 1485*afc0c182SBogdan Liulko } else { 1486*afc0c182SBogdan Liulko res = utee_authenc_enc_final(operation->state, srcData, 1487*afc0c182SBogdan Liulko srcLen, dst, &tmp_dlen, 1488e86f1266SJens Wiklander tag, &tl); 1489*afc0c182SBogdan Liulko } 1490e86f1266SJens Wiklander *tagLen = tl; 1491b0104773SPascal Brand if (res != TEE_SUCCESS) 1492b5816c88SCedric Chaumont goto out; 1493b0104773SPascal Brand 1494b5816c88SCedric Chaumont acc_dlen += tmp_dlen; 1495b0104773SPascal Brand *destLen = acc_dlen; 1496642a1607SCedric Chaumont 1497b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1498b5816c88SCedric Chaumont 1499642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1500642a1607SCedric Chaumont 1501b5816c88SCedric Chaumont out: 1502b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1503b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1504b5816c88SCedric Chaumont TEE_Panic(res); 1505b0104773SPascal Brand 1506b0104773SPascal Brand return res; 1507b0104773SPascal Brand } 1508b0104773SPascal Brand 1509b5816c88SCedric Chaumont TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation, 15108f07fe6fSJerome Forissier const void *srcData, uint32_t srcLen, 1511b5816c88SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 151279a3c601SCedric Chaumont uint32_t tagLen) 1513b0104773SPascal Brand { 1514b0104773SPascal Brand TEE_Result res; 1515b0104773SPascal Brand uint8_t *dst = destData; 1516b0104773SPascal Brand size_t acc_dlen = 0; 1517e86f1266SJens Wiklander uint64_t tmp_dlen; 1518b0104773SPascal Brand size_t req_dlen; 1519b0104773SPascal Brand 1520b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1521b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1522b5816c88SCedric Chaumont destLen == NULL || 1523b5816c88SCedric Chaumont (destData == NULL && *destLen != 0) || 1524b5816c88SCedric Chaumont (tag == NULL && tagLen != 0)) { 1525b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1526b5816c88SCedric Chaumont goto out; 1527b5816c88SCedric Chaumont } 1528b5816c88SCedric Chaumont 1529b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1530b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1531b5816c88SCedric Chaumont goto out; 1532b5816c88SCedric Chaumont } 1533b5816c88SCedric Chaumont 1534b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1535b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1536b5816c88SCedric Chaumont goto out; 1537b5816c88SCedric Chaumont } 1538b0104773SPascal Brand 1539b0104773SPascal Brand /* 1540b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1541b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1542b0104773SPascal Brand * can't restore sync with this API. 1543b0104773SPascal Brand */ 1544b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1545b0104773SPascal Brand if (*destLen < req_dlen) { 1546b0104773SPascal Brand *destLen = req_dlen; 1547b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1548b5816c88SCedric Chaumont goto out; 1549b0104773SPascal Brand } 1550b0104773SPascal Brand 1551b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1552*afc0c182SBogdan Liulko if (operation->block_size > 1) { 1553*afc0c182SBogdan Liulko res = tee_buffer_update(operation, utee_authenc_update_payload, 1554*afc0c182SBogdan Liulko srcData, srcLen, dst, &tmp_dlen); 1555b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1556b5816c88SCedric Chaumont goto out; 1557b5816c88SCedric Chaumont 1558b0104773SPascal Brand dst += tmp_dlen; 1559b0104773SPascal Brand acc_dlen += tmp_dlen; 1560b0104773SPascal Brand 1561b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1562*afc0c182SBogdan Liulko res = utee_authenc_dec_final(operation->state, 1563*afc0c182SBogdan Liulko operation->buffer, 1564*afc0c182SBogdan Liulko operation->buffer_offs, dst, 1565*afc0c182SBogdan Liulko &tmp_dlen, tag, tagLen); 1566*afc0c182SBogdan Liulko } else { 1567*afc0c182SBogdan Liulko res = utee_authenc_dec_final(operation->state, srcData, 1568*afc0c182SBogdan Liulko srcLen, dst, &tmp_dlen, 1569b5816c88SCedric Chaumont tag, tagLen); 1570*afc0c182SBogdan Liulko } 1571b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1572b5816c88SCedric Chaumont goto out; 1573b5816c88SCedric Chaumont 1574b0104773SPascal Brand /* Supplied tagLen should match what we initiated with */ 1575b5816c88SCedric Chaumont if (tagLen != operation->ae_tag_len) 1576b0104773SPascal Brand res = TEE_ERROR_MAC_INVALID; 1577b0104773SPascal Brand 1578b0104773SPascal Brand acc_dlen += tmp_dlen; 1579b0104773SPascal Brand *destLen = acc_dlen; 1580642a1607SCedric Chaumont 1581b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1582b5816c88SCedric Chaumont 1583642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1584642a1607SCedric Chaumont 1585b5816c88SCedric Chaumont out: 1586b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1587b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER && 1588b5816c88SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 1589b5816c88SCedric Chaumont TEE_Panic(res); 1590b0104773SPascal Brand 1591b0104773SPascal Brand return res; 1592b0104773SPascal Brand } 1593b0104773SPascal Brand 1594b0104773SPascal Brand /* Cryptographic Operations API - Asymmetric Functions */ 1595b0104773SPascal Brand 159612e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation, 15978f07fe6fSJerome Forissier const TEE_Attribute *params, 15988f07fe6fSJerome Forissier uint32_t paramCount, const void *srcData, 159979a3c601SCedric Chaumont uint32_t srcLen, void *destData, 160079a3c601SCedric Chaumont uint32_t *destLen) 1601b0104773SPascal Brand { 1602b0104773SPascal Brand TEE_Result res; 1603e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1604e86f1266SJens Wiklander uint64_t dl; 1605b0104773SPascal Brand 160612e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1607b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 1608b0104773SPascal Brand TEE_Panic(0); 160912e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1610b0104773SPascal Brand TEE_Panic(0); 161112e66b6fSCedric Chaumont if (!operation->key1) 1612b0104773SPascal Brand TEE_Panic(0); 161312e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 161412e66b6fSCedric Chaumont TEE_Panic(0); 161512e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_ENCRYPT) 1616b0104773SPascal Brand TEE_Panic(0); 1617b0104773SPascal Brand 1618e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1619e86f1266SJens Wiklander dl = *destLen; 1620e86f1266SJens Wiklander res = utee_asymm_operate(operation->state, ua, paramCount, srcData, 1621e86f1266SJens Wiklander srcLen, destData, &dl); 1622e86f1266SJens Wiklander *destLen = dl; 162312e66b6fSCedric Chaumont 16248844ebfcSPascal Brand if (res != TEE_SUCCESS && 16258844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 16268844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1627b0104773SPascal Brand TEE_Panic(res); 162812e66b6fSCedric Chaumont 1629b0104773SPascal Brand return res; 1630b0104773SPascal Brand } 1631b0104773SPascal Brand 163212e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation, 16338f07fe6fSJerome Forissier const TEE_Attribute *params, 16348f07fe6fSJerome Forissier uint32_t paramCount, const void *srcData, 163579a3c601SCedric Chaumont uint32_t srcLen, void *destData, 163679a3c601SCedric Chaumont uint32_t *destLen) 1637b0104773SPascal Brand { 1638b0104773SPascal Brand TEE_Result res; 1639e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1640e86f1266SJens Wiklander uint64_t dl; 1641b0104773SPascal Brand 164212e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1643b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 1644b0104773SPascal Brand TEE_Panic(0); 164512e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1646b0104773SPascal Brand TEE_Panic(0); 164712e66b6fSCedric Chaumont if (!operation->key1) 1648b0104773SPascal Brand TEE_Panic(0); 164912e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 165012e66b6fSCedric Chaumont TEE_Panic(0); 165112e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_DECRYPT) 1652b0104773SPascal Brand TEE_Panic(0); 1653b0104773SPascal Brand 1654e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1655e86f1266SJens Wiklander dl = *destLen; 1656e86f1266SJens Wiklander res = utee_asymm_operate(operation->state, ua, paramCount, srcData, 1657e86f1266SJens Wiklander srcLen, destData, &dl); 1658e86f1266SJens Wiklander *destLen = dl; 165912e66b6fSCedric Chaumont 16608844ebfcSPascal Brand if (res != TEE_SUCCESS && 16618844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 16628844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1663b0104773SPascal Brand TEE_Panic(res); 166412e66b6fSCedric Chaumont 1665b0104773SPascal Brand return res; 1666b0104773SPascal Brand } 1667b0104773SPascal Brand 166812e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation, 16698f07fe6fSJerome Forissier const TEE_Attribute *params, 16708f07fe6fSJerome Forissier uint32_t paramCount, const void *digest, 167179a3c601SCedric Chaumont uint32_t digestLen, void *signature, 167279a3c601SCedric Chaumont uint32_t *signatureLen) 1673b0104773SPascal Brand { 1674b0104773SPascal Brand TEE_Result res; 1675e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1676e86f1266SJens Wiklander uint64_t sl; 1677b0104773SPascal Brand 167812e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || 167912e66b6fSCedric Chaumont (digest == NULL && digestLen != 0) || 1680b0104773SPascal Brand signature == NULL || signatureLen == NULL) 1681b0104773SPascal Brand TEE_Panic(0); 168212e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1683b0104773SPascal Brand TEE_Panic(0); 168412e66b6fSCedric Chaumont if (!operation->key1) 1685b0104773SPascal Brand TEE_Panic(0); 168612e66b6fSCedric Chaumont if (operation->info.operationClass != 168712e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 168812e66b6fSCedric Chaumont TEE_Panic(0); 168912e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_SIGN) 1690b0104773SPascal Brand TEE_Panic(0); 1691b0104773SPascal Brand 1692e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1693e86f1266SJens Wiklander sl = *signatureLen; 1694e86f1266SJens Wiklander res = utee_asymm_operate(operation->state, ua, paramCount, digest, 1695e86f1266SJens Wiklander digestLen, signature, &sl); 1696e86f1266SJens Wiklander *signatureLen = sl; 169712e66b6fSCedric Chaumont 1698b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER) 1699b0104773SPascal Brand TEE_Panic(res); 170012e66b6fSCedric Chaumont 1701b0104773SPascal Brand return res; 1702b0104773SPascal Brand } 1703b0104773SPascal Brand 170412e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation, 17058f07fe6fSJerome Forissier const TEE_Attribute *params, 17068f07fe6fSJerome Forissier uint32_t paramCount, const void *digest, 17078f07fe6fSJerome Forissier uint32_t digestLen, 17088f07fe6fSJerome Forissier const void *signature, 170979a3c601SCedric Chaumont uint32_t signatureLen) 1710b0104773SPascal Brand { 1711b0104773SPascal Brand TEE_Result res; 1712e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1713b0104773SPascal Brand 171412e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || 171512e66b6fSCedric Chaumont (digest == NULL && digestLen != 0) || 1716b0104773SPascal Brand (signature == NULL && signatureLen != 0)) 1717b0104773SPascal Brand TEE_Panic(0); 171812e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1719b0104773SPascal Brand TEE_Panic(0); 172012e66b6fSCedric Chaumont if (!operation->key1) 1721b0104773SPascal Brand TEE_Panic(0); 172212e66b6fSCedric Chaumont if (operation->info.operationClass != 172312e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 172412e66b6fSCedric Chaumont TEE_Panic(0); 172512e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_VERIFY) 1726b0104773SPascal Brand TEE_Panic(0); 1727b0104773SPascal Brand 1728e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1729e86f1266SJens Wiklander res = utee_asymm_verify(operation->state, ua, paramCount, digest, 173012e66b6fSCedric Chaumont digestLen, signature, signatureLen); 173112e66b6fSCedric Chaumont 1732b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID) 1733b0104773SPascal Brand TEE_Panic(res); 173412e66b6fSCedric Chaumont 1735b0104773SPascal Brand return res; 1736b0104773SPascal Brand } 1737b0104773SPascal Brand 1738b0104773SPascal Brand /* Cryptographic Operations API - Key Derivation Functions */ 1739b0104773SPascal Brand 1740b0104773SPascal Brand void TEE_DeriveKey(TEE_OperationHandle operation, 1741b0104773SPascal Brand const TEE_Attribute *params, uint32_t paramCount, 1742b0104773SPascal Brand TEE_ObjectHandle derivedKey) 1743b0104773SPascal Brand { 1744b0104773SPascal Brand TEE_Result res; 1745b0104773SPascal Brand TEE_ObjectInfo key_info; 1746e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1747b0104773SPascal Brand 1748b0104773SPascal Brand if (operation == TEE_HANDLE_NULL || derivedKey == 0) 1749b0104773SPascal Brand TEE_Panic(0); 175084fa9467SCedric Chaumont if (params == NULL && paramCount != 0) 1751b0104773SPascal Brand TEE_Panic(0); 17528854d3c6SJerome Forissier if (TEE_ALG_GET_CLASS(operation->info.algorithm) != 17538854d3c6SJerome Forissier TEE_OPERATION_KEY_DERIVATION) 1754b0104773SPascal Brand TEE_Panic(0); 1755b0104773SPascal Brand 1756b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION) 1757b0104773SPascal Brand TEE_Panic(0); 175884fa9467SCedric Chaumont if (!operation->key1) 175984fa9467SCedric Chaumont TEE_Panic(0); 1760b0104773SPascal Brand if (operation->info.mode != TEE_MODE_DERIVE) 1761b0104773SPascal Brand TEE_Panic(0); 1762b0104773SPascal Brand if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0) 1763b0104773SPascal Brand TEE_Panic(0); 1764b0104773SPascal Brand 1765e86f1266SJens Wiklander res = utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info); 1766b0104773SPascal Brand if (res != TEE_SUCCESS) 1767b36311adSJerome Forissier TEE_Panic(res); 1768b0104773SPascal Brand 1769b0104773SPascal Brand if (key_info.objectType != TEE_TYPE_GENERIC_SECRET) 1770b0104773SPascal Brand TEE_Panic(0); 1771b0104773SPascal Brand if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1772b0104773SPascal Brand TEE_Panic(0); 1773b0104773SPascal Brand 1774e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1775e86f1266SJens Wiklander res = utee_cryp_derive_key(operation->state, ua, paramCount, 1776e86f1266SJens Wiklander (unsigned long)derivedKey); 1777b0104773SPascal Brand if (res != TEE_SUCCESS) 1778b0104773SPascal Brand TEE_Panic(res); 1779b0104773SPascal Brand } 1780b0104773SPascal Brand 1781b0104773SPascal Brand /* Cryptographic Operations API - Random Number Generation Functions */ 1782b0104773SPascal Brand 178379a3c601SCedric Chaumont void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen) 1784b0104773SPascal Brand { 1785b0104773SPascal Brand TEE_Result res; 1786b0104773SPascal Brand 1787b0104773SPascal Brand res = utee_cryp_random_number_generate(randomBuffer, randomBufferLen); 1788b0104773SPascal Brand if (res != TEE_SUCCESS) 1789b0104773SPascal Brand TEE_Panic(res); 1790b0104773SPascal Brand } 1791