Debian Codegraph @

simulator/ms-tpm-20-ref/TPMCmd/tpm/include/CryptHash.h

raw
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 //** Introduction
36 // This header contains the hash structure definitions used in the TPM code
37 // to define the amount of space to be reserved for the hash state. This allows
38 // the TPM code to not have to import all of the symbols used by the hash
39 // computations. This lets the build environment of the TPM code not to have
40 // include the header files associated with the CryptoEngine code.
41
42 #ifndef _CRYPT_HASH_H
43 #define _CRYPT_HASH_H
44
45 //** Hash-related Structures
46
47 union SMAC_STATES;
48
49 // These definitions add the high-level methods for processing state that may be
50 // an SMAC
51 typedef void(* SMAC_DATA_METHOD)(
52 union SMAC_STATES *state,
53 UINT32 size,
54 const BYTE *buffer
55 );
56
57 typedef UINT16(* SMAC_END_METHOD)(
58 union SMAC_STATES *state,
59 UINT32 size,
60 BYTE *buffer
61 );
62
63 typedef struct sequenceMethods {
64 SMAC_DATA_METHOD data;
65 SMAC_END_METHOD end;
66 } SMAC_METHODS;
67
68 #define SMAC_IMPLEMENTED (CC_MAC || CC_MAC_Start)
69
70 // These definitions are here because the SMAC state is in the union of hash states.
71 typedef struct tpmCmacState {
72 TPM_ALG_ID symAlg;
73 UINT16 keySizeBits;
74 INT16 bcount; // current count of bytes accumulated in IV
75 TPM2B_IV iv; // IV buffer
76 TPM2B_SYM_KEY symKey;
77 } tpmCmacState_t;
78
79 typedef union SMAC_STATES {
80 #if ALG_CMAC
81 tpmCmacState_t cmac;
82 #endif
83 UINT64 pad;
84 } SMAC_STATES;
85
86 typedef struct SMAC_STATE {
87 SMAC_METHODS smacMethods;
88 SMAC_STATES state;
89 } SMAC_STATE;
90
91
92 typedef union
93 {
94 #if ALG_SHA1
95 tpmHashStateSHA1_t Sha1;
96 #endif
97 #if ALG_SHA256
98 tpmHashStateSHA256_t Sha256;
99 #endif
100 #if ALG_SHA384
101 tpmHashStateSHA384_t Sha384;
102 #endif
103 #if ALG_SHA512
104 tpmHashStateSHA512_t Sha512;
105 #endif
106
107 // Additions for symmetric block cipher MAC
108 #if SMAC_IMPLEMENTED
109 SMAC_STATE smac;
110 #endif
111 // to force structure alignment to be no worse than HASH_ALIGNMENT
112 #if HASH_ALIGNMENT == 4
113 uint32_t align;
114 #else
115 uint64_t align;
116 #endif
117 } ANY_HASH_STATE;
118
119 typedef ANY_HASH_STATE *PANY_HASH_STATE;
120 typedef const ANY_HASH_STATE *PCANY_HASH_STATE;
121
122 #define ALIGNED_SIZE(x, b) ((((x) + (b) - 1) / (b)) * (b))
123 // MAX_HASH_STATE_SIZE will change with each implementation. It is assumed that
124 // a hash state will not be larger than twice the block size plus some
125 // overhead (in this case, 16 bytes). The overall size needs to be as
126 // large as any of the hash contexts. The structure needs to start on an
127 // alignment boundary and be an even multiple of the alignment
128 #define MAX_HASH_STATE_SIZE ((2 * MAX_HASH_BLOCK_SIZE) + 16)
129 #define MAX_HASH_STATE_SIZE_ALIGNED \
130 ALIGNED_SIZE(MAX_HASH_STATE_SIZE, HASH_ALIGNMENT)
131
132 // This is an aligned byte array that will hold any of the hash contexts.
133 typedef ANY_HASH_STATE ALIGNED_HASH_STATE;
134
135 // The header associated with the hash library is expected to define the methods
136 // which include the calling sequence. When not compiling CryptHash.c, the methods
137 // are not defined so we need placeholder functions for the structures
138
139 #ifndef HASH_START_METHOD_DEF
140 # define HASH_START_METHOD_DEF void (HASH_START_METHOD)(void)
141 #endif
142 #ifndef HASH_DATA_METHOD_DEF
143 # define HASH_DATA_METHOD_DEF void (HASH_DATA_METHOD)(void)
144 #endif
145 #ifndef HASH_END_METHOD_DEF
146 # define HASH_END_METHOD_DEF void (HASH_END_METHOD)(void)
147 #endif
148 #ifndef HASH_STATE_COPY_METHOD_DEF
149 # define HASH_STATE_COPY_METHOD_DEF void (HASH_STATE_COPY_METHOD)(void)
150 #endif
151 #ifndef HASH_STATE_EXPORT_METHOD_DEF
152 # define HASH_STATE_EXPORT_METHOD_DEF void (HASH_STATE_EXPORT_METHOD)(void)
153 #endif
154 #ifndef HASH_STATE_IMPORT_METHOD_DEF
155 # define HASH_STATE_IMPORT_METHOD_DEF void (HASH_STATE_IMPORT_METHOD)(void)
156 #endif
157
158 // Define the prototypical function call for each of the methods. This defines the
159 // order in which the parameters are passed to the underlying function.
160 typedef HASH_START_METHOD_DEF;
161 typedef HASH_DATA_METHOD_DEF;
162 typedef HASH_END_METHOD_DEF;
163 typedef HASH_STATE_COPY_METHOD_DEF;
164 typedef HASH_STATE_EXPORT_METHOD_DEF;
165 typedef HASH_STATE_IMPORT_METHOD_DEF;
166
167
168 typedef struct _HASH_METHODS
169 {
170 HASH_START_METHOD *start;
171 HASH_DATA_METHOD *data;
172 HASH_END_METHOD *end;
173 HASH_STATE_COPY_METHOD *copy; // Copy a hash block
174 HASH_STATE_EXPORT_METHOD *copyOut; // Copy a hash block from a hash
175 // context
176 HASH_STATE_IMPORT_METHOD *copyIn; // Copy a hash block to a proper hash
177 // context
178 } HASH_METHODS, *PHASH_METHODS;
179
180 #if ALG_SHA1
181 TPM2B_TYPE(SHA1_DIGEST, SHA1_DIGEST_SIZE);
182 #endif
183 #if ALG_SHA256
184 TPM2B_TYPE(SHA256_DIGEST, SHA256_DIGEST_SIZE);
185 #endif
186 #if ALG_SHA384
187 TPM2B_TYPE(SHA384_DIGEST, SHA384_DIGEST_SIZE);
188 #endif
189 #if ALG_SHA512
190 TPM2B_TYPE(SHA512_DIGEST, SHA512_DIGEST_SIZE);
191 #endif
192 #if ALG_SM3_256
193 TPM2B_TYPE(SM3_256_DIGEST, SM3_256_DIGEST_SIZE);
194 #endif
195
196 // When the TPM implements RSA, the hash-dependent OID pointers are part of the
197 // HASH_DEF. These macros conditionally add the OID reference to the HASH_DEF and the
198 // HASH_DEF_TEMPLATE.
199 #if ALG_RSA
200 #define PKCS1_HASH_REF const BYTE *PKCS1;
201 #define PKCS1_OID(NAME) , OID_PKCS1_##NAME
202 #else
203 #define PKCS1_HASH_REF
204 #define PKCS1_OID(NAME)
205 #endif
206
207 // When the TPM implements ECC, the hash-dependent OID pointers are part of the
208 // HASH_DEF. These macros conditionally add the OID reference to the HASH_DEF and the
209 // HASH_DEF_TEMPLATE.
210 #if ALG_ECDSA
211 #define ECDSA_HASH_REF const BYTE *ECDSA;
212 #define ECDSA_OID(NAME) , OID_ECDSA_##NAME
213 #else
214 #define ECDSA_HASH_REF
215 #define ECDSA_OID(NAME)
216 #endif
217
218 typedef const struct HASH_DEF
219 {
220 HASH_METHODS method;
221 uint16_t blockSize;
222 uint16_t digestSize;
223 uint16_t contextSize;
224 uint16_t hashAlg;
225 const BYTE *OID;
226 PKCS1_HASH_REF // PKCS1 OID
227 ECDSA_HASH_REF // ECDSA OID
228 } HASH_DEF, *PHASH_DEF;
229
230 // Macro to fill in the HASH_DEF for an algorithm. For SHA1, the instance would be:
231 // HASH_DEF_TEMPLATE(Sha1, SHA1)
232 // This handles the difference in capitalization for the various pieces.
233 #define HASH_DEF_TEMPLATE(HASH, Hash) \
234 HASH_DEF Hash##_Def= { \
235 {(HASH_START_METHOD *)&tpmHashStart_##HASH, \
236 (HASH_DATA_METHOD *)&tpmHashData_##HASH, \
237 (HASH_END_METHOD *)&tpmHashEnd_##HASH, \
238 (HASH_STATE_COPY_METHOD *)&tpmHashStateCopy_##HASH, \
239 (HASH_STATE_EXPORT_METHOD *)&tpmHashStateExport_##HASH, \
240 (HASH_STATE_IMPORT_METHOD *)&tpmHashStateImport_##HASH, \
241 }, \
242 HASH##_BLOCK_SIZE, /*block size */ \
243 HASH##_DIGEST_SIZE, /*data size */ \
244 sizeof(tpmHashState##HASH##_t), \
245 TPM_ALG_##HASH, OID_##HASH \
246 PKCS1_OID(HASH) ECDSA_OID(HASH)};
247
248 // These definitions are for the types that can be in a hash state structure.
249 // These types are used in the cryptographic utilities. This is a define rather than
250 // an enum so that the size of this field can be explicit.
251 typedef BYTE HASH_STATE_TYPE;
252 #define HASH_STATE_EMPTY ((HASH_STATE_TYPE) 0)
253 #define HASH_STATE_HASH ((HASH_STATE_TYPE) 1)
254 #define HASH_STATE_HMAC ((HASH_STATE_TYPE) 2)
255 #if CC_MAC || CC_MAC_Start
256 #define HASH_STATE_SMAC ((HASH_STATE_TYPE) 3)
257 #endif
258
259
260 // This is the structure that is used for passing a context into the hashing
261 // functions. It should be the same size as the function context used within
262 // the hashing functions. This is checked when the hash function is initialized.
263 // This version uses a new layout for the contexts and a different definition. The
264 // state buffer is an array of HASH_UNIT values so that a decent compiler will put
265 // the structure on a HASH_UNIT boundary. If the structure is not properly aligned,
266 // the code that manipulates the structure will copy to a properly aligned
267 // structure before it is used and copy the result back. This just makes things
268 // slower.
269 // NOTE: This version of the state had the pointer to the update method in the
270 // state. This is to allow the SMAC functions to use the same structure without
271 // having to replicate the entire HASH_DEF structure.
272 typedef struct _HASH_STATE
273 {
274 HASH_STATE_TYPE type; // type of the context
275 TPM_ALG_ID hashAlg;
276 PHASH_DEF def;
277 ANY_HASH_STATE state;
278 } HASH_STATE, *PHASH_STATE;
279 typedef const HASH_STATE *PCHASH_STATE;
280
281
282 //** HMAC State Structures
283
284 // An HMAC_STATE structure contains an opaque HMAC stack state. A caller would
285 // use this structure when performing incremental HMAC operations. This structure
286 // contains a hash state and an HMAC key and allows slightly better stack
287 // optimization than adding an HMAC key to each hash state.
288 typedef struct hmacState
289 {
290 HASH_STATE hashState; // the hash state
291 TPM2B_HASH_BLOCK hmacKey; // the HMAC key
292 } HMAC_STATE, *PHMAC_STATE;
293
294 // This is for the external hash state. This implementation assumes that the size
295 // of the exported hash state is no larger than the internal hash state.
296 typedef struct
297 {
298 BYTE buffer[sizeof(HASH_STATE)];
299 } EXPORT_HASH_STATE, *PEXPORT_HASH_STATE;
300
301 typedef const EXPORT_HASH_STATE *PCEXPORT_HASH_STATE;
302
303 #endif // _CRYPT_HASH_H