Initial commit
mbed-dev-master/targets/TARGET_STM/TARGET_STM32F4/device/stm32f4xx_hal_cryp_ex.c@0:bb348c97df44, 2020-09-16 (annotated)
- Committer:
- lypinator
- Date:
- Wed Sep 16 01:11:49 2020 +0000
- Revision:
- 0:bb348c97df44
Added PWM
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lypinator | 0:bb348c97df44 | 1 | /** |
lypinator | 0:bb348c97df44 | 2 | ****************************************************************************** |
lypinator | 0:bb348c97df44 | 3 | * @file stm32f4xx_hal_cryp_ex.c |
lypinator | 0:bb348c97df44 | 4 | * @author MCD Application Team |
lypinator | 0:bb348c97df44 | 5 | * @brief Extended CRYP HAL module driver |
lypinator | 0:bb348c97df44 | 6 | * This file provides firmware functions to manage the following |
lypinator | 0:bb348c97df44 | 7 | * functionalities of CRYP extension peripheral: |
lypinator | 0:bb348c97df44 | 8 | * + Extended AES processing functions |
lypinator | 0:bb348c97df44 | 9 | * |
lypinator | 0:bb348c97df44 | 10 | @verbatim |
lypinator | 0:bb348c97df44 | 11 | ============================================================================== |
lypinator | 0:bb348c97df44 | 12 | ##### How to use this driver ##### |
lypinator | 0:bb348c97df44 | 13 | ============================================================================== |
lypinator | 0:bb348c97df44 | 14 | [..] |
lypinator | 0:bb348c97df44 | 15 | The CRYP Extension HAL driver can be used as follows: |
lypinator | 0:bb348c97df44 | 16 | (#)Initialize the CRYP low level resources by implementing the HAL_CRYP_MspInit(): |
lypinator | 0:bb348c97df44 | 17 | (##) Enable the CRYP interface clock using __HAL_RCC_CRYP_CLK_ENABLE() |
lypinator | 0:bb348c97df44 | 18 | (##) In case of using interrupts (e.g. HAL_CRYPEx_AESGCM_Encrypt_IT()) |
lypinator | 0:bb348c97df44 | 19 | (+++) Configure the CRYP interrupt priority using HAL_NVIC_SetPriority() |
lypinator | 0:bb348c97df44 | 20 | (+++) Enable the CRYP IRQ handler using HAL_NVIC_EnableIRQ() |
lypinator | 0:bb348c97df44 | 21 | (+++) In CRYP IRQ handler, call HAL_CRYP_IRQHandler() |
lypinator | 0:bb348c97df44 | 22 | (##) In case of using DMA to control data transfer (e.g. HAL_AES_ECB_Encrypt_DMA()) |
lypinator | 0:bb348c97df44 | 23 | (+++) Enable the DMAx interface clock using __DMAx_CLK_ENABLE() |
lypinator | 0:bb348c97df44 | 24 | (+++) Configure and enable two DMA streams one for managing data transfer from |
lypinator | 0:bb348c97df44 | 25 | memory to peripheral (input stream) and another stream for managing data |
lypinator | 0:bb348c97df44 | 26 | transfer from peripheral to memory (output stream) |
lypinator | 0:bb348c97df44 | 27 | (+++) Associate the initialized DMA handle to the CRYP DMA handle |
lypinator | 0:bb348c97df44 | 28 | using __HAL_LINKDMA() |
lypinator | 0:bb348c97df44 | 29 | (+++) Configure the priority and enable the NVIC for the transfer complete |
lypinator | 0:bb348c97df44 | 30 | interrupt on the two DMA Streams. The output stream should have higher |
lypinator | 0:bb348c97df44 | 31 | priority than the input stream HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ() |
lypinator | 0:bb348c97df44 | 32 | (#)Initialize the CRYP HAL using HAL_CRYP_Init(). This function configures mainly: |
lypinator | 0:bb348c97df44 | 33 | (##) The data type: 1-bit, 8-bit, 16-bit and 32-bit |
lypinator | 0:bb348c97df44 | 34 | (##) The key size: 128, 192 and 256. This parameter is relevant only for AES |
lypinator | 0:bb348c97df44 | 35 | (##) The encryption/decryption key. Its size depends on the algorithm |
lypinator | 0:bb348c97df44 | 36 | used for encryption/decryption |
lypinator | 0:bb348c97df44 | 37 | (##) The initialization vector (counter). It is not used ECB mode. |
lypinator | 0:bb348c97df44 | 38 | (#)Three processing (encryption/decryption) functions are available: |
lypinator | 0:bb348c97df44 | 39 | (##) Polling mode: encryption and decryption APIs are blocking functions |
lypinator | 0:bb348c97df44 | 40 | i.e. they process the data and wait till the processing is finished |
lypinator | 0:bb348c97df44 | 41 | e.g. HAL_CRYPEx_AESGCM_Encrypt() |
lypinator | 0:bb348c97df44 | 42 | (##) Interrupt mode: encryption and decryption APIs are not blocking functions |
lypinator | 0:bb348c97df44 | 43 | i.e. they process the data under interrupt |
lypinator | 0:bb348c97df44 | 44 | e.g. HAL_CRYPEx_AESGCM_Encrypt_IT() |
lypinator | 0:bb348c97df44 | 45 | (##) DMA mode: encryption and decryption APIs are not blocking functions |
lypinator | 0:bb348c97df44 | 46 | i.e. the data transfer is ensured by DMA |
lypinator | 0:bb348c97df44 | 47 | e.g. HAL_CRYPEx_AESGCM_Encrypt_DMA() |
lypinator | 0:bb348c97df44 | 48 | (#)When the processing function is called at first time after HAL_CRYP_Init() |
lypinator | 0:bb348c97df44 | 49 | the CRYP peripheral is initialized and processes the buffer in input. |
lypinator | 0:bb348c97df44 | 50 | At second call, the processing function performs an append of the already |
lypinator | 0:bb348c97df44 | 51 | processed buffer. |
lypinator | 0:bb348c97df44 | 52 | When a new data block is to be processed, call HAL_CRYP_Init() then the |
lypinator | 0:bb348c97df44 | 53 | processing function. |
lypinator | 0:bb348c97df44 | 54 | (#)In AES-GCM and AES-CCM modes are an authenticated encryption algorithms |
lypinator | 0:bb348c97df44 | 55 | which provide authentication messages. |
lypinator | 0:bb348c97df44 | 56 | HAL_AES_GCM_Finish() and HAL_AES_CCM_Finish() are used to provide those |
lypinator | 0:bb348c97df44 | 57 | authentication messages. |
lypinator | 0:bb348c97df44 | 58 | Call those functions after the processing ones (polling, interrupt or DMA). |
lypinator | 0:bb348c97df44 | 59 | e.g. in AES-CCM mode call HAL_CRYPEx_AESCCM_Encrypt() to encrypt the plain data |
lypinator | 0:bb348c97df44 | 60 | then call HAL_CRYPEx_AESCCM_Finish() to get the authentication message |
lypinator | 0:bb348c97df44 | 61 | -@- For CCM Encrypt/Decrypt API's, only DataType = 8-bit is supported by this version. |
lypinator | 0:bb348c97df44 | 62 | -@- The HAL_CRYPEx_AESGCM_xxxx() implementation is limited to 32bits inputs data length |
lypinator | 0:bb348c97df44 | 63 | (Plain/Cyphertext, Header) compared with GCM standards specifications (800-38D). |
lypinator | 0:bb348c97df44 | 64 | (#)Call HAL_CRYP_DeInit() to deinitialize the CRYP peripheral. |
lypinator | 0:bb348c97df44 | 65 | |
lypinator | 0:bb348c97df44 | 66 | @endverbatim |
lypinator | 0:bb348c97df44 | 67 | ****************************************************************************** |
lypinator | 0:bb348c97df44 | 68 | * @attention |
lypinator | 0:bb348c97df44 | 69 | * |
lypinator | 0:bb348c97df44 | 70 | * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> |
lypinator | 0:bb348c97df44 | 71 | * |
lypinator | 0:bb348c97df44 | 72 | * Redistribution and use in source and binary forms, with or without modification, |
lypinator | 0:bb348c97df44 | 73 | * are permitted provided that the following conditions are met: |
lypinator | 0:bb348c97df44 | 74 | * 1. Redistributions of source code must retain the above copyright notice, |
lypinator | 0:bb348c97df44 | 75 | * this list of conditions and the following disclaimer. |
lypinator | 0:bb348c97df44 | 76 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
lypinator | 0:bb348c97df44 | 77 | * this list of conditions and the following disclaimer in the documentation |
lypinator | 0:bb348c97df44 | 78 | * and/or other materials provided with the distribution. |
lypinator | 0:bb348c97df44 | 79 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
lypinator | 0:bb348c97df44 | 80 | * may be used to endorse or promote products derived from this software |
lypinator | 0:bb348c97df44 | 81 | * without specific prior written permission. |
lypinator | 0:bb348c97df44 | 82 | * |
lypinator | 0:bb348c97df44 | 83 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
lypinator | 0:bb348c97df44 | 84 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
lypinator | 0:bb348c97df44 | 85 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
lypinator | 0:bb348c97df44 | 86 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
lypinator | 0:bb348c97df44 | 87 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
lypinator | 0:bb348c97df44 | 88 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
lypinator | 0:bb348c97df44 | 89 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
lypinator | 0:bb348c97df44 | 90 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
lypinator | 0:bb348c97df44 | 91 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
lypinator | 0:bb348c97df44 | 92 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
lypinator | 0:bb348c97df44 | 93 | * |
lypinator | 0:bb348c97df44 | 94 | ****************************************************************************** |
lypinator | 0:bb348c97df44 | 95 | */ |
lypinator | 0:bb348c97df44 | 96 | |
lypinator | 0:bb348c97df44 | 97 | /* Includes ------------------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 98 | #include "stm32f4xx_hal.h" |
lypinator | 0:bb348c97df44 | 99 | |
lypinator | 0:bb348c97df44 | 100 | /** @addtogroup STM32F4xx_HAL_Driver |
lypinator | 0:bb348c97df44 | 101 | * @{ |
lypinator | 0:bb348c97df44 | 102 | */ |
lypinator | 0:bb348c97df44 | 103 | |
lypinator | 0:bb348c97df44 | 104 | /** @defgroup CRYPEx CRYPEx |
lypinator | 0:bb348c97df44 | 105 | * @brief CRYP Extension HAL module driver. |
lypinator | 0:bb348c97df44 | 106 | * @{ |
lypinator | 0:bb348c97df44 | 107 | */ |
lypinator | 0:bb348c97df44 | 108 | |
lypinator | 0:bb348c97df44 | 109 | #ifdef HAL_CRYP_MODULE_ENABLED |
lypinator | 0:bb348c97df44 | 110 | |
lypinator | 0:bb348c97df44 | 111 | #if defined(CRYP) |
lypinator | 0:bb348c97df44 | 112 | |
lypinator | 0:bb348c97df44 | 113 | /* Private typedef -----------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 114 | /* Private define ------------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 115 | /** @addtogroup CRYPEx_Private_define |
lypinator | 0:bb348c97df44 | 116 | * @{ |
lypinator | 0:bb348c97df44 | 117 | */ |
lypinator | 0:bb348c97df44 | 118 | #define CRYPEx_TIMEOUT_VALUE 1U |
lypinator | 0:bb348c97df44 | 119 | /** |
lypinator | 0:bb348c97df44 | 120 | * @} |
lypinator | 0:bb348c97df44 | 121 | */ |
lypinator | 0:bb348c97df44 | 122 | |
lypinator | 0:bb348c97df44 | 123 | /* Private macro -------------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 124 | /* Private variables ---------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 125 | /* Private function prototypes -----------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 126 | /** @defgroup CRYPEx_Private_Functions_prototypes CRYP Private Functions Prototypes |
lypinator | 0:bb348c97df44 | 127 | * @{ |
lypinator | 0:bb348c97df44 | 128 | */ |
lypinator | 0:bb348c97df44 | 129 | static void CRYPEx_GCMCCM_SetInitVector(CRYP_HandleTypeDef *hcryp, uint8_t *InitVector); |
lypinator | 0:bb348c97df44 | 130 | static void CRYPEx_GCMCCM_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key, uint32_t KeySize); |
lypinator | 0:bb348c97df44 | 131 | static HAL_StatusTypeDef CRYPEx_GCMCCM_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t *Input, uint16_t Ilength, uint8_t *Output, uint32_t Timeout); |
lypinator | 0:bb348c97df44 | 132 | static HAL_StatusTypeDef CRYPEx_GCMCCM_SetHeaderPhase(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint32_t Timeout); |
lypinator | 0:bb348c97df44 | 133 | static void CRYPEx_GCMCCM_DMAInCplt(DMA_HandleTypeDef *hdma); |
lypinator | 0:bb348c97df44 | 134 | static void CRYPEx_GCMCCM_DMAOutCplt(DMA_HandleTypeDef *hdma); |
lypinator | 0:bb348c97df44 | 135 | static void CRYPEx_GCMCCM_DMAError(DMA_HandleTypeDef *hdma); |
lypinator | 0:bb348c97df44 | 136 | static void CRYPEx_GCMCCM_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr); |
lypinator | 0:bb348c97df44 | 137 | /** |
lypinator | 0:bb348c97df44 | 138 | * @} |
lypinator | 0:bb348c97df44 | 139 | */ |
lypinator | 0:bb348c97df44 | 140 | |
lypinator | 0:bb348c97df44 | 141 | /* Private functions ---------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 142 | /** @addtogroup CRYPEx_Private_Functions |
lypinator | 0:bb348c97df44 | 143 | * @{ |
lypinator | 0:bb348c97df44 | 144 | */ |
lypinator | 0:bb348c97df44 | 145 | |
lypinator | 0:bb348c97df44 | 146 | /** |
lypinator | 0:bb348c97df44 | 147 | * @brief DMA CRYP Input Data process complete callback. |
lypinator | 0:bb348c97df44 | 148 | * @param hdma DMA handle |
lypinator | 0:bb348c97df44 | 149 | * @retval None |
lypinator | 0:bb348c97df44 | 150 | */ |
lypinator | 0:bb348c97df44 | 151 | static void CRYPEx_GCMCCM_DMAInCplt(DMA_HandleTypeDef *hdma) |
lypinator | 0:bb348c97df44 | 152 | { |
lypinator | 0:bb348c97df44 | 153 | CRYP_HandleTypeDef* hcryp = ( CRYP_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
lypinator | 0:bb348c97df44 | 154 | |
lypinator | 0:bb348c97df44 | 155 | /* Disable the DMA transfer for input Fifo request by resetting the DIEN bit |
lypinator | 0:bb348c97df44 | 156 | in the DMACR register */ |
lypinator | 0:bb348c97df44 | 157 | hcryp->Instance->DMACR &= (uint32_t)(~CRYP_DMACR_DIEN); |
lypinator | 0:bb348c97df44 | 158 | |
lypinator | 0:bb348c97df44 | 159 | /* Call input data transfer complete callback */ |
lypinator | 0:bb348c97df44 | 160 | HAL_CRYP_InCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 161 | } |
lypinator | 0:bb348c97df44 | 162 | |
lypinator | 0:bb348c97df44 | 163 | /** |
lypinator | 0:bb348c97df44 | 164 | * @brief DMA CRYP Output Data process complete callback. |
lypinator | 0:bb348c97df44 | 165 | * @param hdma DMA handle |
lypinator | 0:bb348c97df44 | 166 | * @retval None |
lypinator | 0:bb348c97df44 | 167 | */ |
lypinator | 0:bb348c97df44 | 168 | static void CRYPEx_GCMCCM_DMAOutCplt(DMA_HandleTypeDef *hdma) |
lypinator | 0:bb348c97df44 | 169 | { |
lypinator | 0:bb348c97df44 | 170 | CRYP_HandleTypeDef* hcryp = ( CRYP_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
lypinator | 0:bb348c97df44 | 171 | |
lypinator | 0:bb348c97df44 | 172 | /* Disable the DMA transfer for output Fifo request by resetting the DOEN bit |
lypinator | 0:bb348c97df44 | 173 | in the DMACR register */ |
lypinator | 0:bb348c97df44 | 174 | hcryp->Instance->DMACR &= (uint32_t)(~CRYP_DMACR_DOEN); |
lypinator | 0:bb348c97df44 | 175 | |
lypinator | 0:bb348c97df44 | 176 | /* Enable the CRYP peripheral */ |
lypinator | 0:bb348c97df44 | 177 | __HAL_CRYP_DISABLE(hcryp); |
lypinator | 0:bb348c97df44 | 178 | |
lypinator | 0:bb348c97df44 | 179 | /* Change the CRYP peripheral state */ |
lypinator | 0:bb348c97df44 | 180 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 181 | |
lypinator | 0:bb348c97df44 | 182 | /* Call output data transfer complete callback */ |
lypinator | 0:bb348c97df44 | 183 | HAL_CRYP_OutCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 184 | } |
lypinator | 0:bb348c97df44 | 185 | |
lypinator | 0:bb348c97df44 | 186 | /** |
lypinator | 0:bb348c97df44 | 187 | * @brief DMA CRYP communication error callback. |
lypinator | 0:bb348c97df44 | 188 | * @param hdma DMA handle |
lypinator | 0:bb348c97df44 | 189 | * @retval None |
lypinator | 0:bb348c97df44 | 190 | */ |
lypinator | 0:bb348c97df44 | 191 | static void CRYPEx_GCMCCM_DMAError(DMA_HandleTypeDef *hdma) |
lypinator | 0:bb348c97df44 | 192 | { |
lypinator | 0:bb348c97df44 | 193 | CRYP_HandleTypeDef* hcryp = ( CRYP_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
lypinator | 0:bb348c97df44 | 194 | hcryp->State= HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 195 | HAL_CRYP_ErrorCallback(hcryp); |
lypinator | 0:bb348c97df44 | 196 | } |
lypinator | 0:bb348c97df44 | 197 | |
lypinator | 0:bb348c97df44 | 198 | /** |
lypinator | 0:bb348c97df44 | 199 | * @brief Writes the Key in Key registers. |
lypinator | 0:bb348c97df44 | 200 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 201 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 202 | * @param Key Pointer to Key buffer |
lypinator | 0:bb348c97df44 | 203 | * @param KeySize Size of Key |
lypinator | 0:bb348c97df44 | 204 | * @retval None |
lypinator | 0:bb348c97df44 | 205 | */ |
lypinator | 0:bb348c97df44 | 206 | static void CRYPEx_GCMCCM_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key, uint32_t KeySize) |
lypinator | 0:bb348c97df44 | 207 | { |
lypinator | 0:bb348c97df44 | 208 | uint32_t keyaddr = (uint32_t)Key; |
lypinator | 0:bb348c97df44 | 209 | |
lypinator | 0:bb348c97df44 | 210 | switch(KeySize) |
lypinator | 0:bb348c97df44 | 211 | { |
lypinator | 0:bb348c97df44 | 212 | case CRYP_KEYSIZE_256B: |
lypinator | 0:bb348c97df44 | 213 | /* Key Initialisation */ |
lypinator | 0:bb348c97df44 | 214 | hcryp->Instance->K0LR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 215 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 216 | hcryp->Instance->K0RR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 217 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 218 | hcryp->Instance->K1LR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 219 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 220 | hcryp->Instance->K1RR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 221 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 222 | hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 223 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 224 | hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 225 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 226 | hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 227 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 228 | hcryp->Instance->K3RR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 229 | break; |
lypinator | 0:bb348c97df44 | 230 | case CRYP_KEYSIZE_192B: |
lypinator | 0:bb348c97df44 | 231 | hcryp->Instance->K1LR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 232 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 233 | hcryp->Instance->K1RR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 234 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 235 | hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 236 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 237 | hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 238 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 239 | hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 240 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 241 | hcryp->Instance->K3RR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 242 | break; |
lypinator | 0:bb348c97df44 | 243 | case CRYP_KEYSIZE_128B: |
lypinator | 0:bb348c97df44 | 244 | hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 245 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 246 | hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 247 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 248 | hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 249 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 250 | hcryp->Instance->K3RR = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 251 | break; |
lypinator | 0:bb348c97df44 | 252 | default: |
lypinator | 0:bb348c97df44 | 253 | break; |
lypinator | 0:bb348c97df44 | 254 | } |
lypinator | 0:bb348c97df44 | 255 | } |
lypinator | 0:bb348c97df44 | 256 | |
lypinator | 0:bb348c97df44 | 257 | /** |
lypinator | 0:bb348c97df44 | 258 | * @brief Writes the InitVector/InitCounter in IV registers. |
lypinator | 0:bb348c97df44 | 259 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 260 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 261 | * @param InitVector Pointer to InitVector/InitCounter buffer |
lypinator | 0:bb348c97df44 | 262 | * @retval None |
lypinator | 0:bb348c97df44 | 263 | */ |
lypinator | 0:bb348c97df44 | 264 | static void CRYPEx_GCMCCM_SetInitVector(CRYP_HandleTypeDef *hcryp, uint8_t *InitVector) |
lypinator | 0:bb348c97df44 | 265 | { |
lypinator | 0:bb348c97df44 | 266 | uint32_t ivaddr = (uint32_t)InitVector; |
lypinator | 0:bb348c97df44 | 267 | |
lypinator | 0:bb348c97df44 | 268 | hcryp->Instance->IV0LR = __REV(*(uint32_t*)(ivaddr)); |
lypinator | 0:bb348c97df44 | 269 | ivaddr+=4U; |
lypinator | 0:bb348c97df44 | 270 | hcryp->Instance->IV0RR = __REV(*(uint32_t*)(ivaddr)); |
lypinator | 0:bb348c97df44 | 271 | ivaddr+=4U; |
lypinator | 0:bb348c97df44 | 272 | hcryp->Instance->IV1LR = __REV(*(uint32_t*)(ivaddr)); |
lypinator | 0:bb348c97df44 | 273 | ivaddr+=4U; |
lypinator | 0:bb348c97df44 | 274 | hcryp->Instance->IV1RR = __REV(*(uint32_t*)(ivaddr)); |
lypinator | 0:bb348c97df44 | 275 | } |
lypinator | 0:bb348c97df44 | 276 | |
lypinator | 0:bb348c97df44 | 277 | /** |
lypinator | 0:bb348c97df44 | 278 | * @brief Process Data: Writes Input data in polling mode and read the Output data. |
lypinator | 0:bb348c97df44 | 279 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 280 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 281 | * @param Input Pointer to the Input buffer. |
lypinator | 0:bb348c97df44 | 282 | * @param Ilength Length of the Input buffer, must be a multiple of 16 |
lypinator | 0:bb348c97df44 | 283 | * @param Output Pointer to the returned buffer |
lypinator | 0:bb348c97df44 | 284 | * @param Timeout Timeout value |
lypinator | 0:bb348c97df44 | 285 | * @retval None |
lypinator | 0:bb348c97df44 | 286 | */ |
lypinator | 0:bb348c97df44 | 287 | static HAL_StatusTypeDef CRYPEx_GCMCCM_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t *Input, uint16_t Ilength, uint8_t *Output, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 288 | { |
lypinator | 0:bb348c97df44 | 289 | uint32_t tickstart = 0U; |
lypinator | 0:bb348c97df44 | 290 | uint32_t i = 0U; |
lypinator | 0:bb348c97df44 | 291 | uint32_t inputaddr = (uint32_t)Input; |
lypinator | 0:bb348c97df44 | 292 | uint32_t outputaddr = (uint32_t)Output; |
lypinator | 0:bb348c97df44 | 293 | |
lypinator | 0:bb348c97df44 | 294 | for(i=0U; (i < Ilength); i+=16U) |
lypinator | 0:bb348c97df44 | 295 | { |
lypinator | 0:bb348c97df44 | 296 | /* Write the Input block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 297 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 298 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 299 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 300 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 301 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 302 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 303 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 304 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 305 | |
lypinator | 0:bb348c97df44 | 306 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 307 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 308 | |
lypinator | 0:bb348c97df44 | 309 | while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE)) |
lypinator | 0:bb348c97df44 | 310 | { |
lypinator | 0:bb348c97df44 | 311 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 312 | if(Timeout != HAL_MAX_DELAY) |
lypinator | 0:bb348c97df44 | 313 | { |
lypinator | 0:bb348c97df44 | 314 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
lypinator | 0:bb348c97df44 | 315 | { |
lypinator | 0:bb348c97df44 | 316 | /* Change state */ |
lypinator | 0:bb348c97df44 | 317 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 318 | |
lypinator | 0:bb348c97df44 | 319 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 320 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 321 | |
lypinator | 0:bb348c97df44 | 322 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 323 | } |
lypinator | 0:bb348c97df44 | 324 | } |
lypinator | 0:bb348c97df44 | 325 | } |
lypinator | 0:bb348c97df44 | 326 | /* Read the Output block from the OUT FIFO */ |
lypinator | 0:bb348c97df44 | 327 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 328 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 329 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 330 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 331 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 332 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 333 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 334 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 335 | } |
lypinator | 0:bb348c97df44 | 336 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 337 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 338 | } |
lypinator | 0:bb348c97df44 | 339 | |
lypinator | 0:bb348c97df44 | 340 | /** |
lypinator | 0:bb348c97df44 | 341 | * @brief Sets the header phase |
lypinator | 0:bb348c97df44 | 342 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 343 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 344 | * @param Input Pointer to the Input buffer. |
lypinator | 0:bb348c97df44 | 345 | * @param Ilength Length of the Input buffer, must be a multiple of 16 |
lypinator | 0:bb348c97df44 | 346 | * @param Timeout Timeout value |
lypinator | 0:bb348c97df44 | 347 | * @retval None |
lypinator | 0:bb348c97df44 | 348 | */ |
lypinator | 0:bb348c97df44 | 349 | static HAL_StatusTypeDef CRYPEx_GCMCCM_SetHeaderPhase(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 350 | { |
lypinator | 0:bb348c97df44 | 351 | uint32_t tickstart = 0U; |
lypinator | 0:bb348c97df44 | 352 | uint32_t loopcounter = 0U; |
lypinator | 0:bb348c97df44 | 353 | uint32_t headeraddr = (uint32_t)Input; |
lypinator | 0:bb348c97df44 | 354 | |
lypinator | 0:bb348c97df44 | 355 | /* Prevent unused argument(s) compilation warning */ |
lypinator | 0:bb348c97df44 | 356 | UNUSED(Ilength); |
lypinator | 0:bb348c97df44 | 357 | |
lypinator | 0:bb348c97df44 | 358 | /***************************** Header phase *********************************/ |
lypinator | 0:bb348c97df44 | 359 | if(hcryp->Init.HeaderSize != 0U) |
lypinator | 0:bb348c97df44 | 360 | { |
lypinator | 0:bb348c97df44 | 361 | /* Select header phase */ |
lypinator | 0:bb348c97df44 | 362 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER); |
lypinator | 0:bb348c97df44 | 363 | /* Enable the CRYP peripheral */ |
lypinator | 0:bb348c97df44 | 364 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 365 | |
lypinator | 0:bb348c97df44 | 366 | for(loopcounter = 0U; (loopcounter < hcryp->Init.HeaderSize); loopcounter+=16U) |
lypinator | 0:bb348c97df44 | 367 | { |
lypinator | 0:bb348c97df44 | 368 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 369 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 370 | |
lypinator | 0:bb348c97df44 | 371 | while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM)) |
lypinator | 0:bb348c97df44 | 372 | { |
lypinator | 0:bb348c97df44 | 373 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 374 | if(Timeout != HAL_MAX_DELAY) |
lypinator | 0:bb348c97df44 | 375 | { |
lypinator | 0:bb348c97df44 | 376 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
lypinator | 0:bb348c97df44 | 377 | { |
lypinator | 0:bb348c97df44 | 378 | /* Change state */ |
lypinator | 0:bb348c97df44 | 379 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 380 | |
lypinator | 0:bb348c97df44 | 381 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 382 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 383 | |
lypinator | 0:bb348c97df44 | 384 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 385 | } |
lypinator | 0:bb348c97df44 | 386 | } |
lypinator | 0:bb348c97df44 | 387 | } |
lypinator | 0:bb348c97df44 | 388 | /* Write the Input block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 389 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
lypinator | 0:bb348c97df44 | 390 | headeraddr+=4U; |
lypinator | 0:bb348c97df44 | 391 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
lypinator | 0:bb348c97df44 | 392 | headeraddr+=4U; |
lypinator | 0:bb348c97df44 | 393 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
lypinator | 0:bb348c97df44 | 394 | headeraddr+=4U; |
lypinator | 0:bb348c97df44 | 395 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
lypinator | 0:bb348c97df44 | 396 | headeraddr+=4U; |
lypinator | 0:bb348c97df44 | 397 | } |
lypinator | 0:bb348c97df44 | 398 | |
lypinator | 0:bb348c97df44 | 399 | /* Wait until the complete message has been processed */ |
lypinator | 0:bb348c97df44 | 400 | |
lypinator | 0:bb348c97df44 | 401 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 402 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 403 | |
lypinator | 0:bb348c97df44 | 404 | while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY) |
lypinator | 0:bb348c97df44 | 405 | { |
lypinator | 0:bb348c97df44 | 406 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 407 | if(Timeout != HAL_MAX_DELAY) |
lypinator | 0:bb348c97df44 | 408 | { |
lypinator | 0:bb348c97df44 | 409 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
lypinator | 0:bb348c97df44 | 410 | { |
lypinator | 0:bb348c97df44 | 411 | /* Change state */ |
lypinator | 0:bb348c97df44 | 412 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 413 | |
lypinator | 0:bb348c97df44 | 414 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 415 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 416 | |
lypinator | 0:bb348c97df44 | 417 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 418 | } |
lypinator | 0:bb348c97df44 | 419 | } |
lypinator | 0:bb348c97df44 | 420 | } |
lypinator | 0:bb348c97df44 | 421 | } |
lypinator | 0:bb348c97df44 | 422 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 423 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 424 | } |
lypinator | 0:bb348c97df44 | 425 | |
lypinator | 0:bb348c97df44 | 426 | /** |
lypinator | 0:bb348c97df44 | 427 | * @brief Sets the DMA configuration and start the DMA transfer. |
lypinator | 0:bb348c97df44 | 428 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 429 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 430 | * @param inputaddr Address of the Input buffer |
lypinator | 0:bb348c97df44 | 431 | * @param Size Size of the Input buffer, must be a multiple of 16 |
lypinator | 0:bb348c97df44 | 432 | * @param outputaddr Address of the Output buffer |
lypinator | 0:bb348c97df44 | 433 | * @retval None |
lypinator | 0:bb348c97df44 | 434 | */ |
lypinator | 0:bb348c97df44 | 435 | static void CRYPEx_GCMCCM_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr) |
lypinator | 0:bb348c97df44 | 436 | { |
lypinator | 0:bb348c97df44 | 437 | /* Set the CRYP DMA transfer complete callback */ |
lypinator | 0:bb348c97df44 | 438 | hcryp->hdmain->XferCpltCallback = CRYPEx_GCMCCM_DMAInCplt; |
lypinator | 0:bb348c97df44 | 439 | /* Set the DMA error callback */ |
lypinator | 0:bb348c97df44 | 440 | hcryp->hdmain->XferErrorCallback = CRYPEx_GCMCCM_DMAError; |
lypinator | 0:bb348c97df44 | 441 | |
lypinator | 0:bb348c97df44 | 442 | /* Set the CRYP DMA transfer complete callback */ |
lypinator | 0:bb348c97df44 | 443 | hcryp->hdmaout->XferCpltCallback = CRYPEx_GCMCCM_DMAOutCplt; |
lypinator | 0:bb348c97df44 | 444 | /* Set the DMA error callback */ |
lypinator | 0:bb348c97df44 | 445 | hcryp->hdmaout->XferErrorCallback = CRYPEx_GCMCCM_DMAError; |
lypinator | 0:bb348c97df44 | 446 | |
lypinator | 0:bb348c97df44 | 447 | /* Enable the CRYP peripheral */ |
lypinator | 0:bb348c97df44 | 448 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 449 | |
lypinator | 0:bb348c97df44 | 450 | /* Enable the DMA In DMA Stream */ |
lypinator | 0:bb348c97df44 | 451 | HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DR, Size/4U); |
lypinator | 0:bb348c97df44 | 452 | |
lypinator | 0:bb348c97df44 | 453 | /* Enable In DMA request */ |
lypinator | 0:bb348c97df44 | 454 | hcryp->Instance->DMACR = CRYP_DMACR_DIEN; |
lypinator | 0:bb348c97df44 | 455 | |
lypinator | 0:bb348c97df44 | 456 | /* Enable the DMA Out DMA Stream */ |
lypinator | 0:bb348c97df44 | 457 | HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUT, outputaddr, Size/4U); |
lypinator | 0:bb348c97df44 | 458 | |
lypinator | 0:bb348c97df44 | 459 | /* Enable Out DMA request */ |
lypinator | 0:bb348c97df44 | 460 | hcryp->Instance->DMACR |= CRYP_DMACR_DOEN; |
lypinator | 0:bb348c97df44 | 461 | } |
lypinator | 0:bb348c97df44 | 462 | |
lypinator | 0:bb348c97df44 | 463 | /** |
lypinator | 0:bb348c97df44 | 464 | * @} |
lypinator | 0:bb348c97df44 | 465 | */ |
lypinator | 0:bb348c97df44 | 466 | |
lypinator | 0:bb348c97df44 | 467 | /* Exported functions---------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 468 | /** @addtogroup CRYPEx_Exported_Functions |
lypinator | 0:bb348c97df44 | 469 | * @{ |
lypinator | 0:bb348c97df44 | 470 | */ |
lypinator | 0:bb348c97df44 | 471 | |
lypinator | 0:bb348c97df44 | 472 | /** @defgroup CRYPEx_Exported_Functions_Group1 Extended AES processing functions |
lypinator | 0:bb348c97df44 | 473 | * @brief Extended processing functions. |
lypinator | 0:bb348c97df44 | 474 | * |
lypinator | 0:bb348c97df44 | 475 | @verbatim |
lypinator | 0:bb348c97df44 | 476 | ============================================================================== |
lypinator | 0:bb348c97df44 | 477 | ##### Extended AES processing functions ##### |
lypinator | 0:bb348c97df44 | 478 | ============================================================================== |
lypinator | 0:bb348c97df44 | 479 | [..] This section provides functions allowing to: |
lypinator | 0:bb348c97df44 | 480 | (+) Encrypt plaintext using AES-128/192/256 using GCM and CCM chaining modes |
lypinator | 0:bb348c97df44 | 481 | (+) Decrypt cyphertext using AES-128/192/256 using GCM and CCM chaining modes |
lypinator | 0:bb348c97df44 | 482 | (+) Finish the processing. This function is available only for GCM and CCM |
lypinator | 0:bb348c97df44 | 483 | [..] Three processing methods are available: |
lypinator | 0:bb348c97df44 | 484 | (+) Polling mode |
lypinator | 0:bb348c97df44 | 485 | (+) Interrupt mode |
lypinator | 0:bb348c97df44 | 486 | (+) DMA mode |
lypinator | 0:bb348c97df44 | 487 | |
lypinator | 0:bb348c97df44 | 488 | @endverbatim |
lypinator | 0:bb348c97df44 | 489 | * @{ |
lypinator | 0:bb348c97df44 | 490 | */ |
lypinator | 0:bb348c97df44 | 491 | |
lypinator | 0:bb348c97df44 | 492 | |
lypinator | 0:bb348c97df44 | 493 | /** |
lypinator | 0:bb348c97df44 | 494 | * @brief Initializes the CRYP peripheral in AES CCM encryption mode then |
lypinator | 0:bb348c97df44 | 495 | * encrypt pPlainData. The cypher data are available in pCypherData. |
lypinator | 0:bb348c97df44 | 496 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 497 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 498 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 499 | * @param Size Length of the plaintext buffer, must be a multiple of 16 |
lypinator | 0:bb348c97df44 | 500 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 501 | * @param Timeout Timeout duration |
lypinator | 0:bb348c97df44 | 502 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 503 | */ |
lypinator | 0:bb348c97df44 | 504 | HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 505 | { |
lypinator | 0:bb348c97df44 | 506 | uint32_t tickstart = 0U; |
lypinator | 0:bb348c97df44 | 507 | uint32_t headersize = hcryp->Init.HeaderSize; |
lypinator | 0:bb348c97df44 | 508 | uint32_t headeraddr = (uint32_t)hcryp->Init.Header; |
lypinator | 0:bb348c97df44 | 509 | uint32_t loopcounter = 0U; |
lypinator | 0:bb348c97df44 | 510 | uint32_t bufferidx = 0U; |
lypinator | 0:bb348c97df44 | 511 | uint8_t blockb0[16U] = {0};/* Block B0 */ |
lypinator | 0:bb348c97df44 | 512 | uint8_t ctr[16U] = {0}; /* Counter */ |
lypinator | 0:bb348c97df44 | 513 | uint32_t b0addr = (uint32_t)blockb0; |
lypinator | 0:bb348c97df44 | 514 | |
lypinator | 0:bb348c97df44 | 515 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 516 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 517 | |
lypinator | 0:bb348c97df44 | 518 | /* Change the CRYP peripheral state */ |
lypinator | 0:bb348c97df44 | 519 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 520 | |
lypinator | 0:bb348c97df44 | 521 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 522 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 523 | { |
lypinator | 0:bb348c97df44 | 524 | /************************ Formatting the header block *********************/ |
lypinator | 0:bb348c97df44 | 525 | if(headersize != 0U) |
lypinator | 0:bb348c97df44 | 526 | { |
lypinator | 0:bb348c97df44 | 527 | /* Check that the associated data (or header) length is lower than 2^16 - 2^8 = 65536 - 256 = 65280 */ |
lypinator | 0:bb348c97df44 | 528 | if(headersize < 65280U) |
lypinator | 0:bb348c97df44 | 529 | { |
lypinator | 0:bb348c97df44 | 530 | hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize >> 8) & 0xFF); |
lypinator | 0:bb348c97df44 | 531 | hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize) & 0xFF); |
lypinator | 0:bb348c97df44 | 532 | headersize += 2U; |
lypinator | 0:bb348c97df44 | 533 | } |
lypinator | 0:bb348c97df44 | 534 | else |
lypinator | 0:bb348c97df44 | 535 | { |
lypinator | 0:bb348c97df44 | 536 | /* Header is encoded as 0xff || 0xfe || [headersize]32, i.e., six octets */ |
lypinator | 0:bb348c97df44 | 537 | hcryp->Init.pScratch[bufferidx++] = 0xFFU; |
lypinator | 0:bb348c97df44 | 538 | hcryp->Init.pScratch[bufferidx++] = 0xFEU; |
lypinator | 0:bb348c97df44 | 539 | hcryp->Init.pScratch[bufferidx++] = headersize & 0xff000000U; |
lypinator | 0:bb348c97df44 | 540 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x00ff0000U; |
lypinator | 0:bb348c97df44 | 541 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x0000ff00U; |
lypinator | 0:bb348c97df44 | 542 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x000000ffU; |
lypinator | 0:bb348c97df44 | 543 | headersize += 6U; |
lypinator | 0:bb348c97df44 | 544 | } |
lypinator | 0:bb348c97df44 | 545 | /* Copy the header buffer in internal buffer "hcryp->Init.pScratch" */ |
lypinator | 0:bb348c97df44 | 546 | for(loopcounter = 0U; loopcounter < headersize; loopcounter++) |
lypinator | 0:bb348c97df44 | 547 | { |
lypinator | 0:bb348c97df44 | 548 | hcryp->Init.pScratch[bufferidx++] = hcryp->Init.Header[loopcounter]; |
lypinator | 0:bb348c97df44 | 549 | } |
lypinator | 0:bb348c97df44 | 550 | /* Check if the header size is modulo 16 */ |
lypinator | 0:bb348c97df44 | 551 | if ((headersize % 16U) != 0U) |
lypinator | 0:bb348c97df44 | 552 | { |
lypinator | 0:bb348c97df44 | 553 | /* Padd the header buffer with 0s till the hcryp->Init.pScratch length is modulo 16 */ |
lypinator | 0:bb348c97df44 | 554 | for(loopcounter = headersize; loopcounter <= ((headersize/16U) + 1U) * 16U; loopcounter++) |
lypinator | 0:bb348c97df44 | 555 | { |
lypinator | 0:bb348c97df44 | 556 | hcryp->Init.pScratch[loopcounter] = 0U; |
lypinator | 0:bb348c97df44 | 557 | } |
lypinator | 0:bb348c97df44 | 558 | /* Set the header size to modulo 16 */ |
lypinator | 0:bb348c97df44 | 559 | headersize = ((headersize/16U) + 1U) * 16U; |
lypinator | 0:bb348c97df44 | 560 | } |
lypinator | 0:bb348c97df44 | 561 | /* Set the pointer headeraddr to hcryp->Init.pScratch */ |
lypinator | 0:bb348c97df44 | 562 | headeraddr = (uint32_t)hcryp->Init.pScratch; |
lypinator | 0:bb348c97df44 | 563 | } |
lypinator | 0:bb348c97df44 | 564 | /*********************** Formatting the block B0 **************************/ |
lypinator | 0:bb348c97df44 | 565 | if(headersize != 0U) |
lypinator | 0:bb348c97df44 | 566 | { |
lypinator | 0:bb348c97df44 | 567 | blockb0[0U] = 0x40U; |
lypinator | 0:bb348c97df44 | 568 | } |
lypinator | 0:bb348c97df44 | 569 | /* Flags byte */ |
lypinator | 0:bb348c97df44 | 570 | /* blockb0[0] |= 0u | (((( (uint8_t) hcryp->Init.TagSize - 2) / 2) & 0x07 ) << 3 ) | ( ( (uint8_t) (15 - hcryp->Init.IVSize) - 1) & 0x07U) */ |
lypinator | 0:bb348c97df44 | 571 | blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)(((uint8_t)(hcryp->Init.TagSize - (uint8_t)(2))) >> 1U) & (uint8_t)0x07) << 3U); |
lypinator | 0:bb348c97df44 | 572 | blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)((uint8_t)(15) - hcryp->Init.IVSize) - (uint8_t)1) & (uint8_t)0x07); |
lypinator | 0:bb348c97df44 | 573 | |
lypinator | 0:bb348c97df44 | 574 | for (loopcounter = 0U; loopcounter < hcryp->Init.IVSize; loopcounter++) |
lypinator | 0:bb348c97df44 | 575 | { |
lypinator | 0:bb348c97df44 | 576 | blockb0[loopcounter+1U] = hcryp->Init.pInitVect[loopcounter]; |
lypinator | 0:bb348c97df44 | 577 | } |
lypinator | 0:bb348c97df44 | 578 | for ( ; loopcounter < 13U; loopcounter++) |
lypinator | 0:bb348c97df44 | 579 | { |
lypinator | 0:bb348c97df44 | 580 | blockb0[loopcounter+1U] = 0U; |
lypinator | 0:bb348c97df44 | 581 | } |
lypinator | 0:bb348c97df44 | 582 | |
lypinator | 0:bb348c97df44 | 583 | blockb0[14U] = (Size >> 8U); |
lypinator | 0:bb348c97df44 | 584 | blockb0[15U] = (Size & 0xFFU); |
lypinator | 0:bb348c97df44 | 585 | |
lypinator | 0:bb348c97df44 | 586 | /************************* Formatting the initial counter *****************/ |
lypinator | 0:bb348c97df44 | 587 | /* Byte 0: |
lypinator | 0:bb348c97df44 | 588 | Bits 7 and 6 are reserved and shall be set to 0 |
lypinator | 0:bb348c97df44 | 589 | Bits 3, 4, and 5 shall also be set to 0, to ensure that all the counter blocks |
lypinator | 0:bb348c97df44 | 590 | are distinct from B0 |
lypinator | 0:bb348c97df44 | 591 | Bits 0, 1, and 2 contain the same encoding of q as in B0 |
lypinator | 0:bb348c97df44 | 592 | */ |
lypinator | 0:bb348c97df44 | 593 | ctr[0U] = blockb0[0U] & 0x07U; |
lypinator | 0:bb348c97df44 | 594 | /* byte 1 to NonceSize is the IV (Nonce) */ |
lypinator | 0:bb348c97df44 | 595 | for(loopcounter = 1U; loopcounter < hcryp->Init.IVSize + 1U; loopcounter++) |
lypinator | 0:bb348c97df44 | 596 | { |
lypinator | 0:bb348c97df44 | 597 | ctr[loopcounter] = blockb0[loopcounter]; |
lypinator | 0:bb348c97df44 | 598 | } |
lypinator | 0:bb348c97df44 | 599 | /* Set the LSB to 1 */ |
lypinator | 0:bb348c97df44 | 600 | ctr[15U] |= 0x01U; |
lypinator | 0:bb348c97df44 | 601 | |
lypinator | 0:bb348c97df44 | 602 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 603 | CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
lypinator | 0:bb348c97df44 | 604 | |
lypinator | 0:bb348c97df44 | 605 | /* Set the CRYP peripheral in AES CCM mode */ |
lypinator | 0:bb348c97df44 | 606 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CCM_ENCRYPT); |
lypinator | 0:bb348c97df44 | 607 | |
lypinator | 0:bb348c97df44 | 608 | /* Set the Initialization Vector */ |
lypinator | 0:bb348c97df44 | 609 | CRYPEx_GCMCCM_SetInitVector(hcryp, ctr); |
lypinator | 0:bb348c97df44 | 610 | |
lypinator | 0:bb348c97df44 | 611 | /* Select init phase */ |
lypinator | 0:bb348c97df44 | 612 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT); |
lypinator | 0:bb348c97df44 | 613 | |
lypinator | 0:bb348c97df44 | 614 | b0addr = (uint32_t)blockb0; |
lypinator | 0:bb348c97df44 | 615 | /* Write the blockb0 block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 616 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
lypinator | 0:bb348c97df44 | 617 | b0addr+=4U; |
lypinator | 0:bb348c97df44 | 618 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
lypinator | 0:bb348c97df44 | 619 | b0addr+=4U; |
lypinator | 0:bb348c97df44 | 620 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
lypinator | 0:bb348c97df44 | 621 | b0addr+=4U; |
lypinator | 0:bb348c97df44 | 622 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
lypinator | 0:bb348c97df44 | 623 | |
lypinator | 0:bb348c97df44 | 624 | /* Enable the CRYP peripheral */ |
lypinator | 0:bb348c97df44 | 625 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 626 | |
lypinator | 0:bb348c97df44 | 627 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 628 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 629 | |
lypinator | 0:bb348c97df44 | 630 | while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN) |
lypinator | 0:bb348c97df44 | 631 | { |
lypinator | 0:bb348c97df44 | 632 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 633 | if(Timeout != HAL_MAX_DELAY) |
lypinator | 0:bb348c97df44 | 634 | { |
lypinator | 0:bb348c97df44 | 635 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
lypinator | 0:bb348c97df44 | 636 | { |
lypinator | 0:bb348c97df44 | 637 | /* Change state */ |
lypinator | 0:bb348c97df44 | 638 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 639 | |
lypinator | 0:bb348c97df44 | 640 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 641 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 642 | |
lypinator | 0:bb348c97df44 | 643 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 644 | } |
lypinator | 0:bb348c97df44 | 645 | } |
lypinator | 0:bb348c97df44 | 646 | } |
lypinator | 0:bb348c97df44 | 647 | /***************************** Header phase *******************************/ |
lypinator | 0:bb348c97df44 | 648 | if(headersize != 0U) |
lypinator | 0:bb348c97df44 | 649 | { |
lypinator | 0:bb348c97df44 | 650 | /* Select header phase */ |
lypinator | 0:bb348c97df44 | 651 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER); |
lypinator | 0:bb348c97df44 | 652 | |
lypinator | 0:bb348c97df44 | 653 | /* Enable the CRYP peripheral */ |
lypinator | 0:bb348c97df44 | 654 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 655 | |
lypinator | 0:bb348c97df44 | 656 | for(loopcounter = 0U; (loopcounter < headersize); loopcounter+=16U) |
lypinator | 0:bb348c97df44 | 657 | { |
lypinator | 0:bb348c97df44 | 658 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 659 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 660 | |
lypinator | 0:bb348c97df44 | 661 | while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM)) |
lypinator | 0:bb348c97df44 | 662 | { |
lypinator | 0:bb348c97df44 | 663 | { |
lypinator | 0:bb348c97df44 | 664 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 665 | if(Timeout != HAL_MAX_DELAY) |
lypinator | 0:bb348c97df44 | 666 | { |
lypinator | 0:bb348c97df44 | 667 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
lypinator | 0:bb348c97df44 | 668 | { |
lypinator | 0:bb348c97df44 | 669 | /* Change state */ |
lypinator | 0:bb348c97df44 | 670 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 671 | |
lypinator | 0:bb348c97df44 | 672 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 673 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 674 | |
lypinator | 0:bb348c97df44 | 675 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 676 | } |
lypinator | 0:bb348c97df44 | 677 | } |
lypinator | 0:bb348c97df44 | 678 | } |
lypinator | 0:bb348c97df44 | 679 | } |
lypinator | 0:bb348c97df44 | 680 | /* Write the header block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 681 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
lypinator | 0:bb348c97df44 | 682 | headeraddr+=4U; |
lypinator | 0:bb348c97df44 | 683 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
lypinator | 0:bb348c97df44 | 684 | headeraddr+=4U; |
lypinator | 0:bb348c97df44 | 685 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
lypinator | 0:bb348c97df44 | 686 | headeraddr+=4U; |
lypinator | 0:bb348c97df44 | 687 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
lypinator | 0:bb348c97df44 | 688 | headeraddr+=4U; |
lypinator | 0:bb348c97df44 | 689 | } |
lypinator | 0:bb348c97df44 | 690 | |
lypinator | 0:bb348c97df44 | 691 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 692 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 693 | |
lypinator | 0:bb348c97df44 | 694 | while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY) |
lypinator | 0:bb348c97df44 | 695 | { |
lypinator | 0:bb348c97df44 | 696 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 697 | if(Timeout != HAL_MAX_DELAY) |
lypinator | 0:bb348c97df44 | 698 | { |
lypinator | 0:bb348c97df44 | 699 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
lypinator | 0:bb348c97df44 | 700 | { |
lypinator | 0:bb348c97df44 | 701 | /* Change state */ |
lypinator | 0:bb348c97df44 | 702 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 703 | |
lypinator | 0:bb348c97df44 | 704 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 705 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 706 | |
lypinator | 0:bb348c97df44 | 707 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 708 | } |
lypinator | 0:bb348c97df44 | 709 | } |
lypinator | 0:bb348c97df44 | 710 | } |
lypinator | 0:bb348c97df44 | 711 | } |
lypinator | 0:bb348c97df44 | 712 | /* Save formatted counter into the scratch buffer pScratch */ |
lypinator | 0:bb348c97df44 | 713 | for(loopcounter = 0U; (loopcounter < 16U); loopcounter++) |
lypinator | 0:bb348c97df44 | 714 | { |
lypinator | 0:bb348c97df44 | 715 | hcryp->Init.pScratch[loopcounter] = ctr[loopcounter]; |
lypinator | 0:bb348c97df44 | 716 | } |
lypinator | 0:bb348c97df44 | 717 | /* Reset bit 0 */ |
lypinator | 0:bb348c97df44 | 718 | hcryp->Init.pScratch[15U] &= 0xFEU; |
lypinator | 0:bb348c97df44 | 719 | |
lypinator | 0:bb348c97df44 | 720 | /* Select payload phase once the header phase is performed */ |
lypinator | 0:bb348c97df44 | 721 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD); |
lypinator | 0:bb348c97df44 | 722 | |
lypinator | 0:bb348c97df44 | 723 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 724 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 725 | |
lypinator | 0:bb348c97df44 | 726 | /* Enable the CRYP peripheral */ |
lypinator | 0:bb348c97df44 | 727 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 728 | |
lypinator | 0:bb348c97df44 | 729 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 730 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 731 | } |
lypinator | 0:bb348c97df44 | 732 | |
lypinator | 0:bb348c97df44 | 733 | /* Write Plain Data and Get Cypher Data */ |
lypinator | 0:bb348c97df44 | 734 | if(CRYPEx_GCMCCM_ProcessData(hcryp,pPlainData, Size, pCypherData, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 735 | { |
lypinator | 0:bb348c97df44 | 736 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 737 | } |
lypinator | 0:bb348c97df44 | 738 | |
lypinator | 0:bb348c97df44 | 739 | /* Change the CRYP peripheral state */ |
lypinator | 0:bb348c97df44 | 740 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 741 | |
lypinator | 0:bb348c97df44 | 742 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 743 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 744 | |
lypinator | 0:bb348c97df44 | 745 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 746 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 747 | } |
lypinator | 0:bb348c97df44 | 748 | |
lypinator | 0:bb348c97df44 | 749 | /** |
lypinator | 0:bb348c97df44 | 750 | * @brief Initializes the CRYP peripheral in AES GCM encryption mode then |
lypinator | 0:bb348c97df44 | 751 | * encrypt pPlainData. The cypher data are available in pCypherData. |
lypinator | 0:bb348c97df44 | 752 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 753 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 754 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 755 | * @param Size Length of the plaintext buffer, must be a multiple of 16 |
lypinator | 0:bb348c97df44 | 756 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 757 | * @param Timeout Timeout duration |
lypinator | 0:bb348c97df44 | 758 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 759 | */ |
lypinator | 0:bb348c97df44 | 760 | HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 761 | { |
lypinator | 0:bb348c97df44 | 762 | uint32_t tickstart = 0U; |
lypinator | 0:bb348c97df44 | 763 | |
lypinator | 0:bb348c97df44 | 764 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 765 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 766 | |
lypinator | 0:bb348c97df44 | 767 | /* Change the CRYP peripheral state */ |
lypinator | 0:bb348c97df44 | 768 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 769 | |
lypinator | 0:bb348c97df44 | 770 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 771 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 772 | { |
lypinator | 0:bb348c97df44 | 773 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 774 | CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
lypinator | 0:bb348c97df44 | 775 | |
lypinator | 0:bb348c97df44 | 776 | /* Set the CRYP peripheral in AES GCM mode */ |
lypinator | 0:bb348c97df44 | 777 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_GCM_ENCRYPT); |
lypinator | 0:bb348c97df44 | 778 | |
lypinator | 0:bb348c97df44 | 779 | /* Set the Initialization Vector */ |
lypinator | 0:bb348c97df44 | 780 | CRYPEx_GCMCCM_SetInitVector(hcryp, hcryp->Init.pInitVect); |
lypinator | 0:bb348c97df44 | 781 | |
lypinator | 0:bb348c97df44 | 782 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 783 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 784 | |
lypinator | 0:bb348c97df44 | 785 | /* Enable the CRYP peripheral */ |
lypinator | 0:bb348c97df44 | 786 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 787 | |
lypinator | 0:bb348c97df44 | 788 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 789 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 790 | |
lypinator | 0:bb348c97df44 | 791 | while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN) |
lypinator | 0:bb348c97df44 | 792 | { |
lypinator | 0:bb348c97df44 | 793 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 794 | if(Timeout != HAL_MAX_DELAY) |
lypinator | 0:bb348c97df44 | 795 | { |
lypinator | 0:bb348c97df44 | 796 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
lypinator | 0:bb348c97df44 | 797 | { |
lypinator | 0:bb348c97df44 | 798 | /* Change state */ |
lypinator | 0:bb348c97df44 | 799 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 800 | |
lypinator | 0:bb348c97df44 | 801 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 802 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 803 | |
lypinator | 0:bb348c97df44 | 804 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 805 | } |
lypinator | 0:bb348c97df44 | 806 | } |
lypinator | 0:bb348c97df44 | 807 | } |
lypinator | 0:bb348c97df44 | 808 | |
lypinator | 0:bb348c97df44 | 809 | /* Set the header phase */ |
lypinator | 0:bb348c97df44 | 810 | if(CRYPEx_GCMCCM_SetHeaderPhase(hcryp, hcryp->Init.Header, hcryp->Init.HeaderSize, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 811 | { |
lypinator | 0:bb348c97df44 | 812 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 813 | } |
lypinator | 0:bb348c97df44 | 814 | |
lypinator | 0:bb348c97df44 | 815 | /* Disable the CRYP peripheral */ |
lypinator | 0:bb348c97df44 | 816 | __HAL_CRYP_DISABLE(hcryp); |
lypinator | 0:bb348c97df44 | 817 | |
lypinator | 0:bb348c97df44 | 818 | /* Select payload phase once the header phase is performed */ |
lypinator | 0:bb348c97df44 | 819 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD); |
lypinator | 0:bb348c97df44 | 820 | |
lypinator | 0:bb348c97df44 | 821 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 822 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 823 | |
lypinator | 0:bb348c97df44 | 824 | /* Enable the CRYP peripheral */ |
lypinator | 0:bb348c97df44 | 825 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 826 | |
lypinator | 0:bb348c97df44 | 827 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 828 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 829 | } |
lypinator | 0:bb348c97df44 | 830 | |
lypinator | 0:bb348c97df44 | 831 | /* Write Plain Data and Get Cypher Data */ |
lypinator | 0:bb348c97df44 | 832 | if(CRYPEx_GCMCCM_ProcessData(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 833 | { |
lypinator | 0:bb348c97df44 | 834 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 835 | } |
lypinator | 0:bb348c97df44 | 836 | |
lypinator | 0:bb348c97df44 | 837 | /* Change the CRYP peripheral state */ |
lypinator | 0:bb348c97df44 | 838 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 839 | |
lypinator | 0:bb348c97df44 | 840 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 841 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 842 | |
lypinator | 0:bb348c97df44 | 843 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 844 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 845 | } |
lypinator | 0:bb348c97df44 | 846 | |
lypinator | 0:bb348c97df44 | 847 | /** |
lypinator | 0:bb348c97df44 | 848 | * @brief Initializes the CRYP peripheral in AES GCM decryption mode then |
lypinator | 0:bb348c97df44 | 849 | * decrypted pCypherData. The cypher data are available in pPlainData. |
lypinator | 0:bb348c97df44 | 850 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 851 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 852 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 853 | * @param Size Length of the cyphertext buffer, must be a multiple of 16 |
lypinator | 0:bb348c97df44 | 854 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 855 | * @param Timeout Timeout duration |
lypinator | 0:bb348c97df44 | 856 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 857 | */ |
lypinator | 0:bb348c97df44 | 858 | HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 859 | { |
lypinator | 0:bb348c97df44 | 860 | uint32_t tickstart = 0U; |
lypinator | 0:bb348c97df44 | 861 | |
lypinator | 0:bb348c97df44 | 862 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 863 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 864 | |
lypinator | 0:bb348c97df44 | 865 | /* Change the CRYP peripheral state */ |
lypinator | 0:bb348c97df44 | 866 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 867 | |
lypinator | 0:bb348c97df44 | 868 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 869 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 870 | { |
lypinator | 0:bb348c97df44 | 871 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 872 | CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
lypinator | 0:bb348c97df44 | 873 | |
lypinator | 0:bb348c97df44 | 874 | /* Set the CRYP peripheral in AES GCM decryption mode */ |
lypinator | 0:bb348c97df44 | 875 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_GCM_DECRYPT); |
lypinator | 0:bb348c97df44 | 876 | |
lypinator | 0:bb348c97df44 | 877 | /* Set the Initialization Vector */ |
lypinator | 0:bb348c97df44 | 878 | CRYPEx_GCMCCM_SetInitVector(hcryp, hcryp->Init.pInitVect); |
lypinator | 0:bb348c97df44 | 879 | |
lypinator | 0:bb348c97df44 | 880 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 881 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 882 | |
lypinator | 0:bb348c97df44 | 883 | /* Enable the CRYP peripheral */ |
lypinator | 0:bb348c97df44 | 884 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 885 | |
lypinator | 0:bb348c97df44 | 886 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 887 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 888 | |
lypinator | 0:bb348c97df44 | 889 | while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN) |
lypinator | 0:bb348c97df44 | 890 | { |
lypinator | 0:bb348c97df44 | 891 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 892 | if(Timeout != HAL_MAX_DELAY) |
lypinator | 0:bb348c97df44 | 893 | { |
lypinator | 0:bb348c97df44 | 894 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
lypinator | 0:bb348c97df44 | 895 | { |
lypinator | 0:bb348c97df44 | 896 | /* Change state */ |
lypinator | 0:bb348c97df44 | 897 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 898 | |
lypinator | 0:bb348c97df44 | 899 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 900 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 901 | |
lypinator | 0:bb348c97df44 | 902 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 903 | } |
lypinator | 0:bb348c97df44 | 904 | } |
lypinator | 0:bb348c97df44 | 905 | } |
lypinator | 0:bb348c97df44 | 906 | |
lypinator | 0:bb348c97df44 | 907 | /* Set the header phase */ |
lypinator | 0:bb348c97df44 | 908 | if(CRYPEx_GCMCCM_SetHeaderPhase(hcryp, hcryp->Init.Header, hcryp->Init.HeaderSize, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 909 | { |
lypinator | 0:bb348c97df44 | 910 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 911 | } |
lypinator | 0:bb348c97df44 | 912 | /* Disable the CRYP peripheral */ |
lypinator | 0:bb348c97df44 | 913 | __HAL_CRYP_DISABLE(hcryp); |
lypinator | 0:bb348c97df44 | 914 | |
lypinator | 0:bb348c97df44 | 915 | /* Select payload phase once the header phase is performed */ |
lypinator | 0:bb348c97df44 | 916 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD); |
lypinator | 0:bb348c97df44 | 917 | |
lypinator | 0:bb348c97df44 | 918 | /* Enable the CRYP peripheral */ |
lypinator | 0:bb348c97df44 | 919 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 920 | |
lypinator | 0:bb348c97df44 | 921 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 922 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 923 | } |
lypinator | 0:bb348c97df44 | 924 | |
lypinator | 0:bb348c97df44 | 925 | /* Write Plain Data and Get Cypher Data */ |
lypinator | 0:bb348c97df44 | 926 | if(CRYPEx_GCMCCM_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 927 | { |
lypinator | 0:bb348c97df44 | 928 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 929 | } |
lypinator | 0:bb348c97df44 | 930 | |
lypinator | 0:bb348c97df44 | 931 | /* Change the CRYP peripheral state */ |
lypinator | 0:bb348c97df44 | 932 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 933 | |
lypinator | 0:bb348c97df44 | 934 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 935 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 936 | |
lypinator | 0:bb348c97df44 | 937 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 938 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 939 | } |
lypinator | 0:bb348c97df44 | 940 | |
lypinator | 0:bb348c97df44 | 941 | /** |
lypinator | 0:bb348c97df44 | 942 | * @brief Computes the authentication TAG. |
lypinator | 0:bb348c97df44 | 943 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 944 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 945 | * @param Size Total length of the plain/cyphertext buffer |
lypinator | 0:bb348c97df44 | 946 | * @param AuthTag Pointer to the authentication buffer |
lypinator | 0:bb348c97df44 | 947 | * @param Timeout Timeout duration |
lypinator | 0:bb348c97df44 | 948 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 949 | */ |
lypinator | 0:bb348c97df44 | 950 | HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Finish(CRYP_HandleTypeDef *hcryp, uint32_t Size, uint8_t *AuthTag, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 951 | { |
lypinator | 0:bb348c97df44 | 952 | uint32_t tickstart = 0U; |
lypinator | 0:bb348c97df44 | 953 | uint64_t headerlength = hcryp->Init.HeaderSize * 8U; /* Header length in bits */ |
lypinator | 0:bb348c97df44 | 954 | uint64_t inputlength = Size * 8U; /* input length in bits */ |
lypinator | 0:bb348c97df44 | 955 | uint32_t tagaddr = (uint32_t)AuthTag; |
lypinator | 0:bb348c97df44 | 956 | |
lypinator | 0:bb348c97df44 | 957 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 958 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 959 | |
lypinator | 0:bb348c97df44 | 960 | /* Change the CRYP peripheral state */ |
lypinator | 0:bb348c97df44 | 961 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 962 | |
lypinator | 0:bb348c97df44 | 963 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 964 | if(hcryp->Phase == HAL_CRYP_PHASE_PROCESS) |
lypinator | 0:bb348c97df44 | 965 | { |
lypinator | 0:bb348c97df44 | 966 | /* Change the CRYP phase */ |
lypinator | 0:bb348c97df44 | 967 | hcryp->Phase = HAL_CRYP_PHASE_FINAL; |
lypinator | 0:bb348c97df44 | 968 | |
lypinator | 0:bb348c97df44 | 969 | /* Disable CRYP to start the final phase */ |
lypinator | 0:bb348c97df44 | 970 | __HAL_CRYP_DISABLE(hcryp); |
lypinator | 0:bb348c97df44 | 971 | |
lypinator | 0:bb348c97df44 | 972 | /* Select final phase */ |
lypinator | 0:bb348c97df44 | 973 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_FINAL); |
lypinator | 0:bb348c97df44 | 974 | |
lypinator | 0:bb348c97df44 | 975 | /* Enable the CRYP peripheral */ |
lypinator | 0:bb348c97df44 | 976 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 977 | |
lypinator | 0:bb348c97df44 | 978 | /* Write the number of bits in header (64 bits) followed by the number of bits |
lypinator | 0:bb348c97df44 | 979 | in the payload */ |
lypinator | 0:bb348c97df44 | 980 | if(hcryp->Init.DataType == CRYP_DATATYPE_1B) |
lypinator | 0:bb348c97df44 | 981 | { |
lypinator | 0:bb348c97df44 | 982 | hcryp->Instance->DR = __RBIT(headerlength >> 32U); |
lypinator | 0:bb348c97df44 | 983 | hcryp->Instance->DR = __RBIT(headerlength); |
lypinator | 0:bb348c97df44 | 984 | hcryp->Instance->DR = __RBIT(inputlength >> 32U); |
lypinator | 0:bb348c97df44 | 985 | hcryp->Instance->DR = __RBIT(inputlength); |
lypinator | 0:bb348c97df44 | 986 | } |
lypinator | 0:bb348c97df44 | 987 | else if(hcryp->Init.DataType == CRYP_DATATYPE_8B) |
lypinator | 0:bb348c97df44 | 988 | { |
lypinator | 0:bb348c97df44 | 989 | hcryp->Instance->DR = __REV(headerlength >> 32U); |
lypinator | 0:bb348c97df44 | 990 | hcryp->Instance->DR = __REV(headerlength); |
lypinator | 0:bb348c97df44 | 991 | hcryp->Instance->DR = __REV(inputlength >> 32U); |
lypinator | 0:bb348c97df44 | 992 | hcryp->Instance->DR = __REV(inputlength); |
lypinator | 0:bb348c97df44 | 993 | } |
lypinator | 0:bb348c97df44 | 994 | else if(hcryp->Init.DataType == CRYP_DATATYPE_16B) |
lypinator | 0:bb348c97df44 | 995 | { |
lypinator | 0:bb348c97df44 | 996 | hcryp->Instance->DR = __ROR((uint32_t)(headerlength >> 32U), 16U); |
lypinator | 0:bb348c97df44 | 997 | hcryp->Instance->DR = __ROR((uint32_t)headerlength, 16U); |
lypinator | 0:bb348c97df44 | 998 | hcryp->Instance->DR = __ROR((uint32_t)(inputlength >> 32U), 16U); |
lypinator | 0:bb348c97df44 | 999 | hcryp->Instance->DR = __ROR((uint32_t)inputlength, 16U); |
lypinator | 0:bb348c97df44 | 1000 | } |
lypinator | 0:bb348c97df44 | 1001 | else if(hcryp->Init.DataType == CRYP_DATATYPE_32B) |
lypinator | 0:bb348c97df44 | 1002 | { |
lypinator | 0:bb348c97df44 | 1003 | hcryp->Instance->DR = (uint32_t)(headerlength >> 32U); |
lypinator | 0:bb348c97df44 | 1004 | hcryp->Instance->DR = (uint32_t)(headerlength); |
lypinator | 0:bb348c97df44 | 1005 | hcryp->Instance->DR = (uint32_t)(inputlength >> 32U); |
lypinator | 0:bb348c97df44 | 1006 | hcryp->Instance->DR = (uint32_t)(inputlength); |
lypinator | 0:bb348c97df44 | 1007 | } |
lypinator | 0:bb348c97df44 | 1008 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 1009 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 1010 | |
lypinator | 0:bb348c97df44 | 1011 | while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE)) |
lypinator | 0:bb348c97df44 | 1012 | { |
lypinator | 0:bb348c97df44 | 1013 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 1014 | if(Timeout != HAL_MAX_DELAY) |
lypinator | 0:bb348c97df44 | 1015 | { |
lypinator | 0:bb348c97df44 | 1016 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
lypinator | 0:bb348c97df44 | 1017 | { |
lypinator | 0:bb348c97df44 | 1018 | /* Change state */ |
lypinator | 0:bb348c97df44 | 1019 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1020 | |
lypinator | 0:bb348c97df44 | 1021 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1022 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1023 | |
lypinator | 0:bb348c97df44 | 1024 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1025 | } |
lypinator | 0:bb348c97df44 | 1026 | } |
lypinator | 0:bb348c97df44 | 1027 | } |
lypinator | 0:bb348c97df44 | 1028 | |
lypinator | 0:bb348c97df44 | 1029 | /* Read the Auth TAG in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 1030 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1031 | tagaddr+=4U; |
lypinator | 0:bb348c97df44 | 1032 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1033 | tagaddr+=4U; |
lypinator | 0:bb348c97df44 | 1034 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1035 | tagaddr+=4U; |
lypinator | 0:bb348c97df44 | 1036 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1037 | } |
lypinator | 0:bb348c97df44 | 1038 | |
lypinator | 0:bb348c97df44 | 1039 | /* Change the CRYP peripheral state */ |
lypinator | 0:bb348c97df44 | 1040 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 1041 | |
lypinator | 0:bb348c97df44 | 1042 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1043 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1044 | |
lypinator | 0:bb348c97df44 | 1045 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 1046 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1047 | } |
lypinator | 0:bb348c97df44 | 1048 | |
lypinator | 0:bb348c97df44 | 1049 | /** |
lypinator | 0:bb348c97df44 | 1050 | * @brief Computes the authentication TAG for AES CCM mode. |
lypinator | 0:bb348c97df44 | 1051 | * @note This API is called after HAL_AES_CCM_Encrypt()/HAL_AES_CCM_Decrypt() |
lypinator | 0:bb348c97df44 | 1052 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1053 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 1054 | * @param AuthTag Pointer to the authentication buffer |
lypinator | 0:bb348c97df44 | 1055 | * @param Timeout Timeout duration |
lypinator | 0:bb348c97df44 | 1056 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 1057 | */ |
lypinator | 0:bb348c97df44 | 1058 | HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Finish(CRYP_HandleTypeDef *hcryp, uint8_t *AuthTag, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 1059 | { |
lypinator | 0:bb348c97df44 | 1060 | uint32_t tickstart = 0U; |
lypinator | 0:bb348c97df44 | 1061 | uint32_t tagaddr = (uint32_t)AuthTag; |
lypinator | 0:bb348c97df44 | 1062 | uint32_t ctraddr = (uint32_t)hcryp->Init.pScratch; |
lypinator | 0:bb348c97df44 | 1063 | uint32_t temptag[4U] = {0U}; /* Temporary TAG (MAC) */ |
lypinator | 0:bb348c97df44 | 1064 | uint32_t loopcounter; |
lypinator | 0:bb348c97df44 | 1065 | |
lypinator | 0:bb348c97df44 | 1066 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 1067 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1068 | |
lypinator | 0:bb348c97df44 | 1069 | /* Change the CRYP peripheral state */ |
lypinator | 0:bb348c97df44 | 1070 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 1071 | |
lypinator | 0:bb348c97df44 | 1072 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 1073 | if(hcryp->Phase == HAL_CRYP_PHASE_PROCESS) |
lypinator | 0:bb348c97df44 | 1074 | { |
lypinator | 0:bb348c97df44 | 1075 | /* Change the CRYP phase */ |
lypinator | 0:bb348c97df44 | 1076 | hcryp->Phase = HAL_CRYP_PHASE_FINAL; |
lypinator | 0:bb348c97df44 | 1077 | |
lypinator | 0:bb348c97df44 | 1078 | /* Disable CRYP to start the final phase */ |
lypinator | 0:bb348c97df44 | 1079 | __HAL_CRYP_DISABLE(hcryp); |
lypinator | 0:bb348c97df44 | 1080 | |
lypinator | 0:bb348c97df44 | 1081 | /* Select final phase */ |
lypinator | 0:bb348c97df44 | 1082 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_FINAL); |
lypinator | 0:bb348c97df44 | 1083 | |
lypinator | 0:bb348c97df44 | 1084 | /* Enable the CRYP peripheral */ |
lypinator | 0:bb348c97df44 | 1085 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 1086 | |
lypinator | 0:bb348c97df44 | 1087 | /* Write the counter block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 1088 | hcryp->Instance->DR = *(uint32_t*)ctraddr; |
lypinator | 0:bb348c97df44 | 1089 | ctraddr+=4U; |
lypinator | 0:bb348c97df44 | 1090 | hcryp->Instance->DR = *(uint32_t*)ctraddr; |
lypinator | 0:bb348c97df44 | 1091 | ctraddr+=4U; |
lypinator | 0:bb348c97df44 | 1092 | hcryp->Instance->DR = *(uint32_t*)ctraddr; |
lypinator | 0:bb348c97df44 | 1093 | ctraddr+=4U; |
lypinator | 0:bb348c97df44 | 1094 | hcryp->Instance->DR = *(uint32_t*)ctraddr; |
lypinator | 0:bb348c97df44 | 1095 | |
lypinator | 0:bb348c97df44 | 1096 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 1097 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 1098 | |
lypinator | 0:bb348c97df44 | 1099 | while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE)) |
lypinator | 0:bb348c97df44 | 1100 | { |
lypinator | 0:bb348c97df44 | 1101 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 1102 | if(Timeout != HAL_MAX_DELAY) |
lypinator | 0:bb348c97df44 | 1103 | { |
lypinator | 0:bb348c97df44 | 1104 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
lypinator | 0:bb348c97df44 | 1105 | { |
lypinator | 0:bb348c97df44 | 1106 | /* Change state */ |
lypinator | 0:bb348c97df44 | 1107 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1108 | |
lypinator | 0:bb348c97df44 | 1109 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1110 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1111 | |
lypinator | 0:bb348c97df44 | 1112 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1113 | } |
lypinator | 0:bb348c97df44 | 1114 | } |
lypinator | 0:bb348c97df44 | 1115 | } |
lypinator | 0:bb348c97df44 | 1116 | |
lypinator | 0:bb348c97df44 | 1117 | /* Read the Auth TAG in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 1118 | temptag[0U] = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1119 | temptag[1U] = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1120 | temptag[2U] = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1121 | temptag[3U] = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1122 | } |
lypinator | 0:bb348c97df44 | 1123 | |
lypinator | 0:bb348c97df44 | 1124 | /* Copy temporary authentication TAG in user TAG buffer */ |
lypinator | 0:bb348c97df44 | 1125 | for(loopcounter = 0U; loopcounter < hcryp->Init.TagSize ; loopcounter++) |
lypinator | 0:bb348c97df44 | 1126 | { |
lypinator | 0:bb348c97df44 | 1127 | /* Set the authentication TAG buffer */ |
lypinator | 0:bb348c97df44 | 1128 | *((uint8_t*)tagaddr+loopcounter) = *((uint8_t*)temptag+loopcounter); |
lypinator | 0:bb348c97df44 | 1129 | } |
lypinator | 0:bb348c97df44 | 1130 | |
lypinator | 0:bb348c97df44 | 1131 | /* Change the CRYP peripheral state */ |
lypinator | 0:bb348c97df44 | 1132 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 1133 | |
lypinator | 0:bb348c97df44 | 1134 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1135 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1136 | |
lypinator | 0:bb348c97df44 | 1137 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 1138 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1139 | } |
lypinator | 0:bb348c97df44 | 1140 | |
lypinator | 0:bb348c97df44 | 1141 | /** |
lypinator | 0:bb348c97df44 | 1142 | * @brief Initializes the CRYP peripheral in AES CCM decryption mode then |
lypinator | 0:bb348c97df44 | 1143 | * decrypted pCypherData. The cypher data are available in pPlainData. |
lypinator | 0:bb348c97df44 | 1144 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1145 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 1146 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 1147 | * @param Size Length of the plaintext buffer, must be a multiple of 16 |
lypinator | 0:bb348c97df44 | 1148 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 1149 | * @param Timeout Timeout duration |
lypinator | 0:bb348c97df44 | 1150 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 1151 | */ |
lypinator | 0:bb348c97df44 | 1152 | HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 1153 | { |
lypinator | 0:bb348c97df44 | 1154 | uint32_t tickstart = 0U; |
lypinator | 0:bb348c97df44 | 1155 | uint32_t headersize = hcryp->Init.HeaderSize; |
lypinator | 0:bb348c97df44 | 1156 | uint32_t headeraddr = (uint32_t)hcryp->Init.Header; |
lypinator | 0:bb348c97df44 | 1157 | uint32_t loopcounter = 0U; |
lypinator | 0:bb348c97df44 | 1158 | uint32_t bufferidx = 0U; |
lypinator | 0:bb348c97df44 | 1159 | uint8_t blockb0[16U] = {0};/* Block B0 */ |
lypinator | 0:bb348c97df44 | 1160 | uint8_t ctr[16U] = {0}; /* Counter */ |
lypinator | 0:bb348c97df44 | 1161 | uint32_t b0addr = (uint32_t)blockb0; |
lypinator | 0:bb348c97df44 | 1162 | |
lypinator | 0:bb348c97df44 | 1163 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 1164 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1165 | |
lypinator | 0:bb348c97df44 | 1166 | /* Change the CRYP peripheral state */ |
lypinator | 0:bb348c97df44 | 1167 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 1168 | |
lypinator | 0:bb348c97df44 | 1169 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 1170 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 1171 | { |
lypinator | 0:bb348c97df44 | 1172 | /************************ Formatting the header block *********************/ |
lypinator | 0:bb348c97df44 | 1173 | if(headersize != 0U) |
lypinator | 0:bb348c97df44 | 1174 | { |
lypinator | 0:bb348c97df44 | 1175 | /* Check that the associated data (or header) length is lower than 2^16 - 2^8 = 65536 - 256 = 65280 */ |
lypinator | 0:bb348c97df44 | 1176 | if(headersize < 65280U) |
lypinator | 0:bb348c97df44 | 1177 | { |
lypinator | 0:bb348c97df44 | 1178 | hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize >> 8) & 0xFF); |
lypinator | 0:bb348c97df44 | 1179 | hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize) & 0xFF); |
lypinator | 0:bb348c97df44 | 1180 | headersize += 2U; |
lypinator | 0:bb348c97df44 | 1181 | } |
lypinator | 0:bb348c97df44 | 1182 | else |
lypinator | 0:bb348c97df44 | 1183 | { |
lypinator | 0:bb348c97df44 | 1184 | /* Header is encoded as 0xff || 0xfe || [headersize]32, i.e., six octets */ |
lypinator | 0:bb348c97df44 | 1185 | hcryp->Init.pScratch[bufferidx++] = 0xFFU; |
lypinator | 0:bb348c97df44 | 1186 | hcryp->Init.pScratch[bufferidx++] = 0xFEU; |
lypinator | 0:bb348c97df44 | 1187 | hcryp->Init.pScratch[bufferidx++] = headersize & 0xff000000U; |
lypinator | 0:bb348c97df44 | 1188 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x00ff0000U; |
lypinator | 0:bb348c97df44 | 1189 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x0000ff00U; |
lypinator | 0:bb348c97df44 | 1190 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x000000ffU; |
lypinator | 0:bb348c97df44 | 1191 | headersize += 6U; |
lypinator | 0:bb348c97df44 | 1192 | } |
lypinator | 0:bb348c97df44 | 1193 | /* Copy the header buffer in internal buffer "hcryp->Init.pScratch" */ |
lypinator | 0:bb348c97df44 | 1194 | for(loopcounter = 0U; loopcounter < headersize; loopcounter++) |
lypinator | 0:bb348c97df44 | 1195 | { |
lypinator | 0:bb348c97df44 | 1196 | hcryp->Init.pScratch[bufferidx++] = hcryp->Init.Header[loopcounter]; |
lypinator | 0:bb348c97df44 | 1197 | } |
lypinator | 0:bb348c97df44 | 1198 | /* Check if the header size is modulo 16 */ |
lypinator | 0:bb348c97df44 | 1199 | if ((headersize % 16U) != 0U) |
lypinator | 0:bb348c97df44 | 1200 | { |
lypinator | 0:bb348c97df44 | 1201 | /* Padd the header buffer with 0s till the hcryp->Init.pScratch length is modulo 16 */ |
lypinator | 0:bb348c97df44 | 1202 | for(loopcounter = headersize; loopcounter <= ((headersize/16U) + 1U) * 16U; loopcounter++) |
lypinator | 0:bb348c97df44 | 1203 | { |
lypinator | 0:bb348c97df44 | 1204 | hcryp->Init.pScratch[loopcounter] = 0U; |
lypinator | 0:bb348c97df44 | 1205 | } |
lypinator | 0:bb348c97df44 | 1206 | /* Set the header size to modulo 16 */ |
lypinator | 0:bb348c97df44 | 1207 | headersize = ((headersize/16U) + 1U) * 16U; |
lypinator | 0:bb348c97df44 | 1208 | } |
lypinator | 0:bb348c97df44 | 1209 | /* Set the pointer headeraddr to hcryp->Init.pScratch */ |
lypinator | 0:bb348c97df44 | 1210 | headeraddr = (uint32_t)hcryp->Init.pScratch; |
lypinator | 0:bb348c97df44 | 1211 | } |
lypinator | 0:bb348c97df44 | 1212 | /*********************** Formatting the block B0 **************************/ |
lypinator | 0:bb348c97df44 | 1213 | if(headersize != 0U) |
lypinator | 0:bb348c97df44 | 1214 | { |
lypinator | 0:bb348c97df44 | 1215 | blockb0[0U] = 0x40U; |
lypinator | 0:bb348c97df44 | 1216 | } |
lypinator | 0:bb348c97df44 | 1217 | /* Flags byte */ |
lypinator | 0:bb348c97df44 | 1218 | /* blockb0[0] |= 0u | (((( (uint8_t) hcryp->Init.TagSize - 2) / 2) & 0x07 ) << 3 ) | ( ( (uint8_t) (15 - hcryp->Init.IVSize) - 1) & 0x07U) */ |
lypinator | 0:bb348c97df44 | 1219 | blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)(((uint8_t)(hcryp->Init.TagSize - (uint8_t)(2U))) >> 1U) & (uint8_t)0x07U) << 3U); |
lypinator | 0:bb348c97df44 | 1220 | blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)((uint8_t)(15U) - hcryp->Init.IVSize) - (uint8_t)1U) & (uint8_t)0x07U); |
lypinator | 0:bb348c97df44 | 1221 | |
lypinator | 0:bb348c97df44 | 1222 | for (loopcounter = 0U; loopcounter < hcryp->Init.IVSize; loopcounter++) |
lypinator | 0:bb348c97df44 | 1223 | { |
lypinator | 0:bb348c97df44 | 1224 | blockb0[loopcounter+1U] = hcryp->Init.pInitVect[loopcounter]; |
lypinator | 0:bb348c97df44 | 1225 | } |
lypinator | 0:bb348c97df44 | 1226 | for ( ; loopcounter < 13U; loopcounter++) |
lypinator | 0:bb348c97df44 | 1227 | { |
lypinator | 0:bb348c97df44 | 1228 | blockb0[loopcounter+1U] = 0U; |
lypinator | 0:bb348c97df44 | 1229 | } |
lypinator | 0:bb348c97df44 | 1230 | |
lypinator | 0:bb348c97df44 | 1231 | blockb0[14U] = (Size >> 8U); |
lypinator | 0:bb348c97df44 | 1232 | blockb0[15U] = (Size & 0xFFU); |
lypinator | 0:bb348c97df44 | 1233 | |
lypinator | 0:bb348c97df44 | 1234 | /************************* Formatting the initial counter *****************/ |
lypinator | 0:bb348c97df44 | 1235 | /* Byte 0: |
lypinator | 0:bb348c97df44 | 1236 | Bits 7 and 6 are reserved and shall be set to 0 |
lypinator | 0:bb348c97df44 | 1237 | Bits 3, 4, and 5 shall also be set to 0, to ensure that all the counter |
lypinator | 0:bb348c97df44 | 1238 | blocks are distinct from B0 |
lypinator | 0:bb348c97df44 | 1239 | Bits 0, 1, and 2 contain the same encoding of q as in B0 |
lypinator | 0:bb348c97df44 | 1240 | */ |
lypinator | 0:bb348c97df44 | 1241 | ctr[0U] = blockb0[0U] & 0x07U; |
lypinator | 0:bb348c97df44 | 1242 | /* byte 1 to NonceSize is the IV (Nonce) */ |
lypinator | 0:bb348c97df44 | 1243 | for(loopcounter = 1U; loopcounter < hcryp->Init.IVSize + 1U; loopcounter++) |
lypinator | 0:bb348c97df44 | 1244 | { |
lypinator | 0:bb348c97df44 | 1245 | ctr[loopcounter] = blockb0[loopcounter]; |
lypinator | 0:bb348c97df44 | 1246 | } |
lypinator | 0:bb348c97df44 | 1247 | /* Set the LSB to 1 */ |
lypinator | 0:bb348c97df44 | 1248 | ctr[15U] |= 0x01U; |
lypinator | 0:bb348c97df44 | 1249 | |
lypinator | 0:bb348c97df44 | 1250 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 1251 | CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
lypinator | 0:bb348c97df44 | 1252 | |
lypinator | 0:bb348c97df44 | 1253 | /* Set the CRYP peripheral in AES CCM mode */ |
lypinator | 0:bb348c97df44 | 1254 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CCM_DECRYPT); |
lypinator | 0:bb348c97df44 | 1255 | |
lypinator | 0:bb348c97df44 | 1256 | /* Set the Initialization Vector */ |
lypinator | 0:bb348c97df44 | 1257 | CRYPEx_GCMCCM_SetInitVector(hcryp, ctr); |
lypinator | 0:bb348c97df44 | 1258 | |
lypinator | 0:bb348c97df44 | 1259 | /* Select init phase */ |
lypinator | 0:bb348c97df44 | 1260 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT); |
lypinator | 0:bb348c97df44 | 1261 | |
lypinator | 0:bb348c97df44 | 1262 | b0addr = (uint32_t)blockb0; |
lypinator | 0:bb348c97df44 | 1263 | /* Write the blockb0 block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 1264 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
lypinator | 0:bb348c97df44 | 1265 | b0addr+=4U; |
lypinator | 0:bb348c97df44 | 1266 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
lypinator | 0:bb348c97df44 | 1267 | b0addr+=4U; |
lypinator | 0:bb348c97df44 | 1268 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
lypinator | 0:bb348c97df44 | 1269 | b0addr+=4U; |
lypinator | 0:bb348c97df44 | 1270 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
lypinator | 0:bb348c97df44 | 1271 | |
lypinator | 0:bb348c97df44 | 1272 | /* Enable the CRYP peripheral */ |
lypinator | 0:bb348c97df44 | 1273 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 1274 | |
lypinator | 0:bb348c97df44 | 1275 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 1276 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 1277 | |
lypinator | 0:bb348c97df44 | 1278 | while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN) |
lypinator | 0:bb348c97df44 | 1279 | { |
lypinator | 0:bb348c97df44 | 1280 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 1281 | if(Timeout != HAL_MAX_DELAY) |
lypinator | 0:bb348c97df44 | 1282 | { |
lypinator | 0:bb348c97df44 | 1283 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
lypinator | 0:bb348c97df44 | 1284 | { |
lypinator | 0:bb348c97df44 | 1285 | /* Change state */ |
lypinator | 0:bb348c97df44 | 1286 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1287 | |
lypinator | 0:bb348c97df44 | 1288 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1289 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1290 | |
lypinator | 0:bb348c97df44 | 1291 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1292 | } |
lypinator | 0:bb348c97df44 | 1293 | } |
lypinator | 0:bb348c97df44 | 1294 | } |
lypinator | 0:bb348c97df44 | 1295 | /***************************** Header phase *******************************/ |
lypinator | 0:bb348c97df44 | 1296 | if(headersize != 0U) |
lypinator | 0:bb348c97df44 | 1297 | { |
lypinator | 0:bb348c97df44 | 1298 | /* Select header phase */ |
lypinator | 0:bb348c97df44 | 1299 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER); |
lypinator | 0:bb348c97df44 | 1300 | |
lypinator | 0:bb348c97df44 | 1301 | /* Enable Crypto processor */ |
lypinator | 0:bb348c97df44 | 1302 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 1303 | |
lypinator | 0:bb348c97df44 | 1304 | for(loopcounter = 0U; (loopcounter < headersize); loopcounter+=16U) |
lypinator | 0:bb348c97df44 | 1305 | { |
lypinator | 0:bb348c97df44 | 1306 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 1307 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 1308 | |
lypinator | 0:bb348c97df44 | 1309 | while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM)) |
lypinator | 0:bb348c97df44 | 1310 | { |
lypinator | 0:bb348c97df44 | 1311 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 1312 | if(Timeout != HAL_MAX_DELAY) |
lypinator | 0:bb348c97df44 | 1313 | { |
lypinator | 0:bb348c97df44 | 1314 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
lypinator | 0:bb348c97df44 | 1315 | { |
lypinator | 0:bb348c97df44 | 1316 | /* Change state */ |
lypinator | 0:bb348c97df44 | 1317 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1318 | |
lypinator | 0:bb348c97df44 | 1319 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1320 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1321 | |
lypinator | 0:bb348c97df44 | 1322 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1323 | } |
lypinator | 0:bb348c97df44 | 1324 | } |
lypinator | 0:bb348c97df44 | 1325 | } |
lypinator | 0:bb348c97df44 | 1326 | /* Write the header block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 1327 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
lypinator | 0:bb348c97df44 | 1328 | headeraddr+=4U; |
lypinator | 0:bb348c97df44 | 1329 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
lypinator | 0:bb348c97df44 | 1330 | headeraddr+=4U; |
lypinator | 0:bb348c97df44 | 1331 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
lypinator | 0:bb348c97df44 | 1332 | headeraddr+=4U; |
lypinator | 0:bb348c97df44 | 1333 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
lypinator | 0:bb348c97df44 | 1334 | headeraddr+=4U; |
lypinator | 0:bb348c97df44 | 1335 | } |
lypinator | 0:bb348c97df44 | 1336 | |
lypinator | 0:bb348c97df44 | 1337 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 1338 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 1339 | |
lypinator | 0:bb348c97df44 | 1340 | while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY) |
lypinator | 0:bb348c97df44 | 1341 | { |
lypinator | 0:bb348c97df44 | 1342 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 1343 | if(Timeout != HAL_MAX_DELAY) |
lypinator | 0:bb348c97df44 | 1344 | { |
lypinator | 0:bb348c97df44 | 1345 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
lypinator | 0:bb348c97df44 | 1346 | { |
lypinator | 0:bb348c97df44 | 1347 | /* Change state */ |
lypinator | 0:bb348c97df44 | 1348 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1349 | |
lypinator | 0:bb348c97df44 | 1350 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1351 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1352 | |
lypinator | 0:bb348c97df44 | 1353 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1354 | } |
lypinator | 0:bb348c97df44 | 1355 | } |
lypinator | 0:bb348c97df44 | 1356 | } |
lypinator | 0:bb348c97df44 | 1357 | } |
lypinator | 0:bb348c97df44 | 1358 | /* Save formatted counter into the scratch buffer pScratch */ |
lypinator | 0:bb348c97df44 | 1359 | for(loopcounter = 0U; (loopcounter < 16U); loopcounter++) |
lypinator | 0:bb348c97df44 | 1360 | { |
lypinator | 0:bb348c97df44 | 1361 | hcryp->Init.pScratch[loopcounter] = ctr[loopcounter]; |
lypinator | 0:bb348c97df44 | 1362 | } |
lypinator | 0:bb348c97df44 | 1363 | /* Reset bit 0 */ |
lypinator | 0:bb348c97df44 | 1364 | hcryp->Init.pScratch[15U] &= 0xFEU; |
lypinator | 0:bb348c97df44 | 1365 | /* Select payload phase once the header phase is performed */ |
lypinator | 0:bb348c97df44 | 1366 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD); |
lypinator | 0:bb348c97df44 | 1367 | |
lypinator | 0:bb348c97df44 | 1368 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 1369 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 1370 | |
lypinator | 0:bb348c97df44 | 1371 | /* Enable the CRYP peripheral */ |
lypinator | 0:bb348c97df44 | 1372 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 1373 | |
lypinator | 0:bb348c97df44 | 1374 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 1375 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 1376 | } |
lypinator | 0:bb348c97df44 | 1377 | |
lypinator | 0:bb348c97df44 | 1378 | /* Write Plain Data and Get Cypher Data */ |
lypinator | 0:bb348c97df44 | 1379 | if(CRYPEx_GCMCCM_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 1380 | { |
lypinator | 0:bb348c97df44 | 1381 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1382 | } |
lypinator | 0:bb348c97df44 | 1383 | |
lypinator | 0:bb348c97df44 | 1384 | /* Change the CRYP peripheral state */ |
lypinator | 0:bb348c97df44 | 1385 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 1386 | |
lypinator | 0:bb348c97df44 | 1387 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1388 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1389 | |
lypinator | 0:bb348c97df44 | 1390 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 1391 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1392 | } |
lypinator | 0:bb348c97df44 | 1393 | |
lypinator | 0:bb348c97df44 | 1394 | /** |
lypinator | 0:bb348c97df44 | 1395 | * @brief Initializes the CRYP peripheral in AES GCM encryption mode using IT. |
lypinator | 0:bb348c97df44 | 1396 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1397 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 1398 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 1399 | * @param Size Length of the plaintext buffer, must be a multiple of 16 |
lypinator | 0:bb348c97df44 | 1400 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 1401 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 1402 | */ |
lypinator | 0:bb348c97df44 | 1403 | HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) |
lypinator | 0:bb348c97df44 | 1404 | { |
lypinator | 0:bb348c97df44 | 1405 | uint32_t tickstart = 0U; |
lypinator | 0:bb348c97df44 | 1406 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 1407 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 1408 | |
lypinator | 0:bb348c97df44 | 1409 | if(hcryp->State == HAL_CRYP_STATE_READY) |
lypinator | 0:bb348c97df44 | 1410 | { |
lypinator | 0:bb348c97df44 | 1411 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 1412 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1413 | |
lypinator | 0:bb348c97df44 | 1414 | /* Get the buffer addresses and sizes */ |
lypinator | 0:bb348c97df44 | 1415 | hcryp->CrypInCount = Size; |
lypinator | 0:bb348c97df44 | 1416 | hcryp->pCrypInBuffPtr = pPlainData; |
lypinator | 0:bb348c97df44 | 1417 | hcryp->pCrypOutBuffPtr = pCypherData; |
lypinator | 0:bb348c97df44 | 1418 | hcryp->CrypOutCount = Size; |
lypinator | 0:bb348c97df44 | 1419 | |
lypinator | 0:bb348c97df44 | 1420 | /* Change the CRYP peripheral state */ |
lypinator | 0:bb348c97df44 | 1421 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 1422 | |
lypinator | 0:bb348c97df44 | 1423 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 1424 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 1425 | { |
lypinator | 0:bb348c97df44 | 1426 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 1427 | CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
lypinator | 0:bb348c97df44 | 1428 | |
lypinator | 0:bb348c97df44 | 1429 | /* Set the CRYP peripheral in AES GCM mode */ |
lypinator | 0:bb348c97df44 | 1430 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_GCM_ENCRYPT); |
lypinator | 0:bb348c97df44 | 1431 | |
lypinator | 0:bb348c97df44 | 1432 | /* Set the Initialization Vector */ |
lypinator | 0:bb348c97df44 | 1433 | CRYPEx_GCMCCM_SetInitVector(hcryp, hcryp->Init.pInitVect); |
lypinator | 0:bb348c97df44 | 1434 | |
lypinator | 0:bb348c97df44 | 1435 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 1436 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 1437 | |
lypinator | 0:bb348c97df44 | 1438 | /* Enable CRYP to start the init phase */ |
lypinator | 0:bb348c97df44 | 1439 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 1440 | |
lypinator | 0:bb348c97df44 | 1441 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 1442 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 1443 | |
lypinator | 0:bb348c97df44 | 1444 | while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN) |
lypinator | 0:bb348c97df44 | 1445 | { |
lypinator | 0:bb348c97df44 | 1446 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 1447 | |
lypinator | 0:bb348c97df44 | 1448 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
lypinator | 0:bb348c97df44 | 1449 | { |
lypinator | 0:bb348c97df44 | 1450 | /* Change state */ |
lypinator | 0:bb348c97df44 | 1451 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1452 | |
lypinator | 0:bb348c97df44 | 1453 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1454 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1455 | |
lypinator | 0:bb348c97df44 | 1456 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1457 | |
lypinator | 0:bb348c97df44 | 1458 | } |
lypinator | 0:bb348c97df44 | 1459 | } |
lypinator | 0:bb348c97df44 | 1460 | |
lypinator | 0:bb348c97df44 | 1461 | /* Set the header phase */ |
lypinator | 0:bb348c97df44 | 1462 | if(CRYPEx_GCMCCM_SetHeaderPhase(hcryp, hcryp->Init.Header, hcryp->Init.HeaderSize, 1U) != HAL_OK) |
lypinator | 0:bb348c97df44 | 1463 | { |
lypinator | 0:bb348c97df44 | 1464 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1465 | } |
lypinator | 0:bb348c97df44 | 1466 | /* Disable the CRYP peripheral */ |
lypinator | 0:bb348c97df44 | 1467 | __HAL_CRYP_DISABLE(hcryp); |
lypinator | 0:bb348c97df44 | 1468 | |
lypinator | 0:bb348c97df44 | 1469 | /* Select payload phase once the header phase is performed */ |
lypinator | 0:bb348c97df44 | 1470 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD); |
lypinator | 0:bb348c97df44 | 1471 | |
lypinator | 0:bb348c97df44 | 1472 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 1473 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 1474 | |
lypinator | 0:bb348c97df44 | 1475 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 1476 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 1477 | } |
lypinator | 0:bb348c97df44 | 1478 | |
lypinator | 0:bb348c97df44 | 1479 | if(Size != 0U) |
lypinator | 0:bb348c97df44 | 1480 | { |
lypinator | 0:bb348c97df44 | 1481 | /* Enable Interrupts */ |
lypinator | 0:bb348c97df44 | 1482 | __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 1483 | /* Enable the CRYP peripheral */ |
lypinator | 0:bb348c97df44 | 1484 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 1485 | } |
lypinator | 0:bb348c97df44 | 1486 | else |
lypinator | 0:bb348c97df44 | 1487 | { |
lypinator | 0:bb348c97df44 | 1488 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 1489 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1490 | /* Change the CRYP state and phase */ |
lypinator | 0:bb348c97df44 | 1491 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 1492 | } |
lypinator | 0:bb348c97df44 | 1493 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 1494 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1495 | } |
lypinator | 0:bb348c97df44 | 1496 | else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) |
lypinator | 0:bb348c97df44 | 1497 | { |
lypinator | 0:bb348c97df44 | 1498 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
lypinator | 0:bb348c97df44 | 1499 | /* Write the Input block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 1500 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1501 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1502 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1503 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1504 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1505 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1506 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1507 | hcryp->pCrypInBuffPtr += 16U; |
lypinator | 0:bb348c97df44 | 1508 | hcryp->CrypInCount -= 16U; |
lypinator | 0:bb348c97df44 | 1509 | if(hcryp->CrypInCount == 0U) |
lypinator | 0:bb348c97df44 | 1510 | { |
lypinator | 0:bb348c97df44 | 1511 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); |
lypinator | 0:bb348c97df44 | 1512 | /* Call the Input data transfer complete callback */ |
lypinator | 0:bb348c97df44 | 1513 | HAL_CRYP_InCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 1514 | } |
lypinator | 0:bb348c97df44 | 1515 | } |
lypinator | 0:bb348c97df44 | 1516 | else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) |
lypinator | 0:bb348c97df44 | 1517 | { |
lypinator | 0:bb348c97df44 | 1518 | outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; |
lypinator | 0:bb348c97df44 | 1519 | /* Read the Output block from the Output FIFO */ |
lypinator | 0:bb348c97df44 | 1520 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1521 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1522 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1523 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1524 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1525 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1526 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1527 | hcryp->pCrypOutBuffPtr += 16U; |
lypinator | 0:bb348c97df44 | 1528 | hcryp->CrypOutCount -= 16U; |
lypinator | 0:bb348c97df44 | 1529 | if(hcryp->CrypOutCount == 0U) |
lypinator | 0:bb348c97df44 | 1530 | { |
lypinator | 0:bb348c97df44 | 1531 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 1532 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1533 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1534 | /* Change the CRYP peripheral state */ |
lypinator | 0:bb348c97df44 | 1535 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 1536 | /* Call Input transfer complete callback */ |
lypinator | 0:bb348c97df44 | 1537 | HAL_CRYP_OutCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 1538 | } |
lypinator | 0:bb348c97df44 | 1539 | } |
lypinator | 0:bb348c97df44 | 1540 | |
lypinator | 0:bb348c97df44 | 1541 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 1542 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1543 | } |
lypinator | 0:bb348c97df44 | 1544 | |
lypinator | 0:bb348c97df44 | 1545 | /** |
lypinator | 0:bb348c97df44 | 1546 | * @brief Initializes the CRYP peripheral in AES CCM encryption mode using interrupt. |
lypinator | 0:bb348c97df44 | 1547 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1548 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 1549 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 1550 | * @param Size Length of the plaintext buffer, must be a multiple of 16 |
lypinator | 0:bb348c97df44 | 1551 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 1552 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 1553 | */ |
lypinator | 0:bb348c97df44 | 1554 | HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) |
lypinator | 0:bb348c97df44 | 1555 | { |
lypinator | 0:bb348c97df44 | 1556 | uint32_t tickstart = 0U; |
lypinator | 0:bb348c97df44 | 1557 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 1558 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 1559 | |
lypinator | 0:bb348c97df44 | 1560 | uint32_t headersize = hcryp->Init.HeaderSize; |
lypinator | 0:bb348c97df44 | 1561 | uint32_t headeraddr = (uint32_t)hcryp->Init.Header; |
lypinator | 0:bb348c97df44 | 1562 | uint32_t loopcounter = 0U; |
lypinator | 0:bb348c97df44 | 1563 | uint32_t bufferidx = 0U; |
lypinator | 0:bb348c97df44 | 1564 | uint8_t blockb0[16U] = {0};/* Block B0 */ |
lypinator | 0:bb348c97df44 | 1565 | uint8_t ctr[16U] = {0}; /* Counter */ |
lypinator | 0:bb348c97df44 | 1566 | uint32_t b0addr = (uint32_t)blockb0; |
lypinator | 0:bb348c97df44 | 1567 | |
lypinator | 0:bb348c97df44 | 1568 | if(hcryp->State == HAL_CRYP_STATE_READY) |
lypinator | 0:bb348c97df44 | 1569 | { |
lypinator | 0:bb348c97df44 | 1570 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 1571 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1572 | |
lypinator | 0:bb348c97df44 | 1573 | hcryp->CrypInCount = Size; |
lypinator | 0:bb348c97df44 | 1574 | hcryp->pCrypInBuffPtr = pPlainData; |
lypinator | 0:bb348c97df44 | 1575 | hcryp->pCrypOutBuffPtr = pCypherData; |
lypinator | 0:bb348c97df44 | 1576 | hcryp->CrypOutCount = Size; |
lypinator | 0:bb348c97df44 | 1577 | |
lypinator | 0:bb348c97df44 | 1578 | /* Change the CRYP peripheral state */ |
lypinator | 0:bb348c97df44 | 1579 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 1580 | |
lypinator | 0:bb348c97df44 | 1581 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 1582 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 1583 | { |
lypinator | 0:bb348c97df44 | 1584 | /************************ Formatting the header block *******************/ |
lypinator | 0:bb348c97df44 | 1585 | if(headersize != 0U) |
lypinator | 0:bb348c97df44 | 1586 | { |
lypinator | 0:bb348c97df44 | 1587 | /* Check that the associated data (or header) length is lower than 2^16 - 2^8 = 65536 - 256 = 65280 */ |
lypinator | 0:bb348c97df44 | 1588 | if(headersize < 65280U) |
lypinator | 0:bb348c97df44 | 1589 | { |
lypinator | 0:bb348c97df44 | 1590 | hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize >> 8) & 0xFF); |
lypinator | 0:bb348c97df44 | 1591 | hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize) & 0xFF); |
lypinator | 0:bb348c97df44 | 1592 | headersize += 2U; |
lypinator | 0:bb348c97df44 | 1593 | } |
lypinator | 0:bb348c97df44 | 1594 | else |
lypinator | 0:bb348c97df44 | 1595 | { |
lypinator | 0:bb348c97df44 | 1596 | /* Header is encoded as 0xff || 0xfe || [headersize]32, i.e., six octets */ |
lypinator | 0:bb348c97df44 | 1597 | hcryp->Init.pScratch[bufferidx++] = 0xFFU; |
lypinator | 0:bb348c97df44 | 1598 | hcryp->Init.pScratch[bufferidx++] = 0xFEU; |
lypinator | 0:bb348c97df44 | 1599 | hcryp->Init.pScratch[bufferidx++] = headersize & 0xff000000U; |
lypinator | 0:bb348c97df44 | 1600 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x00ff0000U; |
lypinator | 0:bb348c97df44 | 1601 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x0000ff00U; |
lypinator | 0:bb348c97df44 | 1602 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x000000ffU; |
lypinator | 0:bb348c97df44 | 1603 | headersize += 6U; |
lypinator | 0:bb348c97df44 | 1604 | } |
lypinator | 0:bb348c97df44 | 1605 | /* Copy the header buffer in internal buffer "hcryp->Init.pScratch" */ |
lypinator | 0:bb348c97df44 | 1606 | for(loopcounter = 0U; loopcounter < headersize; loopcounter++) |
lypinator | 0:bb348c97df44 | 1607 | { |
lypinator | 0:bb348c97df44 | 1608 | hcryp->Init.pScratch[bufferidx++] = hcryp->Init.Header[loopcounter]; |
lypinator | 0:bb348c97df44 | 1609 | } |
lypinator | 0:bb348c97df44 | 1610 | /* Check if the header size is modulo 16 */ |
lypinator | 0:bb348c97df44 | 1611 | if ((headersize % 16U) != 0U) |
lypinator | 0:bb348c97df44 | 1612 | { |
lypinator | 0:bb348c97df44 | 1613 | /* Padd the header buffer with 0s till the hcryp->Init.pScratch length is modulo 16 */ |
lypinator | 0:bb348c97df44 | 1614 | for(loopcounter = headersize; loopcounter <= ((headersize/16U) + 1U) * 16U; loopcounter++) |
lypinator | 0:bb348c97df44 | 1615 | { |
lypinator | 0:bb348c97df44 | 1616 | hcryp->Init.pScratch[loopcounter] = 0U; |
lypinator | 0:bb348c97df44 | 1617 | } |
lypinator | 0:bb348c97df44 | 1618 | /* Set the header size to modulo 16 */ |
lypinator | 0:bb348c97df44 | 1619 | headersize = ((headersize/16U) + 1U) * 16U; |
lypinator | 0:bb348c97df44 | 1620 | } |
lypinator | 0:bb348c97df44 | 1621 | /* Set the pointer headeraddr to hcryp->Init.pScratch */ |
lypinator | 0:bb348c97df44 | 1622 | headeraddr = (uint32_t)hcryp->Init.pScratch; |
lypinator | 0:bb348c97df44 | 1623 | } |
lypinator | 0:bb348c97df44 | 1624 | /*********************** Formatting the block B0 ************************/ |
lypinator | 0:bb348c97df44 | 1625 | if(headersize != 0U) |
lypinator | 0:bb348c97df44 | 1626 | { |
lypinator | 0:bb348c97df44 | 1627 | blockb0[0U] = 0x40U; |
lypinator | 0:bb348c97df44 | 1628 | } |
lypinator | 0:bb348c97df44 | 1629 | /* Flags byte */ |
lypinator | 0:bb348c97df44 | 1630 | /* blockb0[0] |= 0u | (((( (uint8_t) hcryp->Init.TagSize - 2) / 2) & 0x07 ) << 3 ) | ( ( (uint8_t) (15 - hcryp->Init.IVSize) - 1) & 0x07U) */ |
lypinator | 0:bb348c97df44 | 1631 | blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)(((uint8_t)(hcryp->Init.TagSize - (uint8_t)(2))) >> 1U) & (uint8_t)0x07) << 3U); |
lypinator | 0:bb348c97df44 | 1632 | blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)((uint8_t)(15) - hcryp->Init.IVSize) - (uint8_t)1) & (uint8_t)0x07); |
lypinator | 0:bb348c97df44 | 1633 | |
lypinator | 0:bb348c97df44 | 1634 | for (loopcounter = 0U; loopcounter < hcryp->Init.IVSize; loopcounter++) |
lypinator | 0:bb348c97df44 | 1635 | { |
lypinator | 0:bb348c97df44 | 1636 | blockb0[loopcounter+1U] = hcryp->Init.pInitVect[loopcounter]; |
lypinator | 0:bb348c97df44 | 1637 | } |
lypinator | 0:bb348c97df44 | 1638 | for ( ; loopcounter < 13U; loopcounter++) |
lypinator | 0:bb348c97df44 | 1639 | { |
lypinator | 0:bb348c97df44 | 1640 | blockb0[loopcounter+1U] = 0U; |
lypinator | 0:bb348c97df44 | 1641 | } |
lypinator | 0:bb348c97df44 | 1642 | |
lypinator | 0:bb348c97df44 | 1643 | blockb0[14U] = (Size >> 8U); |
lypinator | 0:bb348c97df44 | 1644 | blockb0[15U] = (Size & 0xFFU); |
lypinator | 0:bb348c97df44 | 1645 | |
lypinator | 0:bb348c97df44 | 1646 | /************************* Formatting the initial counter ***************/ |
lypinator | 0:bb348c97df44 | 1647 | /* Byte 0: |
lypinator | 0:bb348c97df44 | 1648 | Bits 7 and 6 are reserved and shall be set to 0 |
lypinator | 0:bb348c97df44 | 1649 | Bits 3, 4, and 5 shall also be set to 0, to ensure that all the counter |
lypinator | 0:bb348c97df44 | 1650 | blocks are distinct from B0 |
lypinator | 0:bb348c97df44 | 1651 | Bits 0, 1, and 2 contain the same encoding of q as in B0 |
lypinator | 0:bb348c97df44 | 1652 | */ |
lypinator | 0:bb348c97df44 | 1653 | ctr[0U] = blockb0[0U] & 0x07U; |
lypinator | 0:bb348c97df44 | 1654 | /* byte 1 to NonceSize is the IV (Nonce) */ |
lypinator | 0:bb348c97df44 | 1655 | for(loopcounter = 1; loopcounter < hcryp->Init.IVSize + 1U; loopcounter++) |
lypinator | 0:bb348c97df44 | 1656 | { |
lypinator | 0:bb348c97df44 | 1657 | ctr[loopcounter] = blockb0[loopcounter]; |
lypinator | 0:bb348c97df44 | 1658 | } |
lypinator | 0:bb348c97df44 | 1659 | /* Set the LSB to 1 */ |
lypinator | 0:bb348c97df44 | 1660 | ctr[15U] |= 0x01U; |
lypinator | 0:bb348c97df44 | 1661 | |
lypinator | 0:bb348c97df44 | 1662 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 1663 | CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
lypinator | 0:bb348c97df44 | 1664 | |
lypinator | 0:bb348c97df44 | 1665 | /* Set the CRYP peripheral in AES CCM mode */ |
lypinator | 0:bb348c97df44 | 1666 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CCM_ENCRYPT); |
lypinator | 0:bb348c97df44 | 1667 | |
lypinator | 0:bb348c97df44 | 1668 | /* Set the Initialization Vector */ |
lypinator | 0:bb348c97df44 | 1669 | CRYPEx_GCMCCM_SetInitVector(hcryp, ctr); |
lypinator | 0:bb348c97df44 | 1670 | |
lypinator | 0:bb348c97df44 | 1671 | /* Select init phase */ |
lypinator | 0:bb348c97df44 | 1672 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT); |
lypinator | 0:bb348c97df44 | 1673 | |
lypinator | 0:bb348c97df44 | 1674 | b0addr = (uint32_t)blockb0; |
lypinator | 0:bb348c97df44 | 1675 | /* Write the blockb0 block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 1676 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
lypinator | 0:bb348c97df44 | 1677 | b0addr+=4U; |
lypinator | 0:bb348c97df44 | 1678 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
lypinator | 0:bb348c97df44 | 1679 | b0addr+=4U; |
lypinator | 0:bb348c97df44 | 1680 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
lypinator | 0:bb348c97df44 | 1681 | b0addr+=4U; |
lypinator | 0:bb348c97df44 | 1682 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
lypinator | 0:bb348c97df44 | 1683 | |
lypinator | 0:bb348c97df44 | 1684 | /* Enable the CRYP peripheral */ |
lypinator | 0:bb348c97df44 | 1685 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 1686 | |
lypinator | 0:bb348c97df44 | 1687 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 1688 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 1689 | |
lypinator | 0:bb348c97df44 | 1690 | while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN) |
lypinator | 0:bb348c97df44 | 1691 | { |
lypinator | 0:bb348c97df44 | 1692 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 1693 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
lypinator | 0:bb348c97df44 | 1694 | { |
lypinator | 0:bb348c97df44 | 1695 | /* Change state */ |
lypinator | 0:bb348c97df44 | 1696 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1697 | |
lypinator | 0:bb348c97df44 | 1698 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1699 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1700 | |
lypinator | 0:bb348c97df44 | 1701 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1702 | } |
lypinator | 0:bb348c97df44 | 1703 | } |
lypinator | 0:bb348c97df44 | 1704 | /***************************** Header phase *****************************/ |
lypinator | 0:bb348c97df44 | 1705 | if(headersize != 0U) |
lypinator | 0:bb348c97df44 | 1706 | { |
lypinator | 0:bb348c97df44 | 1707 | /* Select header phase */ |
lypinator | 0:bb348c97df44 | 1708 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER); |
lypinator | 0:bb348c97df44 | 1709 | |
lypinator | 0:bb348c97df44 | 1710 | /* Enable Crypto processor */ |
lypinator | 0:bb348c97df44 | 1711 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 1712 | |
lypinator | 0:bb348c97df44 | 1713 | for(loopcounter = 0U; (loopcounter < headersize); loopcounter+=16U) |
lypinator | 0:bb348c97df44 | 1714 | { |
lypinator | 0:bb348c97df44 | 1715 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 1716 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 1717 | |
lypinator | 0:bb348c97df44 | 1718 | while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM)) |
lypinator | 0:bb348c97df44 | 1719 | { |
lypinator | 0:bb348c97df44 | 1720 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 1721 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
lypinator | 0:bb348c97df44 | 1722 | { |
lypinator | 0:bb348c97df44 | 1723 | /* Change state */ |
lypinator | 0:bb348c97df44 | 1724 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1725 | |
lypinator | 0:bb348c97df44 | 1726 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1727 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1728 | |
lypinator | 0:bb348c97df44 | 1729 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1730 | } |
lypinator | 0:bb348c97df44 | 1731 | } |
lypinator | 0:bb348c97df44 | 1732 | /* Write the header block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 1733 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
lypinator | 0:bb348c97df44 | 1734 | headeraddr+=4U; |
lypinator | 0:bb348c97df44 | 1735 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
lypinator | 0:bb348c97df44 | 1736 | headeraddr+=4U; |
lypinator | 0:bb348c97df44 | 1737 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
lypinator | 0:bb348c97df44 | 1738 | headeraddr+=4U; |
lypinator | 0:bb348c97df44 | 1739 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
lypinator | 0:bb348c97df44 | 1740 | headeraddr+=4U; |
lypinator | 0:bb348c97df44 | 1741 | } |
lypinator | 0:bb348c97df44 | 1742 | |
lypinator | 0:bb348c97df44 | 1743 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 1744 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 1745 | |
lypinator | 0:bb348c97df44 | 1746 | while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY) |
lypinator | 0:bb348c97df44 | 1747 | { |
lypinator | 0:bb348c97df44 | 1748 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 1749 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
lypinator | 0:bb348c97df44 | 1750 | { |
lypinator | 0:bb348c97df44 | 1751 | /* Change state */ |
lypinator | 0:bb348c97df44 | 1752 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1753 | |
lypinator | 0:bb348c97df44 | 1754 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1755 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1756 | |
lypinator | 0:bb348c97df44 | 1757 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1758 | } |
lypinator | 0:bb348c97df44 | 1759 | } |
lypinator | 0:bb348c97df44 | 1760 | } |
lypinator | 0:bb348c97df44 | 1761 | /* Save formatted counter into the scratch buffer pScratch */ |
lypinator | 0:bb348c97df44 | 1762 | for(loopcounter = 0U; (loopcounter < 16U); loopcounter++) |
lypinator | 0:bb348c97df44 | 1763 | { |
lypinator | 0:bb348c97df44 | 1764 | hcryp->Init.pScratch[loopcounter] = ctr[loopcounter]; |
lypinator | 0:bb348c97df44 | 1765 | } |
lypinator | 0:bb348c97df44 | 1766 | /* Reset bit 0 */ |
lypinator | 0:bb348c97df44 | 1767 | hcryp->Init.pScratch[15U] &= 0xFEU; |
lypinator | 0:bb348c97df44 | 1768 | |
lypinator | 0:bb348c97df44 | 1769 | /* Select payload phase once the header phase is performed */ |
lypinator | 0:bb348c97df44 | 1770 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD); |
lypinator | 0:bb348c97df44 | 1771 | |
lypinator | 0:bb348c97df44 | 1772 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 1773 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 1774 | |
lypinator | 0:bb348c97df44 | 1775 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 1776 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 1777 | } |
lypinator | 0:bb348c97df44 | 1778 | |
lypinator | 0:bb348c97df44 | 1779 | if(Size != 0U) |
lypinator | 0:bb348c97df44 | 1780 | { |
lypinator | 0:bb348c97df44 | 1781 | /* Enable Interrupts */ |
lypinator | 0:bb348c97df44 | 1782 | __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 1783 | /* Enable the CRYP peripheral */ |
lypinator | 0:bb348c97df44 | 1784 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 1785 | } |
lypinator | 0:bb348c97df44 | 1786 | else |
lypinator | 0:bb348c97df44 | 1787 | { |
lypinator | 0:bb348c97df44 | 1788 | /* Change the CRYP state and phase */ |
lypinator | 0:bb348c97df44 | 1789 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 1790 | } |
lypinator | 0:bb348c97df44 | 1791 | |
lypinator | 0:bb348c97df44 | 1792 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 1793 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1794 | } |
lypinator | 0:bb348c97df44 | 1795 | else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) |
lypinator | 0:bb348c97df44 | 1796 | { |
lypinator | 0:bb348c97df44 | 1797 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
lypinator | 0:bb348c97df44 | 1798 | /* Write the Input block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 1799 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1800 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1801 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1802 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1803 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1804 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1805 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1806 | hcryp->pCrypInBuffPtr += 16U; |
lypinator | 0:bb348c97df44 | 1807 | hcryp->CrypInCount -= 16U; |
lypinator | 0:bb348c97df44 | 1808 | if(hcryp->CrypInCount == 0U) |
lypinator | 0:bb348c97df44 | 1809 | { |
lypinator | 0:bb348c97df44 | 1810 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); |
lypinator | 0:bb348c97df44 | 1811 | /* Call Input transfer complete callback */ |
lypinator | 0:bb348c97df44 | 1812 | HAL_CRYP_InCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 1813 | } |
lypinator | 0:bb348c97df44 | 1814 | } |
lypinator | 0:bb348c97df44 | 1815 | else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) |
lypinator | 0:bb348c97df44 | 1816 | { |
lypinator | 0:bb348c97df44 | 1817 | outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; |
lypinator | 0:bb348c97df44 | 1818 | /* Read the Output block from the Output FIFO */ |
lypinator | 0:bb348c97df44 | 1819 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1820 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1821 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1822 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1823 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1824 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1825 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1826 | hcryp->pCrypOutBuffPtr += 16U; |
lypinator | 0:bb348c97df44 | 1827 | hcryp->CrypOutCount -= 16U; |
lypinator | 0:bb348c97df44 | 1828 | if(hcryp->CrypOutCount == 0U) |
lypinator | 0:bb348c97df44 | 1829 | { |
lypinator | 0:bb348c97df44 | 1830 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 1831 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1832 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1833 | /* Change the CRYP peripheral state */ |
lypinator | 0:bb348c97df44 | 1834 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 1835 | /* Call Input transfer complete callback */ |
lypinator | 0:bb348c97df44 | 1836 | HAL_CRYP_OutCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 1837 | } |
lypinator | 0:bb348c97df44 | 1838 | } |
lypinator | 0:bb348c97df44 | 1839 | |
lypinator | 0:bb348c97df44 | 1840 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 1841 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1842 | } |
lypinator | 0:bb348c97df44 | 1843 | |
lypinator | 0:bb348c97df44 | 1844 | /** |
lypinator | 0:bb348c97df44 | 1845 | * @brief Initializes the CRYP peripheral in AES GCM decryption mode using IT. |
lypinator | 0:bb348c97df44 | 1846 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1847 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 1848 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 1849 | * @param Size Length of the cyphertext buffer, must be a multiple of 16 |
lypinator | 0:bb348c97df44 | 1850 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 1851 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 1852 | */ |
lypinator | 0:bb348c97df44 | 1853 | HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) |
lypinator | 0:bb348c97df44 | 1854 | { |
lypinator | 0:bb348c97df44 | 1855 | uint32_t tickstart = 0U; |
lypinator | 0:bb348c97df44 | 1856 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 1857 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 1858 | |
lypinator | 0:bb348c97df44 | 1859 | if(hcryp->State == HAL_CRYP_STATE_READY) |
lypinator | 0:bb348c97df44 | 1860 | { |
lypinator | 0:bb348c97df44 | 1861 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 1862 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1863 | |
lypinator | 0:bb348c97df44 | 1864 | /* Get the buffer addresses and sizes */ |
lypinator | 0:bb348c97df44 | 1865 | hcryp->CrypInCount = Size; |
lypinator | 0:bb348c97df44 | 1866 | hcryp->pCrypInBuffPtr = pCypherData; |
lypinator | 0:bb348c97df44 | 1867 | hcryp->pCrypOutBuffPtr = pPlainData; |
lypinator | 0:bb348c97df44 | 1868 | hcryp->CrypOutCount = Size; |
lypinator | 0:bb348c97df44 | 1869 | |
lypinator | 0:bb348c97df44 | 1870 | /* Change the CRYP peripheral state */ |
lypinator | 0:bb348c97df44 | 1871 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 1872 | |
lypinator | 0:bb348c97df44 | 1873 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 1874 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 1875 | { |
lypinator | 0:bb348c97df44 | 1876 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 1877 | CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
lypinator | 0:bb348c97df44 | 1878 | |
lypinator | 0:bb348c97df44 | 1879 | /* Set the CRYP peripheral in AES GCM decryption mode */ |
lypinator | 0:bb348c97df44 | 1880 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_GCM_DECRYPT); |
lypinator | 0:bb348c97df44 | 1881 | |
lypinator | 0:bb348c97df44 | 1882 | /* Set the Initialization Vector */ |
lypinator | 0:bb348c97df44 | 1883 | CRYPEx_GCMCCM_SetInitVector(hcryp, hcryp->Init.pInitVect); |
lypinator | 0:bb348c97df44 | 1884 | |
lypinator | 0:bb348c97df44 | 1885 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 1886 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 1887 | |
lypinator | 0:bb348c97df44 | 1888 | /* Enable CRYP to start the init phase */ |
lypinator | 0:bb348c97df44 | 1889 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 1890 | |
lypinator | 0:bb348c97df44 | 1891 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 1892 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 1893 | |
lypinator | 0:bb348c97df44 | 1894 | while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN) |
lypinator | 0:bb348c97df44 | 1895 | { |
lypinator | 0:bb348c97df44 | 1896 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 1897 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
lypinator | 0:bb348c97df44 | 1898 | { |
lypinator | 0:bb348c97df44 | 1899 | /* Change state */ |
lypinator | 0:bb348c97df44 | 1900 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1901 | |
lypinator | 0:bb348c97df44 | 1902 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1903 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1904 | |
lypinator | 0:bb348c97df44 | 1905 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1906 | } |
lypinator | 0:bb348c97df44 | 1907 | } |
lypinator | 0:bb348c97df44 | 1908 | |
lypinator | 0:bb348c97df44 | 1909 | /* Set the header phase */ |
lypinator | 0:bb348c97df44 | 1910 | if(CRYPEx_GCMCCM_SetHeaderPhase(hcryp, hcryp->Init.Header, hcryp->Init.HeaderSize, 1U) != HAL_OK) |
lypinator | 0:bb348c97df44 | 1911 | { |
lypinator | 0:bb348c97df44 | 1912 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1913 | } |
lypinator | 0:bb348c97df44 | 1914 | /* Disable the CRYP peripheral */ |
lypinator | 0:bb348c97df44 | 1915 | __HAL_CRYP_DISABLE(hcryp); |
lypinator | 0:bb348c97df44 | 1916 | |
lypinator | 0:bb348c97df44 | 1917 | /* Select payload phase once the header phase is performed */ |
lypinator | 0:bb348c97df44 | 1918 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD); |
lypinator | 0:bb348c97df44 | 1919 | |
lypinator | 0:bb348c97df44 | 1920 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 1921 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 1922 | } |
lypinator | 0:bb348c97df44 | 1923 | |
lypinator | 0:bb348c97df44 | 1924 | if(Size != 0U) |
lypinator | 0:bb348c97df44 | 1925 | { |
lypinator | 0:bb348c97df44 | 1926 | /* Enable Interrupts */ |
lypinator | 0:bb348c97df44 | 1927 | __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 1928 | /* Enable the CRYP peripheral */ |
lypinator | 0:bb348c97df44 | 1929 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 1930 | } |
lypinator | 0:bb348c97df44 | 1931 | else |
lypinator | 0:bb348c97df44 | 1932 | { |
lypinator | 0:bb348c97df44 | 1933 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 1934 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1935 | /* Change the CRYP state and phase */ |
lypinator | 0:bb348c97df44 | 1936 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 1937 | } |
lypinator | 0:bb348c97df44 | 1938 | |
lypinator | 0:bb348c97df44 | 1939 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 1940 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1941 | } |
lypinator | 0:bb348c97df44 | 1942 | else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) |
lypinator | 0:bb348c97df44 | 1943 | { |
lypinator | 0:bb348c97df44 | 1944 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
lypinator | 0:bb348c97df44 | 1945 | /* Write the Input block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 1946 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1947 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1948 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1949 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1950 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1951 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1952 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 1953 | hcryp->pCrypInBuffPtr += 16U; |
lypinator | 0:bb348c97df44 | 1954 | hcryp->CrypInCount -= 16U; |
lypinator | 0:bb348c97df44 | 1955 | if(hcryp->CrypInCount == 0U) |
lypinator | 0:bb348c97df44 | 1956 | { |
lypinator | 0:bb348c97df44 | 1957 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); |
lypinator | 0:bb348c97df44 | 1958 | /* Call the Input data transfer complete callback */ |
lypinator | 0:bb348c97df44 | 1959 | HAL_CRYP_InCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 1960 | } |
lypinator | 0:bb348c97df44 | 1961 | } |
lypinator | 0:bb348c97df44 | 1962 | else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) |
lypinator | 0:bb348c97df44 | 1963 | { |
lypinator | 0:bb348c97df44 | 1964 | outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; |
lypinator | 0:bb348c97df44 | 1965 | /* Read the Output block from the Output FIFO */ |
lypinator | 0:bb348c97df44 | 1966 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1967 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1968 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1969 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1970 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1971 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 1972 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 1973 | hcryp->pCrypOutBuffPtr += 16U; |
lypinator | 0:bb348c97df44 | 1974 | hcryp->CrypOutCount -= 16U; |
lypinator | 0:bb348c97df44 | 1975 | if(hcryp->CrypOutCount == 0U) |
lypinator | 0:bb348c97df44 | 1976 | { |
lypinator | 0:bb348c97df44 | 1977 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 1978 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1979 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 1980 | /* Change the CRYP peripheral state */ |
lypinator | 0:bb348c97df44 | 1981 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 1982 | /* Call Input transfer complete callback */ |
lypinator | 0:bb348c97df44 | 1983 | HAL_CRYP_OutCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 1984 | } |
lypinator | 0:bb348c97df44 | 1985 | } |
lypinator | 0:bb348c97df44 | 1986 | |
lypinator | 0:bb348c97df44 | 1987 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 1988 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1989 | } |
lypinator | 0:bb348c97df44 | 1990 | |
lypinator | 0:bb348c97df44 | 1991 | /** |
lypinator | 0:bb348c97df44 | 1992 | * @brief Initializes the CRYP peripheral in AES CCM decryption mode using interrupt |
lypinator | 0:bb348c97df44 | 1993 | * then decrypted pCypherData. The cypher data are available in pPlainData. |
lypinator | 0:bb348c97df44 | 1994 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1995 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 1996 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 1997 | * @param Size Length of the plaintext buffer, must be a multiple of 16 |
lypinator | 0:bb348c97df44 | 1998 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 1999 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2000 | */ |
lypinator | 0:bb348c97df44 | 2001 | HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) |
lypinator | 0:bb348c97df44 | 2002 | { |
lypinator | 0:bb348c97df44 | 2003 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 2004 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 2005 | uint32_t tickstart = 0U; |
lypinator | 0:bb348c97df44 | 2006 | uint32_t headersize = hcryp->Init.HeaderSize; |
lypinator | 0:bb348c97df44 | 2007 | uint32_t headeraddr = (uint32_t)hcryp->Init.Header; |
lypinator | 0:bb348c97df44 | 2008 | uint32_t loopcounter = 0U; |
lypinator | 0:bb348c97df44 | 2009 | uint32_t bufferidx = 0U; |
lypinator | 0:bb348c97df44 | 2010 | uint8_t blockb0[16U] = {0};/* Block B0 */ |
lypinator | 0:bb348c97df44 | 2011 | uint8_t ctr[16U] = {0}; /* Counter */ |
lypinator | 0:bb348c97df44 | 2012 | uint32_t b0addr = (uint32_t)blockb0; |
lypinator | 0:bb348c97df44 | 2013 | |
lypinator | 0:bb348c97df44 | 2014 | if(hcryp->State == HAL_CRYP_STATE_READY) |
lypinator | 0:bb348c97df44 | 2015 | { |
lypinator | 0:bb348c97df44 | 2016 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 2017 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2018 | |
lypinator | 0:bb348c97df44 | 2019 | hcryp->CrypInCount = Size; |
lypinator | 0:bb348c97df44 | 2020 | hcryp->pCrypInBuffPtr = pCypherData; |
lypinator | 0:bb348c97df44 | 2021 | hcryp->pCrypOutBuffPtr = pPlainData; |
lypinator | 0:bb348c97df44 | 2022 | hcryp->CrypOutCount = Size; |
lypinator | 0:bb348c97df44 | 2023 | |
lypinator | 0:bb348c97df44 | 2024 | /* Change the CRYP peripheral state */ |
lypinator | 0:bb348c97df44 | 2025 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 2026 | |
lypinator | 0:bb348c97df44 | 2027 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 2028 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 2029 | { |
lypinator | 0:bb348c97df44 | 2030 | /************************ Formatting the header block *******************/ |
lypinator | 0:bb348c97df44 | 2031 | if(headersize != 0U) |
lypinator | 0:bb348c97df44 | 2032 | { |
lypinator | 0:bb348c97df44 | 2033 | /* Check that the associated data (or header) length is lower than 2^16 - 2^8 = 65536 - 256 = 65280 */ |
lypinator | 0:bb348c97df44 | 2034 | if(headersize < 65280U) |
lypinator | 0:bb348c97df44 | 2035 | { |
lypinator | 0:bb348c97df44 | 2036 | hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize >> 8) & 0xFF); |
lypinator | 0:bb348c97df44 | 2037 | hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize) & 0xFF); |
lypinator | 0:bb348c97df44 | 2038 | headersize += 2U; |
lypinator | 0:bb348c97df44 | 2039 | } |
lypinator | 0:bb348c97df44 | 2040 | else |
lypinator | 0:bb348c97df44 | 2041 | { |
lypinator | 0:bb348c97df44 | 2042 | /* Header is encoded as 0xff || 0xfe || [headersize]32, i.e., six octets */ |
lypinator | 0:bb348c97df44 | 2043 | hcryp->Init.pScratch[bufferidx++] = 0xFFU; |
lypinator | 0:bb348c97df44 | 2044 | hcryp->Init.pScratch[bufferidx++] = 0xFEU; |
lypinator | 0:bb348c97df44 | 2045 | hcryp->Init.pScratch[bufferidx++] = headersize & 0xff000000U; |
lypinator | 0:bb348c97df44 | 2046 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x00ff0000U; |
lypinator | 0:bb348c97df44 | 2047 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x0000ff00U; |
lypinator | 0:bb348c97df44 | 2048 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x000000ffU; |
lypinator | 0:bb348c97df44 | 2049 | headersize += 6U; |
lypinator | 0:bb348c97df44 | 2050 | } |
lypinator | 0:bb348c97df44 | 2051 | /* Copy the header buffer in internal buffer "hcryp->Init.pScratch" */ |
lypinator | 0:bb348c97df44 | 2052 | for(loopcounter = 0U; loopcounter < headersize; loopcounter++) |
lypinator | 0:bb348c97df44 | 2053 | { |
lypinator | 0:bb348c97df44 | 2054 | hcryp->Init.pScratch[bufferidx++] = hcryp->Init.Header[loopcounter]; |
lypinator | 0:bb348c97df44 | 2055 | } |
lypinator | 0:bb348c97df44 | 2056 | /* Check if the header size is modulo 16 */ |
lypinator | 0:bb348c97df44 | 2057 | if ((headersize % 16U) != 0U) |
lypinator | 0:bb348c97df44 | 2058 | { |
lypinator | 0:bb348c97df44 | 2059 | /* Padd the header buffer with 0s till the hcryp->Init.pScratch length is modulo 16 */ |
lypinator | 0:bb348c97df44 | 2060 | for(loopcounter = headersize; loopcounter <= ((headersize/16U) + 1U) * 16U; loopcounter++) |
lypinator | 0:bb348c97df44 | 2061 | { |
lypinator | 0:bb348c97df44 | 2062 | hcryp->Init.pScratch[loopcounter] = 0U; |
lypinator | 0:bb348c97df44 | 2063 | } |
lypinator | 0:bb348c97df44 | 2064 | /* Set the header size to modulo 16 */ |
lypinator | 0:bb348c97df44 | 2065 | headersize = ((headersize/16U) + 1U) * 16U; |
lypinator | 0:bb348c97df44 | 2066 | } |
lypinator | 0:bb348c97df44 | 2067 | /* Set the pointer headeraddr to hcryp->Init.pScratch */ |
lypinator | 0:bb348c97df44 | 2068 | headeraddr = (uint32_t)hcryp->Init.pScratch; |
lypinator | 0:bb348c97df44 | 2069 | } |
lypinator | 0:bb348c97df44 | 2070 | /*********************** Formatting the block B0 ************************/ |
lypinator | 0:bb348c97df44 | 2071 | if(headersize != 0U) |
lypinator | 0:bb348c97df44 | 2072 | { |
lypinator | 0:bb348c97df44 | 2073 | blockb0[0U] = 0x40U; |
lypinator | 0:bb348c97df44 | 2074 | } |
lypinator | 0:bb348c97df44 | 2075 | /* Flags byte */ |
lypinator | 0:bb348c97df44 | 2076 | /* blockb0[0] |= 0u | (((( (uint8_t) hcryp->Init.TagSize - 2) / 2) & 0x07 ) << 3 ) | ( ( (uint8_t) (15 - hcryp->Init.IVSize) - 1) & 0x07U) */ |
lypinator | 0:bb348c97df44 | 2077 | blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)(((uint8_t)(hcryp->Init.TagSize - (uint8_t)(2))) >> 1U) & (uint8_t)0x07) << 3U); |
lypinator | 0:bb348c97df44 | 2078 | blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)((uint8_t)(15) - hcryp->Init.IVSize) - (uint8_t)1) & (uint8_t)0x07); |
lypinator | 0:bb348c97df44 | 2079 | |
lypinator | 0:bb348c97df44 | 2080 | for (loopcounter = 0U; loopcounter < hcryp->Init.IVSize; loopcounter++) |
lypinator | 0:bb348c97df44 | 2081 | { |
lypinator | 0:bb348c97df44 | 2082 | blockb0[loopcounter+1U] = hcryp->Init.pInitVect[loopcounter]; |
lypinator | 0:bb348c97df44 | 2083 | } |
lypinator | 0:bb348c97df44 | 2084 | for ( ; loopcounter < 13U; loopcounter++) |
lypinator | 0:bb348c97df44 | 2085 | { |
lypinator | 0:bb348c97df44 | 2086 | blockb0[loopcounter+1U] = 0U; |
lypinator | 0:bb348c97df44 | 2087 | } |
lypinator | 0:bb348c97df44 | 2088 | |
lypinator | 0:bb348c97df44 | 2089 | blockb0[14U] = (Size >> 8U); |
lypinator | 0:bb348c97df44 | 2090 | blockb0[15U] = (Size & 0xFFU); |
lypinator | 0:bb348c97df44 | 2091 | |
lypinator | 0:bb348c97df44 | 2092 | /************************* Formatting the initial counter ***************/ |
lypinator | 0:bb348c97df44 | 2093 | /* Byte 0: |
lypinator | 0:bb348c97df44 | 2094 | Bits 7 and 6 are reserved and shall be set to 0 |
lypinator | 0:bb348c97df44 | 2095 | Bits 3, 4, and 5 shall also be set to 0, to ensure that all the counter |
lypinator | 0:bb348c97df44 | 2096 | blocks are distinct from B0 |
lypinator | 0:bb348c97df44 | 2097 | Bits 0, 1, and 2 contain the same encoding of q as in B0 |
lypinator | 0:bb348c97df44 | 2098 | */ |
lypinator | 0:bb348c97df44 | 2099 | ctr[0U] = blockb0[0U] & 0x07U; |
lypinator | 0:bb348c97df44 | 2100 | /* byte 1 to NonceSize is the IV (Nonce) */ |
lypinator | 0:bb348c97df44 | 2101 | for(loopcounter = 1U; loopcounter < hcryp->Init.IVSize + 1U; loopcounter++) |
lypinator | 0:bb348c97df44 | 2102 | { |
lypinator | 0:bb348c97df44 | 2103 | ctr[loopcounter] = blockb0[loopcounter]; |
lypinator | 0:bb348c97df44 | 2104 | } |
lypinator | 0:bb348c97df44 | 2105 | /* Set the LSB to 1 */ |
lypinator | 0:bb348c97df44 | 2106 | ctr[15U] |= 0x01U; |
lypinator | 0:bb348c97df44 | 2107 | |
lypinator | 0:bb348c97df44 | 2108 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 2109 | CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
lypinator | 0:bb348c97df44 | 2110 | |
lypinator | 0:bb348c97df44 | 2111 | /* Set the CRYP peripheral in AES CCM mode */ |
lypinator | 0:bb348c97df44 | 2112 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CCM_DECRYPT); |
lypinator | 0:bb348c97df44 | 2113 | |
lypinator | 0:bb348c97df44 | 2114 | /* Set the Initialization Vector */ |
lypinator | 0:bb348c97df44 | 2115 | CRYPEx_GCMCCM_SetInitVector(hcryp, ctr); |
lypinator | 0:bb348c97df44 | 2116 | |
lypinator | 0:bb348c97df44 | 2117 | /* Select init phase */ |
lypinator | 0:bb348c97df44 | 2118 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT); |
lypinator | 0:bb348c97df44 | 2119 | |
lypinator | 0:bb348c97df44 | 2120 | b0addr = (uint32_t)blockb0; |
lypinator | 0:bb348c97df44 | 2121 | /* Write the blockb0 block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 2122 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
lypinator | 0:bb348c97df44 | 2123 | b0addr+=4U; |
lypinator | 0:bb348c97df44 | 2124 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
lypinator | 0:bb348c97df44 | 2125 | b0addr+=4U; |
lypinator | 0:bb348c97df44 | 2126 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
lypinator | 0:bb348c97df44 | 2127 | b0addr+=4U; |
lypinator | 0:bb348c97df44 | 2128 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
lypinator | 0:bb348c97df44 | 2129 | |
lypinator | 0:bb348c97df44 | 2130 | /* Enable the CRYP peripheral */ |
lypinator | 0:bb348c97df44 | 2131 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 2132 | |
lypinator | 0:bb348c97df44 | 2133 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 2134 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 2135 | |
lypinator | 0:bb348c97df44 | 2136 | while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN) |
lypinator | 0:bb348c97df44 | 2137 | { |
lypinator | 0:bb348c97df44 | 2138 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 2139 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
lypinator | 0:bb348c97df44 | 2140 | { |
lypinator | 0:bb348c97df44 | 2141 | /* Change state */ |
lypinator | 0:bb348c97df44 | 2142 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2143 | |
lypinator | 0:bb348c97df44 | 2144 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 2145 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2146 | |
lypinator | 0:bb348c97df44 | 2147 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2148 | } |
lypinator | 0:bb348c97df44 | 2149 | } |
lypinator | 0:bb348c97df44 | 2150 | /***************************** Header phase *****************************/ |
lypinator | 0:bb348c97df44 | 2151 | if(headersize != 0U) |
lypinator | 0:bb348c97df44 | 2152 | { |
lypinator | 0:bb348c97df44 | 2153 | /* Select header phase */ |
lypinator | 0:bb348c97df44 | 2154 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER); |
lypinator | 0:bb348c97df44 | 2155 | |
lypinator | 0:bb348c97df44 | 2156 | /* Enable Crypto processor */ |
lypinator | 0:bb348c97df44 | 2157 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 2158 | |
lypinator | 0:bb348c97df44 | 2159 | for(loopcounter = 0U; (loopcounter < headersize); loopcounter+=16U) |
lypinator | 0:bb348c97df44 | 2160 | { |
lypinator | 0:bb348c97df44 | 2161 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 2162 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 2163 | |
lypinator | 0:bb348c97df44 | 2164 | while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM)) |
lypinator | 0:bb348c97df44 | 2165 | { |
lypinator | 0:bb348c97df44 | 2166 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 2167 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
lypinator | 0:bb348c97df44 | 2168 | { |
lypinator | 0:bb348c97df44 | 2169 | /* Change state */ |
lypinator | 0:bb348c97df44 | 2170 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2171 | |
lypinator | 0:bb348c97df44 | 2172 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 2173 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2174 | |
lypinator | 0:bb348c97df44 | 2175 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2176 | } |
lypinator | 0:bb348c97df44 | 2177 | } |
lypinator | 0:bb348c97df44 | 2178 | /* Write the header block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 2179 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
lypinator | 0:bb348c97df44 | 2180 | headeraddr+=4U; |
lypinator | 0:bb348c97df44 | 2181 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
lypinator | 0:bb348c97df44 | 2182 | headeraddr+=4U; |
lypinator | 0:bb348c97df44 | 2183 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
lypinator | 0:bb348c97df44 | 2184 | headeraddr+=4U; |
lypinator | 0:bb348c97df44 | 2185 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
lypinator | 0:bb348c97df44 | 2186 | headeraddr+=4U; |
lypinator | 0:bb348c97df44 | 2187 | } |
lypinator | 0:bb348c97df44 | 2188 | |
lypinator | 0:bb348c97df44 | 2189 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 2190 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 2191 | |
lypinator | 0:bb348c97df44 | 2192 | while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY) |
lypinator | 0:bb348c97df44 | 2193 | { |
lypinator | 0:bb348c97df44 | 2194 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 2195 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
lypinator | 0:bb348c97df44 | 2196 | { |
lypinator | 0:bb348c97df44 | 2197 | /* Change state */ |
lypinator | 0:bb348c97df44 | 2198 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2199 | |
lypinator | 0:bb348c97df44 | 2200 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 2201 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2202 | |
lypinator | 0:bb348c97df44 | 2203 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2204 | } |
lypinator | 0:bb348c97df44 | 2205 | } |
lypinator | 0:bb348c97df44 | 2206 | } |
lypinator | 0:bb348c97df44 | 2207 | /* Save formatted counter into the scratch buffer pScratch */ |
lypinator | 0:bb348c97df44 | 2208 | for(loopcounter = 0U; (loopcounter < 16U); loopcounter++) |
lypinator | 0:bb348c97df44 | 2209 | { |
lypinator | 0:bb348c97df44 | 2210 | hcryp->Init.pScratch[loopcounter] = ctr[loopcounter]; |
lypinator | 0:bb348c97df44 | 2211 | } |
lypinator | 0:bb348c97df44 | 2212 | /* Reset bit 0 */ |
lypinator | 0:bb348c97df44 | 2213 | hcryp->Init.pScratch[15U] &= 0xFEU; |
lypinator | 0:bb348c97df44 | 2214 | /* Select payload phase once the header phase is performed */ |
lypinator | 0:bb348c97df44 | 2215 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD); |
lypinator | 0:bb348c97df44 | 2216 | |
lypinator | 0:bb348c97df44 | 2217 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 2218 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 2219 | |
lypinator | 0:bb348c97df44 | 2220 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 2221 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 2222 | } |
lypinator | 0:bb348c97df44 | 2223 | |
lypinator | 0:bb348c97df44 | 2224 | /* Enable Interrupts */ |
lypinator | 0:bb348c97df44 | 2225 | __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 2226 | |
lypinator | 0:bb348c97df44 | 2227 | /* Enable the CRYP peripheral */ |
lypinator | 0:bb348c97df44 | 2228 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 2229 | |
lypinator | 0:bb348c97df44 | 2230 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 2231 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2232 | } |
lypinator | 0:bb348c97df44 | 2233 | else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) |
lypinator | 0:bb348c97df44 | 2234 | { |
lypinator | 0:bb348c97df44 | 2235 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
lypinator | 0:bb348c97df44 | 2236 | /* Write the Input block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 2237 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 2238 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 2239 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 2240 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 2241 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 2242 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 2243 | hcryp->Instance->DR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 2244 | hcryp->pCrypInBuffPtr += 16U; |
lypinator | 0:bb348c97df44 | 2245 | hcryp->CrypInCount -= 16U; |
lypinator | 0:bb348c97df44 | 2246 | if(hcryp->CrypInCount == 0U) |
lypinator | 0:bb348c97df44 | 2247 | { |
lypinator | 0:bb348c97df44 | 2248 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); |
lypinator | 0:bb348c97df44 | 2249 | /* Call the Input data transfer complete callback */ |
lypinator | 0:bb348c97df44 | 2250 | HAL_CRYP_InCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 2251 | } |
lypinator | 0:bb348c97df44 | 2252 | } |
lypinator | 0:bb348c97df44 | 2253 | else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) |
lypinator | 0:bb348c97df44 | 2254 | { |
lypinator | 0:bb348c97df44 | 2255 | outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; |
lypinator | 0:bb348c97df44 | 2256 | /* Read the Output block from the Output FIFO */ |
lypinator | 0:bb348c97df44 | 2257 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 2258 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 2259 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 2260 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 2261 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 2262 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 2263 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; |
lypinator | 0:bb348c97df44 | 2264 | hcryp->pCrypOutBuffPtr += 16U; |
lypinator | 0:bb348c97df44 | 2265 | hcryp->CrypOutCount -= 16U; |
lypinator | 0:bb348c97df44 | 2266 | if(hcryp->CrypOutCount == 0U) |
lypinator | 0:bb348c97df44 | 2267 | { |
lypinator | 0:bb348c97df44 | 2268 | __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); |
lypinator | 0:bb348c97df44 | 2269 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 2270 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2271 | /* Change the CRYP peripheral state */ |
lypinator | 0:bb348c97df44 | 2272 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 2273 | /* Call Input transfer complete callback */ |
lypinator | 0:bb348c97df44 | 2274 | HAL_CRYP_OutCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 2275 | } |
lypinator | 0:bb348c97df44 | 2276 | } |
lypinator | 0:bb348c97df44 | 2277 | |
lypinator | 0:bb348c97df44 | 2278 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 2279 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2280 | } |
lypinator | 0:bb348c97df44 | 2281 | |
lypinator | 0:bb348c97df44 | 2282 | /** |
lypinator | 0:bb348c97df44 | 2283 | * @brief Initializes the CRYP peripheral in AES GCM encryption mode using DMA. |
lypinator | 0:bb348c97df44 | 2284 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 2285 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 2286 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 2287 | * @param Size Length of the plaintext buffer, must be a multiple of 16 |
lypinator | 0:bb348c97df44 | 2288 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 2289 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2290 | */ |
lypinator | 0:bb348c97df44 | 2291 | HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) |
lypinator | 0:bb348c97df44 | 2292 | { |
lypinator | 0:bb348c97df44 | 2293 | uint32_t tickstart = 0U; |
lypinator | 0:bb348c97df44 | 2294 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 2295 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 2296 | |
lypinator | 0:bb348c97df44 | 2297 | if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) |
lypinator | 0:bb348c97df44 | 2298 | { |
lypinator | 0:bb348c97df44 | 2299 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 2300 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2301 | |
lypinator | 0:bb348c97df44 | 2302 | inputaddr = (uint32_t)pPlainData; |
lypinator | 0:bb348c97df44 | 2303 | outputaddr = (uint32_t)pCypherData; |
lypinator | 0:bb348c97df44 | 2304 | |
lypinator | 0:bb348c97df44 | 2305 | /* Change the CRYP peripheral state */ |
lypinator | 0:bb348c97df44 | 2306 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 2307 | |
lypinator | 0:bb348c97df44 | 2308 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 2309 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 2310 | { |
lypinator | 0:bb348c97df44 | 2311 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 2312 | CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
lypinator | 0:bb348c97df44 | 2313 | |
lypinator | 0:bb348c97df44 | 2314 | /* Set the CRYP peripheral in AES GCM mode */ |
lypinator | 0:bb348c97df44 | 2315 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_GCM_ENCRYPT); |
lypinator | 0:bb348c97df44 | 2316 | |
lypinator | 0:bb348c97df44 | 2317 | /* Set the Initialization Vector */ |
lypinator | 0:bb348c97df44 | 2318 | CRYPEx_GCMCCM_SetInitVector(hcryp, hcryp->Init.pInitVect); |
lypinator | 0:bb348c97df44 | 2319 | |
lypinator | 0:bb348c97df44 | 2320 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 2321 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 2322 | |
lypinator | 0:bb348c97df44 | 2323 | /* Enable CRYP to start the init phase */ |
lypinator | 0:bb348c97df44 | 2324 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 2325 | |
lypinator | 0:bb348c97df44 | 2326 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 2327 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 2328 | |
lypinator | 0:bb348c97df44 | 2329 | while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN) |
lypinator | 0:bb348c97df44 | 2330 | { |
lypinator | 0:bb348c97df44 | 2331 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 2332 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
lypinator | 0:bb348c97df44 | 2333 | { |
lypinator | 0:bb348c97df44 | 2334 | /* Change state */ |
lypinator | 0:bb348c97df44 | 2335 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2336 | |
lypinator | 0:bb348c97df44 | 2337 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 2338 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2339 | |
lypinator | 0:bb348c97df44 | 2340 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2341 | } |
lypinator | 0:bb348c97df44 | 2342 | } |
lypinator | 0:bb348c97df44 | 2343 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 2344 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 2345 | |
lypinator | 0:bb348c97df44 | 2346 | /* Set the header phase */ |
lypinator | 0:bb348c97df44 | 2347 | if(CRYPEx_GCMCCM_SetHeaderPhase(hcryp, hcryp->Init.Header, hcryp->Init.HeaderSize, 1U) != HAL_OK) |
lypinator | 0:bb348c97df44 | 2348 | { |
lypinator | 0:bb348c97df44 | 2349 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2350 | } |
lypinator | 0:bb348c97df44 | 2351 | /* Disable the CRYP peripheral */ |
lypinator | 0:bb348c97df44 | 2352 | __HAL_CRYP_DISABLE(hcryp); |
lypinator | 0:bb348c97df44 | 2353 | |
lypinator | 0:bb348c97df44 | 2354 | /* Select payload phase once the header phase is performed */ |
lypinator | 0:bb348c97df44 | 2355 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD); |
lypinator | 0:bb348c97df44 | 2356 | |
lypinator | 0:bb348c97df44 | 2357 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 2358 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 2359 | |
lypinator | 0:bb348c97df44 | 2360 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 2361 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 2362 | } |
lypinator | 0:bb348c97df44 | 2363 | |
lypinator | 0:bb348c97df44 | 2364 | /* Set the input and output addresses and start DMA transfer */ |
lypinator | 0:bb348c97df44 | 2365 | CRYPEx_GCMCCM_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); |
lypinator | 0:bb348c97df44 | 2366 | |
lypinator | 0:bb348c97df44 | 2367 | /* Unlock process */ |
lypinator | 0:bb348c97df44 | 2368 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2369 | |
lypinator | 0:bb348c97df44 | 2370 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 2371 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2372 | } |
lypinator | 0:bb348c97df44 | 2373 | else |
lypinator | 0:bb348c97df44 | 2374 | { |
lypinator | 0:bb348c97df44 | 2375 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 2376 | } |
lypinator | 0:bb348c97df44 | 2377 | } |
lypinator | 0:bb348c97df44 | 2378 | |
lypinator | 0:bb348c97df44 | 2379 | /** |
lypinator | 0:bb348c97df44 | 2380 | * @brief Initializes the CRYP peripheral in AES CCM encryption mode using interrupt. |
lypinator | 0:bb348c97df44 | 2381 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 2382 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 2383 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 2384 | * @param Size Length of the plaintext buffer, must be a multiple of 16 |
lypinator | 0:bb348c97df44 | 2385 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 2386 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2387 | */ |
lypinator | 0:bb348c97df44 | 2388 | HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) |
lypinator | 0:bb348c97df44 | 2389 | { |
lypinator | 0:bb348c97df44 | 2390 | uint32_t tickstart = 0U; |
lypinator | 0:bb348c97df44 | 2391 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 2392 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 2393 | uint32_t headersize; |
lypinator | 0:bb348c97df44 | 2394 | uint32_t headeraddr; |
lypinator | 0:bb348c97df44 | 2395 | uint32_t loopcounter = 0U; |
lypinator | 0:bb348c97df44 | 2396 | uint32_t bufferidx = 0U; |
lypinator | 0:bb348c97df44 | 2397 | uint8_t blockb0[16U] = {0};/* Block B0 */ |
lypinator | 0:bb348c97df44 | 2398 | uint8_t ctr[16U] = {0}; /* Counter */ |
lypinator | 0:bb348c97df44 | 2399 | uint32_t b0addr = (uint32_t)blockb0; |
lypinator | 0:bb348c97df44 | 2400 | |
lypinator | 0:bb348c97df44 | 2401 | if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) |
lypinator | 0:bb348c97df44 | 2402 | { |
lypinator | 0:bb348c97df44 | 2403 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 2404 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2405 | |
lypinator | 0:bb348c97df44 | 2406 | inputaddr = (uint32_t)pPlainData; |
lypinator | 0:bb348c97df44 | 2407 | outputaddr = (uint32_t)pCypherData; |
lypinator | 0:bb348c97df44 | 2408 | |
lypinator | 0:bb348c97df44 | 2409 | headersize = hcryp->Init.HeaderSize; |
lypinator | 0:bb348c97df44 | 2410 | headeraddr = (uint32_t)hcryp->Init.Header; |
lypinator | 0:bb348c97df44 | 2411 | |
lypinator | 0:bb348c97df44 | 2412 | hcryp->CrypInCount = Size; |
lypinator | 0:bb348c97df44 | 2413 | hcryp->pCrypInBuffPtr = pPlainData; |
lypinator | 0:bb348c97df44 | 2414 | hcryp->pCrypOutBuffPtr = pCypherData; |
lypinator | 0:bb348c97df44 | 2415 | hcryp->CrypOutCount = Size; |
lypinator | 0:bb348c97df44 | 2416 | |
lypinator | 0:bb348c97df44 | 2417 | /* Change the CRYP peripheral state */ |
lypinator | 0:bb348c97df44 | 2418 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 2419 | |
lypinator | 0:bb348c97df44 | 2420 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 2421 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 2422 | { |
lypinator | 0:bb348c97df44 | 2423 | /************************ Formatting the header block *******************/ |
lypinator | 0:bb348c97df44 | 2424 | if(headersize != 0U) |
lypinator | 0:bb348c97df44 | 2425 | { |
lypinator | 0:bb348c97df44 | 2426 | /* Check that the associated data (or header) length is lower than 2^16 - 2^8 = 65536 - 256 = 65280 */ |
lypinator | 0:bb348c97df44 | 2427 | if(headersize < 65280U) |
lypinator | 0:bb348c97df44 | 2428 | { |
lypinator | 0:bb348c97df44 | 2429 | hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize >> 8) & 0xFF); |
lypinator | 0:bb348c97df44 | 2430 | hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize) & 0xFF); |
lypinator | 0:bb348c97df44 | 2431 | headersize += 2U; |
lypinator | 0:bb348c97df44 | 2432 | } |
lypinator | 0:bb348c97df44 | 2433 | else |
lypinator | 0:bb348c97df44 | 2434 | { |
lypinator | 0:bb348c97df44 | 2435 | /* Header is encoded as 0xff || 0xfe || [headersize]32, i.e., six octets */ |
lypinator | 0:bb348c97df44 | 2436 | hcryp->Init.pScratch[bufferidx++] = 0xFFU; |
lypinator | 0:bb348c97df44 | 2437 | hcryp->Init.pScratch[bufferidx++] = 0xFEU; |
lypinator | 0:bb348c97df44 | 2438 | hcryp->Init.pScratch[bufferidx++] = headersize & 0xff000000U; |
lypinator | 0:bb348c97df44 | 2439 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x00ff0000U; |
lypinator | 0:bb348c97df44 | 2440 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x0000ff00U; |
lypinator | 0:bb348c97df44 | 2441 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x000000ffU; |
lypinator | 0:bb348c97df44 | 2442 | headersize += 6U; |
lypinator | 0:bb348c97df44 | 2443 | } |
lypinator | 0:bb348c97df44 | 2444 | /* Copy the header buffer in internal buffer "hcryp->Init.pScratch" */ |
lypinator | 0:bb348c97df44 | 2445 | for(loopcounter = 0U; loopcounter < headersize; loopcounter++) |
lypinator | 0:bb348c97df44 | 2446 | { |
lypinator | 0:bb348c97df44 | 2447 | hcryp->Init.pScratch[bufferidx++] = hcryp->Init.Header[loopcounter]; |
lypinator | 0:bb348c97df44 | 2448 | } |
lypinator | 0:bb348c97df44 | 2449 | /* Check if the header size is modulo 16 */ |
lypinator | 0:bb348c97df44 | 2450 | if ((headersize % 16U) != 0U) |
lypinator | 0:bb348c97df44 | 2451 | { |
lypinator | 0:bb348c97df44 | 2452 | /* Padd the header buffer with 0s till the hcryp->Init.pScratch length is modulo 16 */ |
lypinator | 0:bb348c97df44 | 2453 | for(loopcounter = headersize; loopcounter <= ((headersize/16U) + 1U) * 16U; loopcounter++) |
lypinator | 0:bb348c97df44 | 2454 | { |
lypinator | 0:bb348c97df44 | 2455 | hcryp->Init.pScratch[loopcounter] = 0U; |
lypinator | 0:bb348c97df44 | 2456 | } |
lypinator | 0:bb348c97df44 | 2457 | /* Set the header size to modulo 16 */ |
lypinator | 0:bb348c97df44 | 2458 | headersize = ((headersize/16U) + 1U) * 16U; |
lypinator | 0:bb348c97df44 | 2459 | } |
lypinator | 0:bb348c97df44 | 2460 | /* Set the pointer headeraddr to hcryp->Init.pScratch */ |
lypinator | 0:bb348c97df44 | 2461 | headeraddr = (uint32_t)hcryp->Init.pScratch; |
lypinator | 0:bb348c97df44 | 2462 | } |
lypinator | 0:bb348c97df44 | 2463 | /*********************** Formatting the block B0 ************************/ |
lypinator | 0:bb348c97df44 | 2464 | if(headersize != 0U) |
lypinator | 0:bb348c97df44 | 2465 | { |
lypinator | 0:bb348c97df44 | 2466 | blockb0[0U] = 0x40U; |
lypinator | 0:bb348c97df44 | 2467 | } |
lypinator | 0:bb348c97df44 | 2468 | /* Flags byte */ |
lypinator | 0:bb348c97df44 | 2469 | /* blockb0[0] |= 0u | (((( (uint8_t) hcryp->Init.TagSize - 2) / 2) & 0x07 ) << 3 ) | ( ( (uint8_t) (15 - hcryp->Init.IVSize) - 1) & 0x07U) */ |
lypinator | 0:bb348c97df44 | 2470 | blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)(((uint8_t)(hcryp->Init.TagSize - (uint8_t)(2))) >> 1) & (uint8_t)0x07) << 3); |
lypinator | 0:bb348c97df44 | 2471 | blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)((uint8_t)(15) - hcryp->Init.IVSize) - (uint8_t)1) & (uint8_t)0x07); |
lypinator | 0:bb348c97df44 | 2472 | |
lypinator | 0:bb348c97df44 | 2473 | for (loopcounter = 0U; loopcounter < hcryp->Init.IVSize; loopcounter++) |
lypinator | 0:bb348c97df44 | 2474 | { |
lypinator | 0:bb348c97df44 | 2475 | blockb0[loopcounter+1U] = hcryp->Init.pInitVect[loopcounter]; |
lypinator | 0:bb348c97df44 | 2476 | } |
lypinator | 0:bb348c97df44 | 2477 | for ( ; loopcounter < 13U; loopcounter++) |
lypinator | 0:bb348c97df44 | 2478 | { |
lypinator | 0:bb348c97df44 | 2479 | blockb0[loopcounter+1U] = 0U; |
lypinator | 0:bb348c97df44 | 2480 | } |
lypinator | 0:bb348c97df44 | 2481 | |
lypinator | 0:bb348c97df44 | 2482 | blockb0[14U] = (Size >> 8U); |
lypinator | 0:bb348c97df44 | 2483 | blockb0[15U] = (Size & 0xFFU); |
lypinator | 0:bb348c97df44 | 2484 | |
lypinator | 0:bb348c97df44 | 2485 | /************************* Formatting the initial counter ***************/ |
lypinator | 0:bb348c97df44 | 2486 | /* Byte 0: |
lypinator | 0:bb348c97df44 | 2487 | Bits 7 and 6 are reserved and shall be set to 0 |
lypinator | 0:bb348c97df44 | 2488 | Bits 3, 4, and 5 shall also be set to 0, to ensure that all the counter |
lypinator | 0:bb348c97df44 | 2489 | blocks are distinct from B0 |
lypinator | 0:bb348c97df44 | 2490 | Bits 0, 1, and 2 contain the same encoding of q as in B0 |
lypinator | 0:bb348c97df44 | 2491 | */ |
lypinator | 0:bb348c97df44 | 2492 | ctr[0U] = blockb0[0U] & 0x07U; |
lypinator | 0:bb348c97df44 | 2493 | /* byte 1 to NonceSize is the IV (Nonce) */ |
lypinator | 0:bb348c97df44 | 2494 | for(loopcounter = 1U; loopcounter < hcryp->Init.IVSize + 1U; loopcounter++) |
lypinator | 0:bb348c97df44 | 2495 | { |
lypinator | 0:bb348c97df44 | 2496 | ctr[loopcounter] = blockb0[loopcounter]; |
lypinator | 0:bb348c97df44 | 2497 | } |
lypinator | 0:bb348c97df44 | 2498 | /* Set the LSB to 1 */ |
lypinator | 0:bb348c97df44 | 2499 | ctr[15U] |= 0x01U; |
lypinator | 0:bb348c97df44 | 2500 | |
lypinator | 0:bb348c97df44 | 2501 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 2502 | CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
lypinator | 0:bb348c97df44 | 2503 | |
lypinator | 0:bb348c97df44 | 2504 | /* Set the CRYP peripheral in AES CCM mode */ |
lypinator | 0:bb348c97df44 | 2505 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CCM_ENCRYPT); |
lypinator | 0:bb348c97df44 | 2506 | |
lypinator | 0:bb348c97df44 | 2507 | /* Set the Initialization Vector */ |
lypinator | 0:bb348c97df44 | 2508 | CRYPEx_GCMCCM_SetInitVector(hcryp, ctr); |
lypinator | 0:bb348c97df44 | 2509 | |
lypinator | 0:bb348c97df44 | 2510 | /* Select init phase */ |
lypinator | 0:bb348c97df44 | 2511 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT); |
lypinator | 0:bb348c97df44 | 2512 | |
lypinator | 0:bb348c97df44 | 2513 | b0addr = (uint32_t)blockb0; |
lypinator | 0:bb348c97df44 | 2514 | /* Write the blockb0 block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 2515 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
lypinator | 0:bb348c97df44 | 2516 | b0addr+=4U; |
lypinator | 0:bb348c97df44 | 2517 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
lypinator | 0:bb348c97df44 | 2518 | b0addr+=4U; |
lypinator | 0:bb348c97df44 | 2519 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
lypinator | 0:bb348c97df44 | 2520 | b0addr+=4U; |
lypinator | 0:bb348c97df44 | 2521 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
lypinator | 0:bb348c97df44 | 2522 | |
lypinator | 0:bb348c97df44 | 2523 | /* Enable the CRYP peripheral */ |
lypinator | 0:bb348c97df44 | 2524 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 2525 | |
lypinator | 0:bb348c97df44 | 2526 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 2527 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 2528 | |
lypinator | 0:bb348c97df44 | 2529 | while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN) |
lypinator | 0:bb348c97df44 | 2530 | { |
lypinator | 0:bb348c97df44 | 2531 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 2532 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
lypinator | 0:bb348c97df44 | 2533 | { |
lypinator | 0:bb348c97df44 | 2534 | /* Change state */ |
lypinator | 0:bb348c97df44 | 2535 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2536 | |
lypinator | 0:bb348c97df44 | 2537 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 2538 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2539 | |
lypinator | 0:bb348c97df44 | 2540 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2541 | } |
lypinator | 0:bb348c97df44 | 2542 | } |
lypinator | 0:bb348c97df44 | 2543 | /***************************** Header phase *****************************/ |
lypinator | 0:bb348c97df44 | 2544 | if(headersize != 0U) |
lypinator | 0:bb348c97df44 | 2545 | { |
lypinator | 0:bb348c97df44 | 2546 | /* Select header phase */ |
lypinator | 0:bb348c97df44 | 2547 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER); |
lypinator | 0:bb348c97df44 | 2548 | |
lypinator | 0:bb348c97df44 | 2549 | /* Enable Crypto processor */ |
lypinator | 0:bb348c97df44 | 2550 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 2551 | |
lypinator | 0:bb348c97df44 | 2552 | for(loopcounter = 0U; (loopcounter < headersize); loopcounter+=16U) |
lypinator | 0:bb348c97df44 | 2553 | { |
lypinator | 0:bb348c97df44 | 2554 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 2555 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 2556 | |
lypinator | 0:bb348c97df44 | 2557 | while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM)) |
lypinator | 0:bb348c97df44 | 2558 | { |
lypinator | 0:bb348c97df44 | 2559 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 2560 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
lypinator | 0:bb348c97df44 | 2561 | { |
lypinator | 0:bb348c97df44 | 2562 | /* Change state */ |
lypinator | 0:bb348c97df44 | 2563 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2564 | |
lypinator | 0:bb348c97df44 | 2565 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 2566 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2567 | |
lypinator | 0:bb348c97df44 | 2568 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2569 | } |
lypinator | 0:bb348c97df44 | 2570 | } |
lypinator | 0:bb348c97df44 | 2571 | /* Write the header block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 2572 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
lypinator | 0:bb348c97df44 | 2573 | headeraddr+=4U; |
lypinator | 0:bb348c97df44 | 2574 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
lypinator | 0:bb348c97df44 | 2575 | headeraddr+=4U; |
lypinator | 0:bb348c97df44 | 2576 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
lypinator | 0:bb348c97df44 | 2577 | headeraddr+=4U; |
lypinator | 0:bb348c97df44 | 2578 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
lypinator | 0:bb348c97df44 | 2579 | headeraddr+=4U; |
lypinator | 0:bb348c97df44 | 2580 | } |
lypinator | 0:bb348c97df44 | 2581 | |
lypinator | 0:bb348c97df44 | 2582 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 2583 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 2584 | |
lypinator | 0:bb348c97df44 | 2585 | while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY) |
lypinator | 0:bb348c97df44 | 2586 | { |
lypinator | 0:bb348c97df44 | 2587 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 2588 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
lypinator | 0:bb348c97df44 | 2589 | { |
lypinator | 0:bb348c97df44 | 2590 | /* Change state */ |
lypinator | 0:bb348c97df44 | 2591 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2592 | |
lypinator | 0:bb348c97df44 | 2593 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 2594 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2595 | |
lypinator | 0:bb348c97df44 | 2596 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2597 | } |
lypinator | 0:bb348c97df44 | 2598 | } |
lypinator | 0:bb348c97df44 | 2599 | } |
lypinator | 0:bb348c97df44 | 2600 | /* Save formatted counter into the scratch buffer pScratch */ |
lypinator | 0:bb348c97df44 | 2601 | for(loopcounter = 0U; (loopcounter < 16U); loopcounter++) |
lypinator | 0:bb348c97df44 | 2602 | { |
lypinator | 0:bb348c97df44 | 2603 | hcryp->Init.pScratch[loopcounter] = ctr[loopcounter]; |
lypinator | 0:bb348c97df44 | 2604 | } |
lypinator | 0:bb348c97df44 | 2605 | /* Reset bit 0 */ |
lypinator | 0:bb348c97df44 | 2606 | hcryp->Init.pScratch[15U] &= 0xFEU; |
lypinator | 0:bb348c97df44 | 2607 | |
lypinator | 0:bb348c97df44 | 2608 | /* Select payload phase once the header phase is performed */ |
lypinator | 0:bb348c97df44 | 2609 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD); |
lypinator | 0:bb348c97df44 | 2610 | |
lypinator | 0:bb348c97df44 | 2611 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 2612 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 2613 | |
lypinator | 0:bb348c97df44 | 2614 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 2615 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 2616 | } |
lypinator | 0:bb348c97df44 | 2617 | |
lypinator | 0:bb348c97df44 | 2618 | /* Set the input and output addresses and start DMA transfer */ |
lypinator | 0:bb348c97df44 | 2619 | CRYPEx_GCMCCM_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); |
lypinator | 0:bb348c97df44 | 2620 | |
lypinator | 0:bb348c97df44 | 2621 | /* Unlock process */ |
lypinator | 0:bb348c97df44 | 2622 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2623 | |
lypinator | 0:bb348c97df44 | 2624 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 2625 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2626 | } |
lypinator | 0:bb348c97df44 | 2627 | else |
lypinator | 0:bb348c97df44 | 2628 | { |
lypinator | 0:bb348c97df44 | 2629 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 2630 | } |
lypinator | 0:bb348c97df44 | 2631 | } |
lypinator | 0:bb348c97df44 | 2632 | |
lypinator | 0:bb348c97df44 | 2633 | /** |
lypinator | 0:bb348c97df44 | 2634 | * @brief Initializes the CRYP peripheral in AES GCM decryption mode using DMA. |
lypinator | 0:bb348c97df44 | 2635 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 2636 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 2637 | * @param pCypherData Pointer to the cyphertext buffer. |
lypinator | 0:bb348c97df44 | 2638 | * @param Size Length of the cyphertext buffer, must be a multiple of 16 |
lypinator | 0:bb348c97df44 | 2639 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 2640 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2641 | */ |
lypinator | 0:bb348c97df44 | 2642 | HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) |
lypinator | 0:bb348c97df44 | 2643 | { |
lypinator | 0:bb348c97df44 | 2644 | uint32_t tickstart = 0U; |
lypinator | 0:bb348c97df44 | 2645 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 2646 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 2647 | |
lypinator | 0:bb348c97df44 | 2648 | if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) |
lypinator | 0:bb348c97df44 | 2649 | { |
lypinator | 0:bb348c97df44 | 2650 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 2651 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2652 | |
lypinator | 0:bb348c97df44 | 2653 | inputaddr = (uint32_t)pCypherData; |
lypinator | 0:bb348c97df44 | 2654 | outputaddr = (uint32_t)pPlainData; |
lypinator | 0:bb348c97df44 | 2655 | |
lypinator | 0:bb348c97df44 | 2656 | /* Change the CRYP peripheral state */ |
lypinator | 0:bb348c97df44 | 2657 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 2658 | |
lypinator | 0:bb348c97df44 | 2659 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 2660 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 2661 | { |
lypinator | 0:bb348c97df44 | 2662 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 2663 | CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
lypinator | 0:bb348c97df44 | 2664 | |
lypinator | 0:bb348c97df44 | 2665 | /* Set the CRYP peripheral in AES GCM decryption mode */ |
lypinator | 0:bb348c97df44 | 2666 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_GCM_DECRYPT); |
lypinator | 0:bb348c97df44 | 2667 | |
lypinator | 0:bb348c97df44 | 2668 | /* Set the Initialization Vector */ |
lypinator | 0:bb348c97df44 | 2669 | CRYPEx_GCMCCM_SetInitVector(hcryp, hcryp->Init.pInitVect); |
lypinator | 0:bb348c97df44 | 2670 | |
lypinator | 0:bb348c97df44 | 2671 | /* Enable CRYP to start the init phase */ |
lypinator | 0:bb348c97df44 | 2672 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 2673 | |
lypinator | 0:bb348c97df44 | 2674 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 2675 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 2676 | |
lypinator | 0:bb348c97df44 | 2677 | while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN) |
lypinator | 0:bb348c97df44 | 2678 | { |
lypinator | 0:bb348c97df44 | 2679 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 2680 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
lypinator | 0:bb348c97df44 | 2681 | { |
lypinator | 0:bb348c97df44 | 2682 | /* Change state */ |
lypinator | 0:bb348c97df44 | 2683 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2684 | |
lypinator | 0:bb348c97df44 | 2685 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 2686 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2687 | |
lypinator | 0:bb348c97df44 | 2688 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2689 | } |
lypinator | 0:bb348c97df44 | 2690 | } |
lypinator | 0:bb348c97df44 | 2691 | |
lypinator | 0:bb348c97df44 | 2692 | /* Set the header phase */ |
lypinator | 0:bb348c97df44 | 2693 | if(CRYPEx_GCMCCM_SetHeaderPhase(hcryp, hcryp->Init.Header, hcryp->Init.HeaderSize, 1U) != HAL_OK) |
lypinator | 0:bb348c97df44 | 2694 | { |
lypinator | 0:bb348c97df44 | 2695 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2696 | } |
lypinator | 0:bb348c97df44 | 2697 | /* Disable the CRYP peripheral */ |
lypinator | 0:bb348c97df44 | 2698 | __HAL_CRYP_DISABLE(hcryp); |
lypinator | 0:bb348c97df44 | 2699 | |
lypinator | 0:bb348c97df44 | 2700 | /* Select payload phase once the header phase is performed */ |
lypinator | 0:bb348c97df44 | 2701 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD); |
lypinator | 0:bb348c97df44 | 2702 | |
lypinator | 0:bb348c97df44 | 2703 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 2704 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 2705 | } |
lypinator | 0:bb348c97df44 | 2706 | |
lypinator | 0:bb348c97df44 | 2707 | /* Set the input and output addresses and start DMA transfer */ |
lypinator | 0:bb348c97df44 | 2708 | CRYPEx_GCMCCM_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); |
lypinator | 0:bb348c97df44 | 2709 | |
lypinator | 0:bb348c97df44 | 2710 | /* Unlock process */ |
lypinator | 0:bb348c97df44 | 2711 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2712 | |
lypinator | 0:bb348c97df44 | 2713 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 2714 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2715 | } |
lypinator | 0:bb348c97df44 | 2716 | else |
lypinator | 0:bb348c97df44 | 2717 | { |
lypinator | 0:bb348c97df44 | 2718 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 2719 | } |
lypinator | 0:bb348c97df44 | 2720 | } |
lypinator | 0:bb348c97df44 | 2721 | |
lypinator | 0:bb348c97df44 | 2722 | /** |
lypinator | 0:bb348c97df44 | 2723 | * @brief Initializes the CRYP peripheral in AES CCM decryption mode using DMA |
lypinator | 0:bb348c97df44 | 2724 | * then decrypted pCypherData. The cypher data are available in pPlainData. |
lypinator | 0:bb348c97df44 | 2725 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 2726 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 2727 | * @param pCypherData Pointer to the cyphertext buffer |
lypinator | 0:bb348c97df44 | 2728 | * @param Size Length of the plaintext buffer, must be a multiple of 16 |
lypinator | 0:bb348c97df44 | 2729 | * @param pPlainData Pointer to the plaintext buffer |
lypinator | 0:bb348c97df44 | 2730 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2731 | */ |
lypinator | 0:bb348c97df44 | 2732 | HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) |
lypinator | 0:bb348c97df44 | 2733 | { |
lypinator | 0:bb348c97df44 | 2734 | uint32_t tickstart = 0U; |
lypinator | 0:bb348c97df44 | 2735 | uint32_t inputaddr; |
lypinator | 0:bb348c97df44 | 2736 | uint32_t outputaddr; |
lypinator | 0:bb348c97df44 | 2737 | uint32_t headersize; |
lypinator | 0:bb348c97df44 | 2738 | uint32_t headeraddr; |
lypinator | 0:bb348c97df44 | 2739 | uint32_t loopcounter = 0U; |
lypinator | 0:bb348c97df44 | 2740 | uint32_t bufferidx = 0U; |
lypinator | 0:bb348c97df44 | 2741 | uint8_t blockb0[16U] = {0};/* Block B0 */ |
lypinator | 0:bb348c97df44 | 2742 | uint8_t ctr[16U] = {0}; /* Counter */ |
lypinator | 0:bb348c97df44 | 2743 | uint32_t b0addr = (uint32_t)blockb0; |
lypinator | 0:bb348c97df44 | 2744 | |
lypinator | 0:bb348c97df44 | 2745 | if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) |
lypinator | 0:bb348c97df44 | 2746 | { |
lypinator | 0:bb348c97df44 | 2747 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 2748 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2749 | |
lypinator | 0:bb348c97df44 | 2750 | inputaddr = (uint32_t)pCypherData; |
lypinator | 0:bb348c97df44 | 2751 | outputaddr = (uint32_t)pPlainData; |
lypinator | 0:bb348c97df44 | 2752 | |
lypinator | 0:bb348c97df44 | 2753 | headersize = hcryp->Init.HeaderSize; |
lypinator | 0:bb348c97df44 | 2754 | headeraddr = (uint32_t)hcryp->Init.Header; |
lypinator | 0:bb348c97df44 | 2755 | |
lypinator | 0:bb348c97df44 | 2756 | hcryp->CrypInCount = Size; |
lypinator | 0:bb348c97df44 | 2757 | hcryp->pCrypInBuffPtr = pCypherData; |
lypinator | 0:bb348c97df44 | 2758 | hcryp->pCrypOutBuffPtr = pPlainData; |
lypinator | 0:bb348c97df44 | 2759 | hcryp->CrypOutCount = Size; |
lypinator | 0:bb348c97df44 | 2760 | |
lypinator | 0:bb348c97df44 | 2761 | /* Change the CRYP peripheral state */ |
lypinator | 0:bb348c97df44 | 2762 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 2763 | |
lypinator | 0:bb348c97df44 | 2764 | /* Check if initialization phase has already been performed */ |
lypinator | 0:bb348c97df44 | 2765 | if(hcryp->Phase == HAL_CRYP_PHASE_READY) |
lypinator | 0:bb348c97df44 | 2766 | { |
lypinator | 0:bb348c97df44 | 2767 | /************************ Formatting the header block *******************/ |
lypinator | 0:bb348c97df44 | 2768 | if(headersize != 0U) |
lypinator | 0:bb348c97df44 | 2769 | { |
lypinator | 0:bb348c97df44 | 2770 | /* Check that the associated data (or header) length is lower than 2^16 - 2^8 = 65536 - 256 = 65280 */ |
lypinator | 0:bb348c97df44 | 2771 | if(headersize < 65280U) |
lypinator | 0:bb348c97df44 | 2772 | { |
lypinator | 0:bb348c97df44 | 2773 | hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize >> 8) & 0xFF); |
lypinator | 0:bb348c97df44 | 2774 | hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize) & 0xFF); |
lypinator | 0:bb348c97df44 | 2775 | headersize += 2U; |
lypinator | 0:bb348c97df44 | 2776 | } |
lypinator | 0:bb348c97df44 | 2777 | else |
lypinator | 0:bb348c97df44 | 2778 | { |
lypinator | 0:bb348c97df44 | 2779 | /* Header is encoded as 0xff || 0xfe || [headersize]32, i.e., six octets */ |
lypinator | 0:bb348c97df44 | 2780 | hcryp->Init.pScratch[bufferidx++] = 0xFFU; |
lypinator | 0:bb348c97df44 | 2781 | hcryp->Init.pScratch[bufferidx++] = 0xFEU; |
lypinator | 0:bb348c97df44 | 2782 | hcryp->Init.pScratch[bufferidx++] = headersize & 0xff000000U; |
lypinator | 0:bb348c97df44 | 2783 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x00ff0000U; |
lypinator | 0:bb348c97df44 | 2784 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x0000ff00U; |
lypinator | 0:bb348c97df44 | 2785 | hcryp->Init.pScratch[bufferidx++] = headersize & 0x000000ffU; |
lypinator | 0:bb348c97df44 | 2786 | headersize += 6U; |
lypinator | 0:bb348c97df44 | 2787 | } |
lypinator | 0:bb348c97df44 | 2788 | /* Copy the header buffer in internal buffer "hcryp->Init.pScratch" */ |
lypinator | 0:bb348c97df44 | 2789 | for(loopcounter = 0U; loopcounter < headersize; loopcounter++) |
lypinator | 0:bb348c97df44 | 2790 | { |
lypinator | 0:bb348c97df44 | 2791 | hcryp->Init.pScratch[bufferidx++] = hcryp->Init.Header[loopcounter]; |
lypinator | 0:bb348c97df44 | 2792 | } |
lypinator | 0:bb348c97df44 | 2793 | /* Check if the header size is modulo 16 */ |
lypinator | 0:bb348c97df44 | 2794 | if ((headersize % 16U) != 0U) |
lypinator | 0:bb348c97df44 | 2795 | { |
lypinator | 0:bb348c97df44 | 2796 | /* Padd the header buffer with 0s till the hcryp->Init.pScratch length is modulo 16 */ |
lypinator | 0:bb348c97df44 | 2797 | for(loopcounter = headersize; loopcounter <= ((headersize/16U) + 1U) * 16U; loopcounter++) |
lypinator | 0:bb348c97df44 | 2798 | { |
lypinator | 0:bb348c97df44 | 2799 | hcryp->Init.pScratch[loopcounter] = 0U; |
lypinator | 0:bb348c97df44 | 2800 | } |
lypinator | 0:bb348c97df44 | 2801 | /* Set the header size to modulo 16 */ |
lypinator | 0:bb348c97df44 | 2802 | headersize = ((headersize/16U) + 1U) * 16U; |
lypinator | 0:bb348c97df44 | 2803 | } |
lypinator | 0:bb348c97df44 | 2804 | /* Set the pointer headeraddr to hcryp->Init.pScratch */ |
lypinator | 0:bb348c97df44 | 2805 | headeraddr = (uint32_t)hcryp->Init.pScratch; |
lypinator | 0:bb348c97df44 | 2806 | } |
lypinator | 0:bb348c97df44 | 2807 | /*********************** Formatting the block B0 ************************/ |
lypinator | 0:bb348c97df44 | 2808 | if(headersize != 0U) |
lypinator | 0:bb348c97df44 | 2809 | { |
lypinator | 0:bb348c97df44 | 2810 | blockb0[0U] = 0x40U; |
lypinator | 0:bb348c97df44 | 2811 | } |
lypinator | 0:bb348c97df44 | 2812 | /* Flags byte */ |
lypinator | 0:bb348c97df44 | 2813 | /* blockb0[0] |= 0u | (((( (uint8_t) hcryp->Init.TagSize - 2) / 2) & 0x07 ) << 3 ) | ( ( (uint8_t) (15 - hcryp->Init.IVSize) - 1) & 0x07U) */ |
lypinator | 0:bb348c97df44 | 2814 | blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)(((uint8_t)(hcryp->Init.TagSize - (uint8_t)(2))) >> 1) & (uint8_t)0x07) << 3); |
lypinator | 0:bb348c97df44 | 2815 | blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)((uint8_t)(15) - hcryp->Init.IVSize) - (uint8_t)1) & (uint8_t)0x07); |
lypinator | 0:bb348c97df44 | 2816 | |
lypinator | 0:bb348c97df44 | 2817 | for (loopcounter = 0U; loopcounter < hcryp->Init.IVSize; loopcounter++) |
lypinator | 0:bb348c97df44 | 2818 | { |
lypinator | 0:bb348c97df44 | 2819 | blockb0[loopcounter+1U] = hcryp->Init.pInitVect[loopcounter]; |
lypinator | 0:bb348c97df44 | 2820 | } |
lypinator | 0:bb348c97df44 | 2821 | for ( ; loopcounter < 13U; loopcounter++) |
lypinator | 0:bb348c97df44 | 2822 | { |
lypinator | 0:bb348c97df44 | 2823 | blockb0[loopcounter+1U] = 0U; |
lypinator | 0:bb348c97df44 | 2824 | } |
lypinator | 0:bb348c97df44 | 2825 | |
lypinator | 0:bb348c97df44 | 2826 | blockb0[14U] = (Size >> 8U); |
lypinator | 0:bb348c97df44 | 2827 | blockb0[15U] = (Size & 0xFFU); |
lypinator | 0:bb348c97df44 | 2828 | |
lypinator | 0:bb348c97df44 | 2829 | /************************* Formatting the initial counter ***************/ |
lypinator | 0:bb348c97df44 | 2830 | /* Byte 0: |
lypinator | 0:bb348c97df44 | 2831 | Bits 7 and 6 are reserved and shall be set to 0 |
lypinator | 0:bb348c97df44 | 2832 | Bits 3, 4, and 5 shall also be set to 0, to ensure that all the counter |
lypinator | 0:bb348c97df44 | 2833 | blocks are distinct from B0 |
lypinator | 0:bb348c97df44 | 2834 | Bits 0, 1, and 2 contain the same encoding of q as in B0 |
lypinator | 0:bb348c97df44 | 2835 | */ |
lypinator | 0:bb348c97df44 | 2836 | ctr[0U] = blockb0[0U] & 0x07U; |
lypinator | 0:bb348c97df44 | 2837 | /* byte 1 to NonceSize is the IV (Nonce) */ |
lypinator | 0:bb348c97df44 | 2838 | for(loopcounter = 1U; loopcounter < hcryp->Init.IVSize + 1U; loopcounter++) |
lypinator | 0:bb348c97df44 | 2839 | { |
lypinator | 0:bb348c97df44 | 2840 | ctr[loopcounter] = blockb0[loopcounter]; |
lypinator | 0:bb348c97df44 | 2841 | } |
lypinator | 0:bb348c97df44 | 2842 | /* Set the LSB to 1 */ |
lypinator | 0:bb348c97df44 | 2843 | ctr[15U] |= 0x01U; |
lypinator | 0:bb348c97df44 | 2844 | |
lypinator | 0:bb348c97df44 | 2845 | /* Set the key */ |
lypinator | 0:bb348c97df44 | 2846 | CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); |
lypinator | 0:bb348c97df44 | 2847 | |
lypinator | 0:bb348c97df44 | 2848 | /* Set the CRYP peripheral in AES CCM mode */ |
lypinator | 0:bb348c97df44 | 2849 | __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CCM_DECRYPT); |
lypinator | 0:bb348c97df44 | 2850 | |
lypinator | 0:bb348c97df44 | 2851 | /* Set the Initialization Vector */ |
lypinator | 0:bb348c97df44 | 2852 | CRYPEx_GCMCCM_SetInitVector(hcryp, ctr); |
lypinator | 0:bb348c97df44 | 2853 | |
lypinator | 0:bb348c97df44 | 2854 | /* Select init phase */ |
lypinator | 0:bb348c97df44 | 2855 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT); |
lypinator | 0:bb348c97df44 | 2856 | |
lypinator | 0:bb348c97df44 | 2857 | b0addr = (uint32_t)blockb0; |
lypinator | 0:bb348c97df44 | 2858 | /* Write the blockb0 block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 2859 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
lypinator | 0:bb348c97df44 | 2860 | b0addr+=4U; |
lypinator | 0:bb348c97df44 | 2861 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
lypinator | 0:bb348c97df44 | 2862 | b0addr+=4U; |
lypinator | 0:bb348c97df44 | 2863 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
lypinator | 0:bb348c97df44 | 2864 | b0addr+=4U; |
lypinator | 0:bb348c97df44 | 2865 | hcryp->Instance->DR = *(uint32_t*)(b0addr); |
lypinator | 0:bb348c97df44 | 2866 | |
lypinator | 0:bb348c97df44 | 2867 | /* Enable the CRYP peripheral */ |
lypinator | 0:bb348c97df44 | 2868 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 2869 | |
lypinator | 0:bb348c97df44 | 2870 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 2871 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 2872 | |
lypinator | 0:bb348c97df44 | 2873 | while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN) |
lypinator | 0:bb348c97df44 | 2874 | { |
lypinator | 0:bb348c97df44 | 2875 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 2876 | |
lypinator | 0:bb348c97df44 | 2877 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
lypinator | 0:bb348c97df44 | 2878 | { |
lypinator | 0:bb348c97df44 | 2879 | /* Change state */ |
lypinator | 0:bb348c97df44 | 2880 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2881 | |
lypinator | 0:bb348c97df44 | 2882 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 2883 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2884 | |
lypinator | 0:bb348c97df44 | 2885 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2886 | |
lypinator | 0:bb348c97df44 | 2887 | } |
lypinator | 0:bb348c97df44 | 2888 | } |
lypinator | 0:bb348c97df44 | 2889 | /***************************** Header phase *****************************/ |
lypinator | 0:bb348c97df44 | 2890 | if(headersize != 0U) |
lypinator | 0:bb348c97df44 | 2891 | { |
lypinator | 0:bb348c97df44 | 2892 | /* Select header phase */ |
lypinator | 0:bb348c97df44 | 2893 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER); |
lypinator | 0:bb348c97df44 | 2894 | |
lypinator | 0:bb348c97df44 | 2895 | /* Enable Crypto processor */ |
lypinator | 0:bb348c97df44 | 2896 | __HAL_CRYP_ENABLE(hcryp); |
lypinator | 0:bb348c97df44 | 2897 | |
lypinator | 0:bb348c97df44 | 2898 | for(loopcounter = 0U; (loopcounter < headersize); loopcounter+=16U) |
lypinator | 0:bb348c97df44 | 2899 | { |
lypinator | 0:bb348c97df44 | 2900 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 2901 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 2902 | |
lypinator | 0:bb348c97df44 | 2903 | while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM)) |
lypinator | 0:bb348c97df44 | 2904 | { |
lypinator | 0:bb348c97df44 | 2905 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 2906 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
lypinator | 0:bb348c97df44 | 2907 | { |
lypinator | 0:bb348c97df44 | 2908 | /* Change state */ |
lypinator | 0:bb348c97df44 | 2909 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2910 | |
lypinator | 0:bb348c97df44 | 2911 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 2912 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2913 | |
lypinator | 0:bb348c97df44 | 2914 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2915 | } |
lypinator | 0:bb348c97df44 | 2916 | } |
lypinator | 0:bb348c97df44 | 2917 | /* Write the header block in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 2918 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
lypinator | 0:bb348c97df44 | 2919 | headeraddr+=4U; |
lypinator | 0:bb348c97df44 | 2920 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
lypinator | 0:bb348c97df44 | 2921 | headeraddr+=4U; |
lypinator | 0:bb348c97df44 | 2922 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
lypinator | 0:bb348c97df44 | 2923 | headeraddr+=4U; |
lypinator | 0:bb348c97df44 | 2924 | hcryp->Instance->DR = *(uint32_t*)(headeraddr); |
lypinator | 0:bb348c97df44 | 2925 | headeraddr+=4U; |
lypinator | 0:bb348c97df44 | 2926 | } |
lypinator | 0:bb348c97df44 | 2927 | |
lypinator | 0:bb348c97df44 | 2928 | /* Get tick */ |
lypinator | 0:bb348c97df44 | 2929 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 2930 | |
lypinator | 0:bb348c97df44 | 2931 | while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY) |
lypinator | 0:bb348c97df44 | 2932 | { |
lypinator | 0:bb348c97df44 | 2933 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 2934 | if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE) |
lypinator | 0:bb348c97df44 | 2935 | { |
lypinator | 0:bb348c97df44 | 2936 | /* Change state */ |
lypinator | 0:bb348c97df44 | 2937 | hcryp->State = HAL_CRYP_STATE_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2938 | |
lypinator | 0:bb348c97df44 | 2939 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 2940 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2941 | |
lypinator | 0:bb348c97df44 | 2942 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2943 | } |
lypinator | 0:bb348c97df44 | 2944 | } |
lypinator | 0:bb348c97df44 | 2945 | } |
lypinator | 0:bb348c97df44 | 2946 | /* Save formatted counter into the scratch buffer pScratch */ |
lypinator | 0:bb348c97df44 | 2947 | for(loopcounter = 0U; (loopcounter < 16U); loopcounter++) |
lypinator | 0:bb348c97df44 | 2948 | { |
lypinator | 0:bb348c97df44 | 2949 | hcryp->Init.pScratch[loopcounter] = ctr[loopcounter]; |
lypinator | 0:bb348c97df44 | 2950 | } |
lypinator | 0:bb348c97df44 | 2951 | /* Reset bit 0 */ |
lypinator | 0:bb348c97df44 | 2952 | hcryp->Init.pScratch[15U] &= 0xFEU; |
lypinator | 0:bb348c97df44 | 2953 | /* Select payload phase once the header phase is performed */ |
lypinator | 0:bb348c97df44 | 2954 | __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD); |
lypinator | 0:bb348c97df44 | 2955 | |
lypinator | 0:bb348c97df44 | 2956 | /* Flush FIFO */ |
lypinator | 0:bb348c97df44 | 2957 | __HAL_CRYP_FIFO_FLUSH(hcryp); |
lypinator | 0:bb348c97df44 | 2958 | |
lypinator | 0:bb348c97df44 | 2959 | /* Set the phase */ |
lypinator | 0:bb348c97df44 | 2960 | hcryp->Phase = HAL_CRYP_PHASE_PROCESS; |
lypinator | 0:bb348c97df44 | 2961 | } |
lypinator | 0:bb348c97df44 | 2962 | /* Set the input and output addresses and start DMA transfer */ |
lypinator | 0:bb348c97df44 | 2963 | CRYPEx_GCMCCM_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); |
lypinator | 0:bb348c97df44 | 2964 | |
lypinator | 0:bb348c97df44 | 2965 | /* Unlock process */ |
lypinator | 0:bb348c97df44 | 2966 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 2967 | |
lypinator | 0:bb348c97df44 | 2968 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 2969 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2970 | } |
lypinator | 0:bb348c97df44 | 2971 | else |
lypinator | 0:bb348c97df44 | 2972 | { |
lypinator | 0:bb348c97df44 | 2973 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 2974 | } |
lypinator | 0:bb348c97df44 | 2975 | } |
lypinator | 0:bb348c97df44 | 2976 | |
lypinator | 0:bb348c97df44 | 2977 | /** |
lypinator | 0:bb348c97df44 | 2978 | * @} |
lypinator | 0:bb348c97df44 | 2979 | */ |
lypinator | 0:bb348c97df44 | 2980 | |
lypinator | 0:bb348c97df44 | 2981 | /** @defgroup CRYPEx_Exported_Functions_Group2 CRYPEx IRQ handler management |
lypinator | 0:bb348c97df44 | 2982 | * @brief CRYPEx IRQ handler. |
lypinator | 0:bb348c97df44 | 2983 | * |
lypinator | 0:bb348c97df44 | 2984 | @verbatim |
lypinator | 0:bb348c97df44 | 2985 | ============================================================================== |
lypinator | 0:bb348c97df44 | 2986 | ##### CRYPEx IRQ handler management ##### |
lypinator | 0:bb348c97df44 | 2987 | ============================================================================== |
lypinator | 0:bb348c97df44 | 2988 | [..] This section provides CRYPEx IRQ handler function. |
lypinator | 0:bb348c97df44 | 2989 | |
lypinator | 0:bb348c97df44 | 2990 | @endverbatim |
lypinator | 0:bb348c97df44 | 2991 | * @{ |
lypinator | 0:bb348c97df44 | 2992 | */ |
lypinator | 0:bb348c97df44 | 2993 | |
lypinator | 0:bb348c97df44 | 2994 | /** |
lypinator | 0:bb348c97df44 | 2995 | * @brief This function handles CRYPEx interrupt request. |
lypinator | 0:bb348c97df44 | 2996 | * @param hcryp pointer to a CRYPEx_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 2997 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 2998 | * @retval None |
lypinator | 0:bb348c97df44 | 2999 | */ |
lypinator | 0:bb348c97df44 | 3000 | |
lypinator | 0:bb348c97df44 | 3001 | void HAL_CRYPEx_GCMCCM_IRQHandler(CRYP_HandleTypeDef *hcryp) |
lypinator | 0:bb348c97df44 | 3002 | { |
lypinator | 0:bb348c97df44 | 3003 | switch(CRYP->CR & CRYP_CR_ALGOMODE_DIRECTION) |
lypinator | 0:bb348c97df44 | 3004 | { |
lypinator | 0:bb348c97df44 | 3005 | case CRYP_CR_ALGOMODE_AES_GCM_ENCRYPT: |
lypinator | 0:bb348c97df44 | 3006 | HAL_CRYPEx_AESGCM_Encrypt_IT(hcryp, NULL, 0U, NULL); |
lypinator | 0:bb348c97df44 | 3007 | break; |
lypinator | 0:bb348c97df44 | 3008 | |
lypinator | 0:bb348c97df44 | 3009 | case CRYP_CR_ALGOMODE_AES_GCM_DECRYPT: |
lypinator | 0:bb348c97df44 | 3010 | HAL_CRYPEx_AESGCM_Decrypt_IT(hcryp, NULL, 0U, NULL); |
lypinator | 0:bb348c97df44 | 3011 | break; |
lypinator | 0:bb348c97df44 | 3012 | |
lypinator | 0:bb348c97df44 | 3013 | case CRYP_CR_ALGOMODE_AES_CCM_ENCRYPT: |
lypinator | 0:bb348c97df44 | 3014 | HAL_CRYPEx_AESCCM_Encrypt_IT(hcryp, NULL, 0U, NULL); |
lypinator | 0:bb348c97df44 | 3015 | break; |
lypinator | 0:bb348c97df44 | 3016 | |
lypinator | 0:bb348c97df44 | 3017 | case CRYP_CR_ALGOMODE_AES_CCM_DECRYPT: |
lypinator | 0:bb348c97df44 | 3018 | HAL_CRYPEx_AESCCM_Decrypt_IT(hcryp, NULL, 0U, NULL); |
lypinator | 0:bb348c97df44 | 3019 | break; |
lypinator | 0:bb348c97df44 | 3020 | |
lypinator | 0:bb348c97df44 | 3021 | default: |
lypinator | 0:bb348c97df44 | 3022 | break; |
lypinator | 0:bb348c97df44 | 3023 | } |
lypinator | 0:bb348c97df44 | 3024 | } |
lypinator | 0:bb348c97df44 | 3025 | |
lypinator | 0:bb348c97df44 | 3026 | /** |
lypinator | 0:bb348c97df44 | 3027 | * @} |
lypinator | 0:bb348c97df44 | 3028 | */ |
lypinator | 0:bb348c97df44 | 3029 | |
lypinator | 0:bb348c97df44 | 3030 | /** |
lypinator | 0:bb348c97df44 | 3031 | * @} |
lypinator | 0:bb348c97df44 | 3032 | */ |
lypinator | 0:bb348c97df44 | 3033 | #endif /* CRYP */ |
lypinator | 0:bb348c97df44 | 3034 | |
lypinator | 0:bb348c97df44 | 3035 | #if defined (AES) |
lypinator | 0:bb348c97df44 | 3036 | |
lypinator | 0:bb348c97df44 | 3037 | /** @defgroup CRYPEx_Private_Constants CRYPEx Private Constants |
lypinator | 0:bb348c97df44 | 3038 | * @{ |
lypinator | 0:bb348c97df44 | 3039 | */ |
lypinator | 0:bb348c97df44 | 3040 | #define CRYP_CCF_TIMEOUTVALUE 22000U /*!< CCF flag raising time-out value */ |
lypinator | 0:bb348c97df44 | 3041 | #define CRYP_BUSY_TIMEOUTVALUE 22000U /*!< BUSY flag reset time-out value */ |
lypinator | 0:bb348c97df44 | 3042 | |
lypinator | 0:bb348c97df44 | 3043 | #define CRYP_POLLING_OFF 0x0U /*!< No polling when padding */ |
lypinator | 0:bb348c97df44 | 3044 | #define CRYP_POLLING_ON 0x1U /*!< Polling when padding */ |
lypinator | 0:bb348c97df44 | 3045 | /** |
lypinator | 0:bb348c97df44 | 3046 | * @} |
lypinator | 0:bb348c97df44 | 3047 | */ |
lypinator | 0:bb348c97df44 | 3048 | |
lypinator | 0:bb348c97df44 | 3049 | /* Private macro -------------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 3050 | /* Private variables ---------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 3051 | /* Private function prototypes -----------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 3052 | /** @defgroup CRYPEx_Private_Functions CRYPEx Private Functions |
lypinator | 0:bb348c97df44 | 3053 | * @{ |
lypinator | 0:bb348c97df44 | 3054 | */ |
lypinator | 0:bb348c97df44 | 3055 | static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout); |
lypinator | 0:bb348c97df44 | 3056 | static HAL_StatusTypeDef CRYP_ReadKey(CRYP_HandleTypeDef *hcryp, uint8_t* Output, uint32_t Timeout); |
lypinator | 0:bb348c97df44 | 3057 | static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr); |
lypinator | 0:bb348c97df44 | 3058 | static void CRYP_GCMCMAC_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr); |
lypinator | 0:bb348c97df44 | 3059 | static void CRYP_GCMCMAC_DMAInCplt(DMA_HandleTypeDef *hdma); |
lypinator | 0:bb348c97df44 | 3060 | static void CRYP_GCMCMAC_DMAError(DMA_HandleTypeDef *hdma); |
lypinator | 0:bb348c97df44 | 3061 | static void CRYP_GCMCMAC_DMAOutCplt(DMA_HandleTypeDef *hdma); |
lypinator | 0:bb348c97df44 | 3062 | static HAL_StatusTypeDef CRYP_WaitOnCCFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout); |
lypinator | 0:bb348c97df44 | 3063 | static HAL_StatusTypeDef CRYP_WaitOnBusyFlagReset(CRYP_HandleTypeDef *hcryp, uint32_t Timeout); |
lypinator | 0:bb348c97df44 | 3064 | static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma); |
lypinator | 0:bb348c97df44 | 3065 | static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma); |
lypinator | 0:bb348c97df44 | 3066 | static void CRYP_DMAError(DMA_HandleTypeDef *hdma); |
lypinator | 0:bb348c97df44 | 3067 | static void CRYP_Padding(CRYP_HandleTypeDef *hcryp, uint32_t difflength, uint32_t polling); |
lypinator | 0:bb348c97df44 | 3068 | /** |
lypinator | 0:bb348c97df44 | 3069 | * @} |
lypinator | 0:bb348c97df44 | 3070 | */ |
lypinator | 0:bb348c97df44 | 3071 | |
lypinator | 0:bb348c97df44 | 3072 | /* Exported functions ---------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 3073 | |
lypinator | 0:bb348c97df44 | 3074 | /** @defgroup CRYPEx_Exported_Functions CRYPEx Exported Functions |
lypinator | 0:bb348c97df44 | 3075 | * @{ |
lypinator | 0:bb348c97df44 | 3076 | */ |
lypinator | 0:bb348c97df44 | 3077 | |
lypinator | 0:bb348c97df44 | 3078 | |
lypinator | 0:bb348c97df44 | 3079 | /** @defgroup CRYPEx_Exported_Functions_Group1 Extended callback function |
lypinator | 0:bb348c97df44 | 3080 | * @brief Extended callback functions. |
lypinator | 0:bb348c97df44 | 3081 | * |
lypinator | 0:bb348c97df44 | 3082 | @verbatim |
lypinator | 0:bb348c97df44 | 3083 | =============================================================================== |
lypinator | 0:bb348c97df44 | 3084 | ##### Extended callback functions ##### |
lypinator | 0:bb348c97df44 | 3085 | =============================================================================== |
lypinator | 0:bb348c97df44 | 3086 | [..] This section provides callback function: |
lypinator | 0:bb348c97df44 | 3087 | (+) Computation completed. |
lypinator | 0:bb348c97df44 | 3088 | |
lypinator | 0:bb348c97df44 | 3089 | @endverbatim |
lypinator | 0:bb348c97df44 | 3090 | * @{ |
lypinator | 0:bb348c97df44 | 3091 | */ |
lypinator | 0:bb348c97df44 | 3092 | |
lypinator | 0:bb348c97df44 | 3093 | |
lypinator | 0:bb348c97df44 | 3094 | /** |
lypinator | 0:bb348c97df44 | 3095 | * @brief Computation completed callbacks. |
lypinator | 0:bb348c97df44 | 3096 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 3097 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 3098 | * @retval None |
lypinator | 0:bb348c97df44 | 3099 | */ |
lypinator | 0:bb348c97df44 | 3100 | __weak void HAL_CRYPEx_ComputationCpltCallback(CRYP_HandleTypeDef *hcryp) |
lypinator | 0:bb348c97df44 | 3101 | { |
lypinator | 0:bb348c97df44 | 3102 | /* Prevent unused argument(s) compilation warning */ |
lypinator | 0:bb348c97df44 | 3103 | UNUSED(hcryp); |
lypinator | 0:bb348c97df44 | 3104 | |
lypinator | 0:bb348c97df44 | 3105 | /* NOTE : This function should not be modified; when the callback is needed, |
lypinator | 0:bb348c97df44 | 3106 | the HAL_CRYPEx_ComputationCpltCallback can be implemented in the user file |
lypinator | 0:bb348c97df44 | 3107 | */ |
lypinator | 0:bb348c97df44 | 3108 | } |
lypinator | 0:bb348c97df44 | 3109 | |
lypinator | 0:bb348c97df44 | 3110 | /** |
lypinator | 0:bb348c97df44 | 3111 | * @} |
lypinator | 0:bb348c97df44 | 3112 | */ |
lypinator | 0:bb348c97df44 | 3113 | |
lypinator | 0:bb348c97df44 | 3114 | /** @defgroup CRYPEx_Exported_Functions_Group2 AES extended processing functions |
lypinator | 0:bb348c97df44 | 3115 | * @brief Extended processing functions. |
lypinator | 0:bb348c97df44 | 3116 | * |
lypinator | 0:bb348c97df44 | 3117 | @verbatim |
lypinator | 0:bb348c97df44 | 3118 | ============================================================================== |
lypinator | 0:bb348c97df44 | 3119 | ##### AES extended processing functions ##### |
lypinator | 0:bb348c97df44 | 3120 | ============================================================================== |
lypinator | 0:bb348c97df44 | 3121 | [..] This section provides functions allowing to: |
lypinator | 0:bb348c97df44 | 3122 | (+) Encrypt plaintext or decrypt cipher text using AES algorithm in different chaining modes. |
lypinator | 0:bb348c97df44 | 3123 | Functions are generic (handles ECB, CBC and CTR and all modes) and are only differentiated |
lypinator | 0:bb348c97df44 | 3124 | based on the processing type. Three processing types are available: |
lypinator | 0:bb348c97df44 | 3125 | (++) Polling mode |
lypinator | 0:bb348c97df44 | 3126 | (++) Interrupt mode |
lypinator | 0:bb348c97df44 | 3127 | (++) DMA mode |
lypinator | 0:bb348c97df44 | 3128 | (+) Generate and authentication tag in addition to encrypt/decrypt a plain/cipher text using AES |
lypinator | 0:bb348c97df44 | 3129 | algorithm in different chaining modes. |
lypinator | 0:bb348c97df44 | 3130 | Functions are generic (handles GCM, GMAC, CMAC and CCM when applicable) and process only one phase |
lypinator | 0:bb348c97df44 | 3131 | so that steps can be skipped if so required. Functions are only differentiated based on the processing type. |
lypinator | 0:bb348c97df44 | 3132 | Three processing types are available: |
lypinator | 0:bb348c97df44 | 3133 | (++) Polling mode |
lypinator | 0:bb348c97df44 | 3134 | (++) Interrupt mode |
lypinator | 0:bb348c97df44 | 3135 | (++) DMA mode |
lypinator | 0:bb348c97df44 | 3136 | |
lypinator | 0:bb348c97df44 | 3137 | @endverbatim |
lypinator | 0:bb348c97df44 | 3138 | * @{ |
lypinator | 0:bb348c97df44 | 3139 | */ |
lypinator | 0:bb348c97df44 | 3140 | |
lypinator | 0:bb348c97df44 | 3141 | /** |
lypinator | 0:bb348c97df44 | 3142 | * @brief Carry out in polling mode the ciphering or deciphering operation according to |
lypinator | 0:bb348c97df44 | 3143 | * hcryp->Init structure fields, all operating modes (encryption, key derivation and/or decryption) and |
lypinator | 0:bb348c97df44 | 3144 | * chaining modes ECB, CBC and CTR are managed by this function in polling mode. |
lypinator | 0:bb348c97df44 | 3145 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 3146 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 3147 | * @param pInputData Pointer to the plain text in case of encryption or cipher text in case of decryption |
lypinator | 0:bb348c97df44 | 3148 | * or key derivation+decryption. |
lypinator | 0:bb348c97df44 | 3149 | * Parameter is meaningless in case of key derivation. |
lypinator | 0:bb348c97df44 | 3150 | * @param Size Length of the input data buffer in bytes, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 3151 | * Parameter is meaningless in case of key derivation. |
lypinator | 0:bb348c97df44 | 3152 | * @param pOutputData Pointer to the cipher text in case of encryption or plain text in case of |
lypinator | 0:bb348c97df44 | 3153 | * decryption/key derivation+decryption, or pointer to the derivative keys in |
lypinator | 0:bb348c97df44 | 3154 | * case of key derivation only. |
lypinator | 0:bb348c97df44 | 3155 | * @param Timeout Specify Timeout value |
lypinator | 0:bb348c97df44 | 3156 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 3157 | */ |
lypinator | 0:bb348c97df44 | 3158 | HAL_StatusTypeDef HAL_CRYPEx_AES(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint16_t Size, uint8_t *pOutputData, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 3159 | { |
lypinator | 0:bb348c97df44 | 3160 | |
lypinator | 0:bb348c97df44 | 3161 | if (hcryp->State == HAL_CRYP_STATE_READY) |
lypinator | 0:bb348c97df44 | 3162 | { |
lypinator | 0:bb348c97df44 | 3163 | /* Check parameters setting */ |
lypinator | 0:bb348c97df44 | 3164 | if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION) |
lypinator | 0:bb348c97df44 | 3165 | { |
lypinator | 0:bb348c97df44 | 3166 | if (pOutputData == NULL) |
lypinator | 0:bb348c97df44 | 3167 | { |
lypinator | 0:bb348c97df44 | 3168 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 3169 | } |
lypinator | 0:bb348c97df44 | 3170 | } |
lypinator | 0:bb348c97df44 | 3171 | else |
lypinator | 0:bb348c97df44 | 3172 | { |
lypinator | 0:bb348c97df44 | 3173 | if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0U)) |
lypinator | 0:bb348c97df44 | 3174 | { |
lypinator | 0:bb348c97df44 | 3175 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 3176 | } |
lypinator | 0:bb348c97df44 | 3177 | } |
lypinator | 0:bb348c97df44 | 3178 | |
lypinator | 0:bb348c97df44 | 3179 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 3180 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3181 | |
lypinator | 0:bb348c97df44 | 3182 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 3183 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 3184 | |
lypinator | 0:bb348c97df44 | 3185 | /* Call CRYP_ReadKey() API if the operating mode is set to |
lypinator | 0:bb348c97df44 | 3186 | key derivation, CRYP_ProcessData() otherwise */ |
lypinator | 0:bb348c97df44 | 3187 | if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION) |
lypinator | 0:bb348c97df44 | 3188 | { |
lypinator | 0:bb348c97df44 | 3189 | if(CRYP_ReadKey(hcryp, pOutputData, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 3190 | { |
lypinator | 0:bb348c97df44 | 3191 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 3192 | } |
lypinator | 0:bb348c97df44 | 3193 | } |
lypinator | 0:bb348c97df44 | 3194 | else |
lypinator | 0:bb348c97df44 | 3195 | { |
lypinator | 0:bb348c97df44 | 3196 | if(CRYP_ProcessData(hcryp, pInputData, Size, pOutputData, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 3197 | { |
lypinator | 0:bb348c97df44 | 3198 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 3199 | } |
lypinator | 0:bb348c97df44 | 3200 | } |
lypinator | 0:bb348c97df44 | 3201 | |
lypinator | 0:bb348c97df44 | 3202 | /* If the state has not been set to SUSPENDED, set it to |
lypinator | 0:bb348c97df44 | 3203 | READY, otherwise keep it as it is */ |
lypinator | 0:bb348c97df44 | 3204 | if (hcryp->State != HAL_CRYP_STATE_SUSPENDED) |
lypinator | 0:bb348c97df44 | 3205 | { |
lypinator | 0:bb348c97df44 | 3206 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 3207 | } |
lypinator | 0:bb348c97df44 | 3208 | |
lypinator | 0:bb348c97df44 | 3209 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 3210 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3211 | |
lypinator | 0:bb348c97df44 | 3212 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 3213 | } |
lypinator | 0:bb348c97df44 | 3214 | else |
lypinator | 0:bb348c97df44 | 3215 | { |
lypinator | 0:bb348c97df44 | 3216 | return HAL_BUSY; |
lypinator | 0:bb348c97df44 | 3217 | } |
lypinator | 0:bb348c97df44 | 3218 | } |
lypinator | 0:bb348c97df44 | 3219 | |
lypinator | 0:bb348c97df44 | 3220 | /** |
lypinator | 0:bb348c97df44 | 3221 | * @brief Carry out in interrupt mode the ciphering or deciphering operation according to |
lypinator | 0:bb348c97df44 | 3222 | * hcryp->Init structure fields, all operating modes (encryption, key derivation and/or decryption) and |
lypinator | 0:bb348c97df44 | 3223 | * chaining modes ECB, CBC and CTR are managed by this function in interrupt mode. |
lypinator | 0:bb348c97df44 | 3224 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 3225 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 3226 | * @param pInputData Pointer to the plain text in case of encryption or cipher text in case of decryption |
lypinator | 0:bb348c97df44 | 3227 | * or key derivation+decryption. |
lypinator | 0:bb348c97df44 | 3228 | * Parameter is meaningless in case of key derivation. |
lypinator | 0:bb348c97df44 | 3229 | * @param Size Length of the input data buffer in bytes, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 3230 | * Parameter is meaningless in case of key derivation. |
lypinator | 0:bb348c97df44 | 3231 | * @param pOutputData Pointer to the cipher text in case of encryption or plain text in case of |
lypinator | 0:bb348c97df44 | 3232 | * decryption/key derivation+decryption, or pointer to the derivative keys in |
lypinator | 0:bb348c97df44 | 3233 | * case of key derivation only. |
lypinator | 0:bb348c97df44 | 3234 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 3235 | */ |
lypinator | 0:bb348c97df44 | 3236 | HAL_StatusTypeDef HAL_CRYPEx_AES_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint16_t Size, uint8_t *pOutputData) |
lypinator | 0:bb348c97df44 | 3237 | { |
lypinator | 0:bb348c97df44 | 3238 | uint32_t inputaddr = 0U; |
lypinator | 0:bb348c97df44 | 3239 | |
lypinator | 0:bb348c97df44 | 3240 | if(hcryp->State == HAL_CRYP_STATE_READY) |
lypinator | 0:bb348c97df44 | 3241 | { |
lypinator | 0:bb348c97df44 | 3242 | /* Check parameters setting */ |
lypinator | 0:bb348c97df44 | 3243 | if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION) |
lypinator | 0:bb348c97df44 | 3244 | { |
lypinator | 0:bb348c97df44 | 3245 | if (pOutputData == NULL) |
lypinator | 0:bb348c97df44 | 3246 | { |
lypinator | 0:bb348c97df44 | 3247 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 3248 | } |
lypinator | 0:bb348c97df44 | 3249 | } |
lypinator | 0:bb348c97df44 | 3250 | else |
lypinator | 0:bb348c97df44 | 3251 | { |
lypinator | 0:bb348c97df44 | 3252 | if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0U)) |
lypinator | 0:bb348c97df44 | 3253 | { |
lypinator | 0:bb348c97df44 | 3254 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 3255 | } |
lypinator | 0:bb348c97df44 | 3256 | } |
lypinator | 0:bb348c97df44 | 3257 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 3258 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3259 | |
lypinator | 0:bb348c97df44 | 3260 | /* If operating mode is not limited to key derivation only, |
lypinator | 0:bb348c97df44 | 3261 | get the buffers addresses and sizes */ |
lypinator | 0:bb348c97df44 | 3262 | if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION) |
lypinator | 0:bb348c97df44 | 3263 | { |
lypinator | 0:bb348c97df44 | 3264 | |
lypinator | 0:bb348c97df44 | 3265 | hcryp->CrypInCount = Size; |
lypinator | 0:bb348c97df44 | 3266 | hcryp->pCrypInBuffPtr = pInputData; |
lypinator | 0:bb348c97df44 | 3267 | hcryp->pCrypOutBuffPtr = pOutputData; |
lypinator | 0:bb348c97df44 | 3268 | hcryp->CrypOutCount = Size; |
lypinator | 0:bb348c97df44 | 3269 | } |
lypinator | 0:bb348c97df44 | 3270 | |
lypinator | 0:bb348c97df44 | 3271 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 3272 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 3273 | |
lypinator | 0:bb348c97df44 | 3274 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 3275 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3276 | |
lypinator | 0:bb348c97df44 | 3277 | /* Enable Computation Complete Flag and Error Interrupts */ |
lypinator | 0:bb348c97df44 | 3278 | __HAL_CRYP_ENABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); |
lypinator | 0:bb348c97df44 | 3279 | |
lypinator | 0:bb348c97df44 | 3280 | /* If operating mode is key derivation only, the input data have |
lypinator | 0:bb348c97df44 | 3281 | already been entered during the initialization process. For |
lypinator | 0:bb348c97df44 | 3282 | the other operating modes, they are fed to the CRYP hardware |
lypinator | 0:bb348c97df44 | 3283 | block at this point. */ |
lypinator | 0:bb348c97df44 | 3284 | if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION) |
lypinator | 0:bb348c97df44 | 3285 | { |
lypinator | 0:bb348c97df44 | 3286 | /* Initiate the processing under interrupt in entering |
lypinator | 0:bb348c97df44 | 3287 | the first input data */ |
lypinator | 0:bb348c97df44 | 3288 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
lypinator | 0:bb348c97df44 | 3289 | /* Increment/decrement instance pointer/counter */ |
lypinator | 0:bb348c97df44 | 3290 | hcryp->pCrypInBuffPtr += 16U; |
lypinator | 0:bb348c97df44 | 3291 | hcryp->CrypInCount -= 16U; |
lypinator | 0:bb348c97df44 | 3292 | /* Write the first input block in the Data Input register */ |
lypinator | 0:bb348c97df44 | 3293 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 3294 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 3295 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 3296 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 3297 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 3298 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 3299 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 3300 | } |
lypinator | 0:bb348c97df44 | 3301 | |
lypinator | 0:bb348c97df44 | 3302 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 3303 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 3304 | } |
lypinator | 0:bb348c97df44 | 3305 | else |
lypinator | 0:bb348c97df44 | 3306 | { |
lypinator | 0:bb348c97df44 | 3307 | return HAL_BUSY; |
lypinator | 0:bb348c97df44 | 3308 | } |
lypinator | 0:bb348c97df44 | 3309 | } |
lypinator | 0:bb348c97df44 | 3310 | |
lypinator | 0:bb348c97df44 | 3311 | /** |
lypinator | 0:bb348c97df44 | 3312 | * @brief Carry out in DMA mode the ciphering or deciphering operation according to |
lypinator | 0:bb348c97df44 | 3313 | * hcryp->Init structure fields. |
lypinator | 0:bb348c97df44 | 3314 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 3315 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 3316 | * @param pInputData Pointer to the plain text in case of encryption or cipher text in case of decryption |
lypinator | 0:bb348c97df44 | 3317 | * or key derivation+decryption. |
lypinator | 0:bb348c97df44 | 3318 | * @param Size Length of the input data buffer in bytes, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 3319 | * @param pOutputData Pointer to the cipher text in case of encryption or plain text in case of |
lypinator | 0:bb348c97df44 | 3320 | * decryption/key derivation+decryption. |
lypinator | 0:bb348c97df44 | 3321 | * @note Chaining modes ECB, CBC and CTR are managed by this function in DMA mode. |
lypinator | 0:bb348c97df44 | 3322 | * @note Supported operating modes are encryption, decryption and key derivation with decryption. |
lypinator | 0:bb348c97df44 | 3323 | * @note No DMA channel is provided for key derivation only and therefore, access to AES_KEYRx |
lypinator | 0:bb348c97df44 | 3324 | * registers must be done by software. |
lypinator | 0:bb348c97df44 | 3325 | * @note This API is not applicable to key derivation only; for such a mode, access to AES_KEYRx |
lypinator | 0:bb348c97df44 | 3326 | * registers must be done by software thru HAL_CRYPEx_AES() or HAL_CRYPEx_AES_IT() APIs. |
lypinator | 0:bb348c97df44 | 3327 | * @note pInputData and pOutputData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP. |
lypinator | 0:bb348c97df44 | 3328 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 3329 | */ |
lypinator | 0:bb348c97df44 | 3330 | HAL_StatusTypeDef HAL_CRYPEx_AES_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint16_t Size, uint8_t *pOutputData) |
lypinator | 0:bb348c97df44 | 3331 | { |
lypinator | 0:bb348c97df44 | 3332 | uint32_t inputaddr = 0U; |
lypinator | 0:bb348c97df44 | 3333 | uint32_t outputaddr = 0U; |
lypinator | 0:bb348c97df44 | 3334 | |
lypinator | 0:bb348c97df44 | 3335 | if (hcryp->State == HAL_CRYP_STATE_READY) |
lypinator | 0:bb348c97df44 | 3336 | { |
lypinator | 0:bb348c97df44 | 3337 | /* Check parameters setting */ |
lypinator | 0:bb348c97df44 | 3338 | if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION) |
lypinator | 0:bb348c97df44 | 3339 | { |
lypinator | 0:bb348c97df44 | 3340 | /* no DMA channel is provided for key derivation operating mode, |
lypinator | 0:bb348c97df44 | 3341 | access to AES_KEYRx registers must be done by software */ |
lypinator | 0:bb348c97df44 | 3342 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 3343 | } |
lypinator | 0:bb348c97df44 | 3344 | else |
lypinator | 0:bb348c97df44 | 3345 | { |
lypinator | 0:bb348c97df44 | 3346 | if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0U)) |
lypinator | 0:bb348c97df44 | 3347 | { |
lypinator | 0:bb348c97df44 | 3348 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 3349 | } |
lypinator | 0:bb348c97df44 | 3350 | } |
lypinator | 0:bb348c97df44 | 3351 | |
lypinator | 0:bb348c97df44 | 3352 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 3353 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3354 | |
lypinator | 0:bb348c97df44 | 3355 | inputaddr = (uint32_t)pInputData; |
lypinator | 0:bb348c97df44 | 3356 | outputaddr = (uint32_t)pOutputData; |
lypinator | 0:bb348c97df44 | 3357 | |
lypinator | 0:bb348c97df44 | 3358 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 3359 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 3360 | |
lypinator | 0:bb348c97df44 | 3361 | /* Set the input and output addresses and start DMA transfer */ |
lypinator | 0:bb348c97df44 | 3362 | CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); |
lypinator | 0:bb348c97df44 | 3363 | |
lypinator | 0:bb348c97df44 | 3364 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 3365 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3366 | |
lypinator | 0:bb348c97df44 | 3367 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 3368 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 3369 | } |
lypinator | 0:bb348c97df44 | 3370 | else |
lypinator | 0:bb348c97df44 | 3371 | { |
lypinator | 0:bb348c97df44 | 3372 | return HAL_BUSY; |
lypinator | 0:bb348c97df44 | 3373 | } |
lypinator | 0:bb348c97df44 | 3374 | } |
lypinator | 0:bb348c97df44 | 3375 | |
lypinator | 0:bb348c97df44 | 3376 | /** |
lypinator | 0:bb348c97df44 | 3377 | * @brief Carry out in polling mode the authentication tag generation as well as the ciphering or deciphering |
lypinator | 0:bb348c97df44 | 3378 | * operation according to hcryp->Init structure fields. |
lypinator | 0:bb348c97df44 | 3379 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 3380 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 3381 | * @param pInputData |
lypinator | 0:bb348c97df44 | 3382 | * - pointer to payload data in GCM payload phase, |
lypinator | 0:bb348c97df44 | 3383 | * - pointer to B0 block in CMAC header phase, |
lypinator | 0:bb348c97df44 | 3384 | * - pointer to C block in CMAC final phase. |
lypinator | 0:bb348c97df44 | 3385 | * - Parameter is meaningless in case of GCM/GMAC init, header and final phases. |
lypinator | 0:bb348c97df44 | 3386 | * @param Size |
lypinator | 0:bb348c97df44 | 3387 | * - length of the input payload data buffer in bytes, |
lypinator | 0:bb348c97df44 | 3388 | * - length of B0 block (in bytes) in CMAC header phase, |
lypinator | 0:bb348c97df44 | 3389 | * - length of C block (in bytes) in CMAC final phase. |
lypinator | 0:bb348c97df44 | 3390 | * - Parameter is meaningless in case of GCM/GMAC init and header phases. |
lypinator | 0:bb348c97df44 | 3391 | * @param pOutputData |
lypinator | 0:bb348c97df44 | 3392 | * - pointer to plain or cipher text in GCM payload phase, |
lypinator | 0:bb348c97df44 | 3393 | * - pointer to authentication tag in GCM/GMAC and CMAC final phases. |
lypinator | 0:bb348c97df44 | 3394 | * - Parameter is meaningless in case of GCM/GMAC init and header phases |
lypinator | 0:bb348c97df44 | 3395 | * and in case of CMAC header phase. |
lypinator | 0:bb348c97df44 | 3396 | * @param Timeout Specify Timeout value |
lypinator | 0:bb348c97df44 | 3397 | * @note Supported operating modes are encryption and decryption, supported chaining modes are GCM, GMAC, CMAC and CCM when the latter is applicable. |
lypinator | 0:bb348c97df44 | 3398 | * @note Phases are singly processed according to hcryp->Init.GCMCMACPhase so that steps in these specific chaining modes |
lypinator | 0:bb348c97df44 | 3399 | * can be skipped by the user if so required. |
lypinator | 0:bb348c97df44 | 3400 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 3401 | */ |
lypinator | 0:bb348c97df44 | 3402 | HAL_StatusTypeDef HAL_CRYPEx_AES_Auth(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint64_t Size, uint8_t *pOutputData, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 3403 | { |
lypinator | 0:bb348c97df44 | 3404 | uint32_t index = 0U; |
lypinator | 0:bb348c97df44 | 3405 | uint32_t inputaddr = 0U; |
lypinator | 0:bb348c97df44 | 3406 | uint32_t outputaddr = 0U; |
lypinator | 0:bb348c97df44 | 3407 | uint32_t tagaddr = 0U; |
lypinator | 0:bb348c97df44 | 3408 | uint64_t headerlength = 0U; |
lypinator | 0:bb348c97df44 | 3409 | uint64_t inputlength = 0U; |
lypinator | 0:bb348c97df44 | 3410 | uint64_t payloadlength = 0U; |
lypinator | 0:bb348c97df44 | 3411 | uint32_t difflength = 0U; |
lypinator | 0:bb348c97df44 | 3412 | uint32_t addhoc_process = 0U; |
lypinator | 0:bb348c97df44 | 3413 | |
lypinator | 0:bb348c97df44 | 3414 | if (hcryp->State == HAL_CRYP_STATE_READY) |
lypinator | 0:bb348c97df44 | 3415 | { |
lypinator | 0:bb348c97df44 | 3416 | /* input/output parameters check */ |
lypinator | 0:bb348c97df44 | 3417 | if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE) |
lypinator | 0:bb348c97df44 | 3418 | { |
lypinator | 0:bb348c97df44 | 3419 | if ((hcryp->Init.Header != NULL) && (hcryp->Init.HeaderSize == 0U)) |
lypinator | 0:bb348c97df44 | 3420 | { |
lypinator | 0:bb348c97df44 | 3421 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 3422 | } |
lypinator | 0:bb348c97df44 | 3423 | #if defined(AES_CR_NPBLB) |
lypinator | 0:bb348c97df44 | 3424 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC) |
lypinator | 0:bb348c97df44 | 3425 | #else |
lypinator | 0:bb348c97df44 | 3426 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) |
lypinator | 0:bb348c97df44 | 3427 | #endif |
lypinator | 0:bb348c97df44 | 3428 | { |
lypinator | 0:bb348c97df44 | 3429 | /* In case of CMAC (or CCM) header phase resumption, we can have pInputData = NULL and Size = 0 */ |
lypinator | 0:bb348c97df44 | 3430 | if (((pInputData != NULL) && (Size == 0U)) || ((pInputData == NULL) && (Size != 0U))) |
lypinator | 0:bb348c97df44 | 3431 | { |
lypinator | 0:bb348c97df44 | 3432 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 3433 | } |
lypinator | 0:bb348c97df44 | 3434 | } |
lypinator | 0:bb348c97df44 | 3435 | } |
lypinator | 0:bb348c97df44 | 3436 | else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE) |
lypinator | 0:bb348c97df44 | 3437 | { |
lypinator | 0:bb348c97df44 | 3438 | if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0U)) |
lypinator | 0:bb348c97df44 | 3439 | { |
lypinator | 0:bb348c97df44 | 3440 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 3441 | } |
lypinator | 0:bb348c97df44 | 3442 | } |
lypinator | 0:bb348c97df44 | 3443 | else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE) |
lypinator | 0:bb348c97df44 | 3444 | { |
lypinator | 0:bb348c97df44 | 3445 | if (pOutputData == NULL) |
lypinator | 0:bb348c97df44 | 3446 | { |
lypinator | 0:bb348c97df44 | 3447 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 3448 | } |
lypinator | 0:bb348c97df44 | 3449 | #if defined(AES_CR_NPBLB) |
lypinator | 0:bb348c97df44 | 3450 | if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC) && (pInputData == NULL)) |
lypinator | 0:bb348c97df44 | 3451 | #else |
lypinator | 0:bb348c97df44 | 3452 | if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (pInputData == NULL)) |
lypinator | 0:bb348c97df44 | 3453 | #endif |
lypinator | 0:bb348c97df44 | 3454 | { |
lypinator | 0:bb348c97df44 | 3455 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 3456 | } |
lypinator | 0:bb348c97df44 | 3457 | } |
lypinator | 0:bb348c97df44 | 3458 | |
lypinator | 0:bb348c97df44 | 3459 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 3460 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3461 | |
lypinator | 0:bb348c97df44 | 3462 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 3463 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 3464 | |
lypinator | 0:bb348c97df44 | 3465 | /*==============================================*/ |
lypinator | 0:bb348c97df44 | 3466 | /* GCM/GMAC (or CCM when applicable) init phase */ |
lypinator | 0:bb348c97df44 | 3467 | /*==============================================*/ |
lypinator | 0:bb348c97df44 | 3468 | /* In case of init phase, the input data (Key and Initialization Vector) have |
lypinator | 0:bb348c97df44 | 3469 | already been entered during the initialization process. Therefore, the |
lypinator | 0:bb348c97df44 | 3470 | API just waits for the CCF flag to be set. */ |
lypinator | 0:bb348c97df44 | 3471 | if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE) |
lypinator | 0:bb348c97df44 | 3472 | { |
lypinator | 0:bb348c97df44 | 3473 | /* just wait for hash computation */ |
lypinator | 0:bb348c97df44 | 3474 | if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 3475 | { |
lypinator | 0:bb348c97df44 | 3476 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 3477 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3478 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 3479 | } |
lypinator | 0:bb348c97df44 | 3480 | |
lypinator | 0:bb348c97df44 | 3481 | /* Clear CCF Flag */ |
lypinator | 0:bb348c97df44 | 3482 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
lypinator | 0:bb348c97df44 | 3483 | /* Mark that the initialization phase is over */ |
lypinator | 0:bb348c97df44 | 3484 | hcryp->Phase = HAL_CRYP_PHASE_INIT_OVER; |
lypinator | 0:bb348c97df44 | 3485 | } |
lypinator | 0:bb348c97df44 | 3486 | /*=====================================*/ |
lypinator | 0:bb348c97df44 | 3487 | /* GCM/GMAC or (CCM/)CMAC header phase */ |
lypinator | 0:bb348c97df44 | 3488 | /*=====================================*/ |
lypinator | 0:bb348c97df44 | 3489 | else if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE) |
lypinator | 0:bb348c97df44 | 3490 | { |
lypinator | 0:bb348c97df44 | 3491 | /* Set header phase; for GCM or GMAC, set data-byte at this point */ |
lypinator | 0:bb348c97df44 | 3492 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) |
lypinator | 0:bb348c97df44 | 3493 | { |
lypinator | 0:bb348c97df44 | 3494 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH|AES_CR_DATATYPE, CRYP_HEADER_PHASE|hcryp->Init.DataType); |
lypinator | 0:bb348c97df44 | 3495 | } |
lypinator | 0:bb348c97df44 | 3496 | else |
lypinator | 0:bb348c97df44 | 3497 | { |
lypinator | 0:bb348c97df44 | 3498 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_HEADER_PHASE); |
lypinator | 0:bb348c97df44 | 3499 | } |
lypinator | 0:bb348c97df44 | 3500 | |
lypinator | 0:bb348c97df44 | 3501 | /* Enable the Peripheral */ |
lypinator | 0:bb348c97df44 | 3502 | __HAL_CRYP_ENABLE(); |
lypinator | 0:bb348c97df44 | 3503 | |
lypinator | 0:bb348c97df44 | 3504 | #if !defined(AES_CR_NPBLB) |
lypinator | 0:bb348c97df44 | 3505 | /* in case of CMAC, enter B0 block in header phase, before the header itself. */ |
lypinator | 0:bb348c97df44 | 3506 | /* If Size = 0 (possible case of resumption after CMAC header phase suspension), |
lypinator | 0:bb348c97df44 | 3507 | skip these steps and go directly to header buffer feeding to the HW */ |
lypinator | 0:bb348c97df44 | 3508 | if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (Size != 0U)) |
lypinator | 0:bb348c97df44 | 3509 | { |
lypinator | 0:bb348c97df44 | 3510 | inputaddr = (uint32_t)pInputData; |
lypinator | 0:bb348c97df44 | 3511 | |
lypinator | 0:bb348c97df44 | 3512 | for(index=0U; (index < Size); index += 16U) |
lypinator | 0:bb348c97df44 | 3513 | { |
lypinator | 0:bb348c97df44 | 3514 | /* Write the Input block in the Data Input register */ |
lypinator | 0:bb348c97df44 | 3515 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 3516 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 3517 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 3518 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 3519 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 3520 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 3521 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 3522 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 3523 | |
lypinator | 0:bb348c97df44 | 3524 | if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 3525 | { |
lypinator | 0:bb348c97df44 | 3526 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 3527 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3528 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 3529 | } |
lypinator | 0:bb348c97df44 | 3530 | /* Clear CCF Flag */ |
lypinator | 0:bb348c97df44 | 3531 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
lypinator | 0:bb348c97df44 | 3532 | |
lypinator | 0:bb348c97df44 | 3533 | /* If the suspension flag has been raised and if the processing is not about |
lypinator | 0:bb348c97df44 | 3534 | to end, suspend processing */ |
lypinator | 0:bb348c97df44 | 3535 | if ((hcryp->SuspendRequest == HAL_CRYP_SUSPEND) && ((index+16U) < Size)) |
lypinator | 0:bb348c97df44 | 3536 | { |
lypinator | 0:bb348c97df44 | 3537 | /* reset SuspendRequest */ |
lypinator | 0:bb348c97df44 | 3538 | hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE; |
lypinator | 0:bb348c97df44 | 3539 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 3540 | hcryp->State = HAL_CRYP_STATE_SUSPENDED; |
lypinator | 0:bb348c97df44 | 3541 | /* Mark that the header phase is over */ |
lypinator | 0:bb348c97df44 | 3542 | hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED; |
lypinator | 0:bb348c97df44 | 3543 | |
lypinator | 0:bb348c97df44 | 3544 | /* Save current reading and writing locations of Input and Output buffers */ |
lypinator | 0:bb348c97df44 | 3545 | hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr; |
lypinator | 0:bb348c97df44 | 3546 | /* Save the total number of bytes (B blocks + header) that remain to be |
lypinator | 0:bb348c97df44 | 3547 | processed at this point */ |
lypinator | 0:bb348c97df44 | 3548 | hcryp->CrypInCount = hcryp->Init.HeaderSize + Size - (index+16U); |
lypinator | 0:bb348c97df44 | 3549 | |
lypinator | 0:bb348c97df44 | 3550 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 3551 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3552 | |
lypinator | 0:bb348c97df44 | 3553 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 3554 | } |
lypinator | 0:bb348c97df44 | 3555 | } /* for(index=0; (index < Size); index += 16) */ |
lypinator | 0:bb348c97df44 | 3556 | } |
lypinator | 0:bb348c97df44 | 3557 | #endif /* !defined(AES_CR_NPBLB) */ |
lypinator | 0:bb348c97df44 | 3558 | |
lypinator | 0:bb348c97df44 | 3559 | /* Enter header */ |
lypinator | 0:bb348c97df44 | 3560 | inputaddr = (uint32_t)hcryp->Init.Header; |
lypinator | 0:bb348c97df44 | 3561 | /* Local variable headerlength is a number of bytes multiple of 128 bits, |
lypinator | 0:bb348c97df44 | 3562 | remaining header data (if any) are handled after this loop */ |
lypinator | 0:bb348c97df44 | 3563 | headerlength = (((hcryp->Init.HeaderSize)/16U)*16U) ; |
lypinator | 0:bb348c97df44 | 3564 | if ((hcryp->Init.HeaderSize % 16U) != 0U) |
lypinator | 0:bb348c97df44 | 3565 | { |
lypinator | 0:bb348c97df44 | 3566 | difflength = (uint32_t) (hcryp->Init.HeaderSize - headerlength); |
lypinator | 0:bb348c97df44 | 3567 | } |
lypinator | 0:bb348c97df44 | 3568 | for(index=0U; index < headerlength; index += 16U) |
lypinator | 0:bb348c97df44 | 3569 | { |
lypinator | 0:bb348c97df44 | 3570 | /* Write the Input block in the Data Input register */ |
lypinator | 0:bb348c97df44 | 3571 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 3572 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 3573 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 3574 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 3575 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 3576 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 3577 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 3578 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 3579 | |
lypinator | 0:bb348c97df44 | 3580 | if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 3581 | { |
lypinator | 0:bb348c97df44 | 3582 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 3583 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3584 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 3585 | } |
lypinator | 0:bb348c97df44 | 3586 | /* Clear CCF Flag */ |
lypinator | 0:bb348c97df44 | 3587 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
lypinator | 0:bb348c97df44 | 3588 | |
lypinator | 0:bb348c97df44 | 3589 | /* If the suspension flag has been raised and if the processing is not about |
lypinator | 0:bb348c97df44 | 3590 | to end, suspend processing */ |
lypinator | 0:bb348c97df44 | 3591 | if ((hcryp->SuspendRequest == HAL_CRYP_SUSPEND) && ((index+16U) < headerlength)) |
lypinator | 0:bb348c97df44 | 3592 | { |
lypinator | 0:bb348c97df44 | 3593 | /* reset SuspendRequest */ |
lypinator | 0:bb348c97df44 | 3594 | hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE; |
lypinator | 0:bb348c97df44 | 3595 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 3596 | hcryp->State = HAL_CRYP_STATE_SUSPENDED; |
lypinator | 0:bb348c97df44 | 3597 | /* Mark that the header phase is over */ |
lypinator | 0:bb348c97df44 | 3598 | hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED; |
lypinator | 0:bb348c97df44 | 3599 | |
lypinator | 0:bb348c97df44 | 3600 | /* Save current reading and writing locations of Input and Output buffers */ |
lypinator | 0:bb348c97df44 | 3601 | hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr; |
lypinator | 0:bb348c97df44 | 3602 | /* Save the total number of bytes that remain to be processed at this point */ |
lypinator | 0:bb348c97df44 | 3603 | hcryp->CrypInCount = hcryp->Init.HeaderSize - (index+16U); |
lypinator | 0:bb348c97df44 | 3604 | |
lypinator | 0:bb348c97df44 | 3605 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 3606 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3607 | |
lypinator | 0:bb348c97df44 | 3608 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 3609 | } |
lypinator | 0:bb348c97df44 | 3610 | } |
lypinator | 0:bb348c97df44 | 3611 | |
lypinator | 0:bb348c97df44 | 3612 | /* Case header length is not a multiple of 16 bytes */ |
lypinator | 0:bb348c97df44 | 3613 | if (difflength != 0U) |
lypinator | 0:bb348c97df44 | 3614 | { |
lypinator | 0:bb348c97df44 | 3615 | hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr; |
lypinator | 0:bb348c97df44 | 3616 | CRYP_Padding(hcryp, difflength, CRYP_POLLING_ON); |
lypinator | 0:bb348c97df44 | 3617 | } |
lypinator | 0:bb348c97df44 | 3618 | |
lypinator | 0:bb348c97df44 | 3619 | /* Mark that the header phase is over */ |
lypinator | 0:bb348c97df44 | 3620 | hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER; |
lypinator | 0:bb348c97df44 | 3621 | } |
lypinator | 0:bb348c97df44 | 3622 | /*============================================*/ |
lypinator | 0:bb348c97df44 | 3623 | /* GCM (or CCM when applicable) payload phase */ |
lypinator | 0:bb348c97df44 | 3624 | /*============================================*/ |
lypinator | 0:bb348c97df44 | 3625 | else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE) |
lypinator | 0:bb348c97df44 | 3626 | { |
lypinator | 0:bb348c97df44 | 3627 | |
lypinator | 0:bb348c97df44 | 3628 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PAYLOAD_PHASE); |
lypinator | 0:bb348c97df44 | 3629 | |
lypinator | 0:bb348c97df44 | 3630 | /* if the header phase has been bypassed, AES must be enabled again */ |
lypinator | 0:bb348c97df44 | 3631 | if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER) |
lypinator | 0:bb348c97df44 | 3632 | { |
lypinator | 0:bb348c97df44 | 3633 | __HAL_CRYP_ENABLE(); |
lypinator | 0:bb348c97df44 | 3634 | } |
lypinator | 0:bb348c97df44 | 3635 | |
lypinator | 0:bb348c97df44 | 3636 | inputaddr = (uint32_t)pInputData; |
lypinator | 0:bb348c97df44 | 3637 | outputaddr = (uint32_t)pOutputData; |
lypinator | 0:bb348c97df44 | 3638 | |
lypinator | 0:bb348c97df44 | 3639 | /* Enter payload */ |
lypinator | 0:bb348c97df44 | 3640 | /* Specific handling to manage payload last block size less than 128 bits */ |
lypinator | 0:bb348c97df44 | 3641 | if ((Size % 16U) != 0U) |
lypinator | 0:bb348c97df44 | 3642 | { |
lypinator | 0:bb348c97df44 | 3643 | payloadlength = (Size/16U) * 16U; |
lypinator | 0:bb348c97df44 | 3644 | difflength = (uint32_t) (Size - payloadlength); |
lypinator | 0:bb348c97df44 | 3645 | addhoc_process = 1U; |
lypinator | 0:bb348c97df44 | 3646 | } |
lypinator | 0:bb348c97df44 | 3647 | else |
lypinator | 0:bb348c97df44 | 3648 | { |
lypinator | 0:bb348c97df44 | 3649 | payloadlength = Size; |
lypinator | 0:bb348c97df44 | 3650 | addhoc_process = 0U; |
lypinator | 0:bb348c97df44 | 3651 | } |
lypinator | 0:bb348c97df44 | 3652 | |
lypinator | 0:bb348c97df44 | 3653 | /* Feed payload */ |
lypinator | 0:bb348c97df44 | 3654 | for(index=0U; index < payloadlength; index += 16U) |
lypinator | 0:bb348c97df44 | 3655 | { |
lypinator | 0:bb348c97df44 | 3656 | /* Write the Input block in the Data Input register */ |
lypinator | 0:bb348c97df44 | 3657 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 3658 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 3659 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 3660 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 3661 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 3662 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 3663 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 3664 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 3665 | |
lypinator | 0:bb348c97df44 | 3666 | if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 3667 | { |
lypinator | 0:bb348c97df44 | 3668 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 3669 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3670 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 3671 | } |
lypinator | 0:bb348c97df44 | 3672 | |
lypinator | 0:bb348c97df44 | 3673 | /* Clear CCF Flag */ |
lypinator | 0:bb348c97df44 | 3674 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
lypinator | 0:bb348c97df44 | 3675 | |
lypinator | 0:bb348c97df44 | 3676 | /* Retrieve output data: read the output block |
lypinator | 0:bb348c97df44 | 3677 | from the Data Output Register */ |
lypinator | 0:bb348c97df44 | 3678 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 3679 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 3680 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 3681 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 3682 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 3683 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 3684 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 3685 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 3686 | |
lypinator | 0:bb348c97df44 | 3687 | /* If the suspension flag has been raised and if the processing is not about |
lypinator | 0:bb348c97df44 | 3688 | to end, suspend processing */ |
lypinator | 0:bb348c97df44 | 3689 | if ((hcryp->SuspendRequest == HAL_CRYP_SUSPEND) && ((index+16U) < payloadlength)) |
lypinator | 0:bb348c97df44 | 3690 | { |
lypinator | 0:bb348c97df44 | 3691 | /* no flag waiting under IRQ handling */ |
lypinator | 0:bb348c97df44 | 3692 | if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT) |
lypinator | 0:bb348c97df44 | 3693 | { |
lypinator | 0:bb348c97df44 | 3694 | /* Ensure that Busy flag is reset */ |
lypinator | 0:bb348c97df44 | 3695 | if(CRYP_WaitOnBusyFlagReset(hcryp, CRYP_BUSY_TIMEOUTVALUE) != HAL_OK) |
lypinator | 0:bb348c97df44 | 3696 | { |
lypinator | 0:bb348c97df44 | 3697 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 3698 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3699 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 3700 | } |
lypinator | 0:bb348c97df44 | 3701 | } |
lypinator | 0:bb348c97df44 | 3702 | /* reset SuspendRequest */ |
lypinator | 0:bb348c97df44 | 3703 | hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE; |
lypinator | 0:bb348c97df44 | 3704 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 3705 | hcryp->State = HAL_CRYP_STATE_SUSPENDED; |
lypinator | 0:bb348c97df44 | 3706 | /* Mark that the header phase is over */ |
lypinator | 0:bb348c97df44 | 3707 | hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED; |
lypinator | 0:bb348c97df44 | 3708 | |
lypinator | 0:bb348c97df44 | 3709 | /* Save current reading and writing locations of Input and Output buffers */ |
lypinator | 0:bb348c97df44 | 3710 | hcryp->pCrypOutBuffPtr = (uint8_t *)outputaddr; |
lypinator | 0:bb348c97df44 | 3711 | hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr; |
lypinator | 0:bb348c97df44 | 3712 | /* Save the number of bytes that remain to be processed at this point */ |
lypinator | 0:bb348c97df44 | 3713 | hcryp->CrypInCount = Size - (index+16U); |
lypinator | 0:bb348c97df44 | 3714 | |
lypinator | 0:bb348c97df44 | 3715 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 3716 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3717 | |
lypinator | 0:bb348c97df44 | 3718 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 3719 | } |
lypinator | 0:bb348c97df44 | 3720 | |
lypinator | 0:bb348c97df44 | 3721 | } |
lypinator | 0:bb348c97df44 | 3722 | |
lypinator | 0:bb348c97df44 | 3723 | /* Additional processing to manage GCM(/CCM) encryption and decryption cases when |
lypinator | 0:bb348c97df44 | 3724 | payload last block size less than 128 bits */ |
lypinator | 0:bb348c97df44 | 3725 | if (addhoc_process == 1U) |
lypinator | 0:bb348c97df44 | 3726 | { |
lypinator | 0:bb348c97df44 | 3727 | hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr; |
lypinator | 0:bb348c97df44 | 3728 | hcryp->pCrypOutBuffPtr = (uint8_t *)outputaddr; |
lypinator | 0:bb348c97df44 | 3729 | CRYP_Padding(hcryp, difflength, CRYP_POLLING_ON); |
lypinator | 0:bb348c97df44 | 3730 | } /* (addhoc_process == 1) */ |
lypinator | 0:bb348c97df44 | 3731 | |
lypinator | 0:bb348c97df44 | 3732 | /* Mark that the payload phase is over */ |
lypinator | 0:bb348c97df44 | 3733 | hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER; |
lypinator | 0:bb348c97df44 | 3734 | } |
lypinator | 0:bb348c97df44 | 3735 | /*====================================*/ |
lypinator | 0:bb348c97df44 | 3736 | /* GCM/GMAC or (CCM/)CMAC final phase */ |
lypinator | 0:bb348c97df44 | 3737 | /*====================================*/ |
lypinator | 0:bb348c97df44 | 3738 | else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE) |
lypinator | 0:bb348c97df44 | 3739 | { |
lypinator | 0:bb348c97df44 | 3740 | tagaddr = (uint32_t)pOutputData; |
lypinator | 0:bb348c97df44 | 3741 | |
lypinator | 0:bb348c97df44 | 3742 | #if defined(AES_CR_NPBLB) |
lypinator | 0:bb348c97df44 | 3743 | /* By default, clear NPBLB field */ |
lypinator | 0:bb348c97df44 | 3744 | CLEAR_BIT(hcryp->Instance->CR, AES_CR_NPBLB); |
lypinator | 0:bb348c97df44 | 3745 | #endif |
lypinator | 0:bb348c97df44 | 3746 | |
lypinator | 0:bb348c97df44 | 3747 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_FINAL_PHASE); |
lypinator | 0:bb348c97df44 | 3748 | |
lypinator | 0:bb348c97df44 | 3749 | /* if the header and payload phases have been bypassed, AES must be enabled again */ |
lypinator | 0:bb348c97df44 | 3750 | if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER) |
lypinator | 0:bb348c97df44 | 3751 | { |
lypinator | 0:bb348c97df44 | 3752 | __HAL_CRYP_ENABLE(); |
lypinator | 0:bb348c97df44 | 3753 | } |
lypinator | 0:bb348c97df44 | 3754 | |
lypinator | 0:bb348c97df44 | 3755 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) |
lypinator | 0:bb348c97df44 | 3756 | { |
lypinator | 0:bb348c97df44 | 3757 | headerlength = hcryp->Init.HeaderSize * 8U; /* Header length in bits */ |
lypinator | 0:bb348c97df44 | 3758 | inputlength = Size * 8U; /* input length in bits */ |
lypinator | 0:bb348c97df44 | 3759 | |
lypinator | 0:bb348c97df44 | 3760 | |
lypinator | 0:bb348c97df44 | 3761 | if(hcryp->Init.DataType == CRYP_DATATYPE_1B) |
lypinator | 0:bb348c97df44 | 3762 | { |
lypinator | 0:bb348c97df44 | 3763 | hcryp->Instance->DINR = __RBIT((headerlength)>>32U); |
lypinator | 0:bb348c97df44 | 3764 | hcryp->Instance->DINR = __RBIT(headerlength); |
lypinator | 0:bb348c97df44 | 3765 | hcryp->Instance->DINR = __RBIT((inputlength)>>32U); |
lypinator | 0:bb348c97df44 | 3766 | hcryp->Instance->DINR = __RBIT(inputlength); |
lypinator | 0:bb348c97df44 | 3767 | } |
lypinator | 0:bb348c97df44 | 3768 | else if(hcryp->Init.DataType == CRYP_DATATYPE_8B) |
lypinator | 0:bb348c97df44 | 3769 | { |
lypinator | 0:bb348c97df44 | 3770 | hcryp->Instance->DINR = __REV((headerlength)>>32U); |
lypinator | 0:bb348c97df44 | 3771 | hcryp->Instance->DINR = __REV(headerlength); |
lypinator | 0:bb348c97df44 | 3772 | hcryp->Instance->DINR = __REV((inputlength)>>32U); |
lypinator | 0:bb348c97df44 | 3773 | hcryp->Instance->DINR = __REV(inputlength); |
lypinator | 0:bb348c97df44 | 3774 | } |
lypinator | 0:bb348c97df44 | 3775 | else if(hcryp->Init.DataType == CRYP_DATATYPE_16B) |
lypinator | 0:bb348c97df44 | 3776 | { |
lypinator | 0:bb348c97df44 | 3777 | hcryp->Instance->DINR = __ROR((headerlength)>>32U, 16U); |
lypinator | 0:bb348c97df44 | 3778 | hcryp->Instance->DINR = __ROR(headerlength, 16U); |
lypinator | 0:bb348c97df44 | 3779 | hcryp->Instance->DINR = __ROR((inputlength)>>32U, 16U); |
lypinator | 0:bb348c97df44 | 3780 | hcryp->Instance->DINR = __ROR(inputlength, 16U); |
lypinator | 0:bb348c97df44 | 3781 | } |
lypinator | 0:bb348c97df44 | 3782 | else if(hcryp->Init.DataType == CRYP_DATATYPE_32B) |
lypinator | 0:bb348c97df44 | 3783 | { |
lypinator | 0:bb348c97df44 | 3784 | hcryp->Instance->DINR = (uint32_t)(headerlength>>32U); |
lypinator | 0:bb348c97df44 | 3785 | hcryp->Instance->DINR = (uint32_t)(headerlength); |
lypinator | 0:bb348c97df44 | 3786 | hcryp->Instance->DINR = (uint32_t)(inputlength>>32U); |
lypinator | 0:bb348c97df44 | 3787 | hcryp->Instance->DINR = (uint32_t)(inputlength); |
lypinator | 0:bb348c97df44 | 3788 | } |
lypinator | 0:bb348c97df44 | 3789 | } |
lypinator | 0:bb348c97df44 | 3790 | #if !defined(AES_CR_NPBLB) |
lypinator | 0:bb348c97df44 | 3791 | else if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) |
lypinator | 0:bb348c97df44 | 3792 | { |
lypinator | 0:bb348c97df44 | 3793 | inputaddr = (uint32_t)pInputData; |
lypinator | 0:bb348c97df44 | 3794 | /* Enter the last block made of a 128-bit value formatted |
lypinator | 0:bb348c97df44 | 3795 | from the original B0 packet. */ |
lypinator | 0:bb348c97df44 | 3796 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 3797 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 3798 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 3799 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 3800 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 3801 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 3802 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 3803 | } |
lypinator | 0:bb348c97df44 | 3804 | #endif |
lypinator | 0:bb348c97df44 | 3805 | |
lypinator | 0:bb348c97df44 | 3806 | |
lypinator | 0:bb348c97df44 | 3807 | if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 3808 | { |
lypinator | 0:bb348c97df44 | 3809 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 3810 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3811 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 3812 | } |
lypinator | 0:bb348c97df44 | 3813 | |
lypinator | 0:bb348c97df44 | 3814 | /* Read the Auth TAG in the Data Out register */ |
lypinator | 0:bb348c97df44 | 3815 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 3816 | tagaddr+=4U; |
lypinator | 0:bb348c97df44 | 3817 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 3818 | tagaddr+=4U; |
lypinator | 0:bb348c97df44 | 3819 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 3820 | tagaddr+=4U; |
lypinator | 0:bb348c97df44 | 3821 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 3822 | |
lypinator | 0:bb348c97df44 | 3823 | |
lypinator | 0:bb348c97df44 | 3824 | /* Clear CCF Flag */ |
lypinator | 0:bb348c97df44 | 3825 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
lypinator | 0:bb348c97df44 | 3826 | /* Mark that the final phase is over */ |
lypinator | 0:bb348c97df44 | 3827 | hcryp->Phase = HAL_CRYP_PHASE_FINAL_OVER; |
lypinator | 0:bb348c97df44 | 3828 | /* Disable the Peripheral */ |
lypinator | 0:bb348c97df44 | 3829 | __HAL_CRYP_DISABLE(); |
lypinator | 0:bb348c97df44 | 3830 | } |
lypinator | 0:bb348c97df44 | 3831 | /*=================================================*/ |
lypinator | 0:bb348c97df44 | 3832 | /* case incorrect hcryp->Init.GCMCMACPhase setting */ |
lypinator | 0:bb348c97df44 | 3833 | /*=================================================*/ |
lypinator | 0:bb348c97df44 | 3834 | else |
lypinator | 0:bb348c97df44 | 3835 | { |
lypinator | 0:bb348c97df44 | 3836 | hcryp->State = HAL_CRYP_STATE_ERROR; |
lypinator | 0:bb348c97df44 | 3837 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3838 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 3839 | } |
lypinator | 0:bb348c97df44 | 3840 | |
lypinator | 0:bb348c97df44 | 3841 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 3842 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 3843 | |
lypinator | 0:bb348c97df44 | 3844 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 3845 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3846 | |
lypinator | 0:bb348c97df44 | 3847 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 3848 | } |
lypinator | 0:bb348c97df44 | 3849 | else |
lypinator | 0:bb348c97df44 | 3850 | { |
lypinator | 0:bb348c97df44 | 3851 | return HAL_BUSY; |
lypinator | 0:bb348c97df44 | 3852 | } |
lypinator | 0:bb348c97df44 | 3853 | } |
lypinator | 0:bb348c97df44 | 3854 | |
lypinator | 0:bb348c97df44 | 3855 | |
lypinator | 0:bb348c97df44 | 3856 | |
lypinator | 0:bb348c97df44 | 3857 | |
lypinator | 0:bb348c97df44 | 3858 | /** |
lypinator | 0:bb348c97df44 | 3859 | * @brief Carry out in interrupt mode the authentication tag generation as well as the ciphering or deciphering |
lypinator | 0:bb348c97df44 | 3860 | * operation according to hcryp->Init structure fields. |
lypinator | 0:bb348c97df44 | 3861 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 3862 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 3863 | * @param pInputData |
lypinator | 0:bb348c97df44 | 3864 | * - pointer to payload data in GCM payload phase, |
lypinator | 0:bb348c97df44 | 3865 | * - pointer to B0 block in CMAC header phase, |
lypinator | 0:bb348c97df44 | 3866 | * - pointer to C block in CMAC final phase. |
lypinator | 0:bb348c97df44 | 3867 | * Parameter is meaningless in case of GCM/GMAC init, header and final phases. |
lypinator | 0:bb348c97df44 | 3868 | * @param Size |
lypinator | 0:bb348c97df44 | 3869 | * - length of the input payload data buffer in bytes, |
lypinator | 0:bb348c97df44 | 3870 | * - length of B0 block (in bytes) in CMAC header phase, |
lypinator | 0:bb348c97df44 | 3871 | * - length of C block (in bytes) in CMAC final phase. |
lypinator | 0:bb348c97df44 | 3872 | * - Parameter is meaningless in case of GCM/GMAC init and header phases. |
lypinator | 0:bb348c97df44 | 3873 | * @param pOutputData |
lypinator | 0:bb348c97df44 | 3874 | * - pointer to plain or cipher text in GCM payload phase, |
lypinator | 0:bb348c97df44 | 3875 | * - pointer to authentication tag in GCM/GMAC and CMAC final phases. |
lypinator | 0:bb348c97df44 | 3876 | * - Parameter is meaningless in case of GCM/GMAC init and header phases |
lypinator | 0:bb348c97df44 | 3877 | * and in case of CMAC header phase. |
lypinator | 0:bb348c97df44 | 3878 | * @note Supported operating modes are encryption and decryption, supported chaining modes are GCM, GMAC and CMAC. |
lypinator | 0:bb348c97df44 | 3879 | * @note Phases are singly processed according to hcryp->Init.GCMCMACPhase so that steps in these specific chaining modes |
lypinator | 0:bb348c97df44 | 3880 | * can be skipped by the user if so required. |
lypinator | 0:bb348c97df44 | 3881 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 3882 | */ |
lypinator | 0:bb348c97df44 | 3883 | HAL_StatusTypeDef HAL_CRYPEx_AES_Auth_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint64_t Size, uint8_t *pOutputData) |
lypinator | 0:bb348c97df44 | 3884 | { |
lypinator | 0:bb348c97df44 | 3885 | |
lypinator | 0:bb348c97df44 | 3886 | uint32_t inputaddr = 0U; |
lypinator | 0:bb348c97df44 | 3887 | uint64_t headerlength = 0U; |
lypinator | 0:bb348c97df44 | 3888 | uint64_t inputlength = 0U; |
lypinator | 0:bb348c97df44 | 3889 | uint32_t index = 0U; |
lypinator | 0:bb348c97df44 | 3890 | uint32_t addhoc_process = 0U; |
lypinator | 0:bb348c97df44 | 3891 | uint32_t difflength = 0U; |
lypinator | 0:bb348c97df44 | 3892 | uint32_t difflengthmod4 = 0U; |
lypinator | 0:bb348c97df44 | 3893 | uint32_t mask[3U] = {0x0FFU, 0x0FFFFU, 0x0FFFFFFU}; |
lypinator | 0:bb348c97df44 | 3894 | |
lypinator | 0:bb348c97df44 | 3895 | |
lypinator | 0:bb348c97df44 | 3896 | if (hcryp->State == HAL_CRYP_STATE_READY) |
lypinator | 0:bb348c97df44 | 3897 | { |
lypinator | 0:bb348c97df44 | 3898 | /* input/output parameters check */ |
lypinator | 0:bb348c97df44 | 3899 | if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE) |
lypinator | 0:bb348c97df44 | 3900 | { |
lypinator | 0:bb348c97df44 | 3901 | if ((hcryp->Init.Header != NULL) && (hcryp->Init.HeaderSize == 0U)) |
lypinator | 0:bb348c97df44 | 3902 | { |
lypinator | 0:bb348c97df44 | 3903 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 3904 | } |
lypinator | 0:bb348c97df44 | 3905 | #if defined(AES_CR_NPBLB) |
lypinator | 0:bb348c97df44 | 3906 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC) |
lypinator | 0:bb348c97df44 | 3907 | #else |
lypinator | 0:bb348c97df44 | 3908 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) |
lypinator | 0:bb348c97df44 | 3909 | #endif |
lypinator | 0:bb348c97df44 | 3910 | { |
lypinator | 0:bb348c97df44 | 3911 | /* In case of CMAC header phase resumption, we can have pInputData = NULL and Size = 0 */ |
lypinator | 0:bb348c97df44 | 3912 | if (((pInputData != NULL) && (Size == 0U)) || ((pInputData == NULL) && (Size != 0U))) |
lypinator | 0:bb348c97df44 | 3913 | { |
lypinator | 0:bb348c97df44 | 3914 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 3915 | } |
lypinator | 0:bb348c97df44 | 3916 | } |
lypinator | 0:bb348c97df44 | 3917 | } |
lypinator | 0:bb348c97df44 | 3918 | else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE) |
lypinator | 0:bb348c97df44 | 3919 | { |
lypinator | 0:bb348c97df44 | 3920 | if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0U)) |
lypinator | 0:bb348c97df44 | 3921 | { |
lypinator | 0:bb348c97df44 | 3922 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 3923 | } |
lypinator | 0:bb348c97df44 | 3924 | } |
lypinator | 0:bb348c97df44 | 3925 | else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE) |
lypinator | 0:bb348c97df44 | 3926 | { |
lypinator | 0:bb348c97df44 | 3927 | if (pOutputData == NULL) |
lypinator | 0:bb348c97df44 | 3928 | { |
lypinator | 0:bb348c97df44 | 3929 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 3930 | } |
lypinator | 0:bb348c97df44 | 3931 | #if defined(AES_CR_NPBLB) |
lypinator | 0:bb348c97df44 | 3932 | if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC) && (pInputData == NULL)) |
lypinator | 0:bb348c97df44 | 3933 | #else |
lypinator | 0:bb348c97df44 | 3934 | if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (pInputData == NULL)) |
lypinator | 0:bb348c97df44 | 3935 | #endif |
lypinator | 0:bb348c97df44 | 3936 | { |
lypinator | 0:bb348c97df44 | 3937 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 3938 | } |
lypinator | 0:bb348c97df44 | 3939 | } |
lypinator | 0:bb348c97df44 | 3940 | |
lypinator | 0:bb348c97df44 | 3941 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 3942 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3943 | |
lypinator | 0:bb348c97df44 | 3944 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 3945 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 3946 | |
lypinator | 0:bb348c97df44 | 3947 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 3948 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 3949 | |
lypinator | 0:bb348c97df44 | 3950 | /* Enable Computation Complete Flag and Error Interrupts */ |
lypinator | 0:bb348c97df44 | 3951 | __HAL_CRYP_ENABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); |
lypinator | 0:bb348c97df44 | 3952 | |
lypinator | 0:bb348c97df44 | 3953 | /*==============================================*/ |
lypinator | 0:bb348c97df44 | 3954 | /* GCM/GMAC (or CCM when applicable) init phase */ |
lypinator | 0:bb348c97df44 | 3955 | /*==============================================*/ |
lypinator | 0:bb348c97df44 | 3956 | if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE) |
lypinator | 0:bb348c97df44 | 3957 | { |
lypinator | 0:bb348c97df44 | 3958 | /* In case of init phase, the input data (Key and Initialization Vector) have |
lypinator | 0:bb348c97df44 | 3959 | already been entered during the initialization process. Therefore, the |
lypinator | 0:bb348c97df44 | 3960 | software just waits for the CCF interrupt to be raised and which will |
lypinator | 0:bb348c97df44 | 3961 | be handled by CRYP_AES_Auth_IT() API. */ |
lypinator | 0:bb348c97df44 | 3962 | } |
lypinator | 0:bb348c97df44 | 3963 | /*=====================================*/ |
lypinator | 0:bb348c97df44 | 3964 | /* GCM/GMAC or (CCM/)CMAC header phase */ |
lypinator | 0:bb348c97df44 | 3965 | /*=====================================*/ |
lypinator | 0:bb348c97df44 | 3966 | else if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE) |
lypinator | 0:bb348c97df44 | 3967 | { |
lypinator | 0:bb348c97df44 | 3968 | |
lypinator | 0:bb348c97df44 | 3969 | #if defined(AES_CR_NPBLB) |
lypinator | 0:bb348c97df44 | 3970 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC) |
lypinator | 0:bb348c97df44 | 3971 | #else |
lypinator | 0:bb348c97df44 | 3972 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) |
lypinator | 0:bb348c97df44 | 3973 | #endif |
lypinator | 0:bb348c97df44 | 3974 | { |
lypinator | 0:bb348c97df44 | 3975 | /* In case of CMAC, B blocks are first entered, before the header. |
lypinator | 0:bb348c97df44 | 3976 | Therefore, B blocks and the header are entered back-to-back |
lypinator | 0:bb348c97df44 | 3977 | as if it was only one single block. |
lypinator | 0:bb348c97df44 | 3978 | However, in case of resumption after suspension, if all the |
lypinator | 0:bb348c97df44 | 3979 | B blocks have been entered (in that case, Size = 0), only the |
lypinator | 0:bb348c97df44 | 3980 | remainder of the non-processed header bytes are entered. */ |
lypinator | 0:bb348c97df44 | 3981 | if (Size != 0U) |
lypinator | 0:bb348c97df44 | 3982 | { |
lypinator | 0:bb348c97df44 | 3983 | hcryp->CrypInCount = Size + hcryp->Init.HeaderSize; |
lypinator | 0:bb348c97df44 | 3984 | hcryp->pCrypInBuffPtr = pInputData; |
lypinator | 0:bb348c97df44 | 3985 | } |
lypinator | 0:bb348c97df44 | 3986 | else |
lypinator | 0:bb348c97df44 | 3987 | { |
lypinator | 0:bb348c97df44 | 3988 | hcryp->CrypInCount = hcryp->Init.HeaderSize; |
lypinator | 0:bb348c97df44 | 3989 | hcryp->pCrypInBuffPtr = hcryp->Init.Header; |
lypinator | 0:bb348c97df44 | 3990 | } |
lypinator | 0:bb348c97df44 | 3991 | } |
lypinator | 0:bb348c97df44 | 3992 | else |
lypinator | 0:bb348c97df44 | 3993 | { |
lypinator | 0:bb348c97df44 | 3994 | /* Get the header addresses and sizes */ |
lypinator | 0:bb348c97df44 | 3995 | hcryp->CrypInCount = hcryp->Init.HeaderSize; |
lypinator | 0:bb348c97df44 | 3996 | hcryp->pCrypInBuffPtr = hcryp->Init.Header; |
lypinator | 0:bb348c97df44 | 3997 | } |
lypinator | 0:bb348c97df44 | 3998 | |
lypinator | 0:bb348c97df44 | 3999 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
lypinator | 0:bb348c97df44 | 4000 | |
lypinator | 0:bb348c97df44 | 4001 | /* Set header phase; for GCM or GMAC, set data-byte at this point */ |
lypinator | 0:bb348c97df44 | 4002 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) |
lypinator | 0:bb348c97df44 | 4003 | { |
lypinator | 0:bb348c97df44 | 4004 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH|AES_CR_DATATYPE, CRYP_HEADER_PHASE|hcryp->Init.DataType); |
lypinator | 0:bb348c97df44 | 4005 | } |
lypinator | 0:bb348c97df44 | 4006 | else |
lypinator | 0:bb348c97df44 | 4007 | { |
lypinator | 0:bb348c97df44 | 4008 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_HEADER_PHASE); |
lypinator | 0:bb348c97df44 | 4009 | } |
lypinator | 0:bb348c97df44 | 4010 | |
lypinator | 0:bb348c97df44 | 4011 | /* Enable the Peripheral */ |
lypinator | 0:bb348c97df44 | 4012 | __HAL_CRYP_ENABLE(); |
lypinator | 0:bb348c97df44 | 4013 | |
lypinator | 0:bb348c97df44 | 4014 | /* Increment/decrement instance pointer/counter */ |
lypinator | 0:bb348c97df44 | 4015 | if (hcryp->CrypInCount == 0U) |
lypinator | 0:bb348c97df44 | 4016 | { |
lypinator | 0:bb348c97df44 | 4017 | /* Case of no header */ |
lypinator | 0:bb348c97df44 | 4018 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 4019 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 4020 | } |
lypinator | 0:bb348c97df44 | 4021 | else if (hcryp->CrypInCount < 16U) |
lypinator | 0:bb348c97df44 | 4022 | { |
lypinator | 0:bb348c97df44 | 4023 | hcryp->CrypInCount = 0U; |
lypinator | 0:bb348c97df44 | 4024 | addhoc_process = 1U; |
lypinator | 0:bb348c97df44 | 4025 | difflength = (uint32_t) (hcryp->Init.HeaderSize); |
lypinator | 0:bb348c97df44 | 4026 | difflengthmod4 = difflength%4U; |
lypinator | 0:bb348c97df44 | 4027 | } |
lypinator | 0:bb348c97df44 | 4028 | else |
lypinator | 0:bb348c97df44 | 4029 | { |
lypinator | 0:bb348c97df44 | 4030 | hcryp->pCrypInBuffPtr += 16U; |
lypinator | 0:bb348c97df44 | 4031 | hcryp->CrypInCount -= 16U; |
lypinator | 0:bb348c97df44 | 4032 | } |
lypinator | 0:bb348c97df44 | 4033 | |
lypinator | 0:bb348c97df44 | 4034 | #if defined(AES_CR_NPBLB) |
lypinator | 0:bb348c97df44 | 4035 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC) |
lypinator | 0:bb348c97df44 | 4036 | #else |
lypinator | 0:bb348c97df44 | 4037 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) |
lypinator | 0:bb348c97df44 | 4038 | #endif |
lypinator | 0:bb348c97df44 | 4039 | { |
lypinator | 0:bb348c97df44 | 4040 | if (hcryp->CrypInCount == hcryp->Init.HeaderSize) |
lypinator | 0:bb348c97df44 | 4041 | { |
lypinator | 0:bb348c97df44 | 4042 | /* All B blocks will have been entered after the next |
lypinator | 0:bb348c97df44 | 4043 | four DINR writing, so point at header buffer for |
lypinator | 0:bb348c97df44 | 4044 | the next iteration */ |
lypinator | 0:bb348c97df44 | 4045 | hcryp->pCrypInBuffPtr = hcryp->Init.Header; |
lypinator | 0:bb348c97df44 | 4046 | } |
lypinator | 0:bb348c97df44 | 4047 | } |
lypinator | 0:bb348c97df44 | 4048 | |
lypinator | 0:bb348c97df44 | 4049 | /* Enter header first block to initiate the process |
lypinator | 0:bb348c97df44 | 4050 | in the Data Input register */ |
lypinator | 0:bb348c97df44 | 4051 | if (addhoc_process == 0U) |
lypinator | 0:bb348c97df44 | 4052 | { |
lypinator | 0:bb348c97df44 | 4053 | /* Header has size equal or larger than 128 bits */ |
lypinator | 0:bb348c97df44 | 4054 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 4055 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 4056 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 4057 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 4058 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 4059 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 4060 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 4061 | } |
lypinator | 0:bb348c97df44 | 4062 | else |
lypinator | 0:bb348c97df44 | 4063 | { |
lypinator | 0:bb348c97df44 | 4064 | /* Header has size less than 128 bits */ |
lypinator | 0:bb348c97df44 | 4065 | /* Enter complete words when possible */ |
lypinator | 0:bb348c97df44 | 4066 | for(index=0U; index < (difflength/4U); index ++) |
lypinator | 0:bb348c97df44 | 4067 | { |
lypinator | 0:bb348c97df44 | 4068 | /* Write the Input block in the Data Input register */ |
lypinator | 0:bb348c97df44 | 4069 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 4070 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 4071 | } |
lypinator | 0:bb348c97df44 | 4072 | /* Enter incomplete word padded with zeroes if applicable |
lypinator | 0:bb348c97df44 | 4073 | (case of header length not a multiple of 32-bits) */ |
lypinator | 0:bb348c97df44 | 4074 | if (difflengthmod4 != 0U) |
lypinator | 0:bb348c97df44 | 4075 | { |
lypinator | 0:bb348c97df44 | 4076 | hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[difflengthmod4-1U]); |
lypinator | 0:bb348c97df44 | 4077 | } |
lypinator | 0:bb348c97df44 | 4078 | /* Pad with zero-words to reach 128-bit long block and wrap-up header feeding to the IP */ |
lypinator | 0:bb348c97df44 | 4079 | for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++) |
lypinator | 0:bb348c97df44 | 4080 | { |
lypinator | 0:bb348c97df44 | 4081 | hcryp->Instance->DINR = 0U; |
lypinator | 0:bb348c97df44 | 4082 | } |
lypinator | 0:bb348c97df44 | 4083 | |
lypinator | 0:bb348c97df44 | 4084 | } |
lypinator | 0:bb348c97df44 | 4085 | } |
lypinator | 0:bb348c97df44 | 4086 | /*============================================*/ |
lypinator | 0:bb348c97df44 | 4087 | /* GCM (or CCM when applicable) payload phase */ |
lypinator | 0:bb348c97df44 | 4088 | /*============================================*/ |
lypinator | 0:bb348c97df44 | 4089 | else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE) |
lypinator | 0:bb348c97df44 | 4090 | { |
lypinator | 0:bb348c97df44 | 4091 | /* Get the buffer addresses and sizes */ |
lypinator | 0:bb348c97df44 | 4092 | hcryp->CrypInCount = Size; |
lypinator | 0:bb348c97df44 | 4093 | hcryp->pCrypInBuffPtr = pInputData; |
lypinator | 0:bb348c97df44 | 4094 | hcryp->pCrypOutBuffPtr = pOutputData; |
lypinator | 0:bb348c97df44 | 4095 | hcryp->CrypOutCount = Size; |
lypinator | 0:bb348c97df44 | 4096 | |
lypinator | 0:bb348c97df44 | 4097 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
lypinator | 0:bb348c97df44 | 4098 | |
lypinator | 0:bb348c97df44 | 4099 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_GCM_PAYLOAD_PHASE); |
lypinator | 0:bb348c97df44 | 4100 | |
lypinator | 0:bb348c97df44 | 4101 | /* if the header phase has been bypassed, AES must be enabled again */ |
lypinator | 0:bb348c97df44 | 4102 | if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER) |
lypinator | 0:bb348c97df44 | 4103 | { |
lypinator | 0:bb348c97df44 | 4104 | __HAL_CRYP_ENABLE(); |
lypinator | 0:bb348c97df44 | 4105 | } |
lypinator | 0:bb348c97df44 | 4106 | |
lypinator | 0:bb348c97df44 | 4107 | /* Specific handling to manage payload size less than 128 bits */ |
lypinator | 0:bb348c97df44 | 4108 | if (Size < 16U) |
lypinator | 0:bb348c97df44 | 4109 | { |
lypinator | 0:bb348c97df44 | 4110 | #if defined(AES_CR_NPBLB) |
lypinator | 0:bb348c97df44 | 4111 | /* In case of GCM encryption or CCM decryption, specify the number of padding |
lypinator | 0:bb348c97df44 | 4112 | bytes in last block of payload */ |
lypinator | 0:bb348c97df44 | 4113 | if (READ_BIT(hcryp->Instance->CR, AES_CR_GCMPH) == CRYP_PAYLOAD_PHASE) |
lypinator | 0:bb348c97df44 | 4114 | { |
lypinator | 0:bb348c97df44 | 4115 | if (((READ_BIT(hcryp->Instance->CR, AES_CR_CHMOD) == CRYP_CHAINMODE_AES_GCM_GMAC) |
lypinator | 0:bb348c97df44 | 4116 | && (READ_BIT(hcryp->Instance->CR, AES_CR_MODE) == CRYP_ALGOMODE_ENCRYPT)) |
lypinator | 0:bb348c97df44 | 4117 | || ((READ_BIT(hcryp->Instance->CR, AES_CR_CHMOD) == CRYP_CHAINMODE_AES_CCM_CMAC) |
lypinator | 0:bb348c97df44 | 4118 | && (READ_BIT(hcryp->Instance->CR, AES_CR_MODE) == CRYP_ALGOMODE_DECRYPT))) |
lypinator | 0:bb348c97df44 | 4119 | { |
lypinator | 0:bb348c97df44 | 4120 | /* Set NPBLB field in writing the number of padding bytes |
lypinator | 0:bb348c97df44 | 4121 | for the last block of payload */ |
lypinator | 0:bb348c97df44 | 4122 | MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, 16U - difflength); |
lypinator | 0:bb348c97df44 | 4123 | } |
lypinator | 0:bb348c97df44 | 4124 | } |
lypinator | 0:bb348c97df44 | 4125 | #else |
lypinator | 0:bb348c97df44 | 4126 | /* Software workaround applied to GCM encryption only */ |
lypinator | 0:bb348c97df44 | 4127 | if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT) |
lypinator | 0:bb348c97df44 | 4128 | { |
lypinator | 0:bb348c97df44 | 4129 | /* Change the mode configured in CHMOD bits of CR register to select CTR mode */ |
lypinator | 0:bb348c97df44 | 4130 | __HAL_CRYP_SET_CHAININGMODE(CRYP_CHAINMODE_AES_CTR); |
lypinator | 0:bb348c97df44 | 4131 | } |
lypinator | 0:bb348c97df44 | 4132 | #endif |
lypinator | 0:bb348c97df44 | 4133 | |
lypinator | 0:bb348c97df44 | 4134 | /* Set hcryp->CrypInCount to 0 (no more data to enter) */ |
lypinator | 0:bb348c97df44 | 4135 | hcryp->CrypInCount = 0U; |
lypinator | 0:bb348c97df44 | 4136 | |
lypinator | 0:bb348c97df44 | 4137 | /* Insert the last block (which size is inferior to 128 bits) padded with zeroes, |
lypinator | 0:bb348c97df44 | 4138 | to have a complete block of 128 bits */ |
lypinator | 0:bb348c97df44 | 4139 | difflength = (uint32_t) (Size); |
lypinator | 0:bb348c97df44 | 4140 | difflengthmod4 = difflength%4U; |
lypinator | 0:bb348c97df44 | 4141 | /* Insert the last block (which size is inferior to 128 bits) padded with zeroes |
lypinator | 0:bb348c97df44 | 4142 | to have a complete block of 128 bits */ |
lypinator | 0:bb348c97df44 | 4143 | for(index=0U; index < (difflength/4U); index ++) |
lypinator | 0:bb348c97df44 | 4144 | { |
lypinator | 0:bb348c97df44 | 4145 | /* Write the Input block in the Data Input register */ |
lypinator | 0:bb348c97df44 | 4146 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 4147 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 4148 | } |
lypinator | 0:bb348c97df44 | 4149 | /* If required, manage input data size not multiple of 32 bits */ |
lypinator | 0:bb348c97df44 | 4150 | if (difflengthmod4 != 0U) |
lypinator | 0:bb348c97df44 | 4151 | { |
lypinator | 0:bb348c97df44 | 4152 | hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[difflengthmod4-1U]); |
lypinator | 0:bb348c97df44 | 4153 | } |
lypinator | 0:bb348c97df44 | 4154 | /* Wrap-up in padding with zero-words if applicable */ |
lypinator | 0:bb348c97df44 | 4155 | for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++) |
lypinator | 0:bb348c97df44 | 4156 | { |
lypinator | 0:bb348c97df44 | 4157 | hcryp->Instance->DINR = 0U; |
lypinator | 0:bb348c97df44 | 4158 | } |
lypinator | 0:bb348c97df44 | 4159 | } |
lypinator | 0:bb348c97df44 | 4160 | else |
lypinator | 0:bb348c97df44 | 4161 | { |
lypinator | 0:bb348c97df44 | 4162 | /* Increment/decrement instance pointer/counter */ |
lypinator | 0:bb348c97df44 | 4163 | hcryp->pCrypInBuffPtr += 16U; |
lypinator | 0:bb348c97df44 | 4164 | hcryp->CrypInCount -= 16U; |
lypinator | 0:bb348c97df44 | 4165 | |
lypinator | 0:bb348c97df44 | 4166 | /* Enter payload first block to initiate the process |
lypinator | 0:bb348c97df44 | 4167 | in the Data Input register */ |
lypinator | 0:bb348c97df44 | 4168 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 4169 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 4170 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 4171 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 4172 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 4173 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 4174 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 4175 | } |
lypinator | 0:bb348c97df44 | 4176 | } |
lypinator | 0:bb348c97df44 | 4177 | /*====================================*/ |
lypinator | 0:bb348c97df44 | 4178 | /* GCM/GMAC or (CCM/)CMAC final phase */ |
lypinator | 0:bb348c97df44 | 4179 | /*====================================*/ |
lypinator | 0:bb348c97df44 | 4180 | else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE) |
lypinator | 0:bb348c97df44 | 4181 | { |
lypinator | 0:bb348c97df44 | 4182 | hcryp->pCrypOutBuffPtr = pOutputData; |
lypinator | 0:bb348c97df44 | 4183 | |
lypinator | 0:bb348c97df44 | 4184 | #if defined(AES_CR_NPBLB) |
lypinator | 0:bb348c97df44 | 4185 | /* By default, clear NPBLB field */ |
lypinator | 0:bb348c97df44 | 4186 | CLEAR_BIT(hcryp->Instance->CR, AES_CR_NPBLB); |
lypinator | 0:bb348c97df44 | 4187 | #endif |
lypinator | 0:bb348c97df44 | 4188 | |
lypinator | 0:bb348c97df44 | 4189 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_FINAL_PHASE); |
lypinator | 0:bb348c97df44 | 4190 | |
lypinator | 0:bb348c97df44 | 4191 | /* if the header and payload phases have been bypassed, AES must be enabled again */ |
lypinator | 0:bb348c97df44 | 4192 | if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER) |
lypinator | 0:bb348c97df44 | 4193 | { |
lypinator | 0:bb348c97df44 | 4194 | __HAL_CRYP_ENABLE(); |
lypinator | 0:bb348c97df44 | 4195 | } |
lypinator | 0:bb348c97df44 | 4196 | |
lypinator | 0:bb348c97df44 | 4197 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) |
lypinator | 0:bb348c97df44 | 4198 | { |
lypinator | 0:bb348c97df44 | 4199 | headerlength = hcryp->Init.HeaderSize * 8U; /* Header length in bits */ |
lypinator | 0:bb348c97df44 | 4200 | inputlength = Size * 8U; /* Input length in bits */ |
lypinator | 0:bb348c97df44 | 4201 | /* Write the number of bits in the header on 64 bits followed by the number |
lypinator | 0:bb348c97df44 | 4202 | of bits in the payload on 64 bits as well */ |
lypinator | 0:bb348c97df44 | 4203 | if(hcryp->Init.DataType == CRYP_DATATYPE_1B) |
lypinator | 0:bb348c97df44 | 4204 | { |
lypinator | 0:bb348c97df44 | 4205 | hcryp->Instance->DINR = __RBIT((headerlength)>>32U); |
lypinator | 0:bb348c97df44 | 4206 | hcryp->Instance->DINR = __RBIT(headerlength); |
lypinator | 0:bb348c97df44 | 4207 | hcryp->Instance->DINR = __RBIT((inputlength)>>32U); |
lypinator | 0:bb348c97df44 | 4208 | hcryp->Instance->DINR = __RBIT(inputlength); |
lypinator | 0:bb348c97df44 | 4209 | } |
lypinator | 0:bb348c97df44 | 4210 | else if(hcryp->Init.DataType == CRYP_DATATYPE_8B) |
lypinator | 0:bb348c97df44 | 4211 | { |
lypinator | 0:bb348c97df44 | 4212 | hcryp->Instance->DINR = __REV((headerlength)>>32U); |
lypinator | 0:bb348c97df44 | 4213 | hcryp->Instance->DINR = __REV(headerlength); |
lypinator | 0:bb348c97df44 | 4214 | hcryp->Instance->DINR = __REV((inputlength)>>32U); |
lypinator | 0:bb348c97df44 | 4215 | hcryp->Instance->DINR = __REV(inputlength); |
lypinator | 0:bb348c97df44 | 4216 | } |
lypinator | 0:bb348c97df44 | 4217 | else if(hcryp->Init.DataType == CRYP_DATATYPE_16B) |
lypinator | 0:bb348c97df44 | 4218 | { |
lypinator | 0:bb348c97df44 | 4219 | hcryp->Instance->DINR = __ROR((headerlength)>>32U, 16U); |
lypinator | 0:bb348c97df44 | 4220 | hcryp->Instance->DINR = __ROR(headerlength, 16U); |
lypinator | 0:bb348c97df44 | 4221 | hcryp->Instance->DINR = __ROR((inputlength)>>32U, 16U); |
lypinator | 0:bb348c97df44 | 4222 | hcryp->Instance->DINR = __ROR(inputlength, 16U); |
lypinator | 0:bb348c97df44 | 4223 | } |
lypinator | 0:bb348c97df44 | 4224 | else if(hcryp->Init.DataType == CRYP_DATATYPE_32B) |
lypinator | 0:bb348c97df44 | 4225 | { |
lypinator | 0:bb348c97df44 | 4226 | hcryp->Instance->DINR = (uint32_t)(headerlength>>32U); |
lypinator | 0:bb348c97df44 | 4227 | hcryp->Instance->DINR = (uint32_t)(headerlength); |
lypinator | 0:bb348c97df44 | 4228 | hcryp->Instance->DINR = (uint32_t)(inputlength>>32U); |
lypinator | 0:bb348c97df44 | 4229 | hcryp->Instance->DINR = (uint32_t)(inputlength); |
lypinator | 0:bb348c97df44 | 4230 | } |
lypinator | 0:bb348c97df44 | 4231 | } |
lypinator | 0:bb348c97df44 | 4232 | #if !defined(AES_CR_NPBLB) |
lypinator | 0:bb348c97df44 | 4233 | else if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) |
lypinator | 0:bb348c97df44 | 4234 | { |
lypinator | 0:bb348c97df44 | 4235 | inputaddr = (uint32_t)pInputData; |
lypinator | 0:bb348c97df44 | 4236 | /* Enter the last block made of a 128-bit value formatted |
lypinator | 0:bb348c97df44 | 4237 | from the original B0 packet. */ |
lypinator | 0:bb348c97df44 | 4238 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 4239 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 4240 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 4241 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 4242 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 4243 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 4244 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 4245 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 4246 | } |
lypinator | 0:bb348c97df44 | 4247 | #endif |
lypinator | 0:bb348c97df44 | 4248 | } |
lypinator | 0:bb348c97df44 | 4249 | /*=================================================*/ |
lypinator | 0:bb348c97df44 | 4250 | /* case incorrect hcryp->Init.GCMCMACPhase setting */ |
lypinator | 0:bb348c97df44 | 4251 | /*=================================================*/ |
lypinator | 0:bb348c97df44 | 4252 | else |
lypinator | 0:bb348c97df44 | 4253 | { |
lypinator | 0:bb348c97df44 | 4254 | hcryp->State = HAL_CRYP_STATE_ERROR; |
lypinator | 0:bb348c97df44 | 4255 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4256 | } |
lypinator | 0:bb348c97df44 | 4257 | |
lypinator | 0:bb348c97df44 | 4258 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 4259 | } |
lypinator | 0:bb348c97df44 | 4260 | else |
lypinator | 0:bb348c97df44 | 4261 | { |
lypinator | 0:bb348c97df44 | 4262 | return HAL_BUSY; |
lypinator | 0:bb348c97df44 | 4263 | } |
lypinator | 0:bb348c97df44 | 4264 | } |
lypinator | 0:bb348c97df44 | 4265 | |
lypinator | 0:bb348c97df44 | 4266 | |
lypinator | 0:bb348c97df44 | 4267 | |
lypinator | 0:bb348c97df44 | 4268 | |
lypinator | 0:bb348c97df44 | 4269 | /** |
lypinator | 0:bb348c97df44 | 4270 | * @brief Carry out in DMA mode the authentication tag generation as well as the ciphering or deciphering |
lypinator | 0:bb348c97df44 | 4271 | * operation according to hcryp->Init structure fields. |
lypinator | 0:bb348c97df44 | 4272 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4273 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 4274 | * @param pInputData |
lypinator | 0:bb348c97df44 | 4275 | * - pointer to payload data in GCM payload phase, |
lypinator | 0:bb348c97df44 | 4276 | * - pointer to B0 block in CMAC header phase, |
lypinator | 0:bb348c97df44 | 4277 | * - pointer to C block in CMAC final phase. |
lypinator | 0:bb348c97df44 | 4278 | * - Parameter is meaningless in case of GCM/GMAC init, header and final phases. |
lypinator | 0:bb348c97df44 | 4279 | * @param Size |
lypinator | 0:bb348c97df44 | 4280 | * - length of the input payload data buffer in bytes, |
lypinator | 0:bb348c97df44 | 4281 | * - length of B block (in bytes) in CMAC header phase, |
lypinator | 0:bb348c97df44 | 4282 | * - length of C block (in bytes) in CMAC final phase. |
lypinator | 0:bb348c97df44 | 4283 | * - Parameter is meaningless in case of GCM/GMAC init and header phases. |
lypinator | 0:bb348c97df44 | 4284 | * @param pOutputData |
lypinator | 0:bb348c97df44 | 4285 | * - pointer to plain or cipher text in GCM payload phase, |
lypinator | 0:bb348c97df44 | 4286 | * - pointer to authentication tag in GCM/GMAC and CMAC final phases. |
lypinator | 0:bb348c97df44 | 4287 | * - Parameter is meaningless in case of GCM/GMAC init and header phases |
lypinator | 0:bb348c97df44 | 4288 | * and in case of CMAC header phase. |
lypinator | 0:bb348c97df44 | 4289 | * @note Supported operating modes are encryption and decryption, supported chaining modes are GCM, GMAC and CMAC. |
lypinator | 0:bb348c97df44 | 4290 | * @note Phases are singly processed according to hcryp->Init.GCMCMACPhase so that steps in these specific chaining modes |
lypinator | 0:bb348c97df44 | 4291 | * can be skipped by the user if so required. |
lypinator | 0:bb348c97df44 | 4292 | * @note pInputData and pOutputData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP. |
lypinator | 0:bb348c97df44 | 4293 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 4294 | */ |
lypinator | 0:bb348c97df44 | 4295 | HAL_StatusTypeDef HAL_CRYPEx_AES_Auth_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint64_t Size, uint8_t *pOutputData) |
lypinator | 0:bb348c97df44 | 4296 | { |
lypinator | 0:bb348c97df44 | 4297 | uint32_t inputaddr = 0U; |
lypinator | 0:bb348c97df44 | 4298 | uint32_t outputaddr = 0U; |
lypinator | 0:bb348c97df44 | 4299 | uint32_t tagaddr = 0U; |
lypinator | 0:bb348c97df44 | 4300 | uint64_t headerlength = 0U; |
lypinator | 0:bb348c97df44 | 4301 | uint64_t inputlength = 0U; |
lypinator | 0:bb348c97df44 | 4302 | uint64_t payloadlength = 0U; |
lypinator | 0:bb348c97df44 | 4303 | |
lypinator | 0:bb348c97df44 | 4304 | |
lypinator | 0:bb348c97df44 | 4305 | if (hcryp->State == HAL_CRYP_STATE_READY) |
lypinator | 0:bb348c97df44 | 4306 | { |
lypinator | 0:bb348c97df44 | 4307 | /* input/output parameters check */ |
lypinator | 0:bb348c97df44 | 4308 | if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE) |
lypinator | 0:bb348c97df44 | 4309 | { |
lypinator | 0:bb348c97df44 | 4310 | if ((hcryp->Init.Header != NULL) && (hcryp->Init.HeaderSize == 0U)) |
lypinator | 0:bb348c97df44 | 4311 | { |
lypinator | 0:bb348c97df44 | 4312 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4313 | } |
lypinator | 0:bb348c97df44 | 4314 | #if defined(AES_CR_NPBLB) |
lypinator | 0:bb348c97df44 | 4315 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC) |
lypinator | 0:bb348c97df44 | 4316 | #else |
lypinator | 0:bb348c97df44 | 4317 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) |
lypinator | 0:bb348c97df44 | 4318 | #endif |
lypinator | 0:bb348c97df44 | 4319 | { |
lypinator | 0:bb348c97df44 | 4320 | if ((pInputData == NULL) || (Size == 0U)) |
lypinator | 0:bb348c97df44 | 4321 | { |
lypinator | 0:bb348c97df44 | 4322 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4323 | } |
lypinator | 0:bb348c97df44 | 4324 | } |
lypinator | 0:bb348c97df44 | 4325 | } |
lypinator | 0:bb348c97df44 | 4326 | else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE) |
lypinator | 0:bb348c97df44 | 4327 | { |
lypinator | 0:bb348c97df44 | 4328 | if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0U)) |
lypinator | 0:bb348c97df44 | 4329 | { |
lypinator | 0:bb348c97df44 | 4330 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4331 | } |
lypinator | 0:bb348c97df44 | 4332 | } |
lypinator | 0:bb348c97df44 | 4333 | else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE) |
lypinator | 0:bb348c97df44 | 4334 | { |
lypinator | 0:bb348c97df44 | 4335 | if (pOutputData == NULL) |
lypinator | 0:bb348c97df44 | 4336 | { |
lypinator | 0:bb348c97df44 | 4337 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4338 | } |
lypinator | 0:bb348c97df44 | 4339 | #if defined(AES_CR_NPBLB) |
lypinator | 0:bb348c97df44 | 4340 | if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC) && (pInputData == NULL)) |
lypinator | 0:bb348c97df44 | 4341 | #else |
lypinator | 0:bb348c97df44 | 4342 | if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (pInputData == NULL)) |
lypinator | 0:bb348c97df44 | 4343 | #endif |
lypinator | 0:bb348c97df44 | 4344 | { |
lypinator | 0:bb348c97df44 | 4345 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4346 | } |
lypinator | 0:bb348c97df44 | 4347 | } |
lypinator | 0:bb348c97df44 | 4348 | |
lypinator | 0:bb348c97df44 | 4349 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 4350 | __HAL_LOCK(hcryp); |
lypinator | 0:bb348c97df44 | 4351 | |
lypinator | 0:bb348c97df44 | 4352 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 4353 | hcryp->State = HAL_CRYP_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 4354 | |
lypinator | 0:bb348c97df44 | 4355 | /*==============================================*/ |
lypinator | 0:bb348c97df44 | 4356 | /* GCM/GMAC (or CCM when applicable) init phase */ |
lypinator | 0:bb348c97df44 | 4357 | /*==============================================*/ |
lypinator | 0:bb348c97df44 | 4358 | /* In case of init phase, the input data (Key and Initialization Vector) have |
lypinator | 0:bb348c97df44 | 4359 | already been entered during the initialization process. No DMA transfer is |
lypinator | 0:bb348c97df44 | 4360 | required at that point therefore, the software just waits for the CCF flag |
lypinator | 0:bb348c97df44 | 4361 | to be raised. */ |
lypinator | 0:bb348c97df44 | 4362 | if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE) |
lypinator | 0:bb348c97df44 | 4363 | { |
lypinator | 0:bb348c97df44 | 4364 | /* just wait for hash computation */ |
lypinator | 0:bb348c97df44 | 4365 | if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4366 | { |
lypinator | 0:bb348c97df44 | 4367 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 4368 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 4369 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 4370 | } |
lypinator | 0:bb348c97df44 | 4371 | |
lypinator | 0:bb348c97df44 | 4372 | /* Clear CCF Flag */ |
lypinator | 0:bb348c97df44 | 4373 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
lypinator | 0:bb348c97df44 | 4374 | /* Mark that the initialization phase is over */ |
lypinator | 0:bb348c97df44 | 4375 | hcryp->Phase = HAL_CRYP_PHASE_INIT_OVER; |
lypinator | 0:bb348c97df44 | 4376 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 4377 | } |
lypinator | 0:bb348c97df44 | 4378 | /*===============================*/ |
lypinator | 0:bb348c97df44 | 4379 | /* GCM/GMAC or CMAC header phase */ |
lypinator | 0:bb348c97df44 | 4380 | /*===============================*/ |
lypinator | 0:bb348c97df44 | 4381 | else if (hcryp->Init.GCMCMACPhase == CRYP_GCMCMAC_HEADER_PHASE) |
lypinator | 0:bb348c97df44 | 4382 | { |
lypinator | 0:bb348c97df44 | 4383 | /* Set header phase; for GCM or GMAC, set data-byte at this point */ |
lypinator | 0:bb348c97df44 | 4384 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) |
lypinator | 0:bb348c97df44 | 4385 | { |
lypinator | 0:bb348c97df44 | 4386 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH|AES_CR_DATATYPE, CRYP_GCMCMAC_HEADER_PHASE|hcryp->Init.DataType); |
lypinator | 0:bb348c97df44 | 4387 | } |
lypinator | 0:bb348c97df44 | 4388 | else |
lypinator | 0:bb348c97df44 | 4389 | { |
lypinator | 0:bb348c97df44 | 4390 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_GCMCMAC_HEADER_PHASE); |
lypinator | 0:bb348c97df44 | 4391 | } |
lypinator | 0:bb348c97df44 | 4392 | |
lypinator | 0:bb348c97df44 | 4393 | #if !defined(AES_CR_NPBLB) |
lypinator | 0:bb348c97df44 | 4394 | /* enter first B0 block in polling mode (no DMA transfer for B0) */ |
lypinator | 0:bb348c97df44 | 4395 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) |
lypinator | 0:bb348c97df44 | 4396 | { |
lypinator | 0:bb348c97df44 | 4397 | /* Enable the CRYP peripheral */ |
lypinator | 0:bb348c97df44 | 4398 | __HAL_CRYP_ENABLE(); |
lypinator | 0:bb348c97df44 | 4399 | |
lypinator | 0:bb348c97df44 | 4400 | inputaddr = (uint32_t)pInputData; |
lypinator | 0:bb348c97df44 | 4401 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 4402 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 4403 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 4404 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 4405 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 4406 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 4407 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 4408 | |
lypinator | 0:bb348c97df44 | 4409 | if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4410 | { |
lypinator | 0:bb348c97df44 | 4411 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 4412 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 4413 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 4414 | } |
lypinator | 0:bb348c97df44 | 4415 | /* Clear CCF Flag */ |
lypinator | 0:bb348c97df44 | 4416 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
lypinator | 0:bb348c97df44 | 4417 | } |
lypinator | 0:bb348c97df44 | 4418 | #endif |
lypinator | 0:bb348c97df44 | 4419 | |
lypinator | 0:bb348c97df44 | 4420 | /* No header case */ |
lypinator | 0:bb348c97df44 | 4421 | if (hcryp->Init.Header == NULL) |
lypinator | 0:bb348c97df44 | 4422 | { |
lypinator | 0:bb348c97df44 | 4423 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 4424 | /* Mark that the header phase is over */ |
lypinator | 0:bb348c97df44 | 4425 | hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER; |
lypinator | 0:bb348c97df44 | 4426 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 4427 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 4428 | |
lypinator | 0:bb348c97df44 | 4429 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 4430 | } |
lypinator | 0:bb348c97df44 | 4431 | |
lypinator | 0:bb348c97df44 | 4432 | inputaddr = (uint32_t)hcryp->Init.Header; |
lypinator | 0:bb348c97df44 | 4433 | if ((hcryp->Init.HeaderSize % 16U) != 0U) |
lypinator | 0:bb348c97df44 | 4434 | { |
lypinator | 0:bb348c97df44 | 4435 | |
lypinator | 0:bb348c97df44 | 4436 | if (hcryp->Init.HeaderSize < 16U) |
lypinator | 0:bb348c97df44 | 4437 | { |
lypinator | 0:bb348c97df44 | 4438 | CRYP_Padding(hcryp, (uint32_t) (hcryp->Init.HeaderSize), CRYP_POLLING_OFF); |
lypinator | 0:bb348c97df44 | 4439 | |
lypinator | 0:bb348c97df44 | 4440 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 4441 | /* Mark that the header phase is over */ |
lypinator | 0:bb348c97df44 | 4442 | hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER; |
lypinator | 0:bb348c97df44 | 4443 | |
lypinator | 0:bb348c97df44 | 4444 | /* CCF flag indicating header phase AES processing completion |
lypinator | 0:bb348c97df44 | 4445 | will be checked at the start of the next phase: |
lypinator | 0:bb348c97df44 | 4446 | - payload phase (GCM / CCM when applicable) |
lypinator | 0:bb348c97df44 | 4447 | - final phase (GMAC or CMAC). */ |
lypinator | 0:bb348c97df44 | 4448 | } |
lypinator | 0:bb348c97df44 | 4449 | else |
lypinator | 0:bb348c97df44 | 4450 | { |
lypinator | 0:bb348c97df44 | 4451 | /* Local variable headerlength is a number of bytes multiple of 128 bits, |
lypinator | 0:bb348c97df44 | 4452 | remaining header data (if any) are handled after this loop */ |
lypinator | 0:bb348c97df44 | 4453 | headerlength = (((hcryp->Init.HeaderSize)/16U)*16U) ; |
lypinator | 0:bb348c97df44 | 4454 | /* Store the ending transfer point */ |
lypinator | 0:bb348c97df44 | 4455 | hcryp->pCrypInBuffPtr = hcryp->Init.Header + headerlength; |
lypinator | 0:bb348c97df44 | 4456 | hcryp->CrypInCount = (uint32_t)(hcryp->Init.HeaderSize - headerlength); /* remainder */ |
lypinator | 0:bb348c97df44 | 4457 | |
lypinator | 0:bb348c97df44 | 4458 | /* Set the input and output addresses and start DMA transfer */ |
lypinator | 0:bb348c97df44 | 4459 | /* (incomplete DMA transfer, will be wrapped up after completion of |
lypinator | 0:bb348c97df44 | 4460 | the first one (initiated here) with data padding */ |
lypinator | 0:bb348c97df44 | 4461 | CRYP_GCMCMAC_SetDMAConfig(hcryp, inputaddr, headerlength, 0U); |
lypinator | 0:bb348c97df44 | 4462 | } |
lypinator | 0:bb348c97df44 | 4463 | } |
lypinator | 0:bb348c97df44 | 4464 | else |
lypinator | 0:bb348c97df44 | 4465 | { |
lypinator | 0:bb348c97df44 | 4466 | hcryp->CrypInCount = 0U; |
lypinator | 0:bb348c97df44 | 4467 | /* Set the input address and start DMA transfer */ |
lypinator | 0:bb348c97df44 | 4468 | CRYP_GCMCMAC_SetDMAConfig(hcryp, inputaddr, hcryp->Init.HeaderSize, 0U); |
lypinator | 0:bb348c97df44 | 4469 | } |
lypinator | 0:bb348c97df44 | 4470 | |
lypinator | 0:bb348c97df44 | 4471 | } |
lypinator | 0:bb348c97df44 | 4472 | /*============================================*/ |
lypinator | 0:bb348c97df44 | 4473 | /* GCM (or CCM when applicable) payload phase */ |
lypinator | 0:bb348c97df44 | 4474 | /*============================================*/ |
lypinator | 0:bb348c97df44 | 4475 | else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE) |
lypinator | 0:bb348c97df44 | 4476 | { |
lypinator | 0:bb348c97df44 | 4477 | /* Coming from header phase, wait for CCF flag to be raised |
lypinator | 0:bb348c97df44 | 4478 | if header present and fed to the IP in the previous phase */ |
lypinator | 0:bb348c97df44 | 4479 | if (hcryp->Init.Header != NULL) |
lypinator | 0:bb348c97df44 | 4480 | { |
lypinator | 0:bb348c97df44 | 4481 | if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4482 | { |
lypinator | 0:bb348c97df44 | 4483 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 4484 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 4485 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 4486 | } |
lypinator | 0:bb348c97df44 | 4487 | } |
lypinator | 0:bb348c97df44 | 4488 | else |
lypinator | 0:bb348c97df44 | 4489 | { |
lypinator | 0:bb348c97df44 | 4490 | /* Enable the Peripheral since wasn't in header phase (no header case) */ |
lypinator | 0:bb348c97df44 | 4491 | __HAL_CRYP_ENABLE(); |
lypinator | 0:bb348c97df44 | 4492 | } |
lypinator | 0:bb348c97df44 | 4493 | /* Clear CCF Flag */ |
lypinator | 0:bb348c97df44 | 4494 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
lypinator | 0:bb348c97df44 | 4495 | |
lypinator | 0:bb348c97df44 | 4496 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PAYLOAD_PHASE); |
lypinator | 0:bb348c97df44 | 4497 | |
lypinator | 0:bb348c97df44 | 4498 | /* Specific handling to manage payload size less than 128 bits */ |
lypinator | 0:bb348c97df44 | 4499 | if ((Size % 16U) != 0U) |
lypinator | 0:bb348c97df44 | 4500 | { |
lypinator | 0:bb348c97df44 | 4501 | inputaddr = (uint32_t)pInputData; |
lypinator | 0:bb348c97df44 | 4502 | outputaddr = (uint32_t)pOutputData; |
lypinator | 0:bb348c97df44 | 4503 | if (Size < 16U) |
lypinator | 0:bb348c97df44 | 4504 | { |
lypinator | 0:bb348c97df44 | 4505 | /* Block is now entered in polling mode, no actual gain in resorting to DMA */ |
lypinator | 0:bb348c97df44 | 4506 | hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr; |
lypinator | 0:bb348c97df44 | 4507 | hcryp->pCrypOutBuffPtr = (uint8_t *)outputaddr; |
lypinator | 0:bb348c97df44 | 4508 | |
lypinator | 0:bb348c97df44 | 4509 | CRYP_Padding(hcryp, (uint32_t)Size, CRYP_POLLING_ON); |
lypinator | 0:bb348c97df44 | 4510 | |
lypinator | 0:bb348c97df44 | 4511 | /* Change the CRYP state to ready */ |
lypinator | 0:bb348c97df44 | 4512 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 4513 | /* Mark that the payload phase is over */ |
lypinator | 0:bb348c97df44 | 4514 | hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER; |
lypinator | 0:bb348c97df44 | 4515 | |
lypinator | 0:bb348c97df44 | 4516 | /* Call output data transfer complete callback */ |
lypinator | 0:bb348c97df44 | 4517 | HAL_CRYP_OutCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 4518 | } |
lypinator | 0:bb348c97df44 | 4519 | else |
lypinator | 0:bb348c97df44 | 4520 | { |
lypinator | 0:bb348c97df44 | 4521 | payloadlength = (Size/16U) * 16U; |
lypinator | 0:bb348c97df44 | 4522 | |
lypinator | 0:bb348c97df44 | 4523 | /* Store the ending transfer points */ |
lypinator | 0:bb348c97df44 | 4524 | hcryp->pCrypInBuffPtr = pInputData + payloadlength; |
lypinator | 0:bb348c97df44 | 4525 | hcryp->pCrypOutBuffPtr = pOutputData + payloadlength; |
lypinator | 0:bb348c97df44 | 4526 | hcryp->CrypInCount = (uint32_t)(Size - payloadlength); /* remainder */ |
lypinator | 0:bb348c97df44 | 4527 | |
lypinator | 0:bb348c97df44 | 4528 | /* Set the input and output addresses and start DMA transfer */ |
lypinator | 0:bb348c97df44 | 4529 | /* (incomplete DMA transfer, will be wrapped up with data padding |
lypinator | 0:bb348c97df44 | 4530 | after completion of the one initiated here) */ |
lypinator | 0:bb348c97df44 | 4531 | CRYP_GCMCMAC_SetDMAConfig(hcryp, inputaddr, payloadlength, outputaddr); |
lypinator | 0:bb348c97df44 | 4532 | } |
lypinator | 0:bb348c97df44 | 4533 | } |
lypinator | 0:bb348c97df44 | 4534 | else |
lypinator | 0:bb348c97df44 | 4535 | { |
lypinator | 0:bb348c97df44 | 4536 | hcryp->CrypInCount = 0U; |
lypinator | 0:bb348c97df44 | 4537 | inputaddr = (uint32_t)pInputData; |
lypinator | 0:bb348c97df44 | 4538 | outputaddr = (uint32_t)pOutputData; |
lypinator | 0:bb348c97df44 | 4539 | |
lypinator | 0:bb348c97df44 | 4540 | /* Set the input and output addresses and start DMA transfer */ |
lypinator | 0:bb348c97df44 | 4541 | CRYP_GCMCMAC_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); |
lypinator | 0:bb348c97df44 | 4542 | } |
lypinator | 0:bb348c97df44 | 4543 | } |
lypinator | 0:bb348c97df44 | 4544 | /*====================================*/ |
lypinator | 0:bb348c97df44 | 4545 | /* GCM/GMAC or (CCM/)CMAC final phase */ |
lypinator | 0:bb348c97df44 | 4546 | /*====================================*/ |
lypinator | 0:bb348c97df44 | 4547 | else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE) |
lypinator | 0:bb348c97df44 | 4548 | { |
lypinator | 0:bb348c97df44 | 4549 | /* If coming from header phase (GMAC or CMAC case), |
lypinator | 0:bb348c97df44 | 4550 | wait for CCF flag to be raised */ |
lypinator | 0:bb348c97df44 | 4551 | if (READ_BIT(hcryp->Instance->CR, AES_CR_GCMPH) == CRYP_HEADER_PHASE) |
lypinator | 0:bb348c97df44 | 4552 | { |
lypinator | 0:bb348c97df44 | 4553 | if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4554 | { |
lypinator | 0:bb348c97df44 | 4555 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 4556 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 4557 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 4558 | } |
lypinator | 0:bb348c97df44 | 4559 | /* Clear CCF Flag */ |
lypinator | 0:bb348c97df44 | 4560 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
lypinator | 0:bb348c97df44 | 4561 | } |
lypinator | 0:bb348c97df44 | 4562 | |
lypinator | 0:bb348c97df44 | 4563 | tagaddr = (uint32_t)pOutputData; |
lypinator | 0:bb348c97df44 | 4564 | |
lypinator | 0:bb348c97df44 | 4565 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_FINAL_PHASE); |
lypinator | 0:bb348c97df44 | 4566 | |
lypinator | 0:bb348c97df44 | 4567 | /* if the header and payload phases have been bypassed, AES must be enabled again */ |
lypinator | 0:bb348c97df44 | 4568 | if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER) |
lypinator | 0:bb348c97df44 | 4569 | { |
lypinator | 0:bb348c97df44 | 4570 | __HAL_CRYP_ENABLE(); |
lypinator | 0:bb348c97df44 | 4571 | } |
lypinator | 0:bb348c97df44 | 4572 | |
lypinator | 0:bb348c97df44 | 4573 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) |
lypinator | 0:bb348c97df44 | 4574 | { |
lypinator | 0:bb348c97df44 | 4575 | headerlength = hcryp->Init.HeaderSize * 8U; /* Header length in bits */ |
lypinator | 0:bb348c97df44 | 4576 | inputlength = Size * 8U; /* input length in bits */ |
lypinator | 0:bb348c97df44 | 4577 | /* Write the number of bits in the header on 64 bits followed by the number |
lypinator | 0:bb348c97df44 | 4578 | of bits in the payload on 64 bits as well */ |
lypinator | 0:bb348c97df44 | 4579 | if(hcryp->Init.DataType == CRYP_DATATYPE_1B) |
lypinator | 0:bb348c97df44 | 4580 | { |
lypinator | 0:bb348c97df44 | 4581 | hcryp->Instance->DINR = __RBIT((headerlength)>>32U); |
lypinator | 0:bb348c97df44 | 4582 | hcryp->Instance->DINR = __RBIT(headerlength); |
lypinator | 0:bb348c97df44 | 4583 | hcryp->Instance->DINR = __RBIT((inputlength)>>32U); |
lypinator | 0:bb348c97df44 | 4584 | hcryp->Instance->DINR = __RBIT(inputlength); |
lypinator | 0:bb348c97df44 | 4585 | } |
lypinator | 0:bb348c97df44 | 4586 | else if(hcryp->Init.DataType == CRYP_DATATYPE_8B) |
lypinator | 0:bb348c97df44 | 4587 | { |
lypinator | 0:bb348c97df44 | 4588 | hcryp->Instance->DINR = __REV((headerlength)>>32U); |
lypinator | 0:bb348c97df44 | 4589 | hcryp->Instance->DINR = __REV(headerlength); |
lypinator | 0:bb348c97df44 | 4590 | hcryp->Instance->DINR = __REV((inputlength)>>32U); |
lypinator | 0:bb348c97df44 | 4591 | hcryp->Instance->DINR = __REV(inputlength); |
lypinator | 0:bb348c97df44 | 4592 | } |
lypinator | 0:bb348c97df44 | 4593 | else if(hcryp->Init.DataType == CRYP_DATATYPE_16B) |
lypinator | 0:bb348c97df44 | 4594 | { |
lypinator | 0:bb348c97df44 | 4595 | hcryp->Instance->DINR = __ROR((headerlength)>>32U, 16U); |
lypinator | 0:bb348c97df44 | 4596 | hcryp->Instance->DINR = __ROR(headerlength, 16U); |
lypinator | 0:bb348c97df44 | 4597 | hcryp->Instance->DINR = __ROR((inputlength)>>32U, 16U); |
lypinator | 0:bb348c97df44 | 4598 | hcryp->Instance->DINR = __ROR(inputlength, 16U); |
lypinator | 0:bb348c97df44 | 4599 | } |
lypinator | 0:bb348c97df44 | 4600 | else if(hcryp->Init.DataType == CRYP_DATATYPE_32B) |
lypinator | 0:bb348c97df44 | 4601 | { |
lypinator | 0:bb348c97df44 | 4602 | hcryp->Instance->DINR = (uint32_t)(headerlength>>32U); |
lypinator | 0:bb348c97df44 | 4603 | hcryp->Instance->DINR = (uint32_t)(headerlength); |
lypinator | 0:bb348c97df44 | 4604 | hcryp->Instance->DINR = (uint32_t)(inputlength>>32U); |
lypinator | 0:bb348c97df44 | 4605 | hcryp->Instance->DINR = (uint32_t)(inputlength); |
lypinator | 0:bb348c97df44 | 4606 | } |
lypinator | 0:bb348c97df44 | 4607 | } |
lypinator | 0:bb348c97df44 | 4608 | #if !defined(AES_CR_NPBLB) |
lypinator | 0:bb348c97df44 | 4609 | else if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) |
lypinator | 0:bb348c97df44 | 4610 | { |
lypinator | 0:bb348c97df44 | 4611 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
lypinator | 0:bb348c97df44 | 4612 | |
lypinator | 0:bb348c97df44 | 4613 | inputaddr = (uint32_t)pInputData; |
lypinator | 0:bb348c97df44 | 4614 | /* Enter the last block made of a 128-bit value formatted |
lypinator | 0:bb348c97df44 | 4615 | from the original B0 packet. */ |
lypinator | 0:bb348c97df44 | 4616 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 4617 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 4618 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 4619 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 4620 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 4621 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 4622 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 4623 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 4624 | } |
lypinator | 0:bb348c97df44 | 4625 | #endif |
lypinator | 0:bb348c97df44 | 4626 | |
lypinator | 0:bb348c97df44 | 4627 | /* No DMA transfer is required at that point therefore, the software |
lypinator | 0:bb348c97df44 | 4628 | just waits for the CCF flag to be raised. */ |
lypinator | 0:bb348c97df44 | 4629 | if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4630 | { |
lypinator | 0:bb348c97df44 | 4631 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 4632 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 4633 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 4634 | } |
lypinator | 0:bb348c97df44 | 4635 | /* Clear CCF Flag */ |
lypinator | 0:bb348c97df44 | 4636 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
lypinator | 0:bb348c97df44 | 4637 | /* Read the Auth TAG in the IN FIFO */ |
lypinator | 0:bb348c97df44 | 4638 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 4639 | tagaddr+=4U; |
lypinator | 0:bb348c97df44 | 4640 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 4641 | tagaddr+=4U; |
lypinator | 0:bb348c97df44 | 4642 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 4643 | tagaddr+=4U; |
lypinator | 0:bb348c97df44 | 4644 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 4645 | |
lypinator | 0:bb348c97df44 | 4646 | /* Mark that the final phase is over */ |
lypinator | 0:bb348c97df44 | 4647 | hcryp->Phase = HAL_CRYP_PHASE_FINAL_OVER; |
lypinator | 0:bb348c97df44 | 4648 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 4649 | /* Disable the Peripheral */ |
lypinator | 0:bb348c97df44 | 4650 | __HAL_CRYP_DISABLE(); |
lypinator | 0:bb348c97df44 | 4651 | } |
lypinator | 0:bb348c97df44 | 4652 | /*=================================================*/ |
lypinator | 0:bb348c97df44 | 4653 | /* case incorrect hcryp->Init.GCMCMACPhase setting */ |
lypinator | 0:bb348c97df44 | 4654 | /*=================================================*/ |
lypinator | 0:bb348c97df44 | 4655 | else |
lypinator | 0:bb348c97df44 | 4656 | { |
lypinator | 0:bb348c97df44 | 4657 | hcryp->State = HAL_CRYP_STATE_ERROR; |
lypinator | 0:bb348c97df44 | 4658 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 4659 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 4660 | } |
lypinator | 0:bb348c97df44 | 4661 | |
lypinator | 0:bb348c97df44 | 4662 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 4663 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 4664 | |
lypinator | 0:bb348c97df44 | 4665 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 4666 | } |
lypinator | 0:bb348c97df44 | 4667 | else |
lypinator | 0:bb348c97df44 | 4668 | { |
lypinator | 0:bb348c97df44 | 4669 | return HAL_BUSY; |
lypinator | 0:bb348c97df44 | 4670 | } |
lypinator | 0:bb348c97df44 | 4671 | } |
lypinator | 0:bb348c97df44 | 4672 | |
lypinator | 0:bb348c97df44 | 4673 | /** |
lypinator | 0:bb348c97df44 | 4674 | * @} |
lypinator | 0:bb348c97df44 | 4675 | */ |
lypinator | 0:bb348c97df44 | 4676 | |
lypinator | 0:bb348c97df44 | 4677 | /** @defgroup CRYPEx_Exported_Functions_Group3 AES suspension/resumption functions |
lypinator | 0:bb348c97df44 | 4678 | * @brief Extended processing functions. |
lypinator | 0:bb348c97df44 | 4679 | * |
lypinator | 0:bb348c97df44 | 4680 | @verbatim |
lypinator | 0:bb348c97df44 | 4681 | ============================================================================== |
lypinator | 0:bb348c97df44 | 4682 | ##### AES extended suspension and resumption functions ##### |
lypinator | 0:bb348c97df44 | 4683 | ============================================================================== |
lypinator | 0:bb348c97df44 | 4684 | [..] This section provides functions allowing to: |
lypinator | 0:bb348c97df44 | 4685 | (+) save in memory the Initialization Vector, the Key registers, the Control register or |
lypinator | 0:bb348c97df44 | 4686 | the Suspend registers when a process is suspended by a higher priority message |
lypinator | 0:bb348c97df44 | 4687 | (+) write back in CRYP hardware block the saved values listed above when the suspended |
lypinator | 0:bb348c97df44 | 4688 | lower priority message processing is resumed. |
lypinator | 0:bb348c97df44 | 4689 | |
lypinator | 0:bb348c97df44 | 4690 | @endverbatim |
lypinator | 0:bb348c97df44 | 4691 | * @{ |
lypinator | 0:bb348c97df44 | 4692 | */ |
lypinator | 0:bb348c97df44 | 4693 | |
lypinator | 0:bb348c97df44 | 4694 | |
lypinator | 0:bb348c97df44 | 4695 | /** |
lypinator | 0:bb348c97df44 | 4696 | * @brief In case of message processing suspension, read the Initialization Vector. |
lypinator | 0:bb348c97df44 | 4697 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4698 | * the configuration information for CRYP module. |
lypinator | 0:bb348c97df44 | 4699 | * @param Output Pointer to the buffer containing the saved Initialization Vector. |
lypinator | 0:bb348c97df44 | 4700 | * @note This value has to be stored for reuse by writing the AES_IVRx registers |
lypinator | 0:bb348c97df44 | 4701 | * as soon as the interrupted processing has to be resumed. |
lypinator | 0:bb348c97df44 | 4702 | * Applicable to all chaining modes. |
lypinator | 0:bb348c97df44 | 4703 | * @note AES must be disabled when reading or resetting the IV values. |
lypinator | 0:bb348c97df44 | 4704 | * @retval None |
lypinator | 0:bb348c97df44 | 4705 | */ |
lypinator | 0:bb348c97df44 | 4706 | void HAL_CRYPEx_Read_IVRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Output) |
lypinator | 0:bb348c97df44 | 4707 | { |
lypinator | 0:bb348c97df44 | 4708 | uint32_t outputaddr = (uint32_t)Output; |
lypinator | 0:bb348c97df44 | 4709 | |
lypinator | 0:bb348c97df44 | 4710 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->IVR3); |
lypinator | 0:bb348c97df44 | 4711 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 4712 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->IVR2); |
lypinator | 0:bb348c97df44 | 4713 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 4714 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->IVR1); |
lypinator | 0:bb348c97df44 | 4715 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 4716 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->IVR0); |
lypinator | 0:bb348c97df44 | 4717 | } |
lypinator | 0:bb348c97df44 | 4718 | |
lypinator | 0:bb348c97df44 | 4719 | /** |
lypinator | 0:bb348c97df44 | 4720 | * @brief In case of message processing resumption, rewrite the Initialization |
lypinator | 0:bb348c97df44 | 4721 | * Vector in the AES_IVRx registers. |
lypinator | 0:bb348c97df44 | 4722 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4723 | * the configuration information for CRYP module. |
lypinator | 0:bb348c97df44 | 4724 | * @param Input Pointer to the buffer containing the saved Initialization Vector to |
lypinator | 0:bb348c97df44 | 4725 | * write back in the CRYP hardware block. |
lypinator | 0:bb348c97df44 | 4726 | * @note Applicable to all chaining modes. |
lypinator | 0:bb348c97df44 | 4727 | * @note AES must be disabled when reading or resetting the IV values. |
lypinator | 0:bb348c97df44 | 4728 | * @retval None |
lypinator | 0:bb348c97df44 | 4729 | */ |
lypinator | 0:bb348c97df44 | 4730 | void HAL_CRYPEx_Write_IVRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Input) |
lypinator | 0:bb348c97df44 | 4731 | { |
lypinator | 0:bb348c97df44 | 4732 | uint32_t ivaddr = (uint32_t)Input; |
lypinator | 0:bb348c97df44 | 4733 | |
lypinator | 0:bb348c97df44 | 4734 | hcryp->Instance->IVR3 = __REV(*(uint32_t*)(ivaddr)); |
lypinator | 0:bb348c97df44 | 4735 | ivaddr+=4U; |
lypinator | 0:bb348c97df44 | 4736 | hcryp->Instance->IVR2 = __REV(*(uint32_t*)(ivaddr)); |
lypinator | 0:bb348c97df44 | 4737 | ivaddr+=4U; |
lypinator | 0:bb348c97df44 | 4738 | hcryp->Instance->IVR1 = __REV(*(uint32_t*)(ivaddr)); |
lypinator | 0:bb348c97df44 | 4739 | ivaddr+=4U; |
lypinator | 0:bb348c97df44 | 4740 | hcryp->Instance->IVR0 = __REV(*(uint32_t*)(ivaddr)); |
lypinator | 0:bb348c97df44 | 4741 | } |
lypinator | 0:bb348c97df44 | 4742 | |
lypinator | 0:bb348c97df44 | 4743 | |
lypinator | 0:bb348c97df44 | 4744 | /** |
lypinator | 0:bb348c97df44 | 4745 | * @brief In case of message GCM/GMAC or CMAC processing suspension, read the Suspend Registers. |
lypinator | 0:bb348c97df44 | 4746 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4747 | * the configuration information for CRYP module. |
lypinator | 0:bb348c97df44 | 4748 | * @param Output Pointer to the buffer containing the saved Suspend Registers. |
lypinator | 0:bb348c97df44 | 4749 | * @note These values have to be stored for reuse by writing back the AES_SUSPxR registers |
lypinator | 0:bb348c97df44 | 4750 | * as soon as the interrupted processing has to be resumed. |
lypinator | 0:bb348c97df44 | 4751 | * @retval None |
lypinator | 0:bb348c97df44 | 4752 | */ |
lypinator | 0:bb348c97df44 | 4753 | void HAL_CRYPEx_Read_SuspendRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Output) |
lypinator | 0:bb348c97df44 | 4754 | { |
lypinator | 0:bb348c97df44 | 4755 | uint32_t outputaddr = (uint32_t)Output; |
lypinator | 0:bb348c97df44 | 4756 | |
lypinator | 0:bb348c97df44 | 4757 | /* In case of GCM payload phase encryption, check that suspension can be carried out */ |
lypinator | 0:bb348c97df44 | 4758 | if (READ_BIT(hcryp->Instance->CR, (AES_CR_GCMPH|AES_CR_MODE)) == (CRYP_GCM_PAYLOAD_PHASE|CRYP_ALGOMODE_ENCRYPT)) |
lypinator | 0:bb348c97df44 | 4759 | { |
lypinator | 0:bb348c97df44 | 4760 | /* Ensure that Busy flag is reset */ |
lypinator | 0:bb348c97df44 | 4761 | if(CRYP_WaitOnBusyFlagReset(hcryp, CRYP_BUSY_TIMEOUTVALUE) != HAL_OK) |
lypinator | 0:bb348c97df44 | 4762 | { |
lypinator | 0:bb348c97df44 | 4763 | hcryp->ErrorCode |= HAL_CRYP_BUSY_ERROR; |
lypinator | 0:bb348c97df44 | 4764 | hcryp->State = HAL_CRYP_STATE_ERROR; |
lypinator | 0:bb348c97df44 | 4765 | |
lypinator | 0:bb348c97df44 | 4766 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 4767 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 4768 | |
lypinator | 0:bb348c97df44 | 4769 | HAL_CRYP_ErrorCallback(hcryp); |
lypinator | 0:bb348c97df44 | 4770 | return ; |
lypinator | 0:bb348c97df44 | 4771 | } |
lypinator | 0:bb348c97df44 | 4772 | } |
lypinator | 0:bb348c97df44 | 4773 | |
lypinator | 0:bb348c97df44 | 4774 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP7R); |
lypinator | 0:bb348c97df44 | 4775 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 4776 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP6R); |
lypinator | 0:bb348c97df44 | 4777 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 4778 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP5R); |
lypinator | 0:bb348c97df44 | 4779 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 4780 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP4R); |
lypinator | 0:bb348c97df44 | 4781 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 4782 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP3R); |
lypinator | 0:bb348c97df44 | 4783 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 4784 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP2R); |
lypinator | 0:bb348c97df44 | 4785 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 4786 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP1R); |
lypinator | 0:bb348c97df44 | 4787 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 4788 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP0R); |
lypinator | 0:bb348c97df44 | 4789 | } |
lypinator | 0:bb348c97df44 | 4790 | |
lypinator | 0:bb348c97df44 | 4791 | /** |
lypinator | 0:bb348c97df44 | 4792 | * @brief In case of message GCM/GMAC or CMAC processing resumption, rewrite the Suspend |
lypinator | 0:bb348c97df44 | 4793 | * Registers in the AES_SUSPxR registers. |
lypinator | 0:bb348c97df44 | 4794 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4795 | * the configuration information for CRYP module. |
lypinator | 0:bb348c97df44 | 4796 | * @param Input Pointer to the buffer containing the saved suspend registers to |
lypinator | 0:bb348c97df44 | 4797 | * write back in the CRYP hardware block. |
lypinator | 0:bb348c97df44 | 4798 | * @retval None |
lypinator | 0:bb348c97df44 | 4799 | */ |
lypinator | 0:bb348c97df44 | 4800 | void HAL_CRYPEx_Write_SuspendRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Input) |
lypinator | 0:bb348c97df44 | 4801 | { |
lypinator | 0:bb348c97df44 | 4802 | uint32_t ivaddr = (uint32_t)Input; |
lypinator | 0:bb348c97df44 | 4803 | |
lypinator | 0:bb348c97df44 | 4804 | hcryp->Instance->SUSP7R = __REV(*(uint32_t*)(ivaddr)); |
lypinator | 0:bb348c97df44 | 4805 | ivaddr+=4U; |
lypinator | 0:bb348c97df44 | 4806 | hcryp->Instance->SUSP6R = __REV(*(uint32_t*)(ivaddr)); |
lypinator | 0:bb348c97df44 | 4807 | ivaddr+=4U; |
lypinator | 0:bb348c97df44 | 4808 | hcryp->Instance->SUSP5R = __REV(*(uint32_t*)(ivaddr)); |
lypinator | 0:bb348c97df44 | 4809 | ivaddr+=4U; |
lypinator | 0:bb348c97df44 | 4810 | hcryp->Instance->SUSP4R = __REV(*(uint32_t*)(ivaddr)); |
lypinator | 0:bb348c97df44 | 4811 | ivaddr+=4U; |
lypinator | 0:bb348c97df44 | 4812 | hcryp->Instance->SUSP3R = __REV(*(uint32_t*)(ivaddr)); |
lypinator | 0:bb348c97df44 | 4813 | ivaddr+=4U; |
lypinator | 0:bb348c97df44 | 4814 | hcryp->Instance->SUSP2R = __REV(*(uint32_t*)(ivaddr)); |
lypinator | 0:bb348c97df44 | 4815 | ivaddr+=4U; |
lypinator | 0:bb348c97df44 | 4816 | hcryp->Instance->SUSP1R = __REV(*(uint32_t*)(ivaddr)); |
lypinator | 0:bb348c97df44 | 4817 | ivaddr+=4U; |
lypinator | 0:bb348c97df44 | 4818 | hcryp->Instance->SUSP0R = __REV(*(uint32_t*)(ivaddr)); |
lypinator | 0:bb348c97df44 | 4819 | } |
lypinator | 0:bb348c97df44 | 4820 | |
lypinator | 0:bb348c97df44 | 4821 | |
lypinator | 0:bb348c97df44 | 4822 | /** |
lypinator | 0:bb348c97df44 | 4823 | * @brief In case of message GCM/GMAC or CMAC processing suspension, read the Key Registers. |
lypinator | 0:bb348c97df44 | 4824 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4825 | * the configuration information for CRYP module. |
lypinator | 0:bb348c97df44 | 4826 | * @param Output Pointer to the buffer containing the saved Key Registers. |
lypinator | 0:bb348c97df44 | 4827 | * @param KeySize Indicates the key size (128 or 256 bits). |
lypinator | 0:bb348c97df44 | 4828 | * @note These values have to be stored for reuse by writing back the AES_KEYRx registers |
lypinator | 0:bb348c97df44 | 4829 | * as soon as the interrupted processing has to be resumed. |
lypinator | 0:bb348c97df44 | 4830 | * @retval None |
lypinator | 0:bb348c97df44 | 4831 | */ |
lypinator | 0:bb348c97df44 | 4832 | void HAL_CRYPEx_Read_KeyRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Output, uint32_t KeySize) |
lypinator | 0:bb348c97df44 | 4833 | { |
lypinator | 0:bb348c97df44 | 4834 | uint32_t keyaddr = (uint32_t)Output; |
lypinator | 0:bb348c97df44 | 4835 | |
lypinator | 0:bb348c97df44 | 4836 | if (KeySize == CRYP_KEYSIZE_256B) |
lypinator | 0:bb348c97df44 | 4837 | { |
lypinator | 0:bb348c97df44 | 4838 | *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR7); |
lypinator | 0:bb348c97df44 | 4839 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 4840 | *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR6); |
lypinator | 0:bb348c97df44 | 4841 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 4842 | *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR5); |
lypinator | 0:bb348c97df44 | 4843 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 4844 | *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR4); |
lypinator | 0:bb348c97df44 | 4845 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 4846 | } |
lypinator | 0:bb348c97df44 | 4847 | |
lypinator | 0:bb348c97df44 | 4848 | *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR3); |
lypinator | 0:bb348c97df44 | 4849 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 4850 | *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR2); |
lypinator | 0:bb348c97df44 | 4851 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 4852 | *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR1); |
lypinator | 0:bb348c97df44 | 4853 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 4854 | *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR0); |
lypinator | 0:bb348c97df44 | 4855 | } |
lypinator | 0:bb348c97df44 | 4856 | |
lypinator | 0:bb348c97df44 | 4857 | /** |
lypinator | 0:bb348c97df44 | 4858 | * @brief In case of message GCM/GMAC or CMAC processing resumption, rewrite the Key |
lypinator | 0:bb348c97df44 | 4859 | * Registers in the AES_KEYRx registers. |
lypinator | 0:bb348c97df44 | 4860 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4861 | * the configuration information for CRYP module. |
lypinator | 0:bb348c97df44 | 4862 | * @param Input Pointer to the buffer containing the saved key registers to |
lypinator | 0:bb348c97df44 | 4863 | * write back in the CRYP hardware block. |
lypinator | 0:bb348c97df44 | 4864 | * @param KeySize Indicates the key size (128 or 256 bits) |
lypinator | 0:bb348c97df44 | 4865 | * @retval None |
lypinator | 0:bb348c97df44 | 4866 | */ |
lypinator | 0:bb348c97df44 | 4867 | void HAL_CRYPEx_Write_KeyRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint32_t KeySize) |
lypinator | 0:bb348c97df44 | 4868 | { |
lypinator | 0:bb348c97df44 | 4869 | uint32_t keyaddr = (uint32_t)Input; |
lypinator | 0:bb348c97df44 | 4870 | |
lypinator | 0:bb348c97df44 | 4871 | if (KeySize == CRYP_KEYSIZE_256B) |
lypinator | 0:bb348c97df44 | 4872 | { |
lypinator | 0:bb348c97df44 | 4873 | hcryp->Instance->KEYR7 = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 4874 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 4875 | hcryp->Instance->KEYR6 = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 4876 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 4877 | hcryp->Instance->KEYR5 = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 4878 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 4879 | hcryp->Instance->KEYR4 = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 4880 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 4881 | } |
lypinator | 0:bb348c97df44 | 4882 | |
lypinator | 0:bb348c97df44 | 4883 | hcryp->Instance->KEYR3 = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 4884 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 4885 | hcryp->Instance->KEYR2 = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 4886 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 4887 | hcryp->Instance->KEYR1 = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 4888 | keyaddr+=4U; |
lypinator | 0:bb348c97df44 | 4889 | hcryp->Instance->KEYR0 = __REV(*(uint32_t*)(keyaddr)); |
lypinator | 0:bb348c97df44 | 4890 | } |
lypinator | 0:bb348c97df44 | 4891 | |
lypinator | 0:bb348c97df44 | 4892 | |
lypinator | 0:bb348c97df44 | 4893 | /** |
lypinator | 0:bb348c97df44 | 4894 | * @brief In case of message GCM/GMAC or CMAC processing suspension, read the Control Register. |
lypinator | 0:bb348c97df44 | 4895 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4896 | * the configuration information for CRYP module. |
lypinator | 0:bb348c97df44 | 4897 | * @param Output Pointer to the buffer containing the saved Control Register. |
lypinator | 0:bb348c97df44 | 4898 | * @note This values has to be stored for reuse by writing back the AES_CR register |
lypinator | 0:bb348c97df44 | 4899 | * as soon as the interrupted processing has to be resumed. |
lypinator | 0:bb348c97df44 | 4900 | * @retval None |
lypinator | 0:bb348c97df44 | 4901 | */ |
lypinator | 0:bb348c97df44 | 4902 | void HAL_CRYPEx_Read_ControlRegister(CRYP_HandleTypeDef *hcryp, uint8_t* Output) |
lypinator | 0:bb348c97df44 | 4903 | { |
lypinator | 0:bb348c97df44 | 4904 | *(uint32_t*)(Output) = hcryp->Instance->CR; |
lypinator | 0:bb348c97df44 | 4905 | } |
lypinator | 0:bb348c97df44 | 4906 | |
lypinator | 0:bb348c97df44 | 4907 | /** |
lypinator | 0:bb348c97df44 | 4908 | * @brief In case of message GCM/GMAC or CMAC processing resumption, rewrite the Control |
lypinator | 0:bb348c97df44 | 4909 | * Registers in the AES_CR register. |
lypinator | 0:bb348c97df44 | 4910 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4911 | * the configuration information for CRYP module. |
lypinator | 0:bb348c97df44 | 4912 | * @param Input Pointer to the buffer containing the saved Control Register to |
lypinator | 0:bb348c97df44 | 4913 | * write back in the CRYP hardware block. |
lypinator | 0:bb348c97df44 | 4914 | * @retval None |
lypinator | 0:bb348c97df44 | 4915 | */ |
lypinator | 0:bb348c97df44 | 4916 | void HAL_CRYPEx_Write_ControlRegister(CRYP_HandleTypeDef *hcryp, uint8_t* Input) |
lypinator | 0:bb348c97df44 | 4917 | { |
lypinator | 0:bb348c97df44 | 4918 | hcryp->Instance->CR = *(uint32_t*)(Input); |
lypinator | 0:bb348c97df44 | 4919 | /* At the same time, set handle state back to READY to be able to resume the AES calculations |
lypinator | 0:bb348c97df44 | 4920 | without the processing APIs returning HAL_BUSY when called. */ |
lypinator | 0:bb348c97df44 | 4921 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 4922 | } |
lypinator | 0:bb348c97df44 | 4923 | |
lypinator | 0:bb348c97df44 | 4924 | /** |
lypinator | 0:bb348c97df44 | 4925 | * @brief Request CRYP processing suspension when in polling or interruption mode. |
lypinator | 0:bb348c97df44 | 4926 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 4927 | * the configuration information for CRYP module. |
lypinator | 0:bb348c97df44 | 4928 | * @note Set the handle field SuspendRequest to the appropriate value so that |
lypinator | 0:bb348c97df44 | 4929 | * the on-going CRYP processing is suspended as soon as the required |
lypinator | 0:bb348c97df44 | 4930 | * conditions are met. |
lypinator | 0:bb348c97df44 | 4931 | * @note It is advised not to suspend the CRYP processing when the DMA controller |
lypinator | 0:bb348c97df44 | 4932 | * is managing the data transfer |
lypinator | 0:bb348c97df44 | 4933 | * @retval None |
lypinator | 0:bb348c97df44 | 4934 | */ |
lypinator | 0:bb348c97df44 | 4935 | void HAL_CRYPEx_ProcessSuspend(CRYP_HandleTypeDef *hcryp) |
lypinator | 0:bb348c97df44 | 4936 | { |
lypinator | 0:bb348c97df44 | 4937 | /* Set Handle Suspend Request field */ |
lypinator | 0:bb348c97df44 | 4938 | hcryp->SuspendRequest = HAL_CRYP_SUSPEND; |
lypinator | 0:bb348c97df44 | 4939 | } |
lypinator | 0:bb348c97df44 | 4940 | |
lypinator | 0:bb348c97df44 | 4941 | /** |
lypinator | 0:bb348c97df44 | 4942 | * @} |
lypinator | 0:bb348c97df44 | 4943 | */ |
lypinator | 0:bb348c97df44 | 4944 | |
lypinator | 0:bb348c97df44 | 4945 | /** |
lypinator | 0:bb348c97df44 | 4946 | * @} |
lypinator | 0:bb348c97df44 | 4947 | */ |
lypinator | 0:bb348c97df44 | 4948 | |
lypinator | 0:bb348c97df44 | 4949 | /** @addtogroup CRYPEx_Private_Functions |
lypinator | 0:bb348c97df44 | 4950 | * @{ |
lypinator | 0:bb348c97df44 | 4951 | */ |
lypinator | 0:bb348c97df44 | 4952 | |
lypinator | 0:bb348c97df44 | 4953 | /** |
lypinator | 0:bb348c97df44 | 4954 | * @brief DMA CRYP Input Data process complete callback |
lypinator | 0:bb348c97df44 | 4955 | * for GCM, GMAC or CMAC chainging modes. |
lypinator | 0:bb348c97df44 | 4956 | * @note Specific setting of hcryp fields are required only |
lypinator | 0:bb348c97df44 | 4957 | * in the case of header phase where no output data DMA |
lypinator | 0:bb348c97df44 | 4958 | * transfer is on-going (only input data transfer is enabled |
lypinator | 0:bb348c97df44 | 4959 | * in such a case). |
lypinator | 0:bb348c97df44 | 4960 | * @param hdma DMA handle. |
lypinator | 0:bb348c97df44 | 4961 | * @retval None |
lypinator | 0:bb348c97df44 | 4962 | */ |
lypinator | 0:bb348c97df44 | 4963 | static void CRYP_GCMCMAC_DMAInCplt(DMA_HandleTypeDef *hdma) |
lypinator | 0:bb348c97df44 | 4964 | { |
lypinator | 0:bb348c97df44 | 4965 | uint32_t difflength = 0U; |
lypinator | 0:bb348c97df44 | 4966 | |
lypinator | 0:bb348c97df44 | 4967 | CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
lypinator | 0:bb348c97df44 | 4968 | |
lypinator | 0:bb348c97df44 | 4969 | /* Disable the DMA transfer for input request */ |
lypinator | 0:bb348c97df44 | 4970 | CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAINEN); |
lypinator | 0:bb348c97df44 | 4971 | |
lypinator | 0:bb348c97df44 | 4972 | if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE) |
lypinator | 0:bb348c97df44 | 4973 | { |
lypinator | 0:bb348c97df44 | 4974 | |
lypinator | 0:bb348c97df44 | 4975 | if (hcryp->CrypInCount != 0U) |
lypinator | 0:bb348c97df44 | 4976 | { |
lypinator | 0:bb348c97df44 | 4977 | /* Last block is now entered in polling mode, no actual gain in resorting to DMA */ |
lypinator | 0:bb348c97df44 | 4978 | difflength = hcryp->CrypInCount; |
lypinator | 0:bb348c97df44 | 4979 | hcryp->CrypInCount = 0U; |
lypinator | 0:bb348c97df44 | 4980 | |
lypinator | 0:bb348c97df44 | 4981 | CRYP_Padding(hcryp, difflength, CRYP_POLLING_OFF); |
lypinator | 0:bb348c97df44 | 4982 | } |
lypinator | 0:bb348c97df44 | 4983 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 4984 | /* Mark that the header phase is over */ |
lypinator | 0:bb348c97df44 | 4985 | hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER; |
lypinator | 0:bb348c97df44 | 4986 | } |
lypinator | 0:bb348c97df44 | 4987 | /* CCF flag indicating header phase AES processing completion |
lypinator | 0:bb348c97df44 | 4988 | will be checked at the start of the next phase: |
lypinator | 0:bb348c97df44 | 4989 | - payload phase (GCM or CCM when applicable) |
lypinator | 0:bb348c97df44 | 4990 | - final phase (GMAC or CMAC). |
lypinator | 0:bb348c97df44 | 4991 | This allows to avoid the Wait on Flag within the IRQ handling. */ |
lypinator | 0:bb348c97df44 | 4992 | |
lypinator | 0:bb348c97df44 | 4993 | /* Call input data transfer complete callback */ |
lypinator | 0:bb348c97df44 | 4994 | HAL_CRYP_InCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 4995 | } |
lypinator | 0:bb348c97df44 | 4996 | |
lypinator | 0:bb348c97df44 | 4997 | /** |
lypinator | 0:bb348c97df44 | 4998 | * @brief DMA CRYP Output Data process complete callback |
lypinator | 0:bb348c97df44 | 4999 | * for GCM, GMAC or CMAC chainging modes. |
lypinator | 0:bb348c97df44 | 5000 | * @note This callback is called only in the payload phase. |
lypinator | 0:bb348c97df44 | 5001 | * @param hdma DMA handle. |
lypinator | 0:bb348c97df44 | 5002 | * @retval None |
lypinator | 0:bb348c97df44 | 5003 | */ |
lypinator | 0:bb348c97df44 | 5004 | static void CRYP_GCMCMAC_DMAOutCplt(DMA_HandleTypeDef *hdma) |
lypinator | 0:bb348c97df44 | 5005 | { |
lypinator | 0:bb348c97df44 | 5006 | uint32_t difflength = 0U; |
lypinator | 0:bb348c97df44 | 5007 | CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
lypinator | 0:bb348c97df44 | 5008 | |
lypinator | 0:bb348c97df44 | 5009 | /* Disable the DMA transfer for output request */ |
lypinator | 0:bb348c97df44 | 5010 | CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAOUTEN); |
lypinator | 0:bb348c97df44 | 5011 | |
lypinator | 0:bb348c97df44 | 5012 | /* Clear CCF Flag */ |
lypinator | 0:bb348c97df44 | 5013 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
lypinator | 0:bb348c97df44 | 5014 | |
lypinator | 0:bb348c97df44 | 5015 | /* Initiate additional transfer to wrap-up data feeding to the IP */ |
lypinator | 0:bb348c97df44 | 5016 | if (hcryp->CrypInCount != 0U) |
lypinator | 0:bb348c97df44 | 5017 | { |
lypinator | 0:bb348c97df44 | 5018 | /* Last block is now entered in polling mode, no actual gain in resorting to DMA */ |
lypinator | 0:bb348c97df44 | 5019 | difflength = hcryp->CrypInCount; |
lypinator | 0:bb348c97df44 | 5020 | hcryp->CrypInCount = 0U; |
lypinator | 0:bb348c97df44 | 5021 | |
lypinator | 0:bb348c97df44 | 5022 | CRYP_Padding(hcryp, difflength, CRYP_POLLING_ON); |
lypinator | 0:bb348c97df44 | 5023 | } |
lypinator | 0:bb348c97df44 | 5024 | |
lypinator | 0:bb348c97df44 | 5025 | /* Change the CRYP state to ready */ |
lypinator | 0:bb348c97df44 | 5026 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 5027 | /* Mark that the payload phase is over */ |
lypinator | 0:bb348c97df44 | 5028 | hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER; |
lypinator | 0:bb348c97df44 | 5029 | |
lypinator | 0:bb348c97df44 | 5030 | /* Call output data transfer complete callback */ |
lypinator | 0:bb348c97df44 | 5031 | HAL_CRYP_OutCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 5032 | } |
lypinator | 0:bb348c97df44 | 5033 | |
lypinator | 0:bb348c97df44 | 5034 | /** |
lypinator | 0:bb348c97df44 | 5035 | * @brief DMA CRYP communication error callback |
lypinator | 0:bb348c97df44 | 5036 | * for GCM, GMAC or CMAC chainging modes. |
lypinator | 0:bb348c97df44 | 5037 | * @param hdma DMA handle |
lypinator | 0:bb348c97df44 | 5038 | * @retval None |
lypinator | 0:bb348c97df44 | 5039 | */ |
lypinator | 0:bb348c97df44 | 5040 | static void CRYP_GCMCMAC_DMAError(DMA_HandleTypeDef *hdma) |
lypinator | 0:bb348c97df44 | 5041 | { |
lypinator | 0:bb348c97df44 | 5042 | CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
lypinator | 0:bb348c97df44 | 5043 | |
lypinator | 0:bb348c97df44 | 5044 | hcryp->State= HAL_CRYP_STATE_ERROR; |
lypinator | 0:bb348c97df44 | 5045 | hcryp->ErrorCode |= HAL_CRYP_DMA_ERROR; |
lypinator | 0:bb348c97df44 | 5046 | HAL_CRYP_ErrorCallback(hcryp); |
lypinator | 0:bb348c97df44 | 5047 | /* Clear Error Flag */ |
lypinator | 0:bb348c97df44 | 5048 | __HAL_CRYP_CLEAR_FLAG(CRYP_ERR_CLEAR); |
lypinator | 0:bb348c97df44 | 5049 | } |
lypinator | 0:bb348c97df44 | 5050 | |
lypinator | 0:bb348c97df44 | 5051 | /** |
lypinator | 0:bb348c97df44 | 5052 | * @brief Handle CRYP block input/output data handling under interruption |
lypinator | 0:bb348c97df44 | 5053 | * for GCM, GMAC or CMAC chaining modes. |
lypinator | 0:bb348c97df44 | 5054 | * @note The function is called under interruption only, once |
lypinator | 0:bb348c97df44 | 5055 | * interruptions have been enabled by HAL_CRYPEx_AES_Auth_IT(). |
lypinator | 0:bb348c97df44 | 5056 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 5057 | * the configuration information for CRYP module |
lypinator | 0:bb348c97df44 | 5058 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 5059 | */ |
lypinator | 0:bb348c97df44 | 5060 | HAL_StatusTypeDef CRYP_AES_Auth_IT(CRYP_HandleTypeDef *hcryp) |
lypinator | 0:bb348c97df44 | 5061 | { |
lypinator | 0:bb348c97df44 | 5062 | uint32_t inputaddr = 0x0U; |
lypinator | 0:bb348c97df44 | 5063 | uint32_t outputaddr = 0x0U; |
lypinator | 0:bb348c97df44 | 5064 | uint32_t index = 0x0U; |
lypinator | 0:bb348c97df44 | 5065 | uint32_t addhoc_process = 0U; |
lypinator | 0:bb348c97df44 | 5066 | uint32_t difflength = 0U; |
lypinator | 0:bb348c97df44 | 5067 | uint32_t difflengthmod4 = 0U; |
lypinator | 0:bb348c97df44 | 5068 | uint32_t mask[3] = {0x0FFU, 0x0FFFFU, 0x0FFFFFFU}; |
lypinator | 0:bb348c97df44 | 5069 | uint32_t intermediate_data[4U] = {0U}; |
lypinator | 0:bb348c97df44 | 5070 | |
lypinator | 0:bb348c97df44 | 5071 | if(hcryp->State == HAL_CRYP_STATE_BUSY) |
lypinator | 0:bb348c97df44 | 5072 | { |
lypinator | 0:bb348c97df44 | 5073 | /*===========================*/ |
lypinator | 0:bb348c97df44 | 5074 | /* GCM/GMAC(/CCM) init phase */ |
lypinator | 0:bb348c97df44 | 5075 | /*===========================*/ |
lypinator | 0:bb348c97df44 | 5076 | if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE) |
lypinator | 0:bb348c97df44 | 5077 | { |
lypinator | 0:bb348c97df44 | 5078 | /* Clear Computation Complete Flag */ |
lypinator | 0:bb348c97df44 | 5079 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
lypinator | 0:bb348c97df44 | 5080 | /* Disable Computation Complete Flag and Errors Interrupts */ |
lypinator | 0:bb348c97df44 | 5081 | __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); |
lypinator | 0:bb348c97df44 | 5082 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 5083 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 5084 | |
lypinator | 0:bb348c97df44 | 5085 | /* Mark that the initialization phase is over */ |
lypinator | 0:bb348c97df44 | 5086 | hcryp->Phase = HAL_CRYP_PHASE_INIT_OVER; |
lypinator | 0:bb348c97df44 | 5087 | |
lypinator | 0:bb348c97df44 | 5088 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 5089 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 5090 | /* Call computation complete callback */ |
lypinator | 0:bb348c97df44 | 5091 | HAL_CRYPEx_ComputationCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 5092 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 5093 | } |
lypinator | 0:bb348c97df44 | 5094 | /*=====================================*/ |
lypinator | 0:bb348c97df44 | 5095 | /* GCM/GMAC or (CCM/)CMAC header phase */ |
lypinator | 0:bb348c97df44 | 5096 | /*=====================================*/ |
lypinator | 0:bb348c97df44 | 5097 | else if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE) |
lypinator | 0:bb348c97df44 | 5098 | { |
lypinator | 0:bb348c97df44 | 5099 | /* Check if all input header data have been entered */ |
lypinator | 0:bb348c97df44 | 5100 | if (hcryp->CrypInCount == 0U) |
lypinator | 0:bb348c97df44 | 5101 | { |
lypinator | 0:bb348c97df44 | 5102 | /* Clear Computation Complete Flag */ |
lypinator | 0:bb348c97df44 | 5103 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
lypinator | 0:bb348c97df44 | 5104 | /* Disable Computation Complete Flag and Errors Interrupts */ |
lypinator | 0:bb348c97df44 | 5105 | __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); |
lypinator | 0:bb348c97df44 | 5106 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 5107 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 5108 | /* Mark that the header phase is over */ |
lypinator | 0:bb348c97df44 | 5109 | hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER; |
lypinator | 0:bb348c97df44 | 5110 | |
lypinator | 0:bb348c97df44 | 5111 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 5112 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 5113 | |
lypinator | 0:bb348c97df44 | 5114 | /* Call computation complete callback */ |
lypinator | 0:bb348c97df44 | 5115 | HAL_CRYPEx_ComputationCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 5116 | |
lypinator | 0:bb348c97df44 | 5117 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 5118 | } |
lypinator | 0:bb348c97df44 | 5119 | /* If suspension flag has been raised, suspend processing */ |
lypinator | 0:bb348c97df44 | 5120 | else if (hcryp->SuspendRequest == HAL_CRYP_SUSPEND) |
lypinator | 0:bb348c97df44 | 5121 | { |
lypinator | 0:bb348c97df44 | 5122 | /* Clear CCF Flag */ |
lypinator | 0:bb348c97df44 | 5123 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
lypinator | 0:bb348c97df44 | 5124 | |
lypinator | 0:bb348c97df44 | 5125 | /* reset SuspendRequest */ |
lypinator | 0:bb348c97df44 | 5126 | hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE; |
lypinator | 0:bb348c97df44 | 5127 | /* Disable Computation Complete Flag and Errors Interrupts */ |
lypinator | 0:bb348c97df44 | 5128 | __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); |
lypinator | 0:bb348c97df44 | 5129 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 5130 | hcryp->State = HAL_CRYP_STATE_SUSPENDED; |
lypinator | 0:bb348c97df44 | 5131 | /* Mark that the header phase is over */ |
lypinator | 0:bb348c97df44 | 5132 | hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED; |
lypinator | 0:bb348c97df44 | 5133 | |
lypinator | 0:bb348c97df44 | 5134 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 5135 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 5136 | |
lypinator | 0:bb348c97df44 | 5137 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 5138 | } |
lypinator | 0:bb348c97df44 | 5139 | else /* Carry on feeding input data to the CRYP hardware block */ |
lypinator | 0:bb348c97df44 | 5140 | { |
lypinator | 0:bb348c97df44 | 5141 | /* Clear Computation Complete Flag */ |
lypinator | 0:bb348c97df44 | 5142 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
lypinator | 0:bb348c97df44 | 5143 | /* Get the last Input data address */ |
lypinator | 0:bb348c97df44 | 5144 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
lypinator | 0:bb348c97df44 | 5145 | |
lypinator | 0:bb348c97df44 | 5146 | /* Increment/decrement instance pointer/counter */ |
lypinator | 0:bb348c97df44 | 5147 | if (hcryp->CrypInCount < 16U) |
lypinator | 0:bb348c97df44 | 5148 | { |
lypinator | 0:bb348c97df44 | 5149 | difflength = hcryp->CrypInCount; |
lypinator | 0:bb348c97df44 | 5150 | hcryp->CrypInCount = 0U; |
lypinator | 0:bb348c97df44 | 5151 | addhoc_process = 1U; |
lypinator | 0:bb348c97df44 | 5152 | difflengthmod4 = difflength%4U; |
lypinator | 0:bb348c97df44 | 5153 | } |
lypinator | 0:bb348c97df44 | 5154 | else |
lypinator | 0:bb348c97df44 | 5155 | { |
lypinator | 0:bb348c97df44 | 5156 | hcryp->pCrypInBuffPtr += 16U; |
lypinator | 0:bb348c97df44 | 5157 | hcryp->CrypInCount -= 16U; |
lypinator | 0:bb348c97df44 | 5158 | } |
lypinator | 0:bb348c97df44 | 5159 | |
lypinator | 0:bb348c97df44 | 5160 | #if defined(AES_CR_NPBLB) |
lypinator | 0:bb348c97df44 | 5161 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC) |
lypinator | 0:bb348c97df44 | 5162 | #else |
lypinator | 0:bb348c97df44 | 5163 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) |
lypinator | 0:bb348c97df44 | 5164 | #endif |
lypinator | 0:bb348c97df44 | 5165 | { |
lypinator | 0:bb348c97df44 | 5166 | if (hcryp->CrypInCount == hcryp->Init.HeaderSize) |
lypinator | 0:bb348c97df44 | 5167 | { |
lypinator | 0:bb348c97df44 | 5168 | /* All B blocks will have been entered after the next |
lypinator | 0:bb348c97df44 | 5169 | four DINR writing, so point at header buffer for |
lypinator | 0:bb348c97df44 | 5170 | the next iteration */ |
lypinator | 0:bb348c97df44 | 5171 | hcryp->pCrypInBuffPtr = hcryp->Init.Header; |
lypinator | 0:bb348c97df44 | 5172 | } |
lypinator | 0:bb348c97df44 | 5173 | } |
lypinator | 0:bb348c97df44 | 5174 | |
lypinator | 0:bb348c97df44 | 5175 | /* Write the Input block in the Data Input register */ |
lypinator | 0:bb348c97df44 | 5176 | if (addhoc_process == 0U) |
lypinator | 0:bb348c97df44 | 5177 | { |
lypinator | 0:bb348c97df44 | 5178 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 5179 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5180 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 5181 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5182 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 5183 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5184 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 5185 | } |
lypinator | 0:bb348c97df44 | 5186 | else |
lypinator | 0:bb348c97df44 | 5187 | { |
lypinator | 0:bb348c97df44 | 5188 | /* Header remainder has size less than 128 bits */ |
lypinator | 0:bb348c97df44 | 5189 | /* Enter complete words when possible */ |
lypinator | 0:bb348c97df44 | 5190 | for(index=0U; index < (difflength/4U); index ++) |
lypinator | 0:bb348c97df44 | 5191 | { |
lypinator | 0:bb348c97df44 | 5192 | /* Write the Input block in the Data Input register */ |
lypinator | 0:bb348c97df44 | 5193 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 5194 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5195 | } |
lypinator | 0:bb348c97df44 | 5196 | /* Enter incomplete word padded with zeroes if applicable |
lypinator | 0:bb348c97df44 | 5197 | (case of header length not a multiple of 32-bits) */ |
lypinator | 0:bb348c97df44 | 5198 | if (difflengthmod4 != 0U) |
lypinator | 0:bb348c97df44 | 5199 | { |
lypinator | 0:bb348c97df44 | 5200 | hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[difflengthmod4-1]); |
lypinator | 0:bb348c97df44 | 5201 | } |
lypinator | 0:bb348c97df44 | 5202 | /* Pad with zero-words to reach 128-bit long block and wrap-up header feeding to the IP */ |
lypinator | 0:bb348c97df44 | 5203 | for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++) |
lypinator | 0:bb348c97df44 | 5204 | { |
lypinator | 0:bb348c97df44 | 5205 | hcryp->Instance->DINR = 0U; |
lypinator | 0:bb348c97df44 | 5206 | } |
lypinator | 0:bb348c97df44 | 5207 | } |
lypinator | 0:bb348c97df44 | 5208 | |
lypinator | 0:bb348c97df44 | 5209 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 5210 | } |
lypinator | 0:bb348c97df44 | 5211 | } |
lypinator | 0:bb348c97df44 | 5212 | /*=======================*/ |
lypinator | 0:bb348c97df44 | 5213 | /* GCM/CCM payload phase */ |
lypinator | 0:bb348c97df44 | 5214 | /*=======================*/ |
lypinator | 0:bb348c97df44 | 5215 | else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE) |
lypinator | 0:bb348c97df44 | 5216 | { |
lypinator | 0:bb348c97df44 | 5217 | /* Get the last output data address */ |
lypinator | 0:bb348c97df44 | 5218 | outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; |
lypinator | 0:bb348c97df44 | 5219 | |
lypinator | 0:bb348c97df44 | 5220 | /* Specific handling to manage payload size less than 128 bits |
lypinator | 0:bb348c97df44 | 5221 | when GCM (or CCM when applicable) encryption or decryption is selected. |
lypinator | 0:bb348c97df44 | 5222 | Check here if the last block output data are read */ |
lypinator | 0:bb348c97df44 | 5223 | #if defined(AES_CR_NPBLB) |
lypinator | 0:bb348c97df44 | 5224 | if ((hcryp->CrypOutCount < 16U) && \ |
lypinator | 0:bb348c97df44 | 5225 | (hcryp->CrypOutCount > 0U)) |
lypinator | 0:bb348c97df44 | 5226 | #else |
lypinator | 0:bb348c97df44 | 5227 | if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) && \ |
lypinator | 0:bb348c97df44 | 5228 | (hcryp->CrypOutCount < 16U) && \ |
lypinator | 0:bb348c97df44 | 5229 | (hcryp->CrypOutCount > 0U)) |
lypinator | 0:bb348c97df44 | 5230 | #endif |
lypinator | 0:bb348c97df44 | 5231 | { |
lypinator | 0:bb348c97df44 | 5232 | addhoc_process = 1U; |
lypinator | 0:bb348c97df44 | 5233 | difflength = hcryp->CrypOutCount; |
lypinator | 0:bb348c97df44 | 5234 | difflengthmod4 = difflength%4U; |
lypinator | 0:bb348c97df44 | 5235 | hcryp->CrypOutCount = 0U; /* mark that no more output data will be needed */ |
lypinator | 0:bb348c97df44 | 5236 | /* Retrieve intermediate data */ |
lypinator | 0:bb348c97df44 | 5237 | for(index=0U; index < 4U; index ++) |
lypinator | 0:bb348c97df44 | 5238 | { |
lypinator | 0:bb348c97df44 | 5239 | intermediate_data[index] = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 5240 | } |
lypinator | 0:bb348c97df44 | 5241 | /* Retrieve last words of cyphered data */ |
lypinator | 0:bb348c97df44 | 5242 | /* First, retrieve complete output words */ |
lypinator | 0:bb348c97df44 | 5243 | for(index=0U; index < (difflength/4U); index ++) |
lypinator | 0:bb348c97df44 | 5244 | { |
lypinator | 0:bb348c97df44 | 5245 | *(uint32_t*)(outputaddr) = intermediate_data[index]; |
lypinator | 0:bb348c97df44 | 5246 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5247 | } |
lypinator | 0:bb348c97df44 | 5248 | /* Next, retrieve partial output word if applicable; |
lypinator | 0:bb348c97df44 | 5249 | at the same time, start masking intermediate data |
lypinator | 0:bb348c97df44 | 5250 | with a mask of zeros of same size than the padding |
lypinator | 0:bb348c97df44 | 5251 | applied to the last block of payload */ |
lypinator | 0:bb348c97df44 | 5252 | if (difflengthmod4 != 0U) |
lypinator | 0:bb348c97df44 | 5253 | { |
lypinator | 0:bb348c97df44 | 5254 | intermediate_data[difflength/4U] &= mask[difflengthmod4-1U]; |
lypinator | 0:bb348c97df44 | 5255 | *(uint32_t*)(outputaddr) = intermediate_data[difflength/4U]; |
lypinator | 0:bb348c97df44 | 5256 | } |
lypinator | 0:bb348c97df44 | 5257 | |
lypinator | 0:bb348c97df44 | 5258 | #if !defined(AES_CR_NPBLB) |
lypinator | 0:bb348c97df44 | 5259 | if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT) |
lypinator | 0:bb348c97df44 | 5260 | { |
lypinator | 0:bb348c97df44 | 5261 | /* Change again CHMOD configuration to GCM mode */ |
lypinator | 0:bb348c97df44 | 5262 | __HAL_CRYP_SET_CHAININGMODE(CRYP_CHAINMODE_AES_GCM_GMAC); |
lypinator | 0:bb348c97df44 | 5263 | |
lypinator | 0:bb348c97df44 | 5264 | /* Select FINAL phase */ |
lypinator | 0:bb348c97df44 | 5265 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_GCMCMAC_FINAL_PHASE); |
lypinator | 0:bb348c97df44 | 5266 | |
lypinator | 0:bb348c97df44 | 5267 | /* Before inserting the intermediate data, carry on masking operation |
lypinator | 0:bb348c97df44 | 5268 | with a mask of zeros of same size than the padding applied to the last block of payload */ |
lypinator | 0:bb348c97df44 | 5269 | for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++) |
lypinator | 0:bb348c97df44 | 5270 | { |
lypinator | 0:bb348c97df44 | 5271 | intermediate_data[(difflength+3U)/4U+index] = 0U; |
lypinator | 0:bb348c97df44 | 5272 | } |
lypinator | 0:bb348c97df44 | 5273 | |
lypinator | 0:bb348c97df44 | 5274 | /* Insert intermediate data to trigger an additional DOUTR reading round */ |
lypinator | 0:bb348c97df44 | 5275 | /* Clear Computation Complete Flag before entering new block */ |
lypinator | 0:bb348c97df44 | 5276 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
lypinator | 0:bb348c97df44 | 5277 | for(index=0U; index < 4U; index ++) |
lypinator | 0:bb348c97df44 | 5278 | { |
lypinator | 0:bb348c97df44 | 5279 | hcryp->Instance->DINR = intermediate_data[index]; |
lypinator | 0:bb348c97df44 | 5280 | } |
lypinator | 0:bb348c97df44 | 5281 | } |
lypinator | 0:bb348c97df44 | 5282 | else |
lypinator | 0:bb348c97df44 | 5283 | #endif |
lypinator | 0:bb348c97df44 | 5284 | { |
lypinator | 0:bb348c97df44 | 5285 | /* Payload phase is now over */ |
lypinator | 0:bb348c97df44 | 5286 | /* Clear Computation Complete Flag */ |
lypinator | 0:bb348c97df44 | 5287 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
lypinator | 0:bb348c97df44 | 5288 | /* Disable Computation Complete Flag and Errors Interrupts */ |
lypinator | 0:bb348c97df44 | 5289 | __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); |
lypinator | 0:bb348c97df44 | 5290 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 5291 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 5292 | /* Mark that the payload phase is over */ |
lypinator | 0:bb348c97df44 | 5293 | hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER; |
lypinator | 0:bb348c97df44 | 5294 | |
lypinator | 0:bb348c97df44 | 5295 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 5296 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 5297 | |
lypinator | 0:bb348c97df44 | 5298 | /* Call computation complete callback */ |
lypinator | 0:bb348c97df44 | 5299 | HAL_CRYPEx_ComputationCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 5300 | } |
lypinator | 0:bb348c97df44 | 5301 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 5302 | } |
lypinator | 0:bb348c97df44 | 5303 | else |
lypinator | 0:bb348c97df44 | 5304 | { |
lypinator | 0:bb348c97df44 | 5305 | if (hcryp->CrypOutCount != 0U) |
lypinator | 0:bb348c97df44 | 5306 | { |
lypinator | 0:bb348c97df44 | 5307 | /* Usual case (different than GCM/CCM last block < 128 bits ciphering) */ |
lypinator | 0:bb348c97df44 | 5308 | /* Retrieve the last block available from the CRYP hardware block: |
lypinator | 0:bb348c97df44 | 5309 | read the output block from the Data Output Register */ |
lypinator | 0:bb348c97df44 | 5310 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 5311 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5312 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 5313 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5314 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 5315 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5316 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 5317 | |
lypinator | 0:bb348c97df44 | 5318 | /* Increment/decrement instance pointer/counter */ |
lypinator | 0:bb348c97df44 | 5319 | hcryp->pCrypOutBuffPtr += 16U; |
lypinator | 0:bb348c97df44 | 5320 | hcryp->CrypOutCount -= 16U; |
lypinator | 0:bb348c97df44 | 5321 | } |
lypinator | 0:bb348c97df44 | 5322 | #if !defined(AES_CR_NPBLB) |
lypinator | 0:bb348c97df44 | 5323 | else |
lypinator | 0:bb348c97df44 | 5324 | { |
lypinator | 0:bb348c97df44 | 5325 | /* Software work-around: additional DOUTR reading round to discard the data */ |
lypinator | 0:bb348c97df44 | 5326 | for(index=0U; index < 4U; index ++) |
lypinator | 0:bb348c97df44 | 5327 | { |
lypinator | 0:bb348c97df44 | 5328 | intermediate_data[index] = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 5329 | } |
lypinator | 0:bb348c97df44 | 5330 | } |
lypinator | 0:bb348c97df44 | 5331 | #endif |
lypinator | 0:bb348c97df44 | 5332 | } |
lypinator | 0:bb348c97df44 | 5333 | |
lypinator | 0:bb348c97df44 | 5334 | /* Check if all output text has been retrieved */ |
lypinator | 0:bb348c97df44 | 5335 | if (hcryp->CrypOutCount == 0U) |
lypinator | 0:bb348c97df44 | 5336 | { |
lypinator | 0:bb348c97df44 | 5337 | #if !defined(AES_CR_NPBLB) |
lypinator | 0:bb348c97df44 | 5338 | /* Make sure that software-work around is not running before disabling |
lypinator | 0:bb348c97df44 | 5339 | the interruptions (indeed, if software work-around is running, the |
lypinator | 0:bb348c97df44 | 5340 | interruptions must not be disabled to allow the additional DOUTR |
lypinator | 0:bb348c97df44 | 5341 | reading round */ |
lypinator | 0:bb348c97df44 | 5342 | if (addhoc_process == 0U) |
lypinator | 0:bb348c97df44 | 5343 | #endif |
lypinator | 0:bb348c97df44 | 5344 | { |
lypinator | 0:bb348c97df44 | 5345 | /* Clear Computation Complete Flag */ |
lypinator | 0:bb348c97df44 | 5346 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
lypinator | 0:bb348c97df44 | 5347 | /* Disable Computation Complete Flag and Errors Interrupts */ |
lypinator | 0:bb348c97df44 | 5348 | __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); |
lypinator | 0:bb348c97df44 | 5349 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 5350 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 5351 | /* Mark that the payload phase is over */ |
lypinator | 0:bb348c97df44 | 5352 | hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER; |
lypinator | 0:bb348c97df44 | 5353 | |
lypinator | 0:bb348c97df44 | 5354 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 5355 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 5356 | |
lypinator | 0:bb348c97df44 | 5357 | /* Call computation complete callback */ |
lypinator | 0:bb348c97df44 | 5358 | HAL_CRYPEx_ComputationCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 5359 | } |
lypinator | 0:bb348c97df44 | 5360 | |
lypinator | 0:bb348c97df44 | 5361 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 5362 | } |
lypinator | 0:bb348c97df44 | 5363 | /* If suspension flag has been raised, suspend processing */ |
lypinator | 0:bb348c97df44 | 5364 | else if (hcryp->SuspendRequest == HAL_CRYP_SUSPEND) |
lypinator | 0:bb348c97df44 | 5365 | { |
lypinator | 0:bb348c97df44 | 5366 | /* Clear CCF Flag */ |
lypinator | 0:bb348c97df44 | 5367 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
lypinator | 0:bb348c97df44 | 5368 | |
lypinator | 0:bb348c97df44 | 5369 | /* reset SuspendRequest */ |
lypinator | 0:bb348c97df44 | 5370 | hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE; |
lypinator | 0:bb348c97df44 | 5371 | /* Disable Computation Complete Flag and Errors Interrupts */ |
lypinator | 0:bb348c97df44 | 5372 | __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); |
lypinator | 0:bb348c97df44 | 5373 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 5374 | hcryp->State = HAL_CRYP_STATE_SUSPENDED; |
lypinator | 0:bb348c97df44 | 5375 | /* Mark that the header phase is over */ |
lypinator | 0:bb348c97df44 | 5376 | hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED; |
lypinator | 0:bb348c97df44 | 5377 | |
lypinator | 0:bb348c97df44 | 5378 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 5379 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 5380 | |
lypinator | 0:bb348c97df44 | 5381 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 5382 | } |
lypinator | 0:bb348c97df44 | 5383 | else /* Output data are still expected, carry on feeding the CRYP |
lypinator | 0:bb348c97df44 | 5384 | hardware block with input data */ |
lypinator | 0:bb348c97df44 | 5385 | { |
lypinator | 0:bb348c97df44 | 5386 | /* Clear Computation Complete Flag */ |
lypinator | 0:bb348c97df44 | 5387 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
lypinator | 0:bb348c97df44 | 5388 | /* Get the last Input data address */ |
lypinator | 0:bb348c97df44 | 5389 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
lypinator | 0:bb348c97df44 | 5390 | |
lypinator | 0:bb348c97df44 | 5391 | /* Usual input data feeding case */ |
lypinator | 0:bb348c97df44 | 5392 | if (hcryp->CrypInCount < 16U) |
lypinator | 0:bb348c97df44 | 5393 | { |
lypinator | 0:bb348c97df44 | 5394 | difflength = (uint32_t) (hcryp->CrypInCount); |
lypinator | 0:bb348c97df44 | 5395 | difflengthmod4 = difflength%4U; |
lypinator | 0:bb348c97df44 | 5396 | hcryp->CrypInCount = 0U; |
lypinator | 0:bb348c97df44 | 5397 | |
lypinator | 0:bb348c97df44 | 5398 | #if defined(AES_CR_NPBLB) |
lypinator | 0:bb348c97df44 | 5399 | /* In case of GCM encryption or CCM decryption, specify the number of padding |
lypinator | 0:bb348c97df44 | 5400 | bytes in last block of payload */ |
lypinator | 0:bb348c97df44 | 5401 | if (((READ_BIT(hcryp->Instance->CR, AES_CR_CHMOD) == CRYP_CHAINMODE_AES_GCM_GMAC) |
lypinator | 0:bb348c97df44 | 5402 | && (READ_BIT(hcryp->Instance->CR, AES_CR_MODE) == CRYP_ALGOMODE_ENCRYPT)) |
lypinator | 0:bb348c97df44 | 5403 | || ((READ_BIT(hcryp->Instance->CR, AES_CR_CHMOD) == CRYP_CHAINMODE_AES_CCM_CMAC) |
lypinator | 0:bb348c97df44 | 5404 | && (READ_BIT(hcryp->Instance->CR, AES_CR_MODE) == CRYP_ALGOMODE_DECRYPT))) |
lypinator | 0:bb348c97df44 | 5405 | { |
lypinator | 0:bb348c97df44 | 5406 | /* Set NPBLB field in writing the number of padding bytes |
lypinator | 0:bb348c97df44 | 5407 | for the last block of payload */ |
lypinator | 0:bb348c97df44 | 5408 | MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, 16U - difflength); |
lypinator | 0:bb348c97df44 | 5409 | } |
lypinator | 0:bb348c97df44 | 5410 | #else |
lypinator | 0:bb348c97df44 | 5411 | /* Software workaround applied to GCM encryption only */ |
lypinator | 0:bb348c97df44 | 5412 | if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT) |
lypinator | 0:bb348c97df44 | 5413 | { |
lypinator | 0:bb348c97df44 | 5414 | /* Change the mode configured in CHMOD bits of CR register to select CTR mode */ |
lypinator | 0:bb348c97df44 | 5415 | __HAL_CRYP_SET_CHAININGMODE(CRYP_CHAINMODE_AES_CTR); |
lypinator | 0:bb348c97df44 | 5416 | } |
lypinator | 0:bb348c97df44 | 5417 | #endif |
lypinator | 0:bb348c97df44 | 5418 | |
lypinator | 0:bb348c97df44 | 5419 | /* Insert the last block (which size is inferior to 128 bits) padded with zeroes |
lypinator | 0:bb348c97df44 | 5420 | to have a complete block of 128 bits */ |
lypinator | 0:bb348c97df44 | 5421 | for(index=0U; index < (difflength/4U); index ++) |
lypinator | 0:bb348c97df44 | 5422 | { |
lypinator | 0:bb348c97df44 | 5423 | /* Write the Input block in the Data Input register */ |
lypinator | 0:bb348c97df44 | 5424 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 5425 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5426 | } |
lypinator | 0:bb348c97df44 | 5427 | /* If required, manage input data size not multiple of 32 bits */ |
lypinator | 0:bb348c97df44 | 5428 | if (difflengthmod4 != 0U) |
lypinator | 0:bb348c97df44 | 5429 | { |
lypinator | 0:bb348c97df44 | 5430 | hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[difflengthmod4-1U]); |
lypinator | 0:bb348c97df44 | 5431 | } |
lypinator | 0:bb348c97df44 | 5432 | /* Wrap-up in padding with zero-words if applicable */ |
lypinator | 0:bb348c97df44 | 5433 | for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++) |
lypinator | 0:bb348c97df44 | 5434 | { |
lypinator | 0:bb348c97df44 | 5435 | hcryp->Instance->DINR = 0U; |
lypinator | 0:bb348c97df44 | 5436 | } |
lypinator | 0:bb348c97df44 | 5437 | } |
lypinator | 0:bb348c97df44 | 5438 | else |
lypinator | 0:bb348c97df44 | 5439 | { |
lypinator | 0:bb348c97df44 | 5440 | hcryp->pCrypInBuffPtr += 16U; |
lypinator | 0:bb348c97df44 | 5441 | hcryp->CrypInCount -= 16U; |
lypinator | 0:bb348c97df44 | 5442 | |
lypinator | 0:bb348c97df44 | 5443 | /* Write the Input block in the Data Input register */ |
lypinator | 0:bb348c97df44 | 5444 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 5445 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5446 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 5447 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5448 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 5449 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5450 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 5451 | } |
lypinator | 0:bb348c97df44 | 5452 | |
lypinator | 0:bb348c97df44 | 5453 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 5454 | } |
lypinator | 0:bb348c97df44 | 5455 | } |
lypinator | 0:bb348c97df44 | 5456 | /*====================================*/ |
lypinator | 0:bb348c97df44 | 5457 | /* GCM/GMAC or (CCM/)CMAC final phase */ |
lypinator | 0:bb348c97df44 | 5458 | /*====================================*/ |
lypinator | 0:bb348c97df44 | 5459 | else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE) |
lypinator | 0:bb348c97df44 | 5460 | { |
lypinator | 0:bb348c97df44 | 5461 | /* Clear Computation Complete Flag */ |
lypinator | 0:bb348c97df44 | 5462 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
lypinator | 0:bb348c97df44 | 5463 | |
lypinator | 0:bb348c97df44 | 5464 | /* Get the last output data address */ |
lypinator | 0:bb348c97df44 | 5465 | outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; |
lypinator | 0:bb348c97df44 | 5466 | |
lypinator | 0:bb348c97df44 | 5467 | /* Retrieve the last expected data from the CRYP hardware block: |
lypinator | 0:bb348c97df44 | 5468 | read the output block from the Data Output Register */ |
lypinator | 0:bb348c97df44 | 5469 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 5470 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5471 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 5472 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5473 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 5474 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5475 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 5476 | |
lypinator | 0:bb348c97df44 | 5477 | /* Disable Computation Complete Flag and Errors Interrupts */ |
lypinator | 0:bb348c97df44 | 5478 | __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); |
lypinator | 0:bb348c97df44 | 5479 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 5480 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 5481 | /* Mark that the header phase is over */ |
lypinator | 0:bb348c97df44 | 5482 | hcryp->Phase = HAL_CRYP_PHASE_FINAL_OVER; |
lypinator | 0:bb348c97df44 | 5483 | |
lypinator | 0:bb348c97df44 | 5484 | /* Disable the Peripheral */ |
lypinator | 0:bb348c97df44 | 5485 | __HAL_CRYP_DISABLE(); |
lypinator | 0:bb348c97df44 | 5486 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 5487 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 5488 | |
lypinator | 0:bb348c97df44 | 5489 | /* Call computation complete callback */ |
lypinator | 0:bb348c97df44 | 5490 | HAL_CRYPEx_ComputationCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 5491 | |
lypinator | 0:bb348c97df44 | 5492 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 5493 | } |
lypinator | 0:bb348c97df44 | 5494 | else |
lypinator | 0:bb348c97df44 | 5495 | { |
lypinator | 0:bb348c97df44 | 5496 | /* Clear Computation Complete Flag */ |
lypinator | 0:bb348c97df44 | 5497 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
lypinator | 0:bb348c97df44 | 5498 | hcryp->State = HAL_CRYP_STATE_ERROR; |
lypinator | 0:bb348c97df44 | 5499 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 5500 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 5501 | } |
lypinator | 0:bb348c97df44 | 5502 | } |
lypinator | 0:bb348c97df44 | 5503 | else |
lypinator | 0:bb348c97df44 | 5504 | { |
lypinator | 0:bb348c97df44 | 5505 | return HAL_BUSY; |
lypinator | 0:bb348c97df44 | 5506 | } |
lypinator | 0:bb348c97df44 | 5507 | } |
lypinator | 0:bb348c97df44 | 5508 | |
lypinator | 0:bb348c97df44 | 5509 | /** |
lypinator | 0:bb348c97df44 | 5510 | * @brief Set the DMA configuration and start the DMA transfer |
lypinator | 0:bb348c97df44 | 5511 | * for GCM, GMAC or CMAC chainging modes. |
lypinator | 0:bb348c97df44 | 5512 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 5513 | * the configuration information for CRYP module. |
lypinator | 0:bb348c97df44 | 5514 | * @param inputaddr Address of the Input buffer. |
lypinator | 0:bb348c97df44 | 5515 | * @param Size Size of the Input buffer un bytes, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 5516 | * @param outputaddr Address of the Output buffer, null pointer when no output DMA stream |
lypinator | 0:bb348c97df44 | 5517 | * has to be configured. |
lypinator | 0:bb348c97df44 | 5518 | * @retval None |
lypinator | 0:bb348c97df44 | 5519 | */ |
lypinator | 0:bb348c97df44 | 5520 | static void CRYP_GCMCMAC_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr) |
lypinator | 0:bb348c97df44 | 5521 | { |
lypinator | 0:bb348c97df44 | 5522 | |
lypinator | 0:bb348c97df44 | 5523 | /* Set the input CRYP DMA transfer complete callback */ |
lypinator | 0:bb348c97df44 | 5524 | hcryp->hdmain->XferCpltCallback = CRYP_GCMCMAC_DMAInCplt; |
lypinator | 0:bb348c97df44 | 5525 | /* Set the DMA error callback */ |
lypinator | 0:bb348c97df44 | 5526 | hcryp->hdmain->XferErrorCallback = CRYP_GCMCMAC_DMAError; |
lypinator | 0:bb348c97df44 | 5527 | |
lypinator | 0:bb348c97df44 | 5528 | if (outputaddr != 0U) |
lypinator | 0:bb348c97df44 | 5529 | { |
lypinator | 0:bb348c97df44 | 5530 | /* Set the output CRYP DMA transfer complete callback */ |
lypinator | 0:bb348c97df44 | 5531 | hcryp->hdmaout->XferCpltCallback = CRYP_GCMCMAC_DMAOutCplt; |
lypinator | 0:bb348c97df44 | 5532 | /* Set the DMA error callback */ |
lypinator | 0:bb348c97df44 | 5533 | hcryp->hdmaout->XferErrorCallback = CRYP_GCMCMAC_DMAError; |
lypinator | 0:bb348c97df44 | 5534 | } |
lypinator | 0:bb348c97df44 | 5535 | |
lypinator | 0:bb348c97df44 | 5536 | /* Enable the CRYP peripheral */ |
lypinator | 0:bb348c97df44 | 5537 | __HAL_CRYP_ENABLE(); |
lypinator | 0:bb348c97df44 | 5538 | |
lypinator | 0:bb348c97df44 | 5539 | /* Enable the DMA input stream */ |
lypinator | 0:bb348c97df44 | 5540 | HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DINR, Size/4U); |
lypinator | 0:bb348c97df44 | 5541 | |
lypinator | 0:bb348c97df44 | 5542 | /* Enable the DMA input request */ |
lypinator | 0:bb348c97df44 | 5543 | SET_BIT(hcryp->Instance->CR, AES_CR_DMAINEN); |
lypinator | 0:bb348c97df44 | 5544 | |
lypinator | 0:bb348c97df44 | 5545 | |
lypinator | 0:bb348c97df44 | 5546 | if (outputaddr != 0U) |
lypinator | 0:bb348c97df44 | 5547 | { |
lypinator | 0:bb348c97df44 | 5548 | /* Enable the DMA output stream */ |
lypinator | 0:bb348c97df44 | 5549 | HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUTR, outputaddr, Size/4U); |
lypinator | 0:bb348c97df44 | 5550 | |
lypinator | 0:bb348c97df44 | 5551 | /* Enable the DMA output request */ |
lypinator | 0:bb348c97df44 | 5552 | SET_BIT(hcryp->Instance->CR, AES_CR_DMAOUTEN); |
lypinator | 0:bb348c97df44 | 5553 | } |
lypinator | 0:bb348c97df44 | 5554 | } |
lypinator | 0:bb348c97df44 | 5555 | |
lypinator | 0:bb348c97df44 | 5556 | /** |
lypinator | 0:bb348c97df44 | 5557 | * @brief Write/read input/output data in polling mode. |
lypinator | 0:bb348c97df44 | 5558 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 5559 | * the configuration information for CRYP module. |
lypinator | 0:bb348c97df44 | 5560 | * @param Input Pointer to the Input buffer. |
lypinator | 0:bb348c97df44 | 5561 | * @param Ilength Length of the Input buffer in bytes, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 5562 | * @param Output Pointer to the returned buffer. |
lypinator | 0:bb348c97df44 | 5563 | * @param Timeout Specify Timeout value. |
lypinator | 0:bb348c97df44 | 5564 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 5565 | */ |
lypinator | 0:bb348c97df44 | 5566 | static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 5567 | { |
lypinator | 0:bb348c97df44 | 5568 | uint32_t index = 0U; |
lypinator | 0:bb348c97df44 | 5569 | uint32_t inputaddr = (uint32_t)Input; |
lypinator | 0:bb348c97df44 | 5570 | uint32_t outputaddr = (uint32_t)Output; |
lypinator | 0:bb348c97df44 | 5571 | |
lypinator | 0:bb348c97df44 | 5572 | |
lypinator | 0:bb348c97df44 | 5573 | for(index=0U; (index < Ilength); index += 16U) |
lypinator | 0:bb348c97df44 | 5574 | { |
lypinator | 0:bb348c97df44 | 5575 | /* Write the Input block in the Data Input register */ |
lypinator | 0:bb348c97df44 | 5576 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 5577 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5578 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 5579 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5580 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 5581 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5582 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 5583 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5584 | |
lypinator | 0:bb348c97df44 | 5585 | /* Wait for CCF flag to be raised */ |
lypinator | 0:bb348c97df44 | 5586 | if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 5587 | { |
lypinator | 0:bb348c97df44 | 5588 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 5589 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 5590 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 5591 | } |
lypinator | 0:bb348c97df44 | 5592 | |
lypinator | 0:bb348c97df44 | 5593 | /* Clear CCF Flag */ |
lypinator | 0:bb348c97df44 | 5594 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
lypinator | 0:bb348c97df44 | 5595 | |
lypinator | 0:bb348c97df44 | 5596 | /* Read the Output block from the Data Output Register */ |
lypinator | 0:bb348c97df44 | 5597 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 5598 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5599 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 5600 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5601 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 5602 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5603 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 5604 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5605 | |
lypinator | 0:bb348c97df44 | 5606 | /* If the suspension flag has been raised and if the processing is not about |
lypinator | 0:bb348c97df44 | 5607 | to end, suspend processing */ |
lypinator | 0:bb348c97df44 | 5608 | if ((hcryp->SuspendRequest == HAL_CRYP_SUSPEND) && ((index+16U) < Ilength)) |
lypinator | 0:bb348c97df44 | 5609 | { |
lypinator | 0:bb348c97df44 | 5610 | /* Reset SuspendRequest */ |
lypinator | 0:bb348c97df44 | 5611 | hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE; |
lypinator | 0:bb348c97df44 | 5612 | |
lypinator | 0:bb348c97df44 | 5613 | /* Save current reading and writing locations of Input and Output buffers */ |
lypinator | 0:bb348c97df44 | 5614 | hcryp->pCrypOutBuffPtr = (uint8_t *)outputaddr; |
lypinator | 0:bb348c97df44 | 5615 | hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr; |
lypinator | 0:bb348c97df44 | 5616 | /* Save the number of bytes that remain to be processed at this point */ |
lypinator | 0:bb348c97df44 | 5617 | hcryp->CrypInCount = Ilength - (index+16U); |
lypinator | 0:bb348c97df44 | 5618 | |
lypinator | 0:bb348c97df44 | 5619 | /* Change the CRYP state */ |
lypinator | 0:bb348c97df44 | 5620 | hcryp->State = HAL_CRYP_STATE_SUSPENDED; |
lypinator | 0:bb348c97df44 | 5621 | |
lypinator | 0:bb348c97df44 | 5622 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 5623 | } |
lypinator | 0:bb348c97df44 | 5624 | |
lypinator | 0:bb348c97df44 | 5625 | } |
lypinator | 0:bb348c97df44 | 5626 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 5627 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 5628 | |
lypinator | 0:bb348c97df44 | 5629 | } |
lypinator | 0:bb348c97df44 | 5630 | |
lypinator | 0:bb348c97df44 | 5631 | /** |
lypinator | 0:bb348c97df44 | 5632 | * @brief Read derivative key in polling mode when CRYP hardware block is set |
lypinator | 0:bb348c97df44 | 5633 | * in key derivation operating mode (mode 2). |
lypinator | 0:bb348c97df44 | 5634 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 5635 | * the configuration information for CRYP module. |
lypinator | 0:bb348c97df44 | 5636 | * @param Output Pointer to the returned buffer. |
lypinator | 0:bb348c97df44 | 5637 | * @param Timeout Specify Timeout value. |
lypinator | 0:bb348c97df44 | 5638 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 5639 | */ |
lypinator | 0:bb348c97df44 | 5640 | static HAL_StatusTypeDef CRYP_ReadKey(CRYP_HandleTypeDef *hcryp, uint8_t* Output, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 5641 | { |
lypinator | 0:bb348c97df44 | 5642 | uint32_t outputaddr = (uint32_t)Output; |
lypinator | 0:bb348c97df44 | 5643 | |
lypinator | 0:bb348c97df44 | 5644 | /* Wait for CCF flag to be raised */ |
lypinator | 0:bb348c97df44 | 5645 | if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 5646 | { |
lypinator | 0:bb348c97df44 | 5647 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 5648 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 5649 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 5650 | } |
lypinator | 0:bb348c97df44 | 5651 | /* Clear CCF Flag */ |
lypinator | 0:bb348c97df44 | 5652 | __HAL_CRYP_CLEAR_FLAG( CRYP_CCF_CLEAR); |
lypinator | 0:bb348c97df44 | 5653 | |
lypinator | 0:bb348c97df44 | 5654 | /* Read the derivative key from the AES_KEYRx registers */ |
lypinator | 0:bb348c97df44 | 5655 | if (hcryp->Init.KeySize == CRYP_KEYSIZE_256B) |
lypinator | 0:bb348c97df44 | 5656 | { |
lypinator | 0:bb348c97df44 | 5657 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR7); |
lypinator | 0:bb348c97df44 | 5658 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5659 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR6); |
lypinator | 0:bb348c97df44 | 5660 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5661 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR5); |
lypinator | 0:bb348c97df44 | 5662 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5663 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR4); |
lypinator | 0:bb348c97df44 | 5664 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5665 | } |
lypinator | 0:bb348c97df44 | 5666 | |
lypinator | 0:bb348c97df44 | 5667 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR3); |
lypinator | 0:bb348c97df44 | 5668 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5669 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR2); |
lypinator | 0:bb348c97df44 | 5670 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5671 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR1); |
lypinator | 0:bb348c97df44 | 5672 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5673 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR0); |
lypinator | 0:bb348c97df44 | 5674 | |
lypinator | 0:bb348c97df44 | 5675 | /* Return function status */ |
lypinator | 0:bb348c97df44 | 5676 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 5677 | } |
lypinator | 0:bb348c97df44 | 5678 | |
lypinator | 0:bb348c97df44 | 5679 | /** |
lypinator | 0:bb348c97df44 | 5680 | * @brief Set the DMA configuration and start the DMA transfer. |
lypinator | 0:bb348c97df44 | 5681 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 5682 | * the configuration information for CRYP module. |
lypinator | 0:bb348c97df44 | 5683 | * @param inputaddr Address of the Input buffer. |
lypinator | 0:bb348c97df44 | 5684 | * @param Size Size of the Input buffer in bytes, must be a multiple of 16. |
lypinator | 0:bb348c97df44 | 5685 | * @param outputaddr Address of the Output buffer. |
lypinator | 0:bb348c97df44 | 5686 | * @retval None |
lypinator | 0:bb348c97df44 | 5687 | */ |
lypinator | 0:bb348c97df44 | 5688 | static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr) |
lypinator | 0:bb348c97df44 | 5689 | { |
lypinator | 0:bb348c97df44 | 5690 | /* Set the CRYP DMA transfer complete callback */ |
lypinator | 0:bb348c97df44 | 5691 | hcryp->hdmain->XferCpltCallback = CRYP_DMAInCplt; |
lypinator | 0:bb348c97df44 | 5692 | /* Set the DMA error callback */ |
lypinator | 0:bb348c97df44 | 5693 | hcryp->hdmain->XferErrorCallback = CRYP_DMAError; |
lypinator | 0:bb348c97df44 | 5694 | |
lypinator | 0:bb348c97df44 | 5695 | /* Set the CRYP DMA transfer complete callback */ |
lypinator | 0:bb348c97df44 | 5696 | hcryp->hdmaout->XferCpltCallback = CRYP_DMAOutCplt; |
lypinator | 0:bb348c97df44 | 5697 | /* Set the DMA error callback */ |
lypinator | 0:bb348c97df44 | 5698 | hcryp->hdmaout->XferErrorCallback = CRYP_DMAError; |
lypinator | 0:bb348c97df44 | 5699 | |
lypinator | 0:bb348c97df44 | 5700 | /* Enable the DMA input stream */ |
lypinator | 0:bb348c97df44 | 5701 | HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DINR, Size/4U); |
lypinator | 0:bb348c97df44 | 5702 | |
lypinator | 0:bb348c97df44 | 5703 | /* Enable the DMA output stream */ |
lypinator | 0:bb348c97df44 | 5704 | HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUTR, outputaddr, Size/4U); |
lypinator | 0:bb348c97df44 | 5705 | |
lypinator | 0:bb348c97df44 | 5706 | /* Enable In and Out DMA requests */ |
lypinator | 0:bb348c97df44 | 5707 | SET_BIT(hcryp->Instance->CR, (AES_CR_DMAINEN | AES_CR_DMAOUTEN)); |
lypinator | 0:bb348c97df44 | 5708 | |
lypinator | 0:bb348c97df44 | 5709 | /* Enable the CRYP peripheral */ |
lypinator | 0:bb348c97df44 | 5710 | __HAL_CRYP_ENABLE(); |
lypinator | 0:bb348c97df44 | 5711 | } |
lypinator | 0:bb348c97df44 | 5712 | |
lypinator | 0:bb348c97df44 | 5713 | /** |
lypinator | 0:bb348c97df44 | 5714 | * @brief Handle CRYP hardware block Timeout when waiting for CCF flag to be raised. |
lypinator | 0:bb348c97df44 | 5715 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 5716 | * the configuration information for CRYP module. |
lypinator | 0:bb348c97df44 | 5717 | * @param Timeout Timeout duration. |
lypinator | 0:bb348c97df44 | 5718 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 5719 | */ |
lypinator | 0:bb348c97df44 | 5720 | static HAL_StatusTypeDef CRYP_WaitOnCCFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 5721 | { |
lypinator | 0:bb348c97df44 | 5722 | uint32_t tickstart = 0U; |
lypinator | 0:bb348c97df44 | 5723 | |
lypinator | 0:bb348c97df44 | 5724 | /* Get timeout */ |
lypinator | 0:bb348c97df44 | 5725 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 5726 | |
lypinator | 0:bb348c97df44 | 5727 | while(HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF)) |
lypinator | 0:bb348c97df44 | 5728 | { |
lypinator | 0:bb348c97df44 | 5729 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 5730 | if(Timeout != HAL_MAX_DELAY) |
lypinator | 0:bb348c97df44 | 5731 | { |
lypinator | 0:bb348c97df44 | 5732 | if((HAL_GetTick() - tickstart ) > Timeout) |
lypinator | 0:bb348c97df44 | 5733 | { |
lypinator | 0:bb348c97df44 | 5734 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 5735 | } |
lypinator | 0:bb348c97df44 | 5736 | } |
lypinator | 0:bb348c97df44 | 5737 | } |
lypinator | 0:bb348c97df44 | 5738 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 5739 | } |
lypinator | 0:bb348c97df44 | 5740 | |
lypinator | 0:bb348c97df44 | 5741 | /** |
lypinator | 0:bb348c97df44 | 5742 | * @brief Wait for Busy Flag to be reset during a GCM payload encryption process suspension. |
lypinator | 0:bb348c97df44 | 5743 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 5744 | * the configuration information for CRYP module. |
lypinator | 0:bb348c97df44 | 5745 | * @param Timeout Timeout duration. |
lypinator | 0:bb348c97df44 | 5746 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 5747 | */ |
lypinator | 0:bb348c97df44 | 5748 | static HAL_StatusTypeDef CRYP_WaitOnBusyFlagReset(CRYP_HandleTypeDef *hcryp, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 5749 | { |
lypinator | 0:bb348c97df44 | 5750 | uint32_t tickstart = 0U; |
lypinator | 0:bb348c97df44 | 5751 | |
lypinator | 0:bb348c97df44 | 5752 | /* Get timeout */ |
lypinator | 0:bb348c97df44 | 5753 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 5754 | |
lypinator | 0:bb348c97df44 | 5755 | while(HAL_IS_BIT_SET(hcryp->Instance->SR, AES_SR_BUSY)) |
lypinator | 0:bb348c97df44 | 5756 | { |
lypinator | 0:bb348c97df44 | 5757 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 5758 | if(Timeout != HAL_MAX_DELAY) |
lypinator | 0:bb348c97df44 | 5759 | { |
lypinator | 0:bb348c97df44 | 5760 | if((HAL_GetTick() - tickstart ) > Timeout) |
lypinator | 0:bb348c97df44 | 5761 | { |
lypinator | 0:bb348c97df44 | 5762 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 5763 | } |
lypinator | 0:bb348c97df44 | 5764 | } |
lypinator | 0:bb348c97df44 | 5765 | } |
lypinator | 0:bb348c97df44 | 5766 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 5767 | } |
lypinator | 0:bb348c97df44 | 5768 | |
lypinator | 0:bb348c97df44 | 5769 | /** |
lypinator | 0:bb348c97df44 | 5770 | * @brief DMA CRYP Input Data process complete callback. |
lypinator | 0:bb348c97df44 | 5771 | * @param hdma DMA handle. |
lypinator | 0:bb348c97df44 | 5772 | * @retval None |
lypinator | 0:bb348c97df44 | 5773 | */ |
lypinator | 0:bb348c97df44 | 5774 | static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma) |
lypinator | 0:bb348c97df44 | 5775 | { |
lypinator | 0:bb348c97df44 | 5776 | CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
lypinator | 0:bb348c97df44 | 5777 | |
lypinator | 0:bb348c97df44 | 5778 | /* Disable the DMA transfer for input request */ |
lypinator | 0:bb348c97df44 | 5779 | CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAINEN); |
lypinator | 0:bb348c97df44 | 5780 | |
lypinator | 0:bb348c97df44 | 5781 | /* Call input data transfer complete callback */ |
lypinator | 0:bb348c97df44 | 5782 | HAL_CRYP_InCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 5783 | } |
lypinator | 0:bb348c97df44 | 5784 | |
lypinator | 0:bb348c97df44 | 5785 | /** |
lypinator | 0:bb348c97df44 | 5786 | * @brief DMA CRYP Output Data process complete callback. |
lypinator | 0:bb348c97df44 | 5787 | * @param hdma DMA handle. |
lypinator | 0:bb348c97df44 | 5788 | * @retval None |
lypinator | 0:bb348c97df44 | 5789 | */ |
lypinator | 0:bb348c97df44 | 5790 | static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma) |
lypinator | 0:bb348c97df44 | 5791 | { |
lypinator | 0:bb348c97df44 | 5792 | CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
lypinator | 0:bb348c97df44 | 5793 | |
lypinator | 0:bb348c97df44 | 5794 | /* Disable the DMA transfer for output request */ |
lypinator | 0:bb348c97df44 | 5795 | CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAOUTEN); |
lypinator | 0:bb348c97df44 | 5796 | |
lypinator | 0:bb348c97df44 | 5797 | /* Clear CCF Flag */ |
lypinator | 0:bb348c97df44 | 5798 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
lypinator | 0:bb348c97df44 | 5799 | |
lypinator | 0:bb348c97df44 | 5800 | /* Disable CRYP */ |
lypinator | 0:bb348c97df44 | 5801 | __HAL_CRYP_DISABLE(); |
lypinator | 0:bb348c97df44 | 5802 | |
lypinator | 0:bb348c97df44 | 5803 | /* Change the CRYP state to ready */ |
lypinator | 0:bb348c97df44 | 5804 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 5805 | |
lypinator | 0:bb348c97df44 | 5806 | /* Call output data transfer complete callback */ |
lypinator | 0:bb348c97df44 | 5807 | HAL_CRYP_OutCpltCallback(hcryp); |
lypinator | 0:bb348c97df44 | 5808 | } |
lypinator | 0:bb348c97df44 | 5809 | |
lypinator | 0:bb348c97df44 | 5810 | /** |
lypinator | 0:bb348c97df44 | 5811 | * @brief DMA CRYP communication error callback. |
lypinator | 0:bb348c97df44 | 5812 | * @param hdma DMA handle. |
lypinator | 0:bb348c97df44 | 5813 | * @retval None |
lypinator | 0:bb348c97df44 | 5814 | */ |
lypinator | 0:bb348c97df44 | 5815 | static void CRYP_DMAError(DMA_HandleTypeDef *hdma) |
lypinator | 0:bb348c97df44 | 5816 | { |
lypinator | 0:bb348c97df44 | 5817 | CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
lypinator | 0:bb348c97df44 | 5818 | |
lypinator | 0:bb348c97df44 | 5819 | hcryp->State= HAL_CRYP_STATE_ERROR; |
lypinator | 0:bb348c97df44 | 5820 | hcryp->ErrorCode |= HAL_CRYP_DMA_ERROR; |
lypinator | 0:bb348c97df44 | 5821 | HAL_CRYP_ErrorCallback(hcryp); |
lypinator | 0:bb348c97df44 | 5822 | /* Clear Error Flag */ |
lypinator | 0:bb348c97df44 | 5823 | __HAL_CRYP_CLEAR_FLAG(CRYP_ERR_CLEAR); |
lypinator | 0:bb348c97df44 | 5824 | } |
lypinator | 0:bb348c97df44 | 5825 | |
lypinator | 0:bb348c97df44 | 5826 | /** |
lypinator | 0:bb348c97df44 | 5827 | * @brief Last header or payload block padding when size is not a multiple of 128 bits. |
lypinator | 0:bb348c97df44 | 5828 | * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 5829 | * the configuration information for CRYP module. |
lypinator | 0:bb348c97df44 | 5830 | * @param difflength size remainder after having fed all complete 128-bit blocks. |
lypinator | 0:bb348c97df44 | 5831 | * @param polling specifies whether or not polling on CCF must be done after having |
lypinator | 0:bb348c97df44 | 5832 | * entered a complete block. |
lypinator | 0:bb348c97df44 | 5833 | * @retval None |
lypinator | 0:bb348c97df44 | 5834 | */ |
lypinator | 0:bb348c97df44 | 5835 | static void CRYP_Padding(CRYP_HandleTypeDef *hcryp, uint32_t difflength, uint32_t polling) |
lypinator | 0:bb348c97df44 | 5836 | { |
lypinator | 0:bb348c97df44 | 5837 | uint32_t index = 0U; |
lypinator | 0:bb348c97df44 | 5838 | uint32_t difflengthmod4 = difflength%4U; |
lypinator | 0:bb348c97df44 | 5839 | uint32_t inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
lypinator | 0:bb348c97df44 | 5840 | uint32_t outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; |
lypinator | 0:bb348c97df44 | 5841 | uint32_t mask[3U] = {0x0FFU, 0x0FFFFU, 0x0FFFFFFU}; |
lypinator | 0:bb348c97df44 | 5842 | uint32_t intermediate_data[4U] = {0U}; |
lypinator | 0:bb348c97df44 | 5843 | |
lypinator | 0:bb348c97df44 | 5844 | #if defined(AES_CR_NPBLB) |
lypinator | 0:bb348c97df44 | 5845 | /* In case of GCM encryption or CCM decryption, specify the number of padding |
lypinator | 0:bb348c97df44 | 5846 | bytes in last block of payload */ |
lypinator | 0:bb348c97df44 | 5847 | if (READ_BIT(hcryp->Instance->CR,AES_CR_GCMPH) == CRYP_PAYLOAD_PHASE) |
lypinator | 0:bb348c97df44 | 5848 | { |
lypinator | 0:bb348c97df44 | 5849 | if (((READ_BIT(hcryp->Instance->CR, AES_CR_CHMOD) == CRYP_CHAINMODE_AES_GCM_GMAC) |
lypinator | 0:bb348c97df44 | 5850 | && (READ_BIT(hcryp->Instance->CR, AES_CR_MODE) == CRYP_ALGOMODE_ENCRYPT)) |
lypinator | 0:bb348c97df44 | 5851 | || ((READ_BIT(hcryp->Instance->CR, AES_CR_CHMOD) == CRYP_CHAINMODE_AES_CCM_CMAC) |
lypinator | 0:bb348c97df44 | 5852 | && (READ_BIT(hcryp->Instance->CR, AES_CR_MODE) == CRYP_ALGOMODE_DECRYPT))) |
lypinator | 0:bb348c97df44 | 5853 | { |
lypinator | 0:bb348c97df44 | 5854 | /* Set NPBLB field in writing the number of padding bytes |
lypinator | 0:bb348c97df44 | 5855 | for the last block of payload */ |
lypinator | 0:bb348c97df44 | 5856 | MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, 16U - difflength); |
lypinator | 0:bb348c97df44 | 5857 | } |
lypinator | 0:bb348c97df44 | 5858 | } |
lypinator | 0:bb348c97df44 | 5859 | #else |
lypinator | 0:bb348c97df44 | 5860 | /* Software workaround applied to GCM encryption only */ |
lypinator | 0:bb348c97df44 | 5861 | if ((hcryp->Init.GCMCMACPhase == CRYP_GCM_PAYLOAD_PHASE) && |
lypinator | 0:bb348c97df44 | 5862 | (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT)) |
lypinator | 0:bb348c97df44 | 5863 | { |
lypinator | 0:bb348c97df44 | 5864 | /* Change the mode configured in CHMOD bits of CR register to select CTR mode */ |
lypinator | 0:bb348c97df44 | 5865 | __HAL_CRYP_SET_CHAININGMODE(CRYP_CHAINMODE_AES_CTR); |
lypinator | 0:bb348c97df44 | 5866 | } |
lypinator | 0:bb348c97df44 | 5867 | #endif |
lypinator | 0:bb348c97df44 | 5868 | |
lypinator | 0:bb348c97df44 | 5869 | /* Wrap-up entering header or payload data */ |
lypinator | 0:bb348c97df44 | 5870 | /* Enter complete words when possible */ |
lypinator | 0:bb348c97df44 | 5871 | for(index=0U; index < (difflength/4U); index ++) |
lypinator | 0:bb348c97df44 | 5872 | { |
lypinator | 0:bb348c97df44 | 5873 | /* Write the Input block in the Data Input register */ |
lypinator | 0:bb348c97df44 | 5874 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
lypinator | 0:bb348c97df44 | 5875 | inputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5876 | } |
lypinator | 0:bb348c97df44 | 5877 | /* Enter incomplete word padded with zeroes if applicable |
lypinator | 0:bb348c97df44 | 5878 | (case of header length not a multiple of 32-bits) */ |
lypinator | 0:bb348c97df44 | 5879 | if (difflengthmod4 != 0U) |
lypinator | 0:bb348c97df44 | 5880 | { |
lypinator | 0:bb348c97df44 | 5881 | hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[difflengthmod4-1]); |
lypinator | 0:bb348c97df44 | 5882 | } |
lypinator | 0:bb348c97df44 | 5883 | /* Pad with zero-words to reach 128-bit long block and wrap-up header feeding to the IP */ |
lypinator | 0:bb348c97df44 | 5884 | for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++) |
lypinator | 0:bb348c97df44 | 5885 | { |
lypinator | 0:bb348c97df44 | 5886 | hcryp->Instance->DINR = 0U; |
lypinator | 0:bb348c97df44 | 5887 | } |
lypinator | 0:bb348c97df44 | 5888 | |
lypinator | 0:bb348c97df44 | 5889 | if (polling == CRYP_POLLING_ON) |
lypinator | 0:bb348c97df44 | 5890 | { |
lypinator | 0:bb348c97df44 | 5891 | if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK) |
lypinator | 0:bb348c97df44 | 5892 | { |
lypinator | 0:bb348c97df44 | 5893 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 5894 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 5895 | HAL_CRYP_ErrorCallback(hcryp); |
lypinator | 0:bb348c97df44 | 5896 | } |
lypinator | 0:bb348c97df44 | 5897 | |
lypinator | 0:bb348c97df44 | 5898 | /* Clear CCF Flag */ |
lypinator | 0:bb348c97df44 | 5899 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
lypinator | 0:bb348c97df44 | 5900 | } |
lypinator | 0:bb348c97df44 | 5901 | |
lypinator | 0:bb348c97df44 | 5902 | /* if payload */ |
lypinator | 0:bb348c97df44 | 5903 | if (hcryp->Init.GCMCMACPhase == CRYP_GCM_PAYLOAD_PHASE) |
lypinator | 0:bb348c97df44 | 5904 | { |
lypinator | 0:bb348c97df44 | 5905 | |
lypinator | 0:bb348c97df44 | 5906 | /* Retrieve intermediate data */ |
lypinator | 0:bb348c97df44 | 5907 | for(index=0U; index < 4U; index ++) |
lypinator | 0:bb348c97df44 | 5908 | { |
lypinator | 0:bb348c97df44 | 5909 | intermediate_data[index] = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 5910 | } |
lypinator | 0:bb348c97df44 | 5911 | /* Retrieve last words of cyphered data */ |
lypinator | 0:bb348c97df44 | 5912 | /* First, retrieve complete output words */ |
lypinator | 0:bb348c97df44 | 5913 | for(index=0U; index < (difflength/4U); index ++) |
lypinator | 0:bb348c97df44 | 5914 | { |
lypinator | 0:bb348c97df44 | 5915 | *(uint32_t*)(outputaddr) = intermediate_data[index]; |
lypinator | 0:bb348c97df44 | 5916 | outputaddr+=4U; |
lypinator | 0:bb348c97df44 | 5917 | } |
lypinator | 0:bb348c97df44 | 5918 | /* Next, retrieve partial output word if applicable; |
lypinator | 0:bb348c97df44 | 5919 | at the same time, start masking intermediate data |
lypinator | 0:bb348c97df44 | 5920 | with a mask of zeros of same size than the padding |
lypinator | 0:bb348c97df44 | 5921 | applied to the last block of payload */ |
lypinator | 0:bb348c97df44 | 5922 | if (difflengthmod4 != 0U) |
lypinator | 0:bb348c97df44 | 5923 | { |
lypinator | 0:bb348c97df44 | 5924 | intermediate_data[difflength/4U] &= mask[difflengthmod4-1U]; |
lypinator | 0:bb348c97df44 | 5925 | *(uint32_t*)(outputaddr) = intermediate_data[difflength/4U]; |
lypinator | 0:bb348c97df44 | 5926 | } |
lypinator | 0:bb348c97df44 | 5927 | |
lypinator | 0:bb348c97df44 | 5928 | #if !defined(AES_CR_NPBLB) |
lypinator | 0:bb348c97df44 | 5929 | /* Software workaround applied to GCM encryption only, |
lypinator | 0:bb348c97df44 | 5930 | applicable for AES IP v2 version (where NPBLB is not defined) */ |
lypinator | 0:bb348c97df44 | 5931 | if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT) |
lypinator | 0:bb348c97df44 | 5932 | { |
lypinator | 0:bb348c97df44 | 5933 | /* Change again CHMOD configuration to GCM mode */ |
lypinator | 0:bb348c97df44 | 5934 | __HAL_CRYP_SET_CHAININGMODE(CRYP_CHAINMODE_AES_GCM_GMAC); |
lypinator | 0:bb348c97df44 | 5935 | |
lypinator | 0:bb348c97df44 | 5936 | /* Select FINAL phase */ |
lypinator | 0:bb348c97df44 | 5937 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_GCMCMAC_FINAL_PHASE); |
lypinator | 0:bb348c97df44 | 5938 | |
lypinator | 0:bb348c97df44 | 5939 | /* Before inserting the intermediate data, carry on masking operation |
lypinator | 0:bb348c97df44 | 5940 | with a mask of zeros of same size than the padding applied to the last block of payload */ |
lypinator | 0:bb348c97df44 | 5941 | for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++) |
lypinator | 0:bb348c97df44 | 5942 | { |
lypinator | 0:bb348c97df44 | 5943 | intermediate_data[(difflength+3U)/4U+index] = 0U; |
lypinator | 0:bb348c97df44 | 5944 | } |
lypinator | 0:bb348c97df44 | 5945 | /* Insert intermediate data */ |
lypinator | 0:bb348c97df44 | 5946 | for(index=0U; index < 4U; index ++) |
lypinator | 0:bb348c97df44 | 5947 | { |
lypinator | 0:bb348c97df44 | 5948 | hcryp->Instance->DINR = intermediate_data[index]; |
lypinator | 0:bb348c97df44 | 5949 | } |
lypinator | 0:bb348c97df44 | 5950 | |
lypinator | 0:bb348c97df44 | 5951 | /* Wait for completion, and read data on DOUT. This data is to discard. */ |
lypinator | 0:bb348c97df44 | 5952 | if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK) |
lypinator | 0:bb348c97df44 | 5953 | { |
lypinator | 0:bb348c97df44 | 5954 | hcryp->State = HAL_CRYP_STATE_READY; |
lypinator | 0:bb348c97df44 | 5955 | __HAL_UNLOCK(hcryp); |
lypinator | 0:bb348c97df44 | 5956 | HAL_CRYP_ErrorCallback(hcryp); |
lypinator | 0:bb348c97df44 | 5957 | } |
lypinator | 0:bb348c97df44 | 5958 | |
lypinator | 0:bb348c97df44 | 5959 | /* Read data to discard */ |
lypinator | 0:bb348c97df44 | 5960 | /* Clear CCF Flag */ |
lypinator | 0:bb348c97df44 | 5961 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
lypinator | 0:bb348c97df44 | 5962 | for(index=0U; index < 4U; index ++) |
lypinator | 0:bb348c97df44 | 5963 | { |
lypinator | 0:bb348c97df44 | 5964 | intermediate_data[index] = hcryp->Instance->DOUTR; |
lypinator | 0:bb348c97df44 | 5965 | } |
lypinator | 0:bb348c97df44 | 5966 | |
lypinator | 0:bb348c97df44 | 5967 | } /* if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT) */ |
lypinator | 0:bb348c97df44 | 5968 | #endif /* !defined(AES_CR_NPBLB) */ |
lypinator | 0:bb348c97df44 | 5969 | } /* if (hcryp->Init.GCMCMACPhase == CRYP_GCM_PAYLOAD_PHASE) */ |
lypinator | 0:bb348c97df44 | 5970 | |
lypinator | 0:bb348c97df44 | 5971 | } |
lypinator | 0:bb348c97df44 | 5972 | |
lypinator | 0:bb348c97df44 | 5973 | /** |
lypinator | 0:bb348c97df44 | 5974 | * @} |
lypinator | 0:bb348c97df44 | 5975 | */ |
lypinator | 0:bb348c97df44 | 5976 | |
lypinator | 0:bb348c97df44 | 5977 | #endif /* AES */ |
lypinator | 0:bb348c97df44 | 5978 | |
lypinator | 0:bb348c97df44 | 5979 | #endif /* HAL_CRYP_MODULE_ENABLED */ |
lypinator | 0:bb348c97df44 | 5980 | /** |
lypinator | 0:bb348c97df44 | 5981 | * @} |
lypinator | 0:bb348c97df44 | 5982 | */ |
lypinator | 0:bb348c97df44 | 5983 | |
lypinator | 0:bb348c97df44 | 5984 | /** |
lypinator | 0:bb348c97df44 | 5985 | * @} |
lypinator | 0:bb348c97df44 | 5986 | */ |
lypinator | 0:bb348c97df44 | 5987 | |
lypinator | 0:bb348c97df44 | 5988 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |