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_CCM: 138b0104773SPascal Brand case TEE_ALG_DES_ECB_NOPAD: 139b0104773SPascal Brand case TEE_ALG_DES_CBC_NOPAD: 140b0104773SPascal Brand case TEE_ALG_DES3_ECB_NOPAD: 141b0104773SPascal Brand case TEE_ALG_DES3_CBC_NOPAD: 142b0104773SPascal Brand if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES) 143b0104773SPascal Brand block_size = TEE_AES_BLOCK_SIZE; 144b0104773SPascal Brand else 145b0104773SPascal Brand block_size = TEE_DES_BLOCK_SIZE; 146afc0c182SBogdan Liulko /* FALLTHROUGH */ 147*57aabac5SBogdan Liulko case TEE_ALG_AES_CTR: 148afc0c182SBogdan 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 */ 996*57aabac5SBogdan Liulko if (operation->block_size > 1) { 997*57aabac5SBogdan Liulko req_dlen = ((operation->buffer_offs + srcLen) / 998*57aabac5SBogdan Liulko operation->block_size) * operation->block_size; 999*57aabac5SBogdan Liulko } else { 1000*57aabac5SBogdan Liulko req_dlen = srcLen; 1001*57aabac5SBogdan Liulko } 1002642a1607SCedric Chaumont if (operation->buffer_two_blocks) { 1003642a1607SCedric Chaumont if (req_dlen > operation->block_size * 2) 1004642a1607SCedric Chaumont req_dlen -= operation->block_size * 2; 1005b0104773SPascal Brand else 1006b0104773SPascal Brand req_dlen = 0; 1007b0104773SPascal Brand } 1008b0104773SPascal Brand /* 1009b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1010b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1011b0104773SPascal Brand * can't restore sync with this API. 1012b0104773SPascal Brand */ 1013b0104773SPascal Brand if (*destLen < req_dlen) { 1014b0104773SPascal Brand *destLen = req_dlen; 1015dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1016dea1f2b6SCedric Chaumont goto out; 1017b0104773SPascal Brand } 1018b0104773SPascal Brand 1019e86f1266SJens Wiklander dl = *destLen; 1020*57aabac5SBogdan Liulko if (operation->block_size > 1) { 1021*57aabac5SBogdan Liulko res = tee_buffer_update(operation, utee_cipher_update, srcData, 1022*57aabac5SBogdan Liulko srcLen, destData, &dl); 1023*57aabac5SBogdan Liulko } else { 1024*57aabac5SBogdan Liulko if (srcLen > 0) { 1025*57aabac5SBogdan Liulko res = utee_cipher_update(operation->state, srcData, 1026*57aabac5SBogdan Liulko srcLen, destData, &dl); 1027*57aabac5SBogdan Liulko } else { 1028*57aabac5SBogdan Liulko res = TEE_SUCCESS; 1029*57aabac5SBogdan Liulko dl = 0; 1030*57aabac5SBogdan Liulko } 1031*57aabac5SBogdan Liulko } 1032e86f1266SJens Wiklander *destLen = dl; 1033b0104773SPascal Brand 1034dea1f2b6SCedric Chaumont out: 1035dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 1036dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1037b36311adSJerome Forissier TEE_Panic(res); 1038dea1f2b6SCedric Chaumont 1039dea1f2b6SCedric Chaumont return res; 1040b0104773SPascal Brand } 1041b0104773SPascal Brand 1042642a1607SCedric Chaumont TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation, 10438f07fe6fSJerome Forissier const void *srcData, uint32_t srcLen, 10448f07fe6fSJerome Forissier void *destData, uint32_t *destLen) 1045b0104773SPascal Brand { 1046b0104773SPascal Brand TEE_Result res; 1047b0104773SPascal Brand uint8_t *dst = destData; 1048b0104773SPascal Brand size_t acc_dlen = 0; 1049e86f1266SJens Wiklander uint64_t tmp_dlen; 1050b0104773SPascal Brand size_t req_dlen; 1051b0104773SPascal Brand 1052642a1607SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1053dea1f2b6SCedric Chaumont (srcData == NULL && srcLen != 0) || 1054dea1f2b6SCedric Chaumont destLen == NULL || 1055dea1f2b6SCedric Chaumont (destData == NULL && *destLen != 0)) { 1056dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1057dea1f2b6SCedric Chaumont goto out; 1058dea1f2b6SCedric Chaumont } 1059dea1f2b6SCedric Chaumont 1060642a1607SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 1061dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1062dea1f2b6SCedric Chaumont goto out; 1063dea1f2b6SCedric Chaumont } 1064dea1f2b6SCedric Chaumont 1065642a1607SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1066642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1067642a1607SCedric Chaumont goto out; 1068642a1607SCedric Chaumont } 1069642a1607SCedric Chaumont 1070642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1071dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1072dea1f2b6SCedric Chaumont goto out; 1073dea1f2b6SCedric Chaumont } 1074b0104773SPascal Brand 1075b0104773SPascal Brand /* 1076b0104773SPascal Brand * Check that the final block doesn't require padding for those 1077b0104773SPascal Brand * algorithms that requires client to supply padding. 1078b0104773SPascal Brand */ 1079642a1607SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD || 1080642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD || 1081642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD || 1082642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD || 1083642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || 1084642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD) { 1085642a1607SCedric Chaumont if (((operation->buffer_offs + srcLen) % operation->block_size) 1086642a1607SCedric Chaumont != 0) { 1087dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1088dea1f2b6SCedric Chaumont goto out; 1089dea1f2b6SCedric Chaumont } 1090b0104773SPascal Brand } 1091b0104773SPascal Brand 1092b0104773SPascal Brand /* 1093b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1094b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1095b0104773SPascal Brand * can't restore sync with this API. 1096b0104773SPascal Brand */ 1097*57aabac5SBogdan Liulko if (operation->block_size > 1) { 1098642a1607SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1099*57aabac5SBogdan Liulko } else { 1100*57aabac5SBogdan Liulko req_dlen = srcLen; 1101*57aabac5SBogdan Liulko } 1102b0104773SPascal Brand if (*destLen < req_dlen) { 1103b0104773SPascal Brand *destLen = req_dlen; 1104dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1105dea1f2b6SCedric Chaumont goto out; 1106b0104773SPascal Brand } 1107b0104773SPascal Brand 1108b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1109*57aabac5SBogdan Liulko if (operation->block_size > 1) { 1110*57aabac5SBogdan Liulko res = tee_buffer_update(operation, utee_cipher_update, 1111*57aabac5SBogdan Liulko srcData, srcLen, dst, &tmp_dlen); 1112dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS) 1113dea1f2b6SCedric Chaumont goto out; 1114dea1f2b6SCedric Chaumont 1115b0104773SPascal Brand dst += tmp_dlen; 1116b0104773SPascal Brand acc_dlen += tmp_dlen; 1117b0104773SPascal Brand 1118b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1119642a1607SCedric Chaumont res = utee_cipher_final(operation->state, operation->buffer, 1120642a1607SCedric Chaumont operation->buffer_offs, dst, &tmp_dlen); 1121*57aabac5SBogdan Liulko } else { 1122*57aabac5SBogdan Liulko res = utee_cipher_final(operation->state, srcData, 1123*57aabac5SBogdan Liulko srcLen, dst, &tmp_dlen); 1124*57aabac5SBogdan Liulko } 1125b0104773SPascal Brand if (res != TEE_SUCCESS) 1126dea1f2b6SCedric Chaumont goto out; 1127dea1f2b6SCedric Chaumont 1128b0104773SPascal Brand acc_dlen += tmp_dlen; 1129b0104773SPascal Brand *destLen = acc_dlen; 1130dea1f2b6SCedric Chaumont 1131642a1607SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1132642a1607SCedric Chaumont 1133642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1134642a1607SCedric Chaumont 1135dea1f2b6SCedric Chaumont out: 1136dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 1137dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1138b36311adSJerome Forissier TEE_Panic(res); 1139dea1f2b6SCedric Chaumont 1140dea1f2b6SCedric Chaumont return res; 1141b0104773SPascal Brand } 1142b0104773SPascal Brand 1143b0104773SPascal Brand /* Cryptographic Operations API - MAC Functions */ 1144b0104773SPascal Brand 11458f07fe6fSJerome Forissier void TEE_MACInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen) 1146b0104773SPascal Brand { 1147b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 1148b0104773SPascal Brand TEE_Panic(0); 1149642a1607SCedric Chaumont 1150b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_MAC) 1151b0104773SPascal Brand TEE_Panic(0); 1152642a1607SCedric Chaumont 1153642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 1154642a1607SCedric Chaumont !(operation->key1)) 1155642a1607SCedric Chaumont TEE_Panic(0); 1156642a1607SCedric Chaumont 1157642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 1158642a1607SCedric Chaumont TEE_ResetOperation(operation); 1159642a1607SCedric Chaumont 1160642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1161642a1607SCedric Chaumont 11626d15db08SJerome Forissier init_hash_operation(operation, IV, IVLen); 1163b0104773SPascal Brand } 1164b0104773SPascal Brand 11658f07fe6fSJerome Forissier void TEE_MACUpdate(TEE_OperationHandle operation, const void *chunk, 116628e0efc6SCedric Chaumont uint32_t chunkSize) 1167b0104773SPascal Brand { 1168b0104773SPascal Brand TEE_Result res; 1169b0104773SPascal Brand 117028e0efc6SCedric Chaumont if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0)) 1171b0104773SPascal Brand TEE_Panic(0); 1172642a1607SCedric Chaumont 117328e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) 1174b0104773SPascal Brand TEE_Panic(0); 1175642a1607SCedric Chaumont 117628e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1177b0104773SPascal Brand TEE_Panic(0); 1178b0104773SPascal Brand 1179642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) 1180642a1607SCedric Chaumont TEE_Panic(0); 1181642a1607SCedric Chaumont 118228e0efc6SCedric Chaumont res = utee_hash_update(operation->state, chunk, chunkSize); 1183b0104773SPascal Brand if (res != TEE_SUCCESS) 1184b0104773SPascal Brand TEE_Panic(res); 1185b0104773SPascal Brand } 1186b0104773SPascal Brand 118728e0efc6SCedric Chaumont TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation, 11888f07fe6fSJerome Forissier const void *message, uint32_t messageLen, 118979a3c601SCedric Chaumont void *mac, uint32_t *macLen) 1190b0104773SPascal Brand { 1191b0104773SPascal Brand TEE_Result res; 1192e86f1266SJens Wiklander uint64_t ml; 1193b0104773SPascal Brand 119428e0efc6SCedric Chaumont if (operation == TEE_HANDLE_NULL || 119528e0efc6SCedric Chaumont (message == NULL && messageLen != 0) || 119628e0efc6SCedric Chaumont mac == NULL || 119728e0efc6SCedric Chaumont macLen == NULL) { 119828e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 119928e0efc6SCedric Chaumont goto out; 120028e0efc6SCedric Chaumont } 1201b0104773SPascal Brand 120228e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 120328e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 120428e0efc6SCedric Chaumont goto out; 120528e0efc6SCedric Chaumont } 120628e0efc6SCedric Chaumont 120728e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 120828e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 120928e0efc6SCedric Chaumont goto out; 121028e0efc6SCedric Chaumont } 121128e0efc6SCedric Chaumont 1212642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1213642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1214642a1607SCedric Chaumont goto out; 1215642a1607SCedric Chaumont } 1216642a1607SCedric Chaumont 1217e86f1266SJens Wiklander ml = *macLen; 1218e86f1266SJens Wiklander res = utee_hash_final(operation->state, message, messageLen, mac, &ml); 1219e86f1266SJens Wiklander *macLen = ml; 122028e0efc6SCedric Chaumont if (res != TEE_SUCCESS) 122128e0efc6SCedric Chaumont goto out; 122228e0efc6SCedric Chaumont 122328e0efc6SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 122428e0efc6SCedric Chaumont 1225642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1226642a1607SCedric Chaumont 122728e0efc6SCedric Chaumont out: 122828e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 122928e0efc6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 123028e0efc6SCedric Chaumont TEE_Panic(res); 123128e0efc6SCedric Chaumont 1232b0104773SPascal Brand return res; 1233b0104773SPascal Brand } 1234b0104773SPascal Brand 1235b0104773SPascal Brand TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation, 12368f07fe6fSJerome Forissier const void *message, uint32_t messageLen, 12378f07fe6fSJerome Forissier const void *mac, uint32_t macLen) 1238b0104773SPascal Brand { 1239b0104773SPascal Brand TEE_Result res; 1240b0104773SPascal Brand uint8_t computed_mac[TEE_MAX_HASH_SIZE]; 12417f74c64aSPascal Brand uint32_t computed_mac_size = TEE_MAX_HASH_SIZE; 1242b0104773SPascal Brand 124328e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 124428e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 124528e0efc6SCedric Chaumont goto out; 124628e0efc6SCedric Chaumont } 124728e0efc6SCedric Chaumont 124828e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 124928e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 125028e0efc6SCedric Chaumont goto out; 125128e0efc6SCedric Chaumont } 125228e0efc6SCedric Chaumont 1253642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1254642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1255642a1607SCedric Chaumont goto out; 1256642a1607SCedric Chaumont } 1257642a1607SCedric Chaumont 1258b0104773SPascal Brand res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac, 1259b0104773SPascal Brand &computed_mac_size); 1260b0104773SPascal Brand if (res != TEE_SUCCESS) 126128e0efc6SCedric Chaumont goto out; 126228e0efc6SCedric Chaumont 126328e0efc6SCedric Chaumont if (computed_mac_size != macLen) { 126428e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 126528e0efc6SCedric Chaumont goto out; 126628e0efc6SCedric Chaumont } 126728e0efc6SCedric Chaumont 126828e0efc6SCedric Chaumont if (buf_compare_ct(mac, computed_mac, computed_mac_size) != 0) { 126928e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 127028e0efc6SCedric Chaumont goto out; 127128e0efc6SCedric Chaumont } 127228e0efc6SCedric Chaumont 1273642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1274642a1607SCedric Chaumont 127528e0efc6SCedric Chaumont out: 127628e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 127728e0efc6SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 127828e0efc6SCedric Chaumont TEE_Panic(res); 127928e0efc6SCedric Chaumont 1280b0104773SPascal Brand return res; 1281b0104773SPascal Brand } 1282b0104773SPascal Brand 1283b0104773SPascal Brand /* Cryptographic Operations API - Authenticated Encryption Functions */ 1284b0104773SPascal Brand 12858f07fe6fSJerome Forissier TEE_Result TEE_AEInit(TEE_OperationHandle operation, const void *nonce, 128679a3c601SCedric Chaumont uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen, 1287b0104773SPascal Brand uint32_t payloadLen) 1288b0104773SPascal Brand { 1289b0104773SPascal Brand TEE_Result res; 1290b0104773SPascal Brand 1291b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || nonce == NULL) { 1292b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1293b5816c88SCedric Chaumont goto out; 1294b5816c88SCedric Chaumont } 1295b5816c88SCedric Chaumont 1296b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1297b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1298b5816c88SCedric Chaumont goto out; 1299b5816c88SCedric Chaumont } 1300b0104773SPascal Brand 1301642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 1302642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1303642a1607SCedric Chaumont goto out; 1304642a1607SCedric Chaumont } 1305642a1607SCedric Chaumont 1306b0104773SPascal Brand /* 1307b0104773SPascal Brand * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core 1308b0104773SPascal Brand * in the implementation. But AES-GCM spec doesn't specify the tag len 1309b0104773SPascal Brand * according to the same principle so we have to check here instead to 1310b0104773SPascal Brand * be GP compliant. 1311b0104773SPascal Brand */ 1312b5816c88SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_GCM) { 1313b0104773SPascal Brand /* 1314b0104773SPascal Brand * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96 1315b0104773SPascal Brand */ 1316b5816c88SCedric Chaumont if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) { 1317b5816c88SCedric Chaumont res = TEE_ERROR_NOT_SUPPORTED; 1318b5816c88SCedric Chaumont goto out; 1319b5816c88SCedric Chaumont } 1320b0104773SPascal Brand } 1321b0104773SPascal Brand 1322b5816c88SCedric Chaumont res = utee_authenc_init(operation->state, nonce, nonceLen, 1323b5816c88SCedric Chaumont tagLen / 8, AADLen, payloadLen); 1324b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1325b5816c88SCedric Chaumont goto out; 1326b5816c88SCedric Chaumont 1327b5816c88SCedric Chaumont operation->ae_tag_len = tagLen / 8; 1328b5816c88SCedric Chaumont operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 1329b5816c88SCedric Chaumont 1330b5816c88SCedric Chaumont out: 1331b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1332b5816c88SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 1333b0104773SPascal Brand TEE_Panic(res); 1334b5816c88SCedric Chaumont 1335b0104773SPascal Brand return res; 1336b0104773SPascal Brand } 1337b0104773SPascal Brand 13388f07fe6fSJerome Forissier void TEE_AEUpdateAAD(TEE_OperationHandle operation, const void *AADdata, 133979a3c601SCedric Chaumont uint32_t AADdataLen) 1340b0104773SPascal Brand { 1341b0104773SPascal Brand TEE_Result res; 1342b0104773SPascal Brand 1343b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1344b5816c88SCedric Chaumont (AADdata == NULL && AADdataLen != 0)) 1345b0104773SPascal Brand TEE_Panic(0); 1346642a1607SCedric Chaumont 1347b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) 1348b0104773SPascal Brand TEE_Panic(0); 1349642a1607SCedric Chaumont 1350b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1351b0104773SPascal Brand TEE_Panic(0); 1352b0104773SPascal Brand 1353b5816c88SCedric Chaumont res = utee_authenc_update_aad(operation->state, AADdata, AADdataLen); 1354642a1607SCedric Chaumont 1355642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1356642a1607SCedric Chaumont 1357b0104773SPascal Brand if (res != TEE_SUCCESS) 1358b0104773SPascal Brand TEE_Panic(res); 1359b0104773SPascal Brand } 1360b0104773SPascal Brand 13618f07fe6fSJerome Forissier TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, const void *srcData, 136279a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 1363b0104773SPascal Brand { 1364b5816c88SCedric Chaumont TEE_Result res; 1365b0104773SPascal Brand size_t req_dlen; 1366e86f1266SJens Wiklander uint64_t dl; 1367b0104773SPascal Brand 1368b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1369b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1370b5816c88SCedric Chaumont destLen == NULL || 1371b5816c88SCedric Chaumont (destData == NULL && *destLen != 0)) { 1372b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1373b5816c88SCedric Chaumont goto out; 1374b5816c88SCedric Chaumont } 1375b5816c88SCedric Chaumont 1376b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1377b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1378b5816c88SCedric Chaumont goto out; 1379b5816c88SCedric Chaumont } 1380b5816c88SCedric Chaumont 1381b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1382b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1383b5816c88SCedric Chaumont goto out; 1384b5816c88SCedric Chaumont } 1385b0104773SPascal Brand 1386827308b8SJerome Forissier if (!srcData && !srcLen) { 1387090268f5SJens Wiklander *destLen = 0; 1388827308b8SJerome Forissier res = TEE_SUCCESS; 1389827308b8SJerome Forissier goto out; 1390827308b8SJerome Forissier } 1391827308b8SJerome Forissier 1392b0104773SPascal Brand /* 1393b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1394b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1395b0104773SPascal Brand * can't restore sync with this API. 1396b0104773SPascal Brand */ 1397afc0c182SBogdan Liulko if (operation->block_size > 1) { 1398b5816c88SCedric Chaumont req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen, 1399b5816c88SCedric Chaumont operation->block_size); 1400afc0c182SBogdan Liulko } else { 1401afc0c182SBogdan Liulko req_dlen = srcLen; 1402afc0c182SBogdan Liulko } 1403afc0c182SBogdan Liulko 1404b0104773SPascal Brand if (*destLen < req_dlen) { 1405b0104773SPascal Brand *destLen = req_dlen; 1406b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1407b5816c88SCedric Chaumont goto out; 1408b0104773SPascal Brand } 1409b0104773SPascal Brand 1410e86f1266SJens Wiklander dl = *destLen; 1411afc0c182SBogdan Liulko if (operation->block_size > 1) { 1412afc0c182SBogdan Liulko res = tee_buffer_update(operation, utee_authenc_update_payload, 1413afc0c182SBogdan Liulko srcData, srcLen, destData, &dl); 1414afc0c182SBogdan Liulko } else { 1415afc0c182SBogdan Liulko if (srcLen > 0) { 1416afc0c182SBogdan Liulko res = utee_authenc_update_payload(operation->state, 1417afc0c182SBogdan Liulko srcData, srcLen, 1418afc0c182SBogdan Liulko destData, &dl); 1419afc0c182SBogdan Liulko } else { 1420afc0c182SBogdan Liulko dl = 0; 1421afc0c182SBogdan Liulko res = TEE_SUCCESS; 1422afc0c182SBogdan Liulko } 1423afc0c182SBogdan Liulko } 1424afc0c182SBogdan Liulko if (res != TEE_SUCCESS) 1425afc0c182SBogdan Liulko goto out; 1426afc0c182SBogdan Liulko 1427e86f1266SJens Wiklander *destLen = dl; 1428b0104773SPascal Brand 1429642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1430642a1607SCedric Chaumont 1431b5816c88SCedric Chaumont out: 1432b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1433b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1434b5816c88SCedric Chaumont TEE_Panic(res); 1435b5816c88SCedric Chaumont 1436b5816c88SCedric Chaumont return res; 1437b0104773SPascal Brand } 1438b0104773SPascal Brand 1439b5816c88SCedric Chaumont TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation, 14408f07fe6fSJerome Forissier const void *srcData, uint32_t srcLen, 144179a3c601SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 144279a3c601SCedric Chaumont uint32_t *tagLen) 1443b0104773SPascal Brand { 1444b0104773SPascal Brand TEE_Result res; 1445b0104773SPascal Brand uint8_t *dst = destData; 1446b0104773SPascal Brand size_t acc_dlen = 0; 1447e86f1266SJens Wiklander uint64_t tmp_dlen; 1448b0104773SPascal Brand size_t req_dlen; 1449e86f1266SJens Wiklander uint64_t tl; 1450b0104773SPascal Brand 1451b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1452b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1453b5816c88SCedric Chaumont destLen == NULL || 1454b5816c88SCedric Chaumont (destData == NULL && *destLen != 0) || 1455b5816c88SCedric Chaumont tag == NULL || tagLen == NULL) { 1456b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1457b5816c88SCedric Chaumont goto out; 1458b5816c88SCedric Chaumont } 1459b5816c88SCedric Chaumont 1460b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1461b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1462b5816c88SCedric Chaumont goto out; 1463b5816c88SCedric Chaumont } 1464b5816c88SCedric Chaumont 1465b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1466b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1467b5816c88SCedric Chaumont goto out; 1468b5816c88SCedric Chaumont } 1469b0104773SPascal Brand 1470b0104773SPascal Brand /* 1471b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1472b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1473b0104773SPascal Brand * can't restore sync with this API. 1474b0104773SPascal Brand */ 1475b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1476b0104773SPascal Brand if (*destLen < req_dlen) { 1477b0104773SPascal Brand *destLen = req_dlen; 1478b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1479b5816c88SCedric Chaumont goto out; 1480b0104773SPascal Brand } 1481b0104773SPascal Brand 1482b0104773SPascal Brand /* 1483b0104773SPascal Brand * Need to check this before update_payload since sync would be lost if 1484b0104773SPascal Brand * we return short buffer after that. 1485b0104773SPascal Brand */ 1486b5816c88SCedric Chaumont if (*tagLen < operation->ae_tag_len) { 1487b5816c88SCedric Chaumont *tagLen = operation->ae_tag_len; 1488b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1489b5816c88SCedric Chaumont goto out; 1490b0104773SPascal Brand } 1491b0104773SPascal Brand 1492afc0c182SBogdan Liulko tl = *tagLen; 1493b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1494afc0c182SBogdan Liulko if (operation->block_size > 1) { 1495afc0c182SBogdan Liulko res = tee_buffer_update(operation, utee_authenc_update_payload, 1496afc0c182SBogdan Liulko srcData, srcLen, dst, &tmp_dlen); 1497b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1498b5816c88SCedric Chaumont goto out; 1499b5816c88SCedric Chaumont 1500b0104773SPascal Brand dst += tmp_dlen; 1501b0104773SPascal Brand acc_dlen += tmp_dlen; 1502b0104773SPascal Brand 1503b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1504afc0c182SBogdan Liulko res = utee_authenc_enc_final(operation->state, 1505afc0c182SBogdan Liulko operation->buffer, 1506afc0c182SBogdan Liulko operation->buffer_offs, dst, 1507afc0c182SBogdan Liulko &tmp_dlen, tag, &tl); 1508afc0c182SBogdan Liulko } else { 1509afc0c182SBogdan Liulko res = utee_authenc_enc_final(operation->state, srcData, 1510afc0c182SBogdan Liulko srcLen, dst, &tmp_dlen, 1511e86f1266SJens Wiklander tag, &tl); 1512afc0c182SBogdan Liulko } 1513e86f1266SJens Wiklander *tagLen = tl; 1514b0104773SPascal Brand if (res != TEE_SUCCESS) 1515b5816c88SCedric Chaumont goto out; 1516b0104773SPascal Brand 1517b5816c88SCedric Chaumont acc_dlen += tmp_dlen; 1518b0104773SPascal Brand *destLen = acc_dlen; 1519642a1607SCedric Chaumont 1520b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1521b5816c88SCedric Chaumont 1522642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1523642a1607SCedric Chaumont 1524b5816c88SCedric Chaumont out: 1525b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1526b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1527b5816c88SCedric Chaumont TEE_Panic(res); 1528b0104773SPascal Brand 1529b0104773SPascal Brand return res; 1530b0104773SPascal Brand } 1531b0104773SPascal Brand 1532b5816c88SCedric Chaumont TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation, 15338f07fe6fSJerome Forissier const void *srcData, uint32_t srcLen, 1534b5816c88SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 153579a3c601SCedric Chaumont uint32_t tagLen) 1536b0104773SPascal Brand { 1537b0104773SPascal Brand TEE_Result res; 1538b0104773SPascal Brand uint8_t *dst = destData; 1539b0104773SPascal Brand size_t acc_dlen = 0; 1540e86f1266SJens Wiklander uint64_t tmp_dlen; 1541b0104773SPascal Brand size_t req_dlen; 1542b0104773SPascal Brand 1543b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1544b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1545b5816c88SCedric Chaumont destLen == NULL || 1546b5816c88SCedric Chaumont (destData == NULL && *destLen != 0) || 1547b5816c88SCedric Chaumont (tag == NULL && tagLen != 0)) { 1548b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1549b5816c88SCedric Chaumont goto out; 1550b5816c88SCedric Chaumont } 1551b5816c88SCedric Chaumont 1552b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1553b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1554b5816c88SCedric Chaumont goto out; 1555b5816c88SCedric Chaumont } 1556b5816c88SCedric Chaumont 1557b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1558b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1559b5816c88SCedric Chaumont goto out; 1560b5816c88SCedric Chaumont } 1561b0104773SPascal Brand 1562b0104773SPascal Brand /* 1563b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1564b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1565b0104773SPascal Brand * can't restore sync with this API. 1566b0104773SPascal Brand */ 1567b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1568b0104773SPascal Brand if (*destLen < req_dlen) { 1569b0104773SPascal Brand *destLen = req_dlen; 1570b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1571b5816c88SCedric Chaumont goto out; 1572b0104773SPascal Brand } 1573b0104773SPascal Brand 1574b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1575afc0c182SBogdan Liulko if (operation->block_size > 1) { 1576afc0c182SBogdan Liulko res = tee_buffer_update(operation, utee_authenc_update_payload, 1577afc0c182SBogdan Liulko srcData, srcLen, dst, &tmp_dlen); 1578b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1579b5816c88SCedric Chaumont goto out; 1580b5816c88SCedric Chaumont 1581b0104773SPascal Brand dst += tmp_dlen; 1582b0104773SPascal Brand acc_dlen += tmp_dlen; 1583b0104773SPascal Brand 1584b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1585afc0c182SBogdan Liulko res = utee_authenc_dec_final(operation->state, 1586afc0c182SBogdan Liulko operation->buffer, 1587afc0c182SBogdan Liulko operation->buffer_offs, dst, 1588afc0c182SBogdan Liulko &tmp_dlen, tag, tagLen); 1589afc0c182SBogdan Liulko } else { 1590afc0c182SBogdan Liulko res = utee_authenc_dec_final(operation->state, srcData, 1591afc0c182SBogdan Liulko srcLen, dst, &tmp_dlen, 1592b5816c88SCedric Chaumont tag, tagLen); 1593afc0c182SBogdan Liulko } 1594b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1595b5816c88SCedric Chaumont goto out; 1596b5816c88SCedric Chaumont 1597b0104773SPascal Brand /* Supplied tagLen should match what we initiated with */ 1598b5816c88SCedric Chaumont if (tagLen != operation->ae_tag_len) 1599b0104773SPascal Brand res = TEE_ERROR_MAC_INVALID; 1600b0104773SPascal Brand 1601b0104773SPascal Brand acc_dlen += tmp_dlen; 1602b0104773SPascal Brand *destLen = acc_dlen; 1603642a1607SCedric Chaumont 1604b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1605b5816c88SCedric Chaumont 1606642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1607642a1607SCedric Chaumont 1608b5816c88SCedric Chaumont out: 1609b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1610b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER && 1611b5816c88SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 1612b5816c88SCedric Chaumont TEE_Panic(res); 1613b0104773SPascal Brand 1614b0104773SPascal Brand return res; 1615b0104773SPascal Brand } 1616b0104773SPascal Brand 1617b0104773SPascal Brand /* Cryptographic Operations API - Asymmetric Functions */ 1618b0104773SPascal Brand 161912e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation, 16208f07fe6fSJerome Forissier const TEE_Attribute *params, 16218f07fe6fSJerome Forissier uint32_t paramCount, const void *srcData, 162279a3c601SCedric Chaumont uint32_t srcLen, void *destData, 162379a3c601SCedric Chaumont uint32_t *destLen) 1624b0104773SPascal Brand { 1625b0104773SPascal Brand TEE_Result res; 1626e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1627e86f1266SJens Wiklander uint64_t dl; 1628b0104773SPascal Brand 162912e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1630b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 1631b0104773SPascal Brand TEE_Panic(0); 163212e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1633b0104773SPascal Brand TEE_Panic(0); 163412e66b6fSCedric Chaumont if (!operation->key1) 1635b0104773SPascal Brand TEE_Panic(0); 163612e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 163712e66b6fSCedric Chaumont TEE_Panic(0); 163812e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_ENCRYPT) 1639b0104773SPascal Brand TEE_Panic(0); 1640b0104773SPascal Brand 1641e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1642e86f1266SJens Wiklander dl = *destLen; 1643e86f1266SJens Wiklander res = utee_asymm_operate(operation->state, ua, paramCount, srcData, 1644e86f1266SJens Wiklander srcLen, destData, &dl); 1645e86f1266SJens Wiklander *destLen = dl; 164612e66b6fSCedric Chaumont 16478844ebfcSPascal Brand if (res != TEE_SUCCESS && 16488844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 16498844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1650b0104773SPascal Brand TEE_Panic(res); 165112e66b6fSCedric Chaumont 1652b0104773SPascal Brand return res; 1653b0104773SPascal Brand } 1654b0104773SPascal Brand 165512e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation, 16568f07fe6fSJerome Forissier const TEE_Attribute *params, 16578f07fe6fSJerome Forissier uint32_t paramCount, const void *srcData, 165879a3c601SCedric Chaumont uint32_t srcLen, void *destData, 165979a3c601SCedric Chaumont uint32_t *destLen) 1660b0104773SPascal Brand { 1661b0104773SPascal Brand TEE_Result res; 1662e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1663e86f1266SJens Wiklander uint64_t dl; 1664b0104773SPascal Brand 166512e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1666b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 1667b0104773SPascal Brand TEE_Panic(0); 166812e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1669b0104773SPascal Brand TEE_Panic(0); 167012e66b6fSCedric Chaumont if (!operation->key1) 1671b0104773SPascal Brand TEE_Panic(0); 167212e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 167312e66b6fSCedric Chaumont TEE_Panic(0); 167412e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_DECRYPT) 1675b0104773SPascal Brand TEE_Panic(0); 1676b0104773SPascal Brand 1677e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1678e86f1266SJens Wiklander dl = *destLen; 1679e86f1266SJens Wiklander res = utee_asymm_operate(operation->state, ua, paramCount, srcData, 1680e86f1266SJens Wiklander srcLen, destData, &dl); 1681e86f1266SJens Wiklander *destLen = dl; 168212e66b6fSCedric Chaumont 16838844ebfcSPascal Brand if (res != TEE_SUCCESS && 16848844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 16858844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1686b0104773SPascal Brand TEE_Panic(res); 168712e66b6fSCedric Chaumont 1688b0104773SPascal Brand return res; 1689b0104773SPascal Brand } 1690b0104773SPascal Brand 169112e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation, 16928f07fe6fSJerome Forissier const TEE_Attribute *params, 16938f07fe6fSJerome Forissier uint32_t paramCount, const void *digest, 169479a3c601SCedric Chaumont uint32_t digestLen, void *signature, 169579a3c601SCedric Chaumont uint32_t *signatureLen) 1696b0104773SPascal Brand { 1697b0104773SPascal Brand TEE_Result res; 1698e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1699e86f1266SJens Wiklander uint64_t sl; 1700b0104773SPascal Brand 170112e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || 170212e66b6fSCedric Chaumont (digest == NULL && digestLen != 0) || 1703b0104773SPascal Brand signature == NULL || signatureLen == NULL) 1704b0104773SPascal Brand TEE_Panic(0); 170512e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1706b0104773SPascal Brand TEE_Panic(0); 170712e66b6fSCedric Chaumont if (!operation->key1) 1708b0104773SPascal Brand TEE_Panic(0); 170912e66b6fSCedric Chaumont if (operation->info.operationClass != 171012e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 171112e66b6fSCedric Chaumont TEE_Panic(0); 171212e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_SIGN) 1713b0104773SPascal Brand TEE_Panic(0); 1714b0104773SPascal Brand 1715e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1716e86f1266SJens Wiklander sl = *signatureLen; 1717e86f1266SJens Wiklander res = utee_asymm_operate(operation->state, ua, paramCount, digest, 1718e86f1266SJens Wiklander digestLen, signature, &sl); 1719e86f1266SJens Wiklander *signatureLen = sl; 172012e66b6fSCedric Chaumont 1721b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER) 1722b0104773SPascal Brand TEE_Panic(res); 172312e66b6fSCedric Chaumont 1724b0104773SPascal Brand return res; 1725b0104773SPascal Brand } 1726b0104773SPascal Brand 172712e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation, 17288f07fe6fSJerome Forissier const TEE_Attribute *params, 17298f07fe6fSJerome Forissier uint32_t paramCount, const void *digest, 17308f07fe6fSJerome Forissier uint32_t digestLen, 17318f07fe6fSJerome Forissier const void *signature, 173279a3c601SCedric Chaumont uint32_t signatureLen) 1733b0104773SPascal Brand { 1734b0104773SPascal Brand TEE_Result res; 1735e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1736b0104773SPascal Brand 173712e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || 173812e66b6fSCedric Chaumont (digest == NULL && digestLen != 0) || 1739b0104773SPascal Brand (signature == NULL && signatureLen != 0)) 1740b0104773SPascal Brand TEE_Panic(0); 174112e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1742b0104773SPascal Brand TEE_Panic(0); 174312e66b6fSCedric Chaumont if (!operation->key1) 1744b0104773SPascal Brand TEE_Panic(0); 174512e66b6fSCedric Chaumont if (operation->info.operationClass != 174612e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 174712e66b6fSCedric Chaumont TEE_Panic(0); 174812e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_VERIFY) 1749b0104773SPascal Brand TEE_Panic(0); 1750b0104773SPascal Brand 1751e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1752e86f1266SJens Wiklander res = utee_asymm_verify(operation->state, ua, paramCount, digest, 175312e66b6fSCedric Chaumont digestLen, signature, signatureLen); 175412e66b6fSCedric Chaumont 1755b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID) 1756b0104773SPascal Brand TEE_Panic(res); 175712e66b6fSCedric Chaumont 1758b0104773SPascal Brand return res; 1759b0104773SPascal Brand } 1760b0104773SPascal Brand 1761b0104773SPascal Brand /* Cryptographic Operations API - Key Derivation Functions */ 1762b0104773SPascal Brand 1763b0104773SPascal Brand void TEE_DeriveKey(TEE_OperationHandle operation, 1764b0104773SPascal Brand const TEE_Attribute *params, uint32_t paramCount, 1765b0104773SPascal Brand TEE_ObjectHandle derivedKey) 1766b0104773SPascal Brand { 1767b0104773SPascal Brand TEE_Result res; 1768b0104773SPascal Brand TEE_ObjectInfo key_info; 1769e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1770b0104773SPascal Brand 1771b0104773SPascal Brand if (operation == TEE_HANDLE_NULL || derivedKey == 0) 1772b0104773SPascal Brand TEE_Panic(0); 177384fa9467SCedric Chaumont if (params == NULL && paramCount != 0) 1774b0104773SPascal Brand TEE_Panic(0); 17758854d3c6SJerome Forissier if (TEE_ALG_GET_CLASS(operation->info.algorithm) != 17768854d3c6SJerome Forissier TEE_OPERATION_KEY_DERIVATION) 1777b0104773SPascal Brand TEE_Panic(0); 1778b0104773SPascal Brand 1779b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION) 1780b0104773SPascal Brand TEE_Panic(0); 178184fa9467SCedric Chaumont if (!operation->key1) 178284fa9467SCedric Chaumont TEE_Panic(0); 1783b0104773SPascal Brand if (operation->info.mode != TEE_MODE_DERIVE) 1784b0104773SPascal Brand TEE_Panic(0); 1785b0104773SPascal Brand if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0) 1786b0104773SPascal Brand TEE_Panic(0); 1787b0104773SPascal Brand 1788e86f1266SJens Wiklander res = utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info); 1789b0104773SPascal Brand if (res != TEE_SUCCESS) 1790b36311adSJerome Forissier TEE_Panic(res); 1791b0104773SPascal Brand 1792b0104773SPascal Brand if (key_info.objectType != TEE_TYPE_GENERIC_SECRET) 1793b0104773SPascal Brand TEE_Panic(0); 1794b0104773SPascal Brand if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1795b0104773SPascal Brand TEE_Panic(0); 1796b0104773SPascal Brand 1797e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1798e86f1266SJens Wiklander res = utee_cryp_derive_key(operation->state, ua, paramCount, 1799e86f1266SJens Wiklander (unsigned long)derivedKey); 1800b0104773SPascal Brand if (res != TEE_SUCCESS) 1801b0104773SPascal Brand TEE_Panic(res); 1802b0104773SPascal Brand } 1803b0104773SPascal Brand 1804b0104773SPascal Brand /* Cryptographic Operations API - Random Number Generation Functions */ 1805b0104773SPascal Brand 180679a3c601SCedric Chaumont void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen) 1807b0104773SPascal Brand { 1808b0104773SPascal Brand TEE_Result res; 1809b0104773SPascal Brand 1810b0104773SPascal Brand res = utee_cryp_random_number_generate(randomBuffer, randomBufferLen); 1811b0104773SPascal Brand if (res != TEE_SUCCESS) 1812b0104773SPascal Brand TEE_Panic(res); 1813b0104773SPascal Brand } 1814