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