HashLibBaseCryptoRouterPei.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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/HobLib.h>
  16. #include <Library/HashLib.h>
  17. #include <Guid/ZeroGuid.h>
  18. #include "HashLibBaseCryptoRouterCommon.h"
  19. #define HASH_LIB_PEI_ROUTER_GUID \
  20. { 0x84681c08, 0x6873, 0x46f3, { 0x8b, 0xb7, 0xab, 0x66, 0x18, 0x95, 0xa1, 0xb3 } }
  21. EFI_GUID mHashLibPeiRouterGuid = HASH_LIB_PEI_ROUTER_GUID;
  22. typedef struct {
  23. //
  24. // If gZeroGuid, SupportedHashMask is 0 for FIRST module which consumes HashLib
  25. // or the hash algorithm bitmap of LAST module which consumes HashLib.
  26. // HashInterfaceCount and HashInterface are all 0.
  27. // If gEfiCallerIdGuid, HashInterfaceCount, HashInterface and SupportedHashMask
  28. // are the hash interface information of CURRENT module which consumes HashLib.
  29. //
  30. EFI_GUID Identifier;
  31. UINTN HashInterfaceCount;
  32. HASH_INTERFACE HashInterface[HASH_COUNT];
  33. UINT32 SupportedHashMask;
  34. } HASH_INTERFACE_HOB;
  35. /**
  36. This function gets hash interface hob.
  37. @param Identifier Identifier to get hash interface hob.
  38. @retval hash interface hob.
  39. **/
  40. HASH_INTERFACE_HOB *
  41. InternalGetHashInterfaceHob (
  42. EFI_GUID *Identifier
  43. )
  44. {
  45. EFI_PEI_HOB_POINTERS Hob;
  46. HASH_INTERFACE_HOB *HashInterfaceHob;
  47. Hob.Raw = GetFirstGuidHob (&mHashLibPeiRouterGuid);
  48. while (Hob.Raw != NULL) {
  49. HashInterfaceHob = GET_GUID_HOB_DATA (Hob);
  50. if (CompareGuid (&HashInterfaceHob->Identifier, Identifier)) {
  51. //
  52. // Found the matched one.
  53. //
  54. return HashInterfaceHob;
  55. }
  56. Hob.Raw = GET_NEXT_HOB (Hob);
  57. Hob.Raw = GetNextGuidHob (&mHashLibPeiRouterGuid, Hob.Raw);
  58. }
  59. return NULL;
  60. }
  61. /**
  62. This function creates hash interface hob.
  63. @param Identifier Identifier to create hash interface hob.
  64. @retval hash interface hob.
  65. **/
  66. HASH_INTERFACE_HOB *
  67. InternalCreateHashInterfaceHob (
  68. EFI_GUID *Identifier
  69. )
  70. {
  71. HASH_INTERFACE_HOB LocalHashInterfaceHob;
  72. ZeroMem (&LocalHashInterfaceHob, sizeof (LocalHashInterfaceHob));
  73. CopyGuid (&LocalHashInterfaceHob.Identifier, Identifier);
  74. return BuildGuidDataHob (&mHashLibPeiRouterGuid, &LocalHashInterfaceHob, sizeof (LocalHashInterfaceHob));
  75. }
  76. /**
  77. Check mismatch of supported HashMask between modules
  78. that may link different HashInstanceLib instances.
  79. @param HashInterfaceHobCurrent Pointer to hash interface hob for CURRENT module.
  80. **/
  81. VOID
  82. CheckSupportedHashMaskMismatch (
  83. IN HASH_INTERFACE_HOB *HashInterfaceHobCurrent
  84. )
  85. {
  86. HASH_INTERFACE_HOB *HashInterfaceHobLast;
  87. HashInterfaceHobLast = InternalGetHashInterfaceHob (&gZeroGuid);
  88. ASSERT (HashInterfaceHobLast != NULL);
  89. if ((HashInterfaceHobLast->SupportedHashMask != 0) &&
  90. (HashInterfaceHobCurrent->SupportedHashMask != HashInterfaceHobLast->SupportedHashMask))
  91. {
  92. DEBUG ((
  93. DEBUG_WARN,
  94. "WARNING: There is mismatch of supported HashMask (0x%x - 0x%x) between modules\n",
  95. HashInterfaceHobCurrent->SupportedHashMask,
  96. HashInterfaceHobLast->SupportedHashMask
  97. ));
  98. DEBUG ((DEBUG_WARN, "that are linking different HashInstanceLib instances!\n"));
  99. }
  100. }
  101. /**
  102. Start hash sequence.
  103. @param HashHandle Hash handle.
  104. @retval EFI_SUCCESS Hash sequence start and HandleHandle returned.
  105. @retval EFI_OUT_OF_RESOURCES No enough resource to start hash.
  106. **/
  107. EFI_STATUS
  108. EFIAPI
  109. HashStart (
  110. OUT HASH_HANDLE *HashHandle
  111. )
  112. {
  113. HASH_INTERFACE_HOB *HashInterfaceHob;
  114. HASH_HANDLE *HashCtx;
  115. UINTN Index;
  116. UINT32 HashMask;
  117. HashInterfaceHob = InternalGetHashInterfaceHob (&gEfiCallerIdGuid);
  118. if (HashInterfaceHob == NULL) {
  119. return EFI_UNSUPPORTED;
  120. }
  121. if (HashInterfaceHob->HashInterfaceCount == 0) {
  122. return EFI_UNSUPPORTED;
  123. }
  124. CheckSupportedHashMaskMismatch (HashInterfaceHob);
  125. HashCtx = AllocatePool (sizeof (*HashCtx) * HashInterfaceHob->HashInterfaceCount);
  126. ASSERT (HashCtx != NULL);
  127. for (Index = 0; Index < HashInterfaceHob->HashInterfaceCount; Index++) {
  128. HashMask = Tpm2GetHashMaskFromAlgo (&HashInterfaceHob->HashInterface[Index].HashGuid);
  129. if ((HashMask & PcdGet32 (PcdTpm2HashMask)) != 0) {
  130. HashInterfaceHob->HashInterface[Index].HashInit (&HashCtx[Index]);
  131. }
  132. }
  133. *HashHandle = (HASH_HANDLE)HashCtx;
  134. return EFI_SUCCESS;
  135. }
  136. /**
  137. Update hash sequence data.
  138. @param HashHandle Hash handle.
  139. @param DataToHash Data to be hashed.
  140. @param DataToHashLen Data size.
  141. @retval EFI_SUCCESS Hash sequence updated.
  142. **/
  143. EFI_STATUS
  144. EFIAPI
  145. HashUpdate (
  146. IN HASH_HANDLE HashHandle,
  147. IN VOID *DataToHash,
  148. IN UINTN DataToHashLen
  149. )
  150. {
  151. HASH_INTERFACE_HOB *HashInterfaceHob;
  152. HASH_HANDLE *HashCtx;
  153. UINTN Index;
  154. UINT32 HashMask;
  155. HashInterfaceHob = InternalGetHashInterfaceHob (&gEfiCallerIdGuid);
  156. if (HashInterfaceHob == NULL) {
  157. return EFI_UNSUPPORTED;
  158. }
  159. if (HashInterfaceHob->HashInterfaceCount == 0) {
  160. return EFI_UNSUPPORTED;
  161. }
  162. CheckSupportedHashMaskMismatch (HashInterfaceHob);
  163. HashCtx = (HASH_HANDLE *)HashHandle;
  164. for (Index = 0; Index < HashInterfaceHob->HashInterfaceCount; Index++) {
  165. HashMask = Tpm2GetHashMaskFromAlgo (&HashInterfaceHob->HashInterface[Index].HashGuid);
  166. if ((HashMask & PcdGet32 (PcdTpm2HashMask)) != 0) {
  167. HashInterfaceHob->HashInterface[Index].HashUpdate (HashCtx[Index], DataToHash, DataToHashLen);
  168. }
  169. }
  170. return EFI_SUCCESS;
  171. }
  172. /**
  173. Hash sequence complete and extend to PCR.
  174. @param HashHandle Hash handle.
  175. @param PcrIndex PCR to be extended.
  176. @param DataToHash Data to be hashed.
  177. @param DataToHashLen Data size.
  178. @param DigestList Digest list.
  179. @retval EFI_SUCCESS Hash sequence complete and DigestList is returned.
  180. **/
  181. EFI_STATUS
  182. EFIAPI
  183. HashCompleteAndExtend (
  184. IN HASH_HANDLE HashHandle,
  185. IN TPMI_DH_PCR PcrIndex,
  186. IN VOID *DataToHash,
  187. IN UINTN DataToHashLen,
  188. OUT TPML_DIGEST_VALUES *DigestList
  189. )
  190. {
  191. TPML_DIGEST_VALUES Digest;
  192. HASH_INTERFACE_HOB *HashInterfaceHob;
  193. HASH_HANDLE *HashCtx;
  194. UINTN Index;
  195. EFI_STATUS Status;
  196. UINT32 HashMask;
  197. HashInterfaceHob = InternalGetHashInterfaceHob (&gEfiCallerIdGuid);
  198. if (HashInterfaceHob == NULL) {
  199. return EFI_UNSUPPORTED;
  200. }
  201. if (HashInterfaceHob->HashInterfaceCount == 0) {
  202. return EFI_UNSUPPORTED;
  203. }
  204. CheckSupportedHashMaskMismatch (HashInterfaceHob);
  205. HashCtx = (HASH_HANDLE *)HashHandle;
  206. ZeroMem (DigestList, sizeof (*DigestList));
  207. for (Index = 0; Index < HashInterfaceHob->HashInterfaceCount; Index++) {
  208. HashMask = Tpm2GetHashMaskFromAlgo (&HashInterfaceHob->HashInterface[Index].HashGuid);
  209. if ((HashMask & PcdGet32 (PcdTpm2HashMask)) != 0) {
  210. HashInterfaceHob->HashInterface[Index].HashUpdate (HashCtx[Index], DataToHash, DataToHashLen);
  211. HashInterfaceHob->HashInterface[Index].HashFinal (HashCtx[Index], &Digest);
  212. Tpm2SetHashToDigestList (DigestList, &Digest);
  213. }
  214. }
  215. FreePool (HashCtx);
  216. Status = Tpm2PcrExtend (
  217. PcrIndex,
  218. DigestList
  219. );
  220. return Status;
  221. }
  222. /**
  223. Hash data and extend to PCR.
  224. @param PcrIndex PCR to be extended.
  225. @param DataToHash Data to be hashed.
  226. @param DataToHashLen Data size.
  227. @param DigestList Digest list.
  228. @retval EFI_SUCCESS Hash data and DigestList is returned.
  229. **/
  230. EFI_STATUS
  231. EFIAPI
  232. HashAndExtend (
  233. IN TPMI_DH_PCR PcrIndex,
  234. IN VOID *DataToHash,
  235. IN UINTN DataToHashLen,
  236. OUT TPML_DIGEST_VALUES *DigestList
  237. )
  238. {
  239. HASH_INTERFACE_HOB *HashInterfaceHob;
  240. HASH_HANDLE HashHandle;
  241. EFI_STATUS Status;
  242. HashInterfaceHob = InternalGetHashInterfaceHob (&gEfiCallerIdGuid);
  243. if (HashInterfaceHob == NULL) {
  244. return EFI_UNSUPPORTED;
  245. }
  246. if (HashInterfaceHob->HashInterfaceCount == 0) {
  247. return EFI_UNSUPPORTED;
  248. }
  249. CheckSupportedHashMaskMismatch (HashInterfaceHob);
  250. HashStart (&HashHandle);
  251. HashUpdate (HashHandle, DataToHash, DataToHashLen);
  252. Status = HashCompleteAndExtend (HashHandle, PcrIndex, NULL, 0, DigestList);
  253. return Status;
  254. }
  255. /**
  256. This service register Hash.
  257. @param HashInterface Hash interface
  258. @retval EFI_SUCCESS This hash interface is registered successfully.
  259. @retval EFI_UNSUPPORTED System does not support register this interface.
  260. @retval EFI_ALREADY_STARTED System already register this interface.
  261. **/
  262. EFI_STATUS
  263. EFIAPI
  264. RegisterHashInterfaceLib (
  265. IN HASH_INTERFACE *HashInterface
  266. )
  267. {
  268. UINTN Index;
  269. HASH_INTERFACE_HOB *HashInterfaceHob;
  270. UINT32 HashMask;
  271. EFI_STATUS Status;
  272. //
  273. // Check allow
  274. //
  275. HashMask = Tpm2GetHashMaskFromAlgo (&HashInterface->HashGuid);
  276. if ((HashMask & PcdGet32 (PcdTpm2HashMask)) == 0) {
  277. return EFI_UNSUPPORTED;
  278. }
  279. HashInterfaceHob = InternalGetHashInterfaceHob (&gEfiCallerIdGuid);
  280. if (HashInterfaceHob == NULL) {
  281. HashInterfaceHob = InternalCreateHashInterfaceHob (&gEfiCallerIdGuid);
  282. if (HashInterfaceHob == NULL) {
  283. return EFI_OUT_OF_RESOURCES;
  284. }
  285. }
  286. if (HashInterfaceHob->HashInterfaceCount >= HASH_COUNT) {
  287. return EFI_OUT_OF_RESOURCES;
  288. }
  289. //
  290. // Check duplication
  291. //
  292. for (Index = 0; Index < HashInterfaceHob->HashInterfaceCount; Index++) {
  293. if (CompareGuid (&HashInterfaceHob->HashInterface[Index].HashGuid, &HashInterface->HashGuid)) {
  294. DEBUG ((DEBUG_ERROR, "Hash Interface (%g) has been registered\n", &HashInterface->HashGuid));
  295. return EFI_ALREADY_STARTED;
  296. }
  297. }
  298. //
  299. // Record hash algorithm bitmap of CURRENT module which consumes HashLib.
  300. //
  301. HashInterfaceHob->SupportedHashMask = PcdGet32 (PcdTcg2HashAlgorithmBitmap) | HashMask;
  302. Status = PcdSet32S (PcdTcg2HashAlgorithmBitmap, HashInterfaceHob->SupportedHashMask);
  303. ASSERT_EFI_ERROR (Status);
  304. CopyMem (&HashInterfaceHob->HashInterface[HashInterfaceHob->HashInterfaceCount], HashInterface, sizeof (*HashInterface));
  305. HashInterfaceHob->HashInterfaceCount++;
  306. return EFI_SUCCESS;
  307. }
  308. /**
  309. The constructor function of HashLibBaseCryptoRouterPei.
  310. @param FileHandle The handle of FFS header the loaded driver.
  311. @param PeiServices The pointer to the PEI services.
  312. @retval EFI_SUCCESS The constructor executes successfully.
  313. @retval EFI_OUT_OF_RESOURCES There is no enough resource for the constructor.
  314. **/
  315. EFI_STATUS
  316. EFIAPI
  317. HashLibBaseCryptoRouterPeiConstructor (
  318. IN EFI_PEI_FILE_HANDLE FileHandle,
  319. IN CONST EFI_PEI_SERVICES **PeiServices
  320. )
  321. {
  322. EFI_STATUS Status;
  323. HASH_INTERFACE_HOB *HashInterfaceHob;
  324. HashInterfaceHob = InternalGetHashInterfaceHob (&gZeroGuid);
  325. if (HashInterfaceHob == NULL) {
  326. //
  327. // No HOB with gZeroGuid Identifier has been created,
  328. // this is FIRST module which consumes HashLib.
  329. // Create the HOB with gZeroGuid Identifier.
  330. //
  331. HashInterfaceHob = InternalCreateHashInterfaceHob (&gZeroGuid);
  332. if (HashInterfaceHob == NULL) {
  333. return EFI_OUT_OF_RESOURCES;
  334. }
  335. } else {
  336. //
  337. // Record hash algorithm bitmap of LAST module which also consumes HashLib.
  338. //
  339. HashInterfaceHob->SupportedHashMask = PcdGet32 (PcdTcg2HashAlgorithmBitmap);
  340. }
  341. HashInterfaceHob = InternalGetHashInterfaceHob (&gEfiCallerIdGuid);
  342. if (HashInterfaceHob != NULL) {
  343. //
  344. // In PEI phase, some modules may call RegisterForShadow and will be
  345. // shadowed and executed again after memory is discovered.
  346. // This is the second execution of this module, clear the hash interface
  347. // information registered at its first execution.
  348. //
  349. ZeroMem (&HashInterfaceHob->HashInterface, sizeof (HashInterfaceHob->HashInterface));
  350. HashInterfaceHob->HashInterfaceCount = 0;
  351. HashInterfaceHob->SupportedHashMask = 0;
  352. }
  353. //
  354. // Set PcdTcg2HashAlgorithmBitmap to 0 in CONSTRUCTOR for CURRENT module.
  355. //
  356. Status = PcdSet32S (PcdTcg2HashAlgorithmBitmap, 0);
  357. ASSERT_EFI_ERROR (Status);
  358. return EFI_SUCCESS;
  359. }