Skip to content

Commit 6f73428

Browse files
suncepingmergify[bot]
authored andcommitted
OvmfPkg: Implement TdxMeasurementLib
Add below APIs implementation that copied from TdxHelperLib. - TdxMeasurementMapPcrToMrIndex - TdxMeasurementHashAndExtendToRtmr - TdxMeasurementBuildGuidHob Cc: Erdem Aktas <[email protected]> Cc: Jiewen Yao <[email protected]> Cc: Min Xu <[email protected]> Cc: Gerd Hoffmann <[email protected]> Cc: Elena Reshetova <[email protected]> Signed-off-by: Min Xu <[email protected]> Signed-off-by: Ceping Sun <[email protected]>
1 parent d97f530 commit 6f73428

File tree

5 files changed

+331
-0
lines changed

5 files changed

+331
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/** @file
2+
TdxMeasurement Functions which are used in DXE phase
3+
4+
Copyright (c) 2025, Intel Corporation. All rights reserved.<BR>
5+
SPDX-License-Identifier: BSD-2-Clause-Patent
6+
**/
7+
#include <Base.h>
8+
#include <Library/DebugLib.h>
9+
#include <Library/TdxMeasurementLib.h>
10+
11+
/**
12+
* Build GuidHob for Tdx CC measurement event.
13+
*
14+
* @param RtmrIndex RTMR index
15+
* @param EventType Event type
16+
* @param EventData Event data
17+
* @param EventSize Size of event data
18+
* @param HashValue Hash value
19+
* @param HashSize Size of hash
20+
*
21+
* @retval EFI_SUCCESS Successfully build the GuidHobs
22+
* @retval Others Other error as indicated
23+
*/
24+
EFI_STATUS
25+
EFIAPI
26+
TdxMeasurementBuildGuidHob (
27+
UINT32 RtmrIndex,
28+
UINT32 EventType,
29+
UINT8 *EventData,
30+
UINT32 EventSize,
31+
UINT8 *HashValue,
32+
UINT32 HashSize
33+
)
34+
{
35+
return EFI_UNSUPPORTED;
36+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## @file
2+
# TdxHelperLib Dxe instance
3+
#
4+
# This module provides Tdx helper functions in DXE phase.
5+
# Copyright (c) 2025, Intel Corporation. All rights reserved.<BR>
6+
#
7+
# SPDX-License-Identifier: BSD-2-Clause-Patent
8+
#
9+
##
10+
11+
[Defines]
12+
INF_VERSION = 0x00010005
13+
BASE_NAME = DxeTdxMeasurementLib
14+
FILE_GUID = 7ad50992-cc31-4d2c-9048-6e92ed4b42b5
15+
MODULE_TYPE = BASE
16+
VERSION_STRING = 1.0
17+
LIBRARY_CLASS = TdxMeasurementLib|DXE_DRIVER DXE_RUNTIME_DRIVER
18+
19+
#
20+
# The following information is for reference only and not required by the build tools.
21+
#
22+
# VALID_ARCHITECTURES = X64
23+
#
24+
25+
[Sources]
26+
DxeTdxMeasurement.c
27+
TdxMeasurementCommon.c
28+
29+
[Packages]
30+
MdePkg/MdePkg.dec
31+
MdeModulePkg/MdeModulePkg.dec
32+
UefiCpuPkg/UefiCpuPkg.dec
33+
SecurityPkg/SecurityPkg.dec
34+
CryptoPkg/CryptoPkg.dec
35+
36+
[LibraryClasses]
37+
BaseLib
38+
DebugLib
39+
HobLib
40+
PcdLib
41+
BaseCryptLib
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/** @file
2+
TdxMeasurement Functions which are used in SEC and PEI phase
3+
4+
Copyright (c) 2025, Intel Corporation. All rights reserved.<BR>
5+
6+
SPDX-License-Identifier: BSD-2-Clause-Patent
7+
8+
**/
9+
10+
#include <PiPei.h>
11+
#include <Pi/PiHob.h>
12+
#include <Library/BaseLib.h>
13+
#include <Library/DebugLib.h>
14+
#include <Library/HobLib.h>
15+
#include <Library/BaseMemoryLib.h>
16+
#include <IndustryStandard/Tpm20.h>
17+
#include <IndustryStandard/UefiTcgPlatform.h>
18+
#include <Library/TdxMeasurementLib.h>
19+
20+
/**
21+
* Build GuidHob for Tdx measurement.
22+
*
23+
* Tdx measurement includes the measurement of TdHob and CFV. They're measured
24+
* and extended to RTMR registers in SEC phase. Because at that moment the Hob
25+
* service are not available. So the values of the measurement are saved in
26+
* workarea and will be built into GuidHob after the Hob service is ready.
27+
*
28+
* @param RtmrIndex RTMR index
29+
* @param EventType Event type
30+
* @param EventData Event data
31+
* @param EventSize Size of event data
32+
* @param HashValue Hash value
33+
* @param HashSize Size of hash
34+
*
35+
* @retval EFI_SUCCESS Successfully build the GuidHobs
36+
* @retval Others Other error as indicated
37+
*/
38+
EFI_STATUS
39+
EFIAPI
40+
TdxMeasurementBuildGuidHob (
41+
UINT32 RtmrIndex,
42+
UINT32 EventType,
43+
UINT8 *EventData,
44+
UINT32 EventSize,
45+
UINT8 *HashValue,
46+
UINT32 HashSize
47+
)
48+
{
49+
VOID *EventHobData;
50+
UINT8 *Ptr;
51+
TPML_DIGEST_VALUES *TdxDigest;
52+
53+
if (HashSize != SHA384_DIGEST_SIZE) {
54+
return EFI_INVALID_PARAMETER;
55+
}
56+
57+
#define TDX_DIGEST_VALUE_LEN (sizeof (UINT32) + sizeof (TPMI_ALG_HASH) + SHA384_DIGEST_SIZE)
58+
59+
EventHobData = BuildGuidHob (
60+
&gCcEventEntryHobGuid,
61+
sizeof (TCG_PCRINDEX) + sizeof (TCG_EVENTTYPE) +
62+
TDX_DIGEST_VALUE_LEN +
63+
sizeof (UINT32) + EventSize
64+
);
65+
66+
if (EventHobData == NULL) {
67+
return EFI_OUT_OF_RESOURCES;
68+
}
69+
70+
Ptr = (UINT8 *)EventHobData;
71+
72+
//
73+
// There are 2 types of measurement registers in TDX: MRTD and RTMR[0-3].
74+
// According to UEFI Spec 2.10 Section 38.4.1, RTMR[0-3] is mapped to MrIndex[1-4].
75+
// So RtmrIndex must be increased by 1 before the event log is created.
76+
//
77+
RtmrIndex++;
78+
CopyMem (Ptr, &RtmrIndex, sizeof (UINT32));
79+
Ptr += sizeof (UINT32);
80+
81+
CopyMem (Ptr, &EventType, sizeof (TCG_EVENTTYPE));
82+
Ptr += sizeof (TCG_EVENTTYPE);
83+
84+
TdxDigest = (TPML_DIGEST_VALUES *)Ptr;
85+
TdxDigest->count = 1;
86+
TdxDigest->digests[0].hashAlg = TPM_ALG_SHA384;
87+
CopyMem (
88+
TdxDigest->digests[0].digest.sha384,
89+
HashValue,
90+
SHA384_DIGEST_SIZE
91+
);
92+
Ptr += TDX_DIGEST_VALUE_LEN;
93+
94+
CopyMem (Ptr, &EventSize, sizeof (UINT32));
95+
Ptr += sizeof (UINT32);
96+
97+
CopyMem (Ptr, (VOID *)EventData, EventSize);
98+
Ptr += EventSize;
99+
100+
return EFI_SUCCESS;
101+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## @file
2+
# TdxMeasurement SEC and PEI instance
3+
#
4+
# This module provides Tdx measurement functions in SEC and PEI phase.
5+
# Copyright (c) 2025, Intel Corporation. All rights reserved.<BR>
6+
#
7+
# SPDX-License-Identifier: BSD-2-Clause-Patent
8+
#
9+
##
10+
11+
[Defines]
12+
INF_VERSION = 0x00010005
13+
BASE_NAME = SecPeiTdxMeasurementLib
14+
FILE_GUID = fd565572-5195-4113-9018-113b33939e91
15+
MODULE_TYPE = BASE
16+
VERSION_STRING = 1.0
17+
LIBRARY_CLASS = TdxMeasurementLib|SEC PEIM PEI_CORE
18+
19+
#
20+
# The following information is for reference only and not required by the build tools.
21+
#
22+
# VALID_ARCHITECTURES = X64
23+
#
24+
25+
[Sources]
26+
SecPeiTdxMeasurement.c
27+
TdxMeasurementCommon.c
28+
29+
[Packages]
30+
CryptoPkg/CryptoPkg.dec
31+
MdeModulePkg/MdeModulePkg.dec
32+
MdePkg/MdePkg.dec
33+
UefiCpuPkg/UefiCpuPkg.dec
34+
SecurityPkg/SecurityPkg.dec
35+
36+
[LibraryClasses]
37+
BaseLib
38+
BaseCryptLib
39+
DebugLib
40+
HobLib
41+
PcdLib
42+
TdxLib
43+
44+
[Guids]
45+
gCcEventEntryHobGuid
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/** @file
2+
TdxMeasurement Common Functions
3+
4+
Copyright (c) 2025, Intel Corporation. All rights reserved.<BR>
5+
SPDX-License-Identifier: BSD-2-Clause-Patent
6+
**/
7+
8+
#include <PiPei.h>
9+
#include <Ppi/CcMeasurement.h>
10+
#include <Library/DebugLib.h>
11+
#include <Library/PeiServicesLib.h>
12+
#include <Library/TdxLib.h>
13+
#include <Library/BaseMemoryLib.h>
14+
#include <Library/BaseCryptLib.h>
15+
#include <Library/TdxMeasurementLib.h>
16+
17+
/**
18+
According to UEFI Spec 2.10 Section 38.4.1:
19+
The following table shows the TPM PCR index mapping and CC event log measurement
20+
register index interpretation for Intel TDX, where MRTD means Trust Domain Measurement
21+
Register and RTMR means Runtime Measurement Register
22+
// TPM PCR Index | CC Measurement Register Index | TDX-measurement register
23+
// ------------------------------------------------------------------------
24+
// 0 | 0 | MRTD
25+
// 1, 7 | 1 | RTMR[0]
26+
// 2~6 | 2 | RTMR[1]
27+
// 8~15 | 3 | RTMR[2]
28+
@param[in] PCRIndex Index of the TPM PCR
29+
@retval UINT32 Index of the CC Event Log Measurement Register Index
30+
@retval CC_MR_INDEX_INVALID Invalid MR Index
31+
**/
32+
UINT32
33+
EFIAPI
34+
TdxMeasurementMapPcrToMrIndex (
35+
IN UINT32 PCRIndex
36+
)
37+
{
38+
UINT32 MrIndex;
39+
40+
if (PCRIndex > 15) {
41+
ASSERT (FALSE);
42+
return CC_MR_INDEX_INVALID;
43+
}
44+
45+
MrIndex = 0;
46+
if (PCRIndex == 0) {
47+
MrIndex = CC_MR_INDEX_0_MRTD;
48+
} else if ((PCRIndex == 1) || (PCRIndex == 7)) {
49+
MrIndex = CC_MR_INDEX_1_RTMR0;
50+
} else if ((PCRIndex >= 2) && (PCRIndex <= 6)) {
51+
MrIndex = CC_MR_INDEX_2_RTMR1;
52+
} else if ((PCRIndex >= 8) && (PCRIndex <= 15)) {
53+
MrIndex = CC_MR_INDEX_3_RTMR2;
54+
}
55+
56+
return MrIndex;
57+
}
58+
59+
/**
60+
* Calculate the sha384 of input Data and extend it to RTMR register.
61+
*
62+
* @param RtmrIndex Index of the RTMR register
63+
* @param DataToHash Data to be hashed
64+
* @param DataToHashLen Length of the data
65+
* @param Digest Hash value of the input data
66+
* @param DigestLen Length of the hash value
67+
*
68+
* @retval EFI_SUCCESS Successfully hash and extend to RTMR
69+
* @retval Others Other errors as indicated
70+
*/
71+
EFI_STATUS
72+
EFIAPI
73+
TdxMeasurementHashAndExtendToRtmr (
74+
IN UINT32 RtmrIndex,
75+
IN VOID *DataToHash,
76+
IN UINTN DataToHashLen,
77+
OUT UINT8 *Digest,
78+
IN UINTN DigestLen
79+
)
80+
{
81+
EFI_STATUS Status;
82+
83+
if ((DataToHash == NULL) || (DataToHashLen == 0)) {
84+
return EFI_INVALID_PARAMETER;
85+
}
86+
87+
if ((Digest == NULL) || (DigestLen != SHA384_DIGEST_SIZE)) {
88+
return EFI_INVALID_PARAMETER;
89+
}
90+
91+
//
92+
// Calculate the sha384 of the data
93+
//
94+
if (!Sha384HashAll (DataToHash, DataToHashLen, Digest)) {
95+
return EFI_ABORTED;
96+
}
97+
98+
//
99+
// Extend to RTMR
100+
//
101+
Status = TdExtendRtmr (
102+
(UINT32 *)Digest,
103+
SHA384_DIGEST_SIZE,
104+
(UINT8)RtmrIndex
105+
);
106+
ASSERT (!EFI_ERROR (Status));
107+
return Status;
108+
}

0 commit comments

Comments
 (0)