stm32f2xx_cryp.h 12 KB

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