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 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 10 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 11 | #include "tfm_mbedcrypto_include.h" |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 12 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 13 | #include "tfm_crypto_api.h" |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 14 | #include "tfm_crypto_defs.h" |
Ken Liu | b8ed8ba | 2019-07-09 12:52:08 +0800 | [diff] [blame^] | 15 | #include "secure_fw/core/include/tfm_memory_utils.h" |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 16 | |
| 17 | /** |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 18 | * \def TFM_CRYPTO_CONC_OPER_NUM |
| 19 | * |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 20 | * \brief This is the default value for the maximum number of concurrent |
| 21 | * operations that can be active (allocated) at any time, supported |
| 22 | * by the implementation |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 23 | */ |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 24 | #ifndef TFM_CRYPTO_CONC_OPER_NUM |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 25 | #define TFM_CRYPTO_CONC_OPER_NUM (8) |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 26 | #endif |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 27 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 28 | struct tfm_crypto_operation_s { |
| 29 | uint32_t in_use; /*!< Indicates if the operation is in use */ |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 30 | int32_t owner; /*!< Indicates an ID of the owner of |
| 31 | * the context |
| 32 | */ |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 33 | enum tfm_crypto_operation_type type; /*!< Type of the operation */ |
| 34 | union { |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 35 | psa_cipher_operation_t cipher; /*!< Cipher operation context */ |
| 36 | psa_mac_operation_t mac; /*!< MAC operation context */ |
| 37 | psa_hash_operation_t hash; /*!< Hash operation context */ |
| 38 | psa_crypto_generator_t generator; /*!< Generator operation context */ |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 39 | } operation; |
| 40 | }; |
| 41 | |
| 42 | static struct tfm_crypto_operation_s operation[TFM_CRYPTO_CONC_OPER_NUM] ={{0}}; |
| 43 | |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 44 | /* |
| 45 | * \brief Function used to clear the memory associated to a backend context |
| 46 | * |
| 47 | * \param[in] index Numerical index in the database of the backend contexts |
| 48 | * |
| 49 | * \return None |
| 50 | * |
| 51 | */ |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 52 | static void memset_operation_context(uint32_t index) |
| 53 | { |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 54 | uint32_t mem_size; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 55 | |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 56 | uint8_t *mem_ptr = (uint8_t *) &(operation[index].operation); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 57 | |
| 58 | switch(operation[index].type) { |
| 59 | case TFM_CRYPTO_CIPHER_OPERATION: |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 60 | mem_size = sizeof(psa_cipher_operation_t); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 61 | break; |
| 62 | case TFM_CRYPTO_MAC_OPERATION: |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 63 | mem_size = sizeof(psa_mac_operation_t); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 64 | break; |
| 65 | case TFM_CRYPTO_HASH_OPERATION: |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 66 | mem_size = sizeof(psa_hash_operation_t); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 67 | break; |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 68 | case TFM_CRYPTO_GENERATOR_OPERATION: |
| 69 | mem_size = sizeof(psa_crypto_generator_t); |
| 70 | break; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 71 | case TFM_CRYPTO_OPERATION_NONE: |
| 72 | default: |
| 73 | mem_size = 0; |
| 74 | break; |
| 75 | } |
| 76 | |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 77 | /* Clear the contents of the backend context */ |
| 78 | (void)tfm_memset(mem_ptr, 0, mem_size); |
| 79 | } |
| 80 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 81 | /*! |
| 82 | * \defgroup public Public functions |
| 83 | * |
| 84 | */ |
| 85 | |
| 86 | /*!@{*/ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 87 | psa_status_t tfm_crypto_init_alloc(void) |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 88 | { |
| 89 | /* Clear the contents of the local contexts */ |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 90 | (void)tfm_memset(operation, 0, sizeof(operation)); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 91 | return PSA_SUCCESS; |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 94 | psa_status_t tfm_crypto_operation_alloc(enum tfm_crypto_operation_type type, |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 95 | uint32_t *handle, |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 96 | void **ctx) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 97 | { |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 98 | uint32_t i = 0; |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 99 | int32_t partition_id = 0; |
| 100 | psa_status_t status; |
| 101 | |
| 102 | status = tfm_crypto_get_caller_id(&partition_id); |
| 103 | if (status != PSA_SUCCESS) { |
| 104 | return status; |
| 105 | } |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 106 | |
Jamie Fox | 707caf7 | 2019-05-29 15:14:18 +0100 | [diff] [blame] | 107 | /* Handle must be initialised before calling a setup function */ |
| 108 | if (*handle != TFM_CRYPTO_INVALID_HANDLE) { |
| 109 | return PSA_ERROR_BAD_STATE; |
| 110 | } |
| 111 | |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 112 | /* Init to invalid values */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 113 | if (ctx == NULL) { |
| 114 | return PSA_ERROR_INVALID_ARGUMENT; |
| 115 | } |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 116 | *ctx = NULL; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 117 | |
| 118 | for (i=0; i<TFM_CRYPTO_CONC_OPER_NUM; i++) { |
| 119 | if (operation[i].in_use == TFM_CRYPTO_NOT_IN_USE) { |
| 120 | operation[i].in_use = TFM_CRYPTO_IN_USE; |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 121 | operation[i].owner = partition_id; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 122 | operation[i].type = type; |
Jamie Fox | 707caf7 | 2019-05-29 15:14:18 +0100 | [diff] [blame] | 123 | *handle = i + 1; |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 124 | *ctx = (void *) &(operation[i].operation); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 125 | return PSA_SUCCESS; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 126 | } |
| 127 | } |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 128 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 129 | return PSA_ERROR_NOT_PERMITTED; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 132 | psa_status_t tfm_crypto_operation_release(uint32_t *handle) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 133 | { |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 134 | uint32_t h_val = *handle; |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 135 | int32_t partition_id = 0; |
| 136 | psa_status_t status; |
| 137 | |
| 138 | status = tfm_crypto_get_caller_id(&partition_id); |
| 139 | if (status != PSA_SUCCESS) { |
| 140 | return status; |
| 141 | } |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 142 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 143 | if ( (h_val != TFM_CRYPTO_INVALID_HANDLE) && |
Jamie Fox | 707caf7 | 2019-05-29 15:14:18 +0100 | [diff] [blame] | 144 | (h_val <= TFM_CRYPTO_CONC_OPER_NUM) && |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 145 | (operation[h_val - 1].in_use == TFM_CRYPTO_IN_USE) && |
| 146 | (operation[h_val - 1].owner == partition_id)) { |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 147 | |
Jamie Fox | 707caf7 | 2019-05-29 15:14:18 +0100 | [diff] [blame] | 148 | memset_operation_context(h_val - 1); |
| 149 | operation[h_val - 1].in_use = TFM_CRYPTO_NOT_IN_USE; |
| 150 | operation[h_val - 1].type = TFM_CRYPTO_OPERATION_NONE; |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 151 | operation[h_val - 1].owner = 0; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 152 | *handle = TFM_CRYPTO_INVALID_HANDLE; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 153 | return PSA_SUCCESS; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 154 | } |
| 155 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 156 | return PSA_ERROR_INVALID_ARGUMENT; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 157 | } |
| 158 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 159 | psa_status_t tfm_crypto_operation_lookup(enum tfm_crypto_operation_type type, |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 160 | uint32_t handle, |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 161 | void **ctx) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 162 | { |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 163 | int32_t partition_id = 0; |
| 164 | psa_status_t status; |
| 165 | |
| 166 | status = tfm_crypto_get_caller_id(&partition_id); |
| 167 | if (status != PSA_SUCCESS) { |
| 168 | return status; |
| 169 | } |
| 170 | |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 171 | if ( (handle != TFM_CRYPTO_INVALID_HANDLE) && |
Jamie Fox | 707caf7 | 2019-05-29 15:14:18 +0100 | [diff] [blame] | 172 | (handle <= TFM_CRYPTO_CONC_OPER_NUM) && |
| 173 | (operation[handle - 1].in_use == TFM_CRYPTO_IN_USE) && |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 174 | (operation[handle - 1].type == type) && |
| 175 | (operation[handle - 1].owner == partition_id)) { |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 176 | |
Jamie Fox | 707caf7 | 2019-05-29 15:14:18 +0100 | [diff] [blame] | 177 | *ctx = (void *) &(operation[handle - 1].operation); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 178 | return PSA_SUCCESS; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 179 | } |
| 180 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 181 | return PSA_ERROR_BAD_STATE; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 182 | } |
| 183 | /*!@}*/ |