TF-M Reference Manual  1.2.0
TrustedFirmware-M
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
crypto_alloc.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #include "tfm_mbedcrypto_include.h"
12 
13 #include "tfm_crypto_api.h"
14 #include "tfm_crypto_defs.h"
15 #include "tfm_memory_utils.h"
16 
24 #ifndef TFM_CRYPTO_CONC_OPER_NUM
25 #define TFM_CRYPTO_CONC_OPER_NUM (8)
26 #endif
27 
29  uint32_t in_use;
30  int32_t owner;
34  union {
39  } operation;
40 };
41 
43 
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  */
52 static void memset_operation_context(uint32_t index)
53 {
54  uint32_t mem_size;
55 
56  uint8_t *mem_ptr = (uint8_t *) &(operation[index].operation);
57 
58  switch(operation[index].type) {
60  mem_size = sizeof(psa_cipher_operation_t);
61  break;
63  mem_size = sizeof(psa_mac_operation_t);
64  break;
66  mem_size = sizeof(psa_hash_operation_t);
67  break;
69  mem_size = sizeof(psa_key_derivation_operation_t);
70  break;
72  default:
73  mem_size = 0;
74  break;
75  }
76 
77  /* Clear the contents of the backend context */
78  (void)tfm_memset(mem_ptr, 0, mem_size);
79 }
80 
88 {
89  /* Clear the contents of the local contexts */
90  (void)tfm_memset(operation, 0, sizeof(operation));
91  return PSA_SUCCESS;
92 }
93 
95  uint32_t *handle,
96  void **ctx)
97 {
98  uint32_t i = 0;
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  }
106 
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 
112  /* Init to invalid values */
113  if (ctx == NULL) {
115  }
116  *ctx = NULL;
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;
121  operation[i].owner = partition_id;
122  operation[i].type = type;
123  *handle = i + 1;
124  *ctx = (void *) &(operation[i].operation);
125  return PSA_SUCCESS;
126  }
127  }
128 
130 }
131 
133 {
134  uint32_t h_val = *handle;
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  }
142 
143  if ( (h_val != TFM_CRYPTO_INVALID_HANDLE) &&
144  (h_val <= TFM_CRYPTO_CONC_OPER_NUM) &&
145  (operation[h_val - 1].in_use == TFM_CRYPTO_IN_USE) &&
146  (operation[h_val - 1].owner == partition_id)) {
147 
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;
151  operation[h_val - 1].owner = 0;
152  *handle = TFM_CRYPTO_INVALID_HANDLE;
153  return PSA_SUCCESS;
154  }
155 
157 }
158 
160  uint32_t handle,
161  void **ctx)
162 {
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 
171  if ( (handle != TFM_CRYPTO_INVALID_HANDLE) &&
172  (handle <= TFM_CRYPTO_CONC_OPER_NUM) &&
173  (operation[handle - 1].in_use == TFM_CRYPTO_IN_USE) &&
174  (operation[handle - 1].type == type) &&
175  (operation[handle - 1].owner == partition_id)) {
176 
177  *ctx = (void *) &(operation[handle - 1].operation);
178  return PSA_SUCCESS;
179  }
180 
181  return PSA_ERROR_BAD_STATE;
182 }
__STATIC_INLINE void * tfm_memset(void *ptr, int value, size_t num)
#define PSA_SUCCESS
Definition: crypto_values.h:35
psa_status_t tfm_crypto_operation_alloc(enum tfm_crypto_operation_type type, uint32_t *handle, void **ctx)
Allocate an operation context in the backend.
Definition: crypto_alloc.c:94
#define TFM_CRYPTO_INVALID_HANDLE
This value is used to mark an handle as invalid.
psa_mac_operation_t mac
Definition: crypto_alloc.c:36
psa_status_t tfm_crypto_get_caller_id(int32_t *id)
Returns the ID of the caller.
Definition: crypto_init.c:314
psa_status_t tfm_crypto_operation_release(uint32_t *handle)
Release an operation context in the backend.
Definition: crypto_alloc.c:132
#define PSA_ERROR_INVALID_ARGUMENT
#define PSA_ERROR_NOT_PERMITTED
Definition: crypto_values.h:65
struct psa_hash_operation_s psa_hash_operation_t
Definition: crypto.h:954
union tfm_crypto_operation_s::@8 operation
struct psa_key_derivation_s psa_key_derivation_operation_t
Definition: crypto.h:3095
struct psa_cipher_operation_s psa_cipher_operation_t
Definition: crypto.h:1745
psa_status_t tfm_crypto_init_alloc(void)
Initialise the Alloc module.
Definition: crypto_alloc.c:87
psa_cipher_operation_t cipher
Definition: crypto_alloc.c:35
tfm_crypto_operation_type
List of possible operation types supported by the TFM based implementation. This type is needed by th...
#define PSA_ERROR_BAD_STATE
psa_hash_operation_t hash
Definition: crypto_alloc.c:37
psa_key_derivation_operation_t key_deriv
Definition: crypto_alloc.c:38
psa_status_t tfm_crypto_operation_lookup(enum tfm_crypto_operation_type type, uint32_t handle, void **ctx)
Look up an operation context in the backend for the corresponding frontend operation.
Definition: crypto_alloc.c:159
int32_t psa_status_t
Function return status.
Definition: crypto_types.h:43
struct psa_mac_operation_s psa_mac_operation_t
Definition: crypto.h:1322
#define TFM_CRYPTO_CONC_OPER_NUM
This is the default value for the maximum number of concurrent operations that can be active (allocat...
Definition: crypto_alloc.c:25
enum tfm_crypto_operation_type type
Definition: crypto_alloc.c:33