blob: ef0da17bc79453c61920599a2244f42c4fce753e [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
Jamie Fox0e54ebc2019-04-09 14:21:04 +01008#include <stddef.h>
9#include <stdint.h>
Antonio de Angelis8908f472018-08-31 15:44:25 +010010
Jamie Fox0e54ebc2019-04-09 14:21:04 +010011#include "tfm_mbedcrypto_include.h"
Antonio de Angelis8908f472018-08-31 15:44:25 +010012
Antonio de Angelis8908f472018-08-31 15:44:25 +010013#include "tfm_crypto_api.h"
Jamie Fox0e54ebc2019-04-09 14:21:04 +010014#include "tfm_crypto_defs.h"
Tamas Ban8bd24b72019-02-19 12:13:13 +000015#include "secure_fw/core/tfm_memory_utils.h"
Antonio de Angelis8908f472018-08-31 15:44:25 +010016
17/**
Antonio de Angelis819c2f32019-02-06 14:32:02 +000018 * \def TFM_CRYPTO_CONC_OPER_NUM
19 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +010020 * \brief This is the default value for the maximum number of concurrent
21 * operations that can be active (allocated) at any time, supported
22 * by the implementation
Antonio de Angelis8908f472018-08-31 15:44:25 +010023 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +010024#ifndef TFM_CRYPTO_CONC_OPER_NUM
Antonio de Angelis8908f472018-08-31 15:44:25 +010025#define TFM_CRYPTO_CONC_OPER_NUM (8)
Jamie Fox0e54ebc2019-04-09 14:21:04 +010026#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +010027
Antonio de Angelis8908f472018-08-31 15:44:25 +010028struct tfm_crypto_operation_s {
29 uint32_t in_use; /*!< Indicates if the operation is in use */
30 enum tfm_crypto_operation_type type; /*!< Type of the operation */
31 union {
Jamie Fox0e54ebc2019-04-09 14:21:04 +010032 psa_cipher_operation_t cipher; /*!< Cipher operation context */
33 psa_mac_operation_t mac; /*!< MAC operation context */
34 psa_hash_operation_t hash; /*!< Hash operation context */
Antonio de Angelis8908f472018-08-31 15:44:25 +010035 } operation;
36};
37
38static struct tfm_crypto_operation_s operation[TFM_CRYPTO_CONC_OPER_NUM] ={{0}};
39
Antonio de Angelis819c2f32019-02-06 14:32:02 +000040/*
41 * \brief Function used to clear the memory associated to a backend context
42 *
43 * \param[in] index Numerical index in the database of the backend contexts
44 *
45 * \return None
46 *
47 */
Antonio de Angelis8908f472018-08-31 15:44:25 +010048static void memset_operation_context(uint32_t index)
49{
Antonio de Angelis819c2f32019-02-06 14:32:02 +000050 uint32_t mem_size;
Antonio de Angelis8908f472018-08-31 15:44:25 +010051
Antonio de Angelis819c2f32019-02-06 14:32:02 +000052 uint8_t *mem_ptr = (uint8_t *) &(operation[index].operation);
Antonio de Angelis8908f472018-08-31 15:44:25 +010053
54 switch(operation[index].type) {
55 case TFM_CRYPTO_CIPHER_OPERATION:
Jamie Fox0e54ebc2019-04-09 14:21:04 +010056 mem_size = sizeof(psa_cipher_operation_t);
Antonio de Angelis8908f472018-08-31 15:44:25 +010057 break;
58 case TFM_CRYPTO_MAC_OPERATION:
Jamie Fox0e54ebc2019-04-09 14:21:04 +010059 mem_size = sizeof(psa_mac_operation_t);
Antonio de Angelis8908f472018-08-31 15:44:25 +010060 break;
61 case TFM_CRYPTO_HASH_OPERATION:
Jamie Fox0e54ebc2019-04-09 14:21:04 +010062 mem_size = sizeof(psa_hash_operation_t);
Antonio de Angelis8908f472018-08-31 15:44:25 +010063 break;
64 case TFM_CRYPTO_OPERATION_NONE:
65 default:
66 mem_size = 0;
67 break;
68 }
69
Antonio de Angelis819c2f32019-02-06 14:32:02 +000070 /* Clear the contents of the backend context */
71 (void)tfm_memset(mem_ptr, 0, mem_size);
72}
73
Antonio de Angelis8908f472018-08-31 15:44:25 +010074/*!
75 * \defgroup public Public functions
76 *
77 */
78
79/*!@{*/
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000080psa_status_t tfm_crypto_init_alloc(void)
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010081{
82 /* Clear the contents of the local contexts */
Antonio de Angelis819c2f32019-02-06 14:32:02 +000083 (void)tfm_memset(operation, 0, sizeof(operation));
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000084 return PSA_SUCCESS;
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010085}
86
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000087psa_status_t tfm_crypto_operation_alloc(enum tfm_crypto_operation_type type,
Antonio de Angelis4743e672019-04-11 11:38:48 +010088 uint32_t *handle,
Antonio de Angelis819c2f32019-02-06 14:32:02 +000089 void **ctx)
Antonio de Angelis8908f472018-08-31 15:44:25 +010090{
Antonio de Angelis4743e672019-04-11 11:38:48 +010091 uint32_t i = 0;
Antonio de Angelis819c2f32019-02-06 14:32:02 +000092
93 /* Init to invalid values */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000094 if (ctx == NULL) {
95 return PSA_ERROR_INVALID_ARGUMENT;
96 }
Antonio de Angelis819c2f32019-02-06 14:32:02 +000097 *ctx = NULL;
Antonio de Angelis8908f472018-08-31 15:44:25 +010098
99 for (i=0; i<TFM_CRYPTO_CONC_OPER_NUM; i++) {
100 if (operation[i].in_use == TFM_CRYPTO_NOT_IN_USE) {
101 operation[i].in_use = TFM_CRYPTO_IN_USE;
102 operation[i].type = type;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100103 *handle = i;
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000104 *ctx = (void *) &(operation[i].operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000105 return PSA_SUCCESS;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100106 }
107 }
Antonio de Angelis8908f472018-08-31 15:44:25 +0100108
Antonio de Angelis4743e672019-04-11 11:38:48 +0100109 *handle = TFM_CRYPTO_INVALID_HANDLE;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000110 return PSA_ERROR_NOT_PERMITTED;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100111}
112
Antonio de Angelis4743e672019-04-11 11:38:48 +0100113psa_status_t tfm_crypto_operation_release(uint32_t *handle)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100114{
Antonio de Angelis4743e672019-04-11 11:38:48 +0100115 uint32_t h_val = *handle;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100116
Antonio de Angelis4743e672019-04-11 11:38:48 +0100117 if ( (h_val != TFM_CRYPTO_INVALID_HANDLE) &&
118 (h_val < TFM_CRYPTO_CONC_OPER_NUM) &&
119 (operation[h_val].in_use == TFM_CRYPTO_IN_USE) ) {
120 memset_operation_context(h_val);
121 operation[h_val].in_use = TFM_CRYPTO_NOT_IN_USE;
122 operation[h_val].type = TFM_CRYPTO_OPERATION_NONE;
123 *handle = TFM_CRYPTO_INVALID_HANDLE;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000124 return PSA_SUCCESS;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100125 }
126
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000127 return PSA_ERROR_INVALID_ARGUMENT;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100128}
129
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000130psa_status_t tfm_crypto_operation_lookup(enum tfm_crypto_operation_type type,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100131 uint32_t handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000132 void **ctx)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100133{
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000134 if ( (handle != TFM_CRYPTO_INVALID_HANDLE) &&
135 (handle < TFM_CRYPTO_CONC_OPER_NUM) &&
Antonio de Angelis377a1552018-11-22 17:02:40 +0000136 (operation[handle].in_use == TFM_CRYPTO_IN_USE) &&
137 (operation[handle].type == type) ) {
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000138 *ctx = (void *) &(operation[handle].operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000139 return PSA_SUCCESS;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100140 }
141
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000142 return PSA_ERROR_BAD_STATE;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100143}
144/*!@}*/