CARME-M4 BSP  V1.5
stm32f4xx_hash.c
Go to the documentation of this file.
1 
122 /* Includes ------------------------------------------------------------------*/
123 #include "stm32f4xx_hash.h"
124 #include "stm32f4xx_rcc.h"
125 
135 /* Private typedef -----------------------------------------------------------*/
136 /* Private define ------------------------------------------------------------*/
137 /* Private macro -------------------------------------------------------------*/
138 /* Private variables ---------------------------------------------------------*/
139 /* Private function prototypes -----------------------------------------------*/
140 /* Private functions ---------------------------------------------------------*/
141 
171 void HASH_DeInit(void)
172 {
173  /* Enable HASH reset state */
174  RCC_AHB2PeriphResetCmd(RCC_AHB2Periph_HASH, ENABLE);
175  /* Release HASH from reset state */
176  RCC_AHB2PeriphResetCmd(RCC_AHB2Periph_HASH, DISABLE);
177 }
178 
191 void HASH_Init(HASH_InitTypeDef* HASH_InitStruct)
192 {
193  /* Check the parameters */
194  assert_param(IS_HASH_ALGOSELECTION(HASH_InitStruct->HASH_AlgoSelection));
195  assert_param(IS_HASH_DATATYPE(HASH_InitStruct->HASH_DataType));
196  assert_param(IS_HASH_ALGOMODE(HASH_InitStruct->HASH_AlgoMode));
197 
198  /* Configure the Algorithm used, algorithm mode and the datatype */
199  HASH->CR &= ~ (HASH_CR_ALGO | HASH_CR_DATATYPE | HASH_CR_MODE);
200  HASH->CR |= (HASH_InitStruct->HASH_AlgoSelection | \
201  HASH_InitStruct->HASH_DataType | \
202  HASH_InitStruct->HASH_AlgoMode);
203 
204  /* if algorithm mode is HMAC, set the Key */
205  if(HASH_InitStruct->HASH_AlgoMode == HASH_AlgoMode_HMAC)
206  {
207  assert_param(IS_HASH_HMAC_KEYTYPE(HASH_InitStruct->HASH_HMACKeyType));
208  HASH->CR &= ~HASH_CR_LKEY;
209  HASH->CR |= HASH_InitStruct->HASH_HMACKeyType;
210  }
211 
212  /* Reset the HASH processor core, so that the HASH will be ready to compute
213  the message digest of a new message */
214  HASH->CR |= HASH_CR_INIT;
215 }
216 
225 void HASH_StructInit(HASH_InitTypeDef* HASH_InitStruct)
226 {
227  /* Initialize the HASH_AlgoSelection member */
228  HASH_InitStruct->HASH_AlgoSelection = HASH_AlgoSelection_SHA1;
229 
230  /* Initialize the HASH_AlgoMode member */
231  HASH_InitStruct->HASH_AlgoMode = HASH_AlgoMode_HASH;
232 
233  /* Initialize the HASH_DataType member */
234  HASH_InitStruct->HASH_DataType = HASH_DataType_32b;
235 
236  /* Initialize the HASH_HMACKeyType member */
237  HASH_InitStruct->HASH_HMACKeyType = HASH_HMACKeyType_ShortKey;
238 }
239 
249 void HASH_Reset(void)
250 {
251  /* Reset the HASH processor core */
252  HASH->CR |= HASH_CR_INIT;
253 }
291 void HASH_SetLastWordValidBitsNbr(uint16_t ValidNumber)
292 {
293  /* Check the parameters */
294  assert_param(IS_HASH_VALIDBITSNUMBER(ValidNumber));
295 
296  /* Configure the Number of valid bits in last word of the message */
297  HASH->STR &= ~(HASH_STR_NBW);
298  HASH->STR |= ValidNumber;
299 }
300 
306 void HASH_DataIn(uint32_t Data)
307 {
308  /* Write in the DIN register a new data */
309  HASH->DIN = Data;
310 }
311 
318 {
319  /* Return the value of NBW bits */
320  return ((HASH->CR & HASH_CR_NBW) >> 8);
321 }
322 
335 void HASH_GetDigest(HASH_MsgDigest* HASH_MessageDigest)
336 {
337  /* Get the data field */
338  HASH_MessageDigest->Data[0] = HASH->HR[0];
339  HASH_MessageDigest->Data[1] = HASH->HR[1];
340  HASH_MessageDigest->Data[2] = HASH->HR[2];
341  HASH_MessageDigest->Data[3] = HASH->HR[3];
342  HASH_MessageDigest->Data[4] = HASH->HR[4];
343  HASH_MessageDigest->Data[5] = HASH_DIGEST->HR[5];
344  HASH_MessageDigest->Data[6] = HASH_DIGEST->HR[6];
345  HASH_MessageDigest->Data[7] = HASH_DIGEST->HR[7];
346 }
347 
354 {
355  /* Start the Digest calculation */
356  HASH->STR |= HASH_STR_DCAL;
357 }
396 void HASH_SaveContext(HASH_Context* HASH_ContextSave)
397 {
398  uint8_t i = 0;
399 
400  /* save context registers */
401  HASH_ContextSave->HASH_IMR = HASH->IMR;
402  HASH_ContextSave->HASH_STR = HASH->STR;
403  HASH_ContextSave->HASH_CR = HASH->CR;
404  for(i=0; i<=53;i++)
405  {
406  HASH_ContextSave->HASH_CSR[i] = HASH->CSR[i];
407  }
408 }
409 
418 void HASH_RestoreContext(HASH_Context* HASH_ContextRestore)
419 {
420  uint8_t i = 0;
421 
422  /* restore context registers */
423  HASH->IMR = HASH_ContextRestore->HASH_IMR;
424  HASH->STR = HASH_ContextRestore->HASH_STR;
425  HASH->CR = HASH_ContextRestore->HASH_CR;
426 
427  /* Initialize the hash processor */
428  HASH->CR |= HASH_CR_INIT;
429 
430  /* continue restoring context registers */
431  for(i=0; i<=53;i++)
432  {
433  HASH->CSR[i] = HASH_ContextRestore->HASH_CSR[i];
434  }
435 }
465 void HASH_AutoStartDigest(FunctionalState NewState)
466 {
467  /* Check the parameters */
468  assert_param(IS_FUNCTIONAL_STATE(NewState));
469 
470  if (NewState != DISABLE)
471  {
472  /* Enable the auto start of the final message digest at the end of DMA transfer */
473  HASH->CR &= ~HASH_CR_MDMAT;
474  }
475  else
476  {
477  /* Disable the auto start of the final message digest at the end of DMA transfer */
478  HASH->CR |= HASH_CR_MDMAT;
479  }
480 }
481 
489 void HASH_DMACmd(FunctionalState NewState)
490 {
491  /* Check the parameters */
492  assert_param(IS_FUNCTIONAL_STATE(NewState));
493 
494  if (NewState != DISABLE)
495  {
496  /* Enable the HASH DMA request */
497  HASH->CR |= HASH_CR_DMAE;
498  }
499  else
500  {
501  /* Disable the HASH DMA request */
502  HASH->CR &= ~HASH_CR_DMAE;
503  }
504 }
581 void HASH_ITConfig(uint32_t HASH_IT, FunctionalState NewState)
582 {
583  /* Check the parameters */
584  assert_param(IS_HASH_IT(HASH_IT));
585  assert_param(IS_FUNCTIONAL_STATE(NewState));
586 
587  if (NewState != DISABLE)
588  {
589  /* Enable the selected HASH interrupt */
590  HASH->IMR |= HASH_IT;
591  }
592  else
593  {
594  /* Disable the selected HASH interrupt */
595  HASH->IMR &= (uint32_t)(~HASH_IT);
596  }
597 }
598 
610 FlagStatus HASH_GetFlagStatus(uint32_t HASH_FLAG)
611 {
612  FlagStatus bitstatus = RESET;
613  uint32_t tempreg = 0;
614 
615  /* Check the parameters */
616  assert_param(IS_HASH_GET_FLAG(HASH_FLAG));
617 
618  /* check if the FLAG is in CR register */
619  if ((HASH_FLAG & HASH_FLAG_DINNE) != (uint32_t)RESET )
620  {
621  tempreg = HASH->CR;
622  }
623  else /* The FLAG is in SR register */
624  {
625  tempreg = HASH->SR;
626  }
627 
628  /* Check the status of the specified HASH flag */
629  if ((tempreg & HASH_FLAG) != (uint32_t)RESET)
630  {
631  /* HASH is set */
632  bitstatus = SET;
633  }
634  else
635  {
636  /* HASH_FLAG is reset */
637  bitstatus = RESET;
638  }
639 
640  /* Return the HASH_FLAG status */
641  return bitstatus;
642 }
651 void HASH_ClearFlag(uint32_t HASH_FLAG)
652 {
653  /* Check the parameters */
654  assert_param(IS_HASH_CLEAR_FLAG(HASH_FLAG));
655 
656  /* Clear the selected HASH flags */
657  HASH->SR = ~(uint32_t)HASH_FLAG;
658 }
667 ITStatus HASH_GetITStatus(uint32_t HASH_IT)
668 {
669  ITStatus bitstatus = RESET;
670  uint32_t tmpreg = 0;
671 
672  /* Check the parameters */
673  assert_param(IS_HASH_GET_IT(HASH_IT));
674 
675 
676  /* Check the status of the specified HASH interrupt */
677  tmpreg = HASH->SR;
678 
679  if (((HASH->IMR & tmpreg) & HASH_IT) != RESET)
680  {
681  /* HASH_IT is set */
682  bitstatus = SET;
683  }
684  else
685  {
686  /* HASH_IT is reset */
687  bitstatus = RESET;
688  }
689  /* Return the HASH_IT status */
690  return bitstatus;
691 }
692 
701 void HASH_ClearITPendingBit(uint32_t HASH_IT)
702 {
703  /* Check the parameters */
704  assert_param(IS_HASH_IT(HASH_IT));
705 
706  /* Clear the selected HASH interrupt pending bit */
707  HASH->SR = (uint32_t)(~HASH_IT);
708 }
709 
726 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
This file contains all the functions prototypes for the RCC firmware library.
void HASH_Init(HASH_InitTypeDef *HASH_InitStruct)
Initializes the HASH peripheral according to the specified parameters in the HASH_InitStruct structur...
void HASH_ITConfig(uint32_t HASH_IT, FunctionalState NewState)
Enables or disables the specified HASH interrupts.
#define HASH_FLAG_DINNE
uint32_t HASH_AlgoMode
FlagStatus HASH_GetFlagStatus(uint32_t HASH_FLAG)
Checks whether the specified HASH flag is set or not.
uint32_t HASH_DataType
void HASH_StartDigest(void)
Starts the message padding and calculation of the final message.
void HASH_DMACmd(FunctionalState NewState)
Enables or disables the HASH DMA interface.
#define HASH_HMACKeyType_ShortKey
void HASH_ClearFlag(uint32_t HASH_FLAG)
Clears the HASH flags.
void HASH_Reset(void)
Resets the HASH processor core, so that the HASH will be ready to compute the message digest of a new...
uint8_t HASH_GetInFIFOWordsNbr(void)
Returns the number of words already pushed into the IN FIFO.
void HASH_DataIn(uint32_t Data)
Writes data in the Data Input FIFO.
uint32_t HASH_AlgoSelection
#define HASH_AlgoMode_HMAC
HASH context swapping structure definition.
ITStatus HASH_GetITStatus(uint32_t HASH_IT)
Checks whether the specified HASH interrupt has occurred or not.
void HASH_SetLastWordValidBitsNbr(uint16_t ValidNumber)
Configure the Number of valid bits in last word of the message.
HASH message digest result structure definition.
This file contains all the functions prototypes for the HASH firmware library.
void HASH_StructInit(HASH_InitTypeDef *HASH_InitStruct)
Fills each HASH_InitStruct member with its default value.
void HASH_DeInit(void)
De-initializes the HASH peripheral registers to their default reset values.
void HASH_AutoStartDigest(FunctionalState NewState)
Enables or disables auto-start message padding and calculation of the final message digest at the end...
void HASH_SaveContext(HASH_Context *HASH_ContextSave)
Save the Hash peripheral Context.
#define HASH_AlgoMode_HASH
void HASH_RestoreContext(HASH_Context *HASH_ContextRestore)
Restore the Hash peripheral Context.
#define HASH_AlgoSelection_SHA1
void RCC_AHB2PeriphResetCmd(uint32_t RCC_AHB2Periph, FunctionalState NewState)
Forces or releases AHB2 peripheral reset.
#define HASH_DataType_32b
uint32_t Data[8]
HASH Init structure definition.
void HASH_GetDigest(HASH_MsgDigest *HASH_MessageDigest)
Provides the message digest result.
void HASH_ClearITPendingBit(uint32_t HASH_IT)
Clears the HASH interrupt pending bit(s).
uint32_t HASH_HMACKeyType