blob: 627dab74ffae9ce6020401576076e7e16e551f54 [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
Antonio de Angelis8908f472018-08-31 15:44:25 +010024struct tfm_crypto_operation_s {
25 uint32_t in_use; /*!< Indicates if the operation is in use */
26 enum tfm_crypto_operation_type type; /*!< Type of the operation */
27 union {
Antonio de Angelis377a1552018-11-22 17:02:40 +000028 struct tfm_cipher_operation_s cipher; /*!< Cipher operation context */
29 struct tfm_mac_operation_s mac; /*!< MAC operation context */
30 struct tfm_hash_operation_s hash; /*!< Hash operation context */
Antonio de Angelis8908f472018-08-31 15:44:25 +010031 } operation;
32};
33
34static struct tfm_crypto_operation_s operation[TFM_CRYPTO_CONC_OPER_NUM] ={{0}};
35
36static void memset_operation_context(uint32_t index)
37{
38 uint32_t i, mem_size;
39
40 volatile uint8_t *mem_ptr = (uint8_t *) &(operation[index].operation);
41
42 switch(operation[index].type) {
43 case TFM_CRYPTO_CIPHER_OPERATION:
Antonio de Angelis377a1552018-11-22 17:02:40 +000044 mem_size = sizeof(struct tfm_cipher_operation_s);
Antonio de Angelis8908f472018-08-31 15:44:25 +010045 break;
46 case TFM_CRYPTO_MAC_OPERATION:
Antonio de Angelis377a1552018-11-22 17:02:40 +000047 mem_size = sizeof(struct tfm_mac_operation_s);
Antonio de Angelis8908f472018-08-31 15:44:25 +010048 break;
49 case TFM_CRYPTO_HASH_OPERATION:
Antonio de Angelis377a1552018-11-22 17:02:40 +000050 mem_size = sizeof(struct tfm_hash_operation_s);
Antonio de Angelis8908f472018-08-31 15:44:25 +010051 break;
52 case TFM_CRYPTO_OPERATION_NONE:
53 default:
54 mem_size = 0;
55 break;
56 }
57
58 for (i=0; i<mem_size; i++) {
59 mem_ptr[i] = 0;
60 }
61}
62
63/*!
64 * \defgroup public Public functions
65 *
66 */
67
68/*!@{*/
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010069enum tfm_crypto_err_t tfm_crypto_init_alloc(void)
70{
71 /* Clear the contents of the local contexts */
72 tfm_memset(operation, 0, sizeof(operation));
73 return TFM_CRYPTO_ERR_PSA_SUCCESS;
74}
75
Antonio de Angelis8908f472018-08-31 15:44:25 +010076enum tfm_crypto_err_t tfm_crypto_operation_alloc(
77 enum tfm_crypto_operation_type type,
78 uint32_t *handle)
79{
80 uint32_t i = 0;
81
82 for (i=0; i<TFM_CRYPTO_CONC_OPER_NUM; i++) {
83 if (operation[i].in_use == TFM_CRYPTO_NOT_IN_USE) {
84 operation[i].in_use = TFM_CRYPTO_IN_USE;
85 operation[i].type = type;
86 *handle = i;
87 return TFM_CRYPTO_ERR_PSA_SUCCESS;
88 }
89 }
Louis Mayencourt7a36f782018-09-24 14:00:57 +010090 *handle = TFM_CRYPTO_INVALID_HANDLE;
Antonio de Angelis8908f472018-08-31 15:44:25 +010091
92 return TFM_CRYPTO_ERR_PSA_ERROR_NOT_PERMITTED;
93}
94
95enum tfm_crypto_err_t tfm_crypto_operation_release(uint32_t *handle)
96{
97 uint32_t i = *handle;
98
99 if ( (i<TFM_CRYPTO_CONC_OPER_NUM) &&
100 (operation[i].in_use == TFM_CRYPTO_IN_USE) ) {
101 memset_operation_context(i);
102 operation[i].in_use = TFM_CRYPTO_NOT_IN_USE;
103 operation[i].type = TFM_CRYPTO_OPERATION_NONE;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100104 *handle = TFM_CRYPTO_INVALID_HANDLE;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100105 return TFM_CRYPTO_ERR_PSA_SUCCESS;
106 }
107
108 return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT;
109}
110
111enum tfm_crypto_err_t tfm_crypto_operation_lookup(
112 enum tfm_crypto_operation_type type,
Antonio de Angelis377a1552018-11-22 17:02:40 +0000113 uint32_t handle,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100114 void **oper)
115{
Antonio de Angelis377a1552018-11-22 17:02:40 +0000116 if ( (handle<TFM_CRYPTO_CONC_OPER_NUM) &&
117 (operation[handle].in_use == TFM_CRYPTO_IN_USE) &&
118 (operation[handle].type == type) ) {
119 *oper = (void *) &(operation[handle].operation);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100120 return TFM_CRYPTO_ERR_PSA_SUCCESS;
121 }
122
Jamie Fox82b87ca2018-12-11 16:41:11 +0000123 return TFM_CRYPTO_ERR_PSA_ERROR_BAD_STATE;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100124}
125/*!@}*/