CryptMd5.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /** @file
  2. MD5 Digest Wrapper Implementation over OpenSSL.
  3. Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "InternalCryptLib.h"
  7. #include <openssl/md5.h>
  8. /**
  9. Retrieves the size, in bytes, of the context buffer required for MD5 hash operations.
  10. @return The size, in bytes, of the context buffer required for MD5 hash operations.
  11. **/
  12. UINTN
  13. EFIAPI
  14. Md5GetContextSize (
  15. VOID
  16. )
  17. {
  18. //
  19. // Retrieves the OpenSSL MD5 Context Size
  20. //
  21. return (UINTN) (sizeof (MD5_CTX));
  22. }
  23. /**
  24. Initializes user-supplied memory pointed by Md5Context as MD5 hash context for
  25. subsequent use.
  26. If Md5Context is NULL, then return FALSE.
  27. @param[out] Md5Context Pointer to MD5 context being initialized.
  28. @retval TRUE MD5 context initialization succeeded.
  29. @retval FALSE MD5 context initialization failed.
  30. **/
  31. BOOLEAN
  32. EFIAPI
  33. Md5Init (
  34. OUT VOID *Md5Context
  35. )
  36. {
  37. //
  38. // Check input parameters.
  39. //
  40. if (Md5Context == NULL) {
  41. return FALSE;
  42. }
  43. //
  44. // OpenSSL MD5 Context Initialization
  45. //
  46. return (BOOLEAN) (MD5_Init ((MD5_CTX *) Md5Context));
  47. }
  48. /**
  49. Makes a copy of an existing MD5 context.
  50. If Md5Context is NULL, then return FALSE.
  51. If NewMd5Context is NULL, then return FALSE.
  52. @param[in] Md5Context Pointer to MD5 context being copied.
  53. @param[out] NewMd5Context Pointer to new MD5 context.
  54. @retval TRUE MD5 context copy succeeded.
  55. @retval FALSE MD5 context copy failed.
  56. **/
  57. BOOLEAN
  58. EFIAPI
  59. Md5Duplicate (
  60. IN CONST VOID *Md5Context,
  61. OUT VOID *NewMd5Context
  62. )
  63. {
  64. //
  65. // Check input parameters.
  66. //
  67. if (Md5Context == NULL || NewMd5Context == NULL) {
  68. return FALSE;
  69. }
  70. CopyMem (NewMd5Context, Md5Context, sizeof (MD5_CTX));
  71. return TRUE;
  72. }
  73. /**
  74. Digests the input data and updates MD5 context.
  75. This function performs MD5 digest on a data buffer of the specified size.
  76. It can be called multiple times to compute the digest of long or discontinuous data streams.
  77. MD5 context should be already correctly initialized by Md5Init(), and should not be finalized
  78. by Md5Final(). Behavior with invalid context is undefined.
  79. If Md5Context is NULL, then return FALSE.
  80. @param[in, out] Md5Context Pointer to the MD5 context.
  81. @param[in] Data Pointer to the buffer containing the data to be hashed.
  82. @param[in] DataSize Size of Data buffer in bytes.
  83. @retval TRUE MD5 data digest succeeded.
  84. @retval FALSE MD5 data digest failed.
  85. **/
  86. BOOLEAN
  87. EFIAPI
  88. Md5Update (
  89. IN OUT VOID *Md5Context,
  90. IN CONST VOID *Data,
  91. IN UINTN DataSize
  92. )
  93. {
  94. //
  95. // Check input parameters.
  96. //
  97. if (Md5Context == NULL) {
  98. return FALSE;
  99. }
  100. //
  101. // Check invalid parameters, in case that only DataLength was checked in OpenSSL
  102. //
  103. if (Data == NULL && (DataSize != 0)) {
  104. return FALSE;
  105. }
  106. //
  107. // OpenSSL MD5 Hash Update
  108. //
  109. return (BOOLEAN) (MD5_Update ((MD5_CTX *) Md5Context, Data, DataSize));
  110. }
  111. /**
  112. Completes computation of the MD5 digest value.
  113. This function completes MD5 hash computation and retrieves the digest value into
  114. the specified memory. After this function has been called, the MD5 context cannot
  115. be used again.
  116. MD5 context should be already correctly initialized by Md5Init(), and should not be
  117. finalized by Md5Final(). Behavior with invalid MD5 context is undefined.
  118. If Md5Context is NULL, then return FALSE.
  119. If HashValue is NULL, then return FALSE.
  120. @param[in, out] Md5Context Pointer to the MD5 context.
  121. @param[out] HashValue Pointer to a buffer that receives the MD5 digest
  122. value (16 bytes).
  123. @retval TRUE MD5 digest computation succeeded.
  124. @retval FALSE MD5 digest computation failed.
  125. **/
  126. BOOLEAN
  127. EFIAPI
  128. Md5Final (
  129. IN OUT VOID *Md5Context,
  130. OUT UINT8 *HashValue
  131. )
  132. {
  133. //
  134. // Check input parameters.
  135. //
  136. if (Md5Context == NULL || HashValue == NULL) {
  137. return FALSE;
  138. }
  139. //
  140. // OpenSSL MD5 Hash Finalization
  141. //
  142. return (BOOLEAN) (MD5_Final (HashValue, (MD5_CTX *) Md5Context));
  143. }
  144. /**
  145. Computes the MD5 message digest of a input data buffer.
  146. This function performs the MD5 message digest of a given data buffer, and places
  147. the digest value into the specified memory.
  148. If this interface is not supported, then return FALSE.
  149. @param[in] Data Pointer to the buffer containing the data to be hashed.
  150. @param[in] DataSize Size of Data buffer in bytes.
  151. @param[out] HashValue Pointer to a buffer that receives the MD5 digest
  152. value (16 bytes).
  153. @retval TRUE MD5 digest computation succeeded.
  154. @retval FALSE MD5 digest computation failed.
  155. @retval FALSE This interface is not supported.
  156. **/
  157. BOOLEAN
  158. EFIAPI
  159. Md5HashAll (
  160. IN CONST VOID *Data,
  161. IN UINTN DataSize,
  162. OUT UINT8 *HashValue
  163. )
  164. {
  165. //
  166. // Check input parameters.
  167. //
  168. if (HashValue == NULL) {
  169. return FALSE;
  170. }
  171. if (Data == NULL && (DataSize != 0)) {
  172. return FALSE;
  173. }
  174. //
  175. // OpenSSL MD5 Hash Computation.
  176. //
  177. if (MD5 (Data, DataSize, HashValue) == NULL) {
  178. return FALSE;
  179. } else {
  180. return TRUE;
  181. }
  182. }