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 "PolicyPCR_fp.h"
37
38 #if CC_PolicyPCR // Conditional expansion of this file
39
40 /*(See part 3 specification)
41 // Add a PCR gate for a policy session
42 */
43 // Return Type: TPM_RC
44 // TPM_RC_VALUE if provided, 'pcrDigest' does not match the
45 // current PCR settings
46 // TPM_RC_PCR_CHANGED a previous TPM2_PolicyPCR() set
47 // pcrCounter and it has changed
48 TPM_RC
TPM2_PolicyPCR(PolicyPCR_In * in)49 TPM2_PolicyPCR(
50 PolicyPCR_In *in // IN: input parameter list
51 )
52 {
53 SESSION *session;
54 TPM2B_DIGEST pcrDigest;
55 BYTE pcrs[sizeof(TPML_PCR_SELECTION)];
56 UINT32 pcrSize;
57 BYTE *buffer;
58 TPM_CC commandCode = TPM_CC_PolicyPCR;
59 HASH_STATE hashState;
60
61 // Input Validation
62
63 // Get pointer to the session structure
64 session = SessionGet(in->policySession);
65
66 // Compute current PCR digest
67 PCRComputeCurrentDigest(session->authHashAlg, &in->pcrs, &pcrDigest);
68
69 // Do validation for non trial session
70 if(session->attributes.isTrialPolicy == CLEAR)
71 {
72 // Make sure that this is not going to invalidate a previous PCR check
73 if(session->pcrCounter != 0 && session->pcrCounter != gr.pcrCounter)
74 return TPM_RC_PCR_CHANGED;
75
76 // If the caller specified the PCR digest and it does not
77 // match the current PCR settings, return an error..
78 if(in->pcrDigest.t.size != 0)
79 {
80 if(!MemoryEqual2B(&in->pcrDigest.b, &pcrDigest.b))
81 return TPM_RCS_VALUE + RC_PolicyPCR_pcrDigest;
82 }
83 }
84 else
85 {
86 // For trial session, just use the input PCR digest if one provided
87 // Note: It can't be too big because it is a TPM2B_DIGEST and the size
88 // would have been checked during unmarshaling
89 if(in->pcrDigest.t.size != 0)
90 pcrDigest = in->pcrDigest;
91 }
92 // Internal Data Update
93 // Update policy hash
94 // policyDigestnew = hash( policyDigestold || TPM_CC_PolicyPCR
95 // || PCRS || pcrDigest)
96 // Start hash
97 CryptHashStart(&hashState, session->authHashAlg);
98
99 // add old digest
100 CryptDigestUpdate2B(&hashState, &session->u2.policyDigest.b);
101
102 // add commandCode
103 CryptDigestUpdateInt(&hashState, sizeof(TPM_CC), commandCode);
104
105 // add PCRS
106 buffer = pcrs;
107 pcrSize = TPML_PCR_SELECTION_Marshal(&in->pcrs, &buffer, NULL);
108 CryptDigestUpdate(&hashState, pcrSize, pcrs);
109
110 // add PCR digest
111 CryptDigestUpdate2B(&hashState, &pcrDigest.b);
112
113 // complete the hash and get the results
114 CryptHashEnd2B(&hashState, &session->u2.policyDigest.b);
115
116 // update pcrCounter in session context for non trial session
117 if(session->attributes.isTrialPolicy == CLEAR)
118 {
119 session->pcrCounter = gr.pcrCounter;
120 }
121
122 return TPM_RC_SUCCESS;
123 }
124
125 #endif // CC_PolicyPCR