stm32f4xx_cryp_tdes.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_cryp_tdes.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 TDES 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 TDES in ECB Mode using CRYP_TDES_ECB() function.
  20. (#) Encrypt and decrypt using TDES in CBC Mode using CRYP_TDES_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 TDESBUSY_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_Group7 High Level TDES functions
  55. * @brief High Level TDES functions
  56. *
  57. @verbatim
  58. ===============================================================================
  59. ##### High Level TDES functions #####
  60. ===============================================================================
  61. @endverbatim
  62. * @{
  63. */
  64. /**
  65. * @brief Encrypt and decrypt using TDES 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 TDES 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_TDES_ECB(uint8_t Mode, uint8_t Key[24], uint8_t *Input,
  79. uint32_t Ilength, uint8_t *Output)
  80. {
  81. CRYP_InitTypeDef TDES_CRYP_InitStructure;
  82. CRYP_KeyInitTypeDef TDES_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(&TDES_CRYP_KeyInitStructure);
  92. /* Crypto Init for Encryption process */
  93. if(Mode == MODE_ENCRYPT) /* TDES encryption */
  94. {
  95. TDES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
  96. }
  97. else /*if(Mode == MODE_DECRYPT)*/ /* TDES decryption */
  98. {
  99. TDES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
  100. }
  101. TDES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_TDES_ECB;
  102. TDES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
  103. CRYP_Init(&TDES_CRYP_InitStructure);
  104. /* Key Initialisation */
  105. TDES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
  106. keyaddr+=4;
  107. TDES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
  108. keyaddr+=4;
  109. TDES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
  110. keyaddr+=4;
  111. TDES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
  112. keyaddr+=4;
  113. TDES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
  114. keyaddr+=4;
  115. TDES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
  116. CRYP_KeyInit(& TDES_CRYP_KeyInitStructure);
  117. /* Flush IN/OUT FIFO */
  118. CRYP_FIFOFlush();
  119. /* Enable Crypto processor */
  120. CRYP_Cmd(ENABLE);
  121. if(CRYP_GetCmdStatus() == DISABLE)
  122. {
  123. /* The CRYP peripheral clock is not enabled or the device doesn't embed
  124. the CRYP peripheral (please check the device sales type. */
  125. status = ERROR;
  126. }
  127. else
  128. {
  129. for(i=0; ((i<Ilength) && (status != ERROR)); i+=8)
  130. {
  131. /* Write the Input block in the Input FIFO */
  132. CRYP_DataIn(*(uint32_t*)(inputaddr));
  133. inputaddr+=4;
  134. CRYP_DataIn(*(uint32_t*)(inputaddr));
  135. inputaddr+=4;
  136. /* Wait until the complete message has been processed */
  137. counter = 0;
  138. do
  139. {
  140. busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
  141. counter++;
  142. }while ((counter != TDESBUSY_TIMEOUT) && (busystatus != RESET));
  143. if (busystatus != RESET)
  144. {
  145. status = ERROR;
  146. }
  147. else
  148. {
  149. /* Read the Output block from the Output FIFO */
  150. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  151. outputaddr+=4;
  152. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  153. outputaddr+=4;
  154. }
  155. }
  156. /* Disable Crypto */
  157. CRYP_Cmd(DISABLE);
  158. }
  159. return status;
  160. }
  161. /**
  162. * @brief Encrypt and decrypt using TDES in CBC Mode
  163. * @param Mode: encryption or decryption Mode.
  164. * This parameter can be one of the following values:
  165. * @arg MODE_ENCRYPT: Encryption
  166. * @arg MODE_DECRYPT: Decryption
  167. * @param Key: Key used for TDES algorithm.
  168. * @param InitVectors: Initialisation Vectors used for TDES algorithm.
  169. * @param Input: pointer to the Input buffer.
  170. * @param Ilength: length of the Input buffer, must be a multiple of 8.
  171. * @param Output: pointer to the returned buffer.
  172. * @retval An ErrorStatus enumeration value:
  173. * - SUCCESS: Operation done
  174. * - ERROR: Operation failed
  175. */
  176. ErrorStatus CRYP_TDES_CBC(uint8_t Mode, uint8_t Key[24], uint8_t InitVectors[8],
  177. uint8_t *Input, uint32_t Ilength, uint8_t *Output)
  178. {
  179. CRYP_InitTypeDef TDES_CRYP_InitStructure;
  180. CRYP_KeyInitTypeDef TDES_CRYP_KeyInitStructure;
  181. CRYP_IVInitTypeDef TDES_CRYP_IVInitStructure;
  182. __IO uint32_t counter = 0;
  183. uint32_t busystatus = 0;
  184. ErrorStatus status = SUCCESS;
  185. uint32_t keyaddr = (uint32_t)Key;
  186. uint32_t inputaddr = (uint32_t)Input;
  187. uint32_t outputaddr = (uint32_t)Output;
  188. uint32_t ivaddr = (uint32_t)InitVectors;
  189. uint32_t i = 0;
  190. /* Crypto structures initialisation*/
  191. CRYP_KeyStructInit(&TDES_CRYP_KeyInitStructure);
  192. /* Crypto Init for Encryption process */
  193. if(Mode == MODE_ENCRYPT) /* TDES encryption */
  194. {
  195. TDES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
  196. }
  197. else
  198. {
  199. TDES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
  200. }
  201. TDES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_TDES_CBC;
  202. TDES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
  203. CRYP_Init(&TDES_CRYP_InitStructure);
  204. /* Key Initialisation */
  205. TDES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
  206. keyaddr+=4;
  207. TDES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
  208. keyaddr+=4;
  209. TDES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
  210. keyaddr+=4;
  211. TDES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
  212. keyaddr+=4;
  213. TDES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
  214. keyaddr+=4;
  215. TDES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
  216. CRYP_KeyInit(& TDES_CRYP_KeyInitStructure);
  217. /* Initialization Vectors */
  218. TDES_CRYP_IVInitStructure.CRYP_IV0Left = __REV(*(uint32_t*)(ivaddr));
  219. ivaddr+=4;
  220. TDES_CRYP_IVInitStructure.CRYP_IV0Right= __REV(*(uint32_t*)(ivaddr));
  221. CRYP_IVInit(&TDES_CRYP_IVInitStructure);
  222. /* Flush IN/OUT FIFO */
  223. CRYP_FIFOFlush();
  224. /* Enable Crypto processor */
  225. CRYP_Cmd(ENABLE);
  226. if(CRYP_GetCmdStatus() == DISABLE)
  227. {
  228. /* The CRYP peripheral clock is not enabled or the device doesn't embed
  229. the CRYP peripheral (please check the device sales type. */
  230. status = ERROR;
  231. }
  232. else
  233. {
  234. for(i=0; ((i<Ilength) && (status != ERROR)); i+=8)
  235. {
  236. /* Write the Input block in the Input FIFO */
  237. CRYP_DataIn(*(uint32_t*)(inputaddr));
  238. inputaddr+=4;
  239. CRYP_DataIn(*(uint32_t*)(inputaddr));
  240. inputaddr+=4;
  241. /* Wait until the complete message has been processed */
  242. counter = 0;
  243. do
  244. {
  245. busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
  246. counter++;
  247. }while ((counter != TDESBUSY_TIMEOUT) && (busystatus != RESET));
  248. if (busystatus != RESET)
  249. {
  250. status = ERROR;
  251. }
  252. else
  253. {
  254. /* Read the Output block from the Output FIFO */
  255. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  256. outputaddr+=4;
  257. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  258. outputaddr+=4;
  259. }
  260. }
  261. /* Disable Crypto */
  262. CRYP_Cmd(DISABLE);
  263. }
  264. return status;
  265. }
  266. /**
  267. * @}
  268. */
  269. /**
  270. * @}
  271. */
  272. /**
  273. * @}
  274. */
  275. /**
  276. * @}
  277. */