blob: df322ab46e917a691e7ea4f6c76f4561201310ec [file] [log] [blame]
Antonio de Angelis8908f472018-08-31 15:44:25 +01001/*
Antonio de Angelis377a1552018-11-22 17:02:40 +00002 * Copyright (c) 2018-2019, 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 */
38 psa_crypto_generator_t generator; /*!< Generator 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 Angelis25e2b2d2019-04-25 14:49:50 +010068 case TFM_CRYPTO_GENERATOR_OPERATION:
69 mem_size = sizeof(psa_crypto_generator_t);
70 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) {
109 return PSA_ERROR_BAD_STATE;
110 }
111
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000112 /* Init to invalid values */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000113 if (ctx == NULL) {
114 return PSA_ERROR_INVALID_ARGUMENT;
115 }
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000116 *ctx = NULL;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100117
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 Angelis60a6fe62019-06-18 15:27:34 +0100121 operation[i].owner = partition_id;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100122 operation[i].type = type;
Jamie Fox707caf72019-05-29 15:14:18 +0100123 *handle = i + 1;
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000124 *ctx = (void *) &(operation[i].operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000125 return PSA_SUCCESS;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100126 }
127 }
Antonio de Angelis8908f472018-08-31 15:44:25 +0100128
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000129 return PSA_ERROR_NOT_PERMITTED;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100130}
131
Antonio de Angelis4743e672019-04-11 11:38:48 +0100132psa_status_t tfm_crypto_operation_release(uint32_t *handle)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100133{
Antonio de Angelis4743e672019-04-11 11:38:48 +0100134 uint32_t h_val = *handle;
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100135 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 Angelis8908f472018-08-31 15:44:25 +0100142
Antonio de Angelis4743e672019-04-11 11:38:48 +0100143 if ( (h_val != TFM_CRYPTO_INVALID_HANDLE) &&
Jamie Fox707caf72019-05-29 15:14:18 +0100144 (h_val <= TFM_CRYPTO_CONC_OPER_NUM) &&
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100145 (operation[h_val - 1].in_use == TFM_CRYPTO_IN_USE) &&
146 (operation[h_val - 1].owner == partition_id)) {
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100147
Jamie Fox707caf72019-05-29 15:14:18 +0100148 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 Angelis60a6fe62019-06-18 15:27:34 +0100151 operation[h_val - 1].owner = 0;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100152 *handle = TFM_CRYPTO_INVALID_HANDLE;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000153 return PSA_SUCCESS;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100154 }
155
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000156 return PSA_ERROR_INVALID_ARGUMENT;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100157}
158
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000159psa_status_t tfm_crypto_operation_lookup(enum tfm_crypto_operation_type type,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100160 uint32_t handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000161 void **ctx)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100162{
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100163 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 Angelis819c2f32019-02-06 14:32:02 +0000171 if ( (handle != TFM_CRYPTO_INVALID_HANDLE) &&
Jamie Fox707caf72019-05-29 15:14:18 +0100172 (handle <= TFM_CRYPTO_CONC_OPER_NUM) &&
173 (operation[handle - 1].in_use == TFM_CRYPTO_IN_USE) &&
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100174 (operation[handle - 1].type == type) &&
175 (operation[handle - 1].owner == partition_id)) {
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100176
Jamie Fox707caf72019-05-29 15:14:18 +0100177 *ctx = (void *) &(operation[handle - 1].operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000178 return PSA_SUCCESS;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100179 }
180
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000181 return PSA_ERROR_BAD_STATE;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100182}
183/*!@}*/