stm32f4xx_cryp.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_cryp.h
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 30-September-2011
  7. * @brief This file contains all the functions prototypes for the Cryptographic
  8. * processor(CRYP) firmware library.
  9. ******************************************************************************
  10. * @attention
  11. *
  12. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  13. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  14. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  15. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  16. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  17. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  18. *
  19. * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  20. ******************************************************************************
  21. */
  22. /* Define to prevent recursive inclusion -------------------------------------*/
  23. #ifndef __STM32F4xx_CRYP_H
  24. #define __STM32F4xx_CRYP_H
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. /* Includes ------------------------------------------------------------------*/
  29. #include "stm32f4xx.h"
  30. /** @addtogroup STM32F4xx_StdPeriph_Driver
  31. * @{
  32. */
  33. /** @addtogroup CRYP
  34. * @{
  35. */
  36. /* Exported types ------------------------------------------------------------*/
  37. /**
  38. * @brief CRYP Init structure definition
  39. */
  40. typedef struct
  41. {
  42. uint16_t CRYP_AlgoDir; /*!< Encrypt or Decrypt. This parameter can be a
  43. value of @ref CRYP_Algorithm_Direction */
  44. uint16_t CRYP_AlgoMode; /*!< TDES-ECB, TDES-CBC, DES-ECB, DES-CBC, AES-ECB,
  45. AES-CBC, AES-CTR, AES-Key. This parameter can be
  46. a value of @ref CRYP_Algorithm_Mode */
  47. uint16_t CRYP_DataType; /*!< 32-bit data, 16-bit data, bit data or bit-string.
  48. This parameter can be a value of @ref CRYP_Data_Type */
  49. uint16_t CRYP_KeySize; /*!< Used only in AES mode only : 128, 192 or 256 bit
  50. key length. This parameter can be a value of
  51. @ref CRYP_Key_Size_for_AES_only */
  52. }CRYP_InitTypeDef;
  53. /**
  54. * @brief CRYP Key(s) structure definition
  55. */
  56. typedef struct
  57. {
  58. uint32_t CRYP_Key0Left; /*!< Key 0 Left */
  59. uint32_t CRYP_Key0Right; /*!< Key 0 Right */
  60. uint32_t CRYP_Key1Left; /*!< Key 1 left */
  61. uint32_t CRYP_Key1Right; /*!< Key 1 Right */
  62. uint32_t CRYP_Key2Left; /*!< Key 2 left */
  63. uint32_t CRYP_Key2Right; /*!< Key 2 Right */
  64. uint32_t CRYP_Key3Left; /*!< Key 3 left */
  65. uint32_t CRYP_Key3Right; /*!< Key 3 Right */
  66. }CRYP_KeyInitTypeDef;
  67. /**
  68. * @brief CRYP Initialization Vectors (IV) structure definition
  69. */
  70. typedef struct
  71. {
  72. uint32_t CRYP_IV0Left; /*!< Init Vector 0 Left */
  73. uint32_t CRYP_IV0Right; /*!< Init Vector 0 Right */
  74. uint32_t CRYP_IV1Left; /*!< Init Vector 1 left */
  75. uint32_t CRYP_IV1Right; /*!< Init Vector 1 Right */
  76. }CRYP_IVInitTypeDef;
  77. /**
  78. * @brief CRYP context swapping structure definition
  79. */
  80. typedef struct
  81. {
  82. /*!< Configuration */
  83. uint32_t CR_bits9to2;
  84. /*!< KEY */
  85. uint32_t CRYP_IV0LR;
  86. uint32_t CRYP_IV0RR;
  87. uint32_t CRYP_IV1LR;
  88. uint32_t CRYP_IV1RR;
  89. /*!< IV */
  90. uint32_t CRYP_K0LR;
  91. uint32_t CRYP_K0RR;
  92. uint32_t CRYP_K1LR;
  93. uint32_t CRYP_K1RR;
  94. uint32_t CRYP_K2LR;
  95. uint32_t CRYP_K2RR;
  96. uint32_t CRYP_K3LR;
  97. uint32_t CRYP_K3RR;
  98. }CRYP_Context;
  99. /* Exported constants --------------------------------------------------------*/
  100. /** @defgroup CRYP_Exported_Constants
  101. * @{
  102. */
  103. /** @defgroup CRYP_Algorithm_Direction
  104. * @{
  105. */
  106. #define CRYP_AlgoDir_Encrypt ((uint16_t)0x0000)
  107. #define CRYP_AlgoDir_Decrypt ((uint16_t)0x0004)
  108. #define IS_CRYP_ALGODIR(ALGODIR) (((ALGODIR) == CRYP_AlgoDir_Encrypt) || \
  109. ((ALGODIR) == CRYP_AlgoDir_Decrypt))
  110. /**
  111. * @}
  112. */
  113. /** @defgroup CRYP_Algorithm_Mode
  114. * @{
  115. */
  116. /*!< TDES Modes */
  117. #define CRYP_AlgoMode_TDES_ECB ((uint16_t)0x0000)
  118. #define CRYP_AlgoMode_TDES_CBC ((uint16_t)0x0008)
  119. /*!< DES Modes */
  120. #define CRYP_AlgoMode_DES_ECB ((uint16_t)0x0010)
  121. #define CRYP_AlgoMode_DES_CBC ((uint16_t)0x0018)
  122. /*!< AES Modes */
  123. #define CRYP_AlgoMode_AES_ECB ((uint16_t)0x0020)
  124. #define CRYP_AlgoMode_AES_CBC ((uint16_t)0x0028)
  125. #define CRYP_AlgoMode_AES_CTR ((uint16_t)0x0030)
  126. #define CRYP_AlgoMode_AES_Key ((uint16_t)0x0038)
  127. #define IS_CRYP_ALGOMODE(ALGOMODE) (((ALGOMODE) == CRYP_AlgoMode_TDES_ECB) || \
  128. ((ALGOMODE) == CRYP_AlgoMode_TDES_CBC)|| \
  129. ((ALGOMODE) == CRYP_AlgoMode_DES_ECB)|| \
  130. ((ALGOMODE) == CRYP_AlgoMode_DES_CBC) || \
  131. ((ALGOMODE) == CRYP_AlgoMode_AES_ECB) || \
  132. ((ALGOMODE) == CRYP_AlgoMode_AES_CBC) || \
  133. ((ALGOMODE) == CRYP_AlgoMode_AES_CTR) || \
  134. ((ALGOMODE) == CRYP_AlgoMode_AES_Key))
  135. /**
  136. * @}
  137. */
  138. /** @defgroup CRYP_Data_Type
  139. * @{
  140. */
  141. #define CRYP_DataType_32b ((uint16_t)0x0000)
  142. #define CRYP_DataType_16b ((uint16_t)0x0040)
  143. #define CRYP_DataType_8b ((uint16_t)0x0080)
  144. #define CRYP_DataType_1b ((uint16_t)0x00C0)
  145. #define IS_CRYP_DATATYPE(DATATYPE) (((DATATYPE) == CRYP_DataType_32b) || \
  146. ((DATATYPE) == CRYP_DataType_16b)|| \
  147. ((DATATYPE) == CRYP_DataType_8b)|| \
  148. ((DATATYPE) == CRYP_DataType_1b))
  149. /**
  150. * @}
  151. */
  152. /** @defgroup CRYP_Key_Size_for_AES_only
  153. * @{
  154. */
  155. #define CRYP_KeySize_128b ((uint16_t)0x0000)
  156. #define CRYP_KeySize_192b ((uint16_t)0x0100)
  157. #define CRYP_KeySize_256b ((uint16_t)0x0200)
  158. #define IS_CRYP_KEYSIZE(KEYSIZE) (((KEYSIZE) == CRYP_KeySize_128b)|| \
  159. ((KEYSIZE) == CRYP_KeySize_192b)|| \
  160. ((KEYSIZE) == CRYP_KeySize_256b))
  161. /**
  162. * @}
  163. */
  164. /** @defgroup CRYP_flags_definition
  165. * @{
  166. */
  167. #define CRYP_FLAG_BUSY ((uint8_t)0x10) /*!< The CRYP core is currently
  168. processing a block of data
  169. or a key preparation (for
  170. AES decryption). */
  171. #define CRYP_FLAG_IFEM ((uint8_t)0x01) /*!< Input Fifo Empty */
  172. #define CRYP_FLAG_IFNF ((uint8_t)0x02) /*!< Input Fifo is Not Full */
  173. #define CRYP_FLAG_INRIS ((uint8_t)0x22) /*!< Raw interrupt pending */
  174. #define CRYP_FLAG_OFNE ((uint8_t)0x04) /*!< Input Fifo service raw
  175. interrupt status */
  176. #define CRYP_FLAG_OFFU ((uint8_t)0x08) /*!< Output Fifo is Full */
  177. #define CRYP_FLAG_OUTRIS ((uint8_t)0x21) /*!< Output Fifo service raw
  178. interrupt status */
  179. #define IS_CRYP_GET_FLAG(FLAG) (((FLAG) == CRYP_FLAG_IFEM) || \
  180. ((FLAG) == CRYP_FLAG_IFNF) || \
  181. ((FLAG) == CRYP_FLAG_OFNE) || \
  182. ((FLAG) == CRYP_FLAG_OFFU) || \
  183. ((FLAG) == CRYP_FLAG_BUSY) || \
  184. ((FLAG) == CRYP_FLAG_OUTRIS)|| \
  185. ((FLAG) == CRYP_FLAG_INRIS))
  186. /**
  187. * @}
  188. */
  189. /** @defgroup CRYP_interrupts_definition
  190. * @{
  191. */
  192. #define CRYP_IT_INI ((uint8_t)0x01) /*!< IN Fifo Interrupt */
  193. #define CRYP_IT_OUTI ((uint8_t)0x02) /*!< OUT Fifo Interrupt */
  194. #define IS_CRYP_CONFIG_IT(IT) ((((IT) & (uint8_t)0xFC) == 0x00) && ((IT) != 0x00))
  195. #define IS_CRYP_GET_IT(IT) (((IT) == CRYP_IT_INI) || ((IT) == CRYP_IT_OUTI))
  196. /**
  197. * @}
  198. */
  199. /** @defgroup CRYP_Encryption_Decryption_modes_definition
  200. * @{
  201. */
  202. #define MODE_ENCRYPT ((uint8_t)0x01)
  203. #define MODE_DECRYPT ((uint8_t)0x00)
  204. /**
  205. * @}
  206. */
  207. /** @defgroup CRYP_DMA_transfer_requests
  208. * @{
  209. */
  210. #define CRYP_DMAReq_DataIN ((uint8_t)0x01)
  211. #define CRYP_DMAReq_DataOUT ((uint8_t)0x02)
  212. #define IS_CRYP_DMAREQ(DMAREQ) ((((DMAREQ) & (uint8_t)0xFC) == 0x00) && ((DMAREQ) != 0x00))
  213. /**
  214. * @}
  215. */
  216. /**
  217. * @}
  218. */
  219. /* Exported macro ------------------------------------------------------------*/
  220. /* Exported functions --------------------------------------------------------*/
  221. /* Function used to set the CRYP configuration to the default reset state ****/
  222. void CRYP_DeInit(void);
  223. /* CRYP Initialization and Configuration functions ****************************/
  224. void CRYP_Init(CRYP_InitTypeDef* CRYP_InitStruct);
  225. void CRYP_StructInit(CRYP_InitTypeDef* CRYP_InitStruct);
  226. void CRYP_KeyInit(CRYP_KeyInitTypeDef* CRYP_KeyInitStruct);
  227. void CRYP_KeyStructInit(CRYP_KeyInitTypeDef* CRYP_KeyInitStruct);
  228. void CRYP_IVInit(CRYP_IVInitTypeDef* CRYP_IVInitStruct);
  229. void CRYP_IVStructInit(CRYP_IVInitTypeDef* CRYP_IVInitStruct);
  230. void CRYP_Cmd(FunctionalState NewState);
  231. /* CRYP Data processing functions *********************************************/
  232. void CRYP_DataIn(uint32_t Data);
  233. uint32_t CRYP_DataOut(void);
  234. void CRYP_FIFOFlush(void);
  235. /* CRYP Context swapping functions ********************************************/
  236. ErrorStatus CRYP_SaveContext(CRYP_Context* CRYP_ContextSave,
  237. CRYP_KeyInitTypeDef* CRYP_KeyInitStruct);
  238. void CRYP_RestoreContext(CRYP_Context* CRYP_ContextRestore);
  239. /* CRYP's DMA interface function **********************************************/
  240. void CRYP_DMACmd(uint8_t CRYP_DMAReq, FunctionalState NewState);
  241. /* Interrupts and flags management functions **********************************/
  242. void CRYP_ITConfig(uint8_t CRYP_IT, FunctionalState NewState);
  243. ITStatus CRYP_GetITStatus(uint8_t CRYP_IT);
  244. FlagStatus CRYP_GetFlagStatus(uint8_t CRYP_FLAG);
  245. /* High Level AES functions **************************************************/
  246. ErrorStatus CRYP_AES_ECB(uint8_t Mode,
  247. uint8_t *Key, uint16_t Keysize,
  248. uint8_t *Input, uint32_t Ilength,
  249. uint8_t *Output);
  250. ErrorStatus CRYP_AES_CBC(uint8_t Mode,
  251. uint8_t InitVectors[16],
  252. uint8_t *Key, uint16_t Keysize,
  253. uint8_t *Input, uint32_t Ilength,
  254. uint8_t *Output);
  255. ErrorStatus CRYP_AES_CTR(uint8_t Mode,
  256. uint8_t InitVectors[16],
  257. uint8_t *Key, uint16_t Keysize,
  258. uint8_t *Input, uint32_t Ilength,
  259. uint8_t *Output);
  260. /* High Level TDES functions **************************************************/
  261. ErrorStatus CRYP_TDES_ECB(uint8_t Mode,
  262. uint8_t Key[24],
  263. uint8_t *Input, uint32_t Ilength,
  264. uint8_t *Output);
  265. ErrorStatus CRYP_TDES_CBC(uint8_t Mode,
  266. uint8_t Key[24],
  267. uint8_t InitVectors[8],
  268. uint8_t *Input, uint32_t Ilength,
  269. uint8_t *Output);
  270. /* High Level DES functions **************************************************/
  271. ErrorStatus CRYP_DES_ECB(uint8_t Mode,
  272. uint8_t Key[8],
  273. uint8_t *Input, uint32_t Ilength,
  274. uint8_t *Output);
  275. ErrorStatus CRYP_DES_CBC(uint8_t Mode,
  276. uint8_t Key[8],
  277. uint8_t InitVectors[8],
  278. uint8_t *Input,uint32_t Ilength,
  279. uint8_t *Output);
  280. #ifdef __cplusplus
  281. }
  282. #endif
  283. #endif /*__STM32F4xx_CRYP_H */
  284. /**
  285. * @}
  286. */
  287. /**
  288. * @}
  289. */
  290. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/