stm32f4xx_hash_sha1.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hash_sha1.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 SHA1 and
  8. * HMAC SHA1 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 SHA1 Digest using HASH_SHA1() function.
  20. (#) Calculate the HMAC SHA1 Digest using HMAC_SHA1() 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 SHA1BUSY_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_Group6 High Level SHA1 functions
  55. * @brief High Level SHA1 Hash and HMAC functions
  56. *
  57. @verbatim
  58. ===============================================================================
  59. ##### High Level SHA1 Hash and HMAC functions #####
  60. ===============================================================================
  61. @endverbatim
  62. * @{
  63. */
  64. /**
  65. * @brief Compute the HASH SHA1 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_SHA1(uint8_t *Input, uint32_t Ilen, uint8_t Output[20])
  74. {
  75. HASH_InitTypeDef SHA1_HASH_InitStructure;
  76. HASH_MsgDigest SHA1_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. SHA1_HASH_InitStructure.HASH_AlgoSelection = HASH_AlgoSelection_SHA1;
  90. SHA1_HASH_InitStructure.HASH_AlgoMode = HASH_AlgoMode_HASH;
  91. SHA1_HASH_InitStructure.HASH_DataType = HASH_DataType_8b;
  92. HASH_Init(&SHA1_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 != SHA1BUSY_TIMEOUT) && (busystatus != RESET));
  109. if (busystatus != RESET)
  110. {
  111. status = ERROR;
  112. }
  113. else
  114. {
  115. /* Read the message digest */
  116. HASH_GetDigest(&SHA1_MessageDigest);
  117. *(uint32_t*)(outputaddr) = __REV(SHA1_MessageDigest.Data[0]);
  118. outputaddr+=4;
  119. *(uint32_t*)(outputaddr) = __REV(SHA1_MessageDigest.Data[1]);
  120. outputaddr+=4;
  121. *(uint32_t*)(outputaddr) = __REV(SHA1_MessageDigest.Data[2]);
  122. outputaddr+=4;
  123. *(uint32_t*)(outputaddr) = __REV(SHA1_MessageDigest.Data[3]);
  124. outputaddr+=4;
  125. *(uint32_t*)(outputaddr) = __REV(SHA1_MessageDigest.Data[4]);
  126. }
  127. return status;
  128. }
  129. /**
  130. * @brief Compute the HMAC SHA1 digest.
  131. * @param Key: pointer to the Key used for HMAC.
  132. * @param Keylen: length of the Key used for HMAC.
  133. * @param Input: pointer to the Input buffer to be treated.
  134. * @param Ilen: length of the Input buffer.
  135. * @param Output: the returned digest
  136. * @retval An ErrorStatus enumeration value:
  137. * - SUCCESS: digest computation done
  138. * - ERROR: digest computation failed
  139. */
  140. ErrorStatus HMAC_SHA1(uint8_t *Key, uint32_t Keylen, uint8_t *Input,
  141. uint32_t Ilen, uint8_t Output[20])
  142. {
  143. HASH_InitTypeDef SHA1_HASH_InitStructure;
  144. HASH_MsgDigest SHA1_MessageDigest;
  145. __IO uint16_t nbvalidbitsdata = 0;
  146. __IO uint16_t nbvalidbitskey = 0;
  147. uint32_t i = 0;
  148. __IO uint32_t counter = 0;
  149. uint32_t busystatus = 0;
  150. ErrorStatus status = SUCCESS;
  151. uint32_t keyaddr = (uint32_t)Key;
  152. uint32_t inputaddr = (uint32_t)Input;
  153. uint32_t outputaddr = (uint32_t)Output;
  154. /* Number of valid bits in last word of the Input data */
  155. nbvalidbitsdata = 8 * (Ilen % 4);
  156. /* Number of valid bits in last word of the Key */
  157. nbvalidbitskey = 8 * (Keylen % 4);
  158. /* HASH peripheral initialization */
  159. HASH_DeInit();
  160. /* HASH Configuration */
  161. SHA1_HASH_InitStructure.HASH_AlgoSelection = HASH_AlgoSelection_SHA1;
  162. SHA1_HASH_InitStructure.HASH_AlgoMode = HASH_AlgoMode_HMAC;
  163. SHA1_HASH_InitStructure.HASH_DataType = HASH_DataType_8b;
  164. if(Keylen > 64)
  165. {
  166. /* HMAC long Key */
  167. SHA1_HASH_InitStructure.HASH_HMACKeyType = HASH_HMACKeyType_LongKey;
  168. }
  169. else
  170. {
  171. /* HMAC short Key */
  172. SHA1_HASH_InitStructure.HASH_HMACKeyType = HASH_HMACKeyType_ShortKey;
  173. }
  174. HASH_Init(&SHA1_HASH_InitStructure);
  175. /* Configure the number of valid bits in last word of the Key */
  176. HASH_SetLastWordValidBitsNbr(nbvalidbitskey);
  177. /* Write the Key */
  178. for(i=0; i<Keylen; i+=4)
  179. {
  180. HASH_DataIn(*(uint32_t*)keyaddr);
  181. keyaddr+=4;
  182. }
  183. /* Start the HASH processor */
  184. HASH_StartDigest();
  185. /* wait until the Busy flag is RESET */
  186. do
  187. {
  188. busystatus = HASH_GetFlagStatus(HASH_FLAG_BUSY);
  189. counter++;
  190. }while ((counter != SHA1BUSY_TIMEOUT) && (busystatus != RESET));
  191. if (busystatus != RESET)
  192. {
  193. status = ERROR;
  194. }
  195. else
  196. {
  197. /* Configure the number of valid bits in last word of the Input data */
  198. HASH_SetLastWordValidBitsNbr(nbvalidbitsdata);
  199. /* Write the Input block in the IN FIFO */
  200. for(i=0; i<Ilen; i+=4)
  201. {
  202. HASH_DataIn(*(uint32_t*)inputaddr);
  203. inputaddr+=4;
  204. }
  205. /* Start the HASH processor */
  206. HASH_StartDigest();
  207. /* wait until the Busy flag is RESET */
  208. counter =0;
  209. do
  210. {
  211. busystatus = HASH_GetFlagStatus(HASH_FLAG_BUSY);
  212. counter++;
  213. }while ((counter != SHA1BUSY_TIMEOUT) && (busystatus != RESET));
  214. if (busystatus != RESET)
  215. {
  216. status = ERROR;
  217. }
  218. else
  219. {
  220. /* Configure the number of valid bits in last word of the Key */
  221. HASH_SetLastWordValidBitsNbr(nbvalidbitskey);
  222. /* Write the Key */
  223. keyaddr = (uint32_t)Key;
  224. for(i=0; i<Keylen; i+=4)
  225. {
  226. HASH_DataIn(*(uint32_t*)keyaddr);
  227. keyaddr+=4;
  228. }
  229. /* Start the HASH processor */
  230. HASH_StartDigest();
  231. /* wait until the Busy flag is RESET */
  232. counter =0;
  233. do
  234. {
  235. busystatus = HASH_GetFlagStatus(HASH_FLAG_BUSY);
  236. counter++;
  237. }while ((counter != SHA1BUSY_TIMEOUT) && (busystatus != RESET));
  238. if (busystatus != RESET)
  239. {
  240. status = ERROR;
  241. }
  242. else
  243. {
  244. /* Read the message digest */
  245. HASH_GetDigest(&SHA1_MessageDigest);
  246. *(uint32_t*)(outputaddr) = __REV(SHA1_MessageDigest.Data[0]);
  247. outputaddr+=4;
  248. *(uint32_t*)(outputaddr) = __REV(SHA1_MessageDigest.Data[1]);
  249. outputaddr+=4;
  250. *(uint32_t*)(outputaddr) = __REV(SHA1_MessageDigest.Data[2]);
  251. outputaddr+=4;
  252. *(uint32_t*)(outputaddr) = __REV(SHA1_MessageDigest.Data[3]);
  253. outputaddr+=4;
  254. *(uint32_t*)(outputaddr) = __REV(SHA1_MessageDigest.Data[4]);
  255. }
  256. }
  257. }
  258. return status;
  259. }
  260. /**
  261. * @}
  262. */
  263. /**
  264. * @}
  265. */
  266. /**
  267. * @}
  268. */
  269. /**
  270. * @}
  271. */