xref: /aosp_15_r20/external/ms-tpm-20-ref/TPMCmd/tpm/src/subsystem/Hierarchy.c (revision 5c591343844d1f9da7da26467c4bf7efc8a7a413)
1*5c591343SA. Cody Schuffelen /* Microsoft Reference Implementation for TPM 2.0
2*5c591343SA. Cody Schuffelen  *
3*5c591343SA. Cody Schuffelen  *  The copyright in this software is being made available under the BSD License,
4*5c591343SA. Cody Schuffelen  *  included below. This software may be subject to other third party and
5*5c591343SA. Cody Schuffelen  *  contributor rights, including patent rights, and no such rights are granted
6*5c591343SA. Cody Schuffelen  *  under this license.
7*5c591343SA. Cody Schuffelen  *
8*5c591343SA. Cody Schuffelen  *  Copyright (c) Microsoft Corporation
9*5c591343SA. Cody Schuffelen  *
10*5c591343SA. Cody Schuffelen  *  All rights reserved.
11*5c591343SA. Cody Schuffelen  *
12*5c591343SA. Cody Schuffelen  *  BSD License
13*5c591343SA. Cody Schuffelen  *
14*5c591343SA. Cody Schuffelen  *  Redistribution and use in source and binary forms, with or without modification,
15*5c591343SA. Cody Schuffelen  *  are permitted provided that the following conditions are met:
16*5c591343SA. Cody Schuffelen  *
17*5c591343SA. Cody Schuffelen  *  Redistributions of source code must retain the above copyright notice, this list
18*5c591343SA. Cody Schuffelen  *  of conditions and the following disclaimer.
19*5c591343SA. Cody Schuffelen  *
20*5c591343SA. Cody Schuffelen  *  Redistributions in binary form must reproduce the above copyright notice, this
21*5c591343SA. Cody Schuffelen  *  list of conditions and the following disclaimer in the documentation and/or
22*5c591343SA. Cody Schuffelen  *  other materials provided with the distribution.
23*5c591343SA. Cody Schuffelen  *
24*5c591343SA. Cody Schuffelen  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
25*5c591343SA. Cody Schuffelen  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26*5c591343SA. Cody Schuffelen  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27*5c591343SA. Cody Schuffelen  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28*5c591343SA. Cody Schuffelen  *  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29*5c591343SA. Cody Schuffelen  *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30*5c591343SA. Cody Schuffelen  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31*5c591343SA. Cody Schuffelen  *  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32*5c591343SA. Cody Schuffelen  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33*5c591343SA. Cody Schuffelen  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*5c591343SA. Cody Schuffelen  */
35*5c591343SA. Cody Schuffelen //** Introduction
36*5c591343SA. Cody Schuffelen // This file contains the functions used for managing and accessing the
37*5c591343SA. Cody Schuffelen // hierarchy-related values.
38*5c591343SA. Cody Schuffelen 
39*5c591343SA. Cody Schuffelen //** Includes
40*5c591343SA. Cody Schuffelen 
41*5c591343SA. Cody Schuffelen #include "Tpm.h"
42*5c591343SA. Cody Schuffelen 
43*5c591343SA. Cody Schuffelen //** Functions
44*5c591343SA. Cody Schuffelen 
45*5c591343SA. Cody Schuffelen //*** HierarchyPreInstall()
46*5c591343SA. Cody Schuffelen // This function performs the initialization functions for the hierarchy
47*5c591343SA. Cody Schuffelen // when the TPM is simulated. This function should not be called if the
48*5c591343SA. Cody Schuffelen // TPM is not in a manufacturing mode at the manufacturer, or in a simulated
49*5c591343SA. Cody Schuffelen // environment.
50*5c591343SA. Cody Schuffelen void
HierarchyPreInstall_Init(void)51*5c591343SA. Cody Schuffelen HierarchyPreInstall_Init(
52*5c591343SA. Cody Schuffelen     void
53*5c591343SA. Cody Schuffelen     )
54*5c591343SA. Cody Schuffelen {
55*5c591343SA. Cody Schuffelen     // Allow lockout clear command
56*5c591343SA. Cody Schuffelen     gp.disableClear = FALSE;
57*5c591343SA. Cody Schuffelen 
58*5c591343SA. Cody Schuffelen     // Initialize Primary Seeds
59*5c591343SA. Cody Schuffelen     gp.EPSeed.t.size = sizeof(gp.EPSeed.t.buffer);
60*5c591343SA. Cody Schuffelen     gp.SPSeed.t.size = sizeof(gp.SPSeed.t.buffer);
61*5c591343SA. Cody Schuffelen     gp.PPSeed.t.size = sizeof(gp.PPSeed.t.buffer);
62*5c591343SA. Cody Schuffelen #if (defined USE_PLATFORM_EPS) && (USE_PLATFORM_EPS != NO)
63*5c591343SA. Cody Schuffelen     _plat__GetEPS(gp.EPSeed.t.size, gp.EPSeed.t.buffer);
64*5c591343SA. Cody Schuffelen #else
65*5c591343SA. Cody Schuffelen     CryptRandomGenerate(gp.EPSeed.t.size, gp.EPSeed.t.buffer);
66*5c591343SA. Cody Schuffelen #endif
67*5c591343SA. Cody Schuffelen     CryptRandomGenerate(gp.SPSeed.t.size, gp.SPSeed.t.buffer);
68*5c591343SA. Cody Schuffelen     CryptRandomGenerate(gp.PPSeed.t.size, gp.PPSeed.t.buffer);
69*5c591343SA. Cody Schuffelen 
70*5c591343SA. Cody Schuffelen     // Initialize owner, endorsement and lockout authorization
71*5c591343SA. Cody Schuffelen     gp.ownerAuth.t.size = 0;
72*5c591343SA. Cody Schuffelen     gp.endorsementAuth.t.size = 0;
73*5c591343SA. Cody Schuffelen     gp.lockoutAuth.t.size = 0;
74*5c591343SA. Cody Schuffelen 
75*5c591343SA. Cody Schuffelen     // Initialize owner, endorsement, and lockout policy
76*5c591343SA. Cody Schuffelen     gp.ownerAlg = TPM_ALG_NULL;
77*5c591343SA. Cody Schuffelen     gp.ownerPolicy.t.size = 0;
78*5c591343SA. Cody Schuffelen     gp.endorsementAlg = TPM_ALG_NULL;
79*5c591343SA. Cody Schuffelen     gp.endorsementPolicy.t.size = 0;
80*5c591343SA. Cody Schuffelen     gp.lockoutAlg = TPM_ALG_NULL;
81*5c591343SA. Cody Schuffelen     gp.lockoutPolicy.t.size = 0;
82*5c591343SA. Cody Schuffelen 
83*5c591343SA. Cody Schuffelen     // Initialize ehProof, shProof and phProof
84*5c591343SA. Cody Schuffelen     gp.phProof.t.size = sizeof(gp.phProof.t.buffer);
85*5c591343SA. Cody Schuffelen     gp.shProof.t.size = sizeof(gp.shProof.t.buffer);
86*5c591343SA. Cody Schuffelen     gp.ehProof.t.size = sizeof(gp.ehProof.t.buffer);
87*5c591343SA. Cody Schuffelen     CryptRandomGenerate(gp.phProof.t.size, gp.phProof.t.buffer);
88*5c591343SA. Cody Schuffelen     CryptRandomGenerate(gp.shProof.t.size, gp.shProof.t.buffer);
89*5c591343SA. Cody Schuffelen     CryptRandomGenerate(gp.ehProof.t.size, gp.ehProof.t.buffer);
90*5c591343SA. Cody Schuffelen 
91*5c591343SA. Cody Schuffelen     // Write hierarchy data to NV
92*5c591343SA. Cody Schuffelen     NV_SYNC_PERSISTENT(disableClear);
93*5c591343SA. Cody Schuffelen     NV_SYNC_PERSISTENT(EPSeed);
94*5c591343SA. Cody Schuffelen     NV_SYNC_PERSISTENT(SPSeed);
95*5c591343SA. Cody Schuffelen     NV_SYNC_PERSISTENT(PPSeed);
96*5c591343SA. Cody Schuffelen     NV_SYNC_PERSISTENT(ownerAuth);
97*5c591343SA. Cody Schuffelen     NV_SYNC_PERSISTENT(endorsementAuth);
98*5c591343SA. Cody Schuffelen     NV_SYNC_PERSISTENT(lockoutAuth);
99*5c591343SA. Cody Schuffelen     NV_SYNC_PERSISTENT(ownerAlg);
100*5c591343SA. Cody Schuffelen     NV_SYNC_PERSISTENT(ownerPolicy);
101*5c591343SA. Cody Schuffelen     NV_SYNC_PERSISTENT(endorsementAlg);
102*5c591343SA. Cody Schuffelen     NV_SYNC_PERSISTENT(endorsementPolicy);
103*5c591343SA. Cody Schuffelen     NV_SYNC_PERSISTENT(lockoutAlg);
104*5c591343SA. Cody Schuffelen     NV_SYNC_PERSISTENT(lockoutPolicy);
105*5c591343SA. Cody Schuffelen     NV_SYNC_PERSISTENT(phProof);
106*5c591343SA. Cody Schuffelen     NV_SYNC_PERSISTENT(shProof);
107*5c591343SA. Cody Schuffelen     NV_SYNC_PERSISTENT(ehProof);
108*5c591343SA. Cody Schuffelen 
109*5c591343SA. Cody Schuffelen     return;
110*5c591343SA. Cody Schuffelen }
111*5c591343SA. Cody Schuffelen 
112*5c591343SA. Cody Schuffelen //*** HierarchyStartup()
113*5c591343SA. Cody Schuffelen // This function is called at TPM2_Startup() to initialize the hierarchy
114*5c591343SA. Cody Schuffelen // related values.
115*5c591343SA. Cody Schuffelen BOOL
HierarchyStartup(STARTUP_TYPE type)116*5c591343SA. Cody Schuffelen HierarchyStartup(
117*5c591343SA. Cody Schuffelen     STARTUP_TYPE     type           // IN: start up type
118*5c591343SA. Cody Schuffelen     )
119*5c591343SA. Cody Schuffelen {
120*5c591343SA. Cody Schuffelen     // phEnable is SET on any startup
121*5c591343SA. Cody Schuffelen     g_phEnable = TRUE;
122*5c591343SA. Cody Schuffelen 
123*5c591343SA. Cody Schuffelen     // Reset platformAuth, platformPolicy; enable SH and EH at TPM_RESET and
124*5c591343SA. Cody Schuffelen     // TPM_RESTART
125*5c591343SA. Cody Schuffelen     if(type != SU_RESUME)
126*5c591343SA. Cody Schuffelen     {
127*5c591343SA. Cody Schuffelen         gc.platformAuth.t.size = 0;
128*5c591343SA. Cody Schuffelen         gc.platformPolicy.t.size = 0;
129*5c591343SA. Cody Schuffelen         gc.platformAlg = TPM_ALG_NULL;
130*5c591343SA. Cody Schuffelen 
131*5c591343SA. Cody Schuffelen         // enable the storage and endorsement hierarchies and the platformNV
132*5c591343SA. Cody Schuffelen         gc.shEnable = gc.ehEnable = gc.phEnableNV = TRUE;
133*5c591343SA. Cody Schuffelen     }
134*5c591343SA. Cody Schuffelen 
135*5c591343SA. Cody Schuffelen     // nullProof and nullSeed are updated at every TPM_RESET
136*5c591343SA. Cody Schuffelen     if((type != SU_RESTART) && (type != SU_RESUME))
137*5c591343SA. Cody Schuffelen     {
138*5c591343SA. Cody Schuffelen         gr.nullProof.t.size = sizeof(gr.nullProof.t.buffer);
139*5c591343SA. Cody Schuffelen         CryptRandomGenerate(gr.nullProof.t.size, gr.nullProof.t.buffer);
140*5c591343SA. Cody Schuffelen         gr.nullSeed.t.size = sizeof(gr.nullSeed.t.buffer);
141*5c591343SA. Cody Schuffelen         CryptRandomGenerate(gr.nullSeed.t.size, gr.nullSeed.t.buffer);
142*5c591343SA. Cody Schuffelen     }
143*5c591343SA. Cody Schuffelen 
144*5c591343SA. Cody Schuffelen     return TRUE;
145*5c591343SA. Cody Schuffelen }
146*5c591343SA. Cody Schuffelen 
147*5c591343SA. Cody Schuffelen //*** HierarchyGetProof()
148*5c591343SA. Cody Schuffelen // This function finds the proof value associated with a hierarchy.It returns a
149*5c591343SA. Cody Schuffelen // pointer to the proof value.
150*5c591343SA. Cody Schuffelen TPM2B_PROOF *
HierarchyGetProof(TPMI_RH_HIERARCHY hierarchy)151*5c591343SA. Cody Schuffelen HierarchyGetProof(
152*5c591343SA. Cody Schuffelen     TPMI_RH_HIERARCHY    hierarchy      // IN: hierarchy constant
153*5c591343SA. Cody Schuffelen     )
154*5c591343SA. Cody Schuffelen {
155*5c591343SA. Cody Schuffelen     TPM2B_PROOF         *proof = NULL;
156*5c591343SA. Cody Schuffelen 
157*5c591343SA. Cody Schuffelen     switch(hierarchy)
158*5c591343SA. Cody Schuffelen     {
159*5c591343SA. Cody Schuffelen         case TPM_RH_PLATFORM:
160*5c591343SA. Cody Schuffelen             // phProof for TPM_RH_PLATFORM
161*5c591343SA. Cody Schuffelen             proof = &gp.phProof;
162*5c591343SA. Cody Schuffelen             break;
163*5c591343SA. Cody Schuffelen         case TPM_RH_ENDORSEMENT:
164*5c591343SA. Cody Schuffelen             // ehProof for TPM_RH_ENDORSEMENT
165*5c591343SA. Cody Schuffelen             proof = &gp.ehProof;
166*5c591343SA. Cody Schuffelen             break;
167*5c591343SA. Cody Schuffelen         case TPM_RH_OWNER:
168*5c591343SA. Cody Schuffelen             // shProof for TPM_RH_OWNER
169*5c591343SA. Cody Schuffelen             proof = &gp.shProof;
170*5c591343SA. Cody Schuffelen             break;
171*5c591343SA. Cody Schuffelen         default:
172*5c591343SA. Cody Schuffelen             // nullProof for TPM_RH_NULL or anything else
173*5c591343SA. Cody Schuffelen             proof = &gr.nullProof;
174*5c591343SA. Cody Schuffelen             break;
175*5c591343SA. Cody Schuffelen     }
176*5c591343SA. Cody Schuffelen     return proof;
177*5c591343SA. Cody Schuffelen }
178*5c591343SA. Cody Schuffelen 
179*5c591343SA. Cody Schuffelen //*** HierarchyGetPrimarySeed()
180*5c591343SA. Cody Schuffelen // This function returns the primary seed of a hierarchy.
181*5c591343SA. Cody Schuffelen TPM2B_SEED *
HierarchyGetPrimarySeed(TPMI_RH_HIERARCHY hierarchy)182*5c591343SA. Cody Schuffelen HierarchyGetPrimarySeed(
183*5c591343SA. Cody Schuffelen     TPMI_RH_HIERARCHY    hierarchy      // IN: hierarchy
184*5c591343SA. Cody Schuffelen     )
185*5c591343SA. Cody Schuffelen {
186*5c591343SA. Cody Schuffelen     TPM2B_SEED          *seed = NULL;
187*5c591343SA. Cody Schuffelen     switch(hierarchy)
188*5c591343SA. Cody Schuffelen     {
189*5c591343SA. Cody Schuffelen         case TPM_RH_PLATFORM:
190*5c591343SA. Cody Schuffelen             seed = &gp.PPSeed;
191*5c591343SA. Cody Schuffelen             break;
192*5c591343SA. Cody Schuffelen         case TPM_RH_OWNER:
193*5c591343SA. Cody Schuffelen             seed = &gp.SPSeed;
194*5c591343SA. Cody Schuffelen             break;
195*5c591343SA. Cody Schuffelen         case TPM_RH_ENDORSEMENT:
196*5c591343SA. Cody Schuffelen             seed = &gp.EPSeed;
197*5c591343SA. Cody Schuffelen             break;
198*5c591343SA. Cody Schuffelen          default:
199*5c591343SA. Cody Schuffelen             seed = &gr.nullSeed;
200*5c591343SA. Cody Schuffelen             break;
201*5c591343SA. Cody Schuffelen     }
202*5c591343SA. Cody Schuffelen     return seed;
203*5c591343SA. Cody Schuffelen }
204*5c591343SA. Cody Schuffelen 
205*5c591343SA. Cody Schuffelen //*** HierarchyIsEnabled()
206*5c591343SA. Cody Schuffelen // This function checks to see if a hierarchy is enabled.
207*5c591343SA. Cody Schuffelen // NOTE: The TPM_RH_NULL hierarchy is always enabled.
208*5c591343SA. Cody Schuffelen //  Return Type: BOOL
209*5c591343SA. Cody Schuffelen //      TRUE(1)         hierarchy is enabled
210*5c591343SA. Cody Schuffelen //      FALSE(0)        hierarchy is disabled
211*5c591343SA. Cody Schuffelen BOOL
HierarchyIsEnabled(TPMI_RH_HIERARCHY hierarchy)212*5c591343SA. Cody Schuffelen HierarchyIsEnabled(
213*5c591343SA. Cody Schuffelen     TPMI_RH_HIERARCHY    hierarchy      // IN: hierarchy
214*5c591343SA. Cody Schuffelen     )
215*5c591343SA. Cody Schuffelen {
216*5c591343SA. Cody Schuffelen     BOOL            enabled = FALSE;
217*5c591343SA. Cody Schuffelen 
218*5c591343SA. Cody Schuffelen     switch(hierarchy)
219*5c591343SA. Cody Schuffelen     {
220*5c591343SA. Cody Schuffelen         case TPM_RH_PLATFORM:
221*5c591343SA. Cody Schuffelen             enabled = g_phEnable;
222*5c591343SA. Cody Schuffelen             break;
223*5c591343SA. Cody Schuffelen         case TPM_RH_OWNER:
224*5c591343SA. Cody Schuffelen             enabled = gc.shEnable;
225*5c591343SA. Cody Schuffelen             break;
226*5c591343SA. Cody Schuffelen         case TPM_RH_ENDORSEMENT:
227*5c591343SA. Cody Schuffelen             enabled = gc.ehEnable;
228*5c591343SA. Cody Schuffelen             break;
229*5c591343SA. Cody Schuffelen         case TPM_RH_NULL:
230*5c591343SA. Cody Schuffelen             enabled = TRUE;
231*5c591343SA. Cody Schuffelen             break;
232*5c591343SA. Cody Schuffelen         default:
233*5c591343SA. Cody Schuffelen             enabled = FALSE;
234*5c591343SA. Cody Schuffelen             break;
235*5c591343SA. Cody Schuffelen     }
236*5c591343SA. Cody Schuffelen     return enabled;
237*5c591343SA. Cody Schuffelen }