blob: 119e664c958e62b7586ffcf1bf9be81da11b7945 [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"
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010016#include "secure_fw/core/secure_utilities.h"
Antonio de Angelis8908f472018-08-31 15:44:25 +010017
18/**
19 * \brief This value defines the maximum number of simultaneous operations
20 * supported by this implementation.
21 */
22#define TFM_CRYPTO_CONC_OPER_NUM (8)
23
24/**
25 * \brief This value is used to mark an handle as invalid.
26 *
27 */
28#define INVALID_HANDLE (0xFFFFFFFF)
29
30struct tfm_crypto_operation_s {
31 uint32_t in_use; /*!< Indicates if the operation is in use */
32 enum tfm_crypto_operation_type type; /*!< Type of the operation */
33 union {
Antonio de Angelis377a1552018-11-22 17:02:40 +000034 struct tfm_cipher_operation_s cipher; /*!< Cipher operation context */
35 struct tfm_mac_operation_s mac; /*!< MAC operation context */
36 struct tfm_hash_operation_s hash; /*!< Hash operation context */
Antonio de Angelis8908f472018-08-31 15:44:25 +010037 } operation;
38};
39
40static struct tfm_crypto_operation_s operation[TFM_CRYPTO_CONC_OPER_NUM] ={{0}};
41
42static void memset_operation_context(uint32_t index)
43{
44 uint32_t i, mem_size;
45
46 volatile uint8_t *mem_ptr = (uint8_t *) &(operation[index].operation);
47
48 switch(operation[index].type) {
49 case TFM_CRYPTO_CIPHER_OPERATION:
Antonio de Angelis377a1552018-11-22 17:02:40 +000050 mem_size = sizeof(struct tfm_cipher_operation_s);
Antonio de Angelis8908f472018-08-31 15:44:25 +010051 break;
52 case TFM_CRYPTO_MAC_OPERATION:
Antonio de Angelis377a1552018-11-22 17:02:40 +000053 mem_size = sizeof(struct tfm_mac_operation_s);
Antonio de Angelis8908f472018-08-31 15:44:25 +010054 break;
55 case TFM_CRYPTO_HASH_OPERATION:
Antonio de Angelis377a1552018-11-22 17:02:40 +000056 mem_size = sizeof(struct tfm_hash_operation_s);
Antonio de Angelis8908f472018-08-31 15:44:25 +010057 break;
58 case TFM_CRYPTO_OPERATION_NONE:
59 default:
60 mem_size = 0;
61 break;
62 }
63
64 for (i=0; i<mem_size; i++) {
65 mem_ptr[i] = 0;
66 }
67}
68
69/*!
70 * \defgroup public Public functions
71 *
72 */
73
74/*!@{*/
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010075enum tfm_crypto_err_t tfm_crypto_init_alloc(void)
76{
77 /* Clear the contents of the local contexts */
78 tfm_memset(operation, 0, sizeof(operation));
79 return TFM_CRYPTO_ERR_PSA_SUCCESS;
80}
81
Antonio de Angelis8908f472018-08-31 15:44:25 +010082enum tfm_crypto_err_t tfm_crypto_operation_alloc(
83 enum tfm_crypto_operation_type type,
84 uint32_t *handle)
85{
86 uint32_t i = 0;
87
88 for (i=0; i<TFM_CRYPTO_CONC_OPER_NUM; i++) {
89 if (operation[i].in_use == TFM_CRYPTO_NOT_IN_USE) {
90 operation[i].in_use = TFM_CRYPTO_IN_USE;
91 operation[i].type = type;
92 *handle = i;
93 return TFM_CRYPTO_ERR_PSA_SUCCESS;
94 }
95 }
Antonio de Angelis377a1552018-11-22 17:02:40 +000096 *handle = INVALID_HANDLE;
Antonio de Angelis8908f472018-08-31 15:44:25 +010097
98 return TFM_CRYPTO_ERR_PSA_ERROR_NOT_PERMITTED;
99}
100
101enum tfm_crypto_err_t tfm_crypto_operation_release(uint32_t *handle)
102{
103 uint32_t i = *handle;
104
105 if ( (i<TFM_CRYPTO_CONC_OPER_NUM) &&
106 (operation[i].in_use == TFM_CRYPTO_IN_USE) ) {
107 memset_operation_context(i);
108 operation[i].in_use = TFM_CRYPTO_NOT_IN_USE;
109 operation[i].type = TFM_CRYPTO_OPERATION_NONE;
110 *handle = INVALID_HANDLE;
111 return TFM_CRYPTO_ERR_PSA_SUCCESS;
112 }
113
114 return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT;
115}
116
117enum tfm_crypto_err_t tfm_crypto_operation_lookup(
118 enum tfm_crypto_operation_type type,
Antonio de Angelis377a1552018-11-22 17:02:40 +0000119 uint32_t handle,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100120 void **oper)
121{
Antonio de Angelis377a1552018-11-22 17:02:40 +0000122 if ( (handle<TFM_CRYPTO_CONC_OPER_NUM) &&
123 (operation[handle].in_use == TFM_CRYPTO_IN_USE) &&
124 (operation[handle].type == type) ) {
125 *oper = (void *) &(operation[handle].operation);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100126 return TFM_CRYPTO_ERR_PSA_SUCCESS;
127 }
128
129 return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT;
130}
131/*!@}*/