HashInstanceLibSha1.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /** @file
  2. This library is BaseCrypto SHA1 hash instance.
  3. It can be registered to BaseCrypto router, to serve as hash engine.
  4. Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved. <BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include <PiPei.h>
  8. #include <Library/BaseLib.h>
  9. #include <Library/BaseMemoryLib.h>
  10. #include <Library/Tpm2CommandLib.h>
  11. #include <Library/DebugLib.h>
  12. #include <Library/BaseCryptLib.h>
  13. #include <Library/MemoryAllocationLib.h>
  14. #include <Library/HashLib.h>
  15. /**
  16. The function set SHA1 to digest list.
  17. @param DigestList digest list
  18. @param Sha1Digest SHA1 digest
  19. **/
  20. VOID
  21. Tpm2SetSha1ToDigestList (
  22. IN TPML_DIGEST_VALUES *DigestList,
  23. IN UINT8 *Sha1Digest
  24. )
  25. {
  26. DigestList->count = 1;
  27. DigestList->digests[0].hashAlg = TPM_ALG_SHA1;
  28. CopyMem (
  29. DigestList->digests[0].digest.sha1,
  30. Sha1Digest,
  31. SHA1_DIGEST_SIZE
  32. );
  33. }
  34. /**
  35. Start hash sequence.
  36. @param HashHandle Hash handle.
  37. @retval EFI_SUCCESS Hash sequence start and HandleHandle returned.
  38. @retval EFI_OUT_OF_RESOURCES No enough resource to start hash.
  39. **/
  40. EFI_STATUS
  41. EFIAPI
  42. Sha1HashInit (
  43. OUT HASH_HANDLE *HashHandle
  44. )
  45. {
  46. VOID *Sha1Ctx;
  47. UINTN CtxSize;
  48. CtxSize = Sha1GetContextSize ();
  49. Sha1Ctx = AllocatePool (CtxSize);
  50. ASSERT (Sha1Ctx != NULL);
  51. Sha1Init (Sha1Ctx);
  52. *HashHandle = (HASH_HANDLE)Sha1Ctx;
  53. return EFI_SUCCESS;
  54. }
  55. /**
  56. Update hash sequence data.
  57. @param HashHandle Hash handle.
  58. @param DataToHash Data to be hashed.
  59. @param DataToHashLen Data size.
  60. @retval EFI_SUCCESS Hash sequence updated.
  61. **/
  62. EFI_STATUS
  63. EFIAPI
  64. Sha1HashUpdate (
  65. IN HASH_HANDLE HashHandle,
  66. IN VOID *DataToHash,
  67. IN UINTN DataToHashLen
  68. )
  69. {
  70. VOID *Sha1Ctx;
  71. Sha1Ctx = (VOID *)HashHandle;
  72. Sha1Update (Sha1Ctx, DataToHash, DataToHashLen);
  73. return EFI_SUCCESS;
  74. }
  75. /**
  76. Complete hash sequence complete.
  77. @param HashHandle Hash handle.
  78. @param DigestList Digest list.
  79. @retval EFI_SUCCESS Hash sequence complete and DigestList is returned.
  80. **/
  81. EFI_STATUS
  82. EFIAPI
  83. Sha1HashFinal (
  84. IN HASH_HANDLE HashHandle,
  85. OUT TPML_DIGEST_VALUES *DigestList
  86. )
  87. {
  88. UINT8 Digest[SHA1_DIGEST_SIZE];
  89. VOID *Sha1Ctx;
  90. Sha1Ctx = (VOID *)HashHandle;
  91. Sha1Final (Sha1Ctx, Digest);
  92. FreePool (Sha1Ctx);
  93. Tpm2SetSha1ToDigestList (DigestList, Digest);
  94. return EFI_SUCCESS;
  95. }
  96. HASH_INTERFACE mSha1InternalHashInstance = {
  97. HASH_ALGORITHM_SHA1_GUID,
  98. Sha1HashInit,
  99. Sha1HashUpdate,
  100. Sha1HashFinal,
  101. };
  102. /**
  103. The function register SHA1 instance.
  104. @retval EFI_SUCCESS SHA1 instance is registered, or system does not support register SHA1 instance
  105. **/
  106. EFI_STATUS
  107. EFIAPI
  108. HashInstanceLibSha1Constructor (
  109. VOID
  110. )
  111. {
  112. EFI_STATUS Status;
  113. Status = RegisterHashInterfaceLib (&mSha1InternalHashInstance);
  114. if ((Status == EFI_SUCCESS) || (Status == EFI_UNSUPPORTED)) {
  115. //
  116. // Unsupported means platform policy does not need this instance enabled.
  117. //
  118. return EFI_SUCCESS;
  119. }
  120. return Status;
  121. }