diff --git a/.gitignore b/.gitignore index 70c4d459..36b6a535 100644 --- a/.gitignore +++ b/.gitignore @@ -290,3 +290,5 @@ __pycache__/ # TPM simulator run-time/state files NVChip RsaKeyCacheCrt.data +TPMCmd/OsslInclude/* +TPMCmd/Lib/* diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..d1f75cdf --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "external/wolfssl"] + path = external/wolfssl + url = https://github.com/wolfSSL/wolfssl.git diff --git a/TPMCmd/Platform/platform.vcxproj b/TPMCmd/Platform/platform.vcxproj index 4a29cc65..7284d147 100644 --- a/TPMCmd/Platform/platform.vcxproj +++ b/TPMCmd/Platform/platform.vcxproj @@ -1,5 +1,5 @@  - + Coverage @@ -87,86 +87,86 @@ platform Win32Proj Platform - 8.1 + 10.0.16299.0 StaticLibrary Unicode true - v140 + v141 DynamicLibrary Unicode false - v140 + v141 StaticLibrary Unicode - v100 + v141 Makefile Unicode - v140 + v141 StaticLibrary Unicode - v140 + v141 DynamicLibrary Unicode - v140 + v141 DynamicLibrary Unicode - v140 + v141 DynamicLibrary Unicode - v140 + v141 DynamicLibrary Unicode - v140 + v141 DynamicLibrary Unicode - v140 + v141 DynamicLibrary Unicode - v140 + v141 DynamicLibrary Unicode - v140 + v141 DynamicLibrary Unicode - v140 + v141 DynamicLibrary Unicode - v140 + v141 - v140 + v141 - v140 + v141 Makefile diff --git a/TPMCmd/Simulator/simulator.vcxproj b/TPMCmd/Simulator/simulator.vcxproj index cbe97639..5b022d67 100644 --- a/TPMCmd/Simulator/simulator.vcxproj +++ b/TPMCmd/Simulator/simulator.vcxproj @@ -1,5 +1,5 @@  - + Coverage @@ -71,86 +71,86 @@ simulator Win32Proj Simulator - 8.1 + 10.0.16299.0 Application Unicode false - v140 + v141 Application Unicode false - v140 + v141 Application Unicode - v100 + v141 Makefile Unicode - v140 + v141 Application Unicode - v140 + v141 Application Unicode - v140 + v141 Application Unicode - v140 + v141 Application Unicode - v140 + v141 Application Unicode - v140 + v141 Application Unicode - v140 + v141 Application Unicode - v140 + v141 Application Unicode - v140 + v141 Application Unicode - v140 + v141 Application Unicode - v140 + v141 - v140 + v141 - v140 + v141 Makefile diff --git a/TPMCmd/tpm/Tpm.vcxproj b/TPMCmd/tpm/Tpm.vcxproj index 3246d5b1..8633a2f7 100644 --- a/TPMCmd/tpm/Tpm.vcxproj +++ b/TPMCmd/tpm/Tpm.vcxproj @@ -1,5 +1,5 @@  - + Coverage @@ -63,76 +63,76 @@ {B7456491-A2ED-4B1C-B59E-41C7B32B7E3B} TPMCmd Win32Proj - 8.1 + 10.0.16299.0 DynamicLibrary Unicode false - v140 + v141 DynamicLibrary Unicode - v100 + v141 StaticLibrary Unicode - v140 + v141 DynamicLibrary Unicode - v140 + v141 DynamicLibrary Unicode - v140 + v141 DynamicLibrary Unicode - v140 + v141 DynamicLibrary Unicode false - v140 + v141 DynamicLibrary Unicode - v140 + v141 DynamicLibrary Unicode - v140 + v141 DynamicLibrary Unicode - v140 + v141 DynamicLibrary Unicode - v140 + v141 DynamicLibrary Unicode - v140 + v141 - v140 + v141 - v140 + v141 Makefile @@ -280,7 +280,7 @@ Disabled - $(ProjectDir)include\;$(ProjectDir)include\prototypes\;$(SolutionDir)Platform\include\;$(SolutionDir)Platform\include\prototypes\;$(SolutionDir)LtcInclude\;$(SolutionDIr)OsslInclude\;$(SolutionDir)MsBnInclude\ + $(ProjectDir)include\;$(ProjectDir)include\prototypes\;$(SolutionDir)Platform\include\;$(SolutionDir)Platform\include\prototypes\;$(SolutionDir)LtcInclude\;$(SolutionDIr)OsslInclude\;$(SolutionDir)MsBnInclude\;$(SolutionDir)..\external\wolfssl %(PreprocessorDefinitions) false MultiThreadedDebugDLL @@ -871,6 +871,15 @@ + + $(SolutionDir)..\external\wolfssl\wolfssl;%(AdditionalIncludeDirectories) + $(SolutionDir)..\external\wolfssl\wolfssl;%(AdditionalIncludeDirectories) + + + $(SolutionDir)..\external\wolfssl\wolfssl;$(ProjectDir)include\;$(ProjectDir)include\prototypes\;$(SolutionDir)Platform\include\;$(SolutionDir)Platform\include\prototypes\;$(SolutionDir)LtcInclude\;$(SolutionDIr)OsslInclude\;$(SolutionDir)MsBnInclude\ + $(SolutionDir)..\external\wolfssl\wolfssl;$(ProjectDir)include\;$(ProjectDir)include\prototypes\;$(SolutionDir)Platform\include\;$(SolutionDir)Platform\include\prototypes\;$(SolutionDir)LtcInclude\;$(SolutionDIr)OsslInclude\;$(SolutionDir)MsBnInclude\ + + @@ -1145,6 +1154,10 @@ + + + + diff --git a/TPMCmd/tpm/Tpm.vcxproj.filters b/TPMCmd/tpm/Tpm.vcxproj.filters index 591d1168..7a280454 100644 --- a/TPMCmd/tpm/Tpm.vcxproj.filters +++ b/TPMCmd/tpm/Tpm.vcxproj.filters @@ -577,6 +577,15 @@ Source Files\Crypt + + Source Files\Crypt\wolf + + + Source Files\Crypt\wolf + + + Source Files\Crypt\wolf + @@ -1284,6 +1293,18 @@ Headers\Crypt\ossl + + Headers\Crypt\wolf + + + Headers\Crypt\wolf + + + Headers\Crypt\wolf + + + Headers\Crypt\wolf + @@ -1316,5 +1337,11 @@ {1a979e04-0e58-4286-b6b0-52cb2b86d037} + + {17d101a1-3063-43f5-960b-b0df7b0950cc} + + + {19c87ebb-c1f3-4521-a442-d9120957d238} + \ No newline at end of file diff --git a/TPMCmd/tpm/include/Implementation.h b/TPMCmd/tpm/include/Implementation.h index c57243d2..b3aad82a 100644 --- a/TPMCmd/tpm/include/Implementation.h +++ b/TPMCmd/tpm/include/Implementation.h @@ -314,7 +314,7 @@ #define RADIX_BITS 32 #define HASH_ALIGNMENT 4 #define SYMMETRIC_ALIGNMENT 4 -#define HASH_LIB OSSL +#define HASH_LIB WOLF #define SYM_LIB OSSL #define MATH_LIB OSSL #define BSIZE UINT16 diff --git a/TPMCmd/tpm/include/LibSupport.h b/TPMCmd/tpm/include/LibSupport.h index 9e76ca5a..66b4d8c0 100644 --- a/TPMCmd/tpm/include/LibSupport.h +++ b/TPMCmd/tpm/include/LibSupport.h @@ -50,6 +50,9 @@ // SYMCRYPT only provides symmetric cryptography so would need to be combined with // another library that has math support #define SYMCRYPT 4 +// WolfCrypt from the statically linkable GPL WolfSLL, other commercial licenses +// also available +#define WOLF 5 //********************* #if RADIX_BITS == 32 @@ -69,6 +72,8 @@ # include "ltc/TpmToLtcHash.h" #elif HASH_LIB == SYMCRYPT #include "symcrypt/TpmToSymcryptHash.h" +#elif HASH_LIB == WOLF +# include "wolf/TpmToWolfHash.h" #else # error "No hash library selected" #endif @@ -81,6 +86,8 @@ # include "ltc/TpmToLtcSym.h" #elif SYM_LIB == SYMCRYPT #include "symcrypt/TpmToSymcryptSym.h" +#elif SYM_LIB == WOLF +# include "wolf/TpmToWolfSym.h" #else # error "No symmetric library selected" #endif @@ -98,6 +105,8 @@ # define MATHLIB_H "ltc/TpmToLtcMath.h" #elif MATH_LIB == MSBN #define MATHLIB_H "msbn/TpmToMsBnMath.h" +#elif MATH_LIB == WOLF +# define MATHLIB_H "wolf/TpmToWolfMath.h" #else # error "No math library selected" #endif diff --git a/TPMCmd/tpm/include/VendorString.h b/TPMCmd/tpm/include/VendorString.h index a7740f95..da2c6486 100644 --- a/TPMCmd/tpm/include/VendorString.h +++ b/TPMCmd/tpm/include/VendorString.h @@ -40,7 +40,7 @@ // for TPM_PT_MANUFACTURER in TPM2_GetCapability. // The following line should be un-commented and a vendor specific string // should be provided here. -//#define MANUFACTURER "MSFT" +#define MANUFACTURER "MSFT" // The following #if macro may be deleted after a proper MANUFACTURER is provided. #ifndef MANUFACTURER @@ -57,8 +57,8 @@ manufacturer name. // The following line should be un-commented and a vendor specific string // // A vendor-specific string should be provided here. -//#define VENDOR_STRING_1 "DPA " -//#define VENDOR_STRING_2 "fTPM" +#define VENDOR_STRING_1 "DPA " +#define VENDOR_STRING_2 "fTPM" // The vendor strings 2-4 may also be defined as needed. //#define VENDOR_STRING_3 @@ -75,11 +75,11 @@ string. // A vendor-specific FIRMWARE_V1 is required here. It is // the more significant 32-bits of a vendor-specific value // indicating the version of the firmware -//#define FIRMWARE_V1 (0x20170615) +#define FIRMWARE_V1 (0x20170615) // A vendor-specific FIRMWARE_V2 may be provided here. If present, it is the less // significant 32-bits of the version of the firmware. -//#define FIRMWARE_V2 (0x00000000) +#define FIRMWARE_V2 (0x00000000) // The following macro is just to insure that a FIRMWARE_V1 value is provided. #ifndef FIRMWARE_V1 diff --git a/TPMCmd/tpm/include/wolf/TpmToWolfHash.h b/TPMCmd/tpm/include/wolf/TpmToWolfHash.h new file mode 100644 index 00000000..21bb3358 --- /dev/null +++ b/TPMCmd/tpm/include/wolf/TpmToWolfHash.h @@ -0,0 +1,186 @@ +/* Microsoft Reference Implementation for TPM 2.0 + * + * The copyright in this software is being made available under the BSD License, + * included below. This software may be subject to other third party and + * contributor rights, including patent rights, and no such rights are granted + * under this license. + * + * Copyright (c) Microsoft Corporation + * + * All rights reserved. + * + * BSD License + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this list + * of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS"" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +//** Introduction +// +// This header file is used to 'splice' the OpenSSL hash code into the TPM code. +// +#ifndef _TPM_TO_WOLF_HASH_H_ +#define _TPM_TO_WOLF_HASH_H_ + +#if HASH_LIB == WOLF + +#define WOLFSSL_USER_SETTINGS +#include +#include +#include + + +//*************************************************************** +//** Links to the OpenSSL HASH code +//*************************************************************** + +// Redefine the internal name used for each of the hash state structures to the +// name used by the library. +// These defines need to be known in all parts of the TPM so that the structure +// sizes can be properly computed when needed. + +#define tpmHashStateSHA1_t wc_Sha +#define tpmHashStateSHA256_t wc_Sha256 +#define tpmHashStateSHA384_t wc_Sha512 +#define tpmHashStateSHA512_t wc_Sha512 + +#ifdef TPM_ALG_SM3 +# error "The version of WolfCrypt used by this code does not support SM3" +#endif + +// The defines below are only needed when compiling CryptHash.c or CryptSmac.c. +// This isolation is primarily to avoid name space collision. However, if there +// is a real collision, it will likely show up when the linker tries to put things +// together. + +#ifdef _CRYPT_HASH_C_ + +typedef BYTE *PBYTE; +typedef const BYTE *PCBYTE; + +// Define the interface between CryptHash.c to the functions provided by the +// library. For each method, define the calling parameters of the method and then +// define how the method is invoked in CryptHash.c. +// +// All hashes are required to have the same calling sequence. If they don't, create +// a simple adaptation function that converts from the "standard" form of the call +// to the form used by the specific hash (and then send a nasty letter to the +// person who wrote the hash function for the library). +// +// The macro that calls the method also defines how the +// parameters get swizzled between the default form (in CryptHash.c)and the +// library form. +// +// Initialize the hash context +#define HASH_START_METHOD_DEF void (HASH_START_METHOD)(PANY_HASH_STATE state) +#define HASH_START(hashState) \ + ((hashState)->def->method.start)(&(hashState)->state); + +// Add data to the hash +#define HASH_DATA_METHOD_DEF \ + void (HASH_DATA_METHOD)(PANY_HASH_STATE state, \ + PCBYTE buffer, \ + size_t size) +#define HASH_DATA(hashState, dInSize, dIn) \ + ((hashState)->def->method.data)(&(hashState)->state, dIn, dInSize) + +// Finalize the hash and get the digest +#define HASH_END_METHOD_DEF \ + void (HASH_END_METHOD)(PANY_HASH_STATE state, BYTE *buffer) +#define HASH_END(hashState, buffer) \ + ((hashState)->def->method.end)(&(hashState)->state, buffer) + +// Copy the hash context +// Note: For import, export, and copy, memcpy() is used since there is no +// reformatting necessary between the internal and external forms. +#define HASH_STATE_COPY_METHOD_DEF \ + void (HASH_STATE_COPY_METHOD)(PANY_HASH_STATE to, \ + PCANY_HASH_STATE from, \ + size_t size) +#define HASH_STATE_COPY(hashStateOut, hashStateIn) \ + ((hashStateIn)->def->method.copy)(&(hashStateOut)->state, \ + &(hashStateIn)->state, \ + (hashStateIn)->def->contextSize) + +// Copy (with reformatting when necessary) an internal hash structure to an +// external blob +#define HASH_STATE_EXPORT_METHOD_DEF \ + void (HASH_STATE_EXPORT_METHOD)(BYTE *to, \ + PCANY_HASH_STATE from, \ + size_t size) +#define HASH_STATE_EXPORT(to, hashStateFrom) \ + ((hashStateFrom)->def->method.copyOut) \ + (&(((BYTE *)(to))[offsetof(HASH_STATE, state)]), \ + &(hashStateFrom)->state, \ + (hashStateFrom)->def->contextSize) + +// Copy from an external blob to an internal formate (with reformatting when +// necessary +#define HASH_STATE_IMPORT_METHOD_DEF \ + void (HASH_STATE_IMPORT_METHOD)(PANY_HASH_STATE to, \ + const BYTE *from, \ + size_t size) +#define HASH_STATE_IMPORT(hashStateTo, from) \ + ((hashStateTo)->def->method.copyIn) \ + (&(hashStateTo)->state, \ + &(((const BYTE *)(from))[offsetof(HASH_STATE, state)]),\ + (hashStateTo)->def->contextSize) + + +// Function aliases. The code in CryptHash.c uses the internal designation for the +// functions. These need to be translated to the function names of the library. +// Internal External +// Designation Designation +#define tpmHashStart_SHA1 wc_InitSha // external name of the + // initialization method +#define tpmHashData_SHA1 wc_ShaUpdate +#define tpmHashEnd_SHA1 wc_ShaFinal +#define tpmHashStateCopy_SHA1 memcpy +#define tpmHashStateExport_SHA1 memcpy +#define tpmHashStateImport_SHA1 memcpy +#define tpmHashStart_SHA256 wc_InitSha256 +#define tpmHashData_SHA256 wc_Sha256Update +#define tpmHashEnd_SHA256 wc_Sha256Final +#define tpmHashStateCopy_SHA256 memcpy +#define tpmHashStateExport_SHA256 memcpy +#define tpmHashStateImport_SHA256 memcpy +#define tpmHashStart_SHA384 wc_InitSha384 +#define tpmHashData_SHA384 wc_Sha384Update +#define tpmHashEnd_SHA384 wc_Sha384Final +#define tpmHashStateCopy_SHA384 memcpy +#define tpmHashStateExport_SHA384 memcpy +#define tpmHashStateImport_SHA384 memcpy +#define tpmHashStart_SHA512 wc_InitSha512 +#define tpmHashData_SHA512 wc_Sha512Update +#define tpmHashEnd_SHA512 wc_Sha512Final +#define tpmHashStateCopy_SHA512 memcpy +#define tpmHashStateExport_SHA512 memcpy +#define tpmHashStateImport_SHA512 memcpy + +#endif // _CRYPT_HASH_C_ + +#define LibHashInit() +// This definition would change if there were something to report +#define HashLibSimulationEnd() + +#endif // HASH_LIB == WOLF + +#endif // diff --git a/TPMCmd/tpm/include/wolf/TpmToWolfMath.h b/TPMCmd/tpm/include/wolf/TpmToWolfMath.h new file mode 100644 index 00000000..4f2ce24a --- /dev/null +++ b/TPMCmd/tpm/include/wolf/TpmToWolfMath.h @@ -0,0 +1,114 @@ +/* Microsoft Reference Implementation for TPM 2.0 + * + * The copyright in this software is being made available under the BSD License, + * included below. This software may be subject to other third party and + * contributor rights, including patent rights, and no such rights are granted + * under this license. + * + * Copyright (c) Microsoft Corporation + * + * All rights reserved. + * + * BSD License + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this list + * of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS"" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +//** Introduction +// This file contains the structure definitions used for ECC in the LibTopCrypt +// version of the code. These definitions would change, based on the library. +// The ECC-related structures that cross the TPM interface are defined +// in TpmTypes.h +// + +#ifndef _TPM_TO_WOLF_MATH_H +#define _TPM_TO_WOLF_MATH_H + +#if MATH_LIB == WOLF + +#include +#include +#if OPENSSL_VERSION_NUMBER >= 0x10100000L +#include +#endif +#include + +//** Macros and Defines + +// Make sure that the library is using the correct size for a crypt word +#if defined THIRTY_TWO_BIT && (RADIX_BITS != 32) \ + || defined SIXTY_FOUR_BIT && (RADIX_BITS != 64) +# error "Ossl library is using different radix" +#endif + +// Allocate a local BIGNUM value. For the allocation, a bigNum structure is created +// as is a local BIGNUM. The bigNum is initialized and then the BIGNUM is +// set to reference the local value. +#define BIG_VAR(name, bits) \ + BN_VAR(name##Bn, (bits)); \ + BIGNUM _##name; \ + BIGNUM *name = BigInitialized(&_##name, \ + BnInit(name##Bn, \ + BYTES_TO_CRYPT_WORDS(sizeof(_##name##Bn.d)))) + +// Allocate a BIGNUM and initialize with the values in a bigNum initializer +#define BIG_INITIALIZED(name, initializer) \ + BIGNUM _##name; \ + BIGNUM *name = BigInitialized(&_##name, initializer) + + +typedef struct +{ + const ECC_CURVE_DATA *C; // the TPM curve values + EC_GROUP *G; // group parameters + BN_CTX *CTX; // the context for the math (this might not be + // the context in which the curve was created>; +} OSSL_CURVE_DATA; + +typedef OSSL_CURVE_DATA *bigCurve; + +#define AccessCurveData(E) ((E)->C) + +#define CURVE_INITIALIZED(name, initializer) \ + OSSL_CURVE_DATA _##name; \ + bigCurve name = BnCurveInitialize(&_##name, initializer) + +#include "TpmToOsslSupport_fp.h" + +#define CURVE_FREE(E) \ + if(E != NULL) \ + { \ + if(E->G != NULL) \ + EC_GROUP_free(E->G); \ + OsslContextLeave(E->CTX); \ + } + +#define OSSL_ENTER() BN_CTX *CTX = OsslContextEnter() + +#define OSSL_LEAVE() OsslContextLeave(CTX) + +// This definition would change if there were something to report +#define MathLibSimulationEnd() + +#endif // MATH_LIB == WOLF + +#endif diff --git a/TPMCmd/tpm/include/wolf/TpmToWolfSym.h b/TPMCmd/tpm/include/wolf/TpmToWolfSym.h new file mode 100644 index 00000000..7695a2da --- /dev/null +++ b/TPMCmd/tpm/include/wolf/TpmToWolfSym.h @@ -0,0 +1,123 @@ +/* Microsoft Reference Implementation for TPM 2.0 + * + * The copyright in this software is being made available under the BSD License, + * included below. This software may be subject to other third party and + * contributor rights, including patent rights, and no such rights are granted + * under this license. + * + * Copyright (c) Microsoft Corporation + * + * All rights reserved. + * + * BSD License + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this list + * of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS"" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +//** Introduction +// +// This header file is used to 'splice' the OpenSSL library into the TPM code. +// +// The support required of a library are a hash module, a block cipher module and +// portions of a big number library. + +#ifndef _TPM_TO_WOLF_SYM_H_ +#define _TPM_TO_WOLF_SYM_H_ + +#if SYM_LIB == WOLF + +#include +#include +#include +#include + +//*************************************************************** +//** Links to the OpenSSL AES code +//*************************************************************** + +#ifdef TPM_ALG_SM4 +#error "SM4 is not available" +#endif + +#ifdef TPM_ALG_CAMELLIA +#error "Camellia is not available" +#endif + +// Define the order of parameters to the library functions that do block encryption +// and decryption. +typedef void(*TpmCryptSetSymKeyCall_t)( + const BYTE *in, + BYTE *out, + void *keySchedule + ); + +// The Crypt functions that call the block encryption function use the parameters +// in the order: +// 1) keySchedule +// 2) in buffer +// 3) out buffer +// Since open SSL uses the order in encryptoCall_t above, need to swizzle the +// values to the order required by the library. +#define SWIZZLE(keySchedule, in, out) \ + (const BYTE *)(in), (BYTE *)(out), (void *)(keySchedule) + +// Macros to set up the encryption/decryption key schedules +// +// AES: +#define TpmCryptSetEncryptKeyAES(key, keySizeInBits, schedule) \ + AES_set_encrypt_key((key), (keySizeInBits), (tpmKeyScheduleAES *)(schedule)) +#define TpmCryptSetDecryptKeyAES(key, keySizeInBits, schedule) \ + AES_set_decrypt_key((key), (keySizeInBits), (tpmKeyScheduleAES *)(schedule)) + +// TDES: +#define TpmCryptSetEncryptKeyTDES(key, keySizeInBits, schedule) \ + TDES_set_encrypt_key((key), (keySizeInBits), (tpmKeyScheduleTDES *)(schedule)) +#define TpmCryptSetDecryptKeyTDES(key, keySizeInBits, schedule) \ + TDES_set_encrypt_key((key), (keySizeInBits), (tpmKeyScheduleTDES *)(schedule)) + +// Macros to alias encryption calls to specific algorithms. This should be used +// sparingly. Currently, only used by CryptRand.c +// +// When using these calls, to call the AES block encryption code, the caller +// should use: +// TpmCryptEncryptAES(SWIZZLE(keySchedule, in, out)); +#define TpmCryptEncryptAES AES_encrypt +#define TpmCryptDecryptAES AES_decrypt +#define tpmKeyScheduleAES AES_KEY + + +#define TpmCryptEncryptTDES TDES_encrypt +#define TpmCryptDecryptTDES TDES_decrypt +#define tpmKeyScheduleTDES DES_key_schedule + +typedef union tpmCryptKeySchedule_t tpmCryptKeySchedule_t; + +#ifdef TPM_ALG_TDES +#include "TpmToOsslDesSupport_fp.h" +#endif + +// This definition would change if there were something to report +#define SymLibSimulationEnd() + +#endif // SYM_LIB == WOLF + +#endif // _TPM_TO_WOLF_SYM_H_ diff --git a/TPMCmd/tpm/include/wolf/user_settings.h b/TPMCmd/tpm/include/wolf/user_settings.h new file mode 100644 index 00000000..5cf6e988 --- /dev/null +++ b/TPMCmd/tpm/include/wolf/user_settings.h @@ -0,0 +1,42 @@ +/* settings.h + * + * Copyright (C) 2006-2017 wolfSSL Inc. + * + * This file is part of wolfSSL. + * + * wolfSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + + +/* Place OS specific preprocessor flags, defines, includes here, will be + included into every file because types.h includes it */ + + +#ifndef WOLF_CRYPT_USER_SETTINGS_H +#define WOLF_CRYPT_USER_SETTINGS_H + +#ifdef __cplusplus + extern "C" { +#endif + +#define NO_OLD_WC_NAMES +#define WOLFSSL_SHA384 +#define WOLFSSL_SHA512 + +#ifdef __cplusplus + } /* extern "C" */ +#endif + +#endif diff --git a/TPMCmd/tpm/src/crypt/wolf/TpmToWolfDesSupport.c b/TPMCmd/tpm/src/crypt/wolf/TpmToWolfDesSupport.c new file mode 100644 index 00000000..b2611712 --- /dev/null +++ b/TPMCmd/tpm/src/crypt/wolf/TpmToWolfDesSupport.c @@ -0,0 +1,101 @@ +/* Microsoft Reference Implementation for TPM 2.0 + * + * The copyright in this software is being made available under the BSD License, + * included below. This software may be subject to other third party and + * contributor rights, including patent rights, and no such rights are granted + * under this license. + * + * Copyright (c) Microsoft Corporation + * + * All rights reserved. + * + * BSD License + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this list + * of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS"" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +//** Introduction +// +// The functions in this file are used for initialization of the interface to the +// OpenSSL library. + +//** Defines and Includes + +#include "Tpm.h" + +#if SYM_LIB == WOLF && defined TPM_ALG_TDES + +//**Functions +//*** TDES_set_encyrpt_key() +// This function makes creation of a TDES key look like the creation of a key for +// any of the other OpenSSL block ciphers. It will create three key schedules, +// one for each of the DES keys. If there are only two keys, then the third schedule +// is a copy of the first. +void +TDES_set_encrypt_key( + const BYTE *key, + UINT16 keySizeInBits, + tpmKeyScheduleTDES *keySchedule + ) +{ + DES_set_key_unchecked((const_DES_cblock *)key, &keySchedule[0]); + DES_set_key_unchecked((const_DES_cblock *)&key[8], &keySchedule[1]); + // If is two-key, copy the schedule for K1 into K3, otherwise, compute the + // the schedule for K3 + if(keySizeInBits == 128) + keySchedule[2] = keySchedule[0]; + else + DES_set_key_unchecked((const_DES_cblock *)&key[16], + &keySchedule[2]); +} + + +//*** TDES_encyrpt() +// The TPM code uses one key schedule. For TDES, the schedule contains three +// schedules. OpenSSL wants the schedules referenced separately. This function +// does that. +void TDES_encrypt( + const BYTE *in, + BYTE *out, + tpmKeyScheduleTDES *ks + ) +{ + DES_ecb3_encrypt((const_DES_cblock *)in, (DES_cblock *)out, + &ks[0], &ks[1], &ks[2], + DES_ENCRYPT); +} + +//*** TDES_decrypt() +// As with TDES_encypt() this function bridges between the TPM single schedule +// model and the OpenSSL three schedule model. +void TDES_decrypt( + const BYTE *in, + BYTE *out, + tpmKeyScheduleTDES *ks + ) +{ + DES_ecb3_encrypt((const_DES_cblock *)in, (DES_cblock *)out, + &ks[0], &ks[1], &ks[2], + DES_DECRYPT); +} + +#endif // SYM_LIB == WOLF diff --git a/TPMCmd/tpm/src/crypt/wolf/TpmToWolfMath.c b/TPMCmd/tpm/src/crypt/wolf/TpmToWolfMath.c new file mode 100644 index 00000000..8cbea963 --- /dev/null +++ b/TPMCmd/tpm/src/crypt/wolf/TpmToWolfMath.c @@ -0,0 +1,569 @@ +/* Microsoft Reference Implementation for TPM 2.0 + * + * The copyright in this software is being made available under the BSD License, + * included below. This software may be subject to other third party and + * contributor rights, including patent rights, and no such rights are granted + * under this license. + * + * Copyright (c) Microsoft Corporation + * + * All rights reserved. + * + * BSD License + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this list + * of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS"" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +//** Introduction +// +// This file contains the math functions that are not implemented in the BnMath +// library (yet). These math functions will call the OpenSSL library to execute +// the operations. There is a difference between the internal format and the +// OpenSSL format. To call the OpenSSL function, a BIGNUM structure is created +// for each passed variable. The sizes in the bignum_t are copied and the 'd' +// pointer in the BIGNUM is set to point to the 'd' parameter of the bignum_t. +// On return, SetSizeOsslToTpm is used for each returned variable to make sure that +// the pointers are not changed. The size of the returned BIGGNUM is copied to +// bignum_t. + +//** Includes and Defines +#include "Tpm.h" + +#if MATH_LIB == WOLF + +#include "TpmToOsslMath_fp.h" + +//** Functions + +//*** OsslToTpmBn() +// This function converts an OpenSSL BIGNUM to a TPM bignum. In this implementation +// it is assumed that OpenSSL used the same format for a big number as does the +// TPM -- an array of native-endian words in little-endian order. +// +// If the array allocated for the OpenSSL BIGNUM is not the space within the TPM +// bignum, then the data is copied. Otherwise, just the size field of the BIGNUM +// is copied. +void +OsslToTpmBn( + bigNum bn, + BIGNUM *osslBn + ) +{ + if(bn != NULL) + { + if((crypt_uword_t *)osslBn->d != bn->d) + { + int i; + pAssert((unsigned)osslBn->top <= BnGetAllocated(bn)); + for(i = 0; i < osslBn->top; i++) + bn->d[i] = osslBn->d[i]; + } + BnSetTop(bn, osslBn->top); + } +} + +//*** BigInitialized() +// This function initializes an OSSL BIGNUM from a TPM bignum. +BIGNUM * +BigInitialized( + BIGNUM *toInit, + bigConst initializer + ) +{ + if(toInit == NULL || initializer == NULL) + return NULL; + toInit->d = (BN_ULONG *)&initializer->d[0]; + toInit->dmax = initializer->allocated; + toInit->top = initializer->size; + toInit->neg = 0; + toInit->flags = 0; + return toInit; +} + +#ifndef OSSL_DEBUG +# define BIGNUM_PRINT(label, bn, eol) +# define DEBUG_PRINT(x) +#else +# define DEBUG_PRINT(x) printf("%s", x) +# define BIGNUM_PRINT(label, bn, eol) BIGNUM_print((label), (bn), (eol)) +static +void BIGNUM_print( + const char *label, + const BIGNUM *a, + BOOL eol + ) +{ + BN_ULONG *d; + int i; + int notZero = FALSE; + + if(label != NULL) + printf("%s", label); + if(a == NULL) + { + printf("NULL"); + goto done; + } + if (a->neg) + printf("-"); + for(i = a->top, d = &a->d[i - 1]; i > 0; i--) + { + int j; + BN_ULONG l = *d--; + for(j = BN_BITS2 - 8; j >= 0; j -= 8) + { + BYTE b = (BYTE)((l >> j) & 0xFF); + notZero = notZero || (b != 0); + if(notZero) + printf("%02x", b); + } + if(!notZero) + printf("0"); + } +done: + if(eol) + printf("\n"); + return; +} +#endif + +#ifdef LIBRARY_COMPATIBILITY_CHECK +void +MathLibraryCompatibilityCheck( + void + ) +{ + OSSL_ENTER(); + BIGNUM *osslTemp = BN_CTX_get(CTX); + BN_VAR(tpmTemp, 64 * 8); // allocate some space for a test value + crypt_uword_t i; + TPM2B_TYPE(TEST, 16); + TPM2B_TEST test = {{16, {0x0F, 0x0E, 0x0D, 0x0C, + 0x0B, 0x0A, 0x09, 0x08, + 0x07, 0x06, 0x05, 0x04, + 0x03, 0x02, 0x01, 0x00}}}; + // Convert the test TPM2B to a bigNum + BnFrom2B(tpmTemp, &test.b); + // Convert the test TPM2B to an OpenSSL BIGNUM + BN_bin2bn(test.t.buffer, test.t.size, osslTemp); + // Make sure the values are consistent + cAssert(osslTemp->top == (int)tpmTemp->size); + for(i = 0; i < tpmTemp->size; i++) + cAssert(osslTemp->d[0] == tpmTemp->d[0]); + OSSL_LEAVE(); +} +#endif + +//*** BnModMult() +// Does multiply and divide returning the remainder of the divide. +LIB_EXPORT BOOL +BnModMult( + bigNum result, + bigConst op1, + bigConst op2, + bigConst modulus + ) +{ + OSSL_ENTER(); + BIG_INITIALIZED(bnResult, result); + BIG_INITIALIZED(bnOp1, op1); + BIG_INITIALIZED(bnOp2, op2); + BIG_INITIALIZED(bnMod, modulus); + BIG_VAR(bnTemp, (LARGEST_NUMBER_BITS * 4)); + BOOL OK; + pAssert(BnGetAllocated(result) >= BnGetSize(modulus)); + OK = BN_mul(bnTemp, bnOp1, bnOp2, CTX); + OK = OK && BN_div(NULL, bnResult, bnTemp, bnMod, CTX); + if(OK) + { + result->size = bnResult->top; + OsslToTpmBn(result, bnResult); + } + OSSL_LEAVE(); + return OK; +} + +//*** BnMult() +// Multiplies two numbers +LIB_EXPORT BOOL +BnMult( + bigNum result, + bigConst multiplicand, + bigConst multiplier + ) +{ + OSSL_ENTER(); + BN_VAR(temp, (LARGEST_NUMBER_BITS * 2)); + BIG_INITIALIZED(bnTemp, temp); + BIG_INITIALIZED(bnA, multiplicand); + BIG_INITIALIZED(bnB, multiplier); + BOOL OK; + pAssert(result->allocated >= + (BITS_TO_CRYPT_WORDS(BnSizeInBits(multiplicand) + + BnSizeInBits(multiplier)))); + OK = BN_mul(bnTemp, bnA, bnB, CTX); + if(OK) + { + OsslToTpmBn(temp, bnTemp); + BnCopy(result, temp); + } + OSSL_LEAVE(); + return OK; +} + +//*** BnDiv() +// This function divides two bigNum values. The function returns FALSE if +// there is an error in the operation. +LIB_EXPORT BOOL +BnDiv( + bigNum quotient, + bigNum remainder, + bigConst dividend, + bigConst divisor + ) +{ + OSSL_ENTER(); + BIG_INITIALIZED(bnQ, quotient); + BIG_INITIALIZED(bnR, remainder); + BIG_INITIALIZED(bnDend, dividend); + BIG_INITIALIZED(bnSor, divisor); + BOOL OK; + pAssert(!BnEqualZero(divisor)); + if(BnGetSize(dividend) < BnGetSize(divisor)) + { + if(quotient) + BnSetWord(quotient, 0); + if(remainder) + BnCopy(remainder, dividend); + OK = TRUE; + } + else + { + pAssert((quotient == NULL) + || (quotient->allocated >= (unsigned)(dividend->size + - divisor->size))); + pAssert((remainder == NULL) + || (remainder->allocated >= divisor->size)); + OK = BN_div(bnQ, bnR, bnDend, bnSor, CTX); + if(OK) + { + OsslToTpmBn(quotient, bnQ); + OsslToTpmBn(remainder, bnR); + } + } + DEBUG_PRINT("In BnDiv:\n"); + BIGNUM_PRINT(" bnDividend: ", bnDend, TRUE); + BIGNUM_PRINT(" bnDivisor: ", bnSor, TRUE); + BIGNUM_PRINT(" bnQuotient: ", bnQ, TRUE); + BIGNUM_PRINT(" bnRemainder: ", bnR, TRUE); + OSSL_LEAVE(); + return OK; +} + +#ifdef TPM_ALG_RSA +//*** BnGcd() +// Get the greatest common divisor of two numbers +LIB_EXPORT BOOL +BnGcd( + bigNum gcd, // OUT: the common divisor + bigConst number1, // IN: + bigConst number2 // IN: + ) +{ + OSSL_ENTER(); + BIG_INITIALIZED(bnGcd, gcd); + BIG_INITIALIZED(bn1, number1); + BIG_INITIALIZED(bn2, number2); + BOOL OK; + pAssert(gcd != NULL); + OK = BN_gcd(bnGcd, bn1, bn2, CTX); + if(OK) + { + OsslToTpmBn(gcd, bnGcd); + gcd->size = bnGcd->top; + } + OSSL_LEAVE(); + return OK; +} + +//***BnModExp() +// Do modular exponentiation using bigNum values. The conversion from a bignum_t to +// a bigNum is trivial as they are based on the same structure +LIB_EXPORT BOOL +BnModExp( + bigNum result, // OUT: the result + bigConst number, // IN: number to exponentiate + bigConst exponent, // IN: + bigConst modulus // IN: + ) +{ + OSSL_ENTER(); + BIG_INITIALIZED(bnResult, result); + BIG_INITIALIZED(bnN, number); + BIG_INITIALIZED(bnE, exponent); + BIG_INITIALIZED(bnM, modulus); + BOOL OK; +// + OK = BN_mod_exp(bnResult, bnN, bnE, bnM, CTX); + if(OK) + { + OsslToTpmBn(result, bnResult); + } + OSSL_LEAVE(); + return OK; +} + +//*** BnModInverse() +// Modular multiplicative inverse +LIB_EXPORT BOOL +BnModInverse( + bigNum result, + bigConst number, + bigConst modulus + ) +{ + OSSL_ENTER(); + BIG_INITIALIZED(bnResult, result); + BIG_INITIALIZED(bnN, number); + BIG_INITIALIZED(bnM, modulus); + BOOL OK; + + OK = (BN_mod_inverse(bnResult, bnN, bnM, CTX) != NULL); + if(OK) + { + OsslToTpmBn(result, bnResult); + } + OSSL_LEAVE(); + return OK; +} +#endif // TPM_ALG_RSA + +#ifdef TPM_ALG_ECC + +//*** PointFromOssl() +// Function to copy the point result from an OSSL function to a bigNum +static BOOL +PointFromOssl( + bigPoint pOut, // OUT: resulting point + EC_POINT *pIn, // IN: the point to return + bigCurve E // IN: the curve + ) +{ + BIGNUM *x = NULL; + BIGNUM *y = NULL; + BOOL OK; + BN_CTX_start(E->CTX); +// + x = BN_CTX_get(E->CTX); + y = BN_CTX_get(E->CTX); + + if(y == NULL) + FAIL(FATAL_ERROR_ALLOCATION); + // If this returns false, then the point is at infinity + OK = EC_POINT_get_affine_coordinates_GFp(E->G, pIn, x, y, E->CTX); + if(OK) + { + OsslToTpmBn(pOut->x, x); + OsslToTpmBn(pOut->y, y); + BnSetWord(pOut->z, 1); + } + else + BnSetWord(pOut->z, 0); + BN_CTX_end(E->CTX); + return OK; +} + +//*** EcPointInitialized() +// Allocate and initialize a point. +static EC_POINT * +EcPointInitialized( + pointConst initializer, + bigCurve E + ) +{ + BIG_INITIALIZED(bnX, (initializer != NULL) ? initializer->x : NULL); + BIG_INITIALIZED(bnY, (initializer != NULL) ? initializer->y : NULL); + + EC_POINT *P = (initializer != NULL && E != NULL) + ? EC_POINT_new(E->G) : NULL; + pAssert(E != NULL); + if(P != NULL) + EC_POINT_set_affine_coordinates_GFp(E->G, P, bnX, bnY, E->CTX); + return P; +} + +//*** BnCurveInitialize() +// This function initializes the OpenSSL group definition +// +// It is a fatal error if 'groupContext' is not provided. +// return type: bigCurve * +// NULL the TPM_ECC_CURVE is not valid +// non-NULL points to a structure in 'groupContext' +bigCurve +BnCurveInitialize( + bigCurve E, // IN: curve structure to initialize + TPM_ECC_CURVE curveId // IN: curve identifier + ) +{ + EC_GROUP *group = NULL; + EC_POINT *P = NULL; + const ECC_CURVE_DATA *C = GetCurveData(curveId); + BN_CTX *CTX = NULL; + BIG_INITIALIZED(bnP, C != NULL ? C->prime : NULL); + BIG_INITIALIZED(bnA, C != NULL ? C->a : NULL); + BIG_INITIALIZED(bnB, C != NULL ? C->b : NULL); + BIG_INITIALIZED(bnX, C != NULL ? C->base.x : NULL); + BIG_INITIALIZED(bnY, C != NULL ? C->base.y : NULL); + BIG_INITIALIZED(bnN, C != NULL ? C->order : NULL); + BIG_INITIALIZED(bnH, C != NULL ? C->h : NULL); + int OK = (C != NULL); +// + OK = OK && ((CTX = OsslContextEnter()) != NULL); + + // initialize EC group, associate a generator point and initialize the point + // from the parameter data + // Create a group structure + OK = OK && (group = EC_GROUP_new_curve_GFp(bnP, bnA, bnB, CTX)) != NULL; + + // Allocate a point in the group that will be used in setting the + // generator. This is not needed after the generator is set. + OK = OK && ((P = EC_POINT_new(group)) != NULL); + // Need to use this in case Montgomery method is being used + OK = OK + && EC_POINT_set_affine_coordinates_GFp(group, P, bnX, bnY, CTX); + // Now set the generator + OK = OK && EC_GROUP_set_generator(group, P, bnN, bnH); + + if(P != NULL) + EC_POINT_free(P); + + if(!OK && group != NULL) + { + EC_GROUP_free(group); + group = NULL; + } + if(!OK && CTX != NULL) + { + OsslContextLeave(CTX); + CTX = NULL; + } + + E->G = group; + E->CTX = CTX; + E->C = C; + + return OK ? E : NULL; +} + +//*** BnEccModMult() +// This function does a point multiply of the form R = [d]S +// return type: BOOL +// FALSE failure in operation; treat as result being point at infinity +LIB_EXPORT BOOL +BnEccModMult( + bigPoint R, // OUT: computed point + pointConst S, // IN: point to multiply by 'd' (optional) + bigConst d, // IN: scalar for [d]S + bigCurve E + ) +{ + EC_POINT *pR = EC_POINT_new(E->G); + EC_POINT *pS = EcPointInitialized(S, E); + BIG_INITIALIZED(bnD, d); + + if(S == NULL) + EC_POINT_mul(E->G, pR, bnD, NULL, NULL, E->CTX); + else + EC_POINT_mul(E->G, pR, NULL, pS, bnD, E->CTX); + PointFromOssl(R, pR, E); + EC_POINT_free(pR); + EC_POINT_free(pS); + return !BnEqualZero(R->z); +} + +//*** BnEccModMult2() +// This function does a point multiply of the form R = [d]G + [u]Q +// return type: BOOL +// FALSE failure in operation; treat as result being point at infinity +LIB_EXPORT BOOL +BnEccModMult2( + bigPoint R, // OUT: computed point + pointConst S, // IN: optional point + bigConst d, // IN: scalar for [d]S or [d]G + pointConst Q, // IN: second point + bigConst u, // IN: second scalar + bigCurve E // IN: curve + ) +{ + EC_POINT *pR = EC_POINT_new(E->G); + EC_POINT *pS = EcPointInitialized(S, E); + BIG_INITIALIZED(bnD, d); + EC_POINT *pQ = EcPointInitialized(Q, E); + BIG_INITIALIZED(bnU, u); + + if(S == NULL || S == (pointConst)&E->C->base) + EC_POINT_mul(E->G, pR, bnD, pQ, bnU, E->CTX); + else + { + const EC_POINT *points[2]; + const BIGNUM *scalars[2]; + points[0] = pS; + points[1] = pQ; + scalars[0] = bnD; + scalars[1] = bnU; + EC_POINTs_mul(E->G, pR, NULL, 2, points, scalars, E->CTX); + } + PointFromOssl(R, pR, E); + EC_POINT_free(pR); + EC_POINT_free(pS); + EC_POINT_free(pQ); + return !BnEqualZero(R->z); +} + +//** BnEccAdd() +// This function does addition of two points. +// return type: BOOL +// FALSE failure in operation; treat as result being point at infinity +LIB_EXPORT BOOL +BnEccAdd( + bigPoint R, // OUT: computed point + pointConst S, // IN: point to multiply by 'd' + pointConst Q, // IN: second point + bigCurve E // IN: curve + ) +{ + EC_POINT *pR = EC_POINT_new(E->G); + EC_POINT *pS = EcPointInitialized(S, E); + EC_POINT *pQ = EcPointInitialized(Q, E); +// + EC_POINT_add(E->G, pR, pS, pQ, E->CTX); + + PointFromOssl(R, pR, E); + EC_POINT_free(pR); + EC_POINT_free(pS); + EC_POINT_free(pQ); + return !BnEqualZero(R->z); +} + +#endif // TPM_ALG_ECC + +#endif // MATHLIB WOLF \ No newline at end of file diff --git a/TPMCmd/tpm/src/crypt/wolf/TpmToWolfSupport.c b/TPMCmd/tpm/src/crypt/wolf/TpmToWolfSupport.c new file mode 100644 index 00000000..542058bd --- /dev/null +++ b/TPMCmd/tpm/src/crypt/wolf/TpmToWolfSupport.c @@ -0,0 +1,92 @@ +/* Microsoft Reference Implementation for TPM 2.0 + * + * The copyright in this software is being made available under the BSD License, + * included below. This software may be subject to other third party and + * contributor rights, including patent rights, and no such rights are granted + * under this license. + * + * Copyright (c) Microsoft Corporation + * + * All rights reserved. + * + * BSD License + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this list + * of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS"" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +//** Introduction +// +// The functions in this file are used for initialization of the interface to the +// OpenSSL library. + +//** Defines and Includes + +#include "Tpm.h" + +#if MATH_LIB == WOLF + +// Used to pass the pointers to the correct sub-keys +typedef const BYTE *desKeyPointers[3]; + +//*** SupportLibInit() +// This does any initialization required by the support library. +LIB_EXPORT int +SupportLibInit( + void + ) +{ +#ifdef LIBRARY_COMPATIBILITY_CHECK + MathLibraryCompatibilityCheck(); +#endif + return TRUE; +} + +//*** OsslContextEnter() +// This function is used to initialize an OpenSSL context at the start of a function +// that will call to an OpenSSL math function. +BN_CTX * +OsslContextEnter( + void + ) +{ + BN_CTX *context = BN_CTX_new(); + if(context == NULL) + FAIL(FATAL_ERROR_ALLOCATION); + BN_CTX_start(context); + return context; +} + +//*** OsslContextLeave() +// This is the companion function to OsslContextEnter(). +void +OsslContextLeave( + BN_CTX *context + ) +{ + if(context != NULL) + { + BN_CTX_end(context); + BN_CTX_free(context); + } +} +#endif // MATH_LIB == WOLF + diff --git a/external/wolfssl b/external/wolfssl new file mode 160000 index 00000000..82850422 --- /dev/null +++ b/external/wolfssl @@ -0,0 +1 @@ +Subproject commit 82850422fc77dc49a2a423fdcbf0c3559b27add0