1b0104773SPascal Brand /* 2b0104773SPascal Brand * Copyright (c) 2014, STMicroelectronics International N.V. 3b0104773SPascal Brand * All rights reserved. 4b0104773SPascal Brand * 5b0104773SPascal Brand * Redistribution and use in source and binary forms, with or without 6b0104773SPascal Brand * modification, are permitted provided that the following conditions are met: 7b0104773SPascal Brand * 8b0104773SPascal Brand * 1. Redistributions of source code must retain the above copyright notice, 9b0104773SPascal Brand * this list of conditions and the following disclaimer. 10b0104773SPascal Brand * 11b0104773SPascal Brand * 2. Redistributions in binary form must reproduce the above copyright notice, 12b0104773SPascal Brand * this list of conditions and the following disclaimer in the documentation 13b0104773SPascal Brand * and/or other materials provided with the distribution. 14b0104773SPascal Brand * 15b0104773SPascal Brand * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16b0104773SPascal Brand * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17b0104773SPascal Brand * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18b0104773SPascal Brand * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19b0104773SPascal Brand * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20b0104773SPascal Brand * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21b0104773SPascal Brand * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22b0104773SPascal Brand * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23b0104773SPascal Brand * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24b0104773SPascal Brand * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25b0104773SPascal Brand * POSSIBILITY OF SUCH DAMAGE. 26b0104773SPascal Brand */ 27b0104773SPascal Brand #include <stdlib.h> 28b0104773SPascal Brand #include <string.h> 29b796ebf3SJerome Forissier #include <string_ext.h> 30b0104773SPascal Brand 31b0104773SPascal Brand #include <tee_api.h> 328854d3c6SJerome Forissier #include <tee_api_defines_extensions.h> 33b0104773SPascal Brand #include <tee_internal_api_extensions.h> 34b0104773SPascal Brand #include <utee_syscalls.h> 35b0104773SPascal Brand #include <utee_defines.h> 36fc26c92aSJens Wiklander #include <util.h> 37e86f1266SJens Wiklander #include "tee_api_private.h" 38b0104773SPascal Brand 39b0104773SPascal Brand struct __TEE_OperationHandle { 40b0104773SPascal Brand TEE_OperationInfo info; 41b0104773SPascal Brand TEE_ObjectHandle key1; 42b0104773SPascal Brand TEE_ObjectHandle key2; 43642a1607SCedric Chaumont uint32_t operationState;/* Operation state : INITIAL or ACTIVE */ 44b0104773SPascal Brand uint8_t *buffer; /* buffer to collect complete blocks */ 45b0104773SPascal Brand bool buffer_two_blocks; /* True if two blocks need to be buffered */ 46b0104773SPascal Brand size_t block_size; /* Block size of cipher */ 47b0104773SPascal Brand size_t buffer_offs; /* Offset in buffer */ 48b0104773SPascal Brand uint32_t state; /* Handle to state in TEE Core */ 49b0104773SPascal Brand uint32_t ae_tag_len; /* 50b0104773SPascal Brand * tag_len in bytes for AE operation else unused 51b0104773SPascal Brand */ 52b0104773SPascal Brand }; 53b0104773SPascal Brand 54b0104773SPascal Brand /* Cryptographic Operations API - Generic Operation Functions */ 55b0104773SPascal Brand 56b0104773SPascal Brand TEE_Result TEE_AllocateOperation(TEE_OperationHandle *operation, 57b0104773SPascal Brand uint32_t algorithm, uint32_t mode, 58b0104773SPascal Brand uint32_t maxKeySize) 59b0104773SPascal Brand { 60b0104773SPascal Brand TEE_Result res; 61b0104773SPascal Brand TEE_OperationHandle op = TEE_HANDLE_NULL; 62b0104773SPascal Brand uint32_t handle_state = 0; 63b0104773SPascal Brand size_t block_size = 1; 64b0104773SPascal Brand uint32_t req_key_usage; 65b0104773SPascal Brand bool with_private_key = false; 66b0104773SPascal Brand bool buffer_two_blocks = false; 67b0104773SPascal Brand 689b52c538SCedric Chaumont if (!operation) 69b0104773SPascal Brand TEE_Panic(0); 70b0104773SPascal Brand 71b0104773SPascal Brand if (algorithm == TEE_ALG_AES_XTS) 72b0104773SPascal Brand handle_state = TEE_HANDLE_FLAG_EXPECT_TWO_KEYS; 73b0104773SPascal Brand 74218d9055SCedric Chaumont /* Check algorithm max key size */ 75218d9055SCedric Chaumont switch (algorithm) { 76218d9055SCedric Chaumont case TEE_ALG_DSA_SHA1: 77218d9055SCedric Chaumont if (maxKeySize < 512) 78218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 79218d9055SCedric Chaumont if (maxKeySize > 1024) 80218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 81218d9055SCedric Chaumont if (maxKeySize % 64 != 0) 82218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 83218d9055SCedric Chaumont break; 84218d9055SCedric Chaumont 85218d9055SCedric Chaumont case TEE_ALG_DSA_SHA224: 86218d9055SCedric Chaumont if (maxKeySize != 2048) 87218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 88218d9055SCedric Chaumont break; 89218d9055SCedric Chaumont 90218d9055SCedric Chaumont case TEE_ALG_DSA_SHA256: 91218d9055SCedric Chaumont if (maxKeySize != 2048 && maxKeySize != 3072) 92218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 93218d9055SCedric Chaumont break; 94218d9055SCedric Chaumont 951220586eSCedric Chaumont case TEE_ALG_ECDSA_P192: 961220586eSCedric Chaumont case TEE_ALG_ECDH_P192: 971220586eSCedric Chaumont if (maxKeySize != 192) 981220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 991220586eSCedric Chaumont break; 1001220586eSCedric Chaumont 1011220586eSCedric Chaumont case TEE_ALG_ECDSA_P224: 1021220586eSCedric Chaumont case TEE_ALG_ECDH_P224: 1031220586eSCedric Chaumont if (maxKeySize != 224) 1041220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 1051220586eSCedric Chaumont break; 1061220586eSCedric Chaumont 1071220586eSCedric Chaumont case TEE_ALG_ECDSA_P256: 1081220586eSCedric Chaumont case TEE_ALG_ECDH_P256: 1091220586eSCedric Chaumont if (maxKeySize != 256) 1101220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 1111220586eSCedric Chaumont break; 1121220586eSCedric Chaumont 1131220586eSCedric Chaumont case TEE_ALG_ECDSA_P384: 1141220586eSCedric Chaumont case TEE_ALG_ECDH_P384: 1151220586eSCedric Chaumont if (maxKeySize != 384) 1161220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 1171220586eSCedric Chaumont break; 1181220586eSCedric Chaumont 1191220586eSCedric Chaumont case TEE_ALG_ECDSA_P521: 1201220586eSCedric Chaumont case TEE_ALG_ECDH_P521: 1211220586eSCedric Chaumont if (maxKeySize != 521) 1221220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 1231220586eSCedric Chaumont break; 1241220586eSCedric Chaumont 125218d9055SCedric Chaumont default: 126218d9055SCedric Chaumont break; 127218d9055SCedric Chaumont } 128218d9055SCedric Chaumont 129218d9055SCedric Chaumont /* Check algorithm mode */ 130b0104773SPascal Brand switch (algorithm) { 131b0104773SPascal Brand case TEE_ALG_AES_CTS: 132b0104773SPascal Brand case TEE_ALG_AES_XTS: 133b0104773SPascal Brand buffer_two_blocks = true; 1344bd53c54SJerome Forissier /* FALLTHROUGH */ 1354bd53c54SJerome Forissier case TEE_ALG_AES_ECB_NOPAD: 136b0104773SPascal Brand case TEE_ALG_AES_CBC_NOPAD: 137b0104773SPascal Brand case TEE_ALG_AES_CTR: 138b0104773SPascal Brand case TEE_ALG_AES_CCM: 139b0104773SPascal Brand case TEE_ALG_AES_GCM: 140b0104773SPascal Brand case TEE_ALG_DES_ECB_NOPAD: 141b0104773SPascal Brand case TEE_ALG_DES_CBC_NOPAD: 142b0104773SPascal Brand case TEE_ALG_DES3_ECB_NOPAD: 143b0104773SPascal Brand case TEE_ALG_DES3_CBC_NOPAD: 144b0104773SPascal Brand if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES) 145b0104773SPascal Brand block_size = TEE_AES_BLOCK_SIZE; 146b0104773SPascal Brand else 147b0104773SPascal Brand block_size = TEE_DES_BLOCK_SIZE; 148b0104773SPascal Brand 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) 3499b52c538SCedric Chaumont TEE_Panic(0); 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) 379e889e80bSCedric Chaumont TEE_Panic(0); 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) 48805304565SCedric Chaumont TEE_Panic(0); 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) 586a57c1e2eSCedric Chaumont TEE_Panic(0); 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) 711a57c1e2eSCedric Chaumont TEE_Panic(0); 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 7656d15db08SJerome Forissier static void init_hash_operation(TEE_OperationHandle operation, 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, 78279a3c601SCedric Chaumont 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 797642a1607SCedric Chaumont TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, 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) 826b0104773SPascal Brand TEE_Panic(0); 82773d6c3baSJoakim Bech 82887c2f6b6SCedric Chaumont return res; 829b0104773SPascal Brand } 830b0104773SPascal Brand 831b0104773SPascal Brand /* Cryptographic Operations API - Symmetric Cipher Functions */ 832b0104773SPascal Brand 833642a1607SCedric Chaumont void TEE_CipherInit(TEE_OperationHandle operation, void *IV, uint32_t IVLen) 834b0104773SPascal Brand { 835b0104773SPascal Brand TEE_Result res; 836b0104773SPascal Brand 837b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 838b0104773SPascal Brand TEE_Panic(0); 839642a1607SCedric Chaumont 840b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_CIPHER) 841b0104773SPascal Brand TEE_Panic(0); 842642a1607SCedric Chaumont 843642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 844642a1607SCedric Chaumont !(operation->key1)) 845642a1607SCedric Chaumont TEE_Panic(0); 846642a1607SCedric Chaumont 847642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 848642a1607SCedric Chaumont TEE_ResetOperation(operation); 849642a1607SCedric Chaumont 850642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 851642a1607SCedric Chaumont 852b0104773SPascal Brand res = utee_cipher_init(operation->state, IV, IVLen); 853b0104773SPascal Brand if (res != TEE_SUCCESS) 854b0104773SPascal Brand TEE_Panic(res); 855642a1607SCedric Chaumont 856b0104773SPascal Brand operation->buffer_offs = 0; 857b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 858b0104773SPascal Brand } 859b0104773SPascal Brand 860b0104773SPascal Brand static TEE_Result tee_buffer_update( 861b0104773SPascal Brand TEE_OperationHandle op, 862e86f1266SJens Wiklander TEE_Result(*update_func)(unsigned long state, const void *src, 863e86f1266SJens Wiklander size_t slen, void *dst, uint64_t *dlen), 864b0104773SPascal Brand const void *src_data, size_t src_len, 865e86f1266SJens Wiklander void *dest_data, uint64_t *dest_len) 866b0104773SPascal Brand { 867b0104773SPascal Brand TEE_Result res; 868b0104773SPascal Brand const uint8_t *src = src_data; 869b0104773SPascal Brand size_t slen = src_len; 870b0104773SPascal Brand uint8_t *dst = dest_data; 871b0104773SPascal Brand size_t dlen = *dest_len; 872b0104773SPascal Brand size_t acc_dlen = 0; 873e86f1266SJens Wiklander uint64_t tmp_dlen; 874b0104773SPascal Brand size_t l; 875b0104773SPascal Brand size_t buffer_size; 876d3588802SPascal Brand size_t buffer_left; 877b0104773SPascal Brand 878d3588802SPascal Brand if (op->buffer_two_blocks) { 879b0104773SPascal Brand buffer_size = op->block_size * 2; 880d3588802SPascal Brand buffer_left = 1; 881d3588802SPascal Brand } else { 882b0104773SPascal Brand buffer_size = op->block_size; 883d3588802SPascal Brand buffer_left = 0; 884d3588802SPascal Brand } 885b0104773SPascal Brand 886b0104773SPascal Brand if (op->buffer_offs > 0) { 887b0104773SPascal Brand /* Fill up complete block */ 888b0104773SPascal Brand if (op->buffer_offs < op->block_size) 889b0104773SPascal Brand l = MIN(slen, op->block_size - op->buffer_offs); 890b0104773SPascal Brand else 891b0104773SPascal Brand l = MIN(slen, buffer_size - op->buffer_offs); 892b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, l); 893b0104773SPascal Brand op->buffer_offs += l; 894b0104773SPascal Brand src += l; 895b0104773SPascal Brand slen -= l; 896b0104773SPascal Brand if ((op->buffer_offs % op->block_size) != 0) 897b0104773SPascal Brand goto out; /* Nothing left to do */ 898b0104773SPascal Brand } 899b0104773SPascal Brand 900b0104773SPascal Brand /* If we can feed from buffer */ 901d3588802SPascal Brand if ((op->buffer_offs > 0) && 902d3588802SPascal Brand ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) { 9032ff3fdbbSPascal Brand l = ROUNDUP(op->buffer_offs + slen - buffer_size, 904b0104773SPascal Brand op->block_size); 905b0104773SPascal Brand l = MIN(op->buffer_offs, l); 906b0104773SPascal Brand tmp_dlen = dlen; 907b0104773SPascal Brand res = update_func(op->state, op->buffer, l, dst, &tmp_dlen); 908b0104773SPascal Brand if (res != TEE_SUCCESS) 909b0104773SPascal Brand TEE_Panic(res); 910b0104773SPascal Brand dst += tmp_dlen; 911b0104773SPascal Brand dlen -= tmp_dlen; 912b0104773SPascal Brand acc_dlen += tmp_dlen; 913b0104773SPascal Brand op->buffer_offs -= l; 914b0104773SPascal Brand if (op->buffer_offs > 0) { 915b0104773SPascal Brand /* 916b0104773SPascal Brand * Slen is small enough to be contained in rest buffer. 917b0104773SPascal Brand */ 918b0104773SPascal Brand memcpy(op->buffer, op->buffer + l, buffer_size - l); 919b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 920b0104773SPascal Brand op->buffer_offs += slen; 921b0104773SPascal Brand goto out; /* Nothing left to do */ 922b0104773SPascal Brand } 923b0104773SPascal Brand } 924b0104773SPascal Brand 925d3588802SPascal Brand if (slen >= (buffer_size + buffer_left)) { 926b0104773SPascal Brand /* Buffer is empty, feed as much as possible from src */ 9272ff3fdbbSPascal Brand l = ROUNDUP(slen - buffer_size + 1, op->block_size); 928b0104773SPascal Brand 929b0104773SPascal Brand tmp_dlen = dlen; 930b0104773SPascal Brand res = update_func(op->state, src, l, dst, &tmp_dlen); 931b0104773SPascal Brand if (res != TEE_SUCCESS) 932b0104773SPascal Brand TEE_Panic(res); 933b0104773SPascal Brand src += l; 934b0104773SPascal Brand slen -= l; 935b0104773SPascal Brand dst += tmp_dlen; 936b0104773SPascal Brand dlen -= tmp_dlen; 937b0104773SPascal Brand acc_dlen += tmp_dlen; 938b0104773SPascal Brand } 939b0104773SPascal Brand 940b0104773SPascal Brand /* Slen is small enough to be contained in buffer. */ 941b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 942b0104773SPascal Brand op->buffer_offs += slen; 943b0104773SPascal Brand 944b0104773SPascal Brand out: 945b0104773SPascal Brand *dest_len = acc_dlen; 946b0104773SPascal Brand return TEE_SUCCESS; 947b0104773SPascal Brand } 948b0104773SPascal Brand 949642a1607SCedric Chaumont TEE_Result TEE_CipherUpdate(TEE_OperationHandle operation, void *srcData, 95079a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 951b0104773SPascal Brand { 952dea1f2b6SCedric Chaumont TEE_Result res; 953b0104773SPascal Brand size_t req_dlen; 954e86f1266SJens Wiklander uint64_t dl; 955b0104773SPascal Brand 956642a1607SCedric Chaumont if (operation == TEE_HANDLE_NULL || 957dea1f2b6SCedric Chaumont (srcData == NULL && srcLen != 0) || 958dea1f2b6SCedric Chaumont destLen == NULL || 959dea1f2b6SCedric Chaumont (destData == NULL && *destLen != 0)) { 960dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 961dea1f2b6SCedric Chaumont goto out; 962dea1f2b6SCedric Chaumont } 963dea1f2b6SCedric Chaumont 964642a1607SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 965dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 966dea1f2b6SCedric Chaumont goto out; 967dea1f2b6SCedric Chaumont } 968dea1f2b6SCedric Chaumont 969642a1607SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 970642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 971642a1607SCedric Chaumont goto out; 972642a1607SCedric Chaumont } 973642a1607SCedric Chaumont 974642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 975dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 976dea1f2b6SCedric Chaumont goto out; 977dea1f2b6SCedric Chaumont } 978b0104773SPascal Brand 979e32c5ddfSJerome Forissier if (!srcData && !srcLen) { 980e32c5ddfSJerome Forissier res = TEE_SUCCESS; 981e32c5ddfSJerome Forissier goto out; 982e32c5ddfSJerome Forissier } 983e32c5ddfSJerome Forissier 984b0104773SPascal Brand /* Calculate required dlen */ 985642a1607SCedric Chaumont req_dlen = ((operation->buffer_offs + srcLen) / operation->block_size) * 986642a1607SCedric Chaumont operation->block_size; 987642a1607SCedric Chaumont if (operation->buffer_two_blocks) { 988642a1607SCedric Chaumont if (req_dlen > operation->block_size * 2) 989642a1607SCedric Chaumont req_dlen -= operation->block_size * 2; 990b0104773SPascal Brand else 991b0104773SPascal Brand req_dlen = 0; 992b0104773SPascal Brand } 993b0104773SPascal Brand /* 994b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 995b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 996b0104773SPascal Brand * can't restore sync with this API. 997b0104773SPascal Brand */ 998b0104773SPascal Brand if (*destLen < req_dlen) { 999b0104773SPascal Brand *destLen = req_dlen; 1000dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1001dea1f2b6SCedric Chaumont goto out; 1002b0104773SPascal Brand } 1003b0104773SPascal Brand 1004e86f1266SJens Wiklander dl = *destLen; 1005642a1607SCedric Chaumont res = tee_buffer_update(operation, utee_cipher_update, srcData, srcLen, 1006e86f1266SJens Wiklander destData, &dl); 1007e86f1266SJens Wiklander *destLen = dl; 1008b0104773SPascal Brand 1009dea1f2b6SCedric Chaumont out: 1010dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 1011dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1012dea1f2b6SCedric Chaumont TEE_Panic(0); 1013dea1f2b6SCedric Chaumont 1014dea1f2b6SCedric Chaumont return res; 1015b0104773SPascal Brand } 1016b0104773SPascal Brand 1017642a1607SCedric Chaumont TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation, 1018642a1607SCedric Chaumont void *srcData, uint32_t srcLen, void *destData, 101979a3c601SCedric Chaumont uint32_t *destLen) 1020b0104773SPascal Brand { 1021b0104773SPascal Brand TEE_Result res; 1022b0104773SPascal Brand uint8_t *dst = destData; 1023b0104773SPascal Brand size_t acc_dlen = 0; 1024e86f1266SJens Wiklander uint64_t tmp_dlen; 1025b0104773SPascal Brand size_t req_dlen; 1026b0104773SPascal Brand 1027642a1607SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1028dea1f2b6SCedric Chaumont (srcData == NULL && srcLen != 0) || 1029dea1f2b6SCedric Chaumont destLen == NULL || 1030dea1f2b6SCedric Chaumont (destData == NULL && *destLen != 0)) { 1031dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1032dea1f2b6SCedric Chaumont goto out; 1033dea1f2b6SCedric Chaumont } 1034dea1f2b6SCedric Chaumont 1035642a1607SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 1036dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1037dea1f2b6SCedric Chaumont goto out; 1038dea1f2b6SCedric Chaumont } 1039dea1f2b6SCedric Chaumont 1040642a1607SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1041642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1042642a1607SCedric Chaumont goto out; 1043642a1607SCedric Chaumont } 1044642a1607SCedric Chaumont 1045642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1046dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1047dea1f2b6SCedric Chaumont goto out; 1048dea1f2b6SCedric Chaumont } 1049b0104773SPascal Brand 1050b0104773SPascal Brand /* 1051b0104773SPascal Brand * Check that the final block doesn't require padding for those 1052b0104773SPascal Brand * algorithms that requires client to supply padding. 1053b0104773SPascal Brand */ 1054642a1607SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD || 1055642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD || 1056642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD || 1057642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD || 1058642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || 1059642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD) { 1060642a1607SCedric Chaumont if (((operation->buffer_offs + srcLen) % operation->block_size) 1061642a1607SCedric Chaumont != 0) { 1062dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1063dea1f2b6SCedric Chaumont goto out; 1064dea1f2b6SCedric Chaumont } 1065b0104773SPascal Brand } 1066b0104773SPascal Brand 1067f21873cdSJerome Forissier if (!srcData && !srcLen) { 1068f21873cdSJerome Forissier res = TEE_SUCCESS; 1069f21873cdSJerome Forissier goto out; 1070f21873cdSJerome Forissier } 1071f21873cdSJerome Forissier 1072b0104773SPascal Brand /* 1073b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1074b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1075b0104773SPascal Brand * can't restore sync with this API. 1076b0104773SPascal Brand */ 1077642a1607SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1078b0104773SPascal Brand if (*destLen < req_dlen) { 1079b0104773SPascal Brand *destLen = req_dlen; 1080dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1081dea1f2b6SCedric Chaumont goto out; 1082b0104773SPascal Brand } 1083b0104773SPascal Brand 1084b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1085642a1607SCedric Chaumont res = tee_buffer_update(operation, utee_cipher_update, srcData, srcLen, 1086642a1607SCedric Chaumont dst, &tmp_dlen); 1087dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS) 1088dea1f2b6SCedric Chaumont goto out; 1089dea1f2b6SCedric Chaumont 1090b0104773SPascal Brand dst += tmp_dlen; 1091b0104773SPascal Brand acc_dlen += tmp_dlen; 1092b0104773SPascal Brand 1093b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1094642a1607SCedric Chaumont res = utee_cipher_final(operation->state, operation->buffer, 1095642a1607SCedric Chaumont operation->buffer_offs, dst, &tmp_dlen); 1096b0104773SPascal Brand if (res != TEE_SUCCESS) 1097dea1f2b6SCedric Chaumont goto out; 1098dea1f2b6SCedric Chaumont 1099b0104773SPascal Brand acc_dlen += tmp_dlen; 1100b0104773SPascal Brand *destLen = acc_dlen; 1101dea1f2b6SCedric Chaumont 1102642a1607SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1103642a1607SCedric Chaumont 1104642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1105642a1607SCedric Chaumont 1106dea1f2b6SCedric Chaumont out: 1107dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 1108dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1109dea1f2b6SCedric Chaumont TEE_Panic(0); 1110dea1f2b6SCedric Chaumont 1111dea1f2b6SCedric Chaumont return res; 1112b0104773SPascal Brand } 1113b0104773SPascal Brand 1114b0104773SPascal Brand /* Cryptographic Operations API - MAC Functions */ 1115b0104773SPascal Brand 111628e0efc6SCedric Chaumont void TEE_MACInit(TEE_OperationHandle operation, void *IV, uint32_t IVLen) 1117b0104773SPascal Brand { 1118b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 1119b0104773SPascal Brand TEE_Panic(0); 1120642a1607SCedric Chaumont 1121b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_MAC) 1122b0104773SPascal Brand TEE_Panic(0); 1123642a1607SCedric Chaumont 1124642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 1125642a1607SCedric Chaumont !(operation->key1)) 1126642a1607SCedric Chaumont TEE_Panic(0); 1127642a1607SCedric Chaumont 1128642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 1129642a1607SCedric Chaumont TEE_ResetOperation(operation); 1130642a1607SCedric Chaumont 1131642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1132642a1607SCedric Chaumont 11336d15db08SJerome Forissier init_hash_operation(operation, IV, IVLen); 1134b0104773SPascal Brand } 1135b0104773SPascal Brand 113628e0efc6SCedric Chaumont void TEE_MACUpdate(TEE_OperationHandle operation, void *chunk, 113728e0efc6SCedric Chaumont uint32_t chunkSize) 1138b0104773SPascal Brand { 1139b0104773SPascal Brand TEE_Result res; 1140b0104773SPascal Brand 114128e0efc6SCedric Chaumont if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0)) 1142b0104773SPascal Brand TEE_Panic(0); 1143642a1607SCedric Chaumont 114428e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) 1145b0104773SPascal Brand TEE_Panic(0); 1146642a1607SCedric Chaumont 114728e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1148b0104773SPascal Brand TEE_Panic(0); 1149b0104773SPascal Brand 1150642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) 1151642a1607SCedric Chaumont TEE_Panic(0); 1152642a1607SCedric Chaumont 115328e0efc6SCedric Chaumont res = utee_hash_update(operation->state, chunk, chunkSize); 1154b0104773SPascal Brand if (res != TEE_SUCCESS) 1155b0104773SPascal Brand TEE_Panic(res); 1156b0104773SPascal Brand } 1157b0104773SPascal Brand 115828e0efc6SCedric Chaumont TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation, 115928e0efc6SCedric Chaumont void *message, uint32_t messageLen, 116079a3c601SCedric Chaumont void *mac, uint32_t *macLen) 1161b0104773SPascal Brand { 1162b0104773SPascal Brand TEE_Result res; 1163e86f1266SJens Wiklander uint64_t ml; 1164b0104773SPascal Brand 116528e0efc6SCedric Chaumont if (operation == TEE_HANDLE_NULL || 116628e0efc6SCedric Chaumont (message == NULL && messageLen != 0) || 116728e0efc6SCedric Chaumont mac == NULL || 116828e0efc6SCedric Chaumont macLen == NULL) { 116928e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 117028e0efc6SCedric Chaumont goto out; 117128e0efc6SCedric Chaumont } 1172b0104773SPascal Brand 117328e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 117428e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 117528e0efc6SCedric Chaumont goto out; 117628e0efc6SCedric Chaumont } 117728e0efc6SCedric Chaumont 117828e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 117928e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 118028e0efc6SCedric Chaumont goto out; 118128e0efc6SCedric Chaumont } 118228e0efc6SCedric Chaumont 1183642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1184642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1185642a1607SCedric Chaumont goto out; 1186642a1607SCedric Chaumont } 1187642a1607SCedric Chaumont 1188e86f1266SJens Wiklander ml = *macLen; 1189e86f1266SJens Wiklander res = utee_hash_final(operation->state, message, messageLen, mac, &ml); 1190e86f1266SJens Wiklander *macLen = ml; 119128e0efc6SCedric Chaumont if (res != TEE_SUCCESS) 119228e0efc6SCedric Chaumont goto out; 119328e0efc6SCedric Chaumont 119428e0efc6SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 119528e0efc6SCedric Chaumont 1196642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1197642a1607SCedric Chaumont 119828e0efc6SCedric Chaumont out: 119928e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 120028e0efc6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 120128e0efc6SCedric Chaumont TEE_Panic(res); 120228e0efc6SCedric Chaumont 1203b0104773SPascal Brand return res; 1204b0104773SPascal Brand } 1205b0104773SPascal Brand 1206b0104773SPascal Brand TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation, 120728e0efc6SCedric Chaumont void *message, uint32_t messageLen, 120828e0efc6SCedric Chaumont void *mac, uint32_t macLen) 1209b0104773SPascal Brand { 1210b0104773SPascal Brand TEE_Result res; 1211b0104773SPascal Brand uint8_t computed_mac[TEE_MAX_HASH_SIZE]; 12127f74c64aSPascal Brand uint32_t computed_mac_size = TEE_MAX_HASH_SIZE; 1213b0104773SPascal Brand 121428e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 121528e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 121628e0efc6SCedric Chaumont goto out; 121728e0efc6SCedric Chaumont } 121828e0efc6SCedric Chaumont 121928e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 122028e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 122128e0efc6SCedric Chaumont goto out; 122228e0efc6SCedric Chaumont } 122328e0efc6SCedric Chaumont 1224642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1225642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1226642a1607SCedric Chaumont goto out; 1227642a1607SCedric Chaumont } 1228642a1607SCedric Chaumont 1229b0104773SPascal Brand res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac, 1230b0104773SPascal Brand &computed_mac_size); 1231b0104773SPascal Brand if (res != TEE_SUCCESS) 123228e0efc6SCedric Chaumont goto out; 123328e0efc6SCedric Chaumont 123428e0efc6SCedric Chaumont if (computed_mac_size != macLen) { 123528e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 123628e0efc6SCedric Chaumont goto out; 123728e0efc6SCedric Chaumont } 123828e0efc6SCedric Chaumont 123928e0efc6SCedric Chaumont if (buf_compare_ct(mac, computed_mac, computed_mac_size) != 0) { 124028e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 124128e0efc6SCedric Chaumont goto out; 124228e0efc6SCedric Chaumont } 124328e0efc6SCedric Chaumont 1244642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1245642a1607SCedric Chaumont 124628e0efc6SCedric Chaumont out: 124728e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 124828e0efc6SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 124928e0efc6SCedric Chaumont TEE_Panic(res); 125028e0efc6SCedric Chaumont 1251b0104773SPascal Brand return res; 1252b0104773SPascal Brand } 1253b0104773SPascal Brand 1254b0104773SPascal Brand /* Cryptographic Operations API - Authenticated Encryption Functions */ 1255b0104773SPascal Brand 1256b5816c88SCedric Chaumont TEE_Result TEE_AEInit(TEE_OperationHandle operation, void *nonce, 125779a3c601SCedric Chaumont uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen, 1258b0104773SPascal Brand uint32_t payloadLen) 1259b0104773SPascal Brand { 1260b0104773SPascal Brand TEE_Result res; 1261b0104773SPascal Brand 1262b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || nonce == NULL) { 1263b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1264b5816c88SCedric Chaumont goto out; 1265b5816c88SCedric Chaumont } 1266b5816c88SCedric Chaumont 1267b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1268b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1269b5816c88SCedric Chaumont goto out; 1270b5816c88SCedric Chaumont } 1271b0104773SPascal Brand 1272642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 1273642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1274642a1607SCedric Chaumont goto out; 1275642a1607SCedric Chaumont } 1276642a1607SCedric Chaumont 1277b0104773SPascal Brand /* 1278b0104773SPascal Brand * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core 1279b0104773SPascal Brand * in the implementation. But AES-GCM spec doesn't specify the tag len 1280b0104773SPascal Brand * according to the same principle so we have to check here instead to 1281b0104773SPascal Brand * be GP compliant. 1282b0104773SPascal Brand */ 1283b5816c88SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_GCM) { 1284b0104773SPascal Brand /* 1285b0104773SPascal Brand * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96 1286b0104773SPascal Brand */ 1287b5816c88SCedric Chaumont if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) { 1288b5816c88SCedric Chaumont res = TEE_ERROR_NOT_SUPPORTED; 1289b5816c88SCedric Chaumont goto out; 1290b5816c88SCedric Chaumont } 1291b0104773SPascal Brand } 1292b0104773SPascal Brand 1293b5816c88SCedric Chaumont res = utee_authenc_init(operation->state, nonce, nonceLen, 1294b5816c88SCedric Chaumont tagLen / 8, AADLen, payloadLen); 1295b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1296b5816c88SCedric Chaumont goto out; 1297b5816c88SCedric Chaumont 1298b5816c88SCedric Chaumont operation->ae_tag_len = tagLen / 8; 1299b5816c88SCedric Chaumont operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 1300b5816c88SCedric Chaumont 1301b5816c88SCedric Chaumont out: 1302b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1303b5816c88SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 1304b0104773SPascal Brand TEE_Panic(res); 1305b5816c88SCedric Chaumont 1306b0104773SPascal Brand return res; 1307b0104773SPascal Brand } 1308b0104773SPascal Brand 1309b5816c88SCedric Chaumont void TEE_AEUpdateAAD(TEE_OperationHandle operation, void *AADdata, 131079a3c601SCedric Chaumont uint32_t AADdataLen) 1311b0104773SPascal Brand { 1312b0104773SPascal Brand TEE_Result res; 1313b0104773SPascal Brand 1314b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1315b5816c88SCedric Chaumont (AADdata == NULL && AADdataLen != 0)) 1316b0104773SPascal Brand TEE_Panic(0); 1317642a1607SCedric Chaumont 1318b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) 1319b0104773SPascal Brand TEE_Panic(0); 1320642a1607SCedric Chaumont 1321b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1322b0104773SPascal Brand TEE_Panic(0); 1323b0104773SPascal Brand 1324b5816c88SCedric Chaumont res = utee_authenc_update_aad(operation->state, AADdata, AADdataLen); 1325642a1607SCedric Chaumont 1326642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1327642a1607SCedric Chaumont 1328b0104773SPascal Brand if (res != TEE_SUCCESS) 1329b0104773SPascal Brand TEE_Panic(res); 1330b0104773SPascal Brand } 1331b0104773SPascal Brand 1332b5816c88SCedric Chaumont TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, void *srcData, 133379a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 1334b0104773SPascal Brand { 1335b5816c88SCedric Chaumont TEE_Result res; 1336b0104773SPascal Brand size_t req_dlen; 1337e86f1266SJens Wiklander uint64_t dl; 1338b0104773SPascal Brand 1339b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1340b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1341b5816c88SCedric Chaumont destLen == NULL || 1342b5816c88SCedric Chaumont (destData == NULL && *destLen != 0)) { 1343b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1344b5816c88SCedric Chaumont goto out; 1345b5816c88SCedric Chaumont } 1346b5816c88SCedric Chaumont 1347b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1348b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1349b5816c88SCedric Chaumont goto out; 1350b5816c88SCedric Chaumont } 1351b5816c88SCedric Chaumont 1352b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1353b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1354b5816c88SCedric Chaumont goto out; 1355b5816c88SCedric Chaumont } 1356b0104773SPascal Brand 1357*827308b8SJerome Forissier if (!srcData && !srcLen) { 1358*827308b8SJerome Forissier res = TEE_SUCCESS; 1359*827308b8SJerome Forissier goto out; 1360*827308b8SJerome Forissier } 1361*827308b8SJerome Forissier 1362b0104773SPascal Brand /* 1363b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1364b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1365b0104773SPascal Brand * can't restore sync with this API. 1366b0104773SPascal Brand */ 1367b5816c88SCedric Chaumont req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen, 1368b5816c88SCedric Chaumont operation->block_size); 1369b0104773SPascal Brand if (*destLen < req_dlen) { 1370b0104773SPascal Brand *destLen = req_dlen; 1371b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1372b5816c88SCedric Chaumont goto out; 1373b0104773SPascal Brand } 1374b0104773SPascal Brand 1375e86f1266SJens Wiklander dl = *destLen; 1376b5816c88SCedric Chaumont res = tee_buffer_update(operation, utee_authenc_update_payload, srcData, 1377e86f1266SJens Wiklander srcLen, destData, &dl); 1378e86f1266SJens Wiklander *destLen = dl; 1379b0104773SPascal Brand 1380642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1381642a1607SCedric Chaumont 1382b5816c88SCedric Chaumont out: 1383b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1384b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1385b5816c88SCedric Chaumont TEE_Panic(res); 1386b5816c88SCedric Chaumont 1387b5816c88SCedric Chaumont return res; 1388b0104773SPascal Brand } 1389b0104773SPascal Brand 1390b5816c88SCedric Chaumont TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation, 1391b5816c88SCedric Chaumont void *srcData, uint32_t srcLen, 139279a3c601SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 139379a3c601SCedric Chaumont uint32_t *tagLen) 1394b0104773SPascal Brand { 1395b0104773SPascal Brand TEE_Result res; 1396b0104773SPascal Brand uint8_t *dst = destData; 1397b0104773SPascal Brand size_t acc_dlen = 0; 1398e86f1266SJens Wiklander uint64_t tmp_dlen; 1399b0104773SPascal Brand size_t req_dlen; 1400e86f1266SJens Wiklander uint64_t tl; 1401b0104773SPascal Brand 1402b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1403b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1404b5816c88SCedric Chaumont destLen == NULL || 1405b5816c88SCedric Chaumont (destData == NULL && *destLen != 0) || 1406b5816c88SCedric Chaumont tag == NULL || tagLen == NULL) { 1407b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1408b5816c88SCedric Chaumont goto out; 1409b5816c88SCedric Chaumont } 1410b5816c88SCedric Chaumont 1411b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1412b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1413b5816c88SCedric Chaumont goto out; 1414b5816c88SCedric Chaumont } 1415b5816c88SCedric Chaumont 1416b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1417b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1418b5816c88SCedric Chaumont goto out; 1419b5816c88SCedric Chaumont } 1420b0104773SPascal Brand 1421a9f92a95SJerome Forissier if (!srcData && !srcLen) { 1422a9f92a95SJerome Forissier res = TEE_SUCCESS; 1423a9f92a95SJerome Forissier goto out; 1424a9f92a95SJerome Forissier } 1425a9f92a95SJerome Forissier 1426b0104773SPascal Brand /* 1427b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1428b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1429b0104773SPascal Brand * can't restore sync with this API. 1430b0104773SPascal Brand */ 1431b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1432b0104773SPascal Brand if (*destLen < req_dlen) { 1433b0104773SPascal Brand *destLen = req_dlen; 1434b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1435b5816c88SCedric Chaumont goto out; 1436b0104773SPascal Brand } 1437b0104773SPascal Brand 1438b0104773SPascal Brand /* 1439b0104773SPascal Brand * Need to check this before update_payload since sync would be lost if 1440b0104773SPascal Brand * we return short buffer after that. 1441b0104773SPascal Brand */ 1442b5816c88SCedric Chaumont if (*tagLen < operation->ae_tag_len) { 1443b5816c88SCedric Chaumont *tagLen = operation->ae_tag_len; 1444b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1445b5816c88SCedric Chaumont goto out; 1446b0104773SPascal Brand } 1447b0104773SPascal Brand 1448b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1449b5816c88SCedric Chaumont res = tee_buffer_update(operation, utee_authenc_update_payload, srcData, 1450b5816c88SCedric Chaumont srcLen, dst, &tmp_dlen); 1451b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1452b5816c88SCedric Chaumont goto out; 1453b5816c88SCedric Chaumont 1454b0104773SPascal Brand dst += tmp_dlen; 1455b0104773SPascal Brand acc_dlen += tmp_dlen; 1456b0104773SPascal Brand 1457b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1458e86f1266SJens Wiklander tl = *tagLen; 1459b5816c88SCedric Chaumont res = utee_authenc_enc_final(operation->state, operation->buffer, 1460b5816c88SCedric Chaumont operation->buffer_offs, dst, &tmp_dlen, 1461e86f1266SJens Wiklander tag, &tl); 1462e86f1266SJens Wiklander *tagLen = tl; 1463b0104773SPascal Brand if (res != TEE_SUCCESS) 1464b5816c88SCedric Chaumont goto out; 1465b0104773SPascal Brand 1466b5816c88SCedric Chaumont acc_dlen += tmp_dlen; 1467b0104773SPascal Brand *destLen = acc_dlen; 1468642a1607SCedric Chaumont 1469b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1470b5816c88SCedric Chaumont 1471642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1472642a1607SCedric Chaumont 1473b5816c88SCedric Chaumont out: 1474b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1475b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1476b5816c88SCedric Chaumont TEE_Panic(res); 1477b0104773SPascal Brand 1478b0104773SPascal Brand return res; 1479b0104773SPascal Brand } 1480b0104773SPascal Brand 1481b5816c88SCedric Chaumont TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation, 1482b5816c88SCedric Chaumont void *srcData, uint32_t srcLen, 1483b5816c88SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 148479a3c601SCedric Chaumont uint32_t tagLen) 1485b0104773SPascal Brand { 1486b0104773SPascal Brand TEE_Result res; 1487b0104773SPascal Brand uint8_t *dst = destData; 1488b0104773SPascal Brand size_t acc_dlen = 0; 1489e86f1266SJens Wiklander uint64_t tmp_dlen; 1490b0104773SPascal Brand size_t req_dlen; 1491b0104773SPascal Brand 1492b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1493b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1494b5816c88SCedric Chaumont destLen == NULL || 1495b5816c88SCedric Chaumont (destData == NULL && *destLen != 0) || 1496b5816c88SCedric Chaumont (tag == NULL && tagLen != 0)) { 1497b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1498b5816c88SCedric Chaumont goto out; 1499b5816c88SCedric Chaumont } 1500b5816c88SCedric Chaumont 1501b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1502b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1503b5816c88SCedric Chaumont goto out; 1504b5816c88SCedric Chaumont } 1505b5816c88SCedric Chaumont 1506b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1507b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1508b5816c88SCedric Chaumont goto out; 1509b5816c88SCedric Chaumont } 1510b0104773SPascal Brand 15112fda30ffSJerome Forissier if (!srcData && !srcLen) { 15122fda30ffSJerome Forissier res = TEE_SUCCESS; 15132fda30ffSJerome Forissier goto out; 15142fda30ffSJerome Forissier } 15152fda30ffSJerome Forissier 1516b0104773SPascal Brand /* 1517b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1518b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1519b0104773SPascal Brand * can't restore sync with this API. 1520b0104773SPascal Brand */ 1521b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1522b0104773SPascal Brand if (*destLen < req_dlen) { 1523b0104773SPascal Brand *destLen = req_dlen; 1524b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1525b5816c88SCedric Chaumont goto out; 1526b0104773SPascal Brand } 1527b0104773SPascal Brand 1528b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1529b5816c88SCedric Chaumont res = tee_buffer_update(operation, utee_authenc_update_payload, srcData, 1530b5816c88SCedric Chaumont srcLen, dst, &tmp_dlen); 1531b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1532b5816c88SCedric Chaumont goto out; 1533b5816c88SCedric Chaumont 1534b0104773SPascal Brand dst += tmp_dlen; 1535b0104773SPascal Brand acc_dlen += tmp_dlen; 1536b0104773SPascal Brand 1537b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1538b5816c88SCedric Chaumont res = utee_authenc_dec_final(operation->state, operation->buffer, 1539b5816c88SCedric Chaumont operation->buffer_offs, dst, &tmp_dlen, 1540b5816c88SCedric Chaumont tag, tagLen); 1541b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1542b5816c88SCedric Chaumont goto out; 1543b5816c88SCedric Chaumont 1544b0104773SPascal Brand /* Supplied tagLen should match what we initiated with */ 1545b5816c88SCedric Chaumont if (tagLen != operation->ae_tag_len) 1546b0104773SPascal Brand res = TEE_ERROR_MAC_INVALID; 1547b0104773SPascal Brand 1548b0104773SPascal Brand acc_dlen += tmp_dlen; 1549b0104773SPascal Brand *destLen = acc_dlen; 1550642a1607SCedric Chaumont 1551b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1552b5816c88SCedric Chaumont 1553642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1554642a1607SCedric Chaumont 1555b5816c88SCedric Chaumont out: 1556b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1557b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER && 1558b5816c88SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 1559b5816c88SCedric Chaumont TEE_Panic(res); 1560b0104773SPascal Brand 1561b0104773SPascal Brand return res; 1562b0104773SPascal Brand } 1563b0104773SPascal Brand 1564b0104773SPascal Brand /* Cryptographic Operations API - Asymmetric Functions */ 1565b0104773SPascal Brand 156612e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation, 156712e66b6fSCedric Chaumont TEE_Attribute *params, 156812e66b6fSCedric Chaumont uint32_t paramCount, void *srcData, 156979a3c601SCedric Chaumont uint32_t srcLen, void *destData, 157079a3c601SCedric Chaumont uint32_t *destLen) 1571b0104773SPascal Brand { 1572b0104773SPascal Brand TEE_Result res; 1573e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1574e86f1266SJens Wiklander uint64_t dl; 1575b0104773SPascal Brand 157612e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1577b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 1578b0104773SPascal Brand TEE_Panic(0); 157912e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1580b0104773SPascal Brand TEE_Panic(0); 158112e66b6fSCedric Chaumont if (!operation->key1) 1582b0104773SPascal Brand TEE_Panic(0); 158312e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 158412e66b6fSCedric Chaumont TEE_Panic(0); 158512e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_ENCRYPT) 1586b0104773SPascal Brand TEE_Panic(0); 1587b0104773SPascal Brand 1588e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1589e86f1266SJens Wiklander dl = *destLen; 1590e86f1266SJens Wiklander res = utee_asymm_operate(operation->state, ua, paramCount, srcData, 1591e86f1266SJens Wiklander srcLen, destData, &dl); 1592e86f1266SJens Wiklander *destLen = dl; 159312e66b6fSCedric Chaumont 15948844ebfcSPascal Brand if (res != TEE_SUCCESS && 15958844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 15968844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1597b0104773SPascal Brand TEE_Panic(res); 159812e66b6fSCedric Chaumont 1599b0104773SPascal Brand return res; 1600b0104773SPascal Brand } 1601b0104773SPascal Brand 160212e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation, 160312e66b6fSCedric Chaumont TEE_Attribute *params, 160412e66b6fSCedric Chaumont uint32_t paramCount, void *srcData, 160579a3c601SCedric Chaumont uint32_t srcLen, void *destData, 160679a3c601SCedric Chaumont uint32_t *destLen) 1607b0104773SPascal Brand { 1608b0104773SPascal Brand TEE_Result res; 1609e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1610e86f1266SJens Wiklander uint64_t dl; 1611b0104773SPascal Brand 161212e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1613b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 1614b0104773SPascal Brand TEE_Panic(0); 161512e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1616b0104773SPascal Brand TEE_Panic(0); 161712e66b6fSCedric Chaumont if (!operation->key1) 1618b0104773SPascal Brand TEE_Panic(0); 161912e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 162012e66b6fSCedric Chaumont TEE_Panic(0); 162112e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_DECRYPT) 1622b0104773SPascal Brand TEE_Panic(0); 1623b0104773SPascal Brand 1624e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1625e86f1266SJens Wiklander dl = *destLen; 1626e86f1266SJens Wiklander res = utee_asymm_operate(operation->state, ua, paramCount, srcData, 1627e86f1266SJens Wiklander srcLen, destData, &dl); 1628e86f1266SJens Wiklander *destLen = dl; 162912e66b6fSCedric Chaumont 16308844ebfcSPascal Brand if (res != TEE_SUCCESS && 16318844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 16328844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1633b0104773SPascal Brand TEE_Panic(res); 163412e66b6fSCedric Chaumont 1635b0104773SPascal Brand return res; 1636b0104773SPascal Brand } 1637b0104773SPascal Brand 163812e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation, 163912e66b6fSCedric Chaumont TEE_Attribute *params, 164012e66b6fSCedric Chaumont uint32_t paramCount, void *digest, 164179a3c601SCedric Chaumont uint32_t digestLen, void *signature, 164279a3c601SCedric Chaumont uint32_t *signatureLen) 1643b0104773SPascal Brand { 1644b0104773SPascal Brand TEE_Result res; 1645e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1646e86f1266SJens Wiklander uint64_t sl; 1647b0104773SPascal Brand 164812e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || 164912e66b6fSCedric Chaumont (digest == NULL && digestLen != 0) || 1650b0104773SPascal Brand signature == NULL || signatureLen == NULL) 1651b0104773SPascal Brand TEE_Panic(0); 165212e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1653b0104773SPascal Brand TEE_Panic(0); 165412e66b6fSCedric Chaumont if (!operation->key1) 1655b0104773SPascal Brand TEE_Panic(0); 165612e66b6fSCedric Chaumont if (operation->info.operationClass != 165712e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 165812e66b6fSCedric Chaumont TEE_Panic(0); 165912e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_SIGN) 1660b0104773SPascal Brand TEE_Panic(0); 1661b0104773SPascal Brand 1662e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1663e86f1266SJens Wiklander sl = *signatureLen; 1664e86f1266SJens Wiklander res = utee_asymm_operate(operation->state, ua, paramCount, digest, 1665e86f1266SJens Wiklander digestLen, signature, &sl); 1666e86f1266SJens Wiklander *signatureLen = sl; 166712e66b6fSCedric Chaumont 1668b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER) 1669b0104773SPascal Brand TEE_Panic(res); 167012e66b6fSCedric Chaumont 1671b0104773SPascal Brand return res; 1672b0104773SPascal Brand } 1673b0104773SPascal Brand 167412e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation, 167512e66b6fSCedric Chaumont TEE_Attribute *params, 167612e66b6fSCedric Chaumont uint32_t paramCount, void *digest, 167712e66b6fSCedric Chaumont uint32_t digestLen, void *signature, 167879a3c601SCedric Chaumont uint32_t signatureLen) 1679b0104773SPascal Brand { 1680b0104773SPascal Brand TEE_Result res; 1681e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1682b0104773SPascal Brand 168312e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || 168412e66b6fSCedric Chaumont (digest == NULL && digestLen != 0) || 1685b0104773SPascal Brand (signature == NULL && signatureLen != 0)) 1686b0104773SPascal Brand TEE_Panic(0); 168712e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1688b0104773SPascal Brand TEE_Panic(0); 168912e66b6fSCedric Chaumont if (!operation->key1) 1690b0104773SPascal Brand TEE_Panic(0); 169112e66b6fSCedric Chaumont if (operation->info.operationClass != 169212e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 169312e66b6fSCedric Chaumont TEE_Panic(0); 169412e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_VERIFY) 1695b0104773SPascal Brand TEE_Panic(0); 1696b0104773SPascal Brand 1697e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1698e86f1266SJens Wiklander res = utee_asymm_verify(operation->state, ua, paramCount, digest, 169912e66b6fSCedric Chaumont digestLen, signature, signatureLen); 170012e66b6fSCedric Chaumont 1701b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID) 1702b0104773SPascal Brand TEE_Panic(res); 170312e66b6fSCedric Chaumont 1704b0104773SPascal Brand return res; 1705b0104773SPascal Brand } 1706b0104773SPascal Brand 1707b0104773SPascal Brand /* Cryptographic Operations API - Key Derivation Functions */ 1708b0104773SPascal Brand 1709b0104773SPascal Brand void TEE_DeriveKey(TEE_OperationHandle operation, 1710b0104773SPascal Brand const TEE_Attribute *params, uint32_t paramCount, 1711b0104773SPascal Brand TEE_ObjectHandle derivedKey) 1712b0104773SPascal Brand { 1713b0104773SPascal Brand TEE_Result res; 1714b0104773SPascal Brand TEE_ObjectInfo key_info; 1715e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1716b0104773SPascal Brand 1717b0104773SPascal Brand if (operation == TEE_HANDLE_NULL || derivedKey == 0) 1718b0104773SPascal Brand TEE_Panic(0); 171984fa9467SCedric Chaumont if (params == NULL && paramCount != 0) 1720b0104773SPascal Brand TEE_Panic(0); 17218854d3c6SJerome Forissier if (TEE_ALG_GET_CLASS(operation->info.algorithm) != 17228854d3c6SJerome Forissier TEE_OPERATION_KEY_DERIVATION) 1723b0104773SPascal Brand TEE_Panic(0); 1724b0104773SPascal Brand 1725b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION) 1726b0104773SPascal Brand TEE_Panic(0); 172784fa9467SCedric Chaumont if (!operation->key1) 172884fa9467SCedric Chaumont TEE_Panic(0); 1729b0104773SPascal Brand if (operation->info.mode != TEE_MODE_DERIVE) 1730b0104773SPascal Brand TEE_Panic(0); 1731b0104773SPascal Brand if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0) 1732b0104773SPascal Brand TEE_Panic(0); 1733b0104773SPascal Brand 1734e86f1266SJens Wiklander res = utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info); 1735b0104773SPascal Brand if (res != TEE_SUCCESS) 1736b0104773SPascal Brand TEE_Panic(0); 1737b0104773SPascal Brand 1738b0104773SPascal Brand if (key_info.objectType != TEE_TYPE_GENERIC_SECRET) 1739b0104773SPascal Brand TEE_Panic(0); 1740b0104773SPascal Brand if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1741b0104773SPascal Brand TEE_Panic(0); 1742b0104773SPascal Brand 1743e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1744e86f1266SJens Wiklander res = utee_cryp_derive_key(operation->state, ua, paramCount, 1745e86f1266SJens Wiklander (unsigned long)derivedKey); 1746b0104773SPascal Brand if (res != TEE_SUCCESS) 1747b0104773SPascal Brand TEE_Panic(res); 1748b0104773SPascal Brand } 1749b0104773SPascal Brand 1750b0104773SPascal Brand /* Cryptographic Operations API - Random Number Generation Functions */ 1751b0104773SPascal Brand 175279a3c601SCedric Chaumont void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen) 1753b0104773SPascal Brand { 1754b0104773SPascal Brand TEE_Result res; 1755b0104773SPascal Brand 1756b0104773SPascal Brand res = utee_cryp_random_number_generate(randomBuffer, randomBufferLen); 1757b0104773SPascal Brand if (res != TEE_SUCCESS) 1758b0104773SPascal Brand TEE_Panic(res); 1759b0104773SPascal Brand } 1760