Initial commit
mbed-dev-master/targets/TARGET_STM/TARGET_STM32F4/device/stm32f4xx_hal_cryp.c@0:bb348c97df44, 2020-09-16 (annotated)
- Committer:
- lypinator
- Date:
- Wed Sep 16 01:11:49 2020 +0000
- Revision:
- 0:bb348c97df44
Added PWM
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lypinator | 0:bb348c97df44 | 1 | /** |
lypinator | 0:bb348c97df44 | 2 | ****************************************************************************** |
lypinator | 0:bb348c97df44 | 3 | * @file stm32f4xx_hal_cryp.c |
lypinator | 0:bb348c97df44 | 4 | * @author MCD Application Team |
lypinator | 0:bb348c97df44 | 5 | * @brief CRYP HAL module driver. |
lypinator | 0:bb348c97df44 | 6 | * This file provides firmware functions to manage the following |
lypinator | 0:bb348c97df44 | 7 | * functionalities of the Cryptography (CRYP) peripheral: |
lypinator | 0:bb348c97df44 | 8 | * + Initialization and de-initialization functions |
lypinator | 0:bb348c97df44 | 9 | * + AES processing functions |
lypinator | 0:bb348c97df44 | 10 | * + DES processing functions |
lypinator | 0:bb348c97df44 | 11 | * + TDES processing functions |
lypinator | 0:bb348c97df44 | 12 | * + DMA callback functions |
lypinator | 0:bb348c97df44 | 13 | * + CRYP IRQ handler management |
lypinator | 0:bb348c97df44 | 14 | * + Peripheral State functions |
lypinator | 0:bb348c97df44 | 15 | * |
lypinator | 0:bb348c97df44 | 16 | @verbatim |
lypinator | 0:bb348c97df44 | 17 | ============================================================================== |
lypinator | 0:bb348c97df44 | 18 | ##### How to use this driver ##### |
lypinator | 0:bb348c97df44 | 19 | ============================================================================== |
lypinator | 0:bb348c97df44 | 20 | [..] |
lypinator | 0:bb348c97df44 | 21 | The CRYP HAL driver can be used as follows: |
lypinator | 0:bb348c97df44 | 22 | |
lypinator | 0:bb348c97df44 | 23 | (#)Initialize the CRYP low level resources by implementing the HAL_CRYP_MspInit(): |
lypinator | 0:bb348c97df44 | 24 | (##) Enable the CRYP interface clock using __HAL_RCC_CRYP_CLK_ENABLE() |
lypinator | 0:bb348c97df44 | 25 | (##) In case of using interrupts (e.g. HAL_CRYP_AESECB_Encrypt_IT()) |
lypinator | 0:bb348c97df44 | 26 | (+++) Configure the CRYP interrupt priority using HAL_NVIC_SetPriority() |
lypinator | 0:bb348c97df44 | 27 | (+++) Enable the CRYP IRQ handler using HAL_NVIC_EnableIRQ() |
lypinator | 0:bb348c97df44 | 28 | (+++) In CRYP IRQ handler, call HAL_CRYP_IRQHandler() |
lypinator | 0:bb348c97df44 | 29 | (##) In case of using DMA to control data transfer (e.g. HAL_CRYP_AESECB_Encrypt_DMA()) |
lypinator | 0:bb348c97df44 | 30 | (+++) Enable the DMAx interface clock using __DMAx_CLK_ENABLE() |
lypinator | 0:bb348c97df44 | 31 | (+++) Configure and enable two DMA streams one for managing data transfer from |
lypinator | 0:bb348c97df44 | 32 | memory to peripheral (input stream) and another stream for managing data |
lypinator | 0:bb348c97df44 | 33 | transfer from peripheral to memory (output stream) |
lypinator | 0:bb348c97df44 | 34 | (+++) Associate the initialized DMA handle to the CRYP DMA handle |
lypinator | 0:bb348c97df44 | 35 | using __HAL_LINKDMA() |
lypinator | 0:bb348c97df44 | 36 | (+++) Configure the priority and enable the NVIC for the transfer complete |
lypinator | 0:bb348c97df44 | 37 | interrupt on the two DMA Streams. The output stream should have higher |
lypinator | 0:bb348c97df44 | 38 | priority than the input stream HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ() |
lypinator | 0:bb348c97df44 | 39 | |
lypinator | 0:bb348c97df44 | 40 | (#)Initialize the CRYP HAL using HAL_CRYP_Init(). This function configures mainly: |
lypinator | 0:bb348c97df44 | 41 | (##) The data type: 1-bit, 8-bit, 16-bit and 32-bit |
lypinator | 0:bb348c97df44 | 42 | (##) The key size: 128, 192 and 256. This parameter is relevant only for AES |
lypinator | 0:bb348c97df44 | 43 | (##) The encryption/decryption key. It's size depends on the algorithm |
lypinator | 0:bb348c97df44 | 44 | used for encryption/decryption |
lypinator | 0:bb348c97df44 | 45 | (##) The initialization vector (counter). It is not used ECB mode. |
lypinator | 0:bb348c97df44 | 46 | |
lypinator | 0:bb348c97df44 | 47 | (#)Three processing (encryption/decryption) functions are available: |
lypinator | 0:bb348c97df44 | 48 | (##) Polling mode: encryption and decryption APIs are blocking functions |
lypinator | 0:bb348c97df44 | 49 | i.e. they process the data and wait till the processing is finished, |
lypinator | 0:bb348c97df44 | 50 | e.g. HAL_CRYP_AESCBC_Encrypt() |
lypinator | 0:bb348c97df44 | 51 | (##) Interrupt mode: encryption and decryption APIs are not blocking functions |
lypinator | 0:bb348c97df44 | 52 | i.e. they process the data under interrupt, |
lypinator | 0:bb348c97df44 | 53 | e.g. HAL_CRYP_AESCBC_Encrypt_IT() |
lypinator | 0:bb348c97df44 | 54 | (##) DMA mode: encryption and decryption APIs are not blocking functions |
lypinator | 0:bb348c97df44 | 55 | i.e. the data transfer is ensured by DMA, |
lypinator | 0:bb348c97df44 | 56 | e.g. HAL_CRYP_AESCBC_Encrypt_DMA() |
lypinator | 0:bb348c97df44 | 57 | |
lypinator | 0:bb348c97df44 | 58 | (#)When the processing function is called at first time after HAL_CRYP_Init() |
lypinator | 0:bb348c97df44 | 59 | the CRYP peripheral is initialized and processes the buffer in input. |
lypinator | 0:bb348c97df44 | 60 | At second call, the processing function performs an append of the already |
lypinator | 0:bb348c97df44 | 61 | processed buffer. |
lypinator | 0:bb348c97df44 | 62 | When a new data block is to be processed, call HAL_CRYP_Init() then the |
lypinator | 0:bb348c97df44 | 63 | processing function. |
lypinator | 0:bb348c97df44 | 64 | |
lypinator | 0:bb348c97df44 | 65 | (#)Call HAL_CRYP_DeInit() to deinitialize the CRYP peripheral. |
lypinator | 0:bb348c97df44 | 66 | |
lypinator | 0:bb348c97df44 | 67 | @endverbatim |
lypinator | 0:bb348c97df44 | 68 | ****************************************************************************** |
lypinator | 0:bb348c97df44 | 69 | * @attention |
lypinator | 0:bb348c97df44 | 70 | * |
lypinator | 0:bb348c97df44 | 71 | * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> |
lypinator | 0:bb348c97df44 | 72 | * |
lypinator | 0:bb348c97df44 | 73 | * Redistribution and use in source and binary forms, with or without modification, |
lypinator | 0:bb348c97df44 | 74 | * are permitted provided that the following conditions are met: |
lypinator | 0:bb348c97df44 | 75 | * 1. Redistributions of source code must retain the above copyright notice, |
lypinator | 0:bb348c97df44 | 76 | * this list of conditions and the following disclaimer. |
lypinator | 0:bb348c97df44 | 77 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
lypinator | 0:bb348c97df44 | 78 | * this list of conditions and the following disclaimer in the documentation |
lypinator | 0:bb348c97df44 | 79 | * and/or other materials provided with the distribution. |
lypinator | 0:bb348c97df44 | 80 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
lypinator | 0:bb348c97df44 | 81 | * may be used to endorse or promote products derived from this software |
lypinator | 0:bb348c97df44 | 82 | * without specific prior written permission. |
lypinator | 0:bb348c97df44 | 83 | * |
lypinator | 0:bb348c97df44 | 84 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
lypinator | 0:bb348c97df44 | 85 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
lypinator | 0:bb348c97df44 | 86 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
lypinator | 0:bb348c97df44 | 87 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
lypinator | 0:bb348c97df44 | 88 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
lypinator | 0:bb348c97df44 | 89 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
lypinator | 0:bb348c97df44 | 90 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
lypinator | 0:bb348c97df44 | 91 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
lypinator | 0:bb348c97df44 | 92 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
lypinator | 0:bb348c97df44 | 93 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
lypinator | 0:bb348c97df44 | 94 | * |
lypinator | 0:bb348c97df44 | 95 | ****************************************************************************** |
lypinator | 0:bb348c97df44 | 96 | */ |
lypinator | 0:bb348c97df44 | 97 | |
lypinator | 0:bb348c97df44 | 98 | /* Includes ------------------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 99 | #include "stm32f4xx_hal.h" |
lypinator | 0:bb348c97df44 | 100 | |
lypinator | 0:bb348c97df44 | 101 | /** @addtogroup STM32F4xx_HAL_Driver |
lypinator | 0:bb348c97df44 | 102 | * @{ |
lypinator | 0:bb348c97df44 | 103 | */ |
lypinator | 0:bb348c97df44 | 104 | |
lypinator | 0:bb348c97df44 | 105 | #ifdef HAL_CRYP_MODULE_ENABLED |
lypinator | 0:bb348c97df44 | 106 | |
lypinator | 0:bb348c97df44 | 107 | #if defined(CRYP) |
lypinator | 0:bb348c97df44 | 108 | |
lypinator | 0:bb348c97df44 | 109 | /** @defgroup CRYP CRYP |
lypinator | 0:bb348c97df44 | 110 | * @brief CRYP HAL module driver. |
lypinator | 0:bb348c97df44 | 111 | * @{ |
lypinator | 0:bb348c97df44 | 112 | */ |
lypinator | 0:bb348c97df44 | 113 | |
lypinator | 0:bb348c97df44 | 114 | /* Private typedef -----------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 115 | /* Private define ------------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 116 | /** @addtogroup CRYP_Private_define |
lypinator | 0:bb348c97df44 | 117 | * @{ |
lypinator | 0:bb348c97df44 | 118 | */ |
lypinator | 0:bb348c97df44 | 119 | #define CRYP_TIMEOUT_VALUE 1U |
lypinator | 0:bb348c97df44 | 120 | /** |
lypinator | 0:bb348c97df44 | 121 | * @} |
lypinator | 0:bb348c97df44 | 122 | */ |
lypinator | 0:bb348c97df44 | 123 | |
lypinator | 0:bb348c97df44 | 124 | /* Private macro -------------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 125 | /* Private variables ---------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 126 | /* Private function prototypes -----------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 127 | /** @addtogroup CRYP_Private_Functions_prototypes |
lypinator | 0:bb348c97df44 | 128 | * @{ |
lypinator | 0:bb348c97df44 | 129 | */ |
lypinator | 0:bb348c97df44 | 130 | static void CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp, uint8_t *InitVector, uint32_t IVSize); |
lypinator | 0:bb348c97df44 | 131 | static void CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key, uint32_t KeySize); |
lypinator | 0:bb348c97df44 | 132 | static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout); |
lypinator | 0:bb348c97df44 | 133 | static HAL_StatusTypeDef CRYP_ProcessData2Words(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout); |
lypinator | 0:bb348c97df44 | 134 | static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma); |
lypinator | 0:bb348c97df44 | 135 | static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma); |
lypinator | 0:bb348c97df44 | 136 | static void CRYP_DMAError(DMA_HandleTypeDef *hdma); |
lypinator | 0:bb348c97df44 | 137 | static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr); |
lypinator | 0:bb348c97df44 | 138 | static void CRYP_SetTDESECBMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction); |
lypinator | 0:bb348c97df44 | 139 | static void CRYP_SetTDESCBCMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction); |
lypinator | 0:bb348c97df44 | 140 | static void CRYP_SetDESECBMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction); |
lypinator | 0:bb348c97df44 | 141 | static void CRYP_SetDESCBCMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction); |
lypinator | 0:bb348c97df44 | 142 | /** |
lypinator | 0:bb348c97df44 | 143 | * @} |
lypinator | 0:bb348c97df44 | 144 | */ |
lypinator | 0:bb348c97df44 | 145 | |
lypinator | 0:bb348c97df44 | 146 | |
lypinator | 0:bb348c97df44 | 147 | /* Private functions ---------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 148 | |
lypinator | 0:bb348c97df44 | 149 | /** @addtogroup CRYP_Private_Functions |
lypinator | 0:bb348c97df44 | 150 | * @{ |
lypinator | 0:bb348c97df44 | 151 | */ |
lypinator | 0:bb348c97df44 | 152 | |
lypinator | 0:bb348c97df44 | 153 | |
lypinator | 0:bb348c97df44 | 154 | /** |
lypinator | 0:bb348c97df44 | 155 | * @brief DMA CRYP Input Data process complete callback. |
lypinator | 0:bb348c97df44 | 156 | * @param hdma DMA handle |
lypinator | 0:bb348c97df44 | 157 | * @retval None |
lypinator | 0:bb348c97df44 | 158 | */ |
lypinator | 0:bb348c97df44 | 159 | static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma) |
lypinator | 0:bb348c97df44 | 160 | { |
lypinator | 0:bb348c97df44 | 161 | CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
lypinator | 0:bb348c97df44 | 162 | |
lypinator | 0:bb348c97df44 | 163 | /* Disable the DMA transfer for input FIFO request by resetting the DIEN bit |
lypinator | 0:bb348c97df44 | 164 | in the DMACR register */ |
lypinator | 0:bb348c97df44 | 165 | hcryp->Instance->DMACR &= (uint32_t)(~CRYP_DMACR_DIEN); |
lypinator | 0:bb348c97df44 | 166 | |
lypinator | 0:bb348c97df44 | 167 | /* Call input data transfer complete callback */ |
lypinator | 0:bb348c97df44 | 168 | HAL_CRYP_InCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 169 | } |
lypinator | 0:bb348c97df44 | 170 | |
lypinator | 0:bb348c97df44 | 171 | /** |
lypinator | 0:bb348c97df44 | 172 | * @brief DMA CRYP Output Data process complete callback. |
lypinator | 0:bb348c97df44 | 173 | * @param hdma DMA handle |
lypinator | 0:bb348c97df44 | 174 | * @retval None |
lypinator | 0:bb348c97df44 | 175 | */ |
lypinator | 0:bb348c97df44 | 176 | static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma) |
lypinator | 0:bb348c97df44 | 177 | { |
lypinator | 0:bb348c97df44 | 178 | CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
lypinator | 0:bb348c97df44 | 179 | |
lypinator | 0:bb348c97df44 | 180 | /* Disable the DMA transfer for output FIFO request by resetting the DOEN bit |
lypinator | 0:bb348c97df44 | 181 | in the DMACR register */ |
lypinator | 0:bb348c97df44 | 182 | hcryp->Instance->DMACR &= (uint32_t)(~CRYP_DMACR_DOEN); |
lypinator | 0:bb348c97df44 | 183 | |
lypinator | 0:bb348c97df44 | 184 | /* Disable CRYP */ |
lypinator | 0:bb348c97df44 | 185 | __HAL_CRYP_DISABLE(hcryp); |
lypinator | 0:bb348c97df44 | 186 | |
lypinator | 0:bb348c97df44 | 187 | /* Change the CRYP state to ready */ |
lypinator | 0:bb348c97df44 | 188 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 189 | |
lypinator | 0:bb348c97df44 | 190 | /* Call output data transfer complete callback */ |
lypinator | 0:bb348c97df44 | 191 | HAL_CRYP_OutCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 192 | } |
lypinator | 0:bb348c97df44 | 193 | |
lypinator | 0:bb348c97df44 | 194 | /** |
lypinator | 0:bb348c97df44 | 195 | * @brief DMA CRYP communication error callback. |
lypinator | 0:bb348c97df44 | 196 | * @param hdma DMA handle |
lypinator | 0:bb348c97df44 | 197 | * @retval None |
lypinator | 0:bb348c97df44 | 198 | */ |
lypinator | 0:bb348c97df44 | 199 | static void CRYP_DMAError(DMA_HandleTypeDef *hdma) |
lypinator | 0:bb348c97df44 | 200 | { |
lypinator | 0:bb348c97df44 | 201 | CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
lypinator | 0:bb348c97df44 | 202 | hcryp->State= HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 203 | HAL_CRYP_ErrorCallback(hcryp); |
lypinator | 0:bb348c97df44 | 204 | } |
lypinator | 0:bb348c97df44 | 205 | |
lypinator | 0:bb348c97df44 | 206 | /** |
lypinator | 0:bb348c97df44 | 207 | * @brief Writes the Key in Key registers. |
lypinator | 0:bb348c97df44 | 208 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 209 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 210 | * @param Key Pointer to Key buffer |
lypinator | 0:bb348c97df44 | 211 | * @param KeySize Size of Key |
lypinator | 0:bb348c97df44 | 212 | * @retval None |
lypinator | 0:bb348c97df44 | 213 | */ |
lypinator | 0:bb348c97df44 | 214 | static void CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key, uint32_t KeySize) |
lypinator | 0:bb348c97df44 | 215 | { |
lypinator | 0:bb348c97df44 | 216 | uint32_t keyaddr = (uint32_t)Key; |
lypinator | 0:bb348c97df44 | 217 | |
lypinator | 0:bb348c97df44 | 218 | switch(KeySize) |
lypinator | 0:bb348c97df44 | 219 | { |
lypinator | 0:bb348c97df44 | 220 | case CRYP_KEYSIZE_256B: |
lypinator | 0:bb348c97df44 | 221 | /* Key Initialisation */ |
lypinator | 0:bb348c97df44 | 222 | hcryp->Instance->K0LR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 223 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 224 | hcryp->Instance->K0RR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 225 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 226 | hcryp->Instance->K1LR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 227 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 228 | hcryp->Instance->K1RR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 229 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 230 | hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 231 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 232 | hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 233 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 234 | hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 235 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 236 | hcryp->Instance->K3RR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 237 | break; |
lypinator | 0:bb348c97df44 | 238 | case CRYP_KEYSIZE_192B: |
lypinator | 0:bb348c97df44 | 239 | hcryp->Instance->K1LR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 240 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 241 | hcryp->Instance->K1RR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 242 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 243 | hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 244 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 245 | hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 246 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 247 | hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 248 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 249 | hcryp->Instance->K3RR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 250 | break; |
lypinator | 0:bb348c97df44 | 251 | case CRYP_KEYSIZE_128B: |
lypinator | 0:bb348c97df44 | 252 | hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 253 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 254 | hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 255 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 256 | hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 257 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 258 | hcryp->Instance->K3RR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 259 | break; |
lypinator | 0:bb348c97df44 | 260 | default: |
lypinator | 0:bb348c97df44 | 261 | break; |
lypinator | 0:bb348c97df44 | 262 | } |
lypinator | 0:bb348c97df44 | 263 | } |
lypinator | 0:bb348c97df44 | 264 | |
lypinator | 0:bb348c97df44 | 265 | /** |
lypinator | 0:bb348c97df44 | 266 | * @brief Writes the InitVector/InitCounter in IV registers. |
lypinator | 0:bb348c97df44 | 267 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 268 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 269 | * @param InitVector Pointer to InitVector/InitCounter buffer |
lypinator | 0:bb348c97df44 | 270 | * @param IVSize Size of the InitVector/InitCounter |
lypinator | 0:bb348c97df44 | 271 | * @retval None |
lypinator | 0:bb348c97df44 | 272 | */ |
lypinator | 0:bb348c97df44 | 273 | static void CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp, uint8_t *InitVector, uint32_t IVSize) |
lypinator | 0:bb348c97df44 | 274 | { |
lypinator | 0:bb348c97df44 | 275 | uint32_t ivaddr = (uint32_t)InitVector; |
lypinator | 0:bb348c97df44 | 276 | |
lypinator | 0:bb348c97df44 | 277 | switch(IVSize) |
lypinator | 0:bb348c97df44 | 278 | { |
lypinator | 0:bb348c97df44 | 279 | case CRYP_KEYSIZE_128B: |
lypinator | 0:bb348c97df44 | 280 | hcryp->Instance->IV0LR = __REV(*(uint32_t*)(ivaddr)); |
lypinator | 0:bb348c97df44 | 281 | ivaddr+=4U; |
lypinator | 0:bb348c97df44 | 282 | hcryp->Instance->IV0RR = __REV(*(uint32_t*)(ivaddr)); |
lypinator | 0:bb348c97df44 | 283 | ivaddr+=4U; |
lypinator | 0:bb348c97df44 | 284 | hcryp->Instance->IV1LR = __REV(*(uint32_t*)(ivaddr)); |
lypinator | 0:bb348c97df44 | 285 | ivaddr+=4U; |
lypinator | 0:bb348c97df44 | 286 | hcryp->Instance->IV1RR = __REV(*(uint32_t*)(ivaddr)); |
lypinator | 0:bb348c97df44 | 287 | break; |
lypinator | 0:bb348c97df44 | 288 | /* Whatever key size 192 or 256, Init vector is written in IV0LR and IV0RR */ |
lypinator | 0:bb348c97df44 | 289 | case CRYP_KEYSIZE_192B: |
lypinator | 0:bb348c97df44 | 290 | hcryp->Instance->IV0LR = __REV(*(uint32_t*)(ivaddr)); |
lypinator | 0:bb348c97df44 | 291 | ivaddr+=4U; |
lypinator | 0:bb348c97df44 | 292 | hcryp->Instance->IV0RR = __REV(*(uint32_t*)(ivaddr)); |
lypinator | 0:bb348c97df44 | 293 | break; |
lypinator | 0:bb348c97df44 | 294 | case CRYP_KEYSIZE_256B: |
lypinator | 0:bb348c97df44 | 295 | hcryp->Instance->IV0LR = __REV(*(uint32_t*)(ivaddr)); |
lypinator | 0:bb348c97df44 | 296 | ivaddr+=4U; |
lypinator | 0:bb348c97df44 | 297 | hcryp->Instance->IV0RR = __REV(*(uint32_t*)(ivaddr)); |
lypinator | 0:bb348c97df44 | 298 | break; |
lypinator | 0:bb348c97df44 | 299 | default: |
lypinator | 0:bb348c97df44 | 300 | break; |
lypinator | 0:bb348c97df44 | 301 | } |
lypinator | 0:bb348c97df44 | 302 | } |
lypinator | 0:bb348c97df44 | 303 | |
lypinator | 0:bb348c97df44 | 304 | /** |
lypinator | 0:bb348c97df44 | 305 | * @brief Process Data: Writes Input data in polling mode and read the output data |
lypinator | 0:bb348c97df44 | 306 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 307 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 308 | * @param Input Pointer to the Input buffer |
lypinator | 0:bb348c97df44 | 309 | * @param Ilength Length of the Input buffer, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 310 | * @param Output Pointer to the returned buffer |
lypinator | 0:bb348c97df44 | 311 | * @param Timeout Timeout value |
lypinator | 0:bb348c97df44 | 312 | * @retval None |
lypinator | 0:bb348c97df44 | 313 | */ |
lypinator | 0:bb348c97df44 | 314 | static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 315 | { |
lypinator | 0:bb348c97df44 | 316 | uint32_t tickstart = 0U; |
lypinator | 0:bb348c97df44 | 317 | |
lypinator | 0:bb348c97df44 | 318 | uint32_t i = 0U; |
lypinator | 0:bb348c97df44 | 319 | uint32_t inputaddr = (uint32_t)Input; |
lypinator | 0:bb348c97df44 | 320 | uint32_t outputaddr = (uint32_t)Output; |
lypinator | 0:bb348c97df44 | 321 | |
lypinator | 0:bb348c97df44 | 322 | for(i=0U; (i < Ilength); i+=16U) |
lypinator | 0:bb348c97df44 | 323 | { |
lypinator | 0:bb348c97df44 | 324 | /* Write the Input block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 325 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 326 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 327 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 328 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 329 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 330 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 331 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 332 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 333 | |
lypinator | 0:bb348c97df44 | 334 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 335 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 336 | |
lypinator | 0:bb348c97df44 | 337 | while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE)) |
lypinator | 0:bb348c97df44 | 338 | { |
lypinator | 0:bb348c97df44 | 339 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 340 | if(Timeout != HAL_MAX_DELAY) |
lypinator | 0:bb348c97df44 | 341 | { |
lypinator | 0:bb348c97df44 | 342 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
lypinator | 0:bb348c97df44 | 343 | { |
lypinator | 0:bb348c97df44 | 344 | /* Change state */ |
lypinator | 0:bb348c97df44 | 345 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 346 | |
lypinator | 0:bb348c97df44 | 347 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 348 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 349 | |
lypinator | 0:bb348c97df44 | 350 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 351 | } |
lypinator | 0:bb348c97df44 | 352 | } |
lypinator | 0:bb348c97df44 | 353 | } |
lypinator | 0:bb348c97df44 | 354 | /* Read the Output block from the Output FIFO */ |
lypinator | 0:bb348c97df44 | 355 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 356 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 357 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 358 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 359 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 360 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 361 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 362 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 363 | } |
lypinator | 0:bb348c97df44 | 364 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 365 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 366 | } |
lypinator | 0:bb348c97df44 | 367 | |
lypinator | 0:bb348c97df44 | 368 | /** |
lypinator | 0:bb348c97df44 | 369 | * @brief Process Data: Write Input data in polling mode. |
lypinator | 0:bb348c97df44 | 370 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 371 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 372 | * @param Input Pointer to the Input buffer |
lypinator | 0:bb348c97df44 | 373 | * @param Ilength Length of the Input buffer, must be a multiple of 8 |
lypinator | 0:bb348c97df44 | 374 | * @param Output Pointer to the returned buffer |
lypinator | 0:bb348c97df44 | 375 | * @param Timeout Specify Timeout value |
lypinator | 0:bb348c97df44 | 376 | * @retval None |
lypinator | 0:bb348c97df44 | 377 | */ |
lypinator | 0:bb348c97df44 | 378 | static HAL_StatusTypeDef CRYP_ProcessData2Words(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 379 | { |
lypinator | 0:bb348c97df44 | 380 | uint32_t tickstart = 0U; |
lypinator | 0:bb348c97df44 | 381 | |
lypinator | 0:bb348c97df44 | 382 | uint32_t i = 0U; |
lypinator | 0:bb348c97df44 | 383 | uint32_t inputaddr = (uint32_t)Input; |
lypinator | 0:bb348c97df44 | 384 | uint32_t outputaddr = (uint32_t)Output; |
lypinator | 0:bb348c97df44 | 385 | |
lypinator | 0:bb348c97df44 | 386 | for(i=0U; (i < Ilength); i+=8U) |
lypinator | 0:bb348c97df44 | 387 | { |
lypinator | 0:bb348c97df44 | 388 | /* Write the Input block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 389 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 390 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 391 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 392 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 393 | |
lypinator | 0:bb348c97df44 | 394 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 395 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 396 | |
lypinator | 0:bb348c97df44 | 397 | while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE)) |
lypinator | 0:bb348c97df44 | 398 | { |
lypinator | 0:bb348c97df44 | 399 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 400 | if(Timeout != HAL_MAX_DELAY) |
lypinator | 0:bb348c97df44 | 401 | { |
lypinator | 0:bb348c97df44 | 402 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
lypinator | 0:bb348c97df44 | 403 | { |
lypinator | 0:bb348c97df44 | 404 | /* Change state */ |
lypinator | 0:bb348c97df44 | 405 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 406 | |
lypinator | 0:bb348c97df44 | 407 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 408 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 409 | |
lypinator | 0:bb348c97df44 | 410 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 411 | } |
lypinator | 0:bb348c97df44 | 412 | } |
lypinator | 0:bb348c97df44 | 413 | } |
lypinator | 0:bb348c97df44 | 414 | /* Read the Output block from the Output FIFO */ |
lypinator | 0:bb348c97df44 | 415 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 416 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 417 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 418 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 419 | } |
lypinator | 0:bb348c97df44 | 420 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 421 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 422 | } |
lypinator | 0:bb348c97df44 | 423 | |
lypinator | 0:bb348c97df44 | 424 | /** |
lypinator | 0:bb348c97df44 | 425 | * @brief Set the DMA configuration and start the DMA transfer |
lypinator | 0:bb348c97df44 | 426 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 427 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 428 | * @param inputaddr address of the Input buffer |
lypinator | 0:bb348c97df44 | 429 | * @param Size Size of the Input buffer, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 430 | * @param outputaddr address of the Output buffer |
lypinator | 0:bb348c97df44 | 431 | * @retval None |
lypinator | 0:bb348c97df44 | 432 | */ |
lypinator | 0:bb348c97df44 | 433 | static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr) |
lypinator | 0:bb348c97df44 | 434 | { |
lypinator | 0:bb348c97df44 | 435 | /* Set the CRYP DMA transfer complete callback */ |
lypinator | 0:bb348c97df44 | 436 | hcryp->hdmain->XferCpltCallback = CRYP_DMAInCplt; |
lypinator | 0:bb348c97df44 | 437 | /* Set the DMA error callback */ |
lypinator | 0:bb348c97df44 | 438 | hcryp->hdmain->XferErrorCallback = CRYP_DMAError; |
lypinator | 0:bb348c97df44 | 439 | |
lypinator | 0:bb348c97df44 | 440 | /* Set the CRYP DMA transfer complete callback */ |
lypinator | 0:bb348c97df44 | 441 | hcryp->hdmaout->XferCpltCallback = CRYP_DMAOutCplt; |
lypinator | 0:bb348c97df44 | 442 | /* Set the DMA error callback */ |
lypinator | 0:bb348c97df44 | 443 | hcryp->hdmaout->XferErrorCallback = CRYP_DMAError; |
lypinator | 0:bb348c97df44 | 444 | |
lypinator | 0:bb348c97df44 | 445 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 446 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 447 | |
lypinator | 0:bb348c97df44 | 448 | /* Enable the DMA In DMA Stream */ |
lypinator | 0:bb348c97df44 | 449 | HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DR, Size/4U); |
lypinator | 0:bb348c97df44 | 450 | |
lypinator | 0:bb348c97df44 | 451 | /* Enable In DMA request */ |
lypinator | 0:bb348c97df44 | 452 | hcryp->Instance->DMACR = (CRYP_DMACR_DIEN); |
lypinator | 0:bb348c97df44 | 453 | |
lypinator | 0:bb348c97df44 | 454 | /* Enable the DMA Out DMA Stream */ |
lypinator | 0:bb348c97df44 | 455 | HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUT, outputaddr, Size/4U); |
lypinator | 0:bb348c97df44 | 456 | |
lypinator | 0:bb348c97df44 | 457 | /* Enable Out DMA request */ |
lypinator | 0:bb348c97df44 | 458 | hcryp->Instance->DMACR |= CRYP_DMACR_DOEN; |
lypinator | 0:bb348c97df44 | 459 | |
lypinator | 0:bb348c97df44 | 460 | } |
lypinator | 0:bb348c97df44 | 461 | |
lypinator | 0:bb348c97df44 | 462 | /** |
lypinator | 0:bb348c97df44 | 463 | * @brief Sets the CRYP peripheral in DES ECB mode. |
lypinator | 0:bb348c97df44 | 464 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 465 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 466 | * @param Direction Encryption or decryption |
lypinator | 0:bb348c97df44 | 467 | * @retval None |
lypinator | 0:bb348c97df44 | 468 | */ |
lypinator | 0:bb348c97df44 | 469 | static void CRYP_SetDESECBMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction) |
lypinator | 0:bb348c97df44 | 470 | { |
lypinator | 0:bb348c97df44 | 471 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 472 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 473 | { |
lypinator | 0:bb348c97df44 | 474 | /* Set the CRYP peripheral in AES ECB mode */ |
lypinator | 0:bb348c97df44 | 475 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_DES_ECB | Direction); |
lypinator | 0:bb348c97df44 | 476 | |
lypinator | 0:bb348c97df44 | 477 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 478 | hcryp->Instance->K1LR = __REV(*(uint32_t*)(hcryp->Init.pKey)); |
lypinator | 0:bb348c97df44 | 479 | hcryp->Instance->K1RR = __REV(*(uint32_t*)(hcryp->Init.pKey+4U)); |
lypinator | 0:bb348c97df44 | 480 | |
lypinator | 0:bb348c97df44 | 481 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 482 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 483 | |
lypinator | 0:bb348c97df44 | 484 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 485 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 486 | } |
lypinator | 0:bb348c97df44 | 487 | } |
lypinator | 0:bb348c97df44 | 488 | |
lypinator | 0:bb348c97df44 | 489 | /** |
lypinator | 0:bb348c97df44 | 490 | * @brief Sets the CRYP peripheral in DES CBC mode. |
lypinator | 0:bb348c97df44 | 491 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 492 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 493 | * @param Direction Encryption or decryption |
lypinator | 0:bb348c97df44 | 494 | * @retval None |
lypinator | 0:bb348c97df44 | 495 | */ |
lypinator | 0:bb348c97df44 | 496 | static void CRYP_SetDESCBCMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction) |
lypinator | 0:bb348c97df44 | 497 | { |
lypinator | 0:bb348c97df44 | 498 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 499 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 500 | { |
lypinator | 0:bb348c97df44 | 501 | /* Set the CRYP peripheral in AES ECB mode */ |
lypinator | 0:bb348c97df44 | 502 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_DES_CBC | Direction); |
lypinator | 0:bb348c97df44 | 503 | |
lypinator | 0:bb348c97df44 | 504 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 505 | hcryp->Instance->K1LR = __REV(*(uint32_t*)(hcryp->Init.pKey)); |
lypinator | 0:bb348c97df44 | 506 | hcryp->Instance->K1RR = __REV(*(uint32_t*)(hcryp->Init.pKey+4U)); |
lypinator | 0:bb348c97df44 | 507 | |
lypinator | 0:bb348c97df44 | 508 | /* Set the Initialization Vector */ |
lypinator | 0:bb348c97df44 | 509 | CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_256B); |
lypinator | 0:bb348c97df44 | 510 | |
lypinator | 0:bb348c97df44 | 511 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 512 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 513 | |
lypinator | 0:bb348c97df44 | 514 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 515 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 516 | } |
lypinator | 0:bb348c97df44 | 517 | } |
lypinator | 0:bb348c97df44 | 518 | |
lypinator | 0:bb348c97df44 | 519 | /** |
lypinator | 0:bb348c97df44 | 520 | * @brief Sets the CRYP peripheral in TDES ECB mode. |
lypinator | 0:bb348c97df44 | 521 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 522 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 523 | * @param Direction Encryption or decryption |
lypinator | 0:bb348c97df44 | 524 | * @retval None |
lypinator | 0:bb348c97df44 | 525 | */ |
lypinator | 0:bb348c97df44 | 526 | static void CRYP_SetTDESECBMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction) |
lypinator | 0:bb348c97df44 | 527 | { |
lypinator | 0:bb348c97df44 | 528 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 529 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 530 | { |
lypinator | 0:bb348c97df44 | 531 | /* Set the CRYP peripheral in AES ECB mode */ |
lypinator | 0:bb348c97df44 | 532 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_TDES_ECB | Direction); |
lypinator | 0:bb348c97df44 | 533 | |
lypinator | 0:bb348c97df44 | 534 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 535 | CRYP_SetKey(hcryp, hcryp->Init.pKey, CRYP_KEYSIZE_192B); |
lypinator | 0:bb348c97df44 | 536 | |
lypinator | 0:bb348c97df44 | 537 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 538 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 539 | |
lypinator | 0:bb348c97df44 | 540 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 541 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 542 | } |
lypinator | 0:bb348c97df44 | 543 | } |
lypinator | 0:bb348c97df44 | 544 | |
lypinator | 0:bb348c97df44 | 545 | /** |
lypinator | 0:bb348c97df44 | 546 | * @brief Sets the CRYP peripheral in TDES CBC mode |
lypinator | 0:bb348c97df44 | 547 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 548 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 549 | * @param Direction Encryption or decryption |
lypinator | 0:bb348c97df44 | 550 | * @retval None |
lypinator | 0:bb348c97df44 | 551 | */ |
lypinator | 0:bb348c97df44 | 552 | static void CRYP_SetTDESCBCMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction) |
lypinator | 0:bb348c97df44 | 553 | { |
lypinator | 0:bb348c97df44 | 554 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 555 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 556 | { |
lypinator | 0:bb348c97df44 | 557 | /* Set the CRYP peripheral in AES CBC mode */ |
lypinator | 0:bb348c97df44 | 558 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_TDES_CBC | Direction); |
lypinator | 0:bb348c97df44 | 559 | |
lypinator | 0:bb348c97df44 | 560 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 561 | CRYP_SetKey(hcryp, hcryp->Init.pKey, CRYP_KEYSIZE_192B); |
lypinator | 0:bb348c97df44 | 562 | |
lypinator | 0:bb348c97df44 | 563 | /* Set the Initialization Vector */ |
lypinator | 0:bb348c97df44 | 564 | CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_256B); |
lypinator | 0:bb348c97df44 | 565 | |
lypinator | 0:bb348c97df44 | 566 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 567 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 568 | |
lypinator | 0:bb348c97df44 | 569 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 570 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 571 | } |
lypinator | 0:bb348c97df44 | 572 | } |
lypinator | 0:bb348c97df44 | 573 | |
lypinator | 0:bb348c97df44 | 574 | /** |
lypinator | 0:bb348c97df44 | 575 | * @} |
lypinator | 0:bb348c97df44 | 576 | */ |
lypinator | 0:bb348c97df44 | 577 | |
lypinator | 0:bb348c97df44 | 578 | /* Exported functions --------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 579 | /** @addtogroup CRYP_Exported_Functions |
lypinator | 0:bb348c97df44 | 580 | * @{ |
lypinator | 0:bb348c97df44 | 581 | */ |
lypinator | 0:bb348c97df44 | 582 | |
lypinator | 0:bb348c97df44 | 583 | /** @defgroup CRYP_Exported_Functions_Group1 Initialization and de-initialization functions |
lypinator | 0:bb348c97df44 | 584 | * @brief Initialization and Configuration functions. |
lypinator | 0:bb348c97df44 | 585 | * |
lypinator | 0:bb348c97df44 | 586 | @verbatim |
lypinator | 0:bb348c97df44 | 587 | ============================================================================== |
lypinator | 0:bb348c97df44 | 588 | ##### Initialization and de-initialization functions ##### |
lypinator | 0:bb348c97df44 | 589 | ============================================================================== |
lypinator | 0:bb348c97df44 | 590 | [..] This section provides functions allowing to: |
lypinator | 0:bb348c97df44 | 591 | (+) Initialize the CRYP according to the specified parameters |
lypinator | 0:bb348c97df44 | 592 | in the CRYP_InitTypeDef and creates the associated handle |
lypinator | 0:bb348c97df44 | 593 | (+) DeInitialize the CRYP peripheral |
lypinator | 0:bb348c97df44 | 594 | (+) Initialize the CRYP MSP |
lypinator | 0:bb348c97df44 | 595 | (+) DeInitialize CRYP MSP |
lypinator | 0:bb348c97df44 | 596 | |
lypinator | 0:bb348c97df44 | 597 | @endverbatim |
lypinator | 0:bb348c97df44 | 598 | * @{ |
lypinator | 0:bb348c97df44 | 599 | */ |
lypinator | 0:bb348c97df44 | 600 | |
lypinator | 0:bb348c97df44 | 601 | /** |
lypinator | 0:bb348c97df44 | 602 | * @brief Initializes the CRYP according to the specified |
lypinator | 0:bb348c97df44 | 603 | * parameters in the CRYP_InitTypeDef and creates the associated handle. |
lypinator | 0:bb348c97df44 | 604 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 605 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 606 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 607 | */ |
lypinator | 0:bb348c97df44 | 608 | HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp) |
lypinator | 0:bb348c97df44 | 609 | { |
lypinator | 0:bb348c97df44 | 610 | /* Check the CRYP handle allocation */ |
lypinator | 0:bb348c97df44 | 611 | if(hcryp == NULL) |
lypinator | 0:bb348c97df44 | 612 | { |
lypinator | 0:bb348c97df44 | 613 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 614 | } |
lypinator | 0:bb348c97df44 | 615 | |
lypinator | 0:bb348c97df44 | 616 | /* Check the parameters */ |
lypinator | 0:bb348c97df44 | 617 | assert_param(IS_CRYP_KEYSIZE(hcryp->Init.KeySize)); |
lypinator | 0:bb348c97df44 | 618 | assert_param(IS_CRYP_DATATYPE(hcryp->Init.DataType)); |
lypinator | 0:bb348c97df44 | 619 | |
lypinator | 0:bb348c97df44 | 620 | if(hcryp->State == HAL_CRYP_STATE_RESET) |
lypinator | 0:bb348c97df44 | 621 | { |
lypinator | 0:bb348c97df44 | 622 | /* Allocate lock resource and initialize it */ |
lypinator | 0:bb348c97df44 | 623 | hcryp->Lock = HAL_UNLOCKED; |
lypinator | 0:bb348c97df44 | 624 | /* Init the low level hardware */ |
lypinator | 0:bb348c97df44 | 625 | HAL_CRYP_MspInit(hcryp); |
lypinator | 0:bb348c97df44 | 626 | } |
lypinator | 0:bb348c97df44 | 627 | |
lypinator | 0:bb348c97df44 | 628 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 629 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 630 | |
lypinator | 0:bb348c97df44 | 631 | /* Set the key size and data type*/ |
lypinator | 0:bb348c97df44 | 632 | CRYP->CR = (uint32_t) (hcryp->Init.KeySize | hcryp->Init.DataType); |
lypinator | 0:bb348c97df44 | 633 | |
lypinator | 0:bb348c97df44 | 634 | /* Reset CrypInCount and CrypOutCount */ |
lypinator | 0:bb348c97df44 | 635 | hcryp->CrypInCount = 0U; |
lypinator | 0:bb348c97df44 | 636 | hcryp->CrypOutCount = 0U; |
lypinator | 0:bb348c97df44 | 637 | |
lypinator | 0:bb348c97df44 | 638 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 639 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 640 | |
lypinator | 0:bb348c97df44 | 641 | /* Set the default CRYP phase */ |
lypinator | 0:bb348c97df44 | 642 | hcryp->Phase = HAL_CRYP_PHASE_READY; |
lypinator | 0:bb348c97df44 | 643 | |
lypinator | 0:bb348c97df44 | 644 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 645 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 646 | } |
lypinator | 0:bb348c97df44 | 647 | |
lypinator | 0:bb348c97df44 | 648 | /** |
lypinator | 0:bb348c97df44 | 649 | * @brief DeInitializes the CRYP peripheral. |
lypinator | 0:bb348c97df44 | 650 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 651 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 652 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 653 | */ |
lypinator | 0:bb348c97df44 | 654 | HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp) |
lypinator | 0:bb348c97df44 | 655 | { |
lypinator | 0:bb348c97df44 | 656 | /* Check the CRYP handle allocation */ |
lypinator | 0:bb348c97df44 | 657 | if(hcryp == NULL) |
lypinator | 0:bb348c97df44 | 658 | { |
lypinator | 0:bb348c97df44 | 659 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 660 | } |
lypinator | 0:bb348c97df44 | 661 | |
lypinator | 0:bb348c97df44 | 662 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 663 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 664 | |
lypinator | 0:bb348c97df44 | 665 | /* Set the default CRYP phase */ |
lypinator | 0:bb348c97df44 | 666 | hcryp->Phase = HAL_CRYP_PHASE_READY; |
lypinator | 0:bb348c97df44 | 667 | |
lypinator | 0:bb348c97df44 | 668 | /* Reset CrypInCount and CrypOutCount */ |
lypinator | 0:bb348c97df44 | 669 | hcryp->CrypInCount = 0U; |
lypinator | 0:bb348c97df44 | 670 | hcryp->CrypOutCount = 0U; |
lypinator | 0:bb348c97df44 | 671 | |
lypinator | 0:bb348c97df44 | 672 | /* Disable the CRYP Peripheral Clock */ |
lypinator | 0:bb348c97df44 | 673 | __HAL_CRYP_DISABLE(hcryp); |
lypinator | 0:bb348c97df44 | 674 | |
lypinator | 0:bb348c97df44 | 675 | /* DeInit the low level hardware: CLOCK, NVIC.*/ |
lypinator | 0:bb348c97df44 | 676 | HAL_CRYP_MspDeInit(hcryp); |
lypinator | 0:bb348c97df44 | 677 | |
lypinator | 0:bb348c97df44 | 678 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 679 | hcryp->State = HAL_CRYP_STATE_RESET; |
lypinator | 0:bb348c97df44 | 680 | |
lypinator | 0:bb348c97df44 | 681 | /* Release Lock */ |
lypinator | 0:bb348c97df44 | 682 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 683 | |
lypinator | 0:bb348c97df44 | 684 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 685 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 686 | } |
lypinator | 0:bb348c97df44 | 687 | |
lypinator | 0:bb348c97df44 | 688 | /** |
lypinator | 0:bb348c97df44 | 689 | * @brief Initializes the CRYP MSP. |
lypinator | 0:bb348c97df44 | 690 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 691 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 692 | * @retval None |
lypinator | 0:bb348c97df44 | 693 | */ |
lypinator | 0:bb348c97df44 | 694 | __weak void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp) |
lypinator | 0:bb348c97df44 | 695 | { |
lypinator | 0:bb348c97df44 | 696 | /* Prevent unused argument(s) compilation warning */ |
lypinator | 0:bb348c97df44 | 697 | UNUSED(hcryp); |
lypinator | 0:bb348c97df44 | 698 | /* NOTE : This function Should not be modified, when the callback is needed, |
lypinator | 0:bb348c97df44 | 699 | the HAL_CRYP_MspInit could be implemented in the user file |
lypinator | 0:bb348c97df44 | 700 | */ |
lypinator | 0:bb348c97df44 | 701 | } |
lypinator | 0:bb348c97df44 | 702 | |
lypinator | 0:bb348c97df44 | 703 | /** |
lypinator | 0:bb348c97df44 | 704 | * @brief DeInitializes CRYP MSP. |
lypinator | 0:bb348c97df44 | 705 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 706 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 707 | * @retval None |
lypinator | 0:bb348c97df44 | 708 | */ |
lypinator | 0:bb348c97df44 | 709 | __weak void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp) |
lypinator | 0:bb348c97df44 | 710 | { |
lypinator | 0:bb348c97df44 | 711 | /* Prevent unused argument(s) compilation warning */ |
lypinator | 0:bb348c97df44 | 712 | UNUSED(hcryp); |
lypinator | 0:bb348c97df44 | 713 | /* NOTE : This function Should not be modified, when the callback is needed, |
lypinator | 0:bb348c97df44 | 714 | the HAL_CRYP_MspDeInit could be implemented in the user file |
lypinator | 0:bb348c97df44 | 715 | */ |
lypinator | 0:bb348c97df44 | 716 | } |
lypinator | 0:bb348c97df44 | 717 | |
lypinator | 0:bb348c97df44 | 718 | /** |
lypinator | 0:bb348c97df44 | 719 | * @} |
lypinator | 0:bb348c97df44 | 720 | */ |
lypinator | 0:bb348c97df44 | 721 | |
lypinator | 0:bb348c97df44 | 722 | /** @defgroup CRYP_Exported_Functions_Group2 AES processing functions |
lypinator | 0:bb348c97df44 | 723 | * @brief processing functions. |
lypinator | 0:bb348c97df44 | 724 | * |
lypinator | 0:bb348c97df44 | 725 | @verbatim |
lypinator | 0:bb348c97df44 | 726 | ============================================================================== |
lypinator | 0:bb348c97df44 | 727 | ##### AES processing functions ##### |
lypinator | 0:bb348c97df44 | 728 | ============================================================================== |
lypinator | 0:bb348c97df44 | 729 | [..] This section provides functions allowing to: |
lypinator | 0:bb348c97df44 | 730 | (+) Encrypt plaintext using AES-128/192/256 using chaining modes |
lypinator | 0:bb348c97df44 | 731 | (+) Decrypt cyphertext using AES-128/192/256 using chaining modes |
lypinator | 0:bb348c97df44 | 732 | [..] Three processing functions are available: |
lypinator | 0:bb348c97df44 | 733 | (+) Polling mode |
lypinator | 0:bb348c97df44 | 734 | (+) Interrupt mode |
lypinator | 0:bb348c97df44 | 735 | (+) DMA mode |
lypinator | 0:bb348c97df44 | 736 | |
lypinator | 0:bb348c97df44 | 737 | @endverbatim |
lypinator | 0:bb348c97df44 | 738 | * @{ |
lypinator | 0:bb348c97df44 | 739 | */ |
lypinator | 0:bb348c97df44 | 740 | |
lypinator | 0:bb348c97df44 | 741 | /** |
lypinator | 0:bb348c97df44 | 742 | * @brief Initializes the CRYP peripheral in AES ECB encryption mode |
lypinator | 0:bb348c97df44 | 743 | * then encrypt pPlainData. The cypher data are available in pCypherData |
lypinator | 0:bb348c97df44 | 744 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 745 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 746 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 747 | * @param Size Length of the plaintext buffer, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 748 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 749 | * @param Timeout Specify Timeout value |
lypinator | 0:bb348c97df44 | 750 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 751 | */ |
lypinator | 0:bb348c97df44 | 752 | HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 753 | { |
lypinator | 0:bb348c97df44 | 754 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 755 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 756 | |
lypinator | 0:bb348c97df44 | 757 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 758 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 759 | |
lypinator | 0:bb348c97df44 | 760 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 761 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 762 | { |
lypinator | 0:bb348c97df44 | 763 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 764 | CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
lypinator | 0:bb348c97df44 | 765 | |
lypinator | 0:bb348c97df44 | 766 | /* Set the CRYP peripheral in AES ECB mode */ |
lypinator | 0:bb348c97df44 | 767 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB); |
lypinator | 0:bb348c97df44 | 768 | |
lypinator | 0:bb348c97df44 | 769 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 770 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 771 | |
lypinator | 0:bb348c97df44 | 772 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 773 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 774 | |
lypinator | 0:bb348c97df44 | 775 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 776 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 777 | } |
lypinator | 0:bb348c97df44 | 778 | |
lypinator | 0:bb348c97df44 | 779 | /* Write Plain Data and Get Cypher Data */ |
lypinator | 0:bb348c97df44 | 780 | if(CRYP_ProcessData(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 781 | { |
lypinator | 0:bb348c97df44 | 782 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 783 | } |
lypinator | 0:bb348c97df44 | 784 | |
lypinator | 0:bb348c97df44 | 785 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 786 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 787 | |
lypinator | 0:bb348c97df44 | 788 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 789 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 790 | |
lypinator | 0:bb348c97df44 | 791 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 792 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 793 | } |
lypinator | 0:bb348c97df44 | 794 | |
lypinator | 0:bb348c97df44 | 795 | /** |
lypinator | 0:bb348c97df44 | 796 | * @brief Initializes the CRYP peripheral in AES CBC encryption mode |
lypinator | 0:bb348c97df44 | 797 | * then encrypt pPlainData. The cypher data are available in pCypherData |
lypinator | 0:bb348c97df44 | 798 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 799 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 800 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 801 | * @param Size Length of the plaintext buffer, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 802 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 803 | * @param Timeout Specify Timeout value |
lypinator | 0:bb348c97df44 | 804 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 805 | */ |
lypinator | 0:bb348c97df44 | 806 | HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 807 | { |
lypinator | 0:bb348c97df44 | 808 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 809 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 810 | |
lypinator | 0:bb348c97df44 | 811 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 812 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 813 | |
lypinator | 0:bb348c97df44 | 814 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 815 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 816 | { |
lypinator | 0:bb348c97df44 | 817 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 818 | CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
lypinator | 0:bb348c97df44 | 819 | |
lypinator | 0:bb348c97df44 | 820 | /* Set the CRYP peripheral in AES ECB mode */ |
lypinator | 0:bb348c97df44 | 821 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC); |
lypinator | 0:bb348c97df44 | 822 | |
lypinator | 0:bb348c97df44 | 823 | /* Set the Initialization Vector */ |
lypinator | 0:bb348c97df44 | 824 | CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B); |
lypinator | 0:bb348c97df44 | 825 | |
lypinator | 0:bb348c97df44 | 826 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 827 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 828 | |
lypinator | 0:bb348c97df44 | 829 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 830 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 831 | |
lypinator | 0:bb348c97df44 | 832 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 833 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 834 | } |
lypinator | 0:bb348c97df44 | 835 | |
lypinator | 0:bb348c97df44 | 836 | /* Write Plain Data and Get Cypher Data */ |
lypinator | 0:bb348c97df44 | 837 | if(CRYP_ProcessData(hcryp,pPlainData, Size, pCypherData, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 838 | { |
lypinator | 0:bb348c97df44 | 839 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 840 | } |
lypinator | 0:bb348c97df44 | 841 | |
lypinator | 0:bb348c97df44 | 842 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 843 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 844 | |
lypinator | 0:bb348c97df44 | 845 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 846 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 847 | |
lypinator | 0:bb348c97df44 | 848 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 849 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 850 | } |
lypinator | 0:bb348c97df44 | 851 | |
lypinator | 0:bb348c97df44 | 852 | /** |
lypinator | 0:bb348c97df44 | 853 | * @brief Initializes the CRYP peripheral in AES CTR encryption mode |
lypinator | 0:bb348c97df44 | 854 | * then encrypt pPlainData. The cypher data are available in pCypherData |
lypinator | 0:bb348c97df44 | 855 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 856 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 857 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 858 | * @param Size Length of the plaintext buffer, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 859 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 860 | * @param Timeout Specify Timeout value |
lypinator | 0:bb348c97df44 | 861 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 862 | */ |
lypinator | 0:bb348c97df44 | 863 | HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 864 | { |
lypinator | 0:bb348c97df44 | 865 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 866 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 867 | |
lypinator | 0:bb348c97df44 | 868 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 869 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 870 | |
lypinator | 0:bb348c97df44 | 871 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 872 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 873 | { |
lypinator | 0:bb348c97df44 | 874 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 875 | CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
lypinator | 0:bb348c97df44 | 876 | |
lypinator | 0:bb348c97df44 | 877 | /* Set the CRYP peripheral in AES ECB mode */ |
lypinator | 0:bb348c97df44 | 878 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR); |
lypinator | 0:bb348c97df44 | 879 | |
lypinator | 0:bb348c97df44 | 880 | /* Set the Initialization Vector */ |
lypinator | 0:bb348c97df44 | 881 | CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B); |
lypinator | 0:bb348c97df44 | 882 | |
lypinator | 0:bb348c97df44 | 883 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 884 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 885 | |
lypinator | 0:bb348c97df44 | 886 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 887 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 888 | |
lypinator | 0:bb348c97df44 | 889 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 890 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 891 | } |
lypinator | 0:bb348c97df44 | 892 | |
lypinator | 0:bb348c97df44 | 893 | /* Write Plain Data and Get Cypher Data */ |
lypinator | 0:bb348c97df44 | 894 | if(CRYP_ProcessData(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 895 | { |
lypinator | 0:bb348c97df44 | 896 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 897 | } |
lypinator | 0:bb348c97df44 | 898 | |
lypinator | 0:bb348c97df44 | 899 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 900 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 901 | |
lypinator | 0:bb348c97df44 | 902 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 903 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 904 | |
lypinator | 0:bb348c97df44 | 905 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 906 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 907 | } |
lypinator | 0:bb348c97df44 | 908 | |
lypinator | 0:bb348c97df44 | 909 | |
lypinator | 0:bb348c97df44 | 910 | |
lypinator | 0:bb348c97df44 | 911 | /** |
lypinator | 0:bb348c97df44 | 912 | * @brief Initializes the CRYP peripheral in AES ECB decryption mode |
lypinator | 0:bb348c97df44 | 913 | * then decrypted pCypherData. The cypher data are available in pPlainData |
lypinator | 0:bb348c97df44 | 914 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 915 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 916 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 917 | * @param Size Length of the plaintext buffer, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 918 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 919 | * @param Timeout Specify Timeout value |
lypinator | 0:bb348c97df44 | 920 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 921 | */ |
lypinator | 0:bb348c97df44 | 922 | HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 923 | { |
lypinator | 0:bb348c97df44 | 924 | uint32_t tickstart = 0U; |
lypinator | 0:bb348c97df44 | 925 | |
lypinator | 0:bb348c97df44 | 926 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 927 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 928 | |
lypinator | 0:bb348c97df44 | 929 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 930 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 931 | |
lypinator | 0:bb348c97df44 | 932 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 933 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 934 | { |
lypinator | 0:bb348c97df44 | 935 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 936 | CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
lypinator | 0:bb348c97df44 | 937 | |
lypinator | 0:bb348c97df44 | 938 | /* Set the CRYP peripheral in AES Key mode */ |
lypinator | 0:bb348c97df44 | 939 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR); |
lypinator | 0:bb348c97df44 | 940 | |
lypinator | 0:bb348c97df44 | 941 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 942 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 943 | |
lypinator | 0:bb348c97df44 | 944 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 945 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 946 | |
lypinator | 0:bb348c97df44 | 947 | while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY)) |
lypinator | 0:bb348c97df44 | 948 | { |
lypinator | 0:bb348c97df44 | 949 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 950 | if(Timeout != HAL_MAX_DELAY) |
lypinator | 0:bb348c97df44 | 951 | { |
lypinator | 0:bb348c97df44 | 952 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
lypinator | 0:bb348c97df44 | 953 | { |
lypinator | 0:bb348c97df44 | 954 | /* Change state */ |
lypinator | 0:bb348c97df44 | 955 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 956 | |
lypinator | 0:bb348c97df44 | 957 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 958 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 959 | |
lypinator | 0:bb348c97df44 | 960 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 961 | } |
lypinator | 0:bb348c97df44 | 962 | } |
lypinator | 0:bb348c97df44 | 963 | } |
lypinator | 0:bb348c97df44 | 964 | |
lypinator | 0:bb348c97df44 | 965 | /* Disable CRYP */ |
lypinator | 0:bb348c97df44 | 966 | __HAL_CRYP_DISABLE(hcryp); |
lypinator | 0:bb348c97df44 | 967 | |
lypinator | 0:bb348c97df44 | 968 | /* Reset the ALGOMODE bits*/ |
lypinator | 0:bb348c97df44 | 969 | CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE); |
lypinator | 0:bb348c97df44 | 970 | |
lypinator | 0:bb348c97df44 | 971 | /* Set the CRYP peripheral in AES ECB decryption mode */ |
lypinator | 0:bb348c97df44 | 972 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB | CRYP_CR_ALGODIR); |
lypinator | 0:bb348c97df44 | 973 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 974 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 975 | |
lypinator | 0:bb348c97df44 | 976 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 977 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 978 | |
lypinator | 0:bb348c97df44 | 979 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 980 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 981 | } |
lypinator | 0:bb348c97df44 | 982 | |
lypinator | 0:bb348c97df44 | 983 | /* Write Plain Data and Get Cypher Data */ |
lypinator | 0:bb348c97df44 | 984 | if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 985 | { |
lypinator | 0:bb348c97df44 | 986 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 987 | } |
lypinator | 0:bb348c97df44 | 988 | |
lypinator | 0:bb348c97df44 | 989 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 990 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 991 | |
lypinator | 0:bb348c97df44 | 992 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 993 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 994 | |
lypinator | 0:bb348c97df44 | 995 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 996 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 997 | } |
lypinator | 0:bb348c97df44 | 998 | |
lypinator | 0:bb348c97df44 | 999 | /** |
lypinator | 0:bb348c97df44 | 1000 | * @brief Initializes the CRYP peripheral in AES ECB decryption mode |
lypinator | 0:bb348c97df44 | 1001 | * then decrypted pCypherData. The cypher data are available in pPlainData |
lypinator | 0:bb348c97df44 | 1002 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1003 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 1004 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 1005 | * @param Size Length of the plaintext buffer, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 1006 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 1007 | * @param Timeout Specify Timeout value |
lypinator | 0:bb348c97df44 | 1008 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 1009 | */ |
lypinator | 0:bb348c97df44 | 1010 | HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 1011 | { |
lypinator | 0:bb348c97df44 | 1012 | uint32_t tickstart = 0U; |
lypinator | 0:bb348c97df44 | 1013 | |
lypinator | 0:bb348c97df44 | 1014 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 1015 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1016 | |
lypinator | 0:bb348c97df44 | 1017 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 1018 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 1019 | |
lypinator | 0:bb348c97df44 | 1020 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 1021 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 1022 | { |
lypinator | 0:bb348c97df44 | 1023 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 1024 | CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
lypinator | 0:bb348c97df44 | 1025 | |
lypinator | 0:bb348c97df44 | 1026 | /* Set the CRYP peripheral in AES Key mode */ |
lypinator | 0:bb348c97df44 | 1027 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR); |
lypinator | 0:bb348c97df44 | 1028 | |
lypinator | 0:bb348c97df44 | 1029 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 1030 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 1031 | |
lypinator | 0:bb348c97df44 | 1032 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 1033 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 1034 | |
lypinator | 0:bb348c97df44 | 1035 | while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY)) |
lypinator | 0:bb348c97df44 | 1036 | { |
lypinator | 0:bb348c97df44 | 1037 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 1038 | if(Timeout != HAL_MAX_DELAY) |
lypinator | 0:bb348c97df44 | 1039 | { |
lypinator | 0:bb348c97df44 | 1040 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
lypinator | 0:bb348c97df44 | 1041 | { |
lypinator | 0:bb348c97df44 | 1042 | /* Change state */ |
lypinator | 0:bb348c97df44 | 1043 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1044 | |
lypinator | 0:bb348c97df44 | 1045 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1046 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1047 | |
lypinator | 0:bb348c97df44 | 1048 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1049 | } |
lypinator | 0:bb348c97df44 | 1050 | } |
lypinator | 0:bb348c97df44 | 1051 | } |
lypinator | 0:bb348c97df44 | 1052 | |
lypinator | 0:bb348c97df44 | 1053 | /* Reset the ALGOMODE bits*/ |
lypinator | 0:bb348c97df44 | 1054 | CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE); |
lypinator | 0:bb348c97df44 | 1055 | |
lypinator | 0:bb348c97df44 | 1056 | /* Set the CRYP peripheral in AES CBC decryption mode */ |
lypinator | 0:bb348c97df44 | 1057 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC | CRYP_CR_ALGODIR); |
lypinator | 0:bb348c97df44 | 1058 | |
lypinator | 0:bb348c97df44 | 1059 | /* Set the Initialization Vector */ |
lypinator | 0:bb348c97df44 | 1060 | CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B); |
lypinator | 0:bb348c97df44 | 1061 | |
lypinator | 0:bb348c97df44 | 1062 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 1063 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 1064 | |
lypinator | 0:bb348c97df44 | 1065 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 1066 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 1067 | |
lypinator | 0:bb348c97df44 | 1068 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 1069 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 1070 | } |
lypinator | 0:bb348c97df44 | 1071 | |
lypinator | 0:bb348c97df44 | 1072 | /* Write Plain Data and Get Cypher Data */ |
lypinator | 0:bb348c97df44 | 1073 | if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 1074 | { |
lypinator | 0:bb348c97df44 | 1075 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1076 | } |
lypinator | 0:bb348c97df44 | 1077 | |
lypinator | 0:bb348c97df44 | 1078 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 1079 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 1080 | |
lypinator | 0:bb348c97df44 | 1081 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1082 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1083 | |
lypinator | 0:bb348c97df44 | 1084 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 1085 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1086 | } |
lypinator | 0:bb348c97df44 | 1087 | |
lypinator | 0:bb348c97df44 | 1088 | /** |
lypinator | 0:bb348c97df44 | 1089 | * @brief Initializes the CRYP peripheral in AES CTR decryption mode |
lypinator | 0:bb348c97df44 | 1090 | * then decrypted pCypherData. The cypher data are available in pPlainData |
lypinator | 0:bb348c97df44 | 1091 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1092 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 1093 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 1094 | * @param Size Length of the plaintext buffer, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 1095 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 1096 | * @param Timeout Specify Timeout value |
lypinator | 0:bb348c97df44 | 1097 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 1098 | */ |
lypinator | 0:bb348c97df44 | 1099 | HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 1100 | { |
lypinator | 0:bb348c97df44 | 1101 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 1102 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1103 | |
lypinator | 0:bb348c97df44 | 1104 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 1105 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 1106 | { |
lypinator | 0:bb348c97df44 | 1107 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 1108 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 1109 | |
lypinator | 0:bb348c97df44 | 1110 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 1111 | CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
lypinator | 0:bb348c97df44 | 1112 | |
lypinator | 0:bb348c97df44 | 1113 | /* Set the CRYP peripheral in AES CTR mode */ |
lypinator | 0:bb348c97df44 | 1114 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR | CRYP_CR_ALGODIR); |
lypinator | 0:bb348c97df44 | 1115 | |
lypinator | 0:bb348c97df44 | 1116 | /* Set the Initialization Vector */ |
lypinator | 0:bb348c97df44 | 1117 | CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B); |
lypinator | 0:bb348c97df44 | 1118 | |
lypinator | 0:bb348c97df44 | 1119 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 1120 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 1121 | |
lypinator | 0:bb348c97df44 | 1122 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 1123 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 1124 | |
lypinator | 0:bb348c97df44 | 1125 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 1126 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 1127 | } |
lypinator | 0:bb348c97df44 | 1128 | |
lypinator | 0:bb348c97df44 | 1129 | /* Write Plain Data and Get Cypher Data */ |
lypinator | 0:bb348c97df44 | 1130 | if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 1131 | { |
lypinator | 0:bb348c97df44 | 1132 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1133 | } |
lypinator | 0:bb348c97df44 | 1134 | |
lypinator | 0:bb348c97df44 | 1135 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 1136 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 1137 | |
lypinator | 0:bb348c97df44 | 1138 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1139 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1140 | |
lypinator | 0:bb348c97df44 | 1141 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 1142 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1143 | } |
lypinator | 0:bb348c97df44 | 1144 | |
lypinator | 0:bb348c97df44 | 1145 | /** |
lypinator | 0:bb348c97df44 | 1146 | * @brief Initializes the CRYP peripheral in AES ECB encryption mode using Interrupt. |
lypinator | 0:bb348c97df44 | 1147 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1148 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 1149 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 1150 | * @param Size Length of the plaintext buffer, must be a multiple of 16 bytes |
lypinator | 0:bb348c97df44 | 1151 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 1152 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 1153 | */ |
lypinator | 0:bb348c97df44 | 1154 | HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) |
lypinator | 0:bb348c97df44 | 1155 | { |
lypinator | 0:bb348c97df44 | 1156 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 1157 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 1158 | |
lypinator | 0:bb348c97df44 | 1159 | if(hcryp->State == HAL_CRYP_STATE_READY) |
lypinator | 0:bb348c97df44 | 1160 | { |
lypinator | 0:bb348c97df44 | 1161 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 1162 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1163 | |
lypinator | 0:bb348c97df44 | 1164 | hcryp->CrypInCount = Size; |
lypinator | 0:bb348c97df44 | 1165 | hcryp->pCrypInBuffPtr = pPlainData; |
lypinator | 0:bb348c97df44 | 1166 | hcryp->pCrypOutBuffPtr = pCypherData; |
lypinator | 0:bb348c97df44 | 1167 | hcryp->CrypOutCount = Size; |
lypinator | 0:bb348c97df44 | 1168 | |
lypinator | 0:bb348c97df44 | 1169 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 1170 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 1171 | |
lypinator | 0:bb348c97df44 | 1172 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 1173 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 1174 | { |
lypinator | 0:bb348c97df44 | 1175 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 1176 | CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
lypinator | 0:bb348c97df44 | 1177 | |
lypinator | 0:bb348c97df44 | 1178 | /* Set the CRYP peripheral in AES ECB mode */ |
lypinator | 0:bb348c97df44 | 1179 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB); |
lypinator | 0:bb348c97df44 | 1180 | |
lypinator | 0:bb348c97df44 | 1181 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 1182 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 1183 | |
lypinator | 0:bb348c97df44 | 1184 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 1185 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 1186 | } |
lypinator | 0:bb348c97df44 | 1187 | |
lypinator | 0:bb348c97df44 | 1188 | /* Enable Interrupts */ |
lypinator | 0:bb348c97df44 | 1189 | __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 1190 | |
lypinator | 0:bb348c97df44 | 1191 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 1192 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 1193 | |
lypinator | 0:bb348c97df44 | 1194 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 1195 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1196 | } |
lypinator | 0:bb348c97df44 | 1197 | else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) |
lypinator | 0:bb348c97df44 | 1198 | { |
lypinator | 0:bb348c97df44 | 1199 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
lypinator | 0:bb348c97df44 | 1200 | /* Write the Input block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 1201 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1202 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1203 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1204 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1205 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1206 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1207 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1208 | hcryp->pCrypInBuffPtr += 16U; |
lypinator | 0:bb348c97df44 | 1209 | hcryp->CrypInCount -= 16U; |
lypinator | 0:bb348c97df44 | 1210 | if(hcryp->CrypInCount == 0U) |
lypinator | 0:bb348c97df44 | 1211 | { |
lypinator | 0:bb348c97df44 | 1212 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); |
lypinator | 0:bb348c97df44 | 1213 | /* Call the Input data transfer complete callback */ |
lypinator | 0:bb348c97df44 | 1214 | HAL_CRYP_InCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 1215 | } |
lypinator | 0:bb348c97df44 | 1216 | } |
lypinator | 0:bb348c97df44 | 1217 | else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) |
lypinator | 0:bb348c97df44 | 1218 | { |
lypinator | 0:bb348c97df44 | 1219 | outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; |
lypinator | 0:bb348c97df44 | 1220 | /* Read the Output block from the Output FIFO */ |
lypinator | 0:bb348c97df44 | 1221 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1222 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1223 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1224 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1225 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1226 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1227 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1228 | hcryp->pCrypOutBuffPtr += 16U; |
lypinator | 0:bb348c97df44 | 1229 | hcryp->CrypOutCount -= 16U; |
lypinator | 0:bb348c97df44 | 1230 | if(hcryp->CrypOutCount == 0U) |
lypinator | 0:bb348c97df44 | 1231 | { |
lypinator | 0:bb348c97df44 | 1232 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 1233 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 1234 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1235 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 1236 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 1237 | /* Call Input transfer complete callback */ |
lypinator | 0:bb348c97df44 | 1238 | HAL_CRYP_OutCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 1239 | } |
lypinator | 0:bb348c97df44 | 1240 | } |
lypinator | 0:bb348c97df44 | 1241 | |
lypinator | 0:bb348c97df44 | 1242 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 1243 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1244 | } |
lypinator | 0:bb348c97df44 | 1245 | |
lypinator | 0:bb348c97df44 | 1246 | /** |
lypinator | 0:bb348c97df44 | 1247 | * @brief Initializes the CRYP peripheral in AES CBC encryption mode using Interrupt. |
lypinator | 0:bb348c97df44 | 1248 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1249 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 1250 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 1251 | * @param Size Length of the plaintext buffer, must be a multiple of 16 bytes |
lypinator | 0:bb348c97df44 | 1252 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 1253 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 1254 | */ |
lypinator | 0:bb348c97df44 | 1255 | HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) |
lypinator | 0:bb348c97df44 | 1256 | { |
lypinator | 0:bb348c97df44 | 1257 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 1258 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 1259 | |
lypinator | 0:bb348c97df44 | 1260 | if(hcryp->State == HAL_CRYP_STATE_READY) |
lypinator | 0:bb348c97df44 | 1261 | { |
lypinator | 0:bb348c97df44 | 1262 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 1263 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1264 | |
lypinator | 0:bb348c97df44 | 1265 | hcryp->CrypInCount = Size; |
lypinator | 0:bb348c97df44 | 1266 | hcryp->pCrypInBuffPtr = pPlainData; |
lypinator | 0:bb348c97df44 | 1267 | hcryp->pCrypOutBuffPtr = pCypherData; |
lypinator | 0:bb348c97df44 | 1268 | hcryp->CrypOutCount = Size; |
lypinator | 0:bb348c97df44 | 1269 | |
lypinator | 0:bb348c97df44 | 1270 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 1271 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 1272 | |
lypinator | 0:bb348c97df44 | 1273 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 1274 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 1275 | { |
lypinator | 0:bb348c97df44 | 1276 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 1277 | CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
lypinator | 0:bb348c97df44 | 1278 | |
lypinator | 0:bb348c97df44 | 1279 | /* Set the CRYP peripheral in AES CBC mode */ |
lypinator | 0:bb348c97df44 | 1280 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC); |
lypinator | 0:bb348c97df44 | 1281 | |
lypinator | 0:bb348c97df44 | 1282 | /* Set the Initialization Vector */ |
lypinator | 0:bb348c97df44 | 1283 | CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B); |
lypinator | 0:bb348c97df44 | 1284 | |
lypinator | 0:bb348c97df44 | 1285 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 1286 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 1287 | |
lypinator | 0:bb348c97df44 | 1288 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 1289 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 1290 | } |
lypinator | 0:bb348c97df44 | 1291 | /* Enable Interrupts */ |
lypinator | 0:bb348c97df44 | 1292 | __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 1293 | |
lypinator | 0:bb348c97df44 | 1294 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 1295 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 1296 | |
lypinator | 0:bb348c97df44 | 1297 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 1298 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1299 | } |
lypinator | 0:bb348c97df44 | 1300 | else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) |
lypinator | 0:bb348c97df44 | 1301 | { |
lypinator | 0:bb348c97df44 | 1302 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
lypinator | 0:bb348c97df44 | 1303 | /* Write the Input block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 1304 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1305 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1306 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1307 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1308 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1309 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1310 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1311 | hcryp->pCrypInBuffPtr += 16U; |
lypinator | 0:bb348c97df44 | 1312 | hcryp->CrypInCount -= 16U; |
lypinator | 0:bb348c97df44 | 1313 | if(hcryp->CrypInCount == 0U) |
lypinator | 0:bb348c97df44 | 1314 | { |
lypinator | 0:bb348c97df44 | 1315 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); |
lypinator | 0:bb348c97df44 | 1316 | /* Call the Input data transfer complete callback */ |
lypinator | 0:bb348c97df44 | 1317 | HAL_CRYP_InCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 1318 | } |
lypinator | 0:bb348c97df44 | 1319 | } |
lypinator | 0:bb348c97df44 | 1320 | else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) |
lypinator | 0:bb348c97df44 | 1321 | { |
lypinator | 0:bb348c97df44 | 1322 | outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; |
lypinator | 0:bb348c97df44 | 1323 | /* Read the Output block from the Output FIFO */ |
lypinator | 0:bb348c97df44 | 1324 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1325 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1326 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1327 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1328 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1329 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1330 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1331 | hcryp->pCrypOutBuffPtr += 16U; |
lypinator | 0:bb348c97df44 | 1332 | hcryp->CrypOutCount -= 16U; |
lypinator | 0:bb348c97df44 | 1333 | if(hcryp->CrypOutCount == 0U) |
lypinator | 0:bb348c97df44 | 1334 | { |
lypinator | 0:bb348c97df44 | 1335 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 1336 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 1337 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1338 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 1339 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 1340 | /* Call Input transfer complete callback */ |
lypinator | 0:bb348c97df44 | 1341 | HAL_CRYP_OutCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 1342 | } |
lypinator | 0:bb348c97df44 | 1343 | } |
lypinator | 0:bb348c97df44 | 1344 | |
lypinator | 0:bb348c97df44 | 1345 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 1346 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1347 | } |
lypinator | 0:bb348c97df44 | 1348 | |
lypinator | 0:bb348c97df44 | 1349 | /** |
lypinator | 0:bb348c97df44 | 1350 | * @brief Initializes the CRYP peripheral in AES CTR encryption mode using Interrupt. |
lypinator | 0:bb348c97df44 | 1351 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1352 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 1353 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 1354 | * @param Size Length of the plaintext buffer, must be a multiple of 16 bytes |
lypinator | 0:bb348c97df44 | 1355 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 1356 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 1357 | */ |
lypinator | 0:bb348c97df44 | 1358 | HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) |
lypinator | 0:bb348c97df44 | 1359 | { |
lypinator | 0:bb348c97df44 | 1360 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 1361 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 1362 | |
lypinator | 0:bb348c97df44 | 1363 | if(hcryp->State == HAL_CRYP_STATE_READY) |
lypinator | 0:bb348c97df44 | 1364 | { |
lypinator | 0:bb348c97df44 | 1365 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 1366 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1367 | |
lypinator | 0:bb348c97df44 | 1368 | hcryp->CrypInCount = Size; |
lypinator | 0:bb348c97df44 | 1369 | hcryp->pCrypInBuffPtr = pPlainData; |
lypinator | 0:bb348c97df44 | 1370 | hcryp->pCrypOutBuffPtr = pCypherData; |
lypinator | 0:bb348c97df44 | 1371 | hcryp->CrypOutCount = Size; |
lypinator | 0:bb348c97df44 | 1372 | |
lypinator | 0:bb348c97df44 | 1373 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 1374 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 1375 | |
lypinator | 0:bb348c97df44 | 1376 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 1377 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 1378 | { |
lypinator | 0:bb348c97df44 | 1379 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 1380 | CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
lypinator | 0:bb348c97df44 | 1381 | |
lypinator | 0:bb348c97df44 | 1382 | /* Set the CRYP peripheral in AES CTR mode */ |
lypinator | 0:bb348c97df44 | 1383 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR); |
lypinator | 0:bb348c97df44 | 1384 | |
lypinator | 0:bb348c97df44 | 1385 | /* Set the Initialization Vector */ |
lypinator | 0:bb348c97df44 | 1386 | CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B); |
lypinator | 0:bb348c97df44 | 1387 | |
lypinator | 0:bb348c97df44 | 1388 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 1389 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 1390 | |
lypinator | 0:bb348c97df44 | 1391 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 1392 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 1393 | } |
lypinator | 0:bb348c97df44 | 1394 | /* Enable Interrupts */ |
lypinator | 0:bb348c97df44 | 1395 | __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 1396 | |
lypinator | 0:bb348c97df44 | 1397 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 1398 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 1399 | |
lypinator | 0:bb348c97df44 | 1400 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 1401 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1402 | } |
lypinator | 0:bb348c97df44 | 1403 | else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) |
lypinator | 0:bb348c97df44 | 1404 | { |
lypinator | 0:bb348c97df44 | 1405 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
lypinator | 0:bb348c97df44 | 1406 | /* Write the Input block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 1407 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1408 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1409 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1410 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1411 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1412 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1413 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1414 | hcryp->pCrypInBuffPtr += 16U; |
lypinator | 0:bb348c97df44 | 1415 | hcryp->CrypInCount -= 16U; |
lypinator | 0:bb348c97df44 | 1416 | if(hcryp->CrypInCount == 0U) |
lypinator | 0:bb348c97df44 | 1417 | { |
lypinator | 0:bb348c97df44 | 1418 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); |
lypinator | 0:bb348c97df44 | 1419 | /* Call the Input data transfer complete callback */ |
lypinator | 0:bb348c97df44 | 1420 | HAL_CRYP_InCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 1421 | } |
lypinator | 0:bb348c97df44 | 1422 | } |
lypinator | 0:bb348c97df44 | 1423 | else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) |
lypinator | 0:bb348c97df44 | 1424 | { |
lypinator | 0:bb348c97df44 | 1425 | outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; |
lypinator | 0:bb348c97df44 | 1426 | /* Read the Output block from the Output FIFO */ |
lypinator | 0:bb348c97df44 | 1427 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1428 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1429 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1430 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1431 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1432 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1433 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1434 | hcryp->pCrypOutBuffPtr += 16U; |
lypinator | 0:bb348c97df44 | 1435 | hcryp->CrypOutCount -= 16U; |
lypinator | 0:bb348c97df44 | 1436 | if(hcryp->CrypOutCount == 0U) |
lypinator | 0:bb348c97df44 | 1437 | { |
lypinator | 0:bb348c97df44 | 1438 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 1439 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1440 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1441 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 1442 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 1443 | /* Call Input transfer complete callback */ |
lypinator | 0:bb348c97df44 | 1444 | HAL_CRYP_OutCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 1445 | } |
lypinator | 0:bb348c97df44 | 1446 | } |
lypinator | 0:bb348c97df44 | 1447 | |
lypinator | 0:bb348c97df44 | 1448 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 1449 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1450 | } |
lypinator | 0:bb348c97df44 | 1451 | |
lypinator | 0:bb348c97df44 | 1452 | |
lypinator | 0:bb348c97df44 | 1453 | /** |
lypinator | 0:bb348c97df44 | 1454 | * @brief Initializes the CRYP peripheral in AES ECB decryption mode using Interrupt. |
lypinator | 0:bb348c97df44 | 1455 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1456 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 1457 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 1458 | * @param Size Length of the plaintext buffer, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 1459 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 1460 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 1461 | */ |
lypinator | 0:bb348c97df44 | 1462 | HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) |
lypinator | 0:bb348c97df44 | 1463 | { |
lypinator | 0:bb348c97df44 | 1464 | uint32_t tickstart = 0U; |
lypinator | 0:bb348c97df44 | 1465 | |
lypinator | 0:bb348c97df44 | 1466 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 1467 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 1468 | |
lypinator | 0:bb348c97df44 | 1469 | if(hcryp->State == HAL_CRYP_STATE_READY) |
lypinator | 0:bb348c97df44 | 1470 | { |
lypinator | 0:bb348c97df44 | 1471 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 1472 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1473 | |
lypinator | 0:bb348c97df44 | 1474 | hcryp->CrypInCount = Size; |
lypinator | 0:bb348c97df44 | 1475 | hcryp->pCrypInBuffPtr = pCypherData; |
lypinator | 0:bb348c97df44 | 1476 | hcryp->pCrypOutBuffPtr = pPlainData; |
lypinator | 0:bb348c97df44 | 1477 | hcryp->CrypOutCount = Size; |
lypinator | 0:bb348c97df44 | 1478 | |
lypinator | 0:bb348c97df44 | 1479 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 1480 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 1481 | |
lypinator | 0:bb348c97df44 | 1482 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 1483 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 1484 | { |
lypinator | 0:bb348c97df44 | 1485 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 1486 | CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
lypinator | 0:bb348c97df44 | 1487 | |
lypinator | 0:bb348c97df44 | 1488 | /* Set the CRYP peripheral in AES Key mode */ |
lypinator | 0:bb348c97df44 | 1489 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR); |
lypinator | 0:bb348c97df44 | 1490 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 1491 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 1492 | |
lypinator | 0:bb348c97df44 | 1493 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 1494 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 1495 | |
lypinator | 0:bb348c97df44 | 1496 | while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY)) |
lypinator | 0:bb348c97df44 | 1497 | { |
lypinator | 0:bb348c97df44 | 1498 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 1499 | if((HAL_GetTick() - tickstart ) > CRYP_TIMEOUT_VALUE) |
lypinator | 0:bb348c97df44 | 1500 | { |
lypinator | 0:bb348c97df44 | 1501 | /* Change state */ |
lypinator | 0:bb348c97df44 | 1502 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1503 | |
lypinator | 0:bb348c97df44 | 1504 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1505 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1506 | |
lypinator | 0:bb348c97df44 | 1507 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1508 | } |
lypinator | 0:bb348c97df44 | 1509 | } |
lypinator | 0:bb348c97df44 | 1510 | |
lypinator | 0:bb348c97df44 | 1511 | /* Reset the ALGOMODE bits*/ |
lypinator | 0:bb348c97df44 | 1512 | CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE); |
lypinator | 0:bb348c97df44 | 1513 | |
lypinator | 0:bb348c97df44 | 1514 | /* Set the CRYP peripheral in AES ECB decryption mode */ |
lypinator | 0:bb348c97df44 | 1515 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB | CRYP_CR_ALGODIR); |
lypinator | 0:bb348c97df44 | 1516 | |
lypinator | 0:bb348c97df44 | 1517 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 1518 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 1519 | |
lypinator | 0:bb348c97df44 | 1520 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 1521 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 1522 | } |
lypinator | 0:bb348c97df44 | 1523 | |
lypinator | 0:bb348c97df44 | 1524 | /* Enable Interrupts */ |
lypinator | 0:bb348c97df44 | 1525 | __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 1526 | |
lypinator | 0:bb348c97df44 | 1527 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 1528 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 1529 | |
lypinator | 0:bb348c97df44 | 1530 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 1531 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1532 | } |
lypinator | 0:bb348c97df44 | 1533 | else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) |
lypinator | 0:bb348c97df44 | 1534 | { |
lypinator | 0:bb348c97df44 | 1535 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
lypinator | 0:bb348c97df44 | 1536 | /* Write the Input block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 1537 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1538 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1539 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1540 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1541 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1542 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1543 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1544 | hcryp->pCrypInBuffPtr += 16U; |
lypinator | 0:bb348c97df44 | 1545 | hcryp->CrypInCount -= 16U; |
lypinator | 0:bb348c97df44 | 1546 | if(hcryp->CrypInCount == 0U) |
lypinator | 0:bb348c97df44 | 1547 | { |
lypinator | 0:bb348c97df44 | 1548 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); |
lypinator | 0:bb348c97df44 | 1549 | /* Call the Input data transfer complete callback */ |
lypinator | 0:bb348c97df44 | 1550 | HAL_CRYP_InCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 1551 | } |
lypinator | 0:bb348c97df44 | 1552 | } |
lypinator | 0:bb348c97df44 | 1553 | else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) |
lypinator | 0:bb348c97df44 | 1554 | { |
lypinator | 0:bb348c97df44 | 1555 | outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; |
lypinator | 0:bb348c97df44 | 1556 | /* Read the Output block from the Output FIFO */ |
lypinator | 0:bb348c97df44 | 1557 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1558 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1559 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1560 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1561 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1562 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1563 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1564 | hcryp->pCrypOutBuffPtr += 16U; |
lypinator | 0:bb348c97df44 | 1565 | hcryp->CrypOutCount -= 16U; |
lypinator | 0:bb348c97df44 | 1566 | if(hcryp->CrypOutCount == 0U) |
lypinator | 0:bb348c97df44 | 1567 | { |
lypinator | 0:bb348c97df44 | 1568 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 1569 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1570 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1571 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 1572 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 1573 | /* Call Input transfer complete callback */ |
lypinator | 0:bb348c97df44 | 1574 | HAL_CRYP_OutCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 1575 | } |
lypinator | 0:bb348c97df44 | 1576 | } |
lypinator | 0:bb348c97df44 | 1577 | |
lypinator | 0:bb348c97df44 | 1578 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 1579 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1580 | } |
lypinator | 0:bb348c97df44 | 1581 | |
lypinator | 0:bb348c97df44 | 1582 | /** |
lypinator | 0:bb348c97df44 | 1583 | * @brief Initializes the CRYP peripheral in AES CBC decryption mode using IT. |
lypinator | 0:bb348c97df44 | 1584 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1585 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 1586 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 1587 | * @param Size Length of the plaintext buffer, must be a multiple of 16 |
lypinator | 0:bb348c97df44 | 1588 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 1589 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 1590 | */ |
lypinator | 0:bb348c97df44 | 1591 | HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) |
lypinator | 0:bb348c97df44 | 1592 | { |
lypinator | 0:bb348c97df44 | 1593 | |
lypinator | 0:bb348c97df44 | 1594 | uint32_t tickstart = 0U; |
lypinator | 0:bb348c97df44 | 1595 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 1596 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 1597 | |
lypinator | 0:bb348c97df44 | 1598 | if(hcryp->State == HAL_CRYP_STATE_READY) |
lypinator | 0:bb348c97df44 | 1599 | { |
lypinator | 0:bb348c97df44 | 1600 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 1601 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1602 | |
lypinator | 0:bb348c97df44 | 1603 | /* Get the buffer addresses and sizes */ |
lypinator | 0:bb348c97df44 | 1604 | hcryp->CrypInCount = Size; |
lypinator | 0:bb348c97df44 | 1605 | hcryp->pCrypInBuffPtr = pCypherData; |
lypinator | 0:bb348c97df44 | 1606 | hcryp->pCrypOutBuffPtr = pPlainData; |
lypinator | 0:bb348c97df44 | 1607 | hcryp->CrypOutCount = Size; |
lypinator | 0:bb348c97df44 | 1608 | |
lypinator | 0:bb348c97df44 | 1609 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 1610 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 1611 | |
lypinator | 0:bb348c97df44 | 1612 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 1613 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 1614 | { |
lypinator | 0:bb348c97df44 | 1615 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 1616 | CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
lypinator | 0:bb348c97df44 | 1617 | |
lypinator | 0:bb348c97df44 | 1618 | /* Set the CRYP peripheral in AES Key mode */ |
lypinator | 0:bb348c97df44 | 1619 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR); |
lypinator | 0:bb348c97df44 | 1620 | |
lypinator | 0:bb348c97df44 | 1621 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 1622 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 1623 | |
lypinator | 0:bb348c97df44 | 1624 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 1625 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 1626 | |
lypinator | 0:bb348c97df44 | 1627 | while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY)) |
lypinator | 0:bb348c97df44 | 1628 | { |
lypinator | 0:bb348c97df44 | 1629 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 1630 | if((HAL_GetTick() - tickstart ) > CRYP_TIMEOUT_VALUE) |
lypinator | 0:bb348c97df44 | 1631 | { |
lypinator | 0:bb348c97df44 | 1632 | /* Change state */ |
lypinator | 0:bb348c97df44 | 1633 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1634 | |
lypinator | 0:bb348c97df44 | 1635 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1636 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1637 | |
lypinator | 0:bb348c97df44 | 1638 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1639 | } |
lypinator | 0:bb348c97df44 | 1640 | } |
lypinator | 0:bb348c97df44 | 1641 | |
lypinator | 0:bb348c97df44 | 1642 | /* Reset the ALGOMODE bits*/ |
lypinator | 0:bb348c97df44 | 1643 | CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE); |
lypinator | 0:bb348c97df44 | 1644 | |
lypinator | 0:bb348c97df44 | 1645 | /* Set the CRYP peripheral in AES CBC decryption mode */ |
lypinator | 0:bb348c97df44 | 1646 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC | CRYP_CR_ALGODIR); |
lypinator | 0:bb348c97df44 | 1647 | |
lypinator | 0:bb348c97df44 | 1648 | /* Set the Initialization Vector */ |
lypinator | 0:bb348c97df44 | 1649 | CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B); |
lypinator | 0:bb348c97df44 | 1650 | |
lypinator | 0:bb348c97df44 | 1651 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 1652 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 1653 | |
lypinator | 0:bb348c97df44 | 1654 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 1655 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 1656 | |
lypinator | 0:bb348c97df44 | 1657 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 1658 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 1659 | } |
lypinator | 0:bb348c97df44 | 1660 | |
lypinator | 0:bb348c97df44 | 1661 | /* Enable Interrupts */ |
lypinator | 0:bb348c97df44 | 1662 | __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 1663 | |
lypinator | 0:bb348c97df44 | 1664 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 1665 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 1666 | |
lypinator | 0:bb348c97df44 | 1667 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 1668 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1669 | } |
lypinator | 0:bb348c97df44 | 1670 | else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) |
lypinator | 0:bb348c97df44 | 1671 | { |
lypinator | 0:bb348c97df44 | 1672 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
lypinator | 0:bb348c97df44 | 1673 | /* Write the Input block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 1674 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1675 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1676 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1677 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1678 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1679 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1680 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1681 | hcryp->pCrypInBuffPtr += 16U; |
lypinator | 0:bb348c97df44 | 1682 | hcryp->CrypInCount -= 16U; |
lypinator | 0:bb348c97df44 | 1683 | if(hcryp->CrypInCount == 0U) |
lypinator | 0:bb348c97df44 | 1684 | { |
lypinator | 0:bb348c97df44 | 1685 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); |
lypinator | 0:bb348c97df44 | 1686 | /* Call the Input data transfer complete callback */ |
lypinator | 0:bb348c97df44 | 1687 | HAL_CRYP_InCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 1688 | } |
lypinator | 0:bb348c97df44 | 1689 | } |
lypinator | 0:bb348c97df44 | 1690 | else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) |
lypinator | 0:bb348c97df44 | 1691 | { |
lypinator | 0:bb348c97df44 | 1692 | outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; |
lypinator | 0:bb348c97df44 | 1693 | /* Read the Output block from the Output FIFO */ |
lypinator | 0:bb348c97df44 | 1694 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1695 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1696 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1697 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1698 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1699 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1700 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1701 | hcryp->pCrypOutBuffPtr += 16U; |
lypinator | 0:bb348c97df44 | 1702 | hcryp->CrypOutCount -= 16U; |
lypinator | 0:bb348c97df44 | 1703 | if(hcryp->CrypOutCount == 0U) |
lypinator | 0:bb348c97df44 | 1704 | { |
lypinator | 0:bb348c97df44 | 1705 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 1706 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1707 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1708 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 1709 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 1710 | /* Call Input transfer complete callback */ |
lypinator | 0:bb348c97df44 | 1711 | HAL_CRYP_OutCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 1712 | } |
lypinator | 0:bb348c97df44 | 1713 | } |
lypinator | 0:bb348c97df44 | 1714 | |
lypinator | 0:bb348c97df44 | 1715 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 1716 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1717 | } |
lypinator | 0:bb348c97df44 | 1718 | |
lypinator | 0:bb348c97df44 | 1719 | /** |
lypinator | 0:bb348c97df44 | 1720 | * @brief Initializes the CRYP peripheral in AES CTR decryption mode using Interrupt. |
lypinator | 0:bb348c97df44 | 1721 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1722 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 1723 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 1724 | * @param Size Length of the plaintext buffer, must be a multiple of 16 |
lypinator | 0:bb348c97df44 | 1725 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 1726 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 1727 | */ |
lypinator | 0:bb348c97df44 | 1728 | HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) |
lypinator | 0:bb348c97df44 | 1729 | { |
lypinator | 0:bb348c97df44 | 1730 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 1731 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 1732 | |
lypinator | 0:bb348c97df44 | 1733 | if(hcryp->State == HAL_CRYP_STATE_READY) |
lypinator | 0:bb348c97df44 | 1734 | { |
lypinator | 0:bb348c97df44 | 1735 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 1736 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1737 | |
lypinator | 0:bb348c97df44 | 1738 | /* Get the buffer addresses and sizes */ |
lypinator | 0:bb348c97df44 | 1739 | hcryp->CrypInCount = Size; |
lypinator | 0:bb348c97df44 | 1740 | hcryp->pCrypInBuffPtr = pCypherData; |
lypinator | 0:bb348c97df44 | 1741 | hcryp->pCrypOutBuffPtr = pPlainData; |
lypinator | 0:bb348c97df44 | 1742 | hcryp->CrypOutCount = Size; |
lypinator | 0:bb348c97df44 | 1743 | |
lypinator | 0:bb348c97df44 | 1744 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 1745 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 1746 | |
lypinator | 0:bb348c97df44 | 1747 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 1748 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 1749 | { |
lypinator | 0:bb348c97df44 | 1750 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 1751 | CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
lypinator | 0:bb348c97df44 | 1752 | |
lypinator | 0:bb348c97df44 | 1753 | /* Set the CRYP peripheral in AES CTR mode */ |
lypinator | 0:bb348c97df44 | 1754 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR | CRYP_CR_ALGODIR); |
lypinator | 0:bb348c97df44 | 1755 | |
lypinator | 0:bb348c97df44 | 1756 | /* Set the Initialization Vector */ |
lypinator | 0:bb348c97df44 | 1757 | CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B); |
lypinator | 0:bb348c97df44 | 1758 | |
lypinator | 0:bb348c97df44 | 1759 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 1760 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 1761 | |
lypinator | 0:bb348c97df44 | 1762 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 1763 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 1764 | } |
lypinator | 0:bb348c97df44 | 1765 | |
lypinator | 0:bb348c97df44 | 1766 | /* Enable Interrupts */ |
lypinator | 0:bb348c97df44 | 1767 | __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 1768 | |
lypinator | 0:bb348c97df44 | 1769 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 1770 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 1771 | |
lypinator | 0:bb348c97df44 | 1772 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 1773 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1774 | } |
lypinator | 0:bb348c97df44 | 1775 | else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) |
lypinator | 0:bb348c97df44 | 1776 | { |
lypinator | 0:bb348c97df44 | 1777 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
lypinator | 0:bb348c97df44 | 1778 | /* Write the Input block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 1779 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1780 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1781 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1782 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1783 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1784 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1785 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1786 | hcryp->pCrypInBuffPtr += 16U; |
lypinator | 0:bb348c97df44 | 1787 | hcryp->CrypInCount -= 16U; |
lypinator | 0:bb348c97df44 | 1788 | if(hcryp->CrypInCount == 0U) |
lypinator | 0:bb348c97df44 | 1789 | { |
lypinator | 0:bb348c97df44 | 1790 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); |
lypinator | 0:bb348c97df44 | 1791 | /* Call the Input data transfer complete callback */ |
lypinator | 0:bb348c97df44 | 1792 | HAL_CRYP_InCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 1793 | } |
lypinator | 0:bb348c97df44 | 1794 | } |
lypinator | 0:bb348c97df44 | 1795 | else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) |
lypinator | 0:bb348c97df44 | 1796 | { |
lypinator | 0:bb348c97df44 | 1797 | outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; |
lypinator | 0:bb348c97df44 | 1798 | /* Read the Output block from the Output FIFO */ |
lypinator | 0:bb348c97df44 | 1799 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1800 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1801 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1802 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1803 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1804 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1805 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1806 | hcryp->pCrypOutBuffPtr += 16U; |
lypinator | 0:bb348c97df44 | 1807 | hcryp->CrypOutCount -= 16U; |
lypinator | 0:bb348c97df44 | 1808 | if(hcryp->CrypOutCount == 0U) |
lypinator | 0:bb348c97df44 | 1809 | { |
lypinator | 0:bb348c97df44 | 1810 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 1811 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1812 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1813 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 1814 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 1815 | /* Call Input transfer complete callback */ |
lypinator | 0:bb348c97df44 | 1816 | HAL_CRYP_OutCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 1817 | } |
lypinator | 0:bb348c97df44 | 1818 | } |
lypinator | 0:bb348c97df44 | 1819 | |
lypinator | 0:bb348c97df44 | 1820 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 1821 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1822 | } |
lypinator | 0:bb348c97df44 | 1823 | |
lypinator | 0:bb348c97df44 | 1824 | /** |
lypinator | 0:bb348c97df44 | 1825 | * @brief Initializes the CRYP peripheral in AES ECB encryption mode using DMA. |
lypinator | 0:bb348c97df44 | 1826 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1827 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 1828 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 1829 | * @param Size Length of the plaintext buffer, must be a multiple of 16 bytes |
lypinator | 0:bb348c97df44 | 1830 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 1831 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 1832 | */ |
lypinator | 0:bb348c97df44 | 1833 | HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) |
lypinator | 0:bb348c97df44 | 1834 | { |
lypinator | 0:bb348c97df44 | 1835 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 1836 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 1837 | |
lypinator | 0:bb348c97df44 | 1838 | if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) |
lypinator | 0:bb348c97df44 | 1839 | { |
lypinator | 0:bb348c97df44 | 1840 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 1841 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1842 | |
lypinator | 0:bb348c97df44 | 1843 | inputaddr = (uint32_t)pPlainData; |
lypinator | 0:bb348c97df44 | 1844 | outputaddr = (uint32_t)pCypherData; |
lypinator | 0:bb348c97df44 | 1845 | |
lypinator | 0:bb348c97df44 | 1846 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 1847 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 1848 | |
lypinator | 0:bb348c97df44 | 1849 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 1850 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 1851 | { |
lypinator | 0:bb348c97df44 | 1852 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 1853 | CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
lypinator | 0:bb348c97df44 | 1854 | |
lypinator | 0:bb348c97df44 | 1855 | /* Set the CRYP peripheral in AES ECB mode */ |
lypinator | 0:bb348c97df44 | 1856 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB); |
lypinator | 0:bb348c97df44 | 1857 | |
lypinator | 0:bb348c97df44 | 1858 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 1859 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 1860 | |
lypinator | 0:bb348c97df44 | 1861 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 1862 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 1863 | } |
lypinator | 0:bb348c97df44 | 1864 | /* Set the input and output addresses and start DMA transfer */ |
lypinator | 0:bb348c97df44 | 1865 | CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); |
lypinator | 0:bb348c97df44 | 1866 | |
lypinator | 0:bb348c97df44 | 1867 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1868 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1869 | |
lypinator | 0:bb348c97df44 | 1870 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 1871 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1872 | } |
lypinator | 0:bb348c97df44 | 1873 | else |
lypinator | 0:bb348c97df44 | 1874 | { |
lypinator | 0:bb348c97df44 | 1875 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 1876 | } |
lypinator | 0:bb348c97df44 | 1877 | } |
lypinator | 0:bb348c97df44 | 1878 | |
lypinator | 0:bb348c97df44 | 1879 | /** |
lypinator | 0:bb348c97df44 | 1880 | * @brief Initializes the CRYP peripheral in AES CBC encryption mode using DMA. |
lypinator | 0:bb348c97df44 | 1881 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1882 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 1883 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 1884 | * @param Size Length of the plaintext buffer, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 1885 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 1886 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 1887 | */ |
lypinator | 0:bb348c97df44 | 1888 | HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) |
lypinator | 0:bb348c97df44 | 1889 | { |
lypinator | 0:bb348c97df44 | 1890 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 1891 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 1892 | |
lypinator | 0:bb348c97df44 | 1893 | if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) |
lypinator | 0:bb348c97df44 | 1894 | { |
lypinator | 0:bb348c97df44 | 1895 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 1896 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1897 | |
lypinator | 0:bb348c97df44 | 1898 | inputaddr = (uint32_t)pPlainData; |
lypinator | 0:bb348c97df44 | 1899 | outputaddr = (uint32_t)pCypherData; |
lypinator | 0:bb348c97df44 | 1900 | |
lypinator | 0:bb348c97df44 | 1901 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 1902 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 1903 | |
lypinator | 0:bb348c97df44 | 1904 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 1905 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 1906 | { |
lypinator | 0:bb348c97df44 | 1907 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 1908 | CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
lypinator | 0:bb348c97df44 | 1909 | |
lypinator | 0:bb348c97df44 | 1910 | /* Set the CRYP peripheral in AES ECB mode */ |
lypinator | 0:bb348c97df44 | 1911 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC); |
lypinator | 0:bb348c97df44 | 1912 | |
lypinator | 0:bb348c97df44 | 1913 | /* Set the Initialization Vector */ |
lypinator | 0:bb348c97df44 | 1914 | CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B); |
lypinator | 0:bb348c97df44 | 1915 | |
lypinator | 0:bb348c97df44 | 1916 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 1917 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 1918 | |
lypinator | 0:bb348c97df44 | 1919 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 1920 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 1921 | } |
lypinator | 0:bb348c97df44 | 1922 | /* Set the input and output addresses and start DMA transfer */ |
lypinator | 0:bb348c97df44 | 1923 | CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); |
lypinator | 0:bb348c97df44 | 1924 | |
lypinator | 0:bb348c97df44 | 1925 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1926 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1927 | |
lypinator | 0:bb348c97df44 | 1928 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 1929 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1930 | } |
lypinator | 0:bb348c97df44 | 1931 | else |
lypinator | 0:bb348c97df44 | 1932 | { |
lypinator | 0:bb348c97df44 | 1933 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 1934 | } |
lypinator | 0:bb348c97df44 | 1935 | } |
lypinator | 0:bb348c97df44 | 1936 | |
lypinator | 0:bb348c97df44 | 1937 | /** |
lypinator | 0:bb348c97df44 | 1938 | * @brief Initializes the CRYP peripheral in AES CTR encryption mode using DMA. |
lypinator | 0:bb348c97df44 | 1939 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1940 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 1941 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 1942 | * @param Size Length of the plaintext buffer, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 1943 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 1944 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 1945 | */ |
lypinator | 0:bb348c97df44 | 1946 | HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) |
lypinator | 0:bb348c97df44 | 1947 | { |
lypinator | 0:bb348c97df44 | 1948 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 1949 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 1950 | |
lypinator | 0:bb348c97df44 | 1951 | if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) |
lypinator | 0:bb348c97df44 | 1952 | { |
lypinator | 0:bb348c97df44 | 1953 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 1954 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1955 | |
lypinator | 0:bb348c97df44 | 1956 | inputaddr = (uint32_t)pPlainData; |
lypinator | 0:bb348c97df44 | 1957 | outputaddr = (uint32_t)pCypherData; |
lypinator | 0:bb348c97df44 | 1958 | |
lypinator | 0:bb348c97df44 | 1959 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 1960 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 1961 | |
lypinator | 0:bb348c97df44 | 1962 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 1963 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 1964 | { |
lypinator | 0:bb348c97df44 | 1965 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 1966 | CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
lypinator | 0:bb348c97df44 | 1967 | |
lypinator | 0:bb348c97df44 | 1968 | /* Set the CRYP peripheral in AES ECB mode */ |
lypinator | 0:bb348c97df44 | 1969 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR); |
lypinator | 0:bb348c97df44 | 1970 | |
lypinator | 0:bb348c97df44 | 1971 | /* Set the Initialization Vector */ |
lypinator | 0:bb348c97df44 | 1972 | CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B); |
lypinator | 0:bb348c97df44 | 1973 | |
lypinator | 0:bb348c97df44 | 1974 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 1975 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 1976 | |
lypinator | 0:bb348c97df44 | 1977 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 1978 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 1979 | } |
lypinator | 0:bb348c97df44 | 1980 | |
lypinator | 0:bb348c97df44 | 1981 | /* Set the input and output addresses and start DMA transfer */ |
lypinator | 0:bb348c97df44 | 1982 | CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); |
lypinator | 0:bb348c97df44 | 1983 | |
lypinator | 0:bb348c97df44 | 1984 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1985 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1986 | |
lypinator | 0:bb348c97df44 | 1987 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 1988 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1989 | } |
lypinator | 0:bb348c97df44 | 1990 | else |
lypinator | 0:bb348c97df44 | 1991 | { |
lypinator | 0:bb348c97df44 | 1992 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 1993 | } |
lypinator | 0:bb348c97df44 | 1994 | } |
lypinator | 0:bb348c97df44 | 1995 | |
lypinator | 0:bb348c97df44 | 1996 | /** |
lypinator | 0:bb348c97df44 | 1997 | * @brief Initializes the CRYP peripheral in AES ECB decryption mode using DMA. |
lypinator | 0:bb348c97df44 | 1998 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1999 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 2000 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 2001 | * @param Size Length of the plaintext buffer, must be a multiple of 16 bytes |
lypinator | 0:bb348c97df44 | 2002 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 2003 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2004 | */ |
lypinator | 0:bb348c97df44 | 2005 | HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) |
lypinator | 0:bb348c97df44 | 2006 | { |
lypinator | 0:bb348c97df44 | 2007 | uint32_t tickstart = 0U; |
lypinator | 0:bb348c97df44 | 2008 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 2009 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 2010 | |
lypinator | 0:bb348c97df44 | 2011 | if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) |
lypinator | 0:bb348c97df44 | 2012 | { |
lypinator | 0:bb348c97df44 | 2013 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 2014 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2015 | |
lypinator | 0:bb348c97df44 | 2016 | inputaddr = (uint32_t)pCypherData; |
lypinator | 0:bb348c97df44 | 2017 | outputaddr = (uint32_t)pPlainData; |
lypinator | 0:bb348c97df44 | 2018 | |
lypinator | 0:bb348c97df44 | 2019 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 2020 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 2021 | |
lypinator | 0:bb348c97df44 | 2022 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 2023 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 2024 | { |
lypinator | 0:bb348c97df44 | 2025 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 2026 | CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
lypinator | 0:bb348c97df44 | 2027 | |
lypinator | 0:bb348c97df44 | 2028 | /* Set the CRYP peripheral in AES Key mode */ |
lypinator | 0:bb348c97df44 | 2029 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR); |
lypinator | 0:bb348c97df44 | 2030 | |
lypinator | 0:bb348c97df44 | 2031 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 2032 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 2033 | |
lypinator | 0:bb348c97df44 | 2034 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 2035 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 2036 | |
lypinator | 0:bb348c97df44 | 2037 | while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY)) |
lypinator | 0:bb348c97df44 | 2038 | { |
lypinator | 0:bb348c97df44 | 2039 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 2040 | if((HAL_GetTick() - tickstart ) > CRYP_TIMEOUT_VALUE) |
lypinator | 0:bb348c97df44 | 2041 | { |
lypinator | 0:bb348c97df44 | 2042 | /* Change state */ |
lypinator | 0:bb348c97df44 | 2043 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2044 | |
lypinator | 0:bb348c97df44 | 2045 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 2046 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2047 | |
lypinator | 0:bb348c97df44 | 2048 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2049 | } |
lypinator | 0:bb348c97df44 | 2050 | } |
lypinator | 0:bb348c97df44 | 2051 | |
lypinator | 0:bb348c97df44 | 2052 | /* Reset the ALGOMODE bits*/ |
lypinator | 0:bb348c97df44 | 2053 | CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE); |
lypinator | 0:bb348c97df44 | 2054 | |
lypinator | 0:bb348c97df44 | 2055 | /* Set the CRYP peripheral in AES ECB decryption mode */ |
lypinator | 0:bb348c97df44 | 2056 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB | CRYP_CR_ALGODIR); |
lypinator | 0:bb348c97df44 | 2057 | |
lypinator | 0:bb348c97df44 | 2058 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 2059 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 2060 | |
lypinator | 0:bb348c97df44 | 2061 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 2062 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 2063 | } |
lypinator | 0:bb348c97df44 | 2064 | |
lypinator | 0:bb348c97df44 | 2065 | /* Set the input and output addresses and start DMA transfer */ |
lypinator | 0:bb348c97df44 | 2066 | CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); |
lypinator | 0:bb348c97df44 | 2067 | |
lypinator | 0:bb348c97df44 | 2068 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 2069 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2070 | |
lypinator | 0:bb348c97df44 | 2071 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 2072 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2073 | } |
lypinator | 0:bb348c97df44 | 2074 | else |
lypinator | 0:bb348c97df44 | 2075 | { |
lypinator | 0:bb348c97df44 | 2076 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 2077 | } |
lypinator | 0:bb348c97df44 | 2078 | } |
lypinator | 0:bb348c97df44 | 2079 | |
lypinator | 0:bb348c97df44 | 2080 | /** |
lypinator | 0:bb348c97df44 | 2081 | * @brief Initializes the CRYP peripheral in AES CBC encryption mode using DMA. |
lypinator | 0:bb348c97df44 | 2082 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 2083 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 2084 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 2085 | * @param Size Length of the plaintext buffer, must be a multiple of 16 bytes |
lypinator | 0:bb348c97df44 | 2086 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 2087 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2088 | */ |
lypinator | 0:bb348c97df44 | 2089 | HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) |
lypinator | 0:bb348c97df44 | 2090 | { |
lypinator | 0:bb348c97df44 | 2091 | uint32_t tickstart = 0U; |
lypinator | 0:bb348c97df44 | 2092 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 2093 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 2094 | |
lypinator | 0:bb348c97df44 | 2095 | if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) |
lypinator | 0:bb348c97df44 | 2096 | { |
lypinator | 0:bb348c97df44 | 2097 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 2098 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2099 | |
lypinator | 0:bb348c97df44 | 2100 | inputaddr = (uint32_t)pCypherData; |
lypinator | 0:bb348c97df44 | 2101 | outputaddr = (uint32_t)pPlainData; |
lypinator | 0:bb348c97df44 | 2102 | |
lypinator | 0:bb348c97df44 | 2103 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 2104 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 2105 | |
lypinator | 0:bb348c97df44 | 2106 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 2107 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 2108 | { |
lypinator | 0:bb348c97df44 | 2109 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 2110 | CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
lypinator | 0:bb348c97df44 | 2111 | |
lypinator | 0:bb348c97df44 | 2112 | /* Set the CRYP peripheral in AES Key mode */ |
lypinator | 0:bb348c97df44 | 2113 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR); |
lypinator | 0:bb348c97df44 | 2114 | |
lypinator | 0:bb348c97df44 | 2115 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 2116 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 2117 | |
lypinator | 0:bb348c97df44 | 2118 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 2119 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 2120 | |
lypinator | 0:bb348c97df44 | 2121 | while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY)) |
lypinator | 0:bb348c97df44 | 2122 | { |
lypinator | 0:bb348c97df44 | 2123 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 2124 | if((HAL_GetTick() - tickstart ) > CRYP_TIMEOUT_VALUE) |
lypinator | 0:bb348c97df44 | 2125 | { |
lypinator | 0:bb348c97df44 | 2126 | /* Change state */ |
lypinator | 0:bb348c97df44 | 2127 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2128 | |
lypinator | 0:bb348c97df44 | 2129 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 2130 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2131 | |
lypinator | 0:bb348c97df44 | 2132 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2133 | } |
lypinator | 0:bb348c97df44 | 2134 | } |
lypinator | 0:bb348c97df44 | 2135 | |
lypinator | 0:bb348c97df44 | 2136 | /* Reset the ALGOMODE bits*/ |
lypinator | 0:bb348c97df44 | 2137 | CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE); |
lypinator | 0:bb348c97df44 | 2138 | |
lypinator | 0:bb348c97df44 | 2139 | /* Set the CRYP peripheral in AES CBC decryption mode */ |
lypinator | 0:bb348c97df44 | 2140 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC | CRYP_CR_ALGODIR); |
lypinator | 0:bb348c97df44 | 2141 | |
lypinator | 0:bb348c97df44 | 2142 | /* Set the Initialization Vector */ |
lypinator | 0:bb348c97df44 | 2143 | CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B); |
lypinator | 0:bb348c97df44 | 2144 | |
lypinator | 0:bb348c97df44 | 2145 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 2146 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 2147 | |
lypinator | 0:bb348c97df44 | 2148 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 2149 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 2150 | } |
lypinator | 0:bb348c97df44 | 2151 | |
lypinator | 0:bb348c97df44 | 2152 | /* Set the input and output addresses and start DMA transfer */ |
lypinator | 0:bb348c97df44 | 2153 | CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); |
lypinator | 0:bb348c97df44 | 2154 | |
lypinator | 0:bb348c97df44 | 2155 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 2156 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2157 | |
lypinator | 0:bb348c97df44 | 2158 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 2159 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2160 | } |
lypinator | 0:bb348c97df44 | 2161 | else |
lypinator | 0:bb348c97df44 | 2162 | { |
lypinator | 0:bb348c97df44 | 2163 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 2164 | } |
lypinator | 0:bb348c97df44 | 2165 | } |
lypinator | 0:bb348c97df44 | 2166 | |
lypinator | 0:bb348c97df44 | 2167 | /** |
lypinator | 0:bb348c97df44 | 2168 | * @brief Initializes the CRYP peripheral in AES CTR decryption mode using DMA. |
lypinator | 0:bb348c97df44 | 2169 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 2170 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 2171 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 2172 | * @param Size Length of the plaintext buffer, must be a multiple of 16 |
lypinator | 0:bb348c97df44 | 2173 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 2174 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2175 | */ |
lypinator | 0:bb348c97df44 | 2176 | HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) |
lypinator | 0:bb348c97df44 | 2177 | { |
lypinator | 0:bb348c97df44 | 2178 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 2179 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 2180 | |
lypinator | 0:bb348c97df44 | 2181 | if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) |
lypinator | 0:bb348c97df44 | 2182 | { |
lypinator | 0:bb348c97df44 | 2183 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 2184 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2185 | |
lypinator | 0:bb348c97df44 | 2186 | inputaddr = (uint32_t)pCypherData; |
lypinator | 0:bb348c97df44 | 2187 | outputaddr = (uint32_t)pPlainData; |
lypinator | 0:bb348c97df44 | 2188 | |
lypinator | 0:bb348c97df44 | 2189 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 2190 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 2191 | |
lypinator | 0:bb348c97df44 | 2192 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 2193 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 2194 | { |
lypinator | 0:bb348c97df44 | 2195 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 2196 | CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
lypinator | 0:bb348c97df44 | 2197 | |
lypinator | 0:bb348c97df44 | 2198 | /* Set the CRYP peripheral in AES CTR mode */ |
lypinator | 0:bb348c97df44 | 2199 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR | CRYP_CR_ALGODIR); |
lypinator | 0:bb348c97df44 | 2200 | |
lypinator | 0:bb348c97df44 | 2201 | /* Set the Initialization Vector */ |
lypinator | 0:bb348c97df44 | 2202 | CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B); |
lypinator | 0:bb348c97df44 | 2203 | |
lypinator | 0:bb348c97df44 | 2204 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 2205 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 2206 | |
lypinator | 0:bb348c97df44 | 2207 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 2208 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 2209 | } |
lypinator | 0:bb348c97df44 | 2210 | |
lypinator | 0:bb348c97df44 | 2211 | /* Set the input and output addresses and start DMA transfer */ |
lypinator | 0:bb348c97df44 | 2212 | CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); |
lypinator | 0:bb348c97df44 | 2213 | |
lypinator | 0:bb348c97df44 | 2214 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 2215 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2216 | |
lypinator | 0:bb348c97df44 | 2217 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 2218 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2219 | } |
lypinator | 0:bb348c97df44 | 2220 | else |
lypinator | 0:bb348c97df44 | 2221 | { |
lypinator | 0:bb348c97df44 | 2222 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 2223 | } |
lypinator | 0:bb348c97df44 | 2224 | } |
lypinator | 0:bb348c97df44 | 2225 | |
lypinator | 0:bb348c97df44 | 2226 | |
lypinator | 0:bb348c97df44 | 2227 | /** |
lypinator | 0:bb348c97df44 | 2228 | * @} |
lypinator | 0:bb348c97df44 | 2229 | */ |
lypinator | 0:bb348c97df44 | 2230 | |
lypinator | 0:bb348c97df44 | 2231 | /** @defgroup CRYP_Exported_Functions_Group3 DES processing functions |
lypinator | 0:bb348c97df44 | 2232 | * @brief processing functions. |
lypinator | 0:bb348c97df44 | 2233 | * |
lypinator | 0:bb348c97df44 | 2234 | @verbatim |
lypinator | 0:bb348c97df44 | 2235 | ============================================================================== |
lypinator | 0:bb348c97df44 | 2236 | ##### DES processing functions ##### |
lypinator | 0:bb348c97df44 | 2237 | ============================================================================== |
lypinator | 0:bb348c97df44 | 2238 | [..] This section provides functions allowing to: |
lypinator | 0:bb348c97df44 | 2239 | (+) Encrypt plaintext using DES using ECB or CBC chaining modes |
lypinator | 0:bb348c97df44 | 2240 | (+) Decrypt cyphertext using ECB or CBC chaining modes |
lypinator | 0:bb348c97df44 | 2241 | [..] Three processing functions are available: |
lypinator | 0:bb348c97df44 | 2242 | (+) Polling mode |
lypinator | 0:bb348c97df44 | 2243 | (+) Interrupt mode |
lypinator | 0:bb348c97df44 | 2244 | (+) DMA mode |
lypinator | 0:bb348c97df44 | 2245 | |
lypinator | 0:bb348c97df44 | 2246 | @endverbatim |
lypinator | 0:bb348c97df44 | 2247 | * @{ |
lypinator | 0:bb348c97df44 | 2248 | */ |
lypinator | 0:bb348c97df44 | 2249 | |
lypinator | 0:bb348c97df44 | 2250 | /** |
lypinator | 0:bb348c97df44 | 2251 | * @brief Initializes the CRYP peripheral in DES ECB encryption mode. |
lypinator | 0:bb348c97df44 | 2252 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 2253 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 2254 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 2255 | * @param Size Length of the plaintext buffer, must be a multiple of 8 |
lypinator | 0:bb348c97df44 | 2256 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 2257 | * @param Timeout Specify Timeout value |
lypinator | 0:bb348c97df44 | 2258 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2259 | */ |
lypinator | 0:bb348c97df44 | 2260 | HAL_StatusTypeDef HAL_CRYP_DESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 2261 | { |
lypinator | 0:bb348c97df44 | 2262 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 2263 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2264 | |
lypinator | 0:bb348c97df44 | 2265 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 2266 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 2267 | |
lypinator | 0:bb348c97df44 | 2268 | /* Set CRYP peripheral in DES ECB encryption mode */ |
lypinator | 0:bb348c97df44 | 2269 | CRYP_SetDESECBMode(hcryp, 0U); |
lypinator | 0:bb348c97df44 | 2270 | |
lypinator | 0:bb348c97df44 | 2271 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 2272 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 2273 | |
lypinator | 0:bb348c97df44 | 2274 | /* Write Plain Data and Get Cypher Data */ |
lypinator | 0:bb348c97df44 | 2275 | if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 2276 | { |
lypinator | 0:bb348c97df44 | 2277 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2278 | } |
lypinator | 0:bb348c97df44 | 2279 | |
lypinator | 0:bb348c97df44 | 2280 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 2281 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 2282 | |
lypinator | 0:bb348c97df44 | 2283 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 2284 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2285 | |
lypinator | 0:bb348c97df44 | 2286 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 2287 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2288 | } |
lypinator | 0:bb348c97df44 | 2289 | |
lypinator | 0:bb348c97df44 | 2290 | /** |
lypinator | 0:bb348c97df44 | 2291 | * @brief Initializes the CRYP peripheral in DES ECB decryption mode. |
lypinator | 0:bb348c97df44 | 2292 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 2293 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 2294 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 2295 | * @param Size Length of the plaintext buffer, must be a multiple of 8 |
lypinator | 0:bb348c97df44 | 2296 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 2297 | * @param Timeout Specify Timeout value |
lypinator | 0:bb348c97df44 | 2298 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2299 | */ |
lypinator | 0:bb348c97df44 | 2300 | HAL_StatusTypeDef HAL_CRYP_DESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 2301 | { |
lypinator | 0:bb348c97df44 | 2302 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 2303 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2304 | |
lypinator | 0:bb348c97df44 | 2305 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 2306 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 2307 | |
lypinator | 0:bb348c97df44 | 2308 | /* Set CRYP peripheral in DES ECB decryption mode */ |
lypinator | 0:bb348c97df44 | 2309 | CRYP_SetDESECBMode(hcryp, CRYP_CR_ALGODIR); |
lypinator | 0:bb348c97df44 | 2310 | |
lypinator | 0:bb348c97df44 | 2311 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 2312 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 2313 | |
lypinator | 0:bb348c97df44 | 2314 | /* Write Plain Data and Get Cypher Data */ |
lypinator | 0:bb348c97df44 | 2315 | if(CRYP_ProcessData2Words(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 2316 | { |
lypinator | 0:bb348c97df44 | 2317 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2318 | } |
lypinator | 0:bb348c97df44 | 2319 | |
lypinator | 0:bb348c97df44 | 2320 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 2321 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 2322 | |
lypinator | 0:bb348c97df44 | 2323 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 2324 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2325 | |
lypinator | 0:bb348c97df44 | 2326 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 2327 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2328 | } |
lypinator | 0:bb348c97df44 | 2329 | |
lypinator | 0:bb348c97df44 | 2330 | /** |
lypinator | 0:bb348c97df44 | 2331 | * @brief Initializes the CRYP peripheral in DES CBC encryption mode. |
lypinator | 0:bb348c97df44 | 2332 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 2333 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 2334 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 2335 | * @param Size Length of the plaintext buffer, must be a multiple of 8 |
lypinator | 0:bb348c97df44 | 2336 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 2337 | * @param Timeout Specify Timeout value |
lypinator | 0:bb348c97df44 | 2338 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2339 | */ |
lypinator | 0:bb348c97df44 | 2340 | HAL_StatusTypeDef HAL_CRYP_DESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 2341 | { |
lypinator | 0:bb348c97df44 | 2342 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 2343 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2344 | |
lypinator | 0:bb348c97df44 | 2345 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 2346 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 2347 | |
lypinator | 0:bb348c97df44 | 2348 | /* Set CRYP peripheral in DES CBC encryption mode */ |
lypinator | 0:bb348c97df44 | 2349 | CRYP_SetDESCBCMode(hcryp, 0U); |
lypinator | 0:bb348c97df44 | 2350 | |
lypinator | 0:bb348c97df44 | 2351 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 2352 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 2353 | |
lypinator | 0:bb348c97df44 | 2354 | /* Write Plain Data and Get Cypher Data */ |
lypinator | 0:bb348c97df44 | 2355 | if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 2356 | { |
lypinator | 0:bb348c97df44 | 2357 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2358 | } |
lypinator | 0:bb348c97df44 | 2359 | |
lypinator | 0:bb348c97df44 | 2360 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 2361 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 2362 | |
lypinator | 0:bb348c97df44 | 2363 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 2364 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2365 | |
lypinator | 0:bb348c97df44 | 2366 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 2367 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2368 | } |
lypinator | 0:bb348c97df44 | 2369 | |
lypinator | 0:bb348c97df44 | 2370 | /** |
lypinator | 0:bb348c97df44 | 2371 | * @brief Initializes the CRYP peripheral in DES ECB decryption mode. |
lypinator | 0:bb348c97df44 | 2372 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 2373 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 2374 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 2375 | * @param Size Length of the plaintext buffer, must be a multiple of 8 |
lypinator | 0:bb348c97df44 | 2376 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 2377 | * @param Timeout Specify Timeout value |
lypinator | 0:bb348c97df44 | 2378 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2379 | */ |
lypinator | 0:bb348c97df44 | 2380 | HAL_StatusTypeDef HAL_CRYP_DESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 2381 | { |
lypinator | 0:bb348c97df44 | 2382 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 2383 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2384 | |
lypinator | 0:bb348c97df44 | 2385 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 2386 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 2387 | |
lypinator | 0:bb348c97df44 | 2388 | /* Set CRYP peripheral in DES CBC decryption mode */ |
lypinator | 0:bb348c97df44 | 2389 | CRYP_SetDESCBCMode(hcryp, CRYP_CR_ALGODIR); |
lypinator | 0:bb348c97df44 | 2390 | |
lypinator | 0:bb348c97df44 | 2391 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 2392 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 2393 | |
lypinator | 0:bb348c97df44 | 2394 | /* Write Plain Data and Get Cypher Data */ |
lypinator | 0:bb348c97df44 | 2395 | if(CRYP_ProcessData2Words(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 2396 | { |
lypinator | 0:bb348c97df44 | 2397 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2398 | } |
lypinator | 0:bb348c97df44 | 2399 | |
lypinator | 0:bb348c97df44 | 2400 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 2401 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 2402 | |
lypinator | 0:bb348c97df44 | 2403 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 2404 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2405 | |
lypinator | 0:bb348c97df44 | 2406 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 2407 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2408 | } |
lypinator | 0:bb348c97df44 | 2409 | |
lypinator | 0:bb348c97df44 | 2410 | /** |
lypinator | 0:bb348c97df44 | 2411 | * @brief Initializes the CRYP peripheral in DES ECB encryption mode using IT. |
lypinator | 0:bb348c97df44 | 2412 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 2413 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 2414 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 2415 | * @param Size Length of the plaintext buffer, must be a multiple of 8 |
lypinator | 0:bb348c97df44 | 2416 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 2417 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2418 | */ |
lypinator | 0:bb348c97df44 | 2419 | HAL_StatusTypeDef HAL_CRYP_DESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) |
lypinator | 0:bb348c97df44 | 2420 | { |
lypinator | 0:bb348c97df44 | 2421 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 2422 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 2423 | |
lypinator | 0:bb348c97df44 | 2424 | if(hcryp->State == HAL_CRYP_STATE_READY) |
lypinator | 0:bb348c97df44 | 2425 | { |
lypinator | 0:bb348c97df44 | 2426 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 2427 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2428 | |
lypinator | 0:bb348c97df44 | 2429 | hcryp->CrypInCount = Size; |
lypinator | 0:bb348c97df44 | 2430 | hcryp->pCrypInBuffPtr = pPlainData; |
lypinator | 0:bb348c97df44 | 2431 | hcryp->pCrypOutBuffPtr = pCypherData; |
lypinator | 0:bb348c97df44 | 2432 | hcryp->CrypOutCount = Size; |
lypinator | 0:bb348c97df44 | 2433 | |
lypinator | 0:bb348c97df44 | 2434 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 2435 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 2436 | |
lypinator | 0:bb348c97df44 | 2437 | /* Set CRYP peripheral in DES ECB encryption mode */ |
lypinator | 0:bb348c97df44 | 2438 | CRYP_SetDESECBMode(hcryp, 0U); |
lypinator | 0:bb348c97df44 | 2439 | |
lypinator | 0:bb348c97df44 | 2440 | /* Enable Interrupts */ |
lypinator | 0:bb348c97df44 | 2441 | __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 2442 | |
lypinator | 0:bb348c97df44 | 2443 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 2444 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 2445 | |
lypinator | 0:bb348c97df44 | 2446 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 2447 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2448 | } |
lypinator | 0:bb348c97df44 | 2449 | else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) |
lypinator | 0:bb348c97df44 | 2450 | { |
lypinator | 0:bb348c97df44 | 2451 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
lypinator | 0:bb348c97df44 | 2452 | /* Write the Input block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 2453 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 2454 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 2455 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 2456 | |
lypinator | 0:bb348c97df44 | 2457 | hcryp->pCrypInBuffPtr += 8U; |
lypinator | 0:bb348c97df44 | 2458 | hcryp->CrypInCount -= 8U; |
lypinator | 0:bb348c97df44 | 2459 | if(hcryp->CrypInCount == 0U) |
lypinator | 0:bb348c97df44 | 2460 | { |
lypinator | 0:bb348c97df44 | 2461 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); |
lypinator | 0:bb348c97df44 | 2462 | /* Call the Input data transfer complete callback */ |
lypinator | 0:bb348c97df44 | 2463 | HAL_CRYP_InCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 2464 | } |
lypinator | 0:bb348c97df44 | 2465 | } |
lypinator | 0:bb348c97df44 | 2466 | else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) |
lypinator | 0:bb348c97df44 | 2467 | { |
lypinator | 0:bb348c97df44 | 2468 | outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; |
lypinator | 0:bb348c97df44 | 2469 | /* Read the Output block from the Output FIFO */ |
lypinator | 0:bb348c97df44 | 2470 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 2471 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 2472 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 2473 | |
lypinator | 0:bb348c97df44 | 2474 | hcryp->pCrypOutBuffPtr += 8U; |
lypinator | 0:bb348c97df44 | 2475 | hcryp->CrypOutCount -= 8U; |
lypinator | 0:bb348c97df44 | 2476 | if(hcryp->CrypOutCount == 0U) |
lypinator | 0:bb348c97df44 | 2477 | { |
lypinator | 0:bb348c97df44 | 2478 | /* Disable IT */ |
lypinator | 0:bb348c97df44 | 2479 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 2480 | /* Disable CRYP */ |
lypinator | 0:bb348c97df44 | 2481 | __HAL_CRYP_DISABLE(hcryp); |
lypinator | 0:bb348c97df44 | 2482 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 2483 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2484 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 2485 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 2486 | /* Call Input transfer complete callback */ |
lypinator | 0:bb348c97df44 | 2487 | HAL_CRYP_OutCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 2488 | } |
lypinator | 0:bb348c97df44 | 2489 | } |
lypinator | 0:bb348c97df44 | 2490 | |
lypinator | 0:bb348c97df44 | 2491 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 2492 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2493 | } |
lypinator | 0:bb348c97df44 | 2494 | |
lypinator | 0:bb348c97df44 | 2495 | /** |
lypinator | 0:bb348c97df44 | 2496 | * @brief Initializes the CRYP peripheral in DES CBC encryption mode using interrupt. |
lypinator | 0:bb348c97df44 | 2497 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 2498 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 2499 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 2500 | * @param Size Length of the plaintext buffer, must be a multiple of 8 |
lypinator | 0:bb348c97df44 | 2501 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 2502 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2503 | */ |
lypinator | 0:bb348c97df44 | 2504 | HAL_StatusTypeDef HAL_CRYP_DESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) |
lypinator | 0:bb348c97df44 | 2505 | { |
lypinator | 0:bb348c97df44 | 2506 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 2507 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 2508 | |
lypinator | 0:bb348c97df44 | 2509 | if(hcryp->State == HAL_CRYP_STATE_READY) |
lypinator | 0:bb348c97df44 | 2510 | { |
lypinator | 0:bb348c97df44 | 2511 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 2512 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2513 | |
lypinator | 0:bb348c97df44 | 2514 | hcryp->CrypInCount = Size; |
lypinator | 0:bb348c97df44 | 2515 | hcryp->pCrypInBuffPtr = pPlainData; |
lypinator | 0:bb348c97df44 | 2516 | hcryp->pCrypOutBuffPtr = pCypherData; |
lypinator | 0:bb348c97df44 | 2517 | hcryp->CrypOutCount = Size; |
lypinator | 0:bb348c97df44 | 2518 | |
lypinator | 0:bb348c97df44 | 2519 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 2520 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 2521 | |
lypinator | 0:bb348c97df44 | 2522 | /* Set CRYP peripheral in DES CBC encryption mode */ |
lypinator | 0:bb348c97df44 | 2523 | CRYP_SetDESCBCMode(hcryp, 0U); |
lypinator | 0:bb348c97df44 | 2524 | |
lypinator | 0:bb348c97df44 | 2525 | /* Enable Interrupts */ |
lypinator | 0:bb348c97df44 | 2526 | __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 2527 | |
lypinator | 0:bb348c97df44 | 2528 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 2529 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 2530 | |
lypinator | 0:bb348c97df44 | 2531 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 2532 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2533 | } |
lypinator | 0:bb348c97df44 | 2534 | |
lypinator | 0:bb348c97df44 | 2535 | else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) |
lypinator | 0:bb348c97df44 | 2536 | { |
lypinator | 0:bb348c97df44 | 2537 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
lypinator | 0:bb348c97df44 | 2538 | /* Write the Input block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 2539 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 2540 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 2541 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 2542 | |
lypinator | 0:bb348c97df44 | 2543 | hcryp->pCrypInBuffPtr += 8U; |
lypinator | 0:bb348c97df44 | 2544 | hcryp->CrypInCount -= 8U; |
lypinator | 0:bb348c97df44 | 2545 | if(hcryp->CrypInCount == 0U) |
lypinator | 0:bb348c97df44 | 2546 | { |
lypinator | 0:bb348c97df44 | 2547 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); |
lypinator | 0:bb348c97df44 | 2548 | /* Call the Input data transfer complete callback */ |
lypinator | 0:bb348c97df44 | 2549 | HAL_CRYP_InCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 2550 | } |
lypinator | 0:bb348c97df44 | 2551 | } |
lypinator | 0:bb348c97df44 | 2552 | else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) |
lypinator | 0:bb348c97df44 | 2553 | { |
lypinator | 0:bb348c97df44 | 2554 | outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; |
lypinator | 0:bb348c97df44 | 2555 | /* Read the Output block from the Output FIFO */ |
lypinator | 0:bb348c97df44 | 2556 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 2557 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 2558 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 2559 | |
lypinator | 0:bb348c97df44 | 2560 | hcryp->pCrypOutBuffPtr += 8U; |
lypinator | 0:bb348c97df44 | 2561 | hcryp->CrypOutCount -= 8U; |
lypinator | 0:bb348c97df44 | 2562 | if(hcryp->CrypOutCount == 0U) |
lypinator | 0:bb348c97df44 | 2563 | { |
lypinator | 0:bb348c97df44 | 2564 | /* Disable IT */ |
lypinator | 0:bb348c97df44 | 2565 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 2566 | /* Disable CRYP */ |
lypinator | 0:bb348c97df44 | 2567 | __HAL_CRYP_DISABLE(hcryp); |
lypinator | 0:bb348c97df44 | 2568 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 2569 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2570 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 2571 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 2572 | /* Call Input transfer complete callback */ |
lypinator | 0:bb348c97df44 | 2573 | HAL_CRYP_OutCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 2574 | } |
lypinator | 0:bb348c97df44 | 2575 | } |
lypinator | 0:bb348c97df44 | 2576 | |
lypinator | 0:bb348c97df44 | 2577 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 2578 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2579 | } |
lypinator | 0:bb348c97df44 | 2580 | |
lypinator | 0:bb348c97df44 | 2581 | /** |
lypinator | 0:bb348c97df44 | 2582 | * @brief Initializes the CRYP peripheral in DES ECB decryption mode using IT. |
lypinator | 0:bb348c97df44 | 2583 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 2584 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 2585 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 2586 | * @param Size Length of the plaintext buffer, must be a multiple of 8 |
lypinator | 0:bb348c97df44 | 2587 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 2588 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2589 | */ |
lypinator | 0:bb348c97df44 | 2590 | HAL_StatusTypeDef HAL_CRYP_DESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) |
lypinator | 0:bb348c97df44 | 2591 | { |
lypinator | 0:bb348c97df44 | 2592 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 2593 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 2594 | |
lypinator | 0:bb348c97df44 | 2595 | if(hcryp->State == HAL_CRYP_STATE_READY) |
lypinator | 0:bb348c97df44 | 2596 | { |
lypinator | 0:bb348c97df44 | 2597 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 2598 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2599 | |
lypinator | 0:bb348c97df44 | 2600 | hcryp->CrypInCount = Size; |
lypinator | 0:bb348c97df44 | 2601 | hcryp->pCrypInBuffPtr = pCypherData; |
lypinator | 0:bb348c97df44 | 2602 | hcryp->pCrypOutBuffPtr = pPlainData; |
lypinator | 0:bb348c97df44 | 2603 | hcryp->CrypOutCount = Size; |
lypinator | 0:bb348c97df44 | 2604 | |
lypinator | 0:bb348c97df44 | 2605 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 2606 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 2607 | |
lypinator | 0:bb348c97df44 | 2608 | /* Set CRYP peripheral in DES ECB decryption mode */ |
lypinator | 0:bb348c97df44 | 2609 | CRYP_SetDESECBMode(hcryp, CRYP_CR_ALGODIR); |
lypinator | 0:bb348c97df44 | 2610 | |
lypinator | 0:bb348c97df44 | 2611 | /* Enable Interrupts */ |
lypinator | 0:bb348c97df44 | 2612 | __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 2613 | |
lypinator | 0:bb348c97df44 | 2614 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 2615 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 2616 | |
lypinator | 0:bb348c97df44 | 2617 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 2618 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2619 | } |
lypinator | 0:bb348c97df44 | 2620 | else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) |
lypinator | 0:bb348c97df44 | 2621 | { |
lypinator | 0:bb348c97df44 | 2622 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
lypinator | 0:bb348c97df44 | 2623 | /* Write the Input block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 2624 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 2625 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 2626 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 2627 | |
lypinator | 0:bb348c97df44 | 2628 | hcryp->pCrypInBuffPtr += 8U; |
lypinator | 0:bb348c97df44 | 2629 | hcryp->CrypInCount -= 8U; |
lypinator | 0:bb348c97df44 | 2630 | if(hcryp->CrypInCount == 0U) |
lypinator | 0:bb348c97df44 | 2631 | { |
lypinator | 0:bb348c97df44 | 2632 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); |
lypinator | 0:bb348c97df44 | 2633 | /* Call the Input data transfer complete callback */ |
lypinator | 0:bb348c97df44 | 2634 | HAL_CRYP_InCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 2635 | } |
lypinator | 0:bb348c97df44 | 2636 | } |
lypinator | 0:bb348c97df44 | 2637 | else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) |
lypinator | 0:bb348c97df44 | 2638 | { |
lypinator | 0:bb348c97df44 | 2639 | outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; |
lypinator | 0:bb348c97df44 | 2640 | /* Read the Output block from the Output FIFO */ |
lypinator | 0:bb348c97df44 | 2641 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 2642 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 2643 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 2644 | |
lypinator | 0:bb348c97df44 | 2645 | hcryp->pCrypOutBuffPtr += 8U; |
lypinator | 0:bb348c97df44 | 2646 | hcryp->CrypOutCount -= 8U; |
lypinator | 0:bb348c97df44 | 2647 | if(hcryp->CrypOutCount == 0U) |
lypinator | 0:bb348c97df44 | 2648 | { |
lypinator | 0:bb348c97df44 | 2649 | /* Disable IT */ |
lypinator | 0:bb348c97df44 | 2650 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 2651 | /* Disable CRYP */ |
lypinator | 0:bb348c97df44 | 2652 | __HAL_CRYP_DISABLE(hcryp); |
lypinator | 0:bb348c97df44 | 2653 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 2654 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2655 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 2656 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 2657 | /* Call Input transfer complete callback */ |
lypinator | 0:bb348c97df44 | 2658 | HAL_CRYP_OutCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 2659 | } |
lypinator | 0:bb348c97df44 | 2660 | } |
lypinator | 0:bb348c97df44 | 2661 | |
lypinator | 0:bb348c97df44 | 2662 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 2663 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2664 | } |
lypinator | 0:bb348c97df44 | 2665 | |
lypinator | 0:bb348c97df44 | 2666 | /** |
lypinator | 0:bb348c97df44 | 2667 | * @brief Initializes the CRYP peripheral in DES ECB decryption mode using interrupt. |
lypinator | 0:bb348c97df44 | 2668 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 2669 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 2670 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 2671 | * @param Size Length of the plaintext buffer, must be a multiple of 8 |
lypinator | 0:bb348c97df44 | 2672 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 2673 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2674 | */ |
lypinator | 0:bb348c97df44 | 2675 | HAL_StatusTypeDef HAL_CRYP_DESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) |
lypinator | 0:bb348c97df44 | 2676 | { |
lypinator | 0:bb348c97df44 | 2677 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 2678 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 2679 | |
lypinator | 0:bb348c97df44 | 2680 | if(hcryp->State == HAL_CRYP_STATE_READY) |
lypinator | 0:bb348c97df44 | 2681 | { |
lypinator | 0:bb348c97df44 | 2682 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 2683 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2684 | |
lypinator | 0:bb348c97df44 | 2685 | hcryp->CrypInCount = Size; |
lypinator | 0:bb348c97df44 | 2686 | hcryp->pCrypInBuffPtr = pCypherData; |
lypinator | 0:bb348c97df44 | 2687 | hcryp->pCrypOutBuffPtr = pPlainData; |
lypinator | 0:bb348c97df44 | 2688 | hcryp->CrypOutCount = Size; |
lypinator | 0:bb348c97df44 | 2689 | |
lypinator | 0:bb348c97df44 | 2690 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 2691 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 2692 | |
lypinator | 0:bb348c97df44 | 2693 | /* Set CRYP peripheral in DES CBC decryption mode */ |
lypinator | 0:bb348c97df44 | 2694 | CRYP_SetDESCBCMode(hcryp, CRYP_CR_ALGODIR); |
lypinator | 0:bb348c97df44 | 2695 | |
lypinator | 0:bb348c97df44 | 2696 | /* Enable Interrupts */ |
lypinator | 0:bb348c97df44 | 2697 | __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 2698 | |
lypinator | 0:bb348c97df44 | 2699 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 2700 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 2701 | |
lypinator | 0:bb348c97df44 | 2702 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 2703 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2704 | } |
lypinator | 0:bb348c97df44 | 2705 | else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) |
lypinator | 0:bb348c97df44 | 2706 | { |
lypinator | 0:bb348c97df44 | 2707 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
lypinator | 0:bb348c97df44 | 2708 | /* Write the Input block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 2709 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 2710 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 2711 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 2712 | |
lypinator | 0:bb348c97df44 | 2713 | hcryp->pCrypInBuffPtr += 8U; |
lypinator | 0:bb348c97df44 | 2714 | hcryp->CrypInCount -= 8U; |
lypinator | 0:bb348c97df44 | 2715 | if(hcryp->CrypInCount == 0U) |
lypinator | 0:bb348c97df44 | 2716 | { |
lypinator | 0:bb348c97df44 | 2717 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); |
lypinator | 0:bb348c97df44 | 2718 | /* Call the Input data transfer complete callback */ |
lypinator | 0:bb348c97df44 | 2719 | HAL_CRYP_InCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 2720 | } |
lypinator | 0:bb348c97df44 | 2721 | } |
lypinator | 0:bb348c97df44 | 2722 | else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) |
lypinator | 0:bb348c97df44 | 2723 | { |
lypinator | 0:bb348c97df44 | 2724 | outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; |
lypinator | 0:bb348c97df44 | 2725 | /* Read the Output block from the Output FIFO */ |
lypinator | 0:bb348c97df44 | 2726 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 2727 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 2728 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 2729 | |
lypinator | 0:bb348c97df44 | 2730 | hcryp->pCrypOutBuffPtr += 8U; |
lypinator | 0:bb348c97df44 | 2731 | hcryp->CrypOutCount -= 8U; |
lypinator | 0:bb348c97df44 | 2732 | if(hcryp->CrypOutCount == 0U) |
lypinator | 0:bb348c97df44 | 2733 | { |
lypinator | 0:bb348c97df44 | 2734 | /* Disable IT */ |
lypinator | 0:bb348c97df44 | 2735 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 2736 | /* Disable CRYP */ |
lypinator | 0:bb348c97df44 | 2737 | __HAL_CRYP_DISABLE(hcryp); |
lypinator | 0:bb348c97df44 | 2738 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 2739 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2740 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 2741 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 2742 | /* Call Input transfer complete callback */ |
lypinator | 0:bb348c97df44 | 2743 | HAL_CRYP_OutCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 2744 | } |
lypinator | 0:bb348c97df44 | 2745 | } |
lypinator | 0:bb348c97df44 | 2746 | |
lypinator | 0:bb348c97df44 | 2747 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 2748 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2749 | } |
lypinator | 0:bb348c97df44 | 2750 | |
lypinator | 0:bb348c97df44 | 2751 | /** |
lypinator | 0:bb348c97df44 | 2752 | * @brief Initializes the CRYP peripheral in DES ECB encryption mode using DMA. |
lypinator | 0:bb348c97df44 | 2753 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 2754 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 2755 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 2756 | * @param Size Length of the plaintext buffer, must be a multiple of 8 |
lypinator | 0:bb348c97df44 | 2757 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 2758 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2759 | */ |
lypinator | 0:bb348c97df44 | 2760 | HAL_StatusTypeDef HAL_CRYP_DESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) |
lypinator | 0:bb348c97df44 | 2761 | { |
lypinator | 0:bb348c97df44 | 2762 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 2763 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 2764 | |
lypinator | 0:bb348c97df44 | 2765 | if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) |
lypinator | 0:bb348c97df44 | 2766 | { |
lypinator | 0:bb348c97df44 | 2767 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 2768 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2769 | |
lypinator | 0:bb348c97df44 | 2770 | inputaddr = (uint32_t)pPlainData; |
lypinator | 0:bb348c97df44 | 2771 | outputaddr = (uint32_t)pCypherData; |
lypinator | 0:bb348c97df44 | 2772 | |
lypinator | 0:bb348c97df44 | 2773 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 2774 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 2775 | |
lypinator | 0:bb348c97df44 | 2776 | /* Set CRYP peripheral in DES ECB encryption mode */ |
lypinator | 0:bb348c97df44 | 2777 | CRYP_SetDESECBMode(hcryp, 0U); |
lypinator | 0:bb348c97df44 | 2778 | |
lypinator | 0:bb348c97df44 | 2779 | /* Set the input and output addresses and start DMA transfer */ |
lypinator | 0:bb348c97df44 | 2780 | CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); |
lypinator | 0:bb348c97df44 | 2781 | |
lypinator | 0:bb348c97df44 | 2782 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 2783 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2784 | |
lypinator | 0:bb348c97df44 | 2785 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 2786 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2787 | } |
lypinator | 0:bb348c97df44 | 2788 | else |
lypinator | 0:bb348c97df44 | 2789 | { |
lypinator | 0:bb348c97df44 | 2790 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 2791 | } |
lypinator | 0:bb348c97df44 | 2792 | } |
lypinator | 0:bb348c97df44 | 2793 | |
lypinator | 0:bb348c97df44 | 2794 | /** |
lypinator | 0:bb348c97df44 | 2795 | * @brief Initializes the CRYP peripheral in DES CBC encryption mode using DMA. |
lypinator | 0:bb348c97df44 | 2796 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 2797 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 2798 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 2799 | * @param Size Length of the plaintext buffer, must be a multiple of 8 |
lypinator | 0:bb348c97df44 | 2800 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 2801 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2802 | */ |
lypinator | 0:bb348c97df44 | 2803 | HAL_StatusTypeDef HAL_CRYP_DESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) |
lypinator | 0:bb348c97df44 | 2804 | { |
lypinator | 0:bb348c97df44 | 2805 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 2806 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 2807 | |
lypinator | 0:bb348c97df44 | 2808 | if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) |
lypinator | 0:bb348c97df44 | 2809 | { |
lypinator | 0:bb348c97df44 | 2810 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 2811 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2812 | |
lypinator | 0:bb348c97df44 | 2813 | inputaddr = (uint32_t)pPlainData; |
lypinator | 0:bb348c97df44 | 2814 | outputaddr = (uint32_t)pCypherData; |
lypinator | 0:bb348c97df44 | 2815 | |
lypinator | 0:bb348c97df44 | 2816 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 2817 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 2818 | |
lypinator | 0:bb348c97df44 | 2819 | /* Set CRYP peripheral in DES CBC encryption mode */ |
lypinator | 0:bb348c97df44 | 2820 | CRYP_SetDESCBCMode(hcryp, 0U); |
lypinator | 0:bb348c97df44 | 2821 | |
lypinator | 0:bb348c97df44 | 2822 | /* Set the input and output addresses and start DMA transfer */ |
lypinator | 0:bb348c97df44 | 2823 | CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); |
lypinator | 0:bb348c97df44 | 2824 | |
lypinator | 0:bb348c97df44 | 2825 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 2826 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2827 | |
lypinator | 0:bb348c97df44 | 2828 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 2829 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2830 | } |
lypinator | 0:bb348c97df44 | 2831 | else |
lypinator | 0:bb348c97df44 | 2832 | { |
lypinator | 0:bb348c97df44 | 2833 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 2834 | } |
lypinator | 0:bb348c97df44 | 2835 | } |
lypinator | 0:bb348c97df44 | 2836 | |
lypinator | 0:bb348c97df44 | 2837 | /** |
lypinator | 0:bb348c97df44 | 2838 | * @brief Initializes the CRYP peripheral in DES ECB decryption mode using DMA. |
lypinator | 0:bb348c97df44 | 2839 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 2840 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 2841 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 2842 | * @param Size Length of the plaintext buffer, must be a multiple of 8 |
lypinator | 0:bb348c97df44 | 2843 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 2844 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2845 | */ |
lypinator | 0:bb348c97df44 | 2846 | HAL_StatusTypeDef HAL_CRYP_DESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) |
lypinator | 0:bb348c97df44 | 2847 | { |
lypinator | 0:bb348c97df44 | 2848 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 2849 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 2850 | |
lypinator | 0:bb348c97df44 | 2851 | if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) |
lypinator | 0:bb348c97df44 | 2852 | { |
lypinator | 0:bb348c97df44 | 2853 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 2854 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2855 | |
lypinator | 0:bb348c97df44 | 2856 | inputaddr = (uint32_t)pCypherData; |
lypinator | 0:bb348c97df44 | 2857 | outputaddr = (uint32_t)pPlainData; |
lypinator | 0:bb348c97df44 | 2858 | |
lypinator | 0:bb348c97df44 | 2859 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 2860 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 2861 | |
lypinator | 0:bb348c97df44 | 2862 | /* Set CRYP peripheral in DES ECB decryption mode */ |
lypinator | 0:bb348c97df44 | 2863 | CRYP_SetDESECBMode(hcryp, CRYP_CR_ALGODIR); |
lypinator | 0:bb348c97df44 | 2864 | |
lypinator | 0:bb348c97df44 | 2865 | /* Set the input and output addresses and start DMA transfer */ |
lypinator | 0:bb348c97df44 | 2866 | CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); |
lypinator | 0:bb348c97df44 | 2867 | |
lypinator | 0:bb348c97df44 | 2868 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 2869 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2870 | |
lypinator | 0:bb348c97df44 | 2871 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 2872 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2873 | } |
lypinator | 0:bb348c97df44 | 2874 | else |
lypinator | 0:bb348c97df44 | 2875 | { |
lypinator | 0:bb348c97df44 | 2876 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 2877 | } |
lypinator | 0:bb348c97df44 | 2878 | } |
lypinator | 0:bb348c97df44 | 2879 | |
lypinator | 0:bb348c97df44 | 2880 | /** |
lypinator | 0:bb348c97df44 | 2881 | * @brief Initializes the CRYP peripheral in DES ECB decryption mode using DMA. |
lypinator | 0:bb348c97df44 | 2882 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 2883 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 2884 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 2885 | * @param Size Length of the plaintext buffer, must be a multiple of 8 |
lypinator | 0:bb348c97df44 | 2886 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 2887 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2888 | */ |
lypinator | 0:bb348c97df44 | 2889 | HAL_StatusTypeDef HAL_CRYP_DESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) |
lypinator | 0:bb348c97df44 | 2890 | { |
lypinator | 0:bb348c97df44 | 2891 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 2892 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 2893 | |
lypinator | 0:bb348c97df44 | 2894 | if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) |
lypinator | 0:bb348c97df44 | 2895 | { |
lypinator | 0:bb348c97df44 | 2896 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 2897 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2898 | |
lypinator | 0:bb348c97df44 | 2899 | inputaddr = (uint32_t)pCypherData; |
lypinator | 0:bb348c97df44 | 2900 | outputaddr = (uint32_t)pPlainData; |
lypinator | 0:bb348c97df44 | 2901 | |
lypinator | 0:bb348c97df44 | 2902 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 2903 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 2904 | |
lypinator | 0:bb348c97df44 | 2905 | /* Set CRYP peripheral in DES CBC decryption mode */ |
lypinator | 0:bb348c97df44 | 2906 | CRYP_SetDESCBCMode(hcryp, CRYP_CR_ALGODIR); |
lypinator | 0:bb348c97df44 | 2907 | |
lypinator | 0:bb348c97df44 | 2908 | /* Set the input and output addresses and start DMA transfer */ |
lypinator | 0:bb348c97df44 | 2909 | CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); |
lypinator | 0:bb348c97df44 | 2910 | |
lypinator | 0:bb348c97df44 | 2911 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 2912 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2913 | |
lypinator | 0:bb348c97df44 | 2914 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 2915 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2916 | } |
lypinator | 0:bb348c97df44 | 2917 | else |
lypinator | 0:bb348c97df44 | 2918 | { |
lypinator | 0:bb348c97df44 | 2919 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 2920 | } |
lypinator | 0:bb348c97df44 | 2921 | } |
lypinator | 0:bb348c97df44 | 2922 | |
lypinator | 0:bb348c97df44 | 2923 | /** |
lypinator | 0:bb348c97df44 | 2924 | * @} |
lypinator | 0:bb348c97df44 | 2925 | */ |
lypinator | 0:bb348c97df44 | 2926 | |
lypinator | 0:bb348c97df44 | 2927 | /** @defgroup CRYP_Exported_Functions_Group4 TDES processing functions |
lypinator | 0:bb348c97df44 | 2928 | * @brief processing functions. |
lypinator | 0:bb348c97df44 | 2929 | * |
lypinator | 0:bb348c97df44 | 2930 | @verbatim |
lypinator | 0:bb348c97df44 | 2931 | ============================================================================== |
lypinator | 0:bb348c97df44 | 2932 | ##### TDES processing functions ##### |
lypinator | 0:bb348c97df44 | 2933 | ============================================================================== |
lypinator | 0:bb348c97df44 | 2934 | [..] This section provides functions allowing to: |
lypinator | 0:bb348c97df44 | 2935 | (+) Encrypt plaintext using TDES based on ECB or CBC chaining modes |
lypinator | 0:bb348c97df44 | 2936 | (+) Decrypt cyphertext using TDES based on ECB or CBC chaining modes |
lypinator | 0:bb348c97df44 | 2937 | [..] Three processing functions are available: |
lypinator | 0:bb348c97df44 | 2938 | (+) Polling mode |
lypinator | 0:bb348c97df44 | 2939 | (+) Interrupt mode |
lypinator | 0:bb348c97df44 | 2940 | (+) DMA mode |
lypinator | 0:bb348c97df44 | 2941 | |
lypinator | 0:bb348c97df44 | 2942 | @endverbatim |
lypinator | 0:bb348c97df44 | 2943 | * @{ |
lypinator | 0:bb348c97df44 | 2944 | */ |
lypinator | 0:bb348c97df44 | 2945 | |
lypinator | 0:bb348c97df44 | 2946 | /** |
lypinator | 0:bb348c97df44 | 2947 | * @brief Initializes the CRYP peripheral in TDES ECB encryption mode |
lypinator | 0:bb348c97df44 | 2948 | * then encrypt pPlainData. The cypher data are available in pCypherData |
lypinator | 0:bb348c97df44 | 2949 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 2950 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 2951 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 2952 | * @param Size Length of the plaintext buffer, must be a multiple of 8 |
lypinator | 0:bb348c97df44 | 2953 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 2954 | * @param Timeout Specify Timeout value |
lypinator | 0:bb348c97df44 | 2955 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2956 | */ |
lypinator | 0:bb348c97df44 | 2957 | HAL_StatusTypeDef HAL_CRYP_TDESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 2958 | { |
lypinator | 0:bb348c97df44 | 2959 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 2960 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2961 | |
lypinator | 0:bb348c97df44 | 2962 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 2963 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 2964 | |
lypinator | 0:bb348c97df44 | 2965 | /* Set CRYP peripheral in TDES ECB encryption mode */ |
lypinator | 0:bb348c97df44 | 2966 | CRYP_SetTDESECBMode(hcryp, 0U); |
lypinator | 0:bb348c97df44 | 2967 | |
lypinator | 0:bb348c97df44 | 2968 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 2969 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 2970 | |
lypinator | 0:bb348c97df44 | 2971 | /* Write Plain Data and Get Cypher Data */ |
lypinator | 0:bb348c97df44 | 2972 | if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 2973 | { |
lypinator | 0:bb348c97df44 | 2974 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2975 | } |
lypinator | 0:bb348c97df44 | 2976 | |
lypinator | 0:bb348c97df44 | 2977 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 2978 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 2979 | |
lypinator | 0:bb348c97df44 | 2980 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 2981 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2982 | |
lypinator | 0:bb348c97df44 | 2983 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 2984 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2985 | } |
lypinator | 0:bb348c97df44 | 2986 | |
lypinator | 0:bb348c97df44 | 2987 | /** |
lypinator | 0:bb348c97df44 | 2988 | * @brief Initializes the CRYP peripheral in TDES ECB decryption mode |
lypinator | 0:bb348c97df44 | 2989 | * then decrypted pCypherData. The cypher data are available in pPlainData |
lypinator | 0:bb348c97df44 | 2990 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 2991 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 2992 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 2993 | * @param Size Length of the plaintext buffer, must be a multiple of 8 |
lypinator | 0:bb348c97df44 | 2994 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 2995 | * @param Timeout Specify Timeout value |
lypinator | 0:bb348c97df44 | 2996 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2997 | */ |
lypinator | 0:bb348c97df44 | 2998 | HAL_StatusTypeDef HAL_CRYP_TDESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 2999 | { |
lypinator | 0:bb348c97df44 | 3000 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 3001 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3002 | |
lypinator | 0:bb348c97df44 | 3003 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 3004 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 3005 | |
lypinator | 0:bb348c97df44 | 3006 | /* Set CRYP peripheral in TDES ECB decryption mode */ |
lypinator | 0:bb348c97df44 | 3007 | CRYP_SetTDESECBMode(hcryp, CRYP_CR_ALGODIR); |
lypinator | 0:bb348c97df44 | 3008 | |
lypinator | 0:bb348c97df44 | 3009 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 3010 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 3011 | |
lypinator | 0:bb348c97df44 | 3012 | /* Write Cypher Data and Get Plain Data */ |
lypinator | 0:bb348c97df44 | 3013 | if(CRYP_ProcessData2Words(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 3014 | { |
lypinator | 0:bb348c97df44 | 3015 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 3016 | } |
lypinator | 0:bb348c97df44 | 3017 | |
lypinator | 0:bb348c97df44 | 3018 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 3019 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 3020 | |
lypinator | 0:bb348c97df44 | 3021 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 3022 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3023 | |
lypinator | 0:bb348c97df44 | 3024 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 3025 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 3026 | } |
lypinator | 0:bb348c97df44 | 3027 | |
lypinator | 0:bb348c97df44 | 3028 | /** |
lypinator | 0:bb348c97df44 | 3029 | * @brief Initializes the CRYP peripheral in TDES CBC encryption mode |
lypinator | 0:bb348c97df44 | 3030 | * then encrypt pPlainData. The cypher data are available in pCypherData |
lypinator | 0:bb348c97df44 | 3031 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 3032 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 3033 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 3034 | * @param Size Length of the plaintext buffer, must be a multiple of 8 |
lypinator | 0:bb348c97df44 | 3035 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 3036 | * @param Timeout Specify Timeout value |
lypinator | 0:bb348c97df44 | 3037 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 3038 | */ |
lypinator | 0:bb348c97df44 | 3039 | HAL_StatusTypeDef HAL_CRYP_TDESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 3040 | { |
lypinator | 0:bb348c97df44 | 3041 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 3042 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3043 | |
lypinator | 0:bb348c97df44 | 3044 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 3045 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 3046 | |
lypinator | 0:bb348c97df44 | 3047 | /* Set CRYP peripheral in TDES CBC encryption mode */ |
lypinator | 0:bb348c97df44 | 3048 | CRYP_SetTDESCBCMode(hcryp, 0U); |
lypinator | 0:bb348c97df44 | 3049 | |
lypinator | 0:bb348c97df44 | 3050 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 3051 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 3052 | |
lypinator | 0:bb348c97df44 | 3053 | /* Write Plain Data and Get Cypher Data */ |
lypinator | 0:bb348c97df44 | 3054 | if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 3055 | { |
lypinator | 0:bb348c97df44 | 3056 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 3057 | } |
lypinator | 0:bb348c97df44 | 3058 | |
lypinator | 0:bb348c97df44 | 3059 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 3060 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 3061 | |
lypinator | 0:bb348c97df44 | 3062 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 3063 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3064 | |
lypinator | 0:bb348c97df44 | 3065 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 3066 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 3067 | } |
lypinator | 0:bb348c97df44 | 3068 | |
lypinator | 0:bb348c97df44 | 3069 | /** |
lypinator | 0:bb348c97df44 | 3070 | * @brief Initializes the CRYP peripheral in TDES CBC decryption mode |
lypinator | 0:bb348c97df44 | 3071 | * then decrypted pCypherData. The cypher data are available in pPlainData |
lypinator | 0:bb348c97df44 | 3072 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 3073 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 3074 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 3075 | * @param Size Length of the plaintext buffer, must be a multiple of 8 |
lypinator | 0:bb348c97df44 | 3076 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 3077 | * @param Timeout Specify Timeout value |
lypinator | 0:bb348c97df44 | 3078 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 3079 | */ |
lypinator | 0:bb348c97df44 | 3080 | HAL_StatusTypeDef HAL_CRYP_TDESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 3081 | { |
lypinator | 0:bb348c97df44 | 3082 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 3083 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3084 | |
lypinator | 0:bb348c97df44 | 3085 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 3086 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 3087 | |
lypinator | 0:bb348c97df44 | 3088 | /* Set CRYP peripheral in TDES CBC decryption mode */ |
lypinator | 0:bb348c97df44 | 3089 | CRYP_SetTDESCBCMode(hcryp, CRYP_CR_ALGODIR); |
lypinator | 0:bb348c97df44 | 3090 | |
lypinator | 0:bb348c97df44 | 3091 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 3092 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 3093 | |
lypinator | 0:bb348c97df44 | 3094 | /* Write Cypher Data and Get Plain Data */ |
lypinator | 0:bb348c97df44 | 3095 | if(CRYP_ProcessData2Words(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 3096 | { |
lypinator | 0:bb348c97df44 | 3097 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 3098 | } |
lypinator | 0:bb348c97df44 | 3099 | |
lypinator | 0:bb348c97df44 | 3100 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 3101 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 3102 | |
lypinator | 0:bb348c97df44 | 3103 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 3104 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3105 | |
lypinator | 0:bb348c97df44 | 3106 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 3107 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 3108 | } |
lypinator | 0:bb348c97df44 | 3109 | |
lypinator | 0:bb348c97df44 | 3110 | /** |
lypinator | 0:bb348c97df44 | 3111 | * @brief Initializes the CRYP peripheral in TDES ECB encryption mode using interrupt. |
lypinator | 0:bb348c97df44 | 3112 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 3113 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 3114 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 3115 | * @param Size Length of the plaintext buffer, must be a multiple of 8 |
lypinator | 0:bb348c97df44 | 3116 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 3117 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 3118 | */ |
lypinator | 0:bb348c97df44 | 3119 | HAL_StatusTypeDef HAL_CRYP_TDESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) |
lypinator | 0:bb348c97df44 | 3120 | { |
lypinator | 0:bb348c97df44 | 3121 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 3122 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 3123 | |
lypinator | 0:bb348c97df44 | 3124 | if(hcryp->State == HAL_CRYP_STATE_READY) |
lypinator | 0:bb348c97df44 | 3125 | { |
lypinator | 0:bb348c97df44 | 3126 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 3127 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3128 | |
lypinator | 0:bb348c97df44 | 3129 | hcryp->CrypInCount = Size; |
lypinator | 0:bb348c97df44 | 3130 | hcryp->pCrypInBuffPtr = pPlainData; |
lypinator | 0:bb348c97df44 | 3131 | hcryp->pCrypOutBuffPtr = pCypherData; |
lypinator | 0:bb348c97df44 | 3132 | hcryp->CrypOutCount = Size; |
lypinator | 0:bb348c97df44 | 3133 | |
lypinator | 0:bb348c97df44 | 3134 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 3135 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 3136 | |
lypinator | 0:bb348c97df44 | 3137 | /* Set CRYP peripheral in TDES ECB encryption mode */ |
lypinator | 0:bb348c97df44 | 3138 | CRYP_SetTDESECBMode(hcryp, 0U); |
lypinator | 0:bb348c97df44 | 3139 | |
lypinator | 0:bb348c97df44 | 3140 | /* Enable Interrupts */ |
lypinator | 0:bb348c97df44 | 3141 | __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 3142 | |
lypinator | 0:bb348c97df44 | 3143 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 3144 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 3145 | |
lypinator | 0:bb348c97df44 | 3146 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 3147 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 3148 | } |
lypinator | 0:bb348c97df44 | 3149 | else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) |
lypinator | 0:bb348c97df44 | 3150 | { |
lypinator | 0:bb348c97df44 | 3151 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
lypinator | 0:bb348c97df44 | 3152 | /* Write the Input block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 3153 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 3154 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 3155 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 3156 | |
lypinator | 0:bb348c97df44 | 3157 | hcryp->pCrypInBuffPtr += 8U; |
lypinator | 0:bb348c97df44 | 3158 | hcryp->CrypInCount -= 8U; |
lypinator | 0:bb348c97df44 | 3159 | if(hcryp->CrypInCount == 0U) |
lypinator | 0:bb348c97df44 | 3160 | { |
lypinator | 0:bb348c97df44 | 3161 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); |
lypinator | 0:bb348c97df44 | 3162 | /* Call the Input data transfer complete callback */ |
lypinator | 0:bb348c97df44 | 3163 | HAL_CRYP_InCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 3164 | } |
lypinator | 0:bb348c97df44 | 3165 | } |
lypinator | 0:bb348c97df44 | 3166 | else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) |
lypinator | 0:bb348c97df44 | 3167 | { |
lypinator | 0:bb348c97df44 | 3168 | outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; |
lypinator | 0:bb348c97df44 | 3169 | /* Read the Output block from the Output FIFO */ |
lypinator | 0:bb348c97df44 | 3170 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 3171 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 3172 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 3173 | |
lypinator | 0:bb348c97df44 | 3174 | hcryp->pCrypOutBuffPtr += 8U; |
lypinator | 0:bb348c97df44 | 3175 | hcryp->CrypOutCount -= 8U; |
lypinator | 0:bb348c97df44 | 3176 | if(hcryp->CrypOutCount == 0U) |
lypinator | 0:bb348c97df44 | 3177 | { |
lypinator | 0:bb348c97df44 | 3178 | /* Disable IT */ |
lypinator | 0:bb348c97df44 | 3179 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 3180 | /* Disable CRYP */ |
lypinator | 0:bb348c97df44 | 3181 | __HAL_CRYP_DISABLE(hcryp); |
lypinator | 0:bb348c97df44 | 3182 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 3183 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3184 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 3185 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 3186 | /* Call the Output data transfer complete callback */ |
lypinator | 0:bb348c97df44 | 3187 | HAL_CRYP_OutCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 3188 | } |
lypinator | 0:bb348c97df44 | 3189 | } |
lypinator | 0:bb348c97df44 | 3190 | |
lypinator | 0:bb348c97df44 | 3191 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 3192 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 3193 | } |
lypinator | 0:bb348c97df44 | 3194 | |
lypinator | 0:bb348c97df44 | 3195 | /** |
lypinator | 0:bb348c97df44 | 3196 | * @brief Initializes the CRYP peripheral in TDES CBC encryption mode. |
lypinator | 0:bb348c97df44 | 3197 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 3198 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 3199 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 3200 | * @param Size Length of the plaintext buffer, must be a multiple of 8 |
lypinator | 0:bb348c97df44 | 3201 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 3202 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 3203 | */ |
lypinator | 0:bb348c97df44 | 3204 | HAL_StatusTypeDef HAL_CRYP_TDESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) |
lypinator | 0:bb348c97df44 | 3205 | { |
lypinator | 0:bb348c97df44 | 3206 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 3207 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 3208 | |
lypinator | 0:bb348c97df44 | 3209 | if(hcryp->State == HAL_CRYP_STATE_READY) |
lypinator | 0:bb348c97df44 | 3210 | { |
lypinator | 0:bb348c97df44 | 3211 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 3212 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3213 | |
lypinator | 0:bb348c97df44 | 3214 | hcryp->CrypInCount = Size; |
lypinator | 0:bb348c97df44 | 3215 | hcryp->pCrypInBuffPtr = pPlainData; |
lypinator | 0:bb348c97df44 | 3216 | hcryp->pCrypOutBuffPtr = pCypherData; |
lypinator | 0:bb348c97df44 | 3217 | hcryp->CrypOutCount = Size; |
lypinator | 0:bb348c97df44 | 3218 | |
lypinator | 0:bb348c97df44 | 3219 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 3220 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 3221 | |
lypinator | 0:bb348c97df44 | 3222 | /* Set CRYP peripheral in TDES CBC encryption mode */ |
lypinator | 0:bb348c97df44 | 3223 | CRYP_SetTDESCBCMode(hcryp, 0U); |
lypinator | 0:bb348c97df44 | 3224 | |
lypinator | 0:bb348c97df44 | 3225 | /* Enable Interrupts */ |
lypinator | 0:bb348c97df44 | 3226 | __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 3227 | |
lypinator | 0:bb348c97df44 | 3228 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 3229 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 3230 | |
lypinator | 0:bb348c97df44 | 3231 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 3232 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 3233 | } |
lypinator | 0:bb348c97df44 | 3234 | else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) |
lypinator | 0:bb348c97df44 | 3235 | { |
lypinator | 0:bb348c97df44 | 3236 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
lypinator | 0:bb348c97df44 | 3237 | /* Write the Input block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 3238 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 3239 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 3240 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 3241 | |
lypinator | 0:bb348c97df44 | 3242 | hcryp->pCrypInBuffPtr += 8U; |
lypinator | 0:bb348c97df44 | 3243 | hcryp->CrypInCount -= 8U; |
lypinator | 0:bb348c97df44 | 3244 | if(hcryp->CrypInCount == 0U) |
lypinator | 0:bb348c97df44 | 3245 | { |
lypinator | 0:bb348c97df44 | 3246 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); |
lypinator | 0:bb348c97df44 | 3247 | /* Call the Input data transfer complete callback */ |
lypinator | 0:bb348c97df44 | 3248 | HAL_CRYP_InCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 3249 | } |
lypinator | 0:bb348c97df44 | 3250 | } |
lypinator | 0:bb348c97df44 | 3251 | else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) |
lypinator | 0:bb348c97df44 | 3252 | { |
lypinator | 0:bb348c97df44 | 3253 | outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; |
lypinator | 0:bb348c97df44 | 3254 | /* Read the Output block from the Output FIFO */ |
lypinator | 0:bb348c97df44 | 3255 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 3256 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 3257 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 3258 | |
lypinator | 0:bb348c97df44 | 3259 | hcryp->pCrypOutBuffPtr += 8U; |
lypinator | 0:bb348c97df44 | 3260 | hcryp->CrypOutCount -= 8U; |
lypinator | 0:bb348c97df44 | 3261 | if(hcryp->CrypOutCount == 0U) |
lypinator | 0:bb348c97df44 | 3262 | { |
lypinator | 0:bb348c97df44 | 3263 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 3264 | /* Disable CRYP */ |
lypinator | 0:bb348c97df44 | 3265 | __HAL_CRYP_DISABLE(hcryp); |
lypinator | 0:bb348c97df44 | 3266 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 3267 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3268 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 3269 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 3270 | /* Call Input transfer complete callback */ |
lypinator | 0:bb348c97df44 | 3271 | HAL_CRYP_OutCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 3272 | } |
lypinator | 0:bb348c97df44 | 3273 | } |
lypinator | 0:bb348c97df44 | 3274 | |
lypinator | 0:bb348c97df44 | 3275 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 3276 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 3277 | } |
lypinator | 0:bb348c97df44 | 3278 | |
lypinator | 0:bb348c97df44 | 3279 | /** |
lypinator | 0:bb348c97df44 | 3280 | * @brief Initializes the CRYP peripheral in TDES ECB decryption mode. |
lypinator | 0:bb348c97df44 | 3281 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 3282 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 3283 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 3284 | * @param Size Length of the plaintext buffer, must be a multiple of 8 |
lypinator | 0:bb348c97df44 | 3285 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 3286 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 3287 | */ |
lypinator | 0:bb348c97df44 | 3288 | HAL_StatusTypeDef HAL_CRYP_TDESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) |
lypinator | 0:bb348c97df44 | 3289 | { |
lypinator | 0:bb348c97df44 | 3290 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 3291 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 3292 | |
lypinator | 0:bb348c97df44 | 3293 | if(hcryp->State == HAL_CRYP_STATE_READY) |
lypinator | 0:bb348c97df44 | 3294 | { |
lypinator | 0:bb348c97df44 | 3295 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 3296 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3297 | |
lypinator | 0:bb348c97df44 | 3298 | hcryp->CrypInCount = Size; |
lypinator | 0:bb348c97df44 | 3299 | hcryp->pCrypInBuffPtr = pCypherData; |
lypinator | 0:bb348c97df44 | 3300 | hcryp->pCrypOutBuffPtr = pPlainData; |
lypinator | 0:bb348c97df44 | 3301 | hcryp->CrypOutCount = Size; |
lypinator | 0:bb348c97df44 | 3302 | |
lypinator | 0:bb348c97df44 | 3303 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 3304 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 3305 | |
lypinator | 0:bb348c97df44 | 3306 | /* Set CRYP peripheral in TDES ECB decryption mode */ |
lypinator | 0:bb348c97df44 | 3307 | CRYP_SetTDESECBMode(hcryp, CRYP_CR_ALGODIR); |
lypinator | 0:bb348c97df44 | 3308 | |
lypinator | 0:bb348c97df44 | 3309 | /* Enable Interrupts */ |
lypinator | 0:bb348c97df44 | 3310 | __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 3311 | |
lypinator | 0:bb348c97df44 | 3312 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 3313 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 3314 | |
lypinator | 0:bb348c97df44 | 3315 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 3316 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 3317 | } |
lypinator | 0:bb348c97df44 | 3318 | else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) |
lypinator | 0:bb348c97df44 | 3319 | { |
lypinator | 0:bb348c97df44 | 3320 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
lypinator | 0:bb348c97df44 | 3321 | /* Write the Input block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 3322 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 3323 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 3324 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 3325 | |
lypinator | 0:bb348c97df44 | 3326 | hcryp->pCrypInBuffPtr += 8U; |
lypinator | 0:bb348c97df44 | 3327 | hcryp->CrypInCount -= 8U; |
lypinator | 0:bb348c97df44 | 3328 | if(hcryp->CrypInCount == 0U) |
lypinator | 0:bb348c97df44 | 3329 | { |
lypinator | 0:bb348c97df44 | 3330 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); |
lypinator | 0:bb348c97df44 | 3331 | /* Call the Input data transfer complete callback */ |
lypinator | 0:bb348c97df44 | 3332 | HAL_CRYP_InCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 3333 | } |
lypinator | 0:bb348c97df44 | 3334 | } |
lypinator | 0:bb348c97df44 | 3335 | else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) |
lypinator | 0:bb348c97df44 | 3336 | { |
lypinator | 0:bb348c97df44 | 3337 | outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; |
lypinator | 0:bb348c97df44 | 3338 | /* Read the Output block from the Output FIFO */ |
lypinator | 0:bb348c97df44 | 3339 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 3340 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 3341 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 3342 | |
lypinator | 0:bb348c97df44 | 3343 | hcryp->pCrypOutBuffPtr += 8U; |
lypinator | 0:bb348c97df44 | 3344 | hcryp->CrypOutCount -= 8U; |
lypinator | 0:bb348c97df44 | 3345 | if(hcryp->CrypOutCount == 0U) |
lypinator | 0:bb348c97df44 | 3346 | { |
lypinator | 0:bb348c97df44 | 3347 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 3348 | /* Disable CRYP */ |
lypinator | 0:bb348c97df44 | 3349 | __HAL_CRYP_DISABLE(hcryp); |
lypinator | 0:bb348c97df44 | 3350 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 3351 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3352 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 3353 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 3354 | /* Call Input transfer complete callback */ |
lypinator | 0:bb348c97df44 | 3355 | HAL_CRYP_OutCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 3356 | } |
lypinator | 0:bb348c97df44 | 3357 | } |
lypinator | 0:bb348c97df44 | 3358 | |
lypinator | 0:bb348c97df44 | 3359 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 3360 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 3361 | } |
lypinator | 0:bb348c97df44 | 3362 | |
lypinator | 0:bb348c97df44 | 3363 | /** |
lypinator | 0:bb348c97df44 | 3364 | * @brief Initializes the CRYP peripheral in TDES CBC decryption mode. |
lypinator | 0:bb348c97df44 | 3365 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 3366 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 3367 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 3368 | * @param Size Length of the plaintext buffer, must be a multiple of 8 |
lypinator | 0:bb348c97df44 | 3369 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 3370 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 3371 | */ |
lypinator | 0:bb348c97df44 | 3372 | HAL_StatusTypeDef HAL_CRYP_TDESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) |
lypinator | 0:bb348c97df44 | 3373 | { |
lypinator | 0:bb348c97df44 | 3374 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 3375 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 3376 | |
lypinator | 0:bb348c97df44 | 3377 | if(hcryp->State == HAL_CRYP_STATE_READY) |
lypinator | 0:bb348c97df44 | 3378 | { |
lypinator | 0:bb348c97df44 | 3379 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 3380 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3381 | |
lypinator | 0:bb348c97df44 | 3382 | hcryp->CrypInCount = Size; |
lypinator | 0:bb348c97df44 | 3383 | hcryp->pCrypInBuffPtr = pCypherData; |
lypinator | 0:bb348c97df44 | 3384 | hcryp->pCrypOutBuffPtr = pPlainData; |
lypinator | 0:bb348c97df44 | 3385 | hcryp->CrypOutCount = Size; |
lypinator | 0:bb348c97df44 | 3386 | |
lypinator | 0:bb348c97df44 | 3387 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 3388 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 3389 | |
lypinator | 0:bb348c97df44 | 3390 | /* Set CRYP peripheral in TDES CBC decryption mode */ |
lypinator | 0:bb348c97df44 | 3391 | CRYP_SetTDESCBCMode(hcryp, CRYP_CR_ALGODIR); |
lypinator | 0:bb348c97df44 | 3392 | |
lypinator | 0:bb348c97df44 | 3393 | /* Enable Interrupts */ |
lypinator | 0:bb348c97df44 | 3394 | __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 3395 | |
lypinator | 0:bb348c97df44 | 3396 | /* Enable CRYP */ |
lypinator | 0:bb348c97df44 | 3397 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 3398 | |
lypinator | 0:bb348c97df44 | 3399 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 3400 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 3401 | } |
lypinator | 0:bb348c97df44 | 3402 | else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) |
lypinator | 0:bb348c97df44 | 3403 | { |
lypinator | 0:bb348c97df44 | 3404 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
lypinator | 0:bb348c97df44 | 3405 | /* Write the Input block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 3406 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 3407 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 3408 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 3409 | |
lypinator | 0:bb348c97df44 | 3410 | hcryp->pCrypInBuffPtr += 8U; |
lypinator | 0:bb348c97df44 | 3411 | hcryp->CrypInCount -= 8U; |
lypinator | 0:bb348c97df44 | 3412 | if(hcryp->CrypInCount == 0U) |
lypinator | 0:bb348c97df44 | 3413 | { |
lypinator | 0:bb348c97df44 | 3414 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); |
lypinator | 0:bb348c97df44 | 3415 | /* Call the Input data transfer complete callback */ |
lypinator | 0:bb348c97df44 | 3416 | HAL_CRYP_InCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 3417 | } |
lypinator | 0:bb348c97df44 | 3418 | } |
lypinator | 0:bb348c97df44 | 3419 | else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) |
lypinator | 0:bb348c97df44 | 3420 | { |
lypinator | 0:bb348c97df44 | 3421 | outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; |
lypinator | 0:bb348c97df44 | 3422 | /* Read the Output block from the Output FIFO */ |
lypinator | 0:bb348c97df44 | 3423 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 3424 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 3425 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 3426 | |
lypinator | 0:bb348c97df44 | 3427 | hcryp->pCrypOutBuffPtr += 8U; |
lypinator | 0:bb348c97df44 | 3428 | hcryp->CrypOutCount -= 8U; |
lypinator | 0:bb348c97df44 | 3429 | if(hcryp->CrypOutCount == 0U) |
lypinator | 0:bb348c97df44 | 3430 | { |
lypinator | 0:bb348c97df44 | 3431 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 3432 | /* Disable CRYP */ |
lypinator | 0:bb348c97df44 | 3433 | __HAL_CRYP_DISABLE(hcryp); |
lypinator | 0:bb348c97df44 | 3434 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 3435 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3436 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 3437 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 3438 | /* Call Input transfer complete callback */ |
lypinator | 0:bb348c97df44 | 3439 | HAL_CRYP_OutCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 3440 | } |
lypinator | 0:bb348c97df44 | 3441 | } |
lypinator | 0:bb348c97df44 | 3442 | |
lypinator | 0:bb348c97df44 | 3443 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 3444 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 3445 | } |
lypinator | 0:bb348c97df44 | 3446 | |
lypinator | 0:bb348c97df44 | 3447 | /** |
lypinator | 0:bb348c97df44 | 3448 | * @brief Initializes the CRYP peripheral in TDES ECB encryption mode using DMA. |
lypinator | 0:bb348c97df44 | 3449 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 3450 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 3451 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 3452 | * @param Size Length of the plaintext buffer, must be a multiple of 8 |
lypinator | 0:bb348c97df44 | 3453 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 3454 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 3455 | */ |
lypinator | 0:bb348c97df44 | 3456 | HAL_StatusTypeDef HAL_CRYP_TDESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) |
lypinator | 0:bb348c97df44 | 3457 | { |
lypinator | 0:bb348c97df44 | 3458 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 3459 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 3460 | |
lypinator | 0:bb348c97df44 | 3461 | if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) |
lypinator | 0:bb348c97df44 | 3462 | { |
lypinator | 0:bb348c97df44 | 3463 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 3464 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3465 | |
lypinator | 0:bb348c97df44 | 3466 | inputaddr = (uint32_t)pPlainData; |
lypinator | 0:bb348c97df44 | 3467 | outputaddr = (uint32_t)pCypherData; |
lypinator | 0:bb348c97df44 | 3468 | |
lypinator | 0:bb348c97df44 | 3469 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 3470 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 3471 | |
lypinator | 0:bb348c97df44 | 3472 | /* Set CRYP peripheral in TDES ECB encryption mode */ |
lypinator | 0:bb348c97df44 | 3473 | CRYP_SetTDESECBMode(hcryp, 0U); |
lypinator | 0:bb348c97df44 | 3474 | |
lypinator | 0:bb348c97df44 | 3475 | /* Set the input and output addresses and start DMA transfer */ |
lypinator | 0:bb348c97df44 | 3476 | CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); |
lypinator | 0:bb348c97df44 | 3477 | |
lypinator | 0:bb348c97df44 | 3478 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 3479 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3480 | |
lypinator | 0:bb348c97df44 | 3481 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 3482 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 3483 | } |
lypinator | 0:bb348c97df44 | 3484 | else |
lypinator | 0:bb348c97df44 | 3485 | { |
lypinator | 0:bb348c97df44 | 3486 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 3487 | } |
lypinator | 0:bb348c97df44 | 3488 | } |
lypinator | 0:bb348c97df44 | 3489 | |
lypinator | 0:bb348c97df44 | 3490 | /** |
lypinator | 0:bb348c97df44 | 3491 | * @brief Initializes the CRYP peripheral in TDES CBC encryption mode using DMA. |
lypinator | 0:bb348c97df44 | 3492 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 3493 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 3494 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 3495 | * @param Size Length of the plaintext buffer, must be a multiple of 8 |
lypinator | 0:bb348c97df44 | 3496 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 3497 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 3498 | */ |
lypinator | 0:bb348c97df44 | 3499 | HAL_StatusTypeDef HAL_CRYP_TDESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) |
lypinator | 0:bb348c97df44 | 3500 | { |
lypinator | 0:bb348c97df44 | 3501 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 3502 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 3503 | |
lypinator | 0:bb348c97df44 | 3504 | if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) |
lypinator | 0:bb348c97df44 | 3505 | { |
lypinator | 0:bb348c97df44 | 3506 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 3507 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3508 | |
lypinator | 0:bb348c97df44 | 3509 | inputaddr = (uint32_t)pPlainData; |
lypinator | 0:bb348c97df44 | 3510 | outputaddr = (uint32_t)pCypherData; |
lypinator | 0:bb348c97df44 | 3511 | |
lypinator | 0:bb348c97df44 | 3512 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 3513 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 3514 | |
lypinator | 0:bb348c97df44 | 3515 | /* Set CRYP peripheral in TDES CBC encryption mode */ |
lypinator | 0:bb348c97df44 | 3516 | CRYP_SetTDESCBCMode(hcryp, 0U); |
lypinator | 0:bb348c97df44 | 3517 | |
lypinator | 0:bb348c97df44 | 3518 | /* Set the input and output addresses and start DMA transfer */ |
lypinator | 0:bb348c97df44 | 3519 | CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); |
lypinator | 0:bb348c97df44 | 3520 | |
lypinator | 0:bb348c97df44 | 3521 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 3522 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3523 | |
lypinator | 0:bb348c97df44 | 3524 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 3525 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 3526 | } |
lypinator | 0:bb348c97df44 | 3527 | else |
lypinator | 0:bb348c97df44 | 3528 | { |
lypinator | 0:bb348c97df44 | 3529 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 3530 | } |
lypinator | 0:bb348c97df44 | 3531 | } |
lypinator | 0:bb348c97df44 | 3532 | |
lypinator | 0:bb348c97df44 | 3533 | /** |
lypinator | 0:bb348c97df44 | 3534 | * @brief Initializes the CRYP peripheral in TDES ECB decryption mode using DMA. |
lypinator | 0:bb348c97df44 | 3535 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 3536 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 3537 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 3538 | * @param Size Length of the plaintext buffer, must be a multiple of 8 |
lypinator | 0:bb348c97df44 | 3539 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 3540 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 3541 | */ |
lypinator | 0:bb348c97df44 | 3542 | HAL_StatusTypeDef HAL_CRYP_TDESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) |
lypinator | 0:bb348c97df44 | 3543 | { |
lypinator | 0:bb348c97df44 | 3544 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 3545 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 3546 | |
lypinator | 0:bb348c97df44 | 3547 | if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) |
lypinator | 0:bb348c97df44 | 3548 | { |
lypinator | 0:bb348c97df44 | 3549 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 3550 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3551 | |
lypinator | 0:bb348c97df44 | 3552 | inputaddr = (uint32_t)pCypherData; |
lypinator | 0:bb348c97df44 | 3553 | outputaddr = (uint32_t)pPlainData; |
lypinator | 0:bb348c97df44 | 3554 | |
lypinator | 0:bb348c97df44 | 3555 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 3556 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 3557 | |
lypinator | 0:bb348c97df44 | 3558 | /* Set CRYP peripheral in TDES ECB decryption mode */ |
lypinator | 0:bb348c97df44 | 3559 | CRYP_SetTDESECBMode(hcryp, CRYP_CR_ALGODIR); |
lypinator | 0:bb348c97df44 | 3560 | |
lypinator | 0:bb348c97df44 | 3561 | /* Set the input and output addresses and start DMA transfer */ |
lypinator | 0:bb348c97df44 | 3562 | CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); |
lypinator | 0:bb348c97df44 | 3563 | |
lypinator | 0:bb348c97df44 | 3564 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 3565 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3566 | |
lypinator | 0:bb348c97df44 | 3567 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 3568 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 3569 | } |
lypinator | 0:bb348c97df44 | 3570 | else |
lypinator | 0:bb348c97df44 | 3571 | { |
lypinator | 0:bb348c97df44 | 3572 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 3573 | } |
lypinator | 0:bb348c97df44 | 3574 | } |
lypinator | 0:bb348c97df44 | 3575 | |
lypinator | 0:bb348c97df44 | 3576 | /** |
lypinator | 0:bb348c97df44 | 3577 | * @brief Initializes the CRYP peripheral in TDES CBC decryption mode using DMA. |
lypinator | 0:bb348c97df44 | 3578 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 3579 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 3580 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 3581 | * @param Size Length of the plaintext buffer, must be a multiple of 8 |
lypinator | 0:bb348c97df44 | 3582 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 3583 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 3584 | */ |
lypinator | 0:bb348c97df44 | 3585 | HAL_StatusTypeDef HAL_CRYP_TDESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) |
lypinator | 0:bb348c97df44 | 3586 | { |
lypinator | 0:bb348c97df44 | 3587 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 3588 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 3589 | |
lypinator | 0:bb348c97df44 | 3590 | if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) |
lypinator | 0:bb348c97df44 | 3591 | { |
lypinator | 0:bb348c97df44 | 3592 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 3593 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3594 | |
lypinator | 0:bb348c97df44 | 3595 | inputaddr = (uint32_t)pCypherData; |
lypinator | 0:bb348c97df44 | 3596 | outputaddr = (uint32_t)pPlainData; |
lypinator | 0:bb348c97df44 | 3597 | |
lypinator | 0:bb348c97df44 | 3598 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 3599 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 3600 | |
lypinator | 0:bb348c97df44 | 3601 | /* Set CRYP peripheral in TDES CBC decryption mode */ |
lypinator | 0:bb348c97df44 | 3602 | CRYP_SetTDESCBCMode(hcryp, CRYP_CR_ALGODIR); |
lypinator | 0:bb348c97df44 | 3603 | |
lypinator | 0:bb348c97df44 | 3604 | /* Set the input and output addresses and start DMA transfer */ |
lypinator | 0:bb348c97df44 | 3605 | CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); |
lypinator | 0:bb348c97df44 | 3606 | |
lypinator | 0:bb348c97df44 | 3607 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 3608 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3609 | |
lypinator | 0:bb348c97df44 | 3610 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 3611 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 3612 | } |
lypinator | 0:bb348c97df44 | 3613 | else |
lypinator | 0:bb348c97df44 | 3614 | { |
lypinator | 0:bb348c97df44 | 3615 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 3616 | } |
lypinator | 0:bb348c97df44 | 3617 | } |
lypinator | 0:bb348c97df44 | 3618 | |
lypinator | 0:bb348c97df44 | 3619 | /** |
lypinator | 0:bb348c97df44 | 3620 | * @} |
lypinator | 0:bb348c97df44 | 3621 | */ |
lypinator | 0:bb348c97df44 | 3622 | |
lypinator | 0:bb348c97df44 | 3623 | /** @defgroup CRYP_Exported_Functions_Group5 DMA callback functions |
lypinator | 0:bb348c97df44 | 3624 | * @brief DMA callback functions. |
lypinator | 0:bb348c97df44 | 3625 | * |
lypinator | 0:bb348c97df44 | 3626 | @verbatim |
lypinator | 0:bb348c97df44 | 3627 | ============================================================================== |
lypinator | 0:bb348c97df44 | 3628 | ##### DMA callback functions ##### |
lypinator | 0:bb348c97df44 | 3629 | ============================================================================== |
lypinator | 0:bb348c97df44 | 3630 | [..] This section provides DMA callback functions: |
lypinator | 0:bb348c97df44 | 3631 | (+) DMA Input data transfer complete |
lypinator | 0:bb348c97df44 | 3632 | (+) DMA Output data transfer complete |
lypinator | 0:bb348c97df44 | 3633 | (+) DMA error |
lypinator | 0:bb348c97df44 | 3634 | |
lypinator | 0:bb348c97df44 | 3635 | @endverbatim |
lypinator | 0:bb348c97df44 | 3636 | * @{ |
lypinator | 0:bb348c97df44 | 3637 | */ |
lypinator | 0:bb348c97df44 | 3638 | |
lypinator | 0:bb348c97df44 | 3639 | /** |
lypinator | 0:bb348c97df44 | 3640 | * @brief Input FIFO transfer completed callbacks. |
lypinator | 0:bb348c97df44 | 3641 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 3642 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 3643 | * @retval None |
lypinator | 0:bb348c97df44 | 3644 | */ |
lypinator | 0:bb348c97df44 | 3645 | __weak void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp) |
lypinator | 0:bb348c97df44 | 3646 | { |
lypinator | 0:bb348c97df44 | 3647 | /* Prevent unused argument(s) compilation warning */ |
lypinator | 0:bb348c97df44 | 3648 | UNUSED(hcryp); |
lypinator | 0:bb348c97df44 | 3649 | /* NOTE : This function Should not be modified, when the callback is needed, |
lypinator | 0:bb348c97df44 | 3650 | the HAL_CRYP_InCpltCallback could be implemented in the user file |
lypinator | 0:bb348c97df44 | 3651 | */ |
lypinator | 0:bb348c97df44 | 3652 | } |
lypinator | 0:bb348c97df44 | 3653 | |
lypinator | 0:bb348c97df44 | 3654 | /** |
lypinator | 0:bb348c97df44 | 3655 | * @brief Output FIFO transfer completed callbacks. |
lypinator | 0:bb348c97df44 | 3656 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 3657 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 3658 | * @retval None |
lypinator | 0:bb348c97df44 | 3659 | */ |
lypinator | 0:bb348c97df44 | 3660 | __weak void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp) |
lypinator | 0:bb348c97df44 | 3661 | { |
lypinator | 0:bb348c97df44 | 3662 | /* Prevent unused argument(s) compilation warning */ |
lypinator | 0:bb348c97df44 | 3663 | UNUSED(hcryp); |
lypinator | 0:bb348c97df44 | 3664 | /* NOTE : This function Should not be modified, when the callback is needed, |
lypinator | 0:bb348c97df44 | 3665 | the HAL_CRYP_OutCpltCallback could be implemented in the user file |
lypinator | 0:bb348c97df44 | 3666 | */ |
lypinator | 0:bb348c97df44 | 3667 | } |
lypinator | 0:bb348c97df44 | 3668 | |
lypinator | 0:bb348c97df44 | 3669 | /** |
lypinator | 0:bb348c97df44 | 3670 | * @brief CRYP error callbacks. |
lypinator | 0:bb348c97df44 | 3671 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 3672 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 3673 | * @retval None |
lypinator | 0:bb348c97df44 | 3674 | */ |
lypinator | 0:bb348c97df44 | 3675 | __weak void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp) |
lypinator | 0:bb348c97df44 | 3676 | { |
lypinator | 0:bb348c97df44 | 3677 | /* Prevent unused argument(s) compilation warning */ |
lypinator | 0:bb348c97df44 | 3678 | UNUSED(hcryp); |
lypinator | 0:bb348c97df44 | 3679 | /* NOTE : This function Should not be modified, when the callback is needed, |
lypinator | 0:bb348c97df44 | 3680 | the HAL_CRYP_ErrorCallback could be implemented in the user file |
lypinator | 0:bb348c97df44 | 3681 | */ |
lypinator | 0:bb348c97df44 | 3682 | } |
lypinator | 0:bb348c97df44 | 3683 | |
lypinator | 0:bb348c97df44 | 3684 | /** |
lypinator | 0:bb348c97df44 | 3685 | * @} |
lypinator | 0:bb348c97df44 | 3686 | */ |
lypinator | 0:bb348c97df44 | 3687 | |
lypinator | 0:bb348c97df44 | 3688 | /** @defgroup CRYP_Exported_Functions_Group6 CRYP IRQ handler management |
lypinator | 0:bb348c97df44 | 3689 | * @brief CRYP IRQ handler. |
lypinator | 0:bb348c97df44 | 3690 | * |
lypinator | 0:bb348c97df44 | 3691 | @verbatim |
lypinator | 0:bb348c97df44 | 3692 | ============================================================================== |
lypinator | 0:bb348c97df44 | 3693 | ##### CRYP IRQ handler management ##### |
lypinator | 0:bb348c97df44 | 3694 | ============================================================================== |
lypinator | 0:bb348c97df44 | 3695 | [..] This section provides CRYP IRQ handler function. |
lypinator | 0:bb348c97df44 | 3696 | |
lypinator | 0:bb348c97df44 | 3697 | @endverbatim |
lypinator | 0:bb348c97df44 | 3698 | * @{ |
lypinator | 0:bb348c97df44 | 3699 | */ |
lypinator | 0:bb348c97df44 | 3700 | |
lypinator | 0:bb348c97df44 | 3701 | /** |
lypinator | 0:bb348c97df44 | 3702 | * @brief This function handles CRYP interrupt request. |
lypinator | 0:bb348c97df44 | 3703 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 3704 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 3705 | * @retval None |
lypinator | 0:bb348c97df44 | 3706 | */ |
lypinator | 0:bb348c97df44 | 3707 | void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp) |
lypinator | 0:bb348c97df44 | 3708 | { |
lypinator | 0:bb348c97df44 | 3709 | switch(CRYP->CR & CRYP_CR_ALGOMODE_DIRECTION) |
lypinator | 0:bb348c97df44 | 3710 | { |
lypinator | 0:bb348c97df44 | 3711 | case CRYP_CR_ALGOMODE_TDES_ECB_ENCRYPT: |
lypinator | 0:bb348c97df44 | 3712 | HAL_CRYP_TDESECB_Encrypt_IT(hcryp, NULL, 0U, NULL); |
lypinator | 0:bb348c97df44 | 3713 | break; |
lypinator | 0:bb348c97df44 | 3714 | |
lypinator | 0:bb348c97df44 | 3715 | case CRYP_CR_ALGOMODE_TDES_ECB_DECRYPT: |
lypinator | 0:bb348c97df44 | 3716 | HAL_CRYP_TDESECB_Decrypt_IT(hcryp, NULL, 0U, NULL); |
lypinator | 0:bb348c97df44 | 3717 | break; |
lypinator | 0:bb348c97df44 | 3718 | |
lypinator | 0:bb348c97df44 | 3719 | case CRYP_CR_ALGOMODE_TDES_CBC_ENCRYPT: |
lypinator | 0:bb348c97df44 | 3720 | HAL_CRYP_TDESCBC_Encrypt_IT(hcryp, NULL, 0U, NULL); |
lypinator | 0:bb348c97df44 | 3721 | break; |
lypinator | 0:bb348c97df44 | 3722 | |
lypinator | 0:bb348c97df44 | 3723 | case CRYP_CR_ALGOMODE_TDES_CBC_DECRYPT: |
lypinator | 0:bb348c97df44 | 3724 | HAL_CRYP_TDESCBC_Decrypt_IT(hcryp, NULL, 0U, NULL); |
lypinator | 0:bb348c97df44 | 3725 | break; |
lypinator | 0:bb348c97df44 | 3726 | |
lypinator | 0:bb348c97df44 | 3727 | case CRYP_CR_ALGOMODE_DES_ECB_ENCRYPT: |
lypinator | 0:bb348c97df44 | 3728 | HAL_CRYP_DESECB_Encrypt_IT(hcryp, NULL, 0U, NULL); |
lypinator | 0:bb348c97df44 | 3729 | break; |
lypinator | 0:bb348c97df44 | 3730 | |
lypinator | 0:bb348c97df44 | 3731 | case CRYP_CR_ALGOMODE_DES_ECB_DECRYPT: |
lypinator | 0:bb348c97df44 | 3732 | HAL_CRYP_DESECB_Decrypt_IT(hcryp, NULL, 0U, NULL); |
lypinator | 0:bb348c97df44 | 3733 | break; |
lypinator | 0:bb348c97df44 | 3734 | |
lypinator | 0:bb348c97df44 | 3735 | case CRYP_CR_ALGOMODE_DES_CBC_ENCRYPT: |
lypinator | 0:bb348c97df44 | 3736 | HAL_CRYP_DESCBC_Encrypt_IT(hcryp, NULL, 0U, NULL); |
lypinator | 0:bb348c97df44 | 3737 | break; |
lypinator | 0:bb348c97df44 | 3738 | |
lypinator | 0:bb348c97df44 | 3739 | case CRYP_CR_ALGOMODE_DES_CBC_DECRYPT: |
lypinator | 0:bb348c97df44 | 3740 | HAL_CRYP_DESCBC_Decrypt_IT(hcryp, NULL, 0U, NULL); |
lypinator | 0:bb348c97df44 | 3741 | break; |
lypinator | 0:bb348c97df44 | 3742 | |
lypinator | 0:bb348c97df44 | 3743 | case CRYP_CR_ALGOMODE_AES_ECB_ENCRYPT: |
lypinator | 0:bb348c97df44 | 3744 | HAL_CRYP_AESECB_Encrypt_IT(hcryp, NULL, 0U, NULL); |
lypinator | 0:bb348c97df44 | 3745 | break; |
lypinator | 0:bb348c97df44 | 3746 | |
lypinator | 0:bb348c97df44 | 3747 | case CRYP_CR_ALGOMODE_AES_ECB_DECRYPT: |
lypinator | 0:bb348c97df44 | 3748 | HAL_CRYP_AESECB_Decrypt_IT(hcryp, NULL, 0U, NULL); |
lypinator | 0:bb348c97df44 | 3749 | break; |
lypinator | 0:bb348c97df44 | 3750 | |
lypinator | 0:bb348c97df44 | 3751 | case CRYP_CR_ALGOMODE_AES_CBC_ENCRYPT: |
lypinator | 0:bb348c97df44 | 3752 | HAL_CRYP_AESCBC_Encrypt_IT(hcryp, NULL, 0U, NULL); |
lypinator | 0:bb348c97df44 | 3753 | break; |
lypinator | 0:bb348c97df44 | 3754 | |
lypinator | 0:bb348c97df44 | 3755 | case CRYP_CR_ALGOMODE_AES_CBC_DECRYPT: |
lypinator | 0:bb348c97df44 | 3756 | HAL_CRYP_AESCBC_Decrypt_IT(hcryp, NULL, 0U, NULL); |
lypinator | 0:bb348c97df44 | 3757 | break; |
lypinator | 0:bb348c97df44 | 3758 | |
lypinator | 0:bb348c97df44 | 3759 | case CRYP_CR_ALGOMODE_AES_CTR_ENCRYPT: |
lypinator | 0:bb348c97df44 | 3760 | HAL_CRYP_AESCTR_Encrypt_IT(hcryp, NULL, 0U, NULL); |
lypinator | 0:bb348c97df44 | 3761 | break; |
lypinator | 0:bb348c97df44 | 3762 | |
lypinator | 0:bb348c97df44 | 3763 | case CRYP_CR_ALGOMODE_AES_CTR_DECRYPT: |
lypinator | 0:bb348c97df44 | 3764 | HAL_CRYP_AESCTR_Decrypt_IT(hcryp, NULL, 0U, NULL); |
lypinator | 0:bb348c97df44 | 3765 | break; |
lypinator | 0:bb348c97df44 | 3766 | |
lypinator | 0:bb348c97df44 | 3767 | default: |
lypinator | 0:bb348c97df44 | 3768 | break; |
lypinator | 0:bb348c97df44 | 3769 | } |
lypinator | 0:bb348c97df44 | 3770 | } |
lypinator | 0:bb348c97df44 | 3771 | |
lypinator | 0:bb348c97df44 | 3772 | /** |
lypinator | 0:bb348c97df44 | 3773 | * @} |
lypinator | 0:bb348c97df44 | 3774 | */ |
lypinator | 0:bb348c97df44 | 3775 | |
lypinator | 0:bb348c97df44 | 3776 | /** @defgroup CRYP_Exported_Functions_Group7 Peripheral State functions |
lypinator | 0:bb348c97df44 | 3777 | * @brief Peripheral State functions. |
lypinator | 0:bb348c97df44 | 3778 | * |
lypinator | 0:bb348c97df44 | 3779 | @verbatim |
lypinator | 0:bb348c97df44 | 3780 | ============================================================================== |
lypinator | 0:bb348c97df44 | 3781 | ##### Peripheral State functions ##### |
lypinator | 0:bb348c97df44 | 3782 | ============================================================================== |
lypinator | 0:bb348c97df44 | 3783 | [..] |
lypinator | 0:bb348c97df44 | 3784 | This subsection permits to get in run-time the status of the peripheral. |
lypinator | 0:bb348c97df44 | 3785 | |
lypinator | 0:bb348c97df44 | 3786 | @endverbatim |
lypinator | 0:bb348c97df44 | 3787 | * @{ |
lypinator | 0:bb348c97df44 | 3788 | */ |
lypinator | 0:bb348c97df44 | 3789 | |
lypinator | 0:bb348c97df44 | 3790 | /** |
lypinator | 0:bb348c97df44 | 3791 | * @brief Returns the CRYP state. |
lypinator | 0:bb348c97df44 | 3792 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 3793 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 3794 | * @retval HAL state |
lypinator | 0:bb348c97df44 | 3795 | */ |
lypinator | 0:bb348c97df44 | 3796 | HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp) |
lypinator | 0:bb348c97df44 | 3797 | { |
lypinator | 0:bb348c97df44 | 3798 | return hcryp->State; |
lypinator | 0:bb348c97df44 | 3799 | } |
lypinator | 0:bb348c97df44 | 3800 | |
lypinator | 0:bb348c97df44 | 3801 | /** |
lypinator | 0:bb348c97df44 | 3802 | * @} |
lypinator | 0:bb348c97df44 | 3803 | */ |
lypinator | 0:bb348c97df44 | 3804 | |
lypinator | 0:bb348c97df44 | 3805 | |
lypinator | 0:bb348c97df44 | 3806 | /** |
lypinator | 0:bb348c97df44 | 3807 | * @} |
lypinator | 0:bb348c97df44 | 3808 | */ |
lypinator | 0:bb348c97df44 | 3809 | |
lypinator | 0:bb348c97df44 | 3810 | #endif /* CRYP */ |
lypinator | 0:bb348c97df44 | 3811 | |
lypinator | 0:bb348c97df44 | 3812 | #if defined (AES) |
lypinator | 0:bb348c97df44 | 3813 | |
lypinator | 0:bb348c97df44 | 3814 | /** @defgroup AES AES |
lypinator | 0:bb348c97df44 | 3815 | * @brief AES HAL module driver. |
lypinator | 0:bb348c97df44 | 3816 | * @{ |
lypinator | 0:bb348c97df44 | 3817 | */ |
lypinator | 0:bb348c97df44 | 3818 | |
lypinator | 0:bb348c97df44 | 3819 | /* Private typedef -----------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 3820 | /* Private define ------------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 3821 | /* Private macro -------------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 3822 | /* Private variables ---------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 3823 | /* Private functions --------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 3824 | |
lypinator | 0:bb348c97df44 | 3825 | /** @defgroup CRYP_Private_Functions CRYP Private Functions |
lypinator | 0:bb348c97df44 | 3826 | * @{ |
lypinator | 0:bb348c97df44 | 3827 | */ |
lypinator | 0:bb348c97df44 | 3828 | |
lypinator | 0:bb348c97df44 | 3829 | static HAL_StatusTypeDef CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp); |
lypinator | 0:bb348c97df44 | 3830 | static HAL_StatusTypeDef CRYP_SetKey(CRYP_HandleTypeDef *hcryp); |
lypinator | 0:bb348c97df44 | 3831 | static HAL_StatusTypeDef CRYP_AES_IT(CRYP_HandleTypeDef *hcryp); |
lypinator | 0:bb348c97df44 | 3832 | |
lypinator | 0:bb348c97df44 | 3833 | /** |
lypinator | 0:bb348c97df44 | 3834 | * @} |
lypinator | 0:bb348c97df44 | 3835 | */ |
lypinator | 0:bb348c97df44 | 3836 | |
lypinator | 0:bb348c97df44 | 3837 | /* Exported functions ---------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 3838 | |
lypinator | 0:bb348c97df44 | 3839 | /** @defgroup CRYP_Exported_Functions CRYP Exported Functions |
lypinator | 0:bb348c97df44 | 3840 | * @{ |
lypinator | 0:bb348c97df44 | 3841 | */ |
lypinator | 0:bb348c97df44 | 3842 | |
lypinator | 0:bb348c97df44 | 3843 | /** @defgroup CRYP_Exported_Functions_Group1 Initialization and deinitialization functions |
lypinator | 0:bb348c97df44 | 3844 | * @brief Initialization and Configuration functions. |
lypinator | 0:bb348c97df44 | 3845 | * |
lypinator | 0:bb348c97df44 | 3846 | @verbatim |
lypinator | 0:bb348c97df44 | 3847 | ============================================================================== |
lypinator | 0:bb348c97df44 | 3848 | ##### Initialization and deinitialization functions ##### |
lypinator | 0:bb348c97df44 | 3849 | ============================================================================== |
lypinator | 0:bb348c97df44 | 3850 | [..] This section provides functions allowing to: |
lypinator | 0:bb348c97df44 | 3851 | (+) Initialize the CRYP according to the specified parameters |
lypinator | 0:bb348c97df44 | 3852 | in the CRYP_InitTypeDef and creates the associated handle |
lypinator | 0:bb348c97df44 | 3853 | (+) DeInitialize the CRYP peripheral |
lypinator | 0:bb348c97df44 | 3854 | (+) Initialize the CRYP MSP (MCU Specific Package) |
lypinator | 0:bb348c97df44 | 3855 | (+) De-Initialize the CRYP MSP |
lypinator | 0:bb348c97df44 | 3856 | |
lypinator | 0:bb348c97df44 | 3857 | [..] |
lypinator | 0:bb348c97df44 | 3858 | (@) Specific care must be taken to format the key and the Initialization Vector IV! |
lypinator | 0:bb348c97df44 | 3859 | |
lypinator | 0:bb348c97df44 | 3860 | [..] If the key is defined as a 128-bit long array key[127..0] = {b127 ... b0} where |
lypinator | 0:bb348c97df44 | 3861 | b127 is the MSB and b0 the LSB, the key must be stored in MCU memory |
lypinator | 0:bb348c97df44 | 3862 | (+) as a sequence of words where the MSB word comes first (occupies the |
lypinator | 0:bb348c97df44 | 3863 | lowest memory address) |
lypinator | 0:bb348c97df44 | 3864 | (+) where each word is byte-swapped: |
lypinator | 0:bb348c97df44 | 3865 | (++) address n+0 : 0b b103 .. b96 b111 .. b104 b119 .. b112 b127 .. b120 |
lypinator | 0:bb348c97df44 | 3866 | (++) address n+4 : 0b b71 .. b64 b79 .. b72 b87 .. b80 b95 .. b88 |
lypinator | 0:bb348c97df44 | 3867 | (++) address n+8 : 0b b39 .. b32 b47 .. b40 b55 .. b48 b63 .. b56 |
lypinator | 0:bb348c97df44 | 3868 | (++) address n+C : 0b b7 .. b0 b15 .. b8 b23 .. b16 b31 .. b24 |
lypinator | 0:bb348c97df44 | 3869 | [..] Hereafter, another illustration when considering a 128-bit long key made of 16 bytes {B15..B0}. |
lypinator | 0:bb348c97df44 | 3870 | The 4 32-bit words that make the key must be stored as follows in MCU memory: |
lypinator | 0:bb348c97df44 | 3871 | (+) address n+0 : 0x B12 B13 B14 B15 |
lypinator | 0:bb348c97df44 | 3872 | (+) address n+4 : 0x B8 B9 B10 B11 |
lypinator | 0:bb348c97df44 | 3873 | (+) address n+8 : 0x B4 B5 B6 B7 |
lypinator | 0:bb348c97df44 | 3874 | (+) address n+C : 0x B0 B1 B2 B3 |
lypinator | 0:bb348c97df44 | 3875 | [..] which leads to the expected setting |
lypinator | 0:bb348c97df44 | 3876 | (+) AES_KEYR3 = 0x B15 B14 B13 B12 |
lypinator | 0:bb348c97df44 | 3877 | (+) AES_KEYR2 = 0x B11 B10 B9 B8 |
lypinator | 0:bb348c97df44 | 3878 | (+) AES_KEYR1 = 0x B7 B6 B5 B4 |
lypinator | 0:bb348c97df44 | 3879 | (+) AES_KEYR0 = 0x B3 B2 B1 B0 |
lypinator | 0:bb348c97df44 | 3880 | |
lypinator | 0:bb348c97df44 | 3881 | [..] Same format must be applied for a 256-bit long key made of 32 bytes {B31..B0}. |
lypinator | 0:bb348c97df44 | 3882 | The 8 32-bit words that make the key must be stored as follows in MCU memory: |
lypinator | 0:bb348c97df44 | 3883 | (+) address n+00 : 0x B28 B29 B30 B31 |
lypinator | 0:bb348c97df44 | 3884 | (+) address n+04 : 0x B24 B25 B26 B27 |
lypinator | 0:bb348c97df44 | 3885 | (+) address n+08 : 0x B20 B21 B22 B23 |
lypinator | 0:bb348c97df44 | 3886 | (+) address n+0C : 0x B16 B17 B18 B19 |
lypinator | 0:bb348c97df44 | 3887 | (+) address n+10 : 0x B12 B13 B14 B15 |
lypinator | 0:bb348c97df44 | 3888 | (+) address n+14 : 0x B8 B9 B10 B11 |
lypinator | 0:bb348c97df44 | 3889 | (+) address n+18 : 0x B4 B5 B6 B7 |
lypinator | 0:bb348c97df44 | 3890 | (+) address n+1C : 0x B0 B1 B2 B3 |
lypinator | 0:bb348c97df44 | 3891 | [..] which leads to the expected setting |
lypinator | 0:bb348c97df44 | 3892 | (+) AES_KEYR7 = 0x B31 B30 B29 B28 |
lypinator | 0:bb348c97df44 | 3893 | (+) AES_KEYR6 = 0x B27 B26 B25 B24 |
lypinator | 0:bb348c97df44 | 3894 | (+) AES_KEYR5 = 0x B23 B22 B21 B20 |
lypinator | 0:bb348c97df44 | 3895 | (+) AES_KEYR4 = 0x B19 B18 B17 B16 |
lypinator | 0:bb348c97df44 | 3896 | (+) AES_KEYR3 = 0x B15 B14 B13 B12 |
lypinator | 0:bb348c97df44 | 3897 | (+) AES_KEYR2 = 0x B11 B10 B9 B8 |
lypinator | 0:bb348c97df44 | 3898 | (+) AES_KEYR1 = 0x B7 B6 B5 B4 |
lypinator | 0:bb348c97df44 | 3899 | (+) AES_KEYR0 = 0x B3 B2 B1 B0 |
lypinator | 0:bb348c97df44 | 3900 | |
lypinator | 0:bb348c97df44 | 3901 | [..] Initialization Vector IV (4 32-bit words) format must follow the same as |
lypinator | 0:bb348c97df44 | 3902 | that of a 128-bit long key. |
lypinator | 0:bb348c97df44 | 3903 | |
lypinator | 0:bb348c97df44 | 3904 | [..] |
lypinator | 0:bb348c97df44 | 3905 | |
lypinator | 0:bb348c97df44 | 3906 | @endverbatim |
lypinator | 0:bb348c97df44 | 3907 | * @{ |
lypinator | 0:bb348c97df44 | 3908 | */ |
lypinator | 0:bb348c97df44 | 3909 | |
lypinator | 0:bb348c97df44 | 3910 | /** |
lypinator | 0:bb348c97df44 | 3911 | * @brief Initialize the CRYP according to the specified |
lypinator | 0:bb348c97df44 | 3912 | * parameters in the CRYP_InitTypeDef and initialize the associated handle. |
lypinator | 0:bb348c97df44 | 3913 | * @note Specific care must be taken to format the key and the Initialization Vector IV |
lypinator | 0:bb348c97df44 | 3914 | * stored in the MCU memory before calling HAL_CRYP_Init(). Refer to explanations |
lypinator | 0:bb348c97df44 | 3915 | * hereabove. |
lypinator | 0:bb348c97df44 | 3916 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 3917 | */ |
lypinator | 0:bb348c97df44 | 3918 | HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp) |
lypinator | 0:bb348c97df44 | 3919 | { |
lypinator | 0:bb348c97df44 | 3920 | /* Check the CRYP handle allocation */ |
lypinator | 0:bb348c97df44 | 3921 | if(hcryp == NULL) |
lypinator | 0:bb348c97df44 | 3922 | { |
lypinator | 0:bb348c97df44 | 3923 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 3924 | } |
lypinator | 0:bb348c97df44 | 3925 | |
lypinator | 0:bb348c97df44 | 3926 | /* Check the instance */ |
lypinator | 0:bb348c97df44 | 3927 | assert_param(IS_AES_ALL_INSTANCE(hcryp->Instance)); |
lypinator | 0:bb348c97df44 | 3928 | |
lypinator | 0:bb348c97df44 | 3929 | /* Check the parameters */ |
lypinator | 0:bb348c97df44 | 3930 | assert_param(IS_CRYP_KEYSIZE(hcryp->Init.KeySize)); |
lypinator | 0:bb348c97df44 | 3931 | assert_param(IS_CRYP_DATATYPE(hcryp->Init.DataType)); |
lypinator | 0:bb348c97df44 | 3932 | assert_param(IS_CRYP_ALGOMODE(hcryp->Init.OperatingMode)); |
lypinator | 0:bb348c97df44 | 3933 | /* ChainingMode parameter is irrelevant when mode is set to Key derivation */ |
lypinator | 0:bb348c97df44 | 3934 | if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION) |
lypinator | 0:bb348c97df44 | 3935 | { |
lypinator | 0:bb348c97df44 | 3936 | assert_param(IS_CRYP_CHAINMODE(hcryp->Init.ChainingMode)); |
lypinator | 0:bb348c97df44 | 3937 | } |
lypinator | 0:bb348c97df44 | 3938 | assert_param(IS_CRYP_WRITE(hcryp->Init.KeyWriteFlag)); |
lypinator | 0:bb348c97df44 | 3939 | |
lypinator | 0:bb348c97df44 | 3940 | /*========================================================*/ |
lypinator | 0:bb348c97df44 | 3941 | /* Check the proper operating/chaining modes combinations */ |
lypinator | 0:bb348c97df44 | 3942 | /*========================================================*/ |
lypinator | 0:bb348c97df44 | 3943 | /* Check the proper chaining when the operating mode is key derivation and decryption */ |
lypinator | 0:bb348c97df44 | 3944 | #if defined(AES_CR_NPBLB) |
lypinator | 0:bb348c97df44 | 3945 | if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION_DECRYPT) &&\ |
lypinator | 0:bb348c97df44 | 3946 | ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CTR) \ |
lypinator | 0:bb348c97df44 | 3947 | || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) \ |
lypinator | 0:bb348c97df44 | 3948 | || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC))) |
lypinator | 0:bb348c97df44 | 3949 | #else |
lypinator | 0:bb348c97df44 | 3950 | if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION_DECRYPT) &&\ |
lypinator | 0:bb348c97df44 | 3951 | ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CTR) \ |
lypinator | 0:bb348c97df44 | 3952 | || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) \ |
lypinator | 0:bb348c97df44 | 3953 | || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC))) |
lypinator | 0:bb348c97df44 | 3954 | #endif |
lypinator | 0:bb348c97df44 | 3955 | { |
lypinator | 0:bb348c97df44 | 3956 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 3957 | } |
lypinator | 0:bb348c97df44 | 3958 | /* Check that key derivation is not set in CMAC mode or CCM mode when applicable */ |
lypinator | 0:bb348c97df44 | 3959 | #if defined(AES_CR_NPBLB) |
lypinator | 0:bb348c97df44 | 3960 | if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION) |
lypinator | 0:bb348c97df44 | 3961 | && (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC)) |
lypinator | 0:bb348c97df44 | 3962 | #else |
lypinator | 0:bb348c97df44 | 3963 | if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION) |
lypinator | 0:bb348c97df44 | 3964 | && (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)) |
lypinator | 0:bb348c97df44 | 3965 | #endif |
lypinator | 0:bb348c97df44 | 3966 | { |
lypinator | 0:bb348c97df44 | 3967 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 3968 | } |
lypinator | 0:bb348c97df44 | 3969 | |
lypinator | 0:bb348c97df44 | 3970 | |
lypinator | 0:bb348c97df44 | 3971 | /*================*/ |
lypinator | 0:bb348c97df44 | 3972 | /* Initialization */ |
lypinator | 0:bb348c97df44 | 3973 | /*================*/ |
lypinator | 0:bb348c97df44 | 3974 | /* Initialization start */ |
lypinator | 0:bb348c97df44 | 3975 | if(hcryp->State == HAL_CRYP_STATE_RESET) |
lypinator | 0:bb348c97df44 | 3976 | { |
lypinator | 0:bb348c97df44 | 3977 | /* Allocate lock resource and initialize it */ |
lypinator | 0:bb348c97df44 | 3978 | hcryp->Lock = HAL_UNLOCKED; |
lypinator | 0:bb348c97df44 | 3979 | |
lypinator | 0:bb348c97df44 | 3980 | /* Init the low level hardware */ |
lypinator | 0:bb348c97df44 | 3981 | HAL_CRYP_MspInit(hcryp); |
lypinator | 0:bb348c97df44 | 3982 | } |
lypinator | 0:bb348c97df44 | 3983 | |
lypinator | 0:bb348c97df44 | 3984 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 3985 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 3986 | |
lypinator | 0:bb348c97df44 | 3987 | /* Disable the Peripheral */ |
lypinator | 0:bb348c97df44 | 3988 | __HAL_CRYP_DISABLE(); |
lypinator | 0:bb348c97df44 | 3989 | |
lypinator | 0:bb348c97df44 | 3990 | /*=============================================================*/ |
lypinator | 0:bb348c97df44 | 3991 | /* AES initialization common to all operating modes */ |
lypinator | 0:bb348c97df44 | 3992 | /*=============================================================*/ |
lypinator | 0:bb348c97df44 | 3993 | /* Set the Key size selection */ |
lypinator | 0:bb348c97df44 | 3994 | MODIFY_REG(hcryp->Instance->CR, AES_CR_KEYSIZE, hcryp->Init.KeySize); |
lypinator | 0:bb348c97df44 | 3995 | |
lypinator | 0:bb348c97df44 | 3996 | /* Set the default CRYP phase when this parameter is not used. |
lypinator | 0:bb348c97df44 | 3997 | Phase is updated below in case of GCM/GMAC/CMAC(/CCM) setting. */ |
lypinator | 0:bb348c97df44 | 3998 | hcryp->Phase = HAL_CRYP_PHASE_NOT_USED; |
lypinator | 0:bb348c97df44 | 3999 | |
lypinator | 0:bb348c97df44 | 4000 | |
lypinator | 0:bb348c97df44 | 4001 | |
lypinator | 0:bb348c97df44 | 4002 | /*=============================================================*/ |
lypinator | 0:bb348c97df44 | 4003 | /* Carry on the initialization based on the AES operating mode */ |
lypinator | 0:bb348c97df44 | 4004 | /*=============================================================*/ |
lypinator | 0:bb348c97df44 | 4005 | /* Key derivation */ |
lypinator | 0:bb348c97df44 | 4006 | if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION) |
lypinator | 0:bb348c97df44 | 4007 | { |
lypinator | 0:bb348c97df44 | 4008 | MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_ALGOMODE_KEYDERIVATION); |
lypinator | 0:bb348c97df44 | 4009 | |
lypinator | 0:bb348c97df44 | 4010 | /* Configure the Key registers */ |
lypinator | 0:bb348c97df44 | 4011 | if (CRYP_SetKey(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4012 | { |
lypinator | 0:bb348c97df44 | 4013 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4014 | } |
lypinator | 0:bb348c97df44 | 4015 | } |
lypinator | 0:bb348c97df44 | 4016 | else |
lypinator | 0:bb348c97df44 | 4017 | /* Encryption / Decryption (with or without key derivation) / authentication */ |
lypinator | 0:bb348c97df44 | 4018 | { |
lypinator | 0:bb348c97df44 | 4019 | /* Set data type, operating and chaining modes. |
lypinator | 0:bb348c97df44 | 4020 | In case of GCM or GMAC, data type is forced to 0b00 */ |
lypinator | 0:bb348c97df44 | 4021 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) |
lypinator | 0:bb348c97df44 | 4022 | { |
lypinator | 0:bb348c97df44 | 4023 | MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE|AES_CR_MODE|AES_CR_CHMOD, hcryp->Init.OperatingMode|hcryp->Init.ChainingMode); |
lypinator | 0:bb348c97df44 | 4024 | } |
lypinator | 0:bb348c97df44 | 4025 | else |
lypinator | 0:bb348c97df44 | 4026 | { |
lypinator | 0:bb348c97df44 | 4027 | MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE|AES_CR_MODE|AES_CR_CHMOD, hcryp->Init.DataType|hcryp->Init.OperatingMode|hcryp->Init.ChainingMode); |
lypinator | 0:bb348c97df44 | 4028 | } |
lypinator | 0:bb348c97df44 | 4029 | |
lypinator | 0:bb348c97df44 | 4030 | |
lypinator | 0:bb348c97df44 | 4031 | /* Specify the encryption/decryption phase in case of Galois counter mode (GCM), |
lypinator | 0:bb348c97df44 | 4032 | Galois message authentication code (GMAC), cipher message authentication code (CMAC) |
lypinator | 0:bb348c97df44 | 4033 | or Counter with Cipher Mode (CCM) when applicable */ |
lypinator | 0:bb348c97df44 | 4034 | #if defined(AES_CR_NPBLB) |
lypinator | 0:bb348c97df44 | 4035 | if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) |
lypinator | 0:bb348c97df44 | 4036 | || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC)) |
lypinator | 0:bb348c97df44 | 4037 | #else |
lypinator | 0:bb348c97df44 | 4038 | if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) |
lypinator | 0:bb348c97df44 | 4039 | || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)) |
lypinator | 0:bb348c97df44 | 4040 | #endif |
lypinator | 0:bb348c97df44 | 4041 | { |
lypinator | 0:bb348c97df44 | 4042 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, hcryp->Init.GCMCMACPhase); |
lypinator | 0:bb348c97df44 | 4043 | hcryp->Phase = HAL_CRYP_PHASE_START; |
lypinator | 0:bb348c97df44 | 4044 | } |
lypinator | 0:bb348c97df44 | 4045 | |
lypinator | 0:bb348c97df44 | 4046 | |
lypinator | 0:bb348c97df44 | 4047 | /* Configure the Key registers if no need to bypass this step */ |
lypinator | 0:bb348c97df44 | 4048 | if (hcryp->Init.KeyWriteFlag == CRYP_KEY_WRITE_ENABLE) |
lypinator | 0:bb348c97df44 | 4049 | { |
lypinator | 0:bb348c97df44 | 4050 | if (CRYP_SetKey(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4051 | { |
lypinator | 0:bb348c97df44 | 4052 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4053 | } |
lypinator | 0:bb348c97df44 | 4054 | } |
lypinator | 0:bb348c97df44 | 4055 | |
lypinator | 0:bb348c97df44 | 4056 | /* If applicable, configure the Initialization Vector */ |
lypinator | 0:bb348c97df44 | 4057 | if (hcryp->Init.ChainingMode != CRYP_CHAINMODE_AES_ECB) |
lypinator | 0:bb348c97df44 | 4058 | { |
lypinator | 0:bb348c97df44 | 4059 | if (CRYP_SetInitVector(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4060 | { |
lypinator | 0:bb348c97df44 | 4061 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4062 | } |
lypinator | 0:bb348c97df44 | 4063 | } |
lypinator | 0:bb348c97df44 | 4064 | } |
lypinator | 0:bb348c97df44 | 4065 | |
lypinator | 0:bb348c97df44 | 4066 | #if defined(AES_CR_NPBLB) |
lypinator | 0:bb348c97df44 | 4067 | /* Clear NPBLB field */ |
lypinator | 0:bb348c97df44 | 4068 | CLEAR_BIT(hcryp->Instance->CR, AES_CR_NPBLB); |
lypinator | 0:bb348c97df44 | 4069 | #endif |
lypinator | 0:bb348c97df44 | 4070 | |
lypinator | 0:bb348c97df44 | 4071 | /* Reset CrypInCount and CrypOutCount */ |
lypinator | 0:bb348c97df44 | 4072 | hcryp->CrypInCount = 0U; |
lypinator | 0:bb348c97df44 | 4073 | hcryp->CrypOutCount = 0U; |
lypinator | 0:bb348c97df44 | 4074 | |
lypinator | 0:bb348c97df44 | 4075 | /* Reset ErrorCode field */ |
lypinator | 0:bb348c97df44 | 4076 | hcryp->ErrorCode = HAL_CRYP_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 4077 | |
lypinator | 0:bb348c97df44 | 4078 | /* Reset Mode suspension request */ |
lypinator | 0:bb348c97df44 | 4079 | hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE; |
lypinator | 0:bb348c97df44 | 4080 | |
lypinator | 0:bb348c97df44 | 4081 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 4082 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 4083 | |
lypinator | 0:bb348c97df44 | 4084 | /* Enable the Peripheral */ |
lypinator | 0:bb348c97df44 | 4085 | __HAL_CRYP_ENABLE(); |
lypinator | 0:bb348c97df44 | 4086 | |
lypinator | 0:bb348c97df44 | 4087 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 4088 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 4089 | } |
lypinator | 0:bb348c97df44 | 4090 | |
lypinator | 0:bb348c97df44 | 4091 | /** |
lypinator | 0:bb348c97df44 | 4092 | * @brief DeInitialize the CRYP peripheral. |
lypinator | 0:bb348c97df44 | 4093 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4094 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 4095 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 4096 | */ |
lypinator | 0:bb348c97df44 | 4097 | HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp) |
lypinator | 0:bb348c97df44 | 4098 | { |
lypinator | 0:bb348c97df44 | 4099 | /* Check the CRYP handle allocation */ |
lypinator | 0:bb348c97df44 | 4100 | if(hcryp == NULL) |
lypinator | 0:bb348c97df44 | 4101 | { |
lypinator | 0:bb348c97df44 | 4102 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4103 | } |
lypinator | 0:bb348c97df44 | 4104 | |
lypinator | 0:bb348c97df44 | 4105 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 4106 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 4107 | |
lypinator | 0:bb348c97df44 | 4108 | /* Set the default CRYP phase */ |
lypinator | 0:bb348c97df44 | 4109 | hcryp->Phase = HAL_CRYP_PHASE_READY; |
lypinator | 0:bb348c97df44 | 4110 | |
lypinator | 0:bb348c97df44 | 4111 | /* Reset CrypInCount and CrypOutCount */ |
lypinator | 0:bb348c97df44 | 4112 | hcryp->CrypInCount = 0U; |
lypinator | 0:bb348c97df44 | 4113 | hcryp->CrypOutCount = 0U; |
lypinator | 0:bb348c97df44 | 4114 | |
lypinator | 0:bb348c97df44 | 4115 | /* Disable the CRYP Peripheral Clock */ |
lypinator | 0:bb348c97df44 | 4116 | __HAL_CRYP_DISABLE(); |
lypinator | 0:bb348c97df44 | 4117 | |
lypinator | 0:bb348c97df44 | 4118 | /* DeInit the low level hardware: CLOCK, NVIC.*/ |
lypinator | 0:bb348c97df44 | 4119 | HAL_CRYP_MspDeInit(hcryp); |
lypinator | 0:bb348c97df44 | 4120 | |
lypinator | 0:bb348c97df44 | 4121 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 4122 | hcryp->State = HAL_CRYP_STATE_RESET; |
lypinator | 0:bb348c97df44 | 4123 | |
lypinator | 0:bb348c97df44 | 4124 | /* Release Lock */ |
lypinator | 0:bb348c97df44 | 4125 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 4126 | |
lypinator | 0:bb348c97df44 | 4127 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 4128 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 4129 | } |
lypinator | 0:bb348c97df44 | 4130 | |
lypinator | 0:bb348c97df44 | 4131 | /** |
lypinator | 0:bb348c97df44 | 4132 | * @brief Initialize the CRYP MSP. |
lypinator | 0:bb348c97df44 | 4133 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4134 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 4135 | * @retval None |
lypinator | 0:bb348c97df44 | 4136 | */ |
lypinator | 0:bb348c97df44 | 4137 | __weak void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp) |
lypinator | 0:bb348c97df44 | 4138 | { |
lypinator | 0:bb348c97df44 | 4139 | /* Prevent unused argument(s) compilation warning */ |
lypinator | 0:bb348c97df44 | 4140 | UNUSED(hcryp); |
lypinator | 0:bb348c97df44 | 4141 | |
lypinator | 0:bb348c97df44 | 4142 | /* NOTE : This function should not be modified; when the callback is needed, |
lypinator | 0:bb348c97df44 | 4143 | the HAL_CRYP_MspInit can be implemented in the user file |
lypinator | 0:bb348c97df44 | 4144 | */ |
lypinator | 0:bb348c97df44 | 4145 | } |
lypinator | 0:bb348c97df44 | 4146 | |
lypinator | 0:bb348c97df44 | 4147 | /** |
lypinator | 0:bb348c97df44 | 4148 | * @brief DeInitialize CRYP MSP. |
lypinator | 0:bb348c97df44 | 4149 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4150 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 4151 | * @retval None |
lypinator | 0:bb348c97df44 | 4152 | */ |
lypinator | 0:bb348c97df44 | 4153 | __weak void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp) |
lypinator | 0:bb348c97df44 | 4154 | { |
lypinator | 0:bb348c97df44 | 4155 | /* Prevent unused argument(s) compilation warning */ |
lypinator | 0:bb348c97df44 | 4156 | UNUSED(hcryp); |
lypinator | 0:bb348c97df44 | 4157 | |
lypinator | 0:bb348c97df44 | 4158 | /* NOTE : This function should not be modified; when the callback is needed, |
lypinator | 0:bb348c97df44 | 4159 | the HAL_CRYP_MspDeInit can be implemented in the user file |
lypinator | 0:bb348c97df44 | 4160 | */ |
lypinator | 0:bb348c97df44 | 4161 | } |
lypinator | 0:bb348c97df44 | 4162 | |
lypinator | 0:bb348c97df44 | 4163 | /** |
lypinator | 0:bb348c97df44 | 4164 | * @} |
lypinator | 0:bb348c97df44 | 4165 | */ |
lypinator | 0:bb348c97df44 | 4166 | |
lypinator | 0:bb348c97df44 | 4167 | /** @defgroup CRYP_Exported_Functions_Group2 AES processing functions |
lypinator | 0:bb348c97df44 | 4168 | * @brief Processing functions. |
lypinator | 0:bb348c97df44 | 4169 | * |
lypinator | 0:bb348c97df44 | 4170 | @verbatim |
lypinator | 0:bb348c97df44 | 4171 | ============================================================================== |
lypinator | 0:bb348c97df44 | 4172 | ##### AES processing functions ##### |
lypinator | 0:bb348c97df44 | 4173 | ============================================================================== |
lypinator | 0:bb348c97df44 | 4174 | [..] This section provides functions allowing to: |
lypinator | 0:bb348c97df44 | 4175 | (+) Encrypt plaintext using AES algorithm in different chaining modes |
lypinator | 0:bb348c97df44 | 4176 | (+) Decrypt cyphertext using AES algorithm in different chaining modes |
lypinator | 0:bb348c97df44 | 4177 | [..] Three processing functions are available: |
lypinator | 0:bb348c97df44 | 4178 | (+) Polling mode |
lypinator | 0:bb348c97df44 | 4179 | (+) Interrupt mode |
lypinator | 0:bb348c97df44 | 4180 | (+) DMA mode |
lypinator | 0:bb348c97df44 | 4181 | |
lypinator | 0:bb348c97df44 | 4182 | @endverbatim |
lypinator | 0:bb348c97df44 | 4183 | * @{ |
lypinator | 0:bb348c97df44 | 4184 | */ |
lypinator | 0:bb348c97df44 | 4185 | |
lypinator | 0:bb348c97df44 | 4186 | |
lypinator | 0:bb348c97df44 | 4187 | /** |
lypinator | 0:bb348c97df44 | 4188 | * @brief Encrypt pPlainData in AES ECB encryption mode. The cypher data are available in pCypherData. |
lypinator | 0:bb348c97df44 | 4189 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4190 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 4191 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 4192 | * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 4193 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 4194 | * @param Timeout Specify Timeout value |
lypinator | 0:bb348c97df44 | 4195 | * @note This API is provided only to maintain compatibility with legacy software. Users should directly |
lypinator | 0:bb348c97df44 | 4196 | * resort to generic HAL_CRYPEx_AES() API instead (usage recommended). |
lypinator | 0:bb348c97df44 | 4197 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 4198 | */ |
lypinator | 0:bb348c97df44 | 4199 | HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 4200 | { |
lypinator | 0:bb348c97df44 | 4201 | /* Re-initialize AES IP with proper parameters */ |
lypinator | 0:bb348c97df44 | 4202 | if (HAL_CRYP_DeInit(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4203 | { |
lypinator | 0:bb348c97df44 | 4204 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4205 | } |
lypinator | 0:bb348c97df44 | 4206 | hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT; |
lypinator | 0:bb348c97df44 | 4207 | hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB; |
lypinator | 0:bb348c97df44 | 4208 | hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; |
lypinator | 0:bb348c97df44 | 4209 | if (HAL_CRYP_Init(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4210 | { |
lypinator | 0:bb348c97df44 | 4211 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4212 | } |
lypinator | 0:bb348c97df44 | 4213 | |
lypinator | 0:bb348c97df44 | 4214 | return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout); |
lypinator | 0:bb348c97df44 | 4215 | } |
lypinator | 0:bb348c97df44 | 4216 | |
lypinator | 0:bb348c97df44 | 4217 | |
lypinator | 0:bb348c97df44 | 4218 | /** |
lypinator | 0:bb348c97df44 | 4219 | * @brief Encrypt pPlainData in AES CBC encryption mode with key derivation. The cypher data are available in pCypherData. |
lypinator | 0:bb348c97df44 | 4220 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4221 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 4222 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 4223 | * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 4224 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 4225 | * @param Timeout Specify Timeout value |
lypinator | 0:bb348c97df44 | 4226 | * @note This API is provided only to maintain compatibility with legacy software. Users should directly |
lypinator | 0:bb348c97df44 | 4227 | * resort to generic HAL_CRYPEx_AES() API instead (usage recommended). |
lypinator | 0:bb348c97df44 | 4228 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 4229 | */ |
lypinator | 0:bb348c97df44 | 4230 | HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 4231 | { |
lypinator | 0:bb348c97df44 | 4232 | /* Re-initialize AES IP with proper parameters */ |
lypinator | 0:bb348c97df44 | 4233 | if (HAL_CRYP_DeInit(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4234 | { |
lypinator | 0:bb348c97df44 | 4235 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4236 | } |
lypinator | 0:bb348c97df44 | 4237 | hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT; |
lypinator | 0:bb348c97df44 | 4238 | hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC; |
lypinator | 0:bb348c97df44 | 4239 | hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; |
lypinator | 0:bb348c97df44 | 4240 | if (HAL_CRYP_Init(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4241 | { |
lypinator | 0:bb348c97df44 | 4242 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4243 | } |
lypinator | 0:bb348c97df44 | 4244 | |
lypinator | 0:bb348c97df44 | 4245 | return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout); |
lypinator | 0:bb348c97df44 | 4246 | } |
lypinator | 0:bb348c97df44 | 4247 | |
lypinator | 0:bb348c97df44 | 4248 | |
lypinator | 0:bb348c97df44 | 4249 | /** |
lypinator | 0:bb348c97df44 | 4250 | * @brief Encrypt pPlainData in AES CTR encryption mode. The cypher data are available in pCypherData |
lypinator | 0:bb348c97df44 | 4251 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4252 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 4253 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 4254 | * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 4255 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 4256 | * @param Timeout Specify Timeout value |
lypinator | 0:bb348c97df44 | 4257 | * @note This API is provided only to maintain compatibility with legacy software. Users should directly |
lypinator | 0:bb348c97df44 | 4258 | * resort to generic HAL_CRYPEx_AES() API instead (usage recommended). |
lypinator | 0:bb348c97df44 | 4259 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 4260 | */ |
lypinator | 0:bb348c97df44 | 4261 | HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 4262 | { |
lypinator | 0:bb348c97df44 | 4263 | /* Re-initialize AES IP with proper parameters */ |
lypinator | 0:bb348c97df44 | 4264 | if (HAL_CRYP_DeInit(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4265 | { |
lypinator | 0:bb348c97df44 | 4266 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4267 | } |
lypinator | 0:bb348c97df44 | 4268 | hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT; |
lypinator | 0:bb348c97df44 | 4269 | hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR; |
lypinator | 0:bb348c97df44 | 4270 | hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; |
lypinator | 0:bb348c97df44 | 4271 | if (HAL_CRYP_Init(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4272 | { |
lypinator | 0:bb348c97df44 | 4273 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4274 | } |
lypinator | 0:bb348c97df44 | 4275 | |
lypinator | 0:bb348c97df44 | 4276 | return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout); |
lypinator | 0:bb348c97df44 | 4277 | } |
lypinator | 0:bb348c97df44 | 4278 | |
lypinator | 0:bb348c97df44 | 4279 | /** |
lypinator | 0:bb348c97df44 | 4280 | * @brief Decrypt pCypherData in AES ECB decryption mode with key derivation, |
lypinator | 0:bb348c97df44 | 4281 | * the decyphered data are available in pPlainData. |
lypinator | 0:bb348c97df44 | 4282 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4283 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 4284 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 4285 | * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 4286 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 4287 | * @param Timeout Specify Timeout value |
lypinator | 0:bb348c97df44 | 4288 | * @note This API is provided only to maintain compatibility with legacy software. Users should directly |
lypinator | 0:bb348c97df44 | 4289 | * resort to generic HAL_CRYPEx_AES() API instead (usage recommended). |
lypinator | 0:bb348c97df44 | 4290 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 4291 | */ |
lypinator | 0:bb348c97df44 | 4292 | HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 4293 | { |
lypinator | 0:bb348c97df44 | 4294 | /* Re-initialize AES IP with proper parameters */ |
lypinator | 0:bb348c97df44 | 4295 | if (HAL_CRYP_DeInit(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4296 | { |
lypinator | 0:bb348c97df44 | 4297 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4298 | } |
lypinator | 0:bb348c97df44 | 4299 | hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT; |
lypinator | 0:bb348c97df44 | 4300 | hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB; |
lypinator | 0:bb348c97df44 | 4301 | hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; |
lypinator | 0:bb348c97df44 | 4302 | if (HAL_CRYP_Init(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4303 | { |
lypinator | 0:bb348c97df44 | 4304 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4305 | } |
lypinator | 0:bb348c97df44 | 4306 | |
lypinator | 0:bb348c97df44 | 4307 | return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout); |
lypinator | 0:bb348c97df44 | 4308 | } |
lypinator | 0:bb348c97df44 | 4309 | |
lypinator | 0:bb348c97df44 | 4310 | /** |
lypinator | 0:bb348c97df44 | 4311 | * @brief Decrypt pCypherData in AES ECB decryption mode with key derivation, |
lypinator | 0:bb348c97df44 | 4312 | * the decyphered data are available in pPlainData. |
lypinator | 0:bb348c97df44 | 4313 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4314 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 4315 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 4316 | * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 4317 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 4318 | * @param Timeout Specify Timeout value |
lypinator | 0:bb348c97df44 | 4319 | * @note This API is provided only to maintain compatibility with legacy software. Users should directly |
lypinator | 0:bb348c97df44 | 4320 | * resort to generic HAL_CRYPEx_AES() API instead (usage recommended). |
lypinator | 0:bb348c97df44 | 4321 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 4322 | */ |
lypinator | 0:bb348c97df44 | 4323 | HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 4324 | { |
lypinator | 0:bb348c97df44 | 4325 | /* Re-initialize AES IP with proper parameters */ |
lypinator | 0:bb348c97df44 | 4326 | if (HAL_CRYP_DeInit(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4327 | { |
lypinator | 0:bb348c97df44 | 4328 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4329 | } |
lypinator | 0:bb348c97df44 | 4330 | hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT; |
lypinator | 0:bb348c97df44 | 4331 | hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC; |
lypinator | 0:bb348c97df44 | 4332 | hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; |
lypinator | 0:bb348c97df44 | 4333 | if (HAL_CRYP_Init(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4334 | { |
lypinator | 0:bb348c97df44 | 4335 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4336 | } |
lypinator | 0:bb348c97df44 | 4337 | |
lypinator | 0:bb348c97df44 | 4338 | return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout); |
lypinator | 0:bb348c97df44 | 4339 | } |
lypinator | 0:bb348c97df44 | 4340 | |
lypinator | 0:bb348c97df44 | 4341 | /** |
lypinator | 0:bb348c97df44 | 4342 | * @brief Decrypt pCypherData in AES CTR decryption mode, |
lypinator | 0:bb348c97df44 | 4343 | * the decyphered data are available in pPlainData. |
lypinator | 0:bb348c97df44 | 4344 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4345 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 4346 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 4347 | * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 4348 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 4349 | * @param Timeout Specify Timeout value |
lypinator | 0:bb348c97df44 | 4350 | * @note This API is provided only to maintain compatibility with legacy software. Users should directly |
lypinator | 0:bb348c97df44 | 4351 | * resort to generic HAL_CRYPEx_AES() API instead (usage recommended). |
lypinator | 0:bb348c97df44 | 4352 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 4353 | */ |
lypinator | 0:bb348c97df44 | 4354 | HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 4355 | { |
lypinator | 0:bb348c97df44 | 4356 | /* Re-initialize AES IP with proper parameters */ |
lypinator | 0:bb348c97df44 | 4357 | if (HAL_CRYP_DeInit(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4358 | { |
lypinator | 0:bb348c97df44 | 4359 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4360 | } |
lypinator | 0:bb348c97df44 | 4361 | hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT; |
lypinator | 0:bb348c97df44 | 4362 | hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR; |
lypinator | 0:bb348c97df44 | 4363 | hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; |
lypinator | 0:bb348c97df44 | 4364 | if (HAL_CRYP_Init(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4365 | { |
lypinator | 0:bb348c97df44 | 4366 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4367 | } |
lypinator | 0:bb348c97df44 | 4368 | |
lypinator | 0:bb348c97df44 | 4369 | return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout); |
lypinator | 0:bb348c97df44 | 4370 | } |
lypinator | 0:bb348c97df44 | 4371 | |
lypinator | 0:bb348c97df44 | 4372 | /** |
lypinator | 0:bb348c97df44 | 4373 | * @brief Encrypt pPlainData in AES ECB encryption mode using Interrupt, |
lypinator | 0:bb348c97df44 | 4374 | * the cypher data are available in pCypherData. |
lypinator | 0:bb348c97df44 | 4375 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4376 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 4377 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 4378 | * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 4379 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 4380 | * @note This API is provided only to maintain compatibility with legacy software. Users should directly |
lypinator | 0:bb348c97df44 | 4381 | * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended). |
lypinator | 0:bb348c97df44 | 4382 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 4383 | */ |
lypinator | 0:bb348c97df44 | 4384 | HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) |
lypinator | 0:bb348c97df44 | 4385 | { |
lypinator | 0:bb348c97df44 | 4386 | /* Re-initialize AES IP with proper parameters */ |
lypinator | 0:bb348c97df44 | 4387 | if (HAL_CRYP_DeInit(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4388 | { |
lypinator | 0:bb348c97df44 | 4389 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4390 | } |
lypinator | 0:bb348c97df44 | 4391 | hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT; |
lypinator | 0:bb348c97df44 | 4392 | hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB; |
lypinator | 0:bb348c97df44 | 4393 | hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; |
lypinator | 0:bb348c97df44 | 4394 | if (HAL_CRYP_Init(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4395 | { |
lypinator | 0:bb348c97df44 | 4396 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4397 | } |
lypinator | 0:bb348c97df44 | 4398 | |
lypinator | 0:bb348c97df44 | 4399 | return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData); |
lypinator | 0:bb348c97df44 | 4400 | } |
lypinator | 0:bb348c97df44 | 4401 | |
lypinator | 0:bb348c97df44 | 4402 | /** |
lypinator | 0:bb348c97df44 | 4403 | * @brief Encrypt pPlainData in AES CBC encryption mode using Interrupt, |
lypinator | 0:bb348c97df44 | 4404 | * the cypher data are available in pCypherData. |
lypinator | 0:bb348c97df44 | 4405 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4406 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 4407 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 4408 | * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 4409 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 4410 | * @note This API is provided only to maintain compatibility with legacy software. Users should directly |
lypinator | 0:bb348c97df44 | 4411 | * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended). |
lypinator | 0:bb348c97df44 | 4412 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 4413 | */ |
lypinator | 0:bb348c97df44 | 4414 | HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) |
lypinator | 0:bb348c97df44 | 4415 | { |
lypinator | 0:bb348c97df44 | 4416 | /* Re-initialize AES IP with proper parameters */ |
lypinator | 0:bb348c97df44 | 4417 | if (HAL_CRYP_DeInit(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4418 | { |
lypinator | 0:bb348c97df44 | 4419 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4420 | } |
lypinator | 0:bb348c97df44 | 4421 | hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT; |
lypinator | 0:bb348c97df44 | 4422 | hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC; |
lypinator | 0:bb348c97df44 | 4423 | hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; |
lypinator | 0:bb348c97df44 | 4424 | if (HAL_CRYP_Init(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4425 | { |
lypinator | 0:bb348c97df44 | 4426 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4427 | } |
lypinator | 0:bb348c97df44 | 4428 | |
lypinator | 0:bb348c97df44 | 4429 | return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData); |
lypinator | 0:bb348c97df44 | 4430 | } |
lypinator | 0:bb348c97df44 | 4431 | |
lypinator | 0:bb348c97df44 | 4432 | |
lypinator | 0:bb348c97df44 | 4433 | /** |
lypinator | 0:bb348c97df44 | 4434 | * @brief Encrypt pPlainData in AES CTR encryption mode using Interrupt, |
lypinator | 0:bb348c97df44 | 4435 | * the cypher data are available in pCypherData. |
lypinator | 0:bb348c97df44 | 4436 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4437 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 4438 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 4439 | * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 4440 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 4441 | * @note This API is provided only to maintain compatibility with legacy software. Users should directly |
lypinator | 0:bb348c97df44 | 4442 | * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended). |
lypinator | 0:bb348c97df44 | 4443 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 4444 | */ |
lypinator | 0:bb348c97df44 | 4445 | HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) |
lypinator | 0:bb348c97df44 | 4446 | { |
lypinator | 0:bb348c97df44 | 4447 | /* Re-initialize AES IP with proper parameters */ |
lypinator | 0:bb348c97df44 | 4448 | if (HAL_CRYP_DeInit(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4449 | { |
lypinator | 0:bb348c97df44 | 4450 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4451 | } |
lypinator | 0:bb348c97df44 | 4452 | hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT; |
lypinator | 0:bb348c97df44 | 4453 | hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR; |
lypinator | 0:bb348c97df44 | 4454 | hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; |
lypinator | 0:bb348c97df44 | 4455 | if (HAL_CRYP_Init(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4456 | { |
lypinator | 0:bb348c97df44 | 4457 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4458 | } |
lypinator | 0:bb348c97df44 | 4459 | |
lypinator | 0:bb348c97df44 | 4460 | return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData); |
lypinator | 0:bb348c97df44 | 4461 | } |
lypinator | 0:bb348c97df44 | 4462 | |
lypinator | 0:bb348c97df44 | 4463 | /** |
lypinator | 0:bb348c97df44 | 4464 | * @brief Decrypt pCypherData in AES ECB decryption mode using Interrupt, |
lypinator | 0:bb348c97df44 | 4465 | * the decyphered data are available in pPlainData. |
lypinator | 0:bb348c97df44 | 4466 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4467 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 4468 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 4469 | * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 4470 | * @param pPlainData Pointer to the plaintext buffer. |
lypinator | 0:bb348c97df44 | 4471 | * @note This API is provided only to maintain compatibility with legacy software. Users should directly |
lypinator | 0:bb348c97df44 | 4472 | * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended). |
lypinator | 0:bb348c97df44 | 4473 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 4474 | */ |
lypinator | 0:bb348c97df44 | 4475 | HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) |
lypinator | 0:bb348c97df44 | 4476 | { |
lypinator | 0:bb348c97df44 | 4477 | /* Re-initialize AES IP with proper parameters */ |
lypinator | 0:bb348c97df44 | 4478 | if (HAL_CRYP_DeInit(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4479 | { |
lypinator | 0:bb348c97df44 | 4480 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4481 | } |
lypinator | 0:bb348c97df44 | 4482 | hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT; |
lypinator | 0:bb348c97df44 | 4483 | hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB; |
lypinator | 0:bb348c97df44 | 4484 | hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; |
lypinator | 0:bb348c97df44 | 4485 | if (HAL_CRYP_Init(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4486 | { |
lypinator | 0:bb348c97df44 | 4487 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4488 | } |
lypinator | 0:bb348c97df44 | 4489 | |
lypinator | 0:bb348c97df44 | 4490 | return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData); |
lypinator | 0:bb348c97df44 | 4491 | } |
lypinator | 0:bb348c97df44 | 4492 | |
lypinator | 0:bb348c97df44 | 4493 | /** |
lypinator | 0:bb348c97df44 | 4494 | * @brief Decrypt pCypherData in AES CBC decryption mode using Interrupt, |
lypinator | 0:bb348c97df44 | 4495 | * the decyphered data are available in pPlainData. |
lypinator | 0:bb348c97df44 | 4496 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4497 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 4498 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 4499 | * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 4500 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 4501 | * @note This API is provided only to maintain compatibility with legacy software. Users should directly |
lypinator | 0:bb348c97df44 | 4502 | * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended). |
lypinator | 0:bb348c97df44 | 4503 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 4504 | */ |
lypinator | 0:bb348c97df44 | 4505 | HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) |
lypinator | 0:bb348c97df44 | 4506 | { |
lypinator | 0:bb348c97df44 | 4507 | /* Re-initialize AES IP with proper parameters */ |
lypinator | 0:bb348c97df44 | 4508 | if (HAL_CRYP_DeInit(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4509 | { |
lypinator | 0:bb348c97df44 | 4510 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4511 | } |
lypinator | 0:bb348c97df44 | 4512 | hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT; |
lypinator | 0:bb348c97df44 | 4513 | hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC; |
lypinator | 0:bb348c97df44 | 4514 | hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; |
lypinator | 0:bb348c97df44 | 4515 | if (HAL_CRYP_Init(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4516 | { |
lypinator | 0:bb348c97df44 | 4517 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4518 | } |
lypinator | 0:bb348c97df44 | 4519 | |
lypinator | 0:bb348c97df44 | 4520 | return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData); |
lypinator | 0:bb348c97df44 | 4521 | } |
lypinator | 0:bb348c97df44 | 4522 | |
lypinator | 0:bb348c97df44 | 4523 | /** |
lypinator | 0:bb348c97df44 | 4524 | * @brief Decrypt pCypherData in AES CTR decryption mode using Interrupt, |
lypinator | 0:bb348c97df44 | 4525 | * the decyphered data are available in pPlainData. |
lypinator | 0:bb348c97df44 | 4526 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4527 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 4528 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 4529 | * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 4530 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 4531 | * @note This API is provided only to maintain compatibility with legacy software. Users should directly |
lypinator | 0:bb348c97df44 | 4532 | * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended). |
lypinator | 0:bb348c97df44 | 4533 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 4534 | */ |
lypinator | 0:bb348c97df44 | 4535 | HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) |
lypinator | 0:bb348c97df44 | 4536 | { |
lypinator | 0:bb348c97df44 | 4537 | /* Re-initialize AES IP with proper parameters */ |
lypinator | 0:bb348c97df44 | 4538 | if (HAL_CRYP_DeInit(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4539 | { |
lypinator | 0:bb348c97df44 | 4540 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4541 | } |
lypinator | 0:bb348c97df44 | 4542 | hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT; |
lypinator | 0:bb348c97df44 | 4543 | hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR; |
lypinator | 0:bb348c97df44 | 4544 | hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; |
lypinator | 0:bb348c97df44 | 4545 | if (HAL_CRYP_Init(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4546 | { |
lypinator | 0:bb348c97df44 | 4547 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4548 | } |
lypinator | 0:bb348c97df44 | 4549 | |
lypinator | 0:bb348c97df44 | 4550 | return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData); |
lypinator | 0:bb348c97df44 | 4551 | } |
lypinator | 0:bb348c97df44 | 4552 | |
lypinator | 0:bb348c97df44 | 4553 | /** |
lypinator | 0:bb348c97df44 | 4554 | * @brief Encrypt pPlainData in AES ECB encryption mode using DMA, |
lypinator | 0:bb348c97df44 | 4555 | * the cypher data are available in pCypherData. |
lypinator | 0:bb348c97df44 | 4556 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4557 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 4558 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 4559 | * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 4560 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 4561 | * @note This API is provided only to maintain compatibility with legacy software. Users should directly |
lypinator | 0:bb348c97df44 | 4562 | * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended). |
lypinator | 0:bb348c97df44 | 4563 | * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP. |
lypinator | 0:bb348c97df44 | 4564 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 4565 | */ |
lypinator | 0:bb348c97df44 | 4566 | HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) |
lypinator | 0:bb348c97df44 | 4567 | { |
lypinator | 0:bb348c97df44 | 4568 | /* Re-initialize AES IP with proper parameters */ |
lypinator | 0:bb348c97df44 | 4569 | if (HAL_CRYP_DeInit(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4570 | { |
lypinator | 0:bb348c97df44 | 4571 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4572 | } |
lypinator | 0:bb348c97df44 | 4573 | hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT; |
lypinator | 0:bb348c97df44 | 4574 | hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB; |
lypinator | 0:bb348c97df44 | 4575 | hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; |
lypinator | 0:bb348c97df44 | 4576 | if (HAL_CRYP_Init(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4577 | { |
lypinator | 0:bb348c97df44 | 4578 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4579 | } |
lypinator | 0:bb348c97df44 | 4580 | |
lypinator | 0:bb348c97df44 | 4581 | return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData); |
lypinator | 0:bb348c97df44 | 4582 | } |
lypinator | 0:bb348c97df44 | 4583 | |
lypinator | 0:bb348c97df44 | 4584 | |
lypinator | 0:bb348c97df44 | 4585 | |
lypinator | 0:bb348c97df44 | 4586 | /** |
lypinator | 0:bb348c97df44 | 4587 | * @brief Encrypt pPlainData in AES CBC encryption mode using DMA, |
lypinator | 0:bb348c97df44 | 4588 | * the cypher data are available in pCypherData. |
lypinator | 0:bb348c97df44 | 4589 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4590 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 4591 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 4592 | * @param Size Length of the plaintext buffer, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 4593 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 4594 | * @note This API is provided only to maintain compatibility with legacy software. Users should directly |
lypinator | 0:bb348c97df44 | 4595 | * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended). |
lypinator | 0:bb348c97df44 | 4596 | * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP. |
lypinator | 0:bb348c97df44 | 4597 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 4598 | */ |
lypinator | 0:bb348c97df44 | 4599 | HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) |
lypinator | 0:bb348c97df44 | 4600 | { |
lypinator | 0:bb348c97df44 | 4601 | /* Re-initialize AES IP with proper parameters */ |
lypinator | 0:bb348c97df44 | 4602 | if (HAL_CRYP_DeInit(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4603 | { |
lypinator | 0:bb348c97df44 | 4604 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4605 | } |
lypinator | 0:bb348c97df44 | 4606 | hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT; |
lypinator | 0:bb348c97df44 | 4607 | hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC; |
lypinator | 0:bb348c97df44 | 4608 | hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; |
lypinator | 0:bb348c97df44 | 4609 | if (HAL_CRYP_Init(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4610 | { |
lypinator | 0:bb348c97df44 | 4611 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4612 | } |
lypinator | 0:bb348c97df44 | 4613 | |
lypinator | 0:bb348c97df44 | 4614 | return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData); |
lypinator | 0:bb348c97df44 | 4615 | } |
lypinator | 0:bb348c97df44 | 4616 | |
lypinator | 0:bb348c97df44 | 4617 | /** |
lypinator | 0:bb348c97df44 | 4618 | * @brief Encrypt pPlainData in AES CTR encryption mode using DMA, |
lypinator | 0:bb348c97df44 | 4619 | * the cypher data are available in pCypherData. |
lypinator | 0:bb348c97df44 | 4620 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4621 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 4622 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 4623 | * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 4624 | * @param pCypherData Pointer to the cyphertext buffer. |
lypinator | 0:bb348c97df44 | 4625 | * @note This API is provided only to maintain compatibility with legacy software. Users should directly |
lypinator | 0:bb348c97df44 | 4626 | * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended). |
lypinator | 0:bb348c97df44 | 4627 | * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP. |
lypinator | 0:bb348c97df44 | 4628 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 4629 | */ |
lypinator | 0:bb348c97df44 | 4630 | HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) |
lypinator | 0:bb348c97df44 | 4631 | { |
lypinator | 0:bb348c97df44 | 4632 | /* Re-initialize AES IP with proper parameters */ |
lypinator | 0:bb348c97df44 | 4633 | if (HAL_CRYP_DeInit(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4634 | { |
lypinator | 0:bb348c97df44 | 4635 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4636 | } |
lypinator | 0:bb348c97df44 | 4637 | hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT; |
lypinator | 0:bb348c97df44 | 4638 | hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR; |
lypinator | 0:bb348c97df44 | 4639 | hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; |
lypinator | 0:bb348c97df44 | 4640 | if (HAL_CRYP_Init(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4641 | { |
lypinator | 0:bb348c97df44 | 4642 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4643 | } |
lypinator | 0:bb348c97df44 | 4644 | |
lypinator | 0:bb348c97df44 | 4645 | return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData); |
lypinator | 0:bb348c97df44 | 4646 | } |
lypinator | 0:bb348c97df44 | 4647 | |
lypinator | 0:bb348c97df44 | 4648 | /** |
lypinator | 0:bb348c97df44 | 4649 | * @brief Decrypt pCypherData in AES ECB decryption mode using DMA, |
lypinator | 0:bb348c97df44 | 4650 | * the decyphered data are available in pPlainData. |
lypinator | 0:bb348c97df44 | 4651 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4652 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 4653 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 4654 | * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 4655 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 4656 | * @note This API is provided only to maintain compatibility with legacy software. Users should directly |
lypinator | 0:bb348c97df44 | 4657 | * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended). |
lypinator | 0:bb348c97df44 | 4658 | * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP. |
lypinator | 0:bb348c97df44 | 4659 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 4660 | */ |
lypinator | 0:bb348c97df44 | 4661 | HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) |
lypinator | 0:bb348c97df44 | 4662 | { |
lypinator | 0:bb348c97df44 | 4663 | /* Re-initialize AES IP with proper parameters */ |
lypinator | 0:bb348c97df44 | 4664 | if (HAL_CRYP_DeInit(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4665 | { |
lypinator | 0:bb348c97df44 | 4666 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4667 | } |
lypinator | 0:bb348c97df44 | 4668 | hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT; |
lypinator | 0:bb348c97df44 | 4669 | hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB; |
lypinator | 0:bb348c97df44 | 4670 | hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; |
lypinator | 0:bb348c97df44 | 4671 | if (HAL_CRYP_Init(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4672 | { |
lypinator | 0:bb348c97df44 | 4673 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4674 | } |
lypinator | 0:bb348c97df44 | 4675 | |
lypinator | 0:bb348c97df44 | 4676 | return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData); |
lypinator | 0:bb348c97df44 | 4677 | } |
lypinator | 0:bb348c97df44 | 4678 | |
lypinator | 0:bb348c97df44 | 4679 | /** |
lypinator | 0:bb348c97df44 | 4680 | * @brief Decrypt pCypherData in AES CBC decryption mode using DMA, |
lypinator | 0:bb348c97df44 | 4681 | * the decyphered data are available in pPlainData. |
lypinator | 0:bb348c97df44 | 4682 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4683 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 4684 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 4685 | * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 4686 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 4687 | * @note This API is provided only to maintain compatibility with legacy software. Users should directly |
lypinator | 0:bb348c97df44 | 4688 | * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended). |
lypinator | 0:bb348c97df44 | 4689 | * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP. |
lypinator | 0:bb348c97df44 | 4690 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 4691 | */ |
lypinator | 0:bb348c97df44 | 4692 | HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) |
lypinator | 0:bb348c97df44 | 4693 | { |
lypinator | 0:bb348c97df44 | 4694 | /* Re-initialize AES IP with proper parameters */ |
lypinator | 0:bb348c97df44 | 4695 | if (HAL_CRYP_DeInit(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4696 | { |
lypinator | 0:bb348c97df44 | 4697 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4698 | } |
lypinator | 0:bb348c97df44 | 4699 | hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT; |
lypinator | 0:bb348c97df44 | 4700 | hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC; |
lypinator | 0:bb348c97df44 | 4701 | hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; |
lypinator | 0:bb348c97df44 | 4702 | if (HAL_CRYP_Init(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4703 | { |
lypinator | 0:bb348c97df44 | 4704 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4705 | } |
lypinator | 0:bb348c97df44 | 4706 | |
lypinator | 0:bb348c97df44 | 4707 | return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData); |
lypinator | 0:bb348c97df44 | 4708 | } |
lypinator | 0:bb348c97df44 | 4709 | |
lypinator | 0:bb348c97df44 | 4710 | /** |
lypinator | 0:bb348c97df44 | 4711 | * @brief Decrypt pCypherData in AES CTR decryption mode using DMA, |
lypinator | 0:bb348c97df44 | 4712 | * the decyphered data are available in pPlainData. |
lypinator | 0:bb348c97df44 | 4713 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4714 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 4715 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 4716 | * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 4717 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 4718 | * @note This API is provided only to maintain compatibility with legacy software. Users should directly |
lypinator | 0:bb348c97df44 | 4719 | * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended). |
lypinator | 0:bb348c97df44 | 4720 | * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP. |
lypinator | 0:bb348c97df44 | 4721 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 4722 | */ |
lypinator | 0:bb348c97df44 | 4723 | HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) |
lypinator | 0:bb348c97df44 | 4724 | { |
lypinator | 0:bb348c97df44 | 4725 | /* Re-initialize AES IP with proper parameters */ |
lypinator | 0:bb348c97df44 | 4726 | if (HAL_CRYP_DeInit(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4727 | { |
lypinator | 0:bb348c97df44 | 4728 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4729 | } |
lypinator | 0:bb348c97df44 | 4730 | hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT; |
lypinator | 0:bb348c97df44 | 4731 | hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR; |
lypinator | 0:bb348c97df44 | 4732 | hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; |
lypinator | 0:bb348c97df44 | 4733 | if (HAL_CRYP_Init(hcryp) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4734 | { |
lypinator | 0:bb348c97df44 | 4735 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4736 | } |
lypinator | 0:bb348c97df44 | 4737 | |
lypinator | 0:bb348c97df44 | 4738 | return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData); |
lypinator | 0:bb348c97df44 | 4739 | } |
lypinator | 0:bb348c97df44 | 4740 | |
lypinator | 0:bb348c97df44 | 4741 | |
lypinator | 0:bb348c97df44 | 4742 | /** |
lypinator | 0:bb348c97df44 | 4743 | * @} |
lypinator | 0:bb348c97df44 | 4744 | */ |
lypinator | 0:bb348c97df44 | 4745 | |
lypinator | 0:bb348c97df44 | 4746 | /** @defgroup CRYP_Exported_Functions_Group3 Callback functions |
lypinator | 0:bb348c97df44 | 4747 | * @brief Callback functions. |
lypinator | 0:bb348c97df44 | 4748 | * |
lypinator | 0:bb348c97df44 | 4749 | @verbatim |
lypinator | 0:bb348c97df44 | 4750 | ============================================================================== |
lypinator | 0:bb348c97df44 | 4751 | ##### Callback functions ##### |
lypinator | 0:bb348c97df44 | 4752 | ============================================================================== |
lypinator | 0:bb348c97df44 | 4753 | [..] This section provides Interruption and DMA callback functions: |
lypinator | 0:bb348c97df44 | 4754 | (+) DMA Input data transfer complete |
lypinator | 0:bb348c97df44 | 4755 | (+) DMA Output data transfer complete |
lypinator | 0:bb348c97df44 | 4756 | (+) DMA or Interrupt error |
lypinator | 0:bb348c97df44 | 4757 | |
lypinator | 0:bb348c97df44 | 4758 | @endverbatim |
lypinator | 0:bb348c97df44 | 4759 | * @{ |
lypinator | 0:bb348c97df44 | 4760 | */ |
lypinator | 0:bb348c97df44 | 4761 | |
lypinator | 0:bb348c97df44 | 4762 | /** |
lypinator | 0:bb348c97df44 | 4763 | * @brief CRYP error callback. |
lypinator | 0:bb348c97df44 | 4764 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4765 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 4766 | * @retval None |
lypinator | 0:bb348c97df44 | 4767 | */ |
lypinator | 0:bb348c97df44 | 4768 | __weak void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp) |
lypinator | 0:bb348c97df44 | 4769 | { |
lypinator | 0:bb348c97df44 | 4770 | /* Prevent unused argument(s) compilation warning */ |
lypinator | 0:bb348c97df44 | 4771 | UNUSED(hcryp); |
lypinator | 0:bb348c97df44 | 4772 | |
lypinator | 0:bb348c97df44 | 4773 | /* NOTE : This function should not be modified; when the callback is needed, |
lypinator | 0:bb348c97df44 | 4774 | the HAL_CRYP_ErrorCallback can be implemented in the user file |
lypinator | 0:bb348c97df44 | 4775 | */ |
lypinator | 0:bb348c97df44 | 4776 | } |
lypinator | 0:bb348c97df44 | 4777 | |
lypinator | 0:bb348c97df44 | 4778 | /** |
lypinator | 0:bb348c97df44 | 4779 | * @brief Input DMA transfer complete callback. |
lypinator | 0:bb348c97df44 | 4780 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4781 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 4782 | * @retval None |
lypinator | 0:bb348c97df44 | 4783 | */ |
lypinator | 0:bb348c97df44 | 4784 | __weak void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp) |
lypinator | 0:bb348c97df44 | 4785 | { |
lypinator | 0:bb348c97df44 | 4786 | /* Prevent unused argument(s) compilation warning */ |
lypinator | 0:bb348c97df44 | 4787 | UNUSED(hcryp); |
lypinator | 0:bb348c97df44 | 4788 | |
lypinator | 0:bb348c97df44 | 4789 | /* NOTE : This function should not be modified; when the callback is needed, |
lypinator | 0:bb348c97df44 | 4790 | the HAL_CRYP_InCpltCallback can be implemented in the user file |
lypinator | 0:bb348c97df44 | 4791 | */ |
lypinator | 0:bb348c97df44 | 4792 | } |
lypinator | 0:bb348c97df44 | 4793 | |
lypinator | 0:bb348c97df44 | 4794 | /** |
lypinator | 0:bb348c97df44 | 4795 | * @brief Output DMA transfer complete callback. |
lypinator | 0:bb348c97df44 | 4796 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4797 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 4798 | * @retval None |
lypinator | 0:bb348c97df44 | 4799 | */ |
lypinator | 0:bb348c97df44 | 4800 | __weak void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp) |
lypinator | 0:bb348c97df44 | 4801 | { |
lypinator | 0:bb348c97df44 | 4802 | /* Prevent unused argument(s) compilation warning */ |
lypinator | 0:bb348c97df44 | 4803 | UNUSED(hcryp); |
lypinator | 0:bb348c97df44 | 4804 | |
lypinator | 0:bb348c97df44 | 4805 | /* NOTE : This function should not be modified; when the callback is needed, |
lypinator | 0:bb348c97df44 | 4806 | the HAL_CRYP_OutCpltCallback can be implemented in the user file |
lypinator | 0:bb348c97df44 | 4807 | */ |
lypinator | 0:bb348c97df44 | 4808 | } |
lypinator | 0:bb348c97df44 | 4809 | |
lypinator | 0:bb348c97df44 | 4810 | /** |
lypinator | 0:bb348c97df44 | 4811 | * @} |
lypinator | 0:bb348c97df44 | 4812 | */ |
lypinator | 0:bb348c97df44 | 4813 | |
lypinator | 0:bb348c97df44 | 4814 | /** @defgroup CRYP_Exported_Functions_Group4 CRYP IRQ handler |
lypinator | 0:bb348c97df44 | 4815 | * @brief AES IRQ handler. |
lypinator | 0:bb348c97df44 | 4816 | * |
lypinator | 0:bb348c97df44 | 4817 | @verbatim |
lypinator | 0:bb348c97df44 | 4818 | ============================================================================== |
lypinator | 0:bb348c97df44 | 4819 | ##### AES IRQ handler management ##### |
lypinator | 0:bb348c97df44 | 4820 | ============================================================================== |
lypinator | 0:bb348c97df44 | 4821 | [..] This section provides AES IRQ handler function. |
lypinator | 0:bb348c97df44 | 4822 | |
lypinator | 0:bb348c97df44 | 4823 | @endverbatim |
lypinator | 0:bb348c97df44 | 4824 | * @{ |
lypinator | 0:bb348c97df44 | 4825 | */ |
lypinator | 0:bb348c97df44 | 4826 | |
lypinator | 0:bb348c97df44 | 4827 | /** |
lypinator | 0:bb348c97df44 | 4828 | * @brief Handle AES interrupt request. |
lypinator | 0:bb348c97df44 | 4829 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4830 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 4831 | * @retval None |
lypinator | 0:bb348c97df44 | 4832 | */ |
lypinator | 0:bb348c97df44 | 4833 | void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp) |
lypinator | 0:bb348c97df44 | 4834 | { |
lypinator | 0:bb348c97df44 | 4835 | /* Check if error occurred */ |
lypinator | 0:bb348c97df44 | 4836 | if (__HAL_CRYP_GET_IT_SOURCE(CRYP_IT_ERRIE) != RESET) |
lypinator | 0:bb348c97df44 | 4837 | { |
lypinator | 0:bb348c97df44 | 4838 | /* If Write Error occurred */ |
lypinator | 0:bb348c97df44 | 4839 | if (__HAL_CRYP_GET_FLAG(CRYP_IT_WRERR) != RESET) |
lypinator | 0:bb348c97df44 | 4840 | { |
lypinator | 0:bb348c97df44 | 4841 | hcryp->ErrorCode |= HAL_CRYP_WRITE_ERROR; |
lypinator | 0:bb348c97df44 | 4842 | hcryp->State = HAL_CRYP_STATE_ERROR; |
lypinator | 0:bb348c97df44 | 4843 | } |
lypinator | 0:bb348c97df44 | 4844 | /* If Read Error occurred */ |
lypinator | 0:bb348c97df44 | 4845 | if (__HAL_CRYP_GET_FLAG(CRYP_IT_RDERR) != RESET) |
lypinator | 0:bb348c97df44 | 4846 | { |
lypinator | 0:bb348c97df44 | 4847 | hcryp->ErrorCode |= HAL_CRYP_READ_ERROR; |
lypinator | 0:bb348c97df44 | 4848 | hcryp->State = HAL_CRYP_STATE_ERROR; |
lypinator | 0:bb348c97df44 | 4849 | } |
lypinator | 0:bb348c97df44 | 4850 | |
lypinator | 0:bb348c97df44 | 4851 | /* If an error has been reported */ |
lypinator | 0:bb348c97df44 | 4852 | if (hcryp->State == HAL_CRYP_STATE_ERROR) |
lypinator | 0:bb348c97df44 | 4853 | { |
lypinator | 0:bb348c97df44 | 4854 | /* Disable Error and Computation Complete Interrupts */ |
lypinator | 0:bb348c97df44 | 4855 | __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); |
lypinator | 0:bb348c97df44 | 4856 | /* Clear all Interrupt flags */ |
lypinator | 0:bb348c97df44 | 4857 | __HAL_CRYP_CLEAR_FLAG(CRYP_ERR_CLEAR|CRYP_CCF_CLEAR); |
lypinator | 0:bb348c97df44 | 4858 | |
lypinator | 0:bb348c97df44 | 4859 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 4860 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 4861 | |
lypinator | 0:bb348c97df44 | 4862 | HAL_CRYP_ErrorCallback(hcryp); |
lypinator | 0:bb348c97df44 | 4863 | |
lypinator | 0:bb348c97df44 | 4864 | return; |
lypinator | 0:bb348c97df44 | 4865 | } |
lypinator | 0:bb348c97df44 | 4866 | } |
lypinator | 0:bb348c97df44 | 4867 | |
lypinator | 0:bb348c97df44 | 4868 | /* Check if computation complete interrupt is enabled |
lypinator | 0:bb348c97df44 | 4869 | and if the computation complete flag is raised */ |
lypinator | 0:bb348c97df44 | 4870 | if((__HAL_CRYP_GET_FLAG(CRYP_IT_CCF) != RESET) && (__HAL_CRYP_GET_IT_SOURCE(CRYP_IT_CCFIE) != RESET)) |
lypinator | 0:bb348c97df44 | 4871 | { |
lypinator | 0:bb348c97df44 | 4872 | #if defined(AES_CR_NPBLB) |
lypinator | 0:bb348c97df44 | 4873 | if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) |
lypinator | 0:bb348c97df44 | 4874 | || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC)) |
lypinator | 0:bb348c97df44 | 4875 | #else |
lypinator | 0:bb348c97df44 | 4876 | if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) |
lypinator | 0:bb348c97df44 | 4877 | || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)) |
lypinator | 0:bb348c97df44 | 4878 | #endif |
lypinator | 0:bb348c97df44 | 4879 | { |
lypinator | 0:bb348c97df44 | 4880 | /* To ensure proper suspension requests management, CCF flag |
lypinator | 0:bb348c97df44 | 4881 | is reset in CRYP_AES_Auth_IT() according to the current |
lypinator | 0:bb348c97df44 | 4882 | phase under handling */ |
lypinator | 0:bb348c97df44 | 4883 | CRYP_AES_Auth_IT(hcryp); |
lypinator | 0:bb348c97df44 | 4884 | } |
lypinator | 0:bb348c97df44 | 4885 | else |
lypinator | 0:bb348c97df44 | 4886 | { |
lypinator | 0:bb348c97df44 | 4887 | /* Clear Computation Complete Flag */ |
lypinator | 0:bb348c97df44 | 4888 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
lypinator | 0:bb348c97df44 | 4889 | CRYP_AES_IT(hcryp); |
lypinator | 0:bb348c97df44 | 4890 | } |
lypinator | 0:bb348c97df44 | 4891 | } |
lypinator | 0:bb348c97df44 | 4892 | } |
lypinator | 0:bb348c97df44 | 4893 | |
lypinator | 0:bb348c97df44 | 4894 | /** |
lypinator | 0:bb348c97df44 | 4895 | * @} |
lypinator | 0:bb348c97df44 | 4896 | */ |
lypinator | 0:bb348c97df44 | 4897 | |
lypinator | 0:bb348c97df44 | 4898 | /** @defgroup CRYP_Exported_Functions_Group5 Peripheral State functions |
lypinator | 0:bb348c97df44 | 4899 | * @brief Peripheral State functions. |
lypinator | 0:bb348c97df44 | 4900 | * |
lypinator | 0:bb348c97df44 | 4901 | @verbatim |
lypinator | 0:bb348c97df44 | 4902 | ============================================================================== |
lypinator | 0:bb348c97df44 | 4903 | ##### Peripheral State functions ##### |
lypinator | 0:bb348c97df44 | 4904 | ============================================================================== |
lypinator | 0:bb348c97df44 | 4905 | [..] |
lypinator | 0:bb348c97df44 | 4906 | This subsection permits to get in run-time the status of the peripheral. |
lypinator | 0:bb348c97df44 | 4907 | |
lypinator | 0:bb348c97df44 | 4908 | @endverbatim |
lypinator | 0:bb348c97df44 | 4909 | * @{ |
lypinator | 0:bb348c97df44 | 4910 | */ |
lypinator | 0:bb348c97df44 | 4911 | |
lypinator | 0:bb348c97df44 | 4912 | /** |
lypinator | 0:bb348c97df44 | 4913 | * @brief Return the CRYP handle state. |
lypinator | 0:bb348c97df44 | 4914 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4915 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 4916 | * @retval HAL state |
lypinator | 0:bb348c97df44 | 4917 | */ |
lypinator | 0:bb348c97df44 | 4918 | HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp) |
lypinator | 0:bb348c97df44 | 4919 | { |
lypinator | 0:bb348c97df44 | 4920 | /* Return CRYP handle state */ |
lypinator | 0:bb348c97df44 | 4921 | return hcryp->State; |
lypinator | 0:bb348c97df44 | 4922 | } |
lypinator | 0:bb348c97df44 | 4923 | |
lypinator | 0:bb348c97df44 | 4924 | /** |
lypinator | 0:bb348c97df44 | 4925 | * @brief Return the CRYP peripheral error. |
lypinator | 0:bb348c97df44 | 4926 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4927 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 4928 | * @note The returned error is a bit-map combination of possible errors |
lypinator | 0:bb348c97df44 | 4929 | * @retval Error bit-map |
lypinator | 0:bb348c97df44 | 4930 | */ |
lypinator | 0:bb348c97df44 | 4931 | uint32_t HAL_CRYP_GetError(CRYP_HandleTypeDef *hcryp) |
lypinator | 0:bb348c97df44 | 4932 | { |
lypinator | 0:bb348c97df44 | 4933 | return hcryp->ErrorCode; |
lypinator | 0:bb348c97df44 | 4934 | } |
lypinator | 0:bb348c97df44 | 4935 | |
lypinator | 0:bb348c97df44 | 4936 | /** |
lypinator | 0:bb348c97df44 | 4937 | * @} |
lypinator | 0:bb348c97df44 | 4938 | */ |
lypinator | 0:bb348c97df44 | 4939 | |
lypinator | 0:bb348c97df44 | 4940 | /** |
lypinator | 0:bb348c97df44 | 4941 | * @} |
lypinator | 0:bb348c97df44 | 4942 | */ |
lypinator | 0:bb348c97df44 | 4943 | |
lypinator | 0:bb348c97df44 | 4944 | /** @addtogroup CRYP_Private_Functions |
lypinator | 0:bb348c97df44 | 4945 | * @{ |
lypinator | 0:bb348c97df44 | 4946 | */ |
lypinator | 0:bb348c97df44 | 4947 | |
lypinator | 0:bb348c97df44 | 4948 | |
lypinator | 0:bb348c97df44 | 4949 | /** |
lypinator | 0:bb348c97df44 | 4950 | * @brief Write the Key in KeyRx registers. |
lypinator | 0:bb348c97df44 | 4951 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4952 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 4953 | * @retval None |
lypinator | 0:bb348c97df44 | 4954 | */ |
lypinator | 0:bb348c97df44 | 4955 | static HAL_StatusTypeDef CRYP_SetKey(CRYP_HandleTypeDef *hcryp) |
lypinator | 0:bb348c97df44 | 4956 | { |
lypinator | 0:bb348c97df44 | 4957 | uint32_t keyaddr = 0x0U; |
lypinator | 0:bb348c97df44 | 4958 | |
lypinator | 0:bb348c97df44 | 4959 | if ((uint32_t)(hcryp->Init.pKey == NULL)) |
lypinator | 0:bb348c97df44 | 4960 | { |
lypinator | 0:bb348c97df44 | 4961 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4962 | } |
lypinator | 0:bb348c97df44 | 4963 | |
lypinator | 0:bb348c97df44 | 4964 | |
lypinator | 0:bb348c97df44 | 4965 | keyaddr = (uint32_t)(hcryp->Init.pKey); |
lypinator | 0:bb348c97df44 | 4966 | |
lypinator | 0:bb348c97df44 | 4967 | if (hcryp->Init.KeySize == CRYP_KEYSIZE_256B) |
lypinator | 0:bb348c97df44 | 4968 | { |
lypinator | 0:bb348c97df44 | 4969 | hcryp->Instance->KEYR7 = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 4970 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 4971 | hcryp->Instance->KEYR6 = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 4972 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 4973 | hcryp->Instance->KEYR5 = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 4974 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 4975 | hcryp->Instance->KEYR4 = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 4976 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 4977 | } |
lypinator | 0:bb348c97df44 | 4978 | |
lypinator | 0:bb348c97df44 | 4979 | hcryp->Instance->KEYR3 = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 4980 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 4981 | hcryp->Instance->KEYR2 = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 4982 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 4983 | hcryp->Instance->KEYR1 = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 4984 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 4985 | hcryp->Instance->KEYR0 = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 4986 | |
lypinator | 0:bb348c97df44 | 4987 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 4988 | } |
lypinator | 0:bb348c97df44 | 4989 | |
lypinator | 0:bb348c97df44 | 4990 | /** |
lypinator | 0:bb348c97df44 | 4991 | * @brief Write the InitVector/InitCounter in IVRx registers. |
lypinator | 0:bb348c97df44 | 4992 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4993 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 4994 | * @retval None |
lypinator | 0:bb348c97df44 | 4995 | */ |
lypinator | 0:bb348c97df44 | 4996 | static HAL_StatusTypeDef CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp) |
lypinator | 0:bb348c97df44 | 4997 | { |
lypinator | 0:bb348c97df44 | 4998 | uint32_t ivaddr = 0x0U; |
lypinator | 0:bb348c97df44 | 4999 | |
lypinator | 0:bb348c97df44 | 5000 | #if !defined(AES_CR_NPBLB) |
lypinator | 0:bb348c97df44 | 5001 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) |
lypinator | 0:bb348c97df44 | 5002 | { |
lypinator | 0:bb348c97df44 | 5003 | hcryp->Instance->IVR3 = 0U; |
lypinator | 0:bb348c97df44 | 5004 | hcryp->Instance->IVR2 = 0U; |
lypinator | 0:bb348c97df44 | 5005 | hcryp->Instance->IVR1 = 0U; |
lypinator | 0:bb348c97df44 | 5006 | hcryp->Instance->IVR0 = 0U; |
lypinator | 0:bb348c97df44 | 5007 | } |
lypinator | 0:bb348c97df44 | 5008 | else |
lypinator | 0:bb348c97df44 | 5009 | #endif |
lypinator | 0:bb348c97df44 | 5010 | { |
lypinator | 0:bb348c97df44 | 5011 | if (hcryp->Init.pInitVect == NULL) |
lypinator | 0:bb348c97df44 | 5012 | { |
lypinator | 0:bb348c97df44 | 5013 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 5014 | } |
lypinator | 0:bb348c97df44 | 5015 | |
lypinator | 0:bb348c97df44 | 5016 | ivaddr = (uint32_t)(hcryp->Init.pInitVect); |
lypinator | 0:bb348c97df44 | 5017 | |
lypinator | 0:bb348c97df44 | 5018 | hcryp->Instance->IVR3 = __REV(*(uint32_t*)(ivaddr)); |
lypinator | 0:bb348c97df44 | 5019 | ivaddr+=4U; |
lypinator | 0:bb348c97df44 | 5020 | hcryp->Instance->IVR2 = __REV(*(uint32_t*)(ivaddr)); |
lypinator | 0:bb348c97df44 | 5021 | ivaddr+=4U; |
lypinator | 0:bb348c97df44 | 5022 | hcryp->Instance->IVR1 = __REV(*(uint32_t*)(ivaddr)); |
lypinator | 0:bb348c97df44 | 5023 | ivaddr+=4U; |
lypinator | 0:bb348c97df44 | 5024 | hcryp->Instance->IVR0 = __REV(*(uint32_t*)(ivaddr)); |
lypinator | 0:bb348c97df44 | 5025 | } |
lypinator | 0:bb348c97df44 | 5026 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 5027 | } |
lypinator | 0:bb348c97df44 | 5028 | |
lypinator | 0:bb348c97df44 | 5029 | |
lypinator | 0:bb348c97df44 | 5030 | |
lypinator | 0:bb348c97df44 | 5031 | /** |
lypinator | 0:bb348c97df44 | 5032 | * @brief Handle CRYP block input/output data handling under interruption. |
lypinator | 0:bb348c97df44 | 5033 | * @note The function is called under interruption only, once |
lypinator | 0:bb348c97df44 | 5034 | * interruptions have been enabled by HAL_CRYPEx_AES_IT(). |
lypinator | 0:bb348c97df44 | 5035 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 5036 | * the configuration information for CRYP module. |
lypinator | 0:bb348c97df44 | 5037 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 5038 | */ |
lypinator | 0:bb348c97df44 | 5039 | static HAL_StatusTypeDef CRYP_AES_IT(CRYP_HandleTypeDef *hcryp) |
lypinator | 0:bb348c97df44 | 5040 | { |
lypinator | 0:bb348c97df44 | 5041 | uint32_t inputaddr = 0U; |
lypinator | 0:bb348c97df44 | 5042 | uint32_t outputaddr = 0U; |
lypinator | 0:bb348c97df44 | 5043 | |
lypinator | 0:bb348c97df44 | 5044 | if(hcryp->State == HAL_CRYP_STATE_BUSY) |
lypinator | 0:bb348c97df44 | 5045 | { |
lypinator | 0:bb348c97df44 | 5046 | if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION) |
lypinator | 0:bb348c97df44 | 5047 | { |
lypinator | 0:bb348c97df44 | 5048 | /* Get the output data address */ |
lypinator | 0:bb348c97df44 | 5049 | outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; |
lypinator | 0:bb348c97df44 | 5050 | |
lypinator | 0:bb348c97df44 | 5051 | /* Read the last available output block from the Data Output Register */ |
lypinator | 0:bb348c97df44 | 5052 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 5053 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5054 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 5055 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5056 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 5057 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5058 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 5059 | hcryp->pCrypOutBuffPtr += 16U; |
lypinator | 0:bb348c97df44 | 5060 | hcryp->CrypOutCount -= 16U; |
lypinator | 0:bb348c97df44 | 5061 | |
lypinator | 0:bb348c97df44 | 5062 | } |
lypinator | 0:bb348c97df44 | 5063 | else |
lypinator | 0:bb348c97df44 | 5064 | { |
lypinator | 0:bb348c97df44 | 5065 | /* Read the derived key from the Key registers */ |
lypinator | 0:bb348c97df44 | 5066 | if (hcryp->Init.KeySize == CRYP_KEYSIZE_256B) |
lypinator | 0:bb348c97df44 | 5067 | { |
lypinator | 0:bb348c97df44 | 5068 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR7); |
lypinator | 0:bb348c97df44 | 5069 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5070 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR6); |
lypinator | 0:bb348c97df44 | 5071 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5072 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR5); |
lypinator | 0:bb348c97df44 | 5073 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5074 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR4); |
lypinator | 0:bb348c97df44 | 5075 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5076 | } |
lypinator | 0:bb348c97df44 | 5077 | |
lypinator | 0:bb348c97df44 | 5078 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR3); |
lypinator | 0:bb348c97df44 | 5079 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5080 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR2); |
lypinator | 0:bb348c97df44 | 5081 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5082 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR1); |
lypinator | 0:bb348c97df44 | 5083 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5084 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR0); |
lypinator | 0:bb348c97df44 | 5085 | } |
lypinator | 0:bb348c97df44 | 5086 | |
lypinator | 0:bb348c97df44 | 5087 | /* In case of ciphering or deciphering, check if all output text has been retrieved; |
lypinator | 0:bb348c97df44 | 5088 | In case of key derivation, stop right there */ |
lypinator | 0:bb348c97df44 | 5089 | if ((hcryp->CrypOutCount == 0U) || (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)) |
lypinator | 0:bb348c97df44 | 5090 | { |
lypinator | 0:bb348c97df44 | 5091 | /* Disable Computation Complete Flag and Errors Interrupts */ |
lypinator | 0:bb348c97df44 | 5092 | __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); |
lypinator | 0:bb348c97df44 | 5093 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 5094 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 5095 | |
lypinator | 0:bb348c97df44 | 5096 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 5097 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 5098 | |
lypinator | 0:bb348c97df44 | 5099 | /* Call computation complete callback */ |
lypinator | 0:bb348c97df44 | 5100 | HAL_CRYPEx_ComputationCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 5101 | |
lypinator | 0:bb348c97df44 | 5102 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 5103 | } |
lypinator | 0:bb348c97df44 | 5104 | /* If suspension flag has been raised, suspend processing */ |
lypinator | 0:bb348c97df44 | 5105 | else if (hcryp->SuspendRequest == HAL_CRYP_SUSPEND) |
lypinator | 0:bb348c97df44 | 5106 | { |
lypinator | 0:bb348c97df44 | 5107 | /* reset ModeSuspend */ |
lypinator | 0:bb348c97df44 | 5108 | hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE; |
lypinator | 0:bb348c97df44 | 5109 | |
lypinator | 0:bb348c97df44 | 5110 | /* Disable Computation Complete Flag and Errors Interrupts */ |
lypinator | 0:bb348c97df44 | 5111 | __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); |
lypinator | 0:bb348c97df44 | 5112 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 5113 | hcryp->State = HAL_CRYP_STATE_SUSPENDED; |
lypinator | 0:bb348c97df44 | 5114 | |
lypinator | 0:bb348c97df44 | 5115 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 5116 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 5117 | |
lypinator | 0:bb348c97df44 | 5118 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 5119 | } |
lypinator | 0:bb348c97df44 | 5120 | else /* Process the rest of input data */ |
lypinator | 0:bb348c97df44 | 5121 | { |
lypinator | 0:bb348c97df44 | 5122 | /* Get the Intput data address */ |
lypinator | 0:bb348c97df44 | 5123 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
lypinator | 0:bb348c97df44 | 5124 | |
lypinator | 0:bb348c97df44 | 5125 | /* Increment/decrement instance pointer/counter */ |
lypinator | 0:bb348c97df44 | 5126 | hcryp->pCrypInBuffPtr += 16U; |
lypinator | 0:bb348c97df44 | 5127 | hcryp->CrypInCount -= 16U; |
lypinator | 0:bb348c97df44 | 5128 | |
lypinator | 0:bb348c97df44 | 5129 | /* Write the next input block in the Data Input register */ |
lypinator | 0:bb348c97df44 | 5130 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 5131 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5132 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 5133 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5134 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 5135 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5136 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 5137 | |
lypinator | 0:bb348c97df44 | 5138 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 5139 | } |
lypinator | 0:bb348c97df44 | 5140 | } |
lypinator | 0:bb348c97df44 | 5141 | else |
lypinator | 0:bb348c97df44 | 5142 | { |
lypinator | 0:bb348c97df44 | 5143 | return HAL_BUSY; |
lypinator | 0:bb348c97df44 | 5144 | } |
lypinator | 0:bb348c97df44 | 5145 | } |
lypinator | 0:bb348c97df44 | 5146 | |
lypinator | 0:bb348c97df44 | 5147 | /** |
lypinator | 0:bb348c97df44 | 5148 | * @} |
lypinator | 0:bb348c97df44 | 5149 | */ |
lypinator | 0:bb348c97df44 | 5150 | |
lypinator | 0:bb348c97df44 | 5151 | #endif /* AES */ |
lypinator | 0:bb348c97df44 | 5152 | |
lypinator | 0:bb348c97df44 | 5153 | #endif /* HAL_CRYP_MODULE_ENABLED */ |
lypinator | 0:bb348c97df44 | 5154 | |
lypinator | 0:bb348c97df44 | 5155 | /** |
lypinator | 0:bb348c97df44 | 5156 | * @} |
lypinator | 0:bb348c97df44 | 5157 | */ |
lypinator | 0:bb348c97df44 | 5158 | |
lypinator | 0:bb348c97df44 | 5159 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |