stm32f4xx_hash_md5.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hash_md5.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 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. * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  30. *
  31. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  32. * You may not use this file except in compliance with the License.
  33. * You may obtain a copy of the License at:
  34. *
  35. * http://www.st.com/software_license_agreement_liberty_v2
  36. *
  37. * Unless required by applicable law or agreed to in writing, software
  38. * distributed under the License is distributed on an "AS IS" BASIS,
  39. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  40. * See the License for the specific language governing permissions and
  41. * limitations under the License.
  42. *
  43. ******************************************************************************
  44. */
  45. /* Includes ------------------------------------------------------------------*/
  46. #include "stm32f4xx_hash.h"
  47. /** @addtogroup STM32F4xx_StdPeriph_Driver
  48. * @{
  49. */
  50. /** @defgroup HASH
  51. * @brief HASH driver modules
  52. * @{
  53. */
  54. /* Private typedef -----------------------------------------------------------*/
  55. /* Private define ------------------------------------------------------------*/
  56. #define MD5BUSY_TIMEOUT ((uint32_t) 0x00010000)
  57. /* Private macro -------------------------------------------------------------*/
  58. /* Private variables ---------------------------------------------------------*/
  59. /* Private function prototypes -----------------------------------------------*/
  60. /* Private functions ---------------------------------------------------------*/
  61. /** @defgroup HASH_Private_Functions
  62. * @{
  63. */
  64. /** @defgroup HASH_Group7 High Level MD5 functions
  65. * @brief High Level MD5 Hash and HMAC functions
  66. *
  67. @verbatim
  68. ===============================================================================
  69. High Level MD5 Hash and HMAC functions
  70. ===============================================================================
  71. @endverbatim
  72. * @{
  73. */
  74. /**
  75. * @brief Compute the HASH MD5 digest.
  76. * @param Input: pointer to the Input buffer to be treated.
  77. * @param Ilen: length of the Input buffer.
  78. * @param Output: the returned digest
  79. * @retval An ErrorStatus enumeration value:
  80. * - SUCCESS: digest computation done
  81. * - ERROR: digest computation failed
  82. */
  83. ErrorStatus HASH_MD5(uint8_t *Input, uint32_t Ilen, uint8_t Output[16])
  84. {
  85. HASH_InitTypeDef MD5_HASH_InitStructure;
  86. HASH_MsgDigest MD5_MessageDigest;
  87. __IO uint16_t nbvalidbitsdata = 0;
  88. uint32_t i = 0;
  89. __IO uint32_t counter = 0;
  90. uint32_t busystatus = 0;
  91. ErrorStatus status = SUCCESS;
  92. uint32_t inputaddr = (uint32_t)Input;
  93. uint32_t outputaddr = (uint32_t)Output;
  94. /* Number of valid bits in last word of the Input data */
  95. nbvalidbitsdata = 8 * (Ilen % 4);
  96. /* HASH peripheral initialization */
  97. HASH_DeInit();
  98. /* HASH Configuration */
  99. MD5_HASH_InitStructure.HASH_AlgoSelection = HASH_AlgoSelection_MD5;
  100. MD5_HASH_InitStructure.HASH_AlgoMode = HASH_AlgoMode_HASH;
  101. MD5_HASH_InitStructure.HASH_DataType = HASH_DataType_8b;
  102. HASH_Init(&MD5_HASH_InitStructure);
  103. /* Configure the number of valid bits in last word of the data */
  104. HASH_SetLastWordValidBitsNbr(nbvalidbitsdata);
  105. /* Write the Input block in the IN FIFO */
  106. for(i=0; i<Ilen; i+=4)
  107. {
  108. HASH_DataIn(*(uint32_t*)inputaddr);
  109. inputaddr+=4;
  110. }
  111. /* Start the HASH processor */
  112. HASH_StartDigest();
  113. /* wait until the Busy flag is RESET */
  114. do
  115. {
  116. busystatus = HASH_GetFlagStatus(HASH_FLAG_BUSY);
  117. counter++;
  118. }while ((counter != MD5BUSY_TIMEOUT) && (busystatus != RESET));
  119. if (busystatus != RESET)
  120. {
  121. status = ERROR;
  122. }
  123. else
  124. {
  125. /* Read the message digest */
  126. HASH_GetDigest(&MD5_MessageDigest);
  127. *(uint32_t*)(outputaddr) = __REV(MD5_MessageDigest.Data[0]);
  128. outputaddr+=4;
  129. *(uint32_t*)(outputaddr) = __REV(MD5_MessageDigest.Data[1]);
  130. outputaddr+=4;
  131. *(uint32_t*)(outputaddr) = __REV(MD5_MessageDigest.Data[2]);
  132. outputaddr+=4;
  133. *(uint32_t*)(outputaddr) = __REV(MD5_MessageDigest.Data[3]);
  134. }
  135. return status;
  136. }
  137. /**
  138. * @brief Compute the HMAC MD5 digest.
  139. * @param Key: pointer to the Key used for HMAC.
  140. * @param Keylen: length of the Key used for HMAC.
  141. * @param Input: pointer to the Input buffer to be treated.
  142. * @param Ilen: length of the Input buffer.
  143. * @param Output: the returned digest
  144. * @retval An ErrorStatus enumeration value:
  145. * - SUCCESS: digest computation done
  146. * - ERROR: digest computation failed
  147. */
  148. ErrorStatus HMAC_MD5(uint8_t *Key, uint32_t Keylen, uint8_t *Input,
  149. uint32_t Ilen, uint8_t Output[16])
  150. {
  151. HASH_InitTypeDef MD5_HASH_InitStructure;
  152. HASH_MsgDigest MD5_MessageDigest;
  153. __IO uint16_t nbvalidbitsdata = 0;
  154. __IO uint16_t nbvalidbitskey = 0;
  155. uint32_t i = 0;
  156. __IO uint32_t counter = 0;
  157. uint32_t busystatus = 0;
  158. ErrorStatus status = SUCCESS;
  159. uint32_t keyaddr = (uint32_t)Key;
  160. uint32_t inputaddr = (uint32_t)Input;
  161. uint32_t outputaddr = (uint32_t)Output;
  162. /* Number of valid bits in last word of the Input data */
  163. nbvalidbitsdata = 8 * (Ilen % 4);
  164. /* Number of valid bits in last word of the Key */
  165. nbvalidbitskey = 8 * (Keylen % 4);
  166. /* HASH peripheral initialization */
  167. HASH_DeInit();
  168. /* HASH Configuration */
  169. MD5_HASH_InitStructure.HASH_AlgoSelection = HASH_AlgoSelection_MD5;
  170. MD5_HASH_InitStructure.HASH_AlgoMode = HASH_AlgoMode_HMAC;
  171. MD5_HASH_InitStructure.HASH_DataType = HASH_DataType_8b;
  172. if(Keylen > 64)
  173. {
  174. /* HMAC long Key */
  175. MD5_HASH_InitStructure.HASH_HMACKeyType = HASH_HMACKeyType_LongKey;
  176. }
  177. else
  178. {
  179. /* HMAC short Key */
  180. MD5_HASH_InitStructure.HASH_HMACKeyType = HASH_HMACKeyType_ShortKey;
  181. }
  182. HASH_Init(&MD5_HASH_InitStructure);
  183. /* Configure the number of valid bits in last word of the Key */
  184. HASH_SetLastWordValidBitsNbr(nbvalidbitskey);
  185. /* Write the Key */
  186. for(i=0; i<Keylen; i+=4)
  187. {
  188. HASH_DataIn(*(uint32_t*)keyaddr);
  189. keyaddr+=4;
  190. }
  191. /* Start the HASH processor */
  192. HASH_StartDigest();
  193. /* wait until the Busy flag is RESET */
  194. do
  195. {
  196. busystatus = HASH_GetFlagStatus(HASH_FLAG_BUSY);
  197. counter++;
  198. }while ((counter != MD5BUSY_TIMEOUT) && (busystatus != RESET));
  199. if (busystatus != RESET)
  200. {
  201. status = ERROR;
  202. }
  203. else
  204. {
  205. /* Configure the number of valid bits in last word of the Input data */
  206. HASH_SetLastWordValidBitsNbr(nbvalidbitsdata);
  207. /* Write the Input block in the IN FIFO */
  208. for(i=0; i<Ilen; i+=4)
  209. {
  210. HASH_DataIn(*(uint32_t*)inputaddr);
  211. inputaddr+=4;
  212. }
  213. /* Start the HASH processor */
  214. HASH_StartDigest();
  215. /* wait until the Busy flag is RESET */
  216. counter =0;
  217. do
  218. {
  219. busystatus = HASH_GetFlagStatus(HASH_FLAG_BUSY);
  220. counter++;
  221. }while ((counter != MD5BUSY_TIMEOUT) && (busystatus != RESET));
  222. if (busystatus != RESET)
  223. {
  224. status = ERROR;
  225. }
  226. else
  227. {
  228. /* Configure the number of valid bits in last word of the Key */
  229. HASH_SetLastWordValidBitsNbr(nbvalidbitskey);
  230. /* Write the Key */
  231. keyaddr = (uint32_t)Key;
  232. for(i=0; i<Keylen; i+=4)
  233. {
  234. HASH_DataIn(*(uint32_t*)keyaddr);
  235. keyaddr+=4;
  236. }
  237. /* Start the HASH processor */
  238. HASH_StartDigest();
  239. /* wait until the Busy flag is RESET */
  240. counter =0;
  241. do
  242. {
  243. busystatus = HASH_GetFlagStatus(HASH_FLAG_BUSY);
  244. counter++;
  245. }while ((counter != MD5BUSY_TIMEOUT) && (busystatus != RESET));
  246. if (busystatus != RESET)
  247. {
  248. status = ERROR;
  249. }
  250. else
  251. {
  252. /* Read the message digest */
  253. HASH_GetDigest(&MD5_MessageDigest);
  254. *(uint32_t*)(outputaddr) = __REV(MD5_MessageDigest.Data[0]);
  255. outputaddr+=4;
  256. *(uint32_t*)(outputaddr) = __REV(MD5_MessageDigest.Data[1]);
  257. outputaddr+=4;
  258. *(uint32_t*)(outputaddr) = __REV(MD5_MessageDigest.Data[2]);
  259. outputaddr+=4;
  260. *(uint32_t*)(outputaddr) = __REV(MD5_MessageDigest.Data[3]);
  261. }
  262. }
  263. }
  264. return status;
  265. }
  266. /**
  267. * @}
  268. */
  269. /**
  270. * @}
  271. */
  272. /**
  273. * @}
  274. */
  275. /**
  276. * @}
  277. */
  278. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/