stm32f4xx_cryp.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_cryp.h
  4. * @author MCD Application Team
  5. * @version V1.5.0
  6. * @date 06-March-2015
  7. * @brief This file contains all the functions prototypes for the Cryptographic
  8. * processor(CRYP) firmware library.
  9. ******************************************************************************
  10. * @attention
  11. *
  12. * <h2><center>&copy; COPYRIGHT 2015 STMicroelectronics</center></h2>
  13. *
  14. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  15. * You may not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at:
  17. *
  18. * http://www.st.com/software_license_agreement_liberty_v2
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS,
  22. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. *
  26. ******************************************************************************
  27. */
  28. /* Define to prevent recursive inclusion -------------------------------------*/
  29. #ifndef __STM32F4xx_CRYP_H
  30. #define __STM32F4xx_CRYP_H
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. /* Includes ------------------------------------------------------------------*/
  35. #include "stm32f4xx.h"
  36. /** @addtogroup STM32F4xx_StdPeriph_Driver
  37. * @{
  38. */
  39. /** @addtogroup CRYP
  40. * @{
  41. */
  42. /* Exported types ------------------------------------------------------------*/
  43. /**
  44. * @brief CRYP Init structure definition
  45. */
  46. typedef struct
  47. {
  48. uint32_t CRYP_AlgoDir; /*!< Encrypt or Decrypt. This parameter can be a
  49. value of @ref CRYP_Algorithm_Direction */
  50. uint32_t CRYP_AlgoMode; /*!< TDES-ECB, TDES-CBC, DES-ECB, DES-CBC, AES-ECB,
  51. AES-CBC, AES-CTR, AES-Key, AES-GCM and AES-CCM.
  52. This parameter can be a value of @ref CRYP_Algorithm_Mode */
  53. uint32_t CRYP_DataType; /*!< 32-bit data, 16-bit data, bit data or bit string.
  54. This parameter can be a value of @ref CRYP_Data_Type */
  55. uint32_t CRYP_KeySize; /*!< Used only in AES mode only : 128, 192 or 256 bit
  56. key length. This parameter can be a value of
  57. @ref CRYP_Key_Size_for_AES_only */
  58. }CRYP_InitTypeDef;
  59. /**
  60. * @brief CRYP Key(s) structure definition
  61. */
  62. typedef struct
  63. {
  64. uint32_t CRYP_Key0Left; /*!< Key 0 Left */
  65. uint32_t CRYP_Key0Right; /*!< Key 0 Right */
  66. uint32_t CRYP_Key1Left; /*!< Key 1 left */
  67. uint32_t CRYP_Key1Right; /*!< Key 1 Right */
  68. uint32_t CRYP_Key2Left; /*!< Key 2 left */
  69. uint32_t CRYP_Key2Right; /*!< Key 2 Right */
  70. uint32_t CRYP_Key3Left; /*!< Key 3 left */
  71. uint32_t CRYP_Key3Right; /*!< Key 3 Right */
  72. }CRYP_KeyInitTypeDef;
  73. /**
  74. * @brief CRYP Initialization Vectors (IV) structure definition
  75. */
  76. typedef struct
  77. {
  78. uint32_t CRYP_IV0Left; /*!< Init Vector 0 Left */
  79. uint32_t CRYP_IV0Right; /*!< Init Vector 0 Right */
  80. uint32_t CRYP_IV1Left; /*!< Init Vector 1 left */
  81. uint32_t CRYP_IV1Right; /*!< Init Vector 1 Right */
  82. }CRYP_IVInitTypeDef;
  83. /**
  84. * @brief CRYP context swapping structure definition
  85. */
  86. typedef struct
  87. {
  88. /*!< Current Configuration */
  89. uint32_t CR_CurrentConfig;
  90. /*!< IV */
  91. uint32_t CRYP_IV0LR;
  92. uint32_t CRYP_IV0RR;
  93. uint32_t CRYP_IV1LR;
  94. uint32_t CRYP_IV1RR;
  95. /*!< KEY */
  96. uint32_t CRYP_K0LR;
  97. uint32_t CRYP_K0RR;
  98. uint32_t CRYP_K1LR;
  99. uint32_t CRYP_K1RR;
  100. uint32_t CRYP_K2LR;
  101. uint32_t CRYP_K2RR;
  102. uint32_t CRYP_K3LR;
  103. uint32_t CRYP_K3RR;
  104. uint32_t CRYP_CSGCMCCMR[8];
  105. uint32_t CRYP_CSGCMR[8];
  106. }CRYP_Context;
  107. /* Exported constants --------------------------------------------------------*/
  108. /** @defgroup CRYP_Exported_Constants
  109. * @{
  110. */
  111. /** @defgroup CRYP_Algorithm_Direction
  112. * @{
  113. */
  114. #define CRYP_AlgoDir_Encrypt ((uint16_t)0x0000)
  115. #define CRYP_AlgoDir_Decrypt ((uint16_t)0x0004)
  116. #define IS_CRYP_ALGODIR(ALGODIR) (((ALGODIR) == CRYP_AlgoDir_Encrypt) || \
  117. ((ALGODIR) == CRYP_AlgoDir_Decrypt))
  118. /**
  119. * @}
  120. */
  121. /** @defgroup CRYP_Algorithm_Mode
  122. * @{
  123. */
  124. /*!< TDES Modes */
  125. #define CRYP_AlgoMode_TDES_ECB ((uint32_t)0x00000000)
  126. #define CRYP_AlgoMode_TDES_CBC ((uint32_t)0x00000008)
  127. /*!< DES Modes */
  128. #define CRYP_AlgoMode_DES_ECB ((uint32_t)0x00000010)
  129. #define CRYP_AlgoMode_DES_CBC ((uint32_t)0x00000018)
  130. /*!< AES Modes */
  131. #define CRYP_AlgoMode_AES_ECB ((uint32_t)0x00000020)
  132. #define CRYP_AlgoMode_AES_CBC ((uint32_t)0x00000028)
  133. #define CRYP_AlgoMode_AES_CTR ((uint32_t)0x00000030)
  134. #define CRYP_AlgoMode_AES_Key ((uint32_t)0x00000038)
  135. #define CRYP_AlgoMode_AES_GCM ((uint32_t)0x00080000)
  136. #define CRYP_AlgoMode_AES_CCM ((uint32_t)0x00080008)
  137. #define IS_CRYP_ALGOMODE(ALGOMODE) (((ALGOMODE) == CRYP_AlgoMode_TDES_ECB) || \
  138. ((ALGOMODE) == CRYP_AlgoMode_TDES_CBC)|| \
  139. ((ALGOMODE) == CRYP_AlgoMode_DES_ECB) || \
  140. ((ALGOMODE) == CRYP_AlgoMode_DES_CBC) || \
  141. ((ALGOMODE) == CRYP_AlgoMode_AES_ECB) || \
  142. ((ALGOMODE) == CRYP_AlgoMode_AES_CBC) || \
  143. ((ALGOMODE) == CRYP_AlgoMode_AES_CTR) || \
  144. ((ALGOMODE) == CRYP_AlgoMode_AES_Key) || \
  145. ((ALGOMODE) == CRYP_AlgoMode_AES_GCM) || \
  146. ((ALGOMODE) == CRYP_AlgoMode_AES_CCM))
  147. /**
  148. * @}
  149. */
  150. /** @defgroup CRYP_Phase
  151. * @{
  152. */
  153. /*!< The phases are valid only for AES-GCM and AES-CCM modes */
  154. #define CRYP_Phase_Init ((uint32_t)0x00000000)
  155. #define CRYP_Phase_Header CRYP_CR_GCM_CCMPH_0
  156. #define CRYP_Phase_Payload CRYP_CR_GCM_CCMPH_1
  157. #define CRYP_Phase_Final CRYP_CR_GCM_CCMPH
  158. #define IS_CRYP_PHASE(PHASE) (((PHASE) == CRYP_Phase_Init) || \
  159. ((PHASE) == CRYP_Phase_Header) || \
  160. ((PHASE) == CRYP_Phase_Payload) || \
  161. ((PHASE) == CRYP_Phase_Final))
  162. /**
  163. * @}
  164. */
  165. /** @defgroup CRYP_Data_Type
  166. * @{
  167. */
  168. #define CRYP_DataType_32b ((uint16_t)0x0000)
  169. #define CRYP_DataType_16b ((uint16_t)0x0040)
  170. #define CRYP_DataType_8b ((uint16_t)0x0080)
  171. #define CRYP_DataType_1b ((uint16_t)0x00C0)
  172. #define IS_CRYP_DATATYPE(DATATYPE) (((DATATYPE) == CRYP_DataType_32b) || \
  173. ((DATATYPE) == CRYP_DataType_16b)|| \
  174. ((DATATYPE) == CRYP_DataType_8b)|| \
  175. ((DATATYPE) == CRYP_DataType_1b))
  176. /**
  177. * @}
  178. */
  179. /** @defgroup CRYP_Key_Size_for_AES_only
  180. * @{
  181. */
  182. #define CRYP_KeySize_128b ((uint16_t)0x0000)
  183. #define CRYP_KeySize_192b ((uint16_t)0x0100)
  184. #define CRYP_KeySize_256b ((uint16_t)0x0200)
  185. #define IS_CRYP_KEYSIZE(KEYSIZE) (((KEYSIZE) == CRYP_KeySize_128b)|| \
  186. ((KEYSIZE) == CRYP_KeySize_192b)|| \
  187. ((KEYSIZE) == CRYP_KeySize_256b))
  188. /**
  189. * @}
  190. */
  191. /** @defgroup CRYP_flags_definition
  192. * @{
  193. */
  194. #define CRYP_FLAG_BUSY ((uint8_t)0x10) /*!< The CRYP core is currently
  195. processing a block of data
  196. or a key preparation (for
  197. AES decryption). */
  198. #define CRYP_FLAG_IFEM ((uint8_t)0x01) /*!< Input Fifo Empty */
  199. #define CRYP_FLAG_IFNF ((uint8_t)0x02) /*!< Input Fifo is Not Full */
  200. #define CRYP_FLAG_INRIS ((uint8_t)0x22) /*!< Raw interrupt pending */
  201. #define CRYP_FLAG_OFNE ((uint8_t)0x04) /*!< Input Fifo service raw
  202. interrupt status */
  203. #define CRYP_FLAG_OFFU ((uint8_t)0x08) /*!< Output Fifo is Full */
  204. #define CRYP_FLAG_OUTRIS ((uint8_t)0x21) /*!< Output Fifo service raw
  205. interrupt status */
  206. #define IS_CRYP_GET_FLAG(FLAG) (((FLAG) == CRYP_FLAG_IFEM) || \
  207. ((FLAG) == CRYP_FLAG_IFNF) || \
  208. ((FLAG) == CRYP_FLAG_OFNE) || \
  209. ((FLAG) == CRYP_FLAG_OFFU) || \
  210. ((FLAG) == CRYP_FLAG_BUSY) || \
  211. ((FLAG) == CRYP_FLAG_OUTRIS)|| \
  212. ((FLAG) == CRYP_FLAG_INRIS))
  213. /**
  214. * @}
  215. */
  216. /** @defgroup CRYP_interrupts_definition
  217. * @{
  218. */
  219. #define CRYP_IT_INI ((uint8_t)0x01) /*!< IN Fifo Interrupt */
  220. #define CRYP_IT_OUTI ((uint8_t)0x02) /*!< OUT Fifo Interrupt */
  221. #define IS_CRYP_CONFIG_IT(IT) ((((IT) & (uint8_t)0xFC) == 0x00) && ((IT) != 0x00))
  222. #define IS_CRYP_GET_IT(IT) (((IT) == CRYP_IT_INI) || ((IT) == CRYP_IT_OUTI))
  223. /**
  224. * @}
  225. */
  226. /** @defgroup CRYP_Encryption_Decryption_modes_definition
  227. * @{
  228. */
  229. #define MODE_ENCRYPT ((uint8_t)0x01)
  230. #define MODE_DECRYPT ((uint8_t)0x00)
  231. /**
  232. * @}
  233. */
  234. /** @defgroup CRYP_DMA_transfer_requests
  235. * @{
  236. */
  237. #define CRYP_DMAReq_DataIN ((uint8_t)0x01)
  238. #define CRYP_DMAReq_DataOUT ((uint8_t)0x02)
  239. #define IS_CRYP_DMAREQ(DMAREQ) ((((DMAREQ) & (uint8_t)0xFC) == 0x00) && ((DMAREQ) != 0x00))
  240. /**
  241. * @}
  242. */
  243. /**
  244. * @}
  245. */
  246. /* Exported macro ------------------------------------------------------------*/
  247. /* Exported functions --------------------------------------------------------*/
  248. /* Function used to set the CRYP configuration to the default reset state ****/
  249. void CRYP_DeInit(void);
  250. /* CRYP Initialization and Configuration functions ****************************/
  251. void CRYP_Init(CRYP_InitTypeDef* CRYP_InitStruct);
  252. void CRYP_StructInit(CRYP_InitTypeDef* CRYP_InitStruct);
  253. void CRYP_KeyInit(CRYP_KeyInitTypeDef* CRYP_KeyInitStruct);
  254. void CRYP_KeyStructInit(CRYP_KeyInitTypeDef* CRYP_KeyInitStruct);
  255. void CRYP_IVInit(CRYP_IVInitTypeDef* CRYP_IVInitStruct);
  256. void CRYP_IVStructInit(CRYP_IVInitTypeDef* CRYP_IVInitStruct);
  257. void CRYP_Cmd(FunctionalState NewState);
  258. void CRYP_PhaseConfig(uint32_t CRYP_Phase);
  259. void CRYP_FIFOFlush(void);
  260. /* CRYP Data processing functions *********************************************/
  261. void CRYP_DataIn(uint32_t Data);
  262. uint32_t CRYP_DataOut(void);
  263. /* CRYP Context swapping functions ********************************************/
  264. ErrorStatus CRYP_SaveContext(CRYP_Context* CRYP_ContextSave,
  265. CRYP_KeyInitTypeDef* CRYP_KeyInitStruct);
  266. void CRYP_RestoreContext(CRYP_Context* CRYP_ContextRestore);
  267. /* CRYP DMA interface function ************************************************/
  268. void CRYP_DMACmd(uint8_t CRYP_DMAReq, FunctionalState NewState);
  269. /* Interrupts and flags management functions **********************************/
  270. void CRYP_ITConfig(uint8_t CRYP_IT, FunctionalState NewState);
  271. ITStatus CRYP_GetITStatus(uint8_t CRYP_IT);
  272. FunctionalState CRYP_GetCmdStatus(void);
  273. FlagStatus CRYP_GetFlagStatus(uint8_t CRYP_FLAG);
  274. /* High Level AES functions **************************************************/
  275. ErrorStatus CRYP_AES_ECB(uint8_t Mode,
  276. uint8_t *Key, uint16_t Keysize,
  277. uint8_t *Input, uint32_t Ilength,
  278. uint8_t *Output);
  279. ErrorStatus CRYP_AES_CBC(uint8_t Mode,
  280. uint8_t InitVectors[16],
  281. uint8_t *Key, uint16_t Keysize,
  282. uint8_t *Input, uint32_t Ilength,
  283. uint8_t *Output);
  284. ErrorStatus CRYP_AES_CTR(uint8_t Mode,
  285. uint8_t InitVectors[16],
  286. uint8_t *Key, uint16_t Keysize,
  287. uint8_t *Input, uint32_t Ilength,
  288. uint8_t *Output);
  289. ErrorStatus CRYP_AES_GCM(uint8_t Mode, uint8_t InitVectors[16],
  290. uint8_t *Key, uint16_t Keysize,
  291. uint8_t *Input, uint32_t ILength,
  292. uint8_t *Header, uint32_t HLength,
  293. uint8_t *Output, uint8_t *AuthTAG);
  294. ErrorStatus CRYP_AES_CCM(uint8_t Mode,
  295. uint8_t* Nonce, uint32_t NonceSize,
  296. uint8_t* Key, uint16_t Keysize,
  297. uint8_t* Input, uint32_t ILength,
  298. uint8_t* Header, uint32_t HLength, uint8_t *HBuffer,
  299. uint8_t* Output,
  300. uint8_t* AuthTAG, uint32_t TAGSize);
  301. /* High Level TDES functions **************************************************/
  302. ErrorStatus CRYP_TDES_ECB(uint8_t Mode,
  303. uint8_t Key[24],
  304. uint8_t *Input, uint32_t Ilength,
  305. uint8_t *Output);
  306. ErrorStatus CRYP_TDES_CBC(uint8_t Mode,
  307. uint8_t Key[24],
  308. uint8_t InitVectors[8],
  309. uint8_t *Input, uint32_t Ilength,
  310. uint8_t *Output);
  311. /* High Level DES functions **************************************************/
  312. ErrorStatus CRYP_DES_ECB(uint8_t Mode,
  313. uint8_t Key[8],
  314. uint8_t *Input, uint32_t Ilength,
  315. uint8_t *Output);
  316. ErrorStatus CRYP_DES_CBC(uint8_t Mode,
  317. uint8_t Key[8],
  318. uint8_t InitVectors[8],
  319. uint8_t *Input,uint32_t Ilength,
  320. uint8_t *Output);
  321. #ifdef __cplusplus
  322. }
  323. #endif
  324. #endif /*__STM32F4xx_CRYP_H */
  325. /**
  326. * @}
  327. */
  328. /**
  329. * @}
  330. */
  331. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/