mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Thu Jul 02 16:30:08 2015 +0100
Revision:
581:39197bcd20f2
Parent:
532:fe11edbda85c
Child:
613:bc40b8d2aec4
Synchronized with git revision ae2d3cdffe70184eb8736d94f76c45c93f4b7724

Full URL: https://github.com/mbedmicro/mbed/commit/ae2d3cdffe70184eb8736d94f76c45c93f4b7724/

Make it possible to build the core mbed library with yotta

Who changed what in which revision?

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