blob: e8b95c92c0d18a99a91fba668a0eb4ab8dcda1a1 [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 {
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +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 */
35 psa_crypto_generator_t generator; /*!< Generator operation context */
Antonio de Angelis8908f472018-08-31 15:44:25 +010036 } operation;
37};
38
39static struct tfm_crypto_operation_s operation[TFM_CRYPTO_CONC_OPER_NUM] ={{0}};
40
Antonio de Angelis819c2f32019-02-06 14:32:02 +000041/*
42 * \brief Function used to clear the memory associated to a backend context
43 *
44 * \param[in] index Numerical index in the database of the backend contexts
45 *
46 * \return None
47 *
48 */
Antonio de Angelis8908f472018-08-31 15:44:25 +010049static void memset_operation_context(uint32_t index)
50{
Antonio de Angelis819c2f32019-02-06 14:32:02 +000051 uint32_t mem_size;
Antonio de Angelis8908f472018-08-31 15:44:25 +010052
Antonio de Angelis819c2f32019-02-06 14:32:02 +000053 uint8_t *mem_ptr = (uint8_t *) &(operation[index].operation);
Antonio de Angelis8908f472018-08-31 15:44:25 +010054
55 switch(operation[index].type) {
56 case TFM_CRYPTO_CIPHER_OPERATION:
Jamie Fox0e54ebc2019-04-09 14:21:04 +010057 mem_size = sizeof(psa_cipher_operation_t);
Antonio de Angelis8908f472018-08-31 15:44:25 +010058 break;
59 case TFM_CRYPTO_MAC_OPERATION:
Jamie Fox0e54ebc2019-04-09 14:21:04 +010060 mem_size = sizeof(psa_mac_operation_t);
Antonio de Angelis8908f472018-08-31 15:44:25 +010061 break;
62 case TFM_CRYPTO_HASH_OPERATION:
Jamie Fox0e54ebc2019-04-09 14:21:04 +010063 mem_size = sizeof(psa_hash_operation_t);
Antonio de Angelis8908f472018-08-31 15:44:25 +010064 break;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010065 case TFM_CRYPTO_GENERATOR_OPERATION:
66 mem_size = sizeof(psa_crypto_generator_t);
67 break;
Antonio de Angelis8908f472018-08-31 15:44:25 +010068 case TFM_CRYPTO_OPERATION_NONE:
69 default:
70 mem_size = 0;
71 break;
72 }
73
Antonio de Angelis819c2f32019-02-06 14:32:02 +000074 /* Clear the contents of the backend context */
75 (void)tfm_memset(mem_ptr, 0, mem_size);
76}
77
Antonio de Angelis8908f472018-08-31 15:44:25 +010078/*!
79 * \defgroup public Public functions
80 *
81 */
82
83/*!@{*/
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000084psa_status_t tfm_crypto_init_alloc(void)
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010085{
86 /* Clear the contents of the local contexts */
Antonio de Angelis819c2f32019-02-06 14:32:02 +000087 (void)tfm_memset(operation, 0, sizeof(operation));
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000088 return PSA_SUCCESS;
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010089}
90
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000091psa_status_t tfm_crypto_operation_alloc(enum tfm_crypto_operation_type type,
Antonio de Angelis4743e672019-04-11 11:38:48 +010092 uint32_t *handle,
Antonio de Angelis819c2f32019-02-06 14:32:02 +000093 void **ctx)
Antonio de Angelis8908f472018-08-31 15:44:25 +010094{
Antonio de Angelis4743e672019-04-11 11:38:48 +010095 uint32_t i = 0;
Antonio de Angelis819c2f32019-02-06 14:32:02 +000096
Jamie Fox707caf72019-05-29 15:14:18 +010097 /* Handle must be initialised before calling a setup function */
98 if (*handle != TFM_CRYPTO_INVALID_HANDLE) {
99 return PSA_ERROR_BAD_STATE;
100 }
101
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000102 /* Init to invalid values */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000103 if (ctx == NULL) {
104 return PSA_ERROR_INVALID_ARGUMENT;
105 }
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000106 *ctx = NULL;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100107
108 for (i=0; i<TFM_CRYPTO_CONC_OPER_NUM; i++) {
109 if (operation[i].in_use == TFM_CRYPTO_NOT_IN_USE) {
110 operation[i].in_use = TFM_CRYPTO_IN_USE;
111 operation[i].type = type;
Jamie Fox707caf72019-05-29 15:14:18 +0100112 *handle = i + 1;
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000113 *ctx = (void *) &(operation[i].operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000114 return PSA_SUCCESS;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100115 }
116 }
Antonio de Angelis8908f472018-08-31 15:44:25 +0100117
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000118 return PSA_ERROR_NOT_PERMITTED;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100119}
120
Antonio de Angelis4743e672019-04-11 11:38:48 +0100121psa_status_t tfm_crypto_operation_release(uint32_t *handle)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100122{
Antonio de Angelis4743e672019-04-11 11:38:48 +0100123 uint32_t h_val = *handle;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100124
Antonio de Angelis4743e672019-04-11 11:38:48 +0100125 if ( (h_val != TFM_CRYPTO_INVALID_HANDLE) &&
Jamie Fox707caf72019-05-29 15:14:18 +0100126 (h_val <= TFM_CRYPTO_CONC_OPER_NUM) &&
127 (operation[h_val - 1].in_use == TFM_CRYPTO_IN_USE) ) {
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100128
Jamie Fox707caf72019-05-29 15:14:18 +0100129 memset_operation_context(h_val - 1);
130 operation[h_val - 1].in_use = TFM_CRYPTO_NOT_IN_USE;
131 operation[h_val - 1].type = TFM_CRYPTO_OPERATION_NONE;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100132 *handle = TFM_CRYPTO_INVALID_HANDLE;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000133 return PSA_SUCCESS;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100134 }
135
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000136 return PSA_ERROR_INVALID_ARGUMENT;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100137}
138
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000139psa_status_t tfm_crypto_operation_lookup(enum tfm_crypto_operation_type type,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100140 uint32_t handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000141 void **ctx)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100142{
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000143 if ( (handle != TFM_CRYPTO_INVALID_HANDLE) &&
Jamie Fox707caf72019-05-29 15:14:18 +0100144 (handle <= TFM_CRYPTO_CONC_OPER_NUM) &&
145 (operation[handle - 1].in_use == TFM_CRYPTO_IN_USE) &&
146 (operation[handle - 1].type == type) ) {
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100147
Jamie Fox707caf72019-05-29 15:14:18 +0100148 *ctx = (void *) &(operation[handle - 1].operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000149 return PSA_SUCCESS;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100150 }
151
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000152 return PSA_ERROR_BAD_STATE;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100153}
154/*!@}*/