cancel
Showing results for 
Search instead for 
Did you mean: 

HMAC with DMA transfert

khaled3310
Associate II
Posted on March 09, 2013 at 14:40

The original post was too long to process during our migration. Please click on the attachment to read the original post.
10 REPLIES 10
khaled3310
Associate II
Posted on March 10, 2013 at 19:33

this is the code but i think that's too long to put it in an interruption

/*************************************************************************************/

/**

  * @brief  Encrypt Data using AES 

  * @note   DATA transfer is done by the DMA

  * @note   DMA2 stream6 channel2 is used to transfer data from memory (the 

  *         PlainData Table) to CRYP Peripheral (the INPUT data register). 

  * @note   DMA2 stream5 channel2 is used to transfer data from CRYP Peripheral

  *        (the OUTPUT data register to memory (the EncryptedData Table). 

  * @param  None

  * @retval None

  ************************************************************************************/

  

void AES128_Encrypt_DMA_CBC(uint8_t* key, uint8_t* InitVectors)

{

  uint32_t keyaddr    = (uint32_t)key        ;

  uint32_t ivaddr     = (uint32_t)InitVectors;

  

  CRYP_IVInitTypeDef AES_CRYP_IVInitStructure ;

  CRYP_InitTypeDef CRYP_InitStructure         ;

  CRYP_KeyInitTypeDef CRYP_KeyInitStructure   ;

  CRYP_DeInit();

  /* Enable CRYP clock */

  RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_CRYP, ENABLE);

  /* CRYP configuration********************************************************/

  /* Crypto key structure initialisation */ 

  CRYP_KeyStructInit(&CRYP_KeyInitStructure);

  CRYP_FIFOFlush();

  

  /* Crypto Init for Encryption process */ 

  CRYP_InitStructure.CRYP_AlgoDir  = CRYP_AlgoDir_Encrypt;

  CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_AES_CBC;

  CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;

  CRYP_InitStructure.CRYP_KeySize  = CRYP_KeySize_128b;

  CRYP_Init(&CRYP_InitStructure);

  /* Key Initialisation ********************************************************/

  CRYP_KeyInitStructure.CRYP_Key2Left =  __REV(*(uint32_t*)(keyaddr));keyaddr+=4;

  CRYP_KeyInitStructure.CRYP_Key2Right=  __REV(*(uint32_t*)(keyaddr));keyaddr+=4;

  CRYP_KeyInitStructure.CRYP_Key3Left =  __REV(*(uint32_t*)(keyaddr));keyaddr+=4;

  CRYP_KeyInitStructure.CRYP_Key3Right=  __REV(*(uint32_t*)(keyaddr));

  CRYP_KeyInit(&CRYP_KeyInitStructure);

  

  /* CRYP Initialization Vectors **********************************************/

  AES_CRYP_IVInitStructure.CRYP_IV0Left = __REV(*(uint32_t*)(ivaddr));ivaddr+=4;

  AES_CRYP_IVInitStructure.CRYP_IV0Right= __REV(*(uint32_t*)(ivaddr));ivaddr+=4;

  AES_CRYP_IVInitStructure.CRYP_IV1Left = __REV(*(uint32_t*)(ivaddr));ivaddr+=4;

  AES_CRYP_IVInitStructure.CRYP_IV1Right= __REV(*(uint32_t*)(ivaddr));

  CRYP_IVInit(&AES_CRYP_IVInitStructure);

  

  /* Enable Crypto processor **************************************************/

  CRYP_FIFOFlush();

  CRYP_Cmd(ENABLE);

  CRYP_DMACmd(CRYP_DMAReq_DataIN, ENABLE);

  CRYP_DMACmd(CRYP_DMAReq_DataOUT, ENABLE);

}

/***********************  AES128_Decrypt_DMA_Configuration  **************************/

/*************************************************************************************/

/**

  * @brief  Decrypt Data using AES 

  * @note   DATA transfer is done by DMA  

  * @note   DMA2 stream6 channel2 is used to transfer data from memory (the 

  *         EncryptedData Table) to CRYP Peripheral (the INPUT data register). 

  * @note   DMA2 stream5 channel2 is used to transfer data from CRYP Peripheral

  *        (the OUTPUT data register to memory (the DecryptedData Table). 

  * @param  None

  * @retval None

  ***********************************************************************************/

void AES128_Decrypt_DMA_CBC(uint8_t* key, uint8_t* InitVectors)

{

  uint32_t keyaddr    = (uint32_t)key        ;

  uint32_t ivaddr     = (uint32_t)InitVectors;

  

  CRYP_IVInitTypeDef AES_CRYP_IVInitStructure;

  CRYP_InitTypeDef CRYP_InitStructure;

  CRYP_KeyInitTypeDef CRYP_KeyInitStructure;

//   CRYP_DeInit();

  /* Enable CRYP clock */

  RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_CRYP, ENABLE);

  /* CRYP configuration*******************************************************/

  /* Crypto Init for Key preparation for Decryption process */ 

  CRYP_InitStructure.CRYP_AlgoDir  = CRYP_AlgoDir_Decrypt;

  CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_AES_Key;

  CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;

  CRYP_InitStructure.CRYP_KeySize  = CRYP_KeySize_128b;

  CRYP_Init(&CRYP_InitStructure);

  /* Key Initialisation ******************************************************/

  CRYP_KeyInitStructure.CRYP_Key2Left =  __REV(*(uint32_t*)(keyaddr));keyaddr+=4;

  CRYP_KeyInitStructure.CRYP_Key2Right=  __REV(*(uint32_t*)(keyaddr));keyaddr+=4;

  CRYP_KeyInitStructure.CRYP_Key3Left =  __REV(*(uint32_t*)(keyaddr));keyaddr+=4;

  CRYP_KeyInitStructure.CRYP_Key3Right=  __REV(*(uint32_t*)(keyaddr));keyaddr+=4;

  CRYP_KeyInit(&CRYP_KeyInitStructure);

  /* CRYP Initialization Vectors *********************************************/

  AES_CRYP_IVInitStructure.CRYP_IV0Left = __REV(*(uint32_t*)(ivaddr));ivaddr+=4;

  AES_CRYP_IVInitStructure.CRYP_IV0Right= __REV(*(uint32_t*)(ivaddr));ivaddr+=4;

  AES_CRYP_IVInitStructure.CRYP_IV1Left = __REV(*(uint32_t*)(ivaddr));ivaddr+=4;

  AES_CRYP_IVInitStructure.CRYP_IV1Right= __REV(*(uint32_t*)(ivaddr));

  CRYP_IVInit(&AES_CRYP_IVInitStructure);

  /* Enable Crypto processor */

  CRYP_Cmd(ENABLE);

  /* wait until the Busy flag is reset */

   while (CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET);

  /* Crypto Init for Decryption process */ 

  CRYP_InitStructure.CRYP_AlgoDir  = CRYP_AlgoDir_Decrypt;

  CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_AES_CBC;

  CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;

  CRYP_InitStructure.CRYP_KeySize  = CRYP_KeySize_128b;

  CRYP_Init(&CRYP_InitStructure);

  CRYP_Cmd(ENABLE);

   /* CRYP Initialization Vectors */

  CRYP_DMACmd(CRYP_DMAReq_DataIN, ENABLE);

  CRYP_DMACmd(CRYP_DMAReq_DataOUT, ENABLE);  

}