stm32f2xx_hash_md5.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /**
  2. ******************************************************************************
  3. * @file stm32f2xx_hash_md5.c
  4. * @author MCD Application Team
  5. * @version V1.1.3
  6. * @date 31-December-2021
  7. * @brief This file provides high level functions to compute the HASH MD5 and
  8. * HMAC MD5 Digest of an input message.
  9. * It uses the stm32f2xx_hash.c/.h drivers to access the STM32F2xx HASH
  10. * peripheral.
  11. *
  12. * @verbatim
  13. *
  14. * ===================================================================
  15. * How to use this driver
  16. * ===================================================================
  17. * 1. Enable The HASH controller clock using
  18. * RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_HASH, ENABLE); function.
  19. *
  20. * 2. Calculate the HASH MD5 Digest using HASH_MD5() function.
  21. *
  22. * 3. Calculate the HMAC MD5 Digest using HMAC_MD5() function.
  23. *
  24. * @endverbatim
  25. *
  26. ******************************************************************************
  27. * @attention
  28. *
  29. * Copyright (c) 2012 STMicroelectronics.
  30. * All rights reserved.
  31. *
  32. * This software is licensed under terms that can be found in the LICENSE file
  33. * in the root directory of this software component.
  34. * If no LICENSE file comes with this software, it is provided AS-IS.
  35. *
  36. ******************************************************************************
  37. */
  38. /* Includes ------------------------------------------------------------------*/
  39. #include "stm32f2xx_hash.h"
  40. /** @addtogroup STM32F2xx_StdPeriph_Driver
  41. * @{
  42. */
  43. /** @defgroup HASH
  44. * @brief HASH driver modules
  45. * @{
  46. */
  47. /* Private typedef -----------------------------------------------------------*/
  48. /* Private define ------------------------------------------------------------*/
  49. #define MD5BUSY_TIMEOUT ((uint32_t) 0x00010000)
  50. /* Private macro -------------------------------------------------------------*/
  51. /* Private variables ---------------------------------------------------------*/
  52. /* Private function prototypes -----------------------------------------------*/
  53. /* Private functions ---------------------------------------------------------*/
  54. /** @defgroup HASH_Private_Functions
  55. * @{
  56. */
  57. /** @defgroup HASH_Group7 High Level MD5 functions
  58. * @brief High Level MD5 Hash and HMAC functions
  59. *
  60. @verbatim
  61. ===============================================================================
  62. High Level MD5 Hash and HMAC functions
  63. ===============================================================================
  64. @endverbatim
  65. * @{
  66. */
  67. /**
  68. * @brief Compute the HASH MD5 digest.
  69. * @param Input: pointer to the Input buffer to be treated.
  70. * @param Ilen: length of the Input buffer.
  71. * @param Output: the returned digest
  72. * @retval An ErrorStatus enumeration value:
  73. * - SUCCESS: digest computation done
  74. * - ERROR: digest computation failed
  75. */
  76. ErrorStatus HASH_MD5(uint8_t *Input, uint32_t Ilen, uint8_t Output[16])
  77. {
  78. HASH_InitTypeDef MD5_HASH_InitStructure;
  79. HASH_MsgDigest MD5_MessageDigest;
  80. __IO uint16_t nbvalidbitsdata = 0;
  81. uint32_t i = 0;
  82. __IO uint32_t counter = 0;
  83. uint32_t busystatus = 0;
  84. ErrorStatus status = SUCCESS;
  85. uint32_t inputaddr = (uint32_t)Input;
  86. uint32_t outputaddr = (uint32_t)Output;
  87. /* Number of valid bits in last word of the Input data */
  88. nbvalidbitsdata = 8 * (Ilen % 4);
  89. /* HASH peripheral initialization */
  90. HASH_DeInit();
  91. /* HASH Configuration */
  92. MD5_HASH_InitStructure.HASH_AlgoSelection = HASH_AlgoSelection_MD5;
  93. MD5_HASH_InitStructure.HASH_AlgoMode = HASH_AlgoMode_HASH;
  94. MD5_HASH_InitStructure.HASH_DataType = HASH_DataType_8b;
  95. HASH_Init(&MD5_HASH_InitStructure);
  96. /* Configure the number of valid bits in last word of the data */
  97. HASH_SetLastWordValidBitsNbr(nbvalidbitsdata);
  98. /* Write the Input block in the IN FIFO */
  99. for(i=0; i<Ilen; i+=4)
  100. {
  101. HASH_DataIn(*(uint32_t*)inputaddr);
  102. inputaddr+=4;
  103. }
  104. /* Start the HASH processor */
  105. HASH_StartDigest();
  106. /* wait until the Busy flag is RESET */
  107. do
  108. {
  109. busystatus = HASH_GetFlagStatus(HASH_FLAG_BUSY);
  110. counter++;
  111. }while ((counter != MD5BUSY_TIMEOUT) && (busystatus != RESET));
  112. if (busystatus != RESET)
  113. {
  114. status = ERROR;
  115. }
  116. else
  117. {
  118. /* Read the message digest */
  119. HASH_GetDigest(&MD5_MessageDigest);
  120. *(uint32_t*)(outputaddr) = __REV(MD5_MessageDigest.Data[0]);
  121. outputaddr+=4;
  122. *(uint32_t*)(outputaddr) = __REV(MD5_MessageDigest.Data[1]);
  123. outputaddr+=4;
  124. *(uint32_t*)(outputaddr) = __REV(MD5_MessageDigest.Data[2]);
  125. outputaddr+=4;
  126. *(uint32_t*)(outputaddr) = __REV(MD5_MessageDigest.Data[3]);
  127. }
  128. return status;
  129. }
  130. /**
  131. * @brief Compute the HMAC MD5 digest.
  132. * @param Key: pointer to the Key used for HMAC.
  133. * @param Keylen: length of the Key used for HMAC.
  134. * @param Input: pointer to the Input buffer to be treated.
  135. * @param Ilen: length of the Input buffer.
  136. * @param Output: the returned digest
  137. * @retval An ErrorStatus enumeration value:
  138. * - SUCCESS: digest computation done
  139. * - ERROR: digest computation failed
  140. */
  141. ErrorStatus HMAC_MD5(uint8_t *Key, uint32_t Keylen, uint8_t *Input,
  142. uint32_t Ilen, uint8_t Output[16])
  143. {
  144. HASH_InitTypeDef MD5_HASH_InitStructure;
  145. HASH_MsgDigest MD5_MessageDigest;
  146. __IO uint16_t nbvalidbitsdata = 0;
  147. __IO uint16_t nbvalidbitskey = 0;
  148. uint32_t i = 0;
  149. __IO uint32_t counter = 0;
  150. uint32_t busystatus = 0;
  151. ErrorStatus status = SUCCESS;
  152. uint32_t keyaddr = (uint32_t)Key;
  153. uint32_t inputaddr = (uint32_t)Input;
  154. uint32_t outputaddr = (uint32_t)Output;
  155. /* Number of valid bits in last word of the Input data */
  156. nbvalidbitsdata = 8 * (Ilen % 4);
  157. /* Number of valid bits in last word of the Key */
  158. nbvalidbitskey = 8 * (Keylen % 4);
  159. /* HASH peripheral initialization */
  160. HASH_DeInit();
  161. /* HASH Configuration */
  162. MD5_HASH_InitStructure.HASH_AlgoSelection = HASH_AlgoSelection_MD5;
  163. MD5_HASH_InitStructure.HASH_AlgoMode = HASH_AlgoMode_HMAC;
  164. MD5_HASH_InitStructure.HASH_DataType = HASH_DataType_8b;
  165. if(Keylen > 64)
  166. {
  167. /* HMAC long Key */
  168. MD5_HASH_InitStructure.HASH_HMACKeyType = HASH_HMACKeyType_LongKey;
  169. }
  170. else
  171. {
  172. /* HMAC short Key */
  173. MD5_HASH_InitStructure.HASH_HMACKeyType = HASH_HMACKeyType_ShortKey;
  174. }
  175. HASH_Init(&MD5_HASH_InitStructure);
  176. /* Configure the number of valid bits in last word of the Key */
  177. HASH_SetLastWordValidBitsNbr(nbvalidbitskey);
  178. /* Write the Key */
  179. for(i=0; i<Keylen; i+=4)
  180. {
  181. HASH_DataIn(*(uint32_t*)keyaddr);
  182. keyaddr+=4;
  183. }
  184. /* Start the HASH processor */
  185. HASH_StartDigest();
  186. /* wait until the Busy flag is RESET */
  187. do
  188. {
  189. busystatus = HASH_GetFlagStatus(HASH_FLAG_BUSY);
  190. counter++;
  191. }while ((counter != MD5BUSY_TIMEOUT) && (busystatus != RESET));
  192. if (busystatus != RESET)
  193. {
  194. status = ERROR;
  195. }
  196. else
  197. {
  198. /* Configure the number of valid bits in last word of the Input data */
  199. HASH_SetLastWordValidBitsNbr(nbvalidbitsdata);
  200. /* Write the Input block in the IN FIFO */
  201. for(i=0; i<Ilen; i+=4)
  202. {
  203. HASH_DataIn(*(uint32_t*)inputaddr);
  204. inputaddr+=4;
  205. }
  206. /* Start the HASH processor */
  207. HASH_StartDigest();
  208. /* wait until the Busy flag is RESET */
  209. counter =0;
  210. do
  211. {
  212. busystatus = HASH_GetFlagStatus(HASH_FLAG_BUSY);
  213. counter++;
  214. }while ((counter != MD5BUSY_TIMEOUT) && (busystatus != RESET));
  215. if (busystatus != RESET)
  216. {
  217. status = ERROR;
  218. }
  219. else
  220. {
  221. /* Configure the number of valid bits in last word of the Key */
  222. HASH_SetLastWordValidBitsNbr(nbvalidbitskey);
  223. /* Write the Key */
  224. keyaddr = (uint32_t)Key;
  225. for(i=0; i<Keylen; i+=4)
  226. {
  227. HASH_DataIn(*(uint32_t*)keyaddr);
  228. keyaddr+=4;
  229. }
  230. /* Start the HASH processor */
  231. HASH_StartDigest();
  232. /* wait until the Busy flag is RESET */
  233. counter =0;
  234. do
  235. {
  236. busystatus = HASH_GetFlagStatus(HASH_FLAG_BUSY);
  237. counter++;
  238. }while ((counter != MD5BUSY_TIMEOUT) && (busystatus != RESET));
  239. if (busystatus != RESET)
  240. {
  241. status = ERROR;
  242. }
  243. else
  244. {
  245. /* Read the message digest */
  246. HASH_GetDigest(&MD5_MessageDigest);
  247. *(uint32_t*)(outputaddr) = __REV(MD5_MessageDigest.Data[0]);
  248. outputaddr+=4;
  249. *(uint32_t*)(outputaddr) = __REV(MD5_MessageDigest.Data[1]);
  250. outputaddr+=4;
  251. *(uint32_t*)(outputaddr) = __REV(MD5_MessageDigest.Data[2]);
  252. outputaddr+=4;
  253. *(uint32_t*)(outputaddr) = __REV(MD5_MessageDigest.Data[3]);
  254. }
  255. }
  256. }
  257. return status;
  258. }
  259. /**
  260. * @}
  261. */
  262. /**
  263. * @}
  264. */
  265. /**
  266. * @}
  267. */
  268. /**
  269. * @}
  270. */