summaryrefslogtreecommitdiffstats
path: root/src/tpm2/HashCommands.c
blob: ba74cc39ae92ada7a4b0eb1d3a20505a79b997f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/********************************************************************************/
/*										*/
/*			Hash/HMAC/Event Sequences	     			*/
/*			     Written by Ken Goldman				*/
/*		       IBM Thomas J. Watson Research Center			*/
/*            $Id: HashCommands.c 1490 2019-07-26 21:13:22Z kgoldman $		*/
/*										*/
/*  Licenses and Notices							*/
/*										*/
/*  1. Copyright Licenses:							*/
/*										*/
/*  - Trusted Computing Group (TCG) grants to the user of the source code in	*/
/*    this specification (the "Source Code") a worldwide, irrevocable, 		*/
/*    nonexclusive, royalty free, copyright license to reproduce, create 	*/
/*    derivative works, distribute, display and perform the Source Code and	*/
/*    derivative works thereof, and to grant others the rights granted herein.	*/
/*										*/
/*  - The TCG grants to the user of the other parts of the specification 	*/
/*    (other than the Source Code) the rights to reproduce, distribute, 	*/
/*    display, and perform the specification solely for the purpose of 		*/
/*    developing products based on such documents.				*/
/*										*/
/*  2. Source Code Distribution Conditions:					*/
/*										*/
/*  - Redistributions of Source Code must retain the above copyright licenses, 	*/
/*    this list of conditions and the following disclaimers.			*/
/*										*/
/*  - Redistributions in binary form must reproduce the above copyright 	*/
/*    licenses, this list of conditions	and the following disclaimers in the 	*/
/*    documentation and/or other materials provided with the distribution.	*/
/*										*/
/*  3. Disclaimers:								*/
/*										*/
/*  - THE COPYRIGHT LICENSES SET FORTH ABOVE DO NOT REPRESENT ANY FORM OF	*/
/*  LICENSE OR WAIVER, EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, WITH	*/
/*  RESPECT TO PATENT RIGHTS HELD BY TCG MEMBERS (OR OTHER THIRD PARTIES)	*/
/*  THAT MAY BE NECESSARY TO IMPLEMENT THIS SPECIFICATION OR OTHERWISE.		*/
/*  Contact TCG Administration (admin@trustedcomputinggroup.org) for 		*/
/*  information on specification licensing rights available through TCG 	*/
/*  membership agreements.							*/
/*										*/
/*  - THIS SPECIFICATION IS PROVIDED "AS IS" WITH NO EXPRESS OR IMPLIED 	*/
/*    WARRANTIES WHATSOEVER, INCLUDING ANY WARRANTY OF MERCHANTABILITY OR 	*/
/*    FITNESS FOR A PARTICULAR PURPOSE, ACCURACY, COMPLETENESS, OR 		*/
/*    NONINFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS, OR ANY WARRANTY 		*/
/*    OTHERWISE ARISING OUT OF ANY PROPOSAL, SPECIFICATION OR SAMPLE.		*/
/*										*/
/*  - Without limitation, TCG and its members and licensors disclaim all 	*/
/*    liability, including liability for infringement of any proprietary 	*/
/*    rights, relating to use of information in this specification and to the	*/
/*    implementation of this specification, and TCG disclaims all liability for	*/
/*    cost of procurement of substitute goods or services, lost profits, loss 	*/
/*    of use, loss of data or any incidental, consequential, direct, indirect, 	*/
/*    or special damages, whether under contract, tort, warranty or otherwise, 	*/
/*    arising in any way out of use or reliance upon this specification or any 	*/
/*    information herein.							*/
/*										*/
/*  (c) Copyright IBM Corp. and others, 2016 - 2018				*/
/*										*/
/********************************************************************************/

#include "Tpm.h"
#include "HMAC_Start_fp.h"
#if CC_HMAC_Start  // Conditional expansion of this file
TPM_RC
TPM2_HMAC_Start(
		HMAC_Start_In   *in,            // IN: input parameter list
		HMAC_Start_Out  *out            // OUT: output parameter list
		)
{
    OBJECT                  *keyObject;
    TPMT_PUBLIC             *publicArea;
    TPM_ALG_ID               hashAlg;
    // Input Validation
    // Get HMAC key object and public area pointers
    keyObject = HandleToObject(in->handle);
    publicArea = &keyObject->publicArea;
    // Make sure that the key is an HMAC key
    if(publicArea->type != TPM_ALG_KEYEDHASH)
	return TPM_RCS_TYPE + RC_HMAC_Start_handle;
    // and that it is unrestricted
    if (IS_ATTRIBUTE(publicArea->objectAttributes, TPMA_OBJECT, restricted))
	return TPM_RCS_ATTRIBUTES + RC_HMAC_Start_handle;
    // and that it is a signing key
    if (!IS_ATTRIBUTE(publicArea->objectAttributes, TPMA_OBJECT, sign))
	return TPM_RCS_KEY + RC_HMAC_Start_handle;
    // See if the key has a default
    if(publicArea->parameters.keyedHashDetail.scheme.scheme == TPM_ALG_NULL)
	// it doesn't so use the input value
	hashAlg = in->hashAlg;
    else
	{
	    // key has a default so use it
	    hashAlg
		= publicArea->parameters.keyedHashDetail.scheme.details.hmac.hashAlg;
	    // and verify that the input was either the  TPM_ALG_NULL or the default
	    if(in->hashAlg != TPM_ALG_NULL && in->hashAlg != hashAlg)
		hashAlg = TPM_ALG_NULL;
	}
    // if we ended up without a hash algorithm then return an error
    if(hashAlg == TPM_ALG_NULL)
	return TPM_RCS_VALUE + RC_HMAC_Start_hashAlg;
    // Internal Data Update
    // Create a HMAC sequence object. A TPM_RC_OBJECT_MEMORY error may be
    // returned at this point
    return ObjectCreateHMACSequence(hashAlg,
				    keyObject,
				    &in->auth,
				    &out->sequenceHandle);
}
#endif // CC_HMAC_Start
#include "Tpm.h"
#include "MAC_Start_fp.h"
#if CC_MAC_Start  // Conditional expansion of this file
/* Error Returns Meaning */
/* TPM_RC_ATTRIBUTES key referenced by handle is not a signing key or is restricted */
/* TPM_RC_OBJECT_MEMORY no space to create an internal object */
/* TPM_RC_KEY key referenced by handle is not an HMAC key */
/* TPM_RC_VALUE hashAlg is not compatible with the hash algorithm of the scheme of the object
   referenced by handle */
TPM_RC
TPM2_MAC_Start(
	       MAC_Start_In   *in,            // IN: input parameter list
	       MAC_Start_Out  *out            // OUT: output parameter list
	       )
{
    OBJECT                  *keyObject;
    TPMT_PUBLIC             *publicArea;
    TPM_RC                   result;
    // Input Validation
    // Get HMAC key object and public area pointers
    keyObject = HandleToObject(in->handle);
    publicArea = &keyObject->publicArea;
    // Make sure that the key can do what is required
    result = CryptSelectMac(publicArea, &in->inScheme);
    // If the key is not able to do a MAC, indicate that the handle selects an
    // object that can't do a MAC
    if(result == TPM_RCS_TYPE)
	return TPM_RCS_TYPE + RC_MAC_Start_handle;
    // If there is another error type, indicate that the scheme and key are not
    // compatible
    if(result != TPM_RC_SUCCESS)
	return RcSafeAddToResult(result, RC_MAC_Start_inScheme);
    // Make sure that the key is not restricted
    if(IS_ATTRIBUTE(publicArea->objectAttributes, TPMA_OBJECT, restricted))
	return TPM_RCS_ATTRIBUTES + RC_MAC_Start_handle;
    // and that it is a signing key
    if(!IS_ATTRIBUTE(publicArea->objectAttributes, TPMA_OBJECT, sign))
	return TPM_RCS_KEY + RC_MAC_Start_handle;
    // Internal Data Update
    // Create a HMAC sequence object. A TPM_RC_OBJECT_MEMORY error may be
    // returned at this point
    return ObjectCreateHMACSequence(in->inScheme,
				    keyObject,
				    &in->auth,
				    &out->sequenceHandle);
}
#endif // CC_MAC_Start
#include "Tpm.h"
#include "HashSequenceStart_fp.h"
#if CC_HashSequenceStart  // Conditional expansion of this file
TPM_RC
TPM2_HashSequenceStart(
		       HashSequenceStart_In    *in,            // IN: input parameter list
		       HashSequenceStart_Out   *out            // OUT: output parameter list
		       )
{
    // Internal Data Update
    if(in->hashAlg == TPM_ALG_NULL)
	// Start a event sequence.  A TPM_RC_OBJECT_MEMORY error may be
	// returned at this point
	return ObjectCreateEventSequence(&in->auth, &out->sequenceHandle);
    // Start a hash sequence.  A TPM_RC_OBJECT_MEMORY error may be
    // returned at this point
    return ObjectCreateHashSequence(in->hashAlg, &in->auth, &out->sequenceHandle);
}
#endif // CC_HashSequenceStart
#include "Tpm.h"
#include "SequenceUpdate_fp.h"
#if CC_SequenceUpdate  // Conditional expansion of this file
TPM_RC
TPM2_SequenceUpdate(
		    SequenceUpdate_In   *in             // IN: input parameter list
		    )
{
    OBJECT                  *object;
    HASH_OBJECT             *hashObject;
    // Input Validation
    // Get sequence object pointer
    object = HandleToObject(in->sequenceHandle);
    hashObject = (HASH_OBJECT *)object;
    // Check that referenced object is a sequence object.
    if(!ObjectIsSequence(object))
	return TPM_RCS_MODE + RC_SequenceUpdate_sequenceHandle;
    // Internal Data Update
    if(object->attributes.eventSeq == SET)
	{
	    // Update event sequence object
	    UINT32           i;
	    for(i = 0; i < HASH_COUNT; i++)
	        {
	            // Update sequence object
	            CryptDigestUpdate2B(&hashObject->state.hashState[i], &in->buffer.b);
	        }
	}
    else
	{
	    // Update hash/HMAC sequence object
	    if(hashObject->attributes.hashSeq == SET)
	        {
	            // Is this the first block of the sequence
	            if(hashObject->attributes.firstBlock == CLEAR)
			{
			    // If so, indicate that first block was received
			    hashObject->attributes.firstBlock = SET;
			    // Check the first block to see if the first block can contain
			    // the TPM_GENERATED_VALUE.  If it does, it is not safe for
			    // a ticket.
			    if(TicketIsSafe(&in->buffer.b))
				hashObject->attributes.ticketSafe = SET;
			}
	            // Update sequence object hash/HMAC stack
	            CryptDigestUpdate2B(&hashObject->state.hashState[0], &in->buffer.b);
	        }
	    else if(object->attributes.hmacSeq == SET)
	        {
	            // Update sequence object HMAC stack
	            CryptDigestUpdate2B(&hashObject->state.hmacState.hashState,
	                                &in->buffer.b);
	        }
	}
    return TPM_RC_SUCCESS;
}
#endif // CC_SequenceUpdate
#include "Tpm.h"
#include "SequenceComplete_fp.h"
#if CC_SequenceComplete  // Conditional expansion of this file
/* Error Returns Meaning */
/* TPM_RC_MODE sequenceHandle does not reference a hash or HMAC sequence object */
TPM_RC
TPM2_SequenceComplete(
		      SequenceComplete_In     *in,            // IN: input parameter list
		      SequenceComplete_Out    *out            // OUT: output parameter list
		      )
{
    HASH_OBJECT                      *hashObject;
    // Input validation
    // Get hash object pointer
    hashObject = (HASH_OBJECT *)HandleToObject(in->sequenceHandle);
    // input handle must be a hash or HMAC sequence object.
    if(hashObject->attributes.hashSeq == CLEAR
       && hashObject->attributes.hmacSeq == CLEAR)
	return TPM_RCS_MODE + RC_SequenceComplete_sequenceHandle;
    // Command Output
    if(hashObject->attributes.hashSeq == SET)           // sequence object for hash
	{
	    // Get the hash algorithm before the algorithm is lost in CryptHashEnd
	    TPM_ALG_ID       hashAlg = hashObject->state.hashState[0].hashAlg;
	    // Update last piece of the data
	    CryptDigestUpdate2B(&hashObject->state.hashState[0], &in->buffer.b);
	    // Complete hash
	    out->result.t.size = CryptHashEnd(&hashObject->state.hashState[0],
					      sizeof(out->result.t.buffer),
					      out->result.t.buffer);
	    // Check if the first block of the sequence has been received
	    if(hashObject->attributes.firstBlock == CLEAR)
		{
		    // If not, then this is the first block so see if it is 'safe'
		    // to sign.
		    if(TicketIsSafe(&in->buffer.b))
			hashObject->attributes.ticketSafe = SET;
		}
	    // Output ticket
	    out->validation.tag = TPM_ST_HASHCHECK;
	    out->validation.hierarchy = in->hierarchy;
	    if(in->hierarchy == TPM_RH_NULL)
		{
		    // Ticket is not required
		    out->validation.digest.t.size = 0;
		}
	    else if(hashObject->attributes.ticketSafe == CLEAR)
		{
		    // Ticket is not safe to generate
		    out->validation.hierarchy = TPM_RH_NULL;
		    out->validation.digest.t.size = 0;
		}
	    else
		{
		    // Compute ticket
		    TicketComputeHashCheck(out->validation.hierarchy, hashAlg,
					   &out->result, &out->validation);
		}
	}
    else
	{
	    //   Update last piece of data
	    CryptDigestUpdate2B(&hashObject->state.hmacState.hashState, &in->buffer.b);
#if !SMAC_IMPLEMENTED
	    // Complete HMAC
	    out->result.t.size = CryptHmacEnd(&(hashObject->state.hmacState),
					      sizeof(out->result.t.buffer),
					      out->result.t.buffer);
#else
	    // Complete the MAC
	    out->result.t.size = CryptMacEnd(&hashObject->state.hmacState,
					     sizeof(out->result.t.buffer),
					     out->result.t.buffer);
#endif
	    // No ticket is generated for HMAC sequence
	    out->validation.tag = TPM_ST_HASHCHECK;
	    out->validation.hierarchy = TPM_RH_NULL;
	    out->validation.digest.t.size = 0;
	}
    // Internal Data Update
    // mark sequence object as evict so it will be flushed on the way out
    hashObject->attributes.evict = SET;
    return TPM_RC_SUCCESS;
}
#endif // CC_SequenceComplete
#include "Tpm.h"
#include "EventSequenceComplete_fp.h"
#if CC_EventSequenceComplete  // Conditional expansion of this file
TPM_RC
TPM2_EventSequenceComplete(
			   EventSequenceComplete_In    *in,            // IN: input parameter list
			   EventSequenceComplete_Out   *out            // OUT: output parameter list
			   )
{
    HASH_OBJECT         *hashObject;
    UINT32               i;
    TPM_ALG_ID           hashAlg;
    // Input validation
    // get the event sequence object pointer
    hashObject = (HASH_OBJECT *)HandleToObject(in->sequenceHandle);
    // input handle must reference an event sequence object
    if(hashObject->attributes.eventSeq != SET)
	return TPM_RCS_MODE + RC_EventSequenceComplete_sequenceHandle;
    // see if a PCR extend is requested in call
    if(in->pcrHandle != TPM_RH_NULL)
	{
	    // see if extend of the PCR is allowed at the locality of the command,
	    if(!PCRIsExtendAllowed(in->pcrHandle))
		return TPM_RC_LOCALITY;
	    // if an extend is going to take place, then check to see if there has
	    // been an orderly shutdown. If so, and the selected PCR is one of the
	    // state saved PCR, then the orderly state has to change. The orderly state
	    // does not change for PCR that are not preserved.
	    // NOTE: This doesn't just check for Shutdown(STATE) because the orderly
	    // state will have to change if this is a state-saved PCR regardless
	    // of the current state. This is because a subsequent Shutdown(STATE) will
	    // check to see if there was an orderly shutdown and not do anything if
	    // there was. So, this must indicate that a future Shutdown(STATE) has
	    // something to do.
	    if(PCRIsStateSaved(in->pcrHandle))
		RETURN_IF_ORDERLY;
	}
    // Command Output
    out->results.count = 0;
    for(i = 0; i < HASH_COUNT; i++)
	{
	    hashAlg = CryptHashGetAlgByIndex(i);
	    // Update last piece of data
	    CryptDigestUpdate2B(&hashObject->state.hashState[i], &in->buffer.b);
	    // Complete hash
	    out->results.digests[out->results.count].hashAlg = hashAlg;
	    CryptHashEnd(&hashObject->state.hashState[i],
			 CryptHashGetDigestSize(hashAlg),
			 (BYTE *)&out->results.digests[out->results.count].digest);
	    // Extend PCR
	    if(in->pcrHandle != TPM_RH_NULL)
		PCRExtend(in->pcrHandle, hashAlg,
			  CryptHashGetDigestSize(hashAlg),
			  (BYTE *)&out->results.digests[out->results.count].digest);
	    out->results.count++;
	}
    // Internal Data Update
    // mark sequence object as evict so it will be flushed on the way out
    hashObject->attributes.evict = SET;
    return TPM_RC_SUCCESS;
}
#endif // CC_EventSequenceComplete