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