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 66b0104773SPascal Brand if (operation == NULL) 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 72*218d9055SCedric Chaumont /* Check algorithm max key size */ 73*218d9055SCedric Chaumont switch (algorithm) { 74*218d9055SCedric Chaumont case TEE_ALG_DSA_SHA1: 75*218d9055SCedric Chaumont if (maxKeySize < 512) 76*218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 77*218d9055SCedric Chaumont if (maxKeySize > 1024) 78*218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 79*218d9055SCedric Chaumont if (maxKeySize % 64 != 0) 80*218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 81*218d9055SCedric Chaumont break; 82*218d9055SCedric Chaumont 83*218d9055SCedric Chaumont case TEE_ALG_DSA_SHA224: 84*218d9055SCedric Chaumont if (maxKeySize != 2048) 85*218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 86*218d9055SCedric Chaumont break; 87*218d9055SCedric Chaumont 88*218d9055SCedric Chaumont case TEE_ALG_DSA_SHA256: 89*218d9055SCedric Chaumont if (maxKeySize != 2048 && maxKeySize != 3072) 90*218d9055SCedric Chaumont return TEE_ERROR_NOT_SUPPORTED; 91*218d9055SCedric Chaumont break; 92*218d9055SCedric Chaumont 93*218d9055SCedric Chaumont default: 94*218d9055SCedric Chaumont break; 95*218d9055SCedric Chaumont } 96*218d9055SCedric Chaumont 97*218d9055SCedric Chaumont /* Check algorithm mode */ 98b0104773SPascal Brand switch (algorithm) { 99b0104773SPascal Brand case TEE_ALG_AES_CTS: 100b0104773SPascal Brand case TEE_ALG_AES_XTS: 101b0104773SPascal Brand buffer_two_blocks = true; 102b0104773SPascal Brand /*FALLTHROUGH*/ case TEE_ALG_AES_ECB_NOPAD: 103b0104773SPascal Brand case TEE_ALG_AES_CBC_NOPAD: 104b0104773SPascal Brand case TEE_ALG_AES_CTR: 105b0104773SPascal Brand case TEE_ALG_AES_CCM: 106b0104773SPascal Brand case TEE_ALG_AES_GCM: 107b0104773SPascal Brand case TEE_ALG_DES_ECB_NOPAD: 108b0104773SPascal Brand case TEE_ALG_DES_CBC_NOPAD: 109b0104773SPascal Brand case TEE_ALG_DES3_ECB_NOPAD: 110b0104773SPascal Brand case TEE_ALG_DES3_CBC_NOPAD: 111b0104773SPascal Brand if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES) 112b0104773SPascal Brand block_size = TEE_AES_BLOCK_SIZE; 113b0104773SPascal Brand else 114b0104773SPascal Brand block_size = TEE_DES_BLOCK_SIZE; 115b0104773SPascal Brand 116b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) 117b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT; 118b0104773SPascal Brand else if (mode == TEE_MODE_DECRYPT) 119b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT; 120b0104773SPascal Brand else 121b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 122b0104773SPascal Brand break; 123b0104773SPascal Brand 124b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_MD5: 125b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1: 126b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224: 127b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256: 128b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384: 129b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512: 130b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1: 131b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224: 132b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256: 133b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384: 134b0104773SPascal Brand case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512: 135b0104773SPascal Brand case TEE_ALG_DSA_SHA1: 136*218d9055SCedric Chaumont case TEE_ALG_DSA_SHA224: 137*218d9055SCedric Chaumont case TEE_ALG_DSA_SHA256: 138b0104773SPascal Brand if (mode == TEE_MODE_SIGN) { 139b0104773SPascal Brand with_private_key = true; 140b0104773SPascal Brand req_key_usage = TEE_USAGE_SIGN; 141b0104773SPascal Brand } else if (mode == TEE_MODE_VERIFY) { 142b0104773SPascal Brand req_key_usage = TEE_USAGE_VERIFY; 143b0104773SPascal Brand } else { 144b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 145b0104773SPascal Brand } 146b0104773SPascal Brand break; 147b0104773SPascal Brand 148b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_V1_5: 149b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1: 150b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224: 151b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256: 152b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384: 153b0104773SPascal Brand case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512: 154b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) { 155b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT; 156b0104773SPascal Brand } else if (mode == TEE_MODE_DECRYPT) { 157b0104773SPascal Brand with_private_key = true; 158b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT; 159b0104773SPascal Brand } else { 160b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 161b0104773SPascal Brand } 162b0104773SPascal Brand break; 163b0104773SPascal Brand 164b0104773SPascal Brand case TEE_ALG_RSA_NOPAD: 165b0104773SPascal Brand if (mode == TEE_MODE_ENCRYPT) { 166b0104773SPascal Brand req_key_usage = TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY; 167b0104773SPascal Brand } else if (mode == TEE_MODE_DECRYPT) { 168b0104773SPascal Brand with_private_key = true; 169b0104773SPascal Brand req_key_usage = TEE_USAGE_DECRYPT | TEE_USAGE_SIGN; 170b0104773SPascal Brand } else { 171b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 172b0104773SPascal Brand } 173b0104773SPascal Brand break; 174b0104773SPascal Brand 175b0104773SPascal Brand case TEE_ALG_DH_DERIVE_SHARED_SECRET: 176cdb198a7SJerome Forissier case TEE_ALG_HKDF_MD5_DERIVE_KEY: 177cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA1_DERIVE_KEY: 178cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA224_DERIVE_KEY: 179cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA256_DERIVE_KEY: 180cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA384_DERIVE_KEY: 181cdb198a7SJerome Forissier case TEE_ALG_HKDF_SHA512_DERIVE_KEY: 1828854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA1_DERIVE_KEY: 1838854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA224_DERIVE_KEY: 1848854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA256_DERIVE_KEY: 1858854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA384_DERIVE_KEY: 1868854d3c6SJerome Forissier case TEE_ALG_CONCAT_KDF_SHA512_DERIVE_KEY: 1870f2293b7SJerome Forissier case TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY: 188b0104773SPascal Brand if (mode != TEE_MODE_DERIVE) 189b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 190b0104773SPascal Brand with_private_key = true; 191b0104773SPascal Brand req_key_usage = TEE_USAGE_DERIVE; 192b0104773SPascal Brand break; 193b0104773SPascal Brand 194b0104773SPascal Brand case TEE_ALG_MD5: 195b0104773SPascal Brand case TEE_ALG_SHA1: 196b0104773SPascal Brand case TEE_ALG_SHA224: 197b0104773SPascal Brand case TEE_ALG_SHA256: 198b0104773SPascal Brand case TEE_ALG_SHA384: 199b0104773SPascal Brand case TEE_ALG_SHA512: 200b0104773SPascal Brand if (mode != TEE_MODE_DIGEST) 201b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 202b0104773SPascal Brand handle_state |= TEE_HANDLE_FLAG_KEY_SET; 203b0104773SPascal Brand req_key_usage = 0; 204b0104773SPascal Brand break; 205b0104773SPascal Brand 206b0104773SPascal Brand case TEE_ALG_DES_CBC_MAC_NOPAD: 207b0104773SPascal Brand case TEE_ALG_AES_CBC_MAC_NOPAD: 208b0104773SPascal Brand case TEE_ALG_AES_CBC_MAC_PKCS5: 209b0104773SPascal Brand case TEE_ALG_AES_CMAC: 210b0104773SPascal Brand case TEE_ALG_DES_CBC_MAC_PKCS5: 211b0104773SPascal Brand case TEE_ALG_DES3_CBC_MAC_NOPAD: 212b0104773SPascal Brand case TEE_ALG_DES3_CBC_MAC_PKCS5: 213b0104773SPascal Brand case TEE_ALG_HMAC_MD5: 214b0104773SPascal Brand case TEE_ALG_HMAC_SHA1: 215b0104773SPascal Brand case TEE_ALG_HMAC_SHA224: 216b0104773SPascal Brand case TEE_ALG_HMAC_SHA256: 217b0104773SPascal Brand case TEE_ALG_HMAC_SHA384: 218b0104773SPascal Brand case TEE_ALG_HMAC_SHA512: 219b0104773SPascal Brand if (mode != TEE_MODE_MAC) 220b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 221b0104773SPascal Brand req_key_usage = TEE_USAGE_MAC; 222b0104773SPascal Brand break; 223b0104773SPascal Brand 224b0104773SPascal Brand default: 225b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 226b0104773SPascal Brand } 227b0104773SPascal Brand 228b0104773SPascal Brand op = TEE_Malloc(sizeof(*op), 0); 229b0104773SPascal Brand if (op == NULL) 230b0104773SPascal Brand return TEE_ERROR_OUT_OF_MEMORY; 231b0104773SPascal Brand 232b0104773SPascal Brand op->info.algorithm = algorithm; 233b0104773SPascal Brand op->info.operationClass = TEE_ALG_GET_CLASS(algorithm); 234b0104773SPascal Brand op->info.mode = mode; 235b0104773SPascal Brand op->info.maxKeySize = maxKeySize; 236b0104773SPascal Brand op->info.requiredKeyUsage = req_key_usage; 237b0104773SPascal Brand op->info.handleState = handle_state; 238b0104773SPascal Brand 239b0104773SPascal Brand if (block_size > 1) { 240b0104773SPascal Brand size_t buffer_size = block_size; 241b0104773SPascal Brand 242b0104773SPascal Brand if (buffer_two_blocks) 243b0104773SPascal Brand buffer_size *= 2; 244b0104773SPascal Brand 245b0104773SPascal Brand op->buffer = 246b0104773SPascal Brand TEE_Malloc(buffer_size, TEE_USER_MEM_HINT_NO_FILL_ZERO); 247b0104773SPascal Brand if (op->buffer == NULL) { 248b0104773SPascal Brand res = TEE_ERROR_OUT_OF_MEMORY; 249b0104773SPascal Brand goto out; 250b0104773SPascal Brand } 251b0104773SPascal Brand } 252b0104773SPascal Brand op->block_size = block_size; 253b0104773SPascal Brand op->buffer_two_blocks = buffer_two_blocks; 254b0104773SPascal Brand 255b0104773SPascal Brand if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) { 256b0104773SPascal Brand uint32_t mks = maxKeySize; 257b0104773SPascal Brand TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm, 258b0104773SPascal Brand with_private_key); 259b0104773SPascal Brand 260b0104773SPascal Brand /* 261b0104773SPascal Brand * If two keys are expected the max key size is the sum of 262b0104773SPascal Brand * the size of both keys. 263b0104773SPascal Brand */ 264b0104773SPascal Brand if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) 265b0104773SPascal Brand mks /= 2; 266b0104773SPascal Brand 267b0104773SPascal Brand res = TEE_AllocateTransientObject(key_type, mks, &op->key1); 268b0104773SPascal Brand if (res != TEE_SUCCESS) 269b0104773SPascal Brand goto out; 270b0104773SPascal Brand 271b0104773SPascal Brand if ((op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 272b0104773SPascal Brand 0) { 273b0104773SPascal Brand res = 274b0104773SPascal Brand TEE_AllocateTransientObject(key_type, mks, 275b0104773SPascal Brand &op->key2); 276b0104773SPascal Brand if (res != TEE_SUCCESS) 277b0104773SPascal Brand goto out; 278b0104773SPascal Brand } 279b0104773SPascal Brand } 280b0104773SPascal Brand 281b0104773SPascal Brand res = utee_cryp_state_alloc(algorithm, mode, (uint32_t) op->key1, 282b0104773SPascal Brand (uint32_t) op->key2, &op->state); 283b0104773SPascal Brand if (res != TEE_SUCCESS) 284b0104773SPascal Brand goto out; 285b0104773SPascal Brand 286b0104773SPascal Brand /* For multi-stage operation do an "init". */ 287b0104773SPascal Brand TEE_ResetOperation(op); 288b0104773SPascal Brand *operation = op; 289b0104773SPascal Brand 290b0104773SPascal Brand out: 291b0104773SPascal Brand if (res != TEE_SUCCESS) { 292b0104773SPascal Brand TEE_FreeTransientObject(op->key1); 293b0104773SPascal Brand TEE_FreeTransientObject(op->key2); 294b0104773SPascal Brand TEE_FreeOperation(op); 295b0104773SPascal Brand } 296b0104773SPascal Brand 297b0104773SPascal Brand return res; 298b0104773SPascal Brand } 299b0104773SPascal Brand 300b0104773SPascal Brand void TEE_FreeOperation(TEE_OperationHandle operation) 301b0104773SPascal Brand { 302b0104773SPascal Brand if (operation != TEE_HANDLE_NULL) { 303b0104773SPascal Brand /* 304b0104773SPascal Brand * Note that keys should not be freed here, since they are 305b0104773SPascal Brand * claimed by the operation they will be freed by 306b0104773SPascal Brand * utee_cryp_state_free(). 307b0104773SPascal Brand */ 308b0104773SPascal Brand utee_cryp_state_free(operation->state); 309b0104773SPascal Brand TEE_Free(operation->buffer); 310b0104773SPascal Brand TEE_Free(operation); 311b0104773SPascal Brand } 312b0104773SPascal Brand } 313b0104773SPascal Brand 314b0104773SPascal Brand void TEE_GetOperationInfo(TEE_OperationHandle operation, 315b0104773SPascal Brand TEE_OperationInfo *operationInfo) 316b0104773SPascal Brand { 317b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 318b0104773SPascal Brand TEE_Panic(0); 319b0104773SPascal Brand 320b0104773SPascal Brand if (operationInfo == NULL) 321b0104773SPascal Brand TEE_Panic(0); 322b0104773SPascal Brand 323b0104773SPascal Brand *operationInfo = operation->info; 324b0104773SPascal Brand } 325b0104773SPascal Brand 326b0104773SPascal Brand void TEE_ResetOperation(TEE_OperationHandle operation) 327b0104773SPascal Brand { 328b0104773SPascal Brand TEE_Result res; 329b0104773SPascal Brand 330b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 331b0104773SPascal Brand TEE_Panic(0); 332b0104773SPascal Brand if (operation->info.operationClass == TEE_OPERATION_DIGEST) { 333b0104773SPascal Brand res = utee_hash_init(operation->state, NULL, 0); 334b0104773SPascal Brand if (res != TEE_SUCCESS) 335b0104773SPascal Brand TEE_Panic(res); 336b0104773SPascal Brand } 337b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 338b0104773SPascal Brand } 339b0104773SPascal Brand 340b0104773SPascal Brand TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation, 341b0104773SPascal Brand TEE_ObjectHandle key) 342b0104773SPascal Brand { 3437583c59eSCedric Chaumont TEE_Result res; 344b0104773SPascal Brand uint32_t key_size = 0; 345b0104773SPascal Brand 346b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 347b0104773SPascal Brand TEE_Panic(0); 348b0104773SPascal Brand 349b0104773SPascal Brand /* No key for digests */ 350b0104773SPascal Brand if (operation->info.operationClass == TEE_OPERATION_DIGEST) 351b0104773SPascal Brand TEE_Panic(0); 352b0104773SPascal Brand 353b0104773SPascal Brand /* Two keys expected */ 354b0104773SPascal Brand if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 355b0104773SPascal Brand 0) 356b0104773SPascal Brand TEE_Panic(0); 357b0104773SPascal Brand 358b0104773SPascal Brand if (key != TEE_HANDLE_NULL) { 359b0104773SPascal Brand TEE_ObjectInfo key_info; 360b0104773SPascal Brand 3617583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key, &key_info); 3627583c59eSCedric Chaumont if (res != TEE_SUCCESS) 3637583c59eSCedric Chaumont goto err; 3647583c59eSCedric Chaumont 365b0104773SPascal Brand /* Supplied key has to meet required usage */ 366b0104773SPascal Brand if ((key_info.objectUsage & operation->info.requiredKeyUsage) != 367b0104773SPascal Brand operation->info.requiredKeyUsage) { 368b0104773SPascal Brand TEE_Panic(0); 369b0104773SPascal Brand } 370b0104773SPascal Brand 3717583c59eSCedric Chaumont if (operation->info.maxKeySize < key_info.keySize) 372b0104773SPascal Brand TEE_Panic(0); 373b0104773SPascal Brand 3747583c59eSCedric Chaumont key_size = key_info.keySize; 375b0104773SPascal Brand } 376b0104773SPascal Brand 377b0104773SPascal Brand TEE_ResetTransientObject(operation->key1); 378b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 379b0104773SPascal Brand 380b0104773SPascal Brand if (key != TEE_HANDLE_NULL) { 3817583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key1, key); 3827583c59eSCedric Chaumont if (res != TEE_SUCCESS) 3837583c59eSCedric Chaumont goto err; 3847583c59eSCedric Chaumont 385b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 386b0104773SPascal Brand } 387b0104773SPascal Brand 388b0104773SPascal Brand operation->info.keySize = key_size; 389b0104773SPascal Brand 3907583c59eSCedric Chaumont goto out; 3917583c59eSCedric Chaumont 3927583c59eSCedric Chaumont err: 3937583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT || 3947583c59eSCedric Chaumont res == TEE_ERROR_STORAGE_NOT_AVAILABLE) 3957583c59eSCedric Chaumont return res; 3967583c59eSCedric Chaumont TEE_Panic(0); 3977583c59eSCedric Chaumont out: 398b0104773SPascal Brand return TEE_SUCCESS; 399b0104773SPascal Brand } 400b0104773SPascal Brand 401b0104773SPascal Brand TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation, 402b0104773SPascal Brand TEE_ObjectHandle key1, TEE_ObjectHandle key2) 403b0104773SPascal Brand { 4047583c59eSCedric Chaumont TEE_Result res; 405b0104773SPascal Brand uint32_t key_size = 0; 406b0104773SPascal Brand 407b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 408b0104773SPascal Brand TEE_Panic(0); 409b0104773SPascal Brand 410b0104773SPascal Brand /* Two keys not expected */ 411b0104773SPascal Brand if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 412b0104773SPascal Brand 0) 413b0104773SPascal Brand TEE_Panic(0); 414b0104773SPascal Brand 415b0104773SPascal Brand /* Either both keys are NULL or both are not NULL */ 416b0104773SPascal Brand if ((key1 == TEE_HANDLE_NULL || key2 == TEE_HANDLE_NULL) && 417b0104773SPascal Brand key1 != key2) 418b0104773SPascal Brand TEE_Panic(0); 419b0104773SPascal Brand 420b0104773SPascal Brand if (key1 != TEE_HANDLE_NULL) { 421b0104773SPascal Brand TEE_ObjectInfo key_info1; 422b0104773SPascal Brand TEE_ObjectInfo key_info2; 423b0104773SPascal Brand 4247583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key1, &key_info1); 4257583c59eSCedric Chaumont if (res != TEE_SUCCESS) 4267583c59eSCedric Chaumont goto err; 4277583c59eSCedric Chaumont 428b0104773SPascal Brand /* Supplied key has to meet required usage */ 429b0104773SPascal Brand if ((key_info1.objectUsage & operation->info. 430b0104773SPascal Brand requiredKeyUsage) != operation->info.requiredKeyUsage) { 431b0104773SPascal Brand TEE_Panic(0); 432b0104773SPascal Brand } 433b0104773SPascal Brand 4347583c59eSCedric Chaumont res = TEE_GetObjectInfo1(key2, &key_info2); 4357583c59eSCedric Chaumont if (res != TEE_SUCCESS) { 4367583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) 4377583c59eSCedric Chaumont res = TEE_ERROR_CORRUPT_OBJECT_2; 4387583c59eSCedric Chaumont goto err; 4397583c59eSCedric Chaumont } 4407583c59eSCedric Chaumont 441b0104773SPascal Brand /* Supplied key has to meet required usage */ 442b0104773SPascal Brand if ((key_info2.objectUsage & operation->info. 443b0104773SPascal Brand requiredKeyUsage) != operation->info.requiredKeyUsage) { 444b0104773SPascal Brand TEE_Panic(0); 445b0104773SPascal Brand } 446b0104773SPascal Brand 447b0104773SPascal Brand /* 448b0104773SPascal Brand * AES-XTS (the only multi key algorithm supported, requires the 449b0104773SPascal Brand * keys to be of equal size. 450b0104773SPascal Brand */ 451b0104773SPascal Brand if (operation->info.algorithm == TEE_ALG_AES_XTS && 4527583c59eSCedric Chaumont key_info1.keySize != key_info2.keySize) 453b0104773SPascal Brand TEE_Panic(0); 454b0104773SPascal Brand 4557583c59eSCedric Chaumont if (operation->info.maxKeySize < key_info1.keySize) 456b0104773SPascal Brand TEE_Panic(0); 457b0104773SPascal Brand 458b0104773SPascal Brand /* 459b0104773SPascal Brand * Odd that only the size of one key should be reported while 460b0104773SPascal Brand * size of two key are used when allocating the operation. 461b0104773SPascal Brand */ 4627583c59eSCedric Chaumont key_size = key_info1.keySize; 463b0104773SPascal Brand } 464b0104773SPascal Brand 465b0104773SPascal Brand TEE_ResetTransientObject(operation->key1); 466b0104773SPascal Brand TEE_ResetTransientObject(operation->key2); 467b0104773SPascal Brand operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET; 468b0104773SPascal Brand 469b0104773SPascal Brand if (key1 != TEE_HANDLE_NULL) { 4707583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key1, key1); 4717583c59eSCedric Chaumont if (res != TEE_SUCCESS) 4727583c59eSCedric Chaumont goto err; 4737583c59eSCedric Chaumont 4747583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(operation->key2, key2); 4757583c59eSCedric Chaumont if (res != TEE_SUCCESS) { 4767583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) 4777583c59eSCedric Chaumont res = TEE_ERROR_CORRUPT_OBJECT_2; 4787583c59eSCedric Chaumont goto err; 4797583c59eSCedric Chaumont } 4807583c59eSCedric Chaumont 481b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET; 482b0104773SPascal Brand } 483b0104773SPascal Brand 484b0104773SPascal Brand operation->info.keySize = key_size; 485b0104773SPascal Brand 4867583c59eSCedric Chaumont goto out; 4877583c59eSCedric Chaumont 4887583c59eSCedric Chaumont err: 4897583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT || 4907583c59eSCedric Chaumont res == TEE_ERROR_CORRUPT_OBJECT_2 || 4917583c59eSCedric Chaumont res == TEE_ERROR_STORAGE_NOT_AVAILABLE || 4927583c59eSCedric Chaumont res == TEE_ERROR_STORAGE_NOT_AVAILABLE_2) 4937583c59eSCedric Chaumont return res; 4947583c59eSCedric Chaumont TEE_Panic(0); 4957583c59eSCedric Chaumont out: 496b0104773SPascal Brand return TEE_SUCCESS; 497b0104773SPascal Brand } 498b0104773SPascal Brand 499b0104773SPascal Brand void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op) 500b0104773SPascal Brand { 501b0104773SPascal Brand TEE_Result res; 502b0104773SPascal Brand 503b0104773SPascal Brand if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL) 504b0104773SPascal Brand TEE_Panic(0); 505b0104773SPascal Brand if (dst_op->info.algorithm != src_op->info.algorithm) 506b0104773SPascal Brand TEE_Panic(0); 507b0104773SPascal Brand if (src_op->info.operationClass != TEE_OPERATION_DIGEST) { 508b0104773SPascal Brand TEE_ObjectHandle key1 = TEE_HANDLE_NULL; 509b0104773SPascal Brand TEE_ObjectHandle key2 = TEE_HANDLE_NULL; 510b0104773SPascal Brand 511b0104773SPascal Brand if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) { 512b0104773SPascal Brand key1 = src_op->key1; 513b0104773SPascal Brand key2 = src_op->key2; 514b0104773SPascal Brand } 515b0104773SPascal Brand 516b0104773SPascal Brand if ((src_op->info.handleState & 517b0104773SPascal Brand TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) { 518b0104773SPascal Brand TEE_SetOperationKey(dst_op, key1); 519b0104773SPascal Brand } else { 520b0104773SPascal Brand TEE_SetOperationKey2(dst_op, key1, key2); 521b0104773SPascal Brand } 522b0104773SPascal Brand } 523b0104773SPascal Brand dst_op->info.handleState = src_op->info.handleState; 524b0104773SPascal Brand dst_op->info.keySize = src_op->info.keySize; 525b0104773SPascal Brand 526b0104773SPascal Brand if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks || 527b0104773SPascal Brand dst_op->block_size != src_op->block_size) 528b0104773SPascal Brand TEE_Panic(0); 529b0104773SPascal Brand 530b0104773SPascal Brand if (dst_op->buffer != NULL) { 531b0104773SPascal Brand if (src_op->buffer == NULL) 532b0104773SPascal Brand TEE_Panic(0); 533b0104773SPascal Brand 534b0104773SPascal Brand memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs); 535b0104773SPascal Brand dst_op->buffer_offs = src_op->buffer_offs; 536b0104773SPascal Brand } else if (src_op->buffer != NULL) { 537b0104773SPascal Brand TEE_Panic(0); 538b0104773SPascal Brand } 539b0104773SPascal Brand 540b0104773SPascal Brand res = utee_cryp_state_copy(dst_op->state, src_op->state); 541b0104773SPascal Brand if (res != TEE_SUCCESS) 542b0104773SPascal Brand TEE_Panic(res); 543b0104773SPascal Brand } 544b0104773SPascal Brand 545b0104773SPascal Brand /* Cryptographic Operations API - Message Digest Functions */ 546b0104773SPascal Brand 547b0104773SPascal Brand void TEE_DigestUpdate(TEE_OperationHandle operation, 54879a3c601SCedric Chaumont void *chunk, uint32_t chunkSize) 549b0104773SPascal Brand { 55073d6c3baSJoakim Bech TEE_Result res = TEE_ERROR_GENERIC; 551b0104773SPascal Brand 55273d6c3baSJoakim Bech if (operation == TEE_HANDLE_NULL || 55373d6c3baSJoakim Bech operation->info.operationClass != TEE_OPERATION_DIGEST) 554b0104773SPascal Brand TEE_Panic(0); 55573d6c3baSJoakim Bech 556b0104773SPascal Brand res = utee_hash_update(operation->state, chunk, chunkSize); 557b0104773SPascal Brand if (res != TEE_SUCCESS) 558b0104773SPascal Brand TEE_Panic(res); 559b0104773SPascal Brand } 560b0104773SPascal Brand 561b0104773SPascal Brand TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void *chunk, 56279a3c601SCedric Chaumont uint32_t chunkLen, void *hash, uint32_t *hashLen) 563b0104773SPascal Brand { 56473d6c3baSJoakim Bech if ((operation == TEE_HANDLE_NULL) || (!chunk && chunkLen) || 56573d6c3baSJoakim Bech !hash || !hashLen || 56673d6c3baSJoakim Bech (operation->info.operationClass != TEE_OPERATION_DIGEST)) 567b0104773SPascal Brand TEE_Panic(0); 56873d6c3baSJoakim Bech 569b0104773SPascal Brand return utee_hash_final(operation->state, chunk, chunkLen, hash, 570b0104773SPascal Brand hashLen); 571b0104773SPascal Brand } 572b0104773SPascal Brand 573b0104773SPascal Brand /* Cryptographic Operations API - Symmetric Cipher Functions */ 574b0104773SPascal Brand 57579a3c601SCedric Chaumont void TEE_CipherInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen) 576b0104773SPascal Brand { 577b0104773SPascal Brand TEE_Result res; 578b0104773SPascal Brand 579b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 580b0104773SPascal Brand TEE_Panic(0); 581b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_CIPHER) 582b0104773SPascal Brand TEE_Panic(0); 583b0104773SPascal Brand res = utee_cipher_init(operation->state, IV, IVLen); 584b0104773SPascal Brand if (res != TEE_SUCCESS) 585b0104773SPascal Brand TEE_Panic(res); 586b0104773SPascal Brand operation->buffer_offs = 0; 587b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 588b0104773SPascal Brand } 589b0104773SPascal Brand 590b0104773SPascal Brand static TEE_Result tee_buffer_update( 591b0104773SPascal Brand TEE_OperationHandle op, 592b0104773SPascal Brand TEE_Result(*update_func) (uint32_t state, const void *src, 5937f74c64aSPascal Brand size_t slen, void *dst, uint32_t *dlen), 594b0104773SPascal Brand const void *src_data, size_t src_len, 5957f74c64aSPascal Brand void *dest_data, uint32_t *dest_len) 596b0104773SPascal Brand { 597b0104773SPascal Brand TEE_Result res; 598b0104773SPascal Brand const uint8_t *src = src_data; 599b0104773SPascal Brand size_t slen = src_len; 600b0104773SPascal Brand uint8_t *dst = dest_data; 601b0104773SPascal Brand size_t dlen = *dest_len; 602b0104773SPascal Brand size_t acc_dlen = 0; 6037f74c64aSPascal Brand uint32_t tmp_dlen; 604b0104773SPascal Brand size_t l; 605b0104773SPascal Brand size_t buffer_size; 606d3588802SPascal Brand size_t buffer_left; 607b0104773SPascal Brand 608d3588802SPascal Brand if (op->buffer_two_blocks) { 609b0104773SPascal Brand buffer_size = op->block_size * 2; 610d3588802SPascal Brand buffer_left = 1; 611d3588802SPascal Brand } else { 612b0104773SPascal Brand buffer_size = op->block_size; 613d3588802SPascal Brand buffer_left = 0; 614d3588802SPascal Brand } 615b0104773SPascal Brand 616b0104773SPascal Brand if (op->buffer_offs > 0) { 617b0104773SPascal Brand /* Fill up complete block */ 618b0104773SPascal Brand if (op->buffer_offs < op->block_size) 619b0104773SPascal Brand l = MIN(slen, op->block_size - op->buffer_offs); 620b0104773SPascal Brand else 621b0104773SPascal Brand l = MIN(slen, buffer_size - op->buffer_offs); 622b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, l); 623b0104773SPascal Brand op->buffer_offs += l; 624b0104773SPascal Brand src += l; 625b0104773SPascal Brand slen -= l; 626b0104773SPascal Brand if ((op->buffer_offs % op->block_size) != 0) 627b0104773SPascal Brand goto out; /* Nothing left to do */ 628b0104773SPascal Brand } 629b0104773SPascal Brand 630b0104773SPascal Brand /* If we can feed from buffer */ 631d3588802SPascal Brand if ((op->buffer_offs > 0) && 632d3588802SPascal Brand ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) { 6332ff3fdbbSPascal Brand l = ROUNDUP(op->buffer_offs + slen - buffer_size, 634b0104773SPascal Brand op->block_size); 635b0104773SPascal Brand l = MIN(op->buffer_offs, l); 636b0104773SPascal Brand tmp_dlen = dlen; 637b0104773SPascal Brand res = update_func(op->state, op->buffer, l, dst, &tmp_dlen); 638b0104773SPascal Brand if (res != TEE_SUCCESS) 639b0104773SPascal Brand TEE_Panic(res); 640b0104773SPascal Brand dst += tmp_dlen; 641b0104773SPascal Brand dlen -= tmp_dlen; 642b0104773SPascal Brand acc_dlen += tmp_dlen; 643b0104773SPascal Brand op->buffer_offs -= l; 644b0104773SPascal Brand if (op->buffer_offs > 0) { 645b0104773SPascal Brand /* 646b0104773SPascal Brand * Slen is small enough to be contained in rest buffer. 647b0104773SPascal Brand */ 648b0104773SPascal Brand memcpy(op->buffer, op->buffer + l, buffer_size - l); 649b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 650b0104773SPascal Brand op->buffer_offs += slen; 651b0104773SPascal Brand goto out; /* Nothing left to do */ 652b0104773SPascal Brand } 653b0104773SPascal Brand } 654b0104773SPascal Brand 655d3588802SPascal Brand if (slen >= (buffer_size + buffer_left)) { 656b0104773SPascal Brand /* Buffer is empty, feed as much as possible from src */ 657b0104773SPascal Brand if (TEE_ALIGNMENT_IS_OK(src, uint32_t)) { 6582ff3fdbbSPascal Brand l = ROUNDUP(slen - buffer_size + 1, op->block_size); 659b0104773SPascal Brand 660b0104773SPascal Brand tmp_dlen = dlen; 661b0104773SPascal Brand res = update_func(op->state, src, l, dst, &tmp_dlen); 662b0104773SPascal Brand if (res != TEE_SUCCESS) 663b0104773SPascal Brand TEE_Panic(res); 664b0104773SPascal Brand src += l; 665b0104773SPascal Brand slen -= l; 666b0104773SPascal Brand dst += tmp_dlen; 667b0104773SPascal Brand dlen -= tmp_dlen; 668b0104773SPascal Brand acc_dlen += tmp_dlen; 669b0104773SPascal Brand } else { 670b0104773SPascal Brand /* 671b0104773SPascal Brand * Supplied data isn't well aligned, we're forced to 672b0104773SPascal Brand * feed through the buffer. 673b0104773SPascal Brand */ 674b0104773SPascal Brand while (slen >= op->block_size) { 675b0104773SPascal Brand memcpy(op->buffer, src, op->block_size); 676b0104773SPascal Brand 677b0104773SPascal Brand tmp_dlen = dlen; 678b0104773SPascal Brand res = 679b0104773SPascal Brand update_func(op->state, op->buffer, 680b0104773SPascal Brand op->block_size, dst, &tmp_dlen); 681b0104773SPascal Brand if (res != TEE_SUCCESS) 682b0104773SPascal Brand TEE_Panic(res); 683b0104773SPascal Brand src += op->block_size; 684b0104773SPascal Brand slen -= op->block_size; 685b0104773SPascal Brand dst += tmp_dlen; 686b0104773SPascal Brand dlen -= tmp_dlen; 687b0104773SPascal Brand acc_dlen += tmp_dlen; 688b0104773SPascal Brand } 689b0104773SPascal Brand } 690b0104773SPascal Brand } 691b0104773SPascal Brand 692b0104773SPascal Brand /* Slen is small enough to be contained in buffer. */ 693b0104773SPascal Brand memcpy(op->buffer + op->buffer_offs, src, slen); 694b0104773SPascal Brand op->buffer_offs += slen; 695b0104773SPascal Brand 696b0104773SPascal Brand out: 697b0104773SPascal Brand *dest_len = acc_dlen; 698b0104773SPascal Brand return TEE_SUCCESS; 699b0104773SPascal Brand } 700b0104773SPascal Brand 701b0104773SPascal Brand TEE_Result TEE_CipherUpdate(TEE_OperationHandle op, const void *srcData, 70279a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 703b0104773SPascal Brand { 704b0104773SPascal Brand size_t req_dlen; 705b0104773SPascal Brand 706b0104773SPascal Brand if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 707b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 708b0104773SPascal Brand TEE_Panic(0); 709b0104773SPascal Brand if (op->info.operationClass != TEE_OPERATION_CIPHER) 710b0104773SPascal Brand TEE_Panic(0); 711b0104773SPascal Brand if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 712b0104773SPascal Brand TEE_Panic(0); 713b0104773SPascal Brand 714b0104773SPascal Brand /* Calculate required dlen */ 715b0104773SPascal Brand req_dlen = ((op->buffer_offs + srcLen) / op->block_size) * 716b0104773SPascal Brand op->block_size; 717b0104773SPascal Brand if (op->buffer_two_blocks) { 718b0104773SPascal Brand if (req_dlen > op->block_size * 2) 719b0104773SPascal Brand req_dlen -= op->block_size * 2; 720b0104773SPascal Brand else 721b0104773SPascal Brand req_dlen = 0; 722b0104773SPascal Brand } 723b0104773SPascal Brand /* 724b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 725b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 726b0104773SPascal Brand * can't restore sync with this API. 727b0104773SPascal Brand */ 728b0104773SPascal Brand if (*destLen < req_dlen) { 729b0104773SPascal Brand *destLen = req_dlen; 730b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 731b0104773SPascal Brand } 732b0104773SPascal Brand 733b0104773SPascal Brand tee_buffer_update(op, utee_cipher_update, srcData, srcLen, destData, 734b0104773SPascal Brand destLen); 735b0104773SPascal Brand 736b0104773SPascal Brand return TEE_SUCCESS; 737b0104773SPascal Brand } 738b0104773SPascal Brand 739b0104773SPascal Brand TEE_Result TEE_CipherDoFinal(TEE_OperationHandle op, 74079a3c601SCedric Chaumont const void *srcData, uint32_t srcLen, void *destData, 74179a3c601SCedric Chaumont uint32_t *destLen) 742b0104773SPascal Brand { 743b0104773SPascal Brand TEE_Result res; 744b0104773SPascal Brand uint8_t *dst = destData; 745b0104773SPascal Brand size_t acc_dlen = 0; 7467f74c64aSPascal Brand uint32_t tmp_dlen; 747b0104773SPascal Brand size_t req_dlen; 748b0104773SPascal Brand 749b0104773SPascal Brand if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 750b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 751b0104773SPascal Brand TEE_Panic(0); 752b0104773SPascal Brand if (op->info.operationClass != TEE_OPERATION_CIPHER) 753b0104773SPascal Brand TEE_Panic(0); 754b0104773SPascal Brand if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 755b0104773SPascal Brand TEE_Panic(0); 756b0104773SPascal Brand 757b0104773SPascal Brand /* 758b0104773SPascal Brand * Check that the final block doesn't require padding for those 759b0104773SPascal Brand * algorithms that requires client to supply padding. 760b0104773SPascal Brand */ 761b0104773SPascal Brand if (op->info.algorithm == TEE_ALG_AES_ECB_NOPAD || 762b0104773SPascal Brand op->info.algorithm == TEE_ALG_AES_CBC_NOPAD || 763b0104773SPascal Brand op->info.algorithm == TEE_ALG_DES_ECB_NOPAD || 764b0104773SPascal Brand op->info.algorithm == TEE_ALG_DES_CBC_NOPAD || 765b0104773SPascal Brand op->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || 766b0104773SPascal Brand op->info.algorithm == TEE_ALG_DES3_CBC_NOPAD) { 767b0104773SPascal Brand if (((op->buffer_offs + srcLen) % op->block_size) != 0) 768b0104773SPascal Brand return TEE_ERROR_BAD_PARAMETERS; 769b0104773SPascal Brand } 770b0104773SPascal Brand 771b0104773SPascal Brand /* 772b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 773b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 774b0104773SPascal Brand * can't restore sync with this API. 775b0104773SPascal Brand */ 776b0104773SPascal Brand req_dlen = op->buffer_offs + srcLen; 777b0104773SPascal Brand if (*destLen < req_dlen) { 778b0104773SPascal Brand *destLen = req_dlen; 779b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 780b0104773SPascal Brand } 781b0104773SPascal Brand 782b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 783b0104773SPascal Brand tee_buffer_update(op, utee_cipher_update, srcData, srcLen, dst, 784b0104773SPascal Brand &tmp_dlen); 785b0104773SPascal Brand dst += tmp_dlen; 786b0104773SPascal Brand acc_dlen += tmp_dlen; 787b0104773SPascal Brand 788b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 789b0104773SPascal Brand res = utee_cipher_final(op->state, op->buffer, op->buffer_offs, 790b0104773SPascal Brand dst, &tmp_dlen); 791b0104773SPascal Brand if (res != TEE_SUCCESS) 792b0104773SPascal Brand TEE_Panic(res); 793b0104773SPascal Brand acc_dlen += tmp_dlen; 794b0104773SPascal Brand 795b0104773SPascal Brand op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 796b0104773SPascal Brand *destLen = acc_dlen; 797b0104773SPascal Brand return TEE_SUCCESS; 798b0104773SPascal Brand } 799b0104773SPascal Brand 800b0104773SPascal Brand /* Cryptographic Operations API - MAC Functions */ 801b0104773SPascal Brand 80279a3c601SCedric Chaumont void TEE_MACInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen) 803b0104773SPascal Brand { 804b0104773SPascal Brand TEE_Result res; 805b0104773SPascal Brand 806b0104773SPascal Brand if (operation == TEE_HANDLE_NULL) 807b0104773SPascal Brand TEE_Panic(0); 808b0104773SPascal Brand if (IV == NULL && IVLen != 0) 809b0104773SPascal Brand TEE_Panic(0); 810b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_MAC) 811b0104773SPascal Brand TEE_Panic(0); 812b0104773SPascal Brand res = utee_hash_init(operation->state, IV, IVLen); 813b0104773SPascal Brand if (res != TEE_SUCCESS) 814b0104773SPascal Brand TEE_Panic(res); 815b0104773SPascal Brand operation->buffer_offs = 0; 816b0104773SPascal Brand operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 817b0104773SPascal Brand } 818b0104773SPascal Brand 81979a3c601SCedric Chaumont void TEE_MACUpdate(TEE_OperationHandle op, const void *chunk, uint32_t chunkSize) 820b0104773SPascal Brand { 821b0104773SPascal Brand TEE_Result res; 822b0104773SPascal Brand 823b0104773SPascal Brand if (op == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0)) 824b0104773SPascal Brand TEE_Panic(0); 825b0104773SPascal Brand if (op->info.operationClass != TEE_OPERATION_MAC) 826b0104773SPascal Brand TEE_Panic(0); 827b0104773SPascal Brand if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 828b0104773SPascal Brand TEE_Panic(0); 829b0104773SPascal Brand 830b0104773SPascal Brand res = utee_hash_update(op->state, chunk, chunkSize); 831b0104773SPascal Brand if (res != TEE_SUCCESS) 832b0104773SPascal Brand TEE_Panic(res); 833b0104773SPascal Brand } 834b0104773SPascal Brand 835b0104773SPascal Brand TEE_Result TEE_MACComputeFinal(TEE_OperationHandle op, 83679a3c601SCedric Chaumont const void *message, uint32_t messageLen, 83779a3c601SCedric Chaumont void *mac, uint32_t *macLen) 838b0104773SPascal Brand { 839b0104773SPascal Brand TEE_Result res; 840b0104773SPascal Brand 841b0104773SPascal Brand if (op == TEE_HANDLE_NULL || (message == NULL && messageLen != 0) || 842b0104773SPascal Brand mac == NULL || macLen == NULL) 843b0104773SPascal Brand TEE_Panic(0); 844b0104773SPascal Brand if (op->info.operationClass != TEE_OPERATION_MAC) 845b0104773SPascal Brand TEE_Panic(0); 846b0104773SPascal Brand if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 847b0104773SPascal Brand TEE_Panic(0); 848b0104773SPascal Brand 849b0104773SPascal Brand res = utee_hash_final(op->state, message, messageLen, mac, macLen); 850b0104773SPascal Brand op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 851b0104773SPascal Brand return res; 852b0104773SPascal Brand } 853b0104773SPascal Brand 854b0104773SPascal Brand TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation, 85579a3c601SCedric Chaumont const void *message, uint32_t messageLen, 85679a3c601SCedric Chaumont const void *mac, uint32_t macLen) 857b0104773SPascal Brand { 858b0104773SPascal Brand TEE_Result res; 859b0104773SPascal Brand uint8_t computed_mac[TEE_MAX_HASH_SIZE]; 8607f74c64aSPascal Brand uint32_t computed_mac_size = TEE_MAX_HASH_SIZE; 861b0104773SPascal Brand 862b0104773SPascal Brand res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac, 863b0104773SPascal Brand &computed_mac_size); 864b0104773SPascal Brand if (res != TEE_SUCCESS) 865b0104773SPascal Brand return res; 866b0104773SPascal Brand if (computed_mac_size != macLen) 867b0104773SPascal Brand return TEE_ERROR_MAC_INVALID; 868b796ebf3SJerome Forissier if (buf_compare_ct(mac, computed_mac, computed_mac_size) != 0) 869b0104773SPascal Brand return TEE_ERROR_MAC_INVALID; 870b0104773SPascal Brand return TEE_SUCCESS; 871b0104773SPascal Brand } 872b0104773SPascal Brand 873b0104773SPascal Brand /* Cryptographic Operations API - Authenticated Encryption Functions */ 874b0104773SPascal Brand 875b0104773SPascal Brand TEE_Result TEE_AEInit(TEE_OperationHandle op, const void *nonce, 87679a3c601SCedric Chaumont uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen, 877b0104773SPascal Brand uint32_t payloadLen) 878b0104773SPascal Brand { 879b0104773SPascal Brand TEE_Result res; 880b0104773SPascal Brand 881b0104773SPascal Brand if (op == TEE_HANDLE_NULL || nonce == NULL) 882b0104773SPascal Brand TEE_Panic(0); 883b0104773SPascal Brand if (op->info.operationClass != TEE_OPERATION_AE) 884b0104773SPascal Brand TEE_Panic(0); 885b0104773SPascal Brand 886b0104773SPascal Brand /* 887b0104773SPascal Brand * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core 888b0104773SPascal Brand * in the implementation. But AES-GCM spec doesn't specify the tag len 889b0104773SPascal Brand * according to the same principle so we have to check here instead to 890b0104773SPascal Brand * be GP compliant. 891b0104773SPascal Brand */ 892b0104773SPascal Brand if (op->info.algorithm == TEE_ALG_AES_GCM) { 893b0104773SPascal Brand /* 894b0104773SPascal Brand * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96 895b0104773SPascal Brand */ 896b0104773SPascal Brand if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) 897b0104773SPascal Brand return TEE_ERROR_NOT_SUPPORTED; 898b0104773SPascal Brand } 899b0104773SPascal Brand 900b0104773SPascal Brand res = utee_authenc_init(op->state, nonce, nonceLen, tagLen / 8, AADLen, 901b0104773SPascal Brand payloadLen); 902b0104773SPascal Brand if (res != TEE_SUCCESS) { 903b0104773SPascal Brand if (res != TEE_ERROR_NOT_SUPPORTED) 904b0104773SPascal Brand TEE_Panic(res); 905b0104773SPascal Brand return res; 906b0104773SPascal Brand } 907b0104773SPascal Brand op->ae_tag_len = tagLen / 8; 908b0104773SPascal Brand 909b0104773SPascal Brand op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED; 910b0104773SPascal Brand return TEE_SUCCESS; 911b0104773SPascal Brand } 912b0104773SPascal Brand 913b0104773SPascal Brand void TEE_AEUpdateAAD(TEE_OperationHandle op, const void *AADdata, 91479a3c601SCedric Chaumont uint32_t AADdataLen) 915b0104773SPascal Brand { 916b0104773SPascal Brand TEE_Result res; 917b0104773SPascal Brand 918b0104773SPascal Brand if (op == TEE_HANDLE_NULL || (AADdata == NULL && AADdataLen != 0)) 919b0104773SPascal Brand TEE_Panic(0); 920b0104773SPascal Brand if (op->info.operationClass != TEE_OPERATION_AE) 921b0104773SPascal Brand TEE_Panic(0); 922b0104773SPascal Brand if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 923b0104773SPascal Brand TEE_Panic(0); 924b0104773SPascal Brand 925b0104773SPascal Brand res = utee_authenc_update_aad(op->state, AADdata, AADdataLen); 926b0104773SPascal Brand if (res != TEE_SUCCESS) 927b0104773SPascal Brand TEE_Panic(res); 928b0104773SPascal Brand } 929b0104773SPascal Brand 930b0104773SPascal Brand TEE_Result TEE_AEUpdate(TEE_OperationHandle op, const void *srcData, 93179a3c601SCedric Chaumont uint32_t srcLen, void *destData, uint32_t *destLen) 932b0104773SPascal Brand { 933b0104773SPascal Brand size_t req_dlen; 934b0104773SPascal Brand 935b0104773SPascal Brand if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 936b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 937b0104773SPascal Brand TEE_Panic(0); 938b0104773SPascal Brand if (op->info.operationClass != TEE_OPERATION_AE) 939b0104773SPascal Brand TEE_Panic(0); 940b0104773SPascal Brand if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 941b0104773SPascal Brand TEE_Panic(0); 942b0104773SPascal Brand 943b0104773SPascal Brand /* 944b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 945b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 946b0104773SPascal Brand * can't restore sync with this API. 947b0104773SPascal Brand */ 9482ff3fdbbSPascal Brand req_dlen = ROUNDDOWN(op->buffer_offs + srcLen, op->block_size); 949b0104773SPascal Brand if (*destLen < req_dlen) { 950b0104773SPascal Brand *destLen = req_dlen; 951b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 952b0104773SPascal Brand } 953b0104773SPascal Brand 954b0104773SPascal Brand tee_buffer_update(op, utee_authenc_update_payload, srcData, srcLen, 955b0104773SPascal Brand destData, destLen); 956b0104773SPascal Brand 957b0104773SPascal Brand return TEE_SUCCESS; 958b0104773SPascal Brand } 959b0104773SPascal Brand 960b0104773SPascal Brand TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle op, 96179a3c601SCedric Chaumont const void *srcData, uint32_t srcLen, 96279a3c601SCedric Chaumont void *destData, uint32_t *destLen, void *tag, 96379a3c601SCedric Chaumont uint32_t *tagLen) 964b0104773SPascal Brand { 965b0104773SPascal Brand TEE_Result res; 966b0104773SPascal Brand uint8_t *dst = destData; 967b0104773SPascal Brand size_t acc_dlen = 0; 9687f74c64aSPascal Brand uint32_t tmp_dlen; 969b0104773SPascal Brand size_t req_dlen; 970b0104773SPascal Brand 971b0104773SPascal Brand if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 972b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0) || 973b0104773SPascal Brand tag == NULL || tagLen == NULL) 974b0104773SPascal Brand TEE_Panic(0); 975b0104773SPascal Brand if (op->info.operationClass != TEE_OPERATION_AE) 976b0104773SPascal Brand TEE_Panic(0); 977b0104773SPascal Brand if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 978b0104773SPascal Brand TEE_Panic(0); 979b0104773SPascal Brand 980b0104773SPascal Brand /* 981b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 982b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 983b0104773SPascal Brand * can't restore sync with this API. 984b0104773SPascal Brand */ 985b0104773SPascal Brand req_dlen = op->buffer_offs + srcLen; 986b0104773SPascal Brand if (*destLen < req_dlen) { 987b0104773SPascal Brand *destLen = req_dlen; 988b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 989b0104773SPascal Brand } 990b0104773SPascal Brand 991b0104773SPascal Brand /* 992b0104773SPascal Brand * Need to check this before update_payload since sync would be lost if 993b0104773SPascal Brand * we return short buffer after that. 994b0104773SPascal Brand */ 995b0104773SPascal Brand if (*tagLen < op->ae_tag_len) { 996b0104773SPascal Brand *tagLen = op->ae_tag_len; 997b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 998b0104773SPascal Brand } 999b0104773SPascal Brand 1000b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1001b0104773SPascal Brand tee_buffer_update(op, utee_authenc_update_payload, srcData, srcLen, 1002b0104773SPascal Brand dst, &tmp_dlen); 1003b0104773SPascal Brand dst += tmp_dlen; 1004b0104773SPascal Brand acc_dlen += tmp_dlen; 1005b0104773SPascal Brand 1006b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1007b0104773SPascal Brand res = 1008b0104773SPascal Brand utee_authenc_enc_final(op->state, op->buffer, op->buffer_offs, dst, 1009b0104773SPascal Brand &tmp_dlen, tag, tagLen); 1010b0104773SPascal Brand if (res != TEE_SUCCESS) 1011b0104773SPascal Brand TEE_Panic(res); 1012b0104773SPascal Brand acc_dlen += tmp_dlen; 1013b0104773SPascal Brand 1014b0104773SPascal Brand *destLen = acc_dlen; 1015b0104773SPascal Brand op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1016b0104773SPascal Brand 1017b0104773SPascal Brand return res; 1018b0104773SPascal Brand } 1019b0104773SPascal Brand 1020b0104773SPascal Brand TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle op, 102179a3c601SCedric Chaumont const void *srcData, uint32_t srcLen, 102279a3c601SCedric Chaumont void *destData, uint32_t *destLen, const void *tag, 102379a3c601SCedric Chaumont uint32_t tagLen) 1024b0104773SPascal Brand { 1025b0104773SPascal Brand TEE_Result res; 1026b0104773SPascal Brand uint8_t *dst = destData; 1027b0104773SPascal Brand size_t acc_dlen = 0; 10287f74c64aSPascal Brand uint32_t tmp_dlen; 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 (tag == NULL && tagLen != 0)) 1034b0104773SPascal Brand TEE_Panic(0); 1035b0104773SPascal Brand if (op->info.operationClass != TEE_OPERATION_AE) 1036b0104773SPascal Brand TEE_Panic(0); 1037b0104773SPascal Brand if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) 1038b0104773SPascal Brand TEE_Panic(0); 1039b0104773SPascal Brand 1040b0104773SPascal Brand /* 1041b0104773SPascal Brand * Check that required destLen is big enough before starting to feed 1042b0104773SPascal Brand * data to the algorithm. Errors during feeding of data are fatal as we 1043b0104773SPascal Brand * can't restore sync with this API. 1044b0104773SPascal Brand */ 1045b0104773SPascal Brand req_dlen = op->buffer_offs + srcLen; 1046b0104773SPascal Brand if (*destLen < req_dlen) { 1047b0104773SPascal Brand *destLen = req_dlen; 1048b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 1049b0104773SPascal Brand } 1050b0104773SPascal Brand 1051b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1052b0104773SPascal Brand tee_buffer_update(op, utee_authenc_update_payload, srcData, srcLen, 1053b0104773SPascal Brand dst, &tmp_dlen); 1054b0104773SPascal Brand dst += tmp_dlen; 1055b0104773SPascal Brand acc_dlen += tmp_dlen; 1056b0104773SPascal Brand 1057b0104773SPascal Brand tmp_dlen = *destLen - acc_dlen; 1058b0104773SPascal Brand res = 1059b0104773SPascal Brand utee_authenc_dec_final(op->state, op->buffer, op->buffer_offs, dst, 1060b0104773SPascal Brand &tmp_dlen, tag, tagLen); 1061b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_MAC_INVALID) 1062b0104773SPascal Brand TEE_Panic(res); 1063b0104773SPascal Brand /* Supplied tagLen should match what we initiated with */ 1064b0104773SPascal Brand if (tagLen != op->ae_tag_len) 1065b0104773SPascal Brand res = TEE_ERROR_MAC_INVALID; 1066b0104773SPascal Brand 1067b0104773SPascal Brand acc_dlen += tmp_dlen; 1068b0104773SPascal Brand 1069b0104773SPascal Brand *destLen = acc_dlen; 1070b0104773SPascal Brand op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED; 1071b0104773SPascal Brand 1072b0104773SPascal Brand return res; 1073b0104773SPascal Brand } 1074b0104773SPascal Brand 1075b0104773SPascal Brand /* Cryptographic Operations API - Asymmetric Functions */ 1076b0104773SPascal Brand 1077b0104773SPascal Brand TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle op, 1078b0104773SPascal Brand const TEE_Attribute *params, 1079b0104773SPascal Brand uint32_t paramCount, const void *srcData, 108079a3c601SCedric Chaumont uint32_t srcLen, void *destData, 108179a3c601SCedric Chaumont uint32_t *destLen) 1082b0104773SPascal Brand { 1083b0104773SPascal Brand TEE_Result res; 1084b0104773SPascal Brand 1085b0104773SPascal Brand if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1086b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 1087b0104773SPascal Brand TEE_Panic(0); 1088b0104773SPascal Brand if (paramCount != 0 && params == NULL) 1089b0104773SPascal Brand TEE_Panic(0); 1090b0104773SPascal Brand if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 1091b0104773SPascal Brand TEE_Panic(0); 1092b0104773SPascal Brand if (op->info.mode != TEE_MODE_ENCRYPT) 1093b0104773SPascal Brand TEE_Panic(0); 1094b0104773SPascal Brand 1095b0104773SPascal Brand res = utee_asymm_operate(op->state, params, paramCount, srcData, srcLen, 1096b0104773SPascal Brand destData, destLen); 10978844ebfcSPascal Brand if (res != TEE_SUCCESS && 10988844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 10998844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1100b0104773SPascal Brand TEE_Panic(res); 1101b0104773SPascal Brand return res; 1102b0104773SPascal Brand } 1103b0104773SPascal Brand 1104b0104773SPascal Brand TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle op, 1105b0104773SPascal Brand const TEE_Attribute *params, 1106b0104773SPascal Brand uint32_t paramCount, const void *srcData, 110779a3c601SCedric Chaumont uint32_t srcLen, void *destData, 110879a3c601SCedric Chaumont uint32_t *destLen) 1109b0104773SPascal Brand { 1110b0104773SPascal Brand TEE_Result res; 1111b0104773SPascal Brand 1112b0104773SPascal Brand if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) || 1113b0104773SPascal Brand destLen == NULL || (destData == NULL && *destLen != 0)) 1114b0104773SPascal Brand TEE_Panic(0); 1115b0104773SPascal Brand if (paramCount != 0 && params == NULL) 1116b0104773SPascal Brand TEE_Panic(0); 1117b0104773SPascal Brand if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) 1118b0104773SPascal Brand TEE_Panic(0); 1119b0104773SPascal Brand if (op->info.mode != TEE_MODE_DECRYPT) 1120b0104773SPascal Brand TEE_Panic(0); 1121b0104773SPascal Brand 1122b0104773SPascal Brand res = utee_asymm_operate(op->state, params, paramCount, srcData, srcLen, 1123b0104773SPascal Brand destData, destLen); 11248844ebfcSPascal Brand if (res != TEE_SUCCESS && 11258844ebfcSPascal Brand res != TEE_ERROR_SHORT_BUFFER && 11268844ebfcSPascal Brand res != TEE_ERROR_BAD_PARAMETERS) 1127b0104773SPascal Brand TEE_Panic(res); 1128b0104773SPascal Brand return res; 1129b0104773SPascal Brand } 1130b0104773SPascal Brand 1131b0104773SPascal Brand TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle op, 1132b0104773SPascal Brand const TEE_Attribute *params, 1133b0104773SPascal Brand uint32_t paramCount, const void *digest, 113479a3c601SCedric Chaumont uint32_t digestLen, void *signature, 113579a3c601SCedric Chaumont uint32_t *signatureLen) 1136b0104773SPascal Brand { 1137b0104773SPascal Brand TEE_Result res; 1138b0104773SPascal Brand 1139b0104773SPascal Brand if (op == TEE_HANDLE_NULL || (digest == NULL && digestLen != 0) || 1140b0104773SPascal Brand signature == NULL || signatureLen == NULL) 1141b0104773SPascal Brand TEE_Panic(0); 1142b0104773SPascal Brand if (paramCount != 0 && params == NULL) 1143b0104773SPascal Brand TEE_Panic(0); 1144b0104773SPascal Brand if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_SIGNATURE) 1145b0104773SPascal Brand TEE_Panic(0); 1146b0104773SPascal Brand if (op->info.mode != TEE_MODE_SIGN) 1147b0104773SPascal Brand TEE_Panic(0); 1148b0104773SPascal Brand 1149b0104773SPascal Brand res = 1150b0104773SPascal Brand utee_asymm_operate(op->state, params, paramCount, digest, digestLen, 1151b0104773SPascal Brand signature, signatureLen); 1152b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER) 1153b0104773SPascal Brand TEE_Panic(res); 1154b0104773SPascal Brand return res; 1155b0104773SPascal Brand } 1156b0104773SPascal Brand 1157b0104773SPascal Brand TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle op, 1158b0104773SPascal Brand const TEE_Attribute *params, 1159b0104773SPascal Brand uint32_t paramCount, const void *digest, 116079a3c601SCedric Chaumont uint32_t digestLen, const void *signature, 116179a3c601SCedric Chaumont uint32_t signatureLen) 1162b0104773SPascal Brand { 1163b0104773SPascal Brand TEE_Result res; 1164b0104773SPascal Brand 1165b0104773SPascal Brand if (op == TEE_HANDLE_NULL || (digest == NULL && digestLen != 0) || 1166b0104773SPascal Brand (signature == NULL && signatureLen != 0)) 1167b0104773SPascal Brand TEE_Panic(0); 1168b0104773SPascal Brand if (paramCount != 0 && params == NULL) 1169b0104773SPascal Brand TEE_Panic(0); 1170b0104773SPascal Brand if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_SIGNATURE) 1171b0104773SPascal Brand TEE_Panic(0); 1172b0104773SPascal Brand if (op->info.mode != TEE_MODE_VERIFY) 1173b0104773SPascal Brand TEE_Panic(0); 1174b0104773SPascal Brand 1175b0104773SPascal Brand res = 1176b0104773SPascal Brand utee_asymm_verify(op->state, params, paramCount, digest, digestLen, 1177b0104773SPascal Brand signature, signatureLen); 1178b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID) 1179b0104773SPascal Brand TEE_Panic(res); 1180b0104773SPascal Brand return res; 1181b0104773SPascal Brand } 1182b0104773SPascal Brand 1183b0104773SPascal Brand /* Cryptographic Operations API - Key Derivation Functions */ 1184b0104773SPascal Brand 1185b0104773SPascal Brand void TEE_DeriveKey(TEE_OperationHandle operation, 1186b0104773SPascal Brand const TEE_Attribute *params, uint32_t paramCount, 1187b0104773SPascal Brand TEE_ObjectHandle derivedKey) 1188b0104773SPascal Brand { 1189b0104773SPascal Brand TEE_Result res; 1190b0104773SPascal Brand TEE_ObjectInfo key_info; 1191b0104773SPascal Brand 1192b0104773SPascal Brand if (operation == TEE_HANDLE_NULL || derivedKey == 0) 1193b0104773SPascal Brand TEE_Panic(0); 1194b0104773SPascal Brand if (paramCount != 0 && params == NULL) 1195b0104773SPascal Brand TEE_Panic(0); 11968854d3c6SJerome Forissier if (TEE_ALG_GET_CLASS(operation->info.algorithm) != 11978854d3c6SJerome Forissier TEE_OPERATION_KEY_DERIVATION) 1198b0104773SPascal Brand TEE_Panic(0); 1199b0104773SPascal Brand 1200b0104773SPascal Brand if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION) 1201b0104773SPascal Brand TEE_Panic(0); 1202b0104773SPascal Brand if (operation->info.mode != TEE_MODE_DERIVE) 1203b0104773SPascal Brand TEE_Panic(0); 1204b0104773SPascal Brand if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0) 1205b0104773SPascal Brand TEE_Panic(0); 1206b0104773SPascal Brand 1207b0104773SPascal Brand res = utee_cryp_obj_get_info((uint32_t) derivedKey, &key_info); 1208b0104773SPascal Brand if (res != TEE_SUCCESS) 1209b0104773SPascal Brand TEE_Panic(0); 1210b0104773SPascal Brand 1211b0104773SPascal Brand if (key_info.objectType != TEE_TYPE_GENERIC_SECRET) 1212b0104773SPascal Brand TEE_Panic(0); 1213b0104773SPascal Brand if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 1214b0104773SPascal Brand TEE_Panic(0); 1215b0104773SPascal Brand 1216b0104773SPascal Brand res = utee_cryp_derive_key(operation->state, params, paramCount, 1217b0104773SPascal Brand (uint32_t) derivedKey); 1218b0104773SPascal Brand if (res != TEE_SUCCESS) 1219b0104773SPascal Brand TEE_Panic(res); 1220b0104773SPascal Brand } 1221b0104773SPascal Brand 1222b0104773SPascal Brand /* Cryptographic Operations API - Random Number Generation Functions */ 1223b0104773SPascal Brand 122479a3c601SCedric Chaumont void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen) 1225b0104773SPascal Brand { 1226b0104773SPascal Brand TEE_Result res; 1227b0104773SPascal Brand 1228b0104773SPascal Brand res = utee_cryp_random_number_generate(randomBuffer, randomBufferLen); 1229b0104773SPascal Brand if (res != TEE_SUCCESS) 1230b0104773SPascal Brand TEE_Panic(res); 1231b0104773SPascal Brand } 1232