PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/edk2/CryptoPkg/Library/BaseCryptLib/Hash/CryptMd5.c

https://gitlab.com/envieidoc/Clover
C | 185 lines | 62 code | 14 blank | 109 comment | 16 complexity | aa6eb885aba0a29ada361df6c9bc3d99 MD5 | raw file
  1. /** @file
  2. MD5 Digest Wrapper Implementation over OpenSSL.
  3. Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
  4. This program and the accompanying materials
  5. are licensed and made available under the terms and conditions of the BSD License
  6. which accompanies this distribution. The full text of the license may be found at
  7. http://opensource.org/licenses/bsd-license.php
  8. THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
  10. **/
  11. #include "InternalCryptLib.h"
  12. #include <openssl/md5.h>
  13. /**
  14. Retrieves the size, in bytes, of the context buffer required for MD5 hash operations.
  15. @return The size, in bytes, of the context buffer required for MD5 hash operations.
  16. **/
  17. UINTN
  18. EFIAPI
  19. Md5GetContextSize (
  20. VOID
  21. )
  22. {
  23. //
  24. // Retrieves the OpenSSL MD5 Context Size
  25. //
  26. return (UINTN) (sizeof (MD5_CTX));
  27. }
  28. /**
  29. Initializes user-supplied memory pointed by Md5Context as MD5 hash context for
  30. subsequent use.
  31. If Md5Context is NULL, then return FALSE.
  32. @param[out] Md5Context Pointer to MD5 context being initialized.
  33. @retval TRUE MD5 context initialization succeeded.
  34. @retval FALSE MD5 context initialization failed.
  35. **/
  36. BOOLEAN
  37. EFIAPI
  38. Md5Init (
  39. OUT VOID *Md5Context
  40. )
  41. {
  42. //
  43. // Check input parameters.
  44. //
  45. if (Md5Context == NULL) {
  46. return FALSE;
  47. }
  48. //
  49. // OpenSSL MD5 Context Initialization
  50. //
  51. return (BOOLEAN) (MD5_Init ((MD5_CTX *) Md5Context));
  52. }
  53. /**
  54. Makes a copy of an existing MD5 context.
  55. If Md5Context is NULL, then return FALSE.
  56. If NewMd5Context is NULL, then return FALSE.
  57. @param[in] Md5Context Pointer to MD5 context being copied.
  58. @param[out] NewMd5Context Pointer to new MD5 context.
  59. @retval TRUE MD5 context copy succeeded.
  60. @retval FALSE MD5 context copy failed.
  61. **/
  62. BOOLEAN
  63. EFIAPI
  64. Md5Duplicate (
  65. IN CONST VOID *Md5Context,
  66. OUT VOID *NewMd5Context
  67. )
  68. {
  69. //
  70. // Check input parameters.
  71. //
  72. if (Md5Context == NULL || NewMd5Context == NULL) {
  73. return FALSE;
  74. }
  75. CopyMem (NewMd5Context, Md5Context, sizeof (MD5_CTX));
  76. return TRUE;
  77. }
  78. /**
  79. Digests the input data and updates MD5 context.
  80. This function performs MD5 digest on a data buffer of the specified size.
  81. It can be called multiple times to compute the digest of long or discontinuous data streams.
  82. MD5 context should be already correctly intialized by Md5Init(), and should not be finalized
  83. by Md5Final(). Behavior with invalid context is undefined.
  84. If Md5Context is NULL, then return FALSE.
  85. @param[in, out] Md5Context Pointer to the MD5 context.
  86. @param[in] Data Pointer to the buffer containing the data to be hashed.
  87. @param[in] DataSize Size of Data buffer in bytes.
  88. @retval TRUE MD5 data digest succeeded.
  89. @retval FALSE MD5 data digest failed.
  90. **/
  91. BOOLEAN
  92. EFIAPI
  93. Md5Update (
  94. IN OUT VOID *Md5Context,
  95. IN CONST VOID *Data,
  96. IN UINTN DataSize
  97. )
  98. {
  99. //
  100. // Check input parameters.
  101. //
  102. if (Md5Context == NULL) {
  103. return FALSE;
  104. }
  105. //
  106. // Check invalid parameters, in case that only DataLength was checked in OpenSSL
  107. //
  108. if (Data == NULL && (DataSize != 0)) {
  109. return FALSE;
  110. }
  111. //
  112. // OpenSSL MD5 Hash Update
  113. //
  114. return (BOOLEAN) (MD5_Update ((MD5_CTX *) Md5Context, Data, DataSize));
  115. }
  116. /**
  117. Completes computation of the MD5 digest value.
  118. This function completes MD5 hash computation and retrieves the digest value into
  119. the specified memory. After this function has been called, the MD5 context cannot
  120. be used again.
  121. MD5 context should be already correctly intialized by Md5Init(), and should not be
  122. finalized by Md5Final(). Behavior with invalid MD5 context is undefined.
  123. If Md5Context is NULL, then return FALSE.
  124. If HashValue is NULL, then return FALSE.
  125. @param[in, out] Md5Context Pointer to the MD5 context.
  126. @param[out] HashValue Pointer to a buffer that receives the MD5 digest
  127. value (16 bytes).
  128. @retval TRUE MD5 digest computation succeeded.
  129. @retval FALSE MD5 digest computation failed.
  130. **/
  131. BOOLEAN
  132. EFIAPI
  133. Md5Final (
  134. IN OUT VOID *Md5Context,
  135. OUT UINT8 *HashValue
  136. )
  137. {
  138. //
  139. // Check input parameters.
  140. //
  141. if (Md5Context == NULL || HashValue == NULL) {
  142. return FALSE;
  143. }
  144. //
  145. // OpenSSL MD5 Hash Finalization
  146. //
  147. return (BOOLEAN) (MD5_Final (HashValue, (MD5_CTX *) Md5Context));
  148. }