Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 1 | /* |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019, Arm Limited. All rights reserved. |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <limits.h> |
| 9 | |
| 10 | #include "tfm_crypto_defs.h" |
| 11 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 12 | #include "psa_crypto.h" |
| 13 | #include "tfm_crypto_api.h" |
| 14 | |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 15 | #include "tfm_crypto_struct.h" |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame^] | 16 | #include "secure_fw/core/secure_utilities.h" |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 17 | |
| 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 | |
| 30 | struct 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 Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 34 | 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 Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 37 | } operation; |
| 38 | }; |
| 39 | |
| 40 | static struct tfm_crypto_operation_s operation[TFM_CRYPTO_CONC_OPER_NUM] ={{0}}; |
| 41 | |
| 42 | static 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 Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 50 | mem_size = sizeof(struct tfm_cipher_operation_s); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 51 | break; |
| 52 | case TFM_CRYPTO_MAC_OPERATION: |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 53 | mem_size = sizeof(struct tfm_mac_operation_s); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 54 | break; |
| 55 | case TFM_CRYPTO_HASH_OPERATION: |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 56 | mem_size = sizeof(struct tfm_hash_operation_s); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 57 | 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 Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame^] | 75 | enum 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 Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 82 | enum 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 Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 96 | *handle = INVALID_HANDLE; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 97 | |
| 98 | return TFM_CRYPTO_ERR_PSA_ERROR_NOT_PERMITTED; |
| 99 | } |
| 100 | |
| 101 | enum 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 | |
| 117 | enum tfm_crypto_err_t tfm_crypto_operation_lookup( |
| 118 | enum tfm_crypto_operation_type type, |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 119 | uint32_t handle, |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 120 | void **oper) |
| 121 | { |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 122 | 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 Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 126 | return TFM_CRYPTO_ERR_PSA_SUCCESS; |
| 127 | } |
| 128 | |
| 129 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 130 | } |
| 131 | /*!@}*/ |