1 // This file was extracted from the TCG Published
2 // Trusted Platform Module Library
3 // Part 3: Commands
4 // Family "2.0"
5 // Level 00 Revision 01.16
6 // October 30, 2014
7 
8 #include "InternalRoutines.h"
9 #include "PolicyNameHash_fp.h"
10 //
11 //
12 //     Error Returns                     Meaning
13 //
14 //     TPM_RC_CPHASH                     nameHash has been previously set to a different value
15 //     TPM_RC_SIZE                       nameHash is not the size of the digest produced by the hash
16 //                                       algorithm associated with policySession
17 //
18 TPM_RC
TPM2_PolicyNameHash(PolicyNameHash_In * in)19 TPM2_PolicyNameHash(
20    PolicyNameHash_In     *in                  // IN: input parameter list
21    )
22 {
23    SESSION               *session;
24    TPM_CC                 commandCode = TPM_CC_PolicyNameHash;
25    HASH_STATE             hashState;
26 
27 // Input Validation
28 
29    // Get pointer to the session structure
30    session = SessionGet(in->policySession);
31 
32    // A new nameHash is given in input parameter, but cpHash in session context
33    // is not empty
34    if(in->nameHash.t.size != 0 && session->u1.cpHash.t.size != 0)
35        return TPM_RC_CPHASH;
36 
37    // A valid nameHash must have the same size as session hash digest
38    if(in->nameHash.t.size != CryptGetHashDigestSize(session->authHashAlg))
39        return TPM_RC_SIZE + RC_PolicyNameHash_nameHash;
40 
41 // Internal Data Update
42 
43    // Update policy hash
44    // policyDigestnew = hash(policyDigestold || TPM_CC_PolicyNameHash || nameHash)
45    // Start hash
46    CryptStartHash(session->authHashAlg, &hashState);
47 
48    // add old digest
49    CryptUpdateDigest2B(&hashState, &session->u2.policyDigest.b);
50 
51    // add commandCode
52    CryptUpdateDigestInt(&hashState, sizeof(TPM_CC), &commandCode);
53 
54    // add nameHash
55    CryptUpdateDigest2B(&hashState, &in->nameHash.b);
56 
57    // complete the digest
58    CryptCompleteHash2B(&hashState, &session->u2.policyDigest.b);
59 
60    // clear iscpHashDefined bit to indicate now this field contains a nameHash
61    session->attributes.iscpHashDefined = CLEAR;
62 
63    // update nameHash in session context
64    session->u1.cpHash = in->nameHash;
65 
66    return TPM_RC_SUCCESS;
67 }
68