CryptHmacSha256.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /** @file
  2. HMAC-SHA256 Wrapper Implementation over OpenSSL.
  3. Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "InternalCryptLib.h"
  7. #include <openssl/hmac.h>
  8. #define HMAC_SHA256_CTX_SIZE sizeof(void *) * 4 + sizeof(unsigned int) + \
  9. sizeof(unsigned char) * HMAC_MAX_MD_CBLOCK
  10. /**
  11. Retrieves the size, in bytes, of the context buffer required for HMAC-SHA256 operations.
  12. (NOTE: This API is deprecated.
  13. Use HmacSha256New() / HmacSha256Free() for HMAC-SHA256 Context operations.)
  14. @return The size, in bytes, of the context buffer required for HMAC-SHA256 operations.
  15. **/
  16. UINTN
  17. EFIAPI
  18. HmacSha256GetContextSize (
  19. VOID
  20. )
  21. {
  22. //
  23. // Retrieves the OpenSSL HMAC-SHA256 Context Size
  24. // NOTE: HMAC_CTX object was made opaque in openssl-1.1.x, here we just use the
  25. // fixed size as a workaround to make this API work for compatibility.
  26. // We should retire HmacSha256GetContextSize() in future, and use HmacSha256New()
  27. // and HmacSha256Free() for context allocation and release.
  28. //
  29. return (UINTN)HMAC_SHA256_CTX_SIZE;
  30. }
  31. /**
  32. Allocates and initializes one HMAC_CTX context for subsequent HMAC-SHA256 use.
  33. @return Pointer to the HMAC_CTX context that has been initialized.
  34. If the allocations fails, HmacSha256New() returns NULL.
  35. **/
  36. VOID *
  37. EFIAPI
  38. HmacSha256New (
  39. VOID
  40. )
  41. {
  42. //
  43. // Allocates & Initializes HMAC_CTX Context by OpenSSL HMAC_CTX_new()
  44. //
  45. return (VOID *) HMAC_CTX_new ();
  46. }
  47. /**
  48. Release the specified HMAC_CTX context.
  49. @param[in] HmacSha256Ctx Pointer to the HMAC_CTX context to be released.
  50. **/
  51. VOID
  52. EFIAPI
  53. HmacSha256Free (
  54. IN VOID *HmacSha256Ctx
  55. )
  56. {
  57. //
  58. // Free OpenSSL HMAC_CTX Context
  59. //
  60. HMAC_CTX_free ((HMAC_CTX *)HmacSha256Ctx);
  61. }
  62. /**
  63. Initializes user-supplied memory pointed by HmacSha256Context as HMAC-SHA256 context for
  64. subsequent use.
  65. If HmacSha256Context is NULL, then return FALSE.
  66. @param[out] HmacSha256Context Pointer to HMAC-SHA256 context being initialized.
  67. @param[in] Key Pointer to the user-supplied key.
  68. @param[in] KeySize Key size in bytes.
  69. @retval TRUE HMAC-SHA256 context initialization succeeded.
  70. @retval FALSE HMAC-SHA256 context initialization failed.
  71. **/
  72. BOOLEAN
  73. EFIAPI
  74. HmacSha256Init (
  75. OUT VOID *HmacSha256Context,
  76. IN CONST UINT8 *Key,
  77. IN UINTN KeySize
  78. )
  79. {
  80. //
  81. // Check input parameters.
  82. //
  83. if (HmacSha256Context == NULL || KeySize > INT_MAX) {
  84. return FALSE;
  85. }
  86. //
  87. // OpenSSL HMAC-SHA256 Context Initialization
  88. //
  89. memset(HmacSha256Context, 0, HMAC_SHA256_CTX_SIZE);
  90. if (HMAC_CTX_reset ((HMAC_CTX *)HmacSha256Context) != 1) {
  91. return FALSE;
  92. }
  93. if (HMAC_Init_ex ((HMAC_CTX *)HmacSha256Context, Key, (UINT32) KeySize, EVP_sha256(), NULL) != 1) {
  94. return FALSE;
  95. }
  96. return TRUE;
  97. }
  98. /**
  99. Makes a copy of an existing HMAC-SHA256 context.
  100. If HmacSha256Context is NULL, then return FALSE.
  101. If NewHmacSha256Context is NULL, then return FALSE.
  102. @param[in] HmacSha256Context Pointer to HMAC-SHA256 context being copied.
  103. @param[out] NewHmacSha256Context Pointer to new HMAC-SHA256 context.
  104. @retval TRUE HMAC-SHA256 context copy succeeded.
  105. @retval FALSE HMAC-SHA256 context copy failed.
  106. **/
  107. BOOLEAN
  108. EFIAPI
  109. HmacSha256Duplicate (
  110. IN CONST VOID *HmacSha256Context,
  111. OUT VOID *NewHmacSha256Context
  112. )
  113. {
  114. //
  115. // Check input parameters.
  116. //
  117. if (HmacSha256Context == NULL || NewHmacSha256Context == NULL) {
  118. return FALSE;
  119. }
  120. if (HMAC_CTX_copy ((HMAC_CTX *)NewHmacSha256Context, (HMAC_CTX *)HmacSha256Context) != 1) {
  121. return FALSE;
  122. }
  123. return TRUE;
  124. }
  125. /**
  126. Digests the input data and updates HMAC-SHA256 context.
  127. This function performs HMAC-SHA256 digest on a data buffer of the specified size.
  128. It can be called multiple times to compute the digest of long or discontinuous data streams.
  129. HMAC-SHA256 context should be already correctly initialized by HmacSha256Init(), and should not
  130. be finalized by HmacSha256Final(). Behavior with invalid context is undefined.
  131. If HmacSha256Context is NULL, then return FALSE.
  132. @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.
  133. @param[in] Data Pointer to the buffer containing the data to be digested.
  134. @param[in] DataSize Size of Data buffer in bytes.
  135. @retval TRUE HMAC-SHA256 data digest succeeded.
  136. @retval FALSE HMAC-SHA256 data digest failed.
  137. **/
  138. BOOLEAN
  139. EFIAPI
  140. HmacSha256Update (
  141. IN OUT VOID *HmacSha256Context,
  142. IN CONST VOID *Data,
  143. IN UINTN DataSize
  144. )
  145. {
  146. //
  147. // Check input parameters.
  148. //
  149. if (HmacSha256Context == NULL) {
  150. return FALSE;
  151. }
  152. //
  153. // Check invalid parameters, in case that only DataLength was checked in OpenSSL
  154. //
  155. if (Data == NULL && DataSize != 0) {
  156. return FALSE;
  157. }
  158. //
  159. // OpenSSL HMAC-SHA256 digest update
  160. //
  161. if (HMAC_Update ((HMAC_CTX *)HmacSha256Context, Data, DataSize) != 1) {
  162. return FALSE;
  163. }
  164. return TRUE;
  165. }
  166. /**
  167. Completes computation of the HMAC-SHA256 digest value.
  168. This function completes HMAC-SHA256 hash computation and retrieves the digest value into
  169. the specified memory. After this function has been called, the HMAC-SHA256 context cannot
  170. be used again.
  171. HMAC-SHA256 context should be already correctly initialized by HmacSha256Init(), and should
  172. not be finalized by HmacSha256Final(). Behavior with invalid HMAC-SHA256 context is undefined.
  173. If HmacSha256Context is NULL, then return FALSE.
  174. If HmacValue is NULL, then return FALSE.
  175. @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.
  176. @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA256 digest
  177. value (32 bytes).
  178. @retval TRUE HMAC-SHA256 digest computation succeeded.
  179. @retval FALSE HMAC-SHA256 digest computation failed.
  180. **/
  181. BOOLEAN
  182. EFIAPI
  183. HmacSha256Final (
  184. IN OUT VOID *HmacSha256Context,
  185. OUT UINT8 *HmacValue
  186. )
  187. {
  188. UINT32 Length;
  189. //
  190. // Check input parameters.
  191. //
  192. if (HmacSha256Context == NULL || HmacValue == NULL) {
  193. return FALSE;
  194. }
  195. //
  196. // OpenSSL HMAC-SHA256 digest finalization
  197. //
  198. if (HMAC_Final ((HMAC_CTX *)HmacSha256Context, HmacValue, &Length) != 1) {
  199. return FALSE;
  200. }
  201. if (HMAC_CTX_reset ((HMAC_CTX *)HmacSha256Context) != 1) {
  202. return FALSE;
  203. }
  204. return TRUE;
  205. }