blob: 1036a67eaacd867d616b2037f10e945ebf824e18 [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 Angelis8908f472018-08-31 15:44:25 +010039 } operation;
40};
41
42static struct tfm_crypto_operation_s operation[TFM_CRYPTO_CONC_OPER_NUM] ={{0}};
43
Antonio de Angelis819c2f32019-02-06 14:32:02 +000044/*
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 Angelis8908f472018-08-31 15:44:25 +010052static void memset_operation_context(uint32_t index)
53{
Antonio de Angelis819c2f32019-02-06 14:32:02 +000054 uint32_t mem_size;
Antonio de Angelis8908f472018-08-31 15:44:25 +010055
Antonio de Angelis819c2f32019-02-06 14:32:02 +000056 uint8_t *mem_ptr = (uint8_t *) &(operation[index].operation);
Antonio de Angelis8908f472018-08-31 15:44:25 +010057
58 switch(operation[index].type) {
59 case TFM_CRYPTO_CIPHER_OPERATION:
Jamie Fox0e54ebc2019-04-09 14:21:04 +010060 mem_size = sizeof(psa_cipher_operation_t);
Antonio de Angelis8908f472018-08-31 15:44:25 +010061 break;
62 case TFM_CRYPTO_MAC_OPERATION:
Jamie Fox0e54ebc2019-04-09 14:21:04 +010063 mem_size = sizeof(psa_mac_operation_t);
Antonio de Angelis8908f472018-08-31 15:44:25 +010064 break;
65 case TFM_CRYPTO_HASH_OPERATION:
Jamie Fox0e54ebc2019-04-09 14:21:04 +010066 mem_size = sizeof(psa_hash_operation_t);
Antonio de Angelis8908f472018-08-31 15:44:25 +010067 break;
Antonio de Angelis04debbd2019-10-14 12:12:52 +010068 case TFM_CRYPTO_KEY_DERIVATION_OPERATION:
69 mem_size = sizeof(psa_key_derivation_operation_t);
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010070 break;
Antonio de Angelis8908f472018-08-31 15:44:25 +010071 case TFM_CRYPTO_OPERATION_NONE:
72 default:
73 mem_size = 0;
74 break;
75 }
76
Antonio de Angelis819c2f32019-02-06 14:32:02 +000077 /* Clear the contents of the backend context */
78 (void)tfm_memset(mem_ptr, 0, mem_size);
79}
80
Antonio de Angelis8908f472018-08-31 15:44:25 +010081/*!
82 * \defgroup public Public functions
83 *
84 */
85
86/*!@{*/
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000087psa_status_t tfm_crypto_init_alloc(void)
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010088{
89 /* Clear the contents of the local contexts */
Antonio de Angelis819c2f32019-02-06 14:32:02 +000090 (void)tfm_memset(operation, 0, sizeof(operation));
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000091 return PSA_SUCCESS;
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010092}
93
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000094psa_status_t tfm_crypto_operation_alloc(enum tfm_crypto_operation_type type,
Antonio de Angelis4743e672019-04-11 11:38:48 +010095 uint32_t *handle,
Antonio de Angelis819c2f32019-02-06 14:32:02 +000096 void **ctx)
Antonio de Angelis8908f472018-08-31 15:44:25 +010097{
Antonio de Angelis4743e672019-04-11 11:38:48 +010098 uint32_t i = 0;
Antonio de Angelis60a6fe62019-06-18 15:27:34 +010099 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 Angelis819c2f32019-02-06 14:32:02 +0000106
Jamie Fox707caf72019-05-29 15:14:18 +0100107 /* Handle must be initialised before calling a setup function */
108 if (*handle != TFM_CRYPTO_INVALID_HANDLE) {
Antonio de Angelis5cbaf322022-01-12 11:55:54 +0000109 if ((*handle <= TFM_CRYPTO_CONC_OPER_NUM) &&
110 (operation[*handle - 1].in_use == TFM_CRYPTO_IN_USE) &&
111 (operation[*handle - 1].owner == partition_id)) {
112 /* The handle is a valid one for already in progress operation */
113 return PSA_ERROR_BAD_STATE;
114 }
115
Antonio de Angelisdab11b42022-01-10 14:21:04 +0000116 return PSA_ERROR_INVALID_HANDLE;
Jamie Fox707caf72019-05-29 15:14:18 +0100117 }
118
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000119 /* Init to invalid values */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000120 if (ctx == NULL) {
121 return PSA_ERROR_INVALID_ARGUMENT;
122 }
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000123 *ctx = NULL;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100124
125 for (i=0; i<TFM_CRYPTO_CONC_OPER_NUM; i++) {
126 if (operation[i].in_use == TFM_CRYPTO_NOT_IN_USE) {
127 operation[i].in_use = TFM_CRYPTO_IN_USE;
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100128 operation[i].owner = partition_id;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100129 operation[i].type = type;
Jamie Fox707caf72019-05-29 15:14:18 +0100130 *handle = i + 1;
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000131 *ctx = (void *) &(operation[i].operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000132 return PSA_SUCCESS;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100133 }
134 }
Antonio de Angelis8908f472018-08-31 15:44:25 +0100135
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000136 return PSA_ERROR_NOT_PERMITTED;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100137}
138
Antonio de Angelis4743e672019-04-11 11:38:48 +0100139psa_status_t tfm_crypto_operation_release(uint32_t *handle)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100140{
Antonio de Angelis4743e672019-04-11 11:38:48 +0100141 uint32_t h_val = *handle;
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100142 int32_t partition_id = 0;
143 psa_status_t status;
144
145 status = tfm_crypto_get_caller_id(&partition_id);
146 if (status != PSA_SUCCESS) {
147 return status;
148 }
Antonio de Angelis8908f472018-08-31 15:44:25 +0100149
Antonio de Angelis4743e672019-04-11 11:38:48 +0100150 if ( (h_val != TFM_CRYPTO_INVALID_HANDLE) &&
Jamie Fox707caf72019-05-29 15:14:18 +0100151 (h_val <= TFM_CRYPTO_CONC_OPER_NUM) &&
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100152 (operation[h_val - 1].in_use == TFM_CRYPTO_IN_USE) &&
153 (operation[h_val - 1].owner == partition_id)) {
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100154
Jamie Fox707caf72019-05-29 15:14:18 +0100155 memset_operation_context(h_val - 1);
156 operation[h_val - 1].in_use = TFM_CRYPTO_NOT_IN_USE;
157 operation[h_val - 1].type = TFM_CRYPTO_OPERATION_NONE;
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100158 operation[h_val - 1].owner = 0;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100159 *handle = TFM_CRYPTO_INVALID_HANDLE;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000160 return PSA_SUCCESS;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100161 }
162
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000163 return PSA_ERROR_INVALID_ARGUMENT;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100164}
165
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000166psa_status_t tfm_crypto_operation_lookup(enum tfm_crypto_operation_type type,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100167 uint32_t handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000168 void **ctx)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100169{
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100170 int32_t partition_id = 0;
171 psa_status_t status;
172
173 status = tfm_crypto_get_caller_id(&partition_id);
174 if (status != PSA_SUCCESS) {
175 return status;
176 }
177
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000178 if ( (handle != TFM_CRYPTO_INVALID_HANDLE) &&
Jamie Fox707caf72019-05-29 15:14:18 +0100179 (handle <= TFM_CRYPTO_CONC_OPER_NUM) &&
180 (operation[handle - 1].in_use == TFM_CRYPTO_IN_USE) &&
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100181 (operation[handle - 1].type == type) &&
182 (operation[handle - 1].owner == partition_id)) {
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100183
Jamie Fox707caf72019-05-29 15:14:18 +0100184 *ctx = (void *) &(operation[handle - 1].operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000185 return PSA_SUCCESS;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100186 }
187
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000188 return PSA_ERROR_BAD_STATE;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100189}
190/*!@}*/