stm32f4xx_hash_md5.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hash_md5.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 30-September-2011
  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. * ===================================================================
  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. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  30. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  31. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  32. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  33. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  34. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  35. *
  36. * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  37. ******************************************************************************
  38. */
  39. /* Includes ------------------------------------------------------------------*/
  40. #include "stm32f4xx_hash.h"
  41. /** @addtogroup STM32F4xx_StdPeriph_Driver
  42. * @{
  43. */
  44. /** @defgroup HASH
  45. * @brief HASH driver modules
  46. * @{
  47. */
  48. /* Private typedef -----------------------------------------------------------*/
  49. /* Private define ------------------------------------------------------------*/
  50. #define MD5BUSY_TIMEOUT ((uint32_t) 0x00010000)
  51. /* Private macro -------------------------------------------------------------*/
  52. /* Private variables ---------------------------------------------------------*/
  53. /* Private function prototypes -----------------------------------------------*/
  54. /* Private functions ---------------------------------------------------------*/
  55. /** @defgroup HASH_Private_Functions
  56. * @{
  57. */
  58. /** @defgroup HASH_Group7 High Level MD5 functions
  59. * @brief High Level MD5 Hash and HMAC functions
  60. *
  61. @verbatim
  62. ===============================================================================
  63. High Level MD5 Hash and HMAC functions
  64. ===============================================================================
  65. @endverbatim
  66. * @{
  67. */
  68. /**
  69. * @brief Compute the HASH MD5 digest.
  70. * @param Input: pointer to the Input buffer to be treated.
  71. * @param Ilen: length of the Input buffer.
  72. * @param Output: the returned digest
  73. * @retval An ErrorStatus enumeration value:
  74. * - SUCCESS: digest computation done
  75. * - ERROR: digest computation failed
  76. */
  77. ErrorStatus HASH_MD5(uint8_t *Input, uint32_t Ilen, uint8_t Output[16])
  78. {
  79. HASH_InitTypeDef MD5_HASH_InitStructure;
  80. HASH_MsgDigest MD5_MessageDigest;
  81. __IO uint16_t nbvalidbitsdata = 0;
  82. uint32_t i = 0;
  83. __IO uint32_t counter = 0;
  84. uint32_t busystatus = 0;
  85. ErrorStatus status = SUCCESS;
  86. uint32_t inputaddr = (uint32_t)Input;
  87. uint32_t outputaddr = (uint32_t)Output;
  88. /* Number of valid bits in last word of the Input data */
  89. nbvalidbitsdata = 8 * (Ilen % 4);
  90. /* HASH peripheral initialization */
  91. HASH_DeInit();
  92. /* HASH Configuration */
  93. MD5_HASH_InitStructure.HASH_AlgoSelection = HASH_AlgoSelection_MD5;
  94. MD5_HASH_InitStructure.HASH_AlgoMode = HASH_AlgoMode_HASH;
  95. MD5_HASH_InitStructure.HASH_DataType = HASH_DataType_8b;
  96. HASH_Init(&MD5_HASH_InitStructure);
  97. /* Configure the number of valid bits in last word of the data */
  98. HASH_SetLastWordValidBitsNbr(nbvalidbitsdata);
  99. /* Write the Input block in the IN FIFO */
  100. for(i=0; i<Ilen; i+=4)
  101. {
  102. HASH_DataIn(*(uint32_t*)inputaddr);
  103. inputaddr+=4;
  104. }
  105. /* Start the HASH processor */
  106. HASH_StartDigest();
  107. /* wait until the Busy flag is RESET */
  108. do
  109. {
  110. busystatus = HASH_GetFlagStatus(HASH_FLAG_BUSY);
  111. counter++;
  112. }while ((counter != MD5BUSY_TIMEOUT) && (busystatus != RESET));
  113. if (busystatus != RESET)
  114. {
  115. status = ERROR;
  116. }
  117. else
  118. {
  119. /* Read the message digest */
  120. HASH_GetDigest(&MD5_MessageDigest);
  121. *(uint32_t*)(outputaddr) = __REV(MD5_MessageDigest.Data[0]);
  122. outputaddr+=4;
  123. *(uint32_t*)(outputaddr) = __REV(MD5_MessageDigest.Data[1]);
  124. outputaddr+=4;
  125. *(uint32_t*)(outputaddr) = __REV(MD5_MessageDigest.Data[2]);
  126. outputaddr+=4;
  127. *(uint32_t*)(outputaddr) = __REV(MD5_MessageDigest.Data[3]);
  128. }
  129. return status;
  130. }
  131. /**
  132. * @brief Compute the HMAC MD5 digest.
  133. * @param Key: pointer to the Key used for HMAC.
  134. * @param Keylen: length of the Key used for HMAC.
  135. * @param Input: pointer to the Input buffer to be treated.
  136. * @param Ilen: length of the Input buffer.
  137. * @param Output: the returned digest
  138. * @retval An ErrorStatus enumeration value:
  139. * - SUCCESS: digest computation done
  140. * - ERROR: digest computation failed
  141. */
  142. ErrorStatus HMAC_MD5(uint8_t *Key, uint32_t Keylen, uint8_t *Input,
  143. uint32_t Ilen, uint8_t Output[16])
  144. {
  145. HASH_InitTypeDef MD5_HASH_InitStructure;
  146. HASH_MsgDigest MD5_MessageDigest;
  147. __IO uint16_t nbvalidbitsdata = 0;
  148. __IO uint16_t nbvalidbitskey = 0;
  149. uint32_t i = 0;
  150. __IO uint32_t counter = 0;
  151. uint32_t busystatus = 0;
  152. ErrorStatus status = SUCCESS;
  153. uint32_t keyaddr = (uint32_t)Key;
  154. uint32_t inputaddr = (uint32_t)Input;
  155. uint32_t outputaddr = (uint32_t)Output;
  156. /* Number of valid bits in last word of the Input data */
  157. nbvalidbitsdata = 8 * (Ilen % 4);
  158. /* Number of valid bits in last word of the Key */
  159. nbvalidbitskey = 8 * (Keylen % 4);
  160. /* HASH peripheral initialization */
  161. HASH_DeInit();
  162. /* HASH Configuration */
  163. MD5_HASH_InitStructure.HASH_AlgoSelection = HASH_AlgoSelection_MD5;
  164. MD5_HASH_InitStructure.HASH_AlgoMode = HASH_AlgoMode_HMAC;
  165. MD5_HASH_InitStructure.HASH_DataType = HASH_DataType_8b;
  166. if(Keylen > 64)
  167. {
  168. /* HMAC long Key */
  169. MD5_HASH_InitStructure.HASH_HMACKeyType = HASH_HMACKeyType_LongKey;
  170. }
  171. else
  172. {
  173. /* HMAC short Key */
  174. MD5_HASH_InitStructure.HASH_HMACKeyType = HASH_HMACKeyType_ShortKey;
  175. }
  176. HASH_Init(&MD5_HASH_InitStructure);
  177. /* Configure the number of valid bits in last word of the Key */
  178. HASH_SetLastWordValidBitsNbr(nbvalidbitskey);
  179. /* Write the Key */
  180. for(i=0; i<Keylen; i+=4)
  181. {
  182. HASH_DataIn(*(uint32_t*)keyaddr);
  183. keyaddr+=4;
  184. }
  185. /* Start the HASH processor */
  186. HASH_StartDigest();
  187. /* wait until the Busy flag is RESET */
  188. do
  189. {
  190. busystatus = HASH_GetFlagStatus(HASH_FLAG_BUSY);
  191. counter++;
  192. }while ((counter != MD5BUSY_TIMEOUT) && (busystatus != RESET));
  193. if (busystatus != RESET)
  194. {
  195. status = ERROR;
  196. }
  197. else
  198. {
  199. /* Configure the number of valid bits in last word of the Input data */
  200. HASH_SetLastWordValidBitsNbr(nbvalidbitsdata);
  201. /* Write the Input block in the IN FIFO */
  202. for(i=0; i<Ilen; i+=4)
  203. {
  204. HASH_DataIn(*(uint32_t*)inputaddr);
  205. inputaddr+=4;
  206. }
  207. /* Start the HASH processor */
  208. HASH_StartDigest();
  209. /* wait until the Busy flag is RESET */
  210. counter =0;
  211. do
  212. {
  213. busystatus = HASH_GetFlagStatus(HASH_FLAG_BUSY);
  214. counter++;
  215. }while ((counter != MD5BUSY_TIMEOUT) && (busystatus != RESET));
  216. if (busystatus != RESET)
  217. {
  218. status = ERROR;
  219. }
  220. else
  221. {
  222. /* Configure the number of valid bits in last word of the Key */
  223. HASH_SetLastWordValidBitsNbr(nbvalidbitskey);
  224. /* Write the Key */
  225. keyaddr = (uint32_t)Key;
  226. for(i=0; i<Keylen; i+=4)
  227. {
  228. HASH_DataIn(*(uint32_t*)keyaddr);
  229. keyaddr+=4;
  230. }
  231. /* Start the HASH processor */
  232. HASH_StartDigest();
  233. /* wait until the Busy flag is RESET */
  234. counter =0;
  235. do
  236. {
  237. busystatus = HASH_GetFlagStatus(HASH_FLAG_BUSY);
  238. counter++;
  239. }while ((counter != MD5BUSY_TIMEOUT) && (busystatus != RESET));
  240. if (busystatus != RESET)
  241. {
  242. status = ERROR;
  243. }
  244. else
  245. {
  246. /* Read the message digest */
  247. HASH_GetDigest(&MD5_MessageDigest);
  248. *(uint32_t*)(outputaddr) = __REV(MD5_MessageDigest.Data[0]);
  249. outputaddr+=4;
  250. *(uint32_t*)(outputaddr) = __REV(MD5_MessageDigest.Data[1]);
  251. outputaddr+=4;
  252. *(uint32_t*)(outputaddr) = __REV(MD5_MessageDigest.Data[2]);
  253. outputaddr+=4;
  254. *(uint32_t*)(outputaddr) = __REV(MD5_MessageDigest.Data[3]);
  255. }
  256. }
  257. }
  258. return status;
  259. }
  260. /**
  261. * @}
  262. */
  263. /**
  264. * @}
  265. */
  266. /**
  267. * @}
  268. */
  269. /**
  270. * @}
  271. */
  272. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/