Debian Codegraph @

simulator/ms-tpm-20-ref/TPMCmd/tpm/src/command/Context/Context_spt.c

raw
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 //** Includes
36
37 #include "Tpm.h"
38 #include "Context_spt_fp.h"
39
40 //** Functions
41
42 //*** ComputeContextProtectionKey()
43 // This function retrieves the symmetric protection key for context encryption
44 // It is used by TPM2_ConextSave and TPM2_ContextLoad to create the symmetric
45 // encryption key and iv
46 /*(See part 1 specification)
47 KDFa is used to generate the symmetric encryption key and IV. The parameters
48 of the call are:
49 Symkey = KDFa(hashAlg, hProof, vendorString, sequence, handle, bits)
50 where
51 hashAlg a vendor-defined hash algorithm
52 hProof the hierarchy proof as selected by the hierarchy parameter
53 of the TPMS_CONTEXT
54 vendorString a value used to differentiate the uses of the KDF
55 sequence the sequence parameter of the TPMS_CONTEXT
56 handle the handle parameter of the TPMS_CONTEXT
57 bits the number of bits needed for a symmetric key and IV for
58 the context encryption
59 */
60 // Return Type: void
61 void
62 ComputeContextProtectionKey(
63 TPMS_CONTEXT *contextBlob, // IN: context blob
64 TPM2B_SYM_KEY *symKey, // OUT: the symmetric key
65 TPM2B_IV *iv // OUT: the IV.
66 )
67 {
68 UINT16 symKeyBits; // number of bits in the parent's
69 // symmetric key
70 TPM2B_PROOF *proof = NULL; // the proof value to use. Is null for
71 // everything but a primary object in
72 // the Endorsement Hierarchy
73
74 BYTE kdfResult[sizeof(TPMU_HA) * 2];// Value produced by the KDF
75
76 TPM2B_DATA sequence2B, handle2B;
77
78 // Get proof value
79 proof = HierarchyGetProof(contextBlob->hierarchy);
80
81 // Get sequence value in 2B format
82 sequence2B.t.size = sizeof(contextBlob->sequence);
83 cAssert(sizeof(contextBlob->sequence) <= sizeof(sequence2B.t.buffer));
84 MemoryCopy(sequence2B.t.buffer, &contextBlob->sequence,
85 sizeof(contextBlob->sequence));
86
87 // Get handle value in 2B format
88 handle2B.t.size = sizeof(contextBlob->savedHandle);
89 cAssert(sizeof(contextBlob->savedHandle) <= sizeof(handle2B.t.buffer));
90 MemoryCopy(handle2B.t.buffer, &contextBlob->savedHandle,
91 sizeof(contextBlob->savedHandle));
92
93 // Get the symmetric encryption key size
94 symKey->t.size = CONTEXT_ENCRYPT_KEY_BYTES;
95 symKeyBits = CONTEXT_ENCRYPT_KEY_BITS;
96 // Get the size of the IV for the algorithm
97 iv->t.size = CryptGetSymmetricBlockSize(CONTEXT_ENCRYPT_ALG, symKeyBits);
98
99 // KDFa to generate symmetric key and IV value
100 CryptKDFa(CONTEXT_INTEGRITY_HASH_ALG, &proof->b, CONTEXT_KEY, &sequence2B.b,
101 &handle2B.b, (symKey->t.size + iv->t.size) * 8, kdfResult, NULL,
102 FALSE);
103
104 // Copy part of the returned value as the key
105 pAssert(symKey->t.size <= sizeof(symKey->t.buffer));
106 MemoryCopy(symKey->t.buffer, kdfResult, symKey->t.size);
107
108 // Copy the rest as the IV
109 pAssert(iv->t.size <= sizeof(iv->t.buffer));
110 MemoryCopy(iv->t.buffer, &kdfResult[symKey->t.size], iv->t.size);
111
112 return;
113 }
114
115 //*** ComputeContextIntegrity()
116 // Generate the integrity hash for a context
117 // It is used by TPM2_ContextSave to create an integrity hash
118 // and by TPM2_ContextLoad to compare an integrity hash
119 /*(See part 1 specification)
120 The HMAC integrity computation for a saved context is:
121 HMACvendorAlg(hProof, resetValue {|| clearCount} || sequence || handle ||
122 encContext)
123 where
124 HMACvendorAlg HMAC using a vendor-defined hash algorithm
125 hProof the hierarchy proof as selected by the hierarchy
126 parameter of the TPMS_CONTEXT
127 resetValue either a counter value that increments on each TPM Reset
128 and is not reset over the lifetime of the TPM or a random
129 value that changes on each TPM Reset and has the size of
130 the digest produced by vendorAlg
131 clearCount a counter value that is incremented on each TPM Reset
132 or TPM Restart. This value is only included if the handle
133 value is 0x80000002.
134 sequence the sequence parameter of the TPMS_CONTEXT
135 handle the handle parameter of the TPMS_CONTEXT
136 encContext the encrypted context blob
137 */
138 // Return Type: void
139 void
140 ComputeContextIntegrity(
141 TPMS_CONTEXT *contextBlob, // IN: context blob
142 TPM2B_DIGEST *integrity // OUT: integrity
143 )
144 {
145 HMAC_STATE hmacState;
146 TPM2B_PROOF *proof;
147 UINT16 integritySize;
148
149 // Get proof value
150 proof = HierarchyGetProof(contextBlob->hierarchy);
151
152 // Start HMAC
153 integrity->t.size = CryptHmacStart2B(&hmacState, CONTEXT_INTEGRITY_HASH_ALG,
154 &proof->b);
155
156 // Compute integrity size at the beginning of context blob
157 integritySize = sizeof(integrity->t.size) + integrity->t.size;
158
159 // Adding total reset counter so that the context cannot be
160 // used after a TPM Reset
161 CryptDigestUpdateInt(&hmacState.hashState, sizeof(gp.totalResetCount),
162 gp.totalResetCount);
163
164 // If this is a ST_CLEAR object, add the clear count
165 // so that this contest cannot be loaded after a TPM Restart
166 if(contextBlob->savedHandle == 0x80000002)
167 CryptDigestUpdateInt(&hmacState.hashState, sizeof(gr.clearCount),
168 gr.clearCount);
169
170 // Adding sequence number to the HMAC to make sure that it doesn't
171 // get changed
172 CryptDigestUpdateInt(&hmacState.hashState, sizeof(contextBlob->sequence),
173 contextBlob->sequence);
174
175 // Protect the handle
176 CryptDigestUpdateInt(&hmacState.hashState, sizeof(contextBlob->savedHandle),
177 contextBlob->savedHandle);
178
179 // Adding sensitive contextData, skip the leading integrity area
180 CryptDigestUpdate(&hmacState.hashState,
181 contextBlob->contextBlob.t.size - integritySize,
182 contextBlob->contextBlob.t.buffer + integritySize);
183
184 // Complete HMAC
185 CryptHmacEnd2B(&hmacState, &integrity->b);
186
187 return;
188 }
189
190 //*** SequenceDataExport();
191 // This function is used scan through the sequence object and
192 // either modify the hash state data for export (contextSave) or to
193 // import it into the internal format (contextLoad).
194 // This function should only be called after the sequence object has been copied
195 // to the context buffer (contextSave) or from the context buffer into the sequence
196 // object. The presumption is that the context buffer version of the data is the
197 // same size as the internal representation so nothing outsize of the hash context
198 // area gets modified.
199 void
200 SequenceDataExport(
201 HASH_OBJECT *object, // IN: an internal hash object
202 HASH_OBJECT_BUFFER *exportObject // OUT: a sequence context in a buffer
203 )
204 {
205 // If the hash object is not an event, then only one hash context is needed
206 int count = (object->attributes.eventSeq) ? HASH_COUNT : 1;
207
208 for(count--; count >= 0; count--)
209 {
210 HASH_STATE *hash = &object->state.hashState[count];
211 size_t offset = (BYTE *)hash - (BYTE *)object;
212 BYTE *exportHash = &((BYTE *)exportObject)[offset];
213
214 CryptHashExportState(hash, (EXPORT_HASH_STATE *)exportHash);
215 }
216 }
217
218 //*** SequenceDataImport();
219 // This function is used scan through the sequence object and
220 // either modify the hash state data for export (contextSave) or to
221 // import it into the internal format (contextLoad).
222 // This function should only be called after the sequence object has been copied
223 // to the context buffer (contextSave) or from the context buffer into the sequence
224 // object. The presumption is that the context buffer version of the data is the
225 // same size as the internal representation so nothing outsize of the hash context
226 // area gets modified.
227 void
228 SequenceDataImport(
229 HASH_OBJECT *object, // IN/OUT: an internal hash object
230 HASH_OBJECT_BUFFER *exportObject // IN/OUT: a sequence context in a buffer
231 )
232 {
233 // If the hash object is not an event, then only one hash context is needed
234 int count = (object->attributes.eventSeq) ? HASH_COUNT : 1;
235
236 for(count--; count >= 0; count--)
237 {
238 HASH_STATE *hash = &object->state.hashState[count];
239 size_t offset = (BYTE *)hash - (BYTE *)object;
240 BYTE *importHash = &((BYTE *)exportObject)[offset];
241 //
242 CryptHashImportState(hash, (EXPORT_HASH_STATE *)importHash);
243 }
244 }