Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

309 řádky
9.3 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_cryp_des.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 DES 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 DES in ECB Mode using CRYP_DES_ECB() function.
  20. (#) Encrypt and decrypt using DES in CBC Mode using CRYP_DES_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 DESBUSY_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_Group8 High Level DES functions
  62. * @brief High Level DES functions
  63. *
  64. @verbatim
  65. ===============================================================================
  66. ##### High Level DES functions #####
  67. ===============================================================================
  68. @endverbatim
  69. * @{
  70. */
  71. /**
  72. * @brief Encrypt and decrypt using DES 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 DES 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_DES_ECB(uint8_t Mode, uint8_t Key[8], uint8_t *Input,
  86. uint32_t Ilength, uint8_t *Output)
  87. {
  88. CRYP_InitTypeDef DES_CRYP_InitStructure;
  89. CRYP_KeyInitTypeDef DES_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(&DES_CRYP_KeyInitStructure);
  99. /* Crypto Init for Encryption process */
  100. if( Mode == MODE_ENCRYPT ) /* DES encryption */
  101. {
  102. DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
  103. }
  104. else/* if( Mode == MODE_DECRYPT )*/ /* DES decryption */
  105. {
  106. DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
  107. }
  108. DES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_DES_ECB;
  109. DES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
  110. CRYP_Init(&DES_CRYP_InitStructure);
  111. /* Key Initialisation */
  112. DES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
  113. keyaddr+=4;
  114. DES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
  115. CRYP_KeyInit(& DES_CRYP_KeyInitStructure);
  116. /* Flush IN/OUT FIFO */
  117. CRYP_FIFOFlush();
  118. /* Enable Crypto processor */
  119. CRYP_Cmd(ENABLE);
  120. if(CRYP_GetCmdStatus() == DISABLE)
  121. {
  122. /* The CRYP peripheral clock is not enabled or the device doesn't embedd
  123. the CRYP peripheral (please check the device sales type. */
  124. return(ERROR);
  125. }
  126. for(i=0; ((i<Ilength) && (status != ERROR)); i+=8)
  127. {
  128. /* Write the Input block in the Input FIFO */
  129. CRYP_DataIn(*(uint32_t*)(inputaddr));
  130. inputaddr+=4;
  131. CRYP_DataIn(*(uint32_t*)(inputaddr));
  132. inputaddr+=4;
  133. /* Wait until the complete message has been processed */
  134. counter = 0;
  135. do
  136. {
  137. busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
  138. counter++;
  139. }while ((counter != DESBUSY_TIMEOUT) && (busystatus != RESET));
  140. if (busystatus != RESET)
  141. {
  142. status = ERROR;
  143. }
  144. else
  145. {
  146. /* Read the Output block from the Output FIFO */
  147. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  148. outputaddr+=4;
  149. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  150. outputaddr+=4;
  151. }
  152. }
  153. /* Disable Crypto */
  154. CRYP_Cmd(DISABLE);
  155. return status;
  156. }
  157. /**
  158. * @brief Encrypt and decrypt using DES in CBC Mode
  159. * @param Mode: encryption or decryption Mode.
  160. * This parameter can be one of the following values:
  161. * @arg MODE_ENCRYPT: Encryption
  162. * @arg MODE_DECRYPT: Decryption
  163. * @param Key: Key used for DES algorithm.
  164. * @param InitVectors: Initialisation Vectors used for DES algorithm.
  165. * @param Ilength: length of the Input buffer, must be a multiple of 8.
  166. * @param Input: pointer to the Input buffer.
  167. * @param Output: pointer to the returned buffer.
  168. * @retval An ErrorStatus enumeration value:
  169. * - SUCCESS: Operation done
  170. * - ERROR: Operation failed
  171. */
  172. ErrorStatus CRYP_DES_CBC(uint8_t Mode, uint8_t Key[8], uint8_t InitVectors[8],
  173. uint8_t *Input, uint32_t Ilength, uint8_t *Output)
  174. {
  175. CRYP_InitTypeDef DES_CRYP_InitStructure;
  176. CRYP_KeyInitTypeDef DES_CRYP_KeyInitStructure;
  177. CRYP_IVInitTypeDef DES_CRYP_IVInitStructure;
  178. __IO uint32_t counter = 0;
  179. uint32_t busystatus = 0;
  180. ErrorStatus status = SUCCESS;
  181. uint32_t keyaddr = (uint32_t)Key;
  182. uint32_t inputaddr = (uint32_t)Input;
  183. uint32_t outputaddr = (uint32_t)Output;
  184. uint32_t ivaddr = (uint32_t)InitVectors;
  185. uint32_t i = 0;
  186. /* Crypto structures initialisation*/
  187. CRYP_KeyStructInit(&DES_CRYP_KeyInitStructure);
  188. /* Crypto Init for Encryption process */
  189. if(Mode == MODE_ENCRYPT) /* DES encryption */
  190. {
  191. DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
  192. }
  193. else /*if(Mode == MODE_DECRYPT)*/ /* DES decryption */
  194. {
  195. DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
  196. }
  197. DES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_DES_CBC;
  198. DES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
  199. CRYP_Init(&DES_CRYP_InitStructure);
  200. /* Key Initialisation */
  201. DES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
  202. keyaddr+=4;
  203. DES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
  204. CRYP_KeyInit(& DES_CRYP_KeyInitStructure);
  205. /* Initialization Vectors */
  206. DES_CRYP_IVInitStructure.CRYP_IV0Left = __REV(*(uint32_t*)(ivaddr));
  207. ivaddr+=4;
  208. DES_CRYP_IVInitStructure.CRYP_IV0Right= __REV(*(uint32_t*)(ivaddr));
  209. CRYP_IVInit(&DES_CRYP_IVInitStructure);
  210. /* Flush IN/OUT FIFO */
  211. CRYP_FIFOFlush();
  212. /* Enable Crypto processor */
  213. CRYP_Cmd(ENABLE);
  214. if(CRYP_GetCmdStatus() == DISABLE)
  215. {
  216. /* The CRYP peripheral clock is not enabled or the device doesn't embedd
  217. the CRYP peripheral (please check the device sales type. */
  218. return(ERROR);
  219. }
  220. for(i=0; ((i<Ilength) && (status != ERROR)); i+=8)
  221. {
  222. /* Write the Input block in the Input FIFO */
  223. CRYP_DataIn(*(uint32_t*)(inputaddr));
  224. inputaddr+=4;
  225. CRYP_DataIn(*(uint32_t*)(inputaddr));
  226. inputaddr+=4;
  227. /* Wait until the complete message has been processed */
  228. counter = 0;
  229. do
  230. {
  231. busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
  232. counter++;
  233. }while ((counter != DESBUSY_TIMEOUT) && (busystatus != RESET));
  234. if (busystatus != RESET)
  235. {
  236. status = ERROR;
  237. }
  238. else
  239. {
  240. /* Read the Output block from the Output FIFO */
  241. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  242. outputaddr+=4;
  243. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  244. outputaddr+=4;
  245. }
  246. }
  247. /* Disable Crypto */
  248. CRYP_Cmd(DISABLE);
  249. return status;
  250. }
  251. /**
  252. * @}
  253. */
  254. /**
  255. * @}
  256. */
  257. /**
  258. * @}
  259. */
  260. /**
  261. * @}
  262. */
  263. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/