blob: be8e83c5578c619215505dcefdf349b27df35ff4 [file] [log] [blame]
Antonio de Angelis8908f472018-08-31 15:44:25 +01001/*
Antonio de Angelisdab11b42022-01-10 14:21:04 +00002 * Copyright (c) 2018-2022, Arm Limited. All rights reserved.
Antonio de Angelis8908f472018-08-31 15:44:25 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
Jamie Fox0e54ebc2019-04-09 14:21:04 +01008#include <stddef.h>
9#include <stdint.h>
Antonio de Angelis8908f472018-08-31 15:44:25 +010010
Jamie Fox0e54ebc2019-04-09 14:21:04 +010011#include "tfm_mbedcrypto_include.h"
Antonio de Angelis8908f472018-08-31 15:44:25 +010012
Antonio de Angelis8908f472018-08-31 15:44:25 +010013#include "tfm_crypto_api.h"
Jamie Fox0e54ebc2019-04-09 14:21:04 +010014#include "tfm_crypto_defs.h"
David Hu49a28eb2019-08-14 18:18:15 +080015#include "tfm_memory_utils.h"
Antonio de Angelis8908f472018-08-31 15:44:25 +010016
17/**
Antonio de Angelis819c2f32019-02-06 14:32:02 +000018 * \def TFM_CRYPTO_CONC_OPER_NUM
19 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +010020 * \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 Angelis8908f472018-08-31 15:44:25 +010023 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +010024#ifndef TFM_CRYPTO_CONC_OPER_NUM
Antonio de Angelis8908f472018-08-31 15:44:25 +010025#define TFM_CRYPTO_CONC_OPER_NUM (8)
Jamie Fox0e54ebc2019-04-09 14:21:04 +010026#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +010027
Antonio de Angelis8908f472018-08-31 15:44:25 +010028struct tfm_crypto_operation_s {
29 uint32_t in_use; /*!< Indicates if the operation is in use */
Antonio de Angelis60a6fe62019-06-18 15:27:34 +010030 int32_t owner; /*!< Indicates an ID of the owner of
31 * the context
32 */
Antonio de Angelis8908f472018-08-31 15:44:25 +010033 enum tfm_crypto_operation_type type; /*!< Type of the operation */
34 union {
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010035 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 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +010038 psa_key_derivation_operation_t key_deriv; /*!< Key derivation operation context */
Antonio de Angelisc26af632021-10-07 15:04:12 +010039 psa_aead_operation_t aead; /*!< AEAD operation context */
Antonio de Angelis8908f472018-08-31 15:44:25 +010040 } operation;
41};
42
43static struct tfm_crypto_operation_s operation[TFM_CRYPTO_CONC_OPER_NUM] ={{0}};
44
Antonio de Angelis819c2f32019-02-06 14:32:02 +000045/*
46 * \brief Function used to clear the memory associated to a backend context
47 *
48 * \param[in] index Numerical index in the database of the backend contexts
49 *
50 * \return None
51 *
52 */
Antonio de Angelis8908f472018-08-31 15:44:25 +010053static void memset_operation_context(uint32_t index)
54{
Antonio de Angelis819c2f32019-02-06 14:32:02 +000055 uint32_t mem_size;
Antonio de Angelis8908f472018-08-31 15:44:25 +010056
Antonio de Angelis819c2f32019-02-06 14:32:02 +000057 uint8_t *mem_ptr = (uint8_t *) &(operation[index].operation);
Antonio de Angelis8908f472018-08-31 15:44:25 +010058
59 switch(operation[index].type) {
60 case TFM_CRYPTO_CIPHER_OPERATION:
Jamie Fox0e54ebc2019-04-09 14:21:04 +010061 mem_size = sizeof(psa_cipher_operation_t);
Antonio de Angelis8908f472018-08-31 15:44:25 +010062 break;
63 case TFM_CRYPTO_MAC_OPERATION:
Jamie Fox0e54ebc2019-04-09 14:21:04 +010064 mem_size = sizeof(psa_mac_operation_t);
Antonio de Angelis8908f472018-08-31 15:44:25 +010065 break;
66 case TFM_CRYPTO_HASH_OPERATION:
Jamie Fox0e54ebc2019-04-09 14:21:04 +010067 mem_size = sizeof(psa_hash_operation_t);
Antonio de Angelis8908f472018-08-31 15:44:25 +010068 break;
Antonio de Angelis04debbd2019-10-14 12:12:52 +010069 case TFM_CRYPTO_KEY_DERIVATION_OPERATION:
70 mem_size = sizeof(psa_key_derivation_operation_t);
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010071 break;
Antonio de Angelisc26af632021-10-07 15:04:12 +010072 case TFM_CRYPTO_AEAD_OPERATION:
73 mem_size = sizeof(psa_aead_operation_t);
74 break;
Antonio de Angelis8908f472018-08-31 15:44:25 +010075 case TFM_CRYPTO_OPERATION_NONE:
76 default:
77 mem_size = 0;
78 break;
79 }
80
Antonio de Angelis819c2f32019-02-06 14:32:02 +000081 /* Clear the contents of the backend context */
82 (void)tfm_memset(mem_ptr, 0, mem_size);
83}
84
Antonio de Angelis8908f472018-08-31 15:44:25 +010085/*!
86 * \defgroup public Public functions
87 *
88 */
89
90/*!@{*/
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000091psa_status_t tfm_crypto_init_alloc(void)
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010092{
93 /* Clear the contents of the local contexts */
Antonio de Angelis819c2f32019-02-06 14:32:02 +000094 (void)tfm_memset(operation, 0, sizeof(operation));
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000095 return PSA_SUCCESS;
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010096}
97
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000098psa_status_t tfm_crypto_operation_alloc(enum tfm_crypto_operation_type type,
Antonio de Angelis4743e672019-04-11 11:38:48 +010099 uint32_t *handle,
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000100 void **ctx)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100101{
Antonio de Angelis4743e672019-04-11 11:38:48 +0100102 uint32_t i = 0;
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100103 int32_t partition_id = 0;
104 psa_status_t status;
105
106 status = tfm_crypto_get_caller_id(&partition_id);
107 if (status != PSA_SUCCESS) {
108 return status;
109 }
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000110
Jamie Fox707caf72019-05-29 15:14:18 +0100111 /* Handle must be initialised before calling a setup function */
112 if (*handle != TFM_CRYPTO_INVALID_HANDLE) {
Antonio de Angelis5cbaf322022-01-12 11:55:54 +0000113 if ((*handle <= TFM_CRYPTO_CONC_OPER_NUM) &&
114 (operation[*handle - 1].in_use == TFM_CRYPTO_IN_USE) &&
115 (operation[*handle - 1].owner == partition_id)) {
116 /* The handle is a valid one for already in progress operation */
117 return PSA_ERROR_BAD_STATE;
118 }
119
Antonio de Angelisdab11b42022-01-10 14:21:04 +0000120 return PSA_ERROR_INVALID_HANDLE;
Jamie Fox707caf72019-05-29 15:14:18 +0100121 }
122
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000123 /* Init to invalid values */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000124 if (ctx == NULL) {
125 return PSA_ERROR_INVALID_ARGUMENT;
126 }
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000127 *ctx = NULL;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100128
129 for (i=0; i<TFM_CRYPTO_CONC_OPER_NUM; i++) {
130 if (operation[i].in_use == TFM_CRYPTO_NOT_IN_USE) {
131 operation[i].in_use = TFM_CRYPTO_IN_USE;
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100132 operation[i].owner = partition_id;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100133 operation[i].type = type;
Jamie Fox707caf72019-05-29 15:14:18 +0100134 *handle = i + 1;
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000135 *ctx = (void *) &(operation[i].operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000136 return PSA_SUCCESS;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100137 }
138 }
Antonio de Angelis8908f472018-08-31 15:44:25 +0100139
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000140 return PSA_ERROR_NOT_PERMITTED;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100141}
142
Antonio de Angelis4743e672019-04-11 11:38:48 +0100143psa_status_t tfm_crypto_operation_release(uint32_t *handle)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100144{
Antonio de Angelis4743e672019-04-11 11:38:48 +0100145 uint32_t h_val = *handle;
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100146 int32_t partition_id = 0;
147 psa_status_t status;
148
149 status = tfm_crypto_get_caller_id(&partition_id);
150 if (status != PSA_SUCCESS) {
151 return status;
152 }
Antonio de Angelis8908f472018-08-31 15:44:25 +0100153
Antonio de Angelis4743e672019-04-11 11:38:48 +0100154 if ( (h_val != TFM_CRYPTO_INVALID_HANDLE) &&
Jamie Fox707caf72019-05-29 15:14:18 +0100155 (h_val <= TFM_CRYPTO_CONC_OPER_NUM) &&
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100156 (operation[h_val - 1].in_use == TFM_CRYPTO_IN_USE) &&
157 (operation[h_val - 1].owner == partition_id)) {
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100158
Jamie Fox707caf72019-05-29 15:14:18 +0100159 memset_operation_context(h_val - 1);
160 operation[h_val - 1].in_use = TFM_CRYPTO_NOT_IN_USE;
161 operation[h_val - 1].type = TFM_CRYPTO_OPERATION_NONE;
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100162 operation[h_val - 1].owner = 0;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100163 *handle = TFM_CRYPTO_INVALID_HANDLE;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000164 return PSA_SUCCESS;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100165 }
166
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000167 return PSA_ERROR_INVALID_ARGUMENT;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100168}
169
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000170psa_status_t tfm_crypto_operation_lookup(enum tfm_crypto_operation_type type,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100171 uint32_t handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000172 void **ctx)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100173{
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100174 int32_t partition_id = 0;
175 psa_status_t status;
176
177 status = tfm_crypto_get_caller_id(&partition_id);
178 if (status != PSA_SUCCESS) {
179 return status;
180 }
181
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000182 if ( (handle != TFM_CRYPTO_INVALID_HANDLE) &&
Jamie Fox707caf72019-05-29 15:14:18 +0100183 (handle <= TFM_CRYPTO_CONC_OPER_NUM) &&
184 (operation[handle - 1].in_use == TFM_CRYPTO_IN_USE) &&
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100185 (operation[handle - 1].type == type) &&
186 (operation[handle - 1].owner == partition_id)) {
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100187
Jamie Fox707caf72019-05-29 15:14:18 +0100188 *ctx = (void *) &(operation[handle - 1].operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000189 return PSA_SUCCESS;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100190 }
191
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000192 return PSA_ERROR_BAD_STATE;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100193}
194/*!@}*/