From fc5ff50a799c8ac5154303748694e5c471ae4449 Mon Sep 17 00:00:00 2001 From: David Garske Date: Tue, 30 Apr 2024 13:55:44 -0700 Subject: [PATCH] Added TPM simulator support for UART transport (`--enable-swtpm=uart`). Refactor debug printf to new macro to support Xilinx FSBL. Author: Jacob Barthelmeh --- configure.ac | 20 +- src/tpm2.c | 122 +++++++------ src/tpm2_swtpm.c | 427 +++++++++++++++++++++++++++++++++++++------ src/tpm2_wrap.c | 347 +++++++++++++++++------------------ wolftpm/tpm2.h | 10 + wolftpm/tpm2_types.h | 10 +- 6 files changed, 647 insertions(+), 289 deletions(-) diff --git a/configure.ac b/configure.ac index 72e410bf..6f69d48c 100644 --- a/configure.ac +++ b/configure.ac @@ -230,7 +230,7 @@ AC_ARG_ENABLE([swtpm], [ ENABLED_SWTPM=no ] ) -if test "x$ENABLED_SWTPM" = "xyes" +if test "x$ENABLED_SWTPM" != "xno" then if test "x$ENABLED_DEVTPM" = "xyes" then @@ -238,6 +238,16 @@ then fi AM_CFLAGS="$AM_CFLAGS -DWOLFTPM_SWTPM" + + if test "x$ENABLED_SWTPM" = "xuart" + then + AM_CFLAGS="$AM_CFLAGS -DWOLFTPM_SWTPM_UART" + fi + + if test "x$ENABLED_SWTPM" = "xuartns550" + then + AM_CFLAGS="$AM_CFLAGS -DWOLFTPM_SWTPM_UARTNS550" + fi fi # Windows TBS device Support @@ -253,7 +263,7 @@ AC_ARG_ENABLE([winapi], if test "x$ENABLED_WINAPI" = "xyes" || test "x$ENABLED_WINTBS" = "xyes" then - if test "x$ENABLED_DEVTPM" = "xyes" -o "x$ENABLED_SWTPM" = "xyes" + if test "x$ENABLED_DEVTPM" = "xyes" -o "x$ENABLED_SWTPM" != "xno" then AC_MSG_ERROR([Cannot enable swtpm or devtpm with windows API]) fi @@ -392,7 +402,7 @@ AC_ARG_ENABLE([hal], [ ENABLED_EXAMPLE_HAL=$enableval ], [ ENABLED_EXAMPLE_HAL=yes ] ) -if test "x$ENABLED_EXAMPLE_HAL" = "xyes" +if test "x$ENABLED_EXAMPLE_HAL" = "xyes" || test "x$ENABLED_MMIO" = "xyes" then AM_CFLAGS="$AM_CFLAGS -DWOLFTPM_EXAMPLE_HAL" fi @@ -437,13 +447,13 @@ AM_CONDITIONAL([BUILD_ST], [test "x$ENABLED_ST" = "xyes"]) AM_CONDITIONAL([BUILD_MICROCHIP], [test "x$ENABLED_MICROCHIP" = "xyes"]) AM_CONDITIONAL([BUILD_INFINEON], [test "x$ENABLED_INFINEON" != "xno"]) AM_CONDITIONAL([BUILD_DEVTPM], [test "x$ENABLED_DEVTPM" = "xyes"]) -AM_CONDITIONAL([BUILD_SWTPM], [test "x$ENABLED_SWTPM" = "xyes"]) +AM_CONDITIONAL([BUILD_SWTPM], [test "x$ENABLED_SWTPM" != "xno"]) AM_CONDITIONAL([BUILD_WINAPI], [test "x$ENABLED_WINAPI" = "xyes"]) AM_CONDITIONAL([BUILD_NUVOTON], [test "x$ENABLED_NUVOTON" = "xyes"]) AM_CONDITIONAL([BUILD_CHECKWAITSTATE], [test "x$ENABLED_CHECKWAITSTATE" = "xyes"]) AM_CONDITIONAL([BUILD_AUTODETECT], [test "x$ENABLED_AUTODETECT" = "xyes"]) AM_CONDITIONAL([BUILD_FIRMWARE], [test "x$ENABLED_FIRMWARE" = "xyes"]) -AM_CONDITIONAL([BUILD_HAL], [test "x$ENABLED_EXAMPLE_HAL" = "xyes" || test "x$ENABLED_MMIO" = "xyes"]) +AM_CONDITIONAL([BUILD_HAL], [test "x$ENABLED_EXAMPLE_HAL" = "xyes"]) CREATE_HEX_VERSION diff --git a/src/tpm2.c b/src/tpm2.c index d8f81004..89ea3950 100644 --- a/src/tpm2.c +++ b/src/tpm2.c @@ -37,7 +37,13 @@ /* --- Local Variables -- */ /******************************************************************************/ -static THREAD_LS_T TPM2_CTX* gActiveTPM; +#if defined(WOLFTPM_SWTPM_UARTNS550) + /* having this as static and as thread local storage causes run time issues + * with Xilinx FSBL use */ + TPM2_CTX* gActiveTPM; +#else + static THREAD_LS_T TPM2_CTX* gActiveTPM; +#endif #ifndef WOLFTPM2_NO_WOLFCRYPT static volatile int gWolfCryptRefCount = 0; #endif @@ -69,7 +75,7 @@ static TPM_RC TPM2_AcquireLock(TPM2_CTX* ctx) if (!ctx->hwLockInit) { if (wc_InitMutex(&ctx->hwLock) != 0) { #ifdef DEBUG_WOLFTPM - printf("TPM Mutex Init failed\n"); + DEBUG_PRINTF("TPM Mutex Init failed\n"); #endif return TPM_RC_FAILURE; } @@ -145,7 +151,7 @@ static int TPM2_CommandProcess(TPM2_CTX* ctx, TPM2_Packet* packet, } #ifdef WOLFTPM_DEBUG_VERBOSE - printf("CommandProcess: Handles (Auth %d, In %d), CmdSz %d, AuthSz %d, ParamSz %d, EncSz %d\n", + DEBUG_PRINTF("CommandProcess: Handles (Auth %d, In %d), CmdSz %d, AuthSz %d, ParamSz %d, EncSz %d\n", info->authCnt, info->inHandleCnt, (int)cmdSz, (int)authSz, paramSz, encParamSz); #else (void)paramSz; @@ -200,7 +206,7 @@ static int TPM2_CommandProcess(TPM2_CTX* ctx, TPM2_Packet* packet, rc = TPM2_ParamEnc_CmdRequest(session, encParam, encParamSz); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("Command parameter encryption failed\n"); + DEBUG_PRINTF("Command parameter encryption failed\n"); #endif return rc; } @@ -212,7 +218,7 @@ static int TPM2_CommandProcess(TPM2_CTX* ctx, TPM2_Packet* packet, rc |= TPM2_GetName(ctx, handleValue3, info->inHandleCnt, 2, &name3); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("Error getting names for cpHash!\n"); + DEBUG_PRINTF("Error getting names for cpHash!\n"); #endif return BAD_FUNC_ARG; } @@ -222,7 +228,7 @@ static int TPM2_CommandProcess(TPM2_CTX* ctx, TPM2_Packet* packet, &name2, &name3, param, paramSz, &hash); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("Error calculating cpHash!\n"); + DEBUG_PRINTF("Error calculating cpHash!\n"); #endif return rc; } @@ -233,7 +239,7 @@ static int TPM2_CommandProcess(TPM2_CTX* ctx, TPM2_Packet* packet, authCmd.sessionAttributes, &authCmd.hmac); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("Error calculating command HMAC!\n"); + DEBUG_PRINTF("Error calculating command HMAC!\n"); #endif return rc; } @@ -284,7 +290,7 @@ static int TPM2_ResponseProcess(TPM2_CTX* ctx, TPM2_Packet* packet, } #ifdef WOLFTPM_DEBUG_VERBOSE - printf("ResponseProcess: Handles (Out %d), RespSz %d, ParamSz %d, DecSz %d, AuthSz %d\n", + DEBUG_PRINTF("ResponseProcess: Handles (Out %d), RespSz %d, ParamSz %d, DecSz %d, AuthSz %d\n", info->outHandleCnt, (int)respSz, (int)paramSz, (int)decParamSz, (int)(respSz - authPos)); #endif @@ -318,7 +324,7 @@ static int TPM2_ResponseProcess(TPM2_CTX* ctx, TPM2_Packet* packet, &hash); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("Error calculating rpHash!\n"); + DEBUG_PRINTF("Error calculating rpHash!\n"); #endif return rc; } @@ -329,7 +335,7 @@ static int TPM2_ResponseProcess(TPM2_CTX* ctx, TPM2_Packet* packet, authRsp.sessionAttributes, &hmac); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("Error calculating response HMAC!\n"); + DEBUG_PRINTF("Error calculating response HMAC!\n"); #endif return rc; } @@ -338,7 +344,7 @@ static int TPM2_ResponseProcess(TPM2_CTX* ctx, TPM2_Packet* packet, if (hmac.size != authRsp.hmac.size || XMEMCMP(hmac.buffer, authRsp.hmac.buffer, hmac.size) != 0) { #ifdef DEBUG_WOLFTPM - printf("Response HMAC verification failed!\n"); + DEBUG_PRINTF("Response HMAC verification failed!\n"); #endif return TPM_RC_HMAC; } @@ -354,7 +360,7 @@ static int TPM2_ResponseProcess(TPM2_CTX* ctx, TPM2_Packet* packet, rc = TPM2_ParamDec_CmdResponse(session, decParam, decParamSz); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("Response parameter decryption failed\n"); + DEBUG_PRINTF("Response parameter decryption failed\n"); #endif return rc; } @@ -394,7 +400,7 @@ static TPM_RC TPM2_SendCommandAuth(TPM2_CTX* ctx, TPM2_Packet* packet, return TPM_RC_AUTH_MISSING; #ifdef WOLFTPM_DEBUG_VERBOSE - printf("Found %d auth sessions\n", info->authCnt); + DEBUG_PRINTF("Found %d auth sessions\n", info->authCnt); #endif rc = TPM2_CommandProcess(ctx, packet, info, cmdCode, cmdSz); @@ -448,13 +454,13 @@ static TPM_RC TPM2_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet) #ifdef HAVE_FIPS static void WolfFipsCb(int ok, int err, const char* hash) { - printf("in my Fips callback, ok = %d, err = %d\n", ok, err); - printf("message = %s\n", wc_GetErrorString(err)); - printf("hash = %s\n", hash); + DEBUG_PRINTF("in my Fips callback, ok = %d, err = %d\n", ok, err); + DEBUG_PRINTF("message = %s\n", wc_GetErrorString(err)); + DEBUG_PRINTF("hash = %s\n", hash); if (err == IN_CORE_FIPS_E) { - printf("In core integrity hash check failure, copy above hash\n"); - printf("into verifyCore[] in fips_test.c and rebuild\n"); + DEBUG_PRINTF("In core integrity hash check failure, copy above hash\n"); + DEBUG_PRINTF("into verifyCore[] in fips_test.c and rebuild\n"); } } #endif @@ -593,7 +599,7 @@ TPM_RC TPM2_Init_ex(TPM2_CTX* ctx, TPM2HalIoCb ioCb, void* userCtx, return rc; #endif -#if defined(WOLFTPM_SWTPM) +#if defined(WOLFTPM_SWTPM) && !defined(WOLFTPM_SWTPM_UARTNS550) ctx->tcpCtx.fd = -1; #endif @@ -886,7 +892,7 @@ TPM_RC TPM2_GetCapability(GetCapability_In* in, GetCapability_Out* out) } default: #ifdef DEBUG_WOLFTPM - printf("Unknown capability type 0x%x\n", + DEBUG_PRINTF("Unknown capability type 0x%x\n", (unsigned int)out->capabilityData.capability); #endif break; @@ -5534,7 +5540,7 @@ int TPM2_GetNonce(byte* nonceBuf, int nonceSz) } #ifdef WOLFTPM_DEBUG_VERBOSE - printf("TPM2_GetNonce (%d bytes)\n", nonceSz); + DEBUG_PRINTF("TPM2_GetNonce (%d bytes)\n", nonceSz); #endif #ifdef WOLFTPM2_USE_WOLF_RNG @@ -5559,7 +5565,7 @@ int TPM2_GetNonce(byte* nonceBuf, int nonceSz) TPM2_Packet_Finalize(&packet, TPM_ST_NO_SESSIONS, TPM_CC_GetRandom); rc = TPM2_SendCommand(ctx, &packet); #ifdef WOLFTPM_DEBUG_VERBOSE - printf("TPM2_GetNonce (%d bytes at %d): %d (%s)\n", + DEBUG_PRINTF("TPM2_GetNonce (%d bytes at %d): %d (%s)\n", inSz, randSz, rc, TPM2_GetRCString(rc)); #endif if (rc != TPM_RC_SUCCESS) { @@ -5569,7 +5575,7 @@ int TPM2_GetNonce(byte* nonceBuf, int nonceSz) TPM2_Packet_ParseU16(&packet, &outSz); if (outSz > MAX_RNG_REQ_SIZE) { #ifdef DEBUG_WOLFTPM - printf("TPM2_GetNonce out size error\n"); + DEBUG_PRINTF("TPM2_GetNonce out size error\n"); #endif rc = BAD_FUNC_ARG; break; @@ -5614,7 +5620,7 @@ int TPM2_GetName(TPM2_CTX* ctx, UINT32 handleValue, int handleCnt, int idx, TPM2 } #ifdef WOLFTPM_DEBUG_VERBOSE - printf("Name %d: %d\n", idx, name->size); + DEBUG_PRINTF("Name %d: %d\n", idx, name->size); TPM2_PrintBin(name->name, name->size); #endif return TPM_RC_SUCCESS; @@ -5666,6 +5672,7 @@ void TPM2_SetupPCRSelArray(TPML_PCR_SELECTION* pcr, TPM_ALG_ID alg, const char* TPM2_GetRCString(int rc) { +#if !defined(NO_ERROR_STRINGS) /* for negative return codes use wolfCrypt */ if (rc < 0) { switch (rc) { @@ -5673,7 +5680,7 @@ const char* TPM2_GetRCString(int rc) default: break; } - #ifndef WOLFTPM2_NO_WOLFCRYPT + #if !defined(WOLFTPM2_NO_WOLFCRYPT) #if !defined(WOLFCRYPT_ONLY) && \ (!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)) /* include TLS error codes */ @@ -5786,7 +5793,7 @@ const char* TPM2_GetRCString(int rc) /* bits 8-11 */ int param_num = (rc & 0xF00) >> 8; #ifdef DEBUG_WOLFTPM - printf("Error: Parameter Number %d\n", param_num); + DEBUG_PRINTF("Error: Parameter Number %d\n", param_num); #endif (void)param_num; } @@ -5794,7 +5801,7 @@ const char* TPM2_GetRCString(int rc) /* bits 8-10 */ int session_num = (rc & 0x700) >> 8; #ifdef DEBUG_WOLFTPM - printf("Error: Session Number %d\n", session_num); + DEBUG_PRINTF("Error: Session Number %d\n", session_num); #endif (void)session_num; } @@ -5802,7 +5809,7 @@ const char* TPM2_GetRCString(int rc) /* bits 8-10 */ int handle_num = (rc & 0x700) >> 8; #ifdef DEBUG_WOLFTPM - printf("Error: Handle Number %d\n", handle_num); + DEBUG_PRINTF("Error: Handle Number %d\n", handle_num); #endif (void)handle_num; } @@ -5854,6 +5861,9 @@ const char* TPM2_GetRCString(int rc) } return "Unknown"; +#else + return "Error strings not compiled in"; +#endif } const char* TPM2_GetAlgName(TPM_ALG_ID alg) @@ -6023,7 +6033,7 @@ int TPM2_GetWolfRng(WC_RNG** rng) rc = wc_InitRng_ex(&ctx->rng, NULL, ctx->did_vid); if (rc < 0) { #ifdef DEBUG_WOLFTPM - printf("wc_InitRng_ex failed %d: %s\n", + DEBUG_PRINTF("wc_InitRng_ex failed %d: %s\n", (int)rc, wc_GetErrorString(rc)); #endif return rc; @@ -6140,7 +6150,7 @@ int TPM2_AppendPublic(byte* buf, word32 size, int* sizeUsed, TPM2B_PUBLIC* pub) if (size < sizeof(TPM2B_PUBLIC)) { #ifdef DEBUG_WOLFTPM - printf("Insufficient buffer size for TPM2B_PUBLIC operations\n"); + DEBUG_PRINTF("Insufficient buffer size for TPM2B_PUBLIC operations\n"); #endif return TPM_RC_FAILURE; } @@ -6165,7 +6175,7 @@ int TPM2_ParsePublic(TPM2B_PUBLIC* pub, byte* buf, word32 size, int* sizeUsed) if (size < sizeof(TPM2B_PUBLIC)) { #ifdef DEBUG_WOLFTPM - printf("Insufficient buffer size for TPM2B_PUBLIC operations\n"); + DEBUG_PRINTF("Insufficient buffer size for TPM2B_PUBLIC operations\n"); #endif return TPM_RC_FAILURE; } @@ -6196,7 +6206,7 @@ void TPM2_PrintBin(const byte* buffer, word32 length) word32 i, sz; if (!buffer) { - printf("\tNULL\n"); + DEBUG_PRINTF("\tNULL\n"); return; } @@ -6205,21 +6215,21 @@ void TPM2_PrintBin(const byte* buffer, word32 length) if (sz > LINE_LEN) sz = LINE_LEN; - printf("\t"); + DEBUG_PRINTF("\t"); for (i = 0; i < LINE_LEN; i++) { if (i < length) - printf("%02x ", buffer[i]); + DEBUG_PRINTF("%02x ", buffer[i]); else - printf(" "); + DEBUG_PRINTF(" "); } - printf("| "); + DEBUG_PRINTF("| "); for (i = 0; i < sz; i++) { if (buffer[i] > 31 && buffer[i] < 127) - printf("%c", buffer[i]); + DEBUG_PRINTF("%c", buffer[i]); else - printf("."); + DEBUG_PRINTF("."); } - printf("\r\n"); + DEBUG_PRINTF("\r\n"); buffer += sz; length -= sz; @@ -6231,25 +6241,25 @@ void TPM2_PrintAuth(const TPMS_AUTH_COMMAND* authCmd) if (authCmd == NULL) return; - printf("authCmd:\n"); - printf("sessionHandle=0x%08X\n", (unsigned int)authCmd->sessionHandle); - printf("nonceSize=%u nonceBuffer:\n", authCmd->nonce.size); + DEBUG_PRINTF("authCmd:\n"); + DEBUG_PRINTF("sessionHandle=0x%08X\n", (unsigned int)authCmd->sessionHandle); + DEBUG_PRINTF("nonceSize=%u nonceBuffer:\n", authCmd->nonce.size); TPM2_PrintBin(authCmd->nonce.buffer, authCmd->nonce.size); - printf("sessionAttributes=0x%02X\n", authCmd->sessionAttributes); - printf("hmacSize=%u hmacBuffer:\n", authCmd->hmac.size); + DEBUG_PRINTF("sessionAttributes=0x%02X\n", authCmd->sessionAttributes); + DEBUG_PRINTF("hmacSize=%u hmacBuffer:\n", authCmd->hmac.size); TPM2_PrintBin(authCmd->hmac.buffer, authCmd->hmac.size); } void TPM2_PrintPublicArea(const TPM2B_PUBLIC* pub) { - printf("Public Area (size %d):\n", pub->size); + DEBUG_PRINTF("Public Area (size %d):\n", pub->size); /* Sanity check */ if (pub->size > (sizeof(TPM2B_PUBLIC))) { - printf("Invalid TPM2B_PUBLIC size\n"); + DEBUG_PRINTF("Invalid TPM2B_PUBLIC size\n"); return; } - printf(" Type: %s (0x%X), name: %s (0x%X), objAttr: 0x%X, authPolicy sz: %d\n", + DEBUG_PRINTF(" Type: %s (0x%X), name: %s (0x%X), objAttr: 0x%X, authPolicy sz: %d\n", TPM2_GetAlgName(pub->publicArea.type), pub->publicArea.type, TPM2_GetAlgName(pub->publicArea.nameAlg), pub->publicArea.nameAlg, (unsigned int)pub->publicArea.objectAttributes, @@ -6261,7 +6271,7 @@ void TPM2_PrintPublicArea(const TPM2B_PUBLIC* pub) /* parameters and unique field depend on algType */ switch (pub->publicArea.type) { case TPM_ALG_KEYEDHASH: - printf(" Keyed Hash: scheme: %s (0x%X), scheme hash: %s (0x%X), unique size %d\n", + DEBUG_PRINTF(" Keyed Hash: scheme: %s (0x%X), scheme hash: %s (0x%X), unique size %d\n", TPM2_GetAlgName(pub->publicArea.parameters.keyedHashDetail.scheme.scheme), pub->publicArea.parameters.keyedHashDetail.scheme.scheme, TPM2_GetAlgName(pub->publicArea.parameters.keyedHashDetail.scheme.details.hmac.hashAlg), @@ -6272,7 +6282,7 @@ void TPM2_PrintPublicArea(const TPM2B_PUBLIC* pub) #endif break; case TPM_ALG_SYMCIPHER: - printf(" Symmetric Cipher: algorithm: %s (0x%X), keyBits: %d, mode: %s (0x%X), unique size %d\n", + DEBUG_PRINTF(" Symmetric Cipher: algorithm: %s (0x%X), keyBits: %d, mode: %s (0x%X), unique size %d\n", TPM2_GetAlgName(pub->publicArea.parameters.symDetail.sym.algorithm), pub->publicArea.parameters.symDetail.sym.algorithm, pub->publicArea.parameters.symDetail.sym.keyBits.sym, @@ -6284,18 +6294,18 @@ void TPM2_PrintPublicArea(const TPM2B_PUBLIC* pub) #endif break; case TPM_ALG_RSA: - printf(" RSA: sym algorithm: %s (0x%X), sym keyBits: %d, sym mode: %s (0x%X)\n", + DEBUG_PRINTF(" RSA: sym algorithm: %s (0x%X), sym keyBits: %d, sym mode: %s (0x%X)\n", TPM2_GetAlgName(pub->publicArea.parameters.rsaDetail.symmetric.algorithm), pub->publicArea.parameters.rsaDetail.symmetric.algorithm, pub->publicArea.parameters.rsaDetail.symmetric.keyBits.sym, TPM2_GetAlgName(pub->publicArea.parameters.rsaDetail.symmetric.mode.sym), pub->publicArea.parameters.rsaDetail.symmetric.mode.sym); - printf(" scheme: %s (0x%X), scheme hash: %s (0x%X)\n", + DEBUG_PRINTF(" scheme: %s (0x%X), scheme hash: %s (0x%X)\n", TPM2_GetAlgName(pub->publicArea.parameters.rsaDetail.scheme.scheme), pub->publicArea.parameters.rsaDetail.scheme.scheme, TPM2_GetAlgName(pub->publicArea.parameters.rsaDetail.scheme.details.anySig.hashAlg), pub->publicArea.parameters.rsaDetail.scheme.details.anySig.hashAlg); - printf(" keyBits: %d, exponent: 0x%X, unique size %d\n", + DEBUG_PRINTF(" keyBits: %d, exponent: 0x%X, unique size %d\n", pub->publicArea.parameters.rsaDetail.keyBits, (unsigned int)pub->publicArea.parameters.rsaDetail.exponent, pub->publicArea.unique.rsa.size); @@ -6304,20 +6314,20 @@ void TPM2_PrintPublicArea(const TPM2B_PUBLIC* pub) #endif break; case TPM_ALG_ECC: - printf(" ECC: sym algorithm: %s (0x%X), sym keyBits: %d, sym mode: %s (0x%X)\n", + DEBUG_PRINTF(" ECC: sym algorithm: %s (0x%X), sym keyBits: %d, sym mode: %s (0x%X)\n", TPM2_GetAlgName(pub->publicArea.parameters.eccDetail.symmetric.algorithm), pub->publicArea.parameters.eccDetail.symmetric.algorithm, pub->publicArea.parameters.eccDetail.symmetric.keyBits.sym, TPM2_GetAlgName(pub->publicArea.parameters.eccDetail.symmetric.mode.sym), pub->publicArea.parameters.eccDetail.symmetric.mode.sym); - printf(" scheme: %s (0x%X), scheme hash: %s (0x%X), curveID: size %d, 0x%X\n", + DEBUG_PRINTF(" scheme: %s (0x%X), scheme hash: %s (0x%X), curveID: size %d, 0x%X\n", TPM2_GetAlgName(pub->publicArea.parameters.eccDetail.scheme.scheme), pub->publicArea.parameters.eccDetail.scheme.scheme, TPM2_GetAlgName(pub->publicArea.parameters.eccDetail.scheme.details.any.hashAlg), pub->publicArea.parameters.eccDetail.scheme.details.any.hashAlg, TPM2_GetCurveSize(pub->publicArea.parameters.eccDetail.curveID), pub->publicArea.parameters.eccDetail.curveID); - printf(" KDF scheme: %s (0x%X), KDF alg: %s (0x%X), unique X/Y size %d/%d\n", + DEBUG_PRINTF(" KDF scheme: %s (0x%X), KDF alg: %s (0x%X), unique X/Y size %d/%d\n", TPM2_GetAlgName(pub->publicArea.parameters.eccDetail.kdf.scheme), pub->publicArea.parameters.eccDetail.kdf.scheme, TPM2_GetAlgName(pub->publicArea.parameters.eccDetail.kdf.details.any.hashAlg), @@ -6331,7 +6341,7 @@ void TPM2_PrintPublicArea(const TPM2B_PUBLIC* pub) break; default: /* derive does not seem to have specific fields in the parameters struct */ - printf("Derive Type: unique label size %d, context size %d\n", + DEBUG_PRINTF("Derive Type: unique label size %d, context size %d\n", pub->publicArea.unique.derive.label.size, pub->publicArea.unique.derive.context.size); #ifdef WOLFTPM_DEBUG_VERBOSE diff --git a/src/tpm2_swtpm.c b/src/tpm2_swtpm.c index dbcc8b48..9e5a0f25 100644 --- a/src/tpm2_swtpm.c +++ b/src/tpm2_swtpm.c @@ -39,44 +39,126 @@ #include #ifdef WOLFTPM_SWTPM + #include #include #include #include #include -#include -#include +#include /* necessary for memset */ +#include /* standard in/out procedures */ +#include /* defines system calls */ + #ifdef HAVE_NETDB_H #include #endif -#include +#if defined(WOLFTPM_SWTPM_UART) + #define _XOPEN_SOURCE 600 + #include /* used for all socket calls */ + #include /* used for sockaddr_in6 */ + #include + #include + #include + #include + #include + #include + + #ifndef TPM2_SWTPM_HOST + #ifdef __MACH__ + #define TPM2_SWTPM_HOST "/dev/cu.usbserial-0001" + #else + #define TPM2_SWTPM_HOST "/dev/ttyS0" + #endif + #endif + #ifndef TPM2_SWTPM_PORT + #define TPM2_SWTPM_PORT 115200 + #endif + #ifndef TPM2_TIMEOUT_SECONDS + #define TPM2_TIMEOUT_SECONDS 7200 + #endif + #define WOLFTPM_WRITE write + #define WOLFTPM_READ read + #define WOLFTPM_CLOSE close +#elif defined(WOLFTPM_SWTPM_UARTNS550) + /* Xilinx 16550 UART */ + #ifndef TPM2_SWTPM_HOST + #define TPM2_SWTPM_HOST XPAR_MB0_AXI_UART16550_2_DEVICE_ID + #endif + #ifndef TPM2_SWTPM_PORT + #define TPM2_SWTPM_PORT 115200 + #endif + #ifndef TPM2_TIMEOUT_SECONDS + #define TPM2_TIMEOUT_SECONDS 21600 + #endif + #define WOLFTPM_WRITE(u, b, sz) XUartNs550_Send(&(u), (b), (sz)) + #define WOLFTPM_READ(u, b, sz) XUartNs550_Recv(&(u), (b), (sz)) + #define WOLFTPM_CLOSE(fd) 0 + +#else + #include + + #ifndef TPM2_SWTPM_HOST + #define TPM2_SWTPM_HOST "localhost" + #endif + #ifndef TPM2_SWTPM_PORT + #define TPM2_SWTPM_PORT "2321" + #endif + #ifndef TPM2_TIMEOUT_SECONDS + #define TPM2_TIMEOUT_SECONDS 10 + #endif + #define WOLFTPM_WRITE write + #define WOLFTPM_READ read + #define WOLFTPM_CLOSE close +#endif /* WOLFTPM_SWTPM_UART */ -#ifndef TPM2_SWTPM_HOST -#define TPM2_SWTPM_HOST "localhost" -#endif -#ifndef TPM2_SWTPM_PORT -#define TPM2_SWTPM_PORT "2321" -#endif static TPM_RC SwTpmTransmit(TPM2_CTX* ctx, const void* buffer, ssize_t bufSz) { TPM_RC rc = TPM_RC_SUCCESS; ssize_t wrc = 0; - if (ctx == NULL || ctx->tcpCtx.fd < 0 || buffer == NULL) { + if (ctx == NULL || buffer == NULL) { return BAD_FUNC_ARG; } - wrc = write(ctx->tcpCtx.fd, buffer, bufSz); - if (bufSz != wrc) { - rc = TPM_RC_FAILURE; +#if !defined(WOLFTPM_SWTPM_UARTNS550) + if (ctx->tcpCtx.fd < 0) { + return BAD_FUNC_ARG; + } +#endif + +#ifdef DEBUG_SWTPM_IO + DEBUG_PRINTF("Write %zd\n\r", bufSz); + TPM2_PrintBin(buffer, (word32)bufSz); +#endif + +#if defined(WOLFTPM_SWTPM_UARTNS550) + while (wrc < bufSz) +#endif + { + ssize_t tmp; + + tmp = WOLFTPM_WRITE(ctx->tcpCtx.fd, (unsigned char*)buffer + wrc, + bufSz - wrc); + if (tmp > 0) { + wrc += tmp; + } + if (tmp < 0) { + rc = TPM_RC_FAILURE; + } + +#if !defined(WOLFTPM_SWTPM_UARTNS550) + if (bufSz != wrc) { + rc = TPM_RC_FAILURE; + } +#endif } #ifdef WOLFTPM_DEBUG_VERBOSE if (wrc < 0) { - printf("Failed to send the TPM command to fd %d, got errno %d =" + DEBUG_PRINTF("Failed to send the TPM command to fd %d, got errno %d =" "%s\n", ctx->tcpCtx.fd, errno, strerror(errno)); } #endif @@ -84,46 +166,263 @@ static TPM_RC SwTpmTransmit(TPM2_CTX* ctx, const void* buffer, ssize_t bufSz) return rc; } -static TPM_RC SwTpmReceive(TPM2_CTX* ctx, void* buffer, size_t rxSz) +#if !defined(WOLFTPM_SWTPM_UARTNS550) +static int SwTpmReceive(TPM2_CTX* ctx, void* buffer, size_t rxSz) { - TPM_RC rc = TPM_RC_SUCCESS; - ssize_t wrc = 0; - size_t bytes_remaining = rxSz; + int rc; + size_t remain; char* ptr = (char*)buffer; + fd_set rfds; + struct timeval tv = { TPM2_TIMEOUT_SECONDS, 0}; if (ctx == NULL || ctx->tcpCtx.fd < 0 || buffer == NULL) { return BAD_FUNC_ARG; } - while (bytes_remaining > 0) { - wrc = read(ctx->tcpCtx.fd, ptr, bytes_remaining); - if (wrc <= 0) { - #ifdef DEBUG_WOLFTPM - if (wrc == 0) { - printf("Failed to read from TPM socket: EOF\n"); + FD_ZERO(&rfds); + FD_SET(ctx->tcpCtx.fd, &rfds); + remain = rxSz; + + do { + /* use select to wait for data */ + rc = select(ctx->tcpCtx.fd + 1, &rfds, NULL, NULL, &tv); + if (rc == 0) { + rc = TPM_RC_FAILURE; /* timeout */ + break; + } + rc = (int)WOLFTPM_READ(ctx->tcpCtx.fd, ptr, remain); +#ifdef DEBUG_SWTPM_IO + DEBUG_PRINTF("Read asked %zd, got %d\n\r", remain, rc); +#endif + + if (rc == 0) { + if (remain == 0) { + break; + } + continue; /* keep trying */ + } + + if (rc <= 0) { + #ifdef DEBUG_WOLFTPM + if (rc == 0) { + DEBUG_PRINTF("Failed to read from TPM socket: EOF\n"); } else { - printf("Failed to read from TPM socket %d, got errno %d" + DEBUG_PRINTF("Failed to read from TPM socket %d, got errno %d" " = %s\n", ctx->tcpCtx.fd, errno, strerror(errno)); } - #endif + #endif rc = TPM_RC_FAILURE; break; } - bytes_remaining -= wrc; - ptr += wrc; +#ifdef DEBUG_SWTPM_IO + TPM2_PrintBin((const byte*)ptr, rc); +#endif + remain -= rc; + ptr += rc; - #ifdef WOLFTPM_DEBUG_VERBOSE - printf("TPM socket received %zd waiting for %zu more\n", - wrc, bytes_remaining); + #ifdef WOLFTPM_DEBUG_VERBOSE + DEBUG_PRINTF("TPM socket received %d waiting for %zd more\n\r", + rc, remain); + #endif + } while (remain > 0); + + if (remain <= 0) { + rc = TPM_RC_SUCCESS; + } + + return rc; +} +#endif + +#ifdef WOLFTPM_SWTPM_UART +static int SwTpmConnect(TPM2_CTX* ctx, const char* uartDev, uint32_t baud) +{ + struct termios tty; + int fd; + + if (ctx == NULL) { + return BAD_FUNC_ARG; + } + + /* Open UART file descriptor */ + fd = open(uartDev, O_RDWR | O_NOCTTY); + if (fd < 0) { +#ifdef DEBUG_WOLFTPM + DEBUG_PRINTF("Error opening %s: Error %i (%s)\n", + uartDev, errno, strerror(errno)); +#endif + return TPM_RC_FAILURE; + } + tcgetattr(fd, &tty); + cfsetospeed(&tty, baud); + cfsetispeed(&tty, baud); + tty.c_cflag = (tty.c_cflag & ~CSIZE) | (CS8); + tty.c_iflag &= ~(IGNBRK | IXON | IXOFF | IXANY| INLCR | ICRNL); + tty.c_oflag &= ~OPOST; + tty.c_oflag &= ~(ONLCR|OCRNL); + tty.c_cflag &= ~(PARENB | PARODD | CSTOPB); + tty.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); + tty.c_iflag &= ~ISTRIP; + tty.c_cc[VMIN] = 0; + tty.c_cc[VTIME] = 5; + tcsetattr(fd, TCSANOW, &tty); + + /* Flush any data in the RX buffer */ + tcflush(fd, TCIOFLUSH); + + /* save file descriptor to context */ + ctx->tcpCtx.fd = fd; + + return TPM_RC_SUCCESS; +} + +#elif defined(WOLFTPM_SWTPM_UARTNS550) + +static unsigned char rxBuff[512]; +static int rxBuffIdx = 0; + +static int SwTpmReceive(TPM2_CTX* ctx, void* buffer, size_t rxSz) +{ + int rc; + size_t remain, rxRemain; + int sendAck = 0; + int timeOut = TPM2_TIMEOUT_SECONDS; + + if (ctx == NULL || buffer == NULL) { + return BAD_FUNC_ARG; + } + + remain = rxSz; + rxRemain = rxSz; + + /* use up any leftovers before trying to pull more */ + if (rxBuffIdx > 0) { + int minSz = (rxBuffIdx < (int)remain)? rxBuffIdx : (int)remain; + + memcpy(buffer, rxBuff, minSz); + if (rxBuffIdx > minSz) { + memmove(rxBuff, rxBuff + rxBuffIdx - minSz, rxBuffIdx - minSz); + } + rxBuffIdx -= minSz; + remain -= minSz; + rxRemain -= minSz; + } + + do { + rc = (int)WOLFTPM_READ(ctx->tcpCtx.fd, rxBuff + rxBuffIdx, + sizeof(rxBuff) - rxBuffIdx); + #ifdef DEBUG_SWTPM_IO + DEBUG_PRINTF("Read asked %d, got %d\n\r", remain, rc); + #endif + + /* send ack */ + if (rc > 0 ) { + usleep(500); + sendAck = 1; + timeOut = TPM2_TIMEOUT_SECONDS; /* reset timeout */ + } + + if (rc == 0) { + if (sendAck) { + unsigned char tmpBuf[1] = {0x01}; + + sendAck = 0; + WOLFTPM_WRITE(ctx->tcpCtx.fd, tmpBuf, 1); + } + + if (rxBuffIdx >= rxRemain || rxRemain == 0) { + break; + } + + if ((timeOut--) <= 0) { + rxBuffIdx = 0; /* reset read state */ + rc = TPM_RC_FAILURE; /* timeout */ + #if DEBUG_WOLFTPM + DEBUG_PRINTF("Connection timed out\r\n"); + #endif + break; + } + continue; /* keep trying */ + } + + if (rc <= 0) { + #ifdef DEBUG_WOLFTPM + DEBUG_PRINTF("Failed to read from TPM UART\n\r"); #endif + rc = TPM_RC_FAILURE; + break; + } + rxBuffIdx += rc; + remain -= rc; + + #ifdef WOLFTPM_DEBUG_VERBOSE + DEBUG_PRINTF("TPM socket received %d waiting for %d more\n\r", + rc, remain); + #endif + } while (1); + + if (remain <= 0) { + rc = TPM_RC_SUCCESS; + } + + if (rxBuffIdx > 0 && rxRemain > 0) { + int minSz = (rxRemain < rxBuffIdx)? rxRemain : rxBuffIdx; + memcpy(buffer, rxBuff, minSz); + if (rxBuffIdx > minSz) { + memmove(rxBuff, rxBuff + minSz, rxBuffIdx - minSz); + } + rxBuffIdx -= minSz; + rc = TPM_RC_SUCCESS; } return rc; } -static TPM_RC SwTpmConnect(TPM2_CTX* ctx, const char* host, const char* port) + +static int SwTpmConnect(TPM2_CTX* ctx, uint32_t baud) +{ + int ret = TPM_RC_SUCCESS; + XUartNs550_Config *config; + + config = XUartNs550_LookupConfig(TPM2_SWTPM_HOST); + if (config == NULL) { + ret = TPM_RC_FAILURE; + } + + if (ret == TPM_RC_SUCCESS) { + #ifdef DEBUG_SWTPM_IO + DEBUG_PRINTF("Connecting with UART base address = %X\n\r", + config->BaseAddress); + #endif + if (XUartNs550_CfgInitialize(&(ctx->tcpCtx.fd), config, + config->BaseAddress) != XST_SUCCESS) { + #ifdef DEBUG_SWTPM_IO + DEBUG_PRINTF("cfg initialize fail\n\r"); + #endif + ret = TPM_RC_FAILURE; + } + } + + if (ret == TPM_RC_SUCCESS) { + if (XUartNs550_SelfTest(&(ctx->tcpCtx.fd)) != XST_SUCCESS) { + #ifdef DEBUG_SWTPM_IO + DEBUG_PRINTF("UART tpm selftest failed\n\r"); + #endif + ret = TPM_RC_FAILURE; + } + } + + if (ret == TPM_RC_SUCCESS) { + XUartNs550_SetBaudRate( &(ctx->tcpCtx.fd), baud); + XUartNs550_SetFifoThreshold( &(ctx->tcpCtx.fd), XUN_FIFO_TRIGGER_01); + } + + return ret; +} +#else +static int SwTpmConnect(TPM2_CTX* ctx, const char* host, const char* port) { TPM_RC rc = TPM_RC_FAILURE; struct addrinfo hints; @@ -164,41 +463,50 @@ static TPM_RC SwTpmConnect(TPM2_CTX* ctx, const char* host, const char* port) } #ifdef DEBUG_WOLFTPM else { - printf("Failed to connect to %s %s\n", host, port); + DEBUG_PRINTF("Failed to connect to %s %s\n", host, port); } #endif return rc; } +#endif /* WOLFTPM_SWTPM_UART */ static TPM_RC SwTpmDisconnect(TPM2_CTX* ctx) { TPM_RC rc = TPM_RC_SUCCESS; uint32_t tss_cmd; - if (ctx == NULL || ctx->tcpCtx.fd < 0) { + if (ctx == NULL) { return BAD_FUNC_ARG; } - /* end swtpm session */ +#if !defined(WOLFTPM_SWTPM_UARTNS550) + if (ctx->tcpCtx.fd < 0) { + return BAD_FUNC_ARG; + } +#endif + + /* end software TPM session */ tss_cmd = TPM2_Packet_SwapU32(TPM_SESSION_END); rc = SwTpmTransmit(ctx, &tss_cmd, sizeof(uint32_t)); - #ifdef WOLFTPM_DEBUG_VERBOSE +#ifdef WOLFTPM_DEBUG_VERBOSE if (rc != TPM_RC_SUCCESS) { - printf("Failed to transmit SESSION_END\n"); + DEBUG_PRINTF("Failed to transmit SESSION_END\n"); } - #endif +#endif - if (0 != close(ctx->tcpCtx.fd)) { + if (WOLFTPM_CLOSE(ctx->tcpCtx.fd) != 0) { rc = TPM_RC_FAILURE; - #ifdef WOLFTPM_DEBUG_VERBOSE - printf("Failed to close fd %d, got errno %d =" + #ifdef WOLFTPM_DEBUG_VERBOSE + DEBUG_PRINTF("Failed to close fd %d, got errno %d =" "%s\n", ctx->tcpCtx.fd, errno, strerror(errno)); - #endif + #endif } +#if !defined(WOLFTPM_SWTPM_UARTNS550) ctx->tcpCtx.fd = -1; +#endif return rc; } @@ -209,7 +517,7 @@ static TPM_RC SwTpmDisconnect(TPM2_CTX* ctx) */ int TPM2_SWTPM_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet) { - int rc = TPM_RC_FAILURE; + int rc = TPM_RC_SUCCESS; int rspSz = 0; uint32_t tss_word; @@ -217,12 +525,19 @@ int TPM2_SWTPM_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet) return BAD_FUNC_ARG; } +#if !defined(WOLFTPM_SWTPM_UARTNS550) if (ctx->tcpCtx.fd < 0) { rc = SwTpmConnect(ctx, TPM2_SWTPM_HOST, TPM2_SWTPM_PORT); } +#else + if (ctx->tcpCtx.setup == 0) { + ctx->tcpCtx.setup = 1; + rc = SwTpmConnect(ctx, TPM2_SWTPM_PORT); + } +#endif #ifdef WOLFTPM_DEBUG_VERBOSE - printf("Command size: %d\n", packet->pos); + DEBUG_PRINTF("Command size: %d\n\r", packet->pos); TPM2_PrintBin(packet->buf, packet->pos); #endif @@ -253,10 +568,10 @@ int TPM2_SWTPM_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet) rc = SwTpmReceive(ctx, &tss_word, sizeof(uint32_t)); rspSz = TPM2_Packet_SwapU32(tss_word); if (rspSz > packet->size) { - #ifdef WOLFTPM_DEBUG_VERBOSE - printf("Response size(%d) larger than command buffer(%d)\n", + #ifdef WOLFTPM_DEBUG_VERBOSE + DEBUG_PRINTF("Response size(%d) larger than command buffer(%d)\n", rspSz, packet->pos); - #endif + #endif rc = TPM_RC_FAILURE; } } @@ -272,22 +587,26 @@ int TPM2_SWTPM_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet) if (rc == TPM_RC_SUCCESS) { rc = SwTpmReceive(ctx, &tss_word, sizeof(uint32_t)); tss_word = TPM2_Packet_SwapU32(tss_word); - #ifdef WOLFTPM_DEBUG + #ifdef WOLFTPM_DEBUG if (tss_word != 0) { - printf("SWTPM ack %d\n", tss_word); + DEBUG_PRINTF("SWTPM ack %d\n", tss_word); } - #endif + #endif } - #ifdef WOLFTPM_DEBUG_VERBOSE if (rspSz > 0) { - printf("Response size: %d\n", rspSz); - TPM2_PrintBin(packet->buf, rspSz); + int sz = (rspSz < packet->size) ? rspSz : packet->size; + DEBUG_PRINTF("Response size: %d\n", rspSz); + TPM2_PrintBin(packet->buf, sz); + (void)sz; } #endif - if (ctx->tcpCtx.fd >= 0) { +#if !defined(WOLFTPM_SWTPM_UARTNS550) + if (ctx->tcpCtx.fd >= 0) +#endif + { TPM_RC rc_disconnect = SwTpmDisconnect(ctx); if (rc == TPM_RC_SUCCESS) { rc = rc_disconnect; diff --git a/src/tpm2_wrap.c b/src/tpm2_wrap.c index 9f77e322..f74918b3 100644 --- a/src/tpm2_wrap.c +++ b/src/tpm2_wrap.c @@ -75,12 +75,12 @@ static int wolfTPM2_Init_ex(TPM2_CTX* ctx, TPM2HalIoCb ioCb, void* userCtx, #endif if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_Init failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); + DEBUG_PRINTF("TPM2_Init failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; } #ifdef DEBUG_WOLFTPM - printf("TPM2: Caps 0x%08x, Did 0x%04x, Vid 0x%04x, Rid 0x%2x \n", + DEBUG_PRINTF("TPM2: Caps 0x%08x, Did 0x%04x, Vid 0x%04x, Rid 0x%2x \n", ctx->caps, ctx->did_vid >> 16, ctx->did_vid & 0xFFFF, @@ -95,12 +95,12 @@ static int wolfTPM2_Init_ex(TPM2_CTX* ctx, TPM2HalIoCb ioCb, void* userCtx, if (rc != TPM_RC_SUCCESS && rc != TPM_RC_INITIALIZE /* TPM_RC_INITIALIZE = Already started */ ) { #ifdef DEBUG_WOLFTPM - printf("TPM2_Startup failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); + DEBUG_PRINTF("TPM2_Startup failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; } #ifdef DEBUG_WOLFTPM - printf("TPM2_Startup pass\n"); + DEBUG_PRINTF("TPM2_Startup pass\n"); #endif #if defined(WOLFTPM_MICROCHIP) || defined(WOLFTPM_PERFORM_SELFTEST) @@ -110,12 +110,12 @@ static int wolfTPM2_Init_ex(TPM2_CTX* ctx, TPM2HalIoCb ioCb, void* userCtx, rc = TPM2_SelfTest(&selfTest); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_SelfTest failed 0x%x: %s\n", rc, TPM2_GetRCString(rc)); + DEBUG_PRINTF("TPM2_SelfTest failed 0x%x: %s\n", rc, TPM2_GetRCString(rc)); #endif return rc; } #ifdef DEBUG_WOLFTPM - printf("TPM2_SelfTest pass\n"); + DEBUG_PRINTF("TPM2_SelfTest pass\n"); #endif #else rc = TPM_RC_SUCCESS; @@ -334,7 +334,7 @@ int wolfTPM2_GetKeyBlobAsBuffer(byte *buffer, word32 bufferSz, if (pubAreaSize != (key->pub.size + (int)sizeof(key->pub.size))) { #ifdef DEBUG_WOLFTPM - printf("Sanity check for publicArea size failed\n"); + DEBUG_PRINTF("Sanity check for publicArea size failed\n"); #endif return BUFFER_E; } @@ -365,7 +365,7 @@ int wolfTPM2_GetKeyBlobAsBuffer(byte *buffer, word32 bufferSz, sz += sizeof(UINT16) + key->priv.size; #ifdef WOLFTPM_DEBUG_VERBOSE - printf("Get KeyBlob: %d bytes\n", (int)sz); + DEBUG_PRINTF("Get KeyBlob: %d bytes\n", (int)sz); TPM2_PrintBin(buffer, sz); #endif @@ -392,7 +392,7 @@ int wolfTPM2_GetKeyBlobAsSeparateBuffers(byte* pubBuffer, word32* pubBufferSz, if (pubAreaSize != (key->pub.size + (int)sizeof(key->pub.size))) { #ifdef DEBUG_WOLFTPM - printf("Sanity check for publicArea size failed\n"); + DEBUG_PRINTF("Sanity check for publicArea size failed\n"); #endif return BUFFER_E; } @@ -425,10 +425,10 @@ int wolfTPM2_GetKeyBlobAsSeparateBuffers(byte* pubBuffer, word32* pubBufferSz, *privBufferSz += sizeof(UINT16) + key->priv.size; #ifdef WOLFTPM_DEBUG_VERBOSE - printf("Get KeyBlob public: %d bytes\n", (int)*pubBufferSz); + DEBUG_PRINTF("Get KeyBlob public: %d bytes\n", (int)*pubBufferSz); TPM2_PrintBin(pubBuffer, *pubBufferSz); - printf("Get KeyBlob private: %d bytes\n", (int)*privBufferSz); + DEBUG_PRINTF("Get KeyBlob private: %d bytes\n", (int)*privBufferSz); TPM2_PrintBin(privBuffer, *privBufferSz); #endif @@ -451,13 +451,13 @@ int wolfTPM2_SetKeyBlobFromBuffer(WOLFTPM2_KEYBLOB* key, byte *buffer, XMEMSET(key, 0, sizeof(WOLFTPM2_KEYBLOB)); #ifdef WOLFTPM_DEBUG_VERBOSE - printf("Set KeyBlob: %d bytes\n", (int)bufferSz); + DEBUG_PRINTF("Set KeyBlob: %d bytes\n", (int)bufferSz); TPM2_PrintBin(buffer, bufferSz); #endif if (bufferSz < done_reading + sizeof(key->pub.size)) { #ifdef DEBUG_WOLFTPM - printf("Buffer size check failed (%d)\n", bufferSz); + DEBUG_PRINTF("Buffer size check failed (%d)\n", bufferSz); #endif return BUFFER_E; } @@ -468,7 +468,7 @@ int wolfTPM2_SetKeyBlobFromBuffer(WOLFTPM2_KEYBLOB* key, byte *buffer, if (bufferSz < done_reading + sizeof(UINT16) + key->pub.size) { #ifdef DEBUG_WOLFTPM - printf("Buffer size check failed (%d)\n", bufferSz); + DEBUG_PRINTF("Buffer size check failed (%d)\n", bufferSz); #endif return BUFFER_E; } @@ -486,7 +486,7 @@ int wolfTPM2_SetKeyBlobFromBuffer(WOLFTPM2_KEYBLOB* key, byte *buffer, if (bufferSz < done_reading + sizeof(key->priv.size)) { #ifdef DEBUG_WOLFTPM - printf("Buffer size check failed (%d)\n", bufferSz); + DEBUG_PRINTF("Buffer size check failed (%d)\n", bufferSz); #endif return BUFFER_E; } @@ -497,7 +497,7 @@ int wolfTPM2_SetKeyBlobFromBuffer(WOLFTPM2_KEYBLOB* key, byte *buffer, if (bufferSz < done_reading + key->priv.size) { #ifdef DEBUG_WOLFTPM - printf("Buffer size check failed (%d)\n", bufferSz); + DEBUG_PRINTF("Buffer size check failed (%d)\n", bufferSz); #endif return BUFFER_E; } @@ -507,7 +507,7 @@ int wolfTPM2_SetKeyBlobFromBuffer(WOLFTPM2_KEYBLOB* key, byte *buffer, if (done_reading != bufferSz) { #ifdef DEBUG_WOLFTPM - printf("Extra data left in buffer (%d!=%d)\n", + DEBUG_PRINTF("Extra data left in buffer (%d!=%d)\n", bufferSz, (word32)done_reading); #endif return BUFFER_E; @@ -553,7 +553,7 @@ int wolfTPM2_OpenExisting(WOLFTPM2_DEV* dev, TPM2HalIoCb ioCb, void* userCtx) rc = wolfTPM2_Init_ex(&dev->ctx, ioCb, userCtx, 0); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_Init failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); + DEBUG_PRINTF("TPM2_Init failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; } @@ -589,19 +589,19 @@ int wolfTPM2_SelfTest(WOLFTPM2_DEV* dev) #ifdef WOLFTPM_WINAPI if (rc == (int)TPM_E_COMMAND_BLOCKED) { /* 0x80280400 */ #ifdef DEBUG_WOLFTPM - printf("TPM2_SelfTest not allowed on Windows TBS (err 0x%x)\n", rc); + DEBUG_PRINTF("TPM2_SelfTest not allowed on Windows TBS (err 0x%x)\n", rc); #endif rc = TPM_RC_SUCCESS; /* report success */ } #endif if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_SelfTest failed 0x%x: %s\n", rc, TPM2_GetRCString(rc)); + DEBUG_PRINTF("TPM2_SelfTest failed 0x%x: %s\n", rc, TPM2_GetRCString(rc)); #endif return rc; } #ifdef DEBUG_WOLFTPM - printf("TPM2_SelfTest pass\n"); + DEBUG_PRINTF("TPM2_SelfTest pass\n"); #endif return rc; @@ -736,7 +736,7 @@ static int tpm2_ifx_cap_vendor_get(WOLFTPM2_CAPS* cap, uint32_t property, } else { #ifdef DEBUG_WOLFTPM - printf("TPM2_GetCapability vendor prop 0x%x failed 0x%x: %s\n", + DEBUG_PRINTF("TPM2_GetCapability vendor prop 0x%x failed 0x%x: %s\n", property, rc, TPM2_GetRCString(rc)); #endif } @@ -766,8 +766,8 @@ static int wolfTPM2_GetCapabilities_NoDev(WOLFTPM2_CAPS* cap) rc = TPM2_GetCapability(&in, &out); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_GetCapability manufacture failed 0x%x: %s\n", - rc, TPM2_GetRCString(rc)); + DEBUG_PRINTF("TPM2_GetCapability manufacture failed 0x%x: %s\n", rc, + TPM2_GetRCString(rc)); #endif return rc; } @@ -784,8 +784,8 @@ static int wolfTPM2_GetCapabilities_NoDev(WOLFTPM2_CAPS* cap) rc = TPM2_GetCapability(&in, &out); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_GetCapability modes failed 0x%x: %s\n", - rc, TPM2_GetRCString(rc)); + DEBUG_PRINTF("TPM2_GetCapability modes failed 0x%x: %s\n", rc, + TPM2_GetRCString(rc)); #endif return rc; } @@ -839,7 +839,7 @@ int wolfTPM2_GetHandles(TPM_HANDLE handle, TPML_HANDLE* handles) rc = TPM2_GetCapability(&in, &out); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_GetCapability handles failed 0x%x: %s\n", rc, + DEBUG_PRINTF("TPM2_GetCapability handles failed 0x%x: %s\n", rc, TPM2_GetRCString(rc)); #endif return rc; @@ -850,9 +850,9 @@ int wolfTPM2_GetHandles(TPM_HANDLE handle, TPML_HANDLE* handles) } handles = &out.capabilityData.data.handles; #ifdef DEBUG_WOLFTPM - printf("Handles Cap: Start 0x%x, Count %d\n", handle, handles->count); + DEBUG_PRINTF("Handles Cap: Start 0x%x, Count %d\n", handle, handles->count); for (i=0; icount; i++) { - printf("\tHandle 0x%x\n", handles->handle[i]); + DEBUG_PRINTF("\tHandle 0x%x\n", handles->handle[i]); } #endif return handles->count; @@ -1034,7 +1034,7 @@ int wolfTPM2_CreateAuthSession_EkPolicy(WOLFTPM2_DEV* dev, TPM_SE_POLICY, TPM_ALG_NULL); if (rc == TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_StartAuthSession: sessionHandle 0x%x\n", + DEBUG_PRINTF("TPM2_StartAuthSession: sessionHandle 0x%x\n", (word32)tpmSession->handle.hndl); #endif /* Provide Endorsement Auth using PolicySecret */ @@ -1044,7 +1044,7 @@ int wolfTPM2_CreateAuthSession_EkPolicy(WOLFTPM2_DEV* dev, rc = TPM2_PolicySecret(&policySecretIn, &policySecretOut); #ifdef DEBUG_WOLFTPM if (rc == TPM_RC_SUCCESS) { - printf("policySecret applied on session\n"); + DEBUG_PRINTF("policySecret applied on session\n"); } #endif } @@ -1073,7 +1073,7 @@ int wolfTPM2_Cleanup_ex(WOLFTPM2_DEV* dev, int doShutdown) rc = TPM2_Shutdown(&shutdownIn); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_Shutdown failed %d: %s\n", + DEBUG_PRINTF("TPM2_Shutdown failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif /* finish cleanup and return error */ @@ -1401,7 +1401,7 @@ int wolfTPM2_EncryptSecret(WOLFTPM2_DEV* dev, const WOLFTPM2_KEY* tpmKey, } #ifdef DEBUG_WOLFTPM - printf("Encrypt secret: Alg %s, Label %s\n", + DEBUG_PRINTF("Encrypt secret: Alg %s, Label %s\n", TPM2_GetAlgName(tpmKey->pub.publicArea.type), label); #endif @@ -1423,7 +1423,7 @@ int wolfTPM2_EncryptSecret(WOLFTPM2_DEV* dev, const WOLFTPM2_KEY* tpmKey, } #ifdef WOLFTPM_DEBUG_VERBOSE - printf("Encrypt Secret %d: %d bytes\n", rc, data->size); + DEBUG_PRINTF("Encrypt Secret %d: %d bytes\n", rc, data->size); TPM2_PrintBin(data->buffer, data->size); #endif #endif /* !WOLFTPM2_NO_WOLFCRYPT */ @@ -1499,7 +1499,7 @@ int wolfTPM2_StartSession(WOLFTPM2_DEV* dev, WOLFTPM2_SESSION* session, authSesIn.nonceCaller.size); if (rc < 0) { #ifdef DEBUG_WOLFTPM - printf("TPM2_GetNonce failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); + DEBUG_PRINTF("TPM2_GetNonce failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; } @@ -1517,7 +1517,7 @@ int wolfTPM2_StartSession(WOLFTPM2_DEV* dev, WOLFTPM2_SESSION* session, &authSesIn.encryptedSalt, "SECRET"); if (rc != 0) { #ifdef DEBUG_WOLFTPM - printf("Building encrypted salt failed %d: %s!\n", rc, + DEBUG_PRINTF("Building encrypted salt failed %d: %s!\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; @@ -1527,7 +1527,7 @@ int wolfTPM2_StartSession(WOLFTPM2_DEV* dev, WOLFTPM2_SESSION* session, rc = TPM2_StartAuthSession(&authSesIn, &authSesOut); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_StartAuthSession failed %d: %s\n", rc, + DEBUG_PRINTF("TPM2_StartAuthSession failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; @@ -1553,7 +1553,7 @@ int wolfTPM2_StartSession(WOLFTPM2_DEV* dev, WOLFTPM2_SESSION* session, session->handle.auth.buffer, session->handle.auth.size); if (rc != hashDigestSz) { #ifdef DEBUG_WOLFTPM - printf("KDFa ATH Gen Error %d\n", rc); + DEBUG_PRINTF("KDFa ATH Gen Error %d\n", rc); #endif return TPM_RC_FAILURE; } @@ -1561,7 +1561,7 @@ int wolfTPM2_StartSession(WOLFTPM2_DEV* dev, WOLFTPM2_SESSION* session, } #ifdef WOLFTPM_DEBUG_VERBOSE - printf("Session Key %d\n", session->handle.auth.size); + DEBUG_PRINTF("Session Key %d\n", session->handle.auth.size); TPM2_PrintBin(session->handle.auth.buffer, session->handle.auth.size); #endif @@ -1585,7 +1585,7 @@ int wolfTPM2_StartSession(WOLFTPM2_DEV* dev, WOLFTPM2_SESSION* session, session->nonceTPM.size); #ifdef DEBUG_WOLFTPM - printf("TPM2_StartAuthSession: handle 0x%x, algorithm %s\n", + DEBUG_PRINTF("TPM2_StartAuthSession: handle 0x%x, algorithm %s\n", (word32)session->handle.hndl, TPM2_GetAlgName(authSesIn.symmetric.algorithm)); #endif @@ -1631,7 +1631,7 @@ int wolfTPM2_CreatePrimaryKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, rc = TPM2_CreatePrimary(&createPriIn, &createPriOut); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_CreatePrimary: failed %d: %s\n", rc, + DEBUG_PRINTF("TPM2_CreatePrimary: failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; @@ -1645,7 +1645,7 @@ int wolfTPM2_CreatePrimaryKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, wolfTPM2_CopyPub(&key->pub, &createPriOut.outPublic); #ifdef DEBUG_WOLFTPM - printf("TPM2_CreatePrimary: 0x%x (%d bytes)\n", + DEBUG_PRINTF("TPM2_CreatePrimary: 0x%x (%d bytes)\n", (word32)key->handle.hndl, key->pub.size); #endif @@ -1680,7 +1680,7 @@ int wolfTPM2_ChangeAuthKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, rc = TPM2_ObjectChangeAuth(&changeIn, &changeOut); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_ObjectChangeAuth failed %d: %s\n", rc, + DEBUG_PRINTF("TPM2_ObjectChangeAuth failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; @@ -1700,7 +1700,7 @@ int wolfTPM2_ChangeAuthKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, rc = TPM2_Load(&loadIn, &loadOut); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_Load key failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); + DEBUG_PRINTF("TPM2_Load key failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; } @@ -1709,7 +1709,7 @@ int wolfTPM2_ChangeAuthKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, wolfTPM2_CopyName(&key->handle.name, &loadOut.name); #ifdef DEBUG_WOLFTPM - printf("wolfTPM2_ChangeAuthKey: Key Handle 0x%x\n", + DEBUG_PRINTF("wolfTPM2_ChangeAuthKey: Key Handle 0x%x\n", (word32)key->handle.hndl); #endif @@ -1754,13 +1754,13 @@ int wolfTPM2_CreateKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEYBLOB* keyBlob, rc = TPM2_Create(&createIn, &createOut); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_Create key failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); + DEBUG_PRINTF("TPM2_Create key failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; } #ifdef DEBUG_WOLFTPM - printf("TPM2_Create key: pub %d, priv %d\n", + DEBUG_PRINTF("TPM2_Create key: pub %d, priv %d\n", createOut.outPublic.size, createOut.outPrivate.size); TPM2_PrintPublicArea(&createOut.outPublic); #endif @@ -1796,7 +1796,7 @@ int wolfTPM2_LoadKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEYBLOB* keyBlob, rc = TPM2_Load(&loadIn, &loadOut); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_Load key failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); + DEBUG_PRINTF("TPM2_Load key failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; } @@ -1804,7 +1804,7 @@ int wolfTPM2_LoadKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEYBLOB* keyBlob, wolfTPM2_CopyName(&keyBlob->handle.name, &loadOut.name); #ifdef DEBUG_WOLFTPM - printf("TPM2_Load Key Handle 0x%x\n", (word32)keyBlob->handle.hndl); + DEBUG_PRINTF("TPM2_Load Key Handle 0x%x\n", (word32)keyBlob->handle.hndl); #endif return rc; @@ -1861,13 +1861,13 @@ int wolfTPM2_CreateLoadedKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEYBLOB* keyBlob, rc = TPM2_CreateLoaded(&createLoadedIn, &createLoadedOut); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_CreateLoaded key failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); + DEBUG_PRINTF("TPM2_CreateLoaded key failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; } #ifdef DEBUG_WOLFTPM - printf("TPM2_CreateLoaded key: pub %d, priv %d\n", + DEBUG_PRINTF("TPM2_CreateLoaded key: pub %d, priv %d\n", createLoadedOut.outPublic.size, createLoadedOut.outPrivate.size); TPM2_PrintPublicArea(&createLoadedOut.outPublic); #endif @@ -1903,7 +1903,7 @@ int wolfTPM2_LoadPublicKey_ex(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, rc = TPM2_LoadExternal(&loadExtIn, &loadExtOut); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_LoadExternal: failed %d: %s\n", rc, + DEBUG_PRINTF("TPM2_LoadExternal: failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; @@ -1915,7 +1915,7 @@ int wolfTPM2_LoadPublicKey_ex(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, wolfTPM2_CopyPub(&key->pub, &loadExtIn.inPublic); #ifdef DEBUG_WOLFTPM - printf("TPM2_LoadExternal: 0x%x\n", (word32)loadExtOut.objectHandle); + DEBUG_PRINTF("TPM2_LoadExternal: 0x%x\n", (word32)loadExtOut.objectHandle); #endif return rc; @@ -2027,7 +2027,7 @@ static int SensitiveToPrivate(TPM2B_SENSITIVE* sens, TPM2B_PRIVATE* priv, digestSz = TPM2_GetHashDigestSize(nameAlg); if (digestSz == 0) { #ifdef DEBUG_WOLFTPM - printf("SensitiveToPrivate: Invalid name algorithm %d\n", nameAlg); + DEBUG_PRINTF("SensitiveToPrivate: Invalid name algorithm %d\n", nameAlg); #endif return TPM_RC_FAILURE; } @@ -2077,7 +2077,7 @@ static int SensitiveToPrivate(TPM2B_SENSITIVE* sens, TPM2B_PRIVATE* priv, NULL, symKey.buffer, symKey.size); if (rc != symKey.size) { #ifdef DEBUG_WOLFTPM - printf("KDFa STORAGE Gen Error %d\n", rc); + DEBUG_PRINTF("KDFa STORAGE Gen Error %d\n", rc); #endif return TPM_RC_FAILURE; } @@ -2095,7 +2095,7 @@ static int SensitiveToPrivate(TPM2B_SENSITIVE* sens, TPM2B_PRIVATE* priv, } if (rc != 0) { #ifdef DEBUG_WOLFTPM - printf("SensitiveToPrivate AES error %d!\n", rc); + DEBUG_PRINTF("SensitiveToPrivate AES error %d!\n", rc); #endif return rc; } @@ -2106,7 +2106,7 @@ static int SensitiveToPrivate(TPM2B_SENSITIVE* sens, TPM2B_PRIVATE* priv, hmacKey.buffer, hmacKey.size); if (rc != hmacKey.size) { #ifdef DEBUG_WOLFTPM - printf("KDFa INTEGRITY Gen Error %d\n", rc); + DEBUG_PRINTF("KDFa INTEGRITY Gen Error %d\n", rc); #endif return rc; } @@ -2133,7 +2133,7 @@ static int SensitiveToPrivate(TPM2B_SENSITIVE* sens, TPM2B_PRIVATE* priv, } if (rc != 0) { #ifdef DEBUG_WOLFTPM - printf("SensitiveToPrivate HMAC error %d!\n", rc); + DEBUG_PRINTF("SensitiveToPrivate HMAC error %d!\n", rc); #endif return rc; } @@ -2199,7 +2199,7 @@ int wolfTPM2_ImportPrivateKey(WOLFTPM2_DEV* dev, const WOLFTPM2_KEY* parentKey, rc = wolfTPM2_ComputeName(pub, &name); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("wolfTPM2_ComputeName: failed %d: %s\n", rc, + DEBUG_PRINTF("wolfTPM2_ComputeName: failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; @@ -2211,7 +2211,7 @@ int wolfTPM2_ImportPrivateKey(WOLFTPM2_DEV* dev, const WOLFTPM2_KEY* parentKey, "DUPLICATE"); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("wolfTPM2_EncryptSecret: failed %d: %s\n", rc, + DEBUG_PRINTF("wolfTPM2_EncryptSecret: failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; @@ -2223,7 +2223,7 @@ int wolfTPM2_ImportPrivateKey(WOLFTPM2_DEV* dev, const WOLFTPM2_KEY* parentKey, &symSeed); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("wolfTPM2_SensitiveToPrivate: failed %d: %s\n", rc, + DEBUG_PRINTF("wolfTPM2_SensitiveToPrivate: failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; @@ -2231,7 +2231,7 @@ int wolfTPM2_ImportPrivateKey(WOLFTPM2_DEV* dev, const WOLFTPM2_KEY* parentKey, rc = TPM2_Import(&importIn, &importOut); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_Import: failed %d: %s\n", rc, + DEBUG_PRINTF("TPM2_Import: failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; @@ -2300,7 +2300,7 @@ int wolfTPM2_LoadRsaPublicKey_ex(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, public exponent less than 2^16 + 1. */ if (exponent < 7) { #ifdef DEBUG_WOLFTPM - printf("TPM based RSA with exponent %u not allowed! Using soft RSA\n", + DEBUG_PRINTF("TPM based RSA with exponent %u not allowed! Using soft RSA\n", exponent); #endif return TPM_RC_KEY; @@ -2389,7 +2389,7 @@ int wolfTPM2_ImportRsaPrivateKeySeed(WOLFTPM2_DEV* dev, /* use custom seed */ if (seedSz != digestSz) { #ifdef DEBUG_WOLFTPM - printf("Import RSA seed size invalid! %d != %d\n", + DEBUG_PRINTF("Import RSA seed size invalid! %d != %d\n", seedSz, digestSz); #endif return BAD_FUNC_ARG; @@ -2541,7 +2541,7 @@ int wolfTPM2_ImportEccPrivateKeySeed(WOLFTPM2_DEV* dev, const WOLFTPM2_KEY* pare /* use custom seed */ if (seedSz != digestSz) { #ifdef DEBUG_WOLFTPM - printf("Import ECC seed size invalid! %d != %d\n", + DEBUG_PRINTF("Import ECC seed size invalid! %d != %d\n", seedSz, digestSz); #endif return BAD_FUNC_ARG; @@ -2616,7 +2616,7 @@ int wolfTPM2_ReadPublicKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, rc = TPM2_ReadPublic(&readPubIn, &readPubOut); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_ReadPublic failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); + DEBUG_PRINTF("TPM2_ReadPublic failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; } @@ -2628,7 +2628,7 @@ int wolfTPM2_ReadPublicKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, wolfTPM2_CopyPub(&key->pub, &readPubOut.outPublic); #ifdef DEBUG_WOLFTPM - printf("TPM2_ReadPublic Handle 0x%x: pub %d, name %d, qualifiedName %d\n", + DEBUG_PRINTF("TPM2_ReadPublic Handle 0x%x: pub %d, name %d, qualifiedName %d\n", (word32)readPubIn.objectHandle, readPubOut.outPublic.size, readPubOut.name.size, readPubOut.qualifiedName.size); @@ -2908,7 +2908,7 @@ int wolfTPM2_ExportPublicKeyBuffer(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* tpmKey, } else { #ifdef DEBUG_WOLFTPM - printf("Invalid tpmKey type!\n"); + DEBUG_PRINTF("Invalid tpmKey type!\n"); #endif rc = BAD_FUNC_ARG; } @@ -3074,7 +3074,7 @@ int wolfTPM2_ImportPrivateKeyBuffer(WOLFTPM2_DEV* dev, /* use custom seed */ if (seedSz != digestSz) { #ifdef DEBUG_WOLFTPM - printf("Import %s seed size invalid! %d != %d\n", + DEBUG_PRINTF("Import %s seed size invalid! %d != %d\n", TPM2_GetAlgName(keyType), seedSz, digestSz); #endif return BAD_FUNC_ARG; @@ -3516,22 +3516,22 @@ int wolfTPM2_NVStoreKey(WOLFTPM2_DEV* dev, TPM_HANDLE primaryHandle, #ifdef WOLFTPM_WINAPI if (rc == (int)TPM_E_COMMAND_BLOCKED) { /* 0x80280400 */ #ifdef DEBUG_WOLFTPM - printf("TPM2_EvictControl (storing key to NV) not allowed on " + DEBUG_PRINTF("TPM2_EvictControl (storing key to NV) not allowed on " "Windows TBS (err 0x%x)\n", rc); #endif - rc = TPM_RC_COMMAND_CODE; + rc = TPM_RC_NV_UNAVAILABLE; } #endif #ifdef DEBUG_WOLFTPM - printf("TPM2_EvictControl failed %d: %s\n", rc, + DEBUG_PRINTF("TPM2_EvictControl failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; } #ifdef DEBUG_WOLFTPM - printf("TPM2_EvictControl Auth 0x%x, Key 0x%x, Persistent 0x%x\n", + DEBUG_PRINTF("TPM2_EvictControl Auth 0x%x, Key 0x%x, Persistent 0x%x\n", (word32)in.auth, (word32)in.objectHandle, (word32)in.persistentHandle); #endif @@ -3568,14 +3568,14 @@ int wolfTPM2_NVDeleteKey(WOLFTPM2_DEV* dev, TPM_HANDLE primaryHandle, rc = TPM2_EvictControl(&in); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_EvictControl failed %d: %s\n", rc, + DEBUG_PRINTF("TPM2_EvictControl failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; } #ifdef DEBUG_WOLFTPM - printf("TPM2_EvictControl Auth 0x%x, Key 0x%x, Persistent 0x%x\n", + DEBUG_PRINTF("TPM2_EvictControl Auth 0x%x, Key 0x%x, Persistent 0x%x\n", (word32)in.auth, (word32)in.objectHandle, (word32)in.persistentHandle); #endif @@ -3625,7 +3625,7 @@ int wolfTPM2_SignHashScheme(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, rc = TPM2_Sign(&signIn, &signOut); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_Sign failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); + DEBUG_PRINTF("TPM2_Sign failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; } @@ -3636,7 +3636,7 @@ int wolfTPM2_SignHashScheme(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, signOut.signature.signature.ecdsa.signatureS.size; if (sigOutSz > *sigSz) { #ifdef DEBUG_WOLFTPM - printf("TPM2_Sign: ECC result truncated %d -> %d\n", + DEBUG_PRINTF("TPM2_Sign: ECC result truncated %d -> %d\n", sigOutSz, *sigSz); #endif sigOutSz = *sigSz; @@ -3653,7 +3653,7 @@ int wolfTPM2_SignHashScheme(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, sigOutSz = signOut.signature.signature.rsassa.sig.size; if (sigOutSz > *sigSz) { #ifdef DEBUG_WOLFTPM - printf("TPM2_Sign: RSA result truncated %d -> %d\n", + DEBUG_PRINTF("TPM2_Sign: RSA result truncated %d -> %d\n", sigOutSz, *sigSz); #endif sigOutSz = *sigSz; @@ -3663,7 +3663,7 @@ int wolfTPM2_SignHashScheme(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, *sigSz = sigOutSz; #ifdef DEBUG_WOLFTPM - printf("TPM2_Sign: %s %d\n", + DEBUG_PRINTF("TPM2_Sign: %s %d\n", TPM2_GetAlgName(signIn.inScheme.scheme), *sigSz); #endif @@ -3782,7 +3782,7 @@ int wolfTPM2_VerifyHashTicket(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, rc = TPM2_VerifySignature(&verifySigIn, &verifySigOut); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_VerifySignature failed %d: %s\n", rc, + DEBUG_PRINTF("TPM2_VerifySignature failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif } @@ -3793,7 +3793,8 @@ int wolfTPM2_VerifyHashTicket(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, sizeof(TPMT_TK_VERIFIED)); } #ifdef DEBUG_WOLFTPM - printf("TPM2_VerifySignature: Tag %d\n", verifySigOut.validation.tag); + DEBUG_PRINTF("TPM2_VerifySignature: Tag %d\n", + verifySigOut.validation.tag); #endif } return rc; @@ -3881,7 +3882,7 @@ int wolfTPM2_ECDHGen(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* privKey, rc = TPM2_ECDH_KeyGen(&ecdhIn, &ecdhOut); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_ECDH_KeyGen failed %d: %s\n", rc, + DEBUG_PRINTF("TPM2_ECDH_KeyGen failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; @@ -3894,7 +3895,7 @@ int wolfTPM2_ECDHGen(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* privKey, XMEMCPY(out, ecdhOut.zPoint.point.x.buffer, ecdhOut.zPoint.point.x.size); #ifdef DEBUG_WOLFTPM - printf("TPM2_ECDH_KeyGen: zPt %d, pubPt %d\n", + DEBUG_PRINTF("TPM2_ECDH_KeyGen: zPt %d, pubPt %d\n", ecdhOut.zPoint.size, ecdhOut.pubPoint.size); #endif @@ -3932,7 +3933,7 @@ int wolfTPM2_ECDHGenZ(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* privKey, rc = TPM2_ECDH_ZGen(&ecdhZIn, &ecdhZOut); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_ECDH_ZGen failed %d: %s\n", rc, + DEBUG_PRINTF("TPM2_ECDH_ZGen failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; @@ -3943,7 +3944,7 @@ int wolfTPM2_ECDHGenZ(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* privKey, ecdhZOut.outPoint.point.x.size); #ifdef DEBUG_WOLFTPM - printf("TPM2_ECDH_ZGen: zPt %d\n", ecdhZOut.outPoint.size); + DEBUG_PRINTF("TPM2_ECDH_ZGen: zPt %d\n", ecdhZOut.outPoint.size); #endif return rc; @@ -3967,7 +3968,7 @@ int wolfTPM2_ECDHEGenKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* ecdhKey, int curve_id) rc = TPM2_EC_Ephemeral(&in, &out); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_EC_Ephemeral failed %d: %s\n", rc, + DEBUG_PRINTF("TPM2_EC_Ephemeral failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; @@ -4018,7 +4019,7 @@ int wolfTPM2_ECDHEGenZ(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* parentKey, rc = TPM2_ZGen_2Phase(&inZGen2Ph, &outZGen2Ph); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_ZGen_2Phase failed %d: %s\n", rc, + DEBUG_PRINTF("TPM2_ZGen_2Phase failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; @@ -4029,7 +4030,7 @@ int wolfTPM2_ECDHEGenZ(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* parentKey, outZGen2Ph.outZ2.point.x.size); #ifdef DEBUG_WOLFTPM - printf("TPM2_ZGen_2Phase: zPt %d\n", outZGen2Ph.outZ2.size); + DEBUG_PRINTF("TPM2_ZGen_2Phase: zPt %d\n", outZGen2Ph.outZ2.size); #endif return rc; @@ -4069,7 +4070,7 @@ int wolfTPM2_RsaEncrypt(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, rc = TPM2_RSA_Encrypt(&rsaEncIn, &rsaEncOut); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_RSA_Encrypt failed %d: %s\n", rc, + DEBUG_PRINTF("TPM2_RSA_Encrypt failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; @@ -4079,7 +4080,7 @@ int wolfTPM2_RsaEncrypt(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, XMEMCPY(out, rsaEncOut.outData.buffer, *outSz); #ifdef DEBUG_WOLFTPM - printf("TPM2_RSA_Encrypt: %d\n", rsaEncOut.outData.size); + DEBUG_PRINTF("TPM2_RSA_Encrypt: %d\n", rsaEncOut.outData.size); #endif return rc; @@ -4118,7 +4119,7 @@ int wolfTPM2_RsaDecrypt(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, rc = TPM2_RSA_Decrypt(&rsaDecIn, &rsaDecOut); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_RSA_Decrypt failed %d: %s\n", rc, + DEBUG_PRINTF("TPM2_RSA_Decrypt failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; @@ -4128,7 +4129,7 @@ int wolfTPM2_RsaDecrypt(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, XMEMCPY(msg, rsaDecOut.message.buffer, *msgSz); #ifdef DEBUG_WOLFTPM - printf("TPM2_RSA_Decrypt: %d\n", rsaDecOut.message.size); + DEBUG_PRINTF("TPM2_RSA_Decrypt: %d\n", rsaDecOut.message.size); #endif return rc; @@ -4167,7 +4168,7 @@ int wolfTPM2_ReadPCR(WOLFTPM2_DEV* dev, int pcrIndex, int hashAlg, byte* digest, rc = TPM2_PCR_Read(&pcrReadIn, &pcrReadOut); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_PCR_Read failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); + DEBUG_PRINTF("TPM2_PCR_Read failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; } @@ -4177,7 +4178,7 @@ int wolfTPM2_ReadPCR(WOLFTPM2_DEV* dev, int pcrIndex, int hashAlg, byte* digest, XMEMCPY(digest, pcrReadOut.pcrValues.digests[0].buffer, digestLen); #ifdef DEBUG_WOLFTPM - printf("TPM2_PCR_Read: Index %d, Digest Sz %d, Update Counter %d\n", + DEBUG_PRINTF("TPM2_PCR_Read: Index %d, Digest Sz %d, Update Counter %d\n", pcrIndex, digestLen, (int)pcrReadOut.pcrUpdateCounter); TPM2_PrintBin(digest, digestLen); #endif @@ -4211,12 +4212,12 @@ int wolfTPM2_ExtendPCR(WOLFTPM2_DEV* dev, int pcrIndex, int hashAlg, rc = TPM2_PCR_Extend(&pcrExtend); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_PCR_Extend failed 0x%x: %s\n", rc, TPM2_GetRCString(rc)); + DEBUG_PRINTF("TPM2_PCR_Extend failed 0x%x: %s\n", rc, TPM2_GetRCString(rc)); #endif } #ifdef DEBUG_WOLFTPM - printf("TPM2_PCR_Extend: Index %d, Digest Sz %d\n", pcrIndex, digestLen); + DEBUG_PRINTF("TPM2_PCR_Extend: Index %d, Digest Sz %d\n", pcrIndex, digestLen); #endif return rc; @@ -4241,14 +4242,14 @@ int wolfTPM2_UnloadHandle(WOLFTPM2_DEV* dev, WOLFTPM2_HANDLE* handle) rc = TPM2_FlushContext(&in); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_FlushContext failed %d: %s\n", rc, + DEBUG_PRINTF("TPM2_FlushContext failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; } #ifdef DEBUG_WOLFTPM - printf("TPM2_FlushContext: Closed handle 0x%x\n", (word32)handle->hndl); + DEBUG_PRINTF("TPM2_FlushContext: Closed handle 0x%x\n", (word32)handle->hndl); #endif handle->hndl = TPM_RH_NULL; @@ -4299,12 +4300,12 @@ int wolfTPM2_NVCreateAuthPolicy(WOLFTPM2_DEV* dev, WOLFTPM2_HANDLE* parent, if (rc == TPM_RC_NV_DEFINED) { alreadyExists = 1; #ifdef DEBUG_WOLFTPM - printf("TPM2_NV_DefineSpace: handle already exists\n"); + DEBUG_PRINTF("TPM2_NV_DefineSpace: handle already exists\n"); #endif } else if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_NV_DefineSpace failed %d: %s\n", rc, + DEBUG_PRINTF("TPM2_NV_DefineSpace failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; @@ -4319,7 +4320,7 @@ int wolfTPM2_NVCreateAuthPolicy(WOLFTPM2_DEV* dev, WOLFTPM2_HANDLE* parent, rc = rctmp; #ifdef DEBUG_WOLFTPM - printf("TPM2_NV_DefineSpace: Auth 0x%x, Idx 0x%x, Attribs 0x%d, Size %d\n", + DEBUG_PRINTF("TPM2_NV_DefineSpace: Auth 0x%x, Idx 0x%x, Attribs 0x%d, Size %d\n", (word32)in.authHandle, (word32)in.publicInfo.nvPublic.nvIndex, (word32)in.publicInfo.nvPublic.attributes, @@ -4377,7 +4378,7 @@ int wolfTPM2_NVWriteAuth(WOLFTPM2_DEV* dev, WOLFTPM2_NV* nv, rc |= wolfTPM2_SetAuthHandleName(dev, 1, &nv->handle); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("Setting NV index name failed\n"); + DEBUG_PRINTF("Setting NV index name failed\n"); #endif return TPM_RC_FAILURE; } @@ -4398,7 +4399,7 @@ int wolfTPM2_NVWriteAuth(WOLFTPM2_DEV* dev, WOLFTPM2_NV* nv, rc = TPM2_NV_Write(&in); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_NV_Write failed %d: %s\n", rc, + DEBUG_PRINTF("TPM2_NV_Write failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; @@ -4413,7 +4414,7 @@ int wolfTPM2_NVWriteAuth(WOLFTPM2_DEV* dev, WOLFTPM2_NV* nv, } #ifdef DEBUG_WOLFTPM - printf("TPM2_NV_Write: Auth 0x%x, Idx 0x%x, Offset %d, Size %d\n", + DEBUG_PRINTF("TPM2_NV_Write: Auth 0x%x, Idx 0x%x, Offset %d, Size %d\n", (word32)in.authHandle, (word32)in.nvIndex, in.offset, in.data.size); #endif @@ -4459,7 +4460,7 @@ int wolfTPM2_NVReadAuth(WOLFTPM2_DEV* dev, WOLFTPM2_NV* nv, rc |= wolfTPM2_SetAuthHandleName(dev, 1, &nv->handle); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("Setting NV index name failed\n"); + DEBUG_PRINTF("Setting NV index name failed\n"); #endif return TPM_RC_FAILURE; } @@ -4479,7 +4480,7 @@ int wolfTPM2_NVReadAuth(WOLFTPM2_DEV* dev, WOLFTPM2_NV* nv, rc = TPM2_NV_Read(&in, &out); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_NV_Read failed %d: %s\n", rc, + DEBUG_PRINTF("TPM2_NV_Read failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; @@ -4491,7 +4492,7 @@ int wolfTPM2_NVReadAuth(WOLFTPM2_DEV* dev, WOLFTPM2_NV* nv, } #ifdef DEBUG_WOLFTPM - printf("TPM2_NV_Read: Auth 0x%x, Idx 0x%x, Offset %d, Size %d\n", + DEBUG_PRINTF("TPM2_NV_Read: Auth 0x%x, Idx 0x%x, Offset %d, Size %d\n", (word32)in.authHandle, (word32)in.nvIndex, in.offset, out.data.size); #endif @@ -4532,7 +4533,7 @@ int wolfTPM2_NVReadCert(WOLFTPM2_DEV* dev, TPM_HANDLE handle, } else { #ifdef DEBUG_WOLFTPM - printf("NV public read of handle 0x%x failed %d: %s\n", + DEBUG_PRINTF("NV public read of handle 0x%x failed %d: %s\n", handle, rc, wolfTPM2_GetRCString(rc)); #endif return rc; @@ -4577,7 +4578,7 @@ int wolfTPM2_NVOpen(WOLFTPM2_DEV* dev, WOLFTPM2_NV* nv, word32 nvIndex, rc = wolfTPM2_NVReadPublic(dev, nv->handle.hndl, &nvPublic); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("Failed to open (read) NV\n"); + DEBUG_PRINTF("Failed to open (read) NV\n"); #endif return rc; } @@ -4613,14 +4614,14 @@ int wolfTPM2_NVReadPublic(WOLFTPM2_DEV* dev, word32 nvIndex, rc = TPM2_NV_ReadPublic(&in, &out); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_NV_ReadPublic failed %d: %s\n", rc, + DEBUG_PRINTF("TPM2_NV_ReadPublic failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; } #ifdef DEBUG_WOLFTPM - printf("TPM2_NV_ReadPublic: Sz %d, Idx 0x%x, nameAlg %d, Attr 0x%x, " + DEBUG_PRINTF("TPM2_NV_ReadPublic: Sz %d, Idx 0x%x, nameAlg %d, Attr 0x%x, " "authPol %d, dataSz %d, name %d\n", out.nvPublic.size, (word32)out.nvPublic.nvPublic.nvIndex, @@ -4658,7 +4659,7 @@ int wolfTPM2_NVIncrement(WOLFTPM2_DEV* dev, WOLFTPM2_NV* nv) rc |= wolfTPM2_SetAuthHandleName(dev, 1, &nv->handle); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("Setting NV index name failed\n"); + DEBUG_PRINTF("Setting NV index name failed\n"); #endif return rc; } @@ -4668,14 +4669,14 @@ int wolfTPM2_NVIncrement(WOLFTPM2_DEV* dev, WOLFTPM2_NV* nv) in.nvIndex = nv->handle.hndl; #ifdef DEBUG_WOLFTPM - printf("TPM2_NV_Increment: Auth 0x%x, Idx 0x%x\n", + DEBUG_PRINTF("TPM2_NV_Increment: Auth 0x%x, Idx 0x%x\n", (word32)in.authHandle, (word32)in.nvIndex); #endif rc = TPM2_NV_Increment(&in); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_NV_Increment failed %d: %s\n", rc, + DEBUG_PRINTF("TPM2_NV_Increment failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; @@ -4706,7 +4707,7 @@ int wolfTPM2_NVWriteLock(WOLFTPM2_DEV* dev, WOLFTPM2_NV* nv) rc |= wolfTPM2_SetAuthHandleName(dev, 1, &nv->handle); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("Setting NV index name failed\n"); + DEBUG_PRINTF("Setting NV index name failed\n"); #endif return TPM_RC_FAILURE; } @@ -4740,14 +4741,14 @@ int wolfTPM2_NVDeleteAuth(WOLFTPM2_DEV* dev, WOLFTPM2_HANDLE* parent, rc = TPM2_NV_UndefineSpace(&in); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_NV_UndefineSpace failed %d: %s\n", rc, + DEBUG_PRINTF("TPM2_NV_UndefineSpace failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; } #ifdef DEBUG_WOLFTPM - printf("TPM2_NV_UndefineSpace: Auth 0x%x, Idx 0x%x\n", + DEBUG_PRINTF("TPM2_NV_UndefineSpace: Auth 0x%x, Idx 0x%x\n", (word32)in.authHandle, (word32)in.nvIndex); #endif @@ -4798,7 +4799,7 @@ int wolfTPM2_GetRandom(WOLFTPM2_DEV* dev, byte* buf, word32 len) rc = TPM2_GetRandom(&in, &out); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_GetRandom failed 0x%x: %s\n", rc, + DEBUG_PRINTF("TPM2_GetRandom failed 0x%x: %s\n", rc, TPM2_GetRCString(rc)); #endif break; @@ -4807,7 +4808,7 @@ int wolfTPM2_GetRandom(WOLFTPM2_DEV* dev, byte* buf, word32 len) sz = out.randomBytes.size; /* use actual returned size */ if (sz > MAX_RNG_REQ_SIZE) { #ifdef DEBUG_WOLFTPM - printf("wolfTPM2_GetRandom out size error\n"); + DEBUG_PRINTF("wolfTPM2_GetRandom out size error\n"); #endif rc = BAD_FUNC_ARG; break; @@ -4833,14 +4834,14 @@ int wolfTPM2_Clear(WOLFTPM2_DEV* dev) rc = TPM2_Clear(&in); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_Clear failed %d: %s\n", rc, + DEBUG_PRINTF("TPM2_Clear failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; } #ifdef DEBUG_WOLFTPM - printf("TPM2_Clear Auth 0x%x\n", (word32)in.authHandle); + DEBUG_PRINTF("TPM2_Clear Auth 0x%x\n", (word32)in.authHandle); #endif return rc; @@ -4874,7 +4875,7 @@ int wolfTPM2_HashStart(WOLFTPM2_DEV* dev, WOLFTPM2_HASH* hash, rc = TPM2_HashSequenceStart(&in, &out); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_HashSequenceStart failed 0x%x: %s\n", rc, + DEBUG_PRINTF("TPM2_HashSequenceStart failed 0x%x: %s\n", rc, TPM2_GetRCString(rc)); #endif return rc; @@ -4884,7 +4885,7 @@ int wolfTPM2_HashStart(WOLFTPM2_DEV* dev, WOLFTPM2_HASH* hash, hash->handle.hndl = out.sequenceHandle; #ifdef DEBUG_WOLFTPM - printf("wolfTPM2_HashStart: Handle 0x%x\n", + DEBUG_PRINTF("wolfTPM2_HashStart: Handle 0x%x\n", (word32)out.sequenceHandle); #endif @@ -4919,7 +4920,7 @@ int wolfTPM2_HashUpdate(WOLFTPM2_DEV* dev, WOLFTPM2_HASH* hash, rc = TPM2_SequenceUpdate(&in); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_SequenceUpdate failed 0x%x: %s\n", rc, + DEBUG_PRINTF("TPM2_SequenceUpdate failed 0x%x: %s\n", rc, TPM2_GetRCString(rc)); #endif return rc; @@ -4928,7 +4929,7 @@ int wolfTPM2_HashUpdate(WOLFTPM2_DEV* dev, WOLFTPM2_HASH* hash, } #ifdef DEBUG_WOLFTPM - printf("wolfTPM2_HashUpdate: Handle 0x%x, DataSz %d\n", + DEBUG_PRINTF("wolfTPM2_HashUpdate: Handle 0x%x, DataSz %d\n", (word32)in.sequenceHandle, dataSz); #endif @@ -4960,7 +4961,7 @@ int wolfTPM2_HashFinish(WOLFTPM2_DEV* dev, WOLFTPM2_HASH* hash, if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_SequenceComplete failed 0x%x: %s: Handle 0x%x\n", rc, + DEBUG_PRINTF("TPM2_SequenceComplete failed 0x%x: %s: Handle 0x%x\n", rc, TPM2_GetRCString(rc), (word32)in.sequenceHandle); #endif return rc; @@ -4972,7 +4973,7 @@ int wolfTPM2_HashFinish(WOLFTPM2_DEV* dev, WOLFTPM2_HASH* hash, XMEMCPY(digest, out.result.buffer, *digestSz); #ifdef DEBUG_WOLFTPM - printf("wolfTPM2_HashFinish: Handle 0x%x, DigestSz %d\n", + DEBUG_PRINTF("wolfTPM2_HashFinish: Handle 0x%x, DigestSz %d\n", (word32)in.sequenceHandle, *digestSz); #endif @@ -5115,7 +5116,7 @@ int wolfTPM2_LoadSymmetricKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, int alg, wolfTPM2_CopyPub(&key->pub, &loadExtIn.inPublic); #ifdef DEBUG_WOLFTPM - printf("wolfTPM2_LoadSymmetricKey: 0x%x\n", + DEBUG_PRINTF("wolfTPM2_LoadSymmetricKey: 0x%x\n", (word32)loadExtOut.objectHandle); #endif return rc; @@ -5125,7 +5126,7 @@ int wolfTPM2_LoadSymmetricKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, int alg, if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_LoadExternal: failed %d: %s\n", + DEBUG_PRINTF("TPM2_LoadExternal: failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; @@ -5186,7 +5187,7 @@ int wolfTPM2_EncryptDecryptBlock(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_EncryptDecrypt2 failed 0x%x: %s\n", rc, + DEBUG_PRINTF("TPM2_EncryptDecrypt2 failed 0x%x: %s\n", rc, TPM2_GetRCString(rc)); #endif return rc; @@ -5228,7 +5229,7 @@ int wolfTPM2_EncryptDecrypt(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, } #ifdef DEBUG_WOLFTPM - printf("wolfTPM2_EncryptDecrypt: 0x%x: %s, %d bytes\n", + DEBUG_PRINTF("wolfTPM2_EncryptDecrypt: 0x%x: %s, %d bytes\n", rc, TPM2_GetRCString(rc), inOutSz); #endif @@ -5254,7 +5255,7 @@ int wolfTPM2_SetCommand(WOLFTPM2_DEV* dev, TPM_CC commandCode, int enableFlag) rc = TPM2_SetCommandSet(&in); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_SetCommandSet failed 0x%x: %s\n", rc, + DEBUG_PRINTF("TPM2_SetCommandSet failed 0x%x: %s\n", rc, TPM2_GetRCString(rc)); #endif } @@ -5317,13 +5318,13 @@ int wolfTPM2_LoadKeyedHashKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, rc = TPM2_Create(&createIn, &createOut); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_Create key failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); + DEBUG_PRINTF("TPM2_Create key failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; } #ifdef DEBUG_WOLFTPM - printf("TPM2_Create key: pub %d, priv %d\n", createOut.outPublic.size, + DEBUG_PRINTF("TPM2_Create key: pub %d, priv %d\n", createOut.outPublic.size, createOut.outPrivate.size); TPM2_PrintPublicArea(&createOut.outPublic); #endif @@ -5339,7 +5340,7 @@ int wolfTPM2_LoadKeyedHashKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, rc = TPM2_Load(&loadIn, &loadOut); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_Load key failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); + DEBUG_PRINTF("TPM2_Load key failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; } @@ -5348,7 +5349,7 @@ int wolfTPM2_LoadKeyedHashKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, wolfTPM2_CopyName(&key->handle.name, &loadOut.name); #ifdef DEBUG_WOLFTPM - printf("wolfTPM2_LoadKeyedHashKey Key Handle 0x%x\n", + DEBUG_PRINTF("wolfTPM2_LoadKeyedHashKey Key Handle 0x%x\n", (word32)key->handle.hndl); #endif @@ -5394,7 +5395,7 @@ int wolfTPM2_HmacStart(WOLFTPM2_DEV* dev, WOLFTPM2_HMAC* hmac, rc = TPM2_HMAC_Start(&in, &out); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_HMAC_Start failed 0x%x: %s\n", rc, + DEBUG_PRINTF("TPM2_HMAC_Start failed 0x%x: %s\n", rc, TPM2_GetRCString(rc)); #endif return rc; @@ -5404,7 +5405,7 @@ int wolfTPM2_HmacStart(WOLFTPM2_DEV* dev, WOLFTPM2_HMAC* hmac, hmac->hash.handle.hndl = out.sequenceHandle; #ifdef DEBUG_WOLFTPM - printf("wolfTPM2_HmacStart: Handle 0x%x\n", + DEBUG_PRINTF("wolfTPM2_HmacStart: Handle 0x%x\n", (word32)out.sequenceHandle); #endif @@ -5458,7 +5459,7 @@ int wolfTPM2_Shutdown(WOLFTPM2_DEV* dev, int doStartup) rc = TPM2_Shutdown(&shutdownIn); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_Shutdown failed 0x%x: %s\n", rc, TPM2_GetRCString(rc)); + DEBUG_PRINTF("TPM2_Shutdown failed 0x%x: %s\n", rc, TPM2_GetRCString(rc)); #endif } @@ -5469,7 +5470,7 @@ int wolfTPM2_Shutdown(WOLFTPM2_DEV* dev, int doStartup) rc = TPM2_Startup(&startupIn); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_Startup failed %d: %s\n", + DEBUG_PRINTF("TPM2_Startup failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; @@ -5477,7 +5478,7 @@ int wolfTPM2_Shutdown(WOLFTPM2_DEV* dev, int doStartup) } #ifdef DEBUG_WOLFTPM - printf("wolfTPM2_Shutdown complete\n"); + DEBUG_PRINTF("wolfTPM2_Shutdown complete\n"); #endif return rc; @@ -5541,14 +5542,14 @@ int wolfTPM2_ChangePlatformAuth(WOLFTPM2_DEV* dev, WOLFTPM2_SESSION* session) } #ifdef DEBUG_WOLFTPM if (rc == 0) { - printf("Platform auth set to %d bytes of random\n", in.newAuth.size); + DEBUG_PRINTF("Platform auth set to %d bytes of random\n", in.newAuth.size); #ifdef WOLFTPM_DEBUG_VERBOSE - printf("\tAuth Sz %d\n", in.newAuth.size); + DEBUG_PRINTF("\tAuth Sz %d\n", in.newAuth.size); TPM2_PrintBin(in.newAuth.buffer, in.newAuth.size); #endif } else { - printf("Error %d setting platform auth! %s\n", + DEBUG_PRINTF("Error %d setting platform auth! %s\n", rc, wolfTPM2_GetRCString(rc)); } #endif @@ -6062,7 +6063,7 @@ int wolfTPM2_CreateKeySeal_ex(WOLFTPM2_DEV* dev, WOLFTPM2_KEYBLOB* keyBlob, /* Seal size is limited to TCG defined MAX_SYM_DATA, which is 128 bytes */ if (sealSize < 0 || sealSize > MAX_SYM_DATA) { #ifdef DEBUG_WOLFTPM - printf("Seal size %d should not be larger than %d bytes\n", + DEBUG_PRINTF("Seal size %d should not be larger than %d bytes\n", sealSize, MAX_SYM_DATA); #endif return BAD_FUNC_ARG; @@ -6098,14 +6099,14 @@ int wolfTPM2_CreateKeySeal_ex(WOLFTPM2_DEV* dev, WOLFTPM2_KEYBLOB* keyBlob, rc = TPM2_Create(&createIn, &createOut); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("wolfTPM2_CreateKeySeal failed %d: %s\n", + DEBUG_PRINTF("wolfTPM2_CreateKeySeal failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; } #ifdef DEBUG_WOLFTPM - printf("wolfTPM2_CreateKeySeal generated key with: pub %d, priv %d\n", + DEBUG_PRINTF("wolfTPM2_CreateKeySeal generated key with: pub %d, priv %d\n", createOut.outPublic.size, createOut.outPrivate.size); TPM2_PrintPublicArea(&createOut.outPublic); #endif @@ -6146,7 +6147,7 @@ int wolfTPM2_GetTime(WOLFTPM2_KEY* aikKey, GetTime_Out* getTimeOut) rc = TPM2_GetTime(&getTimeCmd, getTimeOut); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_GetTime failed 0x%x: %s\n", rc, wolfTPM2_GetRCString(rc)); + DEBUG_PRINTF("TPM2_GetTime failed 0x%x: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif } @@ -6479,7 +6480,7 @@ static int CSR_MakeAndSign(WOLFTPM2_DEV* dev, WOLFTPM2_CSR* csr, CSRKey* key, } #else #ifdef DEBUG_WOLFTPM - printf("CSR_MakeAndSign PEM not supported\n") + DEBUG_PRINTF("CSR_MakeAndSign PEM not supported\n") #endif rc = NOT_COMPILED_IN; #endif @@ -6559,7 +6560,7 @@ static int CSR_KeySetup(WOLFTPM2_DEV* dev, WOLFTPM2_CSR* csr, WOLFTPM2_KEY* key, } else { #ifdef DEBUG_WOLFTPM - printf("CSR_KeySetup invalid input key\n"); + DEBUG_PRINTF("CSR_KeySetup invalid input key\n"); #endif rc = BAD_FUNC_ARG; } @@ -6670,7 +6671,7 @@ int wolfTPM2_CSR_SetKeyUsage(WOLFTPM2_DEV* dev, WOLFTPM2_CSR* csr, #else if (keyUsage != NULL) { #ifdef DEBUG_WOLFTPM - printf("CSR_Generate key usage supplied, but not available\n"); + DEBUG_PRINTF("CSR_Generate key usage supplied, but not available\n"); #endif rc = NOT_COMPILED_IN; } @@ -6865,7 +6866,7 @@ int wolfTPM2_PolicyAuthorize(WOLFTPM2_DEV* dev, TPM_HANDLE sessionHandle, } #ifdef DEBUG_WOLFTPM if (rc != TPM_RC_SUCCESS) { - printf("PolicyAuthorize failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); + DEBUG_PRINTF("PolicyAuthorize failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); } #endif return rc; @@ -6911,12 +6912,12 @@ int wolfTPM2_PCRGetDigest(WOLFTPM2_DEV* dev, TPM_ALG_ID pcrAlg, #ifdef DEBUG_WOLFTPM if (rc != 0) { - printf("wolfTPM2_PCRGetDigest failed %d: %s\n", + DEBUG_PRINTF("wolfTPM2_PCRGetDigest failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); } #ifdef WOLFTPM_DEBUG_VERBOSE else { - printf("wolfTPM2_PCRGetDigest: %d\n", *pcrDigestSz); + DEBUG_PRINTF("wolfTPM2_PCRGetDigest: %d\n", *pcrDigestSz); TPM2_PrintBin(pcrDigest, *pcrDigestSz); } #endif @@ -6987,12 +6988,12 @@ int wolfTPM2_PolicyPCRMake(TPM_ALG_ID pcrAlg, byte* pcrArray, word32 pcrArraySz, #ifdef DEBUG_WOLFTPM if (rc != 0) { - printf("wolfTPM2_PolicyPCRMake failed %d: %s\n", + DEBUG_PRINTF("wolfTPM2_PolicyPCRMake failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); } #ifdef WOLFTPM_DEBUG_VERBOSE else { - printf("wolfTPM2_PolicyPCRMake: %d\n", *digestSz); + DEBUG_PRINTF("wolfTPM2_PolicyPCRMake: %d\n", *digestSz); TPM2_PrintBin(digest, *digestSz); } #endif @@ -7043,12 +7044,12 @@ int wolfTPM2_PolicyRefMake(TPM_ALG_ID pcrAlg, byte* digest, word32* digestSz, #ifdef DEBUG_WOLFTPM if (rc != 0) { - printf("wolfTPM_PolicyRefMake failed %d: %s\n", + DEBUG_PRINTF("wolfTPM_PolicyRefMake failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); } #ifdef WOLFTPM_DEBUG_VERBOSE else { - printf("wolfTPM_PolicyRefMake: %d\n", *digestSz); + DEBUG_PRINTF("wolfTPM_PolicyRefMake: %d\n", *digestSz); TPM2_PrintBin(digest, *digestSz); } #endif @@ -7114,12 +7115,12 @@ int wolfTPM2_PolicyAuthorizeMake(TPM_ALG_ID pcrAlg, #ifdef DEBUG_WOLFTPM if (rc != 0) { - printf("wolfTPM2_PolicyAuthorizeMake failed %d: %s\n", + DEBUG_PRINTF("wolfTPM2_PolicyAuthorizeMake failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); } #ifdef WOLFTPM_DEBUG_VERBOSE else { - printf("wolfTPM2_PolicyAuthorizeMake: %d\n", *digestSz); + DEBUG_PRINTF("wolfTPM2_PolicyAuthorizeMake: %d\n", *digestSz); TPM2_PrintBin(digest, *digestSz); } #endif @@ -7161,7 +7162,7 @@ int wolfTPM2_SetIdentityAuth(WOLFTPM2_DEV* dev, WOLFTPM2_HANDLE* handle, rc = TPM2_GetProductInfo(serialNum, (uint16_t)sizeof(serialNum)); if (rc != 0) { #ifdef DEBUG_WOLFTPM - printf("TPM2_GetProductInfo failed %d: %s\n", + DEBUG_PRINTF("TPM2_GetProductInfo failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif return rc; @@ -7254,7 +7255,7 @@ static int tpm2_ifx_firmware_enable_policy(WOLFTPM2_DEV* dev) #ifdef DEBUG_WOLFTPM if (rc != TPM_RC_SUCCESS) { - printf("Enable firmware start policy failed 0x%x: %s\n", + DEBUG_PRINTF("Enable firmware start policy failed 0x%x: %s\n", rc, TPM2_GetRCString(rc)); } #endif @@ -7310,7 +7311,7 @@ static int tpm2_ifx_firmware_start(WOLFTPM2_DEV* dev, TPM_ALG_ID hashAlg, } #ifdef DEBUG_WOLFTPM if (rc != TPM_RC_SUCCESS) { - printf("Firmware upgrade start failed 0x%x: %s\n", + DEBUG_PRINTF("Firmware upgrade start failed 0x%x: %s\n", rc, TPM2_GetRCString(rc)); } #endif @@ -7339,7 +7340,7 @@ static int tpm2_ifx_firmware_manifest(WOLFTPM2_DEV* dev, state = 0; } #ifdef DEBUG_WOLFTPM - printf("Firmware manifest chunk %u offset (%u / %u), state %d\n", + DEBUG_PRINTF("Firmware manifest chunk %u offset (%u / %u), state %d\n", chunk_sz, offset, manifest_sz, state); #endif @@ -7356,7 +7357,7 @@ static int tpm2_ifx_firmware_manifest(WOLFTPM2_DEV* dev, } #ifdef DEBUG_WOLFTPM if (rc != TPM_RC_SUCCESS) { - printf("Firmware upgrade manifest failed 0x%x: %s\n", + DEBUG_PRINTF("Firmware upgrade manifest failed 0x%x: %s\n", rc, TPM2_GetRCString(rc)); } #endif @@ -7383,19 +7384,19 @@ static int tpm2_ifx_firmware_data(WOLFTPM2_DEV* dev, } else if (rc == 0) { #ifdef DEBUG_WOLFTPM - printf("Firmware data done\n"); + DEBUG_PRINTF("Firmware data done\n"); #endif break; } else { #ifdef DEBUG_WOLFTPM - printf("Firmware data callback error! %d\n", rc); + DEBUG_PRINTF("Firmware data callback error! %d\n", rc); #endif break; } #ifdef DEBUG_WOLFTPM - printf("Firmware data chunk offset %u\n", offset); + DEBUG_PRINTF("Firmware data chunk offset %u\n", offset); #endif val16 = be16_to_cpu(chunk_sz); @@ -7420,7 +7421,7 @@ static int tpm2_ifx_firmware_data(WOLFTPM2_DEV* dev, } #ifdef DEBUG_WOLFTPM else { - printf("Firmware upgrade data failed 0x%x: %s\n", + DEBUG_PRINTF("Firmware upgrade data failed 0x%x: %s\n", rc, TPM2_GetRCString(rc)); } #endif @@ -7442,7 +7443,7 @@ static int tpm2_ifx_firmware_final(WOLFTPM2_DEV* dev) cmd, sizeof(cmd)); #ifdef DEBUG_WOLFTPM if (rc != TPM_RC_SUCCESS) { - printf("Firmware finalize failed 0x%x: %s\n", + DEBUG_PRINTF("Firmware finalize failed 0x%x: %s\n", rc, TPM2_GetRCString(rc)); } #endif @@ -7463,7 +7464,7 @@ int wolfTPM2_FirmwareUpgradeHash(WOLFTPM2_DEV* dev, TPM_ALG_ID hashAlg, if (caps.opMode == 0x03) { /* firmware update is done, just needs finalized and TPM reset */ #ifdef DEBUG_WOLFTPM - printf("Firmware update done, finalizing\n"); + DEBUG_PRINTF("Firmware update done, finalizing\n"); #endif return tpm2_ifx_firmware_final(dev); } @@ -7486,7 +7487,7 @@ int wolfTPM2_FirmwareUpgradeHash(WOLFTPM2_DEV* dev, TPM_ALG_ID hashAlg, } #ifdef DEBUG_WOLFTPM if (rc != TPM_RC_SUCCESS) { - printf("Firmware update failed 0x%x: %s\n", + DEBUG_PRINTF("Firmware update failed 0x%x: %s\n", rc, TPM2_GetRCString(rc)); } #endif @@ -7540,7 +7541,7 @@ int wolfTPM2_FirmwareUpgradeCancel(WOLFTPM2_DEV* dev) cmd, sizeof(cmd)); #ifdef DEBUG_WOLFTPM if (rc != TPM_RC_SUCCESS) { - printf("Firmware abandon failed 0x%x: %s\n", + DEBUG_PRINTF("Firmware abandon failed 0x%x: %s\n", rc, TPM2_GetRCString(rc)); } #endif diff --git a/wolftpm/tpm2.h b/wolftpm/tpm2.h index 8332b253..4e976d91 100644 --- a/wolftpm/tpm2.h +++ b/wolftpm/tpm2.h @@ -1664,8 +1664,18 @@ static const BYTE TPM_20_EK_AUTH_POLICY[] = { struct TPM2_CTX; #ifdef WOLFTPM_SWTPM +#if defined(WOLFTPM_SWTPM_UARTNS550) + #include "xparameters.h" + #include "xuartns550.h" +#endif + struct wolfTPM_tcpContext { +#if defined(WOLFTPM_SWTPM_UARTNS550) + XUartNs550 fd; + int setup; +#else int fd; +#endif }; #endif /* WOLFTPM_SWTPM */ diff --git a/wolftpm/tpm2_types.h b/wolftpm/tpm2_types.h index 64d7f36c..87a6a647 100644 --- a/wolftpm/tpm2_types.h +++ b/wolftpm/tpm2_types.h @@ -75,6 +75,14 @@ typedef int64_t INT64; #if defined(WOLFTPM_DEBUG_VERBOSE) && !defined(DEBUG_WOLFTPM) #define DEBUG_WOLFTPM #endif +#ifndef DEBUG_PRINTF + #if defined(__MICROBLAZE__) + /* using xil_printf instead of printf reduces memory footprint size */ + #define DEBUG_PRINTF xil_printf + #else + #define DEBUG_PRINTF printf + #endif +#endif /* ---------------------------------------------------------------------------*/ /* WOLFCRYPT */ @@ -212,7 +220,7 @@ typedef int64_t INT64; #define THREAD_LS_T __declspec(thread) /* Thread local storage only in FreeRTOS v8.2.1 and higher */ #elif defined(FREERTOS) || defined(FREERTOS_TCP) || \ - defined(WOLFSSL_ZEPHYR) + defined(WOLFSSL_ZEPHYR) || defined(__MICROBLAZE__) #define THREAD_LS_T #else #define THREAD_LS_T __thread