xref: /aosp_15_r20/external/ms-tpm-20-ref/TPMCmd/tpm/include/CryptHash.h (revision 5c591343844d1f9da7da26467c4bf7efc8a7a413)
1*5c591343SA. Cody Schuffelen /* Microsoft Reference Implementation for TPM 2.0
2*5c591343SA. Cody Schuffelen  *
3*5c591343SA. Cody Schuffelen  *  The copyright in this software is being made available under the BSD License,
4*5c591343SA. Cody Schuffelen  *  included below. This software may be subject to other third party and
5*5c591343SA. Cody Schuffelen  *  contributor rights, including patent rights, and no such rights are granted
6*5c591343SA. Cody Schuffelen  *  under this license.
7*5c591343SA. Cody Schuffelen  *
8*5c591343SA. Cody Schuffelen  *  Copyright (c) Microsoft Corporation
9*5c591343SA. Cody Schuffelen  *
10*5c591343SA. Cody Schuffelen  *  All rights reserved.
11*5c591343SA. Cody Schuffelen  *
12*5c591343SA. Cody Schuffelen  *  BSD License
13*5c591343SA. Cody Schuffelen  *
14*5c591343SA. Cody Schuffelen  *  Redistribution and use in source and binary forms, with or without modification,
15*5c591343SA. Cody Schuffelen  *  are permitted provided that the following conditions are met:
16*5c591343SA. Cody Schuffelen  *
17*5c591343SA. Cody Schuffelen  *  Redistributions of source code must retain the above copyright notice, this list
18*5c591343SA. Cody Schuffelen  *  of conditions and the following disclaimer.
19*5c591343SA. Cody Schuffelen  *
20*5c591343SA. Cody Schuffelen  *  Redistributions in binary form must reproduce the above copyright notice, this
21*5c591343SA. Cody Schuffelen  *  list of conditions and the following disclaimer in the documentation and/or
22*5c591343SA. Cody Schuffelen  *  other materials provided with the distribution.
23*5c591343SA. Cody Schuffelen  *
24*5c591343SA. Cody Schuffelen  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
25*5c591343SA. Cody Schuffelen  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26*5c591343SA. Cody Schuffelen  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27*5c591343SA. Cody Schuffelen  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28*5c591343SA. Cody Schuffelen  *  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29*5c591343SA. Cody Schuffelen  *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30*5c591343SA. Cody Schuffelen  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31*5c591343SA. Cody Schuffelen  *  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32*5c591343SA. Cody Schuffelen  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33*5c591343SA. Cody Schuffelen  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*5c591343SA. Cody Schuffelen  */
35*5c591343SA. Cody Schuffelen //** Introduction
36*5c591343SA. Cody Schuffelen // This header contains the hash structure definitions used in the TPM code
37*5c591343SA. Cody Schuffelen // to define the amount of space to be reserved for the hash state. This allows
38*5c591343SA. Cody Schuffelen // the TPM code to not have to import all of the symbols used by the hash
39*5c591343SA. Cody Schuffelen // computations. This lets the build environment of the TPM code not to have
40*5c591343SA. Cody Schuffelen // include the header files associated with the CryptoEngine code.
41*5c591343SA. Cody Schuffelen 
42*5c591343SA. Cody Schuffelen #ifndef _CRYPT_HASH_H
43*5c591343SA. Cody Schuffelen #define _CRYPT_HASH_H
44*5c591343SA. Cody Schuffelen 
45*5c591343SA. Cody Schuffelen //** Hash-related Structures
46*5c591343SA. Cody Schuffelen 
47*5c591343SA. Cody Schuffelen union SMAC_STATES;
48*5c591343SA. Cody Schuffelen 
49*5c591343SA. Cody Schuffelen // These definitions add the high-level methods for processing state that may be
50*5c591343SA. Cody Schuffelen // an SMAC
51*5c591343SA. Cody Schuffelen typedef void(* SMAC_DATA_METHOD)(
52*5c591343SA. Cody Schuffelen     union SMAC_STATES       *state,
53*5c591343SA. Cody Schuffelen     UINT32                   size,
54*5c591343SA. Cody Schuffelen     const BYTE              *buffer
55*5c591343SA. Cody Schuffelen     );
56*5c591343SA. Cody Schuffelen 
57*5c591343SA. Cody Schuffelen typedef UINT16(* SMAC_END_METHOD)(
58*5c591343SA. Cody Schuffelen     union SMAC_STATES       *state,
59*5c591343SA. Cody Schuffelen     UINT32                   size,
60*5c591343SA. Cody Schuffelen     BYTE                    *buffer
61*5c591343SA. Cody Schuffelen     );
62*5c591343SA. Cody Schuffelen 
63*5c591343SA. Cody Schuffelen typedef struct sequenceMethods {
64*5c591343SA. Cody Schuffelen     SMAC_DATA_METHOD          data;
65*5c591343SA. Cody Schuffelen     SMAC_END_METHOD           end;
66*5c591343SA. Cody Schuffelen } SMAC_METHODS;
67*5c591343SA. Cody Schuffelen 
68*5c591343SA. Cody Schuffelen #define SMAC_IMPLEMENTED (CC_MAC || CC_MAC_Start)
69*5c591343SA. Cody Schuffelen 
70*5c591343SA. Cody Schuffelen // These definitions are here because the SMAC state is in the union of hash states.
71*5c591343SA. Cody Schuffelen typedef struct tpmCmacState {
72*5c591343SA. Cody Schuffelen     TPM_ALG_ID              symAlg;
73*5c591343SA. Cody Schuffelen     UINT16                  keySizeBits;
74*5c591343SA. Cody Schuffelen     INT16                   bcount; // current count of bytes accumulated in IV
75*5c591343SA. Cody Schuffelen     TPM2B_IV                iv;     // IV buffer
76*5c591343SA. Cody Schuffelen     TPM2B_SYM_KEY           symKey;
77*5c591343SA. Cody Schuffelen } tpmCmacState_t;
78*5c591343SA. Cody Schuffelen 
79*5c591343SA. Cody Schuffelen typedef union SMAC_STATES {
80*5c591343SA. Cody Schuffelen #if ALG_CMAC
81*5c591343SA. Cody Schuffelen     tpmCmacState_t          cmac;
82*5c591343SA. Cody Schuffelen #endif
83*5c591343SA. Cody Schuffelen     UINT64                  pad;
84*5c591343SA. Cody Schuffelen } SMAC_STATES;
85*5c591343SA. Cody Schuffelen 
86*5c591343SA. Cody Schuffelen typedef struct SMAC_STATE {
87*5c591343SA. Cody Schuffelen     SMAC_METHODS            smacMethods;
88*5c591343SA. Cody Schuffelen     SMAC_STATES             state;
89*5c591343SA. Cody Schuffelen } SMAC_STATE;
90*5c591343SA. Cody Schuffelen 
91*5c591343SA. Cody Schuffelen #if ALG_SHA1
92*5c591343SA. Cody Schuffelen #   define  IF_IMPLEMENTED_SHA1(op)     op(SHA1, Sha1)
93*5c591343SA. Cody Schuffelen #else
94*5c591343SA. Cody Schuffelen #   define  IF_IMPLEMENTED_SHA1(op)
95*5c591343SA. Cody Schuffelen #endif
96*5c591343SA. Cody Schuffelen #if ALG_SHA256
97*5c591343SA. Cody Schuffelen #   define  IF_IMPLEMENTED_SHA256(op)     op(SHA256, Sha256)
98*5c591343SA. Cody Schuffelen #else
99*5c591343SA. Cody Schuffelen #   define  IF_IMPLEMENTED_SHA256(op)
100*5c591343SA. Cody Schuffelen #endif
101*5c591343SA. Cody Schuffelen #if ALG_SHA384
102*5c591343SA. Cody Schuffelen #   define  IF_IMPLEMENTED_SHA384(op)     op(SHA384, Sha384)
103*5c591343SA. Cody Schuffelen #else
104*5c591343SA. Cody Schuffelen #   define  IF_IMPLEMENTED_SHA384(op)
105*5c591343SA. Cody Schuffelen #endif
106*5c591343SA. Cody Schuffelen #if ALG_SHA512
107*5c591343SA. Cody Schuffelen #   define  IF_IMPLEMENTED_SHA512(op)     op(SHA512, Sha512)
108*5c591343SA. Cody Schuffelen #else
109*5c591343SA. Cody Schuffelen #   define  IF_IMPLEMENTED_SHA512(op)
110*5c591343SA. Cody Schuffelen #endif
111*5c591343SA. Cody Schuffelen #if ALG_SM3_256
112*5c591343SA. Cody Schuffelen #   define  IF_IMPLEMENTED_SM3_256(op)     op(SM3_256, Sm3_256)
113*5c591343SA. Cody Schuffelen #else
114*5c591343SA. Cody Schuffelen #   define  IF_IMPLEMENTED_SM3_256(op)
115*5c591343SA. Cody Schuffelen #endif
116*5c591343SA. Cody Schuffelen #if ALG_SHA3_256
117*5c591343SA. Cody Schuffelen #   define  IF_IMPLEMENTED_SHA3_256(op)     op(SHA3_256, Sha3_256)
118*5c591343SA. Cody Schuffelen #else
119*5c591343SA. Cody Schuffelen #   define  IF_IMPLEMENTED_SHA3_256(op)
120*5c591343SA. Cody Schuffelen #endif
121*5c591343SA. Cody Schuffelen #if ALG_SHA3_384
122*5c591343SA. Cody Schuffelen #   define  IF_IMPLEMENTED_SHA3_384(op)     op(SHA3_384, Sha3_384)
123*5c591343SA. Cody Schuffelen #else
124*5c591343SA. Cody Schuffelen #   define  IF_IMPLEMENTED_SHA3_384(op)
125*5c591343SA. Cody Schuffelen #endif
126*5c591343SA. Cody Schuffelen #if ALG_SHA3_512
127*5c591343SA. Cody Schuffelen #   define  IF_IMPLEMENTED_SHA3_512(op)     op(SHA3_512, Sha3_512)
128*5c591343SA. Cody Schuffelen #else
129*5c591343SA. Cody Schuffelen #   define  IF_IMPLEMENTED_SHA3_512(op)
130*5c591343SA. Cody Schuffelen #endif
131*5c591343SA. Cody Schuffelen 
132*5c591343SA. Cody Schuffelen #define FOR_EACH_HASH(op)           \
133*5c591343SA. Cody Schuffelen         IF_IMPLEMENTED_SHA1(op)     \
134*5c591343SA. Cody Schuffelen         IF_IMPLEMENTED_SHA256(op)   \
135*5c591343SA. Cody Schuffelen         IF_IMPLEMENTED_SHA384(op)   \
136*5c591343SA. Cody Schuffelen         IF_IMPLEMENTED_SM3_256(op)  \
137*5c591343SA. Cody Schuffelen         IF_IMPLEMENTED_SHA3_256(op) \
138*5c591343SA. Cody Schuffelen         IF_IMPLEMENTED_SHA3_384(op) \
139*5c591343SA. Cody Schuffelen         IF_IMPLEMENTED_SHA3_512(op)
140*5c591343SA. Cody Schuffelen 
141*5c591343SA. Cody Schuffelen 
142*5c591343SA. Cody Schuffelen #define HASH_TYPE(HASH, Hash)   tpmHashState##HASH##_t  Hash;
143*5c591343SA. Cody Schuffelen typedef union
144*5c591343SA. Cody Schuffelen {
145*5c591343SA. Cody Schuffelen     FOR_EACH_HASH(HASH_TYPE)
146*5c591343SA. Cody Schuffelen // Additions for symmetric block cipher MAC
147*5c591343SA. Cody Schuffelen #if SMAC_IMPLEMENTED
148*5c591343SA. Cody Schuffelen     SMAC_STATE                 smac;
149*5c591343SA. Cody Schuffelen #endif
150*5c591343SA. Cody Schuffelen     // to force structure alignment to be no worse than HASH_ALIGNMENT
151*5c591343SA. Cody Schuffelen #if HASH_ALIGNMENT == 8
152*5c591343SA. Cody Schuffelen     uint64_t             align;
153*5c591343SA. Cody Schuffelen #else
154*5c591343SA. Cody Schuffelen     uint32_t             align;
155*5c591343SA. Cody Schuffelen #endif
156*5c591343SA. Cody Schuffelen } ANY_HASH_STATE;
157*5c591343SA. Cody Schuffelen 
158*5c591343SA. Cody Schuffelen typedef ANY_HASH_STATE *PANY_HASH_STATE;
159*5c591343SA. Cody Schuffelen typedef const ANY_HASH_STATE    *PCANY_HASH_STATE;
160*5c591343SA. Cody Schuffelen 
161*5c591343SA. Cody Schuffelen #define ALIGNED_SIZE(x, b) ((((x) + (b) - 1) / (b)) * (b))
162*5c591343SA. Cody Schuffelen // MAX_HASH_STATE_SIZE will change with each implementation. It is assumed that
163*5c591343SA. Cody Schuffelen // a hash state will not be larger than twice the block size plus some
164*5c591343SA. Cody Schuffelen // overhead (in this case, 16 bytes). The overall size needs to be as
165*5c591343SA. Cody Schuffelen // large as any of the hash contexts. The structure needs to start on an
166*5c591343SA. Cody Schuffelen // alignment boundary and be an even multiple of the alignment
167*5c591343SA. Cody Schuffelen #define MAX_HASH_STATE_SIZE ((2 * MAX_HASH_BLOCK_SIZE) + 16)
168*5c591343SA. Cody Schuffelen #define MAX_HASH_STATE_SIZE_ALIGNED                                                 \
169*5c591343SA. Cody Schuffelen                     ALIGNED_SIZE(MAX_HASH_STATE_SIZE, HASH_ALIGNMENT)
170*5c591343SA. Cody Schuffelen 
171*5c591343SA. Cody Schuffelen // This is an aligned byte array that will hold any of the hash contexts.
172*5c591343SA. Cody Schuffelen typedef  ANY_HASH_STATE ALIGNED_HASH_STATE;
173*5c591343SA. Cody Schuffelen 
174*5c591343SA. Cody Schuffelen // The header associated with the hash library is expected to define the methods
175*5c591343SA. Cody Schuffelen // which include the calling sequence. When not compiling CryptHash.c, the methods
176*5c591343SA. Cody Schuffelen // are not defined so we need placeholder functions for the structures
177*5c591343SA. Cody Schuffelen 
178*5c591343SA. Cody Schuffelen #ifndef HASH_START_METHOD_DEF
179*5c591343SA. Cody Schuffelen #   define HASH_START_METHOD_DEF    void (HASH_START_METHOD)(void)
180*5c591343SA. Cody Schuffelen #endif
181*5c591343SA. Cody Schuffelen #ifndef HASH_DATA_METHOD_DEF
182*5c591343SA. Cody Schuffelen #   define HASH_DATA_METHOD_DEF     void (HASH_DATA_METHOD)(void)
183*5c591343SA. Cody Schuffelen #endif
184*5c591343SA. Cody Schuffelen #ifndef HASH_END_METHOD_DEF
185*5c591343SA. Cody Schuffelen #   define HASH_END_METHOD_DEF      void (HASH_END_METHOD)(void)
186*5c591343SA. Cody Schuffelen #endif
187*5c591343SA. Cody Schuffelen #ifndef HASH_STATE_COPY_METHOD_DEF
188*5c591343SA. Cody Schuffelen #   define HASH_STATE_COPY_METHOD_DEF     void (HASH_STATE_COPY_METHOD)(void)
189*5c591343SA. Cody Schuffelen #endif
190*5c591343SA. Cody Schuffelen #ifndef  HASH_STATE_EXPORT_METHOD_DEF
191*5c591343SA. Cody Schuffelen #   define  HASH_STATE_EXPORT_METHOD_DEF   void (HASH_STATE_EXPORT_METHOD)(void)
192*5c591343SA. Cody Schuffelen #endif
193*5c591343SA. Cody Schuffelen #ifndef  HASH_STATE_IMPORT_METHOD_DEF
194*5c591343SA. Cody Schuffelen #   define  HASH_STATE_IMPORT_METHOD_DEF   void (HASH_STATE_IMPORT_METHOD)(void)
195*5c591343SA. Cody Schuffelen #endif
196*5c591343SA. Cody Schuffelen 
197*5c591343SA. Cody Schuffelen // Define the prototypical function call for each of the methods. This defines the
198*5c591343SA. Cody Schuffelen // order in which the parameters are passed to the underlying function.
199*5c591343SA. Cody Schuffelen typedef HASH_START_METHOD_DEF;
200*5c591343SA. Cody Schuffelen typedef HASH_DATA_METHOD_DEF;
201*5c591343SA. Cody Schuffelen typedef HASH_END_METHOD_DEF;
202*5c591343SA. Cody Schuffelen typedef HASH_STATE_COPY_METHOD_DEF;
203*5c591343SA. Cody Schuffelen typedef HASH_STATE_EXPORT_METHOD_DEF;
204*5c591343SA. Cody Schuffelen typedef HASH_STATE_IMPORT_METHOD_DEF;
205*5c591343SA. Cody Schuffelen 
206*5c591343SA. Cody Schuffelen 
207*5c591343SA. Cody Schuffelen typedef struct _HASH_METHODS
208*5c591343SA. Cody Schuffelen {
209*5c591343SA. Cody Schuffelen     HASH_START_METHOD           *start;
210*5c591343SA. Cody Schuffelen     HASH_DATA_METHOD            *data;
211*5c591343SA. Cody Schuffelen     HASH_END_METHOD             *end;
212*5c591343SA. Cody Schuffelen     HASH_STATE_COPY_METHOD      *copy;      // Copy a hash block
213*5c591343SA. Cody Schuffelen     HASH_STATE_EXPORT_METHOD    *copyOut;   // Copy a hash block from a hash
214*5c591343SA. Cody Schuffelen                                             // context
215*5c591343SA. Cody Schuffelen     HASH_STATE_IMPORT_METHOD    *copyIn;    // Copy a hash block to a proper hash
216*5c591343SA. Cody Schuffelen                                             // context
217*5c591343SA. Cody Schuffelen } HASH_METHODS, *PHASH_METHODS;
218*5c591343SA. Cody Schuffelen 
219*5c591343SA. Cody Schuffelen #define HASH_TPM2B(HASH, Hash)  TPM2B_TYPE(HASH##_DIGEST, HASH##_DIGEST_SIZE);
220*5c591343SA. Cody Schuffelen 
221*5c591343SA. Cody Schuffelen FOR_EACH_HASH(HASH_TPM2B)
222*5c591343SA. Cody Schuffelen 
223*5c591343SA. Cody Schuffelen // When the TPM implements RSA, the hash-dependent OID pointers are part of the
224*5c591343SA. Cody Schuffelen // HASH_DEF. These macros conditionally add the OID reference to the HASH_DEF and the
225*5c591343SA. Cody Schuffelen // HASH_DEF_TEMPLATE.
226*5c591343SA. Cody Schuffelen #if ALG_RSA
227*5c591343SA. Cody Schuffelen #define PKCS1_HASH_REF   const BYTE  *PKCS1;
228*5c591343SA. Cody Schuffelen #define PKCS1_OID(NAME)  , OID_PKCS1_##NAME
229*5c591343SA. Cody Schuffelen #else
230*5c591343SA. Cody Schuffelen #define PKCS1_HASH_REF
231*5c591343SA. Cody Schuffelen #define PKCS1_OID(NAME)
232*5c591343SA. Cody Schuffelen #endif
233*5c591343SA. Cody Schuffelen 
234*5c591343SA. Cody Schuffelen // When the TPM implements ECC, the hash-dependent OID pointers are part of the
235*5c591343SA. Cody Schuffelen // HASH_DEF. These macros conditionally add the OID reference to the HASH_DEF and the
236*5c591343SA. Cody Schuffelen // HASH_DEF_TEMPLATE.
237*5c591343SA. Cody Schuffelen #if ALG_ECDSA
238*5c591343SA. Cody Schuffelen #define ECDSA_HASH_REF    const BYTE  *ECDSA;
239*5c591343SA. Cody Schuffelen #define ECDSA_OID(NAME)  , OID_ECDSA_##NAME
240*5c591343SA. Cody Schuffelen #else
241*5c591343SA. Cody Schuffelen #define ECDSA_HASH_REF
242*5c591343SA. Cody Schuffelen #define ECDSA_OID(NAME)
243*5c591343SA. Cody Schuffelen #endif
244*5c591343SA. Cody Schuffelen 
245*5c591343SA. Cody Schuffelen typedef const struct HASH_DEF
246*5c591343SA. Cody Schuffelen {
247*5c591343SA. Cody Schuffelen     HASH_METHODS         method;
248*5c591343SA. Cody Schuffelen     uint16_t             blockSize;
249*5c591343SA. Cody Schuffelen     uint16_t             digestSize;
250*5c591343SA. Cody Schuffelen     uint16_t             contextSize;
251*5c591343SA. Cody Schuffelen     uint16_t             hashAlg;
252*5c591343SA. Cody Schuffelen     const BYTE          *OID;
253*5c591343SA. Cody Schuffelen     PKCS1_HASH_REF      // PKCS1 OID
254*5c591343SA. Cody Schuffelen     ECDSA_HASH_REF      // ECDSA OID
255*5c591343SA. Cody Schuffelen } HASH_DEF, *PHASH_DEF;
256*5c591343SA. Cody Schuffelen 
257*5c591343SA. Cody Schuffelen // Macro to fill in the HASH_DEF for an algorithm. For SHA1, the instance would be:
258*5c591343SA. Cody Schuffelen //  HASH_DEF_TEMPLATE(Sha1, SHA1)
259*5c591343SA. Cody Schuffelen // This handles the difference in capitalization for the various pieces.
260*5c591343SA. Cody Schuffelen #define HASH_DEF_TEMPLATE(HASH, Hash)                                               \
261*5c591343SA. Cody Schuffelen     HASH_DEF    Hash##_Def= {                                                       \
262*5c591343SA. Cody Schuffelen                         {(HASH_START_METHOD *)&tpmHashStart_##HASH,                 \
263*5c591343SA. Cody Schuffelen                          (HASH_DATA_METHOD *)&tpmHashData_##HASH,                   \
264*5c591343SA. Cody Schuffelen                          (HASH_END_METHOD *)&tpmHashEnd_##HASH,                     \
265*5c591343SA. Cody Schuffelen                          (HASH_STATE_COPY_METHOD *)&tpmHashStateCopy_##HASH,        \
266*5c591343SA. Cody Schuffelen                          (HASH_STATE_EXPORT_METHOD *)&tpmHashStateExport_##HASH,    \
267*5c591343SA. Cody Schuffelen                          (HASH_STATE_IMPORT_METHOD *)&tpmHashStateImport_##HASH,    \
268*5c591343SA. Cody Schuffelen                         },                                                          \
269*5c591343SA. Cody Schuffelen                         HASH##_BLOCK_SIZE,     /*block size */                      \
270*5c591343SA. Cody Schuffelen                         HASH##_DIGEST_SIZE,    /*data size */                       \
271*5c591343SA. Cody Schuffelen                         sizeof(tpmHashState##HASH##_t),                             \
272*5c591343SA. Cody Schuffelen                         TPM_ALG_##HASH, OID_##HASH                                  \
273*5c591343SA. Cody Schuffelen                         PKCS1_OID(HASH) ECDSA_OID(HASH)};
274*5c591343SA. Cody Schuffelen 
275*5c591343SA. Cody Schuffelen // These definitions are for the types that can be in a hash state structure.
276*5c591343SA. Cody Schuffelen // These types are used in the cryptographic utilities. This is a define rather than
277*5c591343SA. Cody Schuffelen // an enum so that the size of this field can be explicit.
278*5c591343SA. Cody Schuffelen typedef BYTE    HASH_STATE_TYPE;
279*5c591343SA. Cody Schuffelen #define HASH_STATE_EMPTY        ((HASH_STATE_TYPE) 0)
280*5c591343SA. Cody Schuffelen #define HASH_STATE_HASH         ((HASH_STATE_TYPE) 1)
281*5c591343SA. Cody Schuffelen #define HASH_STATE_HMAC         ((HASH_STATE_TYPE) 2)
282*5c591343SA. Cody Schuffelen #if CC_MAC || CC_MAC_Start
283*5c591343SA. Cody Schuffelen #define HASH_STATE_SMAC         ((HASH_STATE_TYPE) 3)
284*5c591343SA. Cody Schuffelen #endif
285*5c591343SA. Cody Schuffelen 
286*5c591343SA. Cody Schuffelen 
287*5c591343SA. Cody Schuffelen // This is the structure that is used for passing a context into the hashing
288*5c591343SA. Cody Schuffelen // functions. It should be the same size as the function context used within
289*5c591343SA. Cody Schuffelen // the hashing functions. This is checked when the hash function is initialized.
290*5c591343SA. Cody Schuffelen // This version uses a new layout for the contexts and a different definition. The
291*5c591343SA. Cody Schuffelen // state buffer is an array of HASH_UNIT values so that a decent compiler will put
292*5c591343SA. Cody Schuffelen // the structure on a HASH_UNIT boundary. If the structure is not properly aligned,
293*5c591343SA. Cody Schuffelen // the code that manipulates the structure will copy to a properly aligned
294*5c591343SA. Cody Schuffelen // structure before it is used and copy the result back. This just makes things
295*5c591343SA. Cody Schuffelen // slower.
296*5c591343SA. Cody Schuffelen // NOTE: This version of the state had the pointer to the update method in the
297*5c591343SA. Cody Schuffelen // state. This is to allow the SMAC functions to use the same structure without
298*5c591343SA. Cody Schuffelen // having to replicate the entire HASH_DEF structure.
299*5c591343SA. Cody Schuffelen typedef struct _HASH_STATE
300*5c591343SA. Cody Schuffelen {
301*5c591343SA. Cody Schuffelen     HASH_STATE_TYPE          type;               // type of the context
302*5c591343SA. Cody Schuffelen     TPM_ALG_ID               hashAlg;
303*5c591343SA. Cody Schuffelen     PHASH_DEF                def;
304*5c591343SA. Cody Schuffelen     ANY_HASH_STATE           state;
305*5c591343SA. Cody Schuffelen } HASH_STATE, *PHASH_STATE;
306*5c591343SA. Cody Schuffelen typedef const HASH_STATE *PCHASH_STATE;
307*5c591343SA. Cody Schuffelen 
308*5c591343SA. Cody Schuffelen 
309*5c591343SA. Cody Schuffelen //** HMAC State Structures
310*5c591343SA. Cody Schuffelen 
311*5c591343SA. Cody Schuffelen // An HMAC_STATE structure contains an opaque HMAC stack state. A caller would
312*5c591343SA. Cody Schuffelen // use this structure when performing incremental HMAC operations. This structure
313*5c591343SA. Cody Schuffelen // contains a hash state and an HMAC key and allows slightly better stack
314*5c591343SA. Cody Schuffelen // optimization than adding an HMAC key to each hash state.
315*5c591343SA. Cody Schuffelen typedef struct hmacState
316*5c591343SA. Cody Schuffelen {
317*5c591343SA. Cody Schuffelen     HASH_STATE           hashState;          // the hash state
318*5c591343SA. Cody Schuffelen     TPM2B_HASH_BLOCK     hmacKey;            // the HMAC key
319*5c591343SA. Cody Schuffelen } HMAC_STATE, *PHMAC_STATE;
320*5c591343SA. Cody Schuffelen 
321*5c591343SA. Cody Schuffelen // This is for the external hash state. This implementation assumes that the size
322*5c591343SA. Cody Schuffelen // of the exported hash state is no larger than the internal hash state.
323*5c591343SA. Cody Schuffelen typedef struct
324*5c591343SA. Cody Schuffelen {
325*5c591343SA. Cody Schuffelen     BYTE                     buffer[sizeof(HASH_STATE)];
326*5c591343SA. Cody Schuffelen } EXPORT_HASH_STATE, *PEXPORT_HASH_STATE;
327*5c591343SA. Cody Schuffelen 
328*5c591343SA. Cody Schuffelen typedef const EXPORT_HASH_STATE *PCEXPORT_HASH_STATE;
329*5c591343SA. Cody Schuffelen 
330*5c591343SA. Cody Schuffelen #endif //  _CRYPT_HASH_H
331