CryptHmacMd5.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /** @file
  2. HMAC-MD5 Wrapper Implementation over OpenSSL.
  3. Copyright (c) 2010 - 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_MD5_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-MD5 operations.
  12. (NOTE: This API is deprecated.
  13. Use HmacMd5New() / HmacMd5Free() for HMAC-MD5 Context operations.)
  14. @return The size, in bytes, of the context buffer required for HMAC-MD5 operations.
  15. **/
  16. UINTN
  17. EFIAPI
  18. HmacMd5GetContextSize (
  19. VOID
  20. )
  21. {
  22. //
  23. // Retrieves the OpenSSL HMAC-MD5 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 HmacMd5GetContextSize() in future, and use HmacMd5New()
  27. // and HmacMd5Free() for context allocation and release.
  28. //
  29. return (UINTN) HMAC_MD5_CTX_SIZE;
  30. }
  31. /**
  32. Allocates and initializes one HMAC_CTX context for subsequent HMAC-MD5 use.
  33. @return Pointer to the HMAC_CTX context that has been initialized.
  34. If the allocations fails, HmacMd5New() returns NULL.
  35. **/
  36. VOID *
  37. EFIAPI
  38. HmacMd5New (
  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] HmacMd5Ctx Pointer to the HMAC_CTX context to be released.
  50. **/
  51. VOID
  52. EFIAPI
  53. HmacMd5Free (
  54. IN VOID *HmacMd5Ctx
  55. )
  56. {
  57. //
  58. // Free OpenSSL HMAC_CTX Context
  59. //
  60. HMAC_CTX_free ((HMAC_CTX *)HmacMd5Ctx);
  61. }
  62. /**
  63. Initializes user-supplied memory pointed by HmacMd5Context as HMAC-MD5 context for
  64. subsequent use.
  65. If HmacMd5Context is NULL, then return FALSE.
  66. @param[out] HmacMd5Context Pointer to HMAC-MD5 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-MD5 context initialization succeeded.
  70. @retval FALSE HMAC-MD5 context initialization failed.
  71. **/
  72. BOOLEAN
  73. EFIAPI
  74. HmacMd5Init (
  75. OUT VOID *HmacMd5Context,
  76. IN CONST UINT8 *Key,
  77. IN UINTN KeySize
  78. )
  79. {
  80. //
  81. // Check input parameters.
  82. //
  83. if (HmacMd5Context == NULL || KeySize > INT_MAX) {
  84. return FALSE;
  85. }
  86. //
  87. // OpenSSL HMAC-MD5 Context Initialization
  88. //
  89. memset(HmacMd5Context, 0, HMAC_MD5_CTX_SIZE);
  90. if (HMAC_CTX_reset ((HMAC_CTX *)HmacMd5Context) != 1) {
  91. return FALSE;
  92. }
  93. if (HMAC_Init_ex ((HMAC_CTX *)HmacMd5Context, Key, (UINT32) KeySize, EVP_md5(), NULL) != 1) {
  94. return FALSE;
  95. }
  96. return TRUE;
  97. }
  98. /**
  99. Makes a copy of an existing HMAC-MD5 context.
  100. If HmacMd5Context is NULL, then return FALSE.
  101. If NewHmacMd5Context is NULL, then return FALSE.
  102. @param[in] HmacMd5Context Pointer to HMAC-MD5 context being copied.
  103. @param[out] NewHmacMd5Context Pointer to new HMAC-MD5 context.
  104. @retval TRUE HMAC-MD5 context copy succeeded.
  105. @retval FALSE HMAC-MD5 context copy failed.
  106. **/
  107. BOOLEAN
  108. EFIAPI
  109. HmacMd5Duplicate (
  110. IN CONST VOID *HmacMd5Context,
  111. OUT VOID *NewHmacMd5Context
  112. )
  113. {
  114. //
  115. // Check input parameters.
  116. //
  117. if (HmacMd5Context == NULL || NewHmacMd5Context == NULL) {
  118. return FALSE;
  119. }
  120. if (HMAC_CTX_copy ((HMAC_CTX *)NewHmacMd5Context, (HMAC_CTX *)HmacMd5Context) != 1) {
  121. return FALSE;
  122. }
  123. return TRUE;
  124. }
  125. /**
  126. Digests the input data and updates HMAC-MD5 context.
  127. This function performs HMAC-MD5 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-MD5 context should be already correctly initialized by HmacMd5Init(), and should not be
  130. finalized by HmacMd5Final(). Behavior with invalid context is undefined.
  131. If HmacMd5Context is NULL, then return FALSE.
  132. @param[in, out] HmacMd5Context Pointer to the HMAC-MD5 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-MD5 data digest succeeded.
  136. @retval FALSE HMAC-MD5 data digest failed.
  137. **/
  138. BOOLEAN
  139. EFIAPI
  140. HmacMd5Update (
  141. IN OUT VOID *HmacMd5Context,
  142. IN CONST VOID *Data,
  143. IN UINTN DataSize
  144. )
  145. {
  146. //
  147. // Check input parameters.
  148. //
  149. if (HmacMd5Context == 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-MD5 digest update
  160. //
  161. if (HMAC_Update ((HMAC_CTX *)HmacMd5Context, Data, DataSize) != 1) {
  162. return FALSE;
  163. }
  164. return TRUE;
  165. }
  166. /**
  167. Completes computation of the HMAC-MD5 digest value.
  168. This function completes HMAC-MD5 digest computation and retrieves the digest value into
  169. the specified memory. After this function has been called, the HMAC-MD5 context cannot
  170. be used again.
  171. HMAC-MD5 context should be already correctly initialized by HmacMd5Init(), and should not be
  172. finalized by HmacMd5Final(). Behavior with invalid HMAC-MD5 context is undefined.
  173. If HmacMd5Context is NULL, then return FALSE.
  174. If HmacValue is NULL, then return FALSE.
  175. @param[in, out] HmacMd5Context Pointer to the HMAC-MD5 context.
  176. @param[out] HmacValue Pointer to a buffer that receives the HMAC-MD5 digest
  177. value (16 bytes).
  178. @retval TRUE HMAC-MD5 digest computation succeeded.
  179. @retval FALSE HMAC-MD5 digest computation failed.
  180. **/
  181. BOOLEAN
  182. EFIAPI
  183. HmacMd5Final (
  184. IN OUT VOID *HmacMd5Context,
  185. OUT UINT8 *HmacValue
  186. )
  187. {
  188. UINT32 Length;
  189. //
  190. // Check input parameters.
  191. //
  192. if (HmacMd5Context == NULL || HmacValue == NULL) {
  193. return FALSE;
  194. }
  195. //
  196. // OpenSSL HMAC-MD5 digest finalization
  197. //
  198. if (HMAC_Final ((HMAC_CTX *)HmacMd5Context, HmacValue, &Length) != 1) {
  199. return FALSE;
  200. }
  201. if (HMAC_CTX_reset ((HMAC_CTX *)HmacMd5Context) != 1) {
  202. return FALSE;
  203. }
  204. return TRUE;
  205. }