stm32f4xx_hash_md5.c 8.6 KB

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