CryptSha256.c 5.6 KB

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