stm32f4xx_cryp.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_cryp.h
  4. * @author MCD Application Team
  5. * @version V1.0.2
  6. * @date 05-March-2012
  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 2012 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. uint16_t CRYP_AlgoDir; /*!< Encrypt or Decrypt. This parameter can be a
  49. value of @ref CRYP_Algorithm_Direction */
  50. uint16_t CRYP_AlgoMode; /*!< TDES-ECB, TDES-CBC, DES-ECB, DES-CBC, AES-ECB,
  51. AES-CBC, AES-CTR, AES-Key. This parameter can be
  52. a value of @ref CRYP_Algorithm_Mode */
  53. uint16_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. uint16_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. /*!< Configuration */
  89. uint32_t CR_bits9to2;
  90. /*!< KEY */
  91. uint32_t CRYP_IV0LR;
  92. uint32_t CRYP_IV0RR;
  93. uint32_t CRYP_IV1LR;
  94. uint32_t CRYP_IV1RR;
  95. /*!< IV */
  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. }CRYP_Context;
  105. /* Exported constants --------------------------------------------------------*/
  106. /** @defgroup CRYP_Exported_Constants
  107. * @{
  108. */
  109. /** @defgroup CRYP_Algorithm_Direction
  110. * @{
  111. */
  112. #define CRYP_AlgoDir_Encrypt ((uint16_t)0x0000)
  113. #define CRYP_AlgoDir_Decrypt ((uint16_t)0x0004)
  114. #define IS_CRYP_ALGODIR(ALGODIR) (((ALGODIR) == CRYP_AlgoDir_Encrypt) || \
  115. ((ALGODIR) == CRYP_AlgoDir_Decrypt))
  116. /**
  117. * @}
  118. */
  119. /** @defgroup CRYP_Algorithm_Mode
  120. * @{
  121. */
  122. /*!< TDES Modes */
  123. #define CRYP_AlgoMode_TDES_ECB ((uint16_t)0x0000)
  124. #define CRYP_AlgoMode_TDES_CBC ((uint16_t)0x0008)
  125. /*!< DES Modes */
  126. #define CRYP_AlgoMode_DES_ECB ((uint16_t)0x0010)
  127. #define CRYP_AlgoMode_DES_CBC ((uint16_t)0x0018)
  128. /*!< AES Modes */
  129. #define CRYP_AlgoMode_AES_ECB ((uint16_t)0x0020)
  130. #define CRYP_AlgoMode_AES_CBC ((uint16_t)0x0028)
  131. #define CRYP_AlgoMode_AES_CTR ((uint16_t)0x0030)
  132. #define CRYP_AlgoMode_AES_Key ((uint16_t)0x0038)
  133. #define IS_CRYP_ALGOMODE(ALGOMODE) (((ALGOMODE) == CRYP_AlgoMode_TDES_ECB) || \
  134. ((ALGOMODE) == CRYP_AlgoMode_TDES_CBC)|| \
  135. ((ALGOMODE) == CRYP_AlgoMode_DES_ECB)|| \
  136. ((ALGOMODE) == CRYP_AlgoMode_DES_CBC) || \
  137. ((ALGOMODE) == CRYP_AlgoMode_AES_ECB) || \
  138. ((ALGOMODE) == CRYP_AlgoMode_AES_CBC) || \
  139. ((ALGOMODE) == CRYP_AlgoMode_AES_CTR) || \
  140. ((ALGOMODE) == CRYP_AlgoMode_AES_Key))
  141. /**
  142. * @}
  143. */
  144. /** @defgroup CRYP_Data_Type
  145. * @{
  146. */
  147. #define CRYP_DataType_32b ((uint16_t)0x0000)
  148. #define CRYP_DataType_16b ((uint16_t)0x0040)
  149. #define CRYP_DataType_8b ((uint16_t)0x0080)
  150. #define CRYP_DataType_1b ((uint16_t)0x00C0)
  151. #define IS_CRYP_DATATYPE(DATATYPE) (((DATATYPE) == CRYP_DataType_32b) || \
  152. ((DATATYPE) == CRYP_DataType_16b)|| \
  153. ((DATATYPE) == CRYP_DataType_8b)|| \
  154. ((DATATYPE) == CRYP_DataType_1b))
  155. /**
  156. * @}
  157. */
  158. /** @defgroup CRYP_Key_Size_for_AES_only
  159. * @{
  160. */
  161. #define CRYP_KeySize_128b ((uint16_t)0x0000)
  162. #define CRYP_KeySize_192b ((uint16_t)0x0100)
  163. #define CRYP_KeySize_256b ((uint16_t)0x0200)
  164. #define IS_CRYP_KEYSIZE(KEYSIZE) (((KEYSIZE) == CRYP_KeySize_128b)|| \
  165. ((KEYSIZE) == CRYP_KeySize_192b)|| \
  166. ((KEYSIZE) == CRYP_KeySize_256b))
  167. /**
  168. * @}
  169. */
  170. /** @defgroup CRYP_flags_definition
  171. * @{
  172. */
  173. #define CRYP_FLAG_BUSY ((uint8_t)0x10) /*!< The CRYP core is currently
  174. processing a block of data
  175. or a key preparation (for
  176. AES decryption). */
  177. #define CRYP_FLAG_IFEM ((uint8_t)0x01) /*!< Input Fifo Empty */
  178. #define CRYP_FLAG_IFNF ((uint8_t)0x02) /*!< Input Fifo is Not Full */
  179. #define CRYP_FLAG_INRIS ((uint8_t)0x22) /*!< Raw interrupt pending */
  180. #define CRYP_FLAG_OFNE ((uint8_t)0x04) /*!< Input Fifo service raw
  181. interrupt status */
  182. #define CRYP_FLAG_OFFU ((uint8_t)0x08) /*!< Output Fifo is Full */
  183. #define CRYP_FLAG_OUTRIS ((uint8_t)0x21) /*!< Output Fifo service raw
  184. interrupt status */
  185. #define IS_CRYP_GET_FLAG(FLAG) (((FLAG) == CRYP_FLAG_IFEM) || \
  186. ((FLAG) == CRYP_FLAG_IFNF) || \
  187. ((FLAG) == CRYP_FLAG_OFNE) || \
  188. ((FLAG) == CRYP_FLAG_OFFU) || \
  189. ((FLAG) == CRYP_FLAG_BUSY) || \
  190. ((FLAG) == CRYP_FLAG_OUTRIS)|| \
  191. ((FLAG) == CRYP_FLAG_INRIS))
  192. /**
  193. * @}
  194. */
  195. /** @defgroup CRYP_interrupts_definition
  196. * @{
  197. */
  198. #define CRYP_IT_INI ((uint8_t)0x01) /*!< IN Fifo Interrupt */
  199. #define CRYP_IT_OUTI ((uint8_t)0x02) /*!< OUT Fifo Interrupt */
  200. #define IS_CRYP_CONFIG_IT(IT) ((((IT) & (uint8_t)0xFC) == 0x00) && ((IT) != 0x00))
  201. #define IS_CRYP_GET_IT(IT) (((IT) == CRYP_IT_INI) || ((IT) == CRYP_IT_OUTI))
  202. /**
  203. * @}
  204. */
  205. /** @defgroup CRYP_Encryption_Decryption_modes_definition
  206. * @{
  207. */
  208. #define MODE_ENCRYPT ((uint8_t)0x01)
  209. #define MODE_DECRYPT ((uint8_t)0x00)
  210. /**
  211. * @}
  212. */
  213. /** @defgroup CRYP_DMA_transfer_requests
  214. * @{
  215. */
  216. #define CRYP_DMAReq_DataIN ((uint8_t)0x01)
  217. #define CRYP_DMAReq_DataOUT ((uint8_t)0x02)
  218. #define IS_CRYP_DMAREQ(DMAREQ) ((((DMAREQ) & (uint8_t)0xFC) == 0x00) && ((DMAREQ) != 0x00))
  219. /**
  220. * @}
  221. */
  222. /**
  223. * @}
  224. */
  225. /* Exported macro ------------------------------------------------------------*/
  226. /* Exported functions --------------------------------------------------------*/
  227. /* Function used to set the CRYP configuration to the default reset state ****/
  228. void CRYP_DeInit(void);
  229. /* CRYP Initialization and Configuration functions ****************************/
  230. void CRYP_Init(CRYP_InitTypeDef* CRYP_InitStruct);
  231. void CRYP_StructInit(CRYP_InitTypeDef* CRYP_InitStruct);
  232. void CRYP_KeyInit(CRYP_KeyInitTypeDef* CRYP_KeyInitStruct);
  233. void CRYP_KeyStructInit(CRYP_KeyInitTypeDef* CRYP_KeyInitStruct);
  234. void CRYP_IVInit(CRYP_IVInitTypeDef* CRYP_IVInitStruct);
  235. void CRYP_IVStructInit(CRYP_IVInitTypeDef* CRYP_IVInitStruct);
  236. void CRYP_Cmd(FunctionalState NewState);
  237. /* CRYP Data processing functions *********************************************/
  238. void CRYP_DataIn(uint32_t Data);
  239. uint32_t CRYP_DataOut(void);
  240. void CRYP_FIFOFlush(void);
  241. /* CRYP Context swapping functions ********************************************/
  242. ErrorStatus CRYP_SaveContext(CRYP_Context* CRYP_ContextSave,
  243. CRYP_KeyInitTypeDef* CRYP_KeyInitStruct);
  244. void CRYP_RestoreContext(CRYP_Context* CRYP_ContextRestore);
  245. /* CRYP's DMA interface function **********************************************/
  246. void CRYP_DMACmd(uint8_t CRYP_DMAReq, FunctionalState NewState);
  247. /* Interrupts and flags management functions **********************************/
  248. void CRYP_ITConfig(uint8_t CRYP_IT, FunctionalState NewState);
  249. ITStatus CRYP_GetITStatus(uint8_t CRYP_IT);
  250. FlagStatus CRYP_GetFlagStatus(uint8_t CRYP_FLAG);
  251. /* High Level AES functions **************************************************/
  252. ErrorStatus CRYP_AES_ECB(uint8_t Mode,
  253. uint8_t *Key, uint16_t Keysize,
  254. uint8_t *Input, uint32_t Ilength,
  255. uint8_t *Output);
  256. ErrorStatus CRYP_AES_CBC(uint8_t Mode,
  257. uint8_t InitVectors[16],
  258. uint8_t *Key, uint16_t Keysize,
  259. uint8_t *Input, uint32_t Ilength,
  260. uint8_t *Output);
  261. ErrorStatus CRYP_AES_CTR(uint8_t Mode,
  262. uint8_t InitVectors[16],
  263. uint8_t *Key, uint16_t Keysize,
  264. uint8_t *Input, uint32_t Ilength,
  265. uint8_t *Output);
  266. /* High Level TDES functions **************************************************/
  267. ErrorStatus CRYP_TDES_ECB(uint8_t Mode,
  268. uint8_t Key[24],
  269. uint8_t *Input, uint32_t Ilength,
  270. uint8_t *Output);
  271. ErrorStatus CRYP_TDES_CBC(uint8_t Mode,
  272. uint8_t Key[24],
  273. uint8_t InitVectors[8],
  274. uint8_t *Input, uint32_t Ilength,
  275. uint8_t *Output);
  276. /* High Level DES functions **************************************************/
  277. ErrorStatus CRYP_DES_ECB(uint8_t Mode,
  278. uint8_t Key[8],
  279. uint8_t *Input, uint32_t Ilength,
  280. uint8_t *Output);
  281. ErrorStatus CRYP_DES_CBC(uint8_t Mode,
  282. uint8_t Key[8],
  283. uint8_t InitVectors[8],
  284. uint8_t *Input,uint32_t Ilength,
  285. uint8_t *Output);
  286. #ifdef __cplusplus
  287. }
  288. #endif
  289. #endif /*__STM32F4xx_CRYP_H */
  290. /**
  291. * @}
  292. */
  293. /**
  294. * @}
  295. */
  296. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/