inport from local
Dependents: Hobbyking_Cheetah_0511
targets/TARGET_STM/TARGET_STM32F4/device/stm32f4xx_hal_hash.c@0:85b3fd62ea1a, 2020-03-16 (annotated)
- Committer:
- NYX
- Date:
- Mon Mar 16 06:35:48 2020 +0000
- Revision:
- 0:85b3fd62ea1a
reinport to mbed;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
NYX | 0:85b3fd62ea1a | 1 | /** |
NYX | 0:85b3fd62ea1a | 2 | ****************************************************************************** |
NYX | 0:85b3fd62ea1a | 3 | * @file stm32f4xx_hal_hash.c |
NYX | 0:85b3fd62ea1a | 4 | * @author MCD Application Team |
NYX | 0:85b3fd62ea1a | 5 | * @version V1.7.1 |
NYX | 0:85b3fd62ea1a | 6 | * @date 14-April-2017 |
NYX | 0:85b3fd62ea1a | 7 | * @brief HASH HAL module driver. |
NYX | 0:85b3fd62ea1a | 8 | * This file provides firmware functions to manage the following |
NYX | 0:85b3fd62ea1a | 9 | * functionalities of the HASH peripheral: |
NYX | 0:85b3fd62ea1a | 10 | * + Initialization and de-initialization functions |
NYX | 0:85b3fd62ea1a | 11 | * + HASH/HMAC Processing functions by algorithm using polling mode |
NYX | 0:85b3fd62ea1a | 12 | * + HASH/HMAC functions by algorithm using interrupt mode |
NYX | 0:85b3fd62ea1a | 13 | * + HASH/HMAC functions by algorithm using DMA mode |
NYX | 0:85b3fd62ea1a | 14 | * + Peripheral State functions |
NYX | 0:85b3fd62ea1a | 15 | * |
NYX | 0:85b3fd62ea1a | 16 | @verbatim |
NYX | 0:85b3fd62ea1a | 17 | ============================================================================== |
NYX | 0:85b3fd62ea1a | 18 | ##### How to use this driver ##### |
NYX | 0:85b3fd62ea1a | 19 | ============================================================================== |
NYX | 0:85b3fd62ea1a | 20 | [..] |
NYX | 0:85b3fd62ea1a | 21 | The HASH HAL driver can be used as follows: |
NYX | 0:85b3fd62ea1a | 22 | (#)Initialize the HASH low level resources by implementing the HAL_HASH_MspInit(): |
NYX | 0:85b3fd62ea1a | 23 | (##) Enable the HASH interface clock using __HAL_RCC_HASH_CLK_ENABLE() |
NYX | 0:85b3fd62ea1a | 24 | (##) In case of using processing APIs based on interrupts (e.g. HAL_HMAC_SHA1_Start_IT()) |
NYX | 0:85b3fd62ea1a | 25 | (+++) Configure the HASH interrupt priority using HAL_NVIC_SetPriority() |
NYX | 0:85b3fd62ea1a | 26 | (+++) Enable the HASH IRQ handler using HAL_NVIC_EnableIRQ() |
NYX | 0:85b3fd62ea1a | 27 | (+++) In HASH IRQ handler, call HAL_HASH_IRQHandler() |
NYX | 0:85b3fd62ea1a | 28 | (##) In case of using DMA to control data transfer (e.g. HAL_HMAC_SHA1_Start_DMA()) |
NYX | 0:85b3fd62ea1a | 29 | (+++) Enable the DMAx interface clock using __DMAx_CLK_ENABLE() |
NYX | 0:85b3fd62ea1a | 30 | (+++) Configure and enable one DMA stream one for managing data transfer from |
NYX | 0:85b3fd62ea1a | 31 | memory to peripheral (input stream). Managing data transfer from |
NYX | 0:85b3fd62ea1a | 32 | peripheral to memory can be performed only using CPU |
NYX | 0:85b3fd62ea1a | 33 | (+++) Associate the initialized DMA handle to the HASH DMA handle |
NYX | 0:85b3fd62ea1a | 34 | using __HAL_LINKDMA() |
NYX | 0:85b3fd62ea1a | 35 | (+++) Configure the priority and enable the NVIC for the transfer complete |
NYX | 0:85b3fd62ea1a | 36 | interrupt on the DMA Stream using HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ() |
NYX | 0:85b3fd62ea1a | 37 | (#)Initialize the HASH HAL using HAL_HASH_Init(). This function configures mainly: |
NYX | 0:85b3fd62ea1a | 38 | (##) The data type: 1-bit, 8-bit, 16-bit and 32-bit. |
NYX | 0:85b3fd62ea1a | 39 | (##) For HMAC, the encryption key. |
NYX | 0:85b3fd62ea1a | 40 | (##) For HMAC, the key size used for encryption. |
NYX | 0:85b3fd62ea1a | 41 | (#)Three processing functions are available: |
NYX | 0:85b3fd62ea1a | 42 | (##) Polling mode: processing APIs are blocking functions |
NYX | 0:85b3fd62ea1a | 43 | i.e. they process the data and wait till the digest computation is finished |
NYX | 0:85b3fd62ea1a | 44 | e.g. HAL_HASH_SHA1_Start() |
NYX | 0:85b3fd62ea1a | 45 | (##) Interrupt mode: encryption and decryption APIs are not blocking functions |
NYX | 0:85b3fd62ea1a | 46 | i.e. they process the data under interrupt |
NYX | 0:85b3fd62ea1a | 47 | e.g. HAL_HASH_SHA1_Start_IT() |
NYX | 0:85b3fd62ea1a | 48 | (##) DMA mode: processing APIs are not blocking functions and the CPU is |
NYX | 0:85b3fd62ea1a | 49 | not used for data transfer i.e. the data transfer is ensured by DMA |
NYX | 0:85b3fd62ea1a | 50 | e.g. HAL_HASH_SHA1_Start_DMA() |
NYX | 0:85b3fd62ea1a | 51 | (#)When the processing function is called at first time after HAL_HASH_Init() |
NYX | 0:85b3fd62ea1a | 52 | the HASH peripheral is initialized and processes the buffer in input. |
NYX | 0:85b3fd62ea1a | 53 | After that, the digest computation is started. |
NYX | 0:85b3fd62ea1a | 54 | When processing multi-buffer use the accumulate function to write the |
NYX | 0:85b3fd62ea1a | 55 | data in the peripheral without starting the digest computation. In last |
NYX | 0:85b3fd62ea1a | 56 | buffer use the start function to input the last buffer ans start the digest |
NYX | 0:85b3fd62ea1a | 57 | computation. |
NYX | 0:85b3fd62ea1a | 58 | (##) e.g. HAL_HASH_SHA1_Accumulate() : write 1st data buffer in the peripheral without starting the digest computation |
NYX | 0:85b3fd62ea1a | 59 | (##) write (n-1)th data buffer in the peripheral without starting the digest computation |
NYX | 0:85b3fd62ea1a | 60 | (##) HAL_HASH_SHA1_Start() : write (n)th data buffer in the peripheral and start the digest computation |
NYX | 0:85b3fd62ea1a | 61 | (#)In HMAC mode, there is no Accumulate API. Only Start API is available. |
NYX | 0:85b3fd62ea1a | 62 | (#)In case of using DMA, call the DMA start processing e.g. HAL_HASH_SHA1_Start_DMA(). |
NYX | 0:85b3fd62ea1a | 63 | After that, call the finish function in order to get the digest value |
NYX | 0:85b3fd62ea1a | 64 | e.g. HAL_HASH_SHA1_Finish() |
NYX | 0:85b3fd62ea1a | 65 | (#)Call HAL_HASH_DeInit() to deinitialize the HASH peripheral. |
NYX | 0:85b3fd62ea1a | 66 | |
NYX | 0:85b3fd62ea1a | 67 | @endverbatim |
NYX | 0:85b3fd62ea1a | 68 | ****************************************************************************** |
NYX | 0:85b3fd62ea1a | 69 | * @attention |
NYX | 0:85b3fd62ea1a | 70 | * |
NYX | 0:85b3fd62ea1a | 71 | * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> |
NYX | 0:85b3fd62ea1a | 72 | * |
NYX | 0:85b3fd62ea1a | 73 | * Redistribution and use in source and binary forms, with or without modification, |
NYX | 0:85b3fd62ea1a | 74 | * are permitted provided that the following conditions are met: |
NYX | 0:85b3fd62ea1a | 75 | * 1. Redistributions of source code must retain the above copyright notice, |
NYX | 0:85b3fd62ea1a | 76 | * this list of conditions and the following disclaimer. |
NYX | 0:85b3fd62ea1a | 77 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
NYX | 0:85b3fd62ea1a | 78 | * this list of conditions and the following disclaimer in the documentation |
NYX | 0:85b3fd62ea1a | 79 | * and/or other materials provided with the distribution. |
NYX | 0:85b3fd62ea1a | 80 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
NYX | 0:85b3fd62ea1a | 81 | * may be used to endorse or promote products derived from this software |
NYX | 0:85b3fd62ea1a | 82 | * without specific prior written permission. |
NYX | 0:85b3fd62ea1a | 83 | * |
NYX | 0:85b3fd62ea1a | 84 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
NYX | 0:85b3fd62ea1a | 85 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
NYX | 0:85b3fd62ea1a | 86 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
NYX | 0:85b3fd62ea1a | 87 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
NYX | 0:85b3fd62ea1a | 88 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
NYX | 0:85b3fd62ea1a | 89 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
NYX | 0:85b3fd62ea1a | 90 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
NYX | 0:85b3fd62ea1a | 91 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
NYX | 0:85b3fd62ea1a | 92 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
NYX | 0:85b3fd62ea1a | 93 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
NYX | 0:85b3fd62ea1a | 94 | * |
NYX | 0:85b3fd62ea1a | 95 | ****************************************************************************** |
NYX | 0:85b3fd62ea1a | 96 | */ |
NYX | 0:85b3fd62ea1a | 97 | |
NYX | 0:85b3fd62ea1a | 98 | /* Includes ------------------------------------------------------------------*/ |
NYX | 0:85b3fd62ea1a | 99 | #include "stm32f4xx_hal.h" |
NYX | 0:85b3fd62ea1a | 100 | |
NYX | 0:85b3fd62ea1a | 101 | /** @addtogroup STM32F4xx_HAL_Driver |
NYX | 0:85b3fd62ea1a | 102 | * @{ |
NYX | 0:85b3fd62ea1a | 103 | */ |
NYX | 0:85b3fd62ea1a | 104 | |
NYX | 0:85b3fd62ea1a | 105 | /** @defgroup HASH HASH |
NYX | 0:85b3fd62ea1a | 106 | * @brief HASH HAL module driver. |
NYX | 0:85b3fd62ea1a | 107 | * @{ |
NYX | 0:85b3fd62ea1a | 108 | */ |
NYX | 0:85b3fd62ea1a | 109 | |
NYX | 0:85b3fd62ea1a | 110 | #ifdef HAL_HASH_MODULE_ENABLED |
NYX | 0:85b3fd62ea1a | 111 | |
NYX | 0:85b3fd62ea1a | 112 | #if defined(STM32F415xx) || defined(STM32F417xx) || defined(STM32F437xx) || defined(STM32F439xx) || defined(STM32F479xx) |
NYX | 0:85b3fd62ea1a | 113 | |
NYX | 0:85b3fd62ea1a | 114 | /* Private typedef -----------------------------------------------------------*/ |
NYX | 0:85b3fd62ea1a | 115 | /* Private define ------------------------------------------------------------*/ |
NYX | 0:85b3fd62ea1a | 116 | /* Private macro -------------------------------------------------------------*/ |
NYX | 0:85b3fd62ea1a | 117 | /* Private variables ---------------------------------------------------------*/ |
NYX | 0:85b3fd62ea1a | 118 | /* Private function prototypes -----------------------------------------------*/ |
NYX | 0:85b3fd62ea1a | 119 | /** @defgroup HASH_Private_Functions HASH Private Functions |
NYX | 0:85b3fd62ea1a | 120 | * @{ |
NYX | 0:85b3fd62ea1a | 121 | */ |
NYX | 0:85b3fd62ea1a | 122 | static void HASH_DMAXferCplt(DMA_HandleTypeDef *hdma); |
NYX | 0:85b3fd62ea1a | 123 | static void HASH_DMAError(DMA_HandleTypeDef *hdma); |
NYX | 0:85b3fd62ea1a | 124 | static void HASH_GetDigest(uint8_t *pMsgDigest, uint8_t Size); |
NYX | 0:85b3fd62ea1a | 125 | static void HASH_WriteData(uint8_t *pInBuffer, uint32_t Size); |
NYX | 0:85b3fd62ea1a | 126 | /** |
NYX | 0:85b3fd62ea1a | 127 | * @} |
NYX | 0:85b3fd62ea1a | 128 | */ |
NYX | 0:85b3fd62ea1a | 129 | |
NYX | 0:85b3fd62ea1a | 130 | /* Private functions ---------------------------------------------------------*/ |
NYX | 0:85b3fd62ea1a | 131 | /** @addtogroup HASH_Private_Functions |
NYX | 0:85b3fd62ea1a | 132 | * @{ |
NYX | 0:85b3fd62ea1a | 133 | */ |
NYX | 0:85b3fd62ea1a | 134 | |
NYX | 0:85b3fd62ea1a | 135 | /** |
NYX | 0:85b3fd62ea1a | 136 | * @brief DMA HASH Input Data complete callback. |
NYX | 0:85b3fd62ea1a | 137 | * @param hdma: DMA handle |
NYX | 0:85b3fd62ea1a | 138 | * @retval None |
NYX | 0:85b3fd62ea1a | 139 | */ |
NYX | 0:85b3fd62ea1a | 140 | static void HASH_DMAXferCplt(DMA_HandleTypeDef *hdma) |
NYX | 0:85b3fd62ea1a | 141 | { |
NYX | 0:85b3fd62ea1a | 142 | HASH_HandleTypeDef* hhash = ( HASH_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
NYX | 0:85b3fd62ea1a | 143 | uint32_t inputaddr = 0U; |
NYX | 0:85b3fd62ea1a | 144 | uint32_t buffersize = 0U; |
NYX | 0:85b3fd62ea1a | 145 | |
NYX | 0:85b3fd62ea1a | 146 | if((HASH->CR & HASH_CR_MODE) != HASH_CR_MODE) |
NYX | 0:85b3fd62ea1a | 147 | { |
NYX | 0:85b3fd62ea1a | 148 | /* Disable the DMA transfer */ |
NYX | 0:85b3fd62ea1a | 149 | HASH->CR &= (uint32_t)(~HASH_CR_DMAE); |
NYX | 0:85b3fd62ea1a | 150 | |
NYX | 0:85b3fd62ea1a | 151 | /* Change HASH peripheral state */ |
NYX | 0:85b3fd62ea1a | 152 | hhash->State = HAL_HASH_STATE_READY; |
NYX | 0:85b3fd62ea1a | 153 | |
NYX | 0:85b3fd62ea1a | 154 | /* Call Input data transfer complete callback */ |
NYX | 0:85b3fd62ea1a | 155 | HAL_HASH_InCpltCallback(hhash); |
NYX | 0:85b3fd62ea1a | 156 | } |
NYX | 0:85b3fd62ea1a | 157 | else |
NYX | 0:85b3fd62ea1a | 158 | { |
NYX | 0:85b3fd62ea1a | 159 | /* Increment Interrupt counter */ |
NYX | 0:85b3fd62ea1a | 160 | hhash->HashInCount++; |
NYX | 0:85b3fd62ea1a | 161 | /* Disable the DMA transfer before starting the next transfer */ |
NYX | 0:85b3fd62ea1a | 162 | HASH->CR &= (uint32_t)(~HASH_CR_DMAE); |
NYX | 0:85b3fd62ea1a | 163 | |
NYX | 0:85b3fd62ea1a | 164 | if(hhash->HashInCount <= 2U) |
NYX | 0:85b3fd62ea1a | 165 | { |
NYX | 0:85b3fd62ea1a | 166 | /* In case HashInCount = 1, set the DMA to transfer data to HASH DIN register */ |
NYX | 0:85b3fd62ea1a | 167 | if(hhash->HashInCount == 1U) |
NYX | 0:85b3fd62ea1a | 168 | { |
NYX | 0:85b3fd62ea1a | 169 | inputaddr = (uint32_t)hhash->pHashInBuffPtr; |
NYX | 0:85b3fd62ea1a | 170 | buffersize = hhash->HashBuffSize; |
NYX | 0:85b3fd62ea1a | 171 | } |
NYX | 0:85b3fd62ea1a | 172 | /* In case HashInCount = 2, set the DMA to transfer key to HASH DIN register */ |
NYX | 0:85b3fd62ea1a | 173 | else if(hhash->HashInCount == 2U) |
NYX | 0:85b3fd62ea1a | 174 | { |
NYX | 0:85b3fd62ea1a | 175 | inputaddr = (uint32_t)hhash->Init.pKey; |
NYX | 0:85b3fd62ea1a | 176 | buffersize = hhash->Init.KeySize; |
NYX | 0:85b3fd62ea1a | 177 | } |
NYX | 0:85b3fd62ea1a | 178 | /* Configure the number of valid bits in last word of the message */ |
NYX | 0:85b3fd62ea1a | 179 | MODIFY_REG(HASH->STR, HASH_STR_NBLW, 8U * (buffersize % 4U)); |
NYX | 0:85b3fd62ea1a | 180 | |
NYX | 0:85b3fd62ea1a | 181 | /* Set the HASH DMA transfer complete */ |
NYX | 0:85b3fd62ea1a | 182 | hhash->hdmain->XferCpltCallback = HASH_DMAXferCplt; |
NYX | 0:85b3fd62ea1a | 183 | |
NYX | 0:85b3fd62ea1a | 184 | /* Enable the DMA In DMA Stream */ |
NYX | 0:85b3fd62ea1a | 185 | HAL_DMA_Start_IT(hhash->hdmain, inputaddr, (uint32_t)&HASH->DIN, (buffersize%4U ? (buffersize+3U)/4U:buffersize/4U)); |
NYX | 0:85b3fd62ea1a | 186 | |
NYX | 0:85b3fd62ea1a | 187 | /* Enable DMA requests */ |
NYX | 0:85b3fd62ea1a | 188 | HASH->CR |= (HASH_CR_DMAE); |
NYX | 0:85b3fd62ea1a | 189 | } |
NYX | 0:85b3fd62ea1a | 190 | else |
NYX | 0:85b3fd62ea1a | 191 | { |
NYX | 0:85b3fd62ea1a | 192 | /* Disable the DMA transfer */ |
NYX | 0:85b3fd62ea1a | 193 | HASH->CR &= (uint32_t)(~HASH_CR_DMAE); |
NYX | 0:85b3fd62ea1a | 194 | |
NYX | 0:85b3fd62ea1a | 195 | /* Reset the InCount */ |
NYX | 0:85b3fd62ea1a | 196 | hhash->HashInCount = 0U; |
NYX | 0:85b3fd62ea1a | 197 | |
NYX | 0:85b3fd62ea1a | 198 | /* Change HASH peripheral state */ |
NYX | 0:85b3fd62ea1a | 199 | hhash->State = HAL_HASH_STATE_READY; |
NYX | 0:85b3fd62ea1a | 200 | |
NYX | 0:85b3fd62ea1a | 201 | /* Call Input data transfer complete callback */ |
NYX | 0:85b3fd62ea1a | 202 | HAL_HASH_InCpltCallback(hhash); |
NYX | 0:85b3fd62ea1a | 203 | } |
NYX | 0:85b3fd62ea1a | 204 | } |
NYX | 0:85b3fd62ea1a | 205 | } |
NYX | 0:85b3fd62ea1a | 206 | |
NYX | 0:85b3fd62ea1a | 207 | /** |
NYX | 0:85b3fd62ea1a | 208 | * @brief DMA HASH communication error callback. |
NYX | 0:85b3fd62ea1a | 209 | * @param hdma: DMA handle |
NYX | 0:85b3fd62ea1a | 210 | * @retval None |
NYX | 0:85b3fd62ea1a | 211 | */ |
NYX | 0:85b3fd62ea1a | 212 | static void HASH_DMAError(DMA_HandleTypeDef *hdma) |
NYX | 0:85b3fd62ea1a | 213 | { |
NYX | 0:85b3fd62ea1a | 214 | HASH_HandleTypeDef* hhash = ( HASH_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
NYX | 0:85b3fd62ea1a | 215 | hhash->State= HAL_HASH_STATE_READY; |
NYX | 0:85b3fd62ea1a | 216 | HAL_HASH_ErrorCallback(hhash); |
NYX | 0:85b3fd62ea1a | 217 | } |
NYX | 0:85b3fd62ea1a | 218 | |
NYX | 0:85b3fd62ea1a | 219 | /** |
NYX | 0:85b3fd62ea1a | 220 | * @brief Writes the input buffer in data register. |
NYX | 0:85b3fd62ea1a | 221 | * @param pInBuffer: Pointer to input buffer |
NYX | 0:85b3fd62ea1a | 222 | * @param Size: The size of input buffer |
NYX | 0:85b3fd62ea1a | 223 | * @retval None |
NYX | 0:85b3fd62ea1a | 224 | */ |
NYX | 0:85b3fd62ea1a | 225 | static void HASH_WriteData(uint8_t *pInBuffer, uint32_t Size) |
NYX | 0:85b3fd62ea1a | 226 | { |
NYX | 0:85b3fd62ea1a | 227 | uint32_t buffercounter; |
NYX | 0:85b3fd62ea1a | 228 | uint32_t inputaddr = (uint32_t) pInBuffer; |
NYX | 0:85b3fd62ea1a | 229 | |
NYX | 0:85b3fd62ea1a | 230 | for(buffercounter = 0U; buffercounter < Size; buffercounter+=4) |
NYX | 0:85b3fd62ea1a | 231 | { |
NYX | 0:85b3fd62ea1a | 232 | HASH->DIN = *(uint32_t*)inputaddr; |
NYX | 0:85b3fd62ea1a | 233 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 234 | } |
NYX | 0:85b3fd62ea1a | 235 | } |
NYX | 0:85b3fd62ea1a | 236 | |
NYX | 0:85b3fd62ea1a | 237 | /** |
NYX | 0:85b3fd62ea1a | 238 | * @brief Provides the message digest result. |
NYX | 0:85b3fd62ea1a | 239 | * @param pMsgDigest: Pointer to the message digest |
NYX | 0:85b3fd62ea1a | 240 | * @param Size: The size of the message digest in bytes |
NYX | 0:85b3fd62ea1a | 241 | * @retval None |
NYX | 0:85b3fd62ea1a | 242 | */ |
NYX | 0:85b3fd62ea1a | 243 | static void HASH_GetDigest(uint8_t *pMsgDigest, uint8_t Size) |
NYX | 0:85b3fd62ea1a | 244 | { |
NYX | 0:85b3fd62ea1a | 245 | uint32_t msgdigest = (uint32_t)pMsgDigest; |
NYX | 0:85b3fd62ea1a | 246 | |
NYX | 0:85b3fd62ea1a | 247 | switch(Size) |
NYX | 0:85b3fd62ea1a | 248 | { |
NYX | 0:85b3fd62ea1a | 249 | case 16U: |
NYX | 0:85b3fd62ea1a | 250 | /* Read the message digest */ |
NYX | 0:85b3fd62ea1a | 251 | *(uint32_t*)(msgdigest) = __REV(HASH->HR[0U]); |
NYX | 0:85b3fd62ea1a | 252 | msgdigest+=4U; |
NYX | 0:85b3fd62ea1a | 253 | *(uint32_t*)(msgdigest) = __REV(HASH->HR[1U]); |
NYX | 0:85b3fd62ea1a | 254 | msgdigest+=4U; |
NYX | 0:85b3fd62ea1a | 255 | *(uint32_t*)(msgdigest) = __REV(HASH->HR[2U]); |
NYX | 0:85b3fd62ea1a | 256 | msgdigest+=4U; |
NYX | 0:85b3fd62ea1a | 257 | *(uint32_t*)(msgdigest) = __REV(HASH->HR[3U]); |
NYX | 0:85b3fd62ea1a | 258 | break; |
NYX | 0:85b3fd62ea1a | 259 | case 20U: |
NYX | 0:85b3fd62ea1a | 260 | /* Read the message digest */ |
NYX | 0:85b3fd62ea1a | 261 | *(uint32_t*)(msgdigest) = __REV(HASH->HR[0U]); |
NYX | 0:85b3fd62ea1a | 262 | msgdigest+=4U; |
NYX | 0:85b3fd62ea1a | 263 | *(uint32_t*)(msgdigest) = __REV(HASH->HR[1U]); |
NYX | 0:85b3fd62ea1a | 264 | msgdigest+=4U; |
NYX | 0:85b3fd62ea1a | 265 | *(uint32_t*)(msgdigest) = __REV(HASH->HR[2U]); |
NYX | 0:85b3fd62ea1a | 266 | msgdigest+=4U; |
NYX | 0:85b3fd62ea1a | 267 | *(uint32_t*)(msgdigest) = __REV(HASH->HR[3U]); |
NYX | 0:85b3fd62ea1a | 268 | msgdigest+=4U; |
NYX | 0:85b3fd62ea1a | 269 | *(uint32_t*)(msgdigest) = __REV(HASH->HR[4U]); |
NYX | 0:85b3fd62ea1a | 270 | break; |
NYX | 0:85b3fd62ea1a | 271 | case 28U: |
NYX | 0:85b3fd62ea1a | 272 | /* Read the message digest */ |
NYX | 0:85b3fd62ea1a | 273 | *(uint32_t*)(msgdigest) = __REV(HASH->HR[0U]); |
NYX | 0:85b3fd62ea1a | 274 | msgdigest+=4U; |
NYX | 0:85b3fd62ea1a | 275 | *(uint32_t*)(msgdigest) = __REV(HASH->HR[1U]); |
NYX | 0:85b3fd62ea1a | 276 | msgdigest+=4U; |
NYX | 0:85b3fd62ea1a | 277 | *(uint32_t*)(msgdigest) = __REV(HASH->HR[2U]); |
NYX | 0:85b3fd62ea1a | 278 | msgdigest+=4U; |
NYX | 0:85b3fd62ea1a | 279 | *(uint32_t*)(msgdigest) = __REV(HASH->HR[3U]); |
NYX | 0:85b3fd62ea1a | 280 | msgdigest+=4U; |
NYX | 0:85b3fd62ea1a | 281 | *(uint32_t*)(msgdigest) = __REV(HASH->HR[4U]); |
NYX | 0:85b3fd62ea1a | 282 | msgdigest+=4U; |
NYX | 0:85b3fd62ea1a | 283 | *(uint32_t*)(msgdigest) = __REV(HASH_DIGEST->HR[5U]); |
NYX | 0:85b3fd62ea1a | 284 | msgdigest+=4U; |
NYX | 0:85b3fd62ea1a | 285 | *(uint32_t*)(msgdigest) = __REV(HASH_DIGEST->HR[6U]); |
NYX | 0:85b3fd62ea1a | 286 | break; |
NYX | 0:85b3fd62ea1a | 287 | case 32U: |
NYX | 0:85b3fd62ea1a | 288 | /* Read the message digest */ |
NYX | 0:85b3fd62ea1a | 289 | *(uint32_t*)(msgdigest) = __REV(HASH->HR[0U]); |
NYX | 0:85b3fd62ea1a | 290 | msgdigest+=4U; |
NYX | 0:85b3fd62ea1a | 291 | *(uint32_t*)(msgdigest) = __REV(HASH->HR[1U]); |
NYX | 0:85b3fd62ea1a | 292 | msgdigest+=4U; |
NYX | 0:85b3fd62ea1a | 293 | *(uint32_t*)(msgdigest) = __REV(HASH->HR[2U]); |
NYX | 0:85b3fd62ea1a | 294 | msgdigest+=4U; |
NYX | 0:85b3fd62ea1a | 295 | *(uint32_t*)(msgdigest) = __REV(HASH->HR[3U]); |
NYX | 0:85b3fd62ea1a | 296 | msgdigest+=4U; |
NYX | 0:85b3fd62ea1a | 297 | *(uint32_t*)(msgdigest) = __REV(HASH->HR[4U]); |
NYX | 0:85b3fd62ea1a | 298 | msgdigest+=4U; |
NYX | 0:85b3fd62ea1a | 299 | *(uint32_t*)(msgdigest) = __REV(HASH_DIGEST->HR[5U]); |
NYX | 0:85b3fd62ea1a | 300 | msgdigest+=4U; |
NYX | 0:85b3fd62ea1a | 301 | *(uint32_t*)(msgdigest) = __REV(HASH_DIGEST->HR[6U]); |
NYX | 0:85b3fd62ea1a | 302 | msgdigest+=4U; |
NYX | 0:85b3fd62ea1a | 303 | *(uint32_t*)(msgdigest) = __REV(HASH_DIGEST->HR[7U]); |
NYX | 0:85b3fd62ea1a | 304 | break; |
NYX | 0:85b3fd62ea1a | 305 | default: |
NYX | 0:85b3fd62ea1a | 306 | break; |
NYX | 0:85b3fd62ea1a | 307 | } |
NYX | 0:85b3fd62ea1a | 308 | } |
NYX | 0:85b3fd62ea1a | 309 | |
NYX | 0:85b3fd62ea1a | 310 | /** |
NYX | 0:85b3fd62ea1a | 311 | * @} |
NYX | 0:85b3fd62ea1a | 312 | */ |
NYX | 0:85b3fd62ea1a | 313 | |
NYX | 0:85b3fd62ea1a | 314 | /* Exported functions --------------------------------------------------------*/ |
NYX | 0:85b3fd62ea1a | 315 | /** @addtogroup HASH_Exported_Functions |
NYX | 0:85b3fd62ea1a | 316 | * @{ |
NYX | 0:85b3fd62ea1a | 317 | */ |
NYX | 0:85b3fd62ea1a | 318 | |
NYX | 0:85b3fd62ea1a | 319 | |
NYX | 0:85b3fd62ea1a | 320 | /** @addtogroup HASH_Exported_Functions_Group1 Initialization and de-initialization functions |
NYX | 0:85b3fd62ea1a | 321 | * @brief Initialization and Configuration functions. |
NYX | 0:85b3fd62ea1a | 322 | * |
NYX | 0:85b3fd62ea1a | 323 | @verbatim |
NYX | 0:85b3fd62ea1a | 324 | =============================================================================== |
NYX | 0:85b3fd62ea1a | 325 | ##### Initialization and de-initialization functions ##### |
NYX | 0:85b3fd62ea1a | 326 | =============================================================================== |
NYX | 0:85b3fd62ea1a | 327 | [..] This section provides functions allowing to: |
NYX | 0:85b3fd62ea1a | 328 | (+) Initialize the HASH according to the specified parameters |
NYX | 0:85b3fd62ea1a | 329 | in the HASH_InitTypeDef and creates the associated handle. |
NYX | 0:85b3fd62ea1a | 330 | (+) DeInitialize the HASH peripheral. |
NYX | 0:85b3fd62ea1a | 331 | (+) Initialize the HASH MSP. |
NYX | 0:85b3fd62ea1a | 332 | (+) DeInitialize HASH MSP. |
NYX | 0:85b3fd62ea1a | 333 | |
NYX | 0:85b3fd62ea1a | 334 | @endverbatim |
NYX | 0:85b3fd62ea1a | 335 | * @{ |
NYX | 0:85b3fd62ea1a | 336 | */ |
NYX | 0:85b3fd62ea1a | 337 | |
NYX | 0:85b3fd62ea1a | 338 | /** |
NYX | 0:85b3fd62ea1a | 339 | * @brief Initializes the HASH according to the specified parameters in the |
NYX | 0:85b3fd62ea1a | 340 | HASH_HandleTypeDef and creates the associated handle. |
NYX | 0:85b3fd62ea1a | 341 | * @param hhash: pointer to a HASH_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 342 | * the configuration information for HASH module |
NYX | 0:85b3fd62ea1a | 343 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 344 | */ |
NYX | 0:85b3fd62ea1a | 345 | HAL_StatusTypeDef HAL_HASH_Init(HASH_HandleTypeDef *hhash) |
NYX | 0:85b3fd62ea1a | 346 | { |
NYX | 0:85b3fd62ea1a | 347 | /* Check the hash handle allocation */ |
NYX | 0:85b3fd62ea1a | 348 | if(hhash == NULL) |
NYX | 0:85b3fd62ea1a | 349 | { |
NYX | 0:85b3fd62ea1a | 350 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 351 | } |
NYX | 0:85b3fd62ea1a | 352 | |
NYX | 0:85b3fd62ea1a | 353 | /* Check the parameters */ |
NYX | 0:85b3fd62ea1a | 354 | assert_param(IS_HASH_DATATYPE(hhash->Init.DataType)); |
NYX | 0:85b3fd62ea1a | 355 | |
NYX | 0:85b3fd62ea1a | 356 | if(hhash->State == HAL_HASH_STATE_RESET) |
NYX | 0:85b3fd62ea1a | 357 | { |
NYX | 0:85b3fd62ea1a | 358 | /* Allocate lock resource and initialize it */ |
NYX | 0:85b3fd62ea1a | 359 | hhash->Lock = HAL_UNLOCKED; |
NYX | 0:85b3fd62ea1a | 360 | /* Init the low level hardware */ |
NYX | 0:85b3fd62ea1a | 361 | HAL_HASH_MspInit(hhash); |
NYX | 0:85b3fd62ea1a | 362 | } |
NYX | 0:85b3fd62ea1a | 363 | |
NYX | 0:85b3fd62ea1a | 364 | /* Change the HASH state */ |
NYX | 0:85b3fd62ea1a | 365 | hhash->State = HAL_HASH_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 366 | |
NYX | 0:85b3fd62ea1a | 367 | /* Reset HashInCount, HashBuffSize and HashITCounter */ |
NYX | 0:85b3fd62ea1a | 368 | hhash->HashInCount = 0U; |
NYX | 0:85b3fd62ea1a | 369 | hhash->HashBuffSize = 0U; |
NYX | 0:85b3fd62ea1a | 370 | hhash->HashITCounter = 0U; |
NYX | 0:85b3fd62ea1a | 371 | |
NYX | 0:85b3fd62ea1a | 372 | /* Set the data type */ |
NYX | 0:85b3fd62ea1a | 373 | HASH->CR |= (uint32_t) (hhash->Init.DataType); |
NYX | 0:85b3fd62ea1a | 374 | |
NYX | 0:85b3fd62ea1a | 375 | /* Change the HASH state */ |
NYX | 0:85b3fd62ea1a | 376 | hhash->State = HAL_HASH_STATE_READY; |
NYX | 0:85b3fd62ea1a | 377 | |
NYX | 0:85b3fd62ea1a | 378 | /* Set the default HASH phase */ |
NYX | 0:85b3fd62ea1a | 379 | hhash->Phase = HAL_HASH_PHASE_READY; |
NYX | 0:85b3fd62ea1a | 380 | |
NYX | 0:85b3fd62ea1a | 381 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 382 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 383 | } |
NYX | 0:85b3fd62ea1a | 384 | |
NYX | 0:85b3fd62ea1a | 385 | /** |
NYX | 0:85b3fd62ea1a | 386 | * @brief DeInitializes the HASH peripheral. |
NYX | 0:85b3fd62ea1a | 387 | * @note This API must be called before starting a new processing. |
NYX | 0:85b3fd62ea1a | 388 | * @param hhash: pointer to a HASH_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 389 | * the configuration information for HASH module |
NYX | 0:85b3fd62ea1a | 390 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 391 | */ |
NYX | 0:85b3fd62ea1a | 392 | HAL_StatusTypeDef HAL_HASH_DeInit(HASH_HandleTypeDef *hhash) |
NYX | 0:85b3fd62ea1a | 393 | { |
NYX | 0:85b3fd62ea1a | 394 | /* Check the HASH handle allocation */ |
NYX | 0:85b3fd62ea1a | 395 | if(hhash == NULL) |
NYX | 0:85b3fd62ea1a | 396 | { |
NYX | 0:85b3fd62ea1a | 397 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 398 | } |
NYX | 0:85b3fd62ea1a | 399 | |
NYX | 0:85b3fd62ea1a | 400 | /* Change the HASH state */ |
NYX | 0:85b3fd62ea1a | 401 | hhash->State = HAL_HASH_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 402 | |
NYX | 0:85b3fd62ea1a | 403 | /* Set the default HASH phase */ |
NYX | 0:85b3fd62ea1a | 404 | hhash->Phase = HAL_HASH_PHASE_READY; |
NYX | 0:85b3fd62ea1a | 405 | |
NYX | 0:85b3fd62ea1a | 406 | /* Reset HashInCount, HashBuffSize and HashITCounter */ |
NYX | 0:85b3fd62ea1a | 407 | hhash->HashInCount = 0U; |
NYX | 0:85b3fd62ea1a | 408 | hhash->HashBuffSize = 0U; |
NYX | 0:85b3fd62ea1a | 409 | hhash->HashITCounter = 0U; |
NYX | 0:85b3fd62ea1a | 410 | |
NYX | 0:85b3fd62ea1a | 411 | /* DeInit the low level hardware */ |
NYX | 0:85b3fd62ea1a | 412 | HAL_HASH_MspDeInit(hhash); |
NYX | 0:85b3fd62ea1a | 413 | |
NYX | 0:85b3fd62ea1a | 414 | /* Change the HASH state */ |
NYX | 0:85b3fd62ea1a | 415 | hhash->State = HAL_HASH_STATE_RESET; |
NYX | 0:85b3fd62ea1a | 416 | |
NYX | 0:85b3fd62ea1a | 417 | /* Release Lock */ |
NYX | 0:85b3fd62ea1a | 418 | __HAL_UNLOCK(hhash); |
NYX | 0:85b3fd62ea1a | 419 | |
NYX | 0:85b3fd62ea1a | 420 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 421 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 422 | } |
NYX | 0:85b3fd62ea1a | 423 | |
NYX | 0:85b3fd62ea1a | 424 | /** |
NYX | 0:85b3fd62ea1a | 425 | * @brief Initializes the HASH MSP. |
NYX | 0:85b3fd62ea1a | 426 | * @param hhash: pointer to a HASH_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 427 | * the configuration information for HASH module |
NYX | 0:85b3fd62ea1a | 428 | * @retval None |
NYX | 0:85b3fd62ea1a | 429 | */ |
NYX | 0:85b3fd62ea1a | 430 | __weak void HAL_HASH_MspInit(HASH_HandleTypeDef *hhash) |
NYX | 0:85b3fd62ea1a | 431 | { |
NYX | 0:85b3fd62ea1a | 432 | /* Prevent unused argument(s) compilation warning */ |
NYX | 0:85b3fd62ea1a | 433 | UNUSED(hhash); |
NYX | 0:85b3fd62ea1a | 434 | /* NOTE: This function Should not be modified, when the callback is needed, |
NYX | 0:85b3fd62ea1a | 435 | the HAL_HASH_MspInit could be implemented in the user file |
NYX | 0:85b3fd62ea1a | 436 | */ |
NYX | 0:85b3fd62ea1a | 437 | } |
NYX | 0:85b3fd62ea1a | 438 | |
NYX | 0:85b3fd62ea1a | 439 | /** |
NYX | 0:85b3fd62ea1a | 440 | * @brief DeInitializes HASH MSP. |
NYX | 0:85b3fd62ea1a | 441 | * @param hhash: pointer to a HASH_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 442 | * the configuration information for HASH module |
NYX | 0:85b3fd62ea1a | 443 | * @retval None |
NYX | 0:85b3fd62ea1a | 444 | */ |
NYX | 0:85b3fd62ea1a | 445 | __weak void HAL_HASH_MspDeInit(HASH_HandleTypeDef *hhash) |
NYX | 0:85b3fd62ea1a | 446 | { |
NYX | 0:85b3fd62ea1a | 447 | /* Prevent unused argument(s) compilation warning */ |
NYX | 0:85b3fd62ea1a | 448 | UNUSED(hhash); |
NYX | 0:85b3fd62ea1a | 449 | /* NOTE: This function Should not be modified, when the callback is needed, |
NYX | 0:85b3fd62ea1a | 450 | the HAL_HASH_MspDeInit could be implemented in the user file |
NYX | 0:85b3fd62ea1a | 451 | */ |
NYX | 0:85b3fd62ea1a | 452 | } |
NYX | 0:85b3fd62ea1a | 453 | |
NYX | 0:85b3fd62ea1a | 454 | /** |
NYX | 0:85b3fd62ea1a | 455 | * @brief Input data transfer complete callback. |
NYX | 0:85b3fd62ea1a | 456 | * @param hhash: pointer to a HASH_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 457 | * the configuration information for HASH module |
NYX | 0:85b3fd62ea1a | 458 | * @retval None |
NYX | 0:85b3fd62ea1a | 459 | */ |
NYX | 0:85b3fd62ea1a | 460 | __weak void HAL_HASH_InCpltCallback(HASH_HandleTypeDef *hhash) |
NYX | 0:85b3fd62ea1a | 461 | { |
NYX | 0:85b3fd62ea1a | 462 | /* Prevent unused argument(s) compilation warning */ |
NYX | 0:85b3fd62ea1a | 463 | UNUSED(hhash); |
NYX | 0:85b3fd62ea1a | 464 | /* NOTE: This function Should not be modified, when the callback is needed, |
NYX | 0:85b3fd62ea1a | 465 | the HAL_HASH_InCpltCallback could be implemented in the user file |
NYX | 0:85b3fd62ea1a | 466 | */ |
NYX | 0:85b3fd62ea1a | 467 | } |
NYX | 0:85b3fd62ea1a | 468 | |
NYX | 0:85b3fd62ea1a | 469 | /** |
NYX | 0:85b3fd62ea1a | 470 | * @brief Data transfer Error callback. |
NYX | 0:85b3fd62ea1a | 471 | * @param hhash: pointer to a HASH_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 472 | * the configuration information for HASH module |
NYX | 0:85b3fd62ea1a | 473 | * @retval None |
NYX | 0:85b3fd62ea1a | 474 | */ |
NYX | 0:85b3fd62ea1a | 475 | __weak void HAL_HASH_ErrorCallback(HASH_HandleTypeDef *hhash) |
NYX | 0:85b3fd62ea1a | 476 | { |
NYX | 0:85b3fd62ea1a | 477 | /* Prevent unused argument(s) compilation warning */ |
NYX | 0:85b3fd62ea1a | 478 | UNUSED(hhash); |
NYX | 0:85b3fd62ea1a | 479 | /* NOTE: This function Should not be modified, when the callback is needed, |
NYX | 0:85b3fd62ea1a | 480 | the HAL_HASH_ErrorCallback could be implemented in the user file |
NYX | 0:85b3fd62ea1a | 481 | */ |
NYX | 0:85b3fd62ea1a | 482 | } |
NYX | 0:85b3fd62ea1a | 483 | |
NYX | 0:85b3fd62ea1a | 484 | /** |
NYX | 0:85b3fd62ea1a | 485 | * @brief Digest computation complete callback. It is used only with interrupt. |
NYX | 0:85b3fd62ea1a | 486 | * @note This callback is not relevant with DMA. |
NYX | 0:85b3fd62ea1a | 487 | * @param hhash: pointer to a HASH_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 488 | * the configuration information for HASH module |
NYX | 0:85b3fd62ea1a | 489 | * @retval None |
NYX | 0:85b3fd62ea1a | 490 | */ |
NYX | 0:85b3fd62ea1a | 491 | __weak void HAL_HASH_DgstCpltCallback(HASH_HandleTypeDef *hhash) |
NYX | 0:85b3fd62ea1a | 492 | { |
NYX | 0:85b3fd62ea1a | 493 | /* Prevent unused argument(s) compilation warning */ |
NYX | 0:85b3fd62ea1a | 494 | UNUSED(hhash); |
NYX | 0:85b3fd62ea1a | 495 | /* NOTE: This function Should not be modified, when the callback is needed, |
NYX | 0:85b3fd62ea1a | 496 | the HAL_HASH_DgstCpltCallback could be implemented in the user file |
NYX | 0:85b3fd62ea1a | 497 | */ |
NYX | 0:85b3fd62ea1a | 498 | } |
NYX | 0:85b3fd62ea1a | 499 | |
NYX | 0:85b3fd62ea1a | 500 | /** |
NYX | 0:85b3fd62ea1a | 501 | * @} |
NYX | 0:85b3fd62ea1a | 502 | */ |
NYX | 0:85b3fd62ea1a | 503 | |
NYX | 0:85b3fd62ea1a | 504 | /** @defgroup HASH_Exported_Functions_Group2 HASH processing functions using polling mode |
NYX | 0:85b3fd62ea1a | 505 | * @brief processing functions using polling mode |
NYX | 0:85b3fd62ea1a | 506 | * |
NYX | 0:85b3fd62ea1a | 507 | @verbatim |
NYX | 0:85b3fd62ea1a | 508 | =============================================================================== |
NYX | 0:85b3fd62ea1a | 509 | ##### HASH processing using polling mode functions##### |
NYX | 0:85b3fd62ea1a | 510 | =============================================================================== |
NYX | 0:85b3fd62ea1a | 511 | [..] This section provides functions allowing to calculate in polling mode |
NYX | 0:85b3fd62ea1a | 512 | the hash value using one of the following algorithms: |
NYX | 0:85b3fd62ea1a | 513 | (+) MD5 |
NYX | 0:85b3fd62ea1a | 514 | (+) SHA1 |
NYX | 0:85b3fd62ea1a | 515 | |
NYX | 0:85b3fd62ea1a | 516 | @endverbatim |
NYX | 0:85b3fd62ea1a | 517 | * @{ |
NYX | 0:85b3fd62ea1a | 518 | */ |
NYX | 0:85b3fd62ea1a | 519 | |
NYX | 0:85b3fd62ea1a | 520 | /** |
NYX | 0:85b3fd62ea1a | 521 | * @brief Initializes the HASH peripheral in MD5 mode then processes pInBuffer. |
NYX | 0:85b3fd62ea1a | 522 | The digest is available in pOutBuffer. |
NYX | 0:85b3fd62ea1a | 523 | * @param hhash: pointer to a HASH_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 524 | * the configuration information for HASH module |
NYX | 0:85b3fd62ea1a | 525 | * @param pInBuffer: Pointer to the input buffer (buffer to be hashed). |
NYX | 0:85b3fd62ea1a | 526 | * @param Size: Length of the input buffer in bytes. |
NYX | 0:85b3fd62ea1a | 527 | * If the Size is multiple of 64 bytes, appending the input buffer is possible. |
NYX | 0:85b3fd62ea1a | 528 | * If the Size is not multiple of 64 bytes, the padding is managed by hardware |
NYX | 0:85b3fd62ea1a | 529 | * and appending the input buffer is no more possible. |
NYX | 0:85b3fd62ea1a | 530 | * @param pOutBuffer: Pointer to the computed digest. Its size must be 16 bytes. |
NYX | 0:85b3fd62ea1a | 531 | * @param Timeout: Timeout value |
NYX | 0:85b3fd62ea1a | 532 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 533 | */ |
NYX | 0:85b3fd62ea1a | 534 | HAL_StatusTypeDef HAL_HASH_MD5_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout) |
NYX | 0:85b3fd62ea1a | 535 | { |
NYX | 0:85b3fd62ea1a | 536 | uint32_t tickstart = 0U; |
NYX | 0:85b3fd62ea1a | 537 | |
NYX | 0:85b3fd62ea1a | 538 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 539 | __HAL_LOCK(hhash); |
NYX | 0:85b3fd62ea1a | 540 | |
NYX | 0:85b3fd62ea1a | 541 | /* Change the HASH state */ |
NYX | 0:85b3fd62ea1a | 542 | hhash->State = HAL_HASH_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 543 | |
NYX | 0:85b3fd62ea1a | 544 | /* Check if initialization phase has already been performed */ |
NYX | 0:85b3fd62ea1a | 545 | if(hhash->Phase == HAL_HASH_PHASE_READY) |
NYX | 0:85b3fd62ea1a | 546 | { |
NYX | 0:85b3fd62ea1a | 547 | /* Select the MD5 mode and reset the HASH processor core, so that the HASH will be ready to compute |
NYX | 0:85b3fd62ea1a | 548 | the message digest of a new message */ |
NYX | 0:85b3fd62ea1a | 549 | HASH->CR |= HASH_ALGOSELECTION_MD5 | HASH_CR_INIT; |
NYX | 0:85b3fd62ea1a | 550 | } |
NYX | 0:85b3fd62ea1a | 551 | |
NYX | 0:85b3fd62ea1a | 552 | /* Set the phase */ |
NYX | 0:85b3fd62ea1a | 553 | hhash->Phase = HAL_HASH_PHASE_PROCESS; |
NYX | 0:85b3fd62ea1a | 554 | |
NYX | 0:85b3fd62ea1a | 555 | /* Configure the number of valid bits in last word of the message */ |
NYX | 0:85b3fd62ea1a | 556 | __HAL_HASH_SET_NBVALIDBITS(Size); |
NYX | 0:85b3fd62ea1a | 557 | |
NYX | 0:85b3fd62ea1a | 558 | /* Write input buffer in data register */ |
NYX | 0:85b3fd62ea1a | 559 | HASH_WriteData(pInBuffer, Size); |
NYX | 0:85b3fd62ea1a | 560 | |
NYX | 0:85b3fd62ea1a | 561 | /* Start the digest calculation */ |
NYX | 0:85b3fd62ea1a | 562 | __HAL_HASH_START_DIGEST(); |
NYX | 0:85b3fd62ea1a | 563 | |
NYX | 0:85b3fd62ea1a | 564 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 565 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 566 | |
NYX | 0:85b3fd62ea1a | 567 | while(HAL_IS_BIT_SET(HASH->SR, HASH_FLAG_BUSY)) |
NYX | 0:85b3fd62ea1a | 568 | { |
NYX | 0:85b3fd62ea1a | 569 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 570 | if(Timeout != HAL_MAX_DELAY) |
NYX | 0:85b3fd62ea1a | 571 | { |
NYX | 0:85b3fd62ea1a | 572 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
NYX | 0:85b3fd62ea1a | 573 | { |
NYX | 0:85b3fd62ea1a | 574 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 575 | hhash->State = HAL_HASH_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 576 | |
NYX | 0:85b3fd62ea1a | 577 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 578 | __HAL_UNLOCK(hhash); |
NYX | 0:85b3fd62ea1a | 579 | |
NYX | 0:85b3fd62ea1a | 580 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 581 | } |
NYX | 0:85b3fd62ea1a | 582 | } |
NYX | 0:85b3fd62ea1a | 583 | } |
NYX | 0:85b3fd62ea1a | 584 | |
NYX | 0:85b3fd62ea1a | 585 | /* Read the message digest */ |
NYX | 0:85b3fd62ea1a | 586 | HASH_GetDigest(pOutBuffer, 16U); |
NYX | 0:85b3fd62ea1a | 587 | |
NYX | 0:85b3fd62ea1a | 588 | /* Change the HASH state */ |
NYX | 0:85b3fd62ea1a | 589 | hhash->State = HAL_HASH_STATE_READY; |
NYX | 0:85b3fd62ea1a | 590 | |
NYX | 0:85b3fd62ea1a | 591 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 592 | __HAL_UNLOCK(hhash); |
NYX | 0:85b3fd62ea1a | 593 | |
NYX | 0:85b3fd62ea1a | 594 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 595 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 596 | } |
NYX | 0:85b3fd62ea1a | 597 | |
NYX | 0:85b3fd62ea1a | 598 | /** |
NYX | 0:85b3fd62ea1a | 599 | * @brief Initializes the HASH peripheral in MD5 mode then writes the pInBuffer. |
NYX | 0:85b3fd62ea1a | 600 | * @param hhash: pointer to a HASH_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 601 | * the configuration information for HASH module |
NYX | 0:85b3fd62ea1a | 602 | * @param pInBuffer: Pointer to the input buffer (buffer to be hashed). |
NYX | 0:85b3fd62ea1a | 603 | * @param Size: Length of the input buffer in bytes. |
NYX | 0:85b3fd62ea1a | 604 | * If the Size is multiple of 64 bytes, appending the input buffer is possible. |
NYX | 0:85b3fd62ea1a | 605 | * If the Size is not multiple of 64 bytes, the padding is managed by hardware |
NYX | 0:85b3fd62ea1a | 606 | * and appending the input buffer is no more possible. |
NYX | 0:85b3fd62ea1a | 607 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 608 | */ |
NYX | 0:85b3fd62ea1a | 609 | HAL_StatusTypeDef HAL_HASH_MD5_Accumulate(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size) |
NYX | 0:85b3fd62ea1a | 610 | { |
NYX | 0:85b3fd62ea1a | 611 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 612 | __HAL_LOCK(hhash); |
NYX | 0:85b3fd62ea1a | 613 | |
NYX | 0:85b3fd62ea1a | 614 | /* Change the HASH state */ |
NYX | 0:85b3fd62ea1a | 615 | hhash->State = HAL_HASH_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 616 | |
NYX | 0:85b3fd62ea1a | 617 | /* Check if initialization phase has already been performed */ |
NYX | 0:85b3fd62ea1a | 618 | if(hhash->Phase == HAL_HASH_PHASE_READY) |
NYX | 0:85b3fd62ea1a | 619 | { |
NYX | 0:85b3fd62ea1a | 620 | /* Select the MD5 mode and reset the HASH processor core, so that the HASH will be ready to compute |
NYX | 0:85b3fd62ea1a | 621 | the message digest of a new message */ |
NYX | 0:85b3fd62ea1a | 622 | HASH->CR |= HASH_ALGOSELECTION_MD5 | HASH_CR_INIT; |
NYX | 0:85b3fd62ea1a | 623 | } |
NYX | 0:85b3fd62ea1a | 624 | |
NYX | 0:85b3fd62ea1a | 625 | /* Set the phase */ |
NYX | 0:85b3fd62ea1a | 626 | hhash->Phase = HAL_HASH_PHASE_PROCESS; |
NYX | 0:85b3fd62ea1a | 627 | |
NYX | 0:85b3fd62ea1a | 628 | /* Configure the number of valid bits in last word of the message */ |
NYX | 0:85b3fd62ea1a | 629 | __HAL_HASH_SET_NBVALIDBITS(Size); |
NYX | 0:85b3fd62ea1a | 630 | |
NYX | 0:85b3fd62ea1a | 631 | /* Write input buffer in data register */ |
NYX | 0:85b3fd62ea1a | 632 | HASH_WriteData(pInBuffer, Size); |
NYX | 0:85b3fd62ea1a | 633 | |
NYX | 0:85b3fd62ea1a | 634 | /* Change the HASH state */ |
NYX | 0:85b3fd62ea1a | 635 | hhash->State = HAL_HASH_STATE_READY; |
NYX | 0:85b3fd62ea1a | 636 | |
NYX | 0:85b3fd62ea1a | 637 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 638 | __HAL_UNLOCK(hhash); |
NYX | 0:85b3fd62ea1a | 639 | |
NYX | 0:85b3fd62ea1a | 640 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 641 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 642 | } |
NYX | 0:85b3fd62ea1a | 643 | |
NYX | 0:85b3fd62ea1a | 644 | /** |
NYX | 0:85b3fd62ea1a | 645 | * @brief Initializes the HASH peripheral in SHA1 mode then processes pInBuffer. |
NYX | 0:85b3fd62ea1a | 646 | The digest is available in pOutBuffer. |
NYX | 0:85b3fd62ea1a | 647 | * @param hhash: pointer to a HASH_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 648 | * the configuration information for HASH module |
NYX | 0:85b3fd62ea1a | 649 | * @param pInBuffer: Pointer to the input buffer (buffer to be hashed). |
NYX | 0:85b3fd62ea1a | 650 | * @param Size: Length of the input buffer in bytes. |
NYX | 0:85b3fd62ea1a | 651 | * If the Size is not multiple of 64 bytes, the padding is managed by hardware. |
NYX | 0:85b3fd62ea1a | 652 | * @param pOutBuffer: Pointer to the computed digest. Its size must be 20 bytes. |
NYX | 0:85b3fd62ea1a | 653 | * @param Timeout: Timeout value |
NYX | 0:85b3fd62ea1a | 654 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 655 | */ |
NYX | 0:85b3fd62ea1a | 656 | HAL_StatusTypeDef HAL_HASH_SHA1_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout) |
NYX | 0:85b3fd62ea1a | 657 | { |
NYX | 0:85b3fd62ea1a | 658 | uint32_t tickstart = 0U; |
NYX | 0:85b3fd62ea1a | 659 | |
NYX | 0:85b3fd62ea1a | 660 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 661 | __HAL_LOCK(hhash); |
NYX | 0:85b3fd62ea1a | 662 | |
NYX | 0:85b3fd62ea1a | 663 | /* Change the HASH state */ |
NYX | 0:85b3fd62ea1a | 664 | hhash->State = HAL_HASH_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 665 | |
NYX | 0:85b3fd62ea1a | 666 | /* Check if initialization phase has already been performed */ |
NYX | 0:85b3fd62ea1a | 667 | if(hhash->Phase == HAL_HASH_PHASE_READY) |
NYX | 0:85b3fd62ea1a | 668 | { |
NYX | 0:85b3fd62ea1a | 669 | /* Select the SHA1 mode and reset the HASH processor core, so that the HASH will be ready to compute |
NYX | 0:85b3fd62ea1a | 670 | the message digest of a new message */ |
NYX | 0:85b3fd62ea1a | 671 | HASH->CR |= HASH_ALGOSELECTION_SHA1 | HASH_CR_INIT; |
NYX | 0:85b3fd62ea1a | 672 | } |
NYX | 0:85b3fd62ea1a | 673 | |
NYX | 0:85b3fd62ea1a | 674 | /* Set the phase */ |
NYX | 0:85b3fd62ea1a | 675 | hhash->Phase = HAL_HASH_PHASE_PROCESS; |
NYX | 0:85b3fd62ea1a | 676 | |
NYX | 0:85b3fd62ea1a | 677 | /* Configure the number of valid bits in last word of the message */ |
NYX | 0:85b3fd62ea1a | 678 | __HAL_HASH_SET_NBVALIDBITS(Size); |
NYX | 0:85b3fd62ea1a | 679 | |
NYX | 0:85b3fd62ea1a | 680 | /* Write input buffer in data register */ |
NYX | 0:85b3fd62ea1a | 681 | HASH_WriteData(pInBuffer, Size); |
NYX | 0:85b3fd62ea1a | 682 | |
NYX | 0:85b3fd62ea1a | 683 | /* Start the digest calculation */ |
NYX | 0:85b3fd62ea1a | 684 | __HAL_HASH_START_DIGEST(); |
NYX | 0:85b3fd62ea1a | 685 | |
NYX | 0:85b3fd62ea1a | 686 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 687 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 688 | |
NYX | 0:85b3fd62ea1a | 689 | while(HAL_IS_BIT_SET(HASH->SR, HASH_FLAG_BUSY)) |
NYX | 0:85b3fd62ea1a | 690 | { |
NYX | 0:85b3fd62ea1a | 691 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 692 | if(Timeout != HAL_MAX_DELAY) |
NYX | 0:85b3fd62ea1a | 693 | { |
NYX | 0:85b3fd62ea1a | 694 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
NYX | 0:85b3fd62ea1a | 695 | { |
NYX | 0:85b3fd62ea1a | 696 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 697 | hhash->State = HAL_HASH_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 698 | |
NYX | 0:85b3fd62ea1a | 699 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 700 | __HAL_UNLOCK(hhash); |
NYX | 0:85b3fd62ea1a | 701 | |
NYX | 0:85b3fd62ea1a | 702 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 703 | } |
NYX | 0:85b3fd62ea1a | 704 | } |
NYX | 0:85b3fd62ea1a | 705 | } |
NYX | 0:85b3fd62ea1a | 706 | |
NYX | 0:85b3fd62ea1a | 707 | /* Read the message digest */ |
NYX | 0:85b3fd62ea1a | 708 | HASH_GetDigest(pOutBuffer, 20U); |
NYX | 0:85b3fd62ea1a | 709 | |
NYX | 0:85b3fd62ea1a | 710 | /* Change the HASH state */ |
NYX | 0:85b3fd62ea1a | 711 | hhash->State = HAL_HASH_STATE_READY; |
NYX | 0:85b3fd62ea1a | 712 | |
NYX | 0:85b3fd62ea1a | 713 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 714 | __HAL_UNLOCK(hhash); |
NYX | 0:85b3fd62ea1a | 715 | |
NYX | 0:85b3fd62ea1a | 716 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 717 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 718 | } |
NYX | 0:85b3fd62ea1a | 719 | |
NYX | 0:85b3fd62ea1a | 720 | /** |
NYX | 0:85b3fd62ea1a | 721 | * @brief Initializes the HASH peripheral in SHA1 mode then processes pInBuffer. |
NYX | 0:85b3fd62ea1a | 722 | * @param hhash: pointer to a HASH_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 723 | * the configuration information for HASH module |
NYX | 0:85b3fd62ea1a | 724 | * @param pInBuffer: Pointer to the input buffer (buffer to be hashed). |
NYX | 0:85b3fd62ea1a | 725 | * @param Size: Length of the input buffer in bytes. |
NYX | 0:85b3fd62ea1a | 726 | * If the Size is not multiple of 64 bytes, the padding is managed by hardware. |
NYX | 0:85b3fd62ea1a | 727 | * @note Input buffer size in bytes must be a multiple of 4 otherwise the digest computation is corrupted. |
NYX | 0:85b3fd62ea1a | 728 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 729 | */ |
NYX | 0:85b3fd62ea1a | 730 | HAL_StatusTypeDef HAL_HASH_SHA1_Accumulate(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size) |
NYX | 0:85b3fd62ea1a | 731 | { |
NYX | 0:85b3fd62ea1a | 732 | /* Check the parameters */ |
NYX | 0:85b3fd62ea1a | 733 | assert_param(IS_HASH_SHA1_BUFFER_SIZE(Size)); |
NYX | 0:85b3fd62ea1a | 734 | |
NYX | 0:85b3fd62ea1a | 735 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 736 | __HAL_LOCK(hhash); |
NYX | 0:85b3fd62ea1a | 737 | |
NYX | 0:85b3fd62ea1a | 738 | /* Change the HASH state */ |
NYX | 0:85b3fd62ea1a | 739 | hhash->State = HAL_HASH_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 740 | |
NYX | 0:85b3fd62ea1a | 741 | /* Check if initialization phase has already been performed */ |
NYX | 0:85b3fd62ea1a | 742 | if(hhash->Phase == HAL_HASH_PHASE_READY) |
NYX | 0:85b3fd62ea1a | 743 | { |
NYX | 0:85b3fd62ea1a | 744 | /* Select the SHA1 mode and reset the HASH processor core, so that the HASH will be ready to compute |
NYX | 0:85b3fd62ea1a | 745 | the message digest of a new message */ |
NYX | 0:85b3fd62ea1a | 746 | HASH->CR |= HASH_ALGOSELECTION_SHA1 | HASH_CR_INIT; |
NYX | 0:85b3fd62ea1a | 747 | } |
NYX | 0:85b3fd62ea1a | 748 | |
NYX | 0:85b3fd62ea1a | 749 | /* Set the phase */ |
NYX | 0:85b3fd62ea1a | 750 | hhash->Phase = HAL_HASH_PHASE_PROCESS; |
NYX | 0:85b3fd62ea1a | 751 | |
NYX | 0:85b3fd62ea1a | 752 | /* Configure the number of valid bits in last word of the message */ |
NYX | 0:85b3fd62ea1a | 753 | __HAL_HASH_SET_NBVALIDBITS(Size); |
NYX | 0:85b3fd62ea1a | 754 | |
NYX | 0:85b3fd62ea1a | 755 | /* Write input buffer in data register */ |
NYX | 0:85b3fd62ea1a | 756 | HASH_WriteData(pInBuffer, Size); |
NYX | 0:85b3fd62ea1a | 757 | |
NYX | 0:85b3fd62ea1a | 758 | /* Change the HASH state */ |
NYX | 0:85b3fd62ea1a | 759 | hhash->State = HAL_HASH_STATE_READY; |
NYX | 0:85b3fd62ea1a | 760 | |
NYX | 0:85b3fd62ea1a | 761 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 762 | __HAL_UNLOCK(hhash); |
NYX | 0:85b3fd62ea1a | 763 | |
NYX | 0:85b3fd62ea1a | 764 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 765 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 766 | } |
NYX | 0:85b3fd62ea1a | 767 | |
NYX | 0:85b3fd62ea1a | 768 | /** |
NYX | 0:85b3fd62ea1a | 769 | * @} |
NYX | 0:85b3fd62ea1a | 770 | */ |
NYX | 0:85b3fd62ea1a | 771 | |
NYX | 0:85b3fd62ea1a | 772 | /** @defgroup HASH_Exported_Functions_Group3 HASH processing functions using interrupt mode |
NYX | 0:85b3fd62ea1a | 773 | * @brief processing functions using interrupt mode. |
NYX | 0:85b3fd62ea1a | 774 | * |
NYX | 0:85b3fd62ea1a | 775 | @verbatim |
NYX | 0:85b3fd62ea1a | 776 | =============================================================================== |
NYX | 0:85b3fd62ea1a | 777 | ##### HASH processing using interrupt mode functions ##### |
NYX | 0:85b3fd62ea1a | 778 | =============================================================================== |
NYX | 0:85b3fd62ea1a | 779 | [..] This section provides functions allowing to calculate in interrupt mode |
NYX | 0:85b3fd62ea1a | 780 | the hash value using one of the following algorithms: |
NYX | 0:85b3fd62ea1a | 781 | (+) MD5 |
NYX | 0:85b3fd62ea1a | 782 | (+) SHA1 |
NYX | 0:85b3fd62ea1a | 783 | |
NYX | 0:85b3fd62ea1a | 784 | @endverbatim |
NYX | 0:85b3fd62ea1a | 785 | * @{ |
NYX | 0:85b3fd62ea1a | 786 | */ |
NYX | 0:85b3fd62ea1a | 787 | |
NYX | 0:85b3fd62ea1a | 788 | /** |
NYX | 0:85b3fd62ea1a | 789 | * @brief Initializes the HASH peripheral in MD5 mode then processes pInBuffer. |
NYX | 0:85b3fd62ea1a | 790 | * The digest is available in pOutBuffer. |
NYX | 0:85b3fd62ea1a | 791 | * @param hhash: pointer to a HASH_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 792 | * the configuration information for HASH module |
NYX | 0:85b3fd62ea1a | 793 | * @param pInBuffer: Pointer to the input buffer (buffer to be hashed). |
NYX | 0:85b3fd62ea1a | 794 | * @param Size: Length of the input buffer in bytes. |
NYX | 0:85b3fd62ea1a | 795 | * If the Size is not multiple of 64 bytes, the padding is managed by hardware. |
NYX | 0:85b3fd62ea1a | 796 | * @param pOutBuffer: Pointer to the computed digest. Its size must be 16 bytes. |
NYX | 0:85b3fd62ea1a | 797 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 798 | */ |
NYX | 0:85b3fd62ea1a | 799 | HAL_StatusTypeDef HAL_HASH_MD5_Start_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer) |
NYX | 0:85b3fd62ea1a | 800 | { |
NYX | 0:85b3fd62ea1a | 801 | uint32_t inputaddr; |
NYX | 0:85b3fd62ea1a | 802 | uint32_t outputaddr; |
NYX | 0:85b3fd62ea1a | 803 | uint32_t buffercounter; |
NYX | 0:85b3fd62ea1a | 804 | uint32_t inputcounter; |
NYX | 0:85b3fd62ea1a | 805 | |
NYX | 0:85b3fd62ea1a | 806 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 807 | __HAL_LOCK(hhash); |
NYX | 0:85b3fd62ea1a | 808 | |
NYX | 0:85b3fd62ea1a | 809 | if(hhash->State == HAL_HASH_STATE_READY) |
NYX | 0:85b3fd62ea1a | 810 | { |
NYX | 0:85b3fd62ea1a | 811 | /* Change the HASH state */ |
NYX | 0:85b3fd62ea1a | 812 | hhash->State = HAL_HASH_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 813 | |
NYX | 0:85b3fd62ea1a | 814 | hhash->HashInCount = Size; |
NYX | 0:85b3fd62ea1a | 815 | hhash->pHashInBuffPtr = pInBuffer; |
NYX | 0:85b3fd62ea1a | 816 | hhash->pHashOutBuffPtr = pOutBuffer; |
NYX | 0:85b3fd62ea1a | 817 | |
NYX | 0:85b3fd62ea1a | 818 | /* Check if initialization phase has already been performed */ |
NYX | 0:85b3fd62ea1a | 819 | if(hhash->Phase == HAL_HASH_PHASE_READY) |
NYX | 0:85b3fd62ea1a | 820 | { |
NYX | 0:85b3fd62ea1a | 821 | /* Select the SHA1 mode */ |
NYX | 0:85b3fd62ea1a | 822 | HASH->CR |= HASH_ALGOSELECTION_MD5; |
NYX | 0:85b3fd62ea1a | 823 | /* Reset the HASH processor core, so that the HASH will be ready to compute |
NYX | 0:85b3fd62ea1a | 824 | the message digest of a new message */ |
NYX | 0:85b3fd62ea1a | 825 | HASH->CR |= HASH_CR_INIT; |
NYX | 0:85b3fd62ea1a | 826 | } |
NYX | 0:85b3fd62ea1a | 827 | /* Reset interrupt counter */ |
NYX | 0:85b3fd62ea1a | 828 | hhash->HashITCounter = 0U; |
NYX | 0:85b3fd62ea1a | 829 | |
NYX | 0:85b3fd62ea1a | 830 | /* Set the phase */ |
NYX | 0:85b3fd62ea1a | 831 | hhash->Phase = HAL_HASH_PHASE_PROCESS; |
NYX | 0:85b3fd62ea1a | 832 | |
NYX | 0:85b3fd62ea1a | 833 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 834 | __HAL_UNLOCK(hhash); |
NYX | 0:85b3fd62ea1a | 835 | |
NYX | 0:85b3fd62ea1a | 836 | /* Enable Interrupts */ |
NYX | 0:85b3fd62ea1a | 837 | HASH->IMR = (HASH_IT_DINI | HASH_IT_DCI); |
NYX | 0:85b3fd62ea1a | 838 | |
NYX | 0:85b3fd62ea1a | 839 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 840 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 841 | } |
NYX | 0:85b3fd62ea1a | 842 | if(__HAL_HASH_GET_FLAG(HASH_FLAG_DCIS)) |
NYX | 0:85b3fd62ea1a | 843 | { |
NYX | 0:85b3fd62ea1a | 844 | outputaddr = (uint32_t)hhash->pHashOutBuffPtr; |
NYX | 0:85b3fd62ea1a | 845 | /* Read the Output block from the Output FIFO */ |
NYX | 0:85b3fd62ea1a | 846 | *(uint32_t*)(outputaddr) = __REV(HASH->HR[0U]); |
NYX | 0:85b3fd62ea1a | 847 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 848 | *(uint32_t*)(outputaddr) = __REV(HASH->HR[1U]); |
NYX | 0:85b3fd62ea1a | 849 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 850 | *(uint32_t*)(outputaddr) = __REV(HASH->HR[2U]); |
NYX | 0:85b3fd62ea1a | 851 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 852 | *(uint32_t*)(outputaddr) = __REV(HASH->HR[3U]); |
NYX | 0:85b3fd62ea1a | 853 | |
NYX | 0:85b3fd62ea1a | 854 | if(hhash->HashInCount == 0U) |
NYX | 0:85b3fd62ea1a | 855 | { |
NYX | 0:85b3fd62ea1a | 856 | /* Disable Interrupts */ |
NYX | 0:85b3fd62ea1a | 857 | HASH->IMR = 0U; |
NYX | 0:85b3fd62ea1a | 858 | /* Change the HASH state */ |
NYX | 0:85b3fd62ea1a | 859 | hhash->State = HAL_HASH_STATE_READY; |
NYX | 0:85b3fd62ea1a | 860 | /* Call digest computation complete callback */ |
NYX | 0:85b3fd62ea1a | 861 | HAL_HASH_DgstCpltCallback(hhash); |
NYX | 0:85b3fd62ea1a | 862 | |
NYX | 0:85b3fd62ea1a | 863 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 864 | __HAL_UNLOCK(hhash); |
NYX | 0:85b3fd62ea1a | 865 | |
NYX | 0:85b3fd62ea1a | 866 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 867 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 868 | } |
NYX | 0:85b3fd62ea1a | 869 | } |
NYX | 0:85b3fd62ea1a | 870 | if(__HAL_HASH_GET_FLAG(HASH_FLAG_DINIS)) |
NYX | 0:85b3fd62ea1a | 871 | { |
NYX | 0:85b3fd62ea1a | 872 | if(hhash->HashInCount >= 68U) |
NYX | 0:85b3fd62ea1a | 873 | { |
NYX | 0:85b3fd62ea1a | 874 | inputaddr = (uint32_t)hhash->pHashInBuffPtr; |
NYX | 0:85b3fd62ea1a | 875 | /* Write the Input block in the Data IN register */ |
NYX | 0:85b3fd62ea1a | 876 | for(buffercounter = 0U; buffercounter < 64U; buffercounter+=4U) |
NYX | 0:85b3fd62ea1a | 877 | { |
NYX | 0:85b3fd62ea1a | 878 | HASH->DIN = *(uint32_t*)inputaddr; |
NYX | 0:85b3fd62ea1a | 879 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 880 | } |
NYX | 0:85b3fd62ea1a | 881 | if(hhash->HashITCounter == 0U) |
NYX | 0:85b3fd62ea1a | 882 | { |
NYX | 0:85b3fd62ea1a | 883 | HASH->DIN = *(uint32_t*)inputaddr; |
NYX | 0:85b3fd62ea1a | 884 | |
NYX | 0:85b3fd62ea1a | 885 | if(hhash->HashInCount >= 68U) |
NYX | 0:85b3fd62ea1a | 886 | { |
NYX | 0:85b3fd62ea1a | 887 | /* Decrement buffer counter */ |
NYX | 0:85b3fd62ea1a | 888 | hhash->HashInCount -= 68U; |
NYX | 0:85b3fd62ea1a | 889 | hhash->pHashInBuffPtr+= 68U; |
NYX | 0:85b3fd62ea1a | 890 | } |
NYX | 0:85b3fd62ea1a | 891 | else |
NYX | 0:85b3fd62ea1a | 892 | { |
NYX | 0:85b3fd62ea1a | 893 | hhash->HashInCount = 0U; |
NYX | 0:85b3fd62ea1a | 894 | hhash->pHashInBuffPtr+= hhash->HashInCount; |
NYX | 0:85b3fd62ea1a | 895 | } |
NYX | 0:85b3fd62ea1a | 896 | /* Set Interrupt counter */ |
NYX | 0:85b3fd62ea1a | 897 | hhash->HashITCounter = 1U; |
NYX | 0:85b3fd62ea1a | 898 | } |
NYX | 0:85b3fd62ea1a | 899 | else |
NYX | 0:85b3fd62ea1a | 900 | { |
NYX | 0:85b3fd62ea1a | 901 | /* Decrement buffer counter */ |
NYX | 0:85b3fd62ea1a | 902 | hhash->HashInCount -= 64U; |
NYX | 0:85b3fd62ea1a | 903 | hhash->pHashInBuffPtr+= 64U; |
NYX | 0:85b3fd62ea1a | 904 | } |
NYX | 0:85b3fd62ea1a | 905 | } |
NYX | 0:85b3fd62ea1a | 906 | else |
NYX | 0:85b3fd62ea1a | 907 | { |
NYX | 0:85b3fd62ea1a | 908 | /* Get the buffer address */ |
NYX | 0:85b3fd62ea1a | 909 | inputaddr = (uint32_t)hhash->pHashInBuffPtr; |
NYX | 0:85b3fd62ea1a | 910 | /* Get the buffer counter */ |
NYX | 0:85b3fd62ea1a | 911 | inputcounter = hhash->HashInCount; |
NYX | 0:85b3fd62ea1a | 912 | /* Disable Interrupts */ |
NYX | 0:85b3fd62ea1a | 913 | HASH->IMR &= ~(HASH_IT_DINI); |
NYX | 0:85b3fd62ea1a | 914 | /* Configure the number of valid bits in last word of the message */ |
NYX | 0:85b3fd62ea1a | 915 | __HAL_HASH_SET_NBVALIDBITS(inputcounter); |
NYX | 0:85b3fd62ea1a | 916 | |
NYX | 0:85b3fd62ea1a | 917 | if((inputcounter > 4U) && (inputcounter%4U)) |
NYX | 0:85b3fd62ea1a | 918 | { |
NYX | 0:85b3fd62ea1a | 919 | inputcounter = (inputcounter+4U-inputcounter%4U); |
NYX | 0:85b3fd62ea1a | 920 | } |
NYX | 0:85b3fd62ea1a | 921 | else if ((inputcounter < 4U) && (inputcounter != 0U)) |
NYX | 0:85b3fd62ea1a | 922 | { |
NYX | 0:85b3fd62ea1a | 923 | inputcounter = 4U; |
NYX | 0:85b3fd62ea1a | 924 | } |
NYX | 0:85b3fd62ea1a | 925 | /* Write the Input block in the Data IN register */ |
NYX | 0:85b3fd62ea1a | 926 | for(buffercounter = 0U; buffercounter < inputcounter/4U; buffercounter++) |
NYX | 0:85b3fd62ea1a | 927 | { |
NYX | 0:85b3fd62ea1a | 928 | HASH->DIN = *(uint32_t*)inputaddr; |
NYX | 0:85b3fd62ea1a | 929 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 930 | } |
NYX | 0:85b3fd62ea1a | 931 | /* Start the digest calculation */ |
NYX | 0:85b3fd62ea1a | 932 | __HAL_HASH_START_DIGEST(); |
NYX | 0:85b3fd62ea1a | 933 | /* Reset buffer counter */ |
NYX | 0:85b3fd62ea1a | 934 | hhash->HashInCount = 0U; |
NYX | 0:85b3fd62ea1a | 935 | /* Call Input data transfer complete callback */ |
NYX | 0:85b3fd62ea1a | 936 | HAL_HASH_InCpltCallback(hhash); |
NYX | 0:85b3fd62ea1a | 937 | } |
NYX | 0:85b3fd62ea1a | 938 | } |
NYX | 0:85b3fd62ea1a | 939 | |
NYX | 0:85b3fd62ea1a | 940 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 941 | __HAL_UNLOCK(hhash); |
NYX | 0:85b3fd62ea1a | 942 | |
NYX | 0:85b3fd62ea1a | 943 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 944 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 945 | } |
NYX | 0:85b3fd62ea1a | 946 | |
NYX | 0:85b3fd62ea1a | 947 | /** |
NYX | 0:85b3fd62ea1a | 948 | * @brief Initializes the HASH peripheral in SHA1 mode then processes pInBuffer. |
NYX | 0:85b3fd62ea1a | 949 | * The digest is available in pOutBuffer. |
NYX | 0:85b3fd62ea1a | 950 | * @param hhash: pointer to a HASH_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 951 | * the configuration information for HASH module |
NYX | 0:85b3fd62ea1a | 952 | * @param pInBuffer: Pointer to the input buffer (buffer to be hashed). |
NYX | 0:85b3fd62ea1a | 953 | * @param Size: Length of the input buffer in bytes. |
NYX | 0:85b3fd62ea1a | 954 | * If the Size is not multiple of 64 bytes, the padding is managed by hardware. |
NYX | 0:85b3fd62ea1a | 955 | * @param pOutBuffer: Pointer to the computed digest. Its size must be 20 bytes. |
NYX | 0:85b3fd62ea1a | 956 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 957 | */ |
NYX | 0:85b3fd62ea1a | 958 | HAL_StatusTypeDef HAL_HASH_SHA1_Start_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer) |
NYX | 0:85b3fd62ea1a | 959 | { |
NYX | 0:85b3fd62ea1a | 960 | uint32_t inputaddr; |
NYX | 0:85b3fd62ea1a | 961 | uint32_t outputaddr; |
NYX | 0:85b3fd62ea1a | 962 | uint32_t buffercounter; |
NYX | 0:85b3fd62ea1a | 963 | uint32_t inputcounter; |
NYX | 0:85b3fd62ea1a | 964 | |
NYX | 0:85b3fd62ea1a | 965 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 966 | __HAL_LOCK(hhash); |
NYX | 0:85b3fd62ea1a | 967 | |
NYX | 0:85b3fd62ea1a | 968 | if(hhash->State == HAL_HASH_STATE_READY) |
NYX | 0:85b3fd62ea1a | 969 | { |
NYX | 0:85b3fd62ea1a | 970 | /* Change the HASH state */ |
NYX | 0:85b3fd62ea1a | 971 | hhash->State = HAL_HASH_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 972 | |
NYX | 0:85b3fd62ea1a | 973 | hhash->HashInCount = Size; |
NYX | 0:85b3fd62ea1a | 974 | hhash->pHashInBuffPtr = pInBuffer; |
NYX | 0:85b3fd62ea1a | 975 | hhash->pHashOutBuffPtr = pOutBuffer; |
NYX | 0:85b3fd62ea1a | 976 | |
NYX | 0:85b3fd62ea1a | 977 | /* Check if initialization phase has already been performed */ |
NYX | 0:85b3fd62ea1a | 978 | if(hhash->Phase == HAL_HASH_PHASE_READY) |
NYX | 0:85b3fd62ea1a | 979 | { |
NYX | 0:85b3fd62ea1a | 980 | /* Select the SHA1 mode */ |
NYX | 0:85b3fd62ea1a | 981 | HASH->CR |= HASH_ALGOSELECTION_SHA1; |
NYX | 0:85b3fd62ea1a | 982 | /* Reset the HASH processor core, so that the HASH will be ready to compute |
NYX | 0:85b3fd62ea1a | 983 | the message digest of a new message */ |
NYX | 0:85b3fd62ea1a | 984 | HASH->CR |= HASH_CR_INIT; |
NYX | 0:85b3fd62ea1a | 985 | } |
NYX | 0:85b3fd62ea1a | 986 | /* Reset interrupt counter */ |
NYX | 0:85b3fd62ea1a | 987 | hhash->HashITCounter = 0U; |
NYX | 0:85b3fd62ea1a | 988 | |
NYX | 0:85b3fd62ea1a | 989 | /* Set the phase */ |
NYX | 0:85b3fd62ea1a | 990 | hhash->Phase = HAL_HASH_PHASE_PROCESS; |
NYX | 0:85b3fd62ea1a | 991 | |
NYX | 0:85b3fd62ea1a | 992 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 993 | __HAL_UNLOCK(hhash); |
NYX | 0:85b3fd62ea1a | 994 | |
NYX | 0:85b3fd62ea1a | 995 | /* Enable Interrupts */ |
NYX | 0:85b3fd62ea1a | 996 | HASH->IMR = (HASH_IT_DINI | HASH_IT_DCI); |
NYX | 0:85b3fd62ea1a | 997 | |
NYX | 0:85b3fd62ea1a | 998 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 999 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 1000 | } |
NYX | 0:85b3fd62ea1a | 1001 | if(__HAL_HASH_GET_FLAG(HASH_FLAG_DCIS)) |
NYX | 0:85b3fd62ea1a | 1002 | { |
NYX | 0:85b3fd62ea1a | 1003 | outputaddr = (uint32_t)hhash->pHashOutBuffPtr; |
NYX | 0:85b3fd62ea1a | 1004 | /* Read the Output block from the Output FIFO */ |
NYX | 0:85b3fd62ea1a | 1005 | *(uint32_t*)(outputaddr) = __REV(HASH->HR[0U]); |
NYX | 0:85b3fd62ea1a | 1006 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 1007 | *(uint32_t*)(outputaddr) = __REV(HASH->HR[1U]); |
NYX | 0:85b3fd62ea1a | 1008 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 1009 | *(uint32_t*)(outputaddr) = __REV(HASH->HR[2U]); |
NYX | 0:85b3fd62ea1a | 1010 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 1011 | *(uint32_t*)(outputaddr) = __REV(HASH->HR[3U]); |
NYX | 0:85b3fd62ea1a | 1012 | outputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 1013 | *(uint32_t*)(outputaddr) = __REV(HASH->HR[4U]); |
NYX | 0:85b3fd62ea1a | 1014 | if(hhash->HashInCount == 0U) |
NYX | 0:85b3fd62ea1a | 1015 | { |
NYX | 0:85b3fd62ea1a | 1016 | /* Disable Interrupts */ |
NYX | 0:85b3fd62ea1a | 1017 | HASH->IMR = 0U; |
NYX | 0:85b3fd62ea1a | 1018 | /* Change the HASH state */ |
NYX | 0:85b3fd62ea1a | 1019 | hhash->State = HAL_HASH_STATE_READY; |
NYX | 0:85b3fd62ea1a | 1020 | /* Call digest computation complete callback */ |
NYX | 0:85b3fd62ea1a | 1021 | HAL_HASH_DgstCpltCallback(hhash); |
NYX | 0:85b3fd62ea1a | 1022 | |
NYX | 0:85b3fd62ea1a | 1023 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1024 | __HAL_UNLOCK(hhash); |
NYX | 0:85b3fd62ea1a | 1025 | |
NYX | 0:85b3fd62ea1a | 1026 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 1027 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 1028 | } |
NYX | 0:85b3fd62ea1a | 1029 | } |
NYX | 0:85b3fd62ea1a | 1030 | if(__HAL_HASH_GET_FLAG(HASH_FLAG_DINIS)) |
NYX | 0:85b3fd62ea1a | 1031 | { |
NYX | 0:85b3fd62ea1a | 1032 | if(hhash->HashInCount >= 68U) |
NYX | 0:85b3fd62ea1a | 1033 | { |
NYX | 0:85b3fd62ea1a | 1034 | inputaddr = (uint32_t)hhash->pHashInBuffPtr; |
NYX | 0:85b3fd62ea1a | 1035 | /* Write the Input block in the Data IN register */ |
NYX | 0:85b3fd62ea1a | 1036 | for(buffercounter = 0U; buffercounter < 64U; buffercounter+=4U) |
NYX | 0:85b3fd62ea1a | 1037 | { |
NYX | 0:85b3fd62ea1a | 1038 | HASH->DIN = *(uint32_t*)inputaddr; |
NYX | 0:85b3fd62ea1a | 1039 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 1040 | } |
NYX | 0:85b3fd62ea1a | 1041 | if(hhash->HashITCounter == 0U) |
NYX | 0:85b3fd62ea1a | 1042 | { |
NYX | 0:85b3fd62ea1a | 1043 | HASH->DIN = *(uint32_t*)inputaddr; |
NYX | 0:85b3fd62ea1a | 1044 | if(hhash->HashInCount >= 68U) |
NYX | 0:85b3fd62ea1a | 1045 | { |
NYX | 0:85b3fd62ea1a | 1046 | /* Decrement buffer counter */ |
NYX | 0:85b3fd62ea1a | 1047 | hhash->HashInCount -= 68U; |
NYX | 0:85b3fd62ea1a | 1048 | hhash->pHashInBuffPtr+= 68U; |
NYX | 0:85b3fd62ea1a | 1049 | } |
NYX | 0:85b3fd62ea1a | 1050 | else |
NYX | 0:85b3fd62ea1a | 1051 | { |
NYX | 0:85b3fd62ea1a | 1052 | hhash->HashInCount = 0U; |
NYX | 0:85b3fd62ea1a | 1053 | hhash->pHashInBuffPtr+= hhash->HashInCount; |
NYX | 0:85b3fd62ea1a | 1054 | } |
NYX | 0:85b3fd62ea1a | 1055 | /* Set Interrupt counter */ |
NYX | 0:85b3fd62ea1a | 1056 | hhash->HashITCounter = 1U; |
NYX | 0:85b3fd62ea1a | 1057 | } |
NYX | 0:85b3fd62ea1a | 1058 | else |
NYX | 0:85b3fd62ea1a | 1059 | { |
NYX | 0:85b3fd62ea1a | 1060 | /* Decrement buffer counter */ |
NYX | 0:85b3fd62ea1a | 1061 | hhash->HashInCount -= 64U; |
NYX | 0:85b3fd62ea1a | 1062 | hhash->pHashInBuffPtr+= 64U; |
NYX | 0:85b3fd62ea1a | 1063 | } |
NYX | 0:85b3fd62ea1a | 1064 | } |
NYX | 0:85b3fd62ea1a | 1065 | else |
NYX | 0:85b3fd62ea1a | 1066 | { |
NYX | 0:85b3fd62ea1a | 1067 | /* Get the buffer address */ |
NYX | 0:85b3fd62ea1a | 1068 | inputaddr = (uint32_t)hhash->pHashInBuffPtr; |
NYX | 0:85b3fd62ea1a | 1069 | /* Get the buffer counter */ |
NYX | 0:85b3fd62ea1a | 1070 | inputcounter = hhash->HashInCount; |
NYX | 0:85b3fd62ea1a | 1071 | /* Disable Interrupts */ |
NYX | 0:85b3fd62ea1a | 1072 | HASH->IMR &= ~(HASH_IT_DINI); |
NYX | 0:85b3fd62ea1a | 1073 | /* Configure the number of valid bits in last word of the message */ |
NYX | 0:85b3fd62ea1a | 1074 | __HAL_HASH_SET_NBVALIDBITS(inputcounter); |
NYX | 0:85b3fd62ea1a | 1075 | |
NYX | 0:85b3fd62ea1a | 1076 | if((inputcounter > 4U) && (inputcounter%4U)) |
NYX | 0:85b3fd62ea1a | 1077 | { |
NYX | 0:85b3fd62ea1a | 1078 | inputcounter = (inputcounter+4U-inputcounter%4U); |
NYX | 0:85b3fd62ea1a | 1079 | } |
NYX | 0:85b3fd62ea1a | 1080 | else if ((inputcounter < 4U) && (inputcounter != 0U)) |
NYX | 0:85b3fd62ea1a | 1081 | { |
NYX | 0:85b3fd62ea1a | 1082 | inputcounter = 4U; |
NYX | 0:85b3fd62ea1a | 1083 | } |
NYX | 0:85b3fd62ea1a | 1084 | /* Write the Input block in the Data IN register */ |
NYX | 0:85b3fd62ea1a | 1085 | for(buffercounter = 0U; buffercounter < inputcounter/4U; buffercounter++) |
NYX | 0:85b3fd62ea1a | 1086 | { |
NYX | 0:85b3fd62ea1a | 1087 | HASH->DIN = *(uint32_t*)inputaddr; |
NYX | 0:85b3fd62ea1a | 1088 | inputaddr+=4U; |
NYX | 0:85b3fd62ea1a | 1089 | } |
NYX | 0:85b3fd62ea1a | 1090 | /* Start the digest calculation */ |
NYX | 0:85b3fd62ea1a | 1091 | __HAL_HASH_START_DIGEST(); |
NYX | 0:85b3fd62ea1a | 1092 | /* Reset buffer counter */ |
NYX | 0:85b3fd62ea1a | 1093 | hhash->HashInCount = 0U; |
NYX | 0:85b3fd62ea1a | 1094 | /* Call Input data transfer complete callback */ |
NYX | 0:85b3fd62ea1a | 1095 | HAL_HASH_InCpltCallback(hhash); |
NYX | 0:85b3fd62ea1a | 1096 | } |
NYX | 0:85b3fd62ea1a | 1097 | } |
NYX | 0:85b3fd62ea1a | 1098 | |
NYX | 0:85b3fd62ea1a | 1099 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1100 | __HAL_UNLOCK(hhash); |
NYX | 0:85b3fd62ea1a | 1101 | |
NYX | 0:85b3fd62ea1a | 1102 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 1103 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 1104 | } |
NYX | 0:85b3fd62ea1a | 1105 | |
NYX | 0:85b3fd62ea1a | 1106 | /** |
NYX | 0:85b3fd62ea1a | 1107 | * @brief This function handles HASH interrupt request. |
NYX | 0:85b3fd62ea1a | 1108 | * @param hhash: pointer to a HASH_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 1109 | * the configuration information for HASH module |
NYX | 0:85b3fd62ea1a | 1110 | * @retval None |
NYX | 0:85b3fd62ea1a | 1111 | */ |
NYX | 0:85b3fd62ea1a | 1112 | void HAL_HASH_IRQHandler(HASH_HandleTypeDef *hhash) |
NYX | 0:85b3fd62ea1a | 1113 | { |
NYX | 0:85b3fd62ea1a | 1114 | switch(HASH->CR & HASH_CR_ALGO) |
NYX | 0:85b3fd62ea1a | 1115 | { |
NYX | 0:85b3fd62ea1a | 1116 | case HASH_ALGOSELECTION_MD5: |
NYX | 0:85b3fd62ea1a | 1117 | HAL_HASH_MD5_Start_IT(hhash, NULL, 0U, NULL); |
NYX | 0:85b3fd62ea1a | 1118 | break; |
NYX | 0:85b3fd62ea1a | 1119 | |
NYX | 0:85b3fd62ea1a | 1120 | case HASH_ALGOSELECTION_SHA1: |
NYX | 0:85b3fd62ea1a | 1121 | HAL_HASH_SHA1_Start_IT(hhash, NULL, 0U, NULL); |
NYX | 0:85b3fd62ea1a | 1122 | break; |
NYX | 0:85b3fd62ea1a | 1123 | |
NYX | 0:85b3fd62ea1a | 1124 | default: |
NYX | 0:85b3fd62ea1a | 1125 | break; |
NYX | 0:85b3fd62ea1a | 1126 | } |
NYX | 0:85b3fd62ea1a | 1127 | } |
NYX | 0:85b3fd62ea1a | 1128 | |
NYX | 0:85b3fd62ea1a | 1129 | /** |
NYX | 0:85b3fd62ea1a | 1130 | * @} |
NYX | 0:85b3fd62ea1a | 1131 | */ |
NYX | 0:85b3fd62ea1a | 1132 | |
NYX | 0:85b3fd62ea1a | 1133 | /** @defgroup HASH_Exported_Functions_Group4 HASH processing functions using DMA mode |
NYX | 0:85b3fd62ea1a | 1134 | * @brief processing functions using DMA mode. |
NYX | 0:85b3fd62ea1a | 1135 | * |
NYX | 0:85b3fd62ea1a | 1136 | @verbatim |
NYX | 0:85b3fd62ea1a | 1137 | =============================================================================== |
NYX | 0:85b3fd62ea1a | 1138 | ##### HASH processing using DMA mode functions ##### |
NYX | 0:85b3fd62ea1a | 1139 | =============================================================================== |
NYX | 0:85b3fd62ea1a | 1140 | [..] This section provides functions allowing to calculate in DMA mode |
NYX | 0:85b3fd62ea1a | 1141 | the hash value using one of the following algorithms: |
NYX | 0:85b3fd62ea1a | 1142 | (+) MD5 |
NYX | 0:85b3fd62ea1a | 1143 | (+) SHA1 |
NYX | 0:85b3fd62ea1a | 1144 | |
NYX | 0:85b3fd62ea1a | 1145 | @endverbatim |
NYX | 0:85b3fd62ea1a | 1146 | * @{ |
NYX | 0:85b3fd62ea1a | 1147 | */ |
NYX | 0:85b3fd62ea1a | 1148 | |
NYX | 0:85b3fd62ea1a | 1149 | /** |
NYX | 0:85b3fd62ea1a | 1150 | * @brief Initializes the HASH peripheral in MD5 mode then enables DMA to |
NYX | 0:85b3fd62ea1a | 1151 | control data transfer. Use HAL_HASH_MD5_Finish() to get the digest. |
NYX | 0:85b3fd62ea1a | 1152 | * @param hhash: pointer to a HASH_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 1153 | * the configuration information for HASH module |
NYX | 0:85b3fd62ea1a | 1154 | * @param pInBuffer: Pointer to the input buffer (buffer to be hashed). |
NYX | 0:85b3fd62ea1a | 1155 | * @param Size: Length of the input buffer in bytes. |
NYX | 0:85b3fd62ea1a | 1156 | * If the Size is not multiple of 64 bytes, the padding is managed by hardware. |
NYX | 0:85b3fd62ea1a | 1157 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 1158 | */ |
NYX | 0:85b3fd62ea1a | 1159 | HAL_StatusTypeDef HAL_HASH_MD5_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size) |
NYX | 0:85b3fd62ea1a | 1160 | { |
NYX | 0:85b3fd62ea1a | 1161 | uint32_t inputaddr = (uint32_t)pInBuffer; |
NYX | 0:85b3fd62ea1a | 1162 | |
NYX | 0:85b3fd62ea1a | 1163 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 1164 | __HAL_LOCK(hhash); |
NYX | 0:85b3fd62ea1a | 1165 | |
NYX | 0:85b3fd62ea1a | 1166 | /* Change the HASH state */ |
NYX | 0:85b3fd62ea1a | 1167 | hhash->State = HAL_HASH_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 1168 | |
NYX | 0:85b3fd62ea1a | 1169 | /* Check if initialization phase has already been performed */ |
NYX | 0:85b3fd62ea1a | 1170 | if(hhash->Phase == HAL_HASH_PHASE_READY) |
NYX | 0:85b3fd62ea1a | 1171 | { |
NYX | 0:85b3fd62ea1a | 1172 | /* Select the MD5 mode and reset the HASH processor core, so that the HASH will be ready to compute |
NYX | 0:85b3fd62ea1a | 1173 | the message digest of a new message */ |
NYX | 0:85b3fd62ea1a | 1174 | HASH->CR |= HASH_ALGOSELECTION_MD5 | HASH_CR_INIT; |
NYX | 0:85b3fd62ea1a | 1175 | } |
NYX | 0:85b3fd62ea1a | 1176 | |
NYX | 0:85b3fd62ea1a | 1177 | /* Configure the number of valid bits in last word of the message */ |
NYX | 0:85b3fd62ea1a | 1178 | __HAL_HASH_SET_NBVALIDBITS(Size); |
NYX | 0:85b3fd62ea1a | 1179 | |
NYX | 0:85b3fd62ea1a | 1180 | /* Set the phase */ |
NYX | 0:85b3fd62ea1a | 1181 | hhash->Phase = HAL_HASH_PHASE_PROCESS; |
NYX | 0:85b3fd62ea1a | 1182 | |
NYX | 0:85b3fd62ea1a | 1183 | /* Set the HASH DMA transfer complete callback */ |
NYX | 0:85b3fd62ea1a | 1184 | hhash->hdmain->XferCpltCallback = HASH_DMAXferCplt; |
NYX | 0:85b3fd62ea1a | 1185 | /* Set the DMA error callback */ |
NYX | 0:85b3fd62ea1a | 1186 | hhash->hdmain->XferErrorCallback = HASH_DMAError; |
NYX | 0:85b3fd62ea1a | 1187 | |
NYX | 0:85b3fd62ea1a | 1188 | /* Enable the DMA In DMA Stream */ |
NYX | 0:85b3fd62ea1a | 1189 | HAL_DMA_Start_IT(hhash->hdmain, inputaddr, (uint32_t)&HASH->DIN, (Size%4U ? (Size+3U)/4U:Size/4U)); |
NYX | 0:85b3fd62ea1a | 1190 | |
NYX | 0:85b3fd62ea1a | 1191 | /* Enable DMA requests */ |
NYX | 0:85b3fd62ea1a | 1192 | HASH->CR |= (HASH_CR_DMAE); |
NYX | 0:85b3fd62ea1a | 1193 | |
NYX | 0:85b3fd62ea1a | 1194 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1195 | __HAL_UNLOCK(hhash); |
NYX | 0:85b3fd62ea1a | 1196 | |
NYX | 0:85b3fd62ea1a | 1197 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 1198 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 1199 | } |
NYX | 0:85b3fd62ea1a | 1200 | |
NYX | 0:85b3fd62ea1a | 1201 | /** |
NYX | 0:85b3fd62ea1a | 1202 | * @brief Returns the computed digest in MD5 mode |
NYX | 0:85b3fd62ea1a | 1203 | * @param hhash: pointer to a HASH_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 1204 | * the configuration information for HASH module |
NYX | 0:85b3fd62ea1a | 1205 | * @param pOutBuffer: Pointer to the computed digest. Its size must be 16 bytes. |
NYX | 0:85b3fd62ea1a | 1206 | * @param Timeout: Timeout value |
NYX | 0:85b3fd62ea1a | 1207 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 1208 | */ |
NYX | 0:85b3fd62ea1a | 1209 | HAL_StatusTypeDef HAL_HASH_MD5_Finish(HASH_HandleTypeDef *hhash, uint8_t* pOutBuffer, uint32_t Timeout) |
NYX | 0:85b3fd62ea1a | 1210 | { |
NYX | 0:85b3fd62ea1a | 1211 | uint32_t tickstart = 0U; |
NYX | 0:85b3fd62ea1a | 1212 | |
NYX | 0:85b3fd62ea1a | 1213 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 1214 | __HAL_LOCK(hhash); |
NYX | 0:85b3fd62ea1a | 1215 | |
NYX | 0:85b3fd62ea1a | 1216 | /* Change HASH peripheral state */ |
NYX | 0:85b3fd62ea1a | 1217 | hhash->State = HAL_HASH_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 1218 | |
NYX | 0:85b3fd62ea1a | 1219 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 1220 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 1221 | |
NYX | 0:85b3fd62ea1a | 1222 | while(HAL_IS_BIT_CLR(HASH->SR, HASH_FLAG_DCIS)) |
NYX | 0:85b3fd62ea1a | 1223 | { |
NYX | 0:85b3fd62ea1a | 1224 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 1225 | if(Timeout != HAL_MAX_DELAY) |
NYX | 0:85b3fd62ea1a | 1226 | { |
NYX | 0:85b3fd62ea1a | 1227 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
NYX | 0:85b3fd62ea1a | 1228 | { |
NYX | 0:85b3fd62ea1a | 1229 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 1230 | hhash->State = HAL_HASH_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1231 | |
NYX | 0:85b3fd62ea1a | 1232 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1233 | __HAL_UNLOCK(hhash); |
NYX | 0:85b3fd62ea1a | 1234 | |
NYX | 0:85b3fd62ea1a | 1235 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1236 | } |
NYX | 0:85b3fd62ea1a | 1237 | } |
NYX | 0:85b3fd62ea1a | 1238 | } |
NYX | 0:85b3fd62ea1a | 1239 | |
NYX | 0:85b3fd62ea1a | 1240 | /* Read the message digest */ |
NYX | 0:85b3fd62ea1a | 1241 | HASH_GetDigest(pOutBuffer, 16U); |
NYX | 0:85b3fd62ea1a | 1242 | |
NYX | 0:85b3fd62ea1a | 1243 | /* Change HASH peripheral state */ |
NYX | 0:85b3fd62ea1a | 1244 | hhash->State = HAL_HASH_STATE_READY; |
NYX | 0:85b3fd62ea1a | 1245 | |
NYX | 0:85b3fd62ea1a | 1246 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1247 | __HAL_UNLOCK(hhash); |
NYX | 0:85b3fd62ea1a | 1248 | |
NYX | 0:85b3fd62ea1a | 1249 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 1250 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 1251 | } |
NYX | 0:85b3fd62ea1a | 1252 | |
NYX | 0:85b3fd62ea1a | 1253 | /** |
NYX | 0:85b3fd62ea1a | 1254 | * @brief Initializes the HASH peripheral in SHA1 mode then enables DMA to |
NYX | 0:85b3fd62ea1a | 1255 | control data transfer. Use HAL_HASH_SHA1_Finish() to get the digest. |
NYX | 0:85b3fd62ea1a | 1256 | * @param hhash: pointer to a HASH_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 1257 | * the configuration information for HASH module |
NYX | 0:85b3fd62ea1a | 1258 | * @param pInBuffer: Pointer to the input buffer (buffer to be hashed). |
NYX | 0:85b3fd62ea1a | 1259 | * @param Size: Length of the input buffer in bytes. |
NYX | 0:85b3fd62ea1a | 1260 | * If the Size is not multiple of 64 bytes, the padding is managed by hardware. |
NYX | 0:85b3fd62ea1a | 1261 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 1262 | */ |
NYX | 0:85b3fd62ea1a | 1263 | HAL_StatusTypeDef HAL_HASH_SHA1_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size) |
NYX | 0:85b3fd62ea1a | 1264 | { |
NYX | 0:85b3fd62ea1a | 1265 | uint32_t inputaddr = (uint32_t)pInBuffer; |
NYX | 0:85b3fd62ea1a | 1266 | |
NYX | 0:85b3fd62ea1a | 1267 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 1268 | __HAL_LOCK(hhash); |
NYX | 0:85b3fd62ea1a | 1269 | |
NYX | 0:85b3fd62ea1a | 1270 | /* Change the HASH state */ |
NYX | 0:85b3fd62ea1a | 1271 | hhash->State = HAL_HASH_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 1272 | |
NYX | 0:85b3fd62ea1a | 1273 | /* Check if initialization phase has already been performed */ |
NYX | 0:85b3fd62ea1a | 1274 | if(hhash->Phase == HAL_HASH_PHASE_READY) |
NYX | 0:85b3fd62ea1a | 1275 | { |
NYX | 0:85b3fd62ea1a | 1276 | /* Select the SHA1 mode and reset the HASH processor core, so that the HASH will be ready to compute |
NYX | 0:85b3fd62ea1a | 1277 | the message digest of a new message */ |
NYX | 0:85b3fd62ea1a | 1278 | HASH->CR |= HASH_ALGOSELECTION_SHA1; |
NYX | 0:85b3fd62ea1a | 1279 | HASH->CR |= HASH_CR_INIT; |
NYX | 0:85b3fd62ea1a | 1280 | } |
NYX | 0:85b3fd62ea1a | 1281 | |
NYX | 0:85b3fd62ea1a | 1282 | /* Configure the number of valid bits in last word of the message */ |
NYX | 0:85b3fd62ea1a | 1283 | __HAL_HASH_SET_NBVALIDBITS(Size); |
NYX | 0:85b3fd62ea1a | 1284 | |
NYX | 0:85b3fd62ea1a | 1285 | /* Set the phase */ |
NYX | 0:85b3fd62ea1a | 1286 | hhash->Phase = HAL_HASH_PHASE_PROCESS; |
NYX | 0:85b3fd62ea1a | 1287 | |
NYX | 0:85b3fd62ea1a | 1288 | /* Set the HASH DMA transfer complete callback */ |
NYX | 0:85b3fd62ea1a | 1289 | hhash->hdmain->XferCpltCallback = HASH_DMAXferCplt; |
NYX | 0:85b3fd62ea1a | 1290 | /* Set the DMA error callback */ |
NYX | 0:85b3fd62ea1a | 1291 | hhash->hdmain->XferErrorCallback = HASH_DMAError; |
NYX | 0:85b3fd62ea1a | 1292 | |
NYX | 0:85b3fd62ea1a | 1293 | /* Enable the DMA In DMA Stream */ |
NYX | 0:85b3fd62ea1a | 1294 | HAL_DMA_Start_IT(hhash->hdmain, inputaddr, (uint32_t)&HASH->DIN, (Size%4U ? (Size+3U)/4U:Size/4U)); |
NYX | 0:85b3fd62ea1a | 1295 | |
NYX | 0:85b3fd62ea1a | 1296 | /* Enable DMA requests */ |
NYX | 0:85b3fd62ea1a | 1297 | HASH->CR |= (HASH_CR_DMAE); |
NYX | 0:85b3fd62ea1a | 1298 | |
NYX | 0:85b3fd62ea1a | 1299 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1300 | __HAL_UNLOCK(hhash); |
NYX | 0:85b3fd62ea1a | 1301 | |
NYX | 0:85b3fd62ea1a | 1302 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 1303 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 1304 | } |
NYX | 0:85b3fd62ea1a | 1305 | |
NYX | 0:85b3fd62ea1a | 1306 | /** |
NYX | 0:85b3fd62ea1a | 1307 | * @brief Returns the computed digest in SHA1 mode. |
NYX | 0:85b3fd62ea1a | 1308 | * @param hhash: pointer to a HASH_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 1309 | * the configuration information for HASH module |
NYX | 0:85b3fd62ea1a | 1310 | * @param pOutBuffer: Pointer to the computed digest. Its size must be 20 bytes. |
NYX | 0:85b3fd62ea1a | 1311 | * @param Timeout: Timeout value |
NYX | 0:85b3fd62ea1a | 1312 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 1313 | */ |
NYX | 0:85b3fd62ea1a | 1314 | HAL_StatusTypeDef HAL_HASH_SHA1_Finish(HASH_HandleTypeDef *hhash, uint8_t* pOutBuffer, uint32_t Timeout) |
NYX | 0:85b3fd62ea1a | 1315 | { |
NYX | 0:85b3fd62ea1a | 1316 | uint32_t tickstart = 0U; |
NYX | 0:85b3fd62ea1a | 1317 | |
NYX | 0:85b3fd62ea1a | 1318 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 1319 | __HAL_LOCK(hhash); |
NYX | 0:85b3fd62ea1a | 1320 | |
NYX | 0:85b3fd62ea1a | 1321 | /* Change HASH peripheral state */ |
NYX | 0:85b3fd62ea1a | 1322 | hhash->State = HAL_HASH_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 1323 | |
NYX | 0:85b3fd62ea1a | 1324 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 1325 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 1326 | while(HAL_IS_BIT_CLR(HASH->SR, HASH_FLAG_DCIS)) |
NYX | 0:85b3fd62ea1a | 1327 | { |
NYX | 0:85b3fd62ea1a | 1328 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 1329 | if(Timeout != HAL_MAX_DELAY) |
NYX | 0:85b3fd62ea1a | 1330 | { |
NYX | 0:85b3fd62ea1a | 1331 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
NYX | 0:85b3fd62ea1a | 1332 | { |
NYX | 0:85b3fd62ea1a | 1333 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 1334 | hhash->State = HAL_HASH_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1335 | |
NYX | 0:85b3fd62ea1a | 1336 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1337 | __HAL_UNLOCK(hhash); |
NYX | 0:85b3fd62ea1a | 1338 | |
NYX | 0:85b3fd62ea1a | 1339 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1340 | } |
NYX | 0:85b3fd62ea1a | 1341 | } |
NYX | 0:85b3fd62ea1a | 1342 | } |
NYX | 0:85b3fd62ea1a | 1343 | |
NYX | 0:85b3fd62ea1a | 1344 | /* Read the message digest */ |
NYX | 0:85b3fd62ea1a | 1345 | HASH_GetDigest(pOutBuffer, 20U); |
NYX | 0:85b3fd62ea1a | 1346 | |
NYX | 0:85b3fd62ea1a | 1347 | /* Change HASH peripheral state */ |
NYX | 0:85b3fd62ea1a | 1348 | hhash->State = HAL_HASH_STATE_READY; |
NYX | 0:85b3fd62ea1a | 1349 | |
NYX | 0:85b3fd62ea1a | 1350 | /* Process UnLock */ |
NYX | 0:85b3fd62ea1a | 1351 | __HAL_UNLOCK(hhash); |
NYX | 0:85b3fd62ea1a | 1352 | |
NYX | 0:85b3fd62ea1a | 1353 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 1354 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 1355 | } |
NYX | 0:85b3fd62ea1a | 1356 | |
NYX | 0:85b3fd62ea1a | 1357 | |
NYX | 0:85b3fd62ea1a | 1358 | /** |
NYX | 0:85b3fd62ea1a | 1359 | * @} |
NYX | 0:85b3fd62ea1a | 1360 | */ |
NYX | 0:85b3fd62ea1a | 1361 | |
NYX | 0:85b3fd62ea1a | 1362 | /** @defgroup HASH_Exported_Functions_Group5 HASH-MAC (HMAC) processing functions using polling mode |
NYX | 0:85b3fd62ea1a | 1363 | * @brief HMAC processing functions using polling mode . |
NYX | 0:85b3fd62ea1a | 1364 | * |
NYX | 0:85b3fd62ea1a | 1365 | @verbatim |
NYX | 0:85b3fd62ea1a | 1366 | =============================================================================== |
NYX | 0:85b3fd62ea1a | 1367 | ##### HMAC processing using polling mode functions ##### |
NYX | 0:85b3fd62ea1a | 1368 | =============================================================================== |
NYX | 0:85b3fd62ea1a | 1369 | [..] This section provides functions allowing to calculate in polling mode |
NYX | 0:85b3fd62ea1a | 1370 | the HMAC value using one of the following algorithms: |
NYX | 0:85b3fd62ea1a | 1371 | (+) MD5 |
NYX | 0:85b3fd62ea1a | 1372 | (+) SHA1 |
NYX | 0:85b3fd62ea1a | 1373 | |
NYX | 0:85b3fd62ea1a | 1374 | @endverbatim |
NYX | 0:85b3fd62ea1a | 1375 | * @{ |
NYX | 0:85b3fd62ea1a | 1376 | */ |
NYX | 0:85b3fd62ea1a | 1377 | |
NYX | 0:85b3fd62ea1a | 1378 | /** |
NYX | 0:85b3fd62ea1a | 1379 | * @brief Initializes the HASH peripheral in HMAC MD5 mode |
NYX | 0:85b3fd62ea1a | 1380 | * then processes pInBuffer. The digest is available in pOutBuffer |
NYX | 0:85b3fd62ea1a | 1381 | * @param hhash: pointer to a HASH_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 1382 | * the configuration information for HASH module |
NYX | 0:85b3fd62ea1a | 1383 | * @param pInBuffer: Pointer to the input buffer (buffer to be hashed). |
NYX | 0:85b3fd62ea1a | 1384 | * @param Size: Length of the input buffer in bytes. |
NYX | 0:85b3fd62ea1a | 1385 | * If the Size is not multiple of 64 bytes, the padding is managed by hardware. |
NYX | 0:85b3fd62ea1a | 1386 | * @param pOutBuffer: Pointer to the computed digest. Its size must be 20 bytes. |
NYX | 0:85b3fd62ea1a | 1387 | * @param Timeout: Timeout value |
NYX | 0:85b3fd62ea1a | 1388 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 1389 | */ |
NYX | 0:85b3fd62ea1a | 1390 | HAL_StatusTypeDef HAL_HMAC_MD5_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout) |
NYX | 0:85b3fd62ea1a | 1391 | { |
NYX | 0:85b3fd62ea1a | 1392 | uint32_t tickstart = 0U; |
NYX | 0:85b3fd62ea1a | 1393 | |
NYX | 0:85b3fd62ea1a | 1394 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 1395 | __HAL_LOCK(hhash); |
NYX | 0:85b3fd62ea1a | 1396 | |
NYX | 0:85b3fd62ea1a | 1397 | /* Change the HASH state */ |
NYX | 0:85b3fd62ea1a | 1398 | hhash->State = HAL_HASH_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 1399 | |
NYX | 0:85b3fd62ea1a | 1400 | /* Check if initialization phase has already been performed */ |
NYX | 0:85b3fd62ea1a | 1401 | if(hhash->Phase == HAL_HASH_PHASE_READY) |
NYX | 0:85b3fd62ea1a | 1402 | { |
NYX | 0:85b3fd62ea1a | 1403 | /* Check if key size is greater than 64 bytes */ |
NYX | 0:85b3fd62ea1a | 1404 | if(hhash->Init.KeySize > 64U) |
NYX | 0:85b3fd62ea1a | 1405 | { |
NYX | 0:85b3fd62ea1a | 1406 | /* Select the HMAC MD5 mode */ |
NYX | 0:85b3fd62ea1a | 1407 | HASH->CR |= (HASH_ALGOSELECTION_MD5 | HASH_ALGOMODE_HMAC | HASH_HMAC_KEYTYPE_LONGKEY | HASH_CR_INIT); |
NYX | 0:85b3fd62ea1a | 1408 | } |
NYX | 0:85b3fd62ea1a | 1409 | else |
NYX | 0:85b3fd62ea1a | 1410 | { |
NYX | 0:85b3fd62ea1a | 1411 | /* Select the HMAC MD5 mode */ |
NYX | 0:85b3fd62ea1a | 1412 | HASH->CR |= (HASH_ALGOSELECTION_MD5 | HASH_ALGOMODE_HMAC | HASH_CR_INIT); |
NYX | 0:85b3fd62ea1a | 1413 | } |
NYX | 0:85b3fd62ea1a | 1414 | } |
NYX | 0:85b3fd62ea1a | 1415 | |
NYX | 0:85b3fd62ea1a | 1416 | /* Set the phase */ |
NYX | 0:85b3fd62ea1a | 1417 | hhash->Phase = HAL_HASH_PHASE_PROCESS; |
NYX | 0:85b3fd62ea1a | 1418 | |
NYX | 0:85b3fd62ea1a | 1419 | /************************** STEP 1 ******************************************/ |
NYX | 0:85b3fd62ea1a | 1420 | /* Configure the number of valid bits in last word of the message */ |
NYX | 0:85b3fd62ea1a | 1421 | __HAL_HASH_SET_NBVALIDBITS(hhash->Init.KeySize); |
NYX | 0:85b3fd62ea1a | 1422 | |
NYX | 0:85b3fd62ea1a | 1423 | /* Write input buffer in data register */ |
NYX | 0:85b3fd62ea1a | 1424 | HASH_WriteData(hhash->Init.pKey, hhash->Init.KeySize); |
NYX | 0:85b3fd62ea1a | 1425 | |
NYX | 0:85b3fd62ea1a | 1426 | /* Start the digest calculation */ |
NYX | 0:85b3fd62ea1a | 1427 | __HAL_HASH_START_DIGEST(); |
NYX | 0:85b3fd62ea1a | 1428 | |
NYX | 0:85b3fd62ea1a | 1429 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 1430 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 1431 | |
NYX | 0:85b3fd62ea1a | 1432 | while(HAL_IS_BIT_SET(HASH->SR, HASH_FLAG_BUSY)) |
NYX | 0:85b3fd62ea1a | 1433 | { |
NYX | 0:85b3fd62ea1a | 1434 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 1435 | if(Timeout != HAL_MAX_DELAY) |
NYX | 0:85b3fd62ea1a | 1436 | { |
NYX | 0:85b3fd62ea1a | 1437 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
NYX | 0:85b3fd62ea1a | 1438 | { |
NYX | 0:85b3fd62ea1a | 1439 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 1440 | hhash->State = HAL_HASH_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1441 | |
NYX | 0:85b3fd62ea1a | 1442 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1443 | __HAL_UNLOCK(hhash); |
NYX | 0:85b3fd62ea1a | 1444 | |
NYX | 0:85b3fd62ea1a | 1445 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1446 | } |
NYX | 0:85b3fd62ea1a | 1447 | } |
NYX | 0:85b3fd62ea1a | 1448 | } |
NYX | 0:85b3fd62ea1a | 1449 | /************************** STEP 2 ******************************************/ |
NYX | 0:85b3fd62ea1a | 1450 | /* Configure the number of valid bits in last word of the message */ |
NYX | 0:85b3fd62ea1a | 1451 | __HAL_HASH_SET_NBVALIDBITS(Size); |
NYX | 0:85b3fd62ea1a | 1452 | |
NYX | 0:85b3fd62ea1a | 1453 | /* Write input buffer in data register */ |
NYX | 0:85b3fd62ea1a | 1454 | HASH_WriteData(pInBuffer, Size); |
NYX | 0:85b3fd62ea1a | 1455 | |
NYX | 0:85b3fd62ea1a | 1456 | /* Start the digest calculation */ |
NYX | 0:85b3fd62ea1a | 1457 | __HAL_HASH_START_DIGEST(); |
NYX | 0:85b3fd62ea1a | 1458 | |
NYX | 0:85b3fd62ea1a | 1459 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 1460 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 1461 | |
NYX | 0:85b3fd62ea1a | 1462 | while(HAL_IS_BIT_SET(HASH->SR, HASH_FLAG_BUSY)) |
NYX | 0:85b3fd62ea1a | 1463 | { |
NYX | 0:85b3fd62ea1a | 1464 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 1465 | if(Timeout != HAL_MAX_DELAY) |
NYX | 0:85b3fd62ea1a | 1466 | { |
NYX | 0:85b3fd62ea1a | 1467 | if((HAL_GetTick() - tickstart ) > Timeout) |
NYX | 0:85b3fd62ea1a | 1468 | { |
NYX | 0:85b3fd62ea1a | 1469 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 1470 | hhash->State = HAL_HASH_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1471 | |
NYX | 0:85b3fd62ea1a | 1472 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1473 | __HAL_UNLOCK(hhash); |
NYX | 0:85b3fd62ea1a | 1474 | |
NYX | 0:85b3fd62ea1a | 1475 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1476 | } |
NYX | 0:85b3fd62ea1a | 1477 | } |
NYX | 0:85b3fd62ea1a | 1478 | } |
NYX | 0:85b3fd62ea1a | 1479 | /************************** STEP 3 ******************************************/ |
NYX | 0:85b3fd62ea1a | 1480 | /* Configure the number of valid bits in last word of the message */ |
NYX | 0:85b3fd62ea1a | 1481 | __HAL_HASH_SET_NBVALIDBITS(hhash->Init.KeySize); |
NYX | 0:85b3fd62ea1a | 1482 | |
NYX | 0:85b3fd62ea1a | 1483 | /* Write input buffer in data register */ |
NYX | 0:85b3fd62ea1a | 1484 | HASH_WriteData(hhash->Init.pKey, hhash->Init.KeySize); |
NYX | 0:85b3fd62ea1a | 1485 | |
NYX | 0:85b3fd62ea1a | 1486 | /* Start the digest calculation */ |
NYX | 0:85b3fd62ea1a | 1487 | __HAL_HASH_START_DIGEST(); |
NYX | 0:85b3fd62ea1a | 1488 | |
NYX | 0:85b3fd62ea1a | 1489 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 1490 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 1491 | |
NYX | 0:85b3fd62ea1a | 1492 | while(HAL_IS_BIT_SET(HASH->SR, HASH_FLAG_BUSY)) |
NYX | 0:85b3fd62ea1a | 1493 | { |
NYX | 0:85b3fd62ea1a | 1494 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 1495 | if(Timeout != HAL_MAX_DELAY) |
NYX | 0:85b3fd62ea1a | 1496 | { |
NYX | 0:85b3fd62ea1a | 1497 | if((HAL_GetTick() - tickstart ) > Timeout) |
NYX | 0:85b3fd62ea1a | 1498 | { |
NYX | 0:85b3fd62ea1a | 1499 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 1500 | hhash->State = HAL_HASH_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1501 | |
NYX | 0:85b3fd62ea1a | 1502 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1503 | __HAL_UNLOCK(hhash); |
NYX | 0:85b3fd62ea1a | 1504 | |
NYX | 0:85b3fd62ea1a | 1505 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1506 | } |
NYX | 0:85b3fd62ea1a | 1507 | } |
NYX | 0:85b3fd62ea1a | 1508 | } |
NYX | 0:85b3fd62ea1a | 1509 | |
NYX | 0:85b3fd62ea1a | 1510 | /* Read the message digest */ |
NYX | 0:85b3fd62ea1a | 1511 | HASH_GetDigest(pOutBuffer, 16U); |
NYX | 0:85b3fd62ea1a | 1512 | |
NYX | 0:85b3fd62ea1a | 1513 | /* Change the HASH state */ |
NYX | 0:85b3fd62ea1a | 1514 | hhash->State = HAL_HASH_STATE_READY; |
NYX | 0:85b3fd62ea1a | 1515 | |
NYX | 0:85b3fd62ea1a | 1516 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1517 | __HAL_UNLOCK(hhash); |
NYX | 0:85b3fd62ea1a | 1518 | |
NYX | 0:85b3fd62ea1a | 1519 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 1520 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 1521 | } |
NYX | 0:85b3fd62ea1a | 1522 | |
NYX | 0:85b3fd62ea1a | 1523 | /** |
NYX | 0:85b3fd62ea1a | 1524 | * @brief Initializes the HASH peripheral in HMAC SHA1 mode |
NYX | 0:85b3fd62ea1a | 1525 | * then processes pInBuffer. The digest is available in pOutBuffer. |
NYX | 0:85b3fd62ea1a | 1526 | * @param hhash: pointer to a HASH_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 1527 | * the configuration information for HASH module |
NYX | 0:85b3fd62ea1a | 1528 | * @param pInBuffer: Pointer to the input buffer (buffer to be hashed). |
NYX | 0:85b3fd62ea1a | 1529 | * @param Size: Length of the input buffer in bytes. |
NYX | 0:85b3fd62ea1a | 1530 | * If the Size is not multiple of 64 bytes, the padding is managed by hardware. |
NYX | 0:85b3fd62ea1a | 1531 | * @param pOutBuffer: Pointer to the computed digest. Its size must be 20 bytes. |
NYX | 0:85b3fd62ea1a | 1532 | * @param Timeout: Timeout value |
NYX | 0:85b3fd62ea1a | 1533 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 1534 | */ |
NYX | 0:85b3fd62ea1a | 1535 | HAL_StatusTypeDef HAL_HMAC_SHA1_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout) |
NYX | 0:85b3fd62ea1a | 1536 | { |
NYX | 0:85b3fd62ea1a | 1537 | uint32_t tickstart = 0U; |
NYX | 0:85b3fd62ea1a | 1538 | |
NYX | 0:85b3fd62ea1a | 1539 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 1540 | __HAL_LOCK(hhash); |
NYX | 0:85b3fd62ea1a | 1541 | |
NYX | 0:85b3fd62ea1a | 1542 | /* Change the HASH state */ |
NYX | 0:85b3fd62ea1a | 1543 | hhash->State = HAL_HASH_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 1544 | |
NYX | 0:85b3fd62ea1a | 1545 | /* Check if initialization phase has already been performed */ |
NYX | 0:85b3fd62ea1a | 1546 | if(hhash->Phase == HAL_HASH_PHASE_READY) |
NYX | 0:85b3fd62ea1a | 1547 | { |
NYX | 0:85b3fd62ea1a | 1548 | /* Check if key size is greater than 64 bytes */ |
NYX | 0:85b3fd62ea1a | 1549 | if(hhash->Init.KeySize > 64U) |
NYX | 0:85b3fd62ea1a | 1550 | { |
NYX | 0:85b3fd62ea1a | 1551 | /* Select the HMAC SHA1 mode */ |
NYX | 0:85b3fd62ea1a | 1552 | HASH->CR |= (HASH_ALGOSELECTION_SHA1 | HASH_ALGOMODE_HMAC | HASH_HMAC_KEYTYPE_LONGKEY | HASH_CR_INIT); |
NYX | 0:85b3fd62ea1a | 1553 | } |
NYX | 0:85b3fd62ea1a | 1554 | else |
NYX | 0:85b3fd62ea1a | 1555 | { |
NYX | 0:85b3fd62ea1a | 1556 | /* Select the HMAC SHA1 mode */ |
NYX | 0:85b3fd62ea1a | 1557 | HASH->CR |= (HASH_ALGOSELECTION_SHA1 | HASH_ALGOMODE_HMAC | HASH_CR_INIT); |
NYX | 0:85b3fd62ea1a | 1558 | } |
NYX | 0:85b3fd62ea1a | 1559 | } |
NYX | 0:85b3fd62ea1a | 1560 | |
NYX | 0:85b3fd62ea1a | 1561 | /* Set the phase */ |
NYX | 0:85b3fd62ea1a | 1562 | hhash->Phase = HAL_HASH_PHASE_PROCESS; |
NYX | 0:85b3fd62ea1a | 1563 | |
NYX | 0:85b3fd62ea1a | 1564 | /************************** STEP 1 ******************************************/ |
NYX | 0:85b3fd62ea1a | 1565 | /* Configure the number of valid bits in last word of the message */ |
NYX | 0:85b3fd62ea1a | 1566 | __HAL_HASH_SET_NBVALIDBITS(hhash->Init.KeySize); |
NYX | 0:85b3fd62ea1a | 1567 | |
NYX | 0:85b3fd62ea1a | 1568 | /* Write input buffer in data register */ |
NYX | 0:85b3fd62ea1a | 1569 | HASH_WriteData(hhash->Init.pKey, hhash->Init.KeySize); |
NYX | 0:85b3fd62ea1a | 1570 | |
NYX | 0:85b3fd62ea1a | 1571 | /* Start the digest calculation */ |
NYX | 0:85b3fd62ea1a | 1572 | __HAL_HASH_START_DIGEST(); |
NYX | 0:85b3fd62ea1a | 1573 | |
NYX | 0:85b3fd62ea1a | 1574 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 1575 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 1576 | |
NYX | 0:85b3fd62ea1a | 1577 | while(HAL_IS_BIT_SET(HASH->SR, HASH_FLAG_BUSY)) |
NYX | 0:85b3fd62ea1a | 1578 | { |
NYX | 0:85b3fd62ea1a | 1579 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 1580 | if(Timeout != HAL_MAX_DELAY) |
NYX | 0:85b3fd62ea1a | 1581 | { |
NYX | 0:85b3fd62ea1a | 1582 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
NYX | 0:85b3fd62ea1a | 1583 | { |
NYX | 0:85b3fd62ea1a | 1584 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 1585 | hhash->State = HAL_HASH_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1586 | |
NYX | 0:85b3fd62ea1a | 1587 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1588 | __HAL_UNLOCK(hhash); |
NYX | 0:85b3fd62ea1a | 1589 | |
NYX | 0:85b3fd62ea1a | 1590 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1591 | } |
NYX | 0:85b3fd62ea1a | 1592 | } |
NYX | 0:85b3fd62ea1a | 1593 | } |
NYX | 0:85b3fd62ea1a | 1594 | /************************** STEP 2 ******************************************/ |
NYX | 0:85b3fd62ea1a | 1595 | /* Configure the number of valid bits in last word of the message */ |
NYX | 0:85b3fd62ea1a | 1596 | __HAL_HASH_SET_NBVALIDBITS(Size); |
NYX | 0:85b3fd62ea1a | 1597 | |
NYX | 0:85b3fd62ea1a | 1598 | /* Write input buffer in data register */ |
NYX | 0:85b3fd62ea1a | 1599 | HASH_WriteData(pInBuffer, Size); |
NYX | 0:85b3fd62ea1a | 1600 | |
NYX | 0:85b3fd62ea1a | 1601 | /* Start the digest calculation */ |
NYX | 0:85b3fd62ea1a | 1602 | __HAL_HASH_START_DIGEST(); |
NYX | 0:85b3fd62ea1a | 1603 | |
NYX | 0:85b3fd62ea1a | 1604 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 1605 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 1606 | |
NYX | 0:85b3fd62ea1a | 1607 | while(HAL_IS_BIT_SET(HASH->SR, HASH_FLAG_BUSY)) |
NYX | 0:85b3fd62ea1a | 1608 | { |
NYX | 0:85b3fd62ea1a | 1609 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 1610 | if(Timeout != HAL_MAX_DELAY) |
NYX | 0:85b3fd62ea1a | 1611 | { |
NYX | 0:85b3fd62ea1a | 1612 | if((HAL_GetTick() - tickstart ) > Timeout) |
NYX | 0:85b3fd62ea1a | 1613 | { |
NYX | 0:85b3fd62ea1a | 1614 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 1615 | hhash->State = HAL_HASH_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1616 | |
NYX | 0:85b3fd62ea1a | 1617 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1618 | __HAL_UNLOCK(hhash); |
NYX | 0:85b3fd62ea1a | 1619 | |
NYX | 0:85b3fd62ea1a | 1620 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1621 | } |
NYX | 0:85b3fd62ea1a | 1622 | } |
NYX | 0:85b3fd62ea1a | 1623 | } |
NYX | 0:85b3fd62ea1a | 1624 | /************************** STEP 3 ******************************************/ |
NYX | 0:85b3fd62ea1a | 1625 | /* Configure the number of valid bits in last word of the message */ |
NYX | 0:85b3fd62ea1a | 1626 | __HAL_HASH_SET_NBVALIDBITS(hhash->Init.KeySize); |
NYX | 0:85b3fd62ea1a | 1627 | |
NYX | 0:85b3fd62ea1a | 1628 | /* Write input buffer in data register */ |
NYX | 0:85b3fd62ea1a | 1629 | HASH_WriteData(hhash->Init.pKey, hhash->Init.KeySize); |
NYX | 0:85b3fd62ea1a | 1630 | |
NYX | 0:85b3fd62ea1a | 1631 | /* Start the digest calculation */ |
NYX | 0:85b3fd62ea1a | 1632 | __HAL_HASH_START_DIGEST(); |
NYX | 0:85b3fd62ea1a | 1633 | |
NYX | 0:85b3fd62ea1a | 1634 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 1635 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 1636 | |
NYX | 0:85b3fd62ea1a | 1637 | while(HAL_IS_BIT_SET(HASH->SR, HASH_FLAG_BUSY)) |
NYX | 0:85b3fd62ea1a | 1638 | { |
NYX | 0:85b3fd62ea1a | 1639 | /* Check for the Timeout */ |
NYX | 0:85b3fd62ea1a | 1640 | if(Timeout != HAL_MAX_DELAY) |
NYX | 0:85b3fd62ea1a | 1641 | { |
NYX | 0:85b3fd62ea1a | 1642 | if((HAL_GetTick() - tickstart ) > Timeout) |
NYX | 0:85b3fd62ea1a | 1643 | { |
NYX | 0:85b3fd62ea1a | 1644 | /* Change state */ |
NYX | 0:85b3fd62ea1a | 1645 | hhash->State = HAL_HASH_STATE_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1646 | |
NYX | 0:85b3fd62ea1a | 1647 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1648 | __HAL_UNLOCK(hhash); |
NYX | 0:85b3fd62ea1a | 1649 | |
NYX | 0:85b3fd62ea1a | 1650 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 1651 | } |
NYX | 0:85b3fd62ea1a | 1652 | } |
NYX | 0:85b3fd62ea1a | 1653 | } |
NYX | 0:85b3fd62ea1a | 1654 | /* Read the message digest */ |
NYX | 0:85b3fd62ea1a | 1655 | HASH_GetDigest(pOutBuffer, 20U); |
NYX | 0:85b3fd62ea1a | 1656 | |
NYX | 0:85b3fd62ea1a | 1657 | /* Change the HASH state */ |
NYX | 0:85b3fd62ea1a | 1658 | hhash->State = HAL_HASH_STATE_READY; |
NYX | 0:85b3fd62ea1a | 1659 | |
NYX | 0:85b3fd62ea1a | 1660 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1661 | __HAL_UNLOCK(hhash); |
NYX | 0:85b3fd62ea1a | 1662 | |
NYX | 0:85b3fd62ea1a | 1663 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 1664 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 1665 | } |
NYX | 0:85b3fd62ea1a | 1666 | |
NYX | 0:85b3fd62ea1a | 1667 | /** |
NYX | 0:85b3fd62ea1a | 1668 | * @} |
NYX | 0:85b3fd62ea1a | 1669 | */ |
NYX | 0:85b3fd62ea1a | 1670 | |
NYX | 0:85b3fd62ea1a | 1671 | /** @defgroup HASH_Exported_Functions_Group6 HASH-MAC (HMAC) processing functions using DMA mode |
NYX | 0:85b3fd62ea1a | 1672 | * @brief HMAC processing functions using DMA mode . |
NYX | 0:85b3fd62ea1a | 1673 | * |
NYX | 0:85b3fd62ea1a | 1674 | @verbatim |
NYX | 0:85b3fd62ea1a | 1675 | =============================================================================== |
NYX | 0:85b3fd62ea1a | 1676 | ##### HMAC processing using DMA mode functions ##### |
NYX | 0:85b3fd62ea1a | 1677 | =============================================================================== |
NYX | 0:85b3fd62ea1a | 1678 | [..] This section provides functions allowing to calculate in DMA mode |
NYX | 0:85b3fd62ea1a | 1679 | the HMAC value using one of the following algorithms: |
NYX | 0:85b3fd62ea1a | 1680 | (+) MD5 |
NYX | 0:85b3fd62ea1a | 1681 | (+) SHA1 |
NYX | 0:85b3fd62ea1a | 1682 | |
NYX | 0:85b3fd62ea1a | 1683 | @endverbatim |
NYX | 0:85b3fd62ea1a | 1684 | * @{ |
NYX | 0:85b3fd62ea1a | 1685 | */ |
NYX | 0:85b3fd62ea1a | 1686 | |
NYX | 0:85b3fd62ea1a | 1687 | /** |
NYX | 0:85b3fd62ea1a | 1688 | * @brief Initializes the HASH peripheral in HMAC MD5 mode |
NYX | 0:85b3fd62ea1a | 1689 | * then enables DMA to control data transfer. |
NYX | 0:85b3fd62ea1a | 1690 | * @param hhash: pointer to a HASH_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 1691 | * the configuration information for HASH module |
NYX | 0:85b3fd62ea1a | 1692 | * @param pInBuffer: Pointer to the input buffer (buffer to be hashed). |
NYX | 0:85b3fd62ea1a | 1693 | * @param Size: Length of the input buffer in bytes. |
NYX | 0:85b3fd62ea1a | 1694 | * If the Size is not multiple of 64 bytes, the padding is managed by hardware. |
NYX | 0:85b3fd62ea1a | 1695 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 1696 | */ |
NYX | 0:85b3fd62ea1a | 1697 | HAL_StatusTypeDef HAL_HMAC_MD5_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size) |
NYX | 0:85b3fd62ea1a | 1698 | { |
NYX | 0:85b3fd62ea1a | 1699 | uint32_t inputaddr = 0U; |
NYX | 0:85b3fd62ea1a | 1700 | |
NYX | 0:85b3fd62ea1a | 1701 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 1702 | __HAL_LOCK(hhash); |
NYX | 0:85b3fd62ea1a | 1703 | |
NYX | 0:85b3fd62ea1a | 1704 | /* Change the HASH state */ |
NYX | 0:85b3fd62ea1a | 1705 | hhash->State = HAL_HASH_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 1706 | |
NYX | 0:85b3fd62ea1a | 1707 | /* Save buffer pointer and size in handle */ |
NYX | 0:85b3fd62ea1a | 1708 | hhash->pHashInBuffPtr = pInBuffer; |
NYX | 0:85b3fd62ea1a | 1709 | hhash->HashBuffSize = Size; |
NYX | 0:85b3fd62ea1a | 1710 | hhash->HashInCount = 0U; |
NYX | 0:85b3fd62ea1a | 1711 | |
NYX | 0:85b3fd62ea1a | 1712 | /* Check if initialization phase has already been performed */ |
NYX | 0:85b3fd62ea1a | 1713 | if(hhash->Phase == HAL_HASH_PHASE_READY) |
NYX | 0:85b3fd62ea1a | 1714 | { |
NYX | 0:85b3fd62ea1a | 1715 | /* Check if key size is greater than 64 bytes */ |
NYX | 0:85b3fd62ea1a | 1716 | if(hhash->Init.KeySize > 64U) |
NYX | 0:85b3fd62ea1a | 1717 | { |
NYX | 0:85b3fd62ea1a | 1718 | /* Select the HMAC MD5 mode */ |
NYX | 0:85b3fd62ea1a | 1719 | HASH->CR |= (HASH_ALGOSELECTION_MD5 | HASH_ALGOMODE_HMAC | HASH_HMAC_KEYTYPE_LONGKEY | HASH_CR_INIT); |
NYX | 0:85b3fd62ea1a | 1720 | } |
NYX | 0:85b3fd62ea1a | 1721 | else |
NYX | 0:85b3fd62ea1a | 1722 | { |
NYX | 0:85b3fd62ea1a | 1723 | /* Select the HMAC MD5 mode */ |
NYX | 0:85b3fd62ea1a | 1724 | HASH->CR |= (HASH_ALGOSELECTION_MD5 | HASH_ALGOMODE_HMAC | HASH_CR_INIT); |
NYX | 0:85b3fd62ea1a | 1725 | } |
NYX | 0:85b3fd62ea1a | 1726 | } |
NYX | 0:85b3fd62ea1a | 1727 | |
NYX | 0:85b3fd62ea1a | 1728 | /* Set the phase */ |
NYX | 0:85b3fd62ea1a | 1729 | hhash->Phase = HAL_HASH_PHASE_PROCESS; |
NYX | 0:85b3fd62ea1a | 1730 | |
NYX | 0:85b3fd62ea1a | 1731 | /* Configure the number of valid bits in last word of the message */ |
NYX | 0:85b3fd62ea1a | 1732 | __HAL_HASH_SET_NBVALIDBITS(hhash->Init.KeySize); |
NYX | 0:85b3fd62ea1a | 1733 | |
NYX | 0:85b3fd62ea1a | 1734 | /* Get the key address */ |
NYX | 0:85b3fd62ea1a | 1735 | inputaddr = (uint32_t)(hhash->Init.pKey); |
NYX | 0:85b3fd62ea1a | 1736 | |
NYX | 0:85b3fd62ea1a | 1737 | /* Set the HASH DMA transfer complete callback */ |
NYX | 0:85b3fd62ea1a | 1738 | hhash->hdmain->XferCpltCallback = HASH_DMAXferCplt; |
NYX | 0:85b3fd62ea1a | 1739 | /* Set the DMA error callback */ |
NYX | 0:85b3fd62ea1a | 1740 | hhash->hdmain->XferErrorCallback = HASH_DMAError; |
NYX | 0:85b3fd62ea1a | 1741 | |
NYX | 0:85b3fd62ea1a | 1742 | /* Enable the DMA In DMA Stream */ |
NYX | 0:85b3fd62ea1a | 1743 | HAL_DMA_Start_IT(hhash->hdmain, inputaddr, (uint32_t)&HASH->DIN, (hhash->Init.KeySize%4U ? (hhash->Init.KeySize+3U)/4U:hhash->Init.KeySize/4U)); |
NYX | 0:85b3fd62ea1a | 1744 | /* Enable DMA requests */ |
NYX | 0:85b3fd62ea1a | 1745 | HASH->CR |= (HASH_CR_DMAE); |
NYX | 0:85b3fd62ea1a | 1746 | |
NYX | 0:85b3fd62ea1a | 1747 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1748 | __HAL_UNLOCK(hhash); |
NYX | 0:85b3fd62ea1a | 1749 | |
NYX | 0:85b3fd62ea1a | 1750 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 1751 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 1752 | } |
NYX | 0:85b3fd62ea1a | 1753 | |
NYX | 0:85b3fd62ea1a | 1754 | /** |
NYX | 0:85b3fd62ea1a | 1755 | * @brief Initializes the HASH peripheral in HMAC SHA1 mode |
NYX | 0:85b3fd62ea1a | 1756 | * then enables DMA to control data transfer. |
NYX | 0:85b3fd62ea1a | 1757 | * @param hhash: pointer to a HASH_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 1758 | * the configuration information for HASH module |
NYX | 0:85b3fd62ea1a | 1759 | * @param pInBuffer: Pointer to the input buffer (buffer to be hashed). |
NYX | 0:85b3fd62ea1a | 1760 | * @param Size: Length of the input buffer in bytes. |
NYX | 0:85b3fd62ea1a | 1761 | * If the Size is not multiple of 64 bytes, the padding is managed by hardware. |
NYX | 0:85b3fd62ea1a | 1762 | * @retval HAL status |
NYX | 0:85b3fd62ea1a | 1763 | */ |
NYX | 0:85b3fd62ea1a | 1764 | HAL_StatusTypeDef HAL_HMAC_SHA1_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size) |
NYX | 0:85b3fd62ea1a | 1765 | { |
NYX | 0:85b3fd62ea1a | 1766 | uint32_t inputaddr = 0U; |
NYX | 0:85b3fd62ea1a | 1767 | |
NYX | 0:85b3fd62ea1a | 1768 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 1769 | __HAL_LOCK(hhash); |
NYX | 0:85b3fd62ea1a | 1770 | |
NYX | 0:85b3fd62ea1a | 1771 | /* Change the HASH state */ |
NYX | 0:85b3fd62ea1a | 1772 | hhash->State = HAL_HASH_STATE_BUSY; |
NYX | 0:85b3fd62ea1a | 1773 | |
NYX | 0:85b3fd62ea1a | 1774 | /* Save buffer pointer and size in handle */ |
NYX | 0:85b3fd62ea1a | 1775 | hhash->pHashInBuffPtr = pInBuffer; |
NYX | 0:85b3fd62ea1a | 1776 | hhash->HashBuffSize = Size; |
NYX | 0:85b3fd62ea1a | 1777 | hhash->HashInCount = 0U; |
NYX | 0:85b3fd62ea1a | 1778 | |
NYX | 0:85b3fd62ea1a | 1779 | /* Check if initialization phase has already been performed */ |
NYX | 0:85b3fd62ea1a | 1780 | if(hhash->Phase == HAL_HASH_PHASE_READY) |
NYX | 0:85b3fd62ea1a | 1781 | { |
NYX | 0:85b3fd62ea1a | 1782 | /* Check if key size is greater than 64 bytes */ |
NYX | 0:85b3fd62ea1a | 1783 | if(hhash->Init.KeySize > 64U) |
NYX | 0:85b3fd62ea1a | 1784 | { |
NYX | 0:85b3fd62ea1a | 1785 | /* Select the HMAC SHA1 mode */ |
NYX | 0:85b3fd62ea1a | 1786 | HASH->CR |= (HASH_ALGOSELECTION_SHA1 | HASH_ALGOMODE_HMAC | HASH_HMAC_KEYTYPE_LONGKEY | HASH_CR_INIT); |
NYX | 0:85b3fd62ea1a | 1787 | } |
NYX | 0:85b3fd62ea1a | 1788 | else |
NYX | 0:85b3fd62ea1a | 1789 | { |
NYX | 0:85b3fd62ea1a | 1790 | /* Select the HMAC SHA1 mode */ |
NYX | 0:85b3fd62ea1a | 1791 | HASH->CR |= (HASH_ALGOSELECTION_SHA1 | HASH_ALGOMODE_HMAC | HASH_CR_INIT); |
NYX | 0:85b3fd62ea1a | 1792 | } |
NYX | 0:85b3fd62ea1a | 1793 | } |
NYX | 0:85b3fd62ea1a | 1794 | |
NYX | 0:85b3fd62ea1a | 1795 | /* Set the phase */ |
NYX | 0:85b3fd62ea1a | 1796 | hhash->Phase = HAL_HASH_PHASE_PROCESS; |
NYX | 0:85b3fd62ea1a | 1797 | |
NYX | 0:85b3fd62ea1a | 1798 | /* Configure the number of valid bits in last word of the message */ |
NYX | 0:85b3fd62ea1a | 1799 | __HAL_HASH_SET_NBVALIDBITS(hhash->Init.KeySize); |
NYX | 0:85b3fd62ea1a | 1800 | |
NYX | 0:85b3fd62ea1a | 1801 | /* Get the key address */ |
NYX | 0:85b3fd62ea1a | 1802 | inputaddr = (uint32_t)(hhash->Init.pKey); |
NYX | 0:85b3fd62ea1a | 1803 | |
NYX | 0:85b3fd62ea1a | 1804 | /* Set the HASH DMA transfer complete callback */ |
NYX | 0:85b3fd62ea1a | 1805 | hhash->hdmain->XferCpltCallback = HASH_DMAXferCplt; |
NYX | 0:85b3fd62ea1a | 1806 | /* Set the DMA error callback */ |
NYX | 0:85b3fd62ea1a | 1807 | hhash->hdmain->XferErrorCallback = HASH_DMAError; |
NYX | 0:85b3fd62ea1a | 1808 | |
NYX | 0:85b3fd62ea1a | 1809 | /* Enable the DMA In DMA Stream */ |
NYX | 0:85b3fd62ea1a | 1810 | HAL_DMA_Start_IT(hhash->hdmain, inputaddr, (uint32_t)&HASH->DIN, (hhash->Init.KeySize%4U ? (hhash->Init.KeySize+3U)/4U:hhash->Init.KeySize/4U)); |
NYX | 0:85b3fd62ea1a | 1811 | /* Enable DMA requests */ |
NYX | 0:85b3fd62ea1a | 1812 | HASH->CR |= (HASH_CR_DMAE); |
NYX | 0:85b3fd62ea1a | 1813 | |
NYX | 0:85b3fd62ea1a | 1814 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 1815 | __HAL_UNLOCK(hhash); |
NYX | 0:85b3fd62ea1a | 1816 | |
NYX | 0:85b3fd62ea1a | 1817 | /* Return function status */ |
NYX | 0:85b3fd62ea1a | 1818 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 1819 | } |
NYX | 0:85b3fd62ea1a | 1820 | |
NYX | 0:85b3fd62ea1a | 1821 | /** |
NYX | 0:85b3fd62ea1a | 1822 | * @} |
NYX | 0:85b3fd62ea1a | 1823 | */ |
NYX | 0:85b3fd62ea1a | 1824 | |
NYX | 0:85b3fd62ea1a | 1825 | /** @defgroup HASH_Exported_Functions_Group7 Peripheral State functions |
NYX | 0:85b3fd62ea1a | 1826 | * @brief Peripheral State functions. |
NYX | 0:85b3fd62ea1a | 1827 | * |
NYX | 0:85b3fd62ea1a | 1828 | @verbatim |
NYX | 0:85b3fd62ea1a | 1829 | =============================================================================== |
NYX | 0:85b3fd62ea1a | 1830 | ##### Peripheral State functions ##### |
NYX | 0:85b3fd62ea1a | 1831 | =============================================================================== |
NYX | 0:85b3fd62ea1a | 1832 | [..] |
NYX | 0:85b3fd62ea1a | 1833 | This subsection permits to get in run-time the status of the peripheral. |
NYX | 0:85b3fd62ea1a | 1834 | |
NYX | 0:85b3fd62ea1a | 1835 | @endverbatim |
NYX | 0:85b3fd62ea1a | 1836 | * @{ |
NYX | 0:85b3fd62ea1a | 1837 | */ |
NYX | 0:85b3fd62ea1a | 1838 | |
NYX | 0:85b3fd62ea1a | 1839 | /** |
NYX | 0:85b3fd62ea1a | 1840 | * @brief return the HASH state |
NYX | 0:85b3fd62ea1a | 1841 | * @param hhash: pointer to a HASH_HandleTypeDef structure that contains |
NYX | 0:85b3fd62ea1a | 1842 | * the configuration information for HASH module |
NYX | 0:85b3fd62ea1a | 1843 | * @retval HAL state |
NYX | 0:85b3fd62ea1a | 1844 | */ |
NYX | 0:85b3fd62ea1a | 1845 | HAL_HASH_StateTypeDef HAL_HASH_GetState(HASH_HandleTypeDef *hhash) |
NYX | 0:85b3fd62ea1a | 1846 | { |
NYX | 0:85b3fd62ea1a | 1847 | return hhash->State; |
NYX | 0:85b3fd62ea1a | 1848 | } |
NYX | 0:85b3fd62ea1a | 1849 | |
NYX | 0:85b3fd62ea1a | 1850 | /** |
NYX | 0:85b3fd62ea1a | 1851 | * @} |
NYX | 0:85b3fd62ea1a | 1852 | */ |
NYX | 0:85b3fd62ea1a | 1853 | |
NYX | 0:85b3fd62ea1a | 1854 | /** |
NYX | 0:85b3fd62ea1a | 1855 | * @} |
NYX | 0:85b3fd62ea1a | 1856 | */ |
NYX | 0:85b3fd62ea1a | 1857 | |
NYX | 0:85b3fd62ea1a | 1858 | #endif /* STM32F415xx || STM32F417xx || STM32F437xx || STM32F439xx || STM32F479xx */ |
NYX | 0:85b3fd62ea1a | 1859 | #endif /* HAL_HASH_MODULE_ENABLED */ |
NYX | 0:85b3fd62ea1a | 1860 | /** |
NYX | 0:85b3fd62ea1a | 1861 | * @} |
NYX | 0:85b3fd62ea1a | 1862 | */ |
NYX | 0:85b3fd62ea1a | 1863 | |
NYX | 0:85b3fd62ea1a | 1864 | /** |
NYX | 0:85b3fd62ea1a | 1865 | * @} |
NYX | 0:85b3fd62ea1a | 1866 | */ |
NYX | 0:85b3fd62ea1a | 1867 | |
NYX | 0:85b3fd62ea1a | 1868 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |