Coverage Report

Created: 2026-10-23 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tpm2/Hierarchy.c
Line
Count
Source
1
// This file was extracted from the TCG Published
2
// Trusted Platform Module Library
3
// Part 4: Supporting Routines
4
// Family "2.0"
5
// Level 00 Revision 01.16
6
// October 30, 2014
7
8
#include "InternalRoutines.h"
9
//
10
//
11
//             Functions
12
//
13
//               HierarchyPreInstall()
14
//
15
//      This function performs the initialization functions for the hierarchy when the TPM is simulated. This
16
//      function should not be called if the TPM is not in a manufacturing mode at the manufacturer, or in a
17
//      simulated environment.
18
//
19
void
20
HierarchyPreInstall_Init(
21
     void
22
     )
23
277
{
24
     // Allow lockout clear command
25
277
     gp.disableClear = FALSE;
26
     // Initialize Primary Seeds
27
277
     gp.EPSeed.t.size = PRIMARY_SEED_SIZE;
28
277
     CryptGenerateRandom(PRIMARY_SEED_SIZE, gp.EPSeed.t.buffer);
29
277
     gp.SPSeed.t.size = PRIMARY_SEED_SIZE;
30
277
     CryptGenerateRandom(PRIMARY_SEED_SIZE, gp.SPSeed.t.buffer);
31
277
     gp.PPSeed.t.size = PRIMARY_SEED_SIZE;
32
277
     CryptGenerateRandom(PRIMARY_SEED_SIZE, gp.PPSeed.t.buffer);
33
     // Initialize owner, endorsement and lockout auth
34
277
     gp.ownerAuth.t.size = 0;
35
277
     gp.endorsementAuth.t.size = 0;
36
277
     gp.lockoutAuth.t.size = 0;
37
     // Initialize owner, endorsement, and lockout policy
38
277
     gp.ownerAlg = TPM_ALG_NULL;
39
277
     gp.ownerPolicy.t.size = 0;
40
277
     gp.endorsementAlg = TPM_ALG_NULL;
41
277
     gp.endorsementPolicy.t.size = 0;
42
277
     gp.lockoutAlg = TPM_ALG_NULL;
43
277
     gp.lockoutPolicy.t.size = 0;
44
    // Initialize ehProof, shProof and phProof
45
277
    gp.phProof.t.size = PROOF_SIZE;
46
277
    gp.shProof.t.size = PROOF_SIZE;
47
277
    gp.ehProof.t.size = PROOF_SIZE;
48
277
    CryptGenerateRandom(gp.phProof.t.size, gp.phProof.t.buffer);
49
277
    CryptGenerateRandom(gp.shProof.t.size, gp.shProof.t.buffer);
50
277
    CryptGenerateRandom(gp.ehProof.t.size, gp.ehProof.t.buffer);
51
    // Write hierarchy data to NV
52
277
    NvWriteReserved(NV_DISABLE_CLEAR, &gp.disableClear);
53
277
    NvWriteReserved(NV_EP_SEED, &gp.EPSeed);
54
277
    NvWriteReserved(NV_SP_SEED, &gp.SPSeed);
55
277
    NvWriteReserved(NV_PP_SEED, &gp.PPSeed);
56
277
    NvWriteReserved(NV_OWNER_AUTH, &gp.ownerAuth);
57
277
    NvWriteReserved(NV_ENDORSEMENT_AUTH, &gp.endorsementAuth);
58
277
    NvWriteReserved(NV_LOCKOUT_AUTH, &gp.lockoutAuth);
59
277
    NvWriteReserved(NV_OWNER_ALG, &gp.ownerAlg);
60
277
    NvWriteReserved(NV_OWNER_POLICY, &gp.ownerPolicy);
61
277
    NvWriteReserved(NV_ENDORSEMENT_ALG, &gp.endorsementAlg);
62
277
    NvWriteReserved(NV_ENDORSEMENT_POLICY, &gp.endorsementPolicy);
63
277
    NvWriteReserved(NV_LOCKOUT_ALG, &gp.lockoutAlg);
64
277
    NvWriteReserved(NV_LOCKOUT_POLICY, &gp.lockoutPolicy);
65
277
    NvWriteReserved(NV_PH_PROOF, &gp.phProof);
66
277
    NvWriteReserved(NV_SH_PROOF, &gp.shProof);
67
277
    NvWriteReserved(NV_EH_PROOF, &gp.ehProof);
68
277
    return;
69
277
}
70
//
71
//
72
//          HierarchyStartup()
73
//
74
//     This function is called at TPM2_Startup() to initialize the hierarchy related values.
75
//
76
void
77
HierarchyStartup(
78
    STARTUP_TYPE         type                // IN: start up type
79
    )
80
277
{
81
    // phEnable is SET on any startup
82
277
    g_phEnable = TRUE;
83
    // Reset platformAuth, platformPolicy; enable SH and EH at TPM_RESET and
84
    // TPM_RESTART
85
277
    if(type != SU_RESUME)
86
277
    {
87
277
        gc.platformAuth.t.size = 0;
88
277
        gc.platformPolicy.t.size = 0;
89
         // enable the storage and endorsement hierarchies and the platformNV
90
277
         gc.shEnable = gc.ehEnable = gc.phEnableNV = TRUE;
91
277
    }
92
    // nullProof and nullSeed are updated at every TPM_RESET
93
277
    if(type == SU_RESET)
94
277
    {
95
277
        gr.nullProof.t.size = PROOF_SIZE;
96
277
        CryptGenerateRandom(gr.nullProof.t.size,
97
277
                            gr.nullProof.t.buffer);
98
277
        gr.nullSeed.t.size = PRIMARY_SEED_SIZE;
99
277
        CryptGenerateRandom(PRIMARY_SEED_SIZE, gr.nullSeed.t.buffer);
100
277
    }
101
277
    return;
102
277
}
103
//
104
//           HierarchyGetProof()
105
//
106
//      This function finds the proof value associated with a hierarchy.It returns a pointer to the proof value.
107
//
108
TPM2B_AUTH *
109
HierarchyGetProof(
110
    TPMI_RH_HIERARCHY         hierarchy           // IN: hierarchy constant
111
    )
112
1
{
113
1
    TPM2B_AUTH               *auth = NULL;
114
1
    switch(hierarchy)
115
1
    {
116
0
    case TPM_RH_PLATFORM:
117
        // phProof for TPM_RH_PLATFORM
118
0
        auth = &gp.phProof;
119
0
        break;
120
1
    case TPM_RH_ENDORSEMENT:
121
        // ehProof for TPM_RH_ENDORSEMENT
122
1
        auth = &gp.ehProof;
123
1
        break;
124
0
    case TPM_RH_OWNER:
125
        // shProof for TPM_RH_OWNER
126
0
        auth = &gp.shProof;
127
0
        break;
128
0
    case TPM_RH_NULL:
129
        // nullProof for TPM_RH_NULL
130
0
        auth = &gr.nullProof;
131
0
        break;
132
0
    default:
133
0
        pAssert(FALSE);
134
0
        break;
135
1
    }
136
1
    return auth;
137
1
}
138
//
139
//
140
//           HierarchyGetPrimarySeed()
141
//
142
//      This function returns the primary seed of a hierarchy.
143
//
144
TPM2B_SEED *
145
HierarchyGetPrimarySeed(
146
    TPMI_RH_HIERARCHY         hierarchy           // IN: hierarchy
147
    )
148
2
{
149
2
    TPM2B_SEED          *seed = NULL;
150
2
    switch(hierarchy)
151
2
    {
152
0
    case TPM_RH_PLATFORM:
153
0
        seed = &gp.PPSeed;
154
0
        break;
155
0
    case TPM_RH_OWNER:
156
0
        seed = &gp.SPSeed;
157
0
        break;
158
2
    case TPM_RH_ENDORSEMENT:
159
2
        seed = &gp.EPSeed;
160
2
        break;
161
0
    case TPM_RH_NULL:
162
0
        return &gr.nullSeed;
163
0
    default:
164
0
        pAssert(FALSE);
165
0
        break;
166
2
    }
167
2
     return seed;
168
2
}
169
//
170
//
171
//            HierarchyIsEnabled()
172
//
173
//      This function checks to see if a hierarchy is enabled.
174
//
175
//      NOTE:           The TPM_RH_NULL hierarchy is always enabled.
176
//
177
//
178
//      Return Value                     Meaning
179
//
180
//      TRUE                             hierarchy is enabled
181
//      FALSE                            hierarchy is disabled
182
//
183
BOOL
184
HierarchyIsEnabled(
185
     TPMI_RH_HIERARCHY        hierarchy           // IN: hierarchy
186
     )
187
56
{
188
56
     BOOL               enabled = FALSE;
189
56
     switch(hierarchy)
190
56
     {
191
0
     case TPM_RH_PLATFORM:
192
0
         enabled = g_phEnable;
193
0
         break;
194
0
     case TPM_RH_OWNER:
195
0
         enabled = gc.shEnable;
196
0
         break;
197
0
     case TPM_RH_ENDORSEMENT:
198
0
         enabled = gc.ehEnable;
199
0
         break;
200
56
     case TPM_RH_NULL:
201
56
         enabled = TRUE;
202
56
         break;
203
0
     default:
204
0
         pAssert(FALSE);
205
0
         break;
206
56
     }
207
56
     return enabled;
208
56
}