stm32f4xx_cryp_tdes.c 9.7 KB

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