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