示例#1
0
/**
  * @brief  Decrypts 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
  */
static void AES128_Decrypt_DMA(void)
{
  CRYP_InitTypeDef CRYP_InitStructure;
  CRYP_KeyInitTypeDef CRYP_KeyInitStructure;
  DMA_InitTypeDef DMA_InitStructure;

  /* Enable CRYP clock */
  RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_CRYP, ENABLE);

  /* Enable DMA2 clock */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, 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_32b;
  CRYP_InitStructure.CRYP_KeySize  = CRYP_KeySize_128b;
  CRYP_Init(&CRYP_InitStructure);

  /* Key Initialisation */
  CRYP_KeyInitStructure.CRYP_Key2Left = AES128key[0];
  CRYP_KeyInitStructure.CRYP_Key2Right= AES128key[1];
  CRYP_KeyInitStructure.CRYP_Key3Left = AES128key[2];
  CRYP_KeyInitStructure.CRYP_Key3Right= AES128key[3];
  CRYP_KeyInit(&CRYP_KeyInitStructure);

  /* 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_ECB;
  CRYP_InitStructure.CRYP_DataType = CRYP_DataType_32b;
  CRYP_InitStructure.CRYP_KeySize  = CRYP_KeySize_128b;

  CRYP_Init(&CRYP_InitStructure);
  CRYP_Cmd(ENABLE);

  CRYP_DMACmd(CRYP_DMAReq_DataIN, ENABLE);
  CRYP_DMACmd(CRYP_DMAReq_DataOUT, ENABLE);  
  
  /* DMA Configuration*********************************************************/
  DMA_DeInit(DMA2_Stream5);
  DMA_DeInit(DMA2_Stream6);
  
  /* Set common DMA parameters for Stream 5 and 6 */  
  DMA_InitStructure.DMA_Channel = DMA_Channel_2;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
  DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_INC4;
  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_INC4;  
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_BufferSize = DATA_SIZE;

  /* Set the parameters to be configured for stream 6 */
  DMA_InitStructure.DMA_PeripheralBaseAddr = CRYP_DIN_REG_ADDR; 
  DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)EncryptedData;
  DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;

  /* Configure the DMA Stream 6 */
  DMA_Init(DMA2_Stream6, &DMA_InitStructure);

  /* Set the parameters to be configured for stream 5 */
  DMA_InitStructure.DMA_PeripheralBaseAddr = CRYP_DOUT_REG_ADDR;
  DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)DecryptedData;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;

  /* Configure the DMA Stream 5 */
  DMA_Init(DMA2_Stream5, &DMA_InitStructure);

  /* Enable DMA streams *******************************************************/
  DMA_Cmd(DMA2_Stream6, ENABLE);
  DMA_Cmd(DMA2_Stream5, ENABLE);

  /* wait until the last transfer from OUT FIFO :
   all encrypted Data are transferred from crypt processor */
  while (DMA_GetFlagStatus(DMA2_Stream6, DMA_FLAG_TCIF5) == RESET);


  /* Disable Crypto and DMA **************************************************/
  CRYP_Cmd(DISABLE);
  CRYP_DMACmd(CRYP_DMAReq_DataIN, DISABLE);
  CRYP_DMACmd(CRYP_DMAReq_DataOUT, DISABLE);
  DMA_Cmd(DMA2_Stream5, DISABLE);
  DMA_Cmd(DMA2_Stream6, DISABLE);

  DMA_ClearFlag(DMA2_Stream6, DMA_FLAG_TCIF5);  
}
示例#2
0
/**
  * @brief  Encrypt Data using TDES 
  * @note   DATA transfer is done by DMA
  * @note   DMA2 stream6 channel2 is used to transfer data from memory (the 
  *         PlainData Tab) 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 Tab). 
  * @param  None
  * @retval None
  */
void TDES_Encrypt_DMA(void)
{ 
  CRYP_InitTypeDef CRYP_InitStructure;
  CRYP_KeyInitTypeDef CRYP_KeyInitStructure;
  DMA_InitTypeDef DMA_InitStructure;

  /* Enable CRYP clock */
  RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_CRYP, ENABLE);

  /* Enable DMA2 clock */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
  
  /* Configure crypto as encoder TDES *****************************************/
  /* Crypto Init for Encryption process */
  CRYP_InitStructure.CRYP_AlgoDir  = CRYP_AlgoDir_Encrypt;
  CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_TDES_ECB;
  CRYP_InitStructure.CRYP_DataType = CRYP_DataType_32b;

  CRYP_FIFOFlush();
  CRYP_Init(&CRYP_InitStructure);

  /* Key Initialisation */
  CRYP_KeyInitStructure.CRYP_Key1Left = TDESkey[0];
  CRYP_KeyInitStructure.CRYP_Key1Right= TDESkey[1];
  CRYP_KeyInitStructure.CRYP_Key2Left = TDESkey[2];
  CRYP_KeyInitStructure.CRYP_Key2Right= TDESkey[3];
  CRYP_KeyInitStructure.CRYP_Key3Left = TDESkey[4];
  CRYP_KeyInitStructure.CRYP_Key3Right= TDESkey[5];
  CRYP_KeyInit(&CRYP_KeyInitStructure);

  CRYP_DMACmd(CRYP_DMAReq_DataOUT, ENABLE);
  CRYP_DMACmd(CRYP_DMAReq_DataIN, ENABLE);

  /* DMA Configuration*********************************************************/
  /* set commun  DMA parameters for Stream 5 and 6*/
  DMA_DeInit(DMA2_Stream5);
  DMA_DeInit(DMA2_Stream6);
  DMA_InitStructure.DMA_Channel = DMA_Channel_2;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
  DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_BufferSize = DATA_SIZE;

  /* Set the parameters to be configured specific to stream 6*/
  DMA_InitStructure.DMA_PeripheralBaseAddr = CRYP_DIN_REG_ADDR;
  DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)PlainData;
  DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;

  /* Configure the DMA Stream 6 */
  DMA_Init(DMA2_Stream6, &DMA_InitStructure);

  /* Set the parameters to be configured specific to stream 5*/
  DMA_InitStructure.DMA_PeripheralBaseAddr = CRYP_DOUT_REG_ADDR;
  DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)EncryptedData;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;

  /* Configure the DMA Stream 5 */
  DMA_Init(DMA2_Stream5, &DMA_InitStructure);

  /* Enable DMA streams*/
  DMA_Cmd(DMA2_Stream6, ENABLE);
  DMA_Cmd(DMA2_Stream5, ENABLE);
  
  /* Enable Crypto processor */
  CRYP_Cmd(ENABLE);

  /* wait until the last transfer from OUT FIFO :
   all encrypted Data are transfered from crypt processor */
  while (DMA_GetFlagStatus(DMA2_Stream5, DMA_FLAG_TCIF5) == RESET);

  /* Disable Crypto and DMA ***************************************************/
  CRYP_Cmd(DISABLE);
  CRYP_DMACmd(CRYP_DMAReq_DataOUT, DISABLE);
  CRYP_DMACmd(CRYP_DMAReq_DataIN, DISABLE);
  DMA_Cmd(DMA2_Stream5, DISABLE);
  DMA_Cmd(DMA2_Stream6, DISABLE);
}