HashInstanceLibSha384.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /** @file
  2. This library is BaseCrypto SHA384 hash instance.
  3. It can be registered to BaseCrypto router, to serve as hash engine.
  4. Copyright (c) 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/DebugLib.h>
  11. #include <Library/BaseCryptLib.h>
  12. #include <Library/MemoryAllocationLib.h>
  13. #include <Library/HashLib.h>
  14. /**
  15. The function set SHA384 to digest list.
  16. @param DigestList digest list
  17. @param Sha384Digest SHA384 digest
  18. **/
  19. VOID
  20. Tpm2SetSha384ToDigestList (
  21. IN TPML_DIGEST_VALUES *DigestList,
  22. IN UINT8 *Sha384Digest
  23. )
  24. {
  25. DigestList->count = 1;
  26. DigestList->digests[0].hashAlg = TPM_ALG_SHA384;
  27. CopyMem (
  28. DigestList->digests[0].digest.sha384,
  29. Sha384Digest,
  30. SHA384_DIGEST_SIZE
  31. );
  32. }
  33. /**
  34. Start hash sequence.
  35. @param HashHandle Hash handle.
  36. @retval EFI_SUCCESS Hash sequence start and HandleHandle returned.
  37. @retval EFI_OUT_OF_RESOURCES No enough resource to start hash.
  38. **/
  39. EFI_STATUS
  40. EFIAPI
  41. Sha384HashInit (
  42. OUT HASH_HANDLE *HashHandle
  43. )
  44. {
  45. VOID *Sha384Ctx;
  46. UINTN CtxSize;
  47. CtxSize = Sha384GetContextSize ();
  48. Sha384Ctx = AllocatePool (CtxSize);
  49. ASSERT (Sha384Ctx != NULL);
  50. Sha384Init (Sha384Ctx);
  51. *HashHandle = (HASH_HANDLE)Sha384Ctx;
  52. return EFI_SUCCESS;
  53. }
  54. /**
  55. Update hash sequence data.
  56. @param HashHandle Hash handle.
  57. @param DataToHash Data to be hashed.
  58. @param DataToHashLen Data size.
  59. @retval EFI_SUCCESS Hash sequence updated.
  60. **/
  61. EFI_STATUS
  62. EFIAPI
  63. Sha384HashUpdate (
  64. IN HASH_HANDLE HashHandle,
  65. IN VOID *DataToHash,
  66. IN UINTN DataToHashLen
  67. )
  68. {
  69. VOID *Sha384Ctx;
  70. Sha384Ctx = (VOID *)HashHandle;
  71. Sha384Update (Sha384Ctx, DataToHash, DataToHashLen);
  72. return EFI_SUCCESS;
  73. }
  74. /**
  75. Complete hash sequence complete.
  76. @param HashHandle Hash handle.
  77. @param DigestList Digest list.
  78. @retval EFI_SUCCESS Hash sequence complete and DigestList is returned.
  79. **/
  80. EFI_STATUS
  81. EFIAPI
  82. Sha384HashFinal (
  83. IN HASH_HANDLE HashHandle,
  84. OUT TPML_DIGEST_VALUES *DigestList
  85. )
  86. {
  87. UINT8 Digest[SHA384_DIGEST_SIZE];
  88. VOID *Sha384Ctx;
  89. Sha384Ctx = (VOID *)HashHandle;
  90. Sha384Final (Sha384Ctx, Digest);
  91. FreePool (Sha384Ctx);
  92. Tpm2SetSha384ToDigestList (DigestList, Digest);
  93. return EFI_SUCCESS;
  94. }
  95. HASH_INTERFACE mSha384InternalHashInstance = {
  96. HASH_ALGORITHM_SHA384_GUID,
  97. Sha384HashInit,
  98. Sha384HashUpdate,
  99. Sha384HashFinal,
  100. };
  101. /**
  102. The function register SHA384 instance.
  103. @retval EFI_SUCCESS SHA384 instance is registered, or system does not support register SHA384 instance
  104. **/
  105. EFI_STATUS
  106. EFIAPI
  107. HashInstanceLibSha384Constructor (
  108. VOID
  109. )
  110. {
  111. EFI_STATUS Status;
  112. Status = RegisterHashInterfaceLib (&mSha384InternalHashInstance);
  113. if ((Status == EFI_SUCCESS) || (Status == EFI_UNSUPPORTED)) {
  114. //
  115. // Unsupported means platform policy does not need this instance enabled.
  116. //
  117. return EFI_SUCCESS;
  118. }
  119. return Status;
  120. }