stm32f2xx_cryp_des.c 8.5 KB

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