blob: db9c6af88b075fde1b05abc64ad0a94d5adbe456 [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
97 /* Init to invalid values */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000098 if (ctx == NULL) {
99 return PSA_ERROR_INVALID_ARGUMENT;
100 }
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000101 *ctx = NULL;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100102
103 for (i=0; i<TFM_CRYPTO_CONC_OPER_NUM; i++) {
104 if (operation[i].in_use == TFM_CRYPTO_NOT_IN_USE) {
105 operation[i].in_use = TFM_CRYPTO_IN_USE;
106 operation[i].type = type;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100107 *handle = i;
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000108 *ctx = (void *) &(operation[i].operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000109 return PSA_SUCCESS;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100110 }
111 }
Antonio de Angelis8908f472018-08-31 15:44:25 +0100112
Antonio de Angelis4743e672019-04-11 11:38:48 +0100113 *handle = TFM_CRYPTO_INVALID_HANDLE;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000114 return PSA_ERROR_NOT_PERMITTED;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100115}
116
Antonio de Angelis4743e672019-04-11 11:38:48 +0100117psa_status_t tfm_crypto_operation_release(uint32_t *handle)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100118{
Antonio de Angelis4743e672019-04-11 11:38:48 +0100119 uint32_t h_val = *handle;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100120
Antonio de Angelis4743e672019-04-11 11:38:48 +0100121 if ( (h_val != TFM_CRYPTO_INVALID_HANDLE) &&
122 (h_val < TFM_CRYPTO_CONC_OPER_NUM) &&
123 (operation[h_val].in_use == TFM_CRYPTO_IN_USE) ) {
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100124
Antonio de Angelis4743e672019-04-11 11:38:48 +0100125 memset_operation_context(h_val);
126 operation[h_val].in_use = TFM_CRYPTO_NOT_IN_USE;
127 operation[h_val].type = TFM_CRYPTO_OPERATION_NONE;
128 *handle = TFM_CRYPTO_INVALID_HANDLE;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000129 return PSA_SUCCESS;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100130 }
131
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000132 return PSA_ERROR_INVALID_ARGUMENT;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100133}
134
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000135psa_status_t tfm_crypto_operation_lookup(enum tfm_crypto_operation_type type,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100136 uint32_t handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000137 void **ctx)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100138{
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000139 if ( (handle != TFM_CRYPTO_INVALID_HANDLE) &&
140 (handle < TFM_CRYPTO_CONC_OPER_NUM) &&
Antonio de Angelis377a1552018-11-22 17:02:40 +0000141 (operation[handle].in_use == TFM_CRYPTO_IN_USE) &&
142 (operation[handle].type == type) ) {
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100143
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000144 *ctx = (void *) &(operation[handle].operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000145 return PSA_SUCCESS;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100146 }
147
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000148 return PSA_ERROR_BAD_STATE;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100149}
150/*!@}*/