inport from local
Dependents: Hobbyking_Cheetah_0511
targets/TARGET_STM/TARGET_STM32F4/device/stm32f4xx_hal_cryp_ex.c@0:85b3fd62ea1a, 2020-03-16 (annotated)
- Committer:
- NYX
- Date:
- Mon Mar 16 06:35:48 2020 +0000
- Revision:
- 0:85b3fd62ea1a
reinport to mbed;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
NYX | 0:85b3fd62ea1a | 1 | /** |
NYX | 0:85b3fd62ea1a | 2 | ****************************************************************************** |
NYX | 0:85b3fd62ea1a | 3 | * @file stm32f4xx_hal_cryp_ex.c |
NYX | 0:85b3fd62ea1a | 4 | * @author MCD Application Team |
NYX | 0:85b3fd62ea1a | 5 | * @version V1.7.1 |
NYX | 0:85b3fd62ea1a | 6 | * @date 14-April-2017 |
NYX | 0:85b3fd62ea1a | 7 | * @brief Extended CRYP HAL module driver |
NYX | 0:85b3fd62ea1a | 8 | * This file provides firmware functions to manage the following |
NYX | 0:85b3fd62ea1a | 9 | * functionalities of CRYP extension peripheral: |
NYX | 0:85b3fd62ea1a | 10 | * + Extended AES processing functions |
NYX | 0:85b3fd62ea1a | 11 | * |
NYX | 0:85b3fd62ea1a | 12 | @verbatim |
NYX | 0:85b3fd62ea1a | 13 | ============================================================================== |
NYX | 0:85b3fd62ea1a | 14 | ##### How to use this driver ##### |
NYX | 0:85b3fd62ea1a | 15 | ============================================================================== |
NYX | 0:85b3fd62ea1a | 16 | [..] |
NYX | 0:85b3fd62ea1a | 17 | The CRYP Extension HAL driver can be used as follows: |
NYX | 0:85b3fd62ea1a | 18 | (#)Initialize the CRYP low level resources by implementing the HAL_CRYP_MspInit(): |
NYX | 0:85b3fd62ea1a | 19 | (##) Enable the CRYP interface clock using __HAL_RCC_CRYP_CLK_ENABLE() |
NYX | 0:85b3fd62ea1a | 20 | (##) In case of using interrupts (e.g. HAL_CRYPEx_AESGCM_Encrypt_IT()) |
NYX | 0:85b3fd62ea1a | 21 | (+++) Configure the CRYP interrupt priority using HAL_NVIC_SetPriority() |
NYX | 0:85b3fd62ea1a | 22 | (+++) Enable the CRYP IRQ handler using HAL_NVIC_EnableIRQ() |
NYX | 0:85b3fd62ea1a | 23 | (+++) In CRYP IRQ handler, call HAL_CRYP_IRQHandler() |
NYX | 0:85b3fd62ea1a | 24 | (##) In case of using DMA to control data transfer (e.g. HAL_AES_ECB_Encrypt_DMA()) |
NYX | 0:85b3fd62ea1a | 25 | (+++) Enable the DMAx interface clock using __DMAx_CLK_ENABLE() |
NYX | 0:85b3fd62ea1a | 26 | (+++) Configure and enable two DMA streams one for managing data transfer from |
NYX | 0:85b3fd62ea1a | 27 | memory to peripheral (input stream) and another stream for managing data |
NYX | 0:85b3fd62ea1a | 28 | transfer from peripheral to memory (output stream) |
NYX | 0:85b3fd62ea1a | 29 | (+++) Associate the initialized DMA handle to the CRYP DMA handle |
NYX | 0:85b3fd62ea1a | 30 | using __HAL_LINKDMA() |
NYX | 0:85b3fd62ea1a | 31 | (+++) Configure the priority and enable the NVIC for the transfer complete |
NYX | 0:85b3fd62ea1a | 32 | interrupt on the two DMA Streams. The output stream should have higher |
NYX | 0:85b3fd62ea1a | 33 | priority than the input stream HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ() |
NYX | 0:85b3fd62ea1a | 34 | (#)Initialize the CRYP HAL using HAL_CRYP_Init(). This function configures mainly: |
NYX | 0:85b3fd62ea1a | 35 | (##) The data type: 1-bit, 8-bit, 16-bit and 32-bit |
NYX | 0:85b3fd62ea1a | 36 | (##) The key size: 128, 192 and 256. This parameter is relevant only for AES |
NYX | 0:85b3fd62ea1a | 37 | (##) The encryption/decryption key. Its size depends on the algorithm |
NYX | 0:85b3fd62ea1a | 38 | used for encryption/decryption |
NYX | 0:85b3fd62ea1a | 39 | (##) The initialization vector (counter). It is not used ECB mode. |
NYX | 0:85b3fd62ea1a | 40 | (#)Three processing (encryption/decryption) functions are available: |
NYX | 0:85b3fd62ea1a | 41 | (##) Polling mode: encryption and decryption APIs are blocking functions |
NYX | 0:85b3fd62ea1a | 42 | i.e. they process the data and wait till the processing is finished |
NYX | 0:85b3fd62ea1a | 43 | e.g. HAL_CRYPEx_AESGCM_Encrypt() |
NYX | 0:85b3fd62ea1a | 44 | (##) Interrupt mode: encryption and decryption APIs are not blocking functions |
NYX | 0:85b3fd62ea1a | 45 | i.e. they process the data under interrupt |
NYX | 0:85b3fd62ea1a | 46 | e.g. HAL_CRYPEx_AESGCM_Encrypt_IT() |
NYX | 0:85b3fd62ea1a | 47 | (##) DMA mode: encryption and decryption APIs are not blocking functions |
NYX | 0:85b3fd62ea1a | 48 | i.e. the data transfer is ensured by DMA |
NYX | 0:85b3fd62ea1a | 49 | e.g. HAL_CRYPEx_AESGCM_Encrypt_DMA() |
NYX | 0:85b3fd62ea1a | 50 | (#)When the processing function is called at first time after HAL_CRYP_Init() |
NYX | 0:85b3fd62ea1a | 51 | the CRYP peripheral is initialized and processes the buffer in input. |
NYX | 0:85b3fd62ea1a | 52 | At second call, the processing function performs an append of the already |
NYX | 0:85b3fd62ea1a | 53 | processed buffer. |
NYX | 0:85b3fd62ea1a | 54 | When a new data block is to be processed, call HAL_CRYP_Init() then the |
NYX | 0:85b3fd62ea1a | 55 | processing function. |
NYX | 0:85b3fd62ea1a | 56 | (#)In AES-GCM and AES-CCM modes are an authenticated encryption algorithms |
NYX | 0:85b3fd62ea1a | 57 | which provide authentication messages. |
NYX | 0:85b3fd62ea1a | 58 | HAL_AES_GCM_Finish() and HAL_AES_CCM_Finish() are used to provide those |
NYX | 0:85b3fd62ea1a | 59 | authentication messages. |
NYX | 0:85b3fd62ea1a | 60 | Call those functions after the processing ones (polling, interrupt or DMA). |
NYX | 0:85b3fd62ea1a | 61 | e.g. in AES-CCM mode call HAL_CRYPEx_AESCCM_Encrypt() to encrypt the plain data |
NYX | 0:85b3fd62ea1a | 62 | then call HAL_CRYPEx_AESCCM_Finish() to get the authentication message |
NYX | 0:85b3fd62ea1a | 63 | -@- For CCM Encrypt/Decrypt API's, only DataType = 8-bit is supported by this version. |
NYX | 0:85b3fd62ea1a | 64 | -@- The HAL_CRYPEx_AESGCM_xxxx() implementation is limited to 32bits inputs data length |
NYX | 0:85b3fd62ea1a | 65 | (Plain/Cyphertext, Header) compared with GCM standards specifications (800-38D). |
NYX | 0:85b3fd62ea1a | 66 | (#)Call HAL_CRYP_DeInit() to deinitialize the CRYP peripheral. |
NYX | 0:85b3fd62ea1a | 67 | |
NYX | 0:85b3fd62ea1a | 68 | @endverbatim |
NYX | 0:85b3fd62ea1a | 69 | ****************************************************************************** |
NYX | 0:85b3fd62ea1a | 70 | * @attention |
NYX | 0:85b3fd62ea1a | 71 | * |
NYX | 0:85b3fd62ea1a | 72 | * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> |
NYX | 0:85b3fd62ea1a | 73 | * |
NYX | 0:85b3fd62ea1a | 74 | * Redistribution and use in source and binary forms, with or without modification, |
NYX | 0:85b3fd62ea1a | 75 | * are permitted provided that the following conditions are met: |
NYX | 0:85b3fd62ea1a | 76 | * 1. Redistributions of source code must retain the above copyright notice, |
NYX | 0:85b3fd62ea1a | 77 | * this list of conditions and the following disclaimer. |
NYX | 0:85b3fd62ea1a | 78 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
NYX | 0:85b3fd62ea1a | 79 | * this list of conditions and the following disclaimer in the documentation |
NYX | 0:85b3fd62ea1a | 80 | * and/or other materials provided with the distribution. |
NYX | 0:85b3fd62ea1a | 81 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
NYX | 0:85b3fd62ea1a | 82 | * may be used to endorse or promote products derived from this software |
NYX | 0:85b3fd62ea1a | 83 | * without specific prior written permission. |
NYX | 0:85b3fd62ea1a | 84 | * |
NYX | 0:85b3fd62ea1a | 85 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
NYX | 0:85b3fd62ea1a | 86 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
NYX | 0:85b3fd62ea1a | 87 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
NYX | 0:85b3fd62ea1a | 88 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
NYX | 0:85b3fd62ea1a | 89 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
NYX | 0:85b3fd62ea1a | 90 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
NYX | 0:85b3fd62ea1a | 91 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
NYX | 0:85b3fd62ea1a | 92 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
NYX | 0:85b3fd62ea1a | 93 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
NYX | 0:85b3fd62ea1a | 94 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
NYX | 0:85b3fd62ea1a | 95 | * |
NYX | 0:85b3fd62ea1a | 96 | ****************************************************************************** |
NYX | 0:85b3fd62ea1a | 97 | */ |
NYX | 0:85b3fd62ea1a | 98 | |
NYX | 0:85b3fd62ea1a | 99 | /* Includes ------------------------------------------------------------------*/ |
NYX | 0:85b3fd62ea1a | 100 | #include "stm32f4xx_hal.h" |
NYX | 0:85b3fd62ea1a | 101 | |
NYX | 0:85b3fd62ea1a | 102 | /** @addtogroup STM32F4xx_HAL_Driver |
NYX | 0:85b3fd62ea1a | 103 | * @{ |
NYX | 0:85b3fd62ea1a | 104 | */ |
NYX | 0:85b3fd62ea1a | 105 | |
NYX | 0:85b3fd62ea1a | 106 | /** @defgroup CRYPEx CRYPEx |
NYX | 0:85b3fd62ea1a | 107 | * @brief CRYP Extension HAL module driver. |
NYX | 0:85b3fd62ea1a | 108 | * @{ |
NYX | 0:85b3fd62ea1a | 109 | */ |
NYX | 0:85b3fd62ea1a | 110 | |
NYX | 0:85b3fd62ea1a | 111 | #ifdef HAL_CRYP_MODULE_ENABLED |
NYX | 0:85b3fd62ea1a | 112 | |
NYX | 0:85b3fd62ea1a | 113 | #if defined(CRYP) |
NYX | 0:85b3fd62ea1a | 114 | |
NYX | 0:85b3fd62ea1a | 115 | /* Private typedef -----------------------------------------------------------*/ |
NYX | 0:85b3fd62ea1a | 116 | /* Private define ------------------------------------------------------------*/ |
NYX | 0:85b3fd62ea1a | 117 | /** @addtogroup CRYPEx_Private_define |
NYX | 0:85b3fd62ea1a | 118 | * @{ |
NYX | 0:85b3fd62ea1a | 119 | */ |
NYX | 0:85b3fd62ea1a | 120 | #define CRYPEx_TIMEOUT_VALUE 1U |
NYX | 0:85b3fd62ea1a | 121 | /** |
NYX | 0:85b3fd62ea1a | 122 | * @} |
NYX | 0:85b3fd62ea1a | 123 | */ |
NYX | 0:85b3fd62ea1a | 124 | |
NYX | 0:85b3fd62ea1a | 125 | /* Private macro -------------------------------------------------------------*/ |
NYX | 0:85b3fd62ea1a | 126 | /* Private variables ---------------------------------------------------------*/ |
NYX | 0:85b3fd62ea1a | 127 | /* Private function prototypes -----------------------------------------------*/ |
NYX | 0:85b3fd62ea1a | 128 | /** @defgroup CRYPEx_Private_Functions_prototypes CRYP Private Functions Prototypes |
NYX | 0:85b3fd62ea1a | 129 | * @{ |
NYX | 0:85b3fd62ea1a | 130 | */ |
NYX | 0:85b3fd62ea1a | 131 | static void CRYPEx_GCMCCM_SetInitVector(CRYP_HandleTypeDef *hcryp, uint8_t *InitVector); |
NYX | 0:85b3fd62ea1a | 132 | static void CRYPEx_GCMCCM_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key, uint32_t KeySize); |
NYX | 0:85b3fd62ea1a | 133 | static HAL_StatusTypeDef CRYPEx_GCMCCM_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t *Input, uint16_t Ilength, uint8_t *Output, uint32_t Timeout); |
NYX | 0:85b3fd62ea1a | 134 | static HAL_StatusTypeDef CRYPEx_GCMCCM_SetHeaderPhase(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint32_t Timeout); |
NYX | 0:85b3fd62ea1a | 135 | static void CRYPEx_GCMCCM_DMAInCplt(DMA_HandleTypeDef *hdma); |
NYX | 0:85b3fd62ea1a | 136 | static void CRYPEx_GCMCCM_DMAOutCplt(DMA_HandleTypeDef *hdma); |
NYX | 0:85b3fd62ea1a | 137 | static void CRYPEx_GCMCCM_DMAError(DMA_HandleTypeDef *hdma); |
NYX | 0:85b3fd62ea1a | 138 | static void CRYPEx_GCMCCM_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr); |
NYX | 0:85b3fd62ea1a | 139 | /** |
NYX | 0:85b3fd62ea1a | 140 | * @} |
NYX | 0:85b3fd62ea1a | 141 | */ |
NYX | 0:85b3fd62ea1a | 142 | |
NYX | 0:85b3fd62ea1a | 143 | /* Private functions ---------------------------------------------------------*/ |
NYX | 0:85b3fd62ea1a | 144 | /** @addtogroup CRYPEx_Private_Functions |
NYX | 0:85b3fd62ea1a | 145 | * @{ |
NYX | 0:85b3fd62ea1a | 146 | */ |
NYX | 0:85b3fd62ea1a | 147 | |
NYX | 0:85b3fd62ea1a | 148 | /** |
NYX | 0:85b3fd62ea1a | 149 | * @brief DMA CRYP Input Data process complete callback. |
NYX | 0:85b3fd62ea1a | 150 | * @param hdma: DMA handle |
NYX | 0:85b3fd62ea1a | 151 | * @retval None |
NYX | 0:85b3fd62ea1a | 152 | */ |
NYX | 0:85b3fd62ea1a | 153 | static void CRYPEx_GCMCCM_DMAInCplt(DMA_HandleTypeDef *hdma) |
NYX | 0:85b3fd62ea1a | 154 | { |
NYX | 0:85b3fd62ea1a | 155 | CRYP_HandleTypeDef* hcryp = ( CRYP_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
NYX | 0:85b3fd62ea1a | 156 | |
NYX | 0:85b3fd62ea1a | 157 | /* Disable the DMA transfer for input Fifo request by resetting the DIEN bit |
NYX | 0:85b3fd62ea1a | 158 | in the DMACR register */ |
NYX | 0:85b3fd62ea1a | 159 | hcryp->Instance->DMACR &= (uint32_t)(~CRYP_DMACR_DIEN); |
NYX | 0:85b3fd62ea1a | 160 | |
NYX | 0:85b3fd62ea1a | 161 | /* Call input data transfer complete callback */ |
NYX | 0:85b3fd62ea1a | 162 | HAL_CRYP_InCpltCallback(hcryp); |
NYX | 0:85b3fd62ea1a | 163 | } |
NYX | 0:85b3fd62ea1a | 164 | |
NYX | 0:85b3fd62ea1a | 165 | /** |
NYX | 0:85b3fd62ea1a | 166 | * @brief DMA CRYP Output Data process complete callback. |
NYX | 0:85b3fd62ea1a | 167 | * @param hdma: DMA handle |
NYX | 0:85b3fd62ea1a | 168 | * @retval None |
NYX | 0:85b3fd62ea1a | 169 | */ |
NYX | 0:85b3fd62ea1a | 170 | static void CRYPEx_GCMCCM_DMAOutCplt(DMA_HandleTypeDef *hdma) |
NYX | 0:85b3fd62ea1a | 171 | { |
NYX | 0:85b3fd62ea1a | 172 | CRYP_HandleTypeDef* hcryp = ( CRYP_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
NYX | 0:85b3fd62ea1a | 173 | |
NYX | 0:85b3fd62ea1a | 174 | /* Disable the DMA transfer for output Fifo request by resetting the DOEN bit |
NYX | 0:85b3fd62ea1a | 175 | in the DMACR register */ |
NYX | 0:85b3fd62ea1a | 176 | hcryp->Instance->DMACR &= (uint32_t)(~CRYP_DMACR_DOEN); |
NYX | 0:85b3fd62ea1a | 177 | |
NYX | 0:85b3fd62ea1a | 178 | /* Enable the CRYP peripheral */ |
NYX | 0:85b3fd62ea1a | 179 | __HAL_CRYP_DISABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 180 | |
NYX | 0:85b3fd62ea1a | 181 | /* Change the CRYP peripheral state */ |
NYX | 0:85b3fd62ea1a | 182 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 183 | |
NYX | 0:85b3fd62ea1a | 184 | /* Call output data transfer complete callback */ |
NYX | 0:85b3fd62ea1a | 185 | HAL_CRYP_OutCpltCallback(hcryp); |
NYX | 0:85b3fd62ea1a | 186 | } |
NYX | 0:85b3fd62ea1a | 187 | |
NYX | 0:85b3fd62ea1a | 188 | /** |
NYX | 0:85b3fd62ea1a | 189 | * @brief DMA CRYP communication error callback. |
NYX | 0:85b3fd62ea1a | 190 | * @param hdma: DMA handle |
NYX | 0:85b3fd62ea1a | 191 | * @retval None |
NYX | 0:85b3fd62ea1a | 192 | */ |
NYX | 0:85b3fd62ea1a | 193 | static void CRYPEx_GCMCCM_DMAError(DMA_HandleTypeDef *hdma) |
NYX | 0:85b3fd62ea1a | 194 | { |
NYX | 0:85b3fd62ea1a | 195 | CRYP_HandleTypeDef* hcryp = ( CRYP_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
NYX | 0:85b3fd62ea1a | 196 | hcryp->State= HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 197 | HAL_CRYP_ErrorCallback(hcryp); |
NYX | 0:85b3fd62ea1a | 198 | } |
NYX | 0:85b3fd62ea1a | 199 | |
NYX | 0:85b3fd62ea1a | 200 | /** |
NYX | 0:85b3fd62ea1a | 201 | * @brief Writes the Key in Key registers. |
NYX | 0:85b3fd62ea1a | 202 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 203 | * the configuration information for CRYP module |
NYX | 0:85b3fd62ea1a | 204 | * @param Key: Pointer to Key buffer |
NYX | 0:85b3fd62ea1a | 205 | * @param KeySize: Size of Key |
NYX | 0:85b3fd62ea1a | 206 | * @retval None |
NYX | 0:85b3fd62ea1a | 207 | */ |
NYX | 0:85b3fd62ea1a | 208 | static void CRYPEx_GCMCCM_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key, uint32_t KeySize) |
NYX | 0:85b3fd62ea1a | 209 | { |
NYX | 0:85b3fd62ea1a | 210 | uint32_t keyaddr = (uint32_t)Key; |
NYX | 0:85b3fd62ea1a | 211 | |
NYX | 0:85b3fd62ea1a | 212 | switch(KeySize) |
NYX | 0:85b3fd62ea1a | 213 | { |
NYX | 0:85b3fd62ea1a | 214 | case CRYP_KEYSIZE_256B: |
NYX | 0:85b3fd62ea1a | 215 | /* Key Initialisation */ |
NYX | 0:85b3fd62ea1a | 216 | hcryp->Instance->K0LR = __REV(*(uint32_t*)(keyaddr)); |
NYX | 0:85b3fd62ea1a | 217 | keyaddr+=4U; |
NYX | 0:85b3fd62ea1a | 218 | hcryp->Instance->K0RR = __REV(*(uint32_t*)(keyaddr)); |
NYX | 0:85b3fd62ea1a | 219 | keyaddr+=4U; |
NYX | 0:85b3fd62ea1a | 220 | hcryp->Instance->K1LR = __REV(*(uint32_t*)(keyaddr)); |
NYX | 0:85b3fd62ea1a | 221 | keyaddr+=4U; |
NYX | 0:85b3fd62ea1a | 222 | hcryp->Instance->K1RR = __REV(*(uint32_t*)(keyaddr)); |
NYX | 0:85b3fd62ea1a | 223 | keyaddr+=4U; |
NYX | 0:85b3fd62ea1a | 224 | hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr)); |
NYX | 0:85b3fd62ea1a | 225 | keyaddr+=4U; |
NYX | 0:85b3fd62ea1a | 226 | hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr)); |
NYX | 0:85b3fd62ea1a | 227 | keyaddr+=4U; |
NYX | 0:85b3fd62ea1a | 228 | hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr)); |
NYX | 0:85b3fd62ea1a | 229 | keyaddr+=4U; |
NYX | 0:85b3fd62ea1a | 230 | hcryp->Instance->K3RR = __REV(*(uint32_t*)(keyaddr)); |
NYX | 0:85b3fd62ea1a | 231 | break; |
NYX | 0:85b3fd62ea1a | 232 | case CRYP_KEYSIZE_192B: |
NYX | 0:85b3fd62ea1a | 233 | hcryp->Instance->K1LR = __REV(*(uint32_t*)(keyaddr)); |
NYX | 0:85b3fd62ea1a | 234 | keyaddr+=4U; |
NYX | 0:85b3fd62ea1a | 235 | hcryp->Instance->K1RR = __REV(*(uint32_t*)(keyaddr)); |
NYX | 0:85b3fd62ea1a | 236 | keyaddr+=4U; |
NYX | 0:85b3fd62ea1a | 237 | hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr)); |
NYX | 0:85b3fd62ea1a | 238 | keyaddr+=4U; |
NYX | 0:85b3fd62ea1a | 239 | hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr)); |
NYX | 0:85b3fd62ea1a | 240 | keyaddr+=4U; |
NYX | 0:85b3fd62ea1a | 241 | hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr)); |
NYX | 0:85b3fd62ea1a | 242 | keyaddr+=4U; |
NYX | 0:85b3fd62ea1a | 243 | hcryp->Instance->K3RR = __REV(*(uint32_t*)(keyaddr)); |
NYX | 0:85b3fd62ea1a | 244 | break; |
NYX | 0:85b3fd62ea1a | 245 | case CRYP_KEYSIZE_128B: |
NYX | 0:85b3fd62ea1a | 246 | hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr)); |
NYX | 0:85b3fd62ea1a | 247 | keyaddr+=4U; |
NYX | 0:85b3fd62ea1a | 248 | hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr)); |
NYX | 0:85b3fd62ea1a | 249 | keyaddr+=4U; |
NYX | 0:85b3fd62ea1a | 250 | hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr)); |
NYX | 0:85b3fd62ea1a | 251 | keyaddr+=4U; |
NYX | 0:85b3fd62ea1a | 252 | hcryp->Instance->K3RR = __REV(*(uint32_t*)(keyaddr)); |
NYX | 0:85b3fd62ea1a | 253 | break; |
NYX | 0:85b3fd62ea1a | 254 | default: |
NYX | 0:85b3fd62ea1a | 255 | break; |
NYX | 0:85b3fd62ea1a | 256 | } |
NYX | 0:85b3fd62ea1a | 257 | } |
NYX | 0:85b3fd62ea1a | 258 | |
NYX | 0:85b3fd62ea1a | 259 | /** |
NYX | 0:85b3fd62ea1a | 260 | * @brief Writes the InitVector/InitCounter in IV registers. |
NYX | 0:85b3fd62ea1a | 261 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 262 | * the configuration information for CRYP module |
NYX | 0:85b3fd62ea1a | 263 | * @param InitVector: Pointer to InitVector/InitCounter buffer |
NYX | 0:85b3fd62ea1a | 264 | * @retval None |
NYX | 0:85b3fd62ea1a | 265 | */ |
NYX | 0:85b3fd62ea1a | 266 | static void CRYPEx_GCMCCM_SetInitVector(CRYP_HandleTypeDef *hcryp, uint8_t *InitVector) |
NYX | 0:85b3fd62ea1a | 267 | { |
NYX | 0:85b3fd62ea1a | 268 | uint32_t ivaddr = (uint32_t)InitVector; |
NYX | 0:85b3fd62ea1a | 269 | |
NYX | 0:85b3fd62ea1a | 270 | hcryp->Instance->IV0LR = __REV(*(uint32_t*)(ivaddr)); |
NYX | 0:85b3fd62ea1a | 271 | ivaddr+=4U; |
NYX | 0:85b3fd62ea1a | 272 | hcryp->Instance->IV0RR = __REV(*(uint32_t*)(ivaddr)); |
NYX | 0:85b3fd62ea1a | 273 | ivaddr+=4U; |
NYX | 0:85b3fd62ea1a | 274 | hcryp->Instance->IV1LR = __REV(*(uint32_t*)(ivaddr)); |
NYX | 0:85b3fd62ea1a | 275 | ivaddr+=4U; |
NYX | 0:85b3fd62ea1a | 276 | hcryp->Instance->IV1RR = __REV(*(uint32_t*)(ivaddr)); |
NYX | 0:85b3fd62ea1a | 277 | } |
NYX | 0:85b3fd62ea1a | 278 | |
NYX | 0:85b3fd62ea1a | 279 | /** |
NYX | 0:85b3fd62ea1a | 280 | * @brief Process Data: Writes Input data in polling mode and read the Output data. |
NYX | 0:85b3fd62ea1a | 281 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 282 | * the configuration information for CRYP module |
NYX | 0:85b3fd62ea1a | 283 | * @param Input: Pointer to the Input buffer. |
NYX | 0:85b3fd62ea1a | 284 | * @param Ilength: Length of the Input buffer, must be a multiple of 16 |
NYX | 0:85b3fd62ea1a | 285 | * @param Output: Pointer to the returned buffer |
NYX | 0:85b3fd62ea1a | 286 | * @param Timeout: Timeout value |
NYX | 0:85b3fd62ea1a | 287 | * @retval None |
NYX | 0:85b3fd62ea1a | 288 | */ |
NYX | 0:85b3fd62ea1a | 289 | static HAL_StatusTypeDef CRYPEx_GCMCCM_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t *Input, uint16_t Ilength, uint8_t *Output, uint32_t Timeout) |
NYX | 0:85b3fd62ea1a | 290 | { |
NYX | 0:85b3fd62ea1a | 291 | uint32_t tickstart = 0U; |
NYX | 0:85b3fd62ea1a | 292 | uint32_t i = 0U; |
NYX | 0:85b3fd62ea1a | 293 | uint32_t inputaddr = (uint32_t)Input; |
NYX | 0:85b3fd62ea1a | 294 | uint32_t outputaddr = (uint32_t)Output; |
NYX | 0:85b3fd62ea1a | 295 | |
NYX | 0:85b3fd62ea1a | 296 | for(i=0U; (i < Ilength); i+=16U) |
NYX | 0:85b3fd62ea1a | 297 | { |
NYX | 0:85b3fd62ea1a | 298 | /* Write the Input block in the IN FIFO */ |
NYX | 0:85b3fd62ea1a | 299 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 300 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 301 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 302 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 303 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 304 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 305 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 306 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 307 | |
NYX | 0:85b3fd62ea1a | 308 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 309 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 310 | |
NYX | 0:85b3fd62ea1a | 311 | while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE)) |
NYX | 0:85b3fd62ea1a | 312 | { |
NYX | 0:85b3fd62ea1a | 313 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 314 | if(Timeout != HAL_MAX_DELAY) |
NYX | 0:85b3fd62ea1a | 315 | { |
NYX | 0:85b3fd62ea1a | 316 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
NYX | 0:85b3fd62ea1a | 317 | { |
NYX | 0:85b3fd62ea1a | 318 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 319 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 320 | |
NYX | 0:85b3fd62ea1a | 321 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 322 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 323 | |
NYX | 0:85b3fd62ea1a | 324 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 325 | } |
NYX | 0:85b3fd62ea1a | 326 | } |
NYX | 0:85b3fd62ea1a | 327 | } |
NYX | 0:85b3fd62ea1a | 328 | /* Read the Output block from the OUT FIFO */ |
NYX | 0:85b3fd62ea1a | 329 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
NYX | 0:85b3fd62ea1a | 330 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 331 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
NYX | 0:85b3fd62ea1a | 332 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 333 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
NYX | 0:85b3fd62ea1a | 334 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 335 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
NYX | 0:85b3fd62ea1a | 336 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 337 | } |
NYX | 0:85b3fd62ea1a | 338 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 339 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 340 | } |
NYX | 0:85b3fd62ea1a | 341 | |
NYX | 0:85b3fd62ea1a | 342 | /** |
NYX | 0:85b3fd62ea1a | 343 | * @brief Sets the header phase |
NYX | 0:85b3fd62ea1a | 344 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 345 | * the configuration information for CRYP module |
NYX | 0:85b3fd62ea1a | 346 | * @param Input: Pointer to the Input buffer. |
NYX | 0:85b3fd62ea1a | 347 | * @param Ilength: Length of the Input buffer, must be a multiple of 16 |
NYX | 0:85b3fd62ea1a | 348 | * @param Timeout: Timeout value |
NYX | 0:85b3fd62ea1a | 349 | * @retval None |
NYX | 0:85b3fd62ea1a | 350 | */ |
NYX | 0:85b3fd62ea1a | 351 | static HAL_StatusTypeDef CRYPEx_GCMCCM_SetHeaderPhase(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint32_t Timeout) |
NYX | 0:85b3fd62ea1a | 352 | { |
NYX | 0:85b3fd62ea1a | 353 | uint32_t tickstart = 0U; |
NYX | 0:85b3fd62ea1a | 354 | uint32_t loopcounter = 0U; |
NYX | 0:85b3fd62ea1a | 355 | uint32_t headeraddr = (uint32_t)Input; |
NYX | 0:85b3fd62ea1a | 356 | |
NYX | 0:85b3fd62ea1a | 357 | /* Prevent unused argument(s) compilation warning */ |
NYX | 0:85b3fd62ea1a | 358 | UNUSED(Ilength); |
NYX | 0:85b3fd62ea1a | 359 | |
NYX | 0:85b3fd62ea1a | 360 | /***************************** Header phase *********************************/ |
NYX | 0:85b3fd62ea1a | 361 | if(hcryp->Init.HeaderSize != 0U) |
NYX | 0:85b3fd62ea1a | 362 | { |
NYX | 0:85b3fd62ea1a | 363 | /* Select header phase */ |
NYX | 0:85b3fd62ea1a | 364 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER); |
NYX | 0:85b3fd62ea1a | 365 | /* Enable the CRYP peripheral */ |
NYX | 0:85b3fd62ea1a | 366 | __HAL_CRYP_ENABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 367 | |
NYX | 0:85b3fd62ea1a | 368 | for(loopcounter = 0U; (loopcounter < hcryp->Init.HeaderSize); loopcounter+=16U) |
NYX | 0:85b3fd62ea1a | 369 | { |
NYX | 0:85b3fd62ea1a | 370 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 371 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 372 | |
NYX | 0:85b3fd62ea1a | 373 | while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM)) |
NYX | 0:85b3fd62ea1a | 374 | { |
NYX | 0:85b3fd62ea1a | 375 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 376 | if(Timeout != HAL_MAX_DELAY) |
NYX | 0:85b3fd62ea1a | 377 | { |
NYX | 0:85b3fd62ea1a | 378 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
NYX | 0:85b3fd62ea1a | 379 | { |
NYX | 0:85b3fd62ea1a | 380 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 381 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 382 | |
NYX | 0:85b3fd62ea1a | 383 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 384 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 385 | |
NYX | 0:85b3fd62ea1a | 386 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 387 | } |
NYX | 0:85b3fd62ea1a | 388 | } |
NYX | 0:85b3fd62ea1a | 389 | } |
NYX | 0:85b3fd62ea1a | 390 | /* Write the Input block in the IN FIFO */ |
NYX | 0:85b3fd62ea1a | 391 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
NYX | 0:85b3fd62ea1a | 392 | headeraddr+=4U; |
NYX | 0:85b3fd62ea1a | 393 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
NYX | 0:85b3fd62ea1a | 394 | headeraddr+=4U; |
NYX | 0:85b3fd62ea1a | 395 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
NYX | 0:85b3fd62ea1a | 396 | headeraddr+=4U; |
NYX | 0:85b3fd62ea1a | 397 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
NYX | 0:85b3fd62ea1a | 398 | headeraddr+=4U; |
NYX | 0:85b3fd62ea1a | 399 | } |
NYX | 0:85b3fd62ea1a | 400 | |
NYX | 0:85b3fd62ea1a | 401 | /* Wait until the complete message has been processed */ |
NYX | 0:85b3fd62ea1a | 402 | |
NYX | 0:85b3fd62ea1a | 403 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 404 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 405 | |
NYX | 0:85b3fd62ea1a | 406 | while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY) |
NYX | 0:85b3fd62ea1a | 407 | { |
NYX | 0:85b3fd62ea1a | 408 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 409 | if(Timeout != HAL_MAX_DELAY) |
NYX | 0:85b3fd62ea1a | 410 | { |
NYX | 0:85b3fd62ea1a | 411 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
NYX | 0:85b3fd62ea1a | 412 | { |
NYX | 0:85b3fd62ea1a | 413 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 414 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 415 | |
NYX | 0:85b3fd62ea1a | 416 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 417 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 418 | |
NYX | 0:85b3fd62ea1a | 419 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 420 | } |
NYX | 0:85b3fd62ea1a | 421 | } |
NYX | 0:85b3fd62ea1a | 422 | } |
NYX | 0:85b3fd62ea1a | 423 | } |
NYX | 0:85b3fd62ea1a | 424 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 425 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 426 | } |
NYX | 0:85b3fd62ea1a | 427 | |
NYX | 0:85b3fd62ea1a | 428 | /** |
NYX | 0:85b3fd62ea1a | 429 | * @brief Sets the DMA configuration and start the DMA transfer. |
NYX | 0:85b3fd62ea1a | 430 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 431 | * the configuration information for CRYP module |
NYX | 0:85b3fd62ea1a | 432 | * @param inputaddr: Address of the Input buffer |
NYX | 0:85b3fd62ea1a | 433 | * @param Size: Size of the Input buffer, must be a multiple of 16 |
NYX | 0:85b3fd62ea1a | 434 | * @param outputaddr: Address of the Output buffer |
NYX | 0:85b3fd62ea1a | 435 | * @retval None |
NYX | 0:85b3fd62ea1a | 436 | */ |
NYX | 0:85b3fd62ea1a | 437 | static void CRYPEx_GCMCCM_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr) |
NYX | 0:85b3fd62ea1a | 438 | { |
NYX | 0:85b3fd62ea1a | 439 | /* Set the CRYP DMA transfer complete callback */ |
NYX | 0:85b3fd62ea1a | 440 | hcryp->hdmain->XferCpltCallback = CRYPEx_GCMCCM_DMAInCplt; |
NYX | 0:85b3fd62ea1a | 441 | /* Set the DMA error callback */ |
NYX | 0:85b3fd62ea1a | 442 | hcryp->hdmain->XferErrorCallback = CRYPEx_GCMCCM_DMAError; |
NYX | 0:85b3fd62ea1a | 443 | |
NYX | 0:85b3fd62ea1a | 444 | /* Set the CRYP DMA transfer complete callback */ |
NYX | 0:85b3fd62ea1a | 445 | hcryp->hdmaout->XferCpltCallback = CRYPEx_GCMCCM_DMAOutCplt; |
NYX | 0:85b3fd62ea1a | 446 | /* Set the DMA error callback */ |
NYX | 0:85b3fd62ea1a | 447 | hcryp->hdmaout->XferErrorCallback = CRYPEx_GCMCCM_DMAError; |
NYX | 0:85b3fd62ea1a | 448 | |
NYX | 0:85b3fd62ea1a | 449 | /* Enable the CRYP peripheral */ |
NYX | 0:85b3fd62ea1a | 450 | __HAL_CRYP_ENABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 451 | |
NYX | 0:85b3fd62ea1a | 452 | /* Enable the DMA In DMA Stream */ |
NYX | 0:85b3fd62ea1a | 453 | HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DR, Size/4U); |
NYX | 0:85b3fd62ea1a | 454 | |
NYX | 0:85b3fd62ea1a | 455 | /* Enable In DMA request */ |
NYX | 0:85b3fd62ea1a | 456 | hcryp->Instance->DMACR = CRYP_DMACR_DIEN; |
NYX | 0:85b3fd62ea1a | 457 | |
NYX | 0:85b3fd62ea1a | 458 | /* Enable the DMA Out DMA Stream */ |
NYX | 0:85b3fd62ea1a | 459 | HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUT, outputaddr, Size/4U); |
NYX | 0:85b3fd62ea1a | 460 | |
NYX | 0:85b3fd62ea1a | 461 | /* Enable Out DMA request */ |
NYX | 0:85b3fd62ea1a | 462 | hcryp->Instance->DMACR |= CRYP_DMACR_DOEN; |
NYX | 0:85b3fd62ea1a | 463 | } |
NYX | 0:85b3fd62ea1a | 464 | |
NYX | 0:85b3fd62ea1a | 465 | /** |
NYX | 0:85b3fd62ea1a | 466 | * @} |
NYX | 0:85b3fd62ea1a | 467 | */ |
NYX | 0:85b3fd62ea1a | 468 | |
NYX | 0:85b3fd62ea1a | 469 | /* Exported functions---------------------------------------------------------*/ |
NYX | 0:85b3fd62ea1a | 470 | /** @addtogroup CRYPEx_Exported_Functions |
NYX | 0:85b3fd62ea1a | 471 | * @{ |
NYX | 0:85b3fd62ea1a | 472 | */ |
NYX | 0:85b3fd62ea1a | 473 | |
NYX | 0:85b3fd62ea1a | 474 | /** @defgroup CRYPEx_Exported_Functions_Group1 Extended AES processing functions |
NYX | 0:85b3fd62ea1a | 475 | * @brief Extended processing functions. |
NYX | 0:85b3fd62ea1a | 476 | * |
NYX | 0:85b3fd62ea1a | 477 | @verbatim |
NYX | 0:85b3fd62ea1a | 478 | ============================================================================== |
NYX | 0:85b3fd62ea1a | 479 | ##### Extended AES processing functions ##### |
NYX | 0:85b3fd62ea1a | 480 | ============================================================================== |
NYX | 0:85b3fd62ea1a | 481 | [..] This section provides functions allowing to: |
NYX | 0:85b3fd62ea1a | 482 | (+) Encrypt plaintext using AES-128/192/256 using GCM and CCM chaining modes |
NYX | 0:85b3fd62ea1a | 483 | (+) Decrypt cyphertext using AES-128/192/256 using GCM and CCM chaining modes |
NYX | 0:85b3fd62ea1a | 484 | (+) Finish the processing. This function is available only for GCM and CCM |
NYX | 0:85b3fd62ea1a | 485 | [..] Three processing methods are available: |
NYX | 0:85b3fd62ea1a | 486 | (+) Polling mode |
NYX | 0:85b3fd62ea1a | 487 | (+) Interrupt mode |
NYX | 0:85b3fd62ea1a | 488 | (+) DMA mode |
NYX | 0:85b3fd62ea1a | 489 | |
NYX | 0:85b3fd62ea1a | 490 | @endverbatim |
NYX | 0:85b3fd62ea1a | 491 | * @{ |
NYX | 0:85b3fd62ea1a | 492 | */ |
NYX | 0:85b3fd62ea1a | 493 | |
NYX | 0:85b3fd62ea1a | 494 | |
NYX | 0:85b3fd62ea1a | 495 | /** |
NYX | 0:85b3fd62ea1a | 496 | * @brief Initializes the CRYP peripheral in AES CCM encryption mode then |
NYX | 0:85b3fd62ea1a | 497 | * encrypt pPlainData. The cypher data are available in pCypherData. |
NYX | 0:85b3fd62ea1a | 498 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 499 | * the configuration information for CRYP module |
NYX | 0:85b3fd62ea1a | 500 | * @param pPlainData: Pointer to the plaintext buffer |
NYX | 0:85b3fd62ea1a | 501 | * @param Size: Length of the plaintext buffer, must be a multiple of 16 |
NYX | 0:85b3fd62ea1a | 502 | * @param pCypherData: Pointer to the cyphertext buffer |
NYX | 0:85b3fd62ea1a | 503 | * @param Timeout: Timeout duration |
NYX | 0:85b3fd62ea1a | 504 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 505 | */ |
NYX | 0:85b3fd62ea1a | 506 | HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout) |
NYX | 0:85b3fd62ea1a | 507 | { |
NYX | 0:85b3fd62ea1a | 508 | uint32_t tickstart = 0U; |
NYX | 0:85b3fd62ea1a | 509 | uint32_t headersize = hcryp->Init.HeaderSize; |
NYX | 0:85b3fd62ea1a | 510 | uint32_t headeraddr = (uint32_t)hcryp->Init.Header; |
NYX | 0:85b3fd62ea1a | 511 | uint32_t loopcounter = 0U; |
NYX | 0:85b3fd62ea1a | 512 | uint32_t bufferidx = 0U; |
NYX | 0:85b3fd62ea1a | 513 | uint8_t blockb0[16U] = {0};/* Block B0 */ |
NYX | 0:85b3fd62ea1a | 514 | uint8_t ctr[16U] = {0}; /* Counter */ |
NYX | 0:85b3fd62ea1a | 515 | uint32_t b0addr = (uint32_t)blockb0; |
NYX | 0:85b3fd62ea1a | 516 | |
NYX | 0:85b3fd62ea1a | 517 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 518 | __HAL_LOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 519 | |
NYX | 0:85b3fd62ea1a | 520 | /* Change the CRYP peripheral state */ |
NYX | 0:85b3fd62ea1a | 521 | hcryp->State = HAL_CRYP_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 522 | |
NYX | 0:85b3fd62ea1a | 523 | /* Check if initialization phase has already been performed */ |
NYX | 0:85b3fd62ea1a | 524 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
NYX | 0:85b3fd62ea1a | 525 | { |
NYX | 0:85b3fd62ea1a | 526 | /************************ Formatting the header block *********************/ |
NYX | 0:85b3fd62ea1a | 527 | if(headersize != 0U) |
NYX | 0:85b3fd62ea1a | 528 | { |
NYX | 0:85b3fd62ea1a | 529 | /* Check that the associated data (or header) length is lower than 2^16 - 2^8 = 65536 - 256 = 65280 */ |
NYX | 0:85b3fd62ea1a | 530 | if(headersize < 65280U) |
NYX | 0:85b3fd62ea1a | 531 | { |
NYX | 0:85b3fd62ea1a | 532 | hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize >> 8) & 0xFF); |
NYX | 0:85b3fd62ea1a | 533 | hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize) & 0xFF); |
NYX | 0:85b3fd62ea1a | 534 | headersize += 2U; |
NYX | 0:85b3fd62ea1a | 535 | } |
NYX | 0:85b3fd62ea1a | 536 | else |
NYX | 0:85b3fd62ea1a | 537 | { |
NYX | 0:85b3fd62ea1a | 538 | /* Header is encoded as 0xff || 0xfe || [headersize]32, i.e., six octets */ |
NYX | 0:85b3fd62ea1a | 539 | hcryp->Init.pScratch[bufferidx++] = 0xFFU; |
NYX | 0:85b3fd62ea1a | 540 | hcryp->Init.pScratch[bufferidx++] = 0xFEU; |
NYX | 0:85b3fd62ea1a | 541 | hcryp->Init.pScratch[bufferidx++] = headersize & 0xff000000U; |
NYX | 0:85b3fd62ea1a | 542 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x00ff0000U; |
NYX | 0:85b3fd62ea1a | 543 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x0000ff00U; |
NYX | 0:85b3fd62ea1a | 544 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x000000ffU; |
NYX | 0:85b3fd62ea1a | 545 | headersize += 6U; |
NYX | 0:85b3fd62ea1a | 546 | } |
NYX | 0:85b3fd62ea1a | 547 | /* Copy the header buffer in internal buffer "hcryp->Init.pScratch" */ |
NYX | 0:85b3fd62ea1a | 548 | for(loopcounter = 0U; loopcounter < headersize; loopcounter++) |
NYX | 0:85b3fd62ea1a | 549 | { |
NYX | 0:85b3fd62ea1a | 550 | hcryp->Init.pScratch[bufferidx++] = hcryp->Init.Header[loopcounter]; |
NYX | 0:85b3fd62ea1a | 551 | } |
NYX | 0:85b3fd62ea1a | 552 | /* Check if the header size is modulo 16 */ |
NYX | 0:85b3fd62ea1a | 553 | if ((headersize % 16U) != 0U) |
NYX | 0:85b3fd62ea1a | 554 | { |
NYX | 0:85b3fd62ea1a | 555 | /* Padd the header buffer with 0s till the hcryp->Init.pScratch length is modulo 16 */ |
NYX | 0:85b3fd62ea1a | 556 | for(loopcounter = headersize; loopcounter <= ((headersize/16U) + 1U) * 16U; loopcounter++) |
NYX | 0:85b3fd62ea1a | 557 | { |
NYX | 0:85b3fd62ea1a | 558 | hcryp->Init.pScratch[loopcounter] = 0U; |
NYX | 0:85b3fd62ea1a | 559 | } |
NYX | 0:85b3fd62ea1a | 560 | /* Set the header size to modulo 16 */ |
NYX | 0:85b3fd62ea1a | 561 | headersize = ((headersize/16U) + 1U) * 16U; |
NYX | 0:85b3fd62ea1a | 562 | } |
NYX | 0:85b3fd62ea1a | 563 | /* Set the pointer headeraddr to hcryp->Init.pScratch */ |
NYX | 0:85b3fd62ea1a | 564 | headeraddr = (uint32_t)hcryp->Init.pScratch; |
NYX | 0:85b3fd62ea1a | 565 | } |
NYX | 0:85b3fd62ea1a | 566 | /*********************** Formatting the block B0 **************************/ |
NYX | 0:85b3fd62ea1a | 567 | if(headersize != 0U) |
NYX | 0:85b3fd62ea1a | 568 | { |
NYX | 0:85b3fd62ea1a | 569 | blockb0[0U] = 0x40U; |
NYX | 0:85b3fd62ea1a | 570 | } |
NYX | 0:85b3fd62ea1a | 571 | /* Flags byte */ |
NYX | 0:85b3fd62ea1a | 572 | /* blockb0[0] |= 0u | (((( (uint8_t) hcryp->Init.TagSize - 2) / 2) & 0x07 ) << 3 ) | ( ( (uint8_t) (15 - hcryp->Init.IVSize) - 1) & 0x07U) */ |
NYX | 0:85b3fd62ea1a | 573 | blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)(((uint8_t)(hcryp->Init.TagSize - (uint8_t)(2))) >> 1U) & (uint8_t)0x07) << 3U); |
NYX | 0:85b3fd62ea1a | 574 | blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)((uint8_t)(15) - hcryp->Init.IVSize) - (uint8_t)1) & (uint8_t)0x07); |
NYX | 0:85b3fd62ea1a | 575 | |
NYX | 0:85b3fd62ea1a | 576 | for (loopcounter = 0U; loopcounter < hcryp->Init.IVSize; loopcounter++) |
NYX | 0:85b3fd62ea1a | 577 | { |
NYX | 0:85b3fd62ea1a | 578 | blockb0[loopcounter+1U] = hcryp->Init.pInitVect[loopcounter]; |
NYX | 0:85b3fd62ea1a | 579 | } |
NYX | 0:85b3fd62ea1a | 580 | for ( ; loopcounter < 13U; loopcounter++) |
NYX | 0:85b3fd62ea1a | 581 | { |
NYX | 0:85b3fd62ea1a | 582 | blockb0[loopcounter+1U] = 0U; |
NYX | 0:85b3fd62ea1a | 583 | } |
NYX | 0:85b3fd62ea1a | 584 | |
NYX | 0:85b3fd62ea1a | 585 | blockb0[14U] = (Size >> 8U); |
NYX | 0:85b3fd62ea1a | 586 | blockb0[15U] = (Size & 0xFFU); |
NYX | 0:85b3fd62ea1a | 587 | |
NYX | 0:85b3fd62ea1a | 588 | /************************* Formatting the initial counter *****************/ |
NYX | 0:85b3fd62ea1a | 589 | /* Byte 0: |
NYX | 0:85b3fd62ea1a | 590 | Bits 7 and 6 are reserved and shall be set to 0 |
NYX | 0:85b3fd62ea1a | 591 | Bits 3, 4, and 5 shall also be set to 0, to ensure that all the counter blocks |
NYX | 0:85b3fd62ea1a | 592 | are distinct from B0 |
NYX | 0:85b3fd62ea1a | 593 | Bits 0, 1, and 2 contain the same encoding of q as in B0 |
NYX | 0:85b3fd62ea1a | 594 | */ |
NYX | 0:85b3fd62ea1a | 595 | ctr[0U] = blockb0[0U] & 0x07U; |
NYX | 0:85b3fd62ea1a | 596 | /* byte 1 to NonceSize is the IV (Nonce) */ |
NYX | 0:85b3fd62ea1a | 597 | for(loopcounter = 1U; loopcounter < hcryp->Init.IVSize + 1U; loopcounter++) |
NYX | 0:85b3fd62ea1a | 598 | { |
NYX | 0:85b3fd62ea1a | 599 | ctr[loopcounter] = blockb0[loopcounter]; |
NYX | 0:85b3fd62ea1a | 600 | } |
NYX | 0:85b3fd62ea1a | 601 | /* Set the LSB to 1 */ |
NYX | 0:85b3fd62ea1a | 602 | ctr[15U] |= 0x01U; |
NYX | 0:85b3fd62ea1a | 603 | |
NYX | 0:85b3fd62ea1a | 604 | /* Set the key */ |
NYX | 0:85b3fd62ea1a | 605 | CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
NYX | 0:85b3fd62ea1a | 606 | |
NYX | 0:85b3fd62ea1a | 607 | /* Set the CRYP peripheral in AES CCM mode */ |
NYX | 0:85b3fd62ea1a | 608 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CCM_ENCRYPT); |
NYX | 0:85b3fd62ea1a | 609 | |
NYX | 0:85b3fd62ea1a | 610 | /* Set the Initialization Vector */ |
NYX | 0:85b3fd62ea1a | 611 | CRYPEx_GCMCCM_SetInitVector(hcryp, ctr); |
NYX | 0:85b3fd62ea1a | 612 | |
NYX | 0:85b3fd62ea1a | 613 | /* Select init phase */ |
NYX | 0:85b3fd62ea1a | 614 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT); |
NYX | 0:85b3fd62ea1a | 615 | |
NYX | 0:85b3fd62ea1a | 616 | b0addr = (uint32_t)blockb0; |
NYX | 0:85b3fd62ea1a | 617 | /* Write the blockb0 block in the IN FIFO */ |
NYX | 0:85b3fd62ea1a | 618 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
NYX | 0:85b3fd62ea1a | 619 | b0addr+=4U; |
NYX | 0:85b3fd62ea1a | 620 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
NYX | 0:85b3fd62ea1a | 621 | b0addr+=4U; |
NYX | 0:85b3fd62ea1a | 622 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
NYX | 0:85b3fd62ea1a | 623 | b0addr+=4U; |
NYX | 0:85b3fd62ea1a | 624 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
NYX | 0:85b3fd62ea1a | 625 | |
NYX | 0:85b3fd62ea1a | 626 | /* Enable the CRYP peripheral */ |
NYX | 0:85b3fd62ea1a | 627 | __HAL_CRYP_ENABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 628 | |
NYX | 0:85b3fd62ea1a | 629 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 630 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 631 | |
NYX | 0:85b3fd62ea1a | 632 | while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN) |
NYX | 0:85b3fd62ea1a | 633 | { |
NYX | 0:85b3fd62ea1a | 634 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 635 | if(Timeout != HAL_MAX_DELAY) |
NYX | 0:85b3fd62ea1a | 636 | { |
NYX | 0:85b3fd62ea1a | 637 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
NYX | 0:85b3fd62ea1a | 638 | { |
NYX | 0:85b3fd62ea1a | 639 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 640 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 641 | |
NYX | 0:85b3fd62ea1a | 642 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 643 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 644 | |
NYX | 0:85b3fd62ea1a | 645 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 646 | } |
NYX | 0:85b3fd62ea1a | 647 | } |
NYX | 0:85b3fd62ea1a | 648 | } |
NYX | 0:85b3fd62ea1a | 649 | /***************************** Header phase *******************************/ |
NYX | 0:85b3fd62ea1a | 650 | if(headersize != 0U) |
NYX | 0:85b3fd62ea1a | 651 | { |
NYX | 0:85b3fd62ea1a | 652 | /* Select header phase */ |
NYX | 0:85b3fd62ea1a | 653 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER); |
NYX | 0:85b3fd62ea1a | 654 | |
NYX | 0:85b3fd62ea1a | 655 | /* Enable the CRYP peripheral */ |
NYX | 0:85b3fd62ea1a | 656 | __HAL_CRYP_ENABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 657 | |
NYX | 0:85b3fd62ea1a | 658 | for(loopcounter = 0U; (loopcounter < headersize); loopcounter+=16U) |
NYX | 0:85b3fd62ea1a | 659 | { |
NYX | 0:85b3fd62ea1a | 660 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 661 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 662 | |
NYX | 0:85b3fd62ea1a | 663 | while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM)) |
NYX | 0:85b3fd62ea1a | 664 | { |
NYX | 0:85b3fd62ea1a | 665 | { |
NYX | 0:85b3fd62ea1a | 666 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 667 | if(Timeout != HAL_MAX_DELAY) |
NYX | 0:85b3fd62ea1a | 668 | { |
NYX | 0:85b3fd62ea1a | 669 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
NYX | 0:85b3fd62ea1a | 670 | { |
NYX | 0:85b3fd62ea1a | 671 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 672 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 673 | |
NYX | 0:85b3fd62ea1a | 674 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 675 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 676 | |
NYX | 0:85b3fd62ea1a | 677 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 678 | } |
NYX | 0:85b3fd62ea1a | 679 | } |
NYX | 0:85b3fd62ea1a | 680 | } |
NYX | 0:85b3fd62ea1a | 681 | } |
NYX | 0:85b3fd62ea1a | 682 | /* Write the header block in the IN FIFO */ |
NYX | 0:85b3fd62ea1a | 683 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
NYX | 0:85b3fd62ea1a | 684 | headeraddr+=4U; |
NYX | 0:85b3fd62ea1a | 685 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
NYX | 0:85b3fd62ea1a | 686 | headeraddr+=4U; |
NYX | 0:85b3fd62ea1a | 687 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
NYX | 0:85b3fd62ea1a | 688 | headeraddr+=4U; |
NYX | 0:85b3fd62ea1a | 689 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
NYX | 0:85b3fd62ea1a | 690 | headeraddr+=4U; |
NYX | 0:85b3fd62ea1a | 691 | } |
NYX | 0:85b3fd62ea1a | 692 | |
NYX | 0:85b3fd62ea1a | 693 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 694 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 695 | |
NYX | 0:85b3fd62ea1a | 696 | while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY) |
NYX | 0:85b3fd62ea1a | 697 | { |
NYX | 0:85b3fd62ea1a | 698 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 699 | if(Timeout != HAL_MAX_DELAY) |
NYX | 0:85b3fd62ea1a | 700 | { |
NYX | 0:85b3fd62ea1a | 701 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
NYX | 0:85b3fd62ea1a | 702 | { |
NYX | 0:85b3fd62ea1a | 703 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 704 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 705 | |
NYX | 0:85b3fd62ea1a | 706 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 707 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 708 | |
NYX | 0:85b3fd62ea1a | 709 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 710 | } |
NYX | 0:85b3fd62ea1a | 711 | } |
NYX | 0:85b3fd62ea1a | 712 | } |
NYX | 0:85b3fd62ea1a | 713 | } |
NYX | 0:85b3fd62ea1a | 714 | /* Save formatted counter into the scratch buffer pScratch */ |
NYX | 0:85b3fd62ea1a | 715 | for(loopcounter = 0U; (loopcounter < 16U); loopcounter++) |
NYX | 0:85b3fd62ea1a | 716 | { |
NYX | 0:85b3fd62ea1a | 717 | hcryp->Init.pScratch[loopcounter] = ctr[loopcounter]; |
NYX | 0:85b3fd62ea1a | 718 | } |
NYX | 0:85b3fd62ea1a | 719 | /* Reset bit 0 */ |
NYX | 0:85b3fd62ea1a | 720 | hcryp->Init.pScratch[15U] &= 0xFEU; |
NYX | 0:85b3fd62ea1a | 721 | |
NYX | 0:85b3fd62ea1a | 722 | /* Select payload phase once the header phase is performed */ |
NYX | 0:85b3fd62ea1a | 723 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD); |
NYX | 0:85b3fd62ea1a | 724 | |
NYX | 0:85b3fd62ea1a | 725 | /* Flush FIFO */ |
NYX | 0:85b3fd62ea1a | 726 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
NYX | 0:85b3fd62ea1a | 727 | |
NYX | 0:85b3fd62ea1a | 728 | /* Enable the CRYP peripheral */ |
NYX | 0:85b3fd62ea1a | 729 | __HAL_CRYP_ENABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 730 | |
NYX | 0:85b3fd62ea1a | 731 | /* Set the phase */ |
NYX | 0:85b3fd62ea1a | 732 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
NYX | 0:85b3fd62ea1a | 733 | } |
NYX | 0:85b3fd62ea1a | 734 | |
NYX | 0:85b3fd62ea1a | 735 | /* Write Plain Data and Get Cypher Data */ |
NYX | 0:85b3fd62ea1a | 736 | if(CRYPEx_GCMCCM_ProcessData(hcryp,pPlainData, Size, pCypherData, Timeout) != HAL_OK) |
NYX | 0:85b3fd62ea1a | 737 | { |
NYX | 0:85b3fd62ea1a | 738 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 739 | } |
NYX | 0:85b3fd62ea1a | 740 | |
NYX | 0:85b3fd62ea1a | 741 | /* Change the CRYP peripheral state */ |
NYX | 0:85b3fd62ea1a | 742 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 743 | |
NYX | 0:85b3fd62ea1a | 744 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 745 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 746 | |
NYX | 0:85b3fd62ea1a | 747 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 748 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 749 | } |
NYX | 0:85b3fd62ea1a | 750 | |
NYX | 0:85b3fd62ea1a | 751 | /** |
NYX | 0:85b3fd62ea1a | 752 | * @brief Initializes the CRYP peripheral in AES GCM encryption mode then |
NYX | 0:85b3fd62ea1a | 753 | * encrypt pPlainData. The cypher data are available in pCypherData. |
NYX | 0:85b3fd62ea1a | 754 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 755 | * the configuration information for CRYP module |
NYX | 0:85b3fd62ea1a | 756 | * @param pPlainData: Pointer to the plaintext buffer |
NYX | 0:85b3fd62ea1a | 757 | * @param Size: Length of the plaintext buffer, must be a multiple of 16 |
NYX | 0:85b3fd62ea1a | 758 | * @param pCypherData: Pointer to the cyphertext buffer |
NYX | 0:85b3fd62ea1a | 759 | * @param Timeout: Timeout duration |
NYX | 0:85b3fd62ea1a | 760 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 761 | */ |
NYX | 0:85b3fd62ea1a | 762 | HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout) |
NYX | 0:85b3fd62ea1a | 763 | { |
NYX | 0:85b3fd62ea1a | 764 | uint32_t tickstart = 0U; |
NYX | 0:85b3fd62ea1a | 765 | |
NYX | 0:85b3fd62ea1a | 766 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 767 | __HAL_LOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 768 | |
NYX | 0:85b3fd62ea1a | 769 | /* Change the CRYP peripheral state */ |
NYX | 0:85b3fd62ea1a | 770 | hcryp->State = HAL_CRYP_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 771 | |
NYX | 0:85b3fd62ea1a | 772 | /* Check if initialization phase has already been performed */ |
NYX | 0:85b3fd62ea1a | 773 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
NYX | 0:85b3fd62ea1a | 774 | { |
NYX | 0:85b3fd62ea1a | 775 | /* Set the key */ |
NYX | 0:85b3fd62ea1a | 776 | CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
NYX | 0:85b3fd62ea1a | 777 | |
NYX | 0:85b3fd62ea1a | 778 | /* Set the CRYP peripheral in AES GCM mode */ |
NYX | 0:85b3fd62ea1a | 779 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_GCM_ENCRYPT); |
NYX | 0:85b3fd62ea1a | 780 | |
NYX | 0:85b3fd62ea1a | 781 | /* Set the Initialization Vector */ |
NYX | 0:85b3fd62ea1a | 782 | CRYPEx_GCMCCM_SetInitVector(hcryp, hcryp->Init.pInitVect); |
NYX | 0:85b3fd62ea1a | 783 | |
NYX | 0:85b3fd62ea1a | 784 | /* Flush FIFO */ |
NYX | 0:85b3fd62ea1a | 785 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
NYX | 0:85b3fd62ea1a | 786 | |
NYX | 0:85b3fd62ea1a | 787 | /* Enable the CRYP peripheral */ |
NYX | 0:85b3fd62ea1a | 788 | __HAL_CRYP_ENABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 789 | |
NYX | 0:85b3fd62ea1a | 790 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 791 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 792 | |
NYX | 0:85b3fd62ea1a | 793 | while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN) |
NYX | 0:85b3fd62ea1a | 794 | { |
NYX | 0:85b3fd62ea1a | 795 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 796 | if(Timeout != HAL_MAX_DELAY) |
NYX | 0:85b3fd62ea1a | 797 | { |
NYX | 0:85b3fd62ea1a | 798 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
NYX | 0:85b3fd62ea1a | 799 | { |
NYX | 0:85b3fd62ea1a | 800 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 801 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 802 | |
NYX | 0:85b3fd62ea1a | 803 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 804 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 805 | |
NYX | 0:85b3fd62ea1a | 806 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 807 | } |
NYX | 0:85b3fd62ea1a | 808 | } |
NYX | 0:85b3fd62ea1a | 809 | } |
NYX | 0:85b3fd62ea1a | 810 | |
NYX | 0:85b3fd62ea1a | 811 | /* Set the header phase */ |
NYX | 0:85b3fd62ea1a | 812 | if(CRYPEx_GCMCCM_SetHeaderPhase(hcryp, hcryp->Init.Header, hcryp->Init.HeaderSize, Timeout) != HAL_OK) |
NYX | 0:85b3fd62ea1a | 813 | { |
NYX | 0:85b3fd62ea1a | 814 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 815 | } |
NYX | 0:85b3fd62ea1a | 816 | |
NYX | 0:85b3fd62ea1a | 817 | /* Disable the CRYP peripheral */ |
NYX | 0:85b3fd62ea1a | 818 | __HAL_CRYP_DISABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 819 | |
NYX | 0:85b3fd62ea1a | 820 | /* Select payload phase once the header phase is performed */ |
NYX | 0:85b3fd62ea1a | 821 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD); |
NYX | 0:85b3fd62ea1a | 822 | |
NYX | 0:85b3fd62ea1a | 823 | /* Flush FIFO */ |
NYX | 0:85b3fd62ea1a | 824 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
NYX | 0:85b3fd62ea1a | 825 | |
NYX | 0:85b3fd62ea1a | 826 | /* Enable the CRYP peripheral */ |
NYX | 0:85b3fd62ea1a | 827 | __HAL_CRYP_ENABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 828 | |
NYX | 0:85b3fd62ea1a | 829 | /* Set the phase */ |
NYX | 0:85b3fd62ea1a | 830 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
NYX | 0:85b3fd62ea1a | 831 | } |
NYX | 0:85b3fd62ea1a | 832 | |
NYX | 0:85b3fd62ea1a | 833 | /* Write Plain Data and Get Cypher Data */ |
NYX | 0:85b3fd62ea1a | 834 | if(CRYPEx_GCMCCM_ProcessData(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK) |
NYX | 0:85b3fd62ea1a | 835 | { |
NYX | 0:85b3fd62ea1a | 836 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 837 | } |
NYX | 0:85b3fd62ea1a | 838 | |
NYX | 0:85b3fd62ea1a | 839 | /* Change the CRYP peripheral state */ |
NYX | 0:85b3fd62ea1a | 840 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 841 | |
NYX | 0:85b3fd62ea1a | 842 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 843 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 844 | |
NYX | 0:85b3fd62ea1a | 845 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 846 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 847 | } |
NYX | 0:85b3fd62ea1a | 848 | |
NYX | 0:85b3fd62ea1a | 849 | /** |
NYX | 0:85b3fd62ea1a | 850 | * @brief Initializes the CRYP peripheral in AES GCM decryption mode then |
NYX | 0:85b3fd62ea1a | 851 | * decrypted pCypherData. The cypher data are available in pPlainData. |
NYX | 0:85b3fd62ea1a | 852 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 853 | * the configuration information for CRYP module |
NYX | 0:85b3fd62ea1a | 854 | * @param pCypherData: Pointer to the cyphertext buffer |
NYX | 0:85b3fd62ea1a | 855 | * @param Size: Length of the cyphertext buffer, must be a multiple of 16 |
NYX | 0:85b3fd62ea1a | 856 | * @param pPlainData: Pointer to the plaintext buffer |
NYX | 0:85b3fd62ea1a | 857 | * @param Timeout: Timeout duration |
NYX | 0:85b3fd62ea1a | 858 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 859 | */ |
NYX | 0:85b3fd62ea1a | 860 | HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout) |
NYX | 0:85b3fd62ea1a | 861 | { |
NYX | 0:85b3fd62ea1a | 862 | uint32_t tickstart = 0U; |
NYX | 0:85b3fd62ea1a | 863 | |
NYX | 0:85b3fd62ea1a | 864 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 865 | __HAL_LOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 866 | |
NYX | 0:85b3fd62ea1a | 867 | /* Change the CRYP peripheral state */ |
NYX | 0:85b3fd62ea1a | 868 | hcryp->State = HAL_CRYP_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 869 | |
NYX | 0:85b3fd62ea1a | 870 | /* Check if initialization phase has already been performed */ |
NYX | 0:85b3fd62ea1a | 871 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
NYX | 0:85b3fd62ea1a | 872 | { |
NYX | 0:85b3fd62ea1a | 873 | /* Set the key */ |
NYX | 0:85b3fd62ea1a | 874 | CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
NYX | 0:85b3fd62ea1a | 875 | |
NYX | 0:85b3fd62ea1a | 876 | /* Set the CRYP peripheral in AES GCM decryption mode */ |
NYX | 0:85b3fd62ea1a | 877 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_GCM_DECRYPT); |
NYX | 0:85b3fd62ea1a | 878 | |
NYX | 0:85b3fd62ea1a | 879 | /* Set the Initialization Vector */ |
NYX | 0:85b3fd62ea1a | 880 | CRYPEx_GCMCCM_SetInitVector(hcryp, hcryp->Init.pInitVect); |
NYX | 0:85b3fd62ea1a | 881 | |
NYX | 0:85b3fd62ea1a | 882 | /* Flush FIFO */ |
NYX | 0:85b3fd62ea1a | 883 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
NYX | 0:85b3fd62ea1a | 884 | |
NYX | 0:85b3fd62ea1a | 885 | /* Enable the CRYP peripheral */ |
NYX | 0:85b3fd62ea1a | 886 | __HAL_CRYP_ENABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 887 | |
NYX | 0:85b3fd62ea1a | 888 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 889 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 890 | |
NYX | 0:85b3fd62ea1a | 891 | while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN) |
NYX | 0:85b3fd62ea1a | 892 | { |
NYX | 0:85b3fd62ea1a | 893 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 894 | if(Timeout != HAL_MAX_DELAY) |
NYX | 0:85b3fd62ea1a | 895 | { |
NYX | 0:85b3fd62ea1a | 896 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
NYX | 0:85b3fd62ea1a | 897 | { |
NYX | 0:85b3fd62ea1a | 898 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 899 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 900 | |
NYX | 0:85b3fd62ea1a | 901 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 902 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 903 | |
NYX | 0:85b3fd62ea1a | 904 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 905 | } |
NYX | 0:85b3fd62ea1a | 906 | } |
NYX | 0:85b3fd62ea1a | 907 | } |
NYX | 0:85b3fd62ea1a | 908 | |
NYX | 0:85b3fd62ea1a | 909 | /* Set the header phase */ |
NYX | 0:85b3fd62ea1a | 910 | if(CRYPEx_GCMCCM_SetHeaderPhase(hcryp, hcryp->Init.Header, hcryp->Init.HeaderSize, Timeout) != HAL_OK) |
NYX | 0:85b3fd62ea1a | 911 | { |
NYX | 0:85b3fd62ea1a | 912 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 913 | } |
NYX | 0:85b3fd62ea1a | 914 | /* Disable the CRYP peripheral */ |
NYX | 0:85b3fd62ea1a | 915 | __HAL_CRYP_DISABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 916 | |
NYX | 0:85b3fd62ea1a | 917 | /* Select payload phase once the header phase is performed */ |
NYX | 0:85b3fd62ea1a | 918 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD); |
NYX | 0:85b3fd62ea1a | 919 | |
NYX | 0:85b3fd62ea1a | 920 | /* Enable the CRYP peripheral */ |
NYX | 0:85b3fd62ea1a | 921 | __HAL_CRYP_ENABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 922 | |
NYX | 0:85b3fd62ea1a | 923 | /* Set the phase */ |
NYX | 0:85b3fd62ea1a | 924 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
NYX | 0:85b3fd62ea1a | 925 | } |
NYX | 0:85b3fd62ea1a | 926 | |
NYX | 0:85b3fd62ea1a | 927 | /* Write Plain Data and Get Cypher Data */ |
NYX | 0:85b3fd62ea1a | 928 | if(CRYPEx_GCMCCM_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK) |
NYX | 0:85b3fd62ea1a | 929 | { |
NYX | 0:85b3fd62ea1a | 930 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 931 | } |
NYX | 0:85b3fd62ea1a | 932 | |
NYX | 0:85b3fd62ea1a | 933 | /* Change the CRYP peripheral state */ |
NYX | 0:85b3fd62ea1a | 934 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 935 | |
NYX | 0:85b3fd62ea1a | 936 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 937 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 938 | |
NYX | 0:85b3fd62ea1a | 939 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 940 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 941 | } |
NYX | 0:85b3fd62ea1a | 942 | |
NYX | 0:85b3fd62ea1a | 943 | /** |
NYX | 0:85b3fd62ea1a | 944 | * @brief Computes the authentication TAG. |
NYX | 0:85b3fd62ea1a | 945 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 946 | * the configuration information for CRYP module |
NYX | 0:85b3fd62ea1a | 947 | * @param Size: Total length of the plain/cyphertext buffer |
NYX | 0:85b3fd62ea1a | 948 | * @param AuthTag: Pointer to the authentication buffer |
NYX | 0:85b3fd62ea1a | 949 | * @param Timeout: Timeout duration |
NYX | 0:85b3fd62ea1a | 950 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 951 | */ |
NYX | 0:85b3fd62ea1a | 952 | HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Finish(CRYP_HandleTypeDef *hcryp, uint32_t Size, uint8_t *AuthTag, uint32_t Timeout) |
NYX | 0:85b3fd62ea1a | 953 | { |
NYX | 0:85b3fd62ea1a | 954 | uint32_t tickstart = 0U; |
NYX | 0:85b3fd62ea1a | 955 | uint64_t headerlength = hcryp->Init.HeaderSize * 8U; /* Header length in bits */ |
NYX | 0:85b3fd62ea1a | 956 | uint64_t inputlength = Size * 8U; /* input length in bits */ |
NYX | 0:85b3fd62ea1a | 957 | uint32_t tagaddr = (uint32_t)AuthTag; |
NYX | 0:85b3fd62ea1a | 958 | |
NYX | 0:85b3fd62ea1a | 959 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 960 | __HAL_LOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 961 | |
NYX | 0:85b3fd62ea1a | 962 | /* Change the CRYP peripheral state */ |
NYX | 0:85b3fd62ea1a | 963 | hcryp->State = HAL_CRYP_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 964 | |
NYX | 0:85b3fd62ea1a | 965 | /* Check if initialization phase has already been performed */ |
NYX | 0:85b3fd62ea1a | 966 | if(hcryp->Phase == HAL_CRYP_PHASE_PROCESS) |
NYX | 0:85b3fd62ea1a | 967 | { |
NYX | 0:85b3fd62ea1a | 968 | /* Change the CRYP phase */ |
NYX | 0:85b3fd62ea1a | 969 | hcryp->Phase = HAL_CRYP_PHASE_FINAL; |
NYX | 0:85b3fd62ea1a | 970 | |
NYX | 0:85b3fd62ea1a | 971 | /* Disable CRYP to start the final phase */ |
NYX | 0:85b3fd62ea1a | 972 | __HAL_CRYP_DISABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 973 | |
NYX | 0:85b3fd62ea1a | 974 | /* Select final phase */ |
NYX | 0:85b3fd62ea1a | 975 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_FINAL); |
NYX | 0:85b3fd62ea1a | 976 | |
NYX | 0:85b3fd62ea1a | 977 | /* Enable the CRYP peripheral */ |
NYX | 0:85b3fd62ea1a | 978 | __HAL_CRYP_ENABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 979 | |
NYX | 0:85b3fd62ea1a | 980 | /* Write the number of bits in header (64 bits) followed by the number of bits |
NYX | 0:85b3fd62ea1a | 981 | in the payload */ |
NYX | 0:85b3fd62ea1a | 982 | if(hcryp->Init.DataType == CRYP_DATATYPE_1B) |
NYX | 0:85b3fd62ea1a | 983 | { |
NYX | 0:85b3fd62ea1a | 984 | hcryp->Instance->DR = __RBIT(headerlength >> 32U); |
NYX | 0:85b3fd62ea1a | 985 | hcryp->Instance->DR = __RBIT(headerlength); |
NYX | 0:85b3fd62ea1a | 986 | hcryp->Instance->DR = __RBIT(inputlength >> 32U); |
NYX | 0:85b3fd62ea1a | 987 | hcryp->Instance->DR = __RBIT(inputlength); |
NYX | 0:85b3fd62ea1a | 988 | } |
NYX | 0:85b3fd62ea1a | 989 | else if(hcryp->Init.DataType == CRYP_DATATYPE_8B) |
NYX | 0:85b3fd62ea1a | 990 | { |
NYX | 0:85b3fd62ea1a | 991 | hcryp->Instance->DR = __REV(headerlength >> 32U); |
NYX | 0:85b3fd62ea1a | 992 | hcryp->Instance->DR = __REV(headerlength); |
NYX | 0:85b3fd62ea1a | 993 | hcryp->Instance->DR = __REV(inputlength >> 32U); |
NYX | 0:85b3fd62ea1a | 994 | hcryp->Instance->DR = __REV(inputlength); |
NYX | 0:85b3fd62ea1a | 995 | } |
NYX | 0:85b3fd62ea1a | 996 | else if(hcryp->Init.DataType == CRYP_DATATYPE_16B) |
NYX | 0:85b3fd62ea1a | 997 | { |
NYX | 0:85b3fd62ea1a | 998 | hcryp->Instance->DR = __ROR((uint32_t)(headerlength >> 32U), 16U); |
NYX | 0:85b3fd62ea1a | 999 | hcryp->Instance->DR = __ROR((uint32_t)headerlength, 16U); |
NYX | 0:85b3fd62ea1a | 1000 | hcryp->Instance->DR = __ROR((uint32_t)(inputlength >> 32U), 16U); |
NYX | 0:85b3fd62ea1a | 1001 | hcryp->Instance->DR = __ROR((uint32_t)inputlength, 16U); |
NYX | 0:85b3fd62ea1a | 1002 | } |
NYX | 0:85b3fd62ea1a | 1003 | else if(hcryp->Init.DataType == CRYP_DATATYPE_32B) |
NYX | 0:85b3fd62ea1a | 1004 | { |
NYX | 0:85b3fd62ea1a | 1005 | hcryp->Instance->DR = (uint32_t)(headerlength >> 32U); |
NYX | 0:85b3fd62ea1a | 1006 | hcryp->Instance->DR = (uint32_t)(headerlength); |
NYX | 0:85b3fd62ea1a | 1007 | hcryp->Instance->DR = (uint32_t)(inputlength >> 32U); |
NYX | 0:85b3fd62ea1a | 1008 | hcryp->Instance->DR = (uint32_t)(inputlength); |
NYX | 0:85b3fd62ea1a | 1009 | } |
NYX | 0:85b3fd62ea1a | 1010 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 1011 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 1012 | |
NYX | 0:85b3fd62ea1a | 1013 | while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE)) |
NYX | 0:85b3fd62ea1a | 1014 | { |
NYX | 0:85b3fd62ea1a | 1015 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 1016 | if(Timeout != HAL_MAX_DELAY) |
NYX | 0:85b3fd62ea1a | 1017 | { |
NYX | 0:85b3fd62ea1a | 1018 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
NYX | 0:85b3fd62ea1a | 1019 | { |
NYX | 0:85b3fd62ea1a | 1020 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 1021 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1022 | |
NYX | 0:85b3fd62ea1a | 1023 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1024 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 1025 | |
NYX | 0:85b3fd62ea1a | 1026 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1027 | } |
NYX | 0:85b3fd62ea1a | 1028 | } |
NYX | 0:85b3fd62ea1a | 1029 | } |
NYX | 0:85b3fd62ea1a | 1030 | |
NYX | 0:85b3fd62ea1a | 1031 | /* Read the Auth TAG in the IN FIFO */ |
NYX | 0:85b3fd62ea1a | 1032 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUT; |
NYX | 0:85b3fd62ea1a | 1033 | tagaddr+=4U; |
NYX | 0:85b3fd62ea1a | 1034 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUT; |
NYX | 0:85b3fd62ea1a | 1035 | tagaddr+=4U; |
NYX | 0:85b3fd62ea1a | 1036 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUT; |
NYX | 0:85b3fd62ea1a | 1037 | tagaddr+=4U; |
NYX | 0:85b3fd62ea1a | 1038 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUT; |
NYX | 0:85b3fd62ea1a | 1039 | } |
NYX | 0:85b3fd62ea1a | 1040 | |
NYX | 0:85b3fd62ea1a | 1041 | /* Change the CRYP peripheral state */ |
NYX | 0:85b3fd62ea1a | 1042 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 1043 | |
NYX | 0:85b3fd62ea1a | 1044 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1045 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 1046 | |
NYX | 0:85b3fd62ea1a | 1047 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 1048 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 1049 | } |
NYX | 0:85b3fd62ea1a | 1050 | |
NYX | 0:85b3fd62ea1a | 1051 | /** |
NYX | 0:85b3fd62ea1a | 1052 | * @brief Computes the authentication TAG for AES CCM mode. |
NYX | 0:85b3fd62ea1a | 1053 | * @note This API is called after HAL_AES_CCM_Encrypt()/HAL_AES_CCM_Decrypt() |
NYX | 0:85b3fd62ea1a | 1054 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 1055 | * the configuration information for CRYP module |
NYX | 0:85b3fd62ea1a | 1056 | * @param AuthTag: Pointer to the authentication buffer |
NYX | 0:85b3fd62ea1a | 1057 | * @param Timeout: Timeout duration |
NYX | 0:85b3fd62ea1a | 1058 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 1059 | */ |
NYX | 0:85b3fd62ea1a | 1060 | HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Finish(CRYP_HandleTypeDef *hcryp, uint8_t *AuthTag, uint32_t Timeout) |
NYX | 0:85b3fd62ea1a | 1061 | { |
NYX | 0:85b3fd62ea1a | 1062 | uint32_t tickstart = 0U; |
NYX | 0:85b3fd62ea1a | 1063 | uint32_t tagaddr = (uint32_t)AuthTag; |
NYX | 0:85b3fd62ea1a | 1064 | uint32_t ctraddr = (uint32_t)hcryp->Init.pScratch; |
NYX | 0:85b3fd62ea1a | 1065 | uint32_t temptag[4U] = {0U}; /* Temporary TAG (MAC) */ |
NYX | 0:85b3fd62ea1a | 1066 | uint32_t loopcounter; |
NYX | 0:85b3fd62ea1a | 1067 | |
NYX | 0:85b3fd62ea1a | 1068 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 1069 | __HAL_LOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 1070 | |
NYX | 0:85b3fd62ea1a | 1071 | /* Change the CRYP peripheral state */ |
NYX | 0:85b3fd62ea1a | 1072 | hcryp->State = HAL_CRYP_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 1073 | |
NYX | 0:85b3fd62ea1a | 1074 | /* Check if initialization phase has already been performed */ |
NYX | 0:85b3fd62ea1a | 1075 | if(hcryp->Phase == HAL_CRYP_PHASE_PROCESS) |
NYX | 0:85b3fd62ea1a | 1076 | { |
NYX | 0:85b3fd62ea1a | 1077 | /* Change the CRYP phase */ |
NYX | 0:85b3fd62ea1a | 1078 | hcryp->Phase = HAL_CRYP_PHASE_FINAL; |
NYX | 0:85b3fd62ea1a | 1079 | |
NYX | 0:85b3fd62ea1a | 1080 | /* Disable CRYP to start the final phase */ |
NYX | 0:85b3fd62ea1a | 1081 | __HAL_CRYP_DISABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 1082 | |
NYX | 0:85b3fd62ea1a | 1083 | /* Select final phase */ |
NYX | 0:85b3fd62ea1a | 1084 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_FINAL); |
NYX | 0:85b3fd62ea1a | 1085 | |
NYX | 0:85b3fd62ea1a | 1086 | /* Enable the CRYP peripheral */ |
NYX | 0:85b3fd62ea1a | 1087 | __HAL_CRYP_ENABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 1088 | |
NYX | 0:85b3fd62ea1a | 1089 | /* Write the counter block in the IN FIFO */ |
NYX | 0:85b3fd62ea1a | 1090 | hcryp->Instance->DR = *(uint32_t*)ctraddr; |
NYX | 0:85b3fd62ea1a | 1091 | ctraddr+=4U; |
NYX | 0:85b3fd62ea1a | 1092 | hcryp->Instance->DR = *(uint32_t*)ctraddr; |
NYX | 0:85b3fd62ea1a | 1093 | ctraddr+=4U; |
NYX | 0:85b3fd62ea1a | 1094 | hcryp->Instance->DR = *(uint32_t*)ctraddr; |
NYX | 0:85b3fd62ea1a | 1095 | ctraddr+=4U; |
NYX | 0:85b3fd62ea1a | 1096 | hcryp->Instance->DR = *(uint32_t*)ctraddr; |
NYX | 0:85b3fd62ea1a | 1097 | |
NYX | 0:85b3fd62ea1a | 1098 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 1099 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 1100 | |
NYX | 0:85b3fd62ea1a | 1101 | while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE)) |
NYX | 0:85b3fd62ea1a | 1102 | { |
NYX | 0:85b3fd62ea1a | 1103 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 1104 | if(Timeout != HAL_MAX_DELAY) |
NYX | 0:85b3fd62ea1a | 1105 | { |
NYX | 0:85b3fd62ea1a | 1106 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
NYX | 0:85b3fd62ea1a | 1107 | { |
NYX | 0:85b3fd62ea1a | 1108 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 1109 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1110 | |
NYX | 0:85b3fd62ea1a | 1111 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1112 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 1113 | |
NYX | 0:85b3fd62ea1a | 1114 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1115 | } |
NYX | 0:85b3fd62ea1a | 1116 | } |
NYX | 0:85b3fd62ea1a | 1117 | } |
NYX | 0:85b3fd62ea1a | 1118 | |
NYX | 0:85b3fd62ea1a | 1119 | /* Read the Auth TAG in the IN FIFO */ |
NYX | 0:85b3fd62ea1a | 1120 | temptag[0U] = hcryp->Instance->DOUT; |
NYX | 0:85b3fd62ea1a | 1121 | temptag[1U] = hcryp->Instance->DOUT; |
NYX | 0:85b3fd62ea1a | 1122 | temptag[2U] = hcryp->Instance->DOUT; |
NYX | 0:85b3fd62ea1a | 1123 | temptag[3U] = hcryp->Instance->DOUT; |
NYX | 0:85b3fd62ea1a | 1124 | } |
NYX | 0:85b3fd62ea1a | 1125 | |
NYX | 0:85b3fd62ea1a | 1126 | /* Copy temporary authentication TAG in user TAG buffer */ |
NYX | 0:85b3fd62ea1a | 1127 | for(loopcounter = 0U; loopcounter < hcryp->Init.TagSize ; loopcounter++) |
NYX | 0:85b3fd62ea1a | 1128 | { |
NYX | 0:85b3fd62ea1a | 1129 | /* Set the authentication TAG buffer */ |
NYX | 0:85b3fd62ea1a | 1130 | *((uint8_t*)tagaddr+loopcounter) = *((uint8_t*)temptag+loopcounter); |
NYX | 0:85b3fd62ea1a | 1131 | } |
NYX | 0:85b3fd62ea1a | 1132 | |
NYX | 0:85b3fd62ea1a | 1133 | /* Change the CRYP peripheral state */ |
NYX | 0:85b3fd62ea1a | 1134 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 1135 | |
NYX | 0:85b3fd62ea1a | 1136 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1137 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 1138 | |
NYX | 0:85b3fd62ea1a | 1139 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 1140 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 1141 | } |
NYX | 0:85b3fd62ea1a | 1142 | |
NYX | 0:85b3fd62ea1a | 1143 | /** |
NYX | 0:85b3fd62ea1a | 1144 | * @brief Initializes the CRYP peripheral in AES CCM decryption mode then |
NYX | 0:85b3fd62ea1a | 1145 | * decrypted pCypherData. The cypher data are available in pPlainData. |
NYX | 0:85b3fd62ea1a | 1146 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 1147 | * the configuration information for CRYP module |
NYX | 0:85b3fd62ea1a | 1148 | * @param pPlainData: Pointer to the plaintext buffer |
NYX | 0:85b3fd62ea1a | 1149 | * @param Size: Length of the plaintext buffer, must be a multiple of 16 |
NYX | 0:85b3fd62ea1a | 1150 | * @param pCypherData: Pointer to the cyphertext buffer |
NYX | 0:85b3fd62ea1a | 1151 | * @param Timeout: Timeout duration |
NYX | 0:85b3fd62ea1a | 1152 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 1153 | */ |
NYX | 0:85b3fd62ea1a | 1154 | HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout) |
NYX | 0:85b3fd62ea1a | 1155 | { |
NYX | 0:85b3fd62ea1a | 1156 | uint32_t tickstart = 0U; |
NYX | 0:85b3fd62ea1a | 1157 | uint32_t headersize = hcryp->Init.HeaderSize; |
NYX | 0:85b3fd62ea1a | 1158 | uint32_t headeraddr = (uint32_t)hcryp->Init.Header; |
NYX | 0:85b3fd62ea1a | 1159 | uint32_t loopcounter = 0U; |
NYX | 0:85b3fd62ea1a | 1160 | uint32_t bufferidx = 0U; |
NYX | 0:85b3fd62ea1a | 1161 | uint8_t blockb0[16U] = {0};/* Block B0 */ |
NYX | 0:85b3fd62ea1a | 1162 | uint8_t ctr[16U] = {0}; /* Counter */ |
NYX | 0:85b3fd62ea1a | 1163 | uint32_t b0addr = (uint32_t)blockb0; |
NYX | 0:85b3fd62ea1a | 1164 | |
NYX | 0:85b3fd62ea1a | 1165 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 1166 | __HAL_LOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 1167 | |
NYX | 0:85b3fd62ea1a | 1168 | /* Change the CRYP peripheral state */ |
NYX | 0:85b3fd62ea1a | 1169 | hcryp->State = HAL_CRYP_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 1170 | |
NYX | 0:85b3fd62ea1a | 1171 | /* Check if initialization phase has already been performed */ |
NYX | 0:85b3fd62ea1a | 1172 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
NYX | 0:85b3fd62ea1a | 1173 | { |
NYX | 0:85b3fd62ea1a | 1174 | /************************ Formatting the header block *********************/ |
NYX | 0:85b3fd62ea1a | 1175 | if(headersize != 0U) |
NYX | 0:85b3fd62ea1a | 1176 | { |
NYX | 0:85b3fd62ea1a | 1177 | /* Check that the associated data (or header) length is lower than 2^16 - 2^8 = 65536 - 256 = 65280 */ |
NYX | 0:85b3fd62ea1a | 1178 | if(headersize < 65280U) |
NYX | 0:85b3fd62ea1a | 1179 | { |
NYX | 0:85b3fd62ea1a | 1180 | hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize >> 8) & 0xFF); |
NYX | 0:85b3fd62ea1a | 1181 | hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize) & 0xFF); |
NYX | 0:85b3fd62ea1a | 1182 | headersize += 2U; |
NYX | 0:85b3fd62ea1a | 1183 | } |
NYX | 0:85b3fd62ea1a | 1184 | else |
NYX | 0:85b3fd62ea1a | 1185 | { |
NYX | 0:85b3fd62ea1a | 1186 | /* Header is encoded as 0xff || 0xfe || [headersize]32, i.e., six octets */ |
NYX | 0:85b3fd62ea1a | 1187 | hcryp->Init.pScratch[bufferidx++] = 0xFFU; |
NYX | 0:85b3fd62ea1a | 1188 | hcryp->Init.pScratch[bufferidx++] = 0xFEU; |
NYX | 0:85b3fd62ea1a | 1189 | hcryp->Init.pScratch[bufferidx++] = headersize & 0xff000000U; |
NYX | 0:85b3fd62ea1a | 1190 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x00ff0000U; |
NYX | 0:85b3fd62ea1a | 1191 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x0000ff00U; |
NYX | 0:85b3fd62ea1a | 1192 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x000000ffU; |
NYX | 0:85b3fd62ea1a | 1193 | headersize += 6U; |
NYX | 0:85b3fd62ea1a | 1194 | } |
NYX | 0:85b3fd62ea1a | 1195 | /* Copy the header buffer in internal buffer "hcryp->Init.pScratch" */ |
NYX | 0:85b3fd62ea1a | 1196 | for(loopcounter = 0U; loopcounter < headersize; loopcounter++) |
NYX | 0:85b3fd62ea1a | 1197 | { |
NYX | 0:85b3fd62ea1a | 1198 | hcryp->Init.pScratch[bufferidx++] = hcryp->Init.Header[loopcounter]; |
NYX | 0:85b3fd62ea1a | 1199 | } |
NYX | 0:85b3fd62ea1a | 1200 | /* Check if the header size is modulo 16 */ |
NYX | 0:85b3fd62ea1a | 1201 | if ((headersize % 16U) != 0U) |
NYX | 0:85b3fd62ea1a | 1202 | { |
NYX | 0:85b3fd62ea1a | 1203 | /* Padd the header buffer with 0s till the hcryp->Init.pScratch length is modulo 16 */ |
NYX | 0:85b3fd62ea1a | 1204 | for(loopcounter = headersize; loopcounter <= ((headersize/16U) + 1U) * 16U; loopcounter++) |
NYX | 0:85b3fd62ea1a | 1205 | { |
NYX | 0:85b3fd62ea1a | 1206 | hcryp->Init.pScratch[loopcounter] = 0U; |
NYX | 0:85b3fd62ea1a | 1207 | } |
NYX | 0:85b3fd62ea1a | 1208 | /* Set the header size to modulo 16 */ |
NYX | 0:85b3fd62ea1a | 1209 | headersize = ((headersize/16U) + 1U) * 16U; |
NYX | 0:85b3fd62ea1a | 1210 | } |
NYX | 0:85b3fd62ea1a | 1211 | /* Set the pointer headeraddr to hcryp->Init.pScratch */ |
NYX | 0:85b3fd62ea1a | 1212 | headeraddr = (uint32_t)hcryp->Init.pScratch; |
NYX | 0:85b3fd62ea1a | 1213 | } |
NYX | 0:85b3fd62ea1a | 1214 | /*********************** Formatting the block B0 **************************/ |
NYX | 0:85b3fd62ea1a | 1215 | if(headersize != 0U) |
NYX | 0:85b3fd62ea1a | 1216 | { |
NYX | 0:85b3fd62ea1a | 1217 | blockb0[0U] = 0x40U; |
NYX | 0:85b3fd62ea1a | 1218 | } |
NYX | 0:85b3fd62ea1a | 1219 | /* Flags byte */ |
NYX | 0:85b3fd62ea1a | 1220 | /* blockb0[0] |= 0u | (((( (uint8_t) hcryp->Init.TagSize - 2) / 2) & 0x07 ) << 3 ) | ( ( (uint8_t) (15 - hcryp->Init.IVSize) - 1) & 0x07U) */ |
NYX | 0:85b3fd62ea1a | 1221 | blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)(((uint8_t)(hcryp->Init.TagSize - (uint8_t)(2U))) >> 1U) & (uint8_t)0x07U) << 3U); |
NYX | 0:85b3fd62ea1a | 1222 | blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)((uint8_t)(15U) - hcryp->Init.IVSize) - (uint8_t)1U) & (uint8_t)0x07U); |
NYX | 0:85b3fd62ea1a | 1223 | |
NYX | 0:85b3fd62ea1a | 1224 | for (loopcounter = 0U; loopcounter < hcryp->Init.IVSize; loopcounter++) |
NYX | 0:85b3fd62ea1a | 1225 | { |
NYX | 0:85b3fd62ea1a | 1226 | blockb0[loopcounter+1U] = hcryp->Init.pInitVect[loopcounter]; |
NYX | 0:85b3fd62ea1a | 1227 | } |
NYX | 0:85b3fd62ea1a | 1228 | for ( ; loopcounter < 13U; loopcounter++) |
NYX | 0:85b3fd62ea1a | 1229 | { |
NYX | 0:85b3fd62ea1a | 1230 | blockb0[loopcounter+1U] = 0U; |
NYX | 0:85b3fd62ea1a | 1231 | } |
NYX | 0:85b3fd62ea1a | 1232 | |
NYX | 0:85b3fd62ea1a | 1233 | blockb0[14U] = (Size >> 8U); |
NYX | 0:85b3fd62ea1a | 1234 | blockb0[15U] = (Size & 0xFFU); |
NYX | 0:85b3fd62ea1a | 1235 | |
NYX | 0:85b3fd62ea1a | 1236 | /************************* Formatting the initial counter *****************/ |
NYX | 0:85b3fd62ea1a | 1237 | /* Byte 0: |
NYX | 0:85b3fd62ea1a | 1238 | Bits 7 and 6 are reserved and shall be set to 0 |
NYX | 0:85b3fd62ea1a | 1239 | Bits 3, 4, and 5 shall also be set to 0, to ensure that all the counter |
NYX | 0:85b3fd62ea1a | 1240 | blocks are distinct from B0 |
NYX | 0:85b3fd62ea1a | 1241 | Bits 0, 1, and 2 contain the same encoding of q as in B0 |
NYX | 0:85b3fd62ea1a | 1242 | */ |
NYX | 0:85b3fd62ea1a | 1243 | ctr[0U] = blockb0[0U] & 0x07U; |
NYX | 0:85b3fd62ea1a | 1244 | /* byte 1 to NonceSize is the IV (Nonce) */ |
NYX | 0:85b3fd62ea1a | 1245 | for(loopcounter = 1U; loopcounter < hcryp->Init.IVSize + 1U; loopcounter++) |
NYX | 0:85b3fd62ea1a | 1246 | { |
NYX | 0:85b3fd62ea1a | 1247 | ctr[loopcounter] = blockb0[loopcounter]; |
NYX | 0:85b3fd62ea1a | 1248 | } |
NYX | 0:85b3fd62ea1a | 1249 | /* Set the LSB to 1 */ |
NYX | 0:85b3fd62ea1a | 1250 | ctr[15U] |= 0x01U; |
NYX | 0:85b3fd62ea1a | 1251 | |
NYX | 0:85b3fd62ea1a | 1252 | /* Set the key */ |
NYX | 0:85b3fd62ea1a | 1253 | CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
NYX | 0:85b3fd62ea1a | 1254 | |
NYX | 0:85b3fd62ea1a | 1255 | /* Set the CRYP peripheral in AES CCM mode */ |
NYX | 0:85b3fd62ea1a | 1256 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CCM_DECRYPT); |
NYX | 0:85b3fd62ea1a | 1257 | |
NYX | 0:85b3fd62ea1a | 1258 | /* Set the Initialization Vector */ |
NYX | 0:85b3fd62ea1a | 1259 | CRYPEx_GCMCCM_SetInitVector(hcryp, ctr); |
NYX | 0:85b3fd62ea1a | 1260 | |
NYX | 0:85b3fd62ea1a | 1261 | /* Select init phase */ |
NYX | 0:85b3fd62ea1a | 1262 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT); |
NYX | 0:85b3fd62ea1a | 1263 | |
NYX | 0:85b3fd62ea1a | 1264 | b0addr = (uint32_t)blockb0; |
NYX | 0:85b3fd62ea1a | 1265 | /* Write the blockb0 block in the IN FIFO */ |
NYX | 0:85b3fd62ea1a | 1266 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
NYX | 0:85b3fd62ea1a | 1267 | b0addr+=4U; |
NYX | 0:85b3fd62ea1a | 1268 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
NYX | 0:85b3fd62ea1a | 1269 | b0addr+=4U; |
NYX | 0:85b3fd62ea1a | 1270 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
NYX | 0:85b3fd62ea1a | 1271 | b0addr+=4U; |
NYX | 0:85b3fd62ea1a | 1272 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
NYX | 0:85b3fd62ea1a | 1273 | |
NYX | 0:85b3fd62ea1a | 1274 | /* Enable the CRYP peripheral */ |
NYX | 0:85b3fd62ea1a | 1275 | __HAL_CRYP_ENABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 1276 | |
NYX | 0:85b3fd62ea1a | 1277 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 1278 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 1279 | |
NYX | 0:85b3fd62ea1a | 1280 | while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN) |
NYX | 0:85b3fd62ea1a | 1281 | { |
NYX | 0:85b3fd62ea1a | 1282 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 1283 | if(Timeout != HAL_MAX_DELAY) |
NYX | 0:85b3fd62ea1a | 1284 | { |
NYX | 0:85b3fd62ea1a | 1285 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
NYX | 0:85b3fd62ea1a | 1286 | { |
NYX | 0:85b3fd62ea1a | 1287 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 1288 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1289 | |
NYX | 0:85b3fd62ea1a | 1290 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1291 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 1292 | |
NYX | 0:85b3fd62ea1a | 1293 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1294 | } |
NYX | 0:85b3fd62ea1a | 1295 | } |
NYX | 0:85b3fd62ea1a | 1296 | } |
NYX | 0:85b3fd62ea1a | 1297 | /***************************** Header phase *******************************/ |
NYX | 0:85b3fd62ea1a | 1298 | if(headersize != 0U) |
NYX | 0:85b3fd62ea1a | 1299 | { |
NYX | 0:85b3fd62ea1a | 1300 | /* Select header phase */ |
NYX | 0:85b3fd62ea1a | 1301 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER); |
NYX | 0:85b3fd62ea1a | 1302 | |
NYX | 0:85b3fd62ea1a | 1303 | /* Enable Crypto processor */ |
NYX | 0:85b3fd62ea1a | 1304 | __HAL_CRYP_ENABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 1305 | |
NYX | 0:85b3fd62ea1a | 1306 | for(loopcounter = 0U; (loopcounter < headersize); loopcounter+=16U) |
NYX | 0:85b3fd62ea1a | 1307 | { |
NYX | 0:85b3fd62ea1a | 1308 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 1309 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 1310 | |
NYX | 0:85b3fd62ea1a | 1311 | while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM)) |
NYX | 0:85b3fd62ea1a | 1312 | { |
NYX | 0:85b3fd62ea1a | 1313 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 1314 | if(Timeout != HAL_MAX_DELAY) |
NYX | 0:85b3fd62ea1a | 1315 | { |
NYX | 0:85b3fd62ea1a | 1316 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
NYX | 0:85b3fd62ea1a | 1317 | { |
NYX | 0:85b3fd62ea1a | 1318 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 1319 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1320 | |
NYX | 0:85b3fd62ea1a | 1321 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1322 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 1323 | |
NYX | 0:85b3fd62ea1a | 1324 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1325 | } |
NYX | 0:85b3fd62ea1a | 1326 | } |
NYX | 0:85b3fd62ea1a | 1327 | } |
NYX | 0:85b3fd62ea1a | 1328 | /* Write the header block in the IN FIFO */ |
NYX | 0:85b3fd62ea1a | 1329 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
NYX | 0:85b3fd62ea1a | 1330 | headeraddr+=4U; |
NYX | 0:85b3fd62ea1a | 1331 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
NYX | 0:85b3fd62ea1a | 1332 | headeraddr+=4U; |
NYX | 0:85b3fd62ea1a | 1333 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
NYX | 0:85b3fd62ea1a | 1334 | headeraddr+=4U; |
NYX | 0:85b3fd62ea1a | 1335 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
NYX | 0:85b3fd62ea1a | 1336 | headeraddr+=4U; |
NYX | 0:85b3fd62ea1a | 1337 | } |
NYX | 0:85b3fd62ea1a | 1338 | |
NYX | 0:85b3fd62ea1a | 1339 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 1340 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 1341 | |
NYX | 0:85b3fd62ea1a | 1342 | while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY) |
NYX | 0:85b3fd62ea1a | 1343 | { |
NYX | 0:85b3fd62ea1a | 1344 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 1345 | if(Timeout != HAL_MAX_DELAY) |
NYX | 0:85b3fd62ea1a | 1346 | { |
NYX | 0:85b3fd62ea1a | 1347 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
NYX | 0:85b3fd62ea1a | 1348 | { |
NYX | 0:85b3fd62ea1a | 1349 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 1350 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1351 | |
NYX | 0:85b3fd62ea1a | 1352 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1353 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 1354 | |
NYX | 0:85b3fd62ea1a | 1355 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1356 | } |
NYX | 0:85b3fd62ea1a | 1357 | } |
NYX | 0:85b3fd62ea1a | 1358 | } |
NYX | 0:85b3fd62ea1a | 1359 | } |
NYX | 0:85b3fd62ea1a | 1360 | /* Save formatted counter into the scratch buffer pScratch */ |
NYX | 0:85b3fd62ea1a | 1361 | for(loopcounter = 0U; (loopcounter < 16U); loopcounter++) |
NYX | 0:85b3fd62ea1a | 1362 | { |
NYX | 0:85b3fd62ea1a | 1363 | hcryp->Init.pScratch[loopcounter] = ctr[loopcounter]; |
NYX | 0:85b3fd62ea1a | 1364 | } |
NYX | 0:85b3fd62ea1a | 1365 | /* Reset bit 0 */ |
NYX | 0:85b3fd62ea1a | 1366 | hcryp->Init.pScratch[15U] &= 0xFEU; |
NYX | 0:85b3fd62ea1a | 1367 | /* Select payload phase once the header phase is performed */ |
NYX | 0:85b3fd62ea1a | 1368 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD); |
NYX | 0:85b3fd62ea1a | 1369 | |
NYX | 0:85b3fd62ea1a | 1370 | /* Flush FIFO */ |
NYX | 0:85b3fd62ea1a | 1371 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
NYX | 0:85b3fd62ea1a | 1372 | |
NYX | 0:85b3fd62ea1a | 1373 | /* Enable the CRYP peripheral */ |
NYX | 0:85b3fd62ea1a | 1374 | __HAL_CRYP_ENABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 1375 | |
NYX | 0:85b3fd62ea1a | 1376 | /* Set the phase */ |
NYX | 0:85b3fd62ea1a | 1377 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
NYX | 0:85b3fd62ea1a | 1378 | } |
NYX | 0:85b3fd62ea1a | 1379 | |
NYX | 0:85b3fd62ea1a | 1380 | /* Write Plain Data and Get Cypher Data */ |
NYX | 0:85b3fd62ea1a | 1381 | if(CRYPEx_GCMCCM_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK) |
NYX | 0:85b3fd62ea1a | 1382 | { |
NYX | 0:85b3fd62ea1a | 1383 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1384 | } |
NYX | 0:85b3fd62ea1a | 1385 | |
NYX | 0:85b3fd62ea1a | 1386 | /* Change the CRYP peripheral state */ |
NYX | 0:85b3fd62ea1a | 1387 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 1388 | |
NYX | 0:85b3fd62ea1a | 1389 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1390 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 1391 | |
NYX | 0:85b3fd62ea1a | 1392 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 1393 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 1394 | } |
NYX | 0:85b3fd62ea1a | 1395 | |
NYX | 0:85b3fd62ea1a | 1396 | /** |
NYX | 0:85b3fd62ea1a | 1397 | * @brief Initializes the CRYP peripheral in AES GCM encryption mode using IT. |
NYX | 0:85b3fd62ea1a | 1398 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 1399 | * the configuration information for CRYP module |
NYX | 0:85b3fd62ea1a | 1400 | * @param pPlainData: Pointer to the plaintext buffer |
NYX | 0:85b3fd62ea1a | 1401 | * @param Size: Length of the plaintext buffer, must be a multiple of 16 |
NYX | 0:85b3fd62ea1a | 1402 | * @param pCypherData: Pointer to the cyphertext buffer |
NYX | 0:85b3fd62ea1a | 1403 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 1404 | */ |
NYX | 0:85b3fd62ea1a | 1405 | HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) |
NYX | 0:85b3fd62ea1a | 1406 | { |
NYX | 0:85b3fd62ea1a | 1407 | uint32_t tickstart = 0U; |
NYX | 0:85b3fd62ea1a | 1408 | uint32_t inputaddr; |
NYX | 0:85b3fd62ea1a | 1409 | uint32_t outputaddr; |
NYX | 0:85b3fd62ea1a | 1410 | |
NYX | 0:85b3fd62ea1a | 1411 | if(hcryp->State == HAL_CRYP_STATE_READY) |
NYX | 0:85b3fd62ea1a | 1412 | { |
NYX | 0:85b3fd62ea1a | 1413 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 1414 | __HAL_LOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 1415 | |
NYX | 0:85b3fd62ea1a | 1416 | /* Get the buffer addresses and sizes */ |
NYX | 0:85b3fd62ea1a | 1417 | hcryp->CrypInCount = Size; |
NYX | 0:85b3fd62ea1a | 1418 | hcryp->pCrypInBuffPtr = pPlainData; |
NYX | 0:85b3fd62ea1a | 1419 | hcryp->pCrypOutBuffPtr = pCypherData; |
NYX | 0:85b3fd62ea1a | 1420 | hcryp->CrypOutCount = Size; |
NYX | 0:85b3fd62ea1a | 1421 | |
NYX | 0:85b3fd62ea1a | 1422 | /* Change the CRYP peripheral state */ |
NYX | 0:85b3fd62ea1a | 1423 | hcryp->State = HAL_CRYP_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 1424 | |
NYX | 0:85b3fd62ea1a | 1425 | /* Check if initialization phase has already been performed */ |
NYX | 0:85b3fd62ea1a | 1426 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
NYX | 0:85b3fd62ea1a | 1427 | { |
NYX | 0:85b3fd62ea1a | 1428 | /* Set the key */ |
NYX | 0:85b3fd62ea1a | 1429 | CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
NYX | 0:85b3fd62ea1a | 1430 | |
NYX | 0:85b3fd62ea1a | 1431 | /* Set the CRYP peripheral in AES GCM mode */ |
NYX | 0:85b3fd62ea1a | 1432 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_GCM_ENCRYPT); |
NYX | 0:85b3fd62ea1a | 1433 | |
NYX | 0:85b3fd62ea1a | 1434 | /* Set the Initialization Vector */ |
NYX | 0:85b3fd62ea1a | 1435 | CRYPEx_GCMCCM_SetInitVector(hcryp, hcryp->Init.pInitVect); |
NYX | 0:85b3fd62ea1a | 1436 | |
NYX | 0:85b3fd62ea1a | 1437 | /* Flush FIFO */ |
NYX | 0:85b3fd62ea1a | 1438 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
NYX | 0:85b3fd62ea1a | 1439 | |
NYX | 0:85b3fd62ea1a | 1440 | /* Enable CRYP to start the init phase */ |
NYX | 0:85b3fd62ea1a | 1441 | __HAL_CRYP_ENABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 1442 | |
NYX | 0:85b3fd62ea1a | 1443 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 1444 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 1445 | |
NYX | 0:85b3fd62ea1a | 1446 | while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN) |
NYX | 0:85b3fd62ea1a | 1447 | { |
NYX | 0:85b3fd62ea1a | 1448 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 1449 | |
NYX | 0:85b3fd62ea1a | 1450 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
NYX | 0:85b3fd62ea1a | 1451 | { |
NYX | 0:85b3fd62ea1a | 1452 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 1453 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1454 | |
NYX | 0:85b3fd62ea1a | 1455 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1456 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 1457 | |
NYX | 0:85b3fd62ea1a | 1458 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1459 | |
NYX | 0:85b3fd62ea1a | 1460 | } |
NYX | 0:85b3fd62ea1a | 1461 | } |
NYX | 0:85b3fd62ea1a | 1462 | |
NYX | 0:85b3fd62ea1a | 1463 | /* Set the header phase */ |
NYX | 0:85b3fd62ea1a | 1464 | if(CRYPEx_GCMCCM_SetHeaderPhase(hcryp, hcryp->Init.Header, hcryp->Init.HeaderSize, 1U) != HAL_OK) |
NYX | 0:85b3fd62ea1a | 1465 | { |
NYX | 0:85b3fd62ea1a | 1466 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1467 | } |
NYX | 0:85b3fd62ea1a | 1468 | /* Disable the CRYP peripheral */ |
NYX | 0:85b3fd62ea1a | 1469 | __HAL_CRYP_DISABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 1470 | |
NYX | 0:85b3fd62ea1a | 1471 | /* Select payload phase once the header phase is performed */ |
NYX | 0:85b3fd62ea1a | 1472 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD); |
NYX | 0:85b3fd62ea1a | 1473 | |
NYX | 0:85b3fd62ea1a | 1474 | /* Flush FIFO */ |
NYX | 0:85b3fd62ea1a | 1475 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
NYX | 0:85b3fd62ea1a | 1476 | |
NYX | 0:85b3fd62ea1a | 1477 | /* Set the phase */ |
NYX | 0:85b3fd62ea1a | 1478 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
NYX | 0:85b3fd62ea1a | 1479 | } |
NYX | 0:85b3fd62ea1a | 1480 | |
NYX | 0:85b3fd62ea1a | 1481 | if(Size != 0U) |
NYX | 0:85b3fd62ea1a | 1482 | { |
NYX | 0:85b3fd62ea1a | 1483 | /* Enable Interrupts */ |
NYX | 0:85b3fd62ea1a | 1484 | __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); |
NYX | 0:85b3fd62ea1a | 1485 | /* Enable the CRYP peripheral */ |
NYX | 0:85b3fd62ea1a | 1486 | __HAL_CRYP_ENABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 1487 | } |
NYX | 0:85b3fd62ea1a | 1488 | else |
NYX | 0:85b3fd62ea1a | 1489 | { |
NYX | 0:85b3fd62ea1a | 1490 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 1491 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 1492 | /* Change the CRYP state and phase */ |
NYX | 0:85b3fd62ea1a | 1493 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 1494 | } |
NYX | 0:85b3fd62ea1a | 1495 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 1496 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 1497 | } |
NYX | 0:85b3fd62ea1a | 1498 | else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) |
NYX | 0:85b3fd62ea1a | 1499 | { |
NYX | 0:85b3fd62ea1a | 1500 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
NYX | 0:85b3fd62ea1a | 1501 | /* Write the Input block in the IN FIFO */ |
NYX | 0:85b3fd62ea1a | 1502 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 1503 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 1504 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 1505 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 1506 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 1507 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 1508 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 1509 | hcryp->pCrypInBuffPtr += 16U; |
NYX | 0:85b3fd62ea1a | 1510 | hcryp->CrypInCount -= 16U; |
NYX | 0:85b3fd62ea1a | 1511 | if(hcryp->CrypInCount == 0U) |
NYX | 0:85b3fd62ea1a | 1512 | { |
NYX | 0:85b3fd62ea1a | 1513 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); |
NYX | 0:85b3fd62ea1a | 1514 | /* Call the Input data transfer complete callback */ |
NYX | 0:85b3fd62ea1a | 1515 | HAL_CRYP_InCpltCallback(hcryp); |
NYX | 0:85b3fd62ea1a | 1516 | } |
NYX | 0:85b3fd62ea1a | 1517 | } |
NYX | 0:85b3fd62ea1a | 1518 | else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) |
NYX | 0:85b3fd62ea1a | 1519 | { |
NYX | 0:85b3fd62ea1a | 1520 | outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; |
NYX | 0:85b3fd62ea1a | 1521 | /* Read the Output block from the Output FIFO */ |
NYX | 0:85b3fd62ea1a | 1522 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
NYX | 0:85b3fd62ea1a | 1523 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 1524 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
NYX | 0:85b3fd62ea1a | 1525 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 1526 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
NYX | 0:85b3fd62ea1a | 1527 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 1528 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
NYX | 0:85b3fd62ea1a | 1529 | hcryp->pCrypOutBuffPtr += 16U; |
NYX | 0:85b3fd62ea1a | 1530 | hcryp->CrypOutCount -= 16U; |
NYX | 0:85b3fd62ea1a | 1531 | if(hcryp->CrypOutCount == 0U) |
NYX | 0:85b3fd62ea1a | 1532 | { |
NYX | 0:85b3fd62ea1a | 1533 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); |
NYX | 0:85b3fd62ea1a | 1534 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1535 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 1536 | /* Change the CRYP peripheral state */ |
NYX | 0:85b3fd62ea1a | 1537 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 1538 | /* Call Input transfer complete callback */ |
NYX | 0:85b3fd62ea1a | 1539 | HAL_CRYP_OutCpltCallback(hcryp); |
NYX | 0:85b3fd62ea1a | 1540 | } |
NYX | 0:85b3fd62ea1a | 1541 | } |
NYX | 0:85b3fd62ea1a | 1542 | |
NYX | 0:85b3fd62ea1a | 1543 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 1544 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 1545 | } |
NYX | 0:85b3fd62ea1a | 1546 | |
NYX | 0:85b3fd62ea1a | 1547 | /** |
NYX | 0:85b3fd62ea1a | 1548 | * @brief Initializes the CRYP peripheral in AES CCM encryption mode using interrupt. |
NYX | 0:85b3fd62ea1a | 1549 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 1550 | * the configuration information for CRYP module |
NYX | 0:85b3fd62ea1a | 1551 | * @param pPlainData: Pointer to the plaintext buffer |
NYX | 0:85b3fd62ea1a | 1552 | * @param Size: Length of the plaintext buffer, must be a multiple of 16 |
NYX | 0:85b3fd62ea1a | 1553 | * @param pCypherData: Pointer to the cyphertext buffer |
NYX | 0:85b3fd62ea1a | 1554 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 1555 | */ |
NYX | 0:85b3fd62ea1a | 1556 | HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) |
NYX | 0:85b3fd62ea1a | 1557 | { |
NYX | 0:85b3fd62ea1a | 1558 | uint32_t tickstart = 0U; |
NYX | 0:85b3fd62ea1a | 1559 | uint32_t inputaddr; |
NYX | 0:85b3fd62ea1a | 1560 | uint32_t outputaddr; |
NYX | 0:85b3fd62ea1a | 1561 | |
NYX | 0:85b3fd62ea1a | 1562 | uint32_t headersize = hcryp->Init.HeaderSize; |
NYX | 0:85b3fd62ea1a | 1563 | uint32_t headeraddr = (uint32_t)hcryp->Init.Header; |
NYX | 0:85b3fd62ea1a | 1564 | uint32_t loopcounter = 0U; |
NYX | 0:85b3fd62ea1a | 1565 | uint32_t bufferidx = 0U; |
NYX | 0:85b3fd62ea1a | 1566 | uint8_t blockb0[16U] = {0};/* Block B0 */ |
NYX | 0:85b3fd62ea1a | 1567 | uint8_t ctr[16U] = {0}; /* Counter */ |
NYX | 0:85b3fd62ea1a | 1568 | uint32_t b0addr = (uint32_t)blockb0; |
NYX | 0:85b3fd62ea1a | 1569 | |
NYX | 0:85b3fd62ea1a | 1570 | if(hcryp->State == HAL_CRYP_STATE_READY) |
NYX | 0:85b3fd62ea1a | 1571 | { |
NYX | 0:85b3fd62ea1a | 1572 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 1573 | __HAL_LOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 1574 | |
NYX | 0:85b3fd62ea1a | 1575 | hcryp->CrypInCount = Size; |
NYX | 0:85b3fd62ea1a | 1576 | hcryp->pCrypInBuffPtr = pPlainData; |
NYX | 0:85b3fd62ea1a | 1577 | hcryp->pCrypOutBuffPtr = pCypherData; |
NYX | 0:85b3fd62ea1a | 1578 | hcryp->CrypOutCount = Size; |
NYX | 0:85b3fd62ea1a | 1579 | |
NYX | 0:85b3fd62ea1a | 1580 | /* Change the CRYP peripheral state */ |
NYX | 0:85b3fd62ea1a | 1581 | hcryp->State = HAL_CRYP_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 1582 | |
NYX | 0:85b3fd62ea1a | 1583 | /* Check if initialization phase has already been performed */ |
NYX | 0:85b3fd62ea1a | 1584 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
NYX | 0:85b3fd62ea1a | 1585 | { |
NYX | 0:85b3fd62ea1a | 1586 | /************************ Formatting the header block *******************/ |
NYX | 0:85b3fd62ea1a | 1587 | if(headersize != 0U) |
NYX | 0:85b3fd62ea1a | 1588 | { |
NYX | 0:85b3fd62ea1a | 1589 | /* Check that the associated data (or header) length is lower than 2^16 - 2^8 = 65536 - 256 = 65280 */ |
NYX | 0:85b3fd62ea1a | 1590 | if(headersize < 65280U) |
NYX | 0:85b3fd62ea1a | 1591 | { |
NYX | 0:85b3fd62ea1a | 1592 | hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize >> 8) & 0xFF); |
NYX | 0:85b3fd62ea1a | 1593 | hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize) & 0xFF); |
NYX | 0:85b3fd62ea1a | 1594 | headersize += 2U; |
NYX | 0:85b3fd62ea1a | 1595 | } |
NYX | 0:85b3fd62ea1a | 1596 | else |
NYX | 0:85b3fd62ea1a | 1597 | { |
NYX | 0:85b3fd62ea1a | 1598 | /* Header is encoded as 0xff || 0xfe || [headersize]32, i.e., six octets */ |
NYX | 0:85b3fd62ea1a | 1599 | hcryp->Init.pScratch[bufferidx++] = 0xFFU; |
NYX | 0:85b3fd62ea1a | 1600 | hcryp->Init.pScratch[bufferidx++] = 0xFEU; |
NYX | 0:85b3fd62ea1a | 1601 | hcryp->Init.pScratch[bufferidx++] = headersize & 0xff000000U; |
NYX | 0:85b3fd62ea1a | 1602 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x00ff0000U; |
NYX | 0:85b3fd62ea1a | 1603 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x0000ff00U; |
NYX | 0:85b3fd62ea1a | 1604 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x000000ffU; |
NYX | 0:85b3fd62ea1a | 1605 | headersize += 6U; |
NYX | 0:85b3fd62ea1a | 1606 | } |
NYX | 0:85b3fd62ea1a | 1607 | /* Copy the header buffer in internal buffer "hcryp->Init.pScratch" */ |
NYX | 0:85b3fd62ea1a | 1608 | for(loopcounter = 0U; loopcounter < headersize; loopcounter++) |
NYX | 0:85b3fd62ea1a | 1609 | { |
NYX | 0:85b3fd62ea1a | 1610 | hcryp->Init.pScratch[bufferidx++] = hcryp->Init.Header[loopcounter]; |
NYX | 0:85b3fd62ea1a | 1611 | } |
NYX | 0:85b3fd62ea1a | 1612 | /* Check if the header size is modulo 16 */ |
NYX | 0:85b3fd62ea1a | 1613 | if ((headersize % 16U) != 0U) |
NYX | 0:85b3fd62ea1a | 1614 | { |
NYX | 0:85b3fd62ea1a | 1615 | /* Padd the header buffer with 0s till the hcryp->Init.pScratch length is modulo 16 */ |
NYX | 0:85b3fd62ea1a | 1616 | for(loopcounter = headersize; loopcounter <= ((headersize/16U) + 1U) * 16U; loopcounter++) |
NYX | 0:85b3fd62ea1a | 1617 | { |
NYX | 0:85b3fd62ea1a | 1618 | hcryp->Init.pScratch[loopcounter] = 0U; |
NYX | 0:85b3fd62ea1a | 1619 | } |
NYX | 0:85b3fd62ea1a | 1620 | /* Set the header size to modulo 16 */ |
NYX | 0:85b3fd62ea1a | 1621 | headersize = ((headersize/16U) + 1U) * 16U; |
NYX | 0:85b3fd62ea1a | 1622 | } |
NYX | 0:85b3fd62ea1a | 1623 | /* Set the pointer headeraddr to hcryp->Init.pScratch */ |
NYX | 0:85b3fd62ea1a | 1624 | headeraddr = (uint32_t)hcryp->Init.pScratch; |
NYX | 0:85b3fd62ea1a | 1625 | } |
NYX | 0:85b3fd62ea1a | 1626 | /*********************** Formatting the block B0 ************************/ |
NYX | 0:85b3fd62ea1a | 1627 | if(headersize != 0U) |
NYX | 0:85b3fd62ea1a | 1628 | { |
NYX | 0:85b3fd62ea1a | 1629 | blockb0[0U] = 0x40U; |
NYX | 0:85b3fd62ea1a | 1630 | } |
NYX | 0:85b3fd62ea1a | 1631 | /* Flags byte */ |
NYX | 0:85b3fd62ea1a | 1632 | /* blockb0[0] |= 0u | (((( (uint8_t) hcryp->Init.TagSize - 2) / 2) & 0x07 ) << 3 ) | ( ( (uint8_t) (15 - hcryp->Init.IVSize) - 1) & 0x07U) */ |
NYX | 0:85b3fd62ea1a | 1633 | blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)(((uint8_t)(hcryp->Init.TagSize - (uint8_t)(2))) >> 1U) & (uint8_t)0x07) << 3U); |
NYX | 0:85b3fd62ea1a | 1634 | blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)((uint8_t)(15) - hcryp->Init.IVSize) - (uint8_t)1) & (uint8_t)0x07); |
NYX | 0:85b3fd62ea1a | 1635 | |
NYX | 0:85b3fd62ea1a | 1636 | for (loopcounter = 0U; loopcounter < hcryp->Init.IVSize; loopcounter++) |
NYX | 0:85b3fd62ea1a | 1637 | { |
NYX | 0:85b3fd62ea1a | 1638 | blockb0[loopcounter+1U] = hcryp->Init.pInitVect[loopcounter]; |
NYX | 0:85b3fd62ea1a | 1639 | } |
NYX | 0:85b3fd62ea1a | 1640 | for ( ; loopcounter < 13U; loopcounter++) |
NYX | 0:85b3fd62ea1a | 1641 | { |
NYX | 0:85b3fd62ea1a | 1642 | blockb0[loopcounter+1U] = 0U; |
NYX | 0:85b3fd62ea1a | 1643 | } |
NYX | 0:85b3fd62ea1a | 1644 | |
NYX | 0:85b3fd62ea1a | 1645 | blockb0[14U] = (Size >> 8U); |
NYX | 0:85b3fd62ea1a | 1646 | blockb0[15U] = (Size & 0xFFU); |
NYX | 0:85b3fd62ea1a | 1647 | |
NYX | 0:85b3fd62ea1a | 1648 | /************************* Formatting the initial counter ***************/ |
NYX | 0:85b3fd62ea1a | 1649 | /* Byte 0: |
NYX | 0:85b3fd62ea1a | 1650 | Bits 7 and 6 are reserved and shall be set to 0 |
NYX | 0:85b3fd62ea1a | 1651 | Bits 3, 4, and 5 shall also be set to 0, to ensure that all the counter |
NYX | 0:85b3fd62ea1a | 1652 | blocks are distinct from B0 |
NYX | 0:85b3fd62ea1a | 1653 | Bits 0, 1, and 2 contain the same encoding of q as in B0 |
NYX | 0:85b3fd62ea1a | 1654 | */ |
NYX | 0:85b3fd62ea1a | 1655 | ctr[0U] = blockb0[0U] & 0x07U; |
NYX | 0:85b3fd62ea1a | 1656 | /* byte 1 to NonceSize is the IV (Nonce) */ |
NYX | 0:85b3fd62ea1a | 1657 | for(loopcounter = 1; loopcounter < hcryp->Init.IVSize + 1U; loopcounter++) |
NYX | 0:85b3fd62ea1a | 1658 | { |
NYX | 0:85b3fd62ea1a | 1659 | ctr[loopcounter] = blockb0[loopcounter]; |
NYX | 0:85b3fd62ea1a | 1660 | } |
NYX | 0:85b3fd62ea1a | 1661 | /* Set the LSB to 1 */ |
NYX | 0:85b3fd62ea1a | 1662 | ctr[15U] |= 0x01U; |
NYX | 0:85b3fd62ea1a | 1663 | |
NYX | 0:85b3fd62ea1a | 1664 | /* Set the key */ |
NYX | 0:85b3fd62ea1a | 1665 | CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
NYX | 0:85b3fd62ea1a | 1666 | |
NYX | 0:85b3fd62ea1a | 1667 | /* Set the CRYP peripheral in AES CCM mode */ |
NYX | 0:85b3fd62ea1a | 1668 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CCM_ENCRYPT); |
NYX | 0:85b3fd62ea1a | 1669 | |
NYX | 0:85b3fd62ea1a | 1670 | /* Set the Initialization Vector */ |
NYX | 0:85b3fd62ea1a | 1671 | CRYPEx_GCMCCM_SetInitVector(hcryp, ctr); |
NYX | 0:85b3fd62ea1a | 1672 | |
NYX | 0:85b3fd62ea1a | 1673 | /* Select init phase */ |
NYX | 0:85b3fd62ea1a | 1674 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT); |
NYX | 0:85b3fd62ea1a | 1675 | |
NYX | 0:85b3fd62ea1a | 1676 | b0addr = (uint32_t)blockb0; |
NYX | 0:85b3fd62ea1a | 1677 | /* Write the blockb0 block in the IN FIFO */ |
NYX | 0:85b3fd62ea1a | 1678 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
NYX | 0:85b3fd62ea1a | 1679 | b0addr+=4U; |
NYX | 0:85b3fd62ea1a | 1680 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
NYX | 0:85b3fd62ea1a | 1681 | b0addr+=4U; |
NYX | 0:85b3fd62ea1a | 1682 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
NYX | 0:85b3fd62ea1a | 1683 | b0addr+=4U; |
NYX | 0:85b3fd62ea1a | 1684 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
NYX | 0:85b3fd62ea1a | 1685 | |
NYX | 0:85b3fd62ea1a | 1686 | /* Enable the CRYP peripheral */ |
NYX | 0:85b3fd62ea1a | 1687 | __HAL_CRYP_ENABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 1688 | |
NYX | 0:85b3fd62ea1a | 1689 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 1690 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 1691 | |
NYX | 0:85b3fd62ea1a | 1692 | while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN) |
NYX | 0:85b3fd62ea1a | 1693 | { |
NYX | 0:85b3fd62ea1a | 1694 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 1695 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
NYX | 0:85b3fd62ea1a | 1696 | { |
NYX | 0:85b3fd62ea1a | 1697 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 1698 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1699 | |
NYX | 0:85b3fd62ea1a | 1700 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1701 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 1702 | |
NYX | 0:85b3fd62ea1a | 1703 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1704 | } |
NYX | 0:85b3fd62ea1a | 1705 | } |
NYX | 0:85b3fd62ea1a | 1706 | /***************************** Header phase *****************************/ |
NYX | 0:85b3fd62ea1a | 1707 | if(headersize != 0U) |
NYX | 0:85b3fd62ea1a | 1708 | { |
NYX | 0:85b3fd62ea1a | 1709 | /* Select header phase */ |
NYX | 0:85b3fd62ea1a | 1710 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER); |
NYX | 0:85b3fd62ea1a | 1711 | |
NYX | 0:85b3fd62ea1a | 1712 | /* Enable Crypto processor */ |
NYX | 0:85b3fd62ea1a | 1713 | __HAL_CRYP_ENABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 1714 | |
NYX | 0:85b3fd62ea1a | 1715 | for(loopcounter = 0U; (loopcounter < headersize); loopcounter+=16U) |
NYX | 0:85b3fd62ea1a | 1716 | { |
NYX | 0:85b3fd62ea1a | 1717 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 1718 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 1719 | |
NYX | 0:85b3fd62ea1a | 1720 | while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM)) |
NYX | 0:85b3fd62ea1a | 1721 | { |
NYX | 0:85b3fd62ea1a | 1722 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 1723 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
NYX | 0:85b3fd62ea1a | 1724 | { |
NYX | 0:85b3fd62ea1a | 1725 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 1726 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1727 | |
NYX | 0:85b3fd62ea1a | 1728 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1729 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 1730 | |
NYX | 0:85b3fd62ea1a | 1731 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1732 | } |
NYX | 0:85b3fd62ea1a | 1733 | } |
NYX | 0:85b3fd62ea1a | 1734 | /* Write the header block in the IN FIFO */ |
NYX | 0:85b3fd62ea1a | 1735 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
NYX | 0:85b3fd62ea1a | 1736 | headeraddr+=4U; |
NYX | 0:85b3fd62ea1a | 1737 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
NYX | 0:85b3fd62ea1a | 1738 | headeraddr+=4U; |
NYX | 0:85b3fd62ea1a | 1739 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
NYX | 0:85b3fd62ea1a | 1740 | headeraddr+=4U; |
NYX | 0:85b3fd62ea1a | 1741 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
NYX | 0:85b3fd62ea1a | 1742 | headeraddr+=4U; |
NYX | 0:85b3fd62ea1a | 1743 | } |
NYX | 0:85b3fd62ea1a | 1744 | |
NYX | 0:85b3fd62ea1a | 1745 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 1746 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 1747 | |
NYX | 0:85b3fd62ea1a | 1748 | while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY) |
NYX | 0:85b3fd62ea1a | 1749 | { |
NYX | 0:85b3fd62ea1a | 1750 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 1751 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
NYX | 0:85b3fd62ea1a | 1752 | { |
NYX | 0:85b3fd62ea1a | 1753 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 1754 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1755 | |
NYX | 0:85b3fd62ea1a | 1756 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1757 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 1758 | |
NYX | 0:85b3fd62ea1a | 1759 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1760 | } |
NYX | 0:85b3fd62ea1a | 1761 | } |
NYX | 0:85b3fd62ea1a | 1762 | } |
NYX | 0:85b3fd62ea1a | 1763 | /* Save formatted counter into the scratch buffer pScratch */ |
NYX | 0:85b3fd62ea1a | 1764 | for(loopcounter = 0U; (loopcounter < 16U); loopcounter++) |
NYX | 0:85b3fd62ea1a | 1765 | { |
NYX | 0:85b3fd62ea1a | 1766 | hcryp->Init.pScratch[loopcounter] = ctr[loopcounter]; |
NYX | 0:85b3fd62ea1a | 1767 | } |
NYX | 0:85b3fd62ea1a | 1768 | /* Reset bit 0 */ |
NYX | 0:85b3fd62ea1a | 1769 | hcryp->Init.pScratch[15U] &= 0xFEU; |
NYX | 0:85b3fd62ea1a | 1770 | |
NYX | 0:85b3fd62ea1a | 1771 | /* Select payload phase once the header phase is performed */ |
NYX | 0:85b3fd62ea1a | 1772 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD); |
NYX | 0:85b3fd62ea1a | 1773 | |
NYX | 0:85b3fd62ea1a | 1774 | /* Flush FIFO */ |
NYX | 0:85b3fd62ea1a | 1775 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
NYX | 0:85b3fd62ea1a | 1776 | |
NYX | 0:85b3fd62ea1a | 1777 | /* Set the phase */ |
NYX | 0:85b3fd62ea1a | 1778 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
NYX | 0:85b3fd62ea1a | 1779 | } |
NYX | 0:85b3fd62ea1a | 1780 | |
NYX | 0:85b3fd62ea1a | 1781 | if(Size != 0U) |
NYX | 0:85b3fd62ea1a | 1782 | { |
NYX | 0:85b3fd62ea1a | 1783 | /* Enable Interrupts */ |
NYX | 0:85b3fd62ea1a | 1784 | __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); |
NYX | 0:85b3fd62ea1a | 1785 | /* Enable the CRYP peripheral */ |
NYX | 0:85b3fd62ea1a | 1786 | __HAL_CRYP_ENABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 1787 | } |
NYX | 0:85b3fd62ea1a | 1788 | else |
NYX | 0:85b3fd62ea1a | 1789 | { |
NYX | 0:85b3fd62ea1a | 1790 | /* Change the CRYP state and phase */ |
NYX | 0:85b3fd62ea1a | 1791 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 1792 | } |
NYX | 0:85b3fd62ea1a | 1793 | |
NYX | 0:85b3fd62ea1a | 1794 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 1795 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 1796 | } |
NYX | 0:85b3fd62ea1a | 1797 | else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) |
NYX | 0:85b3fd62ea1a | 1798 | { |
NYX | 0:85b3fd62ea1a | 1799 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
NYX | 0:85b3fd62ea1a | 1800 | /* Write the Input block in the IN FIFO */ |
NYX | 0:85b3fd62ea1a | 1801 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 1802 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 1803 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 1804 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 1805 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 1806 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 1807 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 1808 | hcryp->pCrypInBuffPtr += 16U; |
NYX | 0:85b3fd62ea1a | 1809 | hcryp->CrypInCount -= 16U; |
NYX | 0:85b3fd62ea1a | 1810 | if(hcryp->CrypInCount == 0U) |
NYX | 0:85b3fd62ea1a | 1811 | { |
NYX | 0:85b3fd62ea1a | 1812 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); |
NYX | 0:85b3fd62ea1a | 1813 | /* Call Input transfer complete callback */ |
NYX | 0:85b3fd62ea1a | 1814 | HAL_CRYP_InCpltCallback(hcryp); |
NYX | 0:85b3fd62ea1a | 1815 | } |
NYX | 0:85b3fd62ea1a | 1816 | } |
NYX | 0:85b3fd62ea1a | 1817 | else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) |
NYX | 0:85b3fd62ea1a | 1818 | { |
NYX | 0:85b3fd62ea1a | 1819 | outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; |
NYX | 0:85b3fd62ea1a | 1820 | /* Read the Output block from the Output FIFO */ |
NYX | 0:85b3fd62ea1a | 1821 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
NYX | 0:85b3fd62ea1a | 1822 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 1823 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
NYX | 0:85b3fd62ea1a | 1824 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 1825 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
NYX | 0:85b3fd62ea1a | 1826 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 1827 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
NYX | 0:85b3fd62ea1a | 1828 | hcryp->pCrypOutBuffPtr += 16U; |
NYX | 0:85b3fd62ea1a | 1829 | hcryp->CrypOutCount -= 16U; |
NYX | 0:85b3fd62ea1a | 1830 | if(hcryp->CrypOutCount == 0U) |
NYX | 0:85b3fd62ea1a | 1831 | { |
NYX | 0:85b3fd62ea1a | 1832 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); |
NYX | 0:85b3fd62ea1a | 1833 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1834 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 1835 | /* Change the CRYP peripheral state */ |
NYX | 0:85b3fd62ea1a | 1836 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 1837 | /* Call Input transfer complete callback */ |
NYX | 0:85b3fd62ea1a | 1838 | HAL_CRYP_OutCpltCallback(hcryp); |
NYX | 0:85b3fd62ea1a | 1839 | } |
NYX | 0:85b3fd62ea1a | 1840 | } |
NYX | 0:85b3fd62ea1a | 1841 | |
NYX | 0:85b3fd62ea1a | 1842 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 1843 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 1844 | } |
NYX | 0:85b3fd62ea1a | 1845 | |
NYX | 0:85b3fd62ea1a | 1846 | /** |
NYX | 0:85b3fd62ea1a | 1847 | * @brief Initializes the CRYP peripheral in AES GCM decryption mode using IT. |
NYX | 0:85b3fd62ea1a | 1848 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 1849 | * the configuration information for CRYP module |
NYX | 0:85b3fd62ea1a | 1850 | * @param pCypherData: Pointer to the cyphertext buffer |
NYX | 0:85b3fd62ea1a | 1851 | * @param Size: Length of the cyphertext buffer, must be a multiple of 16 |
NYX | 0:85b3fd62ea1a | 1852 | * @param pPlainData: Pointer to the plaintext buffer |
NYX | 0:85b3fd62ea1a | 1853 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 1854 | */ |
NYX | 0:85b3fd62ea1a | 1855 | HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) |
NYX | 0:85b3fd62ea1a | 1856 | { |
NYX | 0:85b3fd62ea1a | 1857 | uint32_t tickstart = 0U; |
NYX | 0:85b3fd62ea1a | 1858 | uint32_t inputaddr; |
NYX | 0:85b3fd62ea1a | 1859 | uint32_t outputaddr; |
NYX | 0:85b3fd62ea1a | 1860 | |
NYX | 0:85b3fd62ea1a | 1861 | if(hcryp->State == HAL_CRYP_STATE_READY) |
NYX | 0:85b3fd62ea1a | 1862 | { |
NYX | 0:85b3fd62ea1a | 1863 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 1864 | __HAL_LOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 1865 | |
NYX | 0:85b3fd62ea1a | 1866 | /* Get the buffer addresses and sizes */ |
NYX | 0:85b3fd62ea1a | 1867 | hcryp->CrypInCount = Size; |
NYX | 0:85b3fd62ea1a | 1868 | hcryp->pCrypInBuffPtr = pCypherData; |
NYX | 0:85b3fd62ea1a | 1869 | hcryp->pCrypOutBuffPtr = pPlainData; |
NYX | 0:85b3fd62ea1a | 1870 | hcryp->CrypOutCount = Size; |
NYX | 0:85b3fd62ea1a | 1871 | |
NYX | 0:85b3fd62ea1a | 1872 | /* Change the CRYP peripheral state */ |
NYX | 0:85b3fd62ea1a | 1873 | hcryp->State = HAL_CRYP_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 1874 | |
NYX | 0:85b3fd62ea1a | 1875 | /* Check if initialization phase has already been performed */ |
NYX | 0:85b3fd62ea1a | 1876 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
NYX | 0:85b3fd62ea1a | 1877 | { |
NYX | 0:85b3fd62ea1a | 1878 | /* Set the key */ |
NYX | 0:85b3fd62ea1a | 1879 | CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
NYX | 0:85b3fd62ea1a | 1880 | |
NYX | 0:85b3fd62ea1a | 1881 | /* Set the CRYP peripheral in AES GCM decryption mode */ |
NYX | 0:85b3fd62ea1a | 1882 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_GCM_DECRYPT); |
NYX | 0:85b3fd62ea1a | 1883 | |
NYX | 0:85b3fd62ea1a | 1884 | /* Set the Initialization Vector */ |
NYX | 0:85b3fd62ea1a | 1885 | CRYPEx_GCMCCM_SetInitVector(hcryp, hcryp->Init.pInitVect); |
NYX | 0:85b3fd62ea1a | 1886 | |
NYX | 0:85b3fd62ea1a | 1887 | /* Flush FIFO */ |
NYX | 0:85b3fd62ea1a | 1888 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
NYX | 0:85b3fd62ea1a | 1889 | |
NYX | 0:85b3fd62ea1a | 1890 | /* Enable CRYP to start the init phase */ |
NYX | 0:85b3fd62ea1a | 1891 | __HAL_CRYP_ENABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 1892 | |
NYX | 0:85b3fd62ea1a | 1893 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 1894 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 1895 | |
NYX | 0:85b3fd62ea1a | 1896 | while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN) |
NYX | 0:85b3fd62ea1a | 1897 | { |
NYX | 0:85b3fd62ea1a | 1898 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 1899 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
NYX | 0:85b3fd62ea1a | 1900 | { |
NYX | 0:85b3fd62ea1a | 1901 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 1902 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1903 | |
NYX | 0:85b3fd62ea1a | 1904 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1905 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 1906 | |
NYX | 0:85b3fd62ea1a | 1907 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1908 | } |
NYX | 0:85b3fd62ea1a | 1909 | } |
NYX | 0:85b3fd62ea1a | 1910 | |
NYX | 0:85b3fd62ea1a | 1911 | /* Set the header phase */ |
NYX | 0:85b3fd62ea1a | 1912 | if(CRYPEx_GCMCCM_SetHeaderPhase(hcryp, hcryp->Init.Header, hcryp->Init.HeaderSize, 1U) != HAL_OK) |
NYX | 0:85b3fd62ea1a | 1913 | { |
NYX | 0:85b3fd62ea1a | 1914 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1915 | } |
NYX | 0:85b3fd62ea1a | 1916 | /* Disable the CRYP peripheral */ |
NYX | 0:85b3fd62ea1a | 1917 | __HAL_CRYP_DISABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 1918 | |
NYX | 0:85b3fd62ea1a | 1919 | /* Select payload phase once the header phase is performed */ |
NYX | 0:85b3fd62ea1a | 1920 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD); |
NYX | 0:85b3fd62ea1a | 1921 | |
NYX | 0:85b3fd62ea1a | 1922 | /* Set the phase */ |
NYX | 0:85b3fd62ea1a | 1923 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
NYX | 0:85b3fd62ea1a | 1924 | } |
NYX | 0:85b3fd62ea1a | 1925 | |
NYX | 0:85b3fd62ea1a | 1926 | if(Size != 0U) |
NYX | 0:85b3fd62ea1a | 1927 | { |
NYX | 0:85b3fd62ea1a | 1928 | /* Enable Interrupts */ |
NYX | 0:85b3fd62ea1a | 1929 | __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); |
NYX | 0:85b3fd62ea1a | 1930 | /* Enable the CRYP peripheral */ |
NYX | 0:85b3fd62ea1a | 1931 | __HAL_CRYP_ENABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 1932 | } |
NYX | 0:85b3fd62ea1a | 1933 | else |
NYX | 0:85b3fd62ea1a | 1934 | { |
NYX | 0:85b3fd62ea1a | 1935 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 1936 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 1937 | /* Change the CRYP state and phase */ |
NYX | 0:85b3fd62ea1a | 1938 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 1939 | } |
NYX | 0:85b3fd62ea1a | 1940 | |
NYX | 0:85b3fd62ea1a | 1941 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 1942 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 1943 | } |
NYX | 0:85b3fd62ea1a | 1944 | else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) |
NYX | 0:85b3fd62ea1a | 1945 | { |
NYX | 0:85b3fd62ea1a | 1946 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
NYX | 0:85b3fd62ea1a | 1947 | /* Write the Input block in the IN FIFO */ |
NYX | 0:85b3fd62ea1a | 1948 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 1949 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 1950 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 1951 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 1952 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 1953 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 1954 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 1955 | hcryp->pCrypInBuffPtr += 16U; |
NYX | 0:85b3fd62ea1a | 1956 | hcryp->CrypInCount -= 16U; |
NYX | 0:85b3fd62ea1a | 1957 | if(hcryp->CrypInCount == 0U) |
NYX | 0:85b3fd62ea1a | 1958 | { |
NYX | 0:85b3fd62ea1a | 1959 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); |
NYX | 0:85b3fd62ea1a | 1960 | /* Call the Input data transfer complete callback */ |
NYX | 0:85b3fd62ea1a | 1961 | HAL_CRYP_InCpltCallback(hcryp); |
NYX | 0:85b3fd62ea1a | 1962 | } |
NYX | 0:85b3fd62ea1a | 1963 | } |
NYX | 0:85b3fd62ea1a | 1964 | else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) |
NYX | 0:85b3fd62ea1a | 1965 | { |
NYX | 0:85b3fd62ea1a | 1966 | outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; |
NYX | 0:85b3fd62ea1a | 1967 | /* Read the Output block from the Output FIFO */ |
NYX | 0:85b3fd62ea1a | 1968 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
NYX | 0:85b3fd62ea1a | 1969 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 1970 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
NYX | 0:85b3fd62ea1a | 1971 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 1972 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
NYX | 0:85b3fd62ea1a | 1973 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 1974 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
NYX | 0:85b3fd62ea1a | 1975 | hcryp->pCrypOutBuffPtr += 16U; |
NYX | 0:85b3fd62ea1a | 1976 | hcryp->CrypOutCount -= 16U; |
NYX | 0:85b3fd62ea1a | 1977 | if(hcryp->CrypOutCount == 0U) |
NYX | 0:85b3fd62ea1a | 1978 | { |
NYX | 0:85b3fd62ea1a | 1979 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); |
NYX | 0:85b3fd62ea1a | 1980 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1981 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 1982 | /* Change the CRYP peripheral state */ |
NYX | 0:85b3fd62ea1a | 1983 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 1984 | /* Call Input transfer complete callback */ |
NYX | 0:85b3fd62ea1a | 1985 | HAL_CRYP_OutCpltCallback(hcryp); |
NYX | 0:85b3fd62ea1a | 1986 | } |
NYX | 0:85b3fd62ea1a | 1987 | } |
NYX | 0:85b3fd62ea1a | 1988 | |
NYX | 0:85b3fd62ea1a | 1989 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 1990 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 1991 | } |
NYX | 0:85b3fd62ea1a | 1992 | |
NYX | 0:85b3fd62ea1a | 1993 | /** |
NYX | 0:85b3fd62ea1a | 1994 | * @brief Initializes the CRYP peripheral in AES CCM decryption mode using interrupt |
NYX | 0:85b3fd62ea1a | 1995 | * then decrypted pCypherData. The cypher data are available in pPlainData. |
NYX | 0:85b3fd62ea1a | 1996 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 1997 | * the configuration information for CRYP module |
NYX | 0:85b3fd62ea1a | 1998 | * @param pCypherData: Pointer to the cyphertext buffer |
NYX | 0:85b3fd62ea1a | 1999 | * @param Size: Length of the plaintext buffer, must be a multiple of 16 |
NYX | 0:85b3fd62ea1a | 2000 | * @param pPlainData: Pointer to the plaintext buffer |
NYX | 0:85b3fd62ea1a | 2001 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 2002 | */ |
NYX | 0:85b3fd62ea1a | 2003 | HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) |
NYX | 0:85b3fd62ea1a | 2004 | { |
NYX | 0:85b3fd62ea1a | 2005 | uint32_t inputaddr; |
NYX | 0:85b3fd62ea1a | 2006 | uint32_t outputaddr; |
NYX | 0:85b3fd62ea1a | 2007 | uint32_t tickstart = 0U; |
NYX | 0:85b3fd62ea1a | 2008 | uint32_t headersize = hcryp->Init.HeaderSize; |
NYX | 0:85b3fd62ea1a | 2009 | uint32_t headeraddr = (uint32_t)hcryp->Init.Header; |
NYX | 0:85b3fd62ea1a | 2010 | uint32_t loopcounter = 0U; |
NYX | 0:85b3fd62ea1a | 2011 | uint32_t bufferidx = 0U; |
NYX | 0:85b3fd62ea1a | 2012 | uint8_t blockb0[16U] = {0};/* Block B0 */ |
NYX | 0:85b3fd62ea1a | 2013 | uint8_t ctr[16U] = {0}; /* Counter */ |
NYX | 0:85b3fd62ea1a | 2014 | uint32_t b0addr = (uint32_t)blockb0; |
NYX | 0:85b3fd62ea1a | 2015 | |
NYX | 0:85b3fd62ea1a | 2016 | if(hcryp->State == HAL_CRYP_STATE_READY) |
NYX | 0:85b3fd62ea1a | 2017 | { |
NYX | 0:85b3fd62ea1a | 2018 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 2019 | __HAL_LOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 2020 | |
NYX | 0:85b3fd62ea1a | 2021 | hcryp->CrypInCount = Size; |
NYX | 0:85b3fd62ea1a | 2022 | hcryp->pCrypInBuffPtr = pCypherData; |
NYX | 0:85b3fd62ea1a | 2023 | hcryp->pCrypOutBuffPtr = pPlainData; |
NYX | 0:85b3fd62ea1a | 2024 | hcryp->CrypOutCount = Size; |
NYX | 0:85b3fd62ea1a | 2025 | |
NYX | 0:85b3fd62ea1a | 2026 | /* Change the CRYP peripheral state */ |
NYX | 0:85b3fd62ea1a | 2027 | hcryp->State = HAL_CRYP_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 2028 | |
NYX | 0:85b3fd62ea1a | 2029 | /* Check if initialization phase has already been performed */ |
NYX | 0:85b3fd62ea1a | 2030 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
NYX | 0:85b3fd62ea1a | 2031 | { |
NYX | 0:85b3fd62ea1a | 2032 | /************************ Formatting the header block *******************/ |
NYX | 0:85b3fd62ea1a | 2033 | if(headersize != 0U) |
NYX | 0:85b3fd62ea1a | 2034 | { |
NYX | 0:85b3fd62ea1a | 2035 | /* Check that the associated data (or header) length is lower than 2^16 - 2^8 = 65536 - 256 = 65280 */ |
NYX | 0:85b3fd62ea1a | 2036 | if(headersize < 65280U) |
NYX | 0:85b3fd62ea1a | 2037 | { |
NYX | 0:85b3fd62ea1a | 2038 | hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize >> 8) & 0xFF); |
NYX | 0:85b3fd62ea1a | 2039 | hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize) & 0xFF); |
NYX | 0:85b3fd62ea1a | 2040 | headersize += 2U; |
NYX | 0:85b3fd62ea1a | 2041 | } |
NYX | 0:85b3fd62ea1a | 2042 | else |
NYX | 0:85b3fd62ea1a | 2043 | { |
NYX | 0:85b3fd62ea1a | 2044 | /* Header is encoded as 0xff || 0xfe || [headersize]32, i.e., six octets */ |
NYX | 0:85b3fd62ea1a | 2045 | hcryp->Init.pScratch[bufferidx++] = 0xFFU; |
NYX | 0:85b3fd62ea1a | 2046 | hcryp->Init.pScratch[bufferidx++] = 0xFEU; |
NYX | 0:85b3fd62ea1a | 2047 | hcryp->Init.pScratch[bufferidx++] = headersize & 0xff000000U; |
NYX | 0:85b3fd62ea1a | 2048 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x00ff0000U; |
NYX | 0:85b3fd62ea1a | 2049 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x0000ff00U; |
NYX | 0:85b3fd62ea1a | 2050 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x000000ffU; |
NYX | 0:85b3fd62ea1a | 2051 | headersize += 6U; |
NYX | 0:85b3fd62ea1a | 2052 | } |
NYX | 0:85b3fd62ea1a | 2053 | /* Copy the header buffer in internal buffer "hcryp->Init.pScratch" */ |
NYX | 0:85b3fd62ea1a | 2054 | for(loopcounter = 0U; loopcounter < headersize; loopcounter++) |
NYX | 0:85b3fd62ea1a | 2055 | { |
NYX | 0:85b3fd62ea1a | 2056 | hcryp->Init.pScratch[bufferidx++] = hcryp->Init.Header[loopcounter]; |
NYX | 0:85b3fd62ea1a | 2057 | } |
NYX | 0:85b3fd62ea1a | 2058 | /* Check if the header size is modulo 16 */ |
NYX | 0:85b3fd62ea1a | 2059 | if ((headersize % 16U) != 0U) |
NYX | 0:85b3fd62ea1a | 2060 | { |
NYX | 0:85b3fd62ea1a | 2061 | /* Padd the header buffer with 0s till the hcryp->Init.pScratch length is modulo 16 */ |
NYX | 0:85b3fd62ea1a | 2062 | for(loopcounter = headersize; loopcounter <= ((headersize/16U) + 1U) * 16U; loopcounter++) |
NYX | 0:85b3fd62ea1a | 2063 | { |
NYX | 0:85b3fd62ea1a | 2064 | hcryp->Init.pScratch[loopcounter] = 0U; |
NYX | 0:85b3fd62ea1a | 2065 | } |
NYX | 0:85b3fd62ea1a | 2066 | /* Set the header size to modulo 16 */ |
NYX | 0:85b3fd62ea1a | 2067 | headersize = ((headersize/16U) + 1U) * 16U; |
NYX | 0:85b3fd62ea1a | 2068 | } |
NYX | 0:85b3fd62ea1a | 2069 | /* Set the pointer headeraddr to hcryp->Init.pScratch */ |
NYX | 0:85b3fd62ea1a | 2070 | headeraddr = (uint32_t)hcryp->Init.pScratch; |
NYX | 0:85b3fd62ea1a | 2071 | } |
NYX | 0:85b3fd62ea1a | 2072 | /*********************** Formatting the block B0 ************************/ |
NYX | 0:85b3fd62ea1a | 2073 | if(headersize != 0U) |
NYX | 0:85b3fd62ea1a | 2074 | { |
NYX | 0:85b3fd62ea1a | 2075 | blockb0[0U] = 0x40U; |
NYX | 0:85b3fd62ea1a | 2076 | } |
NYX | 0:85b3fd62ea1a | 2077 | /* Flags byte */ |
NYX | 0:85b3fd62ea1a | 2078 | /* blockb0[0] |= 0u | (((( (uint8_t) hcryp->Init.TagSize - 2) / 2) & 0x07 ) << 3 ) | ( ( (uint8_t) (15 - hcryp->Init.IVSize) - 1) & 0x07U) */ |
NYX | 0:85b3fd62ea1a | 2079 | blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)(((uint8_t)(hcryp->Init.TagSize - (uint8_t)(2))) >> 1U) & (uint8_t)0x07) << 3U); |
NYX | 0:85b3fd62ea1a | 2080 | blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)((uint8_t)(15) - hcryp->Init.IVSize) - (uint8_t)1) & (uint8_t)0x07); |
NYX | 0:85b3fd62ea1a | 2081 | |
NYX | 0:85b3fd62ea1a | 2082 | for (loopcounter = 0U; loopcounter < hcryp->Init.IVSize; loopcounter++) |
NYX | 0:85b3fd62ea1a | 2083 | { |
NYX | 0:85b3fd62ea1a | 2084 | blockb0[loopcounter+1U] = hcryp->Init.pInitVect[loopcounter]; |
NYX | 0:85b3fd62ea1a | 2085 | } |
NYX | 0:85b3fd62ea1a | 2086 | for ( ; loopcounter < 13U; loopcounter++) |
NYX | 0:85b3fd62ea1a | 2087 | { |
NYX | 0:85b3fd62ea1a | 2088 | blockb0[loopcounter+1U] = 0U; |
NYX | 0:85b3fd62ea1a | 2089 | } |
NYX | 0:85b3fd62ea1a | 2090 | |
NYX | 0:85b3fd62ea1a | 2091 | blockb0[14U] = (Size >> 8U); |
NYX | 0:85b3fd62ea1a | 2092 | blockb0[15U] = (Size & 0xFFU); |
NYX | 0:85b3fd62ea1a | 2093 | |
NYX | 0:85b3fd62ea1a | 2094 | /************************* Formatting the initial counter ***************/ |
NYX | 0:85b3fd62ea1a | 2095 | /* Byte 0: |
NYX | 0:85b3fd62ea1a | 2096 | Bits 7 and 6 are reserved and shall be set to 0 |
NYX | 0:85b3fd62ea1a | 2097 | Bits 3, 4, and 5 shall also be set to 0, to ensure that all the counter |
NYX | 0:85b3fd62ea1a | 2098 | blocks are distinct from B0 |
NYX | 0:85b3fd62ea1a | 2099 | Bits 0, 1, and 2 contain the same encoding of q as in B0 |
NYX | 0:85b3fd62ea1a | 2100 | */ |
NYX | 0:85b3fd62ea1a | 2101 | ctr[0U] = blockb0[0U] & 0x07U; |
NYX | 0:85b3fd62ea1a | 2102 | /* byte 1 to NonceSize is the IV (Nonce) */ |
NYX | 0:85b3fd62ea1a | 2103 | for(loopcounter = 1U; loopcounter < hcryp->Init.IVSize + 1U; loopcounter++) |
NYX | 0:85b3fd62ea1a | 2104 | { |
NYX | 0:85b3fd62ea1a | 2105 | ctr[loopcounter] = blockb0[loopcounter]; |
NYX | 0:85b3fd62ea1a | 2106 | } |
NYX | 0:85b3fd62ea1a | 2107 | /* Set the LSB to 1 */ |
NYX | 0:85b3fd62ea1a | 2108 | ctr[15U] |= 0x01U; |
NYX | 0:85b3fd62ea1a | 2109 | |
NYX | 0:85b3fd62ea1a | 2110 | /* Set the key */ |
NYX | 0:85b3fd62ea1a | 2111 | CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
NYX | 0:85b3fd62ea1a | 2112 | |
NYX | 0:85b3fd62ea1a | 2113 | /* Set the CRYP peripheral in AES CCM mode */ |
NYX | 0:85b3fd62ea1a | 2114 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CCM_DECRYPT); |
NYX | 0:85b3fd62ea1a | 2115 | |
NYX | 0:85b3fd62ea1a | 2116 | /* Set the Initialization Vector */ |
NYX | 0:85b3fd62ea1a | 2117 | CRYPEx_GCMCCM_SetInitVector(hcryp, ctr); |
NYX | 0:85b3fd62ea1a | 2118 | |
NYX | 0:85b3fd62ea1a | 2119 | /* Select init phase */ |
NYX | 0:85b3fd62ea1a | 2120 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT); |
NYX | 0:85b3fd62ea1a | 2121 | |
NYX | 0:85b3fd62ea1a | 2122 | b0addr = (uint32_t)blockb0; |
NYX | 0:85b3fd62ea1a | 2123 | /* Write the blockb0 block in the IN FIFO */ |
NYX | 0:85b3fd62ea1a | 2124 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
NYX | 0:85b3fd62ea1a | 2125 | b0addr+=4U; |
NYX | 0:85b3fd62ea1a | 2126 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
NYX | 0:85b3fd62ea1a | 2127 | b0addr+=4U; |
NYX | 0:85b3fd62ea1a | 2128 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
NYX | 0:85b3fd62ea1a | 2129 | b0addr+=4U; |
NYX | 0:85b3fd62ea1a | 2130 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
NYX | 0:85b3fd62ea1a | 2131 | |
NYX | 0:85b3fd62ea1a | 2132 | /* Enable the CRYP peripheral */ |
NYX | 0:85b3fd62ea1a | 2133 | __HAL_CRYP_ENABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 2134 | |
NYX | 0:85b3fd62ea1a | 2135 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 2136 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 2137 | |
NYX | 0:85b3fd62ea1a | 2138 | while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN) |
NYX | 0:85b3fd62ea1a | 2139 | { |
NYX | 0:85b3fd62ea1a | 2140 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 2141 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
NYX | 0:85b3fd62ea1a | 2142 | { |
NYX | 0:85b3fd62ea1a | 2143 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 2144 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 2145 | |
NYX | 0:85b3fd62ea1a | 2146 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 2147 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 2148 | |
NYX | 0:85b3fd62ea1a | 2149 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 2150 | } |
NYX | 0:85b3fd62ea1a | 2151 | } |
NYX | 0:85b3fd62ea1a | 2152 | /***************************** Header phase *****************************/ |
NYX | 0:85b3fd62ea1a | 2153 | if(headersize != 0U) |
NYX | 0:85b3fd62ea1a | 2154 | { |
NYX | 0:85b3fd62ea1a | 2155 | /* Select header phase */ |
NYX | 0:85b3fd62ea1a | 2156 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER); |
NYX | 0:85b3fd62ea1a | 2157 | |
NYX | 0:85b3fd62ea1a | 2158 | /* Enable Crypto processor */ |
NYX | 0:85b3fd62ea1a | 2159 | __HAL_CRYP_ENABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 2160 | |
NYX | 0:85b3fd62ea1a | 2161 | for(loopcounter = 0U; (loopcounter < headersize); loopcounter+=16U) |
NYX | 0:85b3fd62ea1a | 2162 | { |
NYX | 0:85b3fd62ea1a | 2163 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 2164 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 2165 | |
NYX | 0:85b3fd62ea1a | 2166 | while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM)) |
NYX | 0:85b3fd62ea1a | 2167 | { |
NYX | 0:85b3fd62ea1a | 2168 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 2169 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
NYX | 0:85b3fd62ea1a | 2170 | { |
NYX | 0:85b3fd62ea1a | 2171 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 2172 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 2173 | |
NYX | 0:85b3fd62ea1a | 2174 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 2175 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 2176 | |
NYX | 0:85b3fd62ea1a | 2177 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 2178 | } |
NYX | 0:85b3fd62ea1a | 2179 | } |
NYX | 0:85b3fd62ea1a | 2180 | /* Write the header block in the IN FIFO */ |
NYX | 0:85b3fd62ea1a | 2181 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
NYX | 0:85b3fd62ea1a | 2182 | headeraddr+=4U; |
NYX | 0:85b3fd62ea1a | 2183 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
NYX | 0:85b3fd62ea1a | 2184 | headeraddr+=4U; |
NYX | 0:85b3fd62ea1a | 2185 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
NYX | 0:85b3fd62ea1a | 2186 | headeraddr+=4U; |
NYX | 0:85b3fd62ea1a | 2187 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
NYX | 0:85b3fd62ea1a | 2188 | headeraddr+=4U; |
NYX | 0:85b3fd62ea1a | 2189 | } |
NYX | 0:85b3fd62ea1a | 2190 | |
NYX | 0:85b3fd62ea1a | 2191 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 2192 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 2193 | |
NYX | 0:85b3fd62ea1a | 2194 | while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY) |
NYX | 0:85b3fd62ea1a | 2195 | { |
NYX | 0:85b3fd62ea1a | 2196 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 2197 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
NYX | 0:85b3fd62ea1a | 2198 | { |
NYX | 0:85b3fd62ea1a | 2199 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 2200 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 2201 | |
NYX | 0:85b3fd62ea1a | 2202 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 2203 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 2204 | |
NYX | 0:85b3fd62ea1a | 2205 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 2206 | } |
NYX | 0:85b3fd62ea1a | 2207 | } |
NYX | 0:85b3fd62ea1a | 2208 | } |
NYX | 0:85b3fd62ea1a | 2209 | /* Save formatted counter into the scratch buffer pScratch */ |
NYX | 0:85b3fd62ea1a | 2210 | for(loopcounter = 0U; (loopcounter < 16U); loopcounter++) |
NYX | 0:85b3fd62ea1a | 2211 | { |
NYX | 0:85b3fd62ea1a | 2212 | hcryp->Init.pScratch[loopcounter] = ctr[loopcounter]; |
NYX | 0:85b3fd62ea1a | 2213 | } |
NYX | 0:85b3fd62ea1a | 2214 | /* Reset bit 0 */ |
NYX | 0:85b3fd62ea1a | 2215 | hcryp->Init.pScratch[15U] &= 0xFEU; |
NYX | 0:85b3fd62ea1a | 2216 | /* Select payload phase once the header phase is performed */ |
NYX | 0:85b3fd62ea1a | 2217 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD); |
NYX | 0:85b3fd62ea1a | 2218 | |
NYX | 0:85b3fd62ea1a | 2219 | /* Flush FIFO */ |
NYX | 0:85b3fd62ea1a | 2220 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
NYX | 0:85b3fd62ea1a | 2221 | |
NYX | 0:85b3fd62ea1a | 2222 | /* Set the phase */ |
NYX | 0:85b3fd62ea1a | 2223 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
NYX | 0:85b3fd62ea1a | 2224 | } |
NYX | 0:85b3fd62ea1a | 2225 | |
NYX | 0:85b3fd62ea1a | 2226 | /* Enable Interrupts */ |
NYX | 0:85b3fd62ea1a | 2227 | __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); |
NYX | 0:85b3fd62ea1a | 2228 | |
NYX | 0:85b3fd62ea1a | 2229 | /* Enable the CRYP peripheral */ |
NYX | 0:85b3fd62ea1a | 2230 | __HAL_CRYP_ENABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 2231 | |
NYX | 0:85b3fd62ea1a | 2232 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 2233 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 2234 | } |
NYX | 0:85b3fd62ea1a | 2235 | else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) |
NYX | 0:85b3fd62ea1a | 2236 | { |
NYX | 0:85b3fd62ea1a | 2237 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
NYX | 0:85b3fd62ea1a | 2238 | /* Write the Input block in the IN FIFO */ |
NYX | 0:85b3fd62ea1a | 2239 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 2240 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 2241 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 2242 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 2243 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 2244 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 2245 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 2246 | hcryp->pCrypInBuffPtr += 16U; |
NYX | 0:85b3fd62ea1a | 2247 | hcryp->CrypInCount -= 16U; |
NYX | 0:85b3fd62ea1a | 2248 | if(hcryp->CrypInCount == 0U) |
NYX | 0:85b3fd62ea1a | 2249 | { |
NYX | 0:85b3fd62ea1a | 2250 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); |
NYX | 0:85b3fd62ea1a | 2251 | /* Call the Input data transfer complete callback */ |
NYX | 0:85b3fd62ea1a | 2252 | HAL_CRYP_InCpltCallback(hcryp); |
NYX | 0:85b3fd62ea1a | 2253 | } |
NYX | 0:85b3fd62ea1a | 2254 | } |
NYX | 0:85b3fd62ea1a | 2255 | else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) |
NYX | 0:85b3fd62ea1a | 2256 | { |
NYX | 0:85b3fd62ea1a | 2257 | outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; |
NYX | 0:85b3fd62ea1a | 2258 | /* Read the Output block from the Output FIFO */ |
NYX | 0:85b3fd62ea1a | 2259 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
NYX | 0:85b3fd62ea1a | 2260 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 2261 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
NYX | 0:85b3fd62ea1a | 2262 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 2263 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
NYX | 0:85b3fd62ea1a | 2264 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 2265 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
NYX | 0:85b3fd62ea1a | 2266 | hcryp->pCrypOutBuffPtr += 16U; |
NYX | 0:85b3fd62ea1a | 2267 | hcryp->CrypOutCount -= 16U; |
NYX | 0:85b3fd62ea1a | 2268 | if(hcryp->CrypOutCount == 0U) |
NYX | 0:85b3fd62ea1a | 2269 | { |
NYX | 0:85b3fd62ea1a | 2270 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); |
NYX | 0:85b3fd62ea1a | 2271 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 2272 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 2273 | /* Change the CRYP peripheral state */ |
NYX | 0:85b3fd62ea1a | 2274 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 2275 | /* Call Input transfer complete callback */ |
NYX | 0:85b3fd62ea1a | 2276 | HAL_CRYP_OutCpltCallback(hcryp); |
NYX | 0:85b3fd62ea1a | 2277 | } |
NYX | 0:85b3fd62ea1a | 2278 | } |
NYX | 0:85b3fd62ea1a | 2279 | |
NYX | 0:85b3fd62ea1a | 2280 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 2281 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 2282 | } |
NYX | 0:85b3fd62ea1a | 2283 | |
NYX | 0:85b3fd62ea1a | 2284 | /** |
NYX | 0:85b3fd62ea1a | 2285 | * @brief Initializes the CRYP peripheral in AES GCM encryption mode using DMA. |
NYX | 0:85b3fd62ea1a | 2286 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 2287 | * the configuration information for CRYP module |
NYX | 0:85b3fd62ea1a | 2288 | * @param pPlainData: Pointer to the plaintext buffer |
NYX | 0:85b3fd62ea1a | 2289 | * @param Size: Length of the plaintext buffer, must be a multiple of 16 |
NYX | 0:85b3fd62ea1a | 2290 | * @param pCypherData: Pointer to the cyphertext buffer |
NYX | 0:85b3fd62ea1a | 2291 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 2292 | */ |
NYX | 0:85b3fd62ea1a | 2293 | HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) |
NYX | 0:85b3fd62ea1a | 2294 | { |
NYX | 0:85b3fd62ea1a | 2295 | uint32_t tickstart = 0U; |
NYX | 0:85b3fd62ea1a | 2296 | uint32_t inputaddr; |
NYX | 0:85b3fd62ea1a | 2297 | uint32_t outputaddr; |
NYX | 0:85b3fd62ea1a | 2298 | |
NYX | 0:85b3fd62ea1a | 2299 | if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) |
NYX | 0:85b3fd62ea1a | 2300 | { |
NYX | 0:85b3fd62ea1a | 2301 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 2302 | __HAL_LOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 2303 | |
NYX | 0:85b3fd62ea1a | 2304 | inputaddr = (uint32_t)pPlainData; |
NYX | 0:85b3fd62ea1a | 2305 | outputaddr = (uint32_t)pCypherData; |
NYX | 0:85b3fd62ea1a | 2306 | |
NYX | 0:85b3fd62ea1a | 2307 | /* Change the CRYP peripheral state */ |
NYX | 0:85b3fd62ea1a | 2308 | hcryp->State = HAL_CRYP_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 2309 | |
NYX | 0:85b3fd62ea1a | 2310 | /* Check if initialization phase has already been performed */ |
NYX | 0:85b3fd62ea1a | 2311 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
NYX | 0:85b3fd62ea1a | 2312 | { |
NYX | 0:85b3fd62ea1a | 2313 | /* Set the key */ |
NYX | 0:85b3fd62ea1a | 2314 | CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
NYX | 0:85b3fd62ea1a | 2315 | |
NYX | 0:85b3fd62ea1a | 2316 | /* Set the CRYP peripheral in AES GCM mode */ |
NYX | 0:85b3fd62ea1a | 2317 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_GCM_ENCRYPT); |
NYX | 0:85b3fd62ea1a | 2318 | |
NYX | 0:85b3fd62ea1a | 2319 | /* Set the Initialization Vector */ |
NYX | 0:85b3fd62ea1a | 2320 | CRYPEx_GCMCCM_SetInitVector(hcryp, hcryp->Init.pInitVect); |
NYX | 0:85b3fd62ea1a | 2321 | |
NYX | 0:85b3fd62ea1a | 2322 | /* Flush FIFO */ |
NYX | 0:85b3fd62ea1a | 2323 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
NYX | 0:85b3fd62ea1a | 2324 | |
NYX | 0:85b3fd62ea1a | 2325 | /* Enable CRYP to start the init phase */ |
NYX | 0:85b3fd62ea1a | 2326 | __HAL_CRYP_ENABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 2327 | |
NYX | 0:85b3fd62ea1a | 2328 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 2329 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 2330 | |
NYX | 0:85b3fd62ea1a | 2331 | while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN) |
NYX | 0:85b3fd62ea1a | 2332 | { |
NYX | 0:85b3fd62ea1a | 2333 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 2334 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
NYX | 0:85b3fd62ea1a | 2335 | { |
NYX | 0:85b3fd62ea1a | 2336 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 2337 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 2338 | |
NYX | 0:85b3fd62ea1a | 2339 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 2340 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 2341 | |
NYX | 0:85b3fd62ea1a | 2342 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 2343 | } |
NYX | 0:85b3fd62ea1a | 2344 | } |
NYX | 0:85b3fd62ea1a | 2345 | /* Flush FIFO */ |
NYX | 0:85b3fd62ea1a | 2346 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
NYX | 0:85b3fd62ea1a | 2347 | |
NYX | 0:85b3fd62ea1a | 2348 | /* Set the header phase */ |
NYX | 0:85b3fd62ea1a | 2349 | if(CRYPEx_GCMCCM_SetHeaderPhase(hcryp, hcryp->Init.Header, hcryp->Init.HeaderSize, 1U) != HAL_OK) |
NYX | 0:85b3fd62ea1a | 2350 | { |
NYX | 0:85b3fd62ea1a | 2351 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 2352 | } |
NYX | 0:85b3fd62ea1a | 2353 | /* Disable the CRYP peripheral */ |
NYX | 0:85b3fd62ea1a | 2354 | __HAL_CRYP_DISABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 2355 | |
NYX | 0:85b3fd62ea1a | 2356 | /* Select payload phase once the header phase is performed */ |
NYX | 0:85b3fd62ea1a | 2357 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD); |
NYX | 0:85b3fd62ea1a | 2358 | |
NYX | 0:85b3fd62ea1a | 2359 | /* Flush FIFO */ |
NYX | 0:85b3fd62ea1a | 2360 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
NYX | 0:85b3fd62ea1a | 2361 | |
NYX | 0:85b3fd62ea1a | 2362 | /* Set the phase */ |
NYX | 0:85b3fd62ea1a | 2363 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
NYX | 0:85b3fd62ea1a | 2364 | } |
NYX | 0:85b3fd62ea1a | 2365 | |
NYX | 0:85b3fd62ea1a | 2366 | /* Set the input and output addresses and start DMA transfer */ |
NYX | 0:85b3fd62ea1a | 2367 | CRYPEx_GCMCCM_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); |
NYX | 0:85b3fd62ea1a | 2368 | |
NYX | 0:85b3fd62ea1a | 2369 | /* Unlock process */ |
NYX | 0:85b3fd62ea1a | 2370 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 2371 | |
NYX | 0:85b3fd62ea1a | 2372 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 2373 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 2374 | } |
NYX | 0:85b3fd62ea1a | 2375 | else |
NYX | 0:85b3fd62ea1a | 2376 | { |
NYX | 0:85b3fd62ea1a | 2377 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 2378 | } |
NYX | 0:85b3fd62ea1a | 2379 | } |
NYX | 0:85b3fd62ea1a | 2380 | |
NYX | 0:85b3fd62ea1a | 2381 | /** |
NYX | 0:85b3fd62ea1a | 2382 | * @brief Initializes the CRYP peripheral in AES CCM encryption mode using interrupt. |
NYX | 0:85b3fd62ea1a | 2383 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 2384 | * the configuration information for CRYP module |
NYX | 0:85b3fd62ea1a | 2385 | * @param pPlainData: Pointer to the plaintext buffer |
NYX | 0:85b3fd62ea1a | 2386 | * @param Size: Length of the plaintext buffer, must be a multiple of 16 |
NYX | 0:85b3fd62ea1a | 2387 | * @param pCypherData: Pointer to the cyphertext buffer |
NYX | 0:85b3fd62ea1a | 2388 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 2389 | */ |
NYX | 0:85b3fd62ea1a | 2390 | HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) |
NYX | 0:85b3fd62ea1a | 2391 | { |
NYX | 0:85b3fd62ea1a | 2392 | uint32_t tickstart = 0U; |
NYX | 0:85b3fd62ea1a | 2393 | uint32_t inputaddr; |
NYX | 0:85b3fd62ea1a | 2394 | uint32_t outputaddr; |
NYX | 0:85b3fd62ea1a | 2395 | uint32_t headersize; |
NYX | 0:85b3fd62ea1a | 2396 | uint32_t headeraddr; |
NYX | 0:85b3fd62ea1a | 2397 | uint32_t loopcounter = 0U; |
NYX | 0:85b3fd62ea1a | 2398 | uint32_t bufferidx = 0U; |
NYX | 0:85b3fd62ea1a | 2399 | uint8_t blockb0[16U] = {0};/* Block B0 */ |
NYX | 0:85b3fd62ea1a | 2400 | uint8_t ctr[16U] = {0}; /* Counter */ |
NYX | 0:85b3fd62ea1a | 2401 | uint32_t b0addr = (uint32_t)blockb0; |
NYX | 0:85b3fd62ea1a | 2402 | |
NYX | 0:85b3fd62ea1a | 2403 | if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) |
NYX | 0:85b3fd62ea1a | 2404 | { |
NYX | 0:85b3fd62ea1a | 2405 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 2406 | __HAL_LOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 2407 | |
NYX | 0:85b3fd62ea1a | 2408 | inputaddr = (uint32_t)pPlainData; |
NYX | 0:85b3fd62ea1a | 2409 | outputaddr = (uint32_t)pCypherData; |
NYX | 0:85b3fd62ea1a | 2410 | |
NYX | 0:85b3fd62ea1a | 2411 | headersize = hcryp->Init.HeaderSize; |
NYX | 0:85b3fd62ea1a | 2412 | headeraddr = (uint32_t)hcryp->Init.Header; |
NYX | 0:85b3fd62ea1a | 2413 | |
NYX | 0:85b3fd62ea1a | 2414 | hcryp->CrypInCount = Size; |
NYX | 0:85b3fd62ea1a | 2415 | hcryp->pCrypInBuffPtr = pPlainData; |
NYX | 0:85b3fd62ea1a | 2416 | hcryp->pCrypOutBuffPtr = pCypherData; |
NYX | 0:85b3fd62ea1a | 2417 | hcryp->CrypOutCount = Size; |
NYX | 0:85b3fd62ea1a | 2418 | |
NYX | 0:85b3fd62ea1a | 2419 | /* Change the CRYP peripheral state */ |
NYX | 0:85b3fd62ea1a | 2420 | hcryp->State = HAL_CRYP_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 2421 | |
NYX | 0:85b3fd62ea1a | 2422 | /* Check if initialization phase has already been performed */ |
NYX | 0:85b3fd62ea1a | 2423 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
NYX | 0:85b3fd62ea1a | 2424 | { |
NYX | 0:85b3fd62ea1a | 2425 | /************************ Formatting the header block *******************/ |
NYX | 0:85b3fd62ea1a | 2426 | if(headersize != 0U) |
NYX | 0:85b3fd62ea1a | 2427 | { |
NYX | 0:85b3fd62ea1a | 2428 | /* Check that the associated data (or header) length is lower than 2^16 - 2^8 = 65536 - 256 = 65280 */ |
NYX | 0:85b3fd62ea1a | 2429 | if(headersize < 65280U) |
NYX | 0:85b3fd62ea1a | 2430 | { |
NYX | 0:85b3fd62ea1a | 2431 | hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize >> 8) & 0xFF); |
NYX | 0:85b3fd62ea1a | 2432 | hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize) & 0xFF); |
NYX | 0:85b3fd62ea1a | 2433 | headersize += 2U; |
NYX | 0:85b3fd62ea1a | 2434 | } |
NYX | 0:85b3fd62ea1a | 2435 | else |
NYX | 0:85b3fd62ea1a | 2436 | { |
NYX | 0:85b3fd62ea1a | 2437 | /* Header is encoded as 0xff || 0xfe || [headersize]32, i.e., six octets */ |
NYX | 0:85b3fd62ea1a | 2438 | hcryp->Init.pScratch[bufferidx++] = 0xFFU; |
NYX | 0:85b3fd62ea1a | 2439 | hcryp->Init.pScratch[bufferidx++] = 0xFEU; |
NYX | 0:85b3fd62ea1a | 2440 | hcryp->Init.pScratch[bufferidx++] = headersize & 0xff000000U; |
NYX | 0:85b3fd62ea1a | 2441 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x00ff0000U; |
NYX | 0:85b3fd62ea1a | 2442 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x0000ff00U; |
NYX | 0:85b3fd62ea1a | 2443 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x000000ffU; |
NYX | 0:85b3fd62ea1a | 2444 | headersize += 6U; |
NYX | 0:85b3fd62ea1a | 2445 | } |
NYX | 0:85b3fd62ea1a | 2446 | /* Copy the header buffer in internal buffer "hcryp->Init.pScratch" */ |
NYX | 0:85b3fd62ea1a | 2447 | for(loopcounter = 0U; loopcounter < headersize; loopcounter++) |
NYX | 0:85b3fd62ea1a | 2448 | { |
NYX | 0:85b3fd62ea1a | 2449 | hcryp->Init.pScratch[bufferidx++] = hcryp->Init.Header[loopcounter]; |
NYX | 0:85b3fd62ea1a | 2450 | } |
NYX | 0:85b3fd62ea1a | 2451 | /* Check if the header size is modulo 16 */ |
NYX | 0:85b3fd62ea1a | 2452 | if ((headersize % 16U) != 0U) |
NYX | 0:85b3fd62ea1a | 2453 | { |
NYX | 0:85b3fd62ea1a | 2454 | /* Padd the header buffer with 0s till the hcryp->Init.pScratch length is modulo 16 */ |
NYX | 0:85b3fd62ea1a | 2455 | for(loopcounter = headersize; loopcounter <= ((headersize/16U) + 1U) * 16U; loopcounter++) |
NYX | 0:85b3fd62ea1a | 2456 | { |
NYX | 0:85b3fd62ea1a | 2457 | hcryp->Init.pScratch[loopcounter] = 0U; |
NYX | 0:85b3fd62ea1a | 2458 | } |
NYX | 0:85b3fd62ea1a | 2459 | /* Set the header size to modulo 16 */ |
NYX | 0:85b3fd62ea1a | 2460 | headersize = ((headersize/16U) + 1U) * 16U; |
NYX | 0:85b3fd62ea1a | 2461 | } |
NYX | 0:85b3fd62ea1a | 2462 | /* Set the pointer headeraddr to hcryp->Init.pScratch */ |
NYX | 0:85b3fd62ea1a | 2463 | headeraddr = (uint32_t)hcryp->Init.pScratch; |
NYX | 0:85b3fd62ea1a | 2464 | } |
NYX | 0:85b3fd62ea1a | 2465 | /*********************** Formatting the block B0 ************************/ |
NYX | 0:85b3fd62ea1a | 2466 | if(headersize != 0U) |
NYX | 0:85b3fd62ea1a | 2467 | { |
NYX | 0:85b3fd62ea1a | 2468 | blockb0[0U] = 0x40U; |
NYX | 0:85b3fd62ea1a | 2469 | } |
NYX | 0:85b3fd62ea1a | 2470 | /* Flags byte */ |
NYX | 0:85b3fd62ea1a | 2471 | /* blockb0[0] |= 0u | (((( (uint8_t) hcryp->Init.TagSize - 2) / 2) & 0x07 ) << 3 ) | ( ( (uint8_t) (15 - hcryp->Init.IVSize) - 1) & 0x07U) */ |
NYX | 0:85b3fd62ea1a | 2472 | blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)(((uint8_t)(hcryp->Init.TagSize - (uint8_t)(2))) >> 1) & (uint8_t)0x07) << 3); |
NYX | 0:85b3fd62ea1a | 2473 | blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)((uint8_t)(15) - hcryp->Init.IVSize) - (uint8_t)1) & (uint8_t)0x07); |
NYX | 0:85b3fd62ea1a | 2474 | |
NYX | 0:85b3fd62ea1a | 2475 | for (loopcounter = 0U; loopcounter < hcryp->Init.IVSize; loopcounter++) |
NYX | 0:85b3fd62ea1a | 2476 | { |
NYX | 0:85b3fd62ea1a | 2477 | blockb0[loopcounter+1U] = hcryp->Init.pInitVect[loopcounter]; |
NYX | 0:85b3fd62ea1a | 2478 | } |
NYX | 0:85b3fd62ea1a | 2479 | for ( ; loopcounter < 13U; loopcounter++) |
NYX | 0:85b3fd62ea1a | 2480 | { |
NYX | 0:85b3fd62ea1a | 2481 | blockb0[loopcounter+1U] = 0U; |
NYX | 0:85b3fd62ea1a | 2482 | } |
NYX | 0:85b3fd62ea1a | 2483 | |
NYX | 0:85b3fd62ea1a | 2484 | blockb0[14U] = (Size >> 8U); |
NYX | 0:85b3fd62ea1a | 2485 | blockb0[15U] = (Size & 0xFFU); |
NYX | 0:85b3fd62ea1a | 2486 | |
NYX | 0:85b3fd62ea1a | 2487 | /************************* Formatting the initial counter ***************/ |
NYX | 0:85b3fd62ea1a | 2488 | /* Byte 0: |
NYX | 0:85b3fd62ea1a | 2489 | Bits 7 and 6 are reserved and shall be set to 0 |
NYX | 0:85b3fd62ea1a | 2490 | Bits 3, 4, and 5 shall also be set to 0, to ensure that all the counter |
NYX | 0:85b3fd62ea1a | 2491 | blocks are distinct from B0 |
NYX | 0:85b3fd62ea1a | 2492 | Bits 0, 1, and 2 contain the same encoding of q as in B0 |
NYX | 0:85b3fd62ea1a | 2493 | */ |
NYX | 0:85b3fd62ea1a | 2494 | ctr[0U] = blockb0[0U] & 0x07U; |
NYX | 0:85b3fd62ea1a | 2495 | /* byte 1 to NonceSize is the IV (Nonce) */ |
NYX | 0:85b3fd62ea1a | 2496 | for(loopcounter = 1U; loopcounter < hcryp->Init.IVSize + 1U; loopcounter++) |
NYX | 0:85b3fd62ea1a | 2497 | { |
NYX | 0:85b3fd62ea1a | 2498 | ctr[loopcounter] = blockb0[loopcounter]; |
NYX | 0:85b3fd62ea1a | 2499 | } |
NYX | 0:85b3fd62ea1a | 2500 | /* Set the LSB to 1 */ |
NYX | 0:85b3fd62ea1a | 2501 | ctr[15U] |= 0x01U; |
NYX | 0:85b3fd62ea1a | 2502 | |
NYX | 0:85b3fd62ea1a | 2503 | /* Set the key */ |
NYX | 0:85b3fd62ea1a | 2504 | CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
NYX | 0:85b3fd62ea1a | 2505 | |
NYX | 0:85b3fd62ea1a | 2506 | /* Set the CRYP peripheral in AES CCM mode */ |
NYX | 0:85b3fd62ea1a | 2507 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CCM_ENCRYPT); |
NYX | 0:85b3fd62ea1a | 2508 | |
NYX | 0:85b3fd62ea1a | 2509 | /* Set the Initialization Vector */ |
NYX | 0:85b3fd62ea1a | 2510 | CRYPEx_GCMCCM_SetInitVector(hcryp, ctr); |
NYX | 0:85b3fd62ea1a | 2511 | |
NYX | 0:85b3fd62ea1a | 2512 | /* Select init phase */ |
NYX | 0:85b3fd62ea1a | 2513 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT); |
NYX | 0:85b3fd62ea1a | 2514 | |
NYX | 0:85b3fd62ea1a | 2515 | b0addr = (uint32_t)blockb0; |
NYX | 0:85b3fd62ea1a | 2516 | /* Write the blockb0 block in the IN FIFO */ |
NYX | 0:85b3fd62ea1a | 2517 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
NYX | 0:85b3fd62ea1a | 2518 | b0addr+=4U; |
NYX | 0:85b3fd62ea1a | 2519 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
NYX | 0:85b3fd62ea1a | 2520 | b0addr+=4U; |
NYX | 0:85b3fd62ea1a | 2521 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
NYX | 0:85b3fd62ea1a | 2522 | b0addr+=4U; |
NYX | 0:85b3fd62ea1a | 2523 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
NYX | 0:85b3fd62ea1a | 2524 | |
NYX | 0:85b3fd62ea1a | 2525 | /* Enable the CRYP peripheral */ |
NYX | 0:85b3fd62ea1a | 2526 | __HAL_CRYP_ENABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 2527 | |
NYX | 0:85b3fd62ea1a | 2528 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 2529 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 2530 | |
NYX | 0:85b3fd62ea1a | 2531 | while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN) |
NYX | 0:85b3fd62ea1a | 2532 | { |
NYX | 0:85b3fd62ea1a | 2533 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 2534 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
NYX | 0:85b3fd62ea1a | 2535 | { |
NYX | 0:85b3fd62ea1a | 2536 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 2537 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 2538 | |
NYX | 0:85b3fd62ea1a | 2539 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 2540 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 2541 | |
NYX | 0:85b3fd62ea1a | 2542 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 2543 | } |
NYX | 0:85b3fd62ea1a | 2544 | } |
NYX | 0:85b3fd62ea1a | 2545 | /***************************** Header phase *****************************/ |
NYX | 0:85b3fd62ea1a | 2546 | if(headersize != 0U) |
NYX | 0:85b3fd62ea1a | 2547 | { |
NYX | 0:85b3fd62ea1a | 2548 | /* Select header phase */ |
NYX | 0:85b3fd62ea1a | 2549 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER); |
NYX | 0:85b3fd62ea1a | 2550 | |
NYX | 0:85b3fd62ea1a | 2551 | /* Enable Crypto processor */ |
NYX | 0:85b3fd62ea1a | 2552 | __HAL_CRYP_ENABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 2553 | |
NYX | 0:85b3fd62ea1a | 2554 | for(loopcounter = 0U; (loopcounter < headersize); loopcounter+=16U) |
NYX | 0:85b3fd62ea1a | 2555 | { |
NYX | 0:85b3fd62ea1a | 2556 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 2557 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 2558 | |
NYX | 0:85b3fd62ea1a | 2559 | while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM)) |
NYX | 0:85b3fd62ea1a | 2560 | { |
NYX | 0:85b3fd62ea1a | 2561 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 2562 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
NYX | 0:85b3fd62ea1a | 2563 | { |
NYX | 0:85b3fd62ea1a | 2564 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 2565 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 2566 | |
NYX | 0:85b3fd62ea1a | 2567 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 2568 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 2569 | |
NYX | 0:85b3fd62ea1a | 2570 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 2571 | } |
NYX | 0:85b3fd62ea1a | 2572 | } |
NYX | 0:85b3fd62ea1a | 2573 | /* Write the header block in the IN FIFO */ |
NYX | 0:85b3fd62ea1a | 2574 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
NYX | 0:85b3fd62ea1a | 2575 | headeraddr+=4U; |
NYX | 0:85b3fd62ea1a | 2576 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
NYX | 0:85b3fd62ea1a | 2577 | headeraddr+=4U; |
NYX | 0:85b3fd62ea1a | 2578 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
NYX | 0:85b3fd62ea1a | 2579 | headeraddr+=4U; |
NYX | 0:85b3fd62ea1a | 2580 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
NYX | 0:85b3fd62ea1a | 2581 | headeraddr+=4U; |
NYX | 0:85b3fd62ea1a | 2582 | } |
NYX | 0:85b3fd62ea1a | 2583 | |
NYX | 0:85b3fd62ea1a | 2584 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 2585 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 2586 | |
NYX | 0:85b3fd62ea1a | 2587 | while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY) |
NYX | 0:85b3fd62ea1a | 2588 | { |
NYX | 0:85b3fd62ea1a | 2589 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 2590 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
NYX | 0:85b3fd62ea1a | 2591 | { |
NYX | 0:85b3fd62ea1a | 2592 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 2593 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 2594 | |
NYX | 0:85b3fd62ea1a | 2595 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 2596 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 2597 | |
NYX | 0:85b3fd62ea1a | 2598 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 2599 | } |
NYX | 0:85b3fd62ea1a | 2600 | } |
NYX | 0:85b3fd62ea1a | 2601 | } |
NYX | 0:85b3fd62ea1a | 2602 | /* Save formatted counter into the scratch buffer pScratch */ |
NYX | 0:85b3fd62ea1a | 2603 | for(loopcounter = 0U; (loopcounter < 16U); loopcounter++) |
NYX | 0:85b3fd62ea1a | 2604 | { |
NYX | 0:85b3fd62ea1a | 2605 | hcryp->Init.pScratch[loopcounter] = ctr[loopcounter]; |
NYX | 0:85b3fd62ea1a | 2606 | } |
NYX | 0:85b3fd62ea1a | 2607 | /* Reset bit 0 */ |
NYX | 0:85b3fd62ea1a | 2608 | hcryp->Init.pScratch[15U] &= 0xFEU; |
NYX | 0:85b3fd62ea1a | 2609 | |
NYX | 0:85b3fd62ea1a | 2610 | /* Select payload phase once the header phase is performed */ |
NYX | 0:85b3fd62ea1a | 2611 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD); |
NYX | 0:85b3fd62ea1a | 2612 | |
NYX | 0:85b3fd62ea1a | 2613 | /* Flush FIFO */ |
NYX | 0:85b3fd62ea1a | 2614 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
NYX | 0:85b3fd62ea1a | 2615 | |
NYX | 0:85b3fd62ea1a | 2616 | /* Set the phase */ |
NYX | 0:85b3fd62ea1a | 2617 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
NYX | 0:85b3fd62ea1a | 2618 | } |
NYX | 0:85b3fd62ea1a | 2619 | |
NYX | 0:85b3fd62ea1a | 2620 | /* Set the input and output addresses and start DMA transfer */ |
NYX | 0:85b3fd62ea1a | 2621 | CRYPEx_GCMCCM_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); |
NYX | 0:85b3fd62ea1a | 2622 | |
NYX | 0:85b3fd62ea1a | 2623 | /* Unlock process */ |
NYX | 0:85b3fd62ea1a | 2624 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 2625 | |
NYX | 0:85b3fd62ea1a | 2626 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 2627 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 2628 | } |
NYX | 0:85b3fd62ea1a | 2629 | else |
NYX | 0:85b3fd62ea1a | 2630 | { |
NYX | 0:85b3fd62ea1a | 2631 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 2632 | } |
NYX | 0:85b3fd62ea1a | 2633 | } |
NYX | 0:85b3fd62ea1a | 2634 | |
NYX | 0:85b3fd62ea1a | 2635 | /** |
NYX | 0:85b3fd62ea1a | 2636 | * @brief Initializes the CRYP peripheral in AES GCM decryption mode using DMA. |
NYX | 0:85b3fd62ea1a | 2637 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 2638 | * the configuration information for CRYP module |
NYX | 0:85b3fd62ea1a | 2639 | * @param pCypherData: Pointer to the cyphertext buffer. |
NYX | 0:85b3fd62ea1a | 2640 | * @param Size: Length of the cyphertext buffer, must be a multiple of 16 |
NYX | 0:85b3fd62ea1a | 2641 | * @param pPlainData: Pointer to the plaintext buffer |
NYX | 0:85b3fd62ea1a | 2642 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 2643 | */ |
NYX | 0:85b3fd62ea1a | 2644 | HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) |
NYX | 0:85b3fd62ea1a | 2645 | { |
NYX | 0:85b3fd62ea1a | 2646 | uint32_t tickstart = 0U; |
NYX | 0:85b3fd62ea1a | 2647 | uint32_t inputaddr; |
NYX | 0:85b3fd62ea1a | 2648 | uint32_t outputaddr; |
NYX | 0:85b3fd62ea1a | 2649 | |
NYX | 0:85b3fd62ea1a | 2650 | if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) |
NYX | 0:85b3fd62ea1a | 2651 | { |
NYX | 0:85b3fd62ea1a | 2652 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 2653 | __HAL_LOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 2654 | |
NYX | 0:85b3fd62ea1a | 2655 | inputaddr = (uint32_t)pCypherData; |
NYX | 0:85b3fd62ea1a | 2656 | outputaddr = (uint32_t)pPlainData; |
NYX | 0:85b3fd62ea1a | 2657 | |
NYX | 0:85b3fd62ea1a | 2658 | /* Change the CRYP peripheral state */ |
NYX | 0:85b3fd62ea1a | 2659 | hcryp->State = HAL_CRYP_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 2660 | |
NYX | 0:85b3fd62ea1a | 2661 | /* Check if initialization phase has already been performed */ |
NYX | 0:85b3fd62ea1a | 2662 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
NYX | 0:85b3fd62ea1a | 2663 | { |
NYX | 0:85b3fd62ea1a | 2664 | /* Set the key */ |
NYX | 0:85b3fd62ea1a | 2665 | CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
NYX | 0:85b3fd62ea1a | 2666 | |
NYX | 0:85b3fd62ea1a | 2667 | /* Set the CRYP peripheral in AES GCM decryption mode */ |
NYX | 0:85b3fd62ea1a | 2668 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_GCM_DECRYPT); |
NYX | 0:85b3fd62ea1a | 2669 | |
NYX | 0:85b3fd62ea1a | 2670 | /* Set the Initialization Vector */ |
NYX | 0:85b3fd62ea1a | 2671 | CRYPEx_GCMCCM_SetInitVector(hcryp, hcryp->Init.pInitVect); |
NYX | 0:85b3fd62ea1a | 2672 | |
NYX | 0:85b3fd62ea1a | 2673 | /* Enable CRYP to start the init phase */ |
NYX | 0:85b3fd62ea1a | 2674 | __HAL_CRYP_ENABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 2675 | |
NYX | 0:85b3fd62ea1a | 2676 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 2677 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 2678 | |
NYX | 0:85b3fd62ea1a | 2679 | while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN) |
NYX | 0:85b3fd62ea1a | 2680 | { |
NYX | 0:85b3fd62ea1a | 2681 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 2682 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
NYX | 0:85b3fd62ea1a | 2683 | { |
NYX | 0:85b3fd62ea1a | 2684 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 2685 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 2686 | |
NYX | 0:85b3fd62ea1a | 2687 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 2688 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 2689 | |
NYX | 0:85b3fd62ea1a | 2690 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 2691 | } |
NYX | 0:85b3fd62ea1a | 2692 | } |
NYX | 0:85b3fd62ea1a | 2693 | |
NYX | 0:85b3fd62ea1a | 2694 | /* Set the header phase */ |
NYX | 0:85b3fd62ea1a | 2695 | if(CRYPEx_GCMCCM_SetHeaderPhase(hcryp, hcryp->Init.Header, hcryp->Init.HeaderSize, 1U) != HAL_OK) |
NYX | 0:85b3fd62ea1a | 2696 | { |
NYX | 0:85b3fd62ea1a | 2697 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 2698 | } |
NYX | 0:85b3fd62ea1a | 2699 | /* Disable the CRYP peripheral */ |
NYX | 0:85b3fd62ea1a | 2700 | __HAL_CRYP_DISABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 2701 | |
NYX | 0:85b3fd62ea1a | 2702 | /* Select payload phase once the header phase is performed */ |
NYX | 0:85b3fd62ea1a | 2703 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD); |
NYX | 0:85b3fd62ea1a | 2704 | |
NYX | 0:85b3fd62ea1a | 2705 | /* Set the phase */ |
NYX | 0:85b3fd62ea1a | 2706 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
NYX | 0:85b3fd62ea1a | 2707 | } |
NYX | 0:85b3fd62ea1a | 2708 | |
NYX | 0:85b3fd62ea1a | 2709 | /* Set the input and output addresses and start DMA transfer */ |
NYX | 0:85b3fd62ea1a | 2710 | CRYPEx_GCMCCM_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); |
NYX | 0:85b3fd62ea1a | 2711 | |
NYX | 0:85b3fd62ea1a | 2712 | /* Unlock process */ |
NYX | 0:85b3fd62ea1a | 2713 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 2714 | |
NYX | 0:85b3fd62ea1a | 2715 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 2716 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 2717 | } |
NYX | 0:85b3fd62ea1a | 2718 | else |
NYX | 0:85b3fd62ea1a | 2719 | { |
NYX | 0:85b3fd62ea1a | 2720 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 2721 | } |
NYX | 0:85b3fd62ea1a | 2722 | } |
NYX | 0:85b3fd62ea1a | 2723 | |
NYX | 0:85b3fd62ea1a | 2724 | /** |
NYX | 0:85b3fd62ea1a | 2725 | * @brief Initializes the CRYP peripheral in AES CCM decryption mode using DMA |
NYX | 0:85b3fd62ea1a | 2726 | * then decrypted pCypherData. The cypher data are available in pPlainData. |
NYX | 0:85b3fd62ea1a | 2727 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 2728 | * the configuration information for CRYP module |
NYX | 0:85b3fd62ea1a | 2729 | * @param pCypherData: Pointer to the cyphertext buffer |
NYX | 0:85b3fd62ea1a | 2730 | * @param Size: Length of the plaintext buffer, must be a multiple of 16 |
NYX | 0:85b3fd62ea1a | 2731 | * @param pPlainData: Pointer to the plaintext buffer |
NYX | 0:85b3fd62ea1a | 2732 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 2733 | */ |
NYX | 0:85b3fd62ea1a | 2734 | HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) |
NYX | 0:85b3fd62ea1a | 2735 | { |
NYX | 0:85b3fd62ea1a | 2736 | uint32_t tickstart = 0U; |
NYX | 0:85b3fd62ea1a | 2737 | uint32_t inputaddr; |
NYX | 0:85b3fd62ea1a | 2738 | uint32_t outputaddr; |
NYX | 0:85b3fd62ea1a | 2739 | uint32_t headersize; |
NYX | 0:85b3fd62ea1a | 2740 | uint32_t headeraddr; |
NYX | 0:85b3fd62ea1a | 2741 | uint32_t loopcounter = 0U; |
NYX | 0:85b3fd62ea1a | 2742 | uint32_t bufferidx = 0U; |
NYX | 0:85b3fd62ea1a | 2743 | uint8_t blockb0[16U] = {0};/* Block B0 */ |
NYX | 0:85b3fd62ea1a | 2744 | uint8_t ctr[16U] = {0}; /* Counter */ |
NYX | 0:85b3fd62ea1a | 2745 | uint32_t b0addr = (uint32_t)blockb0; |
NYX | 0:85b3fd62ea1a | 2746 | |
NYX | 0:85b3fd62ea1a | 2747 | if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) |
NYX | 0:85b3fd62ea1a | 2748 | { |
NYX | 0:85b3fd62ea1a | 2749 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 2750 | __HAL_LOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 2751 | |
NYX | 0:85b3fd62ea1a | 2752 | inputaddr = (uint32_t)pCypherData; |
NYX | 0:85b3fd62ea1a | 2753 | outputaddr = (uint32_t)pPlainData; |
NYX | 0:85b3fd62ea1a | 2754 | |
NYX | 0:85b3fd62ea1a | 2755 | headersize = hcryp->Init.HeaderSize; |
NYX | 0:85b3fd62ea1a | 2756 | headeraddr = (uint32_t)hcryp->Init.Header; |
NYX | 0:85b3fd62ea1a | 2757 | |
NYX | 0:85b3fd62ea1a | 2758 | hcryp->CrypInCount = Size; |
NYX | 0:85b3fd62ea1a | 2759 | hcryp->pCrypInBuffPtr = pCypherData; |
NYX | 0:85b3fd62ea1a | 2760 | hcryp->pCrypOutBuffPtr = pPlainData; |
NYX | 0:85b3fd62ea1a | 2761 | hcryp->CrypOutCount = Size; |
NYX | 0:85b3fd62ea1a | 2762 | |
NYX | 0:85b3fd62ea1a | 2763 | /* Change the CRYP peripheral state */ |
NYX | 0:85b3fd62ea1a | 2764 | hcryp->State = HAL_CRYP_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 2765 | |
NYX | 0:85b3fd62ea1a | 2766 | /* Check if initialization phase has already been performed */ |
NYX | 0:85b3fd62ea1a | 2767 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
NYX | 0:85b3fd62ea1a | 2768 | { |
NYX | 0:85b3fd62ea1a | 2769 | /************************ Formatting the header block *******************/ |
NYX | 0:85b3fd62ea1a | 2770 | if(headersize != 0U) |
NYX | 0:85b3fd62ea1a | 2771 | { |
NYX | 0:85b3fd62ea1a | 2772 | /* Check that the associated data (or header) length is lower than 2^16 - 2^8 = 65536 - 256 = 65280 */ |
NYX | 0:85b3fd62ea1a | 2773 | if(headersize < 65280U) |
NYX | 0:85b3fd62ea1a | 2774 | { |
NYX | 0:85b3fd62ea1a | 2775 | hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize >> 8) & 0xFF); |
NYX | 0:85b3fd62ea1a | 2776 | hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize) & 0xFF); |
NYX | 0:85b3fd62ea1a | 2777 | headersize += 2U; |
NYX | 0:85b3fd62ea1a | 2778 | } |
NYX | 0:85b3fd62ea1a | 2779 | else |
NYX | 0:85b3fd62ea1a | 2780 | { |
NYX | 0:85b3fd62ea1a | 2781 | /* Header is encoded as 0xff || 0xfe || [headersize]32, i.e., six octets */ |
NYX | 0:85b3fd62ea1a | 2782 | hcryp->Init.pScratch[bufferidx++] = 0xFFU; |
NYX | 0:85b3fd62ea1a | 2783 | hcryp->Init.pScratch[bufferidx++] = 0xFEU; |
NYX | 0:85b3fd62ea1a | 2784 | hcryp->Init.pScratch[bufferidx++] = headersize & 0xff000000U; |
NYX | 0:85b3fd62ea1a | 2785 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x00ff0000U; |
NYX | 0:85b3fd62ea1a | 2786 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x0000ff00U; |
NYX | 0:85b3fd62ea1a | 2787 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x000000ffU; |
NYX | 0:85b3fd62ea1a | 2788 | headersize += 6U; |
NYX | 0:85b3fd62ea1a | 2789 | } |
NYX | 0:85b3fd62ea1a | 2790 | /* Copy the header buffer in internal buffer "hcryp->Init.pScratch" */ |
NYX | 0:85b3fd62ea1a | 2791 | for(loopcounter = 0U; loopcounter < headersize; loopcounter++) |
NYX | 0:85b3fd62ea1a | 2792 | { |
NYX | 0:85b3fd62ea1a | 2793 | hcryp->Init.pScratch[bufferidx++] = hcryp->Init.Header[loopcounter]; |
NYX | 0:85b3fd62ea1a | 2794 | } |
NYX | 0:85b3fd62ea1a | 2795 | /* Check if the header size is modulo 16 */ |
NYX | 0:85b3fd62ea1a | 2796 | if ((headersize % 16U) != 0U) |
NYX | 0:85b3fd62ea1a | 2797 | { |
NYX | 0:85b3fd62ea1a | 2798 | /* Padd the header buffer with 0s till the hcryp->Init.pScratch length is modulo 16 */ |
NYX | 0:85b3fd62ea1a | 2799 | for(loopcounter = headersize; loopcounter <= ((headersize/16U) + 1U) * 16U; loopcounter++) |
NYX | 0:85b3fd62ea1a | 2800 | { |
NYX | 0:85b3fd62ea1a | 2801 | hcryp->Init.pScratch[loopcounter] = 0U; |
NYX | 0:85b3fd62ea1a | 2802 | } |
NYX | 0:85b3fd62ea1a | 2803 | /* Set the header size to modulo 16 */ |
NYX | 0:85b3fd62ea1a | 2804 | headersize = ((headersize/16U) + 1U) * 16U; |
NYX | 0:85b3fd62ea1a | 2805 | } |
NYX | 0:85b3fd62ea1a | 2806 | /* Set the pointer headeraddr to hcryp->Init.pScratch */ |
NYX | 0:85b3fd62ea1a | 2807 | headeraddr = (uint32_t)hcryp->Init.pScratch; |
NYX | 0:85b3fd62ea1a | 2808 | } |
NYX | 0:85b3fd62ea1a | 2809 | /*********************** Formatting the block B0 ************************/ |
NYX | 0:85b3fd62ea1a | 2810 | if(headersize != 0U) |
NYX | 0:85b3fd62ea1a | 2811 | { |
NYX | 0:85b3fd62ea1a | 2812 | blockb0[0U] = 0x40U; |
NYX | 0:85b3fd62ea1a | 2813 | } |
NYX | 0:85b3fd62ea1a | 2814 | /* Flags byte */ |
NYX | 0:85b3fd62ea1a | 2815 | /* blockb0[0] |= 0u | (((( (uint8_t) hcryp->Init.TagSize - 2) / 2) & 0x07 ) << 3 ) | ( ( (uint8_t) (15 - hcryp->Init.IVSize) - 1) & 0x07U) */ |
NYX | 0:85b3fd62ea1a | 2816 | blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)(((uint8_t)(hcryp->Init.TagSize - (uint8_t)(2))) >> 1) & (uint8_t)0x07) << 3); |
NYX | 0:85b3fd62ea1a | 2817 | blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)((uint8_t)(15) - hcryp->Init.IVSize) - (uint8_t)1) & (uint8_t)0x07); |
NYX | 0:85b3fd62ea1a | 2818 | |
NYX | 0:85b3fd62ea1a | 2819 | for (loopcounter = 0U; loopcounter < hcryp->Init.IVSize; loopcounter++) |
NYX | 0:85b3fd62ea1a | 2820 | { |
NYX | 0:85b3fd62ea1a | 2821 | blockb0[loopcounter+1U] = hcryp->Init.pInitVect[loopcounter]; |
NYX | 0:85b3fd62ea1a | 2822 | } |
NYX | 0:85b3fd62ea1a | 2823 | for ( ; loopcounter < 13U; loopcounter++) |
NYX | 0:85b3fd62ea1a | 2824 | { |
NYX | 0:85b3fd62ea1a | 2825 | blockb0[loopcounter+1U] = 0U; |
NYX | 0:85b3fd62ea1a | 2826 | } |
NYX | 0:85b3fd62ea1a | 2827 | |
NYX | 0:85b3fd62ea1a | 2828 | blockb0[14U] = (Size >> 8U); |
NYX | 0:85b3fd62ea1a | 2829 | blockb0[15U] = (Size & 0xFFU); |
NYX | 0:85b3fd62ea1a | 2830 | |
NYX | 0:85b3fd62ea1a | 2831 | /************************* Formatting the initial counter ***************/ |
NYX | 0:85b3fd62ea1a | 2832 | /* Byte 0: |
NYX | 0:85b3fd62ea1a | 2833 | Bits 7 and 6 are reserved and shall be set to 0 |
NYX | 0:85b3fd62ea1a | 2834 | Bits 3, 4, and 5 shall also be set to 0, to ensure that all the counter |
NYX | 0:85b3fd62ea1a | 2835 | blocks are distinct from B0 |
NYX | 0:85b3fd62ea1a | 2836 | Bits 0, 1, and 2 contain the same encoding of q as in B0 |
NYX | 0:85b3fd62ea1a | 2837 | */ |
NYX | 0:85b3fd62ea1a | 2838 | ctr[0U] = blockb0[0U] & 0x07U; |
NYX | 0:85b3fd62ea1a | 2839 | /* byte 1 to NonceSize is the IV (Nonce) */ |
NYX | 0:85b3fd62ea1a | 2840 | for(loopcounter = 1U; loopcounter < hcryp->Init.IVSize + 1U; loopcounter++) |
NYX | 0:85b3fd62ea1a | 2841 | { |
NYX | 0:85b3fd62ea1a | 2842 | ctr[loopcounter] = blockb0[loopcounter]; |
NYX | 0:85b3fd62ea1a | 2843 | } |
NYX | 0:85b3fd62ea1a | 2844 | /* Set the LSB to 1 */ |
NYX | 0:85b3fd62ea1a | 2845 | ctr[15U] |= 0x01U; |
NYX | 0:85b3fd62ea1a | 2846 | |
NYX | 0:85b3fd62ea1a | 2847 | /* Set the key */ |
NYX | 0:85b3fd62ea1a | 2848 | CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
NYX | 0:85b3fd62ea1a | 2849 | |
NYX | 0:85b3fd62ea1a | 2850 | /* Set the CRYP peripheral in AES CCM mode */ |
NYX | 0:85b3fd62ea1a | 2851 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CCM_DECRYPT); |
NYX | 0:85b3fd62ea1a | 2852 | |
NYX | 0:85b3fd62ea1a | 2853 | /* Set the Initialization Vector */ |
NYX | 0:85b3fd62ea1a | 2854 | CRYPEx_GCMCCM_SetInitVector(hcryp, ctr); |
NYX | 0:85b3fd62ea1a | 2855 | |
NYX | 0:85b3fd62ea1a | 2856 | /* Select init phase */ |
NYX | 0:85b3fd62ea1a | 2857 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT); |
NYX | 0:85b3fd62ea1a | 2858 | |
NYX | 0:85b3fd62ea1a | 2859 | b0addr = (uint32_t)blockb0; |
NYX | 0:85b3fd62ea1a | 2860 | /* Write the blockb0 block in the IN FIFO */ |
NYX | 0:85b3fd62ea1a | 2861 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
NYX | 0:85b3fd62ea1a | 2862 | b0addr+=4U; |
NYX | 0:85b3fd62ea1a | 2863 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
NYX | 0:85b3fd62ea1a | 2864 | b0addr+=4U; |
NYX | 0:85b3fd62ea1a | 2865 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
NYX | 0:85b3fd62ea1a | 2866 | b0addr+=4U; |
NYX | 0:85b3fd62ea1a | 2867 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
NYX | 0:85b3fd62ea1a | 2868 | |
NYX | 0:85b3fd62ea1a | 2869 | /* Enable the CRYP peripheral */ |
NYX | 0:85b3fd62ea1a | 2870 | __HAL_CRYP_ENABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 2871 | |
NYX | 0:85b3fd62ea1a | 2872 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 2873 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 2874 | |
NYX | 0:85b3fd62ea1a | 2875 | while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN) |
NYX | 0:85b3fd62ea1a | 2876 | { |
NYX | 0:85b3fd62ea1a | 2877 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 2878 | |
NYX | 0:85b3fd62ea1a | 2879 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
NYX | 0:85b3fd62ea1a | 2880 | { |
NYX | 0:85b3fd62ea1a | 2881 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 2882 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 2883 | |
NYX | 0:85b3fd62ea1a | 2884 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 2885 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 2886 | |
NYX | 0:85b3fd62ea1a | 2887 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 2888 | |
NYX | 0:85b3fd62ea1a | 2889 | } |
NYX | 0:85b3fd62ea1a | 2890 | } |
NYX | 0:85b3fd62ea1a | 2891 | /***************************** Header phase *****************************/ |
NYX | 0:85b3fd62ea1a | 2892 | if(headersize != 0U) |
NYX | 0:85b3fd62ea1a | 2893 | { |
NYX | 0:85b3fd62ea1a | 2894 | /* Select header phase */ |
NYX | 0:85b3fd62ea1a | 2895 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER); |
NYX | 0:85b3fd62ea1a | 2896 | |
NYX | 0:85b3fd62ea1a | 2897 | /* Enable Crypto processor */ |
NYX | 0:85b3fd62ea1a | 2898 | __HAL_CRYP_ENABLE(hcryp); |
NYX | 0:85b3fd62ea1a | 2899 | |
NYX | 0:85b3fd62ea1a | 2900 | for(loopcounter = 0U; (loopcounter < headersize); loopcounter+=16U) |
NYX | 0:85b3fd62ea1a | 2901 | { |
NYX | 0:85b3fd62ea1a | 2902 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 2903 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 2904 | |
NYX | 0:85b3fd62ea1a | 2905 | while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM)) |
NYX | 0:85b3fd62ea1a | 2906 | { |
NYX | 0:85b3fd62ea1a | 2907 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 2908 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
NYX | 0:85b3fd62ea1a | 2909 | { |
NYX | 0:85b3fd62ea1a | 2910 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 2911 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 2912 | |
NYX | 0:85b3fd62ea1a | 2913 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 2914 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 2915 | |
NYX | 0:85b3fd62ea1a | 2916 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 2917 | } |
NYX | 0:85b3fd62ea1a | 2918 | } |
NYX | 0:85b3fd62ea1a | 2919 | /* Write the header block in the IN FIFO */ |
NYX | 0:85b3fd62ea1a | 2920 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
NYX | 0:85b3fd62ea1a | 2921 | headeraddr+=4U; |
NYX | 0:85b3fd62ea1a | 2922 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
NYX | 0:85b3fd62ea1a | 2923 | headeraddr+=4U; |
NYX | 0:85b3fd62ea1a | 2924 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
NYX | 0:85b3fd62ea1a | 2925 | headeraddr+=4U; |
NYX | 0:85b3fd62ea1a | 2926 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
NYX | 0:85b3fd62ea1a | 2927 | headeraddr+=4U; |
NYX | 0:85b3fd62ea1a | 2928 | } |
NYX | 0:85b3fd62ea1a | 2929 | |
NYX | 0:85b3fd62ea1a | 2930 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 2931 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 2932 | |
NYX | 0:85b3fd62ea1a | 2933 | while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY) |
NYX | 0:85b3fd62ea1a | 2934 | { |
NYX | 0:85b3fd62ea1a | 2935 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 2936 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
NYX | 0:85b3fd62ea1a | 2937 | { |
NYX | 0:85b3fd62ea1a | 2938 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 2939 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 2940 | |
NYX | 0:85b3fd62ea1a | 2941 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 2942 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 2943 | |
NYX | 0:85b3fd62ea1a | 2944 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 2945 | } |
NYX | 0:85b3fd62ea1a | 2946 | } |
NYX | 0:85b3fd62ea1a | 2947 | } |
NYX | 0:85b3fd62ea1a | 2948 | /* Save formatted counter into the scratch buffer pScratch */ |
NYX | 0:85b3fd62ea1a | 2949 | for(loopcounter = 0U; (loopcounter < 16U); loopcounter++) |
NYX | 0:85b3fd62ea1a | 2950 | { |
NYX | 0:85b3fd62ea1a | 2951 | hcryp->Init.pScratch[loopcounter] = ctr[loopcounter]; |
NYX | 0:85b3fd62ea1a | 2952 | } |
NYX | 0:85b3fd62ea1a | 2953 | /* Reset bit 0 */ |
NYX | 0:85b3fd62ea1a | 2954 | hcryp->Init.pScratch[15U] &= 0xFEU; |
NYX | 0:85b3fd62ea1a | 2955 | /* Select payload phase once the header phase is performed */ |
NYX | 0:85b3fd62ea1a | 2956 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD); |
NYX | 0:85b3fd62ea1a | 2957 | |
NYX | 0:85b3fd62ea1a | 2958 | /* Flush FIFO */ |
NYX | 0:85b3fd62ea1a | 2959 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
NYX | 0:85b3fd62ea1a | 2960 | |
NYX | 0:85b3fd62ea1a | 2961 | /* Set the phase */ |
NYX | 0:85b3fd62ea1a | 2962 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
NYX | 0:85b3fd62ea1a | 2963 | } |
NYX | 0:85b3fd62ea1a | 2964 | /* Set the input and output addresses and start DMA transfer */ |
NYX | 0:85b3fd62ea1a | 2965 | CRYPEx_GCMCCM_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); |
NYX | 0:85b3fd62ea1a | 2966 | |
NYX | 0:85b3fd62ea1a | 2967 | /* Unlock process */ |
NYX | 0:85b3fd62ea1a | 2968 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 2969 | |
NYX | 0:85b3fd62ea1a | 2970 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 2971 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 2972 | } |
NYX | 0:85b3fd62ea1a | 2973 | else |
NYX | 0:85b3fd62ea1a | 2974 | { |
NYX | 0:85b3fd62ea1a | 2975 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 2976 | } |
NYX | 0:85b3fd62ea1a | 2977 | } |
NYX | 0:85b3fd62ea1a | 2978 | |
NYX | 0:85b3fd62ea1a | 2979 | /** |
NYX | 0:85b3fd62ea1a | 2980 | * @} |
NYX | 0:85b3fd62ea1a | 2981 | */ |
NYX | 0:85b3fd62ea1a | 2982 | |
NYX | 0:85b3fd62ea1a | 2983 | /** @defgroup CRYPEx_Exported_Functions_Group2 CRYPEx IRQ handler management |
NYX | 0:85b3fd62ea1a | 2984 | * @brief CRYPEx IRQ handler. |
NYX | 0:85b3fd62ea1a | 2985 | * |
NYX | 0:85b3fd62ea1a | 2986 | @verbatim |
NYX | 0:85b3fd62ea1a | 2987 | ============================================================================== |
NYX | 0:85b3fd62ea1a | 2988 | ##### CRYPEx IRQ handler management ##### |
NYX | 0:85b3fd62ea1a | 2989 | ============================================================================== |
NYX | 0:85b3fd62ea1a | 2990 | [..] This section provides CRYPEx IRQ handler function. |
NYX | 0:85b3fd62ea1a | 2991 | |
NYX | 0:85b3fd62ea1a | 2992 | @endverbatim |
NYX | 0:85b3fd62ea1a | 2993 | * @{ |
NYX | 0:85b3fd62ea1a | 2994 | */ |
NYX | 0:85b3fd62ea1a | 2995 | |
NYX | 0:85b3fd62ea1a | 2996 | /** |
NYX | 0:85b3fd62ea1a | 2997 | * @brief This function handles CRYPEx interrupt request. |
NYX | 0:85b3fd62ea1a | 2998 | * @param hcryp: pointer to a CRYPEx_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 2999 | * the configuration information for CRYP module |
NYX | 0:85b3fd62ea1a | 3000 | * @retval None |
NYX | 0:85b3fd62ea1a | 3001 | */ |
NYX | 0:85b3fd62ea1a | 3002 | |
NYX | 0:85b3fd62ea1a | 3003 | void HAL_CRYPEx_GCMCCM_IRQHandler(CRYP_HandleTypeDef *hcryp) |
NYX | 0:85b3fd62ea1a | 3004 | { |
NYX | 0:85b3fd62ea1a | 3005 | switch(CRYP->CR & CRYP_CR_ALGOMODE_DIRECTION) |
NYX | 0:85b3fd62ea1a | 3006 | { |
NYX | 0:85b3fd62ea1a | 3007 | case CRYP_CR_ALGOMODE_AES_GCM_ENCRYPT: |
NYX | 0:85b3fd62ea1a | 3008 | HAL_CRYPEx_AESGCM_Encrypt_IT(hcryp, NULL, 0U, NULL); |
NYX | 0:85b3fd62ea1a | 3009 | break; |
NYX | 0:85b3fd62ea1a | 3010 | |
NYX | 0:85b3fd62ea1a | 3011 | case CRYP_CR_ALGOMODE_AES_GCM_DECRYPT: |
NYX | 0:85b3fd62ea1a | 3012 | HAL_CRYPEx_AESGCM_Decrypt_IT(hcryp, NULL, 0U, NULL); |
NYX | 0:85b3fd62ea1a | 3013 | break; |
NYX | 0:85b3fd62ea1a | 3014 | |
NYX | 0:85b3fd62ea1a | 3015 | case CRYP_CR_ALGOMODE_AES_CCM_ENCRYPT: |
NYX | 0:85b3fd62ea1a | 3016 | HAL_CRYPEx_AESCCM_Encrypt_IT(hcryp, NULL, 0U, NULL); |
NYX | 0:85b3fd62ea1a | 3017 | break; |
NYX | 0:85b3fd62ea1a | 3018 | |
NYX | 0:85b3fd62ea1a | 3019 | case CRYP_CR_ALGOMODE_AES_CCM_DECRYPT: |
NYX | 0:85b3fd62ea1a | 3020 | HAL_CRYPEx_AESCCM_Decrypt_IT(hcryp, NULL, 0U, NULL); |
NYX | 0:85b3fd62ea1a | 3021 | break; |
NYX | 0:85b3fd62ea1a | 3022 | |
NYX | 0:85b3fd62ea1a | 3023 | default: |
NYX | 0:85b3fd62ea1a | 3024 | break; |
NYX | 0:85b3fd62ea1a | 3025 | } |
NYX | 0:85b3fd62ea1a | 3026 | } |
NYX | 0:85b3fd62ea1a | 3027 | |
NYX | 0:85b3fd62ea1a | 3028 | /** |
NYX | 0:85b3fd62ea1a | 3029 | * @} |
NYX | 0:85b3fd62ea1a | 3030 | */ |
NYX | 0:85b3fd62ea1a | 3031 | |
NYX | 0:85b3fd62ea1a | 3032 | /** |
NYX | 0:85b3fd62ea1a | 3033 | * @} |
NYX | 0:85b3fd62ea1a | 3034 | */ |
NYX | 0:85b3fd62ea1a | 3035 | #endif /* CRYP */ |
NYX | 0:85b3fd62ea1a | 3036 | |
NYX | 0:85b3fd62ea1a | 3037 | #if defined (AES) |
NYX | 0:85b3fd62ea1a | 3038 | |
NYX | 0:85b3fd62ea1a | 3039 | /** @defgroup CRYPEx_Private_Constants CRYPEx Private Constants |
NYX | 0:85b3fd62ea1a | 3040 | * @{ |
NYX | 0:85b3fd62ea1a | 3041 | */ |
NYX | 0:85b3fd62ea1a | 3042 | #define CRYP_CCF_TIMEOUTVALUE 22000U /*!< CCF flag raising time-out value */ |
NYX | 0:85b3fd62ea1a | 3043 | #define CRYP_BUSY_TIMEOUTVALUE 22000U /*!< BUSY flag reset time-out value */ |
NYX | 0:85b3fd62ea1a | 3044 | |
NYX | 0:85b3fd62ea1a | 3045 | #define CRYP_POLLING_OFF 0x0U /*!< No polling when padding */ |
NYX | 0:85b3fd62ea1a | 3046 | #define CRYP_POLLING_ON 0x1U /*!< Polling when padding */ |
NYX | 0:85b3fd62ea1a | 3047 | /** |
NYX | 0:85b3fd62ea1a | 3048 | * @} |
NYX | 0:85b3fd62ea1a | 3049 | */ |
NYX | 0:85b3fd62ea1a | 3050 | |
NYX | 0:85b3fd62ea1a | 3051 | /* Private macro -------------------------------------------------------------*/ |
NYX | 0:85b3fd62ea1a | 3052 | /* Private variables ---------------------------------------------------------*/ |
NYX | 0:85b3fd62ea1a | 3053 | /* Private function prototypes -----------------------------------------------*/ |
NYX | 0:85b3fd62ea1a | 3054 | /** @defgroup CRYPEx_Private_Functions CRYPEx Private Functions |
NYX | 0:85b3fd62ea1a | 3055 | * @{ |
NYX | 0:85b3fd62ea1a | 3056 | */ |
NYX | 0:85b3fd62ea1a | 3057 | static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout); |
NYX | 0:85b3fd62ea1a | 3058 | static HAL_StatusTypeDef CRYP_ReadKey(CRYP_HandleTypeDef *hcryp, uint8_t* Output, uint32_t Timeout); |
NYX | 0:85b3fd62ea1a | 3059 | static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr); |
NYX | 0:85b3fd62ea1a | 3060 | static void CRYP_GCMCMAC_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr); |
NYX | 0:85b3fd62ea1a | 3061 | static void CRYP_GCMCMAC_DMAInCplt(DMA_HandleTypeDef *hdma); |
NYX | 0:85b3fd62ea1a | 3062 | static void CRYP_GCMCMAC_DMAError(DMA_HandleTypeDef *hdma); |
NYX | 0:85b3fd62ea1a | 3063 | static void CRYP_GCMCMAC_DMAOutCplt(DMA_HandleTypeDef *hdma); |
NYX | 0:85b3fd62ea1a | 3064 | static HAL_StatusTypeDef CRYP_WaitOnCCFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout); |
NYX | 0:85b3fd62ea1a | 3065 | static HAL_StatusTypeDef CRYP_WaitOnBusyFlagReset(CRYP_HandleTypeDef *hcryp, uint32_t Timeout); |
NYX | 0:85b3fd62ea1a | 3066 | static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma); |
NYX | 0:85b3fd62ea1a | 3067 | static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma); |
NYX | 0:85b3fd62ea1a | 3068 | static void CRYP_DMAError(DMA_HandleTypeDef *hdma); |
NYX | 0:85b3fd62ea1a | 3069 | static void CRYP_Padding(CRYP_HandleTypeDef *hcryp, uint32_t difflength, uint32_t polling); |
NYX | 0:85b3fd62ea1a | 3070 | /** |
NYX | 0:85b3fd62ea1a | 3071 | * @} |
NYX | 0:85b3fd62ea1a | 3072 | */ |
NYX | 0:85b3fd62ea1a | 3073 | |
NYX | 0:85b3fd62ea1a | 3074 | /* Exported functions ---------------------------------------------------------*/ |
NYX | 0:85b3fd62ea1a | 3075 | |
NYX | 0:85b3fd62ea1a | 3076 | /** @defgroup CRYPEx_Exported_Functions CRYPEx Exported Functions |
NYX | 0:85b3fd62ea1a | 3077 | * @{ |
NYX | 0:85b3fd62ea1a | 3078 | */ |
NYX | 0:85b3fd62ea1a | 3079 | |
NYX | 0:85b3fd62ea1a | 3080 | |
NYX | 0:85b3fd62ea1a | 3081 | /** @defgroup CRYPEx_Exported_Functions_Group1 Extended callback function |
NYX | 0:85b3fd62ea1a | 3082 | * @brief Extended callback functions. |
NYX | 0:85b3fd62ea1a | 3083 | * |
NYX | 0:85b3fd62ea1a | 3084 | @verbatim |
NYX | 0:85b3fd62ea1a | 3085 | =============================================================================== |
NYX | 0:85b3fd62ea1a | 3086 | ##### Extended callback functions ##### |
NYX | 0:85b3fd62ea1a | 3087 | =============================================================================== |
NYX | 0:85b3fd62ea1a | 3088 | [..] This section provides callback function: |
NYX | 0:85b3fd62ea1a | 3089 | (+) Computation completed. |
NYX | 0:85b3fd62ea1a | 3090 | |
NYX | 0:85b3fd62ea1a | 3091 | @endverbatim |
NYX | 0:85b3fd62ea1a | 3092 | * @{ |
NYX | 0:85b3fd62ea1a | 3093 | */ |
NYX | 0:85b3fd62ea1a | 3094 | |
NYX | 0:85b3fd62ea1a | 3095 | |
NYX | 0:85b3fd62ea1a | 3096 | /** |
NYX | 0:85b3fd62ea1a | 3097 | * @brief Computation completed callbacks. |
NYX | 0:85b3fd62ea1a | 3098 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 3099 | * the configuration information for CRYP module |
NYX | 0:85b3fd62ea1a | 3100 | * @retval None |
NYX | 0:85b3fd62ea1a | 3101 | */ |
NYX | 0:85b3fd62ea1a | 3102 | __weak void HAL_CRYPEx_ComputationCpltCallback(CRYP_HandleTypeDef *hcryp) |
NYX | 0:85b3fd62ea1a | 3103 | { |
NYX | 0:85b3fd62ea1a | 3104 | /* Prevent unused argument(s) compilation warning */ |
NYX | 0:85b3fd62ea1a | 3105 | UNUSED(hcryp); |
NYX | 0:85b3fd62ea1a | 3106 | |
NYX | 0:85b3fd62ea1a | 3107 | /* NOTE : This function should not be modified; when the callback is needed, |
NYX | 0:85b3fd62ea1a | 3108 | the HAL_CRYPEx_ComputationCpltCallback can be implemented in the user file |
NYX | 0:85b3fd62ea1a | 3109 | */ |
NYX | 0:85b3fd62ea1a | 3110 | } |
NYX | 0:85b3fd62ea1a | 3111 | |
NYX | 0:85b3fd62ea1a | 3112 | /** |
NYX | 0:85b3fd62ea1a | 3113 | * @} |
NYX | 0:85b3fd62ea1a | 3114 | */ |
NYX | 0:85b3fd62ea1a | 3115 | |
NYX | 0:85b3fd62ea1a | 3116 | /** @defgroup CRYPEx_Exported_Functions_Group2 AES extended processing functions |
NYX | 0:85b3fd62ea1a | 3117 | * @brief Extended processing functions. |
NYX | 0:85b3fd62ea1a | 3118 | * |
NYX | 0:85b3fd62ea1a | 3119 | @verbatim |
NYX | 0:85b3fd62ea1a | 3120 | ============================================================================== |
NYX | 0:85b3fd62ea1a | 3121 | ##### AES extended processing functions ##### |
NYX | 0:85b3fd62ea1a | 3122 | ============================================================================== |
NYX | 0:85b3fd62ea1a | 3123 | [..] This section provides functions allowing to: |
NYX | 0:85b3fd62ea1a | 3124 | (+) Encrypt plaintext or decrypt cipher text using AES algorithm in different chaining modes. |
NYX | 0:85b3fd62ea1a | 3125 | Functions are generic (handles ECB, CBC and CTR and all modes) and are only differentiated |
NYX | 0:85b3fd62ea1a | 3126 | based on the processing type. Three processing types are available: |
NYX | 0:85b3fd62ea1a | 3127 | (++) Polling mode |
NYX | 0:85b3fd62ea1a | 3128 | (++) Interrupt mode |
NYX | 0:85b3fd62ea1a | 3129 | (++) DMA mode |
NYX | 0:85b3fd62ea1a | 3130 | (+) Generate and authentication tag in addition to encrypt/decrypt a plain/cipher text using AES |
NYX | 0:85b3fd62ea1a | 3131 | algorithm in different chaining modes. |
NYX | 0:85b3fd62ea1a | 3132 | Functions are generic (handles GCM, GMAC, CMAC and CCM when applicable) and process only one phase |
NYX | 0:85b3fd62ea1a | 3133 | so that steps can be skipped if so required. Functions are only differentiated based on the processing type. |
NYX | 0:85b3fd62ea1a | 3134 | Three processing types are available: |
NYX | 0:85b3fd62ea1a | 3135 | (++) Polling mode |
NYX | 0:85b3fd62ea1a | 3136 | (++) Interrupt mode |
NYX | 0:85b3fd62ea1a | 3137 | (++) DMA mode |
NYX | 0:85b3fd62ea1a | 3138 | |
NYX | 0:85b3fd62ea1a | 3139 | @endverbatim |
NYX | 0:85b3fd62ea1a | 3140 | * @{ |
NYX | 0:85b3fd62ea1a | 3141 | */ |
NYX | 0:85b3fd62ea1a | 3142 | |
NYX | 0:85b3fd62ea1a | 3143 | /** |
NYX | 0:85b3fd62ea1a | 3144 | * @brief Carry out in polling mode the ciphering or deciphering operation according to |
NYX | 0:85b3fd62ea1a | 3145 | * hcryp->Init structure fields, all operating modes (encryption, key derivation and/or decryption) and |
NYX | 0:85b3fd62ea1a | 3146 | * chaining modes ECB, CBC and CTR are managed by this function in polling mode. |
NYX | 0:85b3fd62ea1a | 3147 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 3148 | * the configuration information for CRYP module |
NYX | 0:85b3fd62ea1a | 3149 | * @param pInputData: Pointer to the plain text in case of encryption or cipher text in case of decryption |
NYX | 0:85b3fd62ea1a | 3150 | * or key derivation+decryption. |
NYX | 0:85b3fd62ea1a | 3151 | * Parameter is meaningless in case of key derivation. |
NYX | 0:85b3fd62ea1a | 3152 | * @param Size: Length of the input data buffer in bytes, must be a multiple of 16. |
NYX | 0:85b3fd62ea1a | 3153 | * Parameter is meaningless in case of key derivation. |
NYX | 0:85b3fd62ea1a | 3154 | * @param pOutputData: Pointer to the cipher text in case of encryption or plain text in case of |
NYX | 0:85b3fd62ea1a | 3155 | * decryption/key derivation+decryption, or pointer to the derivative keys in |
NYX | 0:85b3fd62ea1a | 3156 | * case of key derivation only. |
NYX | 0:85b3fd62ea1a | 3157 | * @param Timeout: Specify Timeout value |
NYX | 0:85b3fd62ea1a | 3158 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 3159 | */ |
NYX | 0:85b3fd62ea1a | 3160 | HAL_StatusTypeDef HAL_CRYPEx_AES(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint16_t Size, uint8_t *pOutputData, uint32_t Timeout) |
NYX | 0:85b3fd62ea1a | 3161 | { |
NYX | 0:85b3fd62ea1a | 3162 | |
NYX | 0:85b3fd62ea1a | 3163 | if (hcryp->State == HAL_CRYP_STATE_READY) |
NYX | 0:85b3fd62ea1a | 3164 | { |
NYX | 0:85b3fd62ea1a | 3165 | /* Check parameters setting */ |
NYX | 0:85b3fd62ea1a | 3166 | if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION) |
NYX | 0:85b3fd62ea1a | 3167 | { |
NYX | 0:85b3fd62ea1a | 3168 | if (pOutputData == NULL) |
NYX | 0:85b3fd62ea1a | 3169 | { |
NYX | 0:85b3fd62ea1a | 3170 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 3171 | } |
NYX | 0:85b3fd62ea1a | 3172 | } |
NYX | 0:85b3fd62ea1a | 3173 | else |
NYX | 0:85b3fd62ea1a | 3174 | { |
NYX | 0:85b3fd62ea1a | 3175 | if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0U)) |
NYX | 0:85b3fd62ea1a | 3176 | { |
NYX | 0:85b3fd62ea1a | 3177 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 3178 | } |
NYX | 0:85b3fd62ea1a | 3179 | } |
NYX | 0:85b3fd62ea1a | 3180 | |
NYX | 0:85b3fd62ea1a | 3181 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 3182 | __HAL_LOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 3183 | |
NYX | 0:85b3fd62ea1a | 3184 | /* Change the CRYP state */ |
NYX | 0:85b3fd62ea1a | 3185 | hcryp->State = HAL_CRYP_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 3186 | |
NYX | 0:85b3fd62ea1a | 3187 | /* Call CRYP_ReadKey() API if the operating mode is set to |
NYX | 0:85b3fd62ea1a | 3188 | key derivation, CRYP_ProcessData() otherwise */ |
NYX | 0:85b3fd62ea1a | 3189 | if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION) |
NYX | 0:85b3fd62ea1a | 3190 | { |
NYX | 0:85b3fd62ea1a | 3191 | if(CRYP_ReadKey(hcryp, pOutputData, Timeout) != HAL_OK) |
NYX | 0:85b3fd62ea1a | 3192 | { |
NYX | 0:85b3fd62ea1a | 3193 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 3194 | } |
NYX | 0:85b3fd62ea1a | 3195 | } |
NYX | 0:85b3fd62ea1a | 3196 | else |
NYX | 0:85b3fd62ea1a | 3197 | { |
NYX | 0:85b3fd62ea1a | 3198 | if(CRYP_ProcessData(hcryp, pInputData, Size, pOutputData, Timeout) != HAL_OK) |
NYX | 0:85b3fd62ea1a | 3199 | { |
NYX | 0:85b3fd62ea1a | 3200 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 3201 | } |
NYX | 0:85b3fd62ea1a | 3202 | } |
NYX | 0:85b3fd62ea1a | 3203 | |
NYX | 0:85b3fd62ea1a | 3204 | /* If the state has not been set to SUSPENDED, set it to |
NYX | 0:85b3fd62ea1a | 3205 | READY, otherwise keep it as it is */ |
NYX | 0:85b3fd62ea1a | 3206 | if (hcryp->State != HAL_CRYP_STATE_SUSPENDED) |
NYX | 0:85b3fd62ea1a | 3207 | { |
NYX | 0:85b3fd62ea1a | 3208 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 3209 | } |
NYX | 0:85b3fd62ea1a | 3210 | |
NYX | 0:85b3fd62ea1a | 3211 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 3212 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 3213 | |
NYX | 0:85b3fd62ea1a | 3214 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 3215 | } |
NYX | 0:85b3fd62ea1a | 3216 | else |
NYX | 0:85b3fd62ea1a | 3217 | { |
NYX | 0:85b3fd62ea1a | 3218 | return HAL_BUSY; |
NYX | 0:85b3fd62ea1a | 3219 | } |
NYX | 0:85b3fd62ea1a | 3220 | } |
NYX | 0:85b3fd62ea1a | 3221 | |
NYX | 0:85b3fd62ea1a | 3222 | /** |
NYX | 0:85b3fd62ea1a | 3223 | * @brief Carry out in interrupt mode the ciphering or deciphering operation according to |
NYX | 0:85b3fd62ea1a | 3224 | * hcryp->Init structure fields, all operating modes (encryption, key derivation and/or decryption) and |
NYX | 0:85b3fd62ea1a | 3225 | * chaining modes ECB, CBC and CTR are managed by this function in interrupt mode. |
NYX | 0:85b3fd62ea1a | 3226 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 3227 | * the configuration information for CRYP module |
NYX | 0:85b3fd62ea1a | 3228 | * @param pInputData: Pointer to the plain text in case of encryption or cipher text in case of decryption |
NYX | 0:85b3fd62ea1a | 3229 | * or key derivation+decryption. |
NYX | 0:85b3fd62ea1a | 3230 | * Parameter is meaningless in case of key derivation. |
NYX | 0:85b3fd62ea1a | 3231 | * @param Size: Length of the input data buffer in bytes, must be a multiple of 16. |
NYX | 0:85b3fd62ea1a | 3232 | * Parameter is meaningless in case of key derivation. |
NYX | 0:85b3fd62ea1a | 3233 | * @param pOutputData: Pointer to the cipher text in case of encryption or plain text in case of |
NYX | 0:85b3fd62ea1a | 3234 | * decryption/key derivation+decryption, or pointer to the derivative keys in |
NYX | 0:85b3fd62ea1a | 3235 | * case of key derivation only. |
NYX | 0:85b3fd62ea1a | 3236 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 3237 | */ |
NYX | 0:85b3fd62ea1a | 3238 | HAL_StatusTypeDef HAL_CRYPEx_AES_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint16_t Size, uint8_t *pOutputData) |
NYX | 0:85b3fd62ea1a | 3239 | { |
NYX | 0:85b3fd62ea1a | 3240 | uint32_t inputaddr = 0U; |
NYX | 0:85b3fd62ea1a | 3241 | |
NYX | 0:85b3fd62ea1a | 3242 | if(hcryp->State == HAL_CRYP_STATE_READY) |
NYX | 0:85b3fd62ea1a | 3243 | { |
NYX | 0:85b3fd62ea1a | 3244 | /* Check parameters setting */ |
NYX | 0:85b3fd62ea1a | 3245 | if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION) |
NYX | 0:85b3fd62ea1a | 3246 | { |
NYX | 0:85b3fd62ea1a | 3247 | if (pOutputData == NULL) |
NYX | 0:85b3fd62ea1a | 3248 | { |
NYX | 0:85b3fd62ea1a | 3249 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 3250 | } |
NYX | 0:85b3fd62ea1a | 3251 | } |
NYX | 0:85b3fd62ea1a | 3252 | else |
NYX | 0:85b3fd62ea1a | 3253 | { |
NYX | 0:85b3fd62ea1a | 3254 | if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0U)) |
NYX | 0:85b3fd62ea1a | 3255 | { |
NYX | 0:85b3fd62ea1a | 3256 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 3257 | } |
NYX | 0:85b3fd62ea1a | 3258 | } |
NYX | 0:85b3fd62ea1a | 3259 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 3260 | __HAL_LOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 3261 | |
NYX | 0:85b3fd62ea1a | 3262 | /* If operating mode is not limited to key derivation only, |
NYX | 0:85b3fd62ea1a | 3263 | get the buffers addresses and sizes */ |
NYX | 0:85b3fd62ea1a | 3264 | if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION) |
NYX | 0:85b3fd62ea1a | 3265 | { |
NYX | 0:85b3fd62ea1a | 3266 | |
NYX | 0:85b3fd62ea1a | 3267 | hcryp->CrypInCount = Size; |
NYX | 0:85b3fd62ea1a | 3268 | hcryp->pCrypInBuffPtr = pInputData; |
NYX | 0:85b3fd62ea1a | 3269 | hcryp->pCrypOutBuffPtr = pOutputData; |
NYX | 0:85b3fd62ea1a | 3270 | hcryp->CrypOutCount = Size; |
NYX | 0:85b3fd62ea1a | 3271 | } |
NYX | 0:85b3fd62ea1a | 3272 | |
NYX | 0:85b3fd62ea1a | 3273 | /* Change the CRYP state */ |
NYX | 0:85b3fd62ea1a | 3274 | hcryp->State = HAL_CRYP_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 3275 | |
NYX | 0:85b3fd62ea1a | 3276 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 3277 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 3278 | |
NYX | 0:85b3fd62ea1a | 3279 | /* Enable Computation Complete Flag and Error Interrupts */ |
NYX | 0:85b3fd62ea1a | 3280 | __HAL_CRYP_ENABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); |
NYX | 0:85b3fd62ea1a | 3281 | |
NYX | 0:85b3fd62ea1a | 3282 | /* If operating mode is key derivation only, the input data have |
NYX | 0:85b3fd62ea1a | 3283 | already been entered during the initialization process. For |
NYX | 0:85b3fd62ea1a | 3284 | the other operating modes, they are fed to the CRYP hardware |
NYX | 0:85b3fd62ea1a | 3285 | block at this point. */ |
NYX | 0:85b3fd62ea1a | 3286 | if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION) |
NYX | 0:85b3fd62ea1a | 3287 | { |
NYX | 0:85b3fd62ea1a | 3288 | /* Initiate the processing under interrupt in entering |
NYX | 0:85b3fd62ea1a | 3289 | the first input data */ |
NYX | 0:85b3fd62ea1a | 3290 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
NYX | 0:85b3fd62ea1a | 3291 | /* Increment/decrement instance pointer/counter */ |
NYX | 0:85b3fd62ea1a | 3292 | hcryp->pCrypInBuffPtr += 16U; |
NYX | 0:85b3fd62ea1a | 3293 | hcryp->CrypInCount -= 16U; |
NYX | 0:85b3fd62ea1a | 3294 | /* Write the first input block in the Data Input register */ |
NYX | 0:85b3fd62ea1a | 3295 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 3296 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 3297 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 3298 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 3299 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 3300 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 3301 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 3302 | } |
NYX | 0:85b3fd62ea1a | 3303 | |
NYX | 0:85b3fd62ea1a | 3304 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 3305 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 3306 | } |
NYX | 0:85b3fd62ea1a | 3307 | else |
NYX | 0:85b3fd62ea1a | 3308 | { |
NYX | 0:85b3fd62ea1a | 3309 | return HAL_BUSY; |
NYX | 0:85b3fd62ea1a | 3310 | } |
NYX | 0:85b3fd62ea1a | 3311 | } |
NYX | 0:85b3fd62ea1a | 3312 | |
NYX | 0:85b3fd62ea1a | 3313 | /** |
NYX | 0:85b3fd62ea1a | 3314 | * @brief Carry out in DMA mode the ciphering or deciphering operation according to |
NYX | 0:85b3fd62ea1a | 3315 | * hcryp->Init structure fields. |
NYX | 0:85b3fd62ea1a | 3316 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 3317 | * the configuration information for CRYP module |
NYX | 0:85b3fd62ea1a | 3318 | * @param pInputData: Pointer to the plain text in case of encryption or cipher text in case of decryption |
NYX | 0:85b3fd62ea1a | 3319 | * or key derivation+decryption. |
NYX | 0:85b3fd62ea1a | 3320 | * @param Size: Length of the input data buffer in bytes, must be a multiple of 16. |
NYX | 0:85b3fd62ea1a | 3321 | * @param pOutputData: Pointer to the cipher text in case of encryption or plain text in case of |
NYX | 0:85b3fd62ea1a | 3322 | * decryption/key derivation+decryption. |
NYX | 0:85b3fd62ea1a | 3323 | * @note Chaining modes ECB, CBC and CTR are managed by this function in DMA mode. |
NYX | 0:85b3fd62ea1a | 3324 | * @note Supported operating modes are encryption, decryption and key derivation with decryption. |
NYX | 0:85b3fd62ea1a | 3325 | * @note No DMA channel is provided for key derivation only and therefore, access to AES_KEYRx |
NYX | 0:85b3fd62ea1a | 3326 | * registers must be done by software. |
NYX | 0:85b3fd62ea1a | 3327 | * @note This API is not applicable to key derivation only; for such a mode, access to AES_KEYRx |
NYX | 0:85b3fd62ea1a | 3328 | * registers must be done by software thru HAL_CRYPEx_AES() or HAL_CRYPEx_AES_IT() APIs. |
NYX | 0:85b3fd62ea1a | 3329 | * @note pInputData and pOutputData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP. |
NYX | 0:85b3fd62ea1a | 3330 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 3331 | */ |
NYX | 0:85b3fd62ea1a | 3332 | HAL_StatusTypeDef HAL_CRYPEx_AES_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint16_t Size, uint8_t *pOutputData) |
NYX | 0:85b3fd62ea1a | 3333 | { |
NYX | 0:85b3fd62ea1a | 3334 | uint32_t inputaddr = 0U; |
NYX | 0:85b3fd62ea1a | 3335 | uint32_t outputaddr = 0U; |
NYX | 0:85b3fd62ea1a | 3336 | |
NYX | 0:85b3fd62ea1a | 3337 | if (hcryp->State == HAL_CRYP_STATE_READY) |
NYX | 0:85b3fd62ea1a | 3338 | { |
NYX | 0:85b3fd62ea1a | 3339 | /* Check parameters setting */ |
NYX | 0:85b3fd62ea1a | 3340 | if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION) |
NYX | 0:85b3fd62ea1a | 3341 | { |
NYX | 0:85b3fd62ea1a | 3342 | /* no DMA channel is provided for key derivation operating mode, |
NYX | 0:85b3fd62ea1a | 3343 | access to AES_KEYRx registers must be done by software */ |
NYX | 0:85b3fd62ea1a | 3344 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 3345 | } |
NYX | 0:85b3fd62ea1a | 3346 | else |
NYX | 0:85b3fd62ea1a | 3347 | { |
NYX | 0:85b3fd62ea1a | 3348 | if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0U)) |
NYX | 0:85b3fd62ea1a | 3349 | { |
NYX | 0:85b3fd62ea1a | 3350 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 3351 | } |
NYX | 0:85b3fd62ea1a | 3352 | } |
NYX | 0:85b3fd62ea1a | 3353 | |
NYX | 0:85b3fd62ea1a | 3354 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 3355 | __HAL_LOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 3356 | |
NYX | 0:85b3fd62ea1a | 3357 | inputaddr = (uint32_t)pInputData; |
NYX | 0:85b3fd62ea1a | 3358 | outputaddr = (uint32_t)pOutputData; |
NYX | 0:85b3fd62ea1a | 3359 | |
NYX | 0:85b3fd62ea1a | 3360 | /* Change the CRYP state */ |
NYX | 0:85b3fd62ea1a | 3361 | hcryp->State = HAL_CRYP_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 3362 | |
NYX | 0:85b3fd62ea1a | 3363 | /* Set the input and output addresses and start DMA transfer */ |
NYX | 0:85b3fd62ea1a | 3364 | CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); |
NYX | 0:85b3fd62ea1a | 3365 | |
NYX | 0:85b3fd62ea1a | 3366 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 3367 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 3368 | |
NYX | 0:85b3fd62ea1a | 3369 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 3370 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 3371 | } |
NYX | 0:85b3fd62ea1a | 3372 | else |
NYX | 0:85b3fd62ea1a | 3373 | { |
NYX | 0:85b3fd62ea1a | 3374 | return HAL_BUSY; |
NYX | 0:85b3fd62ea1a | 3375 | } |
NYX | 0:85b3fd62ea1a | 3376 | } |
NYX | 0:85b3fd62ea1a | 3377 | |
NYX | 0:85b3fd62ea1a | 3378 | /** |
NYX | 0:85b3fd62ea1a | 3379 | * @brief Carry out in polling mode the authentication tag generation as well as the ciphering or deciphering |
NYX | 0:85b3fd62ea1a | 3380 | * operation according to hcryp->Init structure fields. |
NYX | 0:85b3fd62ea1a | 3381 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 3382 | * the configuration information for CRYP module |
NYX | 0:85b3fd62ea1a | 3383 | * @param pInputData: |
NYX | 0:85b3fd62ea1a | 3384 | * - pointer to payload data in GCM payload phase, |
NYX | 0:85b3fd62ea1a | 3385 | * - pointer to B0 block in CMAC header phase, |
NYX | 0:85b3fd62ea1a | 3386 | * - pointer to C block in CMAC final phase. |
NYX | 0:85b3fd62ea1a | 3387 | * - Parameter is meaningless in case of GCM/GMAC init, header and final phases. |
NYX | 0:85b3fd62ea1a | 3388 | * @param Size: |
NYX | 0:85b3fd62ea1a | 3389 | * - length of the input payload data buffer in bytes, |
NYX | 0:85b3fd62ea1a | 3390 | * - length of B0 block (in bytes) in CMAC header phase, |
NYX | 0:85b3fd62ea1a | 3391 | * - length of C block (in bytes) in CMAC final phase. |
NYX | 0:85b3fd62ea1a | 3392 | * - Parameter is meaningless in case of GCM/GMAC init and header phases. |
NYX | 0:85b3fd62ea1a | 3393 | * @param pOutputData: |
NYX | 0:85b3fd62ea1a | 3394 | * - pointer to plain or cipher text in GCM payload phase, |
NYX | 0:85b3fd62ea1a | 3395 | * - pointer to authentication tag in GCM/GMAC and CMAC final phases. |
NYX | 0:85b3fd62ea1a | 3396 | * - Parameter is meaningless in case of GCM/GMAC init and header phases |
NYX | 0:85b3fd62ea1a | 3397 | * and in case of CMAC header phase. |
NYX | 0:85b3fd62ea1a | 3398 | * @param Timeout: Specify Timeout value |
NYX | 0:85b3fd62ea1a | 3399 | * @note Supported operating modes are encryption and decryption, supported chaining modes are GCM, GMAC, CMAC and CCM when the latter is applicable. |
NYX | 0:85b3fd62ea1a | 3400 | * @note Phases are singly processed according to hcryp->Init.GCMCMACPhase so that steps in these specific chaining modes |
NYX | 0:85b3fd62ea1a | 3401 | * can be skipped by the user if so required. |
NYX | 0:85b3fd62ea1a | 3402 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 3403 | */ |
NYX | 0:85b3fd62ea1a | 3404 | HAL_StatusTypeDef HAL_CRYPEx_AES_Auth(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint64_t Size, uint8_t *pOutputData, uint32_t Timeout) |
NYX | 0:85b3fd62ea1a | 3405 | { |
NYX | 0:85b3fd62ea1a | 3406 | uint32_t index = 0U; |
NYX | 0:85b3fd62ea1a | 3407 | uint32_t inputaddr = 0U; |
NYX | 0:85b3fd62ea1a | 3408 | uint32_t outputaddr = 0U; |
NYX | 0:85b3fd62ea1a | 3409 | uint32_t tagaddr = 0U; |
NYX | 0:85b3fd62ea1a | 3410 | uint64_t headerlength = 0U; |
NYX | 0:85b3fd62ea1a | 3411 | uint64_t inputlength = 0U; |
NYX | 0:85b3fd62ea1a | 3412 | uint64_t payloadlength = 0U; |
NYX | 0:85b3fd62ea1a | 3413 | uint32_t difflength = 0U; |
NYX | 0:85b3fd62ea1a | 3414 | uint32_t addhoc_process = 0U; |
NYX | 0:85b3fd62ea1a | 3415 | |
NYX | 0:85b3fd62ea1a | 3416 | if (hcryp->State == HAL_CRYP_STATE_READY) |
NYX | 0:85b3fd62ea1a | 3417 | { |
NYX | 0:85b3fd62ea1a | 3418 | /* input/output parameters check */ |
NYX | 0:85b3fd62ea1a | 3419 | if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE) |
NYX | 0:85b3fd62ea1a | 3420 | { |
NYX | 0:85b3fd62ea1a | 3421 | if ((hcryp->Init.Header != NULL) && (hcryp->Init.HeaderSize == 0U)) |
NYX | 0:85b3fd62ea1a | 3422 | { |
NYX | 0:85b3fd62ea1a | 3423 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 3424 | } |
NYX | 0:85b3fd62ea1a | 3425 | #if defined(AES_CR_NPBLB) |
NYX | 0:85b3fd62ea1a | 3426 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC) |
NYX | 0:85b3fd62ea1a | 3427 | #else |
NYX | 0:85b3fd62ea1a | 3428 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) |
NYX | 0:85b3fd62ea1a | 3429 | #endif |
NYX | 0:85b3fd62ea1a | 3430 | { |
NYX | 0:85b3fd62ea1a | 3431 | /* In case of CMAC (or CCM) header phase resumption, we can have pInputData = NULL and Size = 0 */ |
NYX | 0:85b3fd62ea1a | 3432 | if (((pInputData != NULL) && (Size == 0U)) || ((pInputData == NULL) && (Size != 0U))) |
NYX | 0:85b3fd62ea1a | 3433 | { |
NYX | 0:85b3fd62ea1a | 3434 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 3435 | } |
NYX | 0:85b3fd62ea1a | 3436 | } |
NYX | 0:85b3fd62ea1a | 3437 | } |
NYX | 0:85b3fd62ea1a | 3438 | else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE) |
NYX | 0:85b3fd62ea1a | 3439 | { |
NYX | 0:85b3fd62ea1a | 3440 | if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0U)) |
NYX | 0:85b3fd62ea1a | 3441 | { |
NYX | 0:85b3fd62ea1a | 3442 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 3443 | } |
NYX | 0:85b3fd62ea1a | 3444 | } |
NYX | 0:85b3fd62ea1a | 3445 | else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE) |
NYX | 0:85b3fd62ea1a | 3446 | { |
NYX | 0:85b3fd62ea1a | 3447 | if (pOutputData == NULL) |
NYX | 0:85b3fd62ea1a | 3448 | { |
NYX | 0:85b3fd62ea1a | 3449 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 3450 | } |
NYX | 0:85b3fd62ea1a | 3451 | #if defined(AES_CR_NPBLB) |
NYX | 0:85b3fd62ea1a | 3452 | if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC) && (pInputData == NULL)) |
NYX | 0:85b3fd62ea1a | 3453 | #else |
NYX | 0:85b3fd62ea1a | 3454 | if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (pInputData == NULL)) |
NYX | 0:85b3fd62ea1a | 3455 | #endif |
NYX | 0:85b3fd62ea1a | 3456 | { |
NYX | 0:85b3fd62ea1a | 3457 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 3458 | } |
NYX | 0:85b3fd62ea1a | 3459 | } |
NYX | 0:85b3fd62ea1a | 3460 | |
NYX | 0:85b3fd62ea1a | 3461 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 3462 | __HAL_LOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 3463 | |
NYX | 0:85b3fd62ea1a | 3464 | /* Change the CRYP state */ |
NYX | 0:85b3fd62ea1a | 3465 | hcryp->State = HAL_CRYP_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 3466 | |
NYX | 0:85b3fd62ea1a | 3467 | /*==============================================*/ |
NYX | 0:85b3fd62ea1a | 3468 | /* GCM/GMAC (or CCM when applicable) init phase */ |
NYX | 0:85b3fd62ea1a | 3469 | /*==============================================*/ |
NYX | 0:85b3fd62ea1a | 3470 | /* In case of init phase, the input data (Key and Initialization Vector) have |
NYX | 0:85b3fd62ea1a | 3471 | already been entered during the initialization process. Therefore, the |
NYX | 0:85b3fd62ea1a | 3472 | API just waits for the CCF flag to be set. */ |
NYX | 0:85b3fd62ea1a | 3473 | if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE) |
NYX | 0:85b3fd62ea1a | 3474 | { |
NYX | 0:85b3fd62ea1a | 3475 | /* just wait for hash computation */ |
NYX | 0:85b3fd62ea1a | 3476 | if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) |
NYX | 0:85b3fd62ea1a | 3477 | { |
NYX | 0:85b3fd62ea1a | 3478 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 3479 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 3480 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 3481 | } |
NYX | 0:85b3fd62ea1a | 3482 | |
NYX | 0:85b3fd62ea1a | 3483 | /* Clear CCF Flag */ |
NYX | 0:85b3fd62ea1a | 3484 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
NYX | 0:85b3fd62ea1a | 3485 | /* Mark that the initialization phase is over */ |
NYX | 0:85b3fd62ea1a | 3486 | hcryp->Phase = HAL_CRYP_PHASE_INIT_OVER; |
NYX | 0:85b3fd62ea1a | 3487 | } |
NYX | 0:85b3fd62ea1a | 3488 | /*=====================================*/ |
NYX | 0:85b3fd62ea1a | 3489 | /* GCM/GMAC or (CCM/)CMAC header phase */ |
NYX | 0:85b3fd62ea1a | 3490 | /*=====================================*/ |
NYX | 0:85b3fd62ea1a | 3491 | else if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE) |
NYX | 0:85b3fd62ea1a | 3492 | { |
NYX | 0:85b3fd62ea1a | 3493 | /* Set header phase; for GCM or GMAC, set data-byte at this point */ |
NYX | 0:85b3fd62ea1a | 3494 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) |
NYX | 0:85b3fd62ea1a | 3495 | { |
NYX | 0:85b3fd62ea1a | 3496 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH|AES_CR_DATATYPE, CRYP_HEADER_PHASE|hcryp->Init.DataType); |
NYX | 0:85b3fd62ea1a | 3497 | } |
NYX | 0:85b3fd62ea1a | 3498 | else |
NYX | 0:85b3fd62ea1a | 3499 | { |
NYX | 0:85b3fd62ea1a | 3500 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_HEADER_PHASE); |
NYX | 0:85b3fd62ea1a | 3501 | } |
NYX | 0:85b3fd62ea1a | 3502 | |
NYX | 0:85b3fd62ea1a | 3503 | /* Enable the Peripheral */ |
NYX | 0:85b3fd62ea1a | 3504 | __HAL_CRYP_ENABLE(); |
NYX | 0:85b3fd62ea1a | 3505 | |
NYX | 0:85b3fd62ea1a | 3506 | #if !defined(AES_CR_NPBLB) |
NYX | 0:85b3fd62ea1a | 3507 | /* in case of CMAC, enter B0 block in header phase, before the header itself. */ |
NYX | 0:85b3fd62ea1a | 3508 | /* If Size = 0 (possible case of resumption after CMAC header phase suspension), |
NYX | 0:85b3fd62ea1a | 3509 | skip these steps and go directly to header buffer feeding to the HW */ |
NYX | 0:85b3fd62ea1a | 3510 | if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (Size != 0U)) |
NYX | 0:85b3fd62ea1a | 3511 | { |
NYX | 0:85b3fd62ea1a | 3512 | inputaddr = (uint32_t)pInputData; |
NYX | 0:85b3fd62ea1a | 3513 | |
NYX | 0:85b3fd62ea1a | 3514 | for(index=0U; (index < Size); index += 16U) |
NYX | 0:85b3fd62ea1a | 3515 | { |
NYX | 0:85b3fd62ea1a | 3516 | /* Write the Input block in the Data Input register */ |
NYX | 0:85b3fd62ea1a | 3517 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 3518 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 3519 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 3520 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 3521 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 3522 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 3523 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 3524 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 3525 | |
NYX | 0:85b3fd62ea1a | 3526 | if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) |
NYX | 0:85b3fd62ea1a | 3527 | { |
NYX | 0:85b3fd62ea1a | 3528 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 3529 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 3530 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 3531 | } |
NYX | 0:85b3fd62ea1a | 3532 | /* Clear CCF Flag */ |
NYX | 0:85b3fd62ea1a | 3533 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
NYX | 0:85b3fd62ea1a | 3534 | |
NYX | 0:85b3fd62ea1a | 3535 | /* If the suspension flag has been raised and if the processing is not about |
NYX | 0:85b3fd62ea1a | 3536 | to end, suspend processing */ |
NYX | 0:85b3fd62ea1a | 3537 | if ((hcryp->SuspendRequest == HAL_CRYP_SUSPEND) && ((index+16U) < Size)) |
NYX | 0:85b3fd62ea1a | 3538 | { |
NYX | 0:85b3fd62ea1a | 3539 | /* reset SuspendRequest */ |
NYX | 0:85b3fd62ea1a | 3540 | hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE; |
NYX | 0:85b3fd62ea1a | 3541 | /* Change the CRYP state */ |
NYX | 0:85b3fd62ea1a | 3542 | hcryp->State = HAL_CRYP_STATE_SUSPENDED; |
NYX | 0:85b3fd62ea1a | 3543 | /* Mark that the header phase is over */ |
NYX | 0:85b3fd62ea1a | 3544 | hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED; |
NYX | 0:85b3fd62ea1a | 3545 | |
NYX | 0:85b3fd62ea1a | 3546 | /* Save current reading and writing locations of Input and Output buffers */ |
NYX | 0:85b3fd62ea1a | 3547 | hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr; |
NYX | 0:85b3fd62ea1a | 3548 | /* Save the total number of bytes (B blocks + header) that remain to be |
NYX | 0:85b3fd62ea1a | 3549 | processed at this point */ |
NYX | 0:85b3fd62ea1a | 3550 | hcryp->CrypInCount = hcryp->Init.HeaderSize + Size - (index+16U); |
NYX | 0:85b3fd62ea1a | 3551 | |
NYX | 0:85b3fd62ea1a | 3552 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 3553 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 3554 | |
NYX | 0:85b3fd62ea1a | 3555 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 3556 | } |
NYX | 0:85b3fd62ea1a | 3557 | } /* for(index=0; (index < Size); index += 16) */ |
NYX | 0:85b3fd62ea1a | 3558 | } |
NYX | 0:85b3fd62ea1a | 3559 | #endif /* !defined(AES_CR_NPBLB) */ |
NYX | 0:85b3fd62ea1a | 3560 | |
NYX | 0:85b3fd62ea1a | 3561 | /* Enter header */ |
NYX | 0:85b3fd62ea1a | 3562 | inputaddr = (uint32_t)hcryp->Init.Header; |
NYX | 0:85b3fd62ea1a | 3563 | /* Local variable headerlength is a number of bytes multiple of 128 bits, |
NYX | 0:85b3fd62ea1a | 3564 | remaining header data (if any) are handled after this loop */ |
NYX | 0:85b3fd62ea1a | 3565 | headerlength = (((hcryp->Init.HeaderSize)/16U)*16U) ; |
NYX | 0:85b3fd62ea1a | 3566 | if ((hcryp->Init.HeaderSize % 16U) != 0U) |
NYX | 0:85b3fd62ea1a | 3567 | { |
NYX | 0:85b3fd62ea1a | 3568 | difflength = (uint32_t) (hcryp->Init.HeaderSize - headerlength); |
NYX | 0:85b3fd62ea1a | 3569 | } |
NYX | 0:85b3fd62ea1a | 3570 | for(index=0U; index < headerlength; index += 16U) |
NYX | 0:85b3fd62ea1a | 3571 | { |
NYX | 0:85b3fd62ea1a | 3572 | /* Write the Input block in the Data Input register */ |
NYX | 0:85b3fd62ea1a | 3573 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 3574 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 3575 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 3576 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 3577 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 3578 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 3579 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 3580 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 3581 | |
NYX | 0:85b3fd62ea1a | 3582 | if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) |
NYX | 0:85b3fd62ea1a | 3583 | { |
NYX | 0:85b3fd62ea1a | 3584 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 3585 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 3586 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 3587 | } |
NYX | 0:85b3fd62ea1a | 3588 | /* Clear CCF Flag */ |
NYX | 0:85b3fd62ea1a | 3589 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
NYX | 0:85b3fd62ea1a | 3590 | |
NYX | 0:85b3fd62ea1a | 3591 | /* If the suspension flag has been raised and if the processing is not about |
NYX | 0:85b3fd62ea1a | 3592 | to end, suspend processing */ |
NYX | 0:85b3fd62ea1a | 3593 | if ((hcryp->SuspendRequest == HAL_CRYP_SUSPEND) && ((index+16U) < headerlength)) |
NYX | 0:85b3fd62ea1a | 3594 | { |
NYX | 0:85b3fd62ea1a | 3595 | /* reset SuspendRequest */ |
NYX | 0:85b3fd62ea1a | 3596 | hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE; |
NYX | 0:85b3fd62ea1a | 3597 | /* Change the CRYP state */ |
NYX | 0:85b3fd62ea1a | 3598 | hcryp->State = HAL_CRYP_STATE_SUSPENDED; |
NYX | 0:85b3fd62ea1a | 3599 | /* Mark that the header phase is over */ |
NYX | 0:85b3fd62ea1a | 3600 | hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED; |
NYX | 0:85b3fd62ea1a | 3601 | |
NYX | 0:85b3fd62ea1a | 3602 | /* Save current reading and writing locations of Input and Output buffers */ |
NYX | 0:85b3fd62ea1a | 3603 | hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr; |
NYX | 0:85b3fd62ea1a | 3604 | /* Save the total number of bytes that remain to be processed at this point */ |
NYX | 0:85b3fd62ea1a | 3605 | hcryp->CrypInCount = hcryp->Init.HeaderSize - (index+16U); |
NYX | 0:85b3fd62ea1a | 3606 | |
NYX | 0:85b3fd62ea1a | 3607 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 3608 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 3609 | |
NYX | 0:85b3fd62ea1a | 3610 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 3611 | } |
NYX | 0:85b3fd62ea1a | 3612 | } |
NYX | 0:85b3fd62ea1a | 3613 | |
NYX | 0:85b3fd62ea1a | 3614 | /* Case header length is not a multiple of 16 bytes */ |
NYX | 0:85b3fd62ea1a | 3615 | if (difflength != 0U) |
NYX | 0:85b3fd62ea1a | 3616 | { |
NYX | 0:85b3fd62ea1a | 3617 | hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr; |
NYX | 0:85b3fd62ea1a | 3618 | CRYP_Padding(hcryp, difflength, CRYP_POLLING_ON); |
NYX | 0:85b3fd62ea1a | 3619 | } |
NYX | 0:85b3fd62ea1a | 3620 | |
NYX | 0:85b3fd62ea1a | 3621 | /* Mark that the header phase is over */ |
NYX | 0:85b3fd62ea1a | 3622 | hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER; |
NYX | 0:85b3fd62ea1a | 3623 | } |
NYX | 0:85b3fd62ea1a | 3624 | /*============================================*/ |
NYX | 0:85b3fd62ea1a | 3625 | /* GCM (or CCM when applicable) payload phase */ |
NYX | 0:85b3fd62ea1a | 3626 | /*============================================*/ |
NYX | 0:85b3fd62ea1a | 3627 | else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE) |
NYX | 0:85b3fd62ea1a | 3628 | { |
NYX | 0:85b3fd62ea1a | 3629 | |
NYX | 0:85b3fd62ea1a | 3630 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PAYLOAD_PHASE); |
NYX | 0:85b3fd62ea1a | 3631 | |
NYX | 0:85b3fd62ea1a | 3632 | /* if the header phase has been bypassed, AES must be enabled again */ |
NYX | 0:85b3fd62ea1a | 3633 | if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER) |
NYX | 0:85b3fd62ea1a | 3634 | { |
NYX | 0:85b3fd62ea1a | 3635 | __HAL_CRYP_ENABLE(); |
NYX | 0:85b3fd62ea1a | 3636 | } |
NYX | 0:85b3fd62ea1a | 3637 | |
NYX | 0:85b3fd62ea1a | 3638 | inputaddr = (uint32_t)pInputData; |
NYX | 0:85b3fd62ea1a | 3639 | outputaddr = (uint32_t)pOutputData; |
NYX | 0:85b3fd62ea1a | 3640 | |
NYX | 0:85b3fd62ea1a | 3641 | /* Enter payload */ |
NYX | 0:85b3fd62ea1a | 3642 | /* Specific handling to manage payload last block size less than 128 bits */ |
NYX | 0:85b3fd62ea1a | 3643 | if ((Size % 16U) != 0U) |
NYX | 0:85b3fd62ea1a | 3644 | { |
NYX | 0:85b3fd62ea1a | 3645 | payloadlength = (Size/16U) * 16U; |
NYX | 0:85b3fd62ea1a | 3646 | difflength = (uint32_t) (Size - payloadlength); |
NYX | 0:85b3fd62ea1a | 3647 | addhoc_process = 1U; |
NYX | 0:85b3fd62ea1a | 3648 | } |
NYX | 0:85b3fd62ea1a | 3649 | else |
NYX | 0:85b3fd62ea1a | 3650 | { |
NYX | 0:85b3fd62ea1a | 3651 | payloadlength = Size; |
NYX | 0:85b3fd62ea1a | 3652 | addhoc_process = 0U; |
NYX | 0:85b3fd62ea1a | 3653 | } |
NYX | 0:85b3fd62ea1a | 3654 | |
NYX | 0:85b3fd62ea1a | 3655 | /* Feed payload */ |
NYX | 0:85b3fd62ea1a | 3656 | for(index=0U; index < payloadlength; index += 16U) |
NYX | 0:85b3fd62ea1a | 3657 | { |
NYX | 0:85b3fd62ea1a | 3658 | /* Write the Input block in the Data Input register */ |
NYX | 0:85b3fd62ea1a | 3659 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 3660 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 3661 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 3662 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 3663 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 3664 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 3665 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 3666 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 3667 | |
NYX | 0:85b3fd62ea1a | 3668 | if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) |
NYX | 0:85b3fd62ea1a | 3669 | { |
NYX | 0:85b3fd62ea1a | 3670 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 3671 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 3672 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 3673 | } |
NYX | 0:85b3fd62ea1a | 3674 | |
NYX | 0:85b3fd62ea1a | 3675 | /* Clear CCF Flag */ |
NYX | 0:85b3fd62ea1a | 3676 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
NYX | 0:85b3fd62ea1a | 3677 | |
NYX | 0:85b3fd62ea1a | 3678 | /* Retrieve output data: read the output block |
NYX | 0:85b3fd62ea1a | 3679 | from the Data Output Register */ |
NYX | 0:85b3fd62ea1a | 3680 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
NYX | 0:85b3fd62ea1a | 3681 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 3682 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
NYX | 0:85b3fd62ea1a | 3683 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 3684 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
NYX | 0:85b3fd62ea1a | 3685 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 3686 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
NYX | 0:85b3fd62ea1a | 3687 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 3688 | |
NYX | 0:85b3fd62ea1a | 3689 | /* If the suspension flag has been raised and if the processing is not about |
NYX | 0:85b3fd62ea1a | 3690 | to end, suspend processing */ |
NYX | 0:85b3fd62ea1a | 3691 | if ((hcryp->SuspendRequest == HAL_CRYP_SUSPEND) && ((index+16U) < payloadlength)) |
NYX | 0:85b3fd62ea1a | 3692 | { |
NYX | 0:85b3fd62ea1a | 3693 | /* no flag waiting under IRQ handling */ |
NYX | 0:85b3fd62ea1a | 3694 | if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT) |
NYX | 0:85b3fd62ea1a | 3695 | { |
NYX | 0:85b3fd62ea1a | 3696 | /* Ensure that Busy flag is reset */ |
NYX | 0:85b3fd62ea1a | 3697 | if(CRYP_WaitOnBusyFlagReset(hcryp, CRYP_BUSY_TIMEOUTVALUE) != HAL_OK) |
NYX | 0:85b3fd62ea1a | 3698 | { |
NYX | 0:85b3fd62ea1a | 3699 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 3700 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 3701 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 3702 | } |
NYX | 0:85b3fd62ea1a | 3703 | } |
NYX | 0:85b3fd62ea1a | 3704 | /* reset SuspendRequest */ |
NYX | 0:85b3fd62ea1a | 3705 | hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE; |
NYX | 0:85b3fd62ea1a | 3706 | /* Change the CRYP state */ |
NYX | 0:85b3fd62ea1a | 3707 | hcryp->State = HAL_CRYP_STATE_SUSPENDED; |
NYX | 0:85b3fd62ea1a | 3708 | /* Mark that the header phase is over */ |
NYX | 0:85b3fd62ea1a | 3709 | hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED; |
NYX | 0:85b3fd62ea1a | 3710 | |
NYX | 0:85b3fd62ea1a | 3711 | /* Save current reading and writing locations of Input and Output buffers */ |
NYX | 0:85b3fd62ea1a | 3712 | hcryp->pCrypOutBuffPtr = (uint8_t *)outputaddr; |
NYX | 0:85b3fd62ea1a | 3713 | hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr; |
NYX | 0:85b3fd62ea1a | 3714 | /* Save the number of bytes that remain to be processed at this point */ |
NYX | 0:85b3fd62ea1a | 3715 | hcryp->CrypInCount = Size - (index+16U); |
NYX | 0:85b3fd62ea1a | 3716 | |
NYX | 0:85b3fd62ea1a | 3717 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 3718 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 3719 | |
NYX | 0:85b3fd62ea1a | 3720 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 3721 | } |
NYX | 0:85b3fd62ea1a | 3722 | |
NYX | 0:85b3fd62ea1a | 3723 | } |
NYX | 0:85b3fd62ea1a | 3724 | |
NYX | 0:85b3fd62ea1a | 3725 | /* Additional processing to manage GCM(/CCM) encryption and decryption cases when |
NYX | 0:85b3fd62ea1a | 3726 | payload last block size less than 128 bits */ |
NYX | 0:85b3fd62ea1a | 3727 | if (addhoc_process == 1U) |
NYX | 0:85b3fd62ea1a | 3728 | { |
NYX | 0:85b3fd62ea1a | 3729 | hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr; |
NYX | 0:85b3fd62ea1a | 3730 | hcryp->pCrypOutBuffPtr = (uint8_t *)outputaddr; |
NYX | 0:85b3fd62ea1a | 3731 | CRYP_Padding(hcryp, difflength, CRYP_POLLING_ON); |
NYX | 0:85b3fd62ea1a | 3732 | } /* (addhoc_process == 1) */ |
NYX | 0:85b3fd62ea1a | 3733 | |
NYX | 0:85b3fd62ea1a | 3734 | /* Mark that the payload phase is over */ |
NYX | 0:85b3fd62ea1a | 3735 | hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER; |
NYX | 0:85b3fd62ea1a | 3736 | } |
NYX | 0:85b3fd62ea1a | 3737 | /*====================================*/ |
NYX | 0:85b3fd62ea1a | 3738 | /* GCM/GMAC or (CCM/)CMAC final phase */ |
NYX | 0:85b3fd62ea1a | 3739 | /*====================================*/ |
NYX | 0:85b3fd62ea1a | 3740 | else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE) |
NYX | 0:85b3fd62ea1a | 3741 | { |
NYX | 0:85b3fd62ea1a | 3742 | tagaddr = (uint32_t)pOutputData; |
NYX | 0:85b3fd62ea1a | 3743 | |
NYX | 0:85b3fd62ea1a | 3744 | #if defined(AES_CR_NPBLB) |
NYX | 0:85b3fd62ea1a | 3745 | /* By default, clear NPBLB field */ |
NYX | 0:85b3fd62ea1a | 3746 | CLEAR_BIT(hcryp->Instance->CR, AES_CR_NPBLB); |
NYX | 0:85b3fd62ea1a | 3747 | #endif |
NYX | 0:85b3fd62ea1a | 3748 | |
NYX | 0:85b3fd62ea1a | 3749 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_FINAL_PHASE); |
NYX | 0:85b3fd62ea1a | 3750 | |
NYX | 0:85b3fd62ea1a | 3751 | /* if the header and payload phases have been bypassed, AES must be enabled again */ |
NYX | 0:85b3fd62ea1a | 3752 | if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER) |
NYX | 0:85b3fd62ea1a | 3753 | { |
NYX | 0:85b3fd62ea1a | 3754 | __HAL_CRYP_ENABLE(); |
NYX | 0:85b3fd62ea1a | 3755 | } |
NYX | 0:85b3fd62ea1a | 3756 | |
NYX | 0:85b3fd62ea1a | 3757 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) |
NYX | 0:85b3fd62ea1a | 3758 | { |
NYX | 0:85b3fd62ea1a | 3759 | headerlength = hcryp->Init.HeaderSize * 8U; /* Header length in bits */ |
NYX | 0:85b3fd62ea1a | 3760 | inputlength = Size * 8U; /* input length in bits */ |
NYX | 0:85b3fd62ea1a | 3761 | |
NYX | 0:85b3fd62ea1a | 3762 | |
NYX | 0:85b3fd62ea1a | 3763 | if(hcryp->Init.DataType == CRYP_DATATYPE_1B) |
NYX | 0:85b3fd62ea1a | 3764 | { |
NYX | 0:85b3fd62ea1a | 3765 | hcryp->Instance->DINR = __RBIT((headerlength)>>32U); |
NYX | 0:85b3fd62ea1a | 3766 | hcryp->Instance->DINR = __RBIT(headerlength); |
NYX | 0:85b3fd62ea1a | 3767 | hcryp->Instance->DINR = __RBIT((inputlength)>>32U); |
NYX | 0:85b3fd62ea1a | 3768 | hcryp->Instance->DINR = __RBIT(inputlength); |
NYX | 0:85b3fd62ea1a | 3769 | } |
NYX | 0:85b3fd62ea1a | 3770 | else if(hcryp->Init.DataType == CRYP_DATATYPE_8B) |
NYX | 0:85b3fd62ea1a | 3771 | { |
NYX | 0:85b3fd62ea1a | 3772 | hcryp->Instance->DINR = __REV((headerlength)>>32U); |
NYX | 0:85b3fd62ea1a | 3773 | hcryp->Instance->DINR = __REV(headerlength); |
NYX | 0:85b3fd62ea1a | 3774 | hcryp->Instance->DINR = __REV((inputlength)>>32U); |
NYX | 0:85b3fd62ea1a | 3775 | hcryp->Instance->DINR = __REV(inputlength); |
NYX | 0:85b3fd62ea1a | 3776 | } |
NYX | 0:85b3fd62ea1a | 3777 | else if(hcryp->Init.DataType == CRYP_DATATYPE_16B) |
NYX | 0:85b3fd62ea1a | 3778 | { |
NYX | 0:85b3fd62ea1a | 3779 | hcryp->Instance->DINR = __ROR((headerlength)>>32U, 16U); |
NYX | 0:85b3fd62ea1a | 3780 | hcryp->Instance->DINR = __ROR(headerlength, 16U); |
NYX | 0:85b3fd62ea1a | 3781 | hcryp->Instance->DINR = __ROR((inputlength)>>32U, 16U); |
NYX | 0:85b3fd62ea1a | 3782 | hcryp->Instance->DINR = __ROR(inputlength, 16U); |
NYX | 0:85b3fd62ea1a | 3783 | } |
NYX | 0:85b3fd62ea1a | 3784 | else if(hcryp->Init.DataType == CRYP_DATATYPE_32B) |
NYX | 0:85b3fd62ea1a | 3785 | { |
NYX | 0:85b3fd62ea1a | 3786 | hcryp->Instance->DINR = (uint32_t)(headerlength>>32U); |
NYX | 0:85b3fd62ea1a | 3787 | hcryp->Instance->DINR = (uint32_t)(headerlength); |
NYX | 0:85b3fd62ea1a | 3788 | hcryp->Instance->DINR = (uint32_t)(inputlength>>32U); |
NYX | 0:85b3fd62ea1a | 3789 | hcryp->Instance->DINR = (uint32_t)(inputlength); |
NYX | 0:85b3fd62ea1a | 3790 | } |
NYX | 0:85b3fd62ea1a | 3791 | } |
NYX | 0:85b3fd62ea1a | 3792 | #if !defined(AES_CR_NPBLB) |
NYX | 0:85b3fd62ea1a | 3793 | else if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) |
NYX | 0:85b3fd62ea1a | 3794 | { |
NYX | 0:85b3fd62ea1a | 3795 | inputaddr = (uint32_t)pInputData; |
NYX | 0:85b3fd62ea1a | 3796 | /* Enter the last block made of a 128-bit value formatted |
NYX | 0:85b3fd62ea1a | 3797 | from the original B0 packet. */ |
NYX | 0:85b3fd62ea1a | 3798 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 3799 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 3800 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 3801 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 3802 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 3803 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 3804 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 3805 | } |
NYX | 0:85b3fd62ea1a | 3806 | #endif |
NYX | 0:85b3fd62ea1a | 3807 | |
NYX | 0:85b3fd62ea1a | 3808 | |
NYX | 0:85b3fd62ea1a | 3809 | if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) |
NYX | 0:85b3fd62ea1a | 3810 | { |
NYX | 0:85b3fd62ea1a | 3811 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 3812 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 3813 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 3814 | } |
NYX | 0:85b3fd62ea1a | 3815 | |
NYX | 0:85b3fd62ea1a | 3816 | /* Read the Auth TAG in the Data Out register */ |
NYX | 0:85b3fd62ea1a | 3817 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR; |
NYX | 0:85b3fd62ea1a | 3818 | tagaddr+=4U; |
NYX | 0:85b3fd62ea1a | 3819 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR; |
NYX | 0:85b3fd62ea1a | 3820 | tagaddr+=4U; |
NYX | 0:85b3fd62ea1a | 3821 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR; |
NYX | 0:85b3fd62ea1a | 3822 | tagaddr+=4U; |
NYX | 0:85b3fd62ea1a | 3823 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR; |
NYX | 0:85b3fd62ea1a | 3824 | |
NYX | 0:85b3fd62ea1a | 3825 | |
NYX | 0:85b3fd62ea1a | 3826 | /* Clear CCF Flag */ |
NYX | 0:85b3fd62ea1a | 3827 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
NYX | 0:85b3fd62ea1a | 3828 | /* Mark that the final phase is over */ |
NYX | 0:85b3fd62ea1a | 3829 | hcryp->Phase = HAL_CRYP_PHASE_FINAL_OVER; |
NYX | 0:85b3fd62ea1a | 3830 | /* Disable the Peripheral */ |
NYX | 0:85b3fd62ea1a | 3831 | __HAL_CRYP_DISABLE(); |
NYX | 0:85b3fd62ea1a | 3832 | } |
NYX | 0:85b3fd62ea1a | 3833 | /*=================================================*/ |
NYX | 0:85b3fd62ea1a | 3834 | /* case incorrect hcryp->Init.GCMCMACPhase setting */ |
NYX | 0:85b3fd62ea1a | 3835 | /*=================================================*/ |
NYX | 0:85b3fd62ea1a | 3836 | else |
NYX | 0:85b3fd62ea1a | 3837 | { |
NYX | 0:85b3fd62ea1a | 3838 | hcryp->State = HAL_CRYP_STATE_ERROR; |
NYX | 0:85b3fd62ea1a | 3839 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 3840 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 3841 | } |
NYX | 0:85b3fd62ea1a | 3842 | |
NYX | 0:85b3fd62ea1a | 3843 | /* Change the CRYP state */ |
NYX | 0:85b3fd62ea1a | 3844 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 3845 | |
NYX | 0:85b3fd62ea1a | 3846 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 3847 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 3848 | |
NYX | 0:85b3fd62ea1a | 3849 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 3850 | } |
NYX | 0:85b3fd62ea1a | 3851 | else |
NYX | 0:85b3fd62ea1a | 3852 | { |
NYX | 0:85b3fd62ea1a | 3853 | return HAL_BUSY; |
NYX | 0:85b3fd62ea1a | 3854 | } |
NYX | 0:85b3fd62ea1a | 3855 | } |
NYX | 0:85b3fd62ea1a | 3856 | |
NYX | 0:85b3fd62ea1a | 3857 | |
NYX | 0:85b3fd62ea1a | 3858 | |
NYX | 0:85b3fd62ea1a | 3859 | |
NYX | 0:85b3fd62ea1a | 3860 | /** |
NYX | 0:85b3fd62ea1a | 3861 | * @brief Carry out in interrupt mode the authentication tag generation as well as the ciphering or deciphering |
NYX | 0:85b3fd62ea1a | 3862 | * operation according to hcryp->Init structure fields. |
NYX | 0:85b3fd62ea1a | 3863 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 3864 | * the configuration information for CRYP module |
NYX | 0:85b3fd62ea1a | 3865 | * @param pInputData: |
NYX | 0:85b3fd62ea1a | 3866 | * - pointer to payload data in GCM payload phase, |
NYX | 0:85b3fd62ea1a | 3867 | * - pointer to B0 block in CMAC header phase, |
NYX | 0:85b3fd62ea1a | 3868 | * - pointer to C block in CMAC final phase. |
NYX | 0:85b3fd62ea1a | 3869 | * Parameter is meaningless in case of GCM/GMAC init, header and final phases. |
NYX | 0:85b3fd62ea1a | 3870 | * @param Size: |
NYX | 0:85b3fd62ea1a | 3871 | * - length of the input payload data buffer in bytes, |
NYX | 0:85b3fd62ea1a | 3872 | * - length of B0 block (in bytes) in CMAC header phase, |
NYX | 0:85b3fd62ea1a | 3873 | * - length of C block (in bytes) in CMAC final phase. |
NYX | 0:85b3fd62ea1a | 3874 | * - Parameter is meaningless in case of GCM/GMAC init and header phases. |
NYX | 0:85b3fd62ea1a | 3875 | * @param pOutputData: |
NYX | 0:85b3fd62ea1a | 3876 | * - pointer to plain or cipher text in GCM payload phase, |
NYX | 0:85b3fd62ea1a | 3877 | * - pointer to authentication tag in GCM/GMAC and CMAC final phases. |
NYX | 0:85b3fd62ea1a | 3878 | * - Parameter is meaningless in case of GCM/GMAC init and header phases |
NYX | 0:85b3fd62ea1a | 3879 | * and in case of CMAC header phase. |
NYX | 0:85b3fd62ea1a | 3880 | * @note Supported operating modes are encryption and decryption, supported chaining modes are GCM, GMAC and CMAC. |
NYX | 0:85b3fd62ea1a | 3881 | * @note Phases are singly processed according to hcryp->Init.GCMCMACPhase so that steps in these specific chaining modes |
NYX | 0:85b3fd62ea1a | 3882 | * can be skipped by the user if so required. |
NYX | 0:85b3fd62ea1a | 3883 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 3884 | */ |
NYX | 0:85b3fd62ea1a | 3885 | HAL_StatusTypeDef HAL_CRYPEx_AES_Auth_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint64_t Size, uint8_t *pOutputData) |
NYX | 0:85b3fd62ea1a | 3886 | { |
NYX | 0:85b3fd62ea1a | 3887 | |
NYX | 0:85b3fd62ea1a | 3888 | uint32_t inputaddr = 0U; |
NYX | 0:85b3fd62ea1a | 3889 | uint64_t headerlength = 0U; |
NYX | 0:85b3fd62ea1a | 3890 | uint64_t inputlength = 0U; |
NYX | 0:85b3fd62ea1a | 3891 | uint32_t index = 0U; |
NYX | 0:85b3fd62ea1a | 3892 | uint32_t addhoc_process = 0U; |
NYX | 0:85b3fd62ea1a | 3893 | uint32_t difflength = 0U; |
NYX | 0:85b3fd62ea1a | 3894 | uint32_t difflengthmod4 = 0U; |
NYX | 0:85b3fd62ea1a | 3895 | uint32_t mask[3U] = {0x0FFU, 0x0FFFFU, 0x0FFFFFFU}; |
NYX | 0:85b3fd62ea1a | 3896 | |
NYX | 0:85b3fd62ea1a | 3897 | |
NYX | 0:85b3fd62ea1a | 3898 | if (hcryp->State == HAL_CRYP_STATE_READY) |
NYX | 0:85b3fd62ea1a | 3899 | { |
NYX | 0:85b3fd62ea1a | 3900 | /* input/output parameters check */ |
NYX | 0:85b3fd62ea1a | 3901 | if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE) |
NYX | 0:85b3fd62ea1a | 3902 | { |
NYX | 0:85b3fd62ea1a | 3903 | if ((hcryp->Init.Header != NULL) && (hcryp->Init.HeaderSize == 0U)) |
NYX | 0:85b3fd62ea1a | 3904 | { |
NYX | 0:85b3fd62ea1a | 3905 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 3906 | } |
NYX | 0:85b3fd62ea1a | 3907 | #if defined(AES_CR_NPBLB) |
NYX | 0:85b3fd62ea1a | 3908 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC) |
NYX | 0:85b3fd62ea1a | 3909 | #else |
NYX | 0:85b3fd62ea1a | 3910 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) |
NYX | 0:85b3fd62ea1a | 3911 | #endif |
NYX | 0:85b3fd62ea1a | 3912 | { |
NYX | 0:85b3fd62ea1a | 3913 | /* In case of CMAC header phase resumption, we can have pInputData = NULL and Size = 0 */ |
NYX | 0:85b3fd62ea1a | 3914 | if (((pInputData != NULL) && (Size == 0U)) || ((pInputData == NULL) && (Size != 0U))) |
NYX | 0:85b3fd62ea1a | 3915 | { |
NYX | 0:85b3fd62ea1a | 3916 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 3917 | } |
NYX | 0:85b3fd62ea1a | 3918 | } |
NYX | 0:85b3fd62ea1a | 3919 | } |
NYX | 0:85b3fd62ea1a | 3920 | else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE) |
NYX | 0:85b3fd62ea1a | 3921 | { |
NYX | 0:85b3fd62ea1a | 3922 | if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0U)) |
NYX | 0:85b3fd62ea1a | 3923 | { |
NYX | 0:85b3fd62ea1a | 3924 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 3925 | } |
NYX | 0:85b3fd62ea1a | 3926 | } |
NYX | 0:85b3fd62ea1a | 3927 | else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE) |
NYX | 0:85b3fd62ea1a | 3928 | { |
NYX | 0:85b3fd62ea1a | 3929 | if (pOutputData == NULL) |
NYX | 0:85b3fd62ea1a | 3930 | { |
NYX | 0:85b3fd62ea1a | 3931 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 3932 | } |
NYX | 0:85b3fd62ea1a | 3933 | #if defined(AES_CR_NPBLB) |
NYX | 0:85b3fd62ea1a | 3934 | if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC) && (pInputData == NULL)) |
NYX | 0:85b3fd62ea1a | 3935 | #else |
NYX | 0:85b3fd62ea1a | 3936 | if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (pInputData == NULL)) |
NYX | 0:85b3fd62ea1a | 3937 | #endif |
NYX | 0:85b3fd62ea1a | 3938 | { |
NYX | 0:85b3fd62ea1a | 3939 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 3940 | } |
NYX | 0:85b3fd62ea1a | 3941 | } |
NYX | 0:85b3fd62ea1a | 3942 | |
NYX | 0:85b3fd62ea1a | 3943 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 3944 | __HAL_LOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 3945 | |
NYX | 0:85b3fd62ea1a | 3946 | /* Change the CRYP state */ |
NYX | 0:85b3fd62ea1a | 3947 | hcryp->State = HAL_CRYP_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 3948 | |
NYX | 0:85b3fd62ea1a | 3949 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 3950 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 3951 | |
NYX | 0:85b3fd62ea1a | 3952 | /* Enable Computation Complete Flag and Error Interrupts */ |
NYX | 0:85b3fd62ea1a | 3953 | __HAL_CRYP_ENABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); |
NYX | 0:85b3fd62ea1a | 3954 | |
NYX | 0:85b3fd62ea1a | 3955 | /*==============================================*/ |
NYX | 0:85b3fd62ea1a | 3956 | /* GCM/GMAC (or CCM when applicable) init phase */ |
NYX | 0:85b3fd62ea1a | 3957 | /*==============================================*/ |
NYX | 0:85b3fd62ea1a | 3958 | if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE) |
NYX | 0:85b3fd62ea1a | 3959 | { |
NYX | 0:85b3fd62ea1a | 3960 | /* In case of init phase, the input data (Key and Initialization Vector) have |
NYX | 0:85b3fd62ea1a | 3961 | already been entered during the initialization process. Therefore, the |
NYX | 0:85b3fd62ea1a | 3962 | software just waits for the CCF interrupt to be raised and which will |
NYX | 0:85b3fd62ea1a | 3963 | be handled by CRYP_AES_Auth_IT() API. */ |
NYX | 0:85b3fd62ea1a | 3964 | } |
NYX | 0:85b3fd62ea1a | 3965 | /*=====================================*/ |
NYX | 0:85b3fd62ea1a | 3966 | /* GCM/GMAC or (CCM/)CMAC header phase */ |
NYX | 0:85b3fd62ea1a | 3967 | /*=====================================*/ |
NYX | 0:85b3fd62ea1a | 3968 | else if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE) |
NYX | 0:85b3fd62ea1a | 3969 | { |
NYX | 0:85b3fd62ea1a | 3970 | |
NYX | 0:85b3fd62ea1a | 3971 | #if defined(AES_CR_NPBLB) |
NYX | 0:85b3fd62ea1a | 3972 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC) |
NYX | 0:85b3fd62ea1a | 3973 | #else |
NYX | 0:85b3fd62ea1a | 3974 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) |
NYX | 0:85b3fd62ea1a | 3975 | #endif |
NYX | 0:85b3fd62ea1a | 3976 | { |
NYX | 0:85b3fd62ea1a | 3977 | /* In case of CMAC, B blocks are first entered, before the header. |
NYX | 0:85b3fd62ea1a | 3978 | Therefore, B blocks and the header are entered back-to-back |
NYX | 0:85b3fd62ea1a | 3979 | as if it was only one single block. |
NYX | 0:85b3fd62ea1a | 3980 | However, in case of resumption after suspension, if all the |
NYX | 0:85b3fd62ea1a | 3981 | B blocks have been entered (in that case, Size = 0), only the |
NYX | 0:85b3fd62ea1a | 3982 | remainder of the non-processed header bytes are entered. */ |
NYX | 0:85b3fd62ea1a | 3983 | if (Size != 0U) |
NYX | 0:85b3fd62ea1a | 3984 | { |
NYX | 0:85b3fd62ea1a | 3985 | hcryp->CrypInCount = Size + hcryp->Init.HeaderSize; |
NYX | 0:85b3fd62ea1a | 3986 | hcryp->pCrypInBuffPtr = pInputData; |
NYX | 0:85b3fd62ea1a | 3987 | } |
NYX | 0:85b3fd62ea1a | 3988 | else |
NYX | 0:85b3fd62ea1a | 3989 | { |
NYX | 0:85b3fd62ea1a | 3990 | hcryp->CrypInCount = hcryp->Init.HeaderSize; |
NYX | 0:85b3fd62ea1a | 3991 | hcryp->pCrypInBuffPtr = hcryp->Init.Header; |
NYX | 0:85b3fd62ea1a | 3992 | } |
NYX | 0:85b3fd62ea1a | 3993 | } |
NYX | 0:85b3fd62ea1a | 3994 | else |
NYX | 0:85b3fd62ea1a | 3995 | { |
NYX | 0:85b3fd62ea1a | 3996 | /* Get the header addresses and sizes */ |
NYX | 0:85b3fd62ea1a | 3997 | hcryp->CrypInCount = hcryp->Init.HeaderSize; |
NYX | 0:85b3fd62ea1a | 3998 | hcryp->pCrypInBuffPtr = hcryp->Init.Header; |
NYX | 0:85b3fd62ea1a | 3999 | } |
NYX | 0:85b3fd62ea1a | 4000 | |
NYX | 0:85b3fd62ea1a | 4001 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
NYX | 0:85b3fd62ea1a | 4002 | |
NYX | 0:85b3fd62ea1a | 4003 | /* Set header phase; for GCM or GMAC, set data-byte at this point */ |
NYX | 0:85b3fd62ea1a | 4004 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) |
NYX | 0:85b3fd62ea1a | 4005 | { |
NYX | 0:85b3fd62ea1a | 4006 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH|AES_CR_DATATYPE, CRYP_HEADER_PHASE|hcryp->Init.DataType); |
NYX | 0:85b3fd62ea1a | 4007 | } |
NYX | 0:85b3fd62ea1a | 4008 | else |
NYX | 0:85b3fd62ea1a | 4009 | { |
NYX | 0:85b3fd62ea1a | 4010 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_HEADER_PHASE); |
NYX | 0:85b3fd62ea1a | 4011 | } |
NYX | 0:85b3fd62ea1a | 4012 | |
NYX | 0:85b3fd62ea1a | 4013 | /* Enable the Peripheral */ |
NYX | 0:85b3fd62ea1a | 4014 | __HAL_CRYP_ENABLE(); |
NYX | 0:85b3fd62ea1a | 4015 | |
NYX | 0:85b3fd62ea1a | 4016 | /* Increment/decrement instance pointer/counter */ |
NYX | 0:85b3fd62ea1a | 4017 | if (hcryp->CrypInCount == 0U) |
NYX | 0:85b3fd62ea1a | 4018 | { |
NYX | 0:85b3fd62ea1a | 4019 | /* Case of no header */ |
NYX | 0:85b3fd62ea1a | 4020 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 4021 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 4022 | } |
NYX | 0:85b3fd62ea1a | 4023 | else if (hcryp->CrypInCount < 16U) |
NYX | 0:85b3fd62ea1a | 4024 | { |
NYX | 0:85b3fd62ea1a | 4025 | hcryp->CrypInCount = 0U; |
NYX | 0:85b3fd62ea1a | 4026 | addhoc_process = 1U; |
NYX | 0:85b3fd62ea1a | 4027 | difflength = (uint32_t) (hcryp->Init.HeaderSize); |
NYX | 0:85b3fd62ea1a | 4028 | difflengthmod4 = difflength%4U; |
NYX | 0:85b3fd62ea1a | 4029 | } |
NYX | 0:85b3fd62ea1a | 4030 | else |
NYX | 0:85b3fd62ea1a | 4031 | { |
NYX | 0:85b3fd62ea1a | 4032 | hcryp->pCrypInBuffPtr += 16U; |
NYX | 0:85b3fd62ea1a | 4033 | hcryp->CrypInCount -= 16U; |
NYX | 0:85b3fd62ea1a | 4034 | } |
NYX | 0:85b3fd62ea1a | 4035 | |
NYX | 0:85b3fd62ea1a | 4036 | #if defined(AES_CR_NPBLB) |
NYX | 0:85b3fd62ea1a | 4037 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC) |
NYX | 0:85b3fd62ea1a | 4038 | #else |
NYX | 0:85b3fd62ea1a | 4039 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) |
NYX | 0:85b3fd62ea1a | 4040 | #endif |
NYX | 0:85b3fd62ea1a | 4041 | { |
NYX | 0:85b3fd62ea1a | 4042 | if (hcryp->CrypInCount == hcryp->Init.HeaderSize) |
NYX | 0:85b3fd62ea1a | 4043 | { |
NYX | 0:85b3fd62ea1a | 4044 | /* All B blocks will have been entered after the next |
NYX | 0:85b3fd62ea1a | 4045 | four DINR writing, so point at header buffer for |
NYX | 0:85b3fd62ea1a | 4046 | the next iteration */ |
NYX | 0:85b3fd62ea1a | 4047 | hcryp->pCrypInBuffPtr = hcryp->Init.Header; |
NYX | 0:85b3fd62ea1a | 4048 | } |
NYX | 0:85b3fd62ea1a | 4049 | } |
NYX | 0:85b3fd62ea1a | 4050 | |
NYX | 0:85b3fd62ea1a | 4051 | /* Enter header first block to initiate the process |
NYX | 0:85b3fd62ea1a | 4052 | in the Data Input register */ |
NYX | 0:85b3fd62ea1a | 4053 | if (addhoc_process == 0U) |
NYX | 0:85b3fd62ea1a | 4054 | { |
NYX | 0:85b3fd62ea1a | 4055 | /* Header has size equal or larger than 128 bits */ |
NYX | 0:85b3fd62ea1a | 4056 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 4057 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4058 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 4059 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4060 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 4061 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4062 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 4063 | } |
NYX | 0:85b3fd62ea1a | 4064 | else |
NYX | 0:85b3fd62ea1a | 4065 | { |
NYX | 0:85b3fd62ea1a | 4066 | /* Header has size less than 128 bits */ |
NYX | 0:85b3fd62ea1a | 4067 | /* Enter complete words when possible */ |
NYX | 0:85b3fd62ea1a | 4068 | for(index=0U; index < (difflength/4U); index ++) |
NYX | 0:85b3fd62ea1a | 4069 | { |
NYX | 0:85b3fd62ea1a | 4070 | /* Write the Input block in the Data Input register */ |
NYX | 0:85b3fd62ea1a | 4071 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 4072 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4073 | } |
NYX | 0:85b3fd62ea1a | 4074 | /* Enter incomplete word padded with zeroes if applicable |
NYX | 0:85b3fd62ea1a | 4075 | (case of header length not a multiple of 32-bits) */ |
NYX | 0:85b3fd62ea1a | 4076 | if (difflengthmod4 != 0U) |
NYX | 0:85b3fd62ea1a | 4077 | { |
NYX | 0:85b3fd62ea1a | 4078 | hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[difflengthmod4-1U]); |
NYX | 0:85b3fd62ea1a | 4079 | } |
NYX | 0:85b3fd62ea1a | 4080 | /* Pad with zero-words to reach 128-bit long block and wrap-up header feeding to the IP */ |
NYX | 0:85b3fd62ea1a | 4081 | for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++) |
NYX | 0:85b3fd62ea1a | 4082 | { |
NYX | 0:85b3fd62ea1a | 4083 | hcryp->Instance->DINR = 0U; |
NYX | 0:85b3fd62ea1a | 4084 | } |
NYX | 0:85b3fd62ea1a | 4085 | |
NYX | 0:85b3fd62ea1a | 4086 | } |
NYX | 0:85b3fd62ea1a | 4087 | } |
NYX | 0:85b3fd62ea1a | 4088 | /*============================================*/ |
NYX | 0:85b3fd62ea1a | 4089 | /* GCM (or CCM when applicable) payload phase */ |
NYX | 0:85b3fd62ea1a | 4090 | /*============================================*/ |
NYX | 0:85b3fd62ea1a | 4091 | else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE) |
NYX | 0:85b3fd62ea1a | 4092 | { |
NYX | 0:85b3fd62ea1a | 4093 | /* Get the buffer addresses and sizes */ |
NYX | 0:85b3fd62ea1a | 4094 | hcryp->CrypInCount = Size; |
NYX | 0:85b3fd62ea1a | 4095 | hcryp->pCrypInBuffPtr = pInputData; |
NYX | 0:85b3fd62ea1a | 4096 | hcryp->pCrypOutBuffPtr = pOutputData; |
NYX | 0:85b3fd62ea1a | 4097 | hcryp->CrypOutCount = Size; |
NYX | 0:85b3fd62ea1a | 4098 | |
NYX | 0:85b3fd62ea1a | 4099 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
NYX | 0:85b3fd62ea1a | 4100 | |
NYX | 0:85b3fd62ea1a | 4101 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_GCM_PAYLOAD_PHASE); |
NYX | 0:85b3fd62ea1a | 4102 | |
NYX | 0:85b3fd62ea1a | 4103 | /* if the header phase has been bypassed, AES must be enabled again */ |
NYX | 0:85b3fd62ea1a | 4104 | if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER) |
NYX | 0:85b3fd62ea1a | 4105 | { |
NYX | 0:85b3fd62ea1a | 4106 | __HAL_CRYP_ENABLE(); |
NYX | 0:85b3fd62ea1a | 4107 | } |
NYX | 0:85b3fd62ea1a | 4108 | |
NYX | 0:85b3fd62ea1a | 4109 | /* Specific handling to manage payload size less than 128 bits */ |
NYX | 0:85b3fd62ea1a | 4110 | if (Size < 16U) |
NYX | 0:85b3fd62ea1a | 4111 | { |
NYX | 0:85b3fd62ea1a | 4112 | #if defined(AES_CR_NPBLB) |
NYX | 0:85b3fd62ea1a | 4113 | /* In case of GCM encryption or CCM decryption, specify the number of padding |
NYX | 0:85b3fd62ea1a | 4114 | bytes in last block of payload */ |
NYX | 0:85b3fd62ea1a | 4115 | if (READ_BIT(hcryp->Instance->CR, AES_CR_GCMPH) == CRYP_PAYLOAD_PHASE) |
NYX | 0:85b3fd62ea1a | 4116 | { |
NYX | 0:85b3fd62ea1a | 4117 | if (((READ_BIT(hcryp->Instance->CR, AES_CR_CHMOD) == CRYP_CHAINMODE_AES_GCM_GMAC) |
NYX | 0:85b3fd62ea1a | 4118 | && (READ_BIT(hcryp->Instance->CR, AES_CR_MODE) == CRYP_ALGOMODE_ENCRYPT)) |
NYX | 0:85b3fd62ea1a | 4119 | || ((READ_BIT(hcryp->Instance->CR, AES_CR_CHMOD) == CRYP_CHAINMODE_AES_CCM_CMAC) |
NYX | 0:85b3fd62ea1a | 4120 | && (READ_BIT(hcryp->Instance->CR, AES_CR_MODE) == CRYP_ALGOMODE_DECRYPT))) |
NYX | 0:85b3fd62ea1a | 4121 | { |
NYX | 0:85b3fd62ea1a | 4122 | /* Set NPBLB field in writing the number of padding bytes |
NYX | 0:85b3fd62ea1a | 4123 | for the last block of payload */ |
NYX | 0:85b3fd62ea1a | 4124 | MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, 16U - difflength); |
NYX | 0:85b3fd62ea1a | 4125 | } |
NYX | 0:85b3fd62ea1a | 4126 | } |
NYX | 0:85b3fd62ea1a | 4127 | #else |
NYX | 0:85b3fd62ea1a | 4128 | /* Software workaround applied to GCM encryption only */ |
NYX | 0:85b3fd62ea1a | 4129 | if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT) |
NYX | 0:85b3fd62ea1a | 4130 | { |
NYX | 0:85b3fd62ea1a | 4131 | /* Change the mode configured in CHMOD bits of CR register to select CTR mode */ |
NYX | 0:85b3fd62ea1a | 4132 | __HAL_CRYP_SET_CHAININGMODE(CRYP_CHAINMODE_AES_CTR); |
NYX | 0:85b3fd62ea1a | 4133 | } |
NYX | 0:85b3fd62ea1a | 4134 | #endif |
NYX | 0:85b3fd62ea1a | 4135 | |
NYX | 0:85b3fd62ea1a | 4136 | /* Set hcryp->CrypInCount to 0 (no more data to enter) */ |
NYX | 0:85b3fd62ea1a | 4137 | hcryp->CrypInCount = 0U; |
NYX | 0:85b3fd62ea1a | 4138 | |
NYX | 0:85b3fd62ea1a | 4139 | /* Insert the last block (which size is inferior to 128 bits) padded with zeroes, |
NYX | 0:85b3fd62ea1a | 4140 | to have a complete block of 128 bits */ |
NYX | 0:85b3fd62ea1a | 4141 | difflength = (uint32_t) (Size); |
NYX | 0:85b3fd62ea1a | 4142 | difflengthmod4 = difflength%4U; |
NYX | 0:85b3fd62ea1a | 4143 | /* Insert the last block (which size is inferior to 128 bits) padded with zeroes |
NYX | 0:85b3fd62ea1a | 4144 | to have a complete block of 128 bits */ |
NYX | 0:85b3fd62ea1a | 4145 | for(index=0U; index < (difflength/4U); index ++) |
NYX | 0:85b3fd62ea1a | 4146 | { |
NYX | 0:85b3fd62ea1a | 4147 | /* Write the Input block in the Data Input register */ |
NYX | 0:85b3fd62ea1a | 4148 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 4149 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4150 | } |
NYX | 0:85b3fd62ea1a | 4151 | /* If required, manage input data size not multiple of 32 bits */ |
NYX | 0:85b3fd62ea1a | 4152 | if (difflengthmod4 != 0U) |
NYX | 0:85b3fd62ea1a | 4153 | { |
NYX | 0:85b3fd62ea1a | 4154 | hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[difflengthmod4-1U]); |
NYX | 0:85b3fd62ea1a | 4155 | } |
NYX | 0:85b3fd62ea1a | 4156 | /* Wrap-up in padding with zero-words if applicable */ |
NYX | 0:85b3fd62ea1a | 4157 | for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++) |
NYX | 0:85b3fd62ea1a | 4158 | { |
NYX | 0:85b3fd62ea1a | 4159 | hcryp->Instance->DINR = 0U; |
NYX | 0:85b3fd62ea1a | 4160 | } |
NYX | 0:85b3fd62ea1a | 4161 | } |
NYX | 0:85b3fd62ea1a | 4162 | else |
NYX | 0:85b3fd62ea1a | 4163 | { |
NYX | 0:85b3fd62ea1a | 4164 | /* Increment/decrement instance pointer/counter */ |
NYX | 0:85b3fd62ea1a | 4165 | hcryp->pCrypInBuffPtr += 16U; |
NYX | 0:85b3fd62ea1a | 4166 | hcryp->CrypInCount -= 16U; |
NYX | 0:85b3fd62ea1a | 4167 | |
NYX | 0:85b3fd62ea1a | 4168 | /* Enter payload first block to initiate the process |
NYX | 0:85b3fd62ea1a | 4169 | in the Data Input register */ |
NYX | 0:85b3fd62ea1a | 4170 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 4171 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4172 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 4173 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4174 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 4175 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4176 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 4177 | } |
NYX | 0:85b3fd62ea1a | 4178 | } |
NYX | 0:85b3fd62ea1a | 4179 | /*====================================*/ |
NYX | 0:85b3fd62ea1a | 4180 | /* GCM/GMAC or (CCM/)CMAC final phase */ |
NYX | 0:85b3fd62ea1a | 4181 | /*====================================*/ |
NYX | 0:85b3fd62ea1a | 4182 | else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE) |
NYX | 0:85b3fd62ea1a | 4183 | { |
NYX | 0:85b3fd62ea1a | 4184 | hcryp->pCrypOutBuffPtr = pOutputData; |
NYX | 0:85b3fd62ea1a | 4185 | |
NYX | 0:85b3fd62ea1a | 4186 | #if defined(AES_CR_NPBLB) |
NYX | 0:85b3fd62ea1a | 4187 | /* By default, clear NPBLB field */ |
NYX | 0:85b3fd62ea1a | 4188 | CLEAR_BIT(hcryp->Instance->CR, AES_CR_NPBLB); |
NYX | 0:85b3fd62ea1a | 4189 | #endif |
NYX | 0:85b3fd62ea1a | 4190 | |
NYX | 0:85b3fd62ea1a | 4191 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_FINAL_PHASE); |
NYX | 0:85b3fd62ea1a | 4192 | |
NYX | 0:85b3fd62ea1a | 4193 | /* if the header and payload phases have been bypassed, AES must be enabled again */ |
NYX | 0:85b3fd62ea1a | 4194 | if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER) |
NYX | 0:85b3fd62ea1a | 4195 | { |
NYX | 0:85b3fd62ea1a | 4196 | __HAL_CRYP_ENABLE(); |
NYX | 0:85b3fd62ea1a | 4197 | } |
NYX | 0:85b3fd62ea1a | 4198 | |
NYX | 0:85b3fd62ea1a | 4199 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) |
NYX | 0:85b3fd62ea1a | 4200 | { |
NYX | 0:85b3fd62ea1a | 4201 | headerlength = hcryp->Init.HeaderSize * 8U; /* Header length in bits */ |
NYX | 0:85b3fd62ea1a | 4202 | inputlength = Size * 8U; /* Input length in bits */ |
NYX | 0:85b3fd62ea1a | 4203 | /* Write the number of bits in the header on 64 bits followed by the number |
NYX | 0:85b3fd62ea1a | 4204 | of bits in the payload on 64 bits as well */ |
NYX | 0:85b3fd62ea1a | 4205 | if(hcryp->Init.DataType == CRYP_DATATYPE_1B) |
NYX | 0:85b3fd62ea1a | 4206 | { |
NYX | 0:85b3fd62ea1a | 4207 | hcryp->Instance->DINR = __RBIT((headerlength)>>32U); |
NYX | 0:85b3fd62ea1a | 4208 | hcryp->Instance->DINR = __RBIT(headerlength); |
NYX | 0:85b3fd62ea1a | 4209 | hcryp->Instance->DINR = __RBIT((inputlength)>>32U); |
NYX | 0:85b3fd62ea1a | 4210 | hcryp->Instance->DINR = __RBIT(inputlength); |
NYX | 0:85b3fd62ea1a | 4211 | } |
NYX | 0:85b3fd62ea1a | 4212 | else if(hcryp->Init.DataType == CRYP_DATATYPE_8B) |
NYX | 0:85b3fd62ea1a | 4213 | { |
NYX | 0:85b3fd62ea1a | 4214 | hcryp->Instance->DINR = __REV((headerlength)>>32U); |
NYX | 0:85b3fd62ea1a | 4215 | hcryp->Instance->DINR = __REV(headerlength); |
NYX | 0:85b3fd62ea1a | 4216 | hcryp->Instance->DINR = __REV((inputlength)>>32U); |
NYX | 0:85b3fd62ea1a | 4217 | hcryp->Instance->DINR = __REV(inputlength); |
NYX | 0:85b3fd62ea1a | 4218 | } |
NYX | 0:85b3fd62ea1a | 4219 | else if(hcryp->Init.DataType == CRYP_DATATYPE_16B) |
NYX | 0:85b3fd62ea1a | 4220 | { |
NYX | 0:85b3fd62ea1a | 4221 | hcryp->Instance->DINR = __ROR((headerlength)>>32U, 16U); |
NYX | 0:85b3fd62ea1a | 4222 | hcryp->Instance->DINR = __ROR(headerlength, 16U); |
NYX | 0:85b3fd62ea1a | 4223 | hcryp->Instance->DINR = __ROR((inputlength)>>32U, 16U); |
NYX | 0:85b3fd62ea1a | 4224 | hcryp->Instance->DINR = __ROR(inputlength, 16U); |
NYX | 0:85b3fd62ea1a | 4225 | } |
NYX | 0:85b3fd62ea1a | 4226 | else if(hcryp->Init.DataType == CRYP_DATATYPE_32B) |
NYX | 0:85b3fd62ea1a | 4227 | { |
NYX | 0:85b3fd62ea1a | 4228 | hcryp->Instance->DINR = (uint32_t)(headerlength>>32U); |
NYX | 0:85b3fd62ea1a | 4229 | hcryp->Instance->DINR = (uint32_t)(headerlength); |
NYX | 0:85b3fd62ea1a | 4230 | hcryp->Instance->DINR = (uint32_t)(inputlength>>32U); |
NYX | 0:85b3fd62ea1a | 4231 | hcryp->Instance->DINR = (uint32_t)(inputlength); |
NYX | 0:85b3fd62ea1a | 4232 | } |
NYX | 0:85b3fd62ea1a | 4233 | } |
NYX | 0:85b3fd62ea1a | 4234 | #if !defined(AES_CR_NPBLB) |
NYX | 0:85b3fd62ea1a | 4235 | else if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) |
NYX | 0:85b3fd62ea1a | 4236 | { |
NYX | 0:85b3fd62ea1a | 4237 | inputaddr = (uint32_t)pInputData; |
NYX | 0:85b3fd62ea1a | 4238 | /* Enter the last block made of a 128-bit value formatted |
NYX | 0:85b3fd62ea1a | 4239 | from the original B0 packet. */ |
NYX | 0:85b3fd62ea1a | 4240 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 4241 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4242 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 4243 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4244 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 4245 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4246 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 4247 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4248 | } |
NYX | 0:85b3fd62ea1a | 4249 | #endif |
NYX | 0:85b3fd62ea1a | 4250 | } |
NYX | 0:85b3fd62ea1a | 4251 | /*=================================================*/ |
NYX | 0:85b3fd62ea1a | 4252 | /* case incorrect hcryp->Init.GCMCMACPhase setting */ |
NYX | 0:85b3fd62ea1a | 4253 | /*=================================================*/ |
NYX | 0:85b3fd62ea1a | 4254 | else |
NYX | 0:85b3fd62ea1a | 4255 | { |
NYX | 0:85b3fd62ea1a | 4256 | hcryp->State = HAL_CRYP_STATE_ERROR; |
NYX | 0:85b3fd62ea1a | 4257 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 4258 | } |
NYX | 0:85b3fd62ea1a | 4259 | |
NYX | 0:85b3fd62ea1a | 4260 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 4261 | } |
NYX | 0:85b3fd62ea1a | 4262 | else |
NYX | 0:85b3fd62ea1a | 4263 | { |
NYX | 0:85b3fd62ea1a | 4264 | return HAL_BUSY; |
NYX | 0:85b3fd62ea1a | 4265 | } |
NYX | 0:85b3fd62ea1a | 4266 | } |
NYX | 0:85b3fd62ea1a | 4267 | |
NYX | 0:85b3fd62ea1a | 4268 | |
NYX | 0:85b3fd62ea1a | 4269 | |
NYX | 0:85b3fd62ea1a | 4270 | |
NYX | 0:85b3fd62ea1a | 4271 | /** |
NYX | 0:85b3fd62ea1a | 4272 | * @brief Carry out in DMA mode the authentication tag generation as well as the ciphering or deciphering |
NYX | 0:85b3fd62ea1a | 4273 | * operation according to hcryp->Init structure fields. |
NYX | 0:85b3fd62ea1a | 4274 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 4275 | * the configuration information for CRYP module |
NYX | 0:85b3fd62ea1a | 4276 | * @param pInputData: |
NYX | 0:85b3fd62ea1a | 4277 | * - pointer to payload data in GCM payload phase, |
NYX | 0:85b3fd62ea1a | 4278 | * - pointer to B0 block in CMAC header phase, |
NYX | 0:85b3fd62ea1a | 4279 | * - pointer to C block in CMAC final phase. |
NYX | 0:85b3fd62ea1a | 4280 | * - Parameter is meaningless in case of GCM/GMAC init, header and final phases. |
NYX | 0:85b3fd62ea1a | 4281 | * @param Size: |
NYX | 0:85b3fd62ea1a | 4282 | * - length of the input payload data buffer in bytes, |
NYX | 0:85b3fd62ea1a | 4283 | * - length of B block (in bytes) in CMAC header phase, |
NYX | 0:85b3fd62ea1a | 4284 | * - length of C block (in bytes) in CMAC final phase. |
NYX | 0:85b3fd62ea1a | 4285 | * - Parameter is meaningless in case of GCM/GMAC init and header phases. |
NYX | 0:85b3fd62ea1a | 4286 | * @param pOutputData: |
NYX | 0:85b3fd62ea1a | 4287 | * - pointer to plain or cipher text in GCM payload phase, |
NYX | 0:85b3fd62ea1a | 4288 | * - pointer to authentication tag in GCM/GMAC and CMAC final phases. |
NYX | 0:85b3fd62ea1a | 4289 | * - Parameter is meaningless in case of GCM/GMAC init and header phases |
NYX | 0:85b3fd62ea1a | 4290 | * and in case of CMAC header phase. |
NYX | 0:85b3fd62ea1a | 4291 | * @note Supported operating modes are encryption and decryption, supported chaining modes are GCM, GMAC and CMAC. |
NYX | 0:85b3fd62ea1a | 4292 | * @note Phases are singly processed according to hcryp->Init.GCMCMACPhase so that steps in these specific chaining modes |
NYX | 0:85b3fd62ea1a | 4293 | * can be skipped by the user if so required. |
NYX | 0:85b3fd62ea1a | 4294 | * @note pInputData and pOutputData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP. |
NYX | 0:85b3fd62ea1a | 4295 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 4296 | */ |
NYX | 0:85b3fd62ea1a | 4297 | HAL_StatusTypeDef HAL_CRYPEx_AES_Auth_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint64_t Size, uint8_t *pOutputData) |
NYX | 0:85b3fd62ea1a | 4298 | { |
NYX | 0:85b3fd62ea1a | 4299 | uint32_t inputaddr = 0U; |
NYX | 0:85b3fd62ea1a | 4300 | uint32_t outputaddr = 0U; |
NYX | 0:85b3fd62ea1a | 4301 | uint32_t tagaddr = 0U; |
NYX | 0:85b3fd62ea1a | 4302 | uint64_t headerlength = 0U; |
NYX | 0:85b3fd62ea1a | 4303 | uint64_t inputlength = 0U; |
NYX | 0:85b3fd62ea1a | 4304 | uint64_t payloadlength = 0U; |
NYX | 0:85b3fd62ea1a | 4305 | |
NYX | 0:85b3fd62ea1a | 4306 | |
NYX | 0:85b3fd62ea1a | 4307 | if (hcryp->State == HAL_CRYP_STATE_READY) |
NYX | 0:85b3fd62ea1a | 4308 | { |
NYX | 0:85b3fd62ea1a | 4309 | /* input/output parameters check */ |
NYX | 0:85b3fd62ea1a | 4310 | if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE) |
NYX | 0:85b3fd62ea1a | 4311 | { |
NYX | 0:85b3fd62ea1a | 4312 | if ((hcryp->Init.Header != NULL) && (hcryp->Init.HeaderSize == 0U)) |
NYX | 0:85b3fd62ea1a | 4313 | { |
NYX | 0:85b3fd62ea1a | 4314 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 4315 | } |
NYX | 0:85b3fd62ea1a | 4316 | #if defined(AES_CR_NPBLB) |
NYX | 0:85b3fd62ea1a | 4317 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC) |
NYX | 0:85b3fd62ea1a | 4318 | #else |
NYX | 0:85b3fd62ea1a | 4319 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) |
NYX | 0:85b3fd62ea1a | 4320 | #endif |
NYX | 0:85b3fd62ea1a | 4321 | { |
NYX | 0:85b3fd62ea1a | 4322 | if ((pInputData == NULL) || (Size == 0U)) |
NYX | 0:85b3fd62ea1a | 4323 | { |
NYX | 0:85b3fd62ea1a | 4324 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 4325 | } |
NYX | 0:85b3fd62ea1a | 4326 | } |
NYX | 0:85b3fd62ea1a | 4327 | } |
NYX | 0:85b3fd62ea1a | 4328 | else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE) |
NYX | 0:85b3fd62ea1a | 4329 | { |
NYX | 0:85b3fd62ea1a | 4330 | if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0U)) |
NYX | 0:85b3fd62ea1a | 4331 | { |
NYX | 0:85b3fd62ea1a | 4332 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 4333 | } |
NYX | 0:85b3fd62ea1a | 4334 | } |
NYX | 0:85b3fd62ea1a | 4335 | else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE) |
NYX | 0:85b3fd62ea1a | 4336 | { |
NYX | 0:85b3fd62ea1a | 4337 | if (pOutputData == NULL) |
NYX | 0:85b3fd62ea1a | 4338 | { |
NYX | 0:85b3fd62ea1a | 4339 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 4340 | } |
NYX | 0:85b3fd62ea1a | 4341 | #if defined(AES_CR_NPBLB) |
NYX | 0:85b3fd62ea1a | 4342 | if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC) && (pInputData == NULL)) |
NYX | 0:85b3fd62ea1a | 4343 | #else |
NYX | 0:85b3fd62ea1a | 4344 | if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (pInputData == NULL)) |
NYX | 0:85b3fd62ea1a | 4345 | #endif |
NYX | 0:85b3fd62ea1a | 4346 | { |
NYX | 0:85b3fd62ea1a | 4347 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 4348 | } |
NYX | 0:85b3fd62ea1a | 4349 | } |
NYX | 0:85b3fd62ea1a | 4350 | |
NYX | 0:85b3fd62ea1a | 4351 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 4352 | __HAL_LOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 4353 | |
NYX | 0:85b3fd62ea1a | 4354 | /* Change the CRYP state */ |
NYX | 0:85b3fd62ea1a | 4355 | hcryp->State = HAL_CRYP_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 4356 | |
NYX | 0:85b3fd62ea1a | 4357 | /*==============================================*/ |
NYX | 0:85b3fd62ea1a | 4358 | /* GCM/GMAC (or CCM when applicable) init phase */ |
NYX | 0:85b3fd62ea1a | 4359 | /*==============================================*/ |
NYX | 0:85b3fd62ea1a | 4360 | /* In case of init phase, the input data (Key and Initialization Vector) have |
NYX | 0:85b3fd62ea1a | 4361 | already been entered during the initialization process. No DMA transfer is |
NYX | 0:85b3fd62ea1a | 4362 | required at that point therefore, the software just waits for the CCF flag |
NYX | 0:85b3fd62ea1a | 4363 | to be raised. */ |
NYX | 0:85b3fd62ea1a | 4364 | if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE) |
NYX | 0:85b3fd62ea1a | 4365 | { |
NYX | 0:85b3fd62ea1a | 4366 | /* just wait for hash computation */ |
NYX | 0:85b3fd62ea1a | 4367 | if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK) |
NYX | 0:85b3fd62ea1a | 4368 | { |
NYX | 0:85b3fd62ea1a | 4369 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 4370 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 4371 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 4372 | } |
NYX | 0:85b3fd62ea1a | 4373 | |
NYX | 0:85b3fd62ea1a | 4374 | /* Clear CCF Flag */ |
NYX | 0:85b3fd62ea1a | 4375 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
NYX | 0:85b3fd62ea1a | 4376 | /* Mark that the initialization phase is over */ |
NYX | 0:85b3fd62ea1a | 4377 | hcryp->Phase = HAL_CRYP_PHASE_INIT_OVER; |
NYX | 0:85b3fd62ea1a | 4378 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 4379 | } |
NYX | 0:85b3fd62ea1a | 4380 | /*===============================*/ |
NYX | 0:85b3fd62ea1a | 4381 | /* GCM/GMAC or CMAC header phase */ |
NYX | 0:85b3fd62ea1a | 4382 | /*===============================*/ |
NYX | 0:85b3fd62ea1a | 4383 | else if (hcryp->Init.GCMCMACPhase == CRYP_GCMCMAC_HEADER_PHASE) |
NYX | 0:85b3fd62ea1a | 4384 | { |
NYX | 0:85b3fd62ea1a | 4385 | /* Set header phase; for GCM or GMAC, set data-byte at this point */ |
NYX | 0:85b3fd62ea1a | 4386 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) |
NYX | 0:85b3fd62ea1a | 4387 | { |
NYX | 0:85b3fd62ea1a | 4388 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH|AES_CR_DATATYPE, CRYP_GCMCMAC_HEADER_PHASE|hcryp->Init.DataType); |
NYX | 0:85b3fd62ea1a | 4389 | } |
NYX | 0:85b3fd62ea1a | 4390 | else |
NYX | 0:85b3fd62ea1a | 4391 | { |
NYX | 0:85b3fd62ea1a | 4392 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_GCMCMAC_HEADER_PHASE); |
NYX | 0:85b3fd62ea1a | 4393 | } |
NYX | 0:85b3fd62ea1a | 4394 | |
NYX | 0:85b3fd62ea1a | 4395 | #if !defined(AES_CR_NPBLB) |
NYX | 0:85b3fd62ea1a | 4396 | /* enter first B0 block in polling mode (no DMA transfer for B0) */ |
NYX | 0:85b3fd62ea1a | 4397 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) |
NYX | 0:85b3fd62ea1a | 4398 | { |
NYX | 0:85b3fd62ea1a | 4399 | /* Enable the CRYP peripheral */ |
NYX | 0:85b3fd62ea1a | 4400 | __HAL_CRYP_ENABLE(); |
NYX | 0:85b3fd62ea1a | 4401 | |
NYX | 0:85b3fd62ea1a | 4402 | inputaddr = (uint32_t)pInputData; |
NYX | 0:85b3fd62ea1a | 4403 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 4404 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4405 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 4406 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4407 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 4408 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4409 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 4410 | |
NYX | 0:85b3fd62ea1a | 4411 | if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK) |
NYX | 0:85b3fd62ea1a | 4412 | { |
NYX | 0:85b3fd62ea1a | 4413 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 4414 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 4415 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 4416 | } |
NYX | 0:85b3fd62ea1a | 4417 | /* Clear CCF Flag */ |
NYX | 0:85b3fd62ea1a | 4418 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
NYX | 0:85b3fd62ea1a | 4419 | } |
NYX | 0:85b3fd62ea1a | 4420 | #endif |
NYX | 0:85b3fd62ea1a | 4421 | |
NYX | 0:85b3fd62ea1a | 4422 | /* No header case */ |
NYX | 0:85b3fd62ea1a | 4423 | if (hcryp->Init.Header == NULL) |
NYX | 0:85b3fd62ea1a | 4424 | { |
NYX | 0:85b3fd62ea1a | 4425 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 4426 | /* Mark that the header phase is over */ |
NYX | 0:85b3fd62ea1a | 4427 | hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER; |
NYX | 0:85b3fd62ea1a | 4428 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 4429 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 4430 | |
NYX | 0:85b3fd62ea1a | 4431 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 4432 | } |
NYX | 0:85b3fd62ea1a | 4433 | |
NYX | 0:85b3fd62ea1a | 4434 | inputaddr = (uint32_t)hcryp->Init.Header; |
NYX | 0:85b3fd62ea1a | 4435 | if ((hcryp->Init.HeaderSize % 16U) != 0U) |
NYX | 0:85b3fd62ea1a | 4436 | { |
NYX | 0:85b3fd62ea1a | 4437 | |
NYX | 0:85b3fd62ea1a | 4438 | if (hcryp->Init.HeaderSize < 16U) |
NYX | 0:85b3fd62ea1a | 4439 | { |
NYX | 0:85b3fd62ea1a | 4440 | CRYP_Padding(hcryp, (uint32_t) (hcryp->Init.HeaderSize), CRYP_POLLING_OFF); |
NYX | 0:85b3fd62ea1a | 4441 | |
NYX | 0:85b3fd62ea1a | 4442 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 4443 | /* Mark that the header phase is over */ |
NYX | 0:85b3fd62ea1a | 4444 | hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER; |
NYX | 0:85b3fd62ea1a | 4445 | |
NYX | 0:85b3fd62ea1a | 4446 | /* CCF flag indicating header phase AES processing completion |
NYX | 0:85b3fd62ea1a | 4447 | will be checked at the start of the next phase: |
NYX | 0:85b3fd62ea1a | 4448 | - payload phase (GCM / CCM when applicable) |
NYX | 0:85b3fd62ea1a | 4449 | - final phase (GMAC or CMAC). */ |
NYX | 0:85b3fd62ea1a | 4450 | } |
NYX | 0:85b3fd62ea1a | 4451 | else |
NYX | 0:85b3fd62ea1a | 4452 | { |
NYX | 0:85b3fd62ea1a | 4453 | /* Local variable headerlength is a number of bytes multiple of 128 bits, |
NYX | 0:85b3fd62ea1a | 4454 | remaining header data (if any) are handled after this loop */ |
NYX | 0:85b3fd62ea1a | 4455 | headerlength = (((hcryp->Init.HeaderSize)/16U)*16U) ; |
NYX | 0:85b3fd62ea1a | 4456 | /* Store the ending transfer point */ |
NYX | 0:85b3fd62ea1a | 4457 | hcryp->pCrypInBuffPtr = hcryp->Init.Header + headerlength; |
NYX | 0:85b3fd62ea1a | 4458 | hcryp->CrypInCount = (uint32_t)(hcryp->Init.HeaderSize - headerlength); /* remainder */ |
NYX | 0:85b3fd62ea1a | 4459 | |
NYX | 0:85b3fd62ea1a | 4460 | /* Set the input and output addresses and start DMA transfer */ |
NYX | 0:85b3fd62ea1a | 4461 | /* (incomplete DMA transfer, will be wrapped up after completion of |
NYX | 0:85b3fd62ea1a | 4462 | the first one (initiated here) with data padding */ |
NYX | 0:85b3fd62ea1a | 4463 | CRYP_GCMCMAC_SetDMAConfig(hcryp, inputaddr, headerlength, 0U); |
NYX | 0:85b3fd62ea1a | 4464 | } |
NYX | 0:85b3fd62ea1a | 4465 | } |
NYX | 0:85b3fd62ea1a | 4466 | else |
NYX | 0:85b3fd62ea1a | 4467 | { |
NYX | 0:85b3fd62ea1a | 4468 | hcryp->CrypInCount = 0U; |
NYX | 0:85b3fd62ea1a | 4469 | /* Set the input address and start DMA transfer */ |
NYX | 0:85b3fd62ea1a | 4470 | CRYP_GCMCMAC_SetDMAConfig(hcryp, inputaddr, hcryp->Init.HeaderSize, 0U); |
NYX | 0:85b3fd62ea1a | 4471 | } |
NYX | 0:85b3fd62ea1a | 4472 | |
NYX | 0:85b3fd62ea1a | 4473 | } |
NYX | 0:85b3fd62ea1a | 4474 | /*============================================*/ |
NYX | 0:85b3fd62ea1a | 4475 | /* GCM (or CCM when applicable) payload phase */ |
NYX | 0:85b3fd62ea1a | 4476 | /*============================================*/ |
NYX | 0:85b3fd62ea1a | 4477 | else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE) |
NYX | 0:85b3fd62ea1a | 4478 | { |
NYX | 0:85b3fd62ea1a | 4479 | /* Coming from header phase, wait for CCF flag to be raised |
NYX | 0:85b3fd62ea1a | 4480 | if header present and fed to the IP in the previous phase */ |
NYX | 0:85b3fd62ea1a | 4481 | if (hcryp->Init.Header != NULL) |
NYX | 0:85b3fd62ea1a | 4482 | { |
NYX | 0:85b3fd62ea1a | 4483 | if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK) |
NYX | 0:85b3fd62ea1a | 4484 | { |
NYX | 0:85b3fd62ea1a | 4485 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 4486 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 4487 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 4488 | } |
NYX | 0:85b3fd62ea1a | 4489 | } |
NYX | 0:85b3fd62ea1a | 4490 | else |
NYX | 0:85b3fd62ea1a | 4491 | { |
NYX | 0:85b3fd62ea1a | 4492 | /* Enable the Peripheral since wasn't in header phase (no header case) */ |
NYX | 0:85b3fd62ea1a | 4493 | __HAL_CRYP_ENABLE(); |
NYX | 0:85b3fd62ea1a | 4494 | } |
NYX | 0:85b3fd62ea1a | 4495 | /* Clear CCF Flag */ |
NYX | 0:85b3fd62ea1a | 4496 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
NYX | 0:85b3fd62ea1a | 4497 | |
NYX | 0:85b3fd62ea1a | 4498 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PAYLOAD_PHASE); |
NYX | 0:85b3fd62ea1a | 4499 | |
NYX | 0:85b3fd62ea1a | 4500 | /* Specific handling to manage payload size less than 128 bits */ |
NYX | 0:85b3fd62ea1a | 4501 | if ((Size % 16U) != 0U) |
NYX | 0:85b3fd62ea1a | 4502 | { |
NYX | 0:85b3fd62ea1a | 4503 | inputaddr = (uint32_t)pInputData; |
NYX | 0:85b3fd62ea1a | 4504 | outputaddr = (uint32_t)pOutputData; |
NYX | 0:85b3fd62ea1a | 4505 | if (Size < 16U) |
NYX | 0:85b3fd62ea1a | 4506 | { |
NYX | 0:85b3fd62ea1a | 4507 | /* Block is now entered in polling mode, no actual gain in resorting to DMA */ |
NYX | 0:85b3fd62ea1a | 4508 | hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr; |
NYX | 0:85b3fd62ea1a | 4509 | hcryp->pCrypOutBuffPtr = (uint8_t *)outputaddr; |
NYX | 0:85b3fd62ea1a | 4510 | |
NYX | 0:85b3fd62ea1a | 4511 | CRYP_Padding(hcryp, (uint32_t)Size, CRYP_POLLING_ON); |
NYX | 0:85b3fd62ea1a | 4512 | |
NYX | 0:85b3fd62ea1a | 4513 | /* Change the CRYP state to ready */ |
NYX | 0:85b3fd62ea1a | 4514 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 4515 | /* Mark that the payload phase is over */ |
NYX | 0:85b3fd62ea1a | 4516 | hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER; |
NYX | 0:85b3fd62ea1a | 4517 | |
NYX | 0:85b3fd62ea1a | 4518 | /* Call output data transfer complete callback */ |
NYX | 0:85b3fd62ea1a | 4519 | HAL_CRYP_OutCpltCallback(hcryp); |
NYX | 0:85b3fd62ea1a | 4520 | } |
NYX | 0:85b3fd62ea1a | 4521 | else |
NYX | 0:85b3fd62ea1a | 4522 | { |
NYX | 0:85b3fd62ea1a | 4523 | payloadlength = (Size/16U) * 16U; |
NYX | 0:85b3fd62ea1a | 4524 | |
NYX | 0:85b3fd62ea1a | 4525 | /* Store the ending transfer points */ |
NYX | 0:85b3fd62ea1a | 4526 | hcryp->pCrypInBuffPtr = pInputData + payloadlength; |
NYX | 0:85b3fd62ea1a | 4527 | hcryp->pCrypOutBuffPtr = pOutputData + payloadlength; |
NYX | 0:85b3fd62ea1a | 4528 | hcryp->CrypInCount = (uint32_t)(Size - payloadlength); /* remainder */ |
NYX | 0:85b3fd62ea1a | 4529 | |
NYX | 0:85b3fd62ea1a | 4530 | /* Set the input and output addresses and start DMA transfer */ |
NYX | 0:85b3fd62ea1a | 4531 | /* (incomplete DMA transfer, will be wrapped up with data padding |
NYX | 0:85b3fd62ea1a | 4532 | after completion of the one initiated here) */ |
NYX | 0:85b3fd62ea1a | 4533 | CRYP_GCMCMAC_SetDMAConfig(hcryp, inputaddr, payloadlength, outputaddr); |
NYX | 0:85b3fd62ea1a | 4534 | } |
NYX | 0:85b3fd62ea1a | 4535 | } |
NYX | 0:85b3fd62ea1a | 4536 | else |
NYX | 0:85b3fd62ea1a | 4537 | { |
NYX | 0:85b3fd62ea1a | 4538 | hcryp->CrypInCount = 0U; |
NYX | 0:85b3fd62ea1a | 4539 | inputaddr = (uint32_t)pInputData; |
NYX | 0:85b3fd62ea1a | 4540 | outputaddr = (uint32_t)pOutputData; |
NYX | 0:85b3fd62ea1a | 4541 | |
NYX | 0:85b3fd62ea1a | 4542 | /* Set the input and output addresses and start DMA transfer */ |
NYX | 0:85b3fd62ea1a | 4543 | CRYP_GCMCMAC_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); |
NYX | 0:85b3fd62ea1a | 4544 | } |
NYX | 0:85b3fd62ea1a | 4545 | } |
NYX | 0:85b3fd62ea1a | 4546 | /*====================================*/ |
NYX | 0:85b3fd62ea1a | 4547 | /* GCM/GMAC or (CCM/)CMAC final phase */ |
NYX | 0:85b3fd62ea1a | 4548 | /*====================================*/ |
NYX | 0:85b3fd62ea1a | 4549 | else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE) |
NYX | 0:85b3fd62ea1a | 4550 | { |
NYX | 0:85b3fd62ea1a | 4551 | /* If coming from header phase (GMAC or CMAC case), |
NYX | 0:85b3fd62ea1a | 4552 | wait for CCF flag to be raised */ |
NYX | 0:85b3fd62ea1a | 4553 | if (READ_BIT(hcryp->Instance->CR, AES_CR_GCMPH) == CRYP_HEADER_PHASE) |
NYX | 0:85b3fd62ea1a | 4554 | { |
NYX | 0:85b3fd62ea1a | 4555 | if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK) |
NYX | 0:85b3fd62ea1a | 4556 | { |
NYX | 0:85b3fd62ea1a | 4557 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 4558 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 4559 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 4560 | } |
NYX | 0:85b3fd62ea1a | 4561 | /* Clear CCF Flag */ |
NYX | 0:85b3fd62ea1a | 4562 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
NYX | 0:85b3fd62ea1a | 4563 | } |
NYX | 0:85b3fd62ea1a | 4564 | |
NYX | 0:85b3fd62ea1a | 4565 | tagaddr = (uint32_t)pOutputData; |
NYX | 0:85b3fd62ea1a | 4566 | |
NYX | 0:85b3fd62ea1a | 4567 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_FINAL_PHASE); |
NYX | 0:85b3fd62ea1a | 4568 | |
NYX | 0:85b3fd62ea1a | 4569 | /* if the header and payload phases have been bypassed, AES must be enabled again */ |
NYX | 0:85b3fd62ea1a | 4570 | if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER) |
NYX | 0:85b3fd62ea1a | 4571 | { |
NYX | 0:85b3fd62ea1a | 4572 | __HAL_CRYP_ENABLE(); |
NYX | 0:85b3fd62ea1a | 4573 | } |
NYX | 0:85b3fd62ea1a | 4574 | |
NYX | 0:85b3fd62ea1a | 4575 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) |
NYX | 0:85b3fd62ea1a | 4576 | { |
NYX | 0:85b3fd62ea1a | 4577 | headerlength = hcryp->Init.HeaderSize * 8U; /* Header length in bits */ |
NYX | 0:85b3fd62ea1a | 4578 | inputlength = Size * 8U; /* input length in bits */ |
NYX | 0:85b3fd62ea1a | 4579 | /* Write the number of bits in the header on 64 bits followed by the number |
NYX | 0:85b3fd62ea1a | 4580 | of bits in the payload on 64 bits as well */ |
NYX | 0:85b3fd62ea1a | 4581 | if(hcryp->Init.DataType == CRYP_DATATYPE_1B) |
NYX | 0:85b3fd62ea1a | 4582 | { |
NYX | 0:85b3fd62ea1a | 4583 | hcryp->Instance->DINR = __RBIT((headerlength)>>32U); |
NYX | 0:85b3fd62ea1a | 4584 | hcryp->Instance->DINR = __RBIT(headerlength); |
NYX | 0:85b3fd62ea1a | 4585 | hcryp->Instance->DINR = __RBIT((inputlength)>>32U); |
NYX | 0:85b3fd62ea1a | 4586 | hcryp->Instance->DINR = __RBIT(inputlength); |
NYX | 0:85b3fd62ea1a | 4587 | } |
NYX | 0:85b3fd62ea1a | 4588 | else if(hcryp->Init.DataType == CRYP_DATATYPE_8B) |
NYX | 0:85b3fd62ea1a | 4589 | { |
NYX | 0:85b3fd62ea1a | 4590 | hcryp->Instance->DINR = __REV((headerlength)>>32U); |
NYX | 0:85b3fd62ea1a | 4591 | hcryp->Instance->DINR = __REV(headerlength); |
NYX | 0:85b3fd62ea1a | 4592 | hcryp->Instance->DINR = __REV((inputlength)>>32U); |
NYX | 0:85b3fd62ea1a | 4593 | hcryp->Instance->DINR = __REV(inputlength); |
NYX | 0:85b3fd62ea1a | 4594 | } |
NYX | 0:85b3fd62ea1a | 4595 | else if(hcryp->Init.DataType == CRYP_DATATYPE_16B) |
NYX | 0:85b3fd62ea1a | 4596 | { |
NYX | 0:85b3fd62ea1a | 4597 | hcryp->Instance->DINR = __ROR((headerlength)>>32U, 16U); |
NYX | 0:85b3fd62ea1a | 4598 | hcryp->Instance->DINR = __ROR(headerlength, 16U); |
NYX | 0:85b3fd62ea1a | 4599 | hcryp->Instance->DINR = __ROR((inputlength)>>32U, 16U); |
NYX | 0:85b3fd62ea1a | 4600 | hcryp->Instance->DINR = __ROR(inputlength, 16U); |
NYX | 0:85b3fd62ea1a | 4601 | } |
NYX | 0:85b3fd62ea1a | 4602 | else if(hcryp->Init.DataType == CRYP_DATATYPE_32B) |
NYX | 0:85b3fd62ea1a | 4603 | { |
NYX | 0:85b3fd62ea1a | 4604 | hcryp->Instance->DINR = (uint32_t)(headerlength>>32U); |
NYX | 0:85b3fd62ea1a | 4605 | hcryp->Instance->DINR = (uint32_t)(headerlength); |
NYX | 0:85b3fd62ea1a | 4606 | hcryp->Instance->DINR = (uint32_t)(inputlength>>32U); |
NYX | 0:85b3fd62ea1a | 4607 | hcryp->Instance->DINR = (uint32_t)(inputlength); |
NYX | 0:85b3fd62ea1a | 4608 | } |
NYX | 0:85b3fd62ea1a | 4609 | } |
NYX | 0:85b3fd62ea1a | 4610 | #if !defined(AES_CR_NPBLB) |
NYX | 0:85b3fd62ea1a | 4611 | else if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) |
NYX | 0:85b3fd62ea1a | 4612 | { |
NYX | 0:85b3fd62ea1a | 4613 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
NYX | 0:85b3fd62ea1a | 4614 | |
NYX | 0:85b3fd62ea1a | 4615 | inputaddr = (uint32_t)pInputData; |
NYX | 0:85b3fd62ea1a | 4616 | /* Enter the last block made of a 128-bit value formatted |
NYX | 0:85b3fd62ea1a | 4617 | from the original B0 packet. */ |
NYX | 0:85b3fd62ea1a | 4618 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 4619 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4620 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 4621 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4622 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 4623 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4624 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 4625 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4626 | } |
NYX | 0:85b3fd62ea1a | 4627 | #endif |
NYX | 0:85b3fd62ea1a | 4628 | |
NYX | 0:85b3fd62ea1a | 4629 | /* No DMA transfer is required at that point therefore, the software |
NYX | 0:85b3fd62ea1a | 4630 | just waits for the CCF flag to be raised. */ |
NYX | 0:85b3fd62ea1a | 4631 | if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK) |
NYX | 0:85b3fd62ea1a | 4632 | { |
NYX | 0:85b3fd62ea1a | 4633 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 4634 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 4635 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 4636 | } |
NYX | 0:85b3fd62ea1a | 4637 | /* Clear CCF Flag */ |
NYX | 0:85b3fd62ea1a | 4638 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
NYX | 0:85b3fd62ea1a | 4639 | /* Read the Auth TAG in the IN FIFO */ |
NYX | 0:85b3fd62ea1a | 4640 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR; |
NYX | 0:85b3fd62ea1a | 4641 | tagaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4642 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR; |
NYX | 0:85b3fd62ea1a | 4643 | tagaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4644 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR; |
NYX | 0:85b3fd62ea1a | 4645 | tagaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4646 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR; |
NYX | 0:85b3fd62ea1a | 4647 | |
NYX | 0:85b3fd62ea1a | 4648 | /* Mark that the final phase is over */ |
NYX | 0:85b3fd62ea1a | 4649 | hcryp->Phase = HAL_CRYP_PHASE_FINAL_OVER; |
NYX | 0:85b3fd62ea1a | 4650 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 4651 | /* Disable the Peripheral */ |
NYX | 0:85b3fd62ea1a | 4652 | __HAL_CRYP_DISABLE(); |
NYX | 0:85b3fd62ea1a | 4653 | } |
NYX | 0:85b3fd62ea1a | 4654 | /*=================================================*/ |
NYX | 0:85b3fd62ea1a | 4655 | /* case incorrect hcryp->Init.GCMCMACPhase setting */ |
NYX | 0:85b3fd62ea1a | 4656 | /*=================================================*/ |
NYX | 0:85b3fd62ea1a | 4657 | else |
NYX | 0:85b3fd62ea1a | 4658 | { |
NYX | 0:85b3fd62ea1a | 4659 | hcryp->State = HAL_CRYP_STATE_ERROR; |
NYX | 0:85b3fd62ea1a | 4660 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 4661 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 4662 | } |
NYX | 0:85b3fd62ea1a | 4663 | |
NYX | 0:85b3fd62ea1a | 4664 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 4665 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 4666 | |
NYX | 0:85b3fd62ea1a | 4667 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 4668 | } |
NYX | 0:85b3fd62ea1a | 4669 | else |
NYX | 0:85b3fd62ea1a | 4670 | { |
NYX | 0:85b3fd62ea1a | 4671 | return HAL_BUSY; |
NYX | 0:85b3fd62ea1a | 4672 | } |
NYX | 0:85b3fd62ea1a | 4673 | } |
NYX | 0:85b3fd62ea1a | 4674 | |
NYX | 0:85b3fd62ea1a | 4675 | /** |
NYX | 0:85b3fd62ea1a | 4676 | * @} |
NYX | 0:85b3fd62ea1a | 4677 | */ |
NYX | 0:85b3fd62ea1a | 4678 | |
NYX | 0:85b3fd62ea1a | 4679 | /** @defgroup CRYPEx_Exported_Functions_Group3 AES suspension/resumption functions |
NYX | 0:85b3fd62ea1a | 4680 | * @brief Extended processing functions. |
NYX | 0:85b3fd62ea1a | 4681 | * |
NYX | 0:85b3fd62ea1a | 4682 | @verbatim |
NYX | 0:85b3fd62ea1a | 4683 | ============================================================================== |
NYX | 0:85b3fd62ea1a | 4684 | ##### AES extended suspension and resumption functions ##### |
NYX | 0:85b3fd62ea1a | 4685 | ============================================================================== |
NYX | 0:85b3fd62ea1a | 4686 | [..] This section provides functions allowing to: |
NYX | 0:85b3fd62ea1a | 4687 | (+) save in memory the Initialization Vector, the Key registers, the Control register or |
NYX | 0:85b3fd62ea1a | 4688 | the Suspend registers when a process is suspended by a higher priority message |
NYX | 0:85b3fd62ea1a | 4689 | (+) write back in CRYP hardware block the saved values listed above when the suspended |
NYX | 0:85b3fd62ea1a | 4690 | lower priority message processing is resumed. |
NYX | 0:85b3fd62ea1a | 4691 | |
NYX | 0:85b3fd62ea1a | 4692 | @endverbatim |
NYX | 0:85b3fd62ea1a | 4693 | * @{ |
NYX | 0:85b3fd62ea1a | 4694 | */ |
NYX | 0:85b3fd62ea1a | 4695 | |
NYX | 0:85b3fd62ea1a | 4696 | |
NYX | 0:85b3fd62ea1a | 4697 | /** |
NYX | 0:85b3fd62ea1a | 4698 | * @brief In case of message processing suspension, read the Initialization Vector. |
NYX | 0:85b3fd62ea1a | 4699 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 4700 | * the configuration information for CRYP module. |
NYX | 0:85b3fd62ea1a | 4701 | * @param Output: Pointer to the buffer containing the saved Initialization Vector. |
NYX | 0:85b3fd62ea1a | 4702 | * @note This value has to be stored for reuse by writing the AES_IVRx registers |
NYX | 0:85b3fd62ea1a | 4703 | * as soon as the interrupted processing has to be resumed. |
NYX | 0:85b3fd62ea1a | 4704 | * Applicable to all chaining modes. |
NYX | 0:85b3fd62ea1a | 4705 | * @note AES must be disabled when reading or resetting the IV values. |
NYX | 0:85b3fd62ea1a | 4706 | * @retval None |
NYX | 0:85b3fd62ea1a | 4707 | */ |
NYX | 0:85b3fd62ea1a | 4708 | void HAL_CRYPEx_Read_IVRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Output) |
NYX | 0:85b3fd62ea1a | 4709 | { |
NYX | 0:85b3fd62ea1a | 4710 | uint32_t outputaddr = (uint32_t)Output; |
NYX | 0:85b3fd62ea1a | 4711 | |
NYX | 0:85b3fd62ea1a | 4712 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->IVR3); |
NYX | 0:85b3fd62ea1a | 4713 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4714 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->IVR2); |
NYX | 0:85b3fd62ea1a | 4715 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4716 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->IVR1); |
NYX | 0:85b3fd62ea1a | 4717 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4718 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->IVR0); |
NYX | 0:85b3fd62ea1a | 4719 | } |
NYX | 0:85b3fd62ea1a | 4720 | |
NYX | 0:85b3fd62ea1a | 4721 | /** |
NYX | 0:85b3fd62ea1a | 4722 | * @brief In case of message processing resumption, rewrite the Initialization |
NYX | 0:85b3fd62ea1a | 4723 | * Vector in the AES_IVRx registers. |
NYX | 0:85b3fd62ea1a | 4724 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 4725 | * the configuration information for CRYP module. |
NYX | 0:85b3fd62ea1a | 4726 | * @param Input: Pointer to the buffer containing the saved Initialization Vector to |
NYX | 0:85b3fd62ea1a | 4727 | * write back in the CRYP hardware block. |
NYX | 0:85b3fd62ea1a | 4728 | * @note Applicable to all chaining modes. |
NYX | 0:85b3fd62ea1a | 4729 | * @note AES must be disabled when reading or resetting the IV values. |
NYX | 0:85b3fd62ea1a | 4730 | * @retval None |
NYX | 0:85b3fd62ea1a | 4731 | */ |
NYX | 0:85b3fd62ea1a | 4732 | void HAL_CRYPEx_Write_IVRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Input) |
NYX | 0:85b3fd62ea1a | 4733 | { |
NYX | 0:85b3fd62ea1a | 4734 | uint32_t ivaddr = (uint32_t)Input; |
NYX | 0:85b3fd62ea1a | 4735 | |
NYX | 0:85b3fd62ea1a | 4736 | hcryp->Instance->IVR3 = __REV(*(uint32_t*)(ivaddr)); |
NYX | 0:85b3fd62ea1a | 4737 | ivaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4738 | hcryp->Instance->IVR2 = __REV(*(uint32_t*)(ivaddr)); |
NYX | 0:85b3fd62ea1a | 4739 | ivaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4740 | hcryp->Instance->IVR1 = __REV(*(uint32_t*)(ivaddr)); |
NYX | 0:85b3fd62ea1a | 4741 | ivaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4742 | hcryp->Instance->IVR0 = __REV(*(uint32_t*)(ivaddr)); |
NYX | 0:85b3fd62ea1a | 4743 | } |
NYX | 0:85b3fd62ea1a | 4744 | |
NYX | 0:85b3fd62ea1a | 4745 | |
NYX | 0:85b3fd62ea1a | 4746 | /** |
NYX | 0:85b3fd62ea1a | 4747 | * @brief In case of message GCM/GMAC or CMAC processing suspension, read the Suspend Registers. |
NYX | 0:85b3fd62ea1a | 4748 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 4749 | * the configuration information for CRYP module. |
NYX | 0:85b3fd62ea1a | 4750 | * @param Output: Pointer to the buffer containing the saved Suspend Registers. |
NYX | 0:85b3fd62ea1a | 4751 | * @note These values have to be stored for reuse by writing back the AES_SUSPxR registers |
NYX | 0:85b3fd62ea1a | 4752 | * as soon as the interrupted processing has to be resumed. |
NYX | 0:85b3fd62ea1a | 4753 | * @retval None |
NYX | 0:85b3fd62ea1a | 4754 | */ |
NYX | 0:85b3fd62ea1a | 4755 | void HAL_CRYPEx_Read_SuspendRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Output) |
NYX | 0:85b3fd62ea1a | 4756 | { |
NYX | 0:85b3fd62ea1a | 4757 | uint32_t outputaddr = (uint32_t)Output; |
NYX | 0:85b3fd62ea1a | 4758 | |
NYX | 0:85b3fd62ea1a | 4759 | /* In case of GCM payload phase encryption, check that suspension can be carried out */ |
NYX | 0:85b3fd62ea1a | 4760 | if (READ_BIT(hcryp->Instance->CR, (AES_CR_GCMPH|AES_CR_MODE)) == (CRYP_GCM_PAYLOAD_PHASE|CRYP_ALGOMODE_ENCRYPT)) |
NYX | 0:85b3fd62ea1a | 4761 | { |
NYX | 0:85b3fd62ea1a | 4762 | /* Ensure that Busy flag is reset */ |
NYX | 0:85b3fd62ea1a | 4763 | if(CRYP_WaitOnBusyFlagReset(hcryp, CRYP_BUSY_TIMEOUTVALUE) != HAL_OK) |
NYX | 0:85b3fd62ea1a | 4764 | { |
NYX | 0:85b3fd62ea1a | 4765 | hcryp->ErrorCode |= HAL_CRYP_BUSY_ERROR; |
NYX | 0:85b3fd62ea1a | 4766 | hcryp->State = HAL_CRYP_STATE_ERROR; |
NYX | 0:85b3fd62ea1a | 4767 | |
NYX | 0:85b3fd62ea1a | 4768 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 4769 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 4770 | |
NYX | 0:85b3fd62ea1a | 4771 | HAL_CRYP_ErrorCallback(hcryp); |
NYX | 0:85b3fd62ea1a | 4772 | return ; |
NYX | 0:85b3fd62ea1a | 4773 | } |
NYX | 0:85b3fd62ea1a | 4774 | } |
NYX | 0:85b3fd62ea1a | 4775 | |
NYX | 0:85b3fd62ea1a | 4776 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP7R); |
NYX | 0:85b3fd62ea1a | 4777 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4778 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP6R); |
NYX | 0:85b3fd62ea1a | 4779 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4780 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP5R); |
NYX | 0:85b3fd62ea1a | 4781 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4782 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP4R); |
NYX | 0:85b3fd62ea1a | 4783 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4784 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP3R); |
NYX | 0:85b3fd62ea1a | 4785 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4786 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP2R); |
NYX | 0:85b3fd62ea1a | 4787 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4788 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP1R); |
NYX | 0:85b3fd62ea1a | 4789 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4790 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP0R); |
NYX | 0:85b3fd62ea1a | 4791 | } |
NYX | 0:85b3fd62ea1a | 4792 | |
NYX | 0:85b3fd62ea1a | 4793 | /** |
NYX | 0:85b3fd62ea1a | 4794 | * @brief In case of message GCM/GMAC or CMAC processing resumption, rewrite the Suspend |
NYX | 0:85b3fd62ea1a | 4795 | * Registers in the AES_SUSPxR registers. |
NYX | 0:85b3fd62ea1a | 4796 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 4797 | * the configuration information for CRYP module. |
NYX | 0:85b3fd62ea1a | 4798 | * @param Input: Pointer to the buffer containing the saved suspend registers to |
NYX | 0:85b3fd62ea1a | 4799 | * write back in the CRYP hardware block. |
NYX | 0:85b3fd62ea1a | 4800 | * @retval None |
NYX | 0:85b3fd62ea1a | 4801 | */ |
NYX | 0:85b3fd62ea1a | 4802 | void HAL_CRYPEx_Write_SuspendRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Input) |
NYX | 0:85b3fd62ea1a | 4803 | { |
NYX | 0:85b3fd62ea1a | 4804 | uint32_t ivaddr = (uint32_t)Input; |
NYX | 0:85b3fd62ea1a | 4805 | |
NYX | 0:85b3fd62ea1a | 4806 | hcryp->Instance->SUSP7R = __REV(*(uint32_t*)(ivaddr)); |
NYX | 0:85b3fd62ea1a | 4807 | ivaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4808 | hcryp->Instance->SUSP6R = __REV(*(uint32_t*)(ivaddr)); |
NYX | 0:85b3fd62ea1a | 4809 | ivaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4810 | hcryp->Instance->SUSP5R = __REV(*(uint32_t*)(ivaddr)); |
NYX | 0:85b3fd62ea1a | 4811 | ivaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4812 | hcryp->Instance->SUSP4R = __REV(*(uint32_t*)(ivaddr)); |
NYX | 0:85b3fd62ea1a | 4813 | ivaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4814 | hcryp->Instance->SUSP3R = __REV(*(uint32_t*)(ivaddr)); |
NYX | 0:85b3fd62ea1a | 4815 | ivaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4816 | hcryp->Instance->SUSP2R = __REV(*(uint32_t*)(ivaddr)); |
NYX | 0:85b3fd62ea1a | 4817 | ivaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4818 | hcryp->Instance->SUSP1R = __REV(*(uint32_t*)(ivaddr)); |
NYX | 0:85b3fd62ea1a | 4819 | ivaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4820 | hcryp->Instance->SUSP0R = __REV(*(uint32_t*)(ivaddr)); |
NYX | 0:85b3fd62ea1a | 4821 | } |
NYX | 0:85b3fd62ea1a | 4822 | |
NYX | 0:85b3fd62ea1a | 4823 | |
NYX | 0:85b3fd62ea1a | 4824 | /** |
NYX | 0:85b3fd62ea1a | 4825 | * @brief In case of message GCM/GMAC or CMAC processing suspension, read the Key Registers. |
NYX | 0:85b3fd62ea1a | 4826 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 4827 | * the configuration information for CRYP module. |
NYX | 0:85b3fd62ea1a | 4828 | * @param Output: Pointer to the buffer containing the saved Key Registers. |
NYX | 0:85b3fd62ea1a | 4829 | * @param KeySize: Indicates the key size (128 or 256 bits). |
NYX | 0:85b3fd62ea1a | 4830 | * @note These values have to be stored for reuse by writing back the AES_KEYRx registers |
NYX | 0:85b3fd62ea1a | 4831 | * as soon as the interrupted processing has to be resumed. |
NYX | 0:85b3fd62ea1a | 4832 | * @retval None |
NYX | 0:85b3fd62ea1a | 4833 | */ |
NYX | 0:85b3fd62ea1a | 4834 | void HAL_CRYPEx_Read_KeyRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Output, uint32_t KeySize) |
NYX | 0:85b3fd62ea1a | 4835 | { |
NYX | 0:85b3fd62ea1a | 4836 | uint32_t keyaddr = (uint32_t)Output; |
NYX | 0:85b3fd62ea1a | 4837 | |
NYX | 0:85b3fd62ea1a | 4838 | if (KeySize == CRYP_KEYSIZE_256B) |
NYX | 0:85b3fd62ea1a | 4839 | { |
NYX | 0:85b3fd62ea1a | 4840 | *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR7); |
NYX | 0:85b3fd62ea1a | 4841 | keyaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4842 | *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR6); |
NYX | 0:85b3fd62ea1a | 4843 | keyaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4844 | *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR5); |
NYX | 0:85b3fd62ea1a | 4845 | keyaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4846 | *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR4); |
NYX | 0:85b3fd62ea1a | 4847 | keyaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4848 | } |
NYX | 0:85b3fd62ea1a | 4849 | |
NYX | 0:85b3fd62ea1a | 4850 | *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR3); |
NYX | 0:85b3fd62ea1a | 4851 | keyaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4852 | *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR2); |
NYX | 0:85b3fd62ea1a | 4853 | keyaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4854 | *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR1); |
NYX | 0:85b3fd62ea1a | 4855 | keyaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4856 | *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR0); |
NYX | 0:85b3fd62ea1a | 4857 | } |
NYX | 0:85b3fd62ea1a | 4858 | |
NYX | 0:85b3fd62ea1a | 4859 | /** |
NYX | 0:85b3fd62ea1a | 4860 | * @brief In case of message GCM/GMAC or CMAC processing resumption, rewrite the Key |
NYX | 0:85b3fd62ea1a | 4861 | * Registers in the AES_KEYRx registers. |
NYX | 0:85b3fd62ea1a | 4862 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 4863 | * the configuration information for CRYP module. |
NYX | 0:85b3fd62ea1a | 4864 | * @param Input: Pointer to the buffer containing the saved key registers to |
NYX | 0:85b3fd62ea1a | 4865 | * write back in the CRYP hardware block. |
NYX | 0:85b3fd62ea1a | 4866 | * @param KeySize: Indicates the key size (128 or 256 bits) |
NYX | 0:85b3fd62ea1a | 4867 | * @retval None |
NYX | 0:85b3fd62ea1a | 4868 | */ |
NYX | 0:85b3fd62ea1a | 4869 | void HAL_CRYPEx_Write_KeyRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint32_t KeySize) |
NYX | 0:85b3fd62ea1a | 4870 | { |
NYX | 0:85b3fd62ea1a | 4871 | uint32_t keyaddr = (uint32_t)Input; |
NYX | 0:85b3fd62ea1a | 4872 | |
NYX | 0:85b3fd62ea1a | 4873 | if (KeySize == CRYP_KEYSIZE_256B) |
NYX | 0:85b3fd62ea1a | 4874 | { |
NYX | 0:85b3fd62ea1a | 4875 | hcryp->Instance->KEYR7 = __REV(*(uint32_t*)(keyaddr)); |
NYX | 0:85b3fd62ea1a | 4876 | keyaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4877 | hcryp->Instance->KEYR6 = __REV(*(uint32_t*)(keyaddr)); |
NYX | 0:85b3fd62ea1a | 4878 | keyaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4879 | hcryp->Instance->KEYR5 = __REV(*(uint32_t*)(keyaddr)); |
NYX | 0:85b3fd62ea1a | 4880 | keyaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4881 | hcryp->Instance->KEYR4 = __REV(*(uint32_t*)(keyaddr)); |
NYX | 0:85b3fd62ea1a | 4882 | keyaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4883 | } |
NYX | 0:85b3fd62ea1a | 4884 | |
NYX | 0:85b3fd62ea1a | 4885 | hcryp->Instance->KEYR3 = __REV(*(uint32_t*)(keyaddr)); |
NYX | 0:85b3fd62ea1a | 4886 | keyaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4887 | hcryp->Instance->KEYR2 = __REV(*(uint32_t*)(keyaddr)); |
NYX | 0:85b3fd62ea1a | 4888 | keyaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4889 | hcryp->Instance->KEYR1 = __REV(*(uint32_t*)(keyaddr)); |
NYX | 0:85b3fd62ea1a | 4890 | keyaddr+=4U; |
NYX | 0:85b3fd62ea1a | 4891 | hcryp->Instance->KEYR0 = __REV(*(uint32_t*)(keyaddr)); |
NYX | 0:85b3fd62ea1a | 4892 | } |
NYX | 0:85b3fd62ea1a | 4893 | |
NYX | 0:85b3fd62ea1a | 4894 | |
NYX | 0:85b3fd62ea1a | 4895 | /** |
NYX | 0:85b3fd62ea1a | 4896 | * @brief In case of message GCM/GMAC or CMAC processing suspension, read the Control Register. |
NYX | 0:85b3fd62ea1a | 4897 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 4898 | * the configuration information for CRYP module. |
NYX | 0:85b3fd62ea1a | 4899 | * @param Output: Pointer to the buffer containing the saved Control Register. |
NYX | 0:85b3fd62ea1a | 4900 | * @note This values has to be stored for reuse by writing back the AES_CR register |
NYX | 0:85b3fd62ea1a | 4901 | * as soon as the interrupted processing has to be resumed. |
NYX | 0:85b3fd62ea1a | 4902 | * @retval None |
NYX | 0:85b3fd62ea1a | 4903 | */ |
NYX | 0:85b3fd62ea1a | 4904 | void HAL_CRYPEx_Read_ControlRegister(CRYP_HandleTypeDef *hcryp, uint8_t* Output) |
NYX | 0:85b3fd62ea1a | 4905 | { |
NYX | 0:85b3fd62ea1a | 4906 | *(uint32_t*)(Output) = hcryp->Instance->CR; |
NYX | 0:85b3fd62ea1a | 4907 | } |
NYX | 0:85b3fd62ea1a | 4908 | |
NYX | 0:85b3fd62ea1a | 4909 | /** |
NYX | 0:85b3fd62ea1a | 4910 | * @brief In case of message GCM/GMAC or CMAC processing resumption, rewrite the Control |
NYX | 0:85b3fd62ea1a | 4911 | * Registers in the AES_CR register. |
NYX | 0:85b3fd62ea1a | 4912 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 4913 | * the configuration information for CRYP module. |
NYX | 0:85b3fd62ea1a | 4914 | * @param Input: Pointer to the buffer containing the saved Control Register to |
NYX | 0:85b3fd62ea1a | 4915 | * write back in the CRYP hardware block. |
NYX | 0:85b3fd62ea1a | 4916 | * @retval None |
NYX | 0:85b3fd62ea1a | 4917 | */ |
NYX | 0:85b3fd62ea1a | 4918 | void HAL_CRYPEx_Write_ControlRegister(CRYP_HandleTypeDef *hcryp, uint8_t* Input) |
NYX | 0:85b3fd62ea1a | 4919 | { |
NYX | 0:85b3fd62ea1a | 4920 | hcryp->Instance->CR = *(uint32_t*)(Input); |
NYX | 0:85b3fd62ea1a | 4921 | /* At the same time, set handle state back to READY to be able to resume the AES calculations |
NYX | 0:85b3fd62ea1a | 4922 | without the processing APIs returning HAL_BUSY when called. */ |
NYX | 0:85b3fd62ea1a | 4923 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 4924 | } |
NYX | 0:85b3fd62ea1a | 4925 | |
NYX | 0:85b3fd62ea1a | 4926 | /** |
NYX | 0:85b3fd62ea1a | 4927 | * @brief Request CRYP processing suspension when in polling or interruption mode. |
NYX | 0:85b3fd62ea1a | 4928 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 4929 | * the configuration information for CRYP module. |
NYX | 0:85b3fd62ea1a | 4930 | * @note Set the handle field SuspendRequest to the appropriate value so that |
NYX | 0:85b3fd62ea1a | 4931 | * the on-going CRYP processing is suspended as soon as the required |
NYX | 0:85b3fd62ea1a | 4932 | * conditions are met. |
NYX | 0:85b3fd62ea1a | 4933 | * @note It is advised not to suspend the CRYP processing when the DMA controller |
NYX | 0:85b3fd62ea1a | 4934 | * is managing the data transfer |
NYX | 0:85b3fd62ea1a | 4935 | * @retval None |
NYX | 0:85b3fd62ea1a | 4936 | */ |
NYX | 0:85b3fd62ea1a | 4937 | void HAL_CRYPEx_ProcessSuspend(CRYP_HandleTypeDef *hcryp) |
NYX | 0:85b3fd62ea1a | 4938 | { |
NYX | 0:85b3fd62ea1a | 4939 | /* Set Handle Suspend Request field */ |
NYX | 0:85b3fd62ea1a | 4940 | hcryp->SuspendRequest = HAL_CRYP_SUSPEND; |
NYX | 0:85b3fd62ea1a | 4941 | } |
NYX | 0:85b3fd62ea1a | 4942 | |
NYX | 0:85b3fd62ea1a | 4943 | /** |
NYX | 0:85b3fd62ea1a | 4944 | * @} |
NYX | 0:85b3fd62ea1a | 4945 | */ |
NYX | 0:85b3fd62ea1a | 4946 | |
NYX | 0:85b3fd62ea1a | 4947 | /** |
NYX | 0:85b3fd62ea1a | 4948 | * @} |
NYX | 0:85b3fd62ea1a | 4949 | */ |
NYX | 0:85b3fd62ea1a | 4950 | |
NYX | 0:85b3fd62ea1a | 4951 | /** @addtogroup CRYPEx_Private_Functions |
NYX | 0:85b3fd62ea1a | 4952 | * @{ |
NYX | 0:85b3fd62ea1a | 4953 | */ |
NYX | 0:85b3fd62ea1a | 4954 | |
NYX | 0:85b3fd62ea1a | 4955 | /** |
NYX | 0:85b3fd62ea1a | 4956 | * @brief DMA CRYP Input Data process complete callback |
NYX | 0:85b3fd62ea1a | 4957 | * for GCM, GMAC or CMAC chainging modes. |
NYX | 0:85b3fd62ea1a | 4958 | * @note Specific setting of hcryp fields are required only |
NYX | 0:85b3fd62ea1a | 4959 | * in the case of header phase where no output data DMA |
NYX | 0:85b3fd62ea1a | 4960 | * transfer is on-going (only input data transfer is enabled |
NYX | 0:85b3fd62ea1a | 4961 | * in such a case). |
NYX | 0:85b3fd62ea1a | 4962 | * @param hdma: DMA handle. |
NYX | 0:85b3fd62ea1a | 4963 | * @retval None |
NYX | 0:85b3fd62ea1a | 4964 | */ |
NYX | 0:85b3fd62ea1a | 4965 | static void CRYP_GCMCMAC_DMAInCplt(DMA_HandleTypeDef *hdma) |
NYX | 0:85b3fd62ea1a | 4966 | { |
NYX | 0:85b3fd62ea1a | 4967 | uint32_t difflength = 0U; |
NYX | 0:85b3fd62ea1a | 4968 | |
NYX | 0:85b3fd62ea1a | 4969 | CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
NYX | 0:85b3fd62ea1a | 4970 | |
NYX | 0:85b3fd62ea1a | 4971 | /* Disable the DMA transfer for input request */ |
NYX | 0:85b3fd62ea1a | 4972 | CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAINEN); |
NYX | 0:85b3fd62ea1a | 4973 | |
NYX | 0:85b3fd62ea1a | 4974 | if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE) |
NYX | 0:85b3fd62ea1a | 4975 | { |
NYX | 0:85b3fd62ea1a | 4976 | |
NYX | 0:85b3fd62ea1a | 4977 | if (hcryp->CrypInCount != 0U) |
NYX | 0:85b3fd62ea1a | 4978 | { |
NYX | 0:85b3fd62ea1a | 4979 | /* Last block is now entered in polling mode, no actual gain in resorting to DMA */ |
NYX | 0:85b3fd62ea1a | 4980 | difflength = hcryp->CrypInCount; |
NYX | 0:85b3fd62ea1a | 4981 | hcryp->CrypInCount = 0U; |
NYX | 0:85b3fd62ea1a | 4982 | |
NYX | 0:85b3fd62ea1a | 4983 | CRYP_Padding(hcryp, difflength, CRYP_POLLING_OFF); |
NYX | 0:85b3fd62ea1a | 4984 | } |
NYX | 0:85b3fd62ea1a | 4985 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 4986 | /* Mark that the header phase is over */ |
NYX | 0:85b3fd62ea1a | 4987 | hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER; |
NYX | 0:85b3fd62ea1a | 4988 | } |
NYX | 0:85b3fd62ea1a | 4989 | /* CCF flag indicating header phase AES processing completion |
NYX | 0:85b3fd62ea1a | 4990 | will be checked at the start of the next phase: |
NYX | 0:85b3fd62ea1a | 4991 | - payload phase (GCM or CCM when applicable) |
NYX | 0:85b3fd62ea1a | 4992 | - final phase (GMAC or CMAC). |
NYX | 0:85b3fd62ea1a | 4993 | This allows to avoid the Wait on Flag within the IRQ handling. */ |
NYX | 0:85b3fd62ea1a | 4994 | |
NYX | 0:85b3fd62ea1a | 4995 | /* Call input data transfer complete callback */ |
NYX | 0:85b3fd62ea1a | 4996 | HAL_CRYP_InCpltCallback(hcryp); |
NYX | 0:85b3fd62ea1a | 4997 | } |
NYX | 0:85b3fd62ea1a | 4998 | |
NYX | 0:85b3fd62ea1a | 4999 | /** |
NYX | 0:85b3fd62ea1a | 5000 | * @brief DMA CRYP Output Data process complete callback |
NYX | 0:85b3fd62ea1a | 5001 | * for GCM, GMAC or CMAC chainging modes. |
NYX | 0:85b3fd62ea1a | 5002 | * @note This callback is called only in the payload phase. |
NYX | 0:85b3fd62ea1a | 5003 | * @param hdma: DMA handle. |
NYX | 0:85b3fd62ea1a | 5004 | * @retval None |
NYX | 0:85b3fd62ea1a | 5005 | */ |
NYX | 0:85b3fd62ea1a | 5006 | static void CRYP_GCMCMAC_DMAOutCplt(DMA_HandleTypeDef *hdma) |
NYX | 0:85b3fd62ea1a | 5007 | { |
NYX | 0:85b3fd62ea1a | 5008 | uint32_t difflength = 0U; |
NYX | 0:85b3fd62ea1a | 5009 | CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
NYX | 0:85b3fd62ea1a | 5010 | |
NYX | 0:85b3fd62ea1a | 5011 | /* Disable the DMA transfer for output request */ |
NYX | 0:85b3fd62ea1a | 5012 | CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAOUTEN); |
NYX | 0:85b3fd62ea1a | 5013 | |
NYX | 0:85b3fd62ea1a | 5014 | /* Clear CCF Flag */ |
NYX | 0:85b3fd62ea1a | 5015 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
NYX | 0:85b3fd62ea1a | 5016 | |
NYX | 0:85b3fd62ea1a | 5017 | /* Initiate additional transfer to wrap-up data feeding to the IP */ |
NYX | 0:85b3fd62ea1a | 5018 | if (hcryp->CrypInCount != 0U) |
NYX | 0:85b3fd62ea1a | 5019 | { |
NYX | 0:85b3fd62ea1a | 5020 | /* Last block is now entered in polling mode, no actual gain in resorting to DMA */ |
NYX | 0:85b3fd62ea1a | 5021 | difflength = hcryp->CrypInCount; |
NYX | 0:85b3fd62ea1a | 5022 | hcryp->CrypInCount = 0U; |
NYX | 0:85b3fd62ea1a | 5023 | |
NYX | 0:85b3fd62ea1a | 5024 | CRYP_Padding(hcryp, difflength, CRYP_POLLING_ON); |
NYX | 0:85b3fd62ea1a | 5025 | } |
NYX | 0:85b3fd62ea1a | 5026 | |
NYX | 0:85b3fd62ea1a | 5027 | /* Change the CRYP state to ready */ |
NYX | 0:85b3fd62ea1a | 5028 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 5029 | /* Mark that the payload phase is over */ |
NYX | 0:85b3fd62ea1a | 5030 | hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER; |
NYX | 0:85b3fd62ea1a | 5031 | |
NYX | 0:85b3fd62ea1a | 5032 | /* Call output data transfer complete callback */ |
NYX | 0:85b3fd62ea1a | 5033 | HAL_CRYP_OutCpltCallback(hcryp); |
NYX | 0:85b3fd62ea1a | 5034 | } |
NYX | 0:85b3fd62ea1a | 5035 | |
NYX | 0:85b3fd62ea1a | 5036 | /** |
NYX | 0:85b3fd62ea1a | 5037 | * @brief DMA CRYP communication error callback |
NYX | 0:85b3fd62ea1a | 5038 | * for GCM, GMAC or CMAC chainging modes. |
NYX | 0:85b3fd62ea1a | 5039 | * @param hdma: DMA handle |
NYX | 0:85b3fd62ea1a | 5040 | * @retval None |
NYX | 0:85b3fd62ea1a | 5041 | */ |
NYX | 0:85b3fd62ea1a | 5042 | static void CRYP_GCMCMAC_DMAError(DMA_HandleTypeDef *hdma) |
NYX | 0:85b3fd62ea1a | 5043 | { |
NYX | 0:85b3fd62ea1a | 5044 | CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
NYX | 0:85b3fd62ea1a | 5045 | |
NYX | 0:85b3fd62ea1a | 5046 | hcryp->State= HAL_CRYP_STATE_ERROR; |
NYX | 0:85b3fd62ea1a | 5047 | hcryp->ErrorCode |= HAL_CRYP_DMA_ERROR; |
NYX | 0:85b3fd62ea1a | 5048 | HAL_CRYP_ErrorCallback(hcryp); |
NYX | 0:85b3fd62ea1a | 5049 | /* Clear Error Flag */ |
NYX | 0:85b3fd62ea1a | 5050 | __HAL_CRYP_CLEAR_FLAG(CRYP_ERR_CLEAR); |
NYX | 0:85b3fd62ea1a | 5051 | } |
NYX | 0:85b3fd62ea1a | 5052 | |
NYX | 0:85b3fd62ea1a | 5053 | /** |
NYX | 0:85b3fd62ea1a | 5054 | * @brief Handle CRYP block input/output data handling under interruption |
NYX | 0:85b3fd62ea1a | 5055 | * for GCM, GMAC or CMAC chaining modes. |
NYX | 0:85b3fd62ea1a | 5056 | * @note The function is called under interruption only, once |
NYX | 0:85b3fd62ea1a | 5057 | * interruptions have been enabled by HAL_CRYPEx_AES_Auth_IT(). |
NYX | 0:85b3fd62ea1a | 5058 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 5059 | * the configuration information for CRYP module |
NYX | 0:85b3fd62ea1a | 5060 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 5061 | */ |
NYX | 0:85b3fd62ea1a | 5062 | HAL_StatusTypeDef CRYP_AES_Auth_IT(CRYP_HandleTypeDef *hcryp) |
NYX | 0:85b3fd62ea1a | 5063 | { |
NYX | 0:85b3fd62ea1a | 5064 | uint32_t inputaddr = 0x0U; |
NYX | 0:85b3fd62ea1a | 5065 | uint32_t outputaddr = 0x0U; |
NYX | 0:85b3fd62ea1a | 5066 | uint32_t index = 0x0U; |
NYX | 0:85b3fd62ea1a | 5067 | uint32_t addhoc_process = 0U; |
NYX | 0:85b3fd62ea1a | 5068 | uint32_t difflength = 0U; |
NYX | 0:85b3fd62ea1a | 5069 | uint32_t difflengthmod4 = 0U; |
NYX | 0:85b3fd62ea1a | 5070 | uint32_t mask[3] = {0x0FFU, 0x0FFFFU, 0x0FFFFFFU}; |
NYX | 0:85b3fd62ea1a | 5071 | uint32_t intermediate_data[4U] = {0U}; |
NYX | 0:85b3fd62ea1a | 5072 | |
NYX | 0:85b3fd62ea1a | 5073 | if(hcryp->State == HAL_CRYP_STATE_BUSY) |
NYX | 0:85b3fd62ea1a | 5074 | { |
NYX | 0:85b3fd62ea1a | 5075 | /*===========================*/ |
NYX | 0:85b3fd62ea1a | 5076 | /* GCM/GMAC(/CCM) init phase */ |
NYX | 0:85b3fd62ea1a | 5077 | /*===========================*/ |
NYX | 0:85b3fd62ea1a | 5078 | if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE) |
NYX | 0:85b3fd62ea1a | 5079 | { |
NYX | 0:85b3fd62ea1a | 5080 | /* Clear Computation Complete Flag */ |
NYX | 0:85b3fd62ea1a | 5081 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
NYX | 0:85b3fd62ea1a | 5082 | /* Disable Computation Complete Flag and Errors Interrupts */ |
NYX | 0:85b3fd62ea1a | 5083 | __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); |
NYX | 0:85b3fd62ea1a | 5084 | /* Change the CRYP state */ |
NYX | 0:85b3fd62ea1a | 5085 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 5086 | |
NYX | 0:85b3fd62ea1a | 5087 | /* Mark that the initialization phase is over */ |
NYX | 0:85b3fd62ea1a | 5088 | hcryp->Phase = HAL_CRYP_PHASE_INIT_OVER; |
NYX | 0:85b3fd62ea1a | 5089 | |
NYX | 0:85b3fd62ea1a | 5090 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 5091 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 5092 | /* Call computation complete callback */ |
NYX | 0:85b3fd62ea1a | 5093 | HAL_CRYPEx_ComputationCpltCallback(hcryp); |
NYX | 0:85b3fd62ea1a | 5094 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 5095 | } |
NYX | 0:85b3fd62ea1a | 5096 | /*=====================================*/ |
NYX | 0:85b3fd62ea1a | 5097 | /* GCM/GMAC or (CCM/)CMAC header phase */ |
NYX | 0:85b3fd62ea1a | 5098 | /*=====================================*/ |
NYX | 0:85b3fd62ea1a | 5099 | else if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE) |
NYX | 0:85b3fd62ea1a | 5100 | { |
NYX | 0:85b3fd62ea1a | 5101 | /* Check if all input header data have been entered */ |
NYX | 0:85b3fd62ea1a | 5102 | if (hcryp->CrypInCount == 0U) |
NYX | 0:85b3fd62ea1a | 5103 | { |
NYX | 0:85b3fd62ea1a | 5104 | /* Clear Computation Complete Flag */ |
NYX | 0:85b3fd62ea1a | 5105 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
NYX | 0:85b3fd62ea1a | 5106 | /* Disable Computation Complete Flag and Errors Interrupts */ |
NYX | 0:85b3fd62ea1a | 5107 | __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); |
NYX | 0:85b3fd62ea1a | 5108 | /* Change the CRYP state */ |
NYX | 0:85b3fd62ea1a | 5109 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 5110 | /* Mark that the header phase is over */ |
NYX | 0:85b3fd62ea1a | 5111 | hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER; |
NYX | 0:85b3fd62ea1a | 5112 | |
NYX | 0:85b3fd62ea1a | 5113 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 5114 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 5115 | |
NYX | 0:85b3fd62ea1a | 5116 | /* Call computation complete callback */ |
NYX | 0:85b3fd62ea1a | 5117 | HAL_CRYPEx_ComputationCpltCallback(hcryp); |
NYX | 0:85b3fd62ea1a | 5118 | |
NYX | 0:85b3fd62ea1a | 5119 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 5120 | } |
NYX | 0:85b3fd62ea1a | 5121 | /* If suspension flag has been raised, suspend processing */ |
NYX | 0:85b3fd62ea1a | 5122 | else if (hcryp->SuspendRequest == HAL_CRYP_SUSPEND) |
NYX | 0:85b3fd62ea1a | 5123 | { |
NYX | 0:85b3fd62ea1a | 5124 | /* Clear CCF Flag */ |
NYX | 0:85b3fd62ea1a | 5125 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
NYX | 0:85b3fd62ea1a | 5126 | |
NYX | 0:85b3fd62ea1a | 5127 | /* reset SuspendRequest */ |
NYX | 0:85b3fd62ea1a | 5128 | hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE; |
NYX | 0:85b3fd62ea1a | 5129 | /* Disable Computation Complete Flag and Errors Interrupts */ |
NYX | 0:85b3fd62ea1a | 5130 | __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); |
NYX | 0:85b3fd62ea1a | 5131 | /* Change the CRYP state */ |
NYX | 0:85b3fd62ea1a | 5132 | hcryp->State = HAL_CRYP_STATE_SUSPENDED; |
NYX | 0:85b3fd62ea1a | 5133 | /* Mark that the header phase is over */ |
NYX | 0:85b3fd62ea1a | 5134 | hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED; |
NYX | 0:85b3fd62ea1a | 5135 | |
NYX | 0:85b3fd62ea1a | 5136 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 5137 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 5138 | |
NYX | 0:85b3fd62ea1a | 5139 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 5140 | } |
NYX | 0:85b3fd62ea1a | 5141 | else /* Carry on feeding input data to the CRYP hardware block */ |
NYX | 0:85b3fd62ea1a | 5142 | { |
NYX | 0:85b3fd62ea1a | 5143 | /* Clear Computation Complete Flag */ |
NYX | 0:85b3fd62ea1a | 5144 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
NYX | 0:85b3fd62ea1a | 5145 | /* Get the last Input data address */ |
NYX | 0:85b3fd62ea1a | 5146 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
NYX | 0:85b3fd62ea1a | 5147 | |
NYX | 0:85b3fd62ea1a | 5148 | /* Increment/decrement instance pointer/counter */ |
NYX | 0:85b3fd62ea1a | 5149 | if (hcryp->CrypInCount < 16U) |
NYX | 0:85b3fd62ea1a | 5150 | { |
NYX | 0:85b3fd62ea1a | 5151 | difflength = hcryp->CrypInCount; |
NYX | 0:85b3fd62ea1a | 5152 | hcryp->CrypInCount = 0U; |
NYX | 0:85b3fd62ea1a | 5153 | addhoc_process = 1U; |
NYX | 0:85b3fd62ea1a | 5154 | difflengthmod4 = difflength%4U; |
NYX | 0:85b3fd62ea1a | 5155 | } |
NYX | 0:85b3fd62ea1a | 5156 | else |
NYX | 0:85b3fd62ea1a | 5157 | { |
NYX | 0:85b3fd62ea1a | 5158 | hcryp->pCrypInBuffPtr += 16U; |
NYX | 0:85b3fd62ea1a | 5159 | hcryp->CrypInCount -= 16U; |
NYX | 0:85b3fd62ea1a | 5160 | } |
NYX | 0:85b3fd62ea1a | 5161 | |
NYX | 0:85b3fd62ea1a | 5162 | #if defined(AES_CR_NPBLB) |
NYX | 0:85b3fd62ea1a | 5163 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC) |
NYX | 0:85b3fd62ea1a | 5164 | #else |
NYX | 0:85b3fd62ea1a | 5165 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) |
NYX | 0:85b3fd62ea1a | 5166 | #endif |
NYX | 0:85b3fd62ea1a | 5167 | { |
NYX | 0:85b3fd62ea1a | 5168 | if (hcryp->CrypInCount == hcryp->Init.HeaderSize) |
NYX | 0:85b3fd62ea1a | 5169 | { |
NYX | 0:85b3fd62ea1a | 5170 | /* All B blocks will have been entered after the next |
NYX | 0:85b3fd62ea1a | 5171 | four DINR writing, so point at header buffer for |
NYX | 0:85b3fd62ea1a | 5172 | the next iteration */ |
NYX | 0:85b3fd62ea1a | 5173 | hcryp->pCrypInBuffPtr = hcryp->Init.Header; |
NYX | 0:85b3fd62ea1a | 5174 | } |
NYX | 0:85b3fd62ea1a | 5175 | } |
NYX | 0:85b3fd62ea1a | 5176 | |
NYX | 0:85b3fd62ea1a | 5177 | /* Write the Input block in the Data Input register */ |
NYX | 0:85b3fd62ea1a | 5178 | if (addhoc_process == 0U) |
NYX | 0:85b3fd62ea1a | 5179 | { |
NYX | 0:85b3fd62ea1a | 5180 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 5181 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5182 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 5183 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5184 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 5185 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5186 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 5187 | } |
NYX | 0:85b3fd62ea1a | 5188 | else |
NYX | 0:85b3fd62ea1a | 5189 | { |
NYX | 0:85b3fd62ea1a | 5190 | /* Header remainder has size less than 128 bits */ |
NYX | 0:85b3fd62ea1a | 5191 | /* Enter complete words when possible */ |
NYX | 0:85b3fd62ea1a | 5192 | for(index=0U; index < (difflength/4U); index ++) |
NYX | 0:85b3fd62ea1a | 5193 | { |
NYX | 0:85b3fd62ea1a | 5194 | /* Write the Input block in the Data Input register */ |
NYX | 0:85b3fd62ea1a | 5195 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 5196 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5197 | } |
NYX | 0:85b3fd62ea1a | 5198 | /* Enter incomplete word padded with zeroes if applicable |
NYX | 0:85b3fd62ea1a | 5199 | (case of header length not a multiple of 32-bits) */ |
NYX | 0:85b3fd62ea1a | 5200 | if (difflengthmod4 != 0U) |
NYX | 0:85b3fd62ea1a | 5201 | { |
NYX | 0:85b3fd62ea1a | 5202 | hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[difflengthmod4-1]); |
NYX | 0:85b3fd62ea1a | 5203 | } |
NYX | 0:85b3fd62ea1a | 5204 | /* Pad with zero-words to reach 128-bit long block and wrap-up header feeding to the IP */ |
NYX | 0:85b3fd62ea1a | 5205 | for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++) |
NYX | 0:85b3fd62ea1a | 5206 | { |
NYX | 0:85b3fd62ea1a | 5207 | hcryp->Instance->DINR = 0U; |
NYX | 0:85b3fd62ea1a | 5208 | } |
NYX | 0:85b3fd62ea1a | 5209 | } |
NYX | 0:85b3fd62ea1a | 5210 | |
NYX | 0:85b3fd62ea1a | 5211 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 5212 | } |
NYX | 0:85b3fd62ea1a | 5213 | } |
NYX | 0:85b3fd62ea1a | 5214 | /*=======================*/ |
NYX | 0:85b3fd62ea1a | 5215 | /* GCM/CCM payload phase */ |
NYX | 0:85b3fd62ea1a | 5216 | /*=======================*/ |
NYX | 0:85b3fd62ea1a | 5217 | else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE) |
NYX | 0:85b3fd62ea1a | 5218 | { |
NYX | 0:85b3fd62ea1a | 5219 | /* Get the last output data address */ |
NYX | 0:85b3fd62ea1a | 5220 | outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; |
NYX | 0:85b3fd62ea1a | 5221 | |
NYX | 0:85b3fd62ea1a | 5222 | /* Specific handling to manage payload size less than 128 bits |
NYX | 0:85b3fd62ea1a | 5223 | when GCM (or CCM when applicable) encryption or decryption is selected. |
NYX | 0:85b3fd62ea1a | 5224 | Check here if the last block output data are read */ |
NYX | 0:85b3fd62ea1a | 5225 | #if defined(AES_CR_NPBLB) |
NYX | 0:85b3fd62ea1a | 5226 | if ((hcryp->CrypOutCount < 16U) && \ |
NYX | 0:85b3fd62ea1a | 5227 | (hcryp->CrypOutCount > 0U)) |
NYX | 0:85b3fd62ea1a | 5228 | #else |
NYX | 0:85b3fd62ea1a | 5229 | if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) && \ |
NYX | 0:85b3fd62ea1a | 5230 | (hcryp->CrypOutCount < 16U) && \ |
NYX | 0:85b3fd62ea1a | 5231 | (hcryp->CrypOutCount > 0U)) |
NYX | 0:85b3fd62ea1a | 5232 | #endif |
NYX | 0:85b3fd62ea1a | 5233 | { |
NYX | 0:85b3fd62ea1a | 5234 | addhoc_process = 1U; |
NYX | 0:85b3fd62ea1a | 5235 | difflength = hcryp->CrypOutCount; |
NYX | 0:85b3fd62ea1a | 5236 | difflengthmod4 = difflength%4U; |
NYX | 0:85b3fd62ea1a | 5237 | hcryp->CrypOutCount = 0U; /* mark that no more output data will be needed */ |
NYX | 0:85b3fd62ea1a | 5238 | /* Retrieve intermediate data */ |
NYX | 0:85b3fd62ea1a | 5239 | for(index=0U; index < 4U; index ++) |
NYX | 0:85b3fd62ea1a | 5240 | { |
NYX | 0:85b3fd62ea1a | 5241 | intermediate_data[index] = hcryp->Instance->DOUTR; |
NYX | 0:85b3fd62ea1a | 5242 | } |
NYX | 0:85b3fd62ea1a | 5243 | /* Retrieve last words of cyphered data */ |
NYX | 0:85b3fd62ea1a | 5244 | /* First, retrieve complete output words */ |
NYX | 0:85b3fd62ea1a | 5245 | for(index=0U; index < (difflength/4U); index ++) |
NYX | 0:85b3fd62ea1a | 5246 | { |
NYX | 0:85b3fd62ea1a | 5247 | *(uint32_t*)(outputaddr) = intermediate_data[index]; |
NYX | 0:85b3fd62ea1a | 5248 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5249 | } |
NYX | 0:85b3fd62ea1a | 5250 | /* Next, retrieve partial output word if applicable; |
NYX | 0:85b3fd62ea1a | 5251 | at the same time, start masking intermediate data |
NYX | 0:85b3fd62ea1a | 5252 | with a mask of zeros of same size than the padding |
NYX | 0:85b3fd62ea1a | 5253 | applied to the last block of payload */ |
NYX | 0:85b3fd62ea1a | 5254 | if (difflengthmod4 != 0U) |
NYX | 0:85b3fd62ea1a | 5255 | { |
NYX | 0:85b3fd62ea1a | 5256 | intermediate_data[difflength/4U] &= mask[difflengthmod4-1U]; |
NYX | 0:85b3fd62ea1a | 5257 | *(uint32_t*)(outputaddr) = intermediate_data[difflength/4U]; |
NYX | 0:85b3fd62ea1a | 5258 | } |
NYX | 0:85b3fd62ea1a | 5259 | |
NYX | 0:85b3fd62ea1a | 5260 | #if !defined(AES_CR_NPBLB) |
NYX | 0:85b3fd62ea1a | 5261 | if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT) |
NYX | 0:85b3fd62ea1a | 5262 | { |
NYX | 0:85b3fd62ea1a | 5263 | /* Change again CHMOD configuration to GCM mode */ |
NYX | 0:85b3fd62ea1a | 5264 | __HAL_CRYP_SET_CHAININGMODE(CRYP_CHAINMODE_AES_GCM_GMAC); |
NYX | 0:85b3fd62ea1a | 5265 | |
NYX | 0:85b3fd62ea1a | 5266 | /* Select FINAL phase */ |
NYX | 0:85b3fd62ea1a | 5267 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_GCMCMAC_FINAL_PHASE); |
NYX | 0:85b3fd62ea1a | 5268 | |
NYX | 0:85b3fd62ea1a | 5269 | /* Before inserting the intermediate data, carry on masking operation |
NYX | 0:85b3fd62ea1a | 5270 | with a mask of zeros of same size than the padding applied to the last block of payload */ |
NYX | 0:85b3fd62ea1a | 5271 | for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++) |
NYX | 0:85b3fd62ea1a | 5272 | { |
NYX | 0:85b3fd62ea1a | 5273 | intermediate_data[(difflength+3U)/4U+index] = 0U; |
NYX | 0:85b3fd62ea1a | 5274 | } |
NYX | 0:85b3fd62ea1a | 5275 | |
NYX | 0:85b3fd62ea1a | 5276 | /* Insert intermediate data to trigger an additional DOUTR reading round */ |
NYX | 0:85b3fd62ea1a | 5277 | /* Clear Computation Complete Flag before entering new block */ |
NYX | 0:85b3fd62ea1a | 5278 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
NYX | 0:85b3fd62ea1a | 5279 | for(index=0U; index < 4U; index ++) |
NYX | 0:85b3fd62ea1a | 5280 | { |
NYX | 0:85b3fd62ea1a | 5281 | hcryp->Instance->DINR = intermediate_data[index]; |
NYX | 0:85b3fd62ea1a | 5282 | } |
NYX | 0:85b3fd62ea1a | 5283 | } |
NYX | 0:85b3fd62ea1a | 5284 | else |
NYX | 0:85b3fd62ea1a | 5285 | #endif |
NYX | 0:85b3fd62ea1a | 5286 | { |
NYX | 0:85b3fd62ea1a | 5287 | /* Payload phase is now over */ |
NYX | 0:85b3fd62ea1a | 5288 | /* Clear Computation Complete Flag */ |
NYX | 0:85b3fd62ea1a | 5289 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
NYX | 0:85b3fd62ea1a | 5290 | /* Disable Computation Complete Flag and Errors Interrupts */ |
NYX | 0:85b3fd62ea1a | 5291 | __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); |
NYX | 0:85b3fd62ea1a | 5292 | /* Change the CRYP state */ |
NYX | 0:85b3fd62ea1a | 5293 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 5294 | /* Mark that the payload phase is over */ |
NYX | 0:85b3fd62ea1a | 5295 | hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER; |
NYX | 0:85b3fd62ea1a | 5296 | |
NYX | 0:85b3fd62ea1a | 5297 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 5298 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 5299 | |
NYX | 0:85b3fd62ea1a | 5300 | /* Call computation complete callback */ |
NYX | 0:85b3fd62ea1a | 5301 | HAL_CRYPEx_ComputationCpltCallback(hcryp); |
NYX | 0:85b3fd62ea1a | 5302 | } |
NYX | 0:85b3fd62ea1a | 5303 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 5304 | } |
NYX | 0:85b3fd62ea1a | 5305 | else |
NYX | 0:85b3fd62ea1a | 5306 | { |
NYX | 0:85b3fd62ea1a | 5307 | if (hcryp->CrypOutCount != 0U) |
NYX | 0:85b3fd62ea1a | 5308 | { |
NYX | 0:85b3fd62ea1a | 5309 | /* Usual case (different than GCM/CCM last block < 128 bits ciphering) */ |
NYX | 0:85b3fd62ea1a | 5310 | /* Retrieve the last block available from the CRYP hardware block: |
NYX | 0:85b3fd62ea1a | 5311 | read the output block from the Data Output Register */ |
NYX | 0:85b3fd62ea1a | 5312 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
NYX | 0:85b3fd62ea1a | 5313 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5314 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
NYX | 0:85b3fd62ea1a | 5315 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5316 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
NYX | 0:85b3fd62ea1a | 5317 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5318 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
NYX | 0:85b3fd62ea1a | 5319 | |
NYX | 0:85b3fd62ea1a | 5320 | /* Increment/decrement instance pointer/counter */ |
NYX | 0:85b3fd62ea1a | 5321 | hcryp->pCrypOutBuffPtr += 16U; |
NYX | 0:85b3fd62ea1a | 5322 | hcryp->CrypOutCount -= 16U; |
NYX | 0:85b3fd62ea1a | 5323 | } |
NYX | 0:85b3fd62ea1a | 5324 | #if !defined(AES_CR_NPBLB) |
NYX | 0:85b3fd62ea1a | 5325 | else |
NYX | 0:85b3fd62ea1a | 5326 | { |
NYX | 0:85b3fd62ea1a | 5327 | /* Software work-around: additional DOUTR reading round to discard the data */ |
NYX | 0:85b3fd62ea1a | 5328 | for(index=0U; index < 4U; index ++) |
NYX | 0:85b3fd62ea1a | 5329 | { |
NYX | 0:85b3fd62ea1a | 5330 | intermediate_data[index] = hcryp->Instance->DOUTR; |
NYX | 0:85b3fd62ea1a | 5331 | } |
NYX | 0:85b3fd62ea1a | 5332 | } |
NYX | 0:85b3fd62ea1a | 5333 | #endif |
NYX | 0:85b3fd62ea1a | 5334 | } |
NYX | 0:85b3fd62ea1a | 5335 | |
NYX | 0:85b3fd62ea1a | 5336 | /* Check if all output text has been retrieved */ |
NYX | 0:85b3fd62ea1a | 5337 | if (hcryp->CrypOutCount == 0U) |
NYX | 0:85b3fd62ea1a | 5338 | { |
NYX | 0:85b3fd62ea1a | 5339 | #if !defined(AES_CR_NPBLB) |
NYX | 0:85b3fd62ea1a | 5340 | /* Make sure that software-work around is not running before disabling |
NYX | 0:85b3fd62ea1a | 5341 | the interruptions (indeed, if software work-around is running, the |
NYX | 0:85b3fd62ea1a | 5342 | interruptions must not be disabled to allow the additional DOUTR |
NYX | 0:85b3fd62ea1a | 5343 | reading round */ |
NYX | 0:85b3fd62ea1a | 5344 | if (addhoc_process == 0U) |
NYX | 0:85b3fd62ea1a | 5345 | #endif |
NYX | 0:85b3fd62ea1a | 5346 | { |
NYX | 0:85b3fd62ea1a | 5347 | /* Clear Computation Complete Flag */ |
NYX | 0:85b3fd62ea1a | 5348 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
NYX | 0:85b3fd62ea1a | 5349 | /* Disable Computation Complete Flag and Errors Interrupts */ |
NYX | 0:85b3fd62ea1a | 5350 | __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); |
NYX | 0:85b3fd62ea1a | 5351 | /* Change the CRYP state */ |
NYX | 0:85b3fd62ea1a | 5352 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 5353 | /* Mark that the payload phase is over */ |
NYX | 0:85b3fd62ea1a | 5354 | hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER; |
NYX | 0:85b3fd62ea1a | 5355 | |
NYX | 0:85b3fd62ea1a | 5356 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 5357 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 5358 | |
NYX | 0:85b3fd62ea1a | 5359 | /* Call computation complete callback */ |
NYX | 0:85b3fd62ea1a | 5360 | HAL_CRYPEx_ComputationCpltCallback(hcryp); |
NYX | 0:85b3fd62ea1a | 5361 | } |
NYX | 0:85b3fd62ea1a | 5362 | |
NYX | 0:85b3fd62ea1a | 5363 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 5364 | } |
NYX | 0:85b3fd62ea1a | 5365 | /* If suspension flag has been raised, suspend processing */ |
NYX | 0:85b3fd62ea1a | 5366 | else if (hcryp->SuspendRequest == HAL_CRYP_SUSPEND) |
NYX | 0:85b3fd62ea1a | 5367 | { |
NYX | 0:85b3fd62ea1a | 5368 | /* Clear CCF Flag */ |
NYX | 0:85b3fd62ea1a | 5369 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
NYX | 0:85b3fd62ea1a | 5370 | |
NYX | 0:85b3fd62ea1a | 5371 | /* reset SuspendRequest */ |
NYX | 0:85b3fd62ea1a | 5372 | hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE; |
NYX | 0:85b3fd62ea1a | 5373 | /* Disable Computation Complete Flag and Errors Interrupts */ |
NYX | 0:85b3fd62ea1a | 5374 | __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); |
NYX | 0:85b3fd62ea1a | 5375 | /* Change the CRYP state */ |
NYX | 0:85b3fd62ea1a | 5376 | hcryp->State = HAL_CRYP_STATE_SUSPENDED; |
NYX | 0:85b3fd62ea1a | 5377 | /* Mark that the header phase is over */ |
NYX | 0:85b3fd62ea1a | 5378 | hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED; |
NYX | 0:85b3fd62ea1a | 5379 | |
NYX | 0:85b3fd62ea1a | 5380 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 5381 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 5382 | |
NYX | 0:85b3fd62ea1a | 5383 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 5384 | } |
NYX | 0:85b3fd62ea1a | 5385 | else /* Output data are still expected, carry on feeding the CRYP |
NYX | 0:85b3fd62ea1a | 5386 | hardware block with input data */ |
NYX | 0:85b3fd62ea1a | 5387 | { |
NYX | 0:85b3fd62ea1a | 5388 | /* Clear Computation Complete Flag */ |
NYX | 0:85b3fd62ea1a | 5389 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
NYX | 0:85b3fd62ea1a | 5390 | /* Get the last Input data address */ |
NYX | 0:85b3fd62ea1a | 5391 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
NYX | 0:85b3fd62ea1a | 5392 | |
NYX | 0:85b3fd62ea1a | 5393 | /* Usual input data feeding case */ |
NYX | 0:85b3fd62ea1a | 5394 | if (hcryp->CrypInCount < 16U) |
NYX | 0:85b3fd62ea1a | 5395 | { |
NYX | 0:85b3fd62ea1a | 5396 | difflength = (uint32_t) (hcryp->CrypInCount); |
NYX | 0:85b3fd62ea1a | 5397 | difflengthmod4 = difflength%4U; |
NYX | 0:85b3fd62ea1a | 5398 | hcryp->CrypInCount = 0U; |
NYX | 0:85b3fd62ea1a | 5399 | |
NYX | 0:85b3fd62ea1a | 5400 | #if defined(AES_CR_NPBLB) |
NYX | 0:85b3fd62ea1a | 5401 | /* In case of GCM encryption or CCM decryption, specify the number of padding |
NYX | 0:85b3fd62ea1a | 5402 | bytes in last block of payload */ |
NYX | 0:85b3fd62ea1a | 5403 | if (((READ_BIT(hcryp->Instance->CR, AES_CR_CHMOD) == CRYP_CHAINMODE_AES_GCM_GMAC) |
NYX | 0:85b3fd62ea1a | 5404 | && (READ_BIT(hcryp->Instance->CR, AES_CR_MODE) == CRYP_ALGOMODE_ENCRYPT)) |
NYX | 0:85b3fd62ea1a | 5405 | || ((READ_BIT(hcryp->Instance->CR, AES_CR_CHMOD) == CRYP_CHAINMODE_AES_CCM_CMAC) |
NYX | 0:85b3fd62ea1a | 5406 | && (READ_BIT(hcryp->Instance->CR, AES_CR_MODE) == CRYP_ALGOMODE_DECRYPT))) |
NYX | 0:85b3fd62ea1a | 5407 | { |
NYX | 0:85b3fd62ea1a | 5408 | /* Set NPBLB field in writing the number of padding bytes |
NYX | 0:85b3fd62ea1a | 5409 | for the last block of payload */ |
NYX | 0:85b3fd62ea1a | 5410 | MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, 16U - difflength); |
NYX | 0:85b3fd62ea1a | 5411 | } |
NYX | 0:85b3fd62ea1a | 5412 | #else |
NYX | 0:85b3fd62ea1a | 5413 | /* Software workaround applied to GCM encryption only */ |
NYX | 0:85b3fd62ea1a | 5414 | if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT) |
NYX | 0:85b3fd62ea1a | 5415 | { |
NYX | 0:85b3fd62ea1a | 5416 | /* Change the mode configured in CHMOD bits of CR register to select CTR mode */ |
NYX | 0:85b3fd62ea1a | 5417 | __HAL_CRYP_SET_CHAININGMODE(CRYP_CHAINMODE_AES_CTR); |
NYX | 0:85b3fd62ea1a | 5418 | } |
NYX | 0:85b3fd62ea1a | 5419 | #endif |
NYX | 0:85b3fd62ea1a | 5420 | |
NYX | 0:85b3fd62ea1a | 5421 | /* Insert the last block (which size is inferior to 128 bits) padded with zeroes |
NYX | 0:85b3fd62ea1a | 5422 | to have a complete block of 128 bits */ |
NYX | 0:85b3fd62ea1a | 5423 | for(index=0U; index < (difflength/4U); index ++) |
NYX | 0:85b3fd62ea1a | 5424 | { |
NYX | 0:85b3fd62ea1a | 5425 | /* Write the Input block in the Data Input register */ |
NYX | 0:85b3fd62ea1a | 5426 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 5427 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5428 | } |
NYX | 0:85b3fd62ea1a | 5429 | /* If required, manage input data size not multiple of 32 bits */ |
NYX | 0:85b3fd62ea1a | 5430 | if (difflengthmod4 != 0U) |
NYX | 0:85b3fd62ea1a | 5431 | { |
NYX | 0:85b3fd62ea1a | 5432 | hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[difflengthmod4-1U]); |
NYX | 0:85b3fd62ea1a | 5433 | } |
NYX | 0:85b3fd62ea1a | 5434 | /* Wrap-up in padding with zero-words if applicable */ |
NYX | 0:85b3fd62ea1a | 5435 | for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++) |
NYX | 0:85b3fd62ea1a | 5436 | { |
NYX | 0:85b3fd62ea1a | 5437 | hcryp->Instance->DINR = 0U; |
NYX | 0:85b3fd62ea1a | 5438 | } |
NYX | 0:85b3fd62ea1a | 5439 | } |
NYX | 0:85b3fd62ea1a | 5440 | else |
NYX | 0:85b3fd62ea1a | 5441 | { |
NYX | 0:85b3fd62ea1a | 5442 | hcryp->pCrypInBuffPtr += 16U; |
NYX | 0:85b3fd62ea1a | 5443 | hcryp->CrypInCount -= 16U; |
NYX | 0:85b3fd62ea1a | 5444 | |
NYX | 0:85b3fd62ea1a | 5445 | /* Write the Input block in the Data Input register */ |
NYX | 0:85b3fd62ea1a | 5446 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 5447 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5448 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 5449 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5450 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 5451 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5452 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 5453 | } |
NYX | 0:85b3fd62ea1a | 5454 | |
NYX | 0:85b3fd62ea1a | 5455 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 5456 | } |
NYX | 0:85b3fd62ea1a | 5457 | } |
NYX | 0:85b3fd62ea1a | 5458 | /*====================================*/ |
NYX | 0:85b3fd62ea1a | 5459 | /* GCM/GMAC or (CCM/)CMAC final phase */ |
NYX | 0:85b3fd62ea1a | 5460 | /*====================================*/ |
NYX | 0:85b3fd62ea1a | 5461 | else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE) |
NYX | 0:85b3fd62ea1a | 5462 | { |
NYX | 0:85b3fd62ea1a | 5463 | /* Clear Computation Complete Flag */ |
NYX | 0:85b3fd62ea1a | 5464 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
NYX | 0:85b3fd62ea1a | 5465 | |
NYX | 0:85b3fd62ea1a | 5466 | /* Get the last output data address */ |
NYX | 0:85b3fd62ea1a | 5467 | outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; |
NYX | 0:85b3fd62ea1a | 5468 | |
NYX | 0:85b3fd62ea1a | 5469 | /* Retrieve the last expected data from the CRYP hardware block: |
NYX | 0:85b3fd62ea1a | 5470 | read the output block from the Data Output Register */ |
NYX | 0:85b3fd62ea1a | 5471 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
NYX | 0:85b3fd62ea1a | 5472 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5473 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
NYX | 0:85b3fd62ea1a | 5474 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5475 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
NYX | 0:85b3fd62ea1a | 5476 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5477 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
NYX | 0:85b3fd62ea1a | 5478 | |
NYX | 0:85b3fd62ea1a | 5479 | /* Disable Computation Complete Flag and Errors Interrupts */ |
NYX | 0:85b3fd62ea1a | 5480 | __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); |
NYX | 0:85b3fd62ea1a | 5481 | /* Change the CRYP state */ |
NYX | 0:85b3fd62ea1a | 5482 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 5483 | /* Mark that the header phase is over */ |
NYX | 0:85b3fd62ea1a | 5484 | hcryp->Phase = HAL_CRYP_PHASE_FINAL_OVER; |
NYX | 0:85b3fd62ea1a | 5485 | |
NYX | 0:85b3fd62ea1a | 5486 | /* Disable the Peripheral */ |
NYX | 0:85b3fd62ea1a | 5487 | __HAL_CRYP_DISABLE(); |
NYX | 0:85b3fd62ea1a | 5488 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 5489 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 5490 | |
NYX | 0:85b3fd62ea1a | 5491 | /* Call computation complete callback */ |
NYX | 0:85b3fd62ea1a | 5492 | HAL_CRYPEx_ComputationCpltCallback(hcryp); |
NYX | 0:85b3fd62ea1a | 5493 | |
NYX | 0:85b3fd62ea1a | 5494 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 5495 | } |
NYX | 0:85b3fd62ea1a | 5496 | else |
NYX | 0:85b3fd62ea1a | 5497 | { |
NYX | 0:85b3fd62ea1a | 5498 | /* Clear Computation Complete Flag */ |
NYX | 0:85b3fd62ea1a | 5499 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
NYX | 0:85b3fd62ea1a | 5500 | hcryp->State = HAL_CRYP_STATE_ERROR; |
NYX | 0:85b3fd62ea1a | 5501 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 5502 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 5503 | } |
NYX | 0:85b3fd62ea1a | 5504 | } |
NYX | 0:85b3fd62ea1a | 5505 | else |
NYX | 0:85b3fd62ea1a | 5506 | { |
NYX | 0:85b3fd62ea1a | 5507 | return HAL_BUSY; |
NYX | 0:85b3fd62ea1a | 5508 | } |
NYX | 0:85b3fd62ea1a | 5509 | } |
NYX | 0:85b3fd62ea1a | 5510 | |
NYX | 0:85b3fd62ea1a | 5511 | /** |
NYX | 0:85b3fd62ea1a | 5512 | * @brief Set the DMA configuration and start the DMA transfer |
NYX | 0:85b3fd62ea1a | 5513 | * for GCM, GMAC or CMAC chainging modes. |
NYX | 0:85b3fd62ea1a | 5514 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 5515 | * the configuration information for CRYP module. |
NYX | 0:85b3fd62ea1a | 5516 | * @param inputaddr: Address of the Input buffer. |
NYX | 0:85b3fd62ea1a | 5517 | * @param Size: Size of the Input buffer un bytes, must be a multiple of 16. |
NYX | 0:85b3fd62ea1a | 5518 | * @param outputaddr: Address of the Output buffer, null pointer when no output DMA stream |
NYX | 0:85b3fd62ea1a | 5519 | * has to be configured. |
NYX | 0:85b3fd62ea1a | 5520 | * @retval None |
NYX | 0:85b3fd62ea1a | 5521 | */ |
NYX | 0:85b3fd62ea1a | 5522 | static void CRYP_GCMCMAC_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr) |
NYX | 0:85b3fd62ea1a | 5523 | { |
NYX | 0:85b3fd62ea1a | 5524 | |
NYX | 0:85b3fd62ea1a | 5525 | /* Set the input CRYP DMA transfer complete callback */ |
NYX | 0:85b3fd62ea1a | 5526 | hcryp->hdmain->XferCpltCallback = CRYP_GCMCMAC_DMAInCplt; |
NYX | 0:85b3fd62ea1a | 5527 | /* Set the DMA error callback */ |
NYX | 0:85b3fd62ea1a | 5528 | hcryp->hdmain->XferErrorCallback = CRYP_GCMCMAC_DMAError; |
NYX | 0:85b3fd62ea1a | 5529 | |
NYX | 0:85b3fd62ea1a | 5530 | if (outputaddr != 0U) |
NYX | 0:85b3fd62ea1a | 5531 | { |
NYX | 0:85b3fd62ea1a | 5532 | /* Set the output CRYP DMA transfer complete callback */ |
NYX | 0:85b3fd62ea1a | 5533 | hcryp->hdmaout->XferCpltCallback = CRYP_GCMCMAC_DMAOutCplt; |
NYX | 0:85b3fd62ea1a | 5534 | /* Set the DMA error callback */ |
NYX | 0:85b3fd62ea1a | 5535 | hcryp->hdmaout->XferErrorCallback = CRYP_GCMCMAC_DMAError; |
NYX | 0:85b3fd62ea1a | 5536 | } |
NYX | 0:85b3fd62ea1a | 5537 | |
NYX | 0:85b3fd62ea1a | 5538 | /* Enable the CRYP peripheral */ |
NYX | 0:85b3fd62ea1a | 5539 | __HAL_CRYP_ENABLE(); |
NYX | 0:85b3fd62ea1a | 5540 | |
NYX | 0:85b3fd62ea1a | 5541 | /* Enable the DMA input stream */ |
NYX | 0:85b3fd62ea1a | 5542 | HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DINR, Size/4U); |
NYX | 0:85b3fd62ea1a | 5543 | |
NYX | 0:85b3fd62ea1a | 5544 | /* Enable the DMA input request */ |
NYX | 0:85b3fd62ea1a | 5545 | SET_BIT(hcryp->Instance->CR, AES_CR_DMAINEN); |
NYX | 0:85b3fd62ea1a | 5546 | |
NYX | 0:85b3fd62ea1a | 5547 | |
NYX | 0:85b3fd62ea1a | 5548 | if (outputaddr != 0U) |
NYX | 0:85b3fd62ea1a | 5549 | { |
NYX | 0:85b3fd62ea1a | 5550 | /* Enable the DMA output stream */ |
NYX | 0:85b3fd62ea1a | 5551 | HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUTR, outputaddr, Size/4U); |
NYX | 0:85b3fd62ea1a | 5552 | |
NYX | 0:85b3fd62ea1a | 5553 | /* Enable the DMA output request */ |
NYX | 0:85b3fd62ea1a | 5554 | SET_BIT(hcryp->Instance->CR, AES_CR_DMAOUTEN); |
NYX | 0:85b3fd62ea1a | 5555 | } |
NYX | 0:85b3fd62ea1a | 5556 | } |
NYX | 0:85b3fd62ea1a | 5557 | |
NYX | 0:85b3fd62ea1a | 5558 | /** |
NYX | 0:85b3fd62ea1a | 5559 | * @brief Write/read input/output data in polling mode. |
NYX | 0:85b3fd62ea1a | 5560 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 5561 | * the configuration information for CRYP module. |
NYX | 0:85b3fd62ea1a | 5562 | * @param Input: Pointer to the Input buffer. |
NYX | 0:85b3fd62ea1a | 5563 | * @param Ilength: Length of the Input buffer in bytes, must be a multiple of 16. |
NYX | 0:85b3fd62ea1a | 5564 | * @param Output: Pointer to the returned buffer. |
NYX | 0:85b3fd62ea1a | 5565 | * @param Timeout: Specify Timeout value. |
NYX | 0:85b3fd62ea1a | 5566 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 5567 | */ |
NYX | 0:85b3fd62ea1a | 5568 | static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout) |
NYX | 0:85b3fd62ea1a | 5569 | { |
NYX | 0:85b3fd62ea1a | 5570 | uint32_t index = 0U; |
NYX | 0:85b3fd62ea1a | 5571 | uint32_t inputaddr = (uint32_t)Input; |
NYX | 0:85b3fd62ea1a | 5572 | uint32_t outputaddr = (uint32_t)Output; |
NYX | 0:85b3fd62ea1a | 5573 | |
NYX | 0:85b3fd62ea1a | 5574 | |
NYX | 0:85b3fd62ea1a | 5575 | for(index=0U; (index < Ilength); index += 16U) |
NYX | 0:85b3fd62ea1a | 5576 | { |
NYX | 0:85b3fd62ea1a | 5577 | /* Write the Input block in the Data Input register */ |
NYX | 0:85b3fd62ea1a | 5578 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 5579 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5580 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 5581 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5582 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 5583 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5584 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 5585 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5586 | |
NYX | 0:85b3fd62ea1a | 5587 | /* Wait for CCF flag to be raised */ |
NYX | 0:85b3fd62ea1a | 5588 | if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) |
NYX | 0:85b3fd62ea1a | 5589 | { |
NYX | 0:85b3fd62ea1a | 5590 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 5591 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 5592 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 5593 | } |
NYX | 0:85b3fd62ea1a | 5594 | |
NYX | 0:85b3fd62ea1a | 5595 | /* Clear CCF Flag */ |
NYX | 0:85b3fd62ea1a | 5596 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
NYX | 0:85b3fd62ea1a | 5597 | |
NYX | 0:85b3fd62ea1a | 5598 | /* Read the Output block from the Data Output Register */ |
NYX | 0:85b3fd62ea1a | 5599 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
NYX | 0:85b3fd62ea1a | 5600 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5601 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
NYX | 0:85b3fd62ea1a | 5602 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5603 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
NYX | 0:85b3fd62ea1a | 5604 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5605 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
NYX | 0:85b3fd62ea1a | 5606 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5607 | |
NYX | 0:85b3fd62ea1a | 5608 | /* If the suspension flag has been raised and if the processing is not about |
NYX | 0:85b3fd62ea1a | 5609 | to end, suspend processing */ |
NYX | 0:85b3fd62ea1a | 5610 | if ((hcryp->SuspendRequest == HAL_CRYP_SUSPEND) && ((index+16U) < Ilength)) |
NYX | 0:85b3fd62ea1a | 5611 | { |
NYX | 0:85b3fd62ea1a | 5612 | /* Reset SuspendRequest */ |
NYX | 0:85b3fd62ea1a | 5613 | hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE; |
NYX | 0:85b3fd62ea1a | 5614 | |
NYX | 0:85b3fd62ea1a | 5615 | /* Save current reading and writing locations of Input and Output buffers */ |
NYX | 0:85b3fd62ea1a | 5616 | hcryp->pCrypOutBuffPtr = (uint8_t *)outputaddr; |
NYX | 0:85b3fd62ea1a | 5617 | hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr; |
NYX | 0:85b3fd62ea1a | 5618 | /* Save the number of bytes that remain to be processed at this point */ |
NYX | 0:85b3fd62ea1a | 5619 | hcryp->CrypInCount = Ilength - (index+16U); |
NYX | 0:85b3fd62ea1a | 5620 | |
NYX | 0:85b3fd62ea1a | 5621 | /* Change the CRYP state */ |
NYX | 0:85b3fd62ea1a | 5622 | hcryp->State = HAL_CRYP_STATE_SUSPENDED; |
NYX | 0:85b3fd62ea1a | 5623 | |
NYX | 0:85b3fd62ea1a | 5624 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 5625 | } |
NYX | 0:85b3fd62ea1a | 5626 | |
NYX | 0:85b3fd62ea1a | 5627 | } |
NYX | 0:85b3fd62ea1a | 5628 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 5629 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 5630 | |
NYX | 0:85b3fd62ea1a | 5631 | } |
NYX | 0:85b3fd62ea1a | 5632 | |
NYX | 0:85b3fd62ea1a | 5633 | /** |
NYX | 0:85b3fd62ea1a | 5634 | * @brief Read derivative key in polling mode when CRYP hardware block is set |
NYX | 0:85b3fd62ea1a | 5635 | * in key derivation operating mode (mode 2). |
NYX | 0:85b3fd62ea1a | 5636 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 5637 | * the configuration information for CRYP module. |
NYX | 0:85b3fd62ea1a | 5638 | * @param Output: Pointer to the returned buffer. |
NYX | 0:85b3fd62ea1a | 5639 | * @param Timeout: Specify Timeout value. |
NYX | 0:85b3fd62ea1a | 5640 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 5641 | */ |
NYX | 0:85b3fd62ea1a | 5642 | static HAL_StatusTypeDef CRYP_ReadKey(CRYP_HandleTypeDef *hcryp, uint8_t* Output, uint32_t Timeout) |
NYX | 0:85b3fd62ea1a | 5643 | { |
NYX | 0:85b3fd62ea1a | 5644 | uint32_t outputaddr = (uint32_t)Output; |
NYX | 0:85b3fd62ea1a | 5645 | |
NYX | 0:85b3fd62ea1a | 5646 | /* Wait for CCF flag to be raised */ |
NYX | 0:85b3fd62ea1a | 5647 | if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) |
NYX | 0:85b3fd62ea1a | 5648 | { |
NYX | 0:85b3fd62ea1a | 5649 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 5650 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 5651 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 5652 | } |
NYX | 0:85b3fd62ea1a | 5653 | /* Clear CCF Flag */ |
NYX | 0:85b3fd62ea1a | 5654 | __HAL_CRYP_CLEAR_FLAG( CRYP_CCF_CLEAR); |
NYX | 0:85b3fd62ea1a | 5655 | |
NYX | 0:85b3fd62ea1a | 5656 | /* Read the derivative key from the AES_KEYRx registers */ |
NYX | 0:85b3fd62ea1a | 5657 | if (hcryp->Init.KeySize == CRYP_KEYSIZE_256B) |
NYX | 0:85b3fd62ea1a | 5658 | { |
NYX | 0:85b3fd62ea1a | 5659 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR7); |
NYX | 0:85b3fd62ea1a | 5660 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5661 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR6); |
NYX | 0:85b3fd62ea1a | 5662 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5663 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR5); |
NYX | 0:85b3fd62ea1a | 5664 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5665 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR4); |
NYX | 0:85b3fd62ea1a | 5666 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5667 | } |
NYX | 0:85b3fd62ea1a | 5668 | |
NYX | 0:85b3fd62ea1a | 5669 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR3); |
NYX | 0:85b3fd62ea1a | 5670 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5671 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR2); |
NYX | 0:85b3fd62ea1a | 5672 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5673 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR1); |
NYX | 0:85b3fd62ea1a | 5674 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5675 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR0); |
NYX | 0:85b3fd62ea1a | 5676 | |
NYX | 0:85b3fd62ea1a | 5677 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 5678 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 5679 | } |
NYX | 0:85b3fd62ea1a | 5680 | |
NYX | 0:85b3fd62ea1a | 5681 | /** |
NYX | 0:85b3fd62ea1a | 5682 | * @brief Set the DMA configuration and start the DMA transfer. |
NYX | 0:85b3fd62ea1a | 5683 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 5684 | * the configuration information for CRYP module. |
NYX | 0:85b3fd62ea1a | 5685 | * @param inputaddr: Address of the Input buffer. |
NYX | 0:85b3fd62ea1a | 5686 | * @param Size: Size of the Input buffer in bytes, must be a multiple of 16. |
NYX | 0:85b3fd62ea1a | 5687 | * @param outputaddr: Address of the Output buffer. |
NYX | 0:85b3fd62ea1a | 5688 | * @retval None |
NYX | 0:85b3fd62ea1a | 5689 | */ |
NYX | 0:85b3fd62ea1a | 5690 | static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr) |
NYX | 0:85b3fd62ea1a | 5691 | { |
NYX | 0:85b3fd62ea1a | 5692 | /* Set the CRYP DMA transfer complete callback */ |
NYX | 0:85b3fd62ea1a | 5693 | hcryp->hdmain->XferCpltCallback = CRYP_DMAInCplt; |
NYX | 0:85b3fd62ea1a | 5694 | /* Set the DMA error callback */ |
NYX | 0:85b3fd62ea1a | 5695 | hcryp->hdmain->XferErrorCallback = CRYP_DMAError; |
NYX | 0:85b3fd62ea1a | 5696 | |
NYX | 0:85b3fd62ea1a | 5697 | /* Set the CRYP DMA transfer complete callback */ |
NYX | 0:85b3fd62ea1a | 5698 | hcryp->hdmaout->XferCpltCallback = CRYP_DMAOutCplt; |
NYX | 0:85b3fd62ea1a | 5699 | /* Set the DMA error callback */ |
NYX | 0:85b3fd62ea1a | 5700 | hcryp->hdmaout->XferErrorCallback = CRYP_DMAError; |
NYX | 0:85b3fd62ea1a | 5701 | |
NYX | 0:85b3fd62ea1a | 5702 | /* Enable the DMA input stream */ |
NYX | 0:85b3fd62ea1a | 5703 | HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DINR, Size/4U); |
NYX | 0:85b3fd62ea1a | 5704 | |
NYX | 0:85b3fd62ea1a | 5705 | /* Enable the DMA output stream */ |
NYX | 0:85b3fd62ea1a | 5706 | HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUTR, outputaddr, Size/4U); |
NYX | 0:85b3fd62ea1a | 5707 | |
NYX | 0:85b3fd62ea1a | 5708 | /* Enable In and Out DMA requests */ |
NYX | 0:85b3fd62ea1a | 5709 | SET_BIT(hcryp->Instance->CR, (AES_CR_DMAINEN | AES_CR_DMAOUTEN)); |
NYX | 0:85b3fd62ea1a | 5710 | |
NYX | 0:85b3fd62ea1a | 5711 | /* Enable the CRYP peripheral */ |
NYX | 0:85b3fd62ea1a | 5712 | __HAL_CRYP_ENABLE(); |
NYX | 0:85b3fd62ea1a | 5713 | } |
NYX | 0:85b3fd62ea1a | 5714 | |
NYX | 0:85b3fd62ea1a | 5715 | /** |
NYX | 0:85b3fd62ea1a | 5716 | * @brief Handle CRYP hardware block Timeout when waiting for CCF flag to be raised. |
NYX | 0:85b3fd62ea1a | 5717 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 5718 | * the configuration information for CRYP module. |
NYX | 0:85b3fd62ea1a | 5719 | * @param Timeout: Timeout duration. |
NYX | 0:85b3fd62ea1a | 5720 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 5721 | */ |
NYX | 0:85b3fd62ea1a | 5722 | static HAL_StatusTypeDef CRYP_WaitOnCCFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout) |
NYX | 0:85b3fd62ea1a | 5723 | { |
NYX | 0:85b3fd62ea1a | 5724 | uint32_t tickstart = 0U; |
NYX | 0:85b3fd62ea1a | 5725 | |
NYX | 0:85b3fd62ea1a | 5726 | /* Get timeout */ |
NYX | 0:85b3fd62ea1a | 5727 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 5728 | |
NYX | 0:85b3fd62ea1a | 5729 | while(HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF)) |
NYX | 0:85b3fd62ea1a | 5730 | { |
NYX | 0:85b3fd62ea1a | 5731 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 5732 | if(Timeout != HAL_MAX_DELAY) |
NYX | 0:85b3fd62ea1a | 5733 | { |
NYX | 0:85b3fd62ea1a | 5734 | if((HAL_GetTick() - tickstart ) > Timeout) |
NYX | 0:85b3fd62ea1a | 5735 | { |
NYX | 0:85b3fd62ea1a | 5736 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 5737 | } |
NYX | 0:85b3fd62ea1a | 5738 | } |
NYX | 0:85b3fd62ea1a | 5739 | } |
NYX | 0:85b3fd62ea1a | 5740 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 5741 | } |
NYX | 0:85b3fd62ea1a | 5742 | |
NYX | 0:85b3fd62ea1a | 5743 | /** |
NYX | 0:85b3fd62ea1a | 5744 | * @brief Wait for Busy Flag to be reset during a GCM payload encryption process suspension. |
NYX | 0:85b3fd62ea1a | 5745 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 5746 | * the configuration information for CRYP module. |
NYX | 0:85b3fd62ea1a | 5747 | * @param Timeout: Timeout duration. |
NYX | 0:85b3fd62ea1a | 5748 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 5749 | */ |
NYX | 0:85b3fd62ea1a | 5750 | static HAL_StatusTypeDef CRYP_WaitOnBusyFlagReset(CRYP_HandleTypeDef *hcryp, uint32_t Timeout) |
NYX | 0:85b3fd62ea1a | 5751 | { |
NYX | 0:85b3fd62ea1a | 5752 | uint32_t tickstart = 0U; |
NYX | 0:85b3fd62ea1a | 5753 | |
NYX | 0:85b3fd62ea1a | 5754 | /* Get timeout */ |
NYX | 0:85b3fd62ea1a | 5755 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 5756 | |
NYX | 0:85b3fd62ea1a | 5757 | while(HAL_IS_BIT_SET(hcryp->Instance->SR, AES_SR_BUSY)) |
NYX | 0:85b3fd62ea1a | 5758 | { |
NYX | 0:85b3fd62ea1a | 5759 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 5760 | if(Timeout != HAL_MAX_DELAY) |
NYX | 0:85b3fd62ea1a | 5761 | { |
NYX | 0:85b3fd62ea1a | 5762 | if((HAL_GetTick() - tickstart ) > Timeout) |
NYX | 0:85b3fd62ea1a | 5763 | { |
NYX | 0:85b3fd62ea1a | 5764 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 5765 | } |
NYX | 0:85b3fd62ea1a | 5766 | } |
NYX | 0:85b3fd62ea1a | 5767 | } |
NYX | 0:85b3fd62ea1a | 5768 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 5769 | } |
NYX | 0:85b3fd62ea1a | 5770 | |
NYX | 0:85b3fd62ea1a | 5771 | /** |
NYX | 0:85b3fd62ea1a | 5772 | * @brief DMA CRYP Input Data process complete callback. |
NYX | 0:85b3fd62ea1a | 5773 | * @param hdma: DMA handle. |
NYX | 0:85b3fd62ea1a | 5774 | * @retval None |
NYX | 0:85b3fd62ea1a | 5775 | */ |
NYX | 0:85b3fd62ea1a | 5776 | static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma) |
NYX | 0:85b3fd62ea1a | 5777 | { |
NYX | 0:85b3fd62ea1a | 5778 | CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
NYX | 0:85b3fd62ea1a | 5779 | |
NYX | 0:85b3fd62ea1a | 5780 | /* Disable the DMA transfer for input request */ |
NYX | 0:85b3fd62ea1a | 5781 | CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAINEN); |
NYX | 0:85b3fd62ea1a | 5782 | |
NYX | 0:85b3fd62ea1a | 5783 | /* Call input data transfer complete callback */ |
NYX | 0:85b3fd62ea1a | 5784 | HAL_CRYP_InCpltCallback(hcryp); |
NYX | 0:85b3fd62ea1a | 5785 | } |
NYX | 0:85b3fd62ea1a | 5786 | |
NYX | 0:85b3fd62ea1a | 5787 | /** |
NYX | 0:85b3fd62ea1a | 5788 | * @brief DMA CRYP Output Data process complete callback. |
NYX | 0:85b3fd62ea1a | 5789 | * @param hdma: DMA handle. |
NYX | 0:85b3fd62ea1a | 5790 | * @retval None |
NYX | 0:85b3fd62ea1a | 5791 | */ |
NYX | 0:85b3fd62ea1a | 5792 | static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma) |
NYX | 0:85b3fd62ea1a | 5793 | { |
NYX | 0:85b3fd62ea1a | 5794 | CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
NYX | 0:85b3fd62ea1a | 5795 | |
NYX | 0:85b3fd62ea1a | 5796 | /* Disable the DMA transfer for output request */ |
NYX | 0:85b3fd62ea1a | 5797 | CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAOUTEN); |
NYX | 0:85b3fd62ea1a | 5798 | |
NYX | 0:85b3fd62ea1a | 5799 | /* Clear CCF Flag */ |
NYX | 0:85b3fd62ea1a | 5800 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
NYX | 0:85b3fd62ea1a | 5801 | |
NYX | 0:85b3fd62ea1a | 5802 | /* Disable CRYP */ |
NYX | 0:85b3fd62ea1a | 5803 | __HAL_CRYP_DISABLE(); |
NYX | 0:85b3fd62ea1a | 5804 | |
NYX | 0:85b3fd62ea1a | 5805 | /* Change the CRYP state to ready */ |
NYX | 0:85b3fd62ea1a | 5806 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 5807 | |
NYX | 0:85b3fd62ea1a | 5808 | /* Call output data transfer complete callback */ |
NYX | 0:85b3fd62ea1a | 5809 | HAL_CRYP_OutCpltCallback(hcryp); |
NYX | 0:85b3fd62ea1a | 5810 | } |
NYX | 0:85b3fd62ea1a | 5811 | |
NYX | 0:85b3fd62ea1a | 5812 | /** |
NYX | 0:85b3fd62ea1a | 5813 | * @brief DMA CRYP communication error callback. |
NYX | 0:85b3fd62ea1a | 5814 | * @param hdma: DMA handle. |
NYX | 0:85b3fd62ea1a | 5815 | * @retval None |
NYX | 0:85b3fd62ea1a | 5816 | */ |
NYX | 0:85b3fd62ea1a | 5817 | static void CRYP_DMAError(DMA_HandleTypeDef *hdma) |
NYX | 0:85b3fd62ea1a | 5818 | { |
NYX | 0:85b3fd62ea1a | 5819 | CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
NYX | 0:85b3fd62ea1a | 5820 | |
NYX | 0:85b3fd62ea1a | 5821 | hcryp->State= HAL_CRYP_STATE_ERROR; |
NYX | 0:85b3fd62ea1a | 5822 | hcryp->ErrorCode |= HAL_CRYP_DMA_ERROR; |
NYX | 0:85b3fd62ea1a | 5823 | HAL_CRYP_ErrorCallback(hcryp); |
NYX | 0:85b3fd62ea1a | 5824 | /* Clear Error Flag */ |
NYX | 0:85b3fd62ea1a | 5825 | __HAL_CRYP_CLEAR_FLAG(CRYP_ERR_CLEAR); |
NYX | 0:85b3fd62ea1a | 5826 | } |
NYX | 0:85b3fd62ea1a | 5827 | |
NYX | 0:85b3fd62ea1a | 5828 | /** |
NYX | 0:85b3fd62ea1a | 5829 | * @brief Last header or payload block padding when size is not a multiple of 128 bits. |
NYX | 0:85b3fd62ea1a | 5830 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 5831 | * the configuration information for CRYP module. |
NYX | 0:85b3fd62ea1a | 5832 | * @param difflength: size remainder after having fed all complete 128-bit blocks. |
NYX | 0:85b3fd62ea1a | 5833 | * @param polling: specifies whether or not polling on CCF must be done after having |
NYX | 0:85b3fd62ea1a | 5834 | * entered a complete block. |
NYX | 0:85b3fd62ea1a | 5835 | * @retval None |
NYX | 0:85b3fd62ea1a | 5836 | */ |
NYX | 0:85b3fd62ea1a | 5837 | static void CRYP_Padding(CRYP_HandleTypeDef *hcryp, uint32_t difflength, uint32_t polling) |
NYX | 0:85b3fd62ea1a | 5838 | { |
NYX | 0:85b3fd62ea1a | 5839 | uint32_t index = 0U; |
NYX | 0:85b3fd62ea1a | 5840 | uint32_t difflengthmod4 = difflength%4U; |
NYX | 0:85b3fd62ea1a | 5841 | uint32_t inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
NYX | 0:85b3fd62ea1a | 5842 | uint32_t outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; |
NYX | 0:85b3fd62ea1a | 5843 | uint32_t mask[3U] = {0x0FFU, 0x0FFFFU, 0x0FFFFFFU}; |
NYX | 0:85b3fd62ea1a | 5844 | uint32_t intermediate_data[4U] = {0U}; |
NYX | 0:85b3fd62ea1a | 5845 | |
NYX | 0:85b3fd62ea1a | 5846 | #if defined(AES_CR_NPBLB) |
NYX | 0:85b3fd62ea1a | 5847 | /* In case of GCM encryption or CCM decryption, specify the number of padding |
NYX | 0:85b3fd62ea1a | 5848 | bytes in last block of payload */ |
NYX | 0:85b3fd62ea1a | 5849 | if (READ_BIT(hcryp->Instance->CR,AES_CR_GCMPH) == CRYP_PAYLOAD_PHASE) |
NYX | 0:85b3fd62ea1a | 5850 | { |
NYX | 0:85b3fd62ea1a | 5851 | if (((READ_BIT(hcryp->Instance->CR, AES_CR_CHMOD) == CRYP_CHAINMODE_AES_GCM_GMAC) |
NYX | 0:85b3fd62ea1a | 5852 | && (READ_BIT(hcryp->Instance->CR, AES_CR_MODE) == CRYP_ALGOMODE_ENCRYPT)) |
NYX | 0:85b3fd62ea1a | 5853 | || ((READ_BIT(hcryp->Instance->CR, AES_CR_CHMOD) == CRYP_CHAINMODE_AES_CCM_CMAC) |
NYX | 0:85b3fd62ea1a | 5854 | && (READ_BIT(hcryp->Instance->CR, AES_CR_MODE) == CRYP_ALGOMODE_DECRYPT))) |
NYX | 0:85b3fd62ea1a | 5855 | { |
NYX | 0:85b3fd62ea1a | 5856 | /* Set NPBLB field in writing the number of padding bytes |
NYX | 0:85b3fd62ea1a | 5857 | for the last block of payload */ |
NYX | 0:85b3fd62ea1a | 5858 | MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, 16U - difflength); |
NYX | 0:85b3fd62ea1a | 5859 | } |
NYX | 0:85b3fd62ea1a | 5860 | } |
NYX | 0:85b3fd62ea1a | 5861 | #else |
NYX | 0:85b3fd62ea1a | 5862 | /* Software workaround applied to GCM encryption only */ |
NYX | 0:85b3fd62ea1a | 5863 | if ((hcryp->Init.GCMCMACPhase == CRYP_GCM_PAYLOAD_PHASE) && |
NYX | 0:85b3fd62ea1a | 5864 | (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT)) |
NYX | 0:85b3fd62ea1a | 5865 | { |
NYX | 0:85b3fd62ea1a | 5866 | /* Change the mode configured in CHMOD bits of CR register to select CTR mode */ |
NYX | 0:85b3fd62ea1a | 5867 | __HAL_CRYP_SET_CHAININGMODE(CRYP_CHAINMODE_AES_CTR); |
NYX | 0:85b3fd62ea1a | 5868 | } |
NYX | 0:85b3fd62ea1a | 5869 | #endif |
NYX | 0:85b3fd62ea1a | 5870 | |
NYX | 0:85b3fd62ea1a | 5871 | /* Wrap-up entering header or payload data */ |
NYX | 0:85b3fd62ea1a | 5872 | /* Enter complete words when possible */ |
NYX | 0:85b3fd62ea1a | 5873 | for(index=0U; index < (difflength/4U); index ++) |
NYX | 0:85b3fd62ea1a | 5874 | { |
NYX | 0:85b3fd62ea1a | 5875 | /* Write the Input block in the Data Input register */ |
NYX | 0:85b3fd62ea1a | 5876 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
NYX | 0:85b3fd62ea1a | 5877 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5878 | } |
NYX | 0:85b3fd62ea1a | 5879 | /* Enter incomplete word padded with zeroes if applicable |
NYX | 0:85b3fd62ea1a | 5880 | (case of header length not a multiple of 32-bits) */ |
NYX | 0:85b3fd62ea1a | 5881 | if (difflengthmod4 != 0U) |
NYX | 0:85b3fd62ea1a | 5882 | { |
NYX | 0:85b3fd62ea1a | 5883 | hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[difflengthmod4-1]); |
NYX | 0:85b3fd62ea1a | 5884 | } |
NYX | 0:85b3fd62ea1a | 5885 | /* Pad with zero-words to reach 128-bit long block and wrap-up header feeding to the IP */ |
NYX | 0:85b3fd62ea1a | 5886 | for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++) |
NYX | 0:85b3fd62ea1a | 5887 | { |
NYX | 0:85b3fd62ea1a | 5888 | hcryp->Instance->DINR = 0U; |
NYX | 0:85b3fd62ea1a | 5889 | } |
NYX | 0:85b3fd62ea1a | 5890 | |
NYX | 0:85b3fd62ea1a | 5891 | if (polling == CRYP_POLLING_ON) |
NYX | 0:85b3fd62ea1a | 5892 | { |
NYX | 0:85b3fd62ea1a | 5893 | if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK) |
NYX | 0:85b3fd62ea1a | 5894 | { |
NYX | 0:85b3fd62ea1a | 5895 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 5896 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 5897 | HAL_CRYP_ErrorCallback(hcryp); |
NYX | 0:85b3fd62ea1a | 5898 | } |
NYX | 0:85b3fd62ea1a | 5899 | |
NYX | 0:85b3fd62ea1a | 5900 | /* Clear CCF Flag */ |
NYX | 0:85b3fd62ea1a | 5901 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
NYX | 0:85b3fd62ea1a | 5902 | } |
NYX | 0:85b3fd62ea1a | 5903 | |
NYX | 0:85b3fd62ea1a | 5904 | /* if payload */ |
NYX | 0:85b3fd62ea1a | 5905 | if (hcryp->Init.GCMCMACPhase == CRYP_GCM_PAYLOAD_PHASE) |
NYX | 0:85b3fd62ea1a | 5906 | { |
NYX | 0:85b3fd62ea1a | 5907 | |
NYX | 0:85b3fd62ea1a | 5908 | /* Retrieve intermediate data */ |
NYX | 0:85b3fd62ea1a | 5909 | for(index=0U; index < 4U; index ++) |
NYX | 0:85b3fd62ea1a | 5910 | { |
NYX | 0:85b3fd62ea1a | 5911 | intermediate_data[index] = hcryp->Instance->DOUTR; |
NYX | 0:85b3fd62ea1a | 5912 | } |
NYX | 0:85b3fd62ea1a | 5913 | /* Retrieve last words of cyphered data */ |
NYX | 0:85b3fd62ea1a | 5914 | /* First, retrieve complete output words */ |
NYX | 0:85b3fd62ea1a | 5915 | for(index=0U; index < (difflength/4U); index ++) |
NYX | 0:85b3fd62ea1a | 5916 | { |
NYX | 0:85b3fd62ea1a | 5917 | *(uint32_t*)(outputaddr) = intermediate_data[index]; |
NYX | 0:85b3fd62ea1a | 5918 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 5919 | } |
NYX | 0:85b3fd62ea1a | 5920 | /* Next, retrieve partial output word if applicable; |
NYX | 0:85b3fd62ea1a | 5921 | at the same time, start masking intermediate data |
NYX | 0:85b3fd62ea1a | 5922 | with a mask of zeros of same size than the padding |
NYX | 0:85b3fd62ea1a | 5923 | applied to the last block of payload */ |
NYX | 0:85b3fd62ea1a | 5924 | if (difflengthmod4 != 0U) |
NYX | 0:85b3fd62ea1a | 5925 | { |
NYX | 0:85b3fd62ea1a | 5926 | intermediate_data[difflength/4U] &= mask[difflengthmod4-1U]; |
NYX | 0:85b3fd62ea1a | 5927 | *(uint32_t*)(outputaddr) = intermediate_data[difflength/4U]; |
NYX | 0:85b3fd62ea1a | 5928 | } |
NYX | 0:85b3fd62ea1a | 5929 | |
NYX | 0:85b3fd62ea1a | 5930 | #if !defined(AES_CR_NPBLB) |
NYX | 0:85b3fd62ea1a | 5931 | /* Software workaround applied to GCM encryption only, |
NYX | 0:85b3fd62ea1a | 5932 | applicable for AES IP v2 version (where NPBLB is not defined) */ |
NYX | 0:85b3fd62ea1a | 5933 | if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT) |
NYX | 0:85b3fd62ea1a | 5934 | { |
NYX | 0:85b3fd62ea1a | 5935 | /* Change again CHMOD configuration to GCM mode */ |
NYX | 0:85b3fd62ea1a | 5936 | __HAL_CRYP_SET_CHAININGMODE(CRYP_CHAINMODE_AES_GCM_GMAC); |
NYX | 0:85b3fd62ea1a | 5937 | |
NYX | 0:85b3fd62ea1a | 5938 | /* Select FINAL phase */ |
NYX | 0:85b3fd62ea1a | 5939 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_GCMCMAC_FINAL_PHASE); |
NYX | 0:85b3fd62ea1a | 5940 | |
NYX | 0:85b3fd62ea1a | 5941 | /* Before inserting the intermediate data, carry on masking operation |
NYX | 0:85b3fd62ea1a | 5942 | with a mask of zeros of same size than the padding applied to the last block of payload */ |
NYX | 0:85b3fd62ea1a | 5943 | for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++) |
NYX | 0:85b3fd62ea1a | 5944 | { |
NYX | 0:85b3fd62ea1a | 5945 | intermediate_data[(difflength+3U)/4U+index] = 0U; |
NYX | 0:85b3fd62ea1a | 5946 | } |
NYX | 0:85b3fd62ea1a | 5947 | /* Insert intermediate data */ |
NYX | 0:85b3fd62ea1a | 5948 | for(index=0U; index < 4U; index ++) |
NYX | 0:85b3fd62ea1a | 5949 | { |
NYX | 0:85b3fd62ea1a | 5950 | hcryp->Instance->DINR = intermediate_data[index]; |
NYX | 0:85b3fd62ea1a | 5951 | } |
NYX | 0:85b3fd62ea1a | 5952 | |
NYX | 0:85b3fd62ea1a | 5953 | /* Wait for completion, and read data on DOUT. This data is to discard. */ |
NYX | 0:85b3fd62ea1a | 5954 | if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK) |
NYX | 0:85b3fd62ea1a | 5955 | { |
NYX | 0:85b3fd62ea1a | 5956 | hcryp->State = HAL_CRYP_STATE_READY; |
NYX | 0:85b3fd62ea1a | 5957 | __HAL_UNLOCK(hcryp); |
NYX | 0:85b3fd62ea1a | 5958 | HAL_CRYP_ErrorCallback(hcryp); |
NYX | 0:85b3fd62ea1a | 5959 | } |
NYX | 0:85b3fd62ea1a | 5960 | |
NYX | 0:85b3fd62ea1a | 5961 | /* Read data to discard */ |
NYX | 0:85b3fd62ea1a | 5962 | /* Clear CCF Flag */ |
NYX | 0:85b3fd62ea1a | 5963 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
NYX | 0:85b3fd62ea1a | 5964 | for(index=0U; index < 4U; index ++) |
NYX | 0:85b3fd62ea1a | 5965 | { |
NYX | 0:85b3fd62ea1a | 5966 | intermediate_data[index] = hcryp->Instance->DOUTR; |
NYX | 0:85b3fd62ea1a | 5967 | } |
NYX | 0:85b3fd62ea1a | 5968 | |
NYX | 0:85b3fd62ea1a | 5969 | } /* if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT) */ |
NYX | 0:85b3fd62ea1a | 5970 | #endif /* !defined(AES_CR_NPBLB) */ |
NYX | 0:85b3fd62ea1a | 5971 | } /* if (hcryp->Init.GCMCMACPhase == CRYP_GCM_PAYLOAD_PHASE) */ |
NYX | 0:85b3fd62ea1a | 5972 | |
NYX | 0:85b3fd62ea1a | 5973 | } |
NYX | 0:85b3fd62ea1a | 5974 | |
NYX | 0:85b3fd62ea1a | 5975 | /** |
NYX | 0:85b3fd62ea1a | 5976 | * @} |
NYX | 0:85b3fd62ea1a | 5977 | */ |
NYX | 0:85b3fd62ea1a | 5978 | |
NYX | 0:85b3fd62ea1a | 5979 | #endif /* AES */ |
NYX | 0:85b3fd62ea1a | 5980 | |
NYX | 0:85b3fd62ea1a | 5981 | #endif /* HAL_CRYP_MODULE_ENABLED */ |
NYX | 0:85b3fd62ea1a | 5982 | /** |
NYX | 0:85b3fd62ea1a | 5983 | * @} |
NYX | 0:85b3fd62ea1a | 5984 | */ |
NYX | 0:85b3fd62ea1a | 5985 | |
NYX | 0:85b3fd62ea1a | 5986 | /** |
NYX | 0:85b3fd62ea1a | 5987 | * @} |
NYX | 0:85b3fd62ea1a | 5988 | */ |
NYX | 0:85b3fd62ea1a | 5989 | |
NYX | 0:85b3fd62ea1a | 5990 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |