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