stm32f4xx_cryp.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_cryp.h
  4. * @author MCD Application Team
  5. * @version V1.8.1
  6. * @date 27-January-2022
  7. * @brief This file contains all the functions prototypes for the Cryptographic
  8. * processor(CRYP) firmware library.
  9. ******************************************************************************
  10. * @attention
  11. *
  12. * Copyright (c) 2016 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 __STM32F4xx_CRYP_H
  23. #define __STM32F4xx_CRYP_H
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. /* Includes ------------------------------------------------------------------*/
  28. #include "stm32f4xx.h"
  29. /** @addtogroup STM32F4xx_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. uint32_t CRYP_AlgoDir; /*!< Encrypt or Decrypt. This parameter can be a
  42. value of @ref CRYP_Algorithm_Direction */
  43. uint32_t CRYP_AlgoMode; /*!< TDES-ECB, TDES-CBC, DES-ECB, DES-CBC, AES-ECB,
  44. AES-CBC, AES-CTR, AES-Key, AES-GCM and AES-CCM.
  45. This parameter can be a value of @ref CRYP_Algorithm_Mode */
  46. uint32_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. uint32_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. /*!< Current Configuration */
  82. uint32_t CR_CurrentConfig;
  83. /*!< IV */
  84. uint32_t CRYP_IV0LR;
  85. uint32_t CRYP_IV0RR;
  86. uint32_t CRYP_IV1LR;
  87. uint32_t CRYP_IV1RR;
  88. /*!< KEY */
  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. uint32_t CRYP_CSGCMCCMR[8];
  98. uint32_t CRYP_CSGCMR[8];
  99. }CRYP_Context;
  100. /* Exported constants --------------------------------------------------------*/
  101. /** @defgroup CRYP_Exported_Constants
  102. * @{
  103. */
  104. /** @defgroup CRYP_Algorithm_Direction
  105. * @{
  106. */
  107. #define CRYP_AlgoDir_Encrypt ((uint16_t)0x0000)
  108. #define CRYP_AlgoDir_Decrypt ((uint16_t)0x0004)
  109. #define IS_CRYP_ALGODIR(ALGODIR) (((ALGODIR) == CRYP_AlgoDir_Encrypt) || \
  110. ((ALGODIR) == CRYP_AlgoDir_Decrypt))
  111. /**
  112. * @}
  113. */
  114. /** @defgroup CRYP_Algorithm_Mode
  115. * @{
  116. */
  117. /*!< TDES Modes */
  118. #define CRYP_AlgoMode_TDES_ECB ((uint32_t)0x00000000)
  119. #define CRYP_AlgoMode_TDES_CBC ((uint32_t)0x00000008)
  120. /*!< DES Modes */
  121. #define CRYP_AlgoMode_DES_ECB ((uint32_t)0x00000010)
  122. #define CRYP_AlgoMode_DES_CBC ((uint32_t)0x00000018)
  123. /*!< AES Modes */
  124. #define CRYP_AlgoMode_AES_ECB ((uint32_t)0x00000020)
  125. #define CRYP_AlgoMode_AES_CBC ((uint32_t)0x00000028)
  126. #define CRYP_AlgoMode_AES_CTR ((uint32_t)0x00000030)
  127. #define CRYP_AlgoMode_AES_Key ((uint32_t)0x00000038)
  128. #define CRYP_AlgoMode_AES_GCM ((uint32_t)0x00080000)
  129. #define CRYP_AlgoMode_AES_CCM ((uint32_t)0x00080008)
  130. #define IS_CRYP_ALGOMODE(ALGOMODE) (((ALGOMODE) == CRYP_AlgoMode_TDES_ECB) || \
  131. ((ALGOMODE) == CRYP_AlgoMode_TDES_CBC)|| \
  132. ((ALGOMODE) == CRYP_AlgoMode_DES_ECB) || \
  133. ((ALGOMODE) == CRYP_AlgoMode_DES_CBC) || \
  134. ((ALGOMODE) == CRYP_AlgoMode_AES_ECB) || \
  135. ((ALGOMODE) == CRYP_AlgoMode_AES_CBC) || \
  136. ((ALGOMODE) == CRYP_AlgoMode_AES_CTR) || \
  137. ((ALGOMODE) == CRYP_AlgoMode_AES_Key) || \
  138. ((ALGOMODE) == CRYP_AlgoMode_AES_GCM) || \
  139. ((ALGOMODE) == CRYP_AlgoMode_AES_CCM))
  140. /**
  141. * @}
  142. */
  143. /** @defgroup CRYP_Phase
  144. * @{
  145. */
  146. /*!< The phases are valid only for AES-GCM and AES-CCM modes */
  147. #define CRYP_Phase_Init ((uint32_t)0x00000000)
  148. #define CRYP_Phase_Header CRYP_CR_GCM_CCMPH_0
  149. #define CRYP_Phase_Payload CRYP_CR_GCM_CCMPH_1
  150. #define CRYP_Phase_Final CRYP_CR_GCM_CCMPH
  151. #define IS_CRYP_PHASE(PHASE) (((PHASE) == CRYP_Phase_Init) || \
  152. ((PHASE) == CRYP_Phase_Header) || \
  153. ((PHASE) == CRYP_Phase_Payload) || \
  154. ((PHASE) == CRYP_Phase_Final))
  155. /**
  156. * @}
  157. */
  158. /** @defgroup CRYP_Data_Type
  159. * @{
  160. */
  161. #define CRYP_DataType_32b ((uint16_t)0x0000)
  162. #define CRYP_DataType_16b ((uint16_t)0x0040)
  163. #define CRYP_DataType_8b ((uint16_t)0x0080)
  164. #define CRYP_DataType_1b ((uint16_t)0x00C0)
  165. #define IS_CRYP_DATATYPE(DATATYPE) (((DATATYPE) == CRYP_DataType_32b) || \
  166. ((DATATYPE) == CRYP_DataType_16b)|| \
  167. ((DATATYPE) == CRYP_DataType_8b)|| \
  168. ((DATATYPE) == CRYP_DataType_1b))
  169. /**
  170. * @}
  171. */
  172. /** @defgroup CRYP_Key_Size_for_AES_only
  173. * @{
  174. */
  175. #define CRYP_KeySize_128b ((uint16_t)0x0000)
  176. #define CRYP_KeySize_192b ((uint16_t)0x0100)
  177. #define CRYP_KeySize_256b ((uint16_t)0x0200)
  178. #define IS_CRYP_KEYSIZE(KEYSIZE) (((KEYSIZE) == CRYP_KeySize_128b)|| \
  179. ((KEYSIZE) == CRYP_KeySize_192b)|| \
  180. ((KEYSIZE) == CRYP_KeySize_256b))
  181. /**
  182. * @}
  183. */
  184. /** @defgroup CRYP_flags_definition
  185. * @{
  186. */
  187. #define CRYP_FLAG_BUSY ((uint8_t)0x10) /*!< The CRYP core is currently
  188. processing a block of data
  189. or a key preparation (for
  190. AES decryption). */
  191. #define CRYP_FLAG_IFEM ((uint8_t)0x01) /*!< Input Fifo Empty */
  192. #define CRYP_FLAG_IFNF ((uint8_t)0x02) /*!< Input Fifo is Not Full */
  193. #define CRYP_FLAG_INRIS ((uint8_t)0x22) /*!< Raw interrupt pending */
  194. #define CRYP_FLAG_OFNE ((uint8_t)0x04) /*!< Input Fifo service raw
  195. interrupt status */
  196. #define CRYP_FLAG_OFFU ((uint8_t)0x08) /*!< Output Fifo is Full */
  197. #define CRYP_FLAG_OUTRIS ((uint8_t)0x21) /*!< Output Fifo service raw
  198. interrupt status */
  199. #define IS_CRYP_GET_FLAG(FLAG) (((FLAG) == CRYP_FLAG_IFEM) || \
  200. ((FLAG) == CRYP_FLAG_IFNF) || \
  201. ((FLAG) == CRYP_FLAG_OFNE) || \
  202. ((FLAG) == CRYP_FLAG_OFFU) || \
  203. ((FLAG) == CRYP_FLAG_BUSY) || \
  204. ((FLAG) == CRYP_FLAG_OUTRIS)|| \
  205. ((FLAG) == CRYP_FLAG_INRIS))
  206. /**
  207. * @}
  208. */
  209. /** @defgroup CRYP_interrupts_definition
  210. * @{
  211. */
  212. #define CRYP_IT_INI ((uint8_t)0x01) /*!< IN Fifo Interrupt */
  213. #define CRYP_IT_OUTI ((uint8_t)0x02) /*!< OUT Fifo Interrupt */
  214. #define IS_CRYP_CONFIG_IT(IT) ((((IT) & (uint8_t)0xFC) == 0x00) && ((IT) != 0x00))
  215. #define IS_CRYP_GET_IT(IT) (((IT) == CRYP_IT_INI) || ((IT) == CRYP_IT_OUTI))
  216. /**
  217. * @}
  218. */
  219. /** @defgroup CRYP_Encryption_Decryption_modes_definition
  220. * @{
  221. */
  222. #define MODE_ENCRYPT ((uint8_t)0x01)
  223. #define MODE_DECRYPT ((uint8_t)0x00)
  224. /**
  225. * @}
  226. */
  227. /** @defgroup CRYP_DMA_transfer_requests
  228. * @{
  229. */
  230. #define CRYP_DMAReq_DataIN ((uint8_t)0x01)
  231. #define CRYP_DMAReq_DataOUT ((uint8_t)0x02)
  232. #define IS_CRYP_DMAREQ(DMAREQ) ((((DMAREQ) & (uint8_t)0xFC) == 0x00) && ((DMAREQ) != 0x00))
  233. /**
  234. * @}
  235. */
  236. /**
  237. * @}
  238. */
  239. /* Exported macro ------------------------------------------------------------*/
  240. /* Exported functions --------------------------------------------------------*/
  241. /* Function used to set the CRYP configuration to the default reset state ****/
  242. void CRYP_DeInit(void);
  243. /* CRYP Initialization and Configuration functions ****************************/
  244. void CRYP_Init(CRYP_InitTypeDef* CRYP_InitStruct);
  245. void CRYP_StructInit(CRYP_InitTypeDef* CRYP_InitStruct);
  246. void CRYP_KeyInit(CRYP_KeyInitTypeDef* CRYP_KeyInitStruct);
  247. void CRYP_KeyStructInit(CRYP_KeyInitTypeDef* CRYP_KeyInitStruct);
  248. void CRYP_IVInit(CRYP_IVInitTypeDef* CRYP_IVInitStruct);
  249. void CRYP_IVStructInit(CRYP_IVInitTypeDef* CRYP_IVInitStruct);
  250. void CRYP_Cmd(FunctionalState NewState);
  251. void CRYP_PhaseConfig(uint32_t CRYP_Phase);
  252. void CRYP_FIFOFlush(void);
  253. /* CRYP Data processing functions *********************************************/
  254. void CRYP_DataIn(uint32_t Data);
  255. uint32_t CRYP_DataOut(void);
  256. /* CRYP Context swapping functions ********************************************/
  257. ErrorStatus CRYP_SaveContext(CRYP_Context* CRYP_ContextSave,
  258. CRYP_KeyInitTypeDef* CRYP_KeyInitStruct);
  259. void CRYP_RestoreContext(CRYP_Context* CRYP_ContextRestore);
  260. /* CRYP DMA interface function ************************************************/
  261. void CRYP_DMACmd(uint8_t CRYP_DMAReq, FunctionalState NewState);
  262. /* Interrupts and flags management functions **********************************/
  263. void CRYP_ITConfig(uint8_t CRYP_IT, FunctionalState NewState);
  264. ITStatus CRYP_GetITStatus(uint8_t CRYP_IT);
  265. FunctionalState CRYP_GetCmdStatus(void);
  266. FlagStatus CRYP_GetFlagStatus(uint8_t CRYP_FLAG);
  267. /* High Level AES functions **************************************************/
  268. ErrorStatus CRYP_AES_ECB(uint8_t Mode,
  269. uint8_t *Key, uint16_t Keysize,
  270. uint8_t *Input, uint32_t Ilength,
  271. uint8_t *Output);
  272. ErrorStatus CRYP_AES_CBC(uint8_t Mode,
  273. uint8_t InitVectors[16],
  274. uint8_t *Key, uint16_t Keysize,
  275. uint8_t *Input, uint32_t Ilength,
  276. uint8_t *Output);
  277. ErrorStatus CRYP_AES_CTR(uint8_t Mode,
  278. uint8_t InitVectors[16],
  279. uint8_t *Key, uint16_t Keysize,
  280. uint8_t *Input, uint32_t Ilength,
  281. uint8_t *Output);
  282. ErrorStatus CRYP_AES_GCM(uint8_t Mode, uint8_t InitVectors[16],
  283. uint8_t *Key, uint16_t Keysize,
  284. uint8_t *Input, uint32_t ILength,
  285. uint8_t *Header, uint32_t HLength,
  286. uint8_t *Output, uint8_t *AuthTAG);
  287. ErrorStatus CRYP_AES_CCM(uint8_t Mode,
  288. uint8_t* Nonce, uint32_t NonceSize,
  289. uint8_t* Key, uint16_t Keysize,
  290. uint8_t* Input, uint32_t ILength,
  291. uint8_t* Header, uint32_t HLength, uint8_t *HBuffer,
  292. uint8_t* Output,
  293. uint8_t* AuthTAG, uint32_t TAGSize);
  294. /* High Level TDES functions **************************************************/
  295. ErrorStatus CRYP_TDES_ECB(uint8_t Mode,
  296. uint8_t Key[24],
  297. uint8_t *Input, uint32_t Ilength,
  298. uint8_t *Output);
  299. ErrorStatus CRYP_TDES_CBC(uint8_t Mode,
  300. uint8_t Key[24],
  301. uint8_t InitVectors[8],
  302. uint8_t *Input, uint32_t Ilength,
  303. uint8_t *Output);
  304. /* High Level DES functions **************************************************/
  305. ErrorStatus CRYP_DES_ECB(uint8_t Mode,
  306. uint8_t Key[8],
  307. uint8_t *Input, uint32_t Ilength,
  308. uint8_t *Output);
  309. ErrorStatus CRYP_DES_CBC(uint8_t Mode,
  310. uint8_t Key[8],
  311. uint8_t InitVectors[8],
  312. uint8_t *Input,uint32_t Ilength,
  313. uint8_t *Output);
  314. #ifdef __cplusplus
  315. }
  316. #endif
  317. #endif /*__STM32F4xx_CRYP_H */
  318. /**
  319. * @}
  320. */
  321. /**
  322. * @}
  323. */