blob: c7ffec34187bc764d342a7b347c1ae2a2a5c09ce [file] [log] [blame]
Antonio de Angelis8908f472018-08-31 15:44:25 +01001/*
2 * Copyright (c) 2018, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#include <limits.h>
9
10#include "tfm_crypto_defs.h"
11
12/* Pre include Mbed TLS headers */
13#define LIB_PREFIX_NAME __tfm_crypto__
14#include "mbedtls_global_symbols.h"
15
16/* Include the Mbed TLS configuration file, the way Mbed TLS does it
17 * in each of its header files.
18 */
19#if !defined(MBEDTLS_CONFIG_FILE)
20#include "platform/ext/common/tfm_mbedtls_config.h"
21#else
22#include MBEDTLS_CONFIG_FILE
23#endif
24
25#include "psa_crypto.h"
26#include "tfm_crypto_api.h"
27
28/* The file "psa_crypto_struct.h" contains definitions for
29 * implementation-specific structs that are declared in "psa_crypto.h".
30 */
31#include "psa_crypto_struct.h"
32
33/**
34 * \brief This value defines the maximum number of simultaneous operations
35 * supported by this implementation.
36 */
37#define TFM_CRYPTO_CONC_OPER_NUM (8)
38
39/**
40 * \brief This value is used to mark an handle as invalid.
41 *
42 */
43#define INVALID_HANDLE (0xFFFFFFFF)
44
45struct tfm_crypto_operation_s {
46 uint32_t in_use; /*!< Indicates if the operation is in use */
47 enum tfm_crypto_operation_type type; /*!< Type of the operation */
48 union {
49 struct psa_cipher_operation_s cipher; /*!< Cipher operation context */
50 struct psa_mac_operation_s mac; /*!< MAC operation context */
51 struct psa_hash_operation_s hash; /*!< Hash operation context */
52 } operation;
53};
54
55static struct tfm_crypto_operation_s operation[TFM_CRYPTO_CONC_OPER_NUM] ={{0}};
56
57static void memset_operation_context(uint32_t index)
58{
59 uint32_t i, mem_size;
60
61 volatile uint8_t *mem_ptr = (uint8_t *) &(operation[index].operation);
62
63 switch(operation[index].type) {
64 case TFM_CRYPTO_CIPHER_OPERATION:
65 mem_size = sizeof(struct psa_cipher_operation_s);
66 break;
67 case TFM_CRYPTO_MAC_OPERATION:
68 mem_size = sizeof(struct psa_mac_operation_s);
69 break;
70 case TFM_CRYPTO_HASH_OPERATION:
71 mem_size = sizeof(struct psa_hash_operation_s);
72 break;
73 case TFM_CRYPTO_OPERATION_NONE:
74 default:
75 mem_size = 0;
76 break;
77 }
78
79 for (i=0; i<mem_size; i++) {
80 mem_ptr[i] = 0;
81 }
82}
83
84/*!
85 * \defgroup public Public functions
86 *
87 */
88
89/*!@{*/
90enum tfm_crypto_err_t tfm_crypto_operation_alloc(
91 enum tfm_crypto_operation_type type,
92 uint32_t *handle)
93{
94 uint32_t i = 0;
95
96 for (i=0; i<TFM_CRYPTO_CONC_OPER_NUM; i++) {
97 if (operation[i].in_use == TFM_CRYPTO_NOT_IN_USE) {
98 operation[i].in_use = TFM_CRYPTO_IN_USE;
99 operation[i].type = type;
100 *handle = i;
101 return TFM_CRYPTO_ERR_PSA_SUCCESS;
102 }
103 }
104
105
106 return TFM_CRYPTO_ERR_PSA_ERROR_NOT_PERMITTED;
107}
108
109enum tfm_crypto_err_t tfm_crypto_operation_release(uint32_t *handle)
110{
111 uint32_t i = *handle;
112
113 if ( (i<TFM_CRYPTO_CONC_OPER_NUM) &&
114 (operation[i].in_use == TFM_CRYPTO_IN_USE) ) {
115 memset_operation_context(i);
116 operation[i].in_use = TFM_CRYPTO_NOT_IN_USE;
117 operation[i].type = TFM_CRYPTO_OPERATION_NONE;
118 *handle = INVALID_HANDLE;
119 return TFM_CRYPTO_ERR_PSA_SUCCESS;
120 }
121
122 return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT;
123}
124
125enum tfm_crypto_err_t tfm_crypto_operation_lookup(
126 enum tfm_crypto_operation_type type,
127 uint32_t *handle,
128 void **oper)
129{
130 uint32_t i = *handle;
131
132 if ( (i<TFM_CRYPTO_CONC_OPER_NUM) &&
133 (operation[i].in_use == TFM_CRYPTO_IN_USE) &&
134 (operation[i].type == type) ) {
135 *oper = (void *) &(operation[i].operation);
136 return TFM_CRYPTO_ERR_PSA_SUCCESS;
137 }
138
139 return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT;
140}
141/*!@}*/