STM32L4xx_HAL_Driver  1.14.0
AES extended processing functions

Extended processing functions. More...

Functions

HAL_StatusTypeDef HAL_CRYPEx_AES (CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint16_t Size, uint8_t *pOutputData, uint32_t Timeout)
 Carry out in polling mode the ciphering or deciphering operation according to hcryp->Init structure fields, all operating modes (encryption, key derivation and/or decryption) and chaining modes ECB, CBC and CTR are managed by this function in polling mode. More...
 
HAL_StatusTypeDef HAL_CRYPEx_AES_IT (CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint16_t Size, uint8_t *pOutputData)
 Carry out in interrupt mode the ciphering or deciphering operation according to hcryp->Init structure fields, all operating modes (encryption, key derivation and/or decryption) and chaining modes ECB, CBC and CTR are managed by this function in interrupt mode. More...
 
HAL_StatusTypeDef HAL_CRYPEx_AES_DMA (CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint16_t Size, uint8_t *pOutputData)
 Carry out in DMA mode the ciphering or deciphering operation according to hcryp->Init structure fields. More...
 
HAL_StatusTypeDef HAL_CRYPEx_AES_Auth (CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint64_t Size, uint8_t *pOutputData, uint32_t Timeout)
 Carry out in polling mode the authentication tag generation as well as the ciphering or deciphering operation according to hcryp->Init structure fields. More...
 
HAL_StatusTypeDef HAL_CRYPEx_AES_Auth_IT (CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint64_t Size, uint8_t *pOutputData)
 Carry out in interrupt mode the authentication tag generation as well as the ciphering or deciphering operation according to hcryp->Init structure fields. More...
 
HAL_StatusTypeDef HAL_CRYPEx_AES_Auth_DMA (CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint64_t Size, uint8_t *pOutputData)
 Carry out in DMA mode the authentication tag generation as well as the ciphering or deciphering operation according to hcryp->Init structure fields. More...
 

Detailed Description

Extended processing functions.

  ==============================================================================
                      ##### AES extended processing functions #####
  ==============================================================================
    [..]  This section provides functions allowing to:
      (+) Encrypt plaintext or decrypt cipher text using AES algorithm in different chaining modes.
          Functions are generic (handles ECB, CBC and CTR and all modes) and are only differentiated
          based on the processing type. Three processing types are available:
          (++) Polling mode
          (++) Interrupt mode
          (++) DMA mode
      (+) Generate and authentication tag in addition to encrypt/decrypt a plain/cipher text using AES
          algorithm in different chaining modes.
          Functions are generic (handles GCM, GMAC, CMAC and CCM when applicable) and process only one phase
          so that steps can be skipped if so required. Functions are only differentiated based on the processing type.
          Three processing types are available:
          (++) Polling mode
          (++) Interrupt mode
          (++) DMA mode

Function Documentation

◆ HAL_CRYPEx_AES()

HAL_StatusTypeDef HAL_CRYPEx_AES ( CRYP_HandleTypeDef hcryp,
uint8_t *  pInputData,
uint16_t  Size,
uint8_t *  pOutputData,
uint32_t  Timeout 
)

Carry out in polling mode the ciphering or deciphering operation according to hcryp->Init structure fields, all operating modes (encryption, key derivation and/or decryption) and chaining modes ECB, CBC and CTR are managed by this function in polling mode.

Parameters
hcryppointer to a CRYP_HandleTypeDef structure that contains the configuration information for CRYP module
pInputDataPointer to the plain text in case of encryption or cipher text in case of decryption or key derivation+decryption. Parameter is meaningless in case of key derivation.
SizeLength of the input data buffer in bytes, must be a multiple of 16. Parameter is meaningless in case of key derivation.
pOutputDataPointer to the cipher text in case of encryption or plain text in case of decryption/key derivation+decryption, or pointer to the derivative keys in case of key derivation only.
TimeoutSpecify Timeout value
Return values
HALstatus

Definition at line 166 of file stm32l4xx_hal_cryp_ex.c.

167 {
168 
169  if (hcryp->State == HAL_CRYP_STATE_READY)
170  {
171  /* Check parameters setting */
172  if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
173  {
174  if (pOutputData == NULL)
175  {
176  return HAL_ERROR;
177  }
178  }
179  else
180  {
181  if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0U))
182  {
183  return HAL_ERROR;
184  }
185  }
186 
187  /* Process Locked */
188  __HAL_LOCK(hcryp);
189 
190  /* Change the CRYP state */
191  hcryp->State = HAL_CRYP_STATE_BUSY;
192 
193  /* Call CRYP_ReadKey() API if the operating mode is set to
194  key derivation, CRYP_ProcessData() otherwise */
195  if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
196  {
197  if(CRYP_ReadKey(hcryp, pOutputData, Timeout) != HAL_OK)
198  {
199  return HAL_TIMEOUT;
200  }
201  }
202  else
203  {
204  if(CRYP_ProcessData(hcryp, pInputData, Size, pOutputData, Timeout) != HAL_OK)
205  {
206  return HAL_TIMEOUT;
207  }
208  }
209 
210  /* If the state has not been set to SUSPENDED, set it to
211  READY, otherwise keep it as it is */
212  if (hcryp->State != HAL_CRYP_STATE_SUSPENDED)
213  {
214  hcryp->State = HAL_CRYP_STATE_READY;
215  }
216 
217  /* Process Unlocked */
218  __HAL_UNLOCK(hcryp);
219 
220  return HAL_OK;
221  }
222  else
223  {
224  return HAL_BUSY;
225  }
226 }
__IO HAL_CRYP_STATETypeDef State
__HAL_UNLOCK(hrtc)
__HAL_LOCK(hrtc)
return HAL_OK
static HAL_StatusTypeDef CRYP_ReadKey(CRYP_HandleTypeDef *hcryp, uint8_t *Output, uint32_t Timeout)
Read derivative key in polling mode when CRYP hardware block is set in key derivation operating mode ...
static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t *Input, uint16_t Ilength, uint8_t *Output, uint32_t Timeout)
Write/read input/output data in polling mode.

◆ HAL_CRYPEx_AES_Auth()

HAL_StatusTypeDef HAL_CRYPEx_AES_Auth ( CRYP_HandleTypeDef hcryp,
uint8_t *  pInputData,
uint64_t  Size,
uint8_t *  pOutputData,
uint32_t  Timeout 
)

Carry out in polling mode the authentication tag generation as well as the ciphering or deciphering operation according to hcryp->Init structure fields.

Parameters
hcryppointer to a CRYP_HandleTypeDef structure that contains the configuration information for CRYP module
pInputData
  • pointer to payload data in GCM or CCM payload phase,
  • pointer to B0 block in CMAC header phase,
  • pointer to C block in CMAC final phase.
  • Parameter is meaningless in case of GCM/GMAC/CCM init, header and final phases.
Size
  • length of the input payload data buffer in bytes in GCM or CCM payload phase,
  • length of B0 block (in bytes) in CMAC header phase,
  • length of C block (in bytes) in CMAC final phase.
  • Parameter is meaningless in case of GCM/GMAC/CCM init and header phases.
  • Parameter is meaningless in case of CCM final phase.
  • Parameter is message length in bytes in case of GCM final phase.
  • Parameter must be set to zero in case of GMAC final phase.
pOutputData
  • pointer to plain or cipher text in GCM/CCM payload phase,
  • pointer to authentication tag in GCM/GMAC/CCM/CMAC final phase.
  • Parameter is meaningless in case of GCM/GMAC/CCM init and header phases.
  • Parameter is meaningless in case of CMAC header phase.
TimeoutSpecify Timeout value
Note
Supported operating modes are encryption and decryption, supported chaining modes are GCM, GMAC, CMAC and CCM when the latter is applicable.
Phases are singly processed according to hcryp->Init.GCMCMACPhase so that steps in these specific chaining modes can be skipped by the user if so required.
Return values
HALstatus

Definition at line 432 of file stm32l4xx_hal_cryp_ex.c.

433 {
434  uint32_t index ;
435  uint32_t inputaddr ;
436  uint32_t outputaddr ;
437  uint32_t tagaddr ;
438  uint64_t headerlength ;
439  uint64_t inputlength ;
440  uint64_t payloadlength ;
441  uint32_t difflength = 0;
442  uint32_t addhoc_process = 0;
443 
444  if (hcryp->State == HAL_CRYP_STATE_READY)
445  {
446  /* input/output parameters check */
447  if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE)
448  {
449  /* No processing required */
450  }
451  else if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
452  {
453  if (((hcryp->Init.Header != NULL) && (hcryp->Init.HeaderSize == 0U)) ||
454  ((hcryp->Init.Header == NULL) && (hcryp->Init.HeaderSize != 0U)))
455  {
456  return HAL_ERROR;
457  }
458 #if defined(AES_CR_NPBLB)
459  if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM)
460 #else
461  if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
462 #endif
463  {
464  /* In case of CMAC or CCM (when applicable) header phase resumption, we can have pInputData = NULL and Size = 0 */
465  if (((pInputData != NULL) && (Size == 0U)) || ((pInputData == NULL) && (Size != 0U)))
466  {
467  return HAL_ERROR;
468  }
469  }
470  }
471  else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
472  {
473  if (((pInputData == NULL) && (Size != 0U)) || \
474  ((pInputData != NULL) && (Size == 0U)) || \
475  ((pInputData != NULL) && (Size != 0U) && (pOutputData == NULL)))
476  {
477  return HAL_ERROR;
478  }
479  }
480  else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE)
481  {
482  if (pOutputData == NULL)
483  {
484  return HAL_ERROR;
485  }
486 #if !defined(AES_CR_NPBLB)
487  if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (pInputData == NULL))
488  {
489  return HAL_ERROR;
490  }
491 #endif
492  }
493  else
494  {
495  /* Unspecified Phase */
496  return HAL_ERROR;
497  }
498 
499 
500  /* Process Locked */
501  __HAL_LOCK(hcryp);
502 
503  /* Change the CRYP state */
504  hcryp->State = HAL_CRYP_STATE_BUSY;
505 
506  /*==============================================*/
507  /* GCM/GMAC (or CCM when applicable) init phase */
508  /*==============================================*/
509  /* In case of init phase, the input data (Key and Initialization Vector) have
510  already been entered during the initialization process. Therefore, the
511  API just waits for the CCF flag to be set. */
512  if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE)
513  {
514  /* just wait for hash computation */
515  if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
516  {
517  hcryp->State = HAL_CRYP_STATE_READY;
518  __HAL_UNLOCK(hcryp);
519  return HAL_TIMEOUT;
520  }
521 
522  /* Clear CCF Flag */
523  __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
524  /* Mark that the initialization phase is over */
526  }
527  /*=======================================================*/
528  /* GCM/GMAC or (CCM / CMAC when applicable) header phase */
529  /*=======================================================*/
530  else if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
531  {
532 #if !defined(AES_CR_NPBLB)
533  /* Set header phase; for GCM or GMAC, set data-byte at this point */
534  if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
535  {
536  MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH|AES_CR_DATATYPE, CRYP_HEADER_PHASE|hcryp->Init.DataType);
537  }
538  else
539 #endif
540  {
541  MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_HEADER_PHASE);
542  }
543 
544  /* Enable the Peripheral */
545  __HAL_CRYP_ENABLE(hcryp);
546 
547 #if !defined(AES_CR_NPBLB)
548  /* in case of CMAC, enter B0 block in header phase, before the header itself. */
549  /* If Size = 0 (possible case of resumption after CMAC header phase suspension),
550  skip these steps and go directly to header buffer feeding to the HW */
551  if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (Size != 0U))
552  {
553  uint64_t index_test;
554  inputaddr = (uint32_t)pInputData;
555 
556  for(index=0U ; (index < Size); index += 16U)
557  {
558  /* Write the Input block in the Data Input register */
559  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
560  inputaddr+=4U;
561  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
562  inputaddr+=4U;
563  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
564  inputaddr+=4U;
565  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
566  inputaddr+=4U;
567 
568  if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
569  {
570  hcryp->State = HAL_CRYP_STATE_READY;
571  __HAL_UNLOCK(hcryp);
572  return HAL_TIMEOUT;
573  }
574  /* Clear CCF Flag */
575  __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
576 
577  /* If the suspension flag has been raised and if the processing is not about
578  to end, suspend processing */
579  index_test = (uint64_t)index + 16U;
580  if ((hcryp->SuspendRequest == HAL_CRYP_SUSPEND) && (index_test < Size))
581  {
582  /* reset SuspendRequest */
584  /* Change the CRYP state */
586  /* Mark that the header phase is over */
588 
589  /* Save current reading and writing locations of Input and Output buffers */
590  hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
591  /* Save the total number of bytes (B blocks + header) that remain to be
592  processed at this point */
593  hcryp->CrypInCount = (uint32_t) (hcryp->Init.HeaderSize + Size - index_test);
594 
595  /* Process Unlocked */
596  __HAL_UNLOCK(hcryp);
597 
598  return HAL_OK;
599  }
600  } /* for(index=0; (index < Size); index += 16) */
601  }
602 #endif /* !defined(AES_CR_NPBLB) */
603 
604  /* Enter header */
605  inputaddr = (uint32_t)hcryp->Init.Header;
606  /* Local variable headerlength is a number of bytes multiple of 128 bits,
607  remaining header data (if any) are handled after this loop */
608  headerlength = (((hcryp->Init.HeaderSize)/16U)*16U) ;
609  if ((hcryp->Init.HeaderSize % 16U) != 0U)
610  {
611  difflength = (uint32_t) (hcryp->Init.HeaderSize - headerlength);
612  }
613  for(index=0U ; index < headerlength; index += 16U)
614  {
615  uint64_t index_temp;
616  /* Write the Input block in the Data Input register */
617  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
618  inputaddr+=4U;
619  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
620  inputaddr+=4U;
621  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
622  inputaddr+=4U;
623  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
624  inputaddr+=4U;
625 
626  if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
627  {
628  hcryp->State = HAL_CRYP_STATE_READY;
629  __HAL_UNLOCK(hcryp);
630  return HAL_TIMEOUT;
631  }
632  /* Clear CCF Flag */
633  __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
634 
635  /* If the suspension flag has been raised and if the processing is not about
636  to end, suspend processing */
637  index_temp = (uint64_t)index + 16U;
638  if ((hcryp->SuspendRequest == HAL_CRYP_SUSPEND) && (index_temp < headerlength))
639  {
640  /* reset SuspendRequest */
642  /* Change the CRYP state */
644  /* Mark that the header phase is over */
646 
647  /* Save current reading and writing locations of Input and Output buffers */
648  hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
649  /* Save the total number of bytes that remain to be processed at this point */
650  hcryp->CrypInCount = (uint32_t) (hcryp->Init.HeaderSize - index_temp);
651 
652  /* Process Unlocked */
653  __HAL_UNLOCK(hcryp);
654 
655  return HAL_OK;
656  }
657  }
658 
659  /* Case header length is not a multiple of 16 bytes */
660  if (difflength != 0U)
661  {
662  hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
663  CRYP_Padding(hcryp, difflength, CRYP_POLLING_ON);
664  }
665 
666  /* Mark that the header phase is over */
668  }
669  /*============================================*/
670  /* GCM (or CCM when applicable) payload phase */
671  /*============================================*/
672  else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
673  {
674 
675  MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PAYLOAD_PHASE);
676 
677  /* if the header phase has been bypassed, AES must be enabled again */
678  if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER)
679  {
680  __HAL_CRYP_ENABLE(hcryp);
681  }
682 
683  inputaddr = (uint32_t)pInputData;
684  outputaddr = (uint32_t)pOutputData;
685 
686  /* Enter payload */
687  /* Specific handling to manage payload last block size less than 128 bits */
688  if ((Size % 16U) != 0U)
689  {
690  payloadlength = (Size/16U) * 16U;
691  difflength = (uint32_t) (Size - payloadlength);
692  addhoc_process = 1;
693  }
694  else
695  {
696  payloadlength = Size;
697  }
698 
699  /* Feed payload */
700  for(index=0U ; index < payloadlength; index += 16U)
701  {
702  uint64_t index_temp;
703  /* Write the Input block in the Data Input register */
704  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
705  inputaddr+=4U;
706  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
707  inputaddr+=4U;
708  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
709  inputaddr+=4U;
710  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
711  inputaddr+=4U;
712 
713  if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
714  {
715  hcryp->State = HAL_CRYP_STATE_READY;
716  __HAL_UNLOCK(hcryp);
717  return HAL_TIMEOUT;
718  }
719 
720  /* Clear CCF Flag */
721  __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
722 
723  /* Retrieve output data: read the output block
724  from the Data Output Register */
725  *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
726  outputaddr+=4U;
727  *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
728  outputaddr+=4U;
729  *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
730  outputaddr+=4U;
731  *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
732  outputaddr+=4U;
733 
734  /* If the suspension flag has been raised and if the processing is not about
735  to end, suspend processing */
736  index_temp = (uint64_t)index + 16U;
737  if ((hcryp->SuspendRequest == HAL_CRYP_SUSPEND) && (index_temp < payloadlength))
738  {
739  /* no flag waiting under IRQ handling */
740  if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT)
741  {
742  /* Ensure that Busy flag is reset */
743  if(CRYP_WaitOnBusyFlagReset(hcryp, CRYP_BUSY_TIMEOUTVALUE) != HAL_OK)
744  {
745  hcryp->State = HAL_CRYP_STATE_READY;
746  __HAL_UNLOCK(hcryp);
747  return HAL_TIMEOUT;
748  }
749  }
750  /* reset SuspendRequest */
752  /* Change the CRYP state */
754  /* Mark that the header phase is over */
756 
757  /* Save current reading and writing locations of Input and Output buffers */
758  hcryp->pCrypOutBuffPtr = (uint8_t *)outputaddr;
759  hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
760  /* Save the number of bytes that remain to be processed at this point */
761  hcryp->CrypInCount = (uint32_t) (Size - index_temp);
762 
763  /* Process Unlocked */
764  __HAL_UNLOCK(hcryp);
765 
766  return HAL_OK;
767  }
768 
769  }
770 
771  /* Additional processing to manage GCM(/CCM) encryption and decryption cases when
772  payload last block size less than 128 bits */
773  if (addhoc_process == 1U)
774  {
775 
776  hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
777  hcryp->pCrypOutBuffPtr = (uint8_t *)outputaddr;
778  CRYP_Padding(hcryp, difflength, CRYP_POLLING_ON);
779 
780  } /* (addhoc_process == 1) */
781 
782  /* Mark that the payload phase is over */
784  }
785  /*==================================*/
786  /* GCM/GMAC/CCM or CMAC final phase */
787  /*==================================*/
788  else
789  {
790  tagaddr = (uint32_t)pOutputData;
791 
792 #if defined(AES_CR_NPBLB)
793  /* By default, clear NPBLB field */
794  CLEAR_BIT(hcryp->Instance->CR, AES_CR_NPBLB);
795 #endif
796 
797  MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_FINAL_PHASE);
798 
799  /* if the header and payload phases have been bypassed, AES must be enabled again */
800  if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER)
801  {
802  __HAL_CRYP_ENABLE(hcryp);
803  }
804 
805  if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
806  {
807  headerlength = hcryp->Init.HeaderSize * 8U; /* Header length in bits */
808  inputlength = Size * 8U; /* input length in bits */
809 
810 #if !defined(AES_CR_NPBLB)
811  if(hcryp->Init.DataType == CRYP_DATATYPE_1B)
812  {
813  hcryp->Instance->DINR = __RBIT((uint32_t)(headerlength>>32));
814  hcryp->Instance->DINR = __RBIT((uint32_t)headerlength);
815  hcryp->Instance->DINR = __RBIT((uint32_t)(inputlength>>32));
816  hcryp->Instance->DINR = __RBIT((uint32_t)inputlength);
817  }
818  else if(hcryp->Init.DataType == CRYP_DATATYPE_8B)
819  {
820  hcryp->Instance->DINR = __REV((uint32_t)(headerlength>>32));
821  hcryp->Instance->DINR = __REV((uint32_t)headerlength);
822  hcryp->Instance->DINR = __REV((uint32_t)(inputlength>>32));
823  hcryp->Instance->DINR = __REV((uint32_t)inputlength);
824  }
825  else if(hcryp->Init.DataType == CRYP_DATATYPE_16B)
826  {
827  hcryp->Instance->DINR = __ROR((uint32_t)(headerlength>>32), 16);
828  hcryp->Instance->DINR = __ROR((uint32_t)headerlength, 16);
829  hcryp->Instance->DINR = __ROR((uint32_t)(inputlength>>32), 16);
830  hcryp->Instance->DINR = __ROR((uint32_t)inputlength, 16);
831  }
832  else if(hcryp->Init.DataType == CRYP_DATATYPE_32B)
833  {
834  hcryp->Instance->DINR = (uint32_t)(headerlength>>32);
835  hcryp->Instance->DINR = (uint32_t)(headerlength);
836  hcryp->Instance->DINR = (uint32_t)(inputlength>>32);
837  hcryp->Instance->DINR = (uint32_t)(inputlength);
838  }
839  else
840  {
841  /* Unspecified Data Type */
842  return HAL_ERROR;
843  }
844 #else
845  hcryp->Instance->DINR = (uint32_t)(headerlength>>32);
846  hcryp->Instance->DINR = (uint32_t)(headerlength);
847  hcryp->Instance->DINR = (uint32_t)(inputlength>>32);
848  hcryp->Instance->DINR = (uint32_t)(inputlength);
849 #endif
850  }
851 #if !defined(AES_CR_NPBLB)
852  else if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
853  {
854  inputaddr = (uint32_t)pInputData;
855  /* Enter the last block made of a 128-bit value formatted
856  from the original B0 packet. */
857  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
858  inputaddr+=4U;
859  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
860  inputaddr+=4U;
861  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
862  inputaddr+=4U;
863  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
864  }
865  else
866  {
867  /* Unspecified Chaining Mode */
868  return HAL_ERROR;
869  }
870 #endif
871 
872 
873  if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
874  {
875  hcryp->State = HAL_CRYP_STATE_READY;
876  __HAL_UNLOCK(hcryp);
877  return HAL_TIMEOUT;
878  }
879 
880  /* Read the Auth TAG in the Data Out register */
881  *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
882  tagaddr+=4U;
883  *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
884  tagaddr+=4U;
885  *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
886  tagaddr+=4U;
887  *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
888 
889 
890  /* Clear CCF Flag */
891  __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
892  /* Mark that the final phase is over */
894  /* Disable the Peripheral */
895  __HAL_CRYP_DISABLE(hcryp);
896  }
897 
898  /* Change the CRYP state */
899  hcryp->State = HAL_CRYP_STATE_READY;
900 
901  /* Process Unlocked */
902  __HAL_UNLOCK(hcryp);
903 
904  return HAL_OK;
905  }
906  else
907  {
908  return HAL_BUSY;
909  }
910 }
__IO HAL_CRYP_STATETypeDef State
__HAL_UNLOCK(hrtc)
static HAL_StatusTypeDef CRYP_WaitOnBusyFlagReset(CRYP_HandleTypeDef const *const hcryp, uint32_t Timeout)
Wait for Busy Flag to be reset during a GCM payload encryption process suspension.
CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE)
__HAL_LOCK(hrtc)
HAL_SuspendTypeDef SuspendRequest
return HAL_OK
static void CRYP_Padding(CRYP_HandleTypeDef *hcryp, uint32_t difflength, uint32_t polling)
Last header or payload block padding when size is not a multiple of 128 bits.
HAL_PhaseTypeDef Phase
static HAL_StatusTypeDef CRYP_WaitOnCCFlag(CRYP_HandleTypeDef const *const hcryp, uint32_t Timeout)
Handle CRYP hardware block Timeout when waiting for CCF flag to be raised.
MODIFY_REG(hrtc->Instance->CR, RTC_CR_WUCKSEL,(uint32_t) WakeUpClock)

◆ HAL_CRYPEx_AES_Auth_DMA()

HAL_StatusTypeDef HAL_CRYPEx_AES_Auth_DMA ( CRYP_HandleTypeDef hcryp,
uint8_t *  pInputData,
uint64_t  Size,
uint8_t *  pOutputData 
)

Carry out in DMA mode the authentication tag generation as well as the ciphering or deciphering operation according to hcryp->Init structure fields.

Parameters
hcryppointer to a CRYP_HandleTypeDef structure that contains the configuration information for CRYP module
pInputData
  • pointer to payload data in GCM or CCM payload phase,
  • pointer to B0 block in CMAC header phase,
  • pointer to C block in CMAC final phase.
  • Parameter is meaningless in case of GCM/GMAC/CCM init, header and final phases.
Size
  • length of the input payload data buffer in bytes in GCM or CCM payload phase,
  • length of B0 block (in bytes) in CMAC header phase,
  • length of C block (in bytes) in CMAC final phase.
  • Parameter is meaningless in case of GCM/GMAC/CCM init and header phases.
  • Parameter is meaningless in case of CCM final phase.
  • Parameter is message length in bytes in case of GCM final phase.
  • Parameter must be set to zero in case of GMAC final phase.
pOutputData
  • pointer to plain or cipher text in GCM/CCM payload phase,
  • pointer to authentication tag in GCM/GMAC/CCM/CMAC final phase.
  • Parameter is meaningless in case of GCM/GMAC/CCM init and header phases.
  • Parameter is meaningless in case of CMAC header phase.
Note
Supported operating modes are encryption and decryption, supported chaining modes are GCM, GMAC and CMAC.
Phases are singly processed according to hcryp->Init.GCMCMACPhase so that steps in these specific chaining modes can be skipped by the user if so required.
pInputData and pOutputData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
Return values
HALstatus

Definition at line 1403 of file stm32l4xx_hal_cryp_ex.c.

1404 {
1405  uint32_t inputaddr ;
1406  uint32_t outputaddr ;
1407  uint32_t tagaddr ;
1408  uint64_t headerlength ;
1409  uint64_t inputlength ;
1410  uint64_t payloadlength ;
1411 
1412 
1413  if (hcryp->State == HAL_CRYP_STATE_READY)
1414  {
1415  /* input/output parameters check */
1416  if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE)
1417  {
1418  /* No processing required */
1419  }
1420  else if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
1421  {
1422  if ((hcryp->Init.Header != NULL) && (hcryp->Init.HeaderSize == 0U))
1423  {
1424  return HAL_ERROR;
1425  }
1426 #if defined(AES_CR_NPBLB)
1427  if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM)
1428  {
1429  /* In case of CMAC or CCM header phase resumption, we can have pInputData = NULL and Size = 0 */
1430  if (((pInputData != NULL) && (Size == 0U)) || ((pInputData == NULL) && (Size != 0U)))
1431  {
1432  return HAL_ERROR;
1433  }
1434  }
1435 #else
1436  if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
1437  {
1438  if ((pInputData == NULL) || (Size == 0U))
1439  {
1440  return HAL_ERROR;
1441  }
1442  }
1443 #endif
1444  }
1445  else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
1446  {
1447  if ((pInputData != NULL) && (Size != 0U) && (pOutputData == NULL))
1448  {
1449  return HAL_ERROR;
1450  }
1451  }
1452  else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE)
1453  {
1454  if (pOutputData == NULL)
1455  {
1456  return HAL_ERROR;
1457  }
1458 #if !defined(AES_CR_NPBLB)
1459  if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (pInputData == NULL))
1460  {
1461  return HAL_ERROR;
1462  }
1463 #endif
1464  }
1465  else
1466  {
1467  /* Unspecified Phase */
1468  return HAL_ERROR;
1469  }
1470 
1471 
1472  /* Process Locked */
1473  __HAL_LOCK(hcryp);
1474 
1475  /* Change the CRYP state */
1476  hcryp->State = HAL_CRYP_STATE_BUSY;
1477 
1478  /*==============================================*/
1479  /* GCM/GMAC (or CCM when applicable) init phase */
1480  /*==============================================*/
1481  /* In case of init phase, the input data (Key and Initialization Vector) have
1482  already been entered during the initialization process. No DMA transfer is
1483  required at that point therefore, the software just waits for the CCF flag
1484  to be raised. */
1485  if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE)
1486  {
1487  /* just wait for hash computation */
1488  if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
1489  {
1490  hcryp->State = HAL_CRYP_STATE_READY;
1491  __HAL_UNLOCK(hcryp);
1492  return HAL_TIMEOUT;
1493  }
1494 
1495  /* Clear CCF Flag */
1496  __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
1497  /* Mark that the initialization phase is over */
1499  hcryp->State = HAL_CRYP_STATE_READY;
1500  }
1501  /*====================================*/
1502  /* GCM/GMAC/ CCM or CMAC header phase */
1503  /*====================================*/
1504  else if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
1505  {
1506 #if !defined(AES_CR_NPBLB)
1507  /* Set header phase; for GCM or GMAC, set data-byte at this point */
1508  if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
1509  {
1510  MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH|AES_CR_DATATYPE, CRYP_HEADER_PHASE|hcryp->Init.DataType);
1511  }
1512  else
1513 #endif
1514  {
1515  MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_HEADER_PHASE);
1516  }
1517 
1518  /* Enable the CRYP peripheral */
1519  __HAL_CRYP_ENABLE(hcryp);
1520 
1521 #if !defined(AES_CR_NPBLB)
1522  /* enter first B0 block in polling mode (no DMA transfer for B0) */
1523  if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
1524  {
1525  inputaddr = (uint32_t)pInputData;
1526  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1527  inputaddr+=4U;
1528  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1529  inputaddr+=4U;
1530  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1531  inputaddr+=4U;
1532  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1533 
1534  if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
1535  {
1536  hcryp->State = HAL_CRYP_STATE_READY;
1537  __HAL_UNLOCK(hcryp);
1538  return HAL_TIMEOUT;
1539  }
1540  /* Clear CCF Flag */
1541  __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
1542  }
1543 #endif
1544 
1545  /* No header case */
1546  if (hcryp->Init.Header == NULL)
1547  {
1548  hcryp->State = HAL_CRYP_STATE_READY;
1549  /* Mark that the header phase is over */
1551  /* Process Unlocked */
1552  __HAL_UNLOCK(hcryp);
1553 
1554  return HAL_OK;
1555  }
1556 
1557  inputaddr = (uint32_t)hcryp->Init.Header;
1558  if ((hcryp->Init.HeaderSize % 16U) != 0U)
1559  {
1560 
1561  if (hcryp->Init.HeaderSize < 16U)
1562  {
1563  hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
1564  CRYP_Padding(hcryp, (uint32_t) (hcryp->Init.HeaderSize), CRYP_POLLING_OFF);
1565 
1566  hcryp->State = HAL_CRYP_STATE_READY;
1567  /* Mark that the header phase is over */
1569 
1570  /* CCF flag indicating header phase AES processing completion
1571  will be checked at the start of the next phase:
1572  - payload phase (GCM / CCM when applicable)
1573  - final phase (GMAC or CMAC when applicable). */
1574  }
1575  else
1576  {
1577  /* Local variable headerlength is a number of bytes multiple of 128 bits,
1578  remaining header data (if any) are handled after this loop */
1579  headerlength = (((hcryp->Init.HeaderSize)/16U)*16U) ;
1580  /* Store the ending transfer point */
1581  hcryp->pCrypInBuffPtr = hcryp->Init.Header + headerlength;
1582  hcryp->CrypInCount = (uint32_t)(hcryp->Init.HeaderSize - headerlength); /* remainder */
1583 
1584  /* Set the input and output addresses and start DMA transfer */
1585  /* (incomplete DMA transfer, will be wrapped up after completion of
1586  the first one (initiated here) with data padding */
1587  CRYP_Authentication_SetDMAConfig(hcryp, inputaddr, (uint16_t)headerlength, 0);
1588  }
1589  }
1590  else
1591  {
1592  hcryp->CrypInCount = 0;
1593  /* Set the input address and start DMA transfer */
1594  CRYP_Authentication_SetDMAConfig(hcryp, inputaddr, (uint16_t)hcryp->Init.HeaderSize, 0);
1595  }
1596  }
1597  /*============================================*/
1598  /* GCM (or CCM when applicable) payload phase */
1599  /*============================================*/
1600  else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
1601  {
1602  /* Coming from header phase, wait for CCF flag to be raised
1603  if header present and fed to the IP in the previous phase */
1604  if (hcryp->Init.Header != NULL)
1605  {
1606  if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
1607  {
1608  hcryp->State = HAL_CRYP_STATE_READY;
1609  __HAL_UNLOCK(hcryp);
1610  return HAL_TIMEOUT;
1611  }
1612  }
1613  else
1614  {
1615  /* Enable the Peripheral since wasn't in header phase (no header case) */
1616  __HAL_CRYP_ENABLE(hcryp);
1617  }
1618  /* Clear CCF Flag */
1619  __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
1620 
1621  MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PAYLOAD_PHASE);
1622 
1623  /* No payload case */
1624  if (pInputData == NULL)
1625  {
1626  hcryp->State = HAL_CRYP_STATE_READY;
1627  /* Mark that the header phase is over */
1629  /* Process Unlocked */
1630  __HAL_UNLOCK(hcryp);
1631 
1632  return HAL_OK;
1633  }
1634 
1635 
1636  /* Specific handling to manage payload size less than 128 bits */
1637  if ((Size % 16U) != 0U)
1638  {
1639  inputaddr = (uint32_t)pInputData;
1640  outputaddr = (uint32_t)pOutputData;
1641  if (Size < 16U)
1642  {
1643  /* Block is now entered in polling mode, no actual gain in resorting to DMA */
1644  hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
1645  hcryp->pCrypOutBuffPtr = (uint8_t *)outputaddr;
1646 
1647  CRYP_Padding(hcryp, (uint32_t)Size, CRYP_POLLING_ON);
1648 
1649  /* Change the CRYP state to ready */
1650  hcryp->State = HAL_CRYP_STATE_READY;
1651  /* Mark that the payload phase is over */
1653 
1654  /* Call output data transfer complete callback */
1655 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
1656  hcryp->OutCpltCallback(hcryp);
1657 #else
1658  HAL_CRYP_OutCpltCallback(hcryp);
1659 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
1660  }
1661  else
1662  {
1663  payloadlength = (Size/16U) * 16U;
1664 
1665  /* Store the ending transfer points */
1666  hcryp->pCrypInBuffPtr = pInputData;
1667  hcryp->pCrypInBuffPtr += payloadlength;
1668  hcryp->pCrypOutBuffPtr = pOutputData;
1669  hcryp->pCrypOutBuffPtr += payloadlength;
1670  hcryp->CrypInCount = (uint32_t)(Size - payloadlength); /* remainder */
1671 
1672  /* Set the input and output addresses and start DMA transfer */
1673  /* (incomplete DMA transfer, will be wrapped up with data padding
1674  after completion of the one initiated here) */
1675  CRYP_Authentication_SetDMAConfig(hcryp, inputaddr, (uint16_t)payloadlength, outputaddr);
1676  }
1677  }
1678  else
1679  {
1680  hcryp->CrypInCount = 0;
1681  inputaddr = (uint32_t)pInputData;
1682  outputaddr = (uint32_t)pOutputData;
1683 
1684  /* Set the input and output addresses and start DMA transfer */
1685  CRYP_Authentication_SetDMAConfig(hcryp, inputaddr, (uint16_t)Size, outputaddr);
1686  }
1687  }
1688  /*==================================*/
1689  /* GCM/GMAC/CCM or CMAC final phase */
1690  /*==================================*/
1691  else
1692  {
1693  /* If coming from header phase (GMAC or CMAC case when applicable),
1694  wait for CCF flag to be raised */
1695  if (READ_BIT(hcryp->Instance->CR, AES_CR_GCMPH) == CRYP_HEADER_PHASE)
1696  {
1697  if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
1698  {
1699  hcryp->State = HAL_CRYP_STATE_READY;
1700  __HAL_UNLOCK(hcryp);
1701  return HAL_TIMEOUT;
1702  }
1703  /* Clear CCF Flag */
1704  __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
1705  }
1706 
1707  tagaddr = (uint32_t)pOutputData;
1708 
1709 #if defined(AES_CR_NPBLB)
1710  /* By default, clear NPBLB field */
1711  CLEAR_BIT(hcryp->Instance->CR, AES_CR_NPBLB);
1712 #endif
1713  MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_FINAL_PHASE);
1714 
1715  /* if the header and payload phases have been bypassed, AES must be enabled again */
1716  if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER)
1717  {
1718  __HAL_CRYP_ENABLE(hcryp);
1719  }
1720 
1721  if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
1722  {
1723  headerlength = hcryp->Init.HeaderSize * 8U; /* Header length in bits */
1724  inputlength = Size * 8U; /* input length in bits */
1725  /* Write the number of bits in the header on 64 bits followed by the number
1726  of bits in the payload on 64 bits as well */
1727 #if !defined(AES_CR_NPBLB)
1728  if(hcryp->Init.DataType == CRYP_DATATYPE_1B)
1729  {
1730  hcryp->Instance->DINR = __RBIT((uint32_t)(headerlength>>32));
1731  hcryp->Instance->DINR = __RBIT((uint32_t)headerlength);
1732  hcryp->Instance->DINR = __RBIT((uint32_t)(inputlength>>32));
1733  hcryp->Instance->DINR = __RBIT((uint32_t)inputlength);
1734  }
1735  else if(hcryp->Init.DataType == CRYP_DATATYPE_8B)
1736  {
1737  hcryp->Instance->DINR = __REV((uint32_t)(headerlength>>32));
1738  hcryp->Instance->DINR = __REV((uint32_t)headerlength);
1739  hcryp->Instance->DINR = __REV((uint32_t)(inputlength>>32));
1740  hcryp->Instance->DINR = __REV((uint32_t)inputlength);
1741  }
1742  else if(hcryp->Init.DataType == CRYP_DATATYPE_16B)
1743  {
1744  hcryp->Instance->DINR = __ROR((uint32_t)(headerlength>>32), 16);
1745  hcryp->Instance->DINR = __ROR((uint32_t)headerlength, 16);
1746  hcryp->Instance->DINR = __ROR((uint32_t)(inputlength>>32), 16);
1747  hcryp->Instance->DINR = __ROR((uint32_t)inputlength, 16);
1748  }
1749  else if(hcryp->Init.DataType == CRYP_DATATYPE_32B)
1750  {
1751  hcryp->Instance->DINR = (uint32_t)(headerlength>>32);
1752  hcryp->Instance->DINR = (uint32_t)(headerlength);
1753  hcryp->Instance->DINR = (uint32_t)(inputlength>>32);
1754  hcryp->Instance->DINR = (uint32_t)(inputlength);
1755  }
1756  else
1757  {
1758  /* Unspecified Data Type */
1759  return HAL_ERROR;
1760  }
1761 #else
1762  hcryp->Instance->DINR = (uint32_t)(headerlength>>32);
1763  hcryp->Instance->DINR = (uint32_t)(headerlength);
1764  hcryp->Instance->DINR = (uint32_t)(inputlength>>32);
1765  hcryp->Instance->DINR = (uint32_t)(inputlength);
1766 #endif
1767  }
1768 #if !defined(AES_CR_NPBLB)
1769  else if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
1770  {
1771  __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
1772 
1773  inputaddr = (uint32_t)pInputData;
1774  /* Enter the last block made of a 128-bit value formatted
1775  from the original B0 packet. */
1776  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1777  inputaddr+=4U;
1778  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1779  inputaddr+=4U;
1780  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1781  inputaddr+=4U;
1782  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1783  }
1784  else
1785  {
1786  /* Unspecified Chaining Mode */
1787  return HAL_ERROR;
1788  }
1789 #endif
1790 
1791  /* No DMA transfer is required at that point therefore, the software
1792  just waits for the CCF flag to be raised. */
1793  if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
1794  {
1795  hcryp->State = HAL_CRYP_STATE_READY;
1796  __HAL_UNLOCK(hcryp);
1797  return HAL_TIMEOUT;
1798  }
1799 
1800  /* Read the Auth TAG in the IN FIFO */
1801  *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
1802  tagaddr+=4U;
1803  *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
1804  tagaddr+=4U;
1805  *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
1806  tagaddr+=4U;
1807  *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
1808 
1809  /* Clear CCF Flag */
1810  __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
1811 
1812  /* Mark that the final phase is over */
1814  hcryp->State = HAL_CRYP_STATE_READY;
1815  /* Disable the Peripheral */
1816  __HAL_CRYP_DISABLE(hcryp);
1817 
1818  }
1819 
1820  /* Process Unlocked */
1821  __HAL_UNLOCK(hcryp);
1822 
1823  return HAL_OK;
1824  }
1825  else
1826  {
1827  return HAL_BUSY;
1828  }
1829 }
__IO HAL_CRYP_STATETypeDef State
if(lpuartdiv >=LPUART_BRR_MIN_VALUE)
void(* OutCpltCallback)(struct __CRYP_HandleTypeDef *hcryp)
void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp)
Output DMA transfer complete callback.
__HAL_UNLOCK(hrtc)
static void CRYP_Authentication_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr)
Set the DMA configuration and start the DMA transfer for GCM, GMAC, CCM or CMAC chaining modes...
CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE)
__HAL_LOCK(hrtc)
return HAL_OK
static void CRYP_Padding(CRYP_HandleTypeDef *hcryp, uint32_t difflength, uint32_t polling)
Last header or payload block padding when size is not a multiple of 128 bits.
HAL_PhaseTypeDef Phase
static HAL_StatusTypeDef CRYP_WaitOnCCFlag(CRYP_HandleTypeDef const *const hcryp, uint32_t Timeout)
Handle CRYP hardware block Timeout when waiting for CCF flag to be raised.
MODIFY_REG(hrtc->Instance->CR, RTC_CR_WUCKSEL,(uint32_t) WakeUpClock)

◆ HAL_CRYPEx_AES_Auth_IT()

HAL_StatusTypeDef HAL_CRYPEx_AES_Auth_IT ( CRYP_HandleTypeDef hcryp,
uint8_t *  pInputData,
uint64_t  Size,
uint8_t *  pOutputData 
)

Carry out in interrupt mode the authentication tag generation as well as the ciphering or deciphering operation according to hcryp->Init structure fields.

Parameters
hcryppointer to a CRYP_HandleTypeDef structure that contains the configuration information for CRYP module
pInputData
  • pointer to payload data in GCM or CCM payload phase,
  • pointer to B0 block in CMAC header phase,
  • pointer to C block in CMAC final phase.
  • Parameter is meaningless in case of GCM/GMAC/CCM init, header and final phases.
Size
  • length of the input payload data buffer in bytes in GCM or CCM payload phase,
  • length of B0 block (in bytes) in CMAC header phase,
  • length of C block (in bytes) in CMAC final phase.
  • Parameter is meaningless in case of GCM/GMAC/CCM init and header phases.
  • Parameter is meaningless in case of CCM final phase.
  • Parameter is message length in bytes in case of GCM final phase.
  • Parameter must be set to zero in case of GMAC final phase.
pOutputData
  • pointer to plain or cipher text in GCM/CCM payload phase,
  • pointer to authentication tag in GCM/GMAC/CCM/CMAC final phase.
  • Parameter is meaningless in case of GCM/GMAC/CCM init and header phases.
  • Parameter is meaningless in case of CMAC header phase.
Note
Supported operating modes are encryption and decryption, supported chaining modes are GCM, GMAC and CMAC.
Phases are singly processed according to hcryp->Init.GCMCMACPhase so that steps in these specific chaining modes can be skipped by the user if so required.
Return values
HALstatus

Definition at line 943 of file stm32l4xx_hal_cryp_ex.c.

944 {
945 
946  uint32_t inputaddr ;
947  uint64_t headerlength ;
948  uint64_t inputlength ;
949  uint32_t index ;
950  uint32_t addhoc_process = 0;
951  uint32_t difflength = 0;
952  uint32_t difflengthmod4 = 0;
953  uint32_t mask[4][3];
954 
955  uint32_t mask_index = hcryp->Init.DataType >> AES_CR_DATATYPE_Pos;
956 
957  mask[0][0] = 0xFF000000U; mask[0][1] = 0xFFFF0000U; mask[0][2] = 0xFFFFFF00U; /* 32-bit data */
958  mask[1][0] = 0x0000FF00U; mask[1][1] = 0x0000FFFFU; mask[1][2] = 0xFF00FFFFU; /* 16-bit data */
959  mask[2][0] = 0x000000FFU; mask[2][1] = 0x0000FFFFU; mask[2][2] = 0x00FFFFFFU; /* 8-bit data */
960  mask[3][0] = 0x000000FFU; mask[3][1] = 0x0000FFFFU; mask[3][2] = 0x00FFFFFFU; /* Bit data */
961 
962  if (hcryp->State == HAL_CRYP_STATE_READY)
963  {
964  /* input/output parameters check */
965  if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE)
966  {
967  /* No processing required */
968  }
969  else if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
970  {
971  if (((hcryp->Init.Header != NULL) && (hcryp->Init.HeaderSize == 0U)) ||
972  ((hcryp->Init.Header == NULL) && (hcryp->Init.HeaderSize != 0U)))
973  {
974  return HAL_ERROR;
975  }
976 #if defined(AES_CR_NPBLB)
977  if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM)
978 #else
979  if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
980 #endif
981  {
982  /* In case of CMAC or CCM header phase resumption, we can have pInputData = NULL and Size = 0 */
983  if (((pInputData != NULL) && (Size == 0U)) || ((pInputData == NULL) && (Size != 0U)))
984  {
985  return HAL_ERROR;
986  }
987  }
988  }
989  else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
990  {
991  if ((pInputData != NULL) && (Size != 0U) && (pOutputData == NULL))
992  {
993  return HAL_ERROR;
994  }
995  }
996  else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE)
997  {
998  if (pOutputData == NULL)
999  {
1000  return HAL_ERROR;
1001  }
1002 #if !defined(AES_CR_NPBLB)
1003  if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (pInputData == NULL))
1004  {
1005  return HAL_ERROR;
1006  }
1007 #endif
1008  }
1009  else
1010  {
1011  /* Unspecified Phase */
1012  return HAL_ERROR;
1013  }
1014 
1015 
1016  /* Process Locked */
1017  __HAL_LOCK(hcryp);
1018 
1019  /* Change the CRYP state */
1020  hcryp->State = HAL_CRYP_STATE_BUSY;
1021 
1022  /* Process Unlocked */
1023  __HAL_UNLOCK(hcryp);
1024 
1025  /* Enable Computation Complete Flag and Error Interrupts */
1026  __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
1027 
1028 
1029 
1030  /*==============================================*/
1031  /* GCM/GMAC (or CCM when applicable) init phase */
1032  /*==============================================*/
1033  if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE)
1034  {
1035  /* In case of init phase, the input data (Key and Initialization Vector) have
1036  already been entered during the initialization process. Therefore, the
1037  software just waits for the CCF interrupt to be raised and which will
1038  be handled by CRYP_AES_Auth_IT() API. */
1039  }
1040  /*===================================*/
1041  /* GCM/GMAC/CCM or CMAC header phase */
1042  /*===================================*/
1043  else if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
1044  {
1045 
1046 #if defined(AES_CR_NPBLB)
1047  if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM)
1048 #else
1049  if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
1050 #endif
1051  {
1052  /* In case of CMAC, B blocks are first entered, before the header.
1053  Therefore, B blocks and the header are entered back-to-back
1054  as if it was only one single block.
1055  However, in case of resumption after suspension, if all the
1056  B blocks have been entered (in that case, Size = 0), only the
1057  remainder of the non-processed header bytes are entered. */
1058  if (Size != 0U)
1059  {
1060  hcryp->CrypInCount = (uint32_t)(Size + hcryp->Init.HeaderSize);
1061  hcryp->pCrypInBuffPtr = pInputData;
1062  }
1063  else
1064  {
1065  hcryp->CrypInCount = (uint32_t)hcryp->Init.HeaderSize;
1066  hcryp->pCrypInBuffPtr = hcryp->Init.Header;
1067  }
1068  }
1069  else
1070  {
1071  /* Get the header addresses and sizes */
1072  hcryp->CrypInCount = (uint32_t)hcryp->Init.HeaderSize;
1073  hcryp->pCrypInBuffPtr = hcryp->Init.Header;
1074  }
1075 
1076  inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
1077 
1078 
1079 #if !defined(AES_CR_NPBLB)
1080  /* Set header phase; for GCM or GMAC, set data-byte at this point */
1081  if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
1082  {
1083  MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH|AES_CR_DATATYPE, CRYP_HEADER_PHASE|hcryp->Init.DataType);
1084  }
1085  else
1086 #endif
1087  {
1088  MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_HEADER_PHASE);
1089  }
1090 
1091  /* Enable the Peripheral */
1092  __HAL_CRYP_ENABLE(hcryp);
1093 
1094  /* Increment/decrement instance pointer/counter */
1095  if (hcryp->CrypInCount == 0U)
1096  {
1097  /* Case of no header */
1098  hcryp->State = HAL_CRYP_STATE_READY;
1099  /* Mark that the header phase is over */
1101  return HAL_OK;
1102  }
1103  else if (hcryp->CrypInCount < 16U)
1104  {
1105  hcryp->CrypInCount = 0;
1106  addhoc_process = 1;
1107  difflength = (uint32_t) (hcryp->Init.HeaderSize);
1108  difflengthmod4 = difflength%4U;
1109  }
1110  else
1111  {
1112  hcryp->pCrypInBuffPtr += 16;
1113  hcryp->CrypInCount -= 16U;
1114  }
1115 
1116 
1117 #if defined(AES_CR_NPBLB)
1118  if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM)
1119 #else
1120  if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
1121 #endif
1122  {
1123  if (hcryp->CrypInCount == hcryp->Init.HeaderSize)
1124  {
1125  /* All B blocks will have been entered after the next
1126  four DINR writing, so point at header buffer for
1127  the next iteration */
1128  hcryp->pCrypInBuffPtr = hcryp->Init.Header;
1129  }
1130  }
1131 
1132  /* Enter header first block to initiate the process
1133  in the Data Input register */
1134  if (addhoc_process == 0U)
1135  {
1136  /* Header has size equal or larger than 128 bits */
1137  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1138  inputaddr+=4U;
1139  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1140  inputaddr+=4U;
1141  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1142  inputaddr+=4U;
1143  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1144  }
1145  else
1146  {
1147  /* Header has size less than 128 bits */
1148  /* Enter complete words when possible */
1149  for(index=0U ; index < (difflength/4U); index ++)
1150  {
1151  /* Write the Input block in the Data Input register */
1152  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1153  inputaddr+=4U;
1154  }
1155  /* Enter incomplete word padded with zeroes if applicable
1156  (case of header length not a multiple of 32-bits) */
1157  if (difflengthmod4 != 0U)
1158  {
1159  hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[mask_index][difflengthmod4-1U]);
1160  }
1161  /* Pad with zero-words to reach 128-bit long block and wrap-up header feeding to the IP */
1162  for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++)
1163  {
1164  hcryp->Instance->DINR = 0;
1165  }
1166 
1167  }
1168  }
1169  /*============================================*/
1170  /* GCM (or CCM when applicable) payload phase */
1171  /*============================================*/
1172  else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
1173  {
1174  /* Get the buffer addresses and sizes */
1175  hcryp->CrypInCount = (uint32_t)Size;
1176  hcryp->pCrypInBuffPtr = pInputData;
1177  hcryp->pCrypOutBuffPtr = pOutputData;
1178  hcryp->CrypOutCount = (uint32_t)Size;
1179 
1180  inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
1181 
1182  MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PAYLOAD_PHASE);
1183 
1184  /* if the header phase has been bypassed, AES must be enabled again */
1185  if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER)
1186  {
1187  __HAL_CRYP_ENABLE(hcryp);
1188  }
1189 
1190  /* No payload case */
1191  if (pInputData == NULL)
1192  {
1193  hcryp->State = HAL_CRYP_STATE_READY;
1194  /* Mark that the header phase is over */
1196  /* Process Unlocked */
1197  __HAL_UNLOCK(hcryp);
1198 
1199  return HAL_OK;
1200  }
1201 
1202  /* Specific handling to manage payload size less than 128 bits */
1203  if (Size < 16U)
1204  {
1205  difflength = (uint32_t) (Size);
1206 #if defined(AES_CR_NPBLB)
1207  /* In case of GCM encryption or CCM decryption, specify the number of padding
1208  bytes in last block of payload */
1209  if (READ_BIT(hcryp->Instance->CR, AES_CR_GCMPH) == CRYP_PAYLOAD_PHASE)
1210  {
1211  uint32_t cr_temp = hcryp->Instance->CR;
1212 
1213  if (((cr_temp & (AES_CR_CHMOD|AES_CR_MODE)) == (CRYP_CHAINMODE_AES_GCM_GMAC|CRYP_ALGOMODE_ENCRYPT))
1214  || ((cr_temp & (AES_CR_CHMOD|AES_CR_MODE)) == (CRYP_CHAINMODE_AES_CCM|CRYP_ALGOMODE_DECRYPT)))
1215  {
1216  /* Set NPBLB field in writing the number of padding bytes
1217  for the last block of payload */
1218  MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, (16U - difflength) << AES_POSITION_CR_NPBLB);
1219  }
1220  }
1221 #else
1222  /* Software workaround applied to GCM encryption only */
1223  if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT)
1224  {
1225  /* Change the mode configured in CHMOD bits of CR register to select CTR mode */
1226  __HAL_CRYP_SET_CHAININGMODE(hcryp, CRYP_CHAINMODE_AES_CTR);
1227  }
1228 #endif
1229 
1230 
1231  /* Set hcryp->CrypInCount to 0 (no more data to enter) */
1232  hcryp->CrypInCount = 0;
1233 
1234  /* Insert the last block (which size is inferior to 128 bits) padded with zeroes,
1235  to have a complete block of 128 bits */
1236  difflengthmod4 = difflength%4U;
1237  /* Insert the last block (which size is inferior to 128 bits) padded with zeroes
1238  to have a complete block of 128 bits */
1239  for(index=0U; index < (difflength/4U); index ++)
1240  {
1241  /* Write the Input block in the Data Input register */
1242  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1243  inputaddr+=4U;
1244  }
1245  /* If required, manage input data size not multiple of 32 bits */
1246  if (difflengthmod4 != 0U)
1247  {
1248  hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[mask_index][difflengthmod4-1U]);
1249  }
1250  /* Wrap-up in padding with zero-words if applicable */
1251  for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++)
1252  {
1253  hcryp->Instance->DINR = 0;
1254  }
1255  }
1256  else
1257  {
1258  /* Increment/decrement instance pointer/counter */
1259  hcryp->pCrypInBuffPtr += 16;
1260  hcryp->CrypInCount -= 16U;
1261 
1262  /* Enter payload first block to initiate the process
1263  in the Data Input register */
1264  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1265  inputaddr+=4U;
1266  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1267  inputaddr+=4U;
1268  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1269  inputaddr+=4U;
1270  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1271  }
1272  }
1273  /*==================================*/
1274  /* GCM/GMAC/CCM or CMAC final phase */
1275  /*==================================*/
1276  else
1277  {
1278  hcryp->pCrypOutBuffPtr = pOutputData;
1279 
1280 #if defined(AES_CR_NPBLB)
1281  /* By default, clear NPBLB field */
1282  CLEAR_BIT(hcryp->Instance->CR, AES_CR_NPBLB);
1283 #endif
1284 
1285  MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_FINAL_PHASE);
1286 
1287  /* if the header and payload phases have been bypassed, AES must be enabled again */
1288  if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER)
1289  {
1290  __HAL_CRYP_ENABLE(hcryp);
1291  }
1292 
1293  if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
1294  {
1295  headerlength = hcryp->Init.HeaderSize * 8U; /* Header length in bits */
1296  inputlength = Size * 8U; /* Input length in bits */
1297  /* Write the number of bits in the header on 64 bits followed by the number
1298  of bits in the payload on 64 bits as well */
1299 
1300 #if !defined(AES_CR_NPBLB)
1301  if(hcryp->Init.DataType == CRYP_DATATYPE_1B)
1302  {
1303  hcryp->Instance->DINR = __RBIT((uint32_t)((headerlength)>>32));
1304  hcryp->Instance->DINR = __RBIT((uint32_t)headerlength);
1305  hcryp->Instance->DINR = __RBIT((uint32_t)((inputlength)>>32));
1306  hcryp->Instance->DINR = __RBIT((uint32_t)inputlength);
1307  }
1308  else if(hcryp->Init.DataType == CRYP_DATATYPE_8B)
1309  {
1310  hcryp->Instance->DINR = __REV((uint32_t)(headerlength>>32));
1311  hcryp->Instance->DINR = __REV((uint32_t)headerlength);
1312  hcryp->Instance->DINR = __REV((uint32_t)(inputlength>>32));
1313  hcryp->Instance->DINR = __REV((uint32_t)inputlength);
1314  }
1315  else if(hcryp->Init.DataType == CRYP_DATATYPE_16B)
1316  {
1317  hcryp->Instance->DINR = __ROR((uint32_t)((headerlength)>>32), 16);
1318  hcryp->Instance->DINR = __ROR((uint32_t)headerlength, 16);
1319  hcryp->Instance->DINR = __ROR((uint32_t)((inputlength)>>32), 16);
1320  hcryp->Instance->DINR = __ROR((uint32_t)inputlength, 16);
1321  }
1322  else if(hcryp->Init.DataType == CRYP_DATATYPE_32B)
1323  {
1324  hcryp->Instance->DINR = (uint32_t)(headerlength>>32);
1325  hcryp->Instance->DINR = (uint32_t)(headerlength);
1326  hcryp->Instance->DINR = (uint32_t)(inputlength>>32);
1327  hcryp->Instance->DINR = (uint32_t)(inputlength);
1328  }
1329  else
1330  {
1331  /* Unspecified Data Type */
1332  return HAL_ERROR;
1333  }
1334 #else
1335  hcryp->Instance->DINR = (uint32_t)(headerlength>>32);
1336  hcryp->Instance->DINR = (uint32_t)(headerlength);
1337  hcryp->Instance->DINR = (uint32_t)(inputlength>>32);
1338  hcryp->Instance->DINR = (uint32_t)(inputlength);
1339 #endif
1340  }
1341 #if !defined(AES_CR_NPBLB)
1342  else if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
1343  {
1344  inputaddr = (uint32_t)pInputData;
1345  /* Enter the last block made of a 128-bit value formatted
1346  from the original B0 packet. */
1347  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1348  inputaddr+=4U;
1349  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1350  inputaddr+=4U;
1351  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1352  inputaddr+=4U;
1353  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1354  }
1355  else
1356  {
1357  /* Unspecified Chaining Mode */
1358  return HAL_ERROR;
1359  }
1360 #endif
1361  }
1362 
1363  return HAL_OK;
1364  }
1365  else
1366  {
1367  return HAL_BUSY;
1368  }
1369 }
__IO HAL_CRYP_STATETypeDef State
__HAL_UNLOCK(hrtc)
CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE)
__HAL_LOCK(hrtc)
return HAL_OK
HAL_PhaseTypeDef Phase
MODIFY_REG(hrtc->Instance->CR, RTC_CR_WUCKSEL,(uint32_t) WakeUpClock)

◆ HAL_CRYPEx_AES_DMA()

HAL_StatusTypeDef HAL_CRYPEx_AES_DMA ( CRYP_HandleTypeDef hcryp,
uint8_t *  pInputData,
uint16_t  Size,
uint8_t *  pOutputData 
)

Carry out in DMA mode the ciphering or deciphering operation according to hcryp->Init structure fields.

Parameters
hcryppointer to a CRYP_HandleTypeDef structure that contains the configuration information for CRYP module
pInputDataPointer to the plain text in case of encryption or cipher text in case of decryption or key derivation+decryption.
SizeLength of the input data buffer in bytes, must be a multiple of 16.
pOutputDataPointer to the cipher text in case of encryption or plain text in case of decryption/key derivation+decryption.
Note
Chaining modes ECB, CBC and CTR are managed by this function in DMA mode.
Supported operating modes are encryption, decryption and key derivation with decryption.
No DMA channel is provided for key derivation only and therefore, access to AES_KEYRx registers must be done by software.
This API is not applicable to key derivation only; for such a mode, access to AES_KEYRx registers must be done by software thru HAL_CRYPEx_AES() or HAL_CRYPEx_AES_IT() APIs.
pInputData and pOutputData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
Return values
HALstatus

Definition at line 351 of file stm32l4xx_hal_cryp_ex.c.

352 {
353  uint32_t inputaddr;
354  uint32_t outputaddr;
355 
356  if (hcryp->State == HAL_CRYP_STATE_READY)
357  {
358  /* Check parameters setting */
359  if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
360  {
361  /* no DMA channel is provided for key derivation operating mode,
362  access to AES_KEYRx registers must be done by software */
363  return HAL_ERROR;
364  }
365  else
366  {
367  if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0U))
368  {
369  return HAL_ERROR;
370  }
371  }
372 
373 
374  /* Process Locked */
375  __HAL_LOCK(hcryp);
376 
377  inputaddr = (uint32_t)pInputData;
378  outputaddr = (uint32_t)pOutputData;
379 
380  /* Change the CRYP state */
381  hcryp->State = HAL_CRYP_STATE_BUSY;
382 
383  /* Set the input and output addresses and start DMA transfer */
384  CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
385 
386  /* Process Unlocked */
387  __HAL_UNLOCK(hcryp);
388 
389  /* Return function status */
390  return HAL_OK;
391  }
392  else
393  {
394  return HAL_BUSY;
395  }
396 }
__IO HAL_CRYP_STATETypeDef State
__HAL_UNLOCK(hrtc)
__HAL_LOCK(hrtc)
return HAL_OK
static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr)
Set the DMA configuration and start the DMA transfer.

◆ HAL_CRYPEx_AES_IT()

HAL_StatusTypeDef HAL_CRYPEx_AES_IT ( CRYP_HandleTypeDef hcryp,
uint8_t *  pInputData,
uint16_t  Size,
uint8_t *  pOutputData 
)

Carry out in interrupt mode the ciphering or deciphering operation according to hcryp->Init structure fields, all operating modes (encryption, key derivation and/or decryption) and chaining modes ECB, CBC and CTR are managed by this function in interrupt mode.

Parameters
hcryppointer to a CRYP_HandleTypeDef structure that contains the configuration information for CRYP module
pInputDataPointer to the plain text in case of encryption or cipher text in case of decryption or key derivation+decryption. Parameter is meaningless in case of key derivation.
SizeLength of the input data buffer in bytes, must be a multiple of 16. Parameter is meaningless in case of key derivation.
pOutputDataPointer to the cipher text in case of encryption or plain text in case of decryption/key derivation+decryption, or pointer to the derivative keys in case of key derivation only.
Return values
HALstatus

Definition at line 246 of file stm32l4xx_hal_cryp_ex.c.

247 {
248  uint32_t inputaddr;
249 
250  if(hcryp->State == HAL_CRYP_STATE_READY)
251  {
252  /* Check parameters setting */
253  if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
254  {
255  if (pOutputData == NULL)
256  {
257  return HAL_ERROR;
258  }
259  }
260  else
261  {
262  if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0U))
263  {
264  return HAL_ERROR;
265  }
266  }
267  /* Process Locked */
268  __HAL_LOCK(hcryp);
269 
270  /* If operating mode is not limited to key derivation only,
271  get the buffers addresses and sizes */
272  if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION)
273  {
274 
275  hcryp->CrypInCount = Size;
276  hcryp->pCrypInBuffPtr = pInputData;
277  hcryp->pCrypOutBuffPtr = pOutputData;
278  hcryp->CrypOutCount = Size;
279  }
280  else
281  {
282  /* For key derivation, set output buffer only
283  (will point at derivated key) */
284  hcryp->pCrypOutBuffPtr = pOutputData;
285  }
286 
287  /* Change the CRYP state */
288  hcryp->State = HAL_CRYP_STATE_BUSY;
289 
290  /* Process Unlocked */
291  __HAL_UNLOCK(hcryp);
292 
293  /* Enable Computation Complete Flag and Error Interrupts */
294  __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
295 
296 
297  /* If operating mode is key derivation only, the input data have
298  already been entered during the initialization process. For
299  the other operating modes, they are fed to the CRYP hardware
300  block at this point. */
301  if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION)
302  {
303  /* Initiate the processing under interrupt in entering
304  the first input data */
305  inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
306  /* Increment/decrement instance pointer/counter */
307  hcryp->pCrypInBuffPtr += 16;
308  hcryp->CrypInCount -= 16U;
309  /* Write the first input block in the Data Input register */
310  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
311  inputaddr+=4U;
312  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
313  inputaddr+=4U;
314  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
315  inputaddr+=4U;
316  hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
317  }
318 
319  /* Return function status */
320  return HAL_OK;
321  }
322  else
323  {
324  return HAL_BUSY;
325  }
326 }
__IO HAL_CRYP_STATETypeDef State
__HAL_UNLOCK(hrtc)
__HAL_LOCK(hrtc)
return HAL_OK