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> 37*e86f1266SJens 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 322*e86f1266SJens Wiklander res = utee_cryp_state_alloc(algorithm, mode, (unsigned long)op->key1, 323*e86f1266SJens 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; 800*e86f1266SJens 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 811*e86f1266SJens Wiklander hl = *hashLen; 812*e86f1266SJens Wiklander res = utee_hash_final(operation->state, chunk, chunkLen, hash, &hl); 813*e86f1266SJens 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, 861*e86f1266SJens Wiklander TEE_Result(*update_func)(unsigned long state, const void *src, 862*e86f1266SJens Wiklander size_t slen, void *dst, uint64_t *dlen), 863b0104773SPascal Brand const void *src_data, size_t src_len, 864*e86f1266SJens 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; 872*e86f1266SJens 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; 953*e86f1266SJens 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 978b0104773SPascal Brand /* Calculate required dlen */ 979642a1607SCedric Chaumont req_dlen = ((operation->buffer_offs + srcLen) / operation->block_size) * 980642a1607SCedric Chaumont operation->block_size; 981642a1607SCedric Chaumont if (operation->buffer_two_blocks) { 982642a1607SCedric Chaumont if (req_dlen > operation->block_size * 2) 983642a1607SCedric Chaumont req_dlen -= operation->block_size * 2; 984b0104773SPascal Brand else 985b0104773SPascal Brand req_dlen = 0; 986b0104773SPascal Brand } 987b0104773SPascal Brand /* 988b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 989b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 990b0104773SPascal Brand * can't restore sync with this API. 991b0104773SPascal Brand */ 992b0104773SPascal Brand if (*destLen < req_dlen) { 993b0104773SPascal Brand *destLen = req_dlen; 994dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 995dea1f2b6SCedric Chaumont goto out; 996b0104773SPascal Brand } 997b0104773SPascal Brand 998*e86f1266SJens Wiklander dl = *destLen; 999642a1607SCedric Chaumont res = tee_buffer_update(operation, utee_cipher_update, srcData, srcLen, 1000*e86f1266SJens Wiklander destData, &dl); 1001*e86f1266SJens Wiklander *destLen = dl; 1002b0104773SPascal Brand 1003dea1f2b6SCedric Chaumont out: 1004dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 1005dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1006dea1f2b6SCedric Chaumont TEE_Panic(0); 1007dea1f2b6SCedric Chaumont 1008dea1f2b6SCedric Chaumont return res; 1009b0104773SPascal Brand } 1010b0104773SPascal Brand 1011642a1607SCedric Chaumont TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation, 1012642a1607SCedric Chaumont void *srcData, uint32_t srcLen, void *destData, 101379a3c601SCedric Chaumont uint32_t *destLen) 1014b0104773SPascal Brand { 1015b0104773SPascal Brand TEE_Result res; 1016b0104773SPascal Brand uint8_t *dst = destData; 1017b0104773SPascal Brand size_t acc_dlen = 0; 1018*e86f1266SJens Wiklander uint64_t tmp_dlen; 1019b0104773SPascal Brand size_t req_dlen; 1020b0104773SPascal Brand 1021642a1607SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1022dea1f2b6SCedric Chaumont (srcData == NULL && srcLen != 0) || 1023dea1f2b6SCedric Chaumont destLen == NULL || 1024dea1f2b6SCedric Chaumont (destData == NULL && *destLen != 0)) { 1025dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1026dea1f2b6SCedric Chaumont goto out; 1027dea1f2b6SCedric Chaumont } 1028dea1f2b6SCedric Chaumont 1029642a1607SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_CIPHER) { 1030dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1031dea1f2b6SCedric Chaumont goto out; 1032dea1f2b6SCedric Chaumont } 1033dea1f2b6SCedric Chaumont 1034642a1607SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1035642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1036642a1607SCedric Chaumont goto out; 1037642a1607SCedric Chaumont } 1038642a1607SCedric Chaumont 1039642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1040dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1041dea1f2b6SCedric Chaumont goto out; 1042dea1f2b6SCedric Chaumont } 1043b0104773SPascal Brand 1044b0104773SPascal Brand /* 1045b0104773SPascal Brand * Check that the final block doesn't require padding for those 1046b0104773SPascal Brand * algorithms that requires client to supply padding. 1047b0104773SPascal Brand */ 1048642a1607SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD || 1049642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD || 1050642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD || 1051642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD || 1052642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || 1053642a1607SCedric Chaumont operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD) { 1054642a1607SCedric Chaumont if (((operation->buffer_offs + srcLen) % operation->block_size) 1055642a1607SCedric Chaumont != 0) { 1056dea1f2b6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1057dea1f2b6SCedric Chaumont goto out; 1058dea1f2b6SCedric Chaumont } 1059b0104773SPascal Brand } 1060b0104773SPascal Brand 1061b0104773SPascal Brand /* 1062b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1063b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1064b0104773SPascal Brand * can't restore sync with this API. 1065b0104773SPascal Brand */ 1066642a1607SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1067b0104773SPascal Brand if (*destLen < req_dlen) { 1068b0104773SPascal Brand *destLen = req_dlen; 1069dea1f2b6SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1070dea1f2b6SCedric Chaumont goto out; 1071b0104773SPascal Brand } 1072b0104773SPascal Brand 1073b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1074642a1607SCedric Chaumont res = tee_buffer_update(operation, utee_cipher_update, srcData, srcLen, 1075642a1607SCedric Chaumont dst, &tmp_dlen); 1076dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS) 1077dea1f2b6SCedric Chaumont goto out; 1078dea1f2b6SCedric Chaumont 1079b0104773SPascal Brand dst += tmp_dlen; 1080b0104773SPascal Brand acc_dlen += tmp_dlen; 1081b0104773SPascal Brand 1082b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1083642a1607SCedric Chaumont res = utee_cipher_final(operation->state, operation->buffer, 1084642a1607SCedric Chaumont operation->buffer_offs, dst, &tmp_dlen); 1085b0104773SPascal Brand if (res != TEE_SUCCESS) 1086dea1f2b6SCedric Chaumont goto out; 1087dea1f2b6SCedric Chaumont 1088b0104773SPascal Brand acc_dlen += tmp_dlen; 1089b0104773SPascal Brand *destLen = acc_dlen; 1090dea1f2b6SCedric Chaumont 1091642a1607SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1092642a1607SCedric Chaumont 1093642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1094642a1607SCedric Chaumont 1095dea1f2b6SCedric Chaumont out: 1096dea1f2b6SCedric Chaumont if (res != TEE_SUCCESS && 1097dea1f2b6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1098dea1f2b6SCedric Chaumont TEE_Panic(0); 1099dea1f2b6SCedric Chaumont 1100dea1f2b6SCedric Chaumont return res; 1101b0104773SPascal Brand } 1102b0104773SPascal Brand 1103b0104773SPascal Brand /* Cryptographic Operations API - MAC Functions */ 1104b0104773SPascal Brand 110528e0efc6SCedric Chaumont void TEE_MACInit(TEE_OperationHandle operation, void *IV, uint32_t IVLen) 1106b0104773SPascal Brand { 1107b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 1108b0104773SPascal Brand TEE_Panic(0); 1109642a1607SCedric Chaumont 1110b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_MAC) 1111b0104773SPascal Brand TEE_Panic(0); 1112642a1607SCedric Chaumont 1113642a1607SCedric Chaumont if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) || 1114642a1607SCedric Chaumont !(operation->key1)) 1115642a1607SCedric Chaumont TEE_Panic(0); 1116642a1607SCedric Chaumont 1117642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) 1118642a1607SCedric Chaumont TEE_ResetOperation(operation); 1119642a1607SCedric Chaumont 1120642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1121642a1607SCedric Chaumont 11226d15db08SJerome Forissier init_hash_operation(operation, IV, IVLen); 1123b0104773SPascal Brand } 1124b0104773SPascal Brand 112528e0efc6SCedric Chaumont void TEE_MACUpdate(TEE_OperationHandle operation, void *chunk, 112628e0efc6SCedric Chaumont uint32_t chunkSize) 1127b0104773SPascal Brand { 1128b0104773SPascal Brand TEE_Result res; 1129b0104773SPascal Brand 113028e0efc6SCedric Chaumont if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0)) 1131b0104773SPascal Brand TEE_Panic(0); 1132642a1607SCedric Chaumont 113328e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) 1134b0104773SPascal Brand TEE_Panic(0); 1135642a1607SCedric Chaumont 113628e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1137b0104773SPascal Brand TEE_Panic(0); 1138b0104773SPascal Brand 1139642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) 1140642a1607SCedric Chaumont TEE_Panic(0); 1141642a1607SCedric Chaumont 114228e0efc6SCedric Chaumont res = utee_hash_update(operation->state, chunk, chunkSize); 1143b0104773SPascal Brand if (res != TEE_SUCCESS) 1144b0104773SPascal Brand TEE_Panic(res); 1145b0104773SPascal Brand } 1146b0104773SPascal Brand 114728e0efc6SCedric Chaumont TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation, 114828e0efc6SCedric Chaumont void *message, uint32_t messageLen, 114979a3c601SCedric Chaumont void *mac, uint32_t *macLen) 1150b0104773SPascal Brand { 1151b0104773SPascal Brand TEE_Result res; 1152*e86f1266SJens Wiklander uint64_t ml; 1153b0104773SPascal Brand 115428e0efc6SCedric Chaumont if (operation == TEE_HANDLE_NULL || 115528e0efc6SCedric Chaumont (message == NULL && messageLen != 0) || 115628e0efc6SCedric Chaumont mac == NULL || 115728e0efc6SCedric Chaumont macLen == NULL) { 115828e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 115928e0efc6SCedric Chaumont goto out; 116028e0efc6SCedric Chaumont } 1161b0104773SPascal Brand 116228e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 116328e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 116428e0efc6SCedric Chaumont goto out; 116528e0efc6SCedric Chaumont } 116628e0efc6SCedric Chaumont 116728e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 116828e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 116928e0efc6SCedric Chaumont goto out; 117028e0efc6SCedric Chaumont } 117128e0efc6SCedric Chaumont 1172642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1173642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1174642a1607SCedric Chaumont goto out; 1175642a1607SCedric Chaumont } 1176642a1607SCedric Chaumont 1177*e86f1266SJens Wiklander ml = *macLen; 1178*e86f1266SJens Wiklander res = utee_hash_final(operation->state, message, messageLen, mac, &ml); 1179*e86f1266SJens Wiklander *macLen = ml; 118028e0efc6SCedric Chaumont if (res != TEE_SUCCESS) 118128e0efc6SCedric Chaumont goto out; 118228e0efc6SCedric Chaumont 118328e0efc6SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 118428e0efc6SCedric Chaumont 1185642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1186642a1607SCedric Chaumont 118728e0efc6SCedric Chaumont out: 118828e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 118928e0efc6SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 119028e0efc6SCedric Chaumont TEE_Panic(res); 119128e0efc6SCedric Chaumont 1192b0104773SPascal Brand return res; 1193b0104773SPascal Brand } 1194b0104773SPascal Brand 1195b0104773SPascal Brand TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation, 119628e0efc6SCedric Chaumont void *message, uint32_t messageLen, 119728e0efc6SCedric Chaumont void *mac, uint32_t macLen) 1198b0104773SPascal Brand { 1199b0104773SPascal Brand TEE_Result res; 1200b0104773SPascal Brand uint8_t computed_mac[TEE_MAX_HASH_SIZE]; 12017f74c64aSPascal Brand uint32_t computed_mac_size = TEE_MAX_HASH_SIZE; 1202b0104773SPascal Brand 120328e0efc6SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_MAC) { 120428e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 120528e0efc6SCedric Chaumont goto out; 120628e0efc6SCedric Chaumont } 120728e0efc6SCedric Chaumont 120828e0efc6SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 120928e0efc6SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 121028e0efc6SCedric Chaumont goto out; 121128e0efc6SCedric Chaumont } 121228e0efc6SCedric Chaumont 1213642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) { 1214642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1215642a1607SCedric Chaumont goto out; 1216642a1607SCedric Chaumont } 1217642a1607SCedric Chaumont 1218b0104773SPascal Brand res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac, 1219b0104773SPascal Brand &computed_mac_size); 1220b0104773SPascal Brand if (res != TEE_SUCCESS) 122128e0efc6SCedric Chaumont goto out; 122228e0efc6SCedric Chaumont 122328e0efc6SCedric Chaumont if (computed_mac_size != macLen) { 122428e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 122528e0efc6SCedric Chaumont goto out; 122628e0efc6SCedric Chaumont } 122728e0efc6SCedric Chaumont 122828e0efc6SCedric Chaumont if (buf_compare_ct(mac, computed_mac, computed_mac_size) != 0) { 122928e0efc6SCedric Chaumont res = TEE_ERROR_MAC_INVALID; 123028e0efc6SCedric Chaumont goto out; 123128e0efc6SCedric Chaumont } 123228e0efc6SCedric Chaumont 1233642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1234642a1607SCedric Chaumont 123528e0efc6SCedric Chaumont out: 123628e0efc6SCedric Chaumont if (res != TEE_SUCCESS && 123728e0efc6SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 123828e0efc6SCedric Chaumont TEE_Panic(res); 123928e0efc6SCedric Chaumont 1240b0104773SPascal Brand return res; 1241b0104773SPascal Brand } 1242b0104773SPascal Brand 1243b0104773SPascal Brand /* Cryptographic Operations API - Authenticated Encryption Functions */ 1244b0104773SPascal Brand 1245b5816c88SCedric Chaumont TEE_Result TEE_AEInit(TEE_OperationHandle operation, void *nonce, 124679a3c601SCedric Chaumont uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen, 1247b0104773SPascal Brand uint32_t payloadLen) 1248b0104773SPascal Brand { 1249b0104773SPascal Brand TEE_Result res; 1250b0104773SPascal Brand 1251b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || nonce == NULL) { 1252b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1253b5816c88SCedric Chaumont goto out; 1254b5816c88SCedric Chaumont } 1255b5816c88SCedric Chaumont 1256b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1257b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1258b5816c88SCedric Chaumont goto out; 1259b5816c88SCedric Chaumont } 1260b0104773SPascal Brand 1261642a1607SCedric Chaumont if (operation->operationState != TEE_OPERATION_STATE_INITIAL) { 1262642a1607SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1263642a1607SCedric Chaumont goto out; 1264642a1607SCedric Chaumont } 1265642a1607SCedric Chaumont 1266b0104773SPascal Brand /* 1267b0104773SPascal Brand * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core 1268b0104773SPascal Brand * in the implementation. But AES-GCM spec doesn't specify the tag len 1269b0104773SPascal Brand * according to the same principle so we have to check here instead to 1270b0104773SPascal Brand * be GP compliant. 1271b0104773SPascal Brand */ 1272b5816c88SCedric Chaumont if (operation->info.algorithm == TEE_ALG_AES_GCM) { 1273b0104773SPascal Brand /* 1274b0104773SPascal Brand * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96 1275b0104773SPascal Brand */ 1276b5816c88SCedric Chaumont if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) { 1277b5816c88SCedric Chaumont res = TEE_ERROR_NOT_SUPPORTED; 1278b5816c88SCedric Chaumont goto out; 1279b5816c88SCedric Chaumont } 1280b0104773SPascal Brand } 1281b0104773SPascal Brand 1282b5816c88SCedric Chaumont res = utee_authenc_init(operation->state, nonce, nonceLen, 1283b5816c88SCedric Chaumont tagLen / 8, AADLen, payloadLen); 1284b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1285b5816c88SCedric Chaumont goto out; 1286b5816c88SCedric Chaumont 1287b5816c88SCedric Chaumont operation->ae_tag_len = tagLen / 8; 1288b5816c88SCedric Chaumont operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 1289b5816c88SCedric Chaumont 1290b5816c88SCedric Chaumont out: 1291b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1292b5816c88SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 1293b0104773SPascal Brand TEE_Panic(res); 1294b5816c88SCedric Chaumont 1295b0104773SPascal Brand return res; 1296b0104773SPascal Brand } 1297b0104773SPascal Brand 1298b5816c88SCedric Chaumont void TEE_AEUpdateAAD(TEE_OperationHandle operation, void *AADdata, 129979a3c601SCedric Chaumont uint32_t AADdataLen) 1300b0104773SPascal Brand { 1301b0104773SPascal Brand TEE_Result res; 1302b0104773SPascal Brand 1303b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1304b5816c88SCedric Chaumont (AADdata == NULL && AADdataLen != 0)) 1305b0104773SPascal Brand TEE_Panic(0); 1306642a1607SCedric Chaumont 1307b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) 1308b0104773SPascal Brand TEE_Panic(0); 1309642a1607SCedric Chaumont 1310b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1311b0104773SPascal Brand TEE_Panic(0); 1312b0104773SPascal Brand 1313b5816c88SCedric Chaumont res = utee_authenc_update_aad(operation->state, AADdata, AADdataLen); 1314642a1607SCedric Chaumont 1315642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1316642a1607SCedric Chaumont 1317b0104773SPascal Brand if (res != TEE_SUCCESS) 1318b0104773SPascal Brand TEE_Panic(res); 1319b0104773SPascal Brand } 1320b0104773SPascal Brand 1321b5816c88SCedric Chaumont TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, void *srcData, 132279a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 1323b0104773SPascal Brand { 1324b5816c88SCedric Chaumont TEE_Result res; 1325b0104773SPascal Brand size_t req_dlen; 1326*e86f1266SJens Wiklander uint64_t dl; 1327b0104773SPascal Brand 1328b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1329b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1330b5816c88SCedric Chaumont destLen == NULL || 1331b5816c88SCedric Chaumont (destData == NULL && *destLen != 0)) { 1332b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1333b5816c88SCedric Chaumont goto out; 1334b5816c88SCedric Chaumont } 1335b5816c88SCedric Chaumont 1336b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1337b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1338b5816c88SCedric Chaumont goto out; 1339b5816c88SCedric Chaumont } 1340b5816c88SCedric Chaumont 1341b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1342b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1343b5816c88SCedric Chaumont goto out; 1344b5816c88SCedric Chaumont } 1345b0104773SPascal Brand 1346b0104773SPascal Brand /* 1347b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1348b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1349b0104773SPascal Brand * can't restore sync with this API. 1350b0104773SPascal Brand */ 1351b5816c88SCedric Chaumont req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen, 1352b5816c88SCedric Chaumont operation->block_size); 1353b0104773SPascal Brand if (*destLen < req_dlen) { 1354b0104773SPascal Brand *destLen = req_dlen; 1355b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1356b5816c88SCedric Chaumont goto out; 1357b0104773SPascal Brand } 1358b0104773SPascal Brand 1359*e86f1266SJens Wiklander dl = *destLen; 1360b5816c88SCedric Chaumont res = tee_buffer_update(operation, utee_authenc_update_payload, srcData, 1361*e86f1266SJens Wiklander srcLen, destData, &dl); 1362*e86f1266SJens Wiklander *destLen = dl; 1363b0104773SPascal Brand 1364642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_ACTIVE; 1365642a1607SCedric Chaumont 1366b5816c88SCedric Chaumont out: 1367b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1368b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1369b5816c88SCedric Chaumont TEE_Panic(res); 1370b5816c88SCedric Chaumont 1371b5816c88SCedric Chaumont return res; 1372b0104773SPascal Brand } 1373b0104773SPascal Brand 1374b5816c88SCedric Chaumont TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation, 1375b5816c88SCedric Chaumont void *srcData, uint32_t srcLen, 137679a3c601SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 137779a3c601SCedric Chaumont uint32_t *tagLen) 1378b0104773SPascal Brand { 1379b0104773SPascal Brand TEE_Result res; 1380b0104773SPascal Brand uint8_t *dst = destData; 1381b0104773SPascal Brand size_t acc_dlen = 0; 1382*e86f1266SJens Wiklander uint64_t tmp_dlen; 1383b0104773SPascal Brand size_t req_dlen; 1384*e86f1266SJens Wiklander uint64_t tl; 1385b0104773SPascal Brand 1386b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1387b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1388b5816c88SCedric Chaumont destLen == NULL || 1389b5816c88SCedric Chaumont (destData == NULL && *destLen != 0) || 1390b5816c88SCedric Chaumont tag == NULL || tagLen == NULL) { 1391b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1392b5816c88SCedric Chaumont goto out; 1393b5816c88SCedric Chaumont } 1394b5816c88SCedric Chaumont 1395b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1396b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1397b5816c88SCedric Chaumont goto out; 1398b5816c88SCedric Chaumont } 1399b5816c88SCedric Chaumont 1400b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1401b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1402b5816c88SCedric Chaumont goto out; 1403b5816c88SCedric Chaumont } 1404b0104773SPascal Brand 1405b0104773SPascal Brand /* 1406b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1407b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1408b0104773SPascal Brand * can't restore sync with this API. 1409b0104773SPascal Brand */ 1410b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1411b0104773SPascal Brand if (*destLen < req_dlen) { 1412b0104773SPascal Brand *destLen = req_dlen; 1413b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1414b5816c88SCedric Chaumont goto out; 1415b0104773SPascal Brand } 1416b0104773SPascal Brand 1417b0104773SPascal Brand /* 1418b0104773SPascal Brand * Need to check this before update_payload since sync would be lost if 1419b0104773SPascal Brand * we return short buffer after that. 1420b0104773SPascal Brand */ 1421b5816c88SCedric Chaumont if (*tagLen < operation->ae_tag_len) { 1422b5816c88SCedric Chaumont *tagLen = operation->ae_tag_len; 1423b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1424b5816c88SCedric Chaumont goto out; 1425b0104773SPascal Brand } 1426b0104773SPascal Brand 1427b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1428b5816c88SCedric Chaumont res = tee_buffer_update(operation, utee_authenc_update_payload, srcData, 1429b5816c88SCedric Chaumont srcLen, dst, &tmp_dlen); 1430b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1431b5816c88SCedric Chaumont goto out; 1432b5816c88SCedric Chaumont 1433b0104773SPascal Brand dst += tmp_dlen; 1434b0104773SPascal Brand acc_dlen += tmp_dlen; 1435b0104773SPascal Brand 1436b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1437*e86f1266SJens Wiklander tl = *tagLen; 1438b5816c88SCedric Chaumont res = utee_authenc_enc_final(operation->state, operation->buffer, 1439b5816c88SCedric Chaumont operation->buffer_offs, dst, &tmp_dlen, 1440*e86f1266SJens Wiklander tag, &tl); 1441*e86f1266SJens Wiklander *tagLen = tl; 1442b0104773SPascal Brand if (res != TEE_SUCCESS) 1443b5816c88SCedric Chaumont goto out; 1444b0104773SPascal Brand 1445b5816c88SCedric Chaumont acc_dlen += tmp_dlen; 1446b0104773SPascal Brand *destLen = acc_dlen; 1447642a1607SCedric Chaumont 1448b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1449b5816c88SCedric Chaumont 1450642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1451642a1607SCedric Chaumont 1452b5816c88SCedric Chaumont out: 1453b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1454b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER) 1455b5816c88SCedric Chaumont TEE_Panic(res); 1456b0104773SPascal Brand 1457b0104773SPascal Brand return res; 1458b0104773SPascal Brand } 1459b0104773SPascal Brand 1460b5816c88SCedric Chaumont TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation, 1461b5816c88SCedric Chaumont void *srcData, uint32_t srcLen, 1462b5816c88SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 146379a3c601SCedric Chaumont uint32_t tagLen) 1464b0104773SPascal Brand { 1465b0104773SPascal Brand TEE_Result res; 1466b0104773SPascal Brand uint8_t *dst = destData; 1467b0104773SPascal Brand size_t acc_dlen = 0; 1468*e86f1266SJens Wiklander uint64_t tmp_dlen; 1469b0104773SPascal Brand size_t req_dlen; 1470b0104773SPascal Brand 1471b5816c88SCedric Chaumont if (operation == TEE_HANDLE_NULL || 1472b5816c88SCedric Chaumont (srcData == NULL && srcLen != 0) || 1473b5816c88SCedric Chaumont destLen == NULL || 1474b5816c88SCedric Chaumont (destData == NULL && *destLen != 0) || 1475b5816c88SCedric Chaumont (tag == NULL && tagLen != 0)) { 1476b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1477b5816c88SCedric Chaumont goto out; 1478b5816c88SCedric Chaumont } 1479b5816c88SCedric Chaumont 1480b5816c88SCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_AE) { 1481b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1482b5816c88SCedric Chaumont goto out; 1483b5816c88SCedric Chaumont } 1484b5816c88SCedric Chaumont 1485b5816c88SCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) { 1486b5816c88SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 1487b5816c88SCedric Chaumont goto out; 1488b5816c88SCedric Chaumont } 1489b0104773SPascal Brand 1490b0104773SPascal Brand /* 1491b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1492b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1493b0104773SPascal Brand * can't restore sync with this API. 1494b0104773SPascal Brand */ 1495b5816c88SCedric Chaumont req_dlen = operation->buffer_offs + srcLen; 1496b0104773SPascal Brand if (*destLen < req_dlen) { 1497b0104773SPascal Brand *destLen = req_dlen; 1498b5816c88SCedric Chaumont res = TEE_ERROR_SHORT_BUFFER; 1499b5816c88SCedric Chaumont goto out; 1500b0104773SPascal Brand } 1501b0104773SPascal Brand 1502b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1503b5816c88SCedric Chaumont res = tee_buffer_update(operation, utee_authenc_update_payload, srcData, 1504b5816c88SCedric Chaumont srcLen, dst, &tmp_dlen); 1505b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1506b5816c88SCedric Chaumont goto out; 1507b5816c88SCedric Chaumont 1508b0104773SPascal Brand dst += tmp_dlen; 1509b0104773SPascal Brand acc_dlen += tmp_dlen; 1510b0104773SPascal Brand 1511b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1512b5816c88SCedric Chaumont res = utee_authenc_dec_final(operation->state, operation->buffer, 1513b5816c88SCedric Chaumont operation->buffer_offs, dst, &tmp_dlen, 1514b5816c88SCedric Chaumont tag, tagLen); 1515b5816c88SCedric Chaumont if (res != TEE_SUCCESS) 1516b5816c88SCedric Chaumont goto out; 1517b5816c88SCedric Chaumont 1518b0104773SPascal Brand /* Supplied tagLen should match what we initiated with */ 1519b5816c88SCedric Chaumont if (tagLen != operation->ae_tag_len) 1520b0104773SPascal Brand res = TEE_ERROR_MAC_INVALID; 1521b0104773SPascal Brand 1522b0104773SPascal Brand acc_dlen += tmp_dlen; 1523b0104773SPascal Brand *destLen = acc_dlen; 1524642a1607SCedric Chaumont 1525b5816c88SCedric Chaumont operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1526b5816c88SCedric Chaumont 1527642a1607SCedric Chaumont operation->operationState = TEE_OPERATION_STATE_INITIAL; 1528642a1607SCedric Chaumont 1529b5816c88SCedric Chaumont out: 1530b5816c88SCedric Chaumont if (res != TEE_SUCCESS && 1531b5816c88SCedric Chaumont res != TEE_ERROR_SHORT_BUFFER && 1532b5816c88SCedric Chaumont res != TEE_ERROR_MAC_INVALID) 1533b5816c88SCedric Chaumont TEE_Panic(res); 1534b0104773SPascal Brand 1535b0104773SPascal Brand return res; 1536b0104773SPascal Brand } 1537b0104773SPascal Brand 1538b0104773SPascal Brand /* Cryptographic Operations API - Asymmetric Functions */ 1539b0104773SPascal Brand 154012e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation, 154112e66b6fSCedric Chaumont TEE_Attribute *params, 154212e66b6fSCedric Chaumont uint32_t paramCount, void *srcData, 154379a3c601SCedric Chaumont uint32_t srcLen, void *destData, 154479a3c601SCedric Chaumont uint32_t *destLen) 1545b0104773SPascal Brand { 1546b0104773SPascal Brand TEE_Result res; 1547*e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1548*e86f1266SJens Wiklander uint64_t dl; 1549b0104773SPascal Brand 155012e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1551b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 1552b0104773SPascal Brand TEE_Panic(0); 155312e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1554b0104773SPascal Brand TEE_Panic(0); 155512e66b6fSCedric Chaumont if (!operation->key1) 1556b0104773SPascal Brand TEE_Panic(0); 155712e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 155812e66b6fSCedric Chaumont TEE_Panic(0); 155912e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_ENCRYPT) 1560b0104773SPascal Brand TEE_Panic(0); 1561b0104773SPascal Brand 1562*e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1563*e86f1266SJens Wiklander dl = *destLen; 1564*e86f1266SJens Wiklander res = utee_asymm_operate(operation->state, ua, paramCount, srcData, 1565*e86f1266SJens Wiklander srcLen, destData, &dl); 1566*e86f1266SJens Wiklander *destLen = dl; 156712e66b6fSCedric Chaumont 15688844ebfcSPascal Brand if (res != TEE_SUCCESS && 15698844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 15708844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1571b0104773SPascal Brand TEE_Panic(res); 157212e66b6fSCedric Chaumont 1573b0104773SPascal Brand return res; 1574b0104773SPascal Brand } 1575b0104773SPascal Brand 157612e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation, 157712e66b6fSCedric Chaumont TEE_Attribute *params, 157812e66b6fSCedric Chaumont uint32_t paramCount, void *srcData, 157979a3c601SCedric Chaumont uint32_t srcLen, void *destData, 158079a3c601SCedric Chaumont uint32_t *destLen) 1581b0104773SPascal Brand { 1582b0104773SPascal Brand TEE_Result res; 1583*e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1584*e86f1266SJens Wiklander uint64_t dl; 1585b0104773SPascal Brand 158612e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1587b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 1588b0104773SPascal Brand TEE_Panic(0); 158912e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1590b0104773SPascal Brand TEE_Panic(0); 159112e66b6fSCedric Chaumont if (!operation->key1) 1592b0104773SPascal Brand TEE_Panic(0); 159312e66b6fSCedric Chaumont if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 159412e66b6fSCedric Chaumont TEE_Panic(0); 159512e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_DECRYPT) 1596b0104773SPascal Brand TEE_Panic(0); 1597b0104773SPascal Brand 1598*e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1599*e86f1266SJens Wiklander dl = *destLen; 1600*e86f1266SJens Wiklander res = utee_asymm_operate(operation->state, ua, paramCount, srcData, 1601*e86f1266SJens Wiklander srcLen, destData, &dl); 1602*e86f1266SJens Wiklander *destLen = dl; 160312e66b6fSCedric Chaumont 16048844ebfcSPascal Brand if (res != TEE_SUCCESS && 16058844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 16068844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1607b0104773SPascal Brand TEE_Panic(res); 160812e66b6fSCedric Chaumont 1609b0104773SPascal Brand return res; 1610b0104773SPascal Brand } 1611b0104773SPascal Brand 161212e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation, 161312e66b6fSCedric Chaumont TEE_Attribute *params, 161412e66b6fSCedric Chaumont uint32_t paramCount, void *digest, 161579a3c601SCedric Chaumont uint32_t digestLen, void *signature, 161679a3c601SCedric Chaumont uint32_t *signatureLen) 1617b0104773SPascal Brand { 1618b0104773SPascal Brand TEE_Result res; 1619*e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1620*e86f1266SJens Wiklander uint64_t sl; 1621b0104773SPascal Brand 162212e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || 162312e66b6fSCedric Chaumont (digest == NULL && digestLen != 0) || 1624b0104773SPascal Brand signature == NULL || signatureLen == NULL) 1625b0104773SPascal Brand TEE_Panic(0); 162612e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1627b0104773SPascal Brand TEE_Panic(0); 162812e66b6fSCedric Chaumont if (!operation->key1) 1629b0104773SPascal Brand TEE_Panic(0); 163012e66b6fSCedric Chaumont if (operation->info.operationClass != 163112e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 163212e66b6fSCedric Chaumont TEE_Panic(0); 163312e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_SIGN) 1634b0104773SPascal Brand TEE_Panic(0); 1635b0104773SPascal Brand 1636*e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1637*e86f1266SJens Wiklander sl = *signatureLen; 1638*e86f1266SJens Wiklander res = utee_asymm_operate(operation->state, ua, paramCount, digest, 1639*e86f1266SJens Wiklander digestLen, signature, &sl); 1640*e86f1266SJens Wiklander *signatureLen = sl; 164112e66b6fSCedric Chaumont 1642b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER) 1643b0104773SPascal Brand TEE_Panic(res); 164412e66b6fSCedric Chaumont 1645b0104773SPascal Brand return res; 1646b0104773SPascal Brand } 1647b0104773SPascal Brand 164812e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation, 164912e66b6fSCedric Chaumont TEE_Attribute *params, 165012e66b6fSCedric Chaumont uint32_t paramCount, void *digest, 165112e66b6fSCedric Chaumont uint32_t digestLen, void *signature, 165279a3c601SCedric Chaumont uint32_t signatureLen) 1653b0104773SPascal Brand { 1654b0104773SPascal Brand TEE_Result res; 1655*e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1656b0104773SPascal Brand 165712e66b6fSCedric Chaumont if (operation == TEE_HANDLE_NULL || 165812e66b6fSCedric Chaumont (digest == NULL && digestLen != 0) || 1659b0104773SPascal Brand (signature == NULL && signatureLen != 0)) 1660b0104773SPascal Brand TEE_Panic(0); 166112e66b6fSCedric Chaumont if (params == NULL && paramCount != 0) 1662b0104773SPascal Brand TEE_Panic(0); 166312e66b6fSCedric Chaumont if (!operation->key1) 1664b0104773SPascal Brand TEE_Panic(0); 166512e66b6fSCedric Chaumont if (operation->info.operationClass != 166612e66b6fSCedric Chaumont TEE_OPERATION_ASYMMETRIC_SIGNATURE) 166712e66b6fSCedric Chaumont TEE_Panic(0); 166812e66b6fSCedric Chaumont if (operation->info.mode != TEE_MODE_VERIFY) 1669b0104773SPascal Brand TEE_Panic(0); 1670b0104773SPascal Brand 1671*e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1672*e86f1266SJens Wiklander res = utee_asymm_verify(operation->state, ua, paramCount, digest, 167312e66b6fSCedric Chaumont digestLen, signature, signatureLen); 167412e66b6fSCedric Chaumont 1675b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID) 1676b0104773SPascal Brand TEE_Panic(res); 167712e66b6fSCedric Chaumont 1678b0104773SPascal Brand return res; 1679b0104773SPascal Brand } 1680b0104773SPascal Brand 1681b0104773SPascal Brand /* Cryptographic Operations API - Key Derivation Functions */ 1682b0104773SPascal Brand 1683b0104773SPascal Brand void TEE_DeriveKey(TEE_OperationHandle operation, 1684b0104773SPascal Brand const TEE_Attribute *params, uint32_t paramCount, 1685b0104773SPascal Brand TEE_ObjectHandle derivedKey) 1686b0104773SPascal Brand { 1687b0104773SPascal Brand TEE_Result res; 1688b0104773SPascal Brand TEE_ObjectInfo key_info; 1689*e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 1690b0104773SPascal Brand 1691b0104773SPascal Brand if (operation == TEE_HANDLE_NULL || derivedKey == 0) 1692b0104773SPascal Brand TEE_Panic(0); 169384fa9467SCedric Chaumont if (params == NULL && paramCount != 0) 1694b0104773SPascal Brand TEE_Panic(0); 16958854d3c6SJerome Forissier if (TEE_ALG_GET_CLASS(operation->info.algorithm) != 16968854d3c6SJerome Forissier TEE_OPERATION_KEY_DERIVATION) 1697b0104773SPascal Brand TEE_Panic(0); 1698b0104773SPascal Brand 1699b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION) 1700b0104773SPascal Brand TEE_Panic(0); 170184fa9467SCedric Chaumont if (!operation->key1) 170284fa9467SCedric Chaumont TEE_Panic(0); 1703b0104773SPascal Brand if (operation->info.mode != TEE_MODE_DERIVE) 1704b0104773SPascal Brand TEE_Panic(0); 1705b0104773SPascal Brand if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0) 1706b0104773SPascal Brand TEE_Panic(0); 1707b0104773SPascal Brand 1708*e86f1266SJens Wiklander res = utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info); 1709b0104773SPascal Brand if (res != TEE_SUCCESS) 1710b0104773SPascal Brand TEE_Panic(0); 1711b0104773SPascal Brand 1712b0104773SPascal Brand if (key_info.objectType != TEE_TYPE_GENERIC_SECRET) 1713b0104773SPascal Brand TEE_Panic(0); 1714b0104773SPascal Brand if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1715b0104773SPascal Brand TEE_Panic(0); 1716b0104773SPascal Brand 1717*e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 1718*e86f1266SJens Wiklander res = utee_cryp_derive_key(operation->state, ua, paramCount, 1719*e86f1266SJens Wiklander (unsigned long)derivedKey); 1720b0104773SPascal Brand if (res != TEE_SUCCESS) 1721b0104773SPascal Brand TEE_Panic(res); 1722b0104773SPascal Brand } 1723b0104773SPascal Brand 1724b0104773SPascal Brand /* Cryptographic Operations API - Random Number Generation Functions */ 1725b0104773SPascal Brand 172679a3c601SCedric Chaumont void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen) 1727b0104773SPascal Brand { 1728b0104773SPascal Brand TEE_Result res; 1729b0104773SPascal Brand 1730b0104773SPascal Brand res = utee_cryp_random_number_generate(randomBuffer, randomBufferLen); 1731b0104773SPascal Brand if (res != TEE_SUCCESS) 1732b0104773SPascal Brand TEE_Panic(res); 1733b0104773SPascal Brand } 1734