/** * @brief Encrypt and decrypt using TDES in ECB Mode * @param Mode: encryption or decryption Mode. * This parameter can be one of the following values: * @arg MODE_ENCRYPT: Encryption * @arg MODE_DECRYPT: Decryption * @param Key: Key used for TDES algorithm. * @param Ilength: length of the Input buffer, must be a multiple of 8. * @param Input: pointer to the Input buffer. * @param Output: pointer to the returned buffer. * @retval An ErrorStatus enumeration value: * - SUCCESS: Operation done * - ERROR: Operation failed */ ErrorStatus CRYP_TDES_ECB(uint8_t Mode, uint8_t Key[24], uint8_t *Input, uint32_t Ilength, uint8_t *Output) { CRYP_InitTypeDef TDES_CRYP_InitStructure; CRYP_KeyInitTypeDef TDES_CRYP_KeyInitStructure; __IO uint32_t counter = 0; uint32_t busystatus = 0; ErrorStatus status = SUCCESS; uint32_t keyaddr = (uint32_t)Key; uint32_t inputaddr = (uint32_t)Input; uint32_t outputaddr = (uint32_t)Output; uint32_t i = 0; /* Crypto structures initialisation*/ CRYP_KeyStructInit(&TDES_CRYP_KeyInitStructure); /* Crypto Init for Encryption process */ if(Mode == MODE_ENCRYPT) /* TDES encryption */ { TDES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt; } else /*if(Mode == MODE_DECRYPT)*/ /* TDES decryption */ { TDES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt; } TDES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_TDES_ECB; TDES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b; CRYP_Init(&TDES_CRYP_InitStructure); /* Key Initialisation */ TDES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr)); keyaddr+=4; TDES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr)); keyaddr+=4; TDES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr)); keyaddr+=4; TDES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr)); keyaddr+=4; TDES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr)); keyaddr+=4; TDES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr)); CRYP_KeyInit(& TDES_CRYP_KeyInitStructure); /* Flush IN/OUT FIFO */ CRYP_FIFOFlush(); /* Enable Crypto processor */ CRYP_Cmd(ENABLE); for(i=0; ((i<Ilength) && (status != ERROR)); i+=8) { /* Write the Input block in the Input FIFO */ CRYP_DataIn(*(uint32_t*)(inputaddr)); inputaddr+=4; CRYP_DataIn(*(uint32_t*)(inputaddr)); inputaddr+=4; /* Wait until the complete message has been processed */ counter = 0; do { busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY); counter++; }while ((counter != TDESBUSY_TIMEOUT) && (busystatus != RESET)); if (busystatus != RESET) { status = ERROR; } else { /* Read the Output block from the Output FIFO */ *(uint32_t*)(outputaddr) = CRYP_DataOut(); outputaddr+=4; *(uint32_t*)(outputaddr) = CRYP_DataOut(); outputaddr+=4; } } /* Disable Crypto */ CRYP_Cmd(DISABLE); return status; }
/** * @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); }
/** * @brief Encrypt and decrypt using DES in CBC Mode * @param Mode: encryption or decryption Mode. * This parameter can be one of the following values: * @arg MODE_ENCRYPT: Encryption * @arg MODE_DECRYPT: Decryption * @param Key: Key used for DES algorithm. * @param InitVectors: Initialisation Vectors used for DES algorithm. * @param Ilength: length of the Input buffer, must be a multiple of 8. * @param Input: pointer to the Input buffer. * @param Output: pointer to the returned buffer. * @retval An ErrorStatus enumeration value: * - SUCCESS: Operation done * - ERROR: Operation failed */ ErrorStatus CRYP_DES_CBC(uint8_t Mode, uint8_t Key[8], uint8_t InitVectors[8], uint8_t *Input, uint32_t Ilength, uint8_t *Output) { CRYP_InitTypeDef DES_CRYP_InitStructure; CRYP_KeyInitTypeDef DES_CRYP_KeyInitStructure; CRYP_IVInitTypeDef DES_CRYP_IVInitStructure; __IO uint32_t counter = 0; uint32_t busystatus = 0; ErrorStatus status = SUCCESS; uint32_t keyaddr = (uint32_t)Key; uint32_t inputaddr = (uint32_t)Input; uint32_t outputaddr = (uint32_t)Output; uint32_t ivaddr = (uint32_t)InitVectors; uint32_t i = 0; /* Crypto structures initialisation*/ CRYP_KeyStructInit(&DES_CRYP_KeyInitStructure); /* Crypto Init for Encryption process */ if(Mode == MODE_ENCRYPT) /* DES encryption */ { DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt; } else /*if(Mode == MODE_DECRYPT)*/ /* DES decryption */ { DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt; } DES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_DES_CBC; DES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b; CRYP_Init(&DES_CRYP_InitStructure); /* Key Initialisation */ DES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr)); keyaddr+=4; DES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr)); CRYP_KeyInit(& DES_CRYP_KeyInitStructure); /* Initialization Vectors */ DES_CRYP_IVInitStructure.CRYP_IV0Left = __REV(*(uint32_t*)(ivaddr)); ivaddr+=4; DES_CRYP_IVInitStructure.CRYP_IV0Right= __REV(*(uint32_t*)(ivaddr)); CRYP_IVInit(&DES_CRYP_IVInitStructure); /* Flush IN/OUT FIFO */ CRYP_FIFOFlush(); /* Enable Crypto processor */ CRYP_Cmd(ENABLE); if(CRYP_GetCmdStatus() == DISABLE) { /* The CRYP peripheral clock is not enabled or the device doesn't embedd the CRYP peripheral (please check the device sales type. */ return(ERROR); } for(i=0; ((i<Ilength) && (status != ERROR)); i+=8) { /* Write the Input block in the Input FIFO */ CRYP_DataIn(*(uint32_t*)(inputaddr)); inputaddr+=4; CRYP_DataIn(*(uint32_t*)(inputaddr)); inputaddr+=4; /* Wait until the complete message has been processed */ counter = 0; do { busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY); counter++; }while ((counter != DESBUSY_TIMEOUT) && (busystatus != RESET)); if (busystatus != RESET) { status = ERROR; } else { /* Read the Output block from the Output FIFO */ *(uint32_t*)(outputaddr) = CRYP_DataOut(); outputaddr+=4; *(uint32_t*)(outputaddr) = CRYP_DataOut(); outputaddr+=4; } } /* Disable Crypto */ CRYP_Cmd(DISABLE); return status; }
/** * @brief Encrypt and decrypt using AES in ECB Mode * @param Mode: encryption or decryption Mode. * This parameter can be one of the following values: * @arg MODE_ENCRYPT: Encryption * @arg MODE_DECRYPT: Decryption * @param Key: Key used for AES algorithm. * @param Keysize: length of the Key, must be a 128, 192 or 256. * @param Input: pointer to the Input buffer. * @param Ilength: length of the Input buffer, must be a multiple of 16. * @param Output: pointer to the returned buffer. * @retval An ErrorStatus enumeration value: * - SUCCESS: Operation done * - ERROR : Operation failed */ ErrorStatus CRYP_AES_ECB(uint8_t Mode, uint8_t* Key, uint16_t Keysize, uint8_t* Input, uint32_t Ilength, uint8_t* Output) { CRYP_InitTypeDef AES_CRYP_InitStructure; CRYP_KeyInitTypeDef AES_CRYP_KeyInitStructure; __IO uint32_t counter = 0; uint32_t busystatus = 0; ErrorStatus status = SUCCESS; uint32_t keyaddr = (uint32_t)Key; uint32_t inputaddr = (uint32_t)Input; uint32_t outputaddr = (uint32_t)Output; uint32_t i = 0; /* Crypto structures initialisation*/ CRYP_KeyStructInit(&AES_CRYP_KeyInitStructure); switch(Keysize) { case 128: AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_128b; AES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr)); keyaddr+=4; AES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr)); keyaddr+=4; AES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr)); keyaddr+=4; AES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr)); break; case 192: AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_192b; AES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr)); keyaddr+=4; AES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr)); keyaddr+=4; AES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr)); keyaddr+=4; AES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr)); keyaddr+=4; AES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr)); keyaddr+=4; AES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr)); break; case 256: AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_256b; AES_CRYP_KeyInitStructure.CRYP_Key0Left = __REV(*(uint32_t*)(keyaddr)); keyaddr+=4; AES_CRYP_KeyInitStructure.CRYP_Key0Right= __REV(*(uint32_t*)(keyaddr)); keyaddr+=4; AES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr)); keyaddr+=4; AES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr)); keyaddr+=4; AES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr)); keyaddr+=4; AES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr)); keyaddr+=4; AES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr)); keyaddr+=4; AES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr)); break; default: break; } /*------------------ AES Decryption ------------------*/ if(Mode == MODE_DECRYPT) /* AES decryption */ { /* Flush IN/OUT FIFOs */ CRYP_FIFOFlush(); /* Crypto Init for Key preparation for decryption process */ AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt; AES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_AES_Key; AES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_32b; CRYP_Init(&AES_CRYP_InitStructure); /* Key Initialisation */ CRYP_KeyInit(&AES_CRYP_KeyInitStructure); /* Enable Crypto processor */ CRYP_Cmd(ENABLE); /* wait until the Busy flag is RESET */ do { busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY); counter++; }while ((counter != AESBUSY_TIMEOUT) && (busystatus != RESET)); if (busystatus != RESET) { status = ERROR; } else { /* Crypto Init for decryption process */ AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt; } } /*------------------ AES Encryption ------------------*/ else /* AES encryption */ { CRYP_KeyInit(&AES_CRYP_KeyInitStructure); /* Crypto Init for Encryption process */ AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt; } AES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_AES_ECB; AES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b; CRYP_Init(&AES_CRYP_InitStructure); /* Flush IN/OUT FIFOs */ CRYP_FIFOFlush(); /* Enable Crypto processor */ CRYP_Cmd(ENABLE); for(i=0; ((i<Ilength) && (status != ERROR)); i+=16) { /* Write the Input block in the IN FIFO */ CRYP_DataIn(*(uint32_t*)(inputaddr)); inputaddr+=4; CRYP_DataIn(*(uint32_t*)(inputaddr)); inputaddr+=4; CRYP_DataIn(*(uint32_t*)(inputaddr)); inputaddr+=4; CRYP_DataIn(*(uint32_t*)(inputaddr)); inputaddr+=4; /* Wait until the complete message has been processed */ counter = 0; do { busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY); counter++; }while ((counter != AESBUSY_TIMEOUT) && (busystatus != RESET)); if (busystatus != RESET) { status = ERROR; } else { /* Read the Output block from the Output FIFO */ *(uint32_t*)(outputaddr) = CRYP_DataOut(); outputaddr+=4; *(uint32_t*)(outputaddr) = CRYP_DataOut(); outputaddr+=4; *(uint32_t*)(outputaddr) = CRYP_DataOut(); outputaddr+=4; *(uint32_t*)(outputaddr) = CRYP_DataOut(); outputaddr+=4; } } /* Disable Crypto */ CRYP_Cmd(DISABLE); return status; }
void DesCrypt(Des* des, byte* out, const byte* in, word32 sz, int dir, int mode) { word32 *dkey, *iv; CRYP_InitTypeDef DES_CRYP_InitStructure; CRYP_KeyInitTypeDef DES_CRYP_KeyInitStructure; CRYP_IVInitTypeDef DES_CRYP_IVInitStructure; dkey = des->key; iv = des->reg; /* crypto structure initialization */ CRYP_KeyStructInit(&DES_CRYP_KeyInitStructure); CRYP_StructInit(&DES_CRYP_InitStructure); CRYP_IVStructInit(&DES_CRYP_IVInitStructure); /* reset registers to their default values */ CRYP_DeInit(); /* set direction, mode, and datatype */ if (dir == DES_ENCRYPTION) { DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt; } else { /* DES_DECRYPTION */ DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt; } if (mode == DES_CBC) { DES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_DES_CBC; } else { /* DES_ECB */ DES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_DES_ECB; } DES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b; CRYP_Init(&DES_CRYP_InitStructure); /* load key into correct registers */ DES_CRYP_KeyInitStructure.CRYP_Key1Left = dkey[0]; DES_CRYP_KeyInitStructure.CRYP_Key1Right = dkey[1]; CRYP_KeyInit(&DES_CRYP_KeyInitStructure); /* set iv */ ByteReverseWords(iv, iv, DES_BLOCK_SIZE); DES_CRYP_IVInitStructure.CRYP_IV0Left = iv[0]; DES_CRYP_IVInitStructure.CRYP_IV0Right = iv[1]; CRYP_IVInit(&DES_CRYP_IVInitStructure); /* enable crypto processor */ CRYP_Cmd(ENABLE); while (sz > 0) { /* flush IN/OUT FIFOs */ CRYP_FIFOFlush(); /* if input and output same will overwrite input iv */ XMEMCPY(des->tmp, in + sz - DES_BLOCK_SIZE, DES_BLOCK_SIZE); CRYP_DataIn(*(uint32_t*)&in[0]); CRYP_DataIn(*(uint32_t*)&in[4]); /* wait until the complete message has been processed */ while(CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {} *(uint32_t*)&out[0] = CRYP_DataOut(); *(uint32_t*)&out[4] = CRYP_DataOut(); /* store iv for next call */ XMEMCPY(des->reg, des->tmp, DES_BLOCK_SIZE); sz -= DES_BLOCK_SIZE; in += DES_BLOCK_SIZE; out += DES_BLOCK_SIZE; } /* disable crypto processor */ CRYP_Cmd(DISABLE); }
/** * @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); }
PUBLIC void SER_CRYP_Init(t_uint8 cryp_ser_mask) { t_cryp_error error; t_gic_error error_gic; t_gic_func_ptr p_old_handler; if(INIT_CRYP0 == cryp_ser_mask) { #if ((defined ST_8500V1) || (defined ST_HREFV1) || (defined ST_8500V2) || (defined ST_HREFV2)) *((volatile t_uint32 *)PRCC_6_CTRL_REG_BASE_ADDR) |= 0x00000002; *((volatile t_uint32 *)(PRCC_6_CTRL_REG_BASE_ADDR + 0x08)) |= 0x00000002; #endif /* Set base address for the CRYP */ error = CRYP_Init(CRYP_DEVICE_ID_0,CRYP_0_REG_BASE_ADDR); if(CRYP_OK != error) { PRINT("CRYP0 INIT FAILED"); } else { CRYP_SetBaseAddress(CRYP_DEVICE_ID_0,CRYP_0_REG_BASE_ADDR); } error_gic = GIC_DisableItLine(GIC_CRYP_0_LINE); if (GIC_OK != error_gic) { PRINT("GIC_DisableItLine::Error in Disabling Interrupt for CRYPT0"); exit(-1); } error_gic = GIC_ChangeDatum(GIC_CRYP_0_LINE , SER_CRYP0_InterruptHandler, &p_old_handler); if (GIC_OK != error_gic) { PRINT("GIC_ChangeDatum::Error in Binding Interrupt for CRYPT0"); exit(-1); } error_gic = GIC_EnableItLine(GIC_CRYP_0_LINE); if (GIC_OK != error_gic) { PRINT("GIC_EnableItLine::Error in Enabling Interrupt for CRYPT0"); exit(-1); } } else if(INIT_CRYP1 == cryp_ser_mask) { /* Set base address for the CRYP */ error = CRYP_Init(CRYP_DEVICE_ID_1,CRYP_1_REG_BASE_ADDR); if(CRYP_OK != error) { PRINT("CRYP1 INIT FAILED"); } else { CRYP_SetBaseAddress(CRYP_DEVICE_ID_1,CRYP_1_REG_BASE_ADDR); } error_gic = GIC_DisableItLine(GIC_CRYP_1_LINE); if (GIC_OK != error_gic) { PRINT("GIC_DisableItLine::Error in Disabling Interrupt for CRYPT1"); exit(-1); } error_gic = GIC_ChangeDatum(GIC_CRYP_1_LINE , SER_CRYP1_InterruptHandler, &p_old_handler); if (GIC_OK != error_gic) { PRINT("GIC_ChangeDatum::Error in Binding Interrupt for CRYPT1"); exit(-1); } error_gic = GIC_EnableItLine(GIC_CRYP_1_LINE); if (GIC_OK != error_gic) { PRINT("GIC_EnableItLine::Error in Enabling Interrupt for CRYPT1"); exit(-1); } } }