Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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