HashLibBaseCryptoRouterDxe.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /** @file
  2. This library is BaseCrypto router. It will redirect hash request to each individual
  3. hash handler registered, such as SHA1, SHA256.
  4. Platform can use PcdTpm2HashMask to mask some hash engines.
  5. Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved. <BR>
  6. SPDX-License-Identifier: BSD-2-Clause-Patent
  7. **/
  8. #include <PiPei.h>
  9. #include <Library/BaseLib.h>
  10. #include <Library/BaseMemoryLib.h>
  11. #include <Library/Tpm2CommandLib.h>
  12. #include <Library/DebugLib.h>
  13. #include <Library/MemoryAllocationLib.h>
  14. #include <Library/PcdLib.h>
  15. #include <Library/HashLib.h>
  16. #include "HashLibBaseCryptoRouterCommon.h"
  17. HASH_INTERFACE mHashInterface[HASH_COUNT] = {
  18. {
  19. { 0 }, NULL, NULL, NULL
  20. }
  21. };
  22. UINTN mHashInterfaceCount = 0;
  23. UINT32 mSupportedHashMaskLast = 0;
  24. UINT32 mSupportedHashMaskCurrent = 0;
  25. /**
  26. Check mismatch of supported HashMask between modules
  27. that may link different HashInstanceLib instances.
  28. **/
  29. VOID
  30. CheckSupportedHashMaskMismatch (
  31. VOID
  32. )
  33. {
  34. if (mSupportedHashMaskCurrent != mSupportedHashMaskLast) {
  35. DEBUG ((
  36. DEBUG_WARN,
  37. "WARNING: There is mismatch of supported HashMask (0x%x - 0x%x) between modules\n",
  38. mSupportedHashMaskCurrent,
  39. mSupportedHashMaskLast
  40. ));
  41. DEBUG ((DEBUG_WARN, "that are linking different HashInstanceLib instances!\n"));
  42. }
  43. }
  44. /**
  45. Start hash sequence.
  46. @param HashHandle Hash handle.
  47. @retval EFI_SUCCESS Hash sequence start and HandleHandle returned.
  48. @retval EFI_OUT_OF_RESOURCES No enough resource to start hash.
  49. **/
  50. EFI_STATUS
  51. EFIAPI
  52. HashStart (
  53. OUT HASH_HANDLE *HashHandle
  54. )
  55. {
  56. HASH_HANDLE *HashCtx;
  57. UINTN Index;
  58. UINT32 HashMask;
  59. if (mHashInterfaceCount == 0) {
  60. return EFI_UNSUPPORTED;
  61. }
  62. CheckSupportedHashMaskMismatch ();
  63. HashCtx = AllocatePool (sizeof (*HashCtx) * mHashInterfaceCount);
  64. ASSERT (HashCtx != NULL);
  65. for (Index = 0; Index < mHashInterfaceCount; Index++) {
  66. HashMask = Tpm2GetHashMaskFromAlgo (&mHashInterface[Index].HashGuid);
  67. if ((HashMask & PcdGet32 (PcdTpm2HashMask)) != 0) {
  68. mHashInterface[Index].HashInit (&HashCtx[Index]);
  69. }
  70. }
  71. *HashHandle = (HASH_HANDLE)HashCtx;
  72. return EFI_SUCCESS;
  73. }
  74. /**
  75. Update hash sequence data.
  76. @param HashHandle Hash handle.
  77. @param DataToHash Data to be hashed.
  78. @param DataToHashLen Data size.
  79. @retval EFI_SUCCESS Hash sequence updated.
  80. **/
  81. EFI_STATUS
  82. EFIAPI
  83. HashUpdate (
  84. IN HASH_HANDLE HashHandle,
  85. IN VOID *DataToHash,
  86. IN UINTN DataToHashLen
  87. )
  88. {
  89. HASH_HANDLE *HashCtx;
  90. UINTN Index;
  91. UINT32 HashMask;
  92. if (mHashInterfaceCount == 0) {
  93. return EFI_UNSUPPORTED;
  94. }
  95. CheckSupportedHashMaskMismatch ();
  96. HashCtx = (HASH_HANDLE *)HashHandle;
  97. for (Index = 0; Index < mHashInterfaceCount; Index++) {
  98. HashMask = Tpm2GetHashMaskFromAlgo (&mHashInterface[Index].HashGuid);
  99. if ((HashMask & PcdGet32 (PcdTpm2HashMask)) != 0) {
  100. mHashInterface[Index].HashUpdate (HashCtx[Index], DataToHash, DataToHashLen);
  101. }
  102. }
  103. return EFI_SUCCESS;
  104. }
  105. /**
  106. Hash sequence complete and extend to PCR.
  107. @param HashHandle Hash handle.
  108. @param PcrIndex PCR to be extended.
  109. @param DataToHash Data to be hashed.
  110. @param DataToHashLen Data size.
  111. @param DigestList Digest list.
  112. @retval EFI_SUCCESS Hash sequence complete and DigestList is returned.
  113. **/
  114. EFI_STATUS
  115. EFIAPI
  116. HashCompleteAndExtend (
  117. IN HASH_HANDLE HashHandle,
  118. IN TPMI_DH_PCR PcrIndex,
  119. IN VOID *DataToHash,
  120. IN UINTN DataToHashLen,
  121. OUT TPML_DIGEST_VALUES *DigestList
  122. )
  123. {
  124. TPML_DIGEST_VALUES Digest;
  125. HASH_HANDLE *HashCtx;
  126. UINTN Index;
  127. EFI_STATUS Status;
  128. UINT32 HashMask;
  129. if (mHashInterfaceCount == 0) {
  130. return EFI_UNSUPPORTED;
  131. }
  132. CheckSupportedHashMaskMismatch ();
  133. HashCtx = (HASH_HANDLE *)HashHandle;
  134. ZeroMem (DigestList, sizeof (*DigestList));
  135. for (Index = 0; Index < mHashInterfaceCount; Index++) {
  136. HashMask = Tpm2GetHashMaskFromAlgo (&mHashInterface[Index].HashGuid);
  137. if ((HashMask & PcdGet32 (PcdTpm2HashMask)) != 0) {
  138. mHashInterface[Index].HashUpdate (HashCtx[Index], DataToHash, DataToHashLen);
  139. mHashInterface[Index].HashFinal (HashCtx[Index], &Digest);
  140. Tpm2SetHashToDigestList (DigestList, &Digest);
  141. }
  142. }
  143. FreePool (HashCtx);
  144. Status = Tpm2PcrExtend (
  145. PcrIndex,
  146. DigestList
  147. );
  148. return Status;
  149. }
  150. /**
  151. Hash data and extend to PCR.
  152. @param PcrIndex PCR to be extended.
  153. @param DataToHash Data to be hashed.
  154. @param DataToHashLen Data size.
  155. @param DigestList Digest list.
  156. @retval EFI_SUCCESS Hash data and DigestList is returned.
  157. **/
  158. EFI_STATUS
  159. EFIAPI
  160. HashAndExtend (
  161. IN TPMI_DH_PCR PcrIndex,
  162. IN VOID *DataToHash,
  163. IN UINTN DataToHashLen,
  164. OUT TPML_DIGEST_VALUES *DigestList
  165. )
  166. {
  167. HASH_HANDLE HashHandle;
  168. EFI_STATUS Status;
  169. if (mHashInterfaceCount == 0) {
  170. return EFI_UNSUPPORTED;
  171. }
  172. CheckSupportedHashMaskMismatch ();
  173. HashStart (&HashHandle);
  174. HashUpdate (HashHandle, DataToHash, DataToHashLen);
  175. Status = HashCompleteAndExtend (HashHandle, PcrIndex, NULL, 0, DigestList);
  176. return Status;
  177. }
  178. /**
  179. This service register Hash.
  180. @param HashInterface Hash interface
  181. @retval EFI_SUCCESS This hash interface is registered successfully.
  182. @retval EFI_UNSUPPORTED System does not support register this interface.
  183. @retval EFI_ALREADY_STARTED System already register this interface.
  184. **/
  185. EFI_STATUS
  186. EFIAPI
  187. RegisterHashInterfaceLib (
  188. IN HASH_INTERFACE *HashInterface
  189. )
  190. {
  191. UINTN Index;
  192. UINT32 HashMask;
  193. EFI_STATUS Status;
  194. //
  195. // Check allow
  196. //
  197. HashMask = Tpm2GetHashMaskFromAlgo (&HashInterface->HashGuid);
  198. if ((HashMask & PcdGet32 (PcdTpm2HashMask)) == 0) {
  199. return EFI_UNSUPPORTED;
  200. }
  201. if (mHashInterfaceCount >= sizeof (mHashInterface)/sizeof (mHashInterface[0])) {
  202. return EFI_OUT_OF_RESOURCES;
  203. }
  204. //
  205. // Check duplication
  206. //
  207. for (Index = 0; Index < mHashInterfaceCount; Index++) {
  208. if (CompareGuid (&mHashInterface[Index].HashGuid, &HashInterface->HashGuid)) {
  209. DEBUG ((DEBUG_ERROR, "Hash Interface (%g) has been registered\n", &HashInterface->HashGuid));
  210. return EFI_ALREADY_STARTED;
  211. }
  212. }
  213. //
  214. // Record hash algorithm bitmap of CURRENT module which consumes HashLib.
  215. //
  216. mSupportedHashMaskCurrent = PcdGet32 (PcdTcg2HashAlgorithmBitmap) | HashMask;
  217. Status = PcdSet32S (PcdTcg2HashAlgorithmBitmap, mSupportedHashMaskCurrent);
  218. ASSERT_EFI_ERROR (Status);
  219. CopyMem (&mHashInterface[mHashInterfaceCount], HashInterface, sizeof (*HashInterface));
  220. mHashInterfaceCount++;
  221. return EFI_SUCCESS;
  222. }
  223. /**
  224. The constructor function of HashLibBaseCryptoRouterDxe.
  225. @param ImageHandle The firmware allocated handle for the EFI image.
  226. @param SystemTable A pointer to the EFI System Table.
  227. @retval EFI_SUCCESS The constructor executed correctly.
  228. **/
  229. EFI_STATUS
  230. EFIAPI
  231. HashLibBaseCryptoRouterDxeConstructor (
  232. IN EFI_HANDLE ImageHandle,
  233. IN EFI_SYSTEM_TABLE *SystemTable
  234. )
  235. {
  236. EFI_STATUS Status;
  237. //
  238. // Record hash algorithm bitmap of LAST module which also consumes HashLib.
  239. //
  240. mSupportedHashMaskLast = PcdGet32 (PcdTcg2HashAlgorithmBitmap);
  241. //
  242. // Set PcdTcg2HashAlgorithmBitmap to 0 in CONSTRUCTOR for CURRENT module.
  243. //
  244. Status = PcdSet32S (PcdTcg2HashAlgorithmBitmap, 0);
  245. ASSERT_EFI_ERROR (Status);
  246. return EFI_SUCCESS;
  247. }