PeiDxeTpmPlatformHierarchyLib.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /** @file
  2. TPM Platform Hierarchy configuration library.
  3. This library provides functions for customizing the TPM's Platform Hierarchy
  4. Authorization Value (platformAuth) and Platform Hierarchy Authorization
  5. Policy (platformPolicy) can be defined through this function.
  6. Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
  7. Copyright (c) Microsoft Corporation.<BR>
  8. SPDX-License-Identifier: BSD-2-Clause-Patent
  9. @par Specification Reference:
  10. https://trustedcomputinggroup.org/resource/tcg-tpm-v2-0-provisioning-guidance/
  11. **/
  12. #include <Uefi.h>
  13. #include <Library/BaseMemoryLib.h>
  14. #include <Library/DebugLib.h>
  15. #include <Library/MemoryAllocationLib.h>
  16. #include <Library/RngLib.h>
  17. #include <Library/Tpm2CommandLib.h>
  18. #include <Library/Tpm2DeviceLib.h>
  19. //
  20. // The authorization value may be no larger than the digest produced by the hash
  21. // algorithm used for context integrity.
  22. //
  23. UINT16 mAuthSize;
  24. /**
  25. Generate high-quality entropy source through RDRAND.
  26. @param[in] Length Size of the buffer, in bytes, to fill with.
  27. @param[out] Entropy Pointer to the buffer to store the entropy data.
  28. @retval EFI_SUCCESS Entropy generation succeeded.
  29. @retval EFI_NOT_READY Failed to request random data.
  30. **/
  31. EFI_STATUS
  32. EFIAPI
  33. RdRandGenerateEntropy (
  34. IN UINTN Length,
  35. OUT UINT8 *Entropy
  36. )
  37. {
  38. EFI_STATUS Status;
  39. UINTN BlockCount;
  40. UINT64 Seed[2];
  41. UINT8 *Ptr;
  42. Status = EFI_NOT_READY;
  43. BlockCount = Length / sizeof (Seed);
  44. Ptr = (UINT8 *)Entropy;
  45. //
  46. // Generate high-quality seed for DRBG Entropy
  47. //
  48. while (BlockCount > 0) {
  49. Status = GetRandomNumber128 (Seed);
  50. if (EFI_ERROR (Status)) {
  51. return Status;
  52. }
  53. CopyMem (Ptr, Seed, sizeof (Seed));
  54. BlockCount--;
  55. Ptr = Ptr + sizeof (Seed);
  56. }
  57. //
  58. // Populate the remained data as request.
  59. //
  60. Status = GetRandomNumber128 (Seed);
  61. if (EFI_ERROR (Status)) {
  62. return Status;
  63. }
  64. CopyMem (Ptr, Seed, (Length % sizeof (Seed)));
  65. return Status;
  66. }
  67. /**
  68. This function returns the maximum size of TPM2B_AUTH; this structure is used for an authorization value
  69. and limits an authValue to being no larger than the largest digest produced by a TPM.
  70. @param[out] AuthSize Tpm2 Auth size
  71. @retval EFI_SUCCESS Auth size returned.
  72. @retval EFI_DEVICE_ERROR Can not return platform auth due to device error.
  73. **/
  74. EFI_STATUS
  75. EFIAPI
  76. GetAuthSize (
  77. OUT UINT16 *AuthSize
  78. )
  79. {
  80. EFI_STATUS Status;
  81. TPML_PCR_SELECTION Pcrs;
  82. UINTN Index;
  83. UINT16 DigestSize;
  84. Status = EFI_SUCCESS;
  85. while (mAuthSize == 0) {
  86. mAuthSize = SHA1_DIGEST_SIZE;
  87. ZeroMem (&Pcrs, sizeof (TPML_PCR_SELECTION));
  88. Status = Tpm2GetCapabilityPcrs (&Pcrs);
  89. if (EFI_ERROR (Status)) {
  90. DEBUG ((DEBUG_ERROR, "Tpm2GetCapabilityPcrs fail!\n"));
  91. break;
  92. }
  93. DEBUG ((DEBUG_ERROR, "Tpm2GetCapabilityPcrs - %08x\n", Pcrs.count));
  94. for (Index = 0; Index < Pcrs.count; Index++) {
  95. DEBUG ((DEBUG_ERROR, "alg - %x\n", Pcrs.pcrSelections[Index].hash));
  96. switch (Pcrs.pcrSelections[Index].hash) {
  97. case TPM_ALG_SHA1:
  98. DigestSize = SHA1_DIGEST_SIZE;
  99. break;
  100. case TPM_ALG_SHA256:
  101. DigestSize = SHA256_DIGEST_SIZE;
  102. break;
  103. case TPM_ALG_SHA384:
  104. DigestSize = SHA384_DIGEST_SIZE;
  105. break;
  106. case TPM_ALG_SHA512:
  107. DigestSize = SHA512_DIGEST_SIZE;
  108. break;
  109. case TPM_ALG_SM3_256:
  110. DigestSize = SM3_256_DIGEST_SIZE;
  111. break;
  112. default:
  113. DigestSize = SHA1_DIGEST_SIZE;
  114. break;
  115. }
  116. if (DigestSize > mAuthSize) {
  117. mAuthSize = DigestSize;
  118. }
  119. }
  120. break;
  121. }
  122. *AuthSize = mAuthSize;
  123. return Status;
  124. }
  125. /**
  126. Set PlatformAuth to random value.
  127. **/
  128. VOID
  129. RandomizePlatformAuth (
  130. VOID
  131. )
  132. {
  133. EFI_STATUS Status;
  134. UINT16 AuthSize;
  135. TPM2B_AUTH NewPlatformAuth;
  136. //
  137. // Send Tpm2HierarchyChange Auth with random value to avoid PlatformAuth being null
  138. //
  139. GetAuthSize (&AuthSize);
  140. NewPlatformAuth.size = AuthSize;
  141. //
  142. // Create the random bytes in the destination buffer
  143. //
  144. RdRandGenerateEntropy (NewPlatformAuth.size, NewPlatformAuth.buffer);
  145. //
  146. // Send Tpm2HierarchyChangeAuth command with the new Auth value
  147. //
  148. Status = Tpm2HierarchyChangeAuth (TPM_RH_PLATFORM, NULL, &NewPlatformAuth);
  149. DEBUG ((DEBUG_INFO, "Tpm2HierarchyChangeAuth Result: - %r\n", Status));
  150. ZeroMem (NewPlatformAuth.buffer, AuthSize);
  151. }
  152. /**
  153. Disable the TPM platform hierarchy.
  154. @retval EFI_SUCCESS The TPM was disabled successfully.
  155. @retval Others An error occurred attempting to disable the TPM platform hierarchy.
  156. **/
  157. EFI_STATUS
  158. DisableTpmPlatformHierarchy (
  159. VOID
  160. )
  161. {
  162. EFI_STATUS Status;
  163. // Make sure that we have use of the TPM.
  164. Status = Tpm2RequestUseTpm ();
  165. if (EFI_ERROR (Status)) {
  166. DEBUG ((DEBUG_ERROR, "%a:%a() - Tpm2RequestUseTpm Failed! %r\n", gEfiCallerBaseName, __FUNCTION__, Status));
  167. ASSERT_EFI_ERROR (Status);
  168. return Status;
  169. }
  170. // Let's do what we can to shut down the hierarchies.
  171. // Disable the PH NV.
  172. // IMPORTANT NOTE: We *should* be able to disable the PH NV here, but TPM parts have
  173. // been known to store the EK cert in the PH NV. If we disable it, the
  174. // EK cert will be unreadable.
  175. // Disable the PH.
  176. Status = Tpm2HierarchyControl (
  177. TPM_RH_PLATFORM, // AuthHandle
  178. NULL, // AuthSession
  179. TPM_RH_PLATFORM, // Hierarchy
  180. NO // State
  181. );
  182. DEBUG ((DEBUG_VERBOSE, "%a:%a() - Disable PH = %r\n", gEfiCallerBaseName, __FUNCTION__, Status));
  183. if (EFI_ERROR (Status)) {
  184. DEBUG ((DEBUG_ERROR, "%a:%a() - Disable PH Failed! %r\n", gEfiCallerBaseName, __FUNCTION__, Status));
  185. ASSERT_EFI_ERROR (Status);
  186. }
  187. return Status;
  188. }
  189. /**
  190. This service defines the configuration of the Platform Hierarchy Authorization Value (platformAuth)
  191. and Platform Hierarchy Authorization Policy (platformPolicy).
  192. **/
  193. VOID
  194. EFIAPI
  195. ConfigureTpmPlatformHierarchy (
  196. )
  197. {
  198. if (PcdGetBool (PcdRandomizePlatformHierarchy)) {
  199. //
  200. // Send Tpm2HierarchyChange Auth with random value to avoid PlatformAuth being null
  201. //
  202. RandomizePlatformAuth ();
  203. } else {
  204. //
  205. // Disable the hierarchy entirely (do not randomize it)
  206. //
  207. DisableTpmPlatformHierarchy ();
  208. }
  209. }