Coverage Report

Created: 2025-07-11 07:00

/src/ibmswtpm2/src/Context_spt.c
Line
Count
Source (jump to first uncovered line)
1
/********************************************************************************/
2
/*                    */
3
/*                  */
4
/*           Written by Ken Goldman       */
5
/*           IBM Thomas J. Watson Research Center     */
6
/*            $Id: Context_spt.c 1273 2018-07-20 18:49:46Z kgoldman $   */
7
/*                    */
8
/*  Licenses and Notices              */
9
/*                    */
10
/*  1. Copyright Licenses:              */
11
/*                    */
12
/*  - Trusted Computing Group (TCG) grants to the user of the source code in  */
13
/*    this specification (the "Source Code") a worldwide, irrevocable,    */
14
/*    nonexclusive, royalty free, copyright license to reproduce, create  */
15
/*    derivative works, distribute, display and perform the Source Code and */
16
/*    derivative works thereof, and to grant others the rights granted herein.  */
17
/*                    */
18
/*  - The TCG grants to the user of the other parts of the specification  */
19
/*    (other than the Source Code) the rights to reproduce, distribute,   */
20
/*    display, and perform the specification solely for the purpose of    */
21
/*    developing products based on such documents.        */
22
/*                    */
23
/*  2. Source Code Distribution Conditions:         */
24
/*                    */
25
/*  - Redistributions of Source Code must retain the above copyright licenses,  */
26
/*    this list of conditions and the following disclaimers.      */
27
/*                    */
28
/*  - Redistributions in binary form must reproduce the above copyright   */
29
/*    licenses, this list of conditions and the following disclaimers in the  */
30
/*    documentation and/or other materials provided with the distribution.  */
31
/*                    */
32
/*  3. Disclaimers:               */
33
/*                    */
34
/*  - THE COPYRIGHT LICENSES SET FORTH ABOVE DO NOT REPRESENT ANY FORM OF */
35
/*  LICENSE OR WAIVER, EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, WITH */
36
/*  RESPECT TO PATENT RIGHTS HELD BY TCG MEMBERS (OR OTHER THIRD PARTIES) */
37
/*  THAT MAY BE NECESSARY TO IMPLEMENT THIS SPECIFICATION OR OTHERWISE.   */
38
/*  Contact TCG Administration (admin@trustedcomputinggroup.org) for    */
39
/*  information on specification licensing rights available through TCG   */
40
/*  membership agreements.              */
41
/*                    */
42
/*  - THIS SPECIFICATION IS PROVIDED "AS IS" WITH NO EXPRESS OR IMPLIED   */
43
/*    WARRANTIES WHATSOEVER, INCLUDING ANY WARRANTY OF MERCHANTABILITY OR   */
44
/*    FITNESS FOR A PARTICULAR PURPOSE, ACCURACY, COMPLETENESS, OR    */
45
/*    NONINFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS, OR ANY WARRANTY    */
46
/*    OTHERWISE ARISING OUT OF ANY PROPOSAL, SPECIFICATION OR SAMPLE.   */
47
/*                    */
48
/*  - Without limitation, TCG and its members and licensors disclaim all  */
49
/*    liability, including liability for infringement of any proprietary  */
50
/*    rights, relating to use of information in this specification and to the */
51
/*    implementation of this specification, and TCG disclaims all liability for */
52
/*    cost of procurement of substitute goods or services, lost profits, loss   */
53
/*    of use, loss of data or any incidental, consequential, direct, indirect,  */
54
/*    or special damages, whether under contract, tort, warranty or otherwise,  */
55
/*    arising in any way out of use or reliance upon this specification or any  */
56
/*    information herein.             */
57
/*                    */
58
/*  (c) Copyright IBM Corp. and others, 2016, 2017        */
59
/*                    */
60
/********************************************************************************/
61
62
/* 7.3.1 Includes */
63
#include "Tpm.h"
64
#include "Context_spt_fp.h"
65
/* 7.3.2 Functions */
66
/* 7.3.2.1 ComputeContextProtectionKey() */
67
/* This function retrieves the symmetric protection key for context encryption It is used by
68
   TPM2_ConextSave() and TPM2_ContextLoad() to create the symmetric encryption key and iv */
69
void
70
ComputeContextProtectionKey(
71
          TPMS_CONTEXT    *contextBlob,   // IN: context blob
72
          TPM2B_SYM_KEY   *symKey,        // OUT: the symmetric key
73
          TPM2B_IV        *iv             // OUT: the IV.
74
          )
75
0
{
76
0
    UINT16           symKeyBits;    // number of bits in the parent's
77
    //   symmetric key
78
0
    TPM2B_PROOF      *proof = NULL;  // the proof value to use. Is null for
79
    //   everything but a primary object in
80
    //   the Endorsement Hierarchy
81
0
    BYTE             kdfResult[sizeof(TPMU_HA) * 2];// Value produced by the KDF
82
0
    TPM2B_DATA       sequence2B, handle2B;
83
    // Get proof value
84
0
    proof = HierarchyGetProof(contextBlob->hierarchy);
85
    // Get sequence value in 2B format
86
0
    sequence2B.t.size = sizeof(contextBlob->sequence);
87
0
    cAssert(sizeof(contextBlob->sequence) <= sizeof(sequence2B.t.buffer));
88
0
    MemoryCopy(sequence2B.t.buffer, &contextBlob->sequence,
89
0
         sizeof(contextBlob->sequence));
90
    // Get handle value in 2B format
91
0
    handle2B.t.size = sizeof(contextBlob->savedHandle);
92
0
    cAssert(sizeof(contextBlob->savedHandle) <= sizeof(handle2B.t.buffer));
93
0
    MemoryCopy(handle2B.t.buffer, &contextBlob->savedHandle,
94
0
         sizeof(contextBlob->savedHandle));
95
    // Get the symmetric encryption key size
96
0
    symKey->t.size = CONTEXT_ENCRYPT_KEY_BYTES;
97
0
    symKeyBits = CONTEXT_ENCRYPT_KEY_BITS;
98
    // Get the size of the IV for the algorithm
99
0
    iv->t.size = CryptGetSymmetricBlockSize(CONTEXT_ENCRYPT_ALG, symKeyBits);
100
    // KDFa to generate symmetric key and IV value
101
0
    CryptKDFa(CONTEXT_INTEGRITY_HASH_ALG, &proof->b, CONTEXT_KEY, &sequence2B.b,
102
0
        &handle2B.b, (symKey->t.size + iv->t.size) * 8, kdfResult, NULL,
103
0
        FALSE);
104
    // Copy part of the returned value as the key
105
0
    pAssert(symKey->t.size <= sizeof(symKey->t.buffer));
106
0
    MemoryCopy(symKey->t.buffer, kdfResult, symKey->t.size);
107
    // Copy the rest as the IV
108
0
    pAssert(iv->t.size <= sizeof(iv->t.buffer));
109
0
    MemoryCopy(iv->t.buffer, &kdfResult[symKey->t.size], iv->t.size);
110
0
    return;
111
0
}
112
/* 7.3.2.2 ComputeContextIntegrity() */
113
/* Generate the integrity hash for a context It is used by TPM2_ContextSave() to create an integrity
114
   hash and by TPM2_ContextLoad() to compare an integrity hash */
115
void
116
ComputeContextIntegrity(
117
      TPMS_CONTEXT    *contextBlob,   // IN: context blob
118
      TPM2B_DIGEST    *integrity      // OUT: integrity
119
      )
120
0
{
121
0
    HMAC_STATE          hmacState;
122
0
    TPM2B_PROOF         *proof;
123
0
    UINT16              integritySize;
124
    // Get proof value
125
0
    proof = HierarchyGetProof(contextBlob->hierarchy);
126
    // Start HMAC
127
0
    integrity->t.size = CryptHmacStart2B(&hmacState, CONTEXT_INTEGRITY_HASH_ALG,
128
0
           &proof->b);
129
    // Compute integrity size at the beginning of context blob
130
0
    integritySize = sizeof(integrity->t.size) + integrity->t.size;
131
    // Adding total reset counter so that the context cannot be
132
    // used after a TPM Reset
133
0
    CryptDigestUpdateInt(&hmacState.hashState, sizeof(gp.totalResetCount),
134
0
       gp.totalResetCount);
135
    // If this is a ST_CLEAR object, add the clear count
136
    // so that this contest cannot be loaded after a TPM Restart
137
0
    if(contextBlob->savedHandle == 0x80000002)
138
0
  CryptDigestUpdateInt(&hmacState.hashState, sizeof(gr.clearCount),
139
0
           gr.clearCount);
140
    // Adding sequence number to the HMAC to make sure that it doesn't
141
    // get changed
142
0
    CryptDigestUpdateInt(&hmacState.hashState, sizeof(contextBlob->sequence),
143
0
       contextBlob->sequence);
144
    // Protect the handle
145
0
    CryptDigestUpdateInt(&hmacState.hashState, sizeof(contextBlob->savedHandle),
146
0
       contextBlob->savedHandle);
147
    // Adding sensitive contextData, skip the leading integrity area
148
0
    CryptDigestUpdate(&hmacState.hashState,
149
0
          contextBlob->contextBlob.t.size - integritySize,
150
0
          contextBlob->contextBlob.t.buffer + integritySize);
151
    // Complete HMAC
152
0
    CryptHmacEnd2B(&hmacState, &integrity->b);
153
0
    return;
154
0
}
155
/* 7.3.2.3 SequenceDataExport() */
156
/* This function is used scan through the sequence object and either modify the hash state data for
157
   export (contextSave) or to import it into the internal format (contextLoad). This function should
158
   only be called after the sequence object has been copied to the context buffer (contextSave) or
159
   from the context buffer into the sequence object. The presumption is that the context buffer
160
   version of the data is the same size as the internal representation so nothing outsize of the
161
   hash context area gets modified. */
162
void
163
SequenceDataExport(
164
       HASH_OBJECT         *object,        // IN: an internal hash object
165
       HASH_OBJECT_BUFFER  *exportObject   // OUT: a sequence context in a buffer
166
       )
167
0
{
168
    // If the hash object is not an event, then only one hash context is needed
169
0
    int                   count = (object->attributes.eventSeq) ? HASH_COUNT : 1;
170
0
    for(count--; count >= 0; count--)
171
0
  {
172
0
      HASH_STATE          *hash = &object->state.hashState[count];
173
0
      size_t               offset = (BYTE *)hash - (BYTE *)object;
174
0
      BYTE                *exportHash = &((BYTE *)exportObject)[offset];
175
0
      CryptHashExportState(hash, (EXPORT_HASH_STATE *)exportHash);
176
0
  }
177
0
}
178
/* 7.3.2.4 SequenceDataImport() */
179
/* This function is used scan through the sequence object and either modify the hash state data for
180
   export (contextSave) or to import it into the internal format (contextLoad). This function should
181
   only be called after the sequence object has been copied to the context buffer (contextSave) or
182
   from the context buffer into the sequence object. The presumption is that the context buffer
183
   version of the data is the same size as the internal representation so nothing outsize of the
184
   hash context area gets modified. */
185
void
186
SequenceDataImport(
187
       HASH_OBJECT         *object,        // IN/OUT: an internal hash object
188
       HASH_OBJECT_BUFFER  *exportObject   // IN/OUT: a sequence context in a buffer
189
       )
190
0
{
191
    // If the hash object is not an event, then only one hash context is needed
192
0
    int                   count = (object->attributes.eventSeq) ? HASH_COUNT : 1;
193
0
    for(count--; count >= 0; count--)
194
0
  {
195
0
      HASH_STATE          *hash = &object->state.hashState[count];
196
0
      size_t               offset = (BYTE *)hash - (BYTE *)object;
197
0
      BYTE                *importHash = &((BYTE *)exportObject)[offset];
198
      //
199
0
      CryptHashImportState(hash, (EXPORT_HASH_STATE *)importHash);
200
0
  }
201
0
}