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; 134b0104773SPascal Brand /*FALLTHROUGH*/ case TEE_ALG_AES_ECB_NOPAD: 135b0104773SPascal Brand case TEE_ALG_AES_CBC_NOPAD: 136b0104773SPascal Brand case TEE_ALG_AES_CTR: 137b0104773SPascal Brand case TEE_ALG_AES_CCM: 138b0104773SPascal Brand case TEE_ALG_AES_GCM: 139b0104773SPascal Brand case TEE_ALG_DES_ECB_NOPAD: 140b0104773SPascal Brand case TEE_ALG_DES_CBC_NOPAD: 141b0104773SPascal Brand case TEE_ALG_DES3_ECB_NOPAD: 142b0104773SPascal Brand case TEE_ALG_DES3_CBC_NOPAD: 143b0104773SPascal Brand if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES) 144b0104773SPascal Brand block_size = TEE_AES_BLOCK_SIZE; 145b0104773SPascal Brand else 146b0104773SPascal Brand block_size = TEE_DES_BLOCK_SIZE; 147b0104773SPascal Brand 148b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) 149b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT; 150b0104773SPascal Brand else if (mode == TEE_MODE_DECRYPT) 151b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT; 152b0104773SPascal Brand else 153b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 154b0104773SPascal Brand break; 155b0104773SPascal Brand 156b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_MD5: 157b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1: 158b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224: 159b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256: 160b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384: 161b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512: 162b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1: 163b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224: 164b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256: 165b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384: 166b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512: 167b0104773SPascal Brand case TEE_ALG_DSA_SHA1: 168218d9055SCedric Chaumont case TEE_ALG_DSA_SHA224: 169218d9055SCedric Chaumont case TEE_ALG_DSA_SHA256: 1701220586eSCedric Chaumont case TEE_ALG_ECDSA_P192: 1711220586eSCedric Chaumont case TEE_ALG_ECDSA_P224: 1721220586eSCedric Chaumont case TEE_ALG_ECDSA_P256: 1731220586eSCedric Chaumont case TEE_ALG_ECDSA_P384: 1741220586eSCedric Chaumont case TEE_ALG_ECDSA_P521: 175b0104773SPascal Brand if (mode == TEE_MODE_SIGN) { 176b0104773SPascal Brand with_private_key = true; 177b0104773SPascal Brand req_key_usage = TEE_USAGE_SIGN; 178b0104773SPascal Brand } else if (mode == TEE_MODE_VERIFY) { 179b0104773SPascal Brand req_key_usage = TEE_USAGE_VERIFY; 180b0104773SPascal Brand } else { 181b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 182b0104773SPascal Brand } 183b0104773SPascal Brand break; 184b0104773SPascal Brand 185b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_V1_5: 186b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1: 187b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224: 188b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256: 189b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384: 190b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512: 191b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) { 192b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT; 193b0104773SPascal Brand } else if (mode == TEE_MODE_DECRYPT) { 194b0104773SPascal Brand with_private_key = true; 195b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT; 196b0104773SPascal Brand } else { 197b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 198b0104773SPascal Brand } 199b0104773SPascal Brand break; 200b0104773SPascal Brand 201b0104773SPascal Brand case TEE_ALG_RSA_NOPAD: 202b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) { 203b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY; 204b0104773SPascal Brand } else if (mode == TEE_MODE_DECRYPT) { 205b0104773SPascal Brand with_private_key = true; 206b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT | TEE_USAGE_SIGN; 207b0104773SPascal Brand } else { 208b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 209b0104773SPascal Brand } 210b0104773SPascal Brand break; 211b0104773SPascal Brand 212b0104773SPascal Brand case TEE_ALG_DH_DERIVE_SHARED_SECRET: 2131220586eSCedric Chaumont case TEE_ALG_ECDH_P192: 2141220586eSCedric Chaumont case TEE_ALG_ECDH_P224: 2151220586eSCedric Chaumont case TEE_ALG_ECDH_P256: 2161220586eSCedric Chaumont case TEE_ALG_ECDH_P384: 2171220586eSCedric Chaumont case TEE_ALG_ECDH_P521: 218cdb198a7SJerome Forissier case TEE_ALG_HKDF_MD5_DERIVE_KEY: 219cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA1_DERIVE_KEY: 220cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA224_DERIVE_KEY: 221cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA256_DERIVE_KEY: 222cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA384_DERIVE_KEY: 223cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA512_DERIVE_KEY: 2248854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA1_DERIVE_KEY: 2258854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA224_DERIVE_KEY: 2268854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA256_DERIVE_KEY: 2278854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA384_DERIVE_KEY: 2288854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA512_DERIVE_KEY: 2290f2293b7SJerome Forissier case TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY: 230b0104773SPascal Brand if (mode != TEE_MODE_DERIVE) 231b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 232b0104773SPascal Brand with_private_key = true; 233b0104773SPascal Brand req_key_usage = TEE_USAGE_DERIVE; 234b0104773SPascal Brand break; 235b0104773SPascal Brand 236b0104773SPascal Brand case TEE_ALG_MD5: 237b0104773SPascal Brand case TEE_ALG_SHA1: 238b0104773SPascal Brand case TEE_ALG_SHA224: 239b0104773SPascal Brand case TEE_ALG_SHA256: 240b0104773SPascal Brand case TEE_ALG_SHA384: 241b0104773SPascal Brand case TEE_ALG_SHA512: 242b0104773SPascal Brand if (mode != TEE_MODE_DIGEST) 243b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 24405304565SCedric Chaumont /* v1.1: flags always set for digest operations */ 245b0104773SPascal Brand handle_state |= TEE_HANDLE_FLAG_KEY_SET; 246b0104773SPascal Brand req_key_usage = 0; 247b0104773SPascal Brand break; 248b0104773SPascal Brand 249b0104773SPascal Brand case TEE_ALG_DES_CBC_MAC_NOPAD: 250b0104773SPascal Brand case TEE_ALG_AES_CBC_MAC_NOPAD: 251b0104773SPascal Brand case TEE_ALG_AES_CBC_MAC_PKCS5: 252b0104773SPascal Brand case TEE_ALG_AES_CMAC: 253b0104773SPascal Brand case TEE_ALG_DES_CBC_MAC_PKCS5: 254b0104773SPascal Brand case TEE_ALG_DES3_CBC_MAC_NOPAD: 255b0104773SPascal Brand case TEE_ALG_DES3_CBC_MAC_PKCS5: 256b0104773SPascal Brand case TEE_ALG_HMAC_MD5: 257b0104773SPascal Brand case TEE_ALG_HMAC_SHA1: 258b0104773SPascal Brand case TEE_ALG_HMAC_SHA224: 259b0104773SPascal Brand case TEE_ALG_HMAC_SHA256: 260b0104773SPascal Brand case TEE_ALG_HMAC_SHA384: 261b0104773SPascal Brand case TEE_ALG_HMAC_SHA512: 262b0104773SPascal Brand if (mode != TEE_MODE_MAC) 263b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 264b0104773SPascal Brand req_key_usage = TEE_USAGE_MAC; 265b0104773SPascal Brand break; 266b0104773SPascal Brand 267b0104773SPascal Brand default: 268b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 269b0104773SPascal Brand } 270b0104773SPascal Brand 271b66f219bSJens Wiklander op = TEE_Malloc(sizeof(*op), TEE_MALLOC_FILL_ZERO); 2729b52c538SCedric Chaumont if (!op) 273b0104773SPascal Brand return TEE_ERROR_OUT_OF_MEMORY; 274b0104773SPascal Brand 275b0104773SPascal Brand op->info.algorithm = algorithm; 276b0104773SPascal Brand op->info.operationClass = TEE_ALG_GET_CLASS(algorithm); 277b0104773SPascal Brand op->info.mode = mode; 278b0104773SPascal Brand op->info.maxKeySize = maxKeySize; 279b0104773SPascal Brand op->info.requiredKeyUsage = req_key_usage; 280b0104773SPascal Brand op->info.handleState = handle_state; 281b0104773SPascal Brand 282b0104773SPascal Brand if (block_size > 1) { 283b0104773SPascal Brand size_t buffer_size = block_size; 284b0104773SPascal Brand 285b0104773SPascal Brand if (buffer_two_blocks) 286b0104773SPascal Brand buffer_size *= 2; 287b0104773SPascal Brand 2889b52c538SCedric Chaumont op->buffer = TEE_Malloc(buffer_size, 2899b52c538SCedric Chaumont TEE_USER_MEM_HINT_NO_FILL_ZERO); 290b0104773SPascal Brand if (op->buffer == NULL) { 291b0104773SPascal Brand res = TEE_ERROR_OUT_OF_MEMORY; 292b66f219bSJens Wiklander goto out; 293b0104773SPascal Brand } 294b0104773SPascal Brand } 295b0104773SPascal Brand op->block_size = block_size; 296b0104773SPascal Brand op->buffer_two_blocks = buffer_two_blocks; 297b0104773SPascal Brand 298b0104773SPascal Brand if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) { 299b0104773SPascal Brand uint32_t mks = maxKeySize; 300b0104773SPascal Brand TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm, 301b0104773SPascal Brand with_private_key); 302b0104773SPascal Brand 303b0104773SPascal Brand /* 304b0104773SPascal Brand * If two keys are expected the max key size is the sum of 305b0104773SPascal Brand * the size of both keys. 306b0104773SPascal Brand */ 307b0104773SPascal Brand if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) 308b0104773SPascal Brand mks /= 2; 309b0104773SPascal Brand 310b0104773SPascal Brand res = TEE_AllocateTransientObject(key_type, mks, &op->key1); 311b0104773SPascal Brand if (res != TEE_SUCCESS) 312b66f219bSJens Wiklander goto out; 313b0104773SPascal Brand 31405304565SCedric Chaumont if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) { 3159b52c538SCedric Chaumont res = TEE_AllocateTransientObject(key_type, mks, 316b0104773SPascal Brand &op->key2); 317b0104773SPascal Brand if (res != TEE_SUCCESS) 318b66f219bSJens Wiklander goto out; 319b0104773SPascal Brand } 320b0104773SPascal Brand } 321b0104773SPascal Brand 322e86f1266SJens Wiklander res = utee_cryp_state_alloc(algorithm, mode, (unsigned long)op->key1, 323e86f1266SJens Wiklander (unsigned long)op->key2, &op->state); 324b66f219bSJens Wiklander if (res != TEE_SUCCESS) 325b66f219bSJens Wiklander goto out; 326b0104773SPascal Brand 32705304565SCedric Chaumont /* 32805304565SCedric Chaumont * Initialize digest operations 32905304565SCedric Chaumont * Other multi-stage operations initialized w/ TEE_xxxInit functions 33005304565SCedric Chaumont * Non-applicable on asymmetric operations 33105304565SCedric Chaumont */ 33205304565SCedric Chaumont if (TEE_ALG_GET_CLASS(algorithm) == TEE_OPERATION_DIGEST) { 33305304565SCedric Chaumont res = utee_hash_init(op->state, NULL, 0); 33405304565SCedric Chaumont if (res != TEE_SUCCESS) 335b66f219bSJens Wiklander goto out; 33605304565SCedric Chaumont /* v1.1: flags always set for digest operations */ 33705304565SCedric Chaumont op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 33805304565SCedric Chaumont } 33905304565SCedric Chaumont 340642a1607SCedric Chaumont op->operationState = TEE_OPERATION_STATE_INITIAL; 341642a1607SCedric Chaumont 342b0104773SPascal Brand *operation = op; 343b0104773SPascal Brand 344b66f219bSJens Wiklander out: 345b66f219bSJens Wiklander if (res != TEE_SUCCESS) { 346b66f219bSJens Wiklander if (res != TEE_ERROR_OUT_OF_MEMORY && 3479b52c538SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 3489b52c538SCedric Chaumont TEE_Panic(0); 349b66f219bSJens Wiklander if (op) { 350b66f219bSJens Wiklander if (op->state) { 351b66f219bSJens Wiklander TEE_FreeOperation(op); 352b66f219bSJens Wiklander } else { 353b66f219bSJens Wiklander TEE_Free(op->buffer); 354b66f219bSJens Wiklander TEE_FreeTransientObject(op->key1); 355b66f219bSJens Wiklander TEE_FreeTransientObject(op->key2); 356b66f219bSJens Wiklander TEE_Free(op); 357b66f219bSJens Wiklander } 358b66f219bSJens Wiklander } 359b66f219bSJens Wiklander } 360b66f219bSJens Wiklander 361b0104773SPascal Brand return res; 362b0104773SPascal Brand } 363b0104773SPascal Brand 364b0104773SPascal Brand void TEE_FreeOperation(TEE_OperationHandle operation) 365b0104773SPascal Brand { 366e889e80bSCedric Chaumont TEE_Result res; 367e889e80bSCedric Chaumont 368e889e80bSCedric Chaumont if (operation == TEE_HANDLE_NULL) 369e889e80bSCedric Chaumont TEE_Panic(0); 370e889e80bSCedric Chaumont 371b0104773SPascal Brand /* 372b0104773SPascal Brand * Note that keys should not be freed here, since they are 373b0104773SPascal Brand * claimed by the operation they will be freed by 374b0104773SPascal Brand * utee_cryp_state_free(). 375b0104773SPascal Brand */ 376e889e80bSCedric Chaumont res = utee_cryp_state_free(operation->state); 377e889e80bSCedric Chaumont if (res != TEE_SUCCESS) 378e889e80bSCedric Chaumont TEE_Panic(0); 379e889e80bSCedric Chaumont 380b0104773SPascal Brand TEE_Free(operation->buffer); 381b0104773SPascal Brand TEE_Free(operation); 382b0104773SPascal Brand } 383b0104773SPascal Brand 384b0104773SPascal Brand void TEE_GetOperationInfo(TEE_OperationHandle operation, 385b0104773SPascal Brand TEE_OperationInfo *operationInfo) 386b0104773SPascal Brand { 387b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 388b0104773SPascal Brand TEE_Panic(0); 389b0104773SPascal Brand 39005304565SCedric Chaumont if (!operationInfo) 391b0104773SPascal Brand TEE_Panic(0); 392b0104773SPascal Brand 393b0104773SPascal Brand *operationInfo = operation->info; 394b0104773SPascal Brand } 395b0104773SPascal Brand 39605304565SCedric Chaumont TEE_Result TEE_GetOperationInfoMultiple(TEE_OperationHandle operation, 39705304565SCedric Chaumont TEE_OperationInfoMultiple *operationInfoMultiple, 39805304565SCedric Chaumont uint32_t *operationSize) 39905304565SCedric Chaumont { 40005304565SCedric Chaumont TEE_Result res = TEE_SUCCESS; 40105304565SCedric Chaumont TEE_ObjectInfo key_info1; 40205304565SCedric Chaumont TEE_ObjectInfo key_info2; 40305304565SCedric Chaumont uint32_t num_of_keys; 40405304565SCedric Chaumont size_t n; 40505304565SCedric Chaumont 40605304565SCedric Chaumont if (operation == TEE_HANDLE_NULL) { 40705304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 40805304565SCedric Chaumont goto out; 40905304565SCedric Chaumont } 41005304565SCedric Chaumont 41105304565SCedric Chaumont if (!operationInfoMultiple) { 41205304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 41305304565SCedric Chaumont goto out; 41405304565SCedric Chaumont } 41505304565SCedric Chaumont 41605304565SCedric Chaumont if (!operationSize) { 41705304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 41805304565SCedric Chaumont goto out; 41905304565SCedric Chaumont } 42005304565SCedric Chaumont 42105304565SCedric Chaumont num_of_keys = (*operationSize-sizeof(TEE_OperationInfoMultiple))/ 42205304565SCedric Chaumont sizeof(TEE_OperationInfoKey); 42305304565SCedric Chaumont 42405304565SCedric Chaumont if (num_of_keys > 2) { 42505304565SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 42605304565SCedric Chaumont goto out; 42705304565SCedric Chaumont } 42805304565SCedric Chaumont 42905304565SCedric Chaumont /* Two keys flag (TEE_ALG_AES_XTS only) */ 43005304565SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 43105304565SCedric Chaumont 0 && 43205304565SCedric Chaumont (num_of_keys != 2)) { 43305304565SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 43405304565SCedric Chaumont goto out; 43505304565SCedric Chaumont } 43605304565SCedric Chaumont 43705304565SCedric Chaumont /* Clear */ 43805304565SCedric Chaumont for (n = 0; n < num_of_keys; n++) { 43905304565SCedric Chaumont operationInfoMultiple->keyInformation[n].keySize = 0; 44005304565SCedric Chaumont operationInfoMultiple->keyInformation[n].requiredKeyUsage = 0; 44105304565SCedric Chaumont } 44205304565SCedric Chaumont 44305304565SCedric Chaumont if (num_of_keys == 2) { 44405304565SCedric Chaumont res = TEE_GetObjectInfo1(operation->key2, &key_info2); 44505304565SCedric Chaumont /* Key2 is not a valid handle */ 44605304565SCedric Chaumont if (res != TEE_SUCCESS) 44705304565SCedric Chaumont goto out; 44805304565SCedric Chaumont 44905304565SCedric Chaumont operationInfoMultiple->keyInformation[1].keySize = 45005304565SCedric Chaumont key_info2.keySize; 45105304565SCedric Chaumont operationInfoMultiple->keyInformation[1].requiredKeyUsage = 45205304565SCedric Chaumont operation->info.requiredKeyUsage; 45305304565SCedric Chaumont } 45405304565SCedric Chaumont 45505304565SCedric Chaumont if (num_of_keys >= 1) { 45605304565SCedric Chaumont res = TEE_GetObjectInfo1(operation->key1, &key_info1); 45705304565SCedric Chaumont /* Key1 is not a valid handle */ 45805304565SCedric Chaumont if (res != TEE_SUCCESS) { 45905304565SCedric Chaumont if (num_of_keys == 2) { 46005304565SCedric Chaumont operationInfoMultiple->keyInformation[1]. 46105304565SCedric Chaumont keySize = 0; 46205304565SCedric Chaumont operationInfoMultiple->keyInformation[1]. 46305304565SCedric Chaumont requiredKeyUsage = 0; 46405304565SCedric Chaumont } 46505304565SCedric Chaumont goto out; 46605304565SCedric Chaumont } 46705304565SCedric Chaumont 46805304565SCedric Chaumont operationInfoMultiple->keyInformation[0].keySize = 46905304565SCedric Chaumont key_info1.keySize; 47005304565SCedric Chaumont operationInfoMultiple->keyInformation[0].requiredKeyUsage = 47105304565SCedric Chaumont operation->info.requiredKeyUsage; 47205304565SCedric Chaumont } 47305304565SCedric Chaumont 47405304565SCedric Chaumont /* No key */ 47505304565SCedric Chaumont operationInfoMultiple->algorithm = operation->info.algorithm; 47605304565SCedric Chaumont operationInfoMultiple->operationClass = operation->info.operationClass; 47705304565SCedric Chaumont operationInfoMultiple->mode = operation->info.mode; 47805304565SCedric Chaumont operationInfoMultiple->digestLength = operation->info.digestLength; 47905304565SCedric Chaumont operationInfoMultiple->maxKeySize = operation->info.maxKeySize; 48005304565SCedric Chaumont operationInfoMultiple->handleState = operation->info.handleState; 48105304565SCedric Chaumont operationInfoMultiple->operationState = operation->operationState; 48205304565SCedric Chaumont operationInfoMultiple->numberOfKeys = num_of_keys; 48305304565SCedric Chaumont 48405304565SCedric Chaumont out: 48505304565SCedric Chaumont if (res != TEE_SUCCESS && 48605304565SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 48705304565SCedric Chaumont TEE_Panic(0); 48805304565SCedric Chaumont 48905304565SCedric Chaumont return res; 49005304565SCedric Chaumont } 49105304565SCedric Chaumont 492b0104773SPascal Brand void TEE_ResetOperation(TEE_OperationHandle operation) 493b0104773SPascal Brand { 494b0104773SPascal Brand TEE_Result res; 495b0104773SPascal Brand 496b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 497b0104773SPascal Brand TEE_Panic(0); 498bf80076aSCedric Chaumont 499642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET)) 500bf80076aSCedric Chaumont TEE_Panic(0); 501bf80076aSCedric Chaumont 502642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 503642a1607SCedric Chaumont 504b0104773SPascal Brand if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 505b0104773SPascal Brand res = utee_hash_init(operation->state, NULL, 0); 506b0104773SPascal Brand if (res != TEE_SUCCESS) 507b0104773SPascal Brand TEE_Panic(res); 50805304565SCedric Chaumont operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 50905304565SCedric Chaumont } else { 510b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 511b0104773SPascal Brand } 51205304565SCedric Chaumont } 513b0104773SPascal Brand 514b0104773SPascal Brand TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation, 515b0104773SPascal Brand TEE_ObjectHandle key) 516b0104773SPascal Brand { 5177583c59eSCedric Chaumont TEE_Result res; 518b0104773SPascal Brand uint32_t key_size = 0; 519b0104773SPascal Brand TEE_ObjectInfo key_info; 520b0104773SPascal Brand 521a57c1e2eSCedric Chaumont if (operation == TEE_HANDLE_NULL) { 522a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 523a57c1e2eSCedric Chaumont goto out; 524a57c1e2eSCedric Chaumont } 525a57c1e2eSCedric Chaumont 526642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 527642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 528642a1607SCedric Chaumont goto out; 529642a1607SCedric Chaumont } 530642a1607SCedric Chaumont 531a57c1e2eSCedric Chaumont if (key == TEE_HANDLE_NULL) { 532a57c1e2eSCedric Chaumont /* Operation key cleared */ 533a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key1); 534a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 535a57c1e2eSCedric Chaumont goto out; 536a57c1e2eSCedric Chaumont } 537a57c1e2eSCedric Chaumont 538a57c1e2eSCedric Chaumont /* No key for digest operation */ 539a57c1e2eSCedric Chaumont if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 540a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 541a57c1e2eSCedric Chaumont goto out; 542a57c1e2eSCedric Chaumont } 543a57c1e2eSCedric Chaumont 544a57c1e2eSCedric Chaumont /* Two keys flag not expected (TEE_ALG_AES_XTS excluded) */ 545a57c1e2eSCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 546a57c1e2eSCedric Chaumont 0) { 547a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 548a57c1e2eSCedric Chaumont goto out; 549a57c1e2eSCedric Chaumont } 550a57c1e2eSCedric Chaumont 5517583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key, &key_info); 552a57c1e2eSCedric Chaumont /* Key is not a valid handle */ 5537583c59eSCedric Chaumont if (res != TEE_SUCCESS) 554a57c1e2eSCedric Chaumont goto out; 5557583c59eSCedric Chaumont 556b0104773SPascal Brand /* Supplied key has to meet required usage */ 557b0104773SPascal Brand if ((key_info.objectUsage & operation->info.requiredKeyUsage) != 558b0104773SPascal Brand operation->info.requiredKeyUsage) { 559a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 560a57c1e2eSCedric Chaumont goto out; 561b0104773SPascal Brand } 562b0104773SPascal Brand 563a57c1e2eSCedric Chaumont if (operation->info.maxKeySize < key_info.keySize) { 564a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 565a57c1e2eSCedric Chaumont goto out; 566a57c1e2eSCedric Chaumont } 567b0104773SPascal Brand 5687583c59eSCedric Chaumont key_size = key_info.keySize; 569b0104773SPascal Brand 570b0104773SPascal Brand TEE_ResetTransientObject(operation->key1); 571b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 572b0104773SPascal Brand 5737583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key1, key); 5747583c59eSCedric Chaumont if (res != TEE_SUCCESS) 575a57c1e2eSCedric Chaumont goto out; 5767583c59eSCedric Chaumont 577b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 578b0104773SPascal Brand 579b0104773SPascal Brand operation->info.keySize = key_size; 580b0104773SPascal Brand 5817583c59eSCedric Chaumont out: 582a57c1e2eSCedric Chaumont if (res != TEE_SUCCESS && 583a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 584a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 585a57c1e2eSCedric Chaumont TEE_Panic(0); 586a57c1e2eSCedric Chaumont 587a57c1e2eSCedric Chaumont return res; 588b0104773SPascal Brand } 589b0104773SPascal Brand 590b0104773SPascal Brand TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation, 591b0104773SPascal Brand TEE_ObjectHandle key1, TEE_ObjectHandle key2) 592b0104773SPascal Brand { 5937583c59eSCedric Chaumont TEE_Result res; 594b0104773SPascal Brand uint32_t key_size = 0; 595b0104773SPascal Brand TEE_ObjectInfo key_info1; 596b0104773SPascal Brand TEE_ObjectInfo key_info2; 597b0104773SPascal Brand 598a57c1e2eSCedric Chaumont if (operation == TEE_HANDLE_NULL) { 599a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 600a57c1e2eSCedric Chaumont goto out; 601a57c1e2eSCedric Chaumont } 602a57c1e2eSCedric Chaumont 603642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 604642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 605642a1607SCedric Chaumont goto out; 606642a1607SCedric Chaumont } 607642a1607SCedric Chaumont 608a57c1e2eSCedric Chaumont /* 609a57c1e2eSCedric Chaumont * Key1/Key2 and/or are not initialized and 610a57c1e2eSCedric Chaumont * Either both keys are NULL or both are not NULL 611a57c1e2eSCedric Chaumont */ 612a57c1e2eSCedric Chaumont if (key1 == TEE_HANDLE_NULL || key2 == TEE_HANDLE_NULL) { 613a57c1e2eSCedric Chaumont /* Clear operation key1 (if needed) */ 614a57c1e2eSCedric Chaumont if (key1 == TEE_HANDLE_NULL) 615a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key1); 616a57c1e2eSCedric Chaumont /* Clear operation key2 (if needed) */ 617a57c1e2eSCedric Chaumont if (key2 == TEE_HANDLE_NULL) 618a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key2); 619a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 620a57c1e2eSCedric Chaumont goto out; 621a57c1e2eSCedric Chaumont } 622a57c1e2eSCedric Chaumont 623a57c1e2eSCedric Chaumont /* No key for digest operation */ 624a57c1e2eSCedric Chaumont if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 625a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 626a57c1e2eSCedric Chaumont goto out; 627a57c1e2eSCedric Chaumont } 628a57c1e2eSCedric Chaumont 629a57c1e2eSCedric Chaumont /* Two keys flag expected (TEE_ALG_AES_XTS only) */ 630a57c1e2eSCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 631a57c1e2eSCedric Chaumont 0) { 632a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 633a57c1e2eSCedric Chaumont goto out; 634a57c1e2eSCedric Chaumont } 635a57c1e2eSCedric Chaumont 6367583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key1, &key_info1); 637a57c1e2eSCedric Chaumont /* Key1 is not a valid handle */ 6387583c59eSCedric Chaumont if (res != TEE_SUCCESS) 639a57c1e2eSCedric Chaumont goto out; 6407583c59eSCedric Chaumont 641b0104773SPascal Brand /* Supplied key has to meet required usage */ 642b0104773SPascal Brand if ((key_info1.objectUsage & operation->info. 643b0104773SPascal Brand requiredKeyUsage) != operation->info.requiredKeyUsage) { 644a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 645a57c1e2eSCedric Chaumont goto out; 646b0104773SPascal Brand } 647b0104773SPascal Brand 6487583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key2, &key_info2); 649a57c1e2eSCedric Chaumont /* Key2 is not a valid handle */ 6507583c59eSCedric Chaumont if (res != TEE_SUCCESS) { 6517583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) 6527583c59eSCedric Chaumont res = TEE_ERROR_CORRUPT_OBJECT_2; 653a57c1e2eSCedric Chaumont goto out; 6547583c59eSCedric Chaumont } 6557583c59eSCedric Chaumont 656b0104773SPascal Brand /* Supplied key has to meet required usage */ 657b0104773SPascal Brand if ((key_info2.objectUsage & operation->info. 658b0104773SPascal Brand requiredKeyUsage) != operation->info.requiredKeyUsage) { 659a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 660a57c1e2eSCedric Chaumont goto out; 661b0104773SPascal Brand } 662b0104773SPascal Brand 663b0104773SPascal Brand /* 664b0104773SPascal Brand * AES-XTS (the only multi key algorithm supported, requires the 665b0104773SPascal Brand * keys to be of equal size. 666b0104773SPascal Brand */ 667b0104773SPascal Brand if (operation->info.algorithm == TEE_ALG_AES_XTS && 668a57c1e2eSCedric Chaumont key_info1.keySize != key_info2.keySize) { 669a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 670a57c1e2eSCedric Chaumont goto out; 671b0104773SPascal Brand 672a57c1e2eSCedric Chaumont } 673a57c1e2eSCedric Chaumont 674a57c1e2eSCedric Chaumont if (operation->info.maxKeySize < key_info1.keySize) { 675a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 676a57c1e2eSCedric Chaumont goto out; 677a57c1e2eSCedric Chaumont } 678b0104773SPascal Brand 679b0104773SPascal Brand /* 680b0104773SPascal Brand * Odd that only the size of one key should be reported while 681b0104773SPascal Brand * size of two key are used when allocating the operation. 682b0104773SPascal Brand */ 6837583c59eSCedric Chaumont key_size = key_info1.keySize; 684b0104773SPascal Brand 685b0104773SPascal Brand TEE_ResetTransientObject(operation->key1); 686b0104773SPascal Brand TEE_ResetTransientObject(operation->key2); 687b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 688b0104773SPascal Brand 6897583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key1, key1); 6907583c59eSCedric Chaumont if (res != TEE_SUCCESS) 691a57c1e2eSCedric Chaumont goto out; 6927583c59eSCedric Chaumont 6937583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key2, key2); 6947583c59eSCedric Chaumont if (res != TEE_SUCCESS) { 6957583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) 6967583c59eSCedric Chaumont res = TEE_ERROR_CORRUPT_OBJECT_2; 697a57c1e2eSCedric Chaumont goto out; 6987583c59eSCedric Chaumont } 6997583c59eSCedric Chaumont 700b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 701b0104773SPascal Brand 702b0104773SPascal Brand operation->info.keySize = key_size; 703b0104773SPascal Brand 7047583c59eSCedric Chaumont out: 705a57c1e2eSCedric Chaumont if (res != TEE_SUCCESS && 706a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 707a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT_2 && 708a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE && 709a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE_2) 710a57c1e2eSCedric Chaumont TEE_Panic(0); 711a57c1e2eSCedric Chaumont 712a57c1e2eSCedric Chaumont return res; 713b0104773SPascal Brand } 714b0104773SPascal Brand 715b0104773SPascal Brand void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op) 716b0104773SPascal Brand { 717b0104773SPascal Brand TEE_Result res; 718b0104773SPascal Brand 719b0104773SPascal Brand if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL) 720b0104773SPascal Brand TEE_Panic(0); 721b0104773SPascal Brand if (dst_op->info.algorithm != src_op->info.algorithm) 722b0104773SPascal Brand TEE_Panic(0); 723b0104773SPascal Brand if (src_op->info.operationClass != TEE_OPERATION_DIGEST) { 724b0104773SPascal Brand TEE_ObjectHandle key1 = TEE_HANDLE_NULL; 725b0104773SPascal Brand TEE_ObjectHandle key2 = TEE_HANDLE_NULL; 726b0104773SPascal Brand 727b0104773SPascal Brand if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) { 728b0104773SPascal Brand key1 = src_op->key1; 729b0104773SPascal Brand key2 = src_op->key2; 730b0104773SPascal Brand } 731b0104773SPascal Brand 732b0104773SPascal Brand if ((src_op->info.handleState & 733b0104773SPascal Brand TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) { 734b0104773SPascal Brand TEE_SetOperationKey(dst_op, key1); 735b0104773SPascal Brand } else { 736b0104773SPascal Brand TEE_SetOperationKey2(dst_op, key1, key2); 737b0104773SPascal Brand } 738b0104773SPascal Brand } 739b0104773SPascal Brand dst_op->info.handleState = src_op->info.handleState; 740b0104773SPascal Brand dst_op->info.keySize = src_op->info.keySize; 741642a1607SCedric Chaumont dst_op->operationState = src_op->operationState; 742b0104773SPascal Brand 743b0104773SPascal Brand if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks || 744b0104773SPascal Brand dst_op->block_size != src_op->block_size) 745b0104773SPascal Brand TEE_Panic(0); 746b0104773SPascal Brand 747b0104773SPascal Brand if (dst_op->buffer != NULL) { 748b0104773SPascal Brand if (src_op->buffer == NULL) 749b0104773SPascal Brand TEE_Panic(0); 750b0104773SPascal Brand 751b0104773SPascal Brand memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs); 752b0104773SPascal Brand dst_op->buffer_offs = src_op->buffer_offs; 753b0104773SPascal Brand } else if (src_op->buffer != NULL) { 754b0104773SPascal Brand TEE_Panic(0); 755b0104773SPascal Brand } 756b0104773SPascal Brand 757b0104773SPascal Brand res = utee_cryp_state_copy(dst_op->state, src_op->state); 758b0104773SPascal Brand if (res != TEE_SUCCESS) 759b0104773SPascal Brand TEE_Panic(res); 760b0104773SPascal Brand } 761b0104773SPascal Brand 762b0104773SPascal Brand /* Cryptographic Operations API - Message Digest Functions */ 763b0104773SPascal Brand 7646d15db08SJerome Forissier static void init_hash_operation(TEE_OperationHandle operation, void *IV, 7656d15db08SJerome Forissier uint32_t IVLen) 7666d15db08SJerome Forissier { 7676d15db08SJerome Forissier TEE_Result res; 7686d15db08SJerome Forissier 7696d15db08SJerome Forissier /* 7706d15db08SJerome Forissier * Note : IV and IVLen are never used in current implementation 7716d15db08SJerome Forissier * This is why coherent values of IV and IVLen are not checked 7726d15db08SJerome Forissier */ 7736d15db08SJerome Forissier res = utee_hash_init(operation->state, IV, IVLen); 7746d15db08SJerome Forissier if (res != TEE_SUCCESS) 7756d15db08SJerome Forissier TEE_Panic(res); 7766d15db08SJerome Forissier operation->buffer_offs = 0; 7776d15db08SJerome Forissier operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 7786d15db08SJerome Forissier } 7796d15db08SJerome Forissier 780b0104773SPascal Brand void TEE_DigestUpdate(TEE_OperationHandle operation, 78179a3c601SCedric Chaumont void *chunk, uint32_t chunkSize) 782b0104773SPascal Brand { 78373d6c3baSJoakim Bech TEE_Result res = TEE_ERROR_GENERIC; 784b0104773SPascal Brand 78573d6c3baSJoakim Bech if (operation == TEE_HANDLE_NULL || 78673d6c3baSJoakim Bech operation->info.operationClass != TEE_OPERATION_DIGEST) 787b0104773SPascal Brand TEE_Panic(0); 78873d6c3baSJoakim Bech 789642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 790642a1607SCedric Chaumont 791b0104773SPascal Brand res = utee_hash_update(operation->state, chunk, chunkSize); 792b0104773SPascal Brand if (res != TEE_SUCCESS) 793b0104773SPascal Brand TEE_Panic(res); 794b0104773SPascal Brand } 795b0104773SPascal Brand 796642a1607SCedric Chaumont TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, void *chunk, 79779a3c601SCedric Chaumont uint32_t chunkLen, void *hash, uint32_t *hashLen) 798b0104773SPascal Brand { 79987c2f6b6SCedric Chaumont TEE_Result res; 800e86f1266SJens Wiklander uint64_t hl; 80187c2f6b6SCedric Chaumont 80287c2f6b6SCedric Chaumont if ((operation == TEE_HANDLE_NULL) || 80387c2f6b6SCedric Chaumont (!chunk && chunkLen) || 80487c2f6b6SCedric Chaumont !hash || 80587c2f6b6SCedric Chaumont !hashLen || 80687c2f6b6SCedric Chaumont (operation->info.operationClass != TEE_OPERATION_DIGEST)) { 80787c2f6b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 80887c2f6b6SCedric Chaumont goto out; 80987c2f6b6SCedric Chaumont } 81087c2f6b6SCedric Chaumont 811e86f1266SJens Wiklander hl = *hashLen; 812e86f1266SJens Wiklander res = utee_hash_final(operation->state, chunk, chunkLen, hash, &hl); 813e86f1266SJens Wiklander *hashLen = hl; 8146d15db08SJerome Forissier if (res != TEE_SUCCESS) 8156d15db08SJerome Forissier goto out; 8166d15db08SJerome Forissier 8176d15db08SJerome Forissier /* Reset operation state */ 8186d15db08SJerome Forissier init_hash_operation(operation, NULL, 0); 81987c2f6b6SCedric Chaumont 820642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 821642a1607SCedric Chaumont 82287c2f6b6SCedric Chaumont out: 82387c2f6b6SCedric Chaumont if (res != TEE_SUCCESS && 82487c2f6b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 825b0104773SPascal Brand TEE_Panic(0); 82673d6c3baSJoakim Bech 82787c2f6b6SCedric Chaumont return res; 828b0104773SPascal Brand } 829b0104773SPascal Brand 830b0104773SPascal Brand /* Cryptographic Operations API - Symmetric Cipher Functions */ 831b0104773SPascal Brand 832642a1607SCedric Chaumont void TEE_CipherInit(TEE_OperationHandle operation, void *IV, uint32_t IVLen) 833b0104773SPascal Brand { 834b0104773SPascal Brand TEE_Result res; 835b0104773SPascal Brand 836b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 837b0104773SPascal Brand TEE_Panic(0); 838642a1607SCedric Chaumont 839b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_CIPHER) 840b0104773SPascal Brand TEE_Panic(0); 841642a1607SCedric Chaumont 842642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 843642a1607SCedric Chaumont !(operation->key1)) 844642a1607SCedric Chaumont TEE_Panic(0); 845642a1607SCedric Chaumont 846642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 847642a1607SCedric Chaumont TEE_ResetOperation(operation); 848642a1607SCedric Chaumont 849642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 850642a1607SCedric Chaumont 851b0104773SPascal Brand res = utee_cipher_init(operation->state, IV, IVLen); 852b0104773SPascal Brand if (res != TEE_SUCCESS) 853b0104773SPascal Brand TEE_Panic(res); 854642a1607SCedric Chaumont 855b0104773SPascal Brand operation->buffer_offs = 0; 856b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 857b0104773SPascal Brand } 858b0104773SPascal Brand 859b0104773SPascal Brand static TEE_Result tee_buffer_update( 860b0104773SPascal Brand TEE_OperationHandle op, 861e86f1266SJens Wiklander TEE_Result(*update_func)(unsigned long state, const void *src, 862e86f1266SJens Wiklander size_t slen, void *dst, uint64_t *dlen), 863b0104773SPascal Brand const void *src_data, size_t src_len, 864e86f1266SJens Wiklander void *dest_data, uint64_t *dest_len) 865b0104773SPascal Brand { 866b0104773SPascal Brand TEE_Result res; 867b0104773SPascal Brand const uint8_t *src = src_data; 868b0104773SPascal Brand size_t slen = src_len; 869b0104773SPascal Brand uint8_t *dst = dest_data; 870b0104773SPascal Brand size_t dlen = *dest_len; 871b0104773SPascal Brand size_t acc_dlen = 0; 872e86f1266SJens Wiklander uint64_t tmp_dlen; 873b0104773SPascal Brand size_t l; 874b0104773SPascal Brand size_t buffer_size; 875d3588802SPascal Brand size_t buffer_left; 876b0104773SPascal Brand 877d3588802SPascal Brand if (op->buffer_two_blocks) { 878b0104773SPascal Brand buffer_size = op->block_size * 2; 879d3588802SPascal Brand buffer_left = 1; 880d3588802SPascal Brand } else { 881b0104773SPascal Brand buffer_size = op->block_size; 882d3588802SPascal Brand buffer_left = 0; 883d3588802SPascal Brand } 884b0104773SPascal Brand 885b0104773SPascal Brand if (op->buffer_offs > 0) { 886b0104773SPascal Brand /* Fill up complete block */ 887b0104773SPascal Brand if (op->buffer_offs < op->block_size) 888b0104773SPascal Brand l = MIN(slen, op->block_size - op->buffer_offs); 889b0104773SPascal Brand else 890b0104773SPascal Brand l = MIN(slen, buffer_size - op->buffer_offs); 891b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, l); 892b0104773SPascal Brand op->buffer_offs += l; 893b0104773SPascal Brand src += l; 894b0104773SPascal Brand slen -= l; 895b0104773SPascal Brand if ((op->buffer_offs % op->block_size) != 0) 896b0104773SPascal Brand goto out; /* Nothing left to do */ 897b0104773SPascal Brand } 898b0104773SPascal Brand 899b0104773SPascal Brand /* If we can feed from buffer */ 900d3588802SPascal Brand if ((op->buffer_offs > 0) && 901d3588802SPascal Brand ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) { 9022ff3fdbbSPascal Brand l = ROUNDUP(op->buffer_offs + slen - buffer_size, 903b0104773SPascal Brand op->block_size); 904b0104773SPascal Brand l = MIN(op->buffer_offs, l); 905b0104773SPascal Brand tmp_dlen = dlen; 906b0104773SPascal Brand res = update_func(op->state, op->buffer, l, dst, &tmp_dlen); 907b0104773SPascal Brand if (res != TEE_SUCCESS) 908b0104773SPascal Brand TEE_Panic(res); 909b0104773SPascal Brand dst += tmp_dlen; 910b0104773SPascal Brand dlen -= tmp_dlen; 911b0104773SPascal Brand acc_dlen += tmp_dlen; 912b0104773SPascal Brand op->buffer_offs -= l; 913b0104773SPascal Brand if (op->buffer_offs > 0) { 914b0104773SPascal Brand /* 915b0104773SPascal Brand * Slen is small enough to be contained in rest buffer. 916b0104773SPascal Brand */ 917b0104773SPascal Brand memcpy(op->buffer, op->buffer + l, buffer_size - l); 918b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 919b0104773SPascal Brand op->buffer_offs += slen; 920b0104773SPascal Brand goto out; /* Nothing left to do */ 921b0104773SPascal Brand } 922b0104773SPascal Brand } 923b0104773SPascal Brand 924d3588802SPascal Brand if (slen >= (buffer_size + buffer_left)) { 925b0104773SPascal Brand /* Buffer is empty, feed as much as possible from src */ 9262ff3fdbbSPascal Brand l = ROUNDUP(slen - buffer_size + 1, op->block_size); 927b0104773SPascal Brand 928b0104773SPascal Brand tmp_dlen = dlen; 929b0104773SPascal Brand res = update_func(op->state, src, l, dst, &tmp_dlen); 930b0104773SPascal Brand if (res != TEE_SUCCESS) 931b0104773SPascal Brand TEE_Panic(res); 932b0104773SPascal Brand src += l; 933b0104773SPascal Brand slen -= l; 934b0104773SPascal Brand dst += tmp_dlen; 935b0104773SPascal Brand dlen -= tmp_dlen; 936b0104773SPascal Brand acc_dlen += tmp_dlen; 937b0104773SPascal Brand } 938b0104773SPascal Brand 939b0104773SPascal Brand /* Slen is small enough to be contained in buffer. */ 940b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 941b0104773SPascal Brand op->buffer_offs += slen; 942b0104773SPascal Brand 943b0104773SPascal Brand out: 944b0104773SPascal Brand *dest_len = acc_dlen; 945b0104773SPascal Brand return TEE_SUCCESS; 946b0104773SPascal Brand } 947b0104773SPascal Brand 948642a1607SCedric Chaumont TEE_Result TEE_CipherUpdate(TEE_OperationHandle operation, void *srcData, 94979a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 950b0104773SPascal Brand { 951dea1f2b6SCedric Chaumont TEE_Result res; 952b0104773SPascal Brand size_t req_dlen; 953e86f1266SJens Wiklander uint64_t dl; 954b0104773SPascal Brand 955642a1607SCedric Chaumont if (operation == TEE_HANDLE_NULL || 956dea1f2b6SCedric Chaumont (srcData == NULL && srcLen != 0) || 957dea1f2b6SCedric Chaumont destLen == NULL || 958dea1f2b6SCedric Chaumont (destData == NULL && *destLen != 0)) { 959dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 960dea1f2b6SCedric Chaumont goto out; 961dea1f2b6SCedric Chaumont } 962dea1f2b6SCedric Chaumont 963642a1607SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 964dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 965dea1f2b6SCedric Chaumont goto out; 966dea1f2b6SCedric Chaumont } 967dea1f2b6SCedric Chaumont 968642a1607SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 969642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 970642a1607SCedric Chaumont goto out; 971642a1607SCedric Chaumont } 972642a1607SCedric Chaumont 973642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 974dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 975dea1f2b6SCedric Chaumont goto out; 976dea1f2b6SCedric Chaumont } 977b0104773SPascal Brand 978*e32c5ddfSJerome Forissier if (!srcData && !srcLen) { 979*e32c5ddfSJerome Forissier res = TEE_SUCCESS; 980*e32c5ddfSJerome Forissier goto out; 981*e32c5ddfSJerome Forissier } 982*e32c5ddfSJerome Forissier 983b0104773SPascal Brand /* Calculate required dlen */ 984642a1607SCedric Chaumont req_dlen = ((operation->buffer_offs + srcLen) / operation->block_size) * 985642a1607SCedric Chaumont operation->block_size; 986642a1607SCedric Chaumont if (operation->buffer_two_blocks) { 987642a1607SCedric Chaumont if (req_dlen > operation->block_size * 2) 988642a1607SCedric Chaumont req_dlen -= operation->block_size * 2; 989b0104773SPascal Brand else 990b0104773SPascal Brand req_dlen = 0; 991b0104773SPascal Brand } 992b0104773SPascal Brand /* 993b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 994b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 995b0104773SPascal Brand * can't restore sync with this API. 996b0104773SPascal Brand */ 997b0104773SPascal Brand if (*destLen < req_dlen) { 998b0104773SPascal Brand *destLen = req_dlen; 999dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1000dea1f2b6SCedric Chaumont goto out; 1001b0104773SPascal Brand } 1002b0104773SPascal Brand 1003e86f1266SJens Wiklander dl = *destLen; 1004642a1607SCedric Chaumont res = tee_buffer_update(operation, utee_cipher_update, srcData, srcLen, 1005e86f1266SJens Wiklander destData, &dl); 1006e86f1266SJens Wiklander *destLen = dl; 1007b0104773SPascal Brand 1008dea1f2b6SCedric Chaumont out: 1009dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 1010dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1011dea1f2b6SCedric Chaumont TEE_Panic(0); 1012dea1f2b6SCedric Chaumont 1013dea1f2b6SCedric Chaumont return res; 1014b0104773SPascal Brand } 1015b0104773SPascal Brand 1016642a1607SCedric Chaumont TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation, 1017642a1607SCedric Chaumont void *srcData, uint32_t srcLen, void *destData, 101879a3c601SCedric Chaumont uint32_t *destLen) 1019b0104773SPascal Brand { 1020b0104773SPascal Brand TEE_Result res; 1021b0104773SPascal Brand uint8_t *dst = destData; 1022b0104773SPascal Brand size_t acc_dlen = 0; 1023e86f1266SJens Wiklander uint64_t tmp_dlen; 1024b0104773SPascal Brand size_t req_dlen; 1025b0104773SPascal Brand 1026642a1607SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1027dea1f2b6SCedric Chaumont (srcData == NULL && srcLen != 0) || 1028dea1f2b6SCedric Chaumont destLen == NULL || 1029dea1f2b6SCedric Chaumont (destData == NULL && *destLen != 0)) { 1030dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1031dea1f2b6SCedric Chaumont goto out; 1032dea1f2b6SCedric Chaumont } 1033dea1f2b6SCedric Chaumont 1034642a1607SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 1035dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1036dea1f2b6SCedric Chaumont goto out; 1037dea1f2b6SCedric Chaumont } 1038dea1f2b6SCedric Chaumont 1039642a1607SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1040642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1041642a1607SCedric Chaumont goto out; 1042642a1607SCedric Chaumont } 1043642a1607SCedric Chaumont 1044642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1045dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1046dea1f2b6SCedric Chaumont goto out; 1047dea1f2b6SCedric Chaumont } 1048b0104773SPascal Brand 1049b0104773SPascal Brand /* 1050b0104773SPascal Brand * Check that the final block doesn't require padding for those 1051b0104773SPascal Brand * algorithms that requires client to supply padding. 1052b0104773SPascal Brand */ 1053642a1607SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD || 1054642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD || 1055642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD || 1056642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD || 1057642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || 1058642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD) { 1059642a1607SCedric Chaumont if (((operation->buffer_offs + srcLen) % operation->block_size) 1060642a1607SCedric Chaumont != 0) { 1061dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1062dea1f2b6SCedric Chaumont goto out; 1063dea1f2b6SCedric Chaumont } 1064b0104773SPascal Brand } 1065b0104773SPascal Brand 1066b0104773SPascal Brand /* 1067b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1068b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1069b0104773SPascal Brand * can't restore sync with this API. 1070b0104773SPascal Brand */ 1071642a1607SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1072b0104773SPascal Brand if (*destLen < req_dlen) { 1073b0104773SPascal Brand *destLen = req_dlen; 1074dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1075dea1f2b6SCedric Chaumont goto out; 1076b0104773SPascal Brand } 1077b0104773SPascal Brand 1078b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1079642a1607SCedric Chaumont res = tee_buffer_update(operation, utee_cipher_update, srcData, srcLen, 1080642a1607SCedric Chaumont dst, &tmp_dlen); 1081dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS) 1082dea1f2b6SCedric Chaumont goto out; 1083dea1f2b6SCedric Chaumont 1084b0104773SPascal Brand dst += tmp_dlen; 1085b0104773SPascal Brand acc_dlen += tmp_dlen; 1086b0104773SPascal Brand 1087b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1088642a1607SCedric Chaumont res = utee_cipher_final(operation->state, operation->buffer, 1089642a1607SCedric Chaumont operation->buffer_offs, dst, &tmp_dlen); 1090b0104773SPascal Brand if (res != TEE_SUCCESS) 1091dea1f2b6SCedric Chaumont goto out; 1092dea1f2b6SCedric Chaumont 1093b0104773SPascal Brand acc_dlen += tmp_dlen; 1094b0104773SPascal Brand *destLen = acc_dlen; 1095dea1f2b6SCedric Chaumont 1096642a1607SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1097642a1607SCedric Chaumont 1098642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1099642a1607SCedric Chaumont 1100dea1f2b6SCedric Chaumont out: 1101dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 1102dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1103dea1f2b6SCedric Chaumont TEE_Panic(0); 1104dea1f2b6SCedric Chaumont 1105dea1f2b6SCedric Chaumont return res; 1106b0104773SPascal Brand } 1107b0104773SPascal Brand 1108b0104773SPascal Brand /* Cryptographic Operations API - MAC Functions */ 1109b0104773SPascal Brand 111028e0efc6SCedric Chaumont void TEE_MACInit(TEE_OperationHandle operation, void *IV, uint32_t IVLen) 1111b0104773SPascal Brand { 1112b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 1113b0104773SPascal Brand TEE_Panic(0); 1114642a1607SCedric Chaumont 1115b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_MAC) 1116b0104773SPascal Brand TEE_Panic(0); 1117642a1607SCedric Chaumont 1118642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 1119642a1607SCedric Chaumont !(operation->key1)) 1120642a1607SCedric Chaumont TEE_Panic(0); 1121642a1607SCedric Chaumont 1122642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 1123642a1607SCedric Chaumont TEE_ResetOperation(operation); 1124642a1607SCedric Chaumont 1125642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1126642a1607SCedric Chaumont 11276d15db08SJerome Forissier init_hash_operation(operation, IV, IVLen); 1128b0104773SPascal Brand } 1129b0104773SPascal Brand 113028e0efc6SCedric Chaumont void TEE_MACUpdate(TEE_OperationHandle operation, void *chunk, 113128e0efc6SCedric Chaumont uint32_t chunkSize) 1132b0104773SPascal Brand { 1133b0104773SPascal Brand TEE_Result res; 1134b0104773SPascal Brand 113528e0efc6SCedric Chaumont if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0)) 1136b0104773SPascal Brand TEE_Panic(0); 1137642a1607SCedric Chaumont 113828e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) 1139b0104773SPascal Brand TEE_Panic(0); 1140642a1607SCedric Chaumont 114128e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1142b0104773SPascal Brand TEE_Panic(0); 1143b0104773SPascal Brand 1144642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) 1145642a1607SCedric Chaumont TEE_Panic(0); 1146642a1607SCedric Chaumont 114728e0efc6SCedric Chaumont res = utee_hash_update(operation->state, chunk, chunkSize); 1148b0104773SPascal Brand if (res != TEE_SUCCESS) 1149b0104773SPascal Brand TEE_Panic(res); 1150b0104773SPascal Brand } 1151b0104773SPascal Brand 115228e0efc6SCedric Chaumont TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation, 115328e0efc6SCedric Chaumont void *message, uint32_t messageLen, 115479a3c601SCedric Chaumont void *mac, uint32_t *macLen) 1155b0104773SPascal Brand { 1156b0104773SPascal Brand TEE_Result res; 1157e86f1266SJens Wiklander uint64_t ml; 1158b0104773SPascal Brand 115928e0efc6SCedric Chaumont if (operation == TEE_HANDLE_NULL || 116028e0efc6SCedric Chaumont (message == NULL && messageLen != 0) || 116128e0efc6SCedric Chaumont mac == NULL || 116228e0efc6SCedric Chaumont macLen == NULL) { 116328e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 116428e0efc6SCedric Chaumont goto out; 116528e0efc6SCedric Chaumont } 1166b0104773SPascal Brand 116728e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 116828e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 116928e0efc6SCedric Chaumont goto out; 117028e0efc6SCedric Chaumont } 117128e0efc6SCedric Chaumont 117228e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 117328e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 117428e0efc6SCedric Chaumont goto out; 117528e0efc6SCedric Chaumont } 117628e0efc6SCedric Chaumont 1177642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1178642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1179642a1607SCedric Chaumont goto out; 1180642a1607SCedric Chaumont } 1181642a1607SCedric Chaumont 1182e86f1266SJens Wiklander ml = *macLen; 1183e86f1266SJens Wiklander res = utee_hash_final(operation->state, message, messageLen, mac, &ml); 1184e86f1266SJens Wiklander *macLen = ml; 118528e0efc6SCedric Chaumont if (res != TEE_SUCCESS) 118628e0efc6SCedric Chaumont goto out; 118728e0efc6SCedric Chaumont 118828e0efc6SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 118928e0efc6SCedric Chaumont 1190642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1191642a1607SCedric Chaumont 119228e0efc6SCedric Chaumont out: 119328e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 119428e0efc6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 119528e0efc6SCedric Chaumont TEE_Panic(res); 119628e0efc6SCedric Chaumont 1197b0104773SPascal Brand return res; 1198b0104773SPascal Brand } 1199b0104773SPascal Brand 1200b0104773SPascal Brand TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation, 120128e0efc6SCedric Chaumont void *message, uint32_t messageLen, 120228e0efc6SCedric Chaumont void *mac, uint32_t macLen) 1203b0104773SPascal Brand { 1204b0104773SPascal Brand TEE_Result res; 1205b0104773SPascal Brand uint8_t computed_mac[TEE_MAX_HASH_SIZE]; 12067f74c64aSPascal Brand uint32_t computed_mac_size = TEE_MAX_HASH_SIZE; 1207b0104773SPascal Brand 120828e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 120928e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 121028e0efc6SCedric Chaumont goto out; 121128e0efc6SCedric Chaumont } 121228e0efc6SCedric Chaumont 121328e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 121428e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 121528e0efc6SCedric Chaumont goto out; 121628e0efc6SCedric Chaumont } 121728e0efc6SCedric Chaumont 1218642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1219642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1220642a1607SCedric Chaumont goto out; 1221642a1607SCedric Chaumont } 1222642a1607SCedric Chaumont 1223b0104773SPascal Brand res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac, 1224b0104773SPascal Brand &computed_mac_size); 1225b0104773SPascal Brand if (res != TEE_SUCCESS) 122628e0efc6SCedric Chaumont goto out; 122728e0efc6SCedric Chaumont 122828e0efc6SCedric Chaumont if (computed_mac_size != macLen) { 122928e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 123028e0efc6SCedric Chaumont goto out; 123128e0efc6SCedric Chaumont } 123228e0efc6SCedric Chaumont 123328e0efc6SCedric Chaumont if (buf_compare_ct(mac, computed_mac, computed_mac_size) != 0) { 123428e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 123528e0efc6SCedric Chaumont goto out; 123628e0efc6SCedric Chaumont } 123728e0efc6SCedric Chaumont 1238642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1239642a1607SCedric Chaumont 124028e0efc6SCedric Chaumont out: 124128e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 124228e0efc6SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 124328e0efc6SCedric Chaumont TEE_Panic(res); 124428e0efc6SCedric Chaumont 1245b0104773SPascal Brand return res; 1246b0104773SPascal Brand } 1247b0104773SPascal Brand 1248b0104773SPascal Brand /* Cryptographic Operations API - Authenticated Encryption Functions */ 1249b0104773SPascal Brand 1250b5816c88SCedric Chaumont TEE_Result TEE_AEInit(TEE_OperationHandle operation, void *nonce, 125179a3c601SCedric Chaumont uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen, 1252b0104773SPascal Brand uint32_t payloadLen) 1253b0104773SPascal Brand { 1254b0104773SPascal Brand TEE_Result res; 1255b0104773SPascal Brand 1256b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || nonce == NULL) { 1257b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1258b5816c88SCedric Chaumont goto out; 1259b5816c88SCedric Chaumont } 1260b5816c88SCedric Chaumont 1261b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1262b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1263b5816c88SCedric Chaumont goto out; 1264b5816c88SCedric Chaumont } 1265b0104773SPascal Brand 1266642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 1267642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1268642a1607SCedric Chaumont goto out; 1269642a1607SCedric Chaumont } 1270642a1607SCedric Chaumont 1271b0104773SPascal Brand /* 1272b0104773SPascal Brand * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core 1273b0104773SPascal Brand * in the implementation. But AES-GCM spec doesn't specify the tag len 1274b0104773SPascal Brand * according to the same principle so we have to check here instead to 1275b0104773SPascal Brand * be GP compliant. 1276b0104773SPascal Brand */ 1277b5816c88SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_GCM) { 1278b0104773SPascal Brand /* 1279b0104773SPascal Brand * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96 1280b0104773SPascal Brand */ 1281b5816c88SCedric Chaumont if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) { 1282b5816c88SCedric Chaumont res = TEE_ERROR_NOT_SUPPORTED; 1283b5816c88SCedric Chaumont goto out; 1284b5816c88SCedric Chaumont } 1285b0104773SPascal Brand } 1286b0104773SPascal Brand 1287b5816c88SCedric Chaumont res = utee_authenc_init(operation->state, nonce, nonceLen, 1288b5816c88SCedric Chaumont tagLen / 8, AADLen, payloadLen); 1289b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1290b5816c88SCedric Chaumont goto out; 1291b5816c88SCedric Chaumont 1292b5816c88SCedric Chaumont operation->ae_tag_len = tagLen / 8; 1293b5816c88SCedric Chaumont operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 1294b5816c88SCedric Chaumont 1295b5816c88SCedric Chaumont out: 1296b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1297b5816c88SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 1298b0104773SPascal Brand TEE_Panic(res); 1299b5816c88SCedric Chaumont 1300b0104773SPascal Brand return res; 1301b0104773SPascal Brand } 1302b0104773SPascal Brand 1303b5816c88SCedric Chaumont void TEE_AEUpdateAAD(TEE_OperationHandle operation, void *AADdata, 130479a3c601SCedric Chaumont uint32_t AADdataLen) 1305b0104773SPascal Brand { 1306b0104773SPascal Brand TEE_Result res; 1307b0104773SPascal Brand 1308b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1309b5816c88SCedric Chaumont (AADdata == NULL && AADdataLen != 0)) 1310b0104773SPascal Brand TEE_Panic(0); 1311642a1607SCedric Chaumont 1312b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) 1313b0104773SPascal Brand TEE_Panic(0); 1314642a1607SCedric Chaumont 1315b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1316b0104773SPascal Brand TEE_Panic(0); 1317b0104773SPascal Brand 1318b5816c88SCedric Chaumont res = utee_authenc_update_aad(operation->state, AADdata, AADdataLen); 1319642a1607SCedric Chaumont 1320642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1321642a1607SCedric Chaumont 1322b0104773SPascal Brand if (res != TEE_SUCCESS) 1323b0104773SPascal Brand TEE_Panic(res); 1324b0104773SPascal Brand } 1325b0104773SPascal Brand 1326b5816c88SCedric Chaumont TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, void *srcData, 132779a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 1328b0104773SPascal Brand { 1329b5816c88SCedric Chaumont TEE_Result res; 1330b0104773SPascal Brand size_t req_dlen; 1331e86f1266SJens Wiklander uint64_t dl; 1332b0104773SPascal Brand 1333b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1334b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1335b5816c88SCedric Chaumont destLen == NULL || 1336b5816c88SCedric Chaumont (destData == NULL && *destLen != 0)) { 1337b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1338b5816c88SCedric Chaumont goto out; 1339b5816c88SCedric Chaumont } 1340b5816c88SCedric Chaumont 1341b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1342b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1343b5816c88SCedric Chaumont goto out; 1344b5816c88SCedric Chaumont } 1345b5816c88SCedric Chaumont 1346b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1347b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1348b5816c88SCedric Chaumont goto out; 1349b5816c88SCedric Chaumont } 1350b0104773SPascal Brand 1351b0104773SPascal Brand /* 1352b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1353b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1354b0104773SPascal Brand * can't restore sync with this API. 1355b0104773SPascal Brand */ 1356b5816c88SCedric Chaumont req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen, 1357b5816c88SCedric Chaumont operation->block_size); 1358b0104773SPascal Brand if (*destLen < req_dlen) { 1359b0104773SPascal Brand *destLen = req_dlen; 1360b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1361b5816c88SCedric Chaumont goto out; 1362b0104773SPascal Brand } 1363b0104773SPascal Brand 1364e86f1266SJens Wiklander dl = *destLen; 1365b5816c88SCedric Chaumont res = tee_buffer_update(operation, utee_authenc_update_payload, srcData, 1366e86f1266SJens Wiklander srcLen, destData, &dl); 1367e86f1266SJens Wiklander *destLen = dl; 1368b0104773SPascal Brand 1369642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1370642a1607SCedric Chaumont 1371b5816c88SCedric Chaumont out: 1372b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1373b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1374b5816c88SCedric Chaumont TEE_Panic(res); 1375b5816c88SCedric Chaumont 1376b5816c88SCedric Chaumont return res; 1377b0104773SPascal Brand } 1378b0104773SPascal Brand 1379b5816c88SCedric Chaumont TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation, 1380b5816c88SCedric Chaumont void *srcData, uint32_t srcLen, 138179a3c601SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 138279a3c601SCedric Chaumont uint32_t *tagLen) 1383b0104773SPascal Brand { 1384b0104773SPascal Brand TEE_Result res; 1385b0104773SPascal Brand uint8_t *dst = destData; 1386b0104773SPascal Brand size_t acc_dlen = 0; 1387e86f1266SJens Wiklander uint64_t tmp_dlen; 1388b0104773SPascal Brand size_t req_dlen; 1389e86f1266SJens Wiklander uint64_t tl; 1390b0104773SPascal Brand 1391b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1392b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1393b5816c88SCedric Chaumont destLen == NULL || 1394b5816c88SCedric Chaumont (destData == NULL && *destLen != 0) || 1395b5816c88SCedric Chaumont tag == NULL || tagLen == NULL) { 1396b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1397b5816c88SCedric Chaumont goto out; 1398b5816c88SCedric Chaumont } 1399b5816c88SCedric Chaumont 1400b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1401b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1402b5816c88SCedric Chaumont goto out; 1403b5816c88SCedric Chaumont } 1404b5816c88SCedric Chaumont 1405b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1406b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1407b5816c88SCedric Chaumont goto out; 1408b5816c88SCedric Chaumont } 1409b0104773SPascal Brand 1410b0104773SPascal Brand /* 1411b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1412b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1413b0104773SPascal Brand * can't restore sync with this API. 1414b0104773SPascal Brand */ 1415b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1416b0104773SPascal Brand if (*destLen < req_dlen) { 1417b0104773SPascal Brand *destLen = req_dlen; 1418b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1419b5816c88SCedric Chaumont goto out; 1420b0104773SPascal Brand } 1421b0104773SPascal Brand 1422b0104773SPascal Brand /* 1423b0104773SPascal Brand * Need to check this before update_payload since sync would be lost if 1424b0104773SPascal Brand * we return short buffer after that. 1425b0104773SPascal Brand */ 1426b5816c88SCedric Chaumont if (*tagLen < operation->ae_tag_len) { 1427b5816c88SCedric Chaumont *tagLen = operation->ae_tag_len; 1428b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1429b5816c88SCedric Chaumont goto out; 1430b0104773SPascal Brand } 1431b0104773SPascal Brand 1432b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1433b5816c88SCedric Chaumont res = tee_buffer_update(operation, utee_authenc_update_payload, srcData, 1434b5816c88SCedric Chaumont srcLen, dst, &tmp_dlen); 1435b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1436b5816c88SCedric Chaumont goto out; 1437b5816c88SCedric Chaumont 1438b0104773SPascal Brand dst += tmp_dlen; 1439b0104773SPascal Brand acc_dlen += tmp_dlen; 1440b0104773SPascal Brand 1441b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1442e86f1266SJens Wiklander tl = *tagLen; 1443b5816c88SCedric Chaumont res = utee_authenc_enc_final(operation->state, operation->buffer, 1444b5816c88SCedric Chaumont operation->buffer_offs, dst, &tmp_dlen, 1445e86f1266SJens Wiklander tag, &tl); 1446e86f1266SJens Wiklander *tagLen = tl; 1447b0104773SPascal Brand if (res != TEE_SUCCESS) 1448b5816c88SCedric Chaumont goto out; 1449b0104773SPascal Brand 1450b5816c88SCedric Chaumont acc_dlen += tmp_dlen; 1451b0104773SPascal Brand *destLen = acc_dlen; 1452642a1607SCedric Chaumont 1453b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1454b5816c88SCedric Chaumont 1455642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1456642a1607SCedric Chaumont 1457b5816c88SCedric Chaumont out: 1458b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1459b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1460b5816c88SCedric Chaumont TEE_Panic(res); 1461b0104773SPascal Brand 1462b0104773SPascal Brand return res; 1463b0104773SPascal Brand } 1464b0104773SPascal Brand 1465b5816c88SCedric Chaumont TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation, 1466b5816c88SCedric Chaumont void *srcData, uint32_t srcLen, 1467b5816c88SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 146879a3c601SCedric Chaumont uint32_t tagLen) 1469b0104773SPascal Brand { 1470b0104773SPascal Brand TEE_Result res; 1471b0104773SPascal Brand uint8_t *dst = destData; 1472b0104773SPascal Brand size_t acc_dlen = 0; 1473e86f1266SJens Wiklander uint64_t tmp_dlen; 1474b0104773SPascal Brand size_t req_dlen; 1475b0104773SPascal Brand 1476b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1477b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1478b5816c88SCedric Chaumont destLen == NULL || 1479b5816c88SCedric Chaumont (destData == NULL && *destLen != 0) || 1480b5816c88SCedric Chaumont (tag == NULL && tagLen != 0)) { 1481b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1482b5816c88SCedric Chaumont goto out; 1483b5816c88SCedric Chaumont } 1484b5816c88SCedric Chaumont 1485b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1486b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1487b5816c88SCedric Chaumont goto out; 1488b5816c88SCedric Chaumont } 1489b5816c88SCedric Chaumont 1490b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1491b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1492b5816c88SCedric Chaumont goto out; 1493b5816c88SCedric Chaumont } 1494b0104773SPascal Brand 1495b0104773SPascal Brand /* 1496b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1497b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1498b0104773SPascal Brand * can't restore sync with this API. 1499b0104773SPascal Brand */ 1500b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1501b0104773SPascal Brand if (*destLen < req_dlen) { 1502b0104773SPascal Brand *destLen = req_dlen; 1503b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1504b5816c88SCedric Chaumont goto out; 1505b0104773SPascal Brand } 1506b0104773SPascal Brand 1507b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1508b5816c88SCedric Chaumont res = tee_buffer_update(operation, utee_authenc_update_payload, srcData, 1509b5816c88SCedric Chaumont srcLen, dst, &tmp_dlen); 1510b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1511b5816c88SCedric Chaumont goto out; 1512b5816c88SCedric Chaumont 1513b0104773SPascal Brand dst += tmp_dlen; 1514b0104773SPascal Brand acc_dlen += tmp_dlen; 1515b0104773SPascal Brand 1516b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1517b5816c88SCedric Chaumont res = utee_authenc_dec_final(operation->state, operation->buffer, 1518b5816c88SCedric Chaumont operation->buffer_offs, dst, &tmp_dlen, 1519b5816c88SCedric Chaumont tag, tagLen); 1520b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1521b5816c88SCedric Chaumont goto out; 1522b5816c88SCedric Chaumont 1523b0104773SPascal Brand /* Supplied tagLen should match what we initiated with */ 1524b5816c88SCedric Chaumont if (tagLen != operation->ae_tag_len) 1525b0104773SPascal Brand res = TEE_ERROR_MAC_INVALID; 1526b0104773SPascal Brand 1527b0104773SPascal Brand acc_dlen += tmp_dlen; 1528b0104773SPascal Brand *destLen = acc_dlen; 1529642a1607SCedric Chaumont 1530b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1531b5816c88SCedric Chaumont 1532642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1533642a1607SCedric Chaumont 1534b5816c88SCedric Chaumont out: 1535b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1536b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER && 1537b5816c88SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 1538b5816c88SCedric Chaumont TEE_Panic(res); 1539b0104773SPascal Brand 1540b0104773SPascal Brand return res; 1541b0104773SPascal Brand } 1542b0104773SPascal Brand 1543b0104773SPascal Brand /* Cryptographic Operations API - Asymmetric Functions */ 1544b0104773SPascal Brand 154512e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation, 154612e66b6fSCedric Chaumont TEE_Attribute *params, 154712e66b6fSCedric Chaumont uint32_t paramCount, void *srcData, 154879a3c601SCedric Chaumont uint32_t srcLen, void *destData, 154979a3c601SCedric Chaumont uint32_t *destLen) 1550b0104773SPascal Brand { 1551b0104773SPascal Brand TEE_Result res; 1552e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1553e86f1266SJens Wiklander uint64_t dl; 1554b0104773SPascal Brand 155512e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1556b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 1557b0104773SPascal Brand TEE_Panic(0); 155812e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1559b0104773SPascal Brand TEE_Panic(0); 156012e66b6fSCedric Chaumont if (!operation->key1) 1561b0104773SPascal Brand TEE_Panic(0); 156212e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 156312e66b6fSCedric Chaumont TEE_Panic(0); 156412e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_ENCRYPT) 1565b0104773SPascal Brand TEE_Panic(0); 1566b0104773SPascal Brand 1567e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1568e86f1266SJens Wiklander dl = *destLen; 1569e86f1266SJens Wiklander res = utee_asymm_operate(operation->state, ua, paramCount, srcData, 1570e86f1266SJens Wiklander srcLen, destData, &dl); 1571e86f1266SJens Wiklander *destLen = dl; 157212e66b6fSCedric Chaumont 15738844ebfcSPascal Brand if (res != TEE_SUCCESS && 15748844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 15758844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1576b0104773SPascal Brand TEE_Panic(res); 157712e66b6fSCedric Chaumont 1578b0104773SPascal Brand return res; 1579b0104773SPascal Brand } 1580b0104773SPascal Brand 158112e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation, 158212e66b6fSCedric Chaumont TEE_Attribute *params, 158312e66b6fSCedric Chaumont uint32_t paramCount, void *srcData, 158479a3c601SCedric Chaumont uint32_t srcLen, void *destData, 158579a3c601SCedric Chaumont uint32_t *destLen) 1586b0104773SPascal Brand { 1587b0104773SPascal Brand TEE_Result res; 1588e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1589e86f1266SJens Wiklander uint64_t dl; 1590b0104773SPascal Brand 159112e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1592b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 1593b0104773SPascal Brand TEE_Panic(0); 159412e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1595b0104773SPascal Brand TEE_Panic(0); 159612e66b6fSCedric Chaumont if (!operation->key1) 1597b0104773SPascal Brand TEE_Panic(0); 159812e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 159912e66b6fSCedric Chaumont TEE_Panic(0); 160012e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_DECRYPT) 1601b0104773SPascal Brand TEE_Panic(0); 1602b0104773SPascal Brand 1603e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1604e86f1266SJens Wiklander dl = *destLen; 1605e86f1266SJens Wiklander res = utee_asymm_operate(operation->state, ua, paramCount, srcData, 1606e86f1266SJens Wiklander srcLen, destData, &dl); 1607e86f1266SJens Wiklander *destLen = dl; 160812e66b6fSCedric Chaumont 16098844ebfcSPascal Brand if (res != TEE_SUCCESS && 16108844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 16118844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1612b0104773SPascal Brand TEE_Panic(res); 161312e66b6fSCedric Chaumont 1614b0104773SPascal Brand return res; 1615b0104773SPascal Brand } 1616b0104773SPascal Brand 161712e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation, 161812e66b6fSCedric Chaumont TEE_Attribute *params, 161912e66b6fSCedric Chaumont uint32_t paramCount, void *digest, 162079a3c601SCedric Chaumont uint32_t digestLen, void *signature, 162179a3c601SCedric Chaumont uint32_t *signatureLen) 1622b0104773SPascal Brand { 1623b0104773SPascal Brand TEE_Result res; 1624e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1625e86f1266SJens Wiklander uint64_t sl; 1626b0104773SPascal Brand 162712e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || 162812e66b6fSCedric Chaumont (digest == NULL && digestLen != 0) || 1629b0104773SPascal Brand signature == NULL || signatureLen == NULL) 1630b0104773SPascal Brand TEE_Panic(0); 163112e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1632b0104773SPascal Brand TEE_Panic(0); 163312e66b6fSCedric Chaumont if (!operation->key1) 1634b0104773SPascal Brand TEE_Panic(0); 163512e66b6fSCedric Chaumont if (operation->info.operationClass != 163612e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 163712e66b6fSCedric Chaumont TEE_Panic(0); 163812e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_SIGN) 1639b0104773SPascal Brand TEE_Panic(0); 1640b0104773SPascal Brand 1641e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1642e86f1266SJens Wiklander sl = *signatureLen; 1643e86f1266SJens Wiklander res = utee_asymm_operate(operation->state, ua, paramCount, digest, 1644e86f1266SJens Wiklander digestLen, signature, &sl); 1645e86f1266SJens Wiklander *signatureLen = sl; 164612e66b6fSCedric Chaumont 1647b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER) 1648b0104773SPascal Brand TEE_Panic(res); 164912e66b6fSCedric Chaumont 1650b0104773SPascal Brand return res; 1651b0104773SPascal Brand } 1652b0104773SPascal Brand 165312e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation, 165412e66b6fSCedric Chaumont TEE_Attribute *params, 165512e66b6fSCedric Chaumont uint32_t paramCount, void *digest, 165612e66b6fSCedric Chaumont uint32_t digestLen, void *signature, 165779a3c601SCedric Chaumont uint32_t signatureLen) 1658b0104773SPascal Brand { 1659b0104773SPascal Brand TEE_Result res; 1660e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1661b0104773SPascal Brand 166212e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || 166312e66b6fSCedric Chaumont (digest == NULL && digestLen != 0) || 1664b0104773SPascal Brand (signature == NULL && signatureLen != 0)) 1665b0104773SPascal Brand TEE_Panic(0); 166612e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1667b0104773SPascal Brand TEE_Panic(0); 166812e66b6fSCedric Chaumont if (!operation->key1) 1669b0104773SPascal Brand TEE_Panic(0); 167012e66b6fSCedric Chaumont if (operation->info.operationClass != 167112e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 167212e66b6fSCedric Chaumont TEE_Panic(0); 167312e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_VERIFY) 1674b0104773SPascal Brand TEE_Panic(0); 1675b0104773SPascal Brand 1676e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1677e86f1266SJens Wiklander res = utee_asymm_verify(operation->state, ua, paramCount, digest, 167812e66b6fSCedric Chaumont digestLen, signature, signatureLen); 167912e66b6fSCedric Chaumont 1680b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID) 1681b0104773SPascal Brand TEE_Panic(res); 168212e66b6fSCedric Chaumont 1683b0104773SPascal Brand return res; 1684b0104773SPascal Brand } 1685b0104773SPascal Brand 1686b0104773SPascal Brand /* Cryptographic Operations API - Key Derivation Functions */ 1687b0104773SPascal Brand 1688b0104773SPascal Brand void TEE_DeriveKey(TEE_OperationHandle operation, 1689b0104773SPascal Brand const TEE_Attribute *params, uint32_t paramCount, 1690b0104773SPascal Brand TEE_ObjectHandle derivedKey) 1691b0104773SPascal Brand { 1692b0104773SPascal Brand TEE_Result res; 1693b0104773SPascal Brand TEE_ObjectInfo key_info; 1694e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1695b0104773SPascal Brand 1696b0104773SPascal Brand if (operation == TEE_HANDLE_NULL || derivedKey == 0) 1697b0104773SPascal Brand TEE_Panic(0); 169884fa9467SCedric Chaumont if (params == NULL && paramCount != 0) 1699b0104773SPascal Brand TEE_Panic(0); 17008854d3c6SJerome Forissier if (TEE_ALG_GET_CLASS(operation->info.algorithm) != 17018854d3c6SJerome Forissier TEE_OPERATION_KEY_DERIVATION) 1702b0104773SPascal Brand TEE_Panic(0); 1703b0104773SPascal Brand 1704b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION) 1705b0104773SPascal Brand TEE_Panic(0); 170684fa9467SCedric Chaumont if (!operation->key1) 170784fa9467SCedric Chaumont TEE_Panic(0); 1708b0104773SPascal Brand if (operation->info.mode != TEE_MODE_DERIVE) 1709b0104773SPascal Brand TEE_Panic(0); 1710b0104773SPascal Brand if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0) 1711b0104773SPascal Brand TEE_Panic(0); 1712b0104773SPascal Brand 1713e86f1266SJens Wiklander res = utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info); 1714b0104773SPascal Brand if (res != TEE_SUCCESS) 1715b0104773SPascal Brand TEE_Panic(0); 1716b0104773SPascal Brand 1717b0104773SPascal Brand if (key_info.objectType != TEE_TYPE_GENERIC_SECRET) 1718b0104773SPascal Brand TEE_Panic(0); 1719b0104773SPascal Brand if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1720b0104773SPascal Brand TEE_Panic(0); 1721b0104773SPascal Brand 1722e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1723e86f1266SJens Wiklander res = utee_cryp_derive_key(operation->state, ua, paramCount, 1724e86f1266SJens Wiklander (unsigned long)derivedKey); 1725b0104773SPascal Brand if (res != TEE_SUCCESS) 1726b0104773SPascal Brand TEE_Panic(res); 1727b0104773SPascal Brand } 1728b0104773SPascal Brand 1729b0104773SPascal Brand /* Cryptographic Operations API - Random Number Generation Functions */ 1730b0104773SPascal Brand 173179a3c601SCedric Chaumont void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen) 1732b0104773SPascal Brand { 1733b0104773SPascal Brand TEE_Result res; 1734b0104773SPascal Brand 1735b0104773SPascal Brand res = utee_cryp_random_number_generate(randomBuffer, randomBufferLen); 1736b0104773SPascal Brand if (res != TEE_SUCCESS) 1737b0104773SPascal Brand TEE_Panic(res); 1738b0104773SPascal Brand } 1739