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 | }
|
---|