stm32f4xx_cryp_des.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_cryp_des.c
  4. * @author MCD Application Team
  5. * @version V1.8.1
  6. * @date 27-January-2022
  7. * @brief This file provides high level functions to encrypt and decrypt an
  8. * input message using DES in ECB/CBC modes.
  9. * It uses the stm32f4xx_cryp.c/.h drivers to access the STM32F4xx CRYP
  10. * peripheral.
  11. *
  12. @verbatim
  13. ===================================================================
  14. ##### How to use this driver #####
  15. ===================================================================
  16. [..]
  17. (#) Enable The CRYP controller clock using
  18. RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_CRYP, ENABLE); function.
  19. (#) Encrypt and decrypt using DES in ECB Mode using CRYP_DES_ECB() function.
  20. (#) Encrypt and decrypt using DES in CBC Mode using CRYP_DES_CBC() function.
  21. @endverbatim
  22. *
  23. ******************************************************************************
  24. * @attention
  25. *
  26. * Copyright (c) 2016 STMicroelectronics.
  27. * All rights reserved.
  28. *
  29. * This software is licensed under terms that can be found in the LICENSE file
  30. * in the root directory of this software component.
  31. * If no LICENSE file comes with this software, it is provided AS-IS.
  32. *
  33. ******************************************************************************
  34. */
  35. /* Includes ------------------------------------------------------------------*/
  36. #include "stm32f4xx_cryp.h"
  37. /** @addtogroup STM32F4xx_StdPeriph_Driver
  38. * @{
  39. */
  40. /** @defgroup CRYP
  41. * @brief CRYP driver modules
  42. * @{
  43. */
  44. /* Private typedef -----------------------------------------------------------*/
  45. /* Private define ------------------------------------------------------------*/
  46. #define DESBUSY_TIMEOUT ((uint32_t) 0x00010000)
  47. /* Private macro -------------------------------------------------------------*/
  48. /* Private variables ---------------------------------------------------------*/
  49. /* Private function prototypes -----------------------------------------------*/
  50. /* Private functions ---------------------------------------------------------*/
  51. /** @defgroup CRYP_Private_Functions
  52. * @{
  53. */
  54. /** @defgroup CRYP_Group8 High Level DES functions
  55. * @brief High Level DES functions
  56. *
  57. @verbatim
  58. ===============================================================================
  59. ##### High Level DES functions #####
  60. ===============================================================================
  61. @endverbatim
  62. * @{
  63. */
  64. /**
  65. * @brief Encrypt and decrypt using DES in ECB Mode
  66. * @param Mode: encryption or decryption Mode.
  67. * This parameter can be one of the following values:
  68. * @arg MODE_ENCRYPT: Encryption
  69. * @arg MODE_DECRYPT: Decryption
  70. * @param Key: Key used for DES algorithm.
  71. * @param Ilength: length of the Input buffer, must be a multiple of 8.
  72. * @param Input: pointer to the Input buffer.
  73. * @param Output: pointer to the returned buffer.
  74. * @retval An ErrorStatus enumeration value:
  75. * - SUCCESS: Operation done
  76. * - ERROR: Operation failed
  77. */
  78. ErrorStatus CRYP_DES_ECB(uint8_t Mode, uint8_t Key[8], uint8_t *Input,
  79. uint32_t Ilength, uint8_t *Output)
  80. {
  81. CRYP_InitTypeDef DES_CRYP_InitStructure;
  82. CRYP_KeyInitTypeDef DES_CRYP_KeyInitStructure;
  83. __IO uint32_t counter = 0;
  84. uint32_t busystatus = 0;
  85. ErrorStatus status = SUCCESS;
  86. uint32_t keyaddr = (uint32_t)Key;
  87. uint32_t inputaddr = (uint32_t)Input;
  88. uint32_t outputaddr = (uint32_t)Output;
  89. uint32_t i = 0;
  90. /* Crypto structures initialisation*/
  91. CRYP_KeyStructInit(&DES_CRYP_KeyInitStructure);
  92. /* Crypto Init for Encryption process */
  93. if( Mode == MODE_ENCRYPT ) /* DES encryption */
  94. {
  95. DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
  96. }
  97. else/* if( Mode == MODE_DECRYPT )*/ /* DES decryption */
  98. {
  99. DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
  100. }
  101. DES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_DES_ECB;
  102. DES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
  103. CRYP_Init(&DES_CRYP_InitStructure);
  104. /* Key Initialisation */
  105. DES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
  106. keyaddr+=4;
  107. DES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
  108. CRYP_KeyInit(& DES_CRYP_KeyInitStructure);
  109. /* Flush IN/OUT FIFO */
  110. CRYP_FIFOFlush();
  111. /* Enable Crypto processor */
  112. CRYP_Cmd(ENABLE);
  113. if(CRYP_GetCmdStatus() == DISABLE)
  114. {
  115. /* The CRYP peripheral clock is not enabled or the device doesn't embed
  116. the CRYP peripheral (please check the device sales type. */
  117. status = ERROR;
  118. }
  119. else
  120. {
  121. for(i=0; ((i<Ilength) && (status != ERROR)); i+=8)
  122. {
  123. /* Write the Input block in the Input FIFO */
  124. CRYP_DataIn(*(uint32_t*)(inputaddr));
  125. inputaddr+=4;
  126. CRYP_DataIn(*(uint32_t*)(inputaddr));
  127. inputaddr+=4;
  128. /* Wait until the complete message has been processed */
  129. counter = 0;
  130. do
  131. {
  132. busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
  133. counter++;
  134. }while ((counter != DESBUSY_TIMEOUT) && (busystatus != RESET));
  135. if (busystatus != RESET)
  136. {
  137. status = ERROR;
  138. }
  139. else
  140. {
  141. /* Read the Output block from the Output FIFO */
  142. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  143. outputaddr+=4;
  144. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  145. outputaddr+=4;
  146. }
  147. }
  148. /* Disable Crypto */
  149. CRYP_Cmd(DISABLE);
  150. }
  151. return status;
  152. }
  153. /**
  154. * @brief Encrypt and decrypt using DES in CBC Mode
  155. * @param Mode: encryption or decryption Mode.
  156. * This parameter can be one of the following values:
  157. * @arg MODE_ENCRYPT: Encryption
  158. * @arg MODE_DECRYPT: Decryption
  159. * @param Key: Key used for DES algorithm.
  160. * @param InitVectors: Initialisation Vectors used for DES algorithm.
  161. * @param Ilength: length of the Input buffer, must be a multiple of 8.
  162. * @param Input: pointer to the Input buffer.
  163. * @param Output: pointer to the returned buffer.
  164. * @retval An ErrorStatus enumeration value:
  165. * - SUCCESS: Operation done
  166. * - ERROR: Operation failed
  167. */
  168. ErrorStatus CRYP_DES_CBC(uint8_t Mode, uint8_t Key[8], uint8_t InitVectors[8],
  169. uint8_t *Input, uint32_t Ilength, uint8_t *Output)
  170. {
  171. CRYP_InitTypeDef DES_CRYP_InitStructure;
  172. CRYP_KeyInitTypeDef DES_CRYP_KeyInitStructure;
  173. CRYP_IVInitTypeDef DES_CRYP_IVInitStructure;
  174. __IO uint32_t counter = 0;
  175. uint32_t busystatus = 0;
  176. ErrorStatus status = SUCCESS;
  177. uint32_t keyaddr = (uint32_t)Key;
  178. uint32_t inputaddr = (uint32_t)Input;
  179. uint32_t outputaddr = (uint32_t)Output;
  180. uint32_t ivaddr = (uint32_t)InitVectors;
  181. uint32_t i = 0;
  182. /* Crypto structures initialisation*/
  183. CRYP_KeyStructInit(&DES_CRYP_KeyInitStructure);
  184. /* Crypto Init for Encryption process */
  185. if(Mode == MODE_ENCRYPT) /* DES encryption */
  186. {
  187. DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
  188. }
  189. else /*if(Mode == MODE_DECRYPT)*/ /* DES decryption */
  190. {
  191. DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
  192. }
  193. DES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_DES_CBC;
  194. DES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
  195. CRYP_Init(&DES_CRYP_InitStructure);
  196. /* Key Initialisation */
  197. DES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
  198. keyaddr+=4;
  199. DES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
  200. CRYP_KeyInit(& DES_CRYP_KeyInitStructure);
  201. /* Initialization Vectors */
  202. DES_CRYP_IVInitStructure.CRYP_IV0Left = __REV(*(uint32_t*)(ivaddr));
  203. ivaddr+=4;
  204. DES_CRYP_IVInitStructure.CRYP_IV0Right= __REV(*(uint32_t*)(ivaddr));
  205. CRYP_IVInit(&DES_CRYP_IVInitStructure);
  206. /* Flush IN/OUT FIFO */
  207. CRYP_FIFOFlush();
  208. /* Enable Crypto processor */
  209. CRYP_Cmd(ENABLE);
  210. if(CRYP_GetCmdStatus() == DISABLE)
  211. {
  212. /* The CRYP peripheral clock is not enabled or the device doesn't embed
  213. the CRYP peripheral (please check the device sales type. */
  214. status = ERROR;
  215. }
  216. else
  217. {
  218. for(i=0; ((i<Ilength) && (status != ERROR)); i+=8)
  219. {
  220. /* Write the Input block in the Input FIFO */
  221. CRYP_DataIn(*(uint32_t*)(inputaddr));
  222. inputaddr+=4;
  223. CRYP_DataIn(*(uint32_t*)(inputaddr));
  224. inputaddr+=4;
  225. /* Wait until the complete message has been processed */
  226. counter = 0;
  227. do
  228. {
  229. busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
  230. counter++;
  231. }while ((counter != DESBUSY_TIMEOUT) && (busystatus != RESET));
  232. if (busystatus != RESET)
  233. {
  234. status = ERROR;
  235. }
  236. else
  237. {
  238. /* Read the Output block from the Output FIFO */
  239. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  240. outputaddr+=4;
  241. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  242. outputaddr+=4;
  243. }
  244. }
  245. /* Disable Crypto */
  246. CRYP_Cmd(DISABLE);
  247. }
  248. return status;
  249. }
  250. /**
  251. * @}
  252. */
  253. /**
  254. * @}
  255. */
  256. /**
  257. * @}
  258. */
  259. /**
  260. * @}
  261. */