Debian Codegraph @

simulator/ms-tpm-20-ref/TPMCmd/tpm/include/Ossl/TpmToOsslHash.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 //
37 // This header file is used to 'splice' the OpenSSL hash code into the TPM code.
38 //
39 #ifndef HASH_LIB_DEFINED
40 #define HASH_LIB_DEFINED
41
42 #define HASH_LIB_OSSL
43
44 #include <openssl/evp.h>
45 #include <openssl/sha.h>
46 #include <openssl/ossl_typ.h>
47
48
49 //***************************************************************
50 //** Links to the OpenSSL HASH code
51 //***************************************************************
52
53 // Redefine the internal name used for each of the hash state structures to the
54 // name used by the library.
55 // These defines need to be known in all parts of the TPM so that the structure
56 // sizes can be properly computed when needed.
57
58 #define tpmHashStateSHA1_t SHA_CTX
59 #define tpmHashStateSHA256_t SHA256_CTX
60 #define tpmHashStateSHA384_t SHA512_CTX
61 #define tpmHashStateSHA512_t SHA512_CTX
62
63 #if ALG_SM3_256
64 # error "The version of OpenSSL used by this code does not support SM3"
65 #endif
66
67 // The defines below are only needed when compiling CryptHash.c or CryptSmac.c.
68 // This isolation is primarily to avoid name space collision. However, if there
69 // is a real collision, it will likely show up when the linker tries to put things
70 // together.
71
72 #ifdef _CRYPT_HASH_C_
73
74 typedef BYTE *PBYTE;
75 typedef const BYTE *PCBYTE;
76
77 // Define the interface between CryptHash.c to the functions provided by the
78 // library. For each method, define the calling parameters of the method and then
79 // define how the method is invoked in CryptHash.c.
80 //
81 // All hashes are required to have the same calling sequence. If they don't, create
82 // a simple adaptation function that converts from the "standard" form of the call
83 // to the form used by the specific hash (and then send a nasty letter to the
84 // person who wrote the hash function for the library).
85 //
86 // The macro that calls the method also defines how the
87 // parameters get swizzled between the default form (in CryptHash.c)and the
88 // library form.
89 //
90 // Initialize the hash context
91 #define HASH_START_METHOD_DEF void (HASH_START_METHOD)(PANY_HASH_STATE state)
92 #define HASH_START(hashState) \
93 ((hashState)->def->method.start)(&(hashState)->state);
94
95 // Add data to the hash
96 #define HASH_DATA_METHOD_DEF \
97 void (HASH_DATA_METHOD)(PANY_HASH_STATE state, \
98 PCBYTE buffer, \
99 size_t size)
100 #define HASH_DATA(hashState, dInSize, dIn) \
101 ((hashState)->def->method.data)(&(hashState)->state, dIn, dInSize)
102
103 // Finalize the hash and get the digest
104 #define HASH_END_METHOD_DEF \
105 void (HASH_END_METHOD)(BYTE *buffer, PANY_HASH_STATE state)
106 #define HASH_END(hashState, buffer) \
107 ((hashState)->def->method.end)(buffer, &(hashState)->state)
108
109 // Copy the hash context
110 // Note: For import, export, and copy, memcpy() is used since there is no
111 // reformatting necessary between the internal and external forms.
112 #define HASH_STATE_COPY_METHOD_DEF \
113 void (HASH_STATE_COPY_METHOD)(PANY_HASH_STATE to, \
114 PCANY_HASH_STATE from, \
115 size_t size)
116 #define HASH_STATE_COPY(hashStateOut, hashStateIn) \
117 ((hashStateIn)->def->method.copy)(&(hashStateOut)->state, \
118 &(hashStateIn)->state, \
119 (hashStateIn)->def->contextSize)
120
121 // Copy (with reformatting when necessary) an internal hash structure to an
122 // external blob
123 #define HASH_STATE_EXPORT_METHOD_DEF \
124 void (HASH_STATE_EXPORT_METHOD)(BYTE *to, \
125 PCANY_HASH_STATE from, \
126 size_t size)
127 #define HASH_STATE_EXPORT(to, hashStateFrom) \
128 ((hashStateFrom)->def->method.copyOut) \
129 (&(((BYTE *)(to))[offsetof(HASH_STATE, state)]), \
130 &(hashStateFrom)->state, \
131 (hashStateFrom)->def->contextSize)
132
133 // Copy from an external blob to an internal formate (with reformatting when
134 // necessary
135 #define HASH_STATE_IMPORT_METHOD_DEF \
136 void (HASH_STATE_IMPORT_METHOD)(PANY_HASH_STATE to, \
137 const BYTE *from, \
138 size_t size)
139 #define HASH_STATE_IMPORT(hashStateTo, from) \
140 ((hashStateTo)->def->method.copyIn) \
141 (&(hashStateTo)->state, \
142 &(((const BYTE *)(from))[offsetof(HASH_STATE, state)]),\
143 (hashStateTo)->def->contextSize)
144
145
146 // Function aliases. The code in CryptHash.c uses the internal designation for the
147 // functions. These need to be translated to the function names of the library.
148 #define tpmHashStart_SHA1 SHA1_Init // external name of the
149 // initialization method
150 #define tpmHashData_SHA1 SHA1_Update
151 #define tpmHashEnd_SHA1 SHA1_Final
152 #define tpmHashStateCopy_SHA1 memcpy
153 #define tpmHashStateExport_SHA1 memcpy
154 #define tpmHashStateImport_SHA1 memcpy
155 #define tpmHashStart_SHA256 SHA256_Init
156 #define tpmHashData_SHA256 SHA256_Update
157 #define tpmHashEnd_SHA256 SHA256_Final
158 #define tpmHashStateCopy_SHA256 memcpy
159 #define tpmHashStateExport_SHA256 memcpy
160 #define tpmHashStateImport_SHA256 memcpy
161 #define tpmHashStart_SHA384 SHA384_Init
162 #define tpmHashData_SHA384 SHA384_Update
163 #define tpmHashEnd_SHA384 SHA384_Final
164 #define tpmHashStateCopy_SHA384 memcpy
165 #define tpmHashStateExport_SHA384 memcpy
166 #define tpmHashStateImport_SHA384 memcpy
167 #define tpmHashStart_SHA512 SHA512_Init
168 #define tpmHashData_SHA512 SHA512_Update
169 #define tpmHashEnd_SHA512 SHA512_Final
170 #define tpmHashStateCopy_SHA512 memcpy
171 #define tpmHashStateExport_SHA512 memcpy
172 #define tpmHashStateImport_SHA512 memcpy
173
174 #endif // _CRYPT_HASH_C_
175
176 #define LibHashInit()
177 // This definition would change if there were something to report
178 #define HashLibSimulationEnd()
179
180 #endif // HASH_LIB_DEFINED