stm32f4xx_cryp_tdes.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_cryp_tdes.c
  4. * @author MCD Application Team
  5. * @version V1.8.0
  6. * @date 04-November-2016
  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. * <h2><center>&copy; COPYRIGHT 2016 STMicroelectronics</center></h2>
  27. *
  28. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  29. * You may not use this file except in compliance with the License.
  30. * You may obtain a copy of the License at:
  31. *
  32. * http://www.st.com/software_license_agreement_liberty_v2
  33. *
  34. * Unless required by applicable law or agreed to in writing, software
  35. * distributed under the License is distributed on an "AS IS" BASIS,
  36. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  37. * See the License for the specific language governing permissions and
  38. * limitations under the License.
  39. *
  40. ******************************************************************************
  41. */
  42. /* Includes ------------------------------------------------------------------*/
  43. #include "stm32f4xx_cryp.h"
  44. /** @addtogroup STM32F4xx_StdPeriph_Driver
  45. * @{
  46. */
  47. /** @defgroup CRYP
  48. * @brief CRYP driver modules
  49. * @{
  50. */
  51. /* Private typedef -----------------------------------------------------------*/
  52. /* Private define ------------------------------------------------------------*/
  53. #define TDESBUSY_TIMEOUT ((uint32_t) 0x00010000)
  54. /* Private macro -------------------------------------------------------------*/
  55. /* Private variables ---------------------------------------------------------*/
  56. /* Private function prototypes -----------------------------------------------*/
  57. /* Private functions ---------------------------------------------------------*/
  58. /** @defgroup CRYP_Private_Functions
  59. * @{
  60. */
  61. /** @defgroup CRYP_Group7 High Level TDES functions
  62. * @brief High Level TDES functions
  63. *
  64. @verbatim
  65. ===============================================================================
  66. ##### High Level TDES functions #####
  67. ===============================================================================
  68. @endverbatim
  69. * @{
  70. */
  71. /**
  72. * @brief Encrypt and decrypt using TDES in ECB Mode
  73. * @param Mode: encryption or decryption Mode.
  74. * This parameter can be one of the following values:
  75. * @arg MODE_ENCRYPT: Encryption
  76. * @arg MODE_DECRYPT: Decryption
  77. * @param Key: Key used for TDES algorithm.
  78. * @param Ilength: length of the Input buffer, must be a multiple of 8.
  79. * @param Input: pointer to the Input buffer.
  80. * @param Output: pointer to the returned buffer.
  81. * @retval An ErrorStatus enumeration value:
  82. * - SUCCESS: Operation done
  83. * - ERROR: Operation failed
  84. */
  85. ErrorStatus CRYP_TDES_ECB(uint8_t Mode, uint8_t Key[24], uint8_t *Input,
  86. uint32_t Ilength, uint8_t *Output)
  87. {
  88. CRYP_InitTypeDef TDES_CRYP_InitStructure;
  89. CRYP_KeyInitTypeDef TDES_CRYP_KeyInitStructure;
  90. __IO uint32_t counter = 0;
  91. uint32_t busystatus = 0;
  92. ErrorStatus status = SUCCESS;
  93. uint32_t keyaddr = (uint32_t)Key;
  94. uint32_t inputaddr = (uint32_t)Input;
  95. uint32_t outputaddr = (uint32_t)Output;
  96. uint32_t i = 0;
  97. /* Crypto structures initialisation*/
  98. CRYP_KeyStructInit(&TDES_CRYP_KeyInitStructure);
  99. /* Crypto Init for Encryption process */
  100. if(Mode == MODE_ENCRYPT) /* TDES encryption */
  101. {
  102. TDES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
  103. }
  104. else /*if(Mode == MODE_DECRYPT)*/ /* TDES decryption */
  105. {
  106. TDES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
  107. }
  108. TDES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_TDES_ECB;
  109. TDES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
  110. CRYP_Init(&TDES_CRYP_InitStructure);
  111. /* Key Initialisation */
  112. TDES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
  113. keyaddr+=4;
  114. TDES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
  115. keyaddr+=4;
  116. TDES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
  117. keyaddr+=4;
  118. TDES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
  119. keyaddr+=4;
  120. TDES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
  121. keyaddr+=4;
  122. TDES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
  123. CRYP_KeyInit(& TDES_CRYP_KeyInitStructure);
  124. /* Flush IN/OUT FIFO */
  125. CRYP_FIFOFlush();
  126. /* Enable Crypto processor */
  127. CRYP_Cmd(ENABLE);
  128. if(CRYP_GetCmdStatus() == DISABLE)
  129. {
  130. /* The CRYP peripheral clock is not enabled or the device doesn't embed
  131. the CRYP peripheral (please check the device sales type. */
  132. status = ERROR;
  133. }
  134. else
  135. {
  136. for(i=0; ((i<Ilength) && (status != ERROR)); i+=8)
  137. {
  138. /* Write the Input block in the Input FIFO */
  139. CRYP_DataIn(*(uint32_t*)(inputaddr));
  140. inputaddr+=4;
  141. CRYP_DataIn(*(uint32_t*)(inputaddr));
  142. inputaddr+=4;
  143. /* Wait until the complete message has been processed */
  144. counter = 0;
  145. do
  146. {
  147. busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
  148. counter++;
  149. }while ((counter != TDESBUSY_TIMEOUT) && (busystatus != RESET));
  150. if (busystatus != RESET)
  151. {
  152. status = ERROR;
  153. }
  154. else
  155. {
  156. /* Read the Output block from the Output FIFO */
  157. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  158. outputaddr+=4;
  159. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  160. outputaddr+=4;
  161. }
  162. }
  163. /* Disable Crypto */
  164. CRYP_Cmd(DISABLE);
  165. }
  166. return status;
  167. }
  168. /**
  169. * @brief Encrypt and decrypt using TDES in CBC Mode
  170. * @param Mode: encryption or decryption Mode.
  171. * This parameter can be one of the following values:
  172. * @arg MODE_ENCRYPT: Encryption
  173. * @arg MODE_DECRYPT: Decryption
  174. * @param Key: Key used for TDES algorithm.
  175. * @param InitVectors: Initialisation Vectors used for TDES algorithm.
  176. * @param Input: pointer to the Input buffer.
  177. * @param Ilength: length of the Input buffer, must be a multiple of 8.
  178. * @param Output: pointer to the returned buffer.
  179. * @retval An ErrorStatus enumeration value:
  180. * - SUCCESS: Operation done
  181. * - ERROR: Operation failed
  182. */
  183. ErrorStatus CRYP_TDES_CBC(uint8_t Mode, uint8_t Key[24], uint8_t InitVectors[8],
  184. uint8_t *Input, uint32_t Ilength, uint8_t *Output)
  185. {
  186. CRYP_InitTypeDef TDES_CRYP_InitStructure;
  187. CRYP_KeyInitTypeDef TDES_CRYP_KeyInitStructure;
  188. CRYP_IVInitTypeDef TDES_CRYP_IVInitStructure;
  189. __IO uint32_t counter = 0;
  190. uint32_t busystatus = 0;
  191. ErrorStatus status = SUCCESS;
  192. uint32_t keyaddr = (uint32_t)Key;
  193. uint32_t inputaddr = (uint32_t)Input;
  194. uint32_t outputaddr = (uint32_t)Output;
  195. uint32_t ivaddr = (uint32_t)InitVectors;
  196. uint32_t i = 0;
  197. /* Crypto structures initialisation*/
  198. CRYP_KeyStructInit(&TDES_CRYP_KeyInitStructure);
  199. /* Crypto Init for Encryption process */
  200. if(Mode == MODE_ENCRYPT) /* TDES encryption */
  201. {
  202. TDES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
  203. }
  204. else
  205. {
  206. TDES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
  207. }
  208. TDES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_TDES_CBC;
  209. TDES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
  210. CRYP_Init(&TDES_CRYP_InitStructure);
  211. /* Key Initialisation */
  212. TDES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
  213. keyaddr+=4;
  214. TDES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
  215. keyaddr+=4;
  216. TDES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
  217. keyaddr+=4;
  218. TDES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
  219. keyaddr+=4;
  220. TDES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
  221. keyaddr+=4;
  222. TDES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
  223. CRYP_KeyInit(& TDES_CRYP_KeyInitStructure);
  224. /* Initialization Vectors */
  225. TDES_CRYP_IVInitStructure.CRYP_IV0Left = __REV(*(uint32_t*)(ivaddr));
  226. ivaddr+=4;
  227. TDES_CRYP_IVInitStructure.CRYP_IV0Right= __REV(*(uint32_t*)(ivaddr));
  228. CRYP_IVInit(&TDES_CRYP_IVInitStructure);
  229. /* Flush IN/OUT FIFO */
  230. CRYP_FIFOFlush();
  231. /* Enable Crypto processor */
  232. CRYP_Cmd(ENABLE);
  233. if(CRYP_GetCmdStatus() == DISABLE)
  234. {
  235. /* The CRYP peripheral clock is not enabled or the device doesn't embed
  236. the CRYP peripheral (please check the device sales type. */
  237. status = ERROR;
  238. }
  239. else
  240. {
  241. for(i=0; ((i<Ilength) && (status != ERROR)); i+=8)
  242. {
  243. /* Write the Input block in the Input FIFO */
  244. CRYP_DataIn(*(uint32_t*)(inputaddr));
  245. inputaddr+=4;
  246. CRYP_DataIn(*(uint32_t*)(inputaddr));
  247. inputaddr+=4;
  248. /* Wait until the complete message has been processed */
  249. counter = 0;
  250. do
  251. {
  252. busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
  253. counter++;
  254. }while ((counter != TDESBUSY_TIMEOUT) && (busystatus != RESET));
  255. if (busystatus != RESET)
  256. {
  257. status = ERROR;
  258. }
  259. else
  260. {
  261. /* Read the Output block from the Output FIFO */
  262. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  263. outputaddr+=4;
  264. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  265. outputaddr+=4;
  266. }
  267. }
  268. /* Disable Crypto */
  269. CRYP_Cmd(DISABLE);
  270. }
  271. return status;
  272. }
  273. /**
  274. * @}
  275. */
  276. /**
  277. * @}
  278. */
  279. /**
  280. * @}
  281. */
  282. /**
  283. * @}
  284. */
  285. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/