From fccc05845bfbdd328cdbe0fb491422fa139549f3 Mon Sep 17 00:00:00 2001 From: Stefan Berger Date: Wed, 30 Jul 2025 09:48:10 -0400 Subject: [PATCH] tpm2: Add support for CRYPTO_LIB_REPORTING but do not use it Signed-off-by: Stefan Berger --- src/Makefile.am | 1 + src/tpm2/BnSupport_Interface.h | 9 +++ src/tpm2/CryptoInterface.h | 66 ++++++++++++++++++++++ src/tpm2/TpmBuildSwitches.h | 4 ++ src/tpm2/crypto/openssl/BnOssl.h | 12 +++- src/tpm2/crypto/openssl/BnToOsslMath.c | 11 ++++ src/tpm2/crypto/openssl/TpmToOsslSupport.c | 52 +++++++++++++++++ 7 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 src/tpm2/CryptoInterface.h diff --git a/src/Makefile.am b/src/Makefile.am index 94fe6588..c271c287 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -365,6 +365,7 @@ noinst_HEADERS += \ tpm2/Create_fp.h \ tpm2/CreateLoaded_fp.h \ tpm2/CreatePrimary_fp.h \ + tpm2/CryptoInterface.h \ tpm2/CryptSelfTest_fp.h \ tpm2/DA_fp.h \ tpm2/DebugHelpers_fp.h \ diff --git a/src/tpm2/BnSupport_Interface.h b/src/tpm2/BnSupport_Interface.h index 7ad401ea..5928a02f 100644 --- a/src/tpm2/BnSupport_Interface.h +++ b/src/tpm2/BnSupport_Interface.h @@ -79,6 +79,7 @@ #define BN_SUPPORT_INTERFACE_H // TODO_RENAME_INC_FOLDER:private refers to the TPM_CoreLib private headers #include "GpMacros.h" +#include "CryptoInterface.h" #include "BnValues.h" //** BnSupportLibInit() @@ -167,6 +168,14 @@ LIB_EXPORT BOOL BnEccAdd( #endif // ALG_ECC +#if CRYPTO_LIB_REPORTING + +//** BnGetImplementation() +// This function reports the underlying library being used for bignum operations. +void BnGetImplementation(_CRYPTO_IMPL_DESCRIPTION* result); + +#endif // CRYPTO_LIB_REPORTING + // libtpms: added begin bigCurveData* BnCurveInitialize( diff --git a/src/tpm2/CryptoInterface.h b/src/tpm2/CryptoInterface.h new file mode 100644 index 00000000..0550c747 --- /dev/null +++ b/src/tpm2/CryptoInterface.h @@ -0,0 +1,66 @@ +/********************************************************************************/ +/* */ +/* CryptoInterface header file */ +/* Written by Stefan Berger */ +/* IBM Thomas J. Watson Research Center */ +/* */ +/* (c) Copyright IBM Corporation 2017,2018. */ +/* */ +/* All rights reserved. */ +/* */ +/* 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. */ +/* */ +/* Neither the names of the IBM Corporation nor the names of its */ +/* contributors may be used to endorse or promote products derived from */ +/* this software without specific prior written permission. */ +/* */ +/* 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 prototypes that are common to all TPM crypto interfaces. +// +#ifndef CRYPTO_INTERFACE_H +#define CRYPTO_INTERFACE_H + +#include "TpmBuildSwitches.h" + +#if SIMULATION && CRYPTO_LIB_REPORTING + +typedef struct crypto_impl_description +{ + // The name of the crypto library, ASCII encoded. + char name[32]; + // The version of the crypto library, ASCII encoded. + char version[32]; +} _CRYPTO_IMPL_DESCRIPTION; + +// When building the simulator, the plugged-in crypto libraries can report its +// version information by implementing these interfaces. +void _crypto_GetSymImpl(_CRYPTO_IMPL_DESCRIPTION* result); +void _crypto_GetHashImpl(_CRYPTO_IMPL_DESCRIPTION* result); +void _crypto_GetMathImpl(_CRYPTO_IMPL_DESCRIPTION* result); + +#endif // SIMULATION && CRYPTO_LIB_REPORTING + +#endif // CRYPTO_INTERFACE_H diff --git a/src/tpm2/TpmBuildSwitches.h b/src/tpm2/TpmBuildSwitches.h index 49f7630e..033383fe 100644 --- a/src/tpm2/TpmBuildSwitches.h +++ b/src/tpm2/TpmBuildSwitches.h @@ -110,6 +110,10 @@ // ones in the Simulator project. #define SIMULATION NO // libtpms: changed to NO +// The CRYPTO_LIB_REPORTING switch allows the TPM to report its +// crypto library implementation, e.g., at simulation startup. +#define CRYPTO_LIB_REPORTING NO // libtpms: NO + // If doing debug, can set the DRBG to print out the intermediate test values. // Before enabling this, make sure that the dbgDumpMemBlock() function // has been added someplace (preferably, somewhere in CryptRand.c) diff --git a/src/tpm2/crypto/openssl/BnOssl.h b/src/tpm2/crypto/openssl/BnOssl.h index 3073e622..004d9f49 100644 --- a/src/tpm2/crypto/openssl/BnOssl.h +++ b/src/tpm2/crypto/openssl/BnOssl.h @@ -73,4 +73,14 @@ #include "BnMemory_fp.h" #include "BnMath_fp.h" #include "BnConvert_fp.h" -#endif + +#if CRYPTO_LIB_REPORTING +# include + +//*** OsslGetVersion() +// Report the current version of OpenSSL. +void OsslGetVersion(_CRYPTO_IMPL_DESCRIPTION* result); + +#endif // CRYPTO_LIB_REPORTING + +#endif // _BNOSSL_H_ diff --git a/src/tpm2/crypto/openssl/BnToOsslMath.c b/src/tpm2/crypto/openssl/BnToOsslMath.c index 0c64cd8e..290f9fa3 100644 --- a/src/tpm2/crypto/openssl/BnToOsslMath.c +++ b/src/tpm2/crypto/openssl/BnToOsslMath.c @@ -680,4 +680,15 @@ LIB_EXPORT BOOL BnEccAdd(bigPoint R, // OUT: computed point # endif // ALG_ECC +# if CRYPTO_LIB_REPORTING + +//** BnGetImplementation() +// This function reports the underlying library being used for bignum operations. +void BnGetImplementation(_CRYPTO_IMPL_DESCRIPTION* result) +{ + OsslGetVersion(result); +} + +# endif // CRYPTO_LIB_REPORTING + #endif // MATHLIB OSSL diff --git a/src/tpm2/crypto/openssl/TpmToOsslSupport.c b/src/tpm2/crypto/openssl/TpmToOsslSupport.c index 1dfd64f6..b2b421d1 100644 --- a/src/tpm2/crypto/openssl/TpmToOsslSupport.c +++ b/src/tpm2/crypto/openssl/TpmToOsslSupport.c @@ -66,6 +66,34 @@ //** Defines and Includes #include "BnOssl.h" +#include "CryptoInterface.h" +#include "TpmToOsslSym.h" +#include "TpmToOsslHash.h" +#include +#include + +#if CRYPTO_LIB_REPORTING + +//*** OsslGetVersion() +// Report the version of OpenSSL. +void OsslGetVersion(_CRYPTO_IMPL_DESCRIPTION* result) +{ + snprintf(result->name, sizeof(result->name), "OpenSSL"); +# if defined(OPENSSL_VERSION_STR) + snprintf(result->version, sizeof(result->version), "%s", OPENSSL_VERSION_STR); +# else + // decode the hex version string according to the rules described in opensslv.h + snprintf(result->version, + sizeof(result->version), + "%d.%d.%d%c", + (unsigned char)((OPENSSL_VERSION_NUMBER >> 28) & 0x0f), + (unsigned char)((OPENSSL_VERSION_NUMBER >> 20) & 0xff), + (unsigned char)((OPENSSL_VERSION_NUMBER >> 12) & 0xff), + (char)((OPENSSL_VERSION_NUMBER >> 4) & 0xff) - 1 + 'a'); +# endif //OPENSSL_VERSION_STR +} + +#endif //CRYPTO_LIB_REPORTING #if defined(HASH_LIB_OSSL) || defined(MATH_LIB_OSSL) || defined(SYM_LIB_OSSL) // Used to pass the pointers to the correct sub-keys @@ -117,4 +145,28 @@ void OsslPopContext(BN_CTX* CTX) BN_CTX_end(CTX); } +# if CRYPTO_LIB_REPORTING + +# if defined(SYM_LIB_OSSL) && SIMULATION && CRYPTO_LIB_REPORTING +//*** _crypto_GetSymImpl() +// Report the version of OpenSSL being used for symmetric crypto. +void _crypto_GetSymImpl(_CRYPTO_IMPL_DESCRIPTION* result) +{ + OsslGetVersion(result); +} +# else +# error huh? +# endif // defined(SYM_LIB_OSSL) && SIMULATION + +# if defined(HASH_LIB_OSSL) && SIMULATION && CRYPTO_LIB_REPORTING +//*** _crypto_GetHashImpl() +// Report the version of OpenSSL being used for hashing. +void _crypto_GetHashImpl(_CRYPTO_IMPL_DESCRIPTION* result) +{ + OsslGetVersion(result); +} +# endif // defined(HASH_LIB_OSSL) && SIMULATION + +# endif // CRYPTO_LIB_REPORTING + #endif // HASH_LIB_OSSL || MATH_LIB_OSSL || SYM_LIB_OSSL