stm32f4xx_cryp_tdes.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_cryp_tdes.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 30-September-2011
  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. * ===================================================================
  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 TDES in ECB Mode using CRYP_TDES_ECB()
  21. * function.
  22. *
  23. * 3. Encrypt and decrypt using TDES in CBC Mode using CRYP_TDES_CBC()
  24. * function.
  25. *
  26. * @endverbatim
  27. *
  28. ******************************************************************************
  29. * @attention
  30. *
  31. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  32. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  33. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  34. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  35. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  36. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  37. *
  38. * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  39. ******************************************************************************
  40. */
  41. /* Includes ------------------------------------------------------------------*/
  42. #include "stm32f4xx_cryp.h"
  43. /** @addtogroup STM32F4xx_StdPeriph_Driver
  44. * @{
  45. */
  46. /** @defgroup CRYP
  47. * @brief CRYP driver modules
  48. * @{
  49. */
  50. /* Private typedef -----------------------------------------------------------*/
  51. /* Private define ------------------------------------------------------------*/
  52. #define TDESBUSY_TIMEOUT ((uint32_t) 0x00010000)
  53. /* Private macro -------------------------------------------------------------*/
  54. /* Private variables ---------------------------------------------------------*/
  55. /* Private function prototypes -----------------------------------------------*/
  56. /* Private functions ---------------------------------------------------------*/
  57. /** @defgroup CRYP_Private_Functions
  58. * @{
  59. */
  60. /** @defgroup CRYP_Group7 High Level TDES functions
  61. * @brief High Level TDES functions
  62. *
  63. @verbatim
  64. ===============================================================================
  65. High Level TDES functions
  66. ===============================================================================
  67. @endverbatim
  68. * @{
  69. */
  70. /**
  71. * @brief Encrypt and decrypt using TDES in ECB Mode
  72. * @param Mode: encryption or decryption Mode.
  73. * This parameter can be one of the following values:
  74. * @arg MODE_ENCRYPT: Encryption
  75. * @arg MODE_DECRYPT: Decryption
  76. * @param Key: Key used for TDES algorithm.
  77. * @param Ilength: length of the Input buffer, must be a multiple of 8.
  78. * @param Input: pointer to the Input buffer.
  79. * @param Output: pointer to the returned buffer.
  80. * @retval An ErrorStatus enumeration value:
  81. * - SUCCESS: Operation done
  82. * - ERROR: Operation failed
  83. */
  84. ErrorStatus CRYP_TDES_ECB(uint8_t Mode, uint8_t Key[24], uint8_t *Input,
  85. uint32_t Ilength, uint8_t *Output)
  86. {
  87. CRYP_InitTypeDef TDES_CRYP_InitStructure;
  88. CRYP_KeyInitTypeDef TDES_CRYP_KeyInitStructure;
  89. __IO uint32_t counter = 0;
  90. uint32_t busystatus = 0;
  91. ErrorStatus status = SUCCESS;
  92. uint32_t keyaddr = (uint32_t)Key;
  93. uint32_t inputaddr = (uint32_t)Input;
  94. uint32_t outputaddr = (uint32_t)Output;
  95. uint32_t i = 0;
  96. /* Crypto structures initialisation*/
  97. CRYP_KeyStructInit(&TDES_CRYP_KeyInitStructure);
  98. /* Crypto Init for Encryption process */
  99. if(Mode == MODE_ENCRYPT) /* TDES encryption */
  100. {
  101. TDES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
  102. }
  103. else /*if(Mode == MODE_DECRYPT)*/ /* TDES decryption */
  104. {
  105. TDES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
  106. }
  107. TDES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_TDES_ECB;
  108. TDES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
  109. CRYP_Init(&TDES_CRYP_InitStructure);
  110. /* Key Initialisation */
  111. TDES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
  112. keyaddr+=4;
  113. TDES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
  114. keyaddr+=4;
  115. TDES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
  116. keyaddr+=4;
  117. TDES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
  118. keyaddr+=4;
  119. TDES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
  120. keyaddr+=4;
  121. TDES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
  122. CRYP_KeyInit(& TDES_CRYP_KeyInitStructure);
  123. /* Flush IN/OUT FIFO */
  124. CRYP_FIFOFlush();
  125. /* Enable Crypto processor */
  126. CRYP_Cmd(ENABLE);
  127. for(i=0; ((i<Ilength) && (status != ERROR)); i+=8)
  128. {
  129. /* Write the Input block in the Input FIFO */
  130. CRYP_DataIn(*(uint32_t*)(inputaddr));
  131. inputaddr+=4;
  132. CRYP_DataIn(*(uint32_t*)(inputaddr));
  133. inputaddr+=4;
  134. /* Wait until the complete message has been processed */
  135. counter = 0;
  136. do
  137. {
  138. busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
  139. counter++;
  140. }while ((counter != TDESBUSY_TIMEOUT) && (busystatus != RESET));
  141. if (busystatus != RESET)
  142. {
  143. status = ERROR;
  144. }
  145. else
  146. {
  147. /* Read the Output block from the Output FIFO */
  148. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  149. outputaddr+=4;
  150. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  151. outputaddr+=4;
  152. }
  153. }
  154. /* Disable Crypto */
  155. CRYP_Cmd(DISABLE);
  156. return status;
  157. }
  158. /**
  159. * @brief Encrypt and decrypt using TDES in CBC Mode
  160. * @param Mode: encryption or decryption Mode.
  161. * This parameter can be one of the following values:
  162. * @arg MODE_ENCRYPT: Encryption
  163. * @arg MODE_DECRYPT: Decryption
  164. * @param Key: Key used for TDES algorithm.
  165. * @param InitVectors: Initialisation Vectors used for TDES algorithm.
  166. * @param Input: pointer to the Input buffer.
  167. * @param Ilength: length of the Input buffer, must be a multiple of 8.
  168. * @param Output: pointer to the returned buffer.
  169. * @retval An ErrorStatus enumeration value:
  170. * - SUCCESS: Operation done
  171. * - ERROR: Operation failed
  172. */
  173. ErrorStatus CRYP_TDES_CBC(uint8_t Mode, uint8_t Key[24], uint8_t InitVectors[8],
  174. uint8_t *Input, uint32_t Ilength, uint8_t *Output)
  175. {
  176. CRYP_InitTypeDef TDES_CRYP_InitStructure;
  177. CRYP_KeyInitTypeDef TDES_CRYP_KeyInitStructure;
  178. CRYP_IVInitTypeDef TDES_CRYP_IVInitStructure;
  179. __IO uint32_t counter = 0;
  180. uint32_t busystatus = 0;
  181. ErrorStatus status = SUCCESS;
  182. uint32_t keyaddr = (uint32_t)Key;
  183. uint32_t inputaddr = (uint32_t)Input;
  184. uint32_t outputaddr = (uint32_t)Output;
  185. uint32_t ivaddr = (uint32_t)InitVectors;
  186. uint32_t i = 0;
  187. /* Crypto structures initialisation*/
  188. CRYP_KeyStructInit(&TDES_CRYP_KeyInitStructure);
  189. /* Crypto Init for Encryption process */
  190. if(Mode == MODE_ENCRYPT) /* TDES encryption */
  191. {
  192. TDES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
  193. }
  194. else
  195. {
  196. TDES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
  197. }
  198. TDES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_TDES_CBC;
  199. TDES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
  200. CRYP_Init(&TDES_CRYP_InitStructure);
  201. /* Key Initialisation */
  202. TDES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
  203. keyaddr+=4;
  204. TDES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
  205. keyaddr+=4;
  206. TDES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
  207. keyaddr+=4;
  208. TDES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
  209. keyaddr+=4;
  210. TDES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
  211. keyaddr+=4;
  212. TDES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
  213. CRYP_KeyInit(& TDES_CRYP_KeyInitStructure);
  214. /* Initialization Vectors */
  215. TDES_CRYP_IVInitStructure.CRYP_IV0Left = __REV(*(uint32_t*)(ivaddr));
  216. ivaddr+=4;
  217. TDES_CRYP_IVInitStructure.CRYP_IV0Right= __REV(*(uint32_t*)(ivaddr));
  218. CRYP_IVInit(&TDES_CRYP_IVInitStructure);
  219. /* Flush IN/OUT FIFO */
  220. CRYP_FIFOFlush();
  221. /* Enable Crypto processor */
  222. CRYP_Cmd(ENABLE);
  223. for(i=0; ((i<Ilength) && (status != ERROR)); i+=8)
  224. {
  225. /* Write the Input block in the Input FIFO */
  226. CRYP_DataIn(*(uint32_t*)(inputaddr));
  227. inputaddr+=4;
  228. CRYP_DataIn(*(uint32_t*)(inputaddr));
  229. inputaddr+=4;
  230. /* Wait until the complete message has been processed */
  231. counter = 0;
  232. do
  233. {
  234. busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
  235. counter++;
  236. }while ((counter != TDESBUSY_TIMEOUT) && (busystatus != RESET));
  237. if (busystatus != RESET)
  238. {
  239. status = ERROR;
  240. }
  241. else
  242. {
  243. /* Read the Output block from the Output FIFO */
  244. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  245. outputaddr+=4;
  246. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  247. outputaddr+=4;
  248. }
  249. }
  250. /* Disable Crypto */
  251. CRYP_Cmd(DISABLE);
  252. return status;
  253. }
  254. /**
  255. * @}
  256. */
  257. /**
  258. * @}
  259. */
  260. /**
  261. * @}
  262. */
  263. /**
  264. * @}
  265. */
  266. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/