/src/ms-tpm-20-ref/TPMCmd/tpm/src/command/EA/PolicyNameHash.c
Line | Count | Source (jump to first uncovered line) |
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 "PolicyNameHash_fp.h" |
37 | | |
38 | | #if CC_PolicyNameHash // Conditional expansion of this file |
39 | | |
40 | | /*(See part 3 specification) |
41 | | // Add a nameHash restriction to the policyDigest |
42 | | */ |
43 | | // Return Type: TPM_RC |
44 | | // TPM_RC_CPHASH 'nameHash' has been previously set to a different value |
45 | | // TPM_RC_SIZE 'nameHash' is not the size of the digest produced by the |
46 | | // hash algorithm associated with 'policySession' |
47 | | TPM_RC |
48 | | TPM2_PolicyNameHash(PolicyNameHash_In* in // IN: input parameter list |
49 | | ) |
50 | 0 | { |
51 | 0 | SESSION* session; |
52 | 0 | TPM_CC commandCode = TPM_CC_PolicyNameHash; |
53 | 0 | HASH_STATE hashState; |
54 | | |
55 | | // Input Validation |
56 | | |
57 | | // Get pointer to the session structure |
58 | 0 | session = SessionGet(in->policySession); |
59 | | |
60 | | // A valid nameHash must have the same size as session hash digest |
61 | | // Since the authHashAlg for a session cannot be TPM_ALG_NULL, the digest size |
62 | | // is always non-zero. |
63 | 0 | if(in->nameHash.t.size != CryptHashGetDigestSize(session->authHashAlg)) |
64 | 0 | return TPM_RCS_SIZE + RC_PolicyNameHash_nameHash; |
65 | | |
66 | | // u1 in the policy session context cannot otherwise be occupied |
67 | 0 | if(session->u1.cpHash.b.size != 0 || session->attributes.isBound |
68 | 0 | || session->attributes.isCpHashDefined || session->attributes.isTemplateSet) |
69 | 0 | return TPM_RC_CPHASH; |
70 | | |
71 | | // Internal Data Update |
72 | | |
73 | | // Update policy hash |
74 | | // policyDigestnew = hash(policyDigestold || TPM_CC_PolicyNameHash || nameHash) |
75 | | // Start hash |
76 | 0 | CryptHashStart(&hashState, session->authHashAlg); |
77 | | |
78 | | // add old digest |
79 | 0 | CryptDigestUpdate2B(&hashState, &session->u2.policyDigest.b); |
80 | | |
81 | | // add commandCode |
82 | 0 | CryptDigestUpdateInt(&hashState, sizeof(TPM_CC), commandCode); |
83 | | |
84 | | // add nameHash |
85 | 0 | CryptDigestUpdate2B(&hashState, &in->nameHash.b); |
86 | | |
87 | | // complete the digest |
88 | 0 | CryptHashEnd2B(&hashState, &session->u2.policyDigest.b); |
89 | | |
90 | | // update nameHash in session context |
91 | 0 | session->u1.cpHash = in->nameHash; |
92 | |
|
93 | 0 | return TPM_RC_SUCCESS; |
94 | 0 | } |
95 | | |
96 | | #endif // CC_PolicyNameHash |