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 // This header defines the interface between the hashing code and the LIbTomCrypt 38*5c591343SA. Cody Schuffelen // hash functions. 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_LTC 44*5c591343SA. Cody Schuffelen 45*5c591343SA. Cody Schuffelen // Avoid pulling in the MPA math if not doing asymmetric with LTC 46*5c591343SA. Cody Schuffelen #if !(defined MATH_LIB_LTC) 47*5c591343SA. Cody Schuffelen # define LTC_NO_ASYMMETRIC 48*5c591343SA. Cody Schuffelen #endif 49*5c591343SA. Cody Schuffelen 50*5c591343SA. Cody Schuffelen #define HASH_ALIGNMENT RADIX_BYTES 51*5c591343SA. Cody Schuffelen 52*5c591343SA. Cody Schuffelen #include "LtcSettings.h" 53*5c591343SA. Cody Schuffelen 54*5c591343SA. Cody Schuffelen //*************************************************************** 55*5c591343SA. Cody Schuffelen //******** Linking to the TomCrypt HASH code ******************** 56*5c591343SA. Cody Schuffelen //*************************************************************** 57*5c591343SA. Cody Schuffelen // These defines need to be known in all parts of the TPM so that the structure 58*5c591343SA. Cody Schuffelen // sizes can be properly computed when needed. 59*5c591343SA. Cody Schuffelen #define tpmHashStateSHA1_t struct sha1_state 60*5c591343SA. Cody Schuffelen #define tpmHashStateSHA256_t struct sha256_state 61*5c591343SA. Cody Schuffelen #define tpmHashStateSHA512_t struct sha512_state 62*5c591343SA. Cody Schuffelen #define tpmHashStateSHA384_t struct sha512_state 63*5c591343SA. Cody Schuffelen 64*5c591343SA. Cody Schuffelen // The following defines are only needed by CryptHash.c 65*5c591343SA. Cody Schuffelen #ifdef _CRYPT_HASH_C_ 66*5c591343SA. Cody Schuffelen 67*5c591343SA. Cody Schuffelen // Define the interface between CryptHash.c to the functions provided by the 68*5c591343SA. Cody Schuffelen // library. For each method, define the calling parameters of the method and then 69*5c591343SA. Cody Schuffelen // define how the method is invoked in CryptHash.c. 70*5c591343SA. Cody Schuffelen // 71*5c591343SA. Cody Schuffelen // All hashes are required to have the same calling sequence. If they don't, create 72*5c591343SA. Cody Schuffelen // a simple adaptation function that converts from the "standard" form of the call 73*5c591343SA. Cody Schuffelen // to the form used by the specific hash (and then send a nasty letter to the 74*5c591343SA. Cody Schuffelen // person who wrote the hash function for the library). 75*5c591343SA. Cody Schuffelen // 76*5c591343SA. Cody Schuffelen // The macro that calls the method also defines how the 77*5c591343SA. Cody Schuffelen // parameters get swizzled between the default form (in CryptHash.c)and the 78*5c591343SA. Cody Schuffelen // library form. 79*5c591343SA. Cody Schuffelen // 80*5c591343SA. Cody Schuffelen // Initialize the hash context 81*5c591343SA. Cody Schuffelen #define HASH_START_METHOD_DEF \ 82*5c591343SA. Cody Schuffelen void (HASH_START_METHOD)(PANY_HASH_STATE state) 83*5c591343SA. Cody Schuffelen #define HASH_START(hashState) \ 84*5c591343SA. Cody Schuffelen ((hashState)->def->method.start)(&(hashState)->state) 85*5c591343SA. Cody Schuffelen 86*5c591343SA. Cody Schuffelen // Add data to the hash 87*5c591343SA. Cody Schuffelen #define HASH_DATA_METHOD_DEF \ 88*5c591343SA. Cody Schuffelen void (HASH_DATA_METHOD)(PANY_HASH_STATE state, \ 89*5c591343SA. Cody Schuffelen const BYTE *buffer, \ 90*5c591343SA. Cody Schuffelen size_t size) 91*5c591343SA. Cody Schuffelen #define HASH_DATA(hashState, dInSize, dIn) \ 92*5c591343SA. Cody Schuffelen ((hashState)->def->method.data)(&(hashState)->state, dIn, dInSize) 93*5c591343SA. Cody Schuffelen 94*5c591343SA. Cody Schuffelen // Finalize the hash and get the digest 95*5c591343SA. Cody Schuffelen #define HASH_END_METHOD_DEF \ 96*5c591343SA. Cody Schuffelen void (HASH_END_METHOD)(PANY_HASH_STATE \ 97*5c591343SA. Cody Schuffelen state, \ 98*5c591343SA. Cody Schuffelen BYTE *buffer) 99*5c591343SA. Cody Schuffelen #define HASH_END(hashState, buffer) \ 100*5c591343SA. Cody Schuffelen ((hashState)->def->method.end)(&(hashState)->state, buffer) 101*5c591343SA. Cody Schuffelen 102*5c591343SA. Cody Schuffelen // Copy the hash context 103*5c591343SA. Cody Schuffelen // Note: For import, export, and copy, memcpy() is used since there is no 104*5c591343SA. Cody Schuffelen // reformatting necessary between the internal and external forms 105*5c591343SA. Cody Schuffelen #define HASH_STATE_COPY_METHOD_DEF \ 106*5c591343SA. Cody Schuffelen void (HASH_STATE_COPY_METHOD)(PANY_HASH_STATE to, \ 107*5c591343SA. Cody Schuffelen PCANY_HASH_STATE from, \ 108*5c591343SA. Cody Schuffelen size_t size) 109*5c591343SA. Cody Schuffelen #define HASH_STATE_COPY(hashStateOut, hashStateIn) \ 110*5c591343SA. Cody Schuffelen ((hashStateIn)->def->method.copy) \ 111*5c591343SA. Cody Schuffelen (&(hashStateOut)->state, \ 112*5c591343SA. Cody Schuffelen &(hashStateIn)->state, \ 113*5c591343SA. Cody Schuffelen (hashStateIn)->def->contextSize) 114*5c591343SA. Cody Schuffelen 115*5c591343SA. Cody Schuffelen // Copy (with reformatting when necessary) an internal hash structure to an 116*5c591343SA. Cody Schuffelen // external blob 117*5c591343SA. Cody Schuffelen #define HASH_STATE_EXPORT_METHOD_DEF \ 118*5c591343SA. Cody Schuffelen void (HASH_STATE_EXPORT_METHOD)(BYTE *to, \ 119*5c591343SA. Cody Schuffelen PANY_HASH_STATE from, \ 120*5c591343SA. Cody Schuffelen size_t size) 121*5c591343SA. Cody Schuffelen #define HASH_STATE_EXPORT(to, hashStateFrom) \ 122*5c591343SA. Cody Schuffelen ((hashStateFrom)->def->method.copyOut) \ 123*5c591343SA. Cody Schuffelen (&(((BYTE *)(to))[offsetof(HASH_STATE, state)]), \ 124*5c591343SA. Cody Schuffelen &(hashStateFrom)->state, \ 125*5c591343SA. Cody Schuffelen (hashStateFrom)->def->contextSize) 126*5c591343SA. Cody Schuffelen 127*5c591343SA. Cody Schuffelen // Copy from an external blob to an internal formate (with reformatting when 128*5c591343SA. Cody Schuffelen // necessary 129*5c591343SA. Cody Schuffelen #define HASH_STATE_IMPORT_METHOD_DEF \ 130*5c591343SA. Cody Schuffelen void (HASH_STATE_IMPORT_METHOD)(PANY_HASH_STATE to, \ 131*5c591343SA. Cody Schuffelen const BYTE *from, \ 132*5c591343SA. Cody Schuffelen size_t size) 133*5c591343SA. Cody Schuffelen #define HASH_STATE_IMPORT(hashStateTo, from) \ 134*5c591343SA. Cody Schuffelen ((hashStateTo)->def->method.copyIn) \ 135*5c591343SA. Cody Schuffelen (&(hashStateTo)->state, \ 136*5c591343SA. Cody Schuffelen &(((const BYTE *)(from))[offsetof(HASH_STATE, state)]),\ 137*5c591343SA. Cody Schuffelen (hashStateTo)->def->contextSize) 138*5c591343SA. Cody Schuffelen 139*5c591343SA. Cody Schuffelen // Internal External 140*5c591343SA. Cody Schuffelen // Designation Designation 141*5c591343SA. Cody Schuffelen #define tpmHashStart_SHA1 sha1_init 142*5c591343SA. Cody Schuffelen #define tpmHashData_SHA1 sha1_process 143*5c591343SA. Cody Schuffelen #define tpmHashEnd_SHA1 sha1_done 144*5c591343SA. Cody Schuffelen #define tpmHashStateCopy_SHA1 memcpy 145*5c591343SA. Cody Schuffelen #define tpmHashStateExport_SHA1 memcpy 146*5c591343SA. Cody Schuffelen #define tpmHashStateImport_SHA1 memcpy 147*5c591343SA. Cody Schuffelen #define tpmHashStart_SHA256 sha256_init 148*5c591343SA. Cody Schuffelen #define tpmHashData_SHA256 sha256_process 149*5c591343SA. Cody Schuffelen #define tpmHashEnd_SHA256 sha256_done 150*5c591343SA. Cody Schuffelen #define tpmHashStateCopy_SHA256 memcpy 151*5c591343SA. Cody Schuffelen #define tpmHashStateExport_SHA256 memcpy 152*5c591343SA. Cody Schuffelen #define tpmHashStateImport_SHA256 memcpy 153*5c591343SA. Cody Schuffelen #define tpmHashStart_SHA384 sha384_init 154*5c591343SA. Cody Schuffelen #define tpmHashData_SHA384 sha384_process 155*5c591343SA. Cody Schuffelen #define tpmHashEnd_SHA384 sha384_done 156*5c591343SA. Cody Schuffelen #define tpmHashStateCopy_SHA384 memcpy 157*5c591343SA. Cody Schuffelen #define tpmHashStateExport_SHA384 memcpy 158*5c591343SA. Cody Schuffelen #define tpmHashStateImport_SHA384 memcpy 159*5c591343SA. Cody Schuffelen #define tpmHashStart_SHA512 sha512_init 160*5c591343SA. Cody Schuffelen #define tpmHashData_SHA512 sha512_process 161*5c591343SA. Cody Schuffelen #define tpmHashEnd_SHA512 sha512_done 162*5c591343SA. Cody Schuffelen #define tpmHashStateCopy_SHA512 memcpy 163*5c591343SA. Cody Schuffelen #define tpmHashStateExport_SHA512 memcpy 164*5c591343SA. Cody Schuffelen #define tpmHashStateImport_SHA512 memcpy 165*5c591343SA. Cody Schuffelen 166*5c591343SA. Cody Schuffelen #endif // _CRYPT_HASH_C_ 167*5c591343SA. Cody Schuffelen 168*5c591343SA. Cody Schuffelen // No special processing to initialize the LTC hash library 169*5c591343SA. Cody Schuffelen #define LibHashInit() 170*5c591343SA. Cody Schuffelen 171*5c591343SA. Cody Schuffelen // No special processing at the end of the simulation (i.e., no statistics to print) 172*5c591343SA. Cody Schuffelen #define HashLibSimulationEnd() 173*5c591343SA. Cody Schuffelen 174*5c591343SA. Cody Schuffelen #endif // HASH_LIB_DEFINED 175