📄 stm32f2xx_cryp.c
字号:
/**
******************************************************************************
* @file stm32f2xx_cryp.c
* @author MCD Application Team
* @version V1.0.0
* @date 18-April-2011
* @brief This file provides firmware functions to manage the following
* functionalities of the Cryptographic processor (CRYP) peripheral:
* - Initialization and Configuration functions
* - Data treatment functions
* - Context swapping functions
* - DMA interface function
* - Interrupts and flags management
*
* @verbatim
*
* ===================================================================
* How to use this driver
* ===================================================================
* 1. Enable the CRYP controller clock using
* RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_CRYP, ENABLE); function.
*
* 2. Initialise the CRYP using CRYP_Init(), CRYP_KeyInit() and if
* needed CRYP_IVInit().
*
* 3. Flush the IN and OUT FIFOs by using CRYP_FIFOFlush() function.
*
* 4. Enable the CRYP controller using the CRYP_Cmd() function.
*
* 5. If using DMA for Data input and output transfer,
* Activate the needed DMA Requests using CRYP_DMACmd() function
* 6. If DMA is not used for data transfer, use CRYP_DataIn() and
* CRYP_DataOut() functions to enter data to IN FIFO and get result
* from OUT FIFO.
*
* 7. To control CRYP events you can use one of the following
* two methods:
* - Check on CRYP flags using the CRYP_GetFlagStatus() function.
* - Use CRYP interrupts through the function CRYP_ITConfig() at
* initialization phase and CRYP_GetITStatus() function into
* interrupt routines in processing phase.
*
* 8. Save and restore Cryptographic processor context using
* CRYP_SaveContext() and CRYP_RestoreContext() functions.
*
*
* ===================================================================
* Procedure to perform an encryption or a decryption
* ===================================================================
*
* Initialization
* ===============
* 1. Initialize the peripheral using CRYP_Init(), CRYP_KeyInit() and
* CRYP_IVInit functions:
* - Configure the key size (128-, 192- or 256-bit, in the AES only)
* - Enter the symmetric key
* - Configure the data type
* - In case of decryption in AES-ECB or AES-CBC, you must prepare
* the key: configure the key preparation mode. Then Enable the CRYP
* peripheral using CRYP_Cmd() function: the BUSY flag is set.
* Wait until BUSY flag is reset : the key is prepared for decryption
* - Configure the algorithm and chaining (the DES/TDES in ECB/CBC, the
* AES in ECB/CBC/CTR)
* - Configure the direction (encryption/decryption).
* - Write the initialization vectors (in CBC or CTR modes only)
*
* 2. Flush the IN and OUT FIFOs using the CRYP_FIFOFlush() function
*
*
* Basic Processing mode (polling mode)
* ====================================
* 1. Enable the cryptographic processor using CRYP_Cmd() function.
*
* 2. Write the first blocks in the input FIFO (2 to 8 words) using
* CRYP_DataIn() function.
*
* 3. Repeat the following sequence until the complete message has been
* processed:
*
* a) Wait for flag CRYP_FLAG_OFNE occurs (using CRYP_GetFlagStatus()
* function), then read the OUT-FIFO using CRYP_DataOut() function
* (1 block or until the FIFO is empty)
*
* b) Wait for flag CRYP_FLAG_IFNF occurs, (using CRYP_GetFlagStatus()
* function then write the IN FIFO using CRYP_DataIn() function
* (1 block or until the FIFO is full)
*
* 4. At the end of the processing, CRYP_FLAG_BUSY flag will be reset and
* both FIFOs are empty (CRYP_FLAG_IFEM is set and CRYP_FLAG_OFNE is
* reset). You can disable the peripheral using CRYP_Cmd() function.
*
* Interrupts Processing mode
* ===========================
* In this mode, Processing is done when the data are transferred by the
* CPU during interrupts.
*
* 1. Enable the interrupts CRYP_IT_INI and CRYP_IT_OUTI using
* CRYP_ITConfig() function.
*
* 2. Enable the cryptographic processor using CRYP_Cmd() function.
*
* 3. In the CRYP_IT_INI interrupt handler : load the input message into the
* IN FIFO using CRYP_DataIn() function . You can load 2 or 4 words at a
* time, or load data until the IN FIFO is full. When the last word of
* the message has been entered into the IN FIFO, disable the CRYP_IT_INI
* interrupt (using CRYP_ITConfig() function).
*
* 4. In the CRYP_IT_OUTI interrupt handler : read the output message from
* the OUT FIFO using CRYP_DataOut() function. You can read 1 block (2 or
* 4 words) at a time or read data until the FIFO is empty.
* When the last word has been read, INIM=0, BUSY=0 and both FIFOs are
* empty (CRYP_FLAG_IFEM is set and CRYP_FLAG_OFNE is reset).
* You can disable the CRYP_IT_OUTI interrupt (using CRYP_ITConfig()
* function) and you can disable the peripheral using CRYP_Cmd() function.
*
* DMA Processing mode
* ====================
* In this mode, Processing is done when the DMA is used to transfer the
* data from/to the memory.
*
* 1. Configure the DMA controller to transfer the input data from the
* memory using DMA_Init() function.
* The transfer length is the length of the message.
* As message padding is not managed by the peripheral, the message
* length must be an entire number of blocks. The data are transferred
* in burst mode. The burst length is 4 words in the AES and 2 or 4
* words in the DES/TDES. The DMA should be configured to set an
* interrupt on transfer completion of the output data to indicate that
* the processing is finished.
* Refer to DMA peripheral driver for more details.
*
* 2. Enable the cryptographic processor using CRYP_Cmd() function.
* Enable the DMA requests CRYP_DMAReq_DataIN and CRYP_DMAReq_DataOUT
* using CRYP_DMACmd() function.
*
* 3. All the transfers and processing are managed by the DMA and the
* cryptographic processor. The DMA transfer complete interrupt indicates
* that the processing is complete. Both FIFOs are normally empty and
* CRYP_FLAG_BUSY flag is reset.
*
* @endverbatim
*
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f2xx_cryp.h"
#include "stm32f2xx_rcc.h"
/** @addtogroup STM32F2xx_StdPeriph_Driver
* @{
*/
/** @defgroup CRYP
* @brief CRYP driver modules
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define FLAG_MASK ((uint8_t)0x20)
#define MAX_TIMEOUT ((uint16_t)0xFFFF)
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/** @defgroup CRYP_Private_Functions
* @{
*/
/** @defgroup CRYP_Group1 Initialization and Configuration functions
* @brief Initialization and Configuration functions
*
@verbatim
===============================================================================
Initialization and Configuration functions
===============================================================================
This section provides functions allowing to
- Initialize the cryptographic Processor using CRYP_Init() function
- Encrypt or Decrypt
- mode : TDES-ECB, TDES-CBC,
DES-ECB, DES-CBC,
AES-ECB, AES-CBC, AES-CTR, AES-Key
- DataType : 32-bit data, 16-bit data, bit data or bit-string
- Key Size (only in AES modes)
- Configure the Encrypt or Decrypt Key using CRYP_KeyInit() function
- Configure the Initialization Vectors(IV) for CBC and CTR modes using
CRYP_IVInit() function.
- Flushes the IN and OUT FIFOs : using CRYP_FIFOFlush() function.
- Enable or disable the CRYP Processor using CRYP_Cmd() function
@endverbatim
* @{
*/
/**
* @brief Deinitializes the CRYP peripheral registers to their default reset values
* @param None
* @retval None
*/
void CRYP_DeInit(void)
{
/* Enable CRYP reset state */
RCC_AHB2PeriphResetCmd(RCC_AHB2Periph_CRYP, ENABLE);
/* Release CRYP from reset state */
RCC_AHB2PeriphResetCmd(RCC_AHB2Periph_CRYP, DISABLE);
}
/**
* @brief Initializes the CRYP peripheral according to the specified parameters
* in the CRYP_InitStruct.
* @param CRYP_InitStruct: pointer to a CRYP_InitTypeDef structure that contains
* the configuration information for the CRYP peripheral.
* @retval None
*/
void CRYP_Init(CRYP_InitTypeDef* CRYP_InitStruct)
{
/* Check the parameters */
assert_param(IS_CRYP_ALGOMODE(CRYP_InitStruct->CRYP_AlgoMode));
assert_param(IS_CRYP_DATATYPE(CRYP_InitStruct->CRYP_DataType));
assert_param(IS_CRYP_ALGODIR(CRYP_InitStruct->CRYP_AlgoDir));
/* Select Algorithm mode*/
CRYP->CR &= ~CRYP_CR_ALGOMODE;
CRYP->CR |= CRYP_InitStruct->CRYP_AlgoMode;
/* Select dataType */
CRYP->CR &= ~CRYP_CR_DATATYPE;
CRYP->CR |= CRYP_InitStruct->CRYP_DataType;
/* select Key size (used only with AES algorithm) */
if ((CRYP_InitStruct->CRYP_AlgoMode == CRYP_AlgoMode_AES_ECB) ||
(CRYP_InitStruct->CRYP_AlgoMode == CRYP_AlgoMode_AES_CBC) ||
(CRYP_InitStruct->CRYP_AlgoMode == CRYP_AlgoMode_AES_CTR) ||
(CRYP_InitStruct->CRYP_AlgoMode == CRYP_AlgoMode_AES_Key))
{
assert_param(IS_CRYP_KEYSIZE(CRYP_InitStruct->CRYP_KeySize));
CRYP->CR &= ~CRYP_CR_KEYSIZE;
CRYP->CR |= CRYP_InitStruct->CRYP_KeySize; /* Key size and value must be
configured once the key has
been prepared */
}
/* Select data Direction */
CRYP->CR &= ~CRYP_CR_ALGODIR;
CRYP->CR |= CRYP_InitStruct->CRYP_AlgoDir;
}
/**
* @brief Fills each CRYP_InitStruct member with its default value.
* @param CRYP_InitStruct: pointer to a CRYP_InitTypeDef structure which will
* be initialized.
* @retval None
*/
void CRYP_StructInit(CRYP_InitTypeDef* CRYP_InitStruct)
{
/* Initialize the CRYP_AlgoDir member */
CRYP_InitStruct->CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
/* initialize the CRYP_AlgoMode member */
CRYP_InitStruct->CRYP_AlgoMode = CRYP_AlgoMode_TDES_ECB;
/* initialize the CRYP_DataType member */
CRYP_InitStruct->CRYP_DataType = CRYP_DataType_32b;
/* Initialize the CRYP_KeySize member */
CRYP_InitStruct->CRYP_KeySize = CRYP_KeySize_128b;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -