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