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