blob: 5ccd33c3fd2f50b47310151d2950d802730c3e4b [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
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
Antonio de Angelis377a1552018-11-22 17:02:40 +000028#include "tfm_crypto_struct.h"
Antonio de Angelis8908f472018-08-31 15:44:25 +010029
30/**
31 * \brief This value defines the maximum number of simultaneous operations
32 * supported by this implementation.
33 */
34#define TFM_CRYPTO_CONC_OPER_NUM (8)
35
36/**
37 * \brief This value is used to mark an handle as invalid.
38 *
39 */
40#define INVALID_HANDLE (0xFFFFFFFF)
41
42struct tfm_crypto_operation_s {
43 uint32_t in_use; /*!< Indicates if the operation is in use */
44 enum tfm_crypto_operation_type type; /*!< Type of the operation */
45 union {
Antonio de Angelis377a1552018-11-22 17:02:40 +000046 struct tfm_cipher_operation_s cipher; /*!< Cipher operation context */
47 struct tfm_mac_operation_s mac; /*!< MAC operation context */
48 struct tfm_hash_operation_s hash; /*!< Hash operation context */
Antonio de Angelis8908f472018-08-31 15:44:25 +010049 } operation;
50};
51
52static struct tfm_crypto_operation_s operation[TFM_CRYPTO_CONC_OPER_NUM] ={{0}};
53
54static void memset_operation_context(uint32_t index)
55{
56 uint32_t i, mem_size;
57
58 volatile uint8_t *mem_ptr = (uint8_t *) &(operation[index].operation);
59
60 switch(operation[index].type) {
61 case TFM_CRYPTO_CIPHER_OPERATION:
Antonio de Angelis377a1552018-11-22 17:02:40 +000062 mem_size = sizeof(struct tfm_cipher_operation_s);
Antonio de Angelis8908f472018-08-31 15:44:25 +010063 break;
64 case TFM_CRYPTO_MAC_OPERATION:
Antonio de Angelis377a1552018-11-22 17:02:40 +000065 mem_size = sizeof(struct tfm_mac_operation_s);
Antonio de Angelis8908f472018-08-31 15:44:25 +010066 break;
67 case TFM_CRYPTO_HASH_OPERATION:
Antonio de Angelis377a1552018-11-22 17:02:40 +000068 mem_size = sizeof(struct tfm_hash_operation_s);
Antonio de Angelis8908f472018-08-31 15:44:25 +010069 break;
70 case TFM_CRYPTO_OPERATION_NONE:
71 default:
72 mem_size = 0;
73 break;
74 }
75
76 for (i=0; i<mem_size; i++) {
77 mem_ptr[i] = 0;
78 }
79}
80
81/*!
82 * \defgroup public Public functions
83 *
84 */
85
86/*!@{*/
87enum tfm_crypto_err_t tfm_crypto_operation_alloc(
88 enum tfm_crypto_operation_type type,
89 uint32_t *handle)
90{
91 uint32_t i = 0;
92
93 for (i=0; i<TFM_CRYPTO_CONC_OPER_NUM; i++) {
94 if (operation[i].in_use == TFM_CRYPTO_NOT_IN_USE) {
95 operation[i].in_use = TFM_CRYPTO_IN_USE;
96 operation[i].type = type;
97 *handle = i;
98 return TFM_CRYPTO_ERR_PSA_SUCCESS;
99 }
100 }
Antonio de Angelis377a1552018-11-22 17:02:40 +0000101 *handle = INVALID_HANDLE;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100102
103 return TFM_CRYPTO_ERR_PSA_ERROR_NOT_PERMITTED;
104}
105
106enum tfm_crypto_err_t tfm_crypto_operation_release(uint32_t *handle)
107{
108 uint32_t i = *handle;
109
110 if ( (i<TFM_CRYPTO_CONC_OPER_NUM) &&
111 (operation[i].in_use == TFM_CRYPTO_IN_USE) ) {
112 memset_operation_context(i);
113 operation[i].in_use = TFM_CRYPTO_NOT_IN_USE;
114 operation[i].type = TFM_CRYPTO_OPERATION_NONE;
115 *handle = INVALID_HANDLE;
116 return TFM_CRYPTO_ERR_PSA_SUCCESS;
117 }
118
119 return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT;
120}
121
122enum tfm_crypto_err_t tfm_crypto_operation_lookup(
123 enum tfm_crypto_operation_type type,
Antonio de Angelis377a1552018-11-22 17:02:40 +0000124 uint32_t handle,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100125 void **oper)
126{
Antonio de Angelis377a1552018-11-22 17:02:40 +0000127 if ( (handle<TFM_CRYPTO_CONC_OPER_NUM) &&
128 (operation[handle].in_use == TFM_CRYPTO_IN_USE) &&
129 (operation[handle].type == type) ) {
130 *oper = (void *) &(operation[handle].operation);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100131 return TFM_CRYPTO_ERR_PSA_SUCCESS;
132 }
133
134 return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT;
135}
136/*!@}*/