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 
47 #if ALG_SM3_256
48 #   if defined(OPENSSL_NO_SM3) || OPENSSL_VERSION_NUMBER < 0x10101010L
49 #       undef ALG_SM3_256
50 #       define ALG_SM3_256  ALG_NO
51 #   elif OPENSSL_VERSION_NUMBER >= 0x10200000L
52 #       include <openssl/sm3.h>
53 #   else
54         // OpenSSL 1.1.1 keeps smX.h headers in the include/crypto directory,
55         // and they do not get installed as part of the libssl package
56 #       define SM3_LBLOCK      (64/4)
57 
58         typedef struct SM3state_st {
59            unsigned int A, B, C, D, E, F, G, H;
60            unsigned int Nl, Nh;
61            unsigned int data[SM3_LBLOCK];
62            unsigned int num;
63         } SM3_CTX;
64 
65         int sm3_init(SM3_CTX *c);
66         int sm3_update(SM3_CTX *c, const void *data, size_t len);
67         int sm3_final(unsigned char *md, SM3_CTX *c);
68 #   endif // OpenSSL < 1.2
69 #endif // ALG_SM3_256
70 
71 #include <openssl/ossl_typ.h>
72 
73 #define HASH_ALIGNMENT  RADIX_BYTES
74 
75 
76 //***************************************************************
77 //** Links to the OpenSSL HASH code
78 //***************************************************************
79 
80 // Redefine the internal name used for each of the hash state structures to the
81 // name used by the library.
82 // These defines need to be known in all parts of the TPM so that the structure
83 // sizes can be properly computed when needed.
84 #define tpmHashStateSHA1_t        SHA_CTX
85 #define tpmHashStateSHA256_t      SHA256_CTX
86 #define tpmHashStateSHA384_t      SHA512_CTX
87 #define tpmHashStateSHA512_t      SHA512_CTX
88 #define tpmHashStateSM3_256_t     SM3_CTX
89 
90 // The defines below are only needed when compiling CryptHash.c or CryptSmac.c.
91 // This isolation is primarily to avoid name space collision. However, if there
92 // is a real collision, it will likely show up when the linker tries to put things
93 // together.
94 
95 #ifdef _CRYPT_HASH_C_
96 
97 typedef BYTE          *PBYTE;
98 typedef const BYTE    *PCBYTE;
99 
100 // Define the interface between CryptHash.c to the functions provided by the
101 // library. For each method, define the calling parameters of the method and then
102 // define how the method is invoked in CryptHash.c.
103 //
104 // All hashes are required to have the same calling sequence. If they don't, create
105 // a simple adaptation function that converts from the "standard" form of the call
106 // to the form used by the specific hash (and then send a nasty letter to the
107 // person who wrote the hash function for the library).
108 //
109 // The macro that calls the method also defines how the
110 // parameters get swizzled between the default form (in CryptHash.c)and the
111 // library form.
112 //
113 // Initialize the hash context
114 #define HASH_START_METHOD_DEF   void (HASH_START_METHOD)(PANY_HASH_STATE state)
115 #define HASH_START(hashState)                                                   \
116                 ((hashState)->def->method.start)(&(hashState)->state);
117 
118 // Add data to the hash
119 #define HASH_DATA_METHOD_DEF                                                    \
120                 void (HASH_DATA_METHOD)(PANY_HASH_STATE state,                  \
121                                     PCBYTE buffer,                              \
122                                     size_t size)
123 #define HASH_DATA(hashState, dInSize, dIn)                                      \
124                 ((hashState)->def->method.data)(&(hashState)->state, dIn, dInSize)
125 
126 // Finalize the hash and get the digest
127 #define HASH_END_METHOD_DEF                                                     \
128                 void (HASH_END_METHOD)(BYTE *buffer, PANY_HASH_STATE state)
129 #define HASH_END(hashState, buffer)                                             \
130                 ((hashState)->def->method.end)(buffer, &(hashState)->state)
131 
132 // Copy the hash context
133 // Note: For import, export, and copy, memcpy() is used since there is no
134 // reformatting necessary between the internal and external forms.
135 #define HASH_STATE_COPY_METHOD_DEF                                              \
136                 void (HASH_STATE_COPY_METHOD)(PANY_HASH_STATE to,               \
137                                               PCANY_HASH_STATE from,            \
138                                               size_t size)
139 #define HASH_STATE_COPY(hashStateOut, hashStateIn)                              \
140                 ((hashStateIn)->def->method.copy)(&(hashStateOut)->state,       \
141                                               &(hashStateIn)->state,            \
142                                               (hashStateIn)->def->contextSize)
143 
144 // Copy (with reformatting when necessary) an internal hash structure to an
145 // external blob
146 #define  HASH_STATE_EXPORT_METHOD_DEF                                           \
147                 void (HASH_STATE_EXPORT_METHOD)(BYTE *to,                       \
148                                           PCANY_HASH_STATE from,                \
149                                           size_t size)
150 #define  HASH_STATE_EXPORT(to, hashStateFrom)                                   \
151                 ((hashStateFrom)->def->method.copyOut)                          \
152                         (&(((BYTE *)(to))[offsetof(HASH_STATE, state)]),        \
153                          &(hashStateFrom)->state,                               \
154                          (hashStateFrom)->def->contextSize)
155 
156 // Copy from an external blob to an internal formate (with reformatting when
157 // necessary
158 #define  HASH_STATE_IMPORT_METHOD_DEF                                           \
159                 void (HASH_STATE_IMPORT_METHOD)(PANY_HASH_STATE to,             \
160                                                 const BYTE *from,               \
161                                                  size_t size)
162 #define  HASH_STATE_IMPORT(hashStateTo, from)                                   \
163                 ((hashStateTo)->def->method.copyIn)                             \
164                         (&(hashStateTo)->state,                                 \
165                          &(((const BYTE *)(from))[offsetof(HASH_STATE, state)]),\
166                          (hashStateTo)->def->contextSize)
167 
168 
169 // Function aliases. The code in CryptHash.c uses the internal designation for the
170 // functions. These need to be translated to the function names of the library.
171 #define tpmHashStart_SHA1           SHA1_Init   // external name of the
172                                                 // initialization method
173 #define tpmHashData_SHA1            SHA1_Update
174 #define tpmHashEnd_SHA1             SHA1_Final
175 #define tpmHashStateCopy_SHA1       memcpy
176 #define tpmHashStateExport_SHA1     memcpy
177 #define tpmHashStateImport_SHA1     memcpy
178 #define tpmHashStart_SHA256         SHA256_Init
179 #define tpmHashData_SHA256          SHA256_Update
180 #define tpmHashEnd_SHA256           SHA256_Final
181 #define tpmHashStateCopy_SHA256     memcpy
182 #define tpmHashStateExport_SHA256   memcpy
183 #define tpmHashStateImport_SHA256   memcpy
184 #define tpmHashStart_SHA384         SHA384_Init
185 #define tpmHashData_SHA384          SHA384_Update
186 #define tpmHashEnd_SHA384           SHA384_Final
187 #define tpmHashStateCopy_SHA384     memcpy
188 #define tpmHashStateExport_SHA384   memcpy
189 #define tpmHashStateImport_SHA384   memcpy
190 #define tpmHashStart_SHA512         SHA512_Init
191 #define tpmHashData_SHA512          SHA512_Update
192 #define tpmHashEnd_SHA512           SHA512_Final
193 #define tpmHashStateCopy_SHA512     memcpy
194 #define tpmHashStateExport_SHA512   memcpy
195 #define tpmHashStateImport_SHA_512  memcpy
196 #define tpmHashStart_SM3_256        sm3_init
197 #define tpmHashData_SM3_256         sm3_update
198 #define tpmHashEnd_SM3_256          sm3_final
199 #define tpmHashStateCopy_SM3_256    memcpy
200 #define tpmHashStateExport_SM3_256  memcpy
201 #define tpmHashStateImport_SM3_256  memcpy
202 
203 #endif // _CRYPT_HASH_C_
204 
205 #define LibHashInit()
206 // This definition would change if there were something to report
207 #define HashLibSimulationEnd()
208 
209 #endif // HASH_LIB_DEFINED
210