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> 37b0104773SPascal Brand 38b0104773SPascal Brand struct __TEE_OperationHandle { 39b0104773SPascal Brand TEE_OperationInfo info; 40b0104773SPascal Brand TEE_ObjectHandle key1; 41b0104773SPascal Brand TEE_ObjectHandle key2; 42b0104773SPascal Brand uint8_t *buffer; /* buffer to collect complete blocks */ 43b0104773SPascal Brand bool buffer_two_blocks; /* True if two blocks need to be buffered */ 44b0104773SPascal Brand size_t block_size; /* Block size of cipher */ 45b0104773SPascal Brand size_t buffer_offs; /* Offset in buffer */ 46b0104773SPascal Brand uint32_t state; /* Handle to state in TEE Core */ 47b0104773SPascal Brand uint32_t ae_tag_len; /* 48b0104773SPascal Brand * tag_len in bytes for AE operation else unused 49b0104773SPascal Brand */ 50b0104773SPascal Brand }; 51b0104773SPascal Brand 52b0104773SPascal Brand /* Cryptographic Operations API - Generic Operation Functions */ 53b0104773SPascal Brand 54b0104773SPascal Brand TEE_Result TEE_AllocateOperation(TEE_OperationHandle *operation, 55b0104773SPascal Brand uint32_t algorithm, uint32_t mode, 56b0104773SPascal Brand uint32_t maxKeySize) 57b0104773SPascal Brand { 58b0104773SPascal Brand TEE_Result res; 59b0104773SPascal Brand TEE_OperationHandle op = TEE_HANDLE_NULL; 60b0104773SPascal Brand uint32_t handle_state = 0; 61b0104773SPascal Brand size_t block_size = 1; 62b0104773SPascal Brand uint32_t req_key_usage; 63b0104773SPascal Brand bool with_private_key = false; 64b0104773SPascal Brand bool buffer_two_blocks = false; 65b0104773SPascal Brand 669b52c538SCedric Chaumont if (!operation) 67b0104773SPascal Brand TEE_Panic(0); 68b0104773SPascal Brand 69b0104773SPascal Brand if (algorithm == TEE_ALG_AES_XTS) 70b0104773SPascal Brand handle_state = TEE_HANDLE_FLAG_EXPECT_TWO_KEYS; 71b0104773SPascal Brand 72218d9055SCedric Chaumont /* Check algorithm max key size */ 73218d9055SCedric Chaumont switch (algorithm) { 74218d9055SCedric Chaumont case TEE_ALG_DSA_SHA1: 75218d9055SCedric Chaumont if (maxKeySize < 512) 76218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 77218d9055SCedric Chaumont if (maxKeySize > 1024) 78218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 79218d9055SCedric Chaumont if (maxKeySize % 64 != 0) 80218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 81218d9055SCedric Chaumont break; 82218d9055SCedric Chaumont 83218d9055SCedric Chaumont case TEE_ALG_DSA_SHA224: 84218d9055SCedric Chaumont if (maxKeySize != 2048) 85218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 86218d9055SCedric Chaumont break; 87218d9055SCedric Chaumont 88218d9055SCedric Chaumont case TEE_ALG_DSA_SHA256: 89218d9055SCedric Chaumont if (maxKeySize != 2048 && maxKeySize != 3072) 90218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 91218d9055SCedric Chaumont break; 92218d9055SCedric Chaumont 931220586eSCedric Chaumont case TEE_ALG_ECDSA_P192: 941220586eSCedric Chaumont case TEE_ALG_ECDH_P192: 951220586eSCedric Chaumont if (maxKeySize != 192) 961220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 971220586eSCedric Chaumont break; 981220586eSCedric Chaumont 991220586eSCedric Chaumont case TEE_ALG_ECDSA_P224: 1001220586eSCedric Chaumont case TEE_ALG_ECDH_P224: 1011220586eSCedric Chaumont if (maxKeySize != 224) 1021220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 1031220586eSCedric Chaumont break; 1041220586eSCedric Chaumont 1051220586eSCedric Chaumont case TEE_ALG_ECDSA_P256: 1061220586eSCedric Chaumont case TEE_ALG_ECDH_P256: 1071220586eSCedric Chaumont if (maxKeySize != 256) 1081220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 1091220586eSCedric Chaumont break; 1101220586eSCedric Chaumont 1111220586eSCedric Chaumont case TEE_ALG_ECDSA_P384: 1121220586eSCedric Chaumont case TEE_ALG_ECDH_P384: 1131220586eSCedric Chaumont if (maxKeySize != 384) 1141220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 1151220586eSCedric Chaumont break; 1161220586eSCedric Chaumont 1171220586eSCedric Chaumont case TEE_ALG_ECDSA_P521: 1181220586eSCedric Chaumont case TEE_ALG_ECDH_P521: 1191220586eSCedric Chaumont if (maxKeySize != 521) 1201220586eSCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 1211220586eSCedric Chaumont break; 1221220586eSCedric Chaumont 123218d9055SCedric Chaumont default: 124218d9055SCedric Chaumont break; 125218d9055SCedric Chaumont } 126218d9055SCedric Chaumont 127218d9055SCedric Chaumont /* Check algorithm mode */ 128b0104773SPascal Brand switch (algorithm) { 129b0104773SPascal Brand case TEE_ALG_AES_CTS: 130b0104773SPascal Brand case TEE_ALG_AES_XTS: 131b0104773SPascal Brand buffer_two_blocks = true; 132b0104773SPascal Brand /*FALLTHROUGH*/ case TEE_ALG_AES_ECB_NOPAD: 133b0104773SPascal Brand case TEE_ALG_AES_CBC_NOPAD: 134b0104773SPascal Brand case TEE_ALG_AES_CTR: 135b0104773SPascal Brand case TEE_ALG_AES_CCM: 136b0104773SPascal Brand case TEE_ALG_AES_GCM: 137b0104773SPascal Brand case TEE_ALG_DES_ECB_NOPAD: 138b0104773SPascal Brand case TEE_ALG_DES_CBC_NOPAD: 139b0104773SPascal Brand case TEE_ALG_DES3_ECB_NOPAD: 140b0104773SPascal Brand case TEE_ALG_DES3_CBC_NOPAD: 141b0104773SPascal Brand if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES) 142b0104773SPascal Brand block_size = TEE_AES_BLOCK_SIZE; 143b0104773SPascal Brand else 144b0104773SPascal Brand block_size = TEE_DES_BLOCK_SIZE; 145b0104773SPascal Brand 146b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) 147b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT; 148b0104773SPascal Brand else if (mode == TEE_MODE_DECRYPT) 149b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT; 150b0104773SPascal Brand else 151b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 152b0104773SPascal Brand break; 153b0104773SPascal Brand 154b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_MD5: 155b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1: 156b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224: 157b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256: 158b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384: 159b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512: 160b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1: 161b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224: 162b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256: 163b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384: 164b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512: 165b0104773SPascal Brand case TEE_ALG_DSA_SHA1: 166218d9055SCedric Chaumont case TEE_ALG_DSA_SHA224: 167218d9055SCedric Chaumont case TEE_ALG_DSA_SHA256: 1681220586eSCedric Chaumont case TEE_ALG_ECDSA_P192: 1691220586eSCedric Chaumont case TEE_ALG_ECDSA_P224: 1701220586eSCedric Chaumont case TEE_ALG_ECDSA_P256: 1711220586eSCedric Chaumont case TEE_ALG_ECDSA_P384: 1721220586eSCedric Chaumont case TEE_ALG_ECDSA_P521: 173b0104773SPascal Brand if (mode == TEE_MODE_SIGN) { 174b0104773SPascal Brand with_private_key = true; 175b0104773SPascal Brand req_key_usage = TEE_USAGE_SIGN; 176b0104773SPascal Brand } else if (mode == TEE_MODE_VERIFY) { 177b0104773SPascal Brand req_key_usage = TEE_USAGE_VERIFY; 178b0104773SPascal Brand } else { 179b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 180b0104773SPascal Brand } 181b0104773SPascal Brand break; 182b0104773SPascal Brand 183b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_V1_5: 184b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1: 185b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224: 186b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256: 187b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384: 188b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512: 189b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) { 190b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT; 191b0104773SPascal Brand } else if (mode == TEE_MODE_DECRYPT) { 192b0104773SPascal Brand with_private_key = true; 193b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT; 194b0104773SPascal Brand } else { 195b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 196b0104773SPascal Brand } 197b0104773SPascal Brand break; 198b0104773SPascal Brand 199b0104773SPascal Brand case TEE_ALG_RSA_NOPAD: 200b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) { 201b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY; 202b0104773SPascal Brand } else if (mode == TEE_MODE_DECRYPT) { 203b0104773SPascal Brand with_private_key = true; 204b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT | TEE_USAGE_SIGN; 205b0104773SPascal Brand } else { 206b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 207b0104773SPascal Brand } 208b0104773SPascal Brand break; 209b0104773SPascal Brand 210b0104773SPascal Brand case TEE_ALG_DH_DERIVE_SHARED_SECRET: 2111220586eSCedric Chaumont case TEE_ALG_ECDH_P192: 2121220586eSCedric Chaumont case TEE_ALG_ECDH_P224: 2131220586eSCedric Chaumont case TEE_ALG_ECDH_P256: 2141220586eSCedric Chaumont case TEE_ALG_ECDH_P384: 2151220586eSCedric Chaumont case TEE_ALG_ECDH_P521: 216cdb198a7SJerome Forissier case TEE_ALG_HKDF_MD5_DERIVE_KEY: 217cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA1_DERIVE_KEY: 218cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA224_DERIVE_KEY: 219cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA256_DERIVE_KEY: 220cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA384_DERIVE_KEY: 221cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA512_DERIVE_KEY: 2228854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA1_DERIVE_KEY: 2238854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA224_DERIVE_KEY: 2248854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA256_DERIVE_KEY: 2258854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA384_DERIVE_KEY: 2268854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA512_DERIVE_KEY: 2270f2293b7SJerome Forissier case TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY: 228b0104773SPascal Brand if (mode != TEE_MODE_DERIVE) 229b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 230b0104773SPascal Brand with_private_key = true; 231b0104773SPascal Brand req_key_usage = TEE_USAGE_DERIVE; 232b0104773SPascal Brand break; 233b0104773SPascal Brand 234b0104773SPascal Brand case TEE_ALG_MD5: 235b0104773SPascal Brand case TEE_ALG_SHA1: 236b0104773SPascal Brand case TEE_ALG_SHA224: 237b0104773SPascal Brand case TEE_ALG_SHA256: 238b0104773SPascal Brand case TEE_ALG_SHA384: 239b0104773SPascal Brand case TEE_ALG_SHA512: 240b0104773SPascal Brand if (mode != TEE_MODE_DIGEST) 241b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 242b0104773SPascal Brand handle_state |= TEE_HANDLE_FLAG_KEY_SET; 243b0104773SPascal Brand req_key_usage = 0; 244b0104773SPascal Brand break; 245b0104773SPascal Brand 246b0104773SPascal Brand case TEE_ALG_DES_CBC_MAC_NOPAD: 247b0104773SPascal Brand case TEE_ALG_AES_CBC_MAC_NOPAD: 248b0104773SPascal Brand case TEE_ALG_AES_CBC_MAC_PKCS5: 249b0104773SPascal Brand case TEE_ALG_AES_CMAC: 250b0104773SPascal Brand case TEE_ALG_DES_CBC_MAC_PKCS5: 251b0104773SPascal Brand case TEE_ALG_DES3_CBC_MAC_NOPAD: 252b0104773SPascal Brand case TEE_ALG_DES3_CBC_MAC_PKCS5: 253b0104773SPascal Brand case TEE_ALG_HMAC_MD5: 254b0104773SPascal Brand case TEE_ALG_HMAC_SHA1: 255b0104773SPascal Brand case TEE_ALG_HMAC_SHA224: 256b0104773SPascal Brand case TEE_ALG_HMAC_SHA256: 257b0104773SPascal Brand case TEE_ALG_HMAC_SHA384: 258b0104773SPascal Brand case TEE_ALG_HMAC_SHA512: 259b0104773SPascal Brand if (mode != TEE_MODE_MAC) 260b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 261b0104773SPascal Brand req_key_usage = TEE_USAGE_MAC; 262b0104773SPascal Brand break; 263b0104773SPascal Brand 264b0104773SPascal Brand default: 265b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 266b0104773SPascal Brand } 267b0104773SPascal Brand 268b0104773SPascal Brand op = TEE_Malloc(sizeof(*op), 0); 2699b52c538SCedric Chaumont if (!op) 270b0104773SPascal Brand return TEE_ERROR_OUT_OF_MEMORY; 271b0104773SPascal Brand 272b0104773SPascal Brand op->info.algorithm = algorithm; 273b0104773SPascal Brand op->info.operationClass = TEE_ALG_GET_CLASS(algorithm); 274b0104773SPascal Brand op->info.mode = mode; 275b0104773SPascal Brand op->info.maxKeySize = maxKeySize; 276b0104773SPascal Brand op->info.requiredKeyUsage = req_key_usage; 277b0104773SPascal Brand op->info.handleState = handle_state; 278b0104773SPascal Brand 279b0104773SPascal Brand if (block_size > 1) { 280b0104773SPascal Brand size_t buffer_size = block_size; 281b0104773SPascal Brand 282b0104773SPascal Brand if (buffer_two_blocks) 283b0104773SPascal Brand buffer_size *= 2; 284b0104773SPascal Brand 2859b52c538SCedric Chaumont op->buffer = TEE_Malloc(buffer_size, 2869b52c538SCedric Chaumont TEE_USER_MEM_HINT_NO_FILL_ZERO); 287b0104773SPascal Brand if (op->buffer == NULL) { 288b0104773SPascal Brand res = TEE_ERROR_OUT_OF_MEMORY; 2899b52c538SCedric Chaumont goto err0; 290b0104773SPascal Brand } 291b0104773SPascal Brand } 292b0104773SPascal Brand op->block_size = block_size; 293b0104773SPascal Brand op->buffer_two_blocks = buffer_two_blocks; 294b0104773SPascal Brand 295b0104773SPascal Brand if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) { 296b0104773SPascal Brand uint32_t mks = maxKeySize; 297b0104773SPascal Brand TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm, 298b0104773SPascal Brand with_private_key); 299b0104773SPascal Brand 300b0104773SPascal Brand /* 301b0104773SPascal Brand * If two keys are expected the max key size is the sum of 302b0104773SPascal Brand * the size of both keys. 303b0104773SPascal Brand */ 304b0104773SPascal Brand if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) 305b0104773SPascal Brand mks /= 2; 306b0104773SPascal Brand 307b0104773SPascal Brand res = TEE_AllocateTransientObject(key_type, mks, &op->key1); 308b0104773SPascal Brand if (res != TEE_SUCCESS) 3099b52c538SCedric Chaumont goto err1; 310b0104773SPascal Brand 311b0104773SPascal Brand if ((op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 312b0104773SPascal Brand 0) { 3139b52c538SCedric Chaumont res = TEE_AllocateTransientObject(key_type, mks, 314b0104773SPascal Brand &op->key2); 315b0104773SPascal Brand if (res != TEE_SUCCESS) 3169b52c538SCedric Chaumont goto err2; 317b0104773SPascal Brand } 318b0104773SPascal Brand } 319b0104773SPascal Brand 320b0104773SPascal Brand res = utee_cryp_state_alloc(algorithm, mode, (uint32_t) op->key1, 321b0104773SPascal Brand (uint32_t) op->key2, &op->state); 3229b52c538SCedric Chaumont if (res != TEE_SUCCESS) { 3239b52c538SCedric Chaumont if ((op->info.handleState & 3249b52c538SCedric Chaumont TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 0) 3259b52c538SCedric Chaumont goto err2; 3269b52c538SCedric Chaumont goto err1; 3279b52c538SCedric Chaumont } 328b0104773SPascal Brand 329b0104773SPascal Brand /* For multi-stage operation do an "init". */ 330b0104773SPascal Brand TEE_ResetOperation(op); 331b0104773SPascal Brand *operation = op; 3329b52c538SCedric Chaumont goto out; 333b0104773SPascal Brand 3349b52c538SCedric Chaumont err2: 335b0104773SPascal Brand TEE_FreeTransientObject(op->key2); 3369b52c538SCedric Chaumont err1: 3379b52c538SCedric Chaumont TEE_FreeTransientObject(op->key1); 3389b52c538SCedric Chaumont err0: 339b0104773SPascal Brand TEE_FreeOperation(op); 340b0104773SPascal Brand 3419b52c538SCedric Chaumont if (res != TEE_SUCCESS && 3429b52c538SCedric Chaumont res != TEE_ERROR_OUT_OF_MEMORY && 3439b52c538SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 3449b52c538SCedric Chaumont TEE_Panic(0); 3459b52c538SCedric Chaumont out: 346b0104773SPascal Brand return res; 347b0104773SPascal Brand } 348b0104773SPascal Brand 349b0104773SPascal Brand void TEE_FreeOperation(TEE_OperationHandle operation) 350b0104773SPascal Brand { 351e889e80bSCedric Chaumont TEE_Result res; 352e889e80bSCedric Chaumont 353e889e80bSCedric Chaumont if (operation == TEE_HANDLE_NULL) 354e889e80bSCedric Chaumont TEE_Panic(0); 355e889e80bSCedric Chaumont 356b0104773SPascal Brand /* 357b0104773SPascal Brand * Note that keys should not be freed here, since they are 358b0104773SPascal Brand * claimed by the operation they will be freed by 359b0104773SPascal Brand * utee_cryp_state_free(). 360b0104773SPascal Brand */ 361e889e80bSCedric Chaumont res = utee_cryp_state_free(operation->state); 362e889e80bSCedric Chaumont if (res != TEE_SUCCESS) 363e889e80bSCedric Chaumont TEE_Panic(0); 364e889e80bSCedric Chaumont 365b0104773SPascal Brand TEE_Free(operation->buffer); 366b0104773SPascal Brand TEE_Free(operation); 367b0104773SPascal Brand } 368b0104773SPascal Brand 369b0104773SPascal Brand void TEE_GetOperationInfo(TEE_OperationHandle operation, 370b0104773SPascal Brand TEE_OperationInfo *operationInfo) 371b0104773SPascal Brand { 372b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 373b0104773SPascal Brand TEE_Panic(0); 374b0104773SPascal Brand 375b0104773SPascal Brand if (operationInfo == NULL) 376b0104773SPascal Brand TEE_Panic(0); 377b0104773SPascal Brand 378b0104773SPascal Brand *operationInfo = operation->info; 379b0104773SPascal Brand } 380b0104773SPascal Brand 381b0104773SPascal Brand void TEE_ResetOperation(TEE_OperationHandle operation) 382b0104773SPascal Brand { 383b0104773SPascal Brand TEE_Result res; 384b0104773SPascal Brand 385b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 386b0104773SPascal Brand TEE_Panic(0); 387bf80076aSCedric Chaumont 388bf80076aSCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) != 0) { 389bf80076aSCedric Chaumont if (operation->info.keySize == 0) 390bf80076aSCedric Chaumont TEE_Panic(0); 391bf80076aSCedric Chaumont } 392bf80076aSCedric Chaumont 393b0104773SPascal Brand if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 394b0104773SPascal Brand res = utee_hash_init(operation->state, NULL, 0); 395b0104773SPascal Brand if (res != TEE_SUCCESS) 396b0104773SPascal Brand TEE_Panic(res); 397b0104773SPascal Brand } 398b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 399b0104773SPascal Brand } 400b0104773SPascal Brand 401b0104773SPascal Brand TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation, 402b0104773SPascal Brand TEE_ObjectHandle key) 403b0104773SPascal Brand { 4047583c59eSCedric Chaumont TEE_Result res; 405b0104773SPascal Brand uint32_t key_size = 0; 406b0104773SPascal Brand TEE_ObjectInfo key_info; 407b0104773SPascal Brand 408*a57c1e2eSCedric Chaumont /* Operation is not a valid handle */ 409*a57c1e2eSCedric Chaumont if (operation == TEE_HANDLE_NULL) { 410*a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 411*a57c1e2eSCedric Chaumont goto out; 412*a57c1e2eSCedric Chaumont } 413*a57c1e2eSCedric Chaumont 414*a57c1e2eSCedric Chaumont /* Key is not initialized */ 415*a57c1e2eSCedric Chaumont if (key == TEE_HANDLE_NULL) { 416*a57c1e2eSCedric Chaumont /* Operation key cleared */ 417*a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key1); 418*a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 419*a57c1e2eSCedric Chaumont goto out; 420*a57c1e2eSCedric Chaumont } 421*a57c1e2eSCedric Chaumont 422*a57c1e2eSCedric Chaumont /* No key for digest operation */ 423*a57c1e2eSCedric Chaumont if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 424*a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 425*a57c1e2eSCedric Chaumont goto out; 426*a57c1e2eSCedric Chaumont } 427*a57c1e2eSCedric Chaumont 428*a57c1e2eSCedric Chaumont /* Two keys flag not expected (TEE_ALG_AES_XTS excluded) */ 429*a57c1e2eSCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 430*a57c1e2eSCedric Chaumont 0) { 431*a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 432*a57c1e2eSCedric Chaumont goto out; 433*a57c1e2eSCedric Chaumont } 434*a57c1e2eSCedric Chaumont 4357583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key, &key_info); 436*a57c1e2eSCedric Chaumont /* Key is not a valid handle */ 4377583c59eSCedric Chaumont if (res != TEE_SUCCESS) 438*a57c1e2eSCedric Chaumont goto out; 4397583c59eSCedric Chaumont 440b0104773SPascal Brand /* Supplied key has to meet required usage */ 441b0104773SPascal Brand if ((key_info.objectUsage & operation->info.requiredKeyUsage) != 442b0104773SPascal Brand operation->info.requiredKeyUsage) { 443*a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 444*a57c1e2eSCedric Chaumont goto out; 445b0104773SPascal Brand } 446b0104773SPascal Brand 447*a57c1e2eSCedric Chaumont if (operation->info.maxKeySize < key_info.keySize) { 448*a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 449*a57c1e2eSCedric Chaumont goto out; 450*a57c1e2eSCedric Chaumont } 451b0104773SPascal Brand 4527583c59eSCedric Chaumont key_size = key_info.keySize; 453b0104773SPascal Brand 454b0104773SPascal Brand TEE_ResetTransientObject(operation->key1); 455b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 456b0104773SPascal Brand 4577583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key1, key); 4587583c59eSCedric Chaumont if (res != TEE_SUCCESS) 459*a57c1e2eSCedric Chaumont goto out; 4607583c59eSCedric Chaumont 461b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 462b0104773SPascal Brand 463b0104773SPascal Brand operation->info.keySize = key_size; 464b0104773SPascal Brand 4657583c59eSCedric Chaumont out: 466*a57c1e2eSCedric Chaumont if (res != TEE_SUCCESS && 467*a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 468*a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 469*a57c1e2eSCedric Chaumont TEE_Panic(0); 470*a57c1e2eSCedric Chaumont 471*a57c1e2eSCedric Chaumont return res; 472b0104773SPascal Brand } 473b0104773SPascal Brand 474b0104773SPascal Brand TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation, 475b0104773SPascal Brand TEE_ObjectHandle key1, TEE_ObjectHandle key2) 476b0104773SPascal Brand { 4777583c59eSCedric Chaumont TEE_Result res; 478b0104773SPascal Brand uint32_t key_size = 0; 479b0104773SPascal Brand TEE_ObjectInfo key_info1; 480b0104773SPascal Brand TEE_ObjectInfo key_info2; 481b0104773SPascal Brand 482*a57c1e2eSCedric Chaumont /* Operation is not a valid handle */ 483*a57c1e2eSCedric Chaumont if (operation == TEE_HANDLE_NULL) { 484*a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 485*a57c1e2eSCedric Chaumont goto out; 486*a57c1e2eSCedric Chaumont } 487*a57c1e2eSCedric Chaumont 488*a57c1e2eSCedric Chaumont /* 489*a57c1e2eSCedric Chaumont * Key1/Key2 and/or are not initialized and 490*a57c1e2eSCedric Chaumont * Either both keys are NULL or both are not NULL 491*a57c1e2eSCedric Chaumont */ 492*a57c1e2eSCedric Chaumont if (key1 == TEE_HANDLE_NULL || key2 == TEE_HANDLE_NULL) { 493*a57c1e2eSCedric Chaumont /* Clear operation key1 (if needed) */ 494*a57c1e2eSCedric Chaumont if (key1 == TEE_HANDLE_NULL) 495*a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key1); 496*a57c1e2eSCedric Chaumont /* Clear operation key2 (if needed) */ 497*a57c1e2eSCedric Chaumont if (key2 == TEE_HANDLE_NULL) 498*a57c1e2eSCedric Chaumont TEE_ResetTransientObject(operation->key2); 499*a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 500*a57c1e2eSCedric Chaumont goto out; 501*a57c1e2eSCedric Chaumont } 502*a57c1e2eSCedric Chaumont 503*a57c1e2eSCedric Chaumont /* No key for digest operation */ 504*a57c1e2eSCedric Chaumont if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 505*a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 506*a57c1e2eSCedric Chaumont goto out; 507*a57c1e2eSCedric Chaumont } 508*a57c1e2eSCedric Chaumont 509*a57c1e2eSCedric Chaumont /* Two keys flag expected (TEE_ALG_AES_XTS only) */ 510*a57c1e2eSCedric Chaumont if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 511*a57c1e2eSCedric Chaumont 0) { 512*a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 513*a57c1e2eSCedric Chaumont goto out; 514*a57c1e2eSCedric Chaumont } 515*a57c1e2eSCedric Chaumont 5167583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key1, &key_info1); 517*a57c1e2eSCedric Chaumont /* Key1 is not a valid handle */ 5187583c59eSCedric Chaumont if (res != TEE_SUCCESS) 519*a57c1e2eSCedric Chaumont goto out; 5207583c59eSCedric Chaumont 521b0104773SPascal Brand /* Supplied key has to meet required usage */ 522b0104773SPascal Brand if ((key_info1.objectUsage & operation->info. 523b0104773SPascal Brand requiredKeyUsage) != operation->info.requiredKeyUsage) { 524*a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 525*a57c1e2eSCedric Chaumont goto out; 526b0104773SPascal Brand } 527b0104773SPascal Brand 5287583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key2, &key_info2); 529*a57c1e2eSCedric Chaumont /* Key2 is not a valid handle */ 5307583c59eSCedric Chaumont if (res != TEE_SUCCESS) { 5317583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) 5327583c59eSCedric Chaumont res = TEE_ERROR_CORRUPT_OBJECT_2; 533*a57c1e2eSCedric Chaumont goto out; 5347583c59eSCedric Chaumont } 5357583c59eSCedric Chaumont 536b0104773SPascal Brand /* Supplied key has to meet required usage */ 537b0104773SPascal Brand if ((key_info2.objectUsage & operation->info. 538b0104773SPascal Brand requiredKeyUsage) != operation->info.requiredKeyUsage) { 539*a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 540*a57c1e2eSCedric Chaumont goto out; 541b0104773SPascal Brand } 542b0104773SPascal Brand 543b0104773SPascal Brand /* 544b0104773SPascal Brand * AES-XTS (the only multi key algorithm supported, requires the 545b0104773SPascal Brand * keys to be of equal size. 546b0104773SPascal Brand */ 547b0104773SPascal Brand if (operation->info.algorithm == TEE_ALG_AES_XTS && 548*a57c1e2eSCedric Chaumont key_info1.keySize != key_info2.keySize) { 549*a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 550*a57c1e2eSCedric Chaumont goto out; 551b0104773SPascal Brand 552*a57c1e2eSCedric Chaumont } 553*a57c1e2eSCedric Chaumont 554*a57c1e2eSCedric Chaumont if (operation->info.maxKeySize < key_info1.keySize) { 555*a57c1e2eSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 556*a57c1e2eSCedric Chaumont goto out; 557*a57c1e2eSCedric Chaumont } 558b0104773SPascal Brand 559b0104773SPascal Brand /* 560b0104773SPascal Brand * Odd that only the size of one key should be reported while 561b0104773SPascal Brand * size of two key are used when allocating the operation. 562b0104773SPascal Brand */ 5637583c59eSCedric Chaumont key_size = key_info1.keySize; 564b0104773SPascal Brand 565b0104773SPascal Brand TEE_ResetTransientObject(operation->key1); 566b0104773SPascal Brand TEE_ResetTransientObject(operation->key2); 567b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 568b0104773SPascal Brand 5697583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key1, key1); 5707583c59eSCedric Chaumont if (res != TEE_SUCCESS) 571*a57c1e2eSCedric Chaumont goto out; 5727583c59eSCedric Chaumont 5737583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key2, key2); 5747583c59eSCedric Chaumont if (res != TEE_SUCCESS) { 5757583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) 5767583c59eSCedric Chaumont res = TEE_ERROR_CORRUPT_OBJECT_2; 577*a57c1e2eSCedric Chaumont goto out; 5787583c59eSCedric Chaumont } 5797583c59eSCedric Chaumont 580b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 581b0104773SPascal Brand 582b0104773SPascal Brand operation->info.keySize = key_size; 583b0104773SPascal Brand 5847583c59eSCedric Chaumont out: 585*a57c1e2eSCedric Chaumont if (res != TEE_SUCCESS && 586*a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 587*a57c1e2eSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT_2 && 588*a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE && 589*a57c1e2eSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE_2) 590*a57c1e2eSCedric Chaumont TEE_Panic(0); 591*a57c1e2eSCedric Chaumont 592*a57c1e2eSCedric Chaumont return res; 593b0104773SPascal Brand } 594b0104773SPascal Brand 595b0104773SPascal Brand void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op) 596b0104773SPascal Brand { 597b0104773SPascal Brand TEE_Result res; 598b0104773SPascal Brand 599b0104773SPascal Brand if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL) 600b0104773SPascal Brand TEE_Panic(0); 601b0104773SPascal Brand if (dst_op->info.algorithm != src_op->info.algorithm) 602b0104773SPascal Brand TEE_Panic(0); 603b0104773SPascal Brand if (src_op->info.operationClass != TEE_OPERATION_DIGEST) { 604b0104773SPascal Brand TEE_ObjectHandle key1 = TEE_HANDLE_NULL; 605b0104773SPascal Brand TEE_ObjectHandle key2 = TEE_HANDLE_NULL; 606b0104773SPascal Brand 607b0104773SPascal Brand if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) { 608b0104773SPascal Brand key1 = src_op->key1; 609b0104773SPascal Brand key2 = src_op->key2; 610b0104773SPascal Brand } 611b0104773SPascal Brand 612b0104773SPascal Brand if ((src_op->info.handleState & 613b0104773SPascal Brand TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) { 614b0104773SPascal Brand TEE_SetOperationKey(dst_op, key1); 615b0104773SPascal Brand } else { 616b0104773SPascal Brand TEE_SetOperationKey2(dst_op, key1, key2); 617b0104773SPascal Brand } 618b0104773SPascal Brand } 619b0104773SPascal Brand dst_op->info.handleState = src_op->info.handleState; 620b0104773SPascal Brand dst_op->info.keySize = src_op->info.keySize; 621b0104773SPascal Brand 622b0104773SPascal Brand if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks || 623b0104773SPascal Brand dst_op->block_size != src_op->block_size) 624b0104773SPascal Brand TEE_Panic(0); 625b0104773SPascal Brand 626b0104773SPascal Brand if (dst_op->buffer != NULL) { 627b0104773SPascal Brand if (src_op->buffer == NULL) 628b0104773SPascal Brand TEE_Panic(0); 629b0104773SPascal Brand 630b0104773SPascal Brand memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs); 631b0104773SPascal Brand dst_op->buffer_offs = src_op->buffer_offs; 632b0104773SPascal Brand } else if (src_op->buffer != NULL) { 633b0104773SPascal Brand TEE_Panic(0); 634b0104773SPascal Brand } 635b0104773SPascal Brand 636b0104773SPascal Brand res = utee_cryp_state_copy(dst_op->state, src_op->state); 637b0104773SPascal Brand if (res != TEE_SUCCESS) 638b0104773SPascal Brand TEE_Panic(res); 639b0104773SPascal Brand } 640b0104773SPascal Brand 641b0104773SPascal Brand /* Cryptographic Operations API - Message Digest Functions */ 642b0104773SPascal Brand 643b0104773SPascal Brand void TEE_DigestUpdate(TEE_OperationHandle operation, 64479a3c601SCedric Chaumont void *chunk, uint32_t chunkSize) 645b0104773SPascal Brand { 64673d6c3baSJoakim Bech TEE_Result res = TEE_ERROR_GENERIC; 647b0104773SPascal Brand 64873d6c3baSJoakim Bech if (operation == TEE_HANDLE_NULL || 64973d6c3baSJoakim Bech operation->info.operationClass != TEE_OPERATION_DIGEST) 650b0104773SPascal Brand TEE_Panic(0); 65173d6c3baSJoakim Bech 652b0104773SPascal Brand res = utee_hash_update(operation->state, chunk, chunkSize); 653b0104773SPascal Brand if (res != TEE_SUCCESS) 654b0104773SPascal Brand TEE_Panic(res); 655b0104773SPascal Brand } 656b0104773SPascal Brand 657b0104773SPascal Brand TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void *chunk, 65879a3c601SCedric Chaumont uint32_t chunkLen, void *hash, uint32_t *hashLen) 659b0104773SPascal Brand { 66073d6c3baSJoakim Bech if ((operation == TEE_HANDLE_NULL) || (!chunk && chunkLen) || 66173d6c3baSJoakim Bech !hash || !hashLen || 66273d6c3baSJoakim Bech (operation->info.operationClass != TEE_OPERATION_DIGEST)) 663b0104773SPascal Brand TEE_Panic(0); 66473d6c3baSJoakim Bech 665b0104773SPascal Brand return utee_hash_final(operation->state, chunk, chunkLen, hash, 666b0104773SPascal Brand hashLen); 667b0104773SPascal Brand } 668b0104773SPascal Brand 669b0104773SPascal Brand /* Cryptographic Operations API - Symmetric Cipher Functions */ 670b0104773SPascal Brand 67179a3c601SCedric Chaumont void TEE_CipherInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen) 672b0104773SPascal Brand { 673b0104773SPascal Brand TEE_Result res; 674b0104773SPascal Brand 675b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 676b0104773SPascal Brand TEE_Panic(0); 677b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_CIPHER) 678b0104773SPascal Brand TEE_Panic(0); 679b0104773SPascal Brand res = utee_cipher_init(operation->state, IV, IVLen); 680b0104773SPascal Brand if (res != TEE_SUCCESS) 681b0104773SPascal Brand TEE_Panic(res); 682b0104773SPascal Brand operation->buffer_offs = 0; 683b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 684b0104773SPascal Brand } 685b0104773SPascal Brand 686b0104773SPascal Brand static TEE_Result tee_buffer_update( 687b0104773SPascal Brand TEE_OperationHandle op, 688b0104773SPascal Brand TEE_Result(*update_func) (uint32_t state, const void *src, 6897f74c64aSPascal Brand size_t slen, void *dst, uint32_t *dlen), 690b0104773SPascal Brand const void *src_data, size_t src_len, 6917f74c64aSPascal Brand void *dest_data, uint32_t *dest_len) 692b0104773SPascal Brand { 693b0104773SPascal Brand TEE_Result res; 694b0104773SPascal Brand const uint8_t *src = src_data; 695b0104773SPascal Brand size_t slen = src_len; 696b0104773SPascal Brand uint8_t *dst = dest_data; 697b0104773SPascal Brand size_t dlen = *dest_len; 698b0104773SPascal Brand size_t acc_dlen = 0; 6997f74c64aSPascal Brand uint32_t tmp_dlen; 700b0104773SPascal Brand size_t l; 701b0104773SPascal Brand size_t buffer_size; 702d3588802SPascal Brand size_t buffer_left; 703b0104773SPascal Brand 704d3588802SPascal Brand if (op->buffer_two_blocks) { 705b0104773SPascal Brand buffer_size = op->block_size * 2; 706d3588802SPascal Brand buffer_left = 1; 707d3588802SPascal Brand } else { 708b0104773SPascal Brand buffer_size = op->block_size; 709d3588802SPascal Brand buffer_left = 0; 710d3588802SPascal Brand } 711b0104773SPascal Brand 712b0104773SPascal Brand if (op->buffer_offs > 0) { 713b0104773SPascal Brand /* Fill up complete block */ 714b0104773SPascal Brand if (op->buffer_offs < op->block_size) 715b0104773SPascal Brand l = MIN(slen, op->block_size - op->buffer_offs); 716b0104773SPascal Brand else 717b0104773SPascal Brand l = MIN(slen, buffer_size - op->buffer_offs); 718b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, l); 719b0104773SPascal Brand op->buffer_offs += l; 720b0104773SPascal Brand src += l; 721b0104773SPascal Brand slen -= l; 722b0104773SPascal Brand if ((op->buffer_offs % op->block_size) != 0) 723b0104773SPascal Brand goto out; /* Nothing left to do */ 724b0104773SPascal Brand } 725b0104773SPascal Brand 726b0104773SPascal Brand /* If we can feed from buffer */ 727d3588802SPascal Brand if ((op->buffer_offs > 0) && 728d3588802SPascal Brand ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) { 7292ff3fdbbSPascal Brand l = ROUNDUP(op->buffer_offs + slen - buffer_size, 730b0104773SPascal Brand op->block_size); 731b0104773SPascal Brand l = MIN(op->buffer_offs, l); 732b0104773SPascal Brand tmp_dlen = dlen; 733b0104773SPascal Brand res = update_func(op->state, op->buffer, l, dst, &tmp_dlen); 734b0104773SPascal Brand if (res != TEE_SUCCESS) 735b0104773SPascal Brand TEE_Panic(res); 736b0104773SPascal Brand dst += tmp_dlen; 737b0104773SPascal Brand dlen -= tmp_dlen; 738b0104773SPascal Brand acc_dlen += tmp_dlen; 739b0104773SPascal Brand op->buffer_offs -= l; 740b0104773SPascal Brand if (op->buffer_offs > 0) { 741b0104773SPascal Brand /* 742b0104773SPascal Brand * Slen is small enough to be contained in rest buffer. 743b0104773SPascal Brand */ 744b0104773SPascal Brand memcpy(op->buffer, op->buffer + l, buffer_size - l); 745b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 746b0104773SPascal Brand op->buffer_offs += slen; 747b0104773SPascal Brand goto out; /* Nothing left to do */ 748b0104773SPascal Brand } 749b0104773SPascal Brand } 750b0104773SPascal Brand 751d3588802SPascal Brand if (slen >= (buffer_size + buffer_left)) { 752b0104773SPascal Brand /* Buffer is empty, feed as much as possible from src */ 753b0104773SPascal Brand if (TEE_ALIGNMENT_IS_OK(src, uint32_t)) { 7542ff3fdbbSPascal Brand l = ROUNDUP(slen - buffer_size + 1, op->block_size); 755b0104773SPascal Brand 756b0104773SPascal Brand tmp_dlen = dlen; 757b0104773SPascal Brand res = update_func(op->state, src, l, dst, &tmp_dlen); 758b0104773SPascal Brand if (res != TEE_SUCCESS) 759b0104773SPascal Brand TEE_Panic(res); 760b0104773SPascal Brand src += l; 761b0104773SPascal Brand slen -= l; 762b0104773SPascal Brand dst += tmp_dlen; 763b0104773SPascal Brand dlen -= tmp_dlen; 764b0104773SPascal Brand acc_dlen += tmp_dlen; 765b0104773SPascal Brand } else { 766b0104773SPascal Brand /* 767b0104773SPascal Brand * Supplied data isn't well aligned, we're forced to 768b0104773SPascal Brand * feed through the buffer. 769b0104773SPascal Brand */ 770b0104773SPascal Brand while (slen >= op->block_size) { 771b0104773SPascal Brand memcpy(op->buffer, src, op->block_size); 772b0104773SPascal Brand 773b0104773SPascal Brand tmp_dlen = dlen; 774b0104773SPascal Brand res = 775b0104773SPascal Brand update_func(op->state, op->buffer, 776b0104773SPascal Brand op->block_size, dst, &tmp_dlen); 777b0104773SPascal Brand if (res != TEE_SUCCESS) 778b0104773SPascal Brand TEE_Panic(res); 779b0104773SPascal Brand src += op->block_size; 780b0104773SPascal Brand slen -= op->block_size; 781b0104773SPascal Brand dst += tmp_dlen; 782b0104773SPascal Brand dlen -= tmp_dlen; 783b0104773SPascal Brand acc_dlen += tmp_dlen; 784b0104773SPascal Brand } 785b0104773SPascal Brand } 786b0104773SPascal Brand } 787b0104773SPascal Brand 788b0104773SPascal Brand /* Slen is small enough to be contained in buffer. */ 789b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 790b0104773SPascal Brand op->buffer_offs += slen; 791b0104773SPascal Brand 792b0104773SPascal Brand out: 793b0104773SPascal Brand *dest_len = acc_dlen; 794b0104773SPascal Brand return TEE_SUCCESS; 795b0104773SPascal Brand } 796b0104773SPascal Brand 797b0104773SPascal Brand TEE_Result TEE_CipherUpdate(TEE_OperationHandle op, const void *srcData, 79879a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 799b0104773SPascal Brand { 800b0104773SPascal Brand size_t req_dlen; 801b0104773SPascal Brand 802b0104773SPascal Brand if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 803b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 804b0104773SPascal Brand TEE_Panic(0); 805b0104773SPascal Brand if (op->info.operationClass != TEE_OPERATION_CIPHER) 806b0104773SPascal Brand TEE_Panic(0); 807b0104773SPascal Brand if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 808b0104773SPascal Brand TEE_Panic(0); 809b0104773SPascal Brand 810b0104773SPascal Brand /* Calculate required dlen */ 811b0104773SPascal Brand req_dlen = ((op->buffer_offs + srcLen) / op->block_size) * 812b0104773SPascal Brand op->block_size; 813b0104773SPascal Brand if (op->buffer_two_blocks) { 814b0104773SPascal Brand if (req_dlen > op->block_size * 2) 815b0104773SPascal Brand req_dlen -= op->block_size * 2; 816b0104773SPascal Brand else 817b0104773SPascal Brand req_dlen = 0; 818b0104773SPascal Brand } 819b0104773SPascal Brand /* 820b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 821b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 822b0104773SPascal Brand * can't restore sync with this API. 823b0104773SPascal Brand */ 824b0104773SPascal Brand if (*destLen < req_dlen) { 825b0104773SPascal Brand *destLen = req_dlen; 826b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 827b0104773SPascal Brand } 828b0104773SPascal Brand 829b0104773SPascal Brand tee_buffer_update(op, utee_cipher_update, srcData, srcLen, destData, 830b0104773SPascal Brand destLen); 831b0104773SPascal Brand 832b0104773SPascal Brand return TEE_SUCCESS; 833b0104773SPascal Brand } 834b0104773SPascal Brand 835b0104773SPascal Brand TEE_Result TEE_CipherDoFinal(TEE_OperationHandle op, 83679a3c601SCedric Chaumont const void *srcData, uint32_t srcLen, void *destData, 83779a3c601SCedric Chaumont uint32_t *destLen) 838b0104773SPascal Brand { 839b0104773SPascal Brand TEE_Result res; 840b0104773SPascal Brand uint8_t *dst = destData; 841b0104773SPascal Brand size_t acc_dlen = 0; 8427f74c64aSPascal Brand uint32_t tmp_dlen; 843b0104773SPascal Brand size_t req_dlen; 844b0104773SPascal Brand 845b0104773SPascal Brand if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 846b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 847b0104773SPascal Brand TEE_Panic(0); 848b0104773SPascal Brand if (op->info.operationClass != TEE_OPERATION_CIPHER) 849b0104773SPascal Brand TEE_Panic(0); 850b0104773SPascal Brand if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 851b0104773SPascal Brand TEE_Panic(0); 852b0104773SPascal Brand 853b0104773SPascal Brand /* 854b0104773SPascal Brand * Check that the final block doesn't require padding for those 855b0104773SPascal Brand * algorithms that requires client to supply padding. 856b0104773SPascal Brand */ 857b0104773SPascal Brand if (op->info.algorithm == TEE_ALG_AES_ECB_NOPAD || 858b0104773SPascal Brand op->info.algorithm == TEE_ALG_AES_CBC_NOPAD || 859b0104773SPascal Brand op->info.algorithm == TEE_ALG_DES_ECB_NOPAD || 860b0104773SPascal Brand op->info.algorithm == TEE_ALG_DES_CBC_NOPAD || 861b0104773SPascal Brand op->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || 862b0104773SPascal Brand op->info.algorithm == TEE_ALG_DES3_CBC_NOPAD) { 863b0104773SPascal Brand if (((op->buffer_offs + srcLen) % op->block_size) != 0) 864b0104773SPascal Brand return TEE_ERROR_BAD_PARAMETERS; 865b0104773SPascal Brand } 866b0104773SPascal Brand 867b0104773SPascal Brand /* 868b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 869b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 870b0104773SPascal Brand * can't restore sync with this API. 871b0104773SPascal Brand */ 872b0104773SPascal Brand req_dlen = op->buffer_offs + srcLen; 873b0104773SPascal Brand if (*destLen < req_dlen) { 874b0104773SPascal Brand *destLen = req_dlen; 875b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 876b0104773SPascal Brand } 877b0104773SPascal Brand 878b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 879b0104773SPascal Brand tee_buffer_update(op, utee_cipher_update, srcData, srcLen, dst, 880b0104773SPascal Brand &tmp_dlen); 881b0104773SPascal Brand dst += tmp_dlen; 882b0104773SPascal Brand acc_dlen += tmp_dlen; 883b0104773SPascal Brand 884b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 885b0104773SPascal Brand res = utee_cipher_final(op->state, op->buffer, op->buffer_offs, 886b0104773SPascal Brand dst, &tmp_dlen); 887b0104773SPascal Brand if (res != TEE_SUCCESS) 888b0104773SPascal Brand TEE_Panic(res); 889b0104773SPascal Brand acc_dlen += tmp_dlen; 890b0104773SPascal Brand 891b0104773SPascal Brand op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 892b0104773SPascal Brand *destLen = acc_dlen; 893b0104773SPascal Brand return TEE_SUCCESS; 894b0104773SPascal Brand } 895b0104773SPascal Brand 896b0104773SPascal Brand /* Cryptographic Operations API - MAC Functions */ 897b0104773SPascal Brand 89879a3c601SCedric Chaumont void TEE_MACInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen) 899b0104773SPascal Brand { 900b0104773SPascal Brand TEE_Result res; 901b0104773SPascal Brand 902b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 903b0104773SPascal Brand TEE_Panic(0); 904b0104773SPascal Brand if (IV == NULL && IVLen != 0) 905b0104773SPascal Brand TEE_Panic(0); 906b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_MAC) 907b0104773SPascal Brand TEE_Panic(0); 908b0104773SPascal Brand res = utee_hash_init(operation->state, IV, IVLen); 909b0104773SPascal Brand if (res != TEE_SUCCESS) 910b0104773SPascal Brand TEE_Panic(res); 911b0104773SPascal Brand operation->buffer_offs = 0; 912b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 913b0104773SPascal Brand } 914b0104773SPascal Brand 91579a3c601SCedric Chaumont void TEE_MACUpdate(TEE_OperationHandle op, const void *chunk, uint32_t chunkSize) 916b0104773SPascal Brand { 917b0104773SPascal Brand TEE_Result res; 918b0104773SPascal Brand 919b0104773SPascal Brand if (op == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0)) 920b0104773SPascal Brand TEE_Panic(0); 921b0104773SPascal Brand if (op->info.operationClass != TEE_OPERATION_MAC) 922b0104773SPascal Brand TEE_Panic(0); 923b0104773SPascal Brand if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 924b0104773SPascal Brand TEE_Panic(0); 925b0104773SPascal Brand 926b0104773SPascal Brand res = utee_hash_update(op->state, chunk, chunkSize); 927b0104773SPascal Brand if (res != TEE_SUCCESS) 928b0104773SPascal Brand TEE_Panic(res); 929b0104773SPascal Brand } 930b0104773SPascal Brand 931b0104773SPascal Brand TEE_Result TEE_MACComputeFinal(TEE_OperationHandle op, 93279a3c601SCedric Chaumont const void *message, uint32_t messageLen, 93379a3c601SCedric Chaumont void *mac, uint32_t *macLen) 934b0104773SPascal Brand { 935b0104773SPascal Brand TEE_Result res; 936b0104773SPascal Brand 937b0104773SPascal Brand if (op == TEE_HANDLE_NULL || (message == NULL && messageLen != 0) || 938b0104773SPascal Brand mac == NULL || macLen == NULL) 939b0104773SPascal Brand TEE_Panic(0); 940b0104773SPascal Brand if (op->info.operationClass != TEE_OPERATION_MAC) 941b0104773SPascal Brand TEE_Panic(0); 942b0104773SPascal Brand if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 943b0104773SPascal Brand TEE_Panic(0); 944b0104773SPascal Brand 945b0104773SPascal Brand res = utee_hash_final(op->state, message, messageLen, mac, macLen); 946b0104773SPascal Brand op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 947b0104773SPascal Brand return res; 948b0104773SPascal Brand } 949b0104773SPascal Brand 950b0104773SPascal Brand TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation, 95179a3c601SCedric Chaumont const void *message, uint32_t messageLen, 95279a3c601SCedric Chaumont const void *mac, uint32_t macLen) 953b0104773SPascal Brand { 954b0104773SPascal Brand TEE_Result res; 955b0104773SPascal Brand uint8_t computed_mac[TEE_MAX_HASH_SIZE]; 9567f74c64aSPascal Brand uint32_t computed_mac_size = TEE_MAX_HASH_SIZE; 957b0104773SPascal Brand 958b0104773SPascal Brand res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac, 959b0104773SPascal Brand &computed_mac_size); 960b0104773SPascal Brand if (res != TEE_SUCCESS) 961b0104773SPascal Brand return res; 962b0104773SPascal Brand if (computed_mac_size != macLen) 963b0104773SPascal Brand return TEE_ERROR_MAC_INVALID; 964b796ebf3SJerome Forissier if (buf_compare_ct(mac, computed_mac, computed_mac_size) != 0) 965b0104773SPascal Brand return TEE_ERROR_MAC_INVALID; 966b0104773SPascal Brand return TEE_SUCCESS; 967b0104773SPascal Brand } 968b0104773SPascal Brand 969b0104773SPascal Brand /* Cryptographic Operations API - Authenticated Encryption Functions */ 970b0104773SPascal Brand 971b0104773SPascal Brand TEE_Result TEE_AEInit(TEE_OperationHandle op, const void *nonce, 97279a3c601SCedric Chaumont uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen, 973b0104773SPascal Brand uint32_t payloadLen) 974b0104773SPascal Brand { 975b0104773SPascal Brand TEE_Result res; 976b0104773SPascal Brand 977b0104773SPascal Brand if (op == TEE_HANDLE_NULL || nonce == NULL) 978b0104773SPascal Brand TEE_Panic(0); 979b0104773SPascal Brand if (op->info.operationClass != TEE_OPERATION_AE) 980b0104773SPascal Brand TEE_Panic(0); 981b0104773SPascal Brand 982b0104773SPascal Brand /* 983b0104773SPascal Brand * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core 984b0104773SPascal Brand * in the implementation. But AES-GCM spec doesn't specify the tag len 985b0104773SPascal Brand * according to the same principle so we have to check here instead to 986b0104773SPascal Brand * be GP compliant. 987b0104773SPascal Brand */ 988b0104773SPascal Brand if (op->info.algorithm == TEE_ALG_AES_GCM) { 989b0104773SPascal Brand /* 990b0104773SPascal Brand * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96 991b0104773SPascal Brand */ 992b0104773SPascal Brand if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) 993b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 994b0104773SPascal Brand } 995b0104773SPascal Brand 996b0104773SPascal Brand res = utee_authenc_init(op->state, nonce, nonceLen, tagLen / 8, AADLen, 997b0104773SPascal Brand payloadLen); 998b0104773SPascal Brand if (res != TEE_SUCCESS) { 999b0104773SPascal Brand if (res != TEE_ERROR_NOT_SUPPORTED) 1000b0104773SPascal Brand TEE_Panic(res); 1001b0104773SPascal Brand return res; 1002b0104773SPascal Brand } 1003b0104773SPascal Brand op->ae_tag_len = tagLen / 8; 1004b0104773SPascal Brand 1005b0104773SPascal Brand op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 1006b0104773SPascal Brand return TEE_SUCCESS; 1007b0104773SPascal Brand } 1008b0104773SPascal Brand 1009b0104773SPascal Brand void TEE_AEUpdateAAD(TEE_OperationHandle op, const void *AADdata, 101079a3c601SCedric Chaumont uint32_t AADdataLen) 1011b0104773SPascal Brand { 1012b0104773SPascal Brand TEE_Result res; 1013b0104773SPascal Brand 1014b0104773SPascal Brand if (op == TEE_HANDLE_NULL || (AADdata == NULL && AADdataLen != 0)) 1015b0104773SPascal Brand TEE_Panic(0); 1016b0104773SPascal Brand if (op->info.operationClass != TEE_OPERATION_AE) 1017b0104773SPascal Brand TEE_Panic(0); 1018b0104773SPascal Brand if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1019b0104773SPascal Brand TEE_Panic(0); 1020b0104773SPascal Brand 1021b0104773SPascal Brand res = utee_authenc_update_aad(op->state, AADdata, AADdataLen); 1022b0104773SPascal Brand if (res != TEE_SUCCESS) 1023b0104773SPascal Brand TEE_Panic(res); 1024b0104773SPascal Brand } 1025b0104773SPascal Brand 1026b0104773SPascal Brand TEE_Result TEE_AEUpdate(TEE_OperationHandle op, const void *srcData, 102779a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 1028b0104773SPascal Brand { 1029b0104773SPascal Brand size_t req_dlen; 1030b0104773SPascal Brand 1031b0104773SPascal Brand if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1032b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 1033b0104773SPascal Brand TEE_Panic(0); 1034b0104773SPascal Brand if (op->info.operationClass != TEE_OPERATION_AE) 1035b0104773SPascal Brand TEE_Panic(0); 1036b0104773SPascal Brand if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1037b0104773SPascal Brand TEE_Panic(0); 1038b0104773SPascal Brand 1039b0104773SPascal Brand /* 1040b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1041b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1042b0104773SPascal Brand * can't restore sync with this API. 1043b0104773SPascal Brand */ 10442ff3fdbbSPascal Brand req_dlen = ROUNDDOWN(op->buffer_offs + srcLen, op->block_size); 1045b0104773SPascal Brand if (*destLen < req_dlen) { 1046b0104773SPascal Brand *destLen = req_dlen; 1047b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 1048b0104773SPascal Brand } 1049b0104773SPascal Brand 1050b0104773SPascal Brand tee_buffer_update(op, utee_authenc_update_payload, srcData, srcLen, 1051b0104773SPascal Brand destData, destLen); 1052b0104773SPascal Brand 1053b0104773SPascal Brand return TEE_SUCCESS; 1054b0104773SPascal Brand } 1055b0104773SPascal Brand 1056b0104773SPascal Brand TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle op, 105779a3c601SCedric Chaumont const void *srcData, uint32_t srcLen, 105879a3c601SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 105979a3c601SCedric Chaumont uint32_t *tagLen) 1060b0104773SPascal Brand { 1061b0104773SPascal Brand TEE_Result res; 1062b0104773SPascal Brand uint8_t *dst = destData; 1063b0104773SPascal Brand size_t acc_dlen = 0; 10647f74c64aSPascal Brand uint32_t tmp_dlen; 1065b0104773SPascal Brand size_t req_dlen; 1066b0104773SPascal Brand 1067b0104773SPascal Brand if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1068b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0) || 1069b0104773SPascal Brand tag == NULL || tagLen == NULL) 1070b0104773SPascal Brand TEE_Panic(0); 1071b0104773SPascal Brand if (op->info.operationClass != TEE_OPERATION_AE) 1072b0104773SPascal Brand TEE_Panic(0); 1073b0104773SPascal Brand if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1074b0104773SPascal Brand TEE_Panic(0); 1075b0104773SPascal Brand 1076b0104773SPascal Brand /* 1077b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1078b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1079b0104773SPascal Brand * can't restore sync with this API. 1080b0104773SPascal Brand */ 1081b0104773SPascal Brand req_dlen = op->buffer_offs + srcLen; 1082b0104773SPascal Brand if (*destLen < req_dlen) { 1083b0104773SPascal Brand *destLen = req_dlen; 1084b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 1085b0104773SPascal Brand } 1086b0104773SPascal Brand 1087b0104773SPascal Brand /* 1088b0104773SPascal Brand * Need to check this before update_payload since sync would be lost if 1089b0104773SPascal Brand * we return short buffer after that. 1090b0104773SPascal Brand */ 1091b0104773SPascal Brand if (*tagLen < op->ae_tag_len) { 1092b0104773SPascal Brand *tagLen = op->ae_tag_len; 1093b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 1094b0104773SPascal Brand } 1095b0104773SPascal Brand 1096b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1097b0104773SPascal Brand tee_buffer_update(op, utee_authenc_update_payload, srcData, srcLen, 1098b0104773SPascal Brand dst, &tmp_dlen); 1099b0104773SPascal Brand dst += tmp_dlen; 1100b0104773SPascal Brand acc_dlen += tmp_dlen; 1101b0104773SPascal Brand 1102b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1103b0104773SPascal Brand res = 1104b0104773SPascal Brand utee_authenc_enc_final(op->state, op->buffer, op->buffer_offs, dst, 1105b0104773SPascal Brand &tmp_dlen, tag, tagLen); 1106b0104773SPascal Brand if (res != TEE_SUCCESS) 1107b0104773SPascal Brand TEE_Panic(res); 1108b0104773SPascal Brand acc_dlen += tmp_dlen; 1109b0104773SPascal Brand 1110b0104773SPascal Brand *destLen = acc_dlen; 1111b0104773SPascal Brand op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1112b0104773SPascal Brand 1113b0104773SPascal Brand return res; 1114b0104773SPascal Brand } 1115b0104773SPascal Brand 1116b0104773SPascal Brand TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle op, 111779a3c601SCedric Chaumont const void *srcData, uint32_t srcLen, 111879a3c601SCedric Chaumont void *destData, uint32_t *destLen, const void *tag, 111979a3c601SCedric Chaumont uint32_t tagLen) 1120b0104773SPascal Brand { 1121b0104773SPascal Brand TEE_Result res; 1122b0104773SPascal Brand uint8_t *dst = destData; 1123b0104773SPascal Brand size_t acc_dlen = 0; 11247f74c64aSPascal Brand uint32_t tmp_dlen; 1125b0104773SPascal Brand size_t req_dlen; 1126b0104773SPascal Brand 1127b0104773SPascal Brand if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1128b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0) || 1129b0104773SPascal Brand (tag == NULL && tagLen != 0)) 1130b0104773SPascal Brand TEE_Panic(0); 1131b0104773SPascal Brand if (op->info.operationClass != TEE_OPERATION_AE) 1132b0104773SPascal Brand TEE_Panic(0); 1133b0104773SPascal Brand if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1134b0104773SPascal Brand TEE_Panic(0); 1135b0104773SPascal Brand 1136b0104773SPascal Brand /* 1137b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1138b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1139b0104773SPascal Brand * can't restore sync with this API. 1140b0104773SPascal Brand */ 1141b0104773SPascal Brand req_dlen = op->buffer_offs + srcLen; 1142b0104773SPascal Brand if (*destLen < req_dlen) { 1143b0104773SPascal Brand *destLen = req_dlen; 1144b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 1145b0104773SPascal Brand } 1146b0104773SPascal Brand 1147b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1148b0104773SPascal Brand tee_buffer_update(op, utee_authenc_update_payload, srcData, srcLen, 1149b0104773SPascal Brand dst, &tmp_dlen); 1150b0104773SPascal Brand dst += tmp_dlen; 1151b0104773SPascal Brand acc_dlen += tmp_dlen; 1152b0104773SPascal Brand 1153b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1154b0104773SPascal Brand res = 1155b0104773SPascal Brand utee_authenc_dec_final(op->state, op->buffer, op->buffer_offs, dst, 1156b0104773SPascal Brand &tmp_dlen, tag, tagLen); 1157b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_MAC_INVALID) 1158b0104773SPascal Brand TEE_Panic(res); 1159b0104773SPascal Brand /* Supplied tagLen should match what we initiated with */ 1160b0104773SPascal Brand if (tagLen != op->ae_tag_len) 1161b0104773SPascal Brand res = TEE_ERROR_MAC_INVALID; 1162b0104773SPascal Brand 1163b0104773SPascal Brand acc_dlen += tmp_dlen; 1164b0104773SPascal Brand 1165b0104773SPascal Brand *destLen = acc_dlen; 1166b0104773SPascal Brand op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1167b0104773SPascal Brand 1168b0104773SPascal Brand return res; 1169b0104773SPascal Brand } 1170b0104773SPascal Brand 1171b0104773SPascal Brand /* Cryptographic Operations API - Asymmetric Functions */ 1172b0104773SPascal Brand 1173b0104773SPascal Brand TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle op, 1174b0104773SPascal Brand const TEE_Attribute *params, 1175b0104773SPascal Brand uint32_t paramCount, const void *srcData, 117679a3c601SCedric Chaumont uint32_t srcLen, void *destData, 117779a3c601SCedric Chaumont uint32_t *destLen) 1178b0104773SPascal Brand { 1179b0104773SPascal Brand TEE_Result res; 1180b0104773SPascal Brand 1181b0104773SPascal Brand if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1182b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 1183b0104773SPascal Brand TEE_Panic(0); 1184b0104773SPascal Brand if (paramCount != 0 && params == NULL) 1185b0104773SPascal Brand TEE_Panic(0); 1186b0104773SPascal Brand if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 1187b0104773SPascal Brand TEE_Panic(0); 1188b0104773SPascal Brand if (op->info.mode != TEE_MODE_ENCRYPT) 1189b0104773SPascal Brand TEE_Panic(0); 1190b0104773SPascal Brand 1191b0104773SPascal Brand res = utee_asymm_operate(op->state, params, paramCount, srcData, srcLen, 1192b0104773SPascal Brand destData, destLen); 11938844ebfcSPascal Brand if (res != TEE_SUCCESS && 11948844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 11958844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1196b0104773SPascal Brand TEE_Panic(res); 1197b0104773SPascal Brand return res; 1198b0104773SPascal Brand } 1199b0104773SPascal Brand 1200b0104773SPascal Brand TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle op, 1201b0104773SPascal Brand const TEE_Attribute *params, 1202b0104773SPascal Brand uint32_t paramCount, const void *srcData, 120379a3c601SCedric Chaumont uint32_t srcLen, void *destData, 120479a3c601SCedric Chaumont uint32_t *destLen) 1205b0104773SPascal Brand { 1206b0104773SPascal Brand TEE_Result res; 1207b0104773SPascal Brand 1208b0104773SPascal Brand if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1209b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 1210b0104773SPascal Brand TEE_Panic(0); 1211b0104773SPascal Brand if (paramCount != 0 && params == NULL) 1212b0104773SPascal Brand TEE_Panic(0); 1213b0104773SPascal Brand if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 1214b0104773SPascal Brand TEE_Panic(0); 1215b0104773SPascal Brand if (op->info.mode != TEE_MODE_DECRYPT) 1216b0104773SPascal Brand TEE_Panic(0); 1217b0104773SPascal Brand 1218b0104773SPascal Brand res = utee_asymm_operate(op->state, params, paramCount, srcData, srcLen, 1219b0104773SPascal Brand destData, destLen); 12208844ebfcSPascal Brand if (res != TEE_SUCCESS && 12218844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 12228844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1223b0104773SPascal Brand TEE_Panic(res); 1224b0104773SPascal Brand return res; 1225b0104773SPascal Brand } 1226b0104773SPascal Brand 1227b0104773SPascal Brand TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle op, 1228b0104773SPascal Brand const TEE_Attribute *params, 1229b0104773SPascal Brand uint32_t paramCount, const void *digest, 123079a3c601SCedric Chaumont uint32_t digestLen, void *signature, 123179a3c601SCedric Chaumont uint32_t *signatureLen) 1232b0104773SPascal Brand { 1233b0104773SPascal Brand TEE_Result res; 1234b0104773SPascal Brand 1235b0104773SPascal Brand if (op == TEE_HANDLE_NULL || (digest == NULL && digestLen != 0) || 1236b0104773SPascal Brand signature == NULL || signatureLen == NULL) 1237b0104773SPascal Brand TEE_Panic(0); 1238b0104773SPascal Brand if (paramCount != 0 && params == NULL) 1239b0104773SPascal Brand TEE_Panic(0); 1240b0104773SPascal Brand if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_SIGNATURE) 1241b0104773SPascal Brand TEE_Panic(0); 1242b0104773SPascal Brand if (op->info.mode != TEE_MODE_SIGN) 1243b0104773SPascal Brand TEE_Panic(0); 1244b0104773SPascal Brand 1245b0104773SPascal Brand res = 1246b0104773SPascal Brand utee_asymm_operate(op->state, params, paramCount, digest, digestLen, 1247b0104773SPascal Brand signature, signatureLen); 1248b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER) 1249b0104773SPascal Brand TEE_Panic(res); 1250b0104773SPascal Brand return res; 1251b0104773SPascal Brand } 1252b0104773SPascal Brand 1253b0104773SPascal Brand TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle op, 1254b0104773SPascal Brand const TEE_Attribute *params, 1255b0104773SPascal Brand uint32_t paramCount, const void *digest, 125679a3c601SCedric Chaumont uint32_t digestLen, const void *signature, 125779a3c601SCedric Chaumont uint32_t signatureLen) 1258b0104773SPascal Brand { 1259b0104773SPascal Brand TEE_Result res; 1260b0104773SPascal Brand 1261b0104773SPascal Brand if (op == TEE_HANDLE_NULL || (digest == NULL && digestLen != 0) || 1262b0104773SPascal Brand (signature == NULL && signatureLen != 0)) 1263b0104773SPascal Brand TEE_Panic(0); 1264b0104773SPascal Brand if (paramCount != 0 && params == NULL) 1265b0104773SPascal Brand TEE_Panic(0); 1266b0104773SPascal Brand if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_SIGNATURE) 1267b0104773SPascal Brand TEE_Panic(0); 1268b0104773SPascal Brand if (op->info.mode != TEE_MODE_VERIFY) 1269b0104773SPascal Brand TEE_Panic(0); 1270b0104773SPascal Brand 1271b0104773SPascal Brand res = 1272b0104773SPascal Brand utee_asymm_verify(op->state, params, paramCount, digest, digestLen, 1273b0104773SPascal Brand signature, signatureLen); 1274b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID) 1275b0104773SPascal Brand TEE_Panic(res); 1276b0104773SPascal Brand return res; 1277b0104773SPascal Brand } 1278b0104773SPascal Brand 1279b0104773SPascal Brand /* Cryptographic Operations API - Key Derivation Functions */ 1280b0104773SPascal Brand 1281b0104773SPascal Brand void TEE_DeriveKey(TEE_OperationHandle operation, 1282b0104773SPascal Brand const TEE_Attribute *params, uint32_t paramCount, 1283b0104773SPascal Brand TEE_ObjectHandle derivedKey) 1284b0104773SPascal Brand { 1285b0104773SPascal Brand TEE_Result res; 1286b0104773SPascal Brand TEE_ObjectInfo key_info; 1287b0104773SPascal Brand 1288b0104773SPascal Brand if (operation == TEE_HANDLE_NULL || derivedKey == 0) 1289b0104773SPascal Brand TEE_Panic(0); 1290b0104773SPascal Brand if (paramCount != 0 && params == NULL) 1291b0104773SPascal Brand TEE_Panic(0); 12928854d3c6SJerome Forissier if (TEE_ALG_GET_CLASS(operation->info.algorithm) != 12938854d3c6SJerome Forissier TEE_OPERATION_KEY_DERIVATION) 1294b0104773SPascal Brand TEE_Panic(0); 1295b0104773SPascal Brand 1296b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION) 1297b0104773SPascal Brand TEE_Panic(0); 1298b0104773SPascal Brand if (operation->info.mode != TEE_MODE_DERIVE) 1299b0104773SPascal Brand TEE_Panic(0); 1300b0104773SPascal Brand if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0) 1301b0104773SPascal Brand TEE_Panic(0); 1302b0104773SPascal Brand 1303b0104773SPascal Brand res = utee_cryp_obj_get_info((uint32_t) derivedKey, &key_info); 1304b0104773SPascal Brand if (res != TEE_SUCCESS) 1305b0104773SPascal Brand TEE_Panic(0); 1306b0104773SPascal Brand 1307b0104773SPascal Brand if (key_info.objectType != TEE_TYPE_GENERIC_SECRET) 1308b0104773SPascal Brand TEE_Panic(0); 1309b0104773SPascal Brand if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1310b0104773SPascal Brand TEE_Panic(0); 1311b0104773SPascal Brand 1312b0104773SPascal Brand res = utee_cryp_derive_key(operation->state, params, paramCount, 1313b0104773SPascal Brand (uint32_t) derivedKey); 1314b0104773SPascal Brand if (res != TEE_SUCCESS) 1315b0104773SPascal Brand TEE_Panic(res); 1316b0104773SPascal Brand } 1317b0104773SPascal Brand 1318b0104773SPascal Brand /* Cryptographic Operations API - Random Number Generation Functions */ 1319b0104773SPascal Brand 132079a3c601SCedric Chaumont void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen) 1321b0104773SPascal Brand { 1322b0104773SPascal Brand TEE_Result res; 1323b0104773SPascal Brand 1324b0104773SPascal Brand res = utee_cryp_random_number_generate(randomBuffer, randomBufferLen); 1325b0104773SPascal Brand if (res != TEE_SUCCESS) 1326b0104773SPascal Brand TEE_Panic(res); 1327b0104773SPascal Brand } 1328