stm32f4xx_cryp_des.c 9.0 KB

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