diff --git a/configure.ac b/configure.ac index 5681eacf..3a6f761d 100644 --- a/configure.ac +++ b/configure.ac @@ -243,6 +243,11 @@ then 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 diff --git a/docs/SWTPM.md b/docs/SWTPM.md index efa2ecf4..ab736309 100644 --- a/docs/SWTPM.md +++ b/docs/SWTPM.md @@ -30,13 +30,28 @@ make ## Build Options * `WOLFTPM_SWTPM`: Use socket transport (no TIS layer) -* `TPM2_SWTPM_HOST`: The host TPM address (default=localhost) -* `TPM2_SWTPM_PORT`: The socket port (default=2321) +* `TPM2_SWTPM_HOST`: The serial device to use (default=/dev/ttyS0) +* `TPM2_SWTPM_PORT`: The baud rate (default=115200) * `WOLFTPM_SWTPM_UART`: Use UART transport (no TIS layer) - ## SWTPM simulator setup +### Xilinx UART + +Alternatively for raw API calls with Xilinx + +```sh +./cofnigure --enable-swtpm=uartns550 +make +``` + +## Build Options + +* `WOLFTPM_SWTPM`: Use socket transport (no TIS layer) +* `TPM2_SWTPM_PORT`: Used as the default baud rate (default=115200) +* `TPM2_SWTPM_HOST`: The device to connect with (default=XPAR_MB0_AXI_UART16550_2_DEVICE_ID) +* `WOLFTPM_SWTPM_UARTNS550`: Use Xilinx UART transport (no TIS layer) + ### ibmswtpm2 Checkout and Build diff --git a/src/tpm2.c b/src/tpm2.c index 75f7ec70..128de744 100644 --- a/src/tpm2.c +++ b/src/tpm2.c @@ -34,7 +34,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 @@ -66,7 +72,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; } @@ -142,7 +148,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; @@ -197,7 +203,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; } @@ -209,7 +215,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; } @@ -219,7 +225,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; } @@ -230,7 +236,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; } @@ -281,7 +287,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 @@ -315,7 +321,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; } @@ -326,7 +332,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; } @@ -335,7 +341,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; } @@ -351,7 +357,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; } @@ -391,7 +397,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); @@ -590,7 +596,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 @@ -858,7 +864,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; @@ -5405,7 +5411,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 @@ -5430,7 +5436,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) { @@ -5440,7 +5446,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; @@ -5485,7 +5491,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; @@ -5537,6 +5543,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) { @@ -5544,7 +5551,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 */ @@ -5725,6 +5732,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) @@ -5894,7 +5904,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; @@ -6011,7 +6021,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; } @@ -6036,7 +6046,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; } @@ -6067,7 +6077,7 @@ void TPM2_PrintBin(const byte* buffer, word32 length) word32 i, sz; if (!buffer) { - printf("\tNULL\n"); + DEBUG_PRINTF("\tNULL\n"); return; } @@ -6076,21 +6086,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; @@ -6102,25 +6112,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, @@ -6132,7 +6142,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), @@ -6143,7 +6153,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, @@ -6155,18 +6165,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); @@ -6175,20 +6185,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), @@ -6202,7 +6212,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 dc05d62d..0770774c 100644 --- a/src/tpm2_swtpm.c +++ b/src/tpm2_swtpm.c @@ -46,7 +46,8 @@ #include /* standard in/out procedures */ #include /* defines system calls */ -#ifdef WOLFTPM_SWTPM_UART + +#if defined(WOLFTPM_SWTPM_UART) #define _XOPEN_SOURCE 600 #include #include /* used for all socket calls */ @@ -62,15 +63,33 @@ #ifdef __MACH__ #define TPM2_SWTPM_HOST "/dev/cu.usbserial-0001" #else - #define TPM2_SWTPM_HOST "/dev/ttyUSB0" + #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 60 + #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 @@ -83,6 +102,9 @@ #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 */ @@ -91,23 +113,46 @@ 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; + } + +#if !defined(WOLFTPM_SWTPM_UARTNS550) + if (ctx->tcpCtx.fd < 0) { return BAD_FUNC_ARG; } +#endif #ifdef DEBUG_SWTPM_IO - printf("Write %zd\n", bufSz); + DEBUG_PRINTF("Write %zd\n\r", bufSz); TPM2_PrintBin(buffer, (word32)bufSz); #endif - wrc = write(ctx->tcpCtx.fd, buffer, bufSz); - if (bufSz != wrc) { - rc = TPM_RC_FAILURE; +#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 @@ -115,6 +160,7 @@ static TPM_RC SwTpmTransmit(TPM2_CTX* ctx, const void* buffer, ssize_t bufSz) return rc; } +#if !defined(WOLFTPM_SWTPM_UARTNS550) static int SwTpmReceive(TPM2_CTX* ctx, void* buffer, size_t rxSz) { int rc; @@ -129,30 +175,34 @@ static int SwTpmReceive(TPM2_CTX* ctx, void* buffer, size_t rxSz) FD_ZERO(&rfds); FD_SET(ctx->tcpCtx.fd, &rfds); + remain = rxSz; - remain = rxSz; - while (remain > 0) { + do { /* use select to wait for data */ rc = select(ctx->tcpCtx.fd + 1, &rfds, NULL, NULL, &tv); if (rc == 0) { rc = SOCKET_ERROR_E; /* timeout */ break; } - rc = (int)read(ctx->tcpCtx.fd, ptr, remain); + rc = (int)WOLFTPM_READ(ctx->tcpCtx.fd, ptr, remain); #ifdef DEBUG_SWTPM_IO - printf("Read asked %zd, got %d\n", remain, rc); + 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) { - #ifdef WOLFTPM_SWTPM_UART - continue; /* keep trying */ - #else - printf("Failed to read from TPM socket: EOF\n"); - #endif + 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 @@ -167,16 +217,18 @@ static int SwTpmReceive(TPM2_CTX* ctx, void* buffer, size_t rxSz) ptr += rc; #ifdef WOLFTPM_DEBUG_VERBOSE - printf("TPM socket received %d waiting for %zu more\n", + DEBUG_PRINTF("TPM socket received %d waiting for %zd more\n\r", rc, remain); #endif - } - if (remain == 0) { + } 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) @@ -192,7 +244,7 @@ static int SwTpmConnect(TPM2_CTX* ctx, const char* uartDev, uint32_t baud) fd = open(uartDev, O_RDWR | O_NOCTTY); if (fd < 0) { #ifdef DEBUG_WOLFTPM - printf("Error opening %s: Error %i (%s)\n", + DEBUG_PRINTF("Error opening %s: Error %i (%s)\n", uartDev, errno, strerror(errno)); #endif return SOCKET_ERROR_E; @@ -219,6 +271,150 @@ static int SwTpmConnect(TPM2_CTX* ctx, const char* uartDev, uint32_t baud) 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 = SOCKET_ERROR_E; /* 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 = SOCKET_ERROR_E; + 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 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) { @@ -261,7 +457,7 @@ static int 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 @@ -274,29 +470,37 @@ 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; + } + +#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 if (rc != TPM_RC_SUCCESS) { - printf("Failed to transmit SESSION_END\n"); + DEBUG_PRINTF("Failed to transmit SESSION_END\n"); } #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 =" + DEBUG_PRINTF("Failed to close fd %d, got errno %d =" "%s\n", ctx->tcpCtx.fd, errno, strerror(errno)); #endif } +#if !defined(WOLFTPM_SWTPM_UARTNS550) ctx->tcpCtx.fd = -1; +#endif return rc; } @@ -307,7 +511,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; @@ -315,12 +519,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 @@ -352,7 +563,7 @@ int TPM2_SWTPM_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet) rspSz = TPM2_Packet_SwapU32(tss_word); if (rspSz > packet->size) { #ifdef WOLFTPM_DEBUG_VERBOSE - printf("Response size(%d) larger than command buffer(%d)\n", + DEBUG_PRINTF("Response size(%d) larger than command buffer(%d)\n", rspSz, packet->pos); #endif rc = SOCKET_ERROR_E; @@ -372,19 +583,23 @@ int TPM2_SWTPM_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet) tss_word = TPM2_Packet_SwapU32(tss_word); #ifdef WOLFTPM_DEBUG if (tss_word != 0) { - printf("SWTPM ack %d\n", tss_word); + DEBUG_PRINTF("SWTPM ack %d\n", tss_word); } #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); } #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 d2257dd3..346bfafd 100644 --- a/src/tpm2_wrap.c +++ b/src/tpm2_wrap.c @@ -71,12 +71,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, @@ -91,12 +91,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) @@ -106,12 +106,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; @@ -330,7 +330,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; } @@ -361,7 +361,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 @@ -388,7 +388,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; } @@ -421,10 +421,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 @@ -447,13 +447,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; } @@ -464,7 +464,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; } @@ -482,7 +482,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; } @@ -493,7 +493,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; } @@ -503,7 +503,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; @@ -549,7 +549,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; } @@ -585,19 +585,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; @@ -714,7 +714,7 @@ static int wolfTPM2_GetCapabilities_NoDev(WOLFTPM2_CAPS* cap) rc = TPM2_GetCapability(&in, &out); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_GetCapability failed 0x%x: %s\n", rc, + DEBUG_PRINTF("TPM2_GetCapability failed 0x%x: %s\n", rc, TPM2_GetRCString(rc)); #endif return rc; @@ -731,7 +731,7 @@ static int wolfTPM2_GetCapabilities_NoDev(WOLFTPM2_CAPS* cap) rc = TPM2_GetCapability(&in, &out); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM - printf("TPM2_GetCapability failed 0x%x: %s\n", rc, + DEBUG_PRINTF("TPM2_GetCapability failed 0x%x: %s\n", rc, TPM2_GetRCString(rc)); #endif return rc; @@ -925,7 +925,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 */ @@ -935,7 +935,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 } @@ -964,7 +964,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 */ @@ -1292,7 +1292,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 @@ -1390,7 +1390,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; } @@ -1408,7 +1408,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; @@ -1418,7 +1418,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; @@ -1444,7 +1444,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; } @@ -1452,7 +1452,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 @@ -1476,7 +1476,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 @@ -1522,7 +1522,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; @@ -1536,7 +1536,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 @@ -1571,7 +1571,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; @@ -1591,7 +1591,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; } @@ -1600,7 +1600,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 @@ -1645,13 +1645,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 @@ -1687,7 +1687,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; } @@ -1695,7 +1695,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; @@ -1752,13 +1752,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 @@ -1794,7 +1794,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; @@ -1806,7 +1806,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; @@ -1918,7 +1918,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; } @@ -1968,7 +1968,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; } @@ -1986,7 +1986,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; } @@ -1997,7 +1997,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; } @@ -2024,7 +2024,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; } @@ -2090,7 +2090,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; @@ -2102,7 +2102,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; @@ -2114,7 +2114,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; @@ -2122,7 +2122,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; @@ -2191,7 +2191,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; @@ -2280,7 +2280,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; @@ -2432,7 +2432,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; @@ -2507,7 +2507,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; } @@ -2519,7 +2519,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); @@ -2859,7 +2859,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; @@ -3354,22 +3354,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 @@ -3406,14 +3406,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 @@ -3463,7 +3463,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; } @@ -3474,7 +3474,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; @@ -3491,7 +3491,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; @@ -3501,7 +3501,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 @@ -3620,7 +3620,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 } @@ -3631,7 +3631,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; @@ -3719,7 +3720,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; @@ -3732,7 +3733,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 @@ -3770,7 +3771,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; @@ -3781,7 +3782,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; @@ -3805,7 +3806,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; @@ -3856,7 +3857,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; @@ -3867,7 +3868,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; @@ -3907,7 +3908,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; @@ -3917,7 +3918,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; @@ -3956,7 +3957,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; @@ -3966,7 +3967,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; @@ -4005,7 +4006,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; } @@ -4015,7 +4016,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 @@ -4049,12 +4050,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; @@ -4079,14 +4080,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; @@ -4128,12 +4129,12 @@ int wolfTPM2_NVCreateAuth(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; @@ -4148,7 +4149,7 @@ int wolfTPM2_NVCreateAuth(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, @@ -4198,7 +4199,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; } @@ -4219,7 +4220,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; @@ -4234,7 +4235,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 @@ -4280,7 +4281,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; } @@ -4300,7 +4301,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; @@ -4312,7 +4313,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 @@ -4361,7 +4362,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; } @@ -4397,14 +4398,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, @@ -4442,7 +4443,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; } @@ -4452,14 +4453,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; @@ -4524,14 +4525,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 @@ -4582,7 +4583,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; @@ -4591,7 +4592,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; @@ -4617,14 +4618,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; @@ -4658,7 +4659,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; @@ -4668,7 +4669,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 @@ -4703,7 +4704,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; @@ -4712,7 +4713,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 @@ -4744,7 +4745,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; @@ -4756,7 +4757,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 @@ -4899,7 +4900,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; @@ -4909,7 +4910,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; @@ -4967,7 +4968,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; @@ -5009,7 +5010,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 @@ -5032,7 +5033,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 } @@ -5095,13 +5096,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 @@ -5117,7 +5118,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; } @@ -5126,7 +5127,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 @@ -5172,7 +5173,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; @@ -5182,7 +5183,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 @@ -5236,7 +5237,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 } @@ -5247,7 +5248,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; @@ -5255,7 +5256,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; @@ -5824,7 +5825,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; @@ -5860,14 +5861,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 @@ -5908,7 +5909,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 } @@ -6241,7 +6242,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 @@ -6321,7 +6322,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; } @@ -6432,7 +6433,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; } diff --git a/wolftpm/tpm2.h b/wolftpm/tpm2.h index 63288ae1..ab3ad0a5 100644 --- a/wolftpm/tpm2.h +++ b/wolftpm/tpm2.h @@ -1653,8 +1653,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 7f4b93da..f97d737b 100644 --- a/wolftpm/tpm2_types.h +++ b/wolftpm/tpm2_types.h @@ -79,6 +79,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 */ @@ -217,7 +225,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