blob: 9726fa0a1fb551576ce43d66424d04b1d88bfa6f [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
Antonio de Angelis8908f472018-08-31 15:44:25 +010012#include "psa_crypto.h"
13#include "tfm_crypto_api.h"
14
Antonio de Angelis377a1552018-11-22 17:02:40 +000015#include "tfm_crypto_struct.h"
Tamas Ban8bd24b72019-02-19 12:13:13 +000016#include "secure_fw/core/tfm_memory_utils.h"
Antonio de Angelis8908f472018-08-31 15:44:25 +010017
18/**
Antonio de Angelis819c2f32019-02-06 14:32:02 +000019 * \def TFM_CRYPTO_CONC_OPER_NUM
20 *
Antonio de Angelis8908f472018-08-31 15:44:25 +010021 * \brief This value defines the maximum number of simultaneous operations
22 * supported by this implementation.
23 */
24#define TFM_CRYPTO_CONC_OPER_NUM (8)
25
Antonio de Angelis8908f472018-08-31 15:44:25 +010026struct tfm_crypto_operation_s {
27 uint32_t in_use; /*!< Indicates if the operation is in use */
28 enum tfm_crypto_operation_type type; /*!< Type of the operation */
29 union {
Antonio de Angelis377a1552018-11-22 17:02:40 +000030 struct tfm_cipher_operation_s cipher; /*!< Cipher operation context */
31 struct tfm_mac_operation_s mac; /*!< MAC operation context */
32 struct tfm_hash_operation_s hash; /*!< Hash operation context */
Antonio de Angelis8908f472018-08-31 15:44:25 +010033 } operation;
34};
35
36static struct tfm_crypto_operation_s operation[TFM_CRYPTO_CONC_OPER_NUM] ={{0}};
37
Antonio de Angelis819c2f32019-02-06 14:32:02 +000038/*
39 * \brief Function used to clear the memory associated to a backend context
40 *
41 * \param[in] index Numerical index in the database of the backend contexts
42 *
43 * \return None
44 *
45 */
Antonio de Angelis8908f472018-08-31 15:44:25 +010046static void memset_operation_context(uint32_t index)
47{
Antonio de Angelis819c2f32019-02-06 14:32:02 +000048 uint32_t mem_size;
Antonio de Angelis8908f472018-08-31 15:44:25 +010049
Antonio de Angelis819c2f32019-02-06 14:32:02 +000050 uint8_t *mem_ptr = (uint8_t *) &(operation[index].operation);
Antonio de Angelis8908f472018-08-31 15:44:25 +010051
52 switch(operation[index].type) {
53 case TFM_CRYPTO_CIPHER_OPERATION:
Antonio de Angelis377a1552018-11-22 17:02:40 +000054 mem_size = sizeof(struct tfm_cipher_operation_s);
Antonio de Angelis8908f472018-08-31 15:44:25 +010055 break;
56 case TFM_CRYPTO_MAC_OPERATION:
Antonio de Angelis377a1552018-11-22 17:02:40 +000057 mem_size = sizeof(struct tfm_mac_operation_s);
Antonio de Angelis8908f472018-08-31 15:44:25 +010058 break;
59 case TFM_CRYPTO_HASH_OPERATION:
Antonio de Angelis377a1552018-11-22 17:02:40 +000060 mem_size = sizeof(struct tfm_hash_operation_s);
Antonio de Angelis8908f472018-08-31 15:44:25 +010061 break;
62 case TFM_CRYPTO_OPERATION_NONE:
63 default:
64 mem_size = 0;
65 break;
66 }
67
Antonio de Angelis819c2f32019-02-06 14:32:02 +000068 /* Clear the contents of the backend context */
69 (void)tfm_memset(mem_ptr, 0, mem_size);
70}
71
Antonio de Angelis8908f472018-08-31 15:44:25 +010072/*!
73 * \defgroup public Public functions
74 *
75 */
76
77/*!@{*/
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000078psa_status_t tfm_crypto_init_alloc(void)
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010079{
80 /* Clear the contents of the local contexts */
Antonio de Angelis819c2f32019-02-06 14:32:02 +000081 (void)tfm_memset(operation, 0, sizeof(operation));
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000082 return PSA_SUCCESS;
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010083}
84
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000085psa_status_t tfm_crypto_operation_alloc(enum tfm_crypto_operation_type type,
Antonio de Angelis4743e672019-04-11 11:38:48 +010086 uint32_t *handle,
Antonio de Angelis819c2f32019-02-06 14:32:02 +000087 void **ctx)
Antonio de Angelis8908f472018-08-31 15:44:25 +010088{
Antonio de Angelis4743e672019-04-11 11:38:48 +010089 uint32_t i = 0;
Antonio de Angelis819c2f32019-02-06 14:32:02 +000090
91 /* Init to invalid values */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000092 if (ctx == NULL) {
93 return PSA_ERROR_INVALID_ARGUMENT;
94 }
Antonio de Angelis819c2f32019-02-06 14:32:02 +000095 *ctx = NULL;
Antonio de Angelis8908f472018-08-31 15:44:25 +010096
97 for (i=0; i<TFM_CRYPTO_CONC_OPER_NUM; i++) {
98 if (operation[i].in_use == TFM_CRYPTO_NOT_IN_USE) {
99 operation[i].in_use = TFM_CRYPTO_IN_USE;
100 operation[i].type = type;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100101 *handle = i;
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000102 *ctx = (void *) &(operation[i].operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000103 return PSA_SUCCESS;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100104 }
105 }
Antonio de Angelis8908f472018-08-31 15:44:25 +0100106
Antonio de Angelis4743e672019-04-11 11:38:48 +0100107 *handle = TFM_CRYPTO_INVALID_HANDLE;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000108 return PSA_ERROR_NOT_PERMITTED;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100109}
110
Antonio de Angelis4743e672019-04-11 11:38:48 +0100111psa_status_t tfm_crypto_operation_release(uint32_t *handle)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100112{
Antonio de Angelis4743e672019-04-11 11:38:48 +0100113 uint32_t h_val = *handle;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100114
Antonio de Angelis4743e672019-04-11 11:38:48 +0100115 if ( (h_val != TFM_CRYPTO_INVALID_HANDLE) &&
116 (h_val < TFM_CRYPTO_CONC_OPER_NUM) &&
117 (operation[h_val].in_use == TFM_CRYPTO_IN_USE) ) {
118 memset_operation_context(h_val);
119 operation[h_val].in_use = TFM_CRYPTO_NOT_IN_USE;
120 operation[h_val].type = TFM_CRYPTO_OPERATION_NONE;
121 *handle = TFM_CRYPTO_INVALID_HANDLE;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000122 return PSA_SUCCESS;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100123 }
124
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000125 return PSA_ERROR_INVALID_ARGUMENT;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100126}
127
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000128psa_status_t tfm_crypto_operation_lookup(enum tfm_crypto_operation_type type,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100129 uint32_t handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000130 void **ctx)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100131{
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000132 if ( (handle != TFM_CRYPTO_INVALID_HANDLE) &&
133 (handle < TFM_CRYPTO_CONC_OPER_NUM) &&
Antonio de Angelis377a1552018-11-22 17:02:40 +0000134 (operation[handle].in_use == TFM_CRYPTO_IN_USE) &&
135 (operation[handle].type == type) ) {
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000136 *ctx = (void *) &(operation[handle].operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000137 return PSA_SUCCESS;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100138 }
139
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000140 return PSA_ERROR_BAD_STATE;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100141}
142/*!@}*/