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) 349*b36311adSJerome 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) 379*b36311adSJerome 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) 488*b36311adSJerome 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) 586*b36311adSJerome 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) 711*b36311adSJerome 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 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) 826*b36311adSJerome Forissier TEE_Panic(res); 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 878090268f5SJens Wiklander if (!src) { 879090268f5SJens Wiklander if (slen) 880090268f5SJens Wiklander TEE_Panic(0); 881090268f5SJens Wiklander goto out; 882090268f5SJens Wiklander } 883090268f5SJens Wiklander 884d3588802SPascal Brand if (op->buffer_two_blocks) { 885b0104773SPascal Brand buffer_size = op->block_size * 2; 886d3588802SPascal Brand buffer_left = 1; 887d3588802SPascal Brand } else { 888b0104773SPascal Brand buffer_size = op->block_size; 889d3588802SPascal Brand buffer_left = 0; 890d3588802SPascal Brand } 891b0104773SPascal Brand 892b0104773SPascal Brand if (op->buffer_offs > 0) { 893b0104773SPascal Brand /* Fill up complete block */ 894b0104773SPascal Brand if (op->buffer_offs < op->block_size) 895b0104773SPascal Brand l = MIN(slen, op->block_size - op->buffer_offs); 896b0104773SPascal Brand else 897b0104773SPascal Brand l = MIN(slen, buffer_size - op->buffer_offs); 898b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, l); 899b0104773SPascal Brand op->buffer_offs += l; 900b0104773SPascal Brand src += l; 901b0104773SPascal Brand slen -= l; 902b0104773SPascal Brand if ((op->buffer_offs % op->block_size) != 0) 903b0104773SPascal Brand goto out; /* Nothing left to do */ 904b0104773SPascal Brand } 905b0104773SPascal Brand 906b0104773SPascal Brand /* If we can feed from buffer */ 907d3588802SPascal Brand if ((op->buffer_offs > 0) && 908d3588802SPascal Brand ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) { 9092ff3fdbbSPascal Brand l = ROUNDUP(op->buffer_offs + slen - buffer_size, 910b0104773SPascal Brand op->block_size); 911b0104773SPascal Brand l = MIN(op->buffer_offs, l); 912b0104773SPascal Brand tmp_dlen = dlen; 913b0104773SPascal Brand res = update_func(op->state, op->buffer, l, dst, &tmp_dlen); 914b0104773SPascal Brand if (res != TEE_SUCCESS) 915b0104773SPascal Brand TEE_Panic(res); 916b0104773SPascal Brand dst += tmp_dlen; 917b0104773SPascal Brand dlen -= tmp_dlen; 918b0104773SPascal Brand acc_dlen += tmp_dlen; 919b0104773SPascal Brand op->buffer_offs -= l; 920b0104773SPascal Brand if (op->buffer_offs > 0) { 921b0104773SPascal Brand /* 922b0104773SPascal Brand * Slen is small enough to be contained in rest buffer. 923b0104773SPascal Brand */ 924b0104773SPascal Brand memcpy(op->buffer, op->buffer + l, buffer_size - l); 925b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 926b0104773SPascal Brand op->buffer_offs += slen; 927b0104773SPascal Brand goto out; /* Nothing left to do */ 928b0104773SPascal Brand } 929b0104773SPascal Brand } 930b0104773SPascal Brand 931d3588802SPascal Brand if (slen >= (buffer_size + buffer_left)) { 932b0104773SPascal Brand /* Buffer is empty, feed as much as possible from src */ 933bf7a587fSJerome Forissier if (op->info.algorithm == TEE_ALG_AES_CTS) 934b1ecda78SJerome Forissier l = ROUNDUP(slen - buffer_size, op->block_size); 935bf7a587fSJerome Forissier else 936bf7a587fSJerome Forissier l = ROUNDUP(slen - buffer_size + 1, op->block_size); 937b0104773SPascal Brand 938b0104773SPascal Brand tmp_dlen = dlen; 939b0104773SPascal Brand res = update_func(op->state, src, l, dst, &tmp_dlen); 940b0104773SPascal Brand if (res != TEE_SUCCESS) 941b0104773SPascal Brand TEE_Panic(res); 942b0104773SPascal Brand src += l; 943b0104773SPascal Brand slen -= l; 944b0104773SPascal Brand dst += tmp_dlen; 945b0104773SPascal Brand dlen -= tmp_dlen; 946b0104773SPascal Brand acc_dlen += tmp_dlen; 947b0104773SPascal Brand } 948b0104773SPascal Brand 949b0104773SPascal Brand /* Slen is small enough to be contained in buffer. */ 950b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 951b0104773SPascal Brand op->buffer_offs += slen; 952b0104773SPascal Brand 953b0104773SPascal Brand out: 954b0104773SPascal Brand *dest_len = acc_dlen; 955b0104773SPascal Brand return TEE_SUCCESS; 956b0104773SPascal Brand } 957b0104773SPascal Brand 958642a1607SCedric Chaumont TEE_Result TEE_CipherUpdate(TEE_OperationHandle operation, void *srcData, 95979a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 960b0104773SPascal Brand { 961dea1f2b6SCedric Chaumont TEE_Result res; 962b0104773SPascal Brand size_t req_dlen; 963e86f1266SJens Wiklander uint64_t dl; 964b0104773SPascal Brand 965642a1607SCedric Chaumont if (operation == TEE_HANDLE_NULL || 966dea1f2b6SCedric Chaumont (srcData == NULL && srcLen != 0) || 967dea1f2b6SCedric Chaumont destLen == NULL || 968dea1f2b6SCedric Chaumont (destData == NULL && *destLen != 0)) { 969dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 970dea1f2b6SCedric Chaumont goto out; 971dea1f2b6SCedric Chaumont } 972dea1f2b6SCedric Chaumont 973642a1607SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 974dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 975dea1f2b6SCedric Chaumont goto out; 976dea1f2b6SCedric Chaumont } 977dea1f2b6SCedric Chaumont 978642a1607SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 979642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 980642a1607SCedric Chaumont goto out; 981642a1607SCedric Chaumont } 982642a1607SCedric Chaumont 983642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 984dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 985dea1f2b6SCedric Chaumont goto out; 986dea1f2b6SCedric Chaumont } 987b0104773SPascal Brand 988e32c5ddfSJerome Forissier if (!srcData && !srcLen) { 989090268f5SJens Wiklander *destLen = 0; 990e32c5ddfSJerome Forissier res = TEE_SUCCESS; 991e32c5ddfSJerome Forissier goto out; 992e32c5ddfSJerome Forissier } 993e32c5ddfSJerome Forissier 994b0104773SPascal Brand /* Calculate required dlen */ 995642a1607SCedric Chaumont req_dlen = ((operation->buffer_offs + srcLen) / operation->block_size) * 996642a1607SCedric Chaumont operation->block_size; 997642a1607SCedric Chaumont if (operation->buffer_two_blocks) { 998642a1607SCedric Chaumont if (req_dlen > operation->block_size * 2) 999642a1607SCedric Chaumont req_dlen -= operation->block_size * 2; 1000b0104773SPascal Brand else 1001b0104773SPascal Brand req_dlen = 0; 1002b0104773SPascal Brand } 1003b0104773SPascal Brand /* 1004b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1005b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1006b0104773SPascal Brand * can't restore sync with this API. 1007b0104773SPascal Brand */ 1008b0104773SPascal Brand if (*destLen < req_dlen) { 1009b0104773SPascal Brand *destLen = req_dlen; 1010dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1011dea1f2b6SCedric Chaumont goto out; 1012b0104773SPascal Brand } 1013b0104773SPascal Brand 1014e86f1266SJens Wiklander dl = *destLen; 1015642a1607SCedric Chaumont res = tee_buffer_update(operation, utee_cipher_update, srcData, srcLen, 1016e86f1266SJens Wiklander destData, &dl); 1017e86f1266SJens Wiklander *destLen = dl; 1018b0104773SPascal Brand 1019dea1f2b6SCedric Chaumont out: 1020dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 1021dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1022*b36311adSJerome Forissier TEE_Panic(res); 1023dea1f2b6SCedric Chaumont 1024dea1f2b6SCedric Chaumont return res; 1025b0104773SPascal Brand } 1026b0104773SPascal Brand 1027642a1607SCedric Chaumont TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation, 1028642a1607SCedric Chaumont void *srcData, uint32_t srcLen, void *destData, 102979a3c601SCedric Chaumont uint32_t *destLen) 1030b0104773SPascal Brand { 1031b0104773SPascal Brand TEE_Result res; 1032b0104773SPascal Brand uint8_t *dst = destData; 1033b0104773SPascal Brand size_t acc_dlen = 0; 1034e86f1266SJens Wiklander uint64_t tmp_dlen; 1035b0104773SPascal Brand size_t req_dlen; 1036b0104773SPascal Brand 1037642a1607SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1038dea1f2b6SCedric Chaumont (srcData == NULL && srcLen != 0) || 1039dea1f2b6SCedric Chaumont destLen == NULL || 1040dea1f2b6SCedric Chaumont (destData == NULL && *destLen != 0)) { 1041dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1042dea1f2b6SCedric Chaumont goto out; 1043dea1f2b6SCedric Chaumont } 1044dea1f2b6SCedric Chaumont 1045642a1607SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 1046dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1047dea1f2b6SCedric Chaumont goto out; 1048dea1f2b6SCedric Chaumont } 1049dea1f2b6SCedric Chaumont 1050642a1607SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1051642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1052642a1607SCedric Chaumont goto out; 1053642a1607SCedric Chaumont } 1054642a1607SCedric Chaumont 1055642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1056dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1057dea1f2b6SCedric Chaumont goto out; 1058dea1f2b6SCedric Chaumont } 1059b0104773SPascal Brand 1060b0104773SPascal Brand /* 1061b0104773SPascal Brand * Check that the final block doesn't require padding for those 1062b0104773SPascal Brand * algorithms that requires client to supply padding. 1063b0104773SPascal Brand */ 1064642a1607SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD || 1065642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD || 1066642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD || 1067642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD || 1068642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || 1069642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD) { 1070642a1607SCedric Chaumont if (((operation->buffer_offs + srcLen) % operation->block_size) 1071642a1607SCedric Chaumont != 0) { 1072dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1073dea1f2b6SCedric Chaumont goto out; 1074dea1f2b6SCedric Chaumont } 1075b0104773SPascal Brand } 1076b0104773SPascal Brand 1077b0104773SPascal Brand /* 1078b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1079b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1080b0104773SPascal Brand * can't restore sync with this API. 1081b0104773SPascal Brand */ 1082642a1607SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1083b0104773SPascal Brand if (*destLen < req_dlen) { 1084b0104773SPascal Brand *destLen = req_dlen; 1085dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1086dea1f2b6SCedric Chaumont goto out; 1087b0104773SPascal Brand } 1088b0104773SPascal Brand 1089b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1090642a1607SCedric Chaumont res = tee_buffer_update(operation, utee_cipher_update, srcData, srcLen, 1091642a1607SCedric Chaumont dst, &tmp_dlen); 1092dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS) 1093dea1f2b6SCedric Chaumont goto out; 1094dea1f2b6SCedric Chaumont 1095b0104773SPascal Brand dst += tmp_dlen; 1096b0104773SPascal Brand acc_dlen += tmp_dlen; 1097b0104773SPascal Brand 1098b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1099642a1607SCedric Chaumont res = utee_cipher_final(operation->state, operation->buffer, 1100642a1607SCedric Chaumont operation->buffer_offs, dst, &tmp_dlen); 1101b0104773SPascal Brand if (res != TEE_SUCCESS) 1102dea1f2b6SCedric Chaumont goto out; 1103dea1f2b6SCedric Chaumont 1104b0104773SPascal Brand acc_dlen += tmp_dlen; 1105b0104773SPascal Brand *destLen = acc_dlen; 1106dea1f2b6SCedric Chaumont 1107642a1607SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1108642a1607SCedric Chaumont 1109642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1110642a1607SCedric Chaumont 1111dea1f2b6SCedric Chaumont out: 1112dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 1113dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1114*b36311adSJerome Forissier TEE_Panic(res); 1115dea1f2b6SCedric Chaumont 1116dea1f2b6SCedric Chaumont return res; 1117b0104773SPascal Brand } 1118b0104773SPascal Brand 1119b0104773SPascal Brand /* Cryptographic Operations API - MAC Functions */ 1120b0104773SPascal Brand 112128e0efc6SCedric Chaumont void TEE_MACInit(TEE_OperationHandle operation, void *IV, uint32_t IVLen) 1122b0104773SPascal Brand { 1123b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 1124b0104773SPascal Brand TEE_Panic(0); 1125642a1607SCedric Chaumont 1126b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_MAC) 1127b0104773SPascal Brand TEE_Panic(0); 1128642a1607SCedric Chaumont 1129642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 1130642a1607SCedric Chaumont !(operation->key1)) 1131642a1607SCedric Chaumont TEE_Panic(0); 1132642a1607SCedric Chaumont 1133642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 1134642a1607SCedric Chaumont TEE_ResetOperation(operation); 1135642a1607SCedric Chaumont 1136642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1137642a1607SCedric Chaumont 11386d15db08SJerome Forissier init_hash_operation(operation, IV, IVLen); 1139b0104773SPascal Brand } 1140b0104773SPascal Brand 114128e0efc6SCedric Chaumont void TEE_MACUpdate(TEE_OperationHandle operation, void *chunk, 114228e0efc6SCedric Chaumont uint32_t chunkSize) 1143b0104773SPascal Brand { 1144b0104773SPascal Brand TEE_Result res; 1145b0104773SPascal Brand 114628e0efc6SCedric Chaumont if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0)) 1147b0104773SPascal Brand TEE_Panic(0); 1148642a1607SCedric Chaumont 114928e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) 1150b0104773SPascal Brand TEE_Panic(0); 1151642a1607SCedric Chaumont 115228e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1153b0104773SPascal Brand TEE_Panic(0); 1154b0104773SPascal Brand 1155642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) 1156642a1607SCedric Chaumont TEE_Panic(0); 1157642a1607SCedric Chaumont 115828e0efc6SCedric Chaumont res = utee_hash_update(operation->state, chunk, chunkSize); 1159b0104773SPascal Brand if (res != TEE_SUCCESS) 1160b0104773SPascal Brand TEE_Panic(res); 1161b0104773SPascal Brand } 1162b0104773SPascal Brand 116328e0efc6SCedric Chaumont TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation, 116428e0efc6SCedric Chaumont void *message, uint32_t messageLen, 116579a3c601SCedric Chaumont void *mac, uint32_t *macLen) 1166b0104773SPascal Brand { 1167b0104773SPascal Brand TEE_Result res; 1168e86f1266SJens Wiklander uint64_t ml; 1169b0104773SPascal Brand 117028e0efc6SCedric Chaumont if (operation == TEE_HANDLE_NULL || 117128e0efc6SCedric Chaumont (message == NULL && messageLen != 0) || 117228e0efc6SCedric Chaumont mac == NULL || 117328e0efc6SCedric Chaumont macLen == NULL) { 117428e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 117528e0efc6SCedric Chaumont goto out; 117628e0efc6SCedric Chaumont } 1177b0104773SPascal Brand 117828e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 117928e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 118028e0efc6SCedric Chaumont goto out; 118128e0efc6SCedric Chaumont } 118228e0efc6SCedric Chaumont 118328e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 118428e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 118528e0efc6SCedric Chaumont goto out; 118628e0efc6SCedric Chaumont } 118728e0efc6SCedric Chaumont 1188642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1189642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1190642a1607SCedric Chaumont goto out; 1191642a1607SCedric Chaumont } 1192642a1607SCedric Chaumont 1193e86f1266SJens Wiklander ml = *macLen; 1194e86f1266SJens Wiklander res = utee_hash_final(operation->state, message, messageLen, mac, &ml); 1195e86f1266SJens Wiklander *macLen = ml; 119628e0efc6SCedric Chaumont if (res != TEE_SUCCESS) 119728e0efc6SCedric Chaumont goto out; 119828e0efc6SCedric Chaumont 119928e0efc6SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 120028e0efc6SCedric Chaumont 1201642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1202642a1607SCedric Chaumont 120328e0efc6SCedric Chaumont out: 120428e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 120528e0efc6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 120628e0efc6SCedric Chaumont TEE_Panic(res); 120728e0efc6SCedric Chaumont 1208b0104773SPascal Brand return res; 1209b0104773SPascal Brand } 1210b0104773SPascal Brand 1211b0104773SPascal Brand TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation, 121228e0efc6SCedric Chaumont void *message, uint32_t messageLen, 121328e0efc6SCedric Chaumont void *mac, uint32_t macLen) 1214b0104773SPascal Brand { 1215b0104773SPascal Brand TEE_Result res; 1216b0104773SPascal Brand uint8_t computed_mac[TEE_MAX_HASH_SIZE]; 12177f74c64aSPascal Brand uint32_t computed_mac_size = TEE_MAX_HASH_SIZE; 1218b0104773SPascal Brand 121928e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 122028e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 122128e0efc6SCedric Chaumont goto out; 122228e0efc6SCedric Chaumont } 122328e0efc6SCedric Chaumont 122428e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 122528e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 122628e0efc6SCedric Chaumont goto out; 122728e0efc6SCedric Chaumont } 122828e0efc6SCedric Chaumont 1229642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1230642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1231642a1607SCedric Chaumont goto out; 1232642a1607SCedric Chaumont } 1233642a1607SCedric Chaumont 1234b0104773SPascal Brand res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac, 1235b0104773SPascal Brand &computed_mac_size); 1236b0104773SPascal Brand if (res != TEE_SUCCESS) 123728e0efc6SCedric Chaumont goto out; 123828e0efc6SCedric Chaumont 123928e0efc6SCedric Chaumont if (computed_mac_size != macLen) { 124028e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 124128e0efc6SCedric Chaumont goto out; 124228e0efc6SCedric Chaumont } 124328e0efc6SCedric Chaumont 124428e0efc6SCedric Chaumont if (buf_compare_ct(mac, computed_mac, computed_mac_size) != 0) { 124528e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 124628e0efc6SCedric Chaumont goto out; 124728e0efc6SCedric Chaumont } 124828e0efc6SCedric Chaumont 1249642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1250642a1607SCedric Chaumont 125128e0efc6SCedric Chaumont out: 125228e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 125328e0efc6SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 125428e0efc6SCedric Chaumont TEE_Panic(res); 125528e0efc6SCedric Chaumont 1256b0104773SPascal Brand return res; 1257b0104773SPascal Brand } 1258b0104773SPascal Brand 1259b0104773SPascal Brand /* Cryptographic Operations API - Authenticated Encryption Functions */ 1260b0104773SPascal Brand 1261b5816c88SCedric Chaumont TEE_Result TEE_AEInit(TEE_OperationHandle operation, void *nonce, 126279a3c601SCedric Chaumont uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen, 1263b0104773SPascal Brand uint32_t payloadLen) 1264b0104773SPascal Brand { 1265b0104773SPascal Brand TEE_Result res; 1266b0104773SPascal Brand 1267b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || nonce == NULL) { 1268b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1269b5816c88SCedric Chaumont goto out; 1270b5816c88SCedric Chaumont } 1271b5816c88SCedric Chaumont 1272b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1273b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1274b5816c88SCedric Chaumont goto out; 1275b5816c88SCedric Chaumont } 1276b0104773SPascal Brand 1277642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 1278642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1279642a1607SCedric Chaumont goto out; 1280642a1607SCedric Chaumont } 1281642a1607SCedric Chaumont 1282b0104773SPascal Brand /* 1283b0104773SPascal Brand * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core 1284b0104773SPascal Brand * in the implementation. But AES-GCM spec doesn't specify the tag len 1285b0104773SPascal Brand * according to the same principle so we have to check here instead to 1286b0104773SPascal Brand * be GP compliant. 1287b0104773SPascal Brand */ 1288b5816c88SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_GCM) { 1289b0104773SPascal Brand /* 1290b0104773SPascal Brand * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96 1291b0104773SPascal Brand */ 1292b5816c88SCedric Chaumont if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) { 1293b5816c88SCedric Chaumont res = TEE_ERROR_NOT_SUPPORTED; 1294b5816c88SCedric Chaumont goto out; 1295b5816c88SCedric Chaumont } 1296b0104773SPascal Brand } 1297b0104773SPascal Brand 1298b5816c88SCedric Chaumont res = utee_authenc_init(operation->state, nonce, nonceLen, 1299b5816c88SCedric Chaumont tagLen / 8, AADLen, payloadLen); 1300b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1301b5816c88SCedric Chaumont goto out; 1302b5816c88SCedric Chaumont 1303b5816c88SCedric Chaumont operation->ae_tag_len = tagLen / 8; 1304b5816c88SCedric Chaumont operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 1305b5816c88SCedric Chaumont 1306b5816c88SCedric Chaumont out: 1307b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1308b5816c88SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 1309b0104773SPascal Brand TEE_Panic(res); 1310b5816c88SCedric Chaumont 1311b0104773SPascal Brand return res; 1312b0104773SPascal Brand } 1313b0104773SPascal Brand 1314b5816c88SCedric Chaumont void TEE_AEUpdateAAD(TEE_OperationHandle operation, void *AADdata, 131579a3c601SCedric Chaumont uint32_t AADdataLen) 1316b0104773SPascal Brand { 1317b0104773SPascal Brand TEE_Result res; 1318b0104773SPascal Brand 1319b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1320b5816c88SCedric Chaumont (AADdata == NULL && AADdataLen != 0)) 1321b0104773SPascal Brand TEE_Panic(0); 1322642a1607SCedric Chaumont 1323b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) 1324b0104773SPascal Brand TEE_Panic(0); 1325642a1607SCedric Chaumont 1326b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1327b0104773SPascal Brand TEE_Panic(0); 1328b0104773SPascal Brand 1329b5816c88SCedric Chaumont res = utee_authenc_update_aad(operation->state, AADdata, AADdataLen); 1330642a1607SCedric Chaumont 1331642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1332642a1607SCedric Chaumont 1333b0104773SPascal Brand if (res != TEE_SUCCESS) 1334b0104773SPascal Brand TEE_Panic(res); 1335b0104773SPascal Brand } 1336b0104773SPascal Brand 1337b5816c88SCedric Chaumont TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, void *srcData, 133879a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 1339b0104773SPascal Brand { 1340b5816c88SCedric Chaumont TEE_Result res; 1341b0104773SPascal Brand size_t req_dlen; 1342e86f1266SJens Wiklander uint64_t dl; 1343b0104773SPascal Brand 1344b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1345b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1346b5816c88SCedric Chaumont destLen == NULL || 1347b5816c88SCedric Chaumont (destData == NULL && *destLen != 0)) { 1348b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1349b5816c88SCedric Chaumont goto out; 1350b5816c88SCedric Chaumont } 1351b5816c88SCedric Chaumont 1352b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1353b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1354b5816c88SCedric Chaumont goto out; 1355b5816c88SCedric Chaumont } 1356b5816c88SCedric Chaumont 1357b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1358b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1359b5816c88SCedric Chaumont goto out; 1360b5816c88SCedric Chaumont } 1361b0104773SPascal Brand 1362827308b8SJerome Forissier if (!srcData && !srcLen) { 1363090268f5SJens Wiklander *destLen = 0; 1364827308b8SJerome Forissier res = TEE_SUCCESS; 1365827308b8SJerome Forissier goto out; 1366827308b8SJerome Forissier } 1367827308b8SJerome Forissier 1368b0104773SPascal Brand /* 1369b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1370b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1371b0104773SPascal Brand * can't restore sync with this API. 1372b0104773SPascal Brand */ 1373b5816c88SCedric Chaumont req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen, 1374b5816c88SCedric Chaumont operation->block_size); 1375b0104773SPascal Brand if (*destLen < req_dlen) { 1376b0104773SPascal Brand *destLen = req_dlen; 1377b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1378b5816c88SCedric Chaumont goto out; 1379b0104773SPascal Brand } 1380b0104773SPascal Brand 1381e86f1266SJens Wiklander dl = *destLen; 1382b5816c88SCedric Chaumont res = tee_buffer_update(operation, utee_authenc_update_payload, srcData, 1383e86f1266SJens Wiklander srcLen, destData, &dl); 1384e86f1266SJens Wiklander *destLen = dl; 1385b0104773SPascal Brand 1386642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1387642a1607SCedric Chaumont 1388b5816c88SCedric Chaumont out: 1389b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1390b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1391b5816c88SCedric Chaumont TEE_Panic(res); 1392b5816c88SCedric Chaumont 1393b5816c88SCedric Chaumont return res; 1394b0104773SPascal Brand } 1395b0104773SPascal Brand 1396b5816c88SCedric Chaumont TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation, 1397b5816c88SCedric Chaumont void *srcData, uint32_t srcLen, 139879a3c601SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 139979a3c601SCedric Chaumont uint32_t *tagLen) 1400b0104773SPascal Brand { 1401b0104773SPascal Brand TEE_Result res; 1402b0104773SPascal Brand uint8_t *dst = destData; 1403b0104773SPascal Brand size_t acc_dlen = 0; 1404e86f1266SJens Wiklander uint64_t tmp_dlen; 1405b0104773SPascal Brand size_t req_dlen; 1406e86f1266SJens Wiklander uint64_t tl; 1407b0104773SPascal Brand 1408b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1409b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1410b5816c88SCedric Chaumont destLen == NULL || 1411b5816c88SCedric Chaumont (destData == NULL && *destLen != 0) || 1412b5816c88SCedric Chaumont tag == NULL || tagLen == NULL) { 1413b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1414b5816c88SCedric Chaumont goto out; 1415b5816c88SCedric Chaumont } 1416b5816c88SCedric Chaumont 1417b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1418b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1419b5816c88SCedric Chaumont goto out; 1420b5816c88SCedric Chaumont } 1421b5816c88SCedric Chaumont 1422b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1423b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1424b5816c88SCedric Chaumont goto out; 1425b5816c88SCedric Chaumont } 1426b0104773SPascal Brand 1427b0104773SPascal Brand /* 1428b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1429b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1430b0104773SPascal Brand * can't restore sync with this API. 1431b0104773SPascal Brand */ 1432b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1433b0104773SPascal Brand if (*destLen < req_dlen) { 1434b0104773SPascal Brand *destLen = req_dlen; 1435b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1436b5816c88SCedric Chaumont goto out; 1437b0104773SPascal Brand } 1438b0104773SPascal Brand 1439b0104773SPascal Brand /* 1440b0104773SPascal Brand * Need to check this before update_payload since sync would be lost if 1441b0104773SPascal Brand * we return short buffer after that. 1442b0104773SPascal Brand */ 1443b5816c88SCedric Chaumont if (*tagLen < operation->ae_tag_len) { 1444b5816c88SCedric Chaumont *tagLen = operation->ae_tag_len; 1445b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1446b5816c88SCedric Chaumont goto out; 1447b0104773SPascal Brand } 1448b0104773SPascal Brand 1449b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1450b5816c88SCedric Chaumont res = tee_buffer_update(operation, utee_authenc_update_payload, srcData, 1451b5816c88SCedric Chaumont srcLen, dst, &tmp_dlen); 1452b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1453b5816c88SCedric Chaumont goto out; 1454b5816c88SCedric Chaumont 1455b0104773SPascal Brand dst += tmp_dlen; 1456b0104773SPascal Brand acc_dlen += tmp_dlen; 1457b0104773SPascal Brand 1458b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1459e86f1266SJens Wiklander tl = *tagLen; 1460b5816c88SCedric Chaumont res = utee_authenc_enc_final(operation->state, operation->buffer, 1461b5816c88SCedric Chaumont operation->buffer_offs, dst, &tmp_dlen, 1462e86f1266SJens Wiklander tag, &tl); 1463e86f1266SJens Wiklander *tagLen = tl; 1464b0104773SPascal Brand if (res != TEE_SUCCESS) 1465b5816c88SCedric Chaumont goto out; 1466b0104773SPascal Brand 1467b5816c88SCedric Chaumont acc_dlen += tmp_dlen; 1468b0104773SPascal Brand *destLen = acc_dlen; 1469642a1607SCedric Chaumont 1470b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1471b5816c88SCedric Chaumont 1472642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1473642a1607SCedric Chaumont 1474b5816c88SCedric Chaumont out: 1475b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1476b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1477b5816c88SCedric Chaumont TEE_Panic(res); 1478b0104773SPascal Brand 1479b0104773SPascal Brand return res; 1480b0104773SPascal Brand } 1481b0104773SPascal Brand 1482b5816c88SCedric Chaumont TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation, 1483b5816c88SCedric Chaumont void *srcData, uint32_t srcLen, 1484b5816c88SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 148579a3c601SCedric Chaumont uint32_t tagLen) 1486b0104773SPascal Brand { 1487b0104773SPascal Brand TEE_Result res; 1488b0104773SPascal Brand uint8_t *dst = destData; 1489b0104773SPascal Brand size_t acc_dlen = 0; 1490e86f1266SJens Wiklander uint64_t tmp_dlen; 1491b0104773SPascal Brand size_t req_dlen; 1492b0104773SPascal Brand 1493b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1494b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1495b5816c88SCedric Chaumont destLen == NULL || 1496b5816c88SCedric Chaumont (destData == NULL && *destLen != 0) || 1497b5816c88SCedric Chaumont (tag == NULL && tagLen != 0)) { 1498b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1499b5816c88SCedric Chaumont goto out; 1500b5816c88SCedric Chaumont } 1501b5816c88SCedric Chaumont 1502b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1503b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1504b5816c88SCedric Chaumont goto out; 1505b5816c88SCedric Chaumont } 1506b5816c88SCedric Chaumont 1507b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1508b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1509b5816c88SCedric Chaumont goto out; 1510b5816c88SCedric Chaumont } 1511b0104773SPascal Brand 1512b0104773SPascal Brand /* 1513b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1514b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1515b0104773SPascal Brand * can't restore sync with this API. 1516b0104773SPascal Brand */ 1517b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1518b0104773SPascal Brand if (*destLen < req_dlen) { 1519b0104773SPascal Brand *destLen = req_dlen; 1520b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1521b5816c88SCedric Chaumont goto out; 1522b0104773SPascal Brand } 1523b0104773SPascal Brand 1524b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1525b5816c88SCedric Chaumont res = tee_buffer_update(operation, utee_authenc_update_payload, srcData, 1526b5816c88SCedric Chaumont srcLen, dst, &tmp_dlen); 1527b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1528b5816c88SCedric Chaumont goto out; 1529b5816c88SCedric Chaumont 1530b0104773SPascal Brand dst += tmp_dlen; 1531b0104773SPascal Brand acc_dlen += tmp_dlen; 1532b0104773SPascal Brand 1533b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1534b5816c88SCedric Chaumont res = utee_authenc_dec_final(operation->state, operation->buffer, 1535b5816c88SCedric Chaumont operation->buffer_offs, dst, &tmp_dlen, 1536b5816c88SCedric Chaumont tag, tagLen); 1537b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1538b5816c88SCedric Chaumont goto out; 1539b5816c88SCedric Chaumont 1540b0104773SPascal Brand /* Supplied tagLen should match what we initiated with */ 1541b5816c88SCedric Chaumont if (tagLen != operation->ae_tag_len) 1542b0104773SPascal Brand res = TEE_ERROR_MAC_INVALID; 1543b0104773SPascal Brand 1544b0104773SPascal Brand acc_dlen += tmp_dlen; 1545b0104773SPascal Brand *destLen = acc_dlen; 1546642a1607SCedric Chaumont 1547b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1548b5816c88SCedric Chaumont 1549642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1550642a1607SCedric Chaumont 1551b5816c88SCedric Chaumont out: 1552b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1553b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER && 1554b5816c88SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 1555b5816c88SCedric Chaumont TEE_Panic(res); 1556b0104773SPascal Brand 1557b0104773SPascal Brand return res; 1558b0104773SPascal Brand } 1559b0104773SPascal Brand 1560b0104773SPascal Brand /* Cryptographic Operations API - Asymmetric Functions */ 1561b0104773SPascal Brand 156212e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation, 156312e66b6fSCedric Chaumont TEE_Attribute *params, 156412e66b6fSCedric Chaumont uint32_t paramCount, void *srcData, 156579a3c601SCedric Chaumont uint32_t srcLen, void *destData, 156679a3c601SCedric Chaumont uint32_t *destLen) 1567b0104773SPascal Brand { 1568b0104773SPascal Brand TEE_Result res; 1569e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1570e86f1266SJens Wiklander uint64_t dl; 1571b0104773SPascal Brand 157212e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1573b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 1574b0104773SPascal Brand TEE_Panic(0); 157512e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1576b0104773SPascal Brand TEE_Panic(0); 157712e66b6fSCedric Chaumont if (!operation->key1) 1578b0104773SPascal Brand TEE_Panic(0); 157912e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 158012e66b6fSCedric Chaumont TEE_Panic(0); 158112e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_ENCRYPT) 1582b0104773SPascal Brand TEE_Panic(0); 1583b0104773SPascal Brand 1584e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1585e86f1266SJens Wiklander dl = *destLen; 1586e86f1266SJens Wiklander res = utee_asymm_operate(operation->state, ua, paramCount, srcData, 1587e86f1266SJens Wiklander srcLen, destData, &dl); 1588e86f1266SJens Wiklander *destLen = dl; 158912e66b6fSCedric Chaumont 15908844ebfcSPascal Brand if (res != TEE_SUCCESS && 15918844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 15928844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1593b0104773SPascal Brand TEE_Panic(res); 159412e66b6fSCedric Chaumont 1595b0104773SPascal Brand return res; 1596b0104773SPascal Brand } 1597b0104773SPascal Brand 159812e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation, 159912e66b6fSCedric Chaumont TEE_Attribute *params, 160012e66b6fSCedric Chaumont uint32_t paramCount, void *srcData, 160179a3c601SCedric Chaumont uint32_t srcLen, void *destData, 160279a3c601SCedric Chaumont uint32_t *destLen) 1603b0104773SPascal Brand { 1604b0104773SPascal Brand TEE_Result res; 1605e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1606e86f1266SJens Wiklander uint64_t dl; 1607b0104773SPascal Brand 160812e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1609b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 1610b0104773SPascal Brand TEE_Panic(0); 161112e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1612b0104773SPascal Brand TEE_Panic(0); 161312e66b6fSCedric Chaumont if (!operation->key1) 1614b0104773SPascal Brand TEE_Panic(0); 161512e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 161612e66b6fSCedric Chaumont TEE_Panic(0); 161712e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_DECRYPT) 1618b0104773SPascal Brand TEE_Panic(0); 1619b0104773SPascal Brand 1620e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1621e86f1266SJens Wiklander dl = *destLen; 1622e86f1266SJens Wiklander res = utee_asymm_operate(operation->state, ua, paramCount, srcData, 1623e86f1266SJens Wiklander srcLen, destData, &dl); 1624e86f1266SJens Wiklander *destLen = dl; 162512e66b6fSCedric Chaumont 16268844ebfcSPascal Brand if (res != TEE_SUCCESS && 16278844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 16288844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1629b0104773SPascal Brand TEE_Panic(res); 163012e66b6fSCedric Chaumont 1631b0104773SPascal Brand return res; 1632b0104773SPascal Brand } 1633b0104773SPascal Brand 163412e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation, 163512e66b6fSCedric Chaumont TEE_Attribute *params, 163612e66b6fSCedric Chaumont uint32_t paramCount, void *digest, 163779a3c601SCedric Chaumont uint32_t digestLen, void *signature, 163879a3c601SCedric Chaumont uint32_t *signatureLen) 1639b0104773SPascal Brand { 1640b0104773SPascal Brand TEE_Result res; 1641e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1642e86f1266SJens Wiklander uint64_t sl; 1643b0104773SPascal Brand 164412e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || 164512e66b6fSCedric Chaumont (digest == NULL && digestLen != 0) || 1646b0104773SPascal Brand signature == NULL || signatureLen == NULL) 1647b0104773SPascal Brand TEE_Panic(0); 164812e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1649b0104773SPascal Brand TEE_Panic(0); 165012e66b6fSCedric Chaumont if (!operation->key1) 1651b0104773SPascal Brand TEE_Panic(0); 165212e66b6fSCedric Chaumont if (operation->info.operationClass != 165312e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 165412e66b6fSCedric Chaumont TEE_Panic(0); 165512e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_SIGN) 1656b0104773SPascal Brand TEE_Panic(0); 1657b0104773SPascal Brand 1658e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1659e86f1266SJens Wiklander sl = *signatureLen; 1660e86f1266SJens Wiklander res = utee_asymm_operate(operation->state, ua, paramCount, digest, 1661e86f1266SJens Wiklander digestLen, signature, &sl); 1662e86f1266SJens Wiklander *signatureLen = sl; 166312e66b6fSCedric Chaumont 1664b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER) 1665b0104773SPascal Brand TEE_Panic(res); 166612e66b6fSCedric Chaumont 1667b0104773SPascal Brand return res; 1668b0104773SPascal Brand } 1669b0104773SPascal Brand 167012e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation, 167112e66b6fSCedric Chaumont TEE_Attribute *params, 167212e66b6fSCedric Chaumont uint32_t paramCount, void *digest, 167312e66b6fSCedric Chaumont uint32_t digestLen, void *signature, 167479a3c601SCedric Chaumont uint32_t signatureLen) 1675b0104773SPascal Brand { 1676b0104773SPascal Brand TEE_Result res; 1677e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1678b0104773SPascal Brand 167912e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || 168012e66b6fSCedric Chaumont (digest == NULL && digestLen != 0) || 1681b0104773SPascal Brand (signature == NULL && signatureLen != 0)) 1682b0104773SPascal Brand TEE_Panic(0); 168312e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1684b0104773SPascal Brand TEE_Panic(0); 168512e66b6fSCedric Chaumont if (!operation->key1) 1686b0104773SPascal Brand TEE_Panic(0); 168712e66b6fSCedric Chaumont if (operation->info.operationClass != 168812e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 168912e66b6fSCedric Chaumont TEE_Panic(0); 169012e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_VERIFY) 1691b0104773SPascal Brand TEE_Panic(0); 1692b0104773SPascal Brand 1693e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1694e86f1266SJens Wiklander res = utee_asymm_verify(operation->state, ua, paramCount, digest, 169512e66b6fSCedric Chaumont digestLen, signature, signatureLen); 169612e66b6fSCedric Chaumont 1697b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID) 1698b0104773SPascal Brand TEE_Panic(res); 169912e66b6fSCedric Chaumont 1700b0104773SPascal Brand return res; 1701b0104773SPascal Brand } 1702b0104773SPascal Brand 1703b0104773SPascal Brand /* Cryptographic Operations API - Key Derivation Functions */ 1704b0104773SPascal Brand 1705b0104773SPascal Brand void TEE_DeriveKey(TEE_OperationHandle operation, 1706b0104773SPascal Brand const TEE_Attribute *params, uint32_t paramCount, 1707b0104773SPascal Brand TEE_ObjectHandle derivedKey) 1708b0104773SPascal Brand { 1709b0104773SPascal Brand TEE_Result res; 1710b0104773SPascal Brand TEE_ObjectInfo key_info; 1711e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1712b0104773SPascal Brand 1713b0104773SPascal Brand if (operation == TEE_HANDLE_NULL || derivedKey == 0) 1714b0104773SPascal Brand TEE_Panic(0); 171584fa9467SCedric Chaumont if (params == NULL && paramCount != 0) 1716b0104773SPascal Brand TEE_Panic(0); 17178854d3c6SJerome Forissier if (TEE_ALG_GET_CLASS(operation->info.algorithm) != 17188854d3c6SJerome Forissier TEE_OPERATION_KEY_DERIVATION) 1719b0104773SPascal Brand TEE_Panic(0); 1720b0104773SPascal Brand 1721b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION) 1722b0104773SPascal Brand TEE_Panic(0); 172384fa9467SCedric Chaumont if (!operation->key1) 172484fa9467SCedric Chaumont TEE_Panic(0); 1725b0104773SPascal Brand if (operation->info.mode != TEE_MODE_DERIVE) 1726b0104773SPascal Brand TEE_Panic(0); 1727b0104773SPascal Brand if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0) 1728b0104773SPascal Brand TEE_Panic(0); 1729b0104773SPascal Brand 1730e86f1266SJens Wiklander res = utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info); 1731b0104773SPascal Brand if (res != TEE_SUCCESS) 1732*b36311adSJerome Forissier TEE_Panic(res); 1733b0104773SPascal Brand 1734b0104773SPascal Brand if (key_info.objectType != TEE_TYPE_GENERIC_SECRET) 1735b0104773SPascal Brand TEE_Panic(0); 1736b0104773SPascal Brand if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1737b0104773SPascal Brand TEE_Panic(0); 1738b0104773SPascal Brand 1739e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1740e86f1266SJens Wiklander res = utee_cryp_derive_key(operation->state, ua, paramCount, 1741e86f1266SJens Wiklander (unsigned long)derivedKey); 1742b0104773SPascal Brand if (res != TEE_SUCCESS) 1743b0104773SPascal Brand TEE_Panic(res); 1744b0104773SPascal Brand } 1745b0104773SPascal Brand 1746b0104773SPascal Brand /* Cryptographic Operations API - Random Number Generation Functions */ 1747b0104773SPascal Brand 174879a3c601SCedric Chaumont void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen) 1749b0104773SPascal Brand { 1750b0104773SPascal Brand TEE_Result res; 1751b0104773SPascal Brand 1752b0104773SPascal Brand res = utee_cryp_random_number_generate(randomBuffer, randomBufferLen); 1753b0104773SPascal Brand if (res != TEE_SUCCESS) 1754b0104773SPascal Brand TEE_Panic(res); 1755b0104773SPascal Brand } 1756