Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 1 | /* |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2018-2019, Arm Limited. All rights reserved. |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <limits.h> |
| 9 | |
| 10 | #include "tfm_crypto_defs.h" |
| 11 | |
| 12 | /* Pre include Mbed TLS headers */ |
| 13 | #define LIB_PREFIX_NAME __tfm_crypto__ |
| 14 | #include "mbedtls_global_symbols.h" |
| 15 | |
| 16 | /* Include the Mbed TLS configuration file, the way Mbed TLS does it |
| 17 | * in each of its header files. |
| 18 | */ |
| 19 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 20 | #include "platform/ext/common/tfm_mbedtls_config.h" |
| 21 | #else |
| 22 | #include MBEDTLS_CONFIG_FILE |
| 23 | #endif |
| 24 | |
| 25 | #include "psa_crypto.h" |
| 26 | #include "tfm_crypto_api.h" |
| 27 | |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame^] | 28 | #include "tfm_crypto_struct.h" |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * \brief This value defines the maximum number of simultaneous operations |
| 32 | * supported by this implementation. |
| 33 | */ |
| 34 | #define TFM_CRYPTO_CONC_OPER_NUM (8) |
| 35 | |
| 36 | /** |
| 37 | * \brief This value is used to mark an handle as invalid. |
| 38 | * |
| 39 | */ |
| 40 | #define INVALID_HANDLE (0xFFFFFFFF) |
| 41 | |
| 42 | struct tfm_crypto_operation_s { |
| 43 | uint32_t in_use; /*!< Indicates if the operation is in use */ |
| 44 | enum tfm_crypto_operation_type type; /*!< Type of the operation */ |
| 45 | union { |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame^] | 46 | struct tfm_cipher_operation_s cipher; /*!< Cipher operation context */ |
| 47 | struct tfm_mac_operation_s mac; /*!< MAC operation context */ |
| 48 | struct tfm_hash_operation_s hash; /*!< Hash operation context */ |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 49 | } operation; |
| 50 | }; |
| 51 | |
| 52 | static struct tfm_crypto_operation_s operation[TFM_CRYPTO_CONC_OPER_NUM] ={{0}}; |
| 53 | |
| 54 | static void memset_operation_context(uint32_t index) |
| 55 | { |
| 56 | uint32_t i, mem_size; |
| 57 | |
| 58 | volatile uint8_t *mem_ptr = (uint8_t *) &(operation[index].operation); |
| 59 | |
| 60 | switch(operation[index].type) { |
| 61 | case TFM_CRYPTO_CIPHER_OPERATION: |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame^] | 62 | mem_size = sizeof(struct tfm_cipher_operation_s); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 63 | break; |
| 64 | case TFM_CRYPTO_MAC_OPERATION: |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame^] | 65 | mem_size = sizeof(struct tfm_mac_operation_s); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 66 | break; |
| 67 | case TFM_CRYPTO_HASH_OPERATION: |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame^] | 68 | mem_size = sizeof(struct tfm_hash_operation_s); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 69 | break; |
| 70 | case TFM_CRYPTO_OPERATION_NONE: |
| 71 | default: |
| 72 | mem_size = 0; |
| 73 | break; |
| 74 | } |
| 75 | |
| 76 | for (i=0; i<mem_size; i++) { |
| 77 | mem_ptr[i] = 0; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /*! |
| 82 | * \defgroup public Public functions |
| 83 | * |
| 84 | */ |
| 85 | |
| 86 | /*!@{*/ |
| 87 | enum tfm_crypto_err_t tfm_crypto_operation_alloc( |
| 88 | enum tfm_crypto_operation_type type, |
| 89 | uint32_t *handle) |
| 90 | { |
| 91 | uint32_t i = 0; |
| 92 | |
| 93 | for (i=0; i<TFM_CRYPTO_CONC_OPER_NUM; i++) { |
| 94 | if (operation[i].in_use == TFM_CRYPTO_NOT_IN_USE) { |
| 95 | operation[i].in_use = TFM_CRYPTO_IN_USE; |
| 96 | operation[i].type = type; |
| 97 | *handle = i; |
| 98 | return TFM_CRYPTO_ERR_PSA_SUCCESS; |
| 99 | } |
| 100 | } |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame^] | 101 | *handle = INVALID_HANDLE; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 102 | |
| 103 | return TFM_CRYPTO_ERR_PSA_ERROR_NOT_PERMITTED; |
| 104 | } |
| 105 | |
| 106 | enum tfm_crypto_err_t tfm_crypto_operation_release(uint32_t *handle) |
| 107 | { |
| 108 | uint32_t i = *handle; |
| 109 | |
| 110 | if ( (i<TFM_CRYPTO_CONC_OPER_NUM) && |
| 111 | (operation[i].in_use == TFM_CRYPTO_IN_USE) ) { |
| 112 | memset_operation_context(i); |
| 113 | operation[i].in_use = TFM_CRYPTO_NOT_IN_USE; |
| 114 | operation[i].type = TFM_CRYPTO_OPERATION_NONE; |
| 115 | *handle = INVALID_HANDLE; |
| 116 | return TFM_CRYPTO_ERR_PSA_SUCCESS; |
| 117 | } |
| 118 | |
| 119 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 120 | } |
| 121 | |
| 122 | enum tfm_crypto_err_t tfm_crypto_operation_lookup( |
| 123 | enum tfm_crypto_operation_type type, |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame^] | 124 | uint32_t handle, |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 125 | void **oper) |
| 126 | { |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame^] | 127 | if ( (handle<TFM_CRYPTO_CONC_OPER_NUM) && |
| 128 | (operation[handle].in_use == TFM_CRYPTO_IN_USE) && |
| 129 | (operation[handle].type == type) ) { |
| 130 | *oper = (void *) &(operation[handle].operation); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 131 | return TFM_CRYPTO_ERR_PSA_SUCCESS; |
| 132 | } |
| 133 | |
| 134 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 135 | } |
| 136 | /*!@}*/ |