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