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 #include "Tpm.h"
36 #include "SequenceComplete_fp.h"
37
38 #if CC_SequenceComplete // Conditional expansion of this file
39
40 /*(See part 3 specification)
41 // Complete a sequence and flush the object.
42 */
43 // Return Type: TPM_RC
44 // TPM_RC_MODE 'sequenceHandle' does not reference a hash or HMAC
45 // sequence object
46 TPM_RC
TPM2_SequenceComplete(SequenceComplete_In * in,SequenceComplete_Out * out)47 TPM2_SequenceComplete(
48 SequenceComplete_In *in, // IN: input parameter list
49 SequenceComplete_Out *out // OUT: output parameter list
50 )
51 {
52 HASH_OBJECT *hashObject;
53 // Input validation
54 // Get hash object pointer
55 hashObject = (HASH_OBJECT *)HandleToObject(in->sequenceHandle);
56
57 // input handle must be a hash or HMAC sequence object.
58 if(hashObject->attributes.hashSeq == CLEAR
59 && hashObject->attributes.hmacSeq == CLEAR)
60 return TPM_RCS_MODE + RC_SequenceComplete_sequenceHandle;
61 // Command Output
62 if(hashObject->attributes.hashSeq == SET) // sequence object for hash
63 {
64 // Get the hash algorithm before the algorithm is lost in CryptHashEnd
65 TPM_ALG_ID hashAlg = hashObject->state.hashState[0].hashAlg;
66
67 // Update last piece of the data
68 CryptDigestUpdate2B(&hashObject->state.hashState[0], &in->buffer.b);
69
70 // Complete hash
71 out->result.t.size = CryptHashEnd(&hashObject->state.hashState[0],
72 sizeof(out->result.t.buffer),
73 out->result.t.buffer);
74 // Check if the first block of the sequence has been received
75 if(hashObject->attributes.firstBlock == CLEAR)
76 {
77 // If not, then this is the first block so see if it is 'safe'
78 // to sign.
79 if(TicketIsSafe(&in->buffer.b))
80 hashObject->attributes.ticketSafe = SET;
81 }
82 // Output ticket
83 out->validation.tag = TPM_ST_HASHCHECK;
84 out->validation.hierarchy = in->hierarchy;
85
86 if(in->hierarchy == TPM_RH_NULL)
87 {
88 // Ticket is not required
89 out->validation.digest.t.size = 0;
90 }
91 else if(hashObject->attributes.ticketSafe == CLEAR)
92 {
93 // Ticket is not safe to generate
94 out->validation.hierarchy = TPM_RH_NULL;
95 out->validation.digest.t.size = 0;
96 }
97 else
98 {
99 // Compute ticket
100 TicketComputeHashCheck(out->validation.hierarchy, hashAlg,
101 &out->result, &out->validation);
102 }
103 }
104 else
105 {
106 // Update last piece of data
107 CryptDigestUpdate2B(&hashObject->state.hmacState.hashState, &in->buffer.b);
108 #if !SMAC_IMPLEMENTED
109 // Complete HMAC
110 out->result.t.size = CryptHmacEnd(&(hashObject->state.hmacState),
111 sizeof(out->result.t.buffer),
112 out->result.t.buffer);
113 #else
114 // Complete the MAC
115 out->result.t.size = CryptMacEnd(&hashObject->state.hmacState,
116 sizeof(out->result.t.buffer),
117 out->result.t.buffer);
118 #endif
119 // No ticket is generated for HMAC sequence
120 out->validation.tag = TPM_ST_HASHCHECK;
121 out->validation.hierarchy = TPM_RH_NULL;
122 out->validation.digest.t.size = 0;
123 }
124 // Internal Data Update
125 // mark sequence object as evict so it will be flushed on the way out
126 hashObject->attributes.evict = SET;
127
128 return TPM_RC_SUCCESS;
129 }
130
131 #endif // CC_SequenceComplete