1 /* Microsoft Reference Implementation for TPM 2.0
2 *
3 * The copyright in this software is being made available under the BSD License,
4 * included below. This software may be subject to other third party and
5 * contributor rights, including patent rights, and no such rights are granted
6 * under this license.
7 *
8 * Copyright (c) Microsoft Corporation
9 *
10 * All rights reserved.
11 *
12 * BSD License
13 *
14 * Redistribution and use in source and binary forms, with or without modification,
15 * are permitted provided that the following conditions are met:
16 *
17 * Redistributions of source code must retain the above copyright notice, this list
18 * of conditions and the following disclaimer.
19 *
20 * Redistributions in binary form must reproduce the above copyright notice, this
21 * list of conditions and the following disclaimer in the documentation and/or
22 * other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35 //** Description
36 // This file contains the function that performs the "manufacturing" of the TPM
37 // in a simulated environment. These functions should not be used outside of
38 // a manufacturing or simulation environment.
39
40 //** Includes and Data Definitions
41 #define MANUFACTURE_C
42 #include "Tpm.h"
43 #include "TpmSizeChecks_fp.h"
44
45 //** Functions
46
47 //*** TPM_Manufacture()
48 // This function initializes the TPM values in preparation for the TPM's first
49 // use. This function will fail if previously called. The TPM can be re-manufactured
50 // by calling TPM_Teardown() first and then calling this function again.
51 // Return Type: int
52 // -1 failure
53 // 0 success
54 // 1 manufacturing process previously performed
55 LIB_EXPORT int
TPM_Manufacture(int firstTime)56 TPM_Manufacture(
57 int firstTime // IN: indicates if this is the first call from
58 // main()
59 )
60 {
61 TPM_SU orderlyShutdown;
62
63 #if RUNTIME_SIZE_CHECKS
64 // Call the function to verify the sizes of values that result from different
65 // compile options.
66 if(!TpmSizeChecks())
67 return -1;
68 #endif
69 #if LIBRARY_COMPATIBILITY_CHECK
70 // Make sure that the attached library performs as expected.
71 if(!MathLibraryCompatibilityCheck())
72 return -1;
73 #endif
74
75 // If TPM has been manufactured, return indication.
76 if(!firstTime && g_manufactured)
77 return 1;
78
79 // Do power on initializations of the cryptographic libraries.
80 CryptInit();
81
82 s_DAPendingOnNV = FALSE;
83
84 // initialize NV
85 NvManufacture();
86
87 // Clear the magic value in the DRBG state
88 go.drbgState.magic = 0;
89
90 CryptStartup(SU_RESET);
91
92 // default configuration for PCR
93 PCRSimStart();
94
95 // initialize pre-installed hierarchy data
96 // This should happen after NV is initialized because hierarchy data is
97 // stored in NV.
98 HierarchyPreInstall_Init();
99
100 // initialize dictionary attack parameters
101 DAPreInstall_Init();
102
103 // initialize PP list
104 PhysicalPresencePreInstall_Init();
105
106 // initialize command audit list
107 CommandAuditPreInstall_Init();
108
109 // first start up is required to be Startup(CLEAR)
110 orderlyShutdown = TPM_SU_CLEAR;
111 NV_WRITE_PERSISTENT(orderlyState, orderlyShutdown);
112
113 // initialize the firmware version
114 gp.firmwareV1 = FIRMWARE_V1;
115 #ifdef FIRMWARE_V2
116 gp.firmwareV2 = FIRMWARE_V2;
117 #else
118 gp.firmwareV2 = 0;
119 #endif
120 NV_SYNC_PERSISTENT(firmwareV1);
121 NV_SYNC_PERSISTENT(firmwareV2);
122
123 // initialize the total reset counter to 0
124 gp.totalResetCount = 0;
125 NV_SYNC_PERSISTENT(totalResetCount);
126
127 // initialize the clock stuff
128 go.clock = 0;
129 go.clockSafe = YES;
130
131 NvWrite(NV_ORDERLY_DATA, sizeof(ORDERLY_DATA), &go);
132
133 // Commit NV writes. Manufacture process is an artificial process existing
134 // only in simulator environment and it is not defined in the specification
135 // that what should be the expected behavior if the NV write fails at this
136 // point. Therefore, it is assumed the NV write here is always success and
137 // no return code of this function is checked.
138 NvCommit();
139
140 g_manufactured = TRUE;
141
142 return 0;
143 }
144
145 //*** TPM_TearDown()
146 // This function prepares the TPM for re-manufacture. It should not be implemented
147 // in anything other than a simulated TPM.
148 //
149 // In this implementation, all that is needs is to stop the cryptographic units
150 // and set a flag to indicate that the TPM can be re-manufactured. This should
151 // be all that is necessary to start the manufacturing process again.
152 // Return Type: int
153 // 0 success
154 // 1 TPM not previously manufactured
155 LIB_EXPORT int
TPM_TearDown(void)156 TPM_TearDown(
157 void
158 )
159 {
160 g_manufactured = FALSE;
161 return 0;
162 }
163
164
165 //*** TpmEndSimulation()
166 // This function is called at the end of the simulation run. It is used to provoke
167 // printing of any statistics that might be needed.
168 LIB_EXPORT void
TpmEndSimulation(void)169 TpmEndSimulation(
170 void
171 )
172 {
173 #if SIMULATION
174 HashLibSimulationEnd();
175 SymLibSimulationEnd();
176 MathLibSimulationEnd();
177 #if ALG_RSA
178 RsaSimulationEnd();
179 #endif
180 #if ALG_ECC
181 EccSimulationEnd();
182 #endif
183 #endif // SIMULATION
184 }