stm32f4xx_hash_sha1.c 9.5 KB

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