stm32f2xx_cryp_des.c 8.9 KB

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