選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

326 行
10 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_cryp_tdes.c
  4. * @author MCD Application Team
  5. * @version V1.1.0
  6. * @date 11-January-2013
  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 2013 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 embedd
  131. the CRYP peripheral (please check the device sales type. */
  132. return(ERROR);
  133. }
  134. for(i=0; ((i<Ilength) && (status != ERROR)); i+=8)
  135. {
  136. /* Write the Input block in the Input FIFO */
  137. CRYP_DataIn(*(uint32_t*)(inputaddr));
  138. inputaddr+=4;
  139. CRYP_DataIn(*(uint32_t*)(inputaddr));
  140. inputaddr+=4;
  141. /* Wait until the complete message has been processed */
  142. counter = 0;
  143. do
  144. {
  145. busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
  146. counter++;
  147. }while ((counter != TDESBUSY_TIMEOUT) && (busystatus != RESET));
  148. if (busystatus != RESET)
  149. {
  150. status = ERROR;
  151. }
  152. else
  153. {
  154. /* Read the Output block from the Output FIFO */
  155. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  156. outputaddr+=4;
  157. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  158. outputaddr+=4;
  159. }
  160. }
  161. /* Disable Crypto */
  162. CRYP_Cmd(DISABLE);
  163. return status;
  164. }
  165. /**
  166. * @brief Encrypt and decrypt using TDES in CBC Mode
  167. * @param Mode: encryption or decryption Mode.
  168. * This parameter can be one of the following values:
  169. * @arg MODE_ENCRYPT: Encryption
  170. * @arg MODE_DECRYPT: Decryption
  171. * @param Key: Key used for TDES algorithm.
  172. * @param InitVectors: Initialisation Vectors used for TDES algorithm.
  173. * @param Input: pointer to the Input buffer.
  174. * @param Ilength: length of the Input buffer, must be a multiple of 8.
  175. * @param Output: pointer to the returned buffer.
  176. * @retval An ErrorStatus enumeration value:
  177. * - SUCCESS: Operation done
  178. * - ERROR: Operation failed
  179. */
  180. ErrorStatus CRYP_TDES_CBC(uint8_t Mode, uint8_t Key[24], uint8_t InitVectors[8],
  181. uint8_t *Input, uint32_t Ilength, uint8_t *Output)
  182. {
  183. CRYP_InitTypeDef TDES_CRYP_InitStructure;
  184. CRYP_KeyInitTypeDef TDES_CRYP_KeyInitStructure;
  185. CRYP_IVInitTypeDef TDES_CRYP_IVInitStructure;
  186. __IO uint32_t counter = 0;
  187. uint32_t busystatus = 0;
  188. ErrorStatus status = SUCCESS;
  189. uint32_t keyaddr = (uint32_t)Key;
  190. uint32_t inputaddr = (uint32_t)Input;
  191. uint32_t outputaddr = (uint32_t)Output;
  192. uint32_t ivaddr = (uint32_t)InitVectors;
  193. uint32_t i = 0;
  194. /* Crypto structures initialisation*/
  195. CRYP_KeyStructInit(&TDES_CRYP_KeyInitStructure);
  196. /* Crypto Init for Encryption process */
  197. if(Mode == MODE_ENCRYPT) /* TDES encryption */
  198. {
  199. TDES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
  200. }
  201. else
  202. {
  203. TDES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
  204. }
  205. TDES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_TDES_CBC;
  206. TDES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
  207. CRYP_Init(&TDES_CRYP_InitStructure);
  208. /* Key Initialisation */
  209. TDES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
  210. keyaddr+=4;
  211. TDES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
  212. keyaddr+=4;
  213. TDES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
  214. keyaddr+=4;
  215. TDES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
  216. keyaddr+=4;
  217. TDES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
  218. keyaddr+=4;
  219. TDES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
  220. CRYP_KeyInit(& TDES_CRYP_KeyInitStructure);
  221. /* Initialization Vectors */
  222. TDES_CRYP_IVInitStructure.CRYP_IV0Left = __REV(*(uint32_t*)(ivaddr));
  223. ivaddr+=4;
  224. TDES_CRYP_IVInitStructure.CRYP_IV0Right= __REV(*(uint32_t*)(ivaddr));
  225. CRYP_IVInit(&TDES_CRYP_IVInitStructure);
  226. /* Flush IN/OUT FIFO */
  227. CRYP_FIFOFlush();
  228. /* Enable Crypto processor */
  229. CRYP_Cmd(ENABLE);
  230. if(CRYP_GetCmdStatus() == DISABLE)
  231. {
  232. /* The CRYP peripheral clock is not enabled or the device doesn't embedd
  233. the CRYP peripheral (please check the device sales type. */
  234. return(ERROR);
  235. }
  236. for(i=0; ((i<Ilength) && (status != ERROR)); i+=8)
  237. {
  238. /* Write the Input block in the Input FIFO */
  239. CRYP_DataIn(*(uint32_t*)(inputaddr));
  240. inputaddr+=4;
  241. CRYP_DataIn(*(uint32_t*)(inputaddr));
  242. inputaddr+=4;
  243. /* Wait until the complete message has been processed */
  244. counter = 0;
  245. do
  246. {
  247. busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
  248. counter++;
  249. }while ((counter != TDESBUSY_TIMEOUT) && (busystatus != RESET));
  250. if (busystatus != RESET)
  251. {
  252. status = ERROR;
  253. }
  254. else
  255. {
  256. /* Read the Output block from the Output FIFO */
  257. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  258. outputaddr+=4;
  259. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  260. outputaddr+=4;
  261. }
  262. }
  263. /* Disable Crypto */
  264. CRYP_Cmd(DISABLE);
  265. return status;
  266. }
  267. /**
  268. * @}
  269. */
  270. /**
  271. * @}
  272. */
  273. /**
  274. * @}
  275. */
  276. /**
  277. * @}
  278. */
  279. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/