PageRenderTime 113ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://gitlab.com/envieidoc/Clover
C | 184 lines | 62 code | 13 blank | 109 comment | 16 complexity | 0cd57092a362433726ba2a938cec36b7 MD5 | raw file
  1. /** @file
  2. SHA-1 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/sha.h>
  13. /**
  14. Retrieves the size, in bytes, of the context buffer required for SHA-1 hash operations.
  15. @return The size, in bytes, of the context buffer required for SHA-1 hash operations.
  16. **/
  17. UINTN
  18. EFIAPI
  19. Sha1GetContextSize (
  20. VOID
  21. )
  22. {
  23. //
  24. // Retrieves OpenSSL SHA Context Size
  25. //
  26. return (UINTN) (sizeof (SHA_CTX));
  27. }
  28. /**
  29. Initializes user-supplied memory pointed by Sha1Context as SHA-1 hash context for
  30. subsequent use.
  31. If Sha1Context is NULL, then return FALSE.
  32. @param[out] Sha1Context Pointer to SHA-1 context being initialized.
  33. @retval TRUE SHA-1 context initialization succeeded.
  34. @retval FALSE SHA-1 context initialization failed.
  35. **/
  36. BOOLEAN
  37. EFIAPI
  38. Sha1Init (
  39. OUT VOID *Sha1Context
  40. )
  41. {
  42. //
  43. // Check input parameters.
  44. //
  45. if (Sha1Context == NULL) {
  46. return FALSE;
  47. }
  48. //
  49. // OpenSSL SHA-1 Context Initialization
  50. //
  51. return (BOOLEAN) (SHA1_Init ((SHA_CTX *) Sha1Context));
  52. }
  53. /**
  54. Makes a copy of an existing SHA-1 context.
  55. If Sha1Context is NULL, then return FALSE.
  56. If NewSha1Context is NULL, then return FALSE.
  57. @param[in] Sha1Context Pointer to SHA-1 context being copied.
  58. @param[out] NewSha1Context Pointer to new SHA-1 context.
  59. @retval TRUE SHA-1 context copy succeeded.
  60. @retval FALSE SHA-1 context copy failed.
  61. **/
  62. BOOLEAN
  63. EFIAPI
  64. Sha1Duplicate (
  65. IN CONST VOID *Sha1Context,
  66. OUT VOID *NewSha1Context
  67. )
  68. {
  69. //
  70. // Check input parameters.
  71. //
  72. if (Sha1Context == NULL || NewSha1Context == NULL) {
  73. return FALSE;
  74. }
  75. CopyMem (NewSha1Context, Sha1Context, sizeof (SHA_CTX));
  76. return TRUE;
  77. }
  78. /**
  79. Digests the input data and updates SHA-1 context.
  80. This function performs SHA-1 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. SHA-1 context should be already correctly intialized by Sha1Init(), and should not be finalized
  83. by Sha1Final(). Behavior with invalid context is undefined.
  84. If Sha1Context is NULL, then return FALSE.
  85. @param[in, out] Sha1Context Pointer to the SHA-1 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 SHA-1 data digest succeeded.
  89. @retval FALSE SHA-1 data digest failed.
  90. **/
  91. BOOLEAN
  92. EFIAPI
  93. Sha1Update (
  94. IN OUT VOID *Sha1Context,
  95. IN CONST VOID *Data,
  96. IN UINTN DataSize
  97. )
  98. {
  99. //
  100. // Check input parameters.
  101. //
  102. if (Sha1Context == 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 SHA-1 Hash Update
  113. //
  114. return (BOOLEAN) (SHA1_Update ((SHA_CTX *) Sha1Context, Data, DataSize));
  115. }
  116. /**
  117. Completes computation of the SHA-1 digest value.
  118. This function completes SHA-1 hash computation and retrieves the digest value into
  119. the specified memory. After this function has been called, the SHA-1 context cannot
  120. be used again.
  121. SHA-1 context should be already correctly intialized by Sha1Init(), and should not be
  122. finalized by Sha1Final(). Behavior with invalid SHA-1 context is undefined.
  123. If Sha1Context is NULL, then return FALSE.
  124. If HashValue is NULL, then return FALSE.
  125. @param[in, out] Sha1Context Pointer to the SHA-1 context.
  126. @param[out] HashValue Pointer to a buffer that receives the SHA-1 digest
  127. value (20 bytes).
  128. @retval TRUE SHA-1 digest computation succeeded.
  129. @retval FALSE SHA-1 digest computation failed.
  130. **/
  131. BOOLEAN
  132. EFIAPI
  133. Sha1Final (
  134. IN OUT VOID *Sha1Context,
  135. OUT UINT8 *HashValue
  136. )
  137. {
  138. //
  139. // Check input parameters.
  140. //
  141. if (Sha1Context == NULL || HashValue == NULL) {
  142. return FALSE;
  143. }
  144. //
  145. // OpenSSL SHA-1 Hash Finalization
  146. //
  147. return (BOOLEAN) (SHA1_Final (HashValue, (SHA_CTX *) Sha1Context));
  148. }