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 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 10 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 11 | #include "tfm_mbedcrypto_include.h" |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 12 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 13 | #include "tfm_crypto_api.h" |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 14 | #include "tfm_crypto_defs.h" |
Tamas Ban | 8bd24b7 | 2019-02-19 12:13:13 +0000 | [diff] [blame] | 15 | #include "secure_fw/core/tfm_memory_utils.h" |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 16 | |
| 17 | /** |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 18 | * \def TFM_CRYPTO_CONC_OPER_NUM |
| 19 | * |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 20 | * \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 Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 23 | */ |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 24 | #ifndef TFM_CRYPTO_CONC_OPER_NUM |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 25 | #define TFM_CRYPTO_CONC_OPER_NUM (8) |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 26 | #endif |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 27 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 28 | struct 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 Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 32 | 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 Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 36 | } operation; |
| 37 | }; |
| 38 | |
| 39 | static struct tfm_crypto_operation_s operation[TFM_CRYPTO_CONC_OPER_NUM] ={{0}}; |
| 40 | |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 41 | /* |
| 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 Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 49 | static void memset_operation_context(uint32_t index) |
| 50 | { |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 51 | uint32_t mem_size; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 52 | |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 53 | uint8_t *mem_ptr = (uint8_t *) &(operation[index].operation); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 54 | |
| 55 | switch(operation[index].type) { |
| 56 | case TFM_CRYPTO_CIPHER_OPERATION: |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 57 | mem_size = sizeof(psa_cipher_operation_t); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 58 | break; |
| 59 | case TFM_CRYPTO_MAC_OPERATION: |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 60 | mem_size = sizeof(psa_mac_operation_t); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 61 | break; |
| 62 | case TFM_CRYPTO_HASH_OPERATION: |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 63 | mem_size = sizeof(psa_hash_operation_t); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 64 | break; |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 65 | case TFM_CRYPTO_GENERATOR_OPERATION: |
| 66 | mem_size = sizeof(psa_crypto_generator_t); |
| 67 | break; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 68 | case TFM_CRYPTO_OPERATION_NONE: |
| 69 | default: |
| 70 | mem_size = 0; |
| 71 | break; |
| 72 | } |
| 73 | |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 74 | /* Clear the contents of the backend context */ |
| 75 | (void)tfm_memset(mem_ptr, 0, mem_size); |
| 76 | } |
| 77 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 78 | /*! |
| 79 | * \defgroup public Public functions |
| 80 | * |
| 81 | */ |
| 82 | |
| 83 | /*!@{*/ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 84 | psa_status_t tfm_crypto_init_alloc(void) |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 85 | { |
| 86 | /* Clear the contents of the local contexts */ |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 87 | (void)tfm_memset(operation, 0, sizeof(operation)); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 88 | return PSA_SUCCESS; |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 91 | psa_status_t tfm_crypto_operation_alloc(enum tfm_crypto_operation_type type, |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 92 | uint32_t *handle, |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 93 | void **ctx) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 94 | { |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 95 | uint32_t i = 0; |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 96 | |
Jamie Fox | 707caf7 | 2019-05-29 15:14:18 +0100 | [diff] [blame^] | 97 | /* 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 Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 102 | /* Init to invalid values */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 103 | if (ctx == NULL) { |
| 104 | return PSA_ERROR_INVALID_ARGUMENT; |
| 105 | } |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 106 | *ctx = NULL; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 107 | |
| 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 Fox | 707caf7 | 2019-05-29 15:14:18 +0100 | [diff] [blame^] | 112 | *handle = i + 1; |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 113 | *ctx = (void *) &(operation[i].operation); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 114 | return PSA_SUCCESS; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 115 | } |
| 116 | } |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 117 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 118 | return PSA_ERROR_NOT_PERMITTED; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 119 | } |
| 120 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 121 | psa_status_t tfm_crypto_operation_release(uint32_t *handle) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 122 | { |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 123 | uint32_t h_val = *handle; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 124 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 125 | if ( (h_val != TFM_CRYPTO_INVALID_HANDLE) && |
Jamie Fox | 707caf7 | 2019-05-29 15:14:18 +0100 | [diff] [blame^] | 126 | (h_val <= TFM_CRYPTO_CONC_OPER_NUM) && |
| 127 | (operation[h_val - 1].in_use == TFM_CRYPTO_IN_USE) ) { |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 128 | |
Jamie Fox | 707caf7 | 2019-05-29 15:14:18 +0100 | [diff] [blame^] | 129 | 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 Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 132 | *handle = TFM_CRYPTO_INVALID_HANDLE; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 133 | return PSA_SUCCESS; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 134 | } |
| 135 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 136 | return PSA_ERROR_INVALID_ARGUMENT; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 137 | } |
| 138 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 139 | psa_status_t tfm_crypto_operation_lookup(enum tfm_crypto_operation_type type, |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 140 | uint32_t handle, |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 141 | void **ctx) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 142 | { |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 143 | if ( (handle != TFM_CRYPTO_INVALID_HANDLE) && |
Jamie Fox | 707caf7 | 2019-05-29 15:14:18 +0100 | [diff] [blame^] | 144 | (handle <= TFM_CRYPTO_CONC_OPER_NUM) && |
| 145 | (operation[handle - 1].in_use == TFM_CRYPTO_IN_USE) && |
| 146 | (operation[handle - 1].type == type) ) { |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 147 | |
Jamie Fox | 707caf7 | 2019-05-29 15:14:18 +0100 | [diff] [blame^] | 148 | *ctx = (void *) &(operation[handle - 1].operation); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 149 | return PSA_SUCCESS; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 150 | } |
| 151 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 152 | return PSA_ERROR_BAD_STATE; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 153 | } |
| 154 | /*!@}*/ |