CryptSha1.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /** @file
  2. SHA-1 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/sha.h>
  8. /**
  9. Retrieves the size, in bytes, of the context buffer required for SHA-1 hash operations.
  10. @return The size, in bytes, of the context buffer required for SHA-1 hash operations.
  11. **/
  12. UINTN
  13. EFIAPI
  14. Sha1GetContextSize (
  15. VOID
  16. )
  17. {
  18. //
  19. // Retrieves OpenSSL SHA Context Size
  20. //
  21. return (UINTN) (sizeof (SHA_CTX));
  22. }
  23. /**
  24. Initializes user-supplied memory pointed by Sha1Context as SHA-1 hash context for
  25. subsequent use.
  26. If Sha1Context is NULL, then return FALSE.
  27. @param[out] Sha1Context Pointer to SHA-1 context being initialized.
  28. @retval TRUE SHA-1 context initialization succeeded.
  29. @retval FALSE SHA-1 context initialization failed.
  30. **/
  31. BOOLEAN
  32. EFIAPI
  33. Sha1Init (
  34. OUT VOID *Sha1Context
  35. )
  36. {
  37. //
  38. // Check input parameters.
  39. //
  40. if (Sha1Context == NULL) {
  41. return FALSE;
  42. }
  43. //
  44. // OpenSSL SHA-1 Context Initialization
  45. //
  46. return (BOOLEAN) (SHA1_Init ((SHA_CTX *) Sha1Context));
  47. }
  48. /**
  49. Makes a copy of an existing SHA-1 context.
  50. If Sha1Context is NULL, then return FALSE.
  51. If NewSha1Context is NULL, then return FALSE.
  52. @param[in] Sha1Context Pointer to SHA-1 context being copied.
  53. @param[out] NewSha1Context Pointer to new SHA-1 context.
  54. @retval TRUE SHA-1 context copy succeeded.
  55. @retval FALSE SHA-1 context copy failed.
  56. **/
  57. BOOLEAN
  58. EFIAPI
  59. Sha1Duplicate (
  60. IN CONST VOID *Sha1Context,
  61. OUT VOID *NewSha1Context
  62. )
  63. {
  64. //
  65. // Check input parameters.
  66. //
  67. if (Sha1Context == NULL || NewSha1Context == NULL) {
  68. return FALSE;
  69. }
  70. CopyMem (NewSha1Context, Sha1Context, sizeof (SHA_CTX));
  71. return TRUE;
  72. }
  73. /**
  74. Digests the input data and updates SHA-1 context.
  75. This function performs SHA-1 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. SHA-1 context should be already correctly initialized by Sha1Init(), and should not be finalized
  78. by Sha1Final(). Behavior with invalid context is undefined.
  79. If Sha1Context is NULL, then return FALSE.
  80. @param[in, out] Sha1Context Pointer to the SHA-1 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 SHA-1 data digest succeeded.
  84. @retval FALSE SHA-1 data digest failed.
  85. **/
  86. BOOLEAN
  87. EFIAPI
  88. Sha1Update (
  89. IN OUT VOID *Sha1Context,
  90. IN CONST VOID *Data,
  91. IN UINTN DataSize
  92. )
  93. {
  94. //
  95. // Check input parameters.
  96. //
  97. if (Sha1Context == 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 SHA-1 Hash Update
  108. //
  109. return (BOOLEAN) (SHA1_Update ((SHA_CTX *) Sha1Context, Data, DataSize));
  110. }
  111. /**
  112. Completes computation of the SHA-1 digest value.
  113. This function completes SHA-1 hash computation and retrieves the digest value into
  114. the specified memory. After this function has been called, the SHA-1 context cannot
  115. be used again.
  116. SHA-1 context should be already correctly initialized by Sha1Init(), and should not be
  117. finalized by Sha1Final(). Behavior with invalid SHA-1 context is undefined.
  118. If Sha1Context is NULL, then return FALSE.
  119. If HashValue is NULL, then return FALSE.
  120. @param[in, out] Sha1Context Pointer to the SHA-1 context.
  121. @param[out] HashValue Pointer to a buffer that receives the SHA-1 digest
  122. value (20 bytes).
  123. @retval TRUE SHA-1 digest computation succeeded.
  124. @retval FALSE SHA-1 digest computation failed.
  125. **/
  126. BOOLEAN
  127. EFIAPI
  128. Sha1Final (
  129. IN OUT VOID *Sha1Context,
  130. OUT UINT8 *HashValue
  131. )
  132. {
  133. //
  134. // Check input parameters.
  135. //
  136. if (Sha1Context == NULL || HashValue == NULL) {
  137. return FALSE;
  138. }
  139. //
  140. // OpenSSL SHA-1 Hash Finalization
  141. //
  142. return (BOOLEAN) (SHA1_Final (HashValue, (SHA_CTX *) Sha1Context));
  143. }
  144. /**
  145. Computes the SHA-1 message digest of a input data buffer.
  146. This function performs the SHA-1 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 SHA-1 digest
  152. value (20 bytes).
  153. @retval TRUE SHA-1 digest computation succeeded.
  154. @retval FALSE SHA-1 digest computation failed.
  155. @retval FALSE This interface is not supported.
  156. **/
  157. BOOLEAN
  158. EFIAPI
  159. Sha1HashAll (
  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 SHA-1 Hash Computation.
  176. //
  177. if (SHA1 (Data, DataSize, HashValue) == NULL) {
  178. return FALSE;
  179. } else {
  180. return TRUE;
  181. }
  182. }