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 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 24 | struct 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 Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 28 | 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 Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 31 | } operation; |
| 32 | }; |
| 33 | |
| 34 | static struct tfm_crypto_operation_s operation[TFM_CRYPTO_CONC_OPER_NUM] ={{0}}; |
| 35 | |
| 36 | static 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 Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 44 | mem_size = sizeof(struct tfm_cipher_operation_s); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 45 | break; |
| 46 | case TFM_CRYPTO_MAC_OPERATION: |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 47 | mem_size = sizeof(struct tfm_mac_operation_s); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 48 | break; |
| 49 | case TFM_CRYPTO_HASH_OPERATION: |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 50 | mem_size = sizeof(struct tfm_hash_operation_s); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 51 | 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 Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 69 | enum 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 Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 76 | enum 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 Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 90 | *handle = TFM_CRYPTO_INVALID_HANDLE; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 91 | |
| 92 | return TFM_CRYPTO_ERR_PSA_ERROR_NOT_PERMITTED; |
| 93 | } |
| 94 | |
| 95 | enum 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 Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 104 | *handle = TFM_CRYPTO_INVALID_HANDLE; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 105 | return TFM_CRYPTO_ERR_PSA_SUCCESS; |
| 106 | } |
| 107 | |
| 108 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 109 | } |
| 110 | |
| 111 | enum tfm_crypto_err_t tfm_crypto_operation_lookup( |
| 112 | enum tfm_crypto_operation_type type, |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 113 | uint32_t handle, |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 114 | void **oper) |
| 115 | { |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 116 | 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 Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 120 | return TFM_CRYPTO_ERR_PSA_SUCCESS; |
| 121 | } |
| 122 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 123 | return TFM_CRYPTO_ERR_PSA_ERROR_BAD_STATE; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 124 | } |
| 125 | /*!@}*/ |