diff --git a/examples/csr/csr.c b/examples/csr/csr.c old mode 100644 new mode 100755 index 6ff07b7..8935ea9 --- a/examples/csr/csr.c +++ b/examples/csr/csr.c @@ -32,6 +32,8 @@ #include #include +#include + static const char* gClientCertRsaFile = "./certs/tpm-rsa-cert.csr"; static const char* gClientCertEccFile = "./certs/tpm-ecc-cert.csr"; diff --git a/examples/native/native_test.c b/examples/native/native_test.c old mode 100644 new mode 100755 index c596ef9..83360b3 --- a/examples/native/native_test.c +++ b/examples/native/native_test.c @@ -27,6 +27,8 @@ #include #include +#include + /******************************************************************************/ /* --- BEGIN TPM Native API Tests -- */ /******************************************************************************/ @@ -426,7 +428,7 @@ int TPM2_Native_Test(void* userCtx) goto exit; } sessionHandle = cmdOut.authSes.sessionHandle; - printf("TPM2_StartAuthSession: sessionHandle 0x%x\n", sessionHandle); + printf("TPM2_StartAuthSession: sessionHandle 0x%x\n", (word32)sessionHandle); /* Policy Get Digest */ @@ -513,7 +515,7 @@ int TPM2_Native_Test(void* userCtx) goto exit; } handle = cmdOut.hashSeqStart.sequenceHandle; - printf("TPM2_HashSequenceStart: sequenceHandle 0x%x\n", handle); + printf("TPM2_HashSequenceStart: sequenceHandle 0x%x\n", (word32)handle); /* set auth for hashing handle */ session[0].auth.size = sizeof(usageAuth)-1; @@ -598,7 +600,7 @@ int TPM2_Native_Test(void* userCtx) endorse.name = cmdOut.createPri.name; endorse.symmetric = cmdIn.createPri.inPublic.publicArea.parameters.rsaDetail.symmetric; printf("TPM2_CreatePrimary: Endorsement 0x%x (%d bytes)\n", - endorse.handle, endorse.pub.size); + (word32)endorse.handle, endorse.pub.size); /* Create Primary (Storage) */ @@ -630,7 +632,7 @@ int TPM2_Native_Test(void* userCtx) storage.pub = cmdOut.createPri.outPublic; storage.name = cmdOut.createPri.name; printf("TPM2_CreatePrimary: Storage 0x%x (%d bytes)\n", - storage.handle, storage.pub.size); + (word32)storage.handle, storage.pub.size); #if 0 /* Move new primary key into NV to persist */ @@ -657,7 +659,7 @@ int TPM2_Native_Test(void* userCtx) goto exit; } handle = cmdOut.loadExt.objectHandle; - printf("TPM2_LoadExternal: 0x%x\n", handle); + printf("TPM2_LoadExternal: 0x%x\n", (word32)handle); /* Make a credential */ XMEMSET(&cmdIn.makeCred, 0, sizeof(cmdIn.makeCred)); @@ -687,7 +689,7 @@ int TPM2_Native_Test(void* userCtx) goto exit; } printf("TPM2_ReadPublic Handle 0x%x: pub %d, name %d, qualifiedName %d\n", - cmdIn.readPub.objectHandle, + (word32)cmdIn.readPub.objectHandle, cmdOut.readPub.outPublic.size, cmdOut.readPub.name.size, cmdOut.readPub.qualifiedName.size); @@ -736,7 +738,7 @@ int TPM2_Native_Test(void* userCtx) goto exit; } hmacKey.handle = cmdOut.load.objectHandle; - printf("TPM2_Load New HMAC Key Handle 0x%x\n", hmacKey.handle); + printf("TPM2_Load New HMAC Key Handle 0x%x\n", (word32)hmacKey.handle); /* set auth for HMAC handle */ session[0].auth.size = sizeof(usageAuth)-1; @@ -856,7 +858,7 @@ int TPM2_Native_Test(void* userCtx) goto exit; } eccKey.handle = cmdOut.load.objectHandle; - printf("TPM2_Load ECDSA Key Handle 0x%x\n", eccKey.handle); + printf("TPM2_Load ECDSA Key Handle 0x%x\n", (word32)eccKey.handle); /* set session auth for ecc key */ session[0].auth.size = sizeof(usageAuth)-1; @@ -942,7 +944,7 @@ int TPM2_Native_Test(void* userCtx) goto exit; } eccKey.handle = cmdOut.load.objectHandle; - printf("TPM2_Load ECDH Key Handle 0x%x\n", eccKey.handle); + printf("TPM2_Load ECDH Key Handle 0x%x\n", (word32)eccKey.handle); /* set session auth for ecc key */ session[0].auth.size = sizeof(usageAuth)-1; @@ -1034,7 +1036,7 @@ int TPM2_Native_Test(void* userCtx) goto exit; } rsaKey.handle = cmdOut.load.objectHandle; - printf("TPM2_Load RSA Key Handle 0x%x\n", rsaKey.handle); + printf("TPM2_Load RSA Key Handle 0x%x\n", (word32)rsaKey.handle); /* set session auth for RSA key */ session[0].auth.size = sizeof(usageAuth)-1; @@ -1110,7 +1112,7 @@ int TPM2_Native_Test(void* userCtx) TPM2_GetRCString(rc)); goto exit; } - printf("TPM2_NV_DefineSpace: 0x%x\n", nvIndex); + printf("TPM2_NV_DefineSpace: 0x%x\n", (word32)nvIndex); /* Read NV */ XMEMSET(&cmdIn.nvReadPub, 0, sizeof(cmdIn.nvReadPub)); @@ -1124,9 +1126,9 @@ int TPM2_Native_Test(void* userCtx) printf("TPM2_NV_ReadPublic: Sz %d, Idx 0x%x, nameAlg %d, Attr 0x%x, " "authPol %d, dataSz %d, name %d\n", cmdOut.nvReadPub.nvPublic.size, - cmdOut.nvReadPub.nvPublic.nvPublic.nvIndex, + (word32)cmdOut.nvReadPub.nvPublic.nvPublic.nvIndex, cmdOut.nvReadPub.nvPublic.nvPublic.nameAlg, - cmdOut.nvReadPub.nvPublic.nvPublic.attributes, + (word32)cmdOut.nvReadPub.nvPublic.nvPublic.attributes, cmdOut.nvReadPub.nvPublic.nvPublic.authPolicy.size, cmdOut.nvReadPub.nvPublic.nvPublic.dataSize, cmdOut.nvReadPub.nvName.size); @@ -1209,7 +1211,7 @@ int TPM2_Native_Test(void* userCtx) goto exit; } aesKey.handle = cmdOut.load.objectHandle; - printf("TPM2_Load New AES Key Handle 0x%x\n", aesKey.handle); + printf("TPM2_Load New AES Key Handle 0x%x\n", (word32)aesKey.handle); /* set auth for AES handle */ session[0].auth.size = sizeof(usageAuth)-1; diff --git a/examples/pkcs7/pkcs7.c b/examples/pkcs7/pkcs7.c old mode 100644 new mode 100755 index 1f86aa8..1836ce3 --- a/examples/pkcs7/pkcs7.c +++ b/examples/pkcs7/pkcs7.c @@ -32,6 +32,8 @@ #include #include +#include + /* Sign PKCS7 using TPM based key: * Must Run: * 1. `./examples/csr/csr` diff --git a/examples/tls/tls_client.c b/examples/tls/tls_client.c old mode 100644 new mode 100755 index 1fa2cda..460604c --- a/examples/tls/tls_client.c +++ b/examples/tls/tls_client.c @@ -40,6 +40,8 @@ #define USE_CERT_BUFFERS_256 #include +#include + #ifdef TLS_BENCH_MODE double benchStart; #endif diff --git a/examples/tls/tls_client_notpm.c b/examples/tls/tls_client_notpm.c old mode 100644 new mode 100755 index f66976f..da35379 --- a/examples/tls/tls_client_notpm.c +++ b/examples/tls/tls_client_notpm.c @@ -40,6 +40,8 @@ #define USE_CERT_BUFFERS_256 #include +#include + #ifdef TLS_BENCH_MODE double benchStart; #endif diff --git a/examples/tls/tls_common.h b/examples/tls/tls_common.h old mode 100644 new mode 100755 index 1bda223..43dce7e --- a/examples/tls/tls_common.h +++ b/examples/tls/tls_common.h @@ -34,6 +34,8 @@ #include +#include + #ifdef __cplusplus extern "C" { #endif diff --git a/examples/tls/tls_server.c b/examples/tls/tls_server.c old mode 100644 new mode 100755 index 6abde5f..ad2e820 --- a/examples/tls/tls_server.c +++ b/examples/tls/tls_server.c @@ -34,6 +34,8 @@ #include +#include + #ifdef TLS_BENCH_MODE double benchStart; #endif diff --git a/examples/wrap/wrap_test.c b/examples/wrap/wrap_test.c old mode 100644 new mode 100755 index 41860bd..0568085 --- a/examples/wrap/wrap_test.c +++ b/examples/wrap/wrap_test.c @@ -30,6 +30,8 @@ #include #include +#include + /* Configuration */ #define TPM2_DEMO_NV_TEST_INDEX 0x01800200 #define TPM2_DEMO_NV_TEST_SIZE 1024 /* max size on Infineon SLB9670 is 1664 */ diff --git a/src/tpm2.c b/src/tpm2.c index f34350d..aced554 100755 --- a/src/tpm2.c +++ b/src/tpm2.c @@ -245,6 +245,11 @@ TPM2_CTX* TPM2_GetActiveCtx(void) return gActiveTPM; } +void TPM2_SetActiveCtx(TPM2_CTX* ctx) +{ + gActiveTPM = ctx; +} + TPM_RC TPM2_SetSessionAuth(TPMS_AUTH_COMMAND* cmd) { TPM_RC rc; @@ -261,7 +266,51 @@ TPM_RC TPM2_SetSessionAuth(TPMS_AUTH_COMMAND* cmd) return rc; } -TPM_RC TPM2_Init(TPM2_CTX* ctx, TPM2HalIoCb ioCb, void* userCtx) +/* Assumes caller has handled mutex protection */ +TPM_RC TPM2_ChipStartup(TPM2_CTX* ctx, int timeoutTries) +{ + TPM_RC rc; + + if (ctx == NULL) { + return TPM_RC_FAILURE; + } + + /* Wait for chip startup to complete */ + rc = TPM2_TIS_StartupWait(ctx, timeoutTries); + if (rc == TPM_RC_SUCCESS) { + + /* Request locality for TPM module */ + rc = TPM2_TIS_RequestLocality(ctx, timeoutTries); + if (rc == TPM_RC_SUCCESS) { + + /* Get device information */ + rc = TPM2_TIS_GetInfo(ctx); + } + } + return rc; +} + +TPM_RC TPM2_SetHalIoCb(TPM2_CTX* ctx, TPM2HalIoCb ioCb, void* userCtx) +{ + TPM_RC rc; + + if (ctx == NULL) { + return TPM_RC_FAILURE; + } + + rc = TPM2_AcquireLock(ctx); + if (rc == TPM_RC_SUCCESS) { + ctx->ioCb = ioCb; + ctx->userCtx = userCtx; + + TPM2_ReleaseLock(ctx); + } + + return rc; +} + +TPM_RC TPM2_Init_ex(TPM2_CTX* ctx, TPM2HalIoCb ioCb, void* userCtx, + int timeoutTries) { TPM_RC rc; @@ -270,8 +319,9 @@ TPM_RC TPM2_Init(TPM2_CTX* ctx, TPM2HalIoCb ioCb, void* userCtx) } XMEMSET(ctx, 0, sizeof(TPM2_CTX)); - ctx->ioCb = ioCb; - ctx->userCtx = userCtx; + rc = TPM2_SetHalIoCb(ctx, ioCb, userCtx); + if (rc != TPM_RC_SUCCESS) + return rc; #ifndef WOLFTPM2_NO_WOLFCRYPT #ifdef DEBUG_WOLFSSL @@ -303,27 +353,21 @@ TPM_RC TPM2_Init(TPM2_CTX* ctx, TPM2HalIoCb ioCb, void* userCtx) if (rc == TPM_RC_SUCCESS) { /* Set the active TPM global */ - gActiveTPM = ctx; + TPM2_SetActiveCtx(ctx); - - /* Wait for chip startup to complete */ - rc = TPM2_TIS_StartupWait(ctx, TPM_TIMEOUT_TRIES); - if (rc == TPM_RC_SUCCESS) { - - /* Request locality for TPM module */ - rc = TPM2_TIS_RequestLocality(ctx, TPM_TIMEOUT_TRIES); - if (rc == TPM_RC_SUCCESS) { - - /* Get device information */ - rc = TPM2_TIS_GetInfo(ctx); - } - } + /* Perform chip startup */ + rc = TPM2_ChipStartup(ctx, timeoutTries); TPM2_ReleaseLock(ctx); } return rc; } +TPM_RC TPM2_Init(TPM2_CTX* ctx, TPM2HalIoCb ioCb, void* userCtx) +{ + return TPM2_Init_ex(ctx, ioCb, userCtx, TPM_TIMEOUT_TRIES); +} + TPM_RC TPM2_Cleanup(TPM2_CTX* ctx) { TPM_RC rc; diff --git a/src/tpm2_wrap.c b/src/tpm2_wrap.c index b39cc93..afc98b9 100644 --- a/src/tpm2_wrap.c +++ b/src/tpm2_wrap.c @@ -34,7 +34,8 @@ static int wolfTPM2_GetCapabilities_NoDev(WOLFTPM2_CAPS* cap); /* --- BEGIN Wrapper Device Functions -- */ /******************************************************************************/ -static int wolfTPM2_Init_NoDev(TPM2_CTX* ctx, TPM2HalIoCb ioCb, void* userCtx) +static int wolfTPM2_Init_NoDev(TPM2_CTX* ctx, TPM2HalIoCb ioCb, void* userCtx, + int timeoutTries) { int rc; Startup_In startupIn; @@ -45,7 +46,7 @@ static int wolfTPM2_Init_NoDev(TPM2_CTX* ctx, TPM2HalIoCb ioCb, void* userCtx) if (ctx == NULL) return BAD_FUNC_ARG; - rc = TPM2_Init(ctx, ioCb, userCtx); + rc = TPM2_Init_ex(ctx, ioCb, userCtx, timeoutTries); if (rc != TPM_RC_SUCCESS) { #ifdef DEBUG_WOLFTPM printf("TPM2_Init failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); @@ -101,7 +102,7 @@ int wolfTPM2_Test(TPM2HalIoCb ioCb, void* userCtx, WOLFTPM2_CAPS* caps) int rc; TPM2_CTX ctx; - rc = wolfTPM2_Init_NoDev(&ctx, ioCb, userCtx); + rc = wolfTPM2_Init_NoDev(&ctx, ioCb, userCtx, TPM_STARTUP_TEST_TRIES); if (rc != TPM_RC_SUCCESS) { return rc; } @@ -111,6 +112,8 @@ int wolfTPM2_Test(TPM2HalIoCb ioCb, void* userCtx, WOLFTPM2_CAPS* caps) rc = wolfTPM2_GetCapabilities_NoDev(caps); } + TPM2_Cleanup(&ctx); + return rc; } @@ -121,7 +124,7 @@ int wolfTPM2_Init(WOLFTPM2_DEV* dev, TPM2HalIoCb ioCb, void* userCtx) if (dev == NULL) return BAD_FUNC_ARG; - rc = wolfTPM2_Init_NoDev(&dev->ctx, ioCb, userCtx); + rc = wolfTPM2_Init_NoDev(&dev->ctx, ioCb, userCtx, TPM_TIMEOUT_TRIES); if (rc != TPM_RC_SUCCESS) { return rc; } diff --git a/wolftpm/tpm2.h b/wolftpm/tpm2.h index 6c39809..f32a840 100644 --- a/wolftpm/tpm2.h +++ b/wolftpm/tpm2.h @@ -2701,11 +2701,16 @@ WOLFTPM_API int TPM2_SetCommandSet(SetCommandSet_In* in); /* Non-standard API's */ #define _TPM_Init TPM2_Init WOLFTPM_API TPM_RC TPM2_Init(TPM2_CTX* ctx, TPM2HalIoCb ioCb, void* userCtx); +WOLFTPM_API TPM_RC TPM2_Init_ex(TPM2_CTX* ctx, TPM2HalIoCb ioCb, void* userCtx, + int timeoutTries); WOLFTPM_API TPM_RC TPM2_Cleanup(TPM2_CTX* ctx); - -/* Other API's - Not TPM Spec */ +/* Other API's - Not in TPM Specification */ +WOLFTPM_API TPM_RC TPM2_ChipStartup(TPM2_CTX* ctx, int timeoutTries); +WOLFTPM_API TPM_RC TPM2_SetHalIoCb(TPM2_CTX* ctx, TPM2HalIoCb ioCb, void* userCtx); WOLFTPM_API TPM_RC TPM2_SetSessionAuth(TPMS_AUTH_COMMAND *cmd); + +WOLFTPM_API void TPM2_SetActiveCtx(TPM2_CTX* ctx); WOLFTPM_API TPM2_CTX* TPM2_GetActiveCtx(void); WOLFTPM_API int TPM2_GetHashDigestSize(TPMI_ALG_HASH hashAlg); @@ -2722,8 +2727,6 @@ WOLFTPM_API int TPM2_GetWolfCurve(int curve_id); #ifdef DEBUG_WOLFTPM WOLFTPM_API void TPM2_PrintBin(const byte* buffer, word32 length); - - #else #define TPM2_PrintBin(b, l) #endif diff --git a/wolftpm/tpm2_types.h b/wolftpm/tpm2_types.h old mode 100644 new mode 100755 index 7732b46..c30acdd --- a/wolftpm/tpm2_types.h +++ b/wolftpm/tpm2_types.h @@ -45,7 +45,9 @@ typedef uint8_t UINT8; typedef uint8_t BYTE; typedef int8_t INT8; +#ifndef HAVE_BOOL typedef int BOOL; +#endif typedef uint16_t UINT16; typedef int16_t INT16; typedef uint32_t UINT32; @@ -178,6 +180,10 @@ typedef int64_t INT64; #define MAX_SPI_FRAMESIZE 64 #endif +#ifndef TPM_STARTUP_TEST_TRIES +#define TPM_STARTUP_TEST_TRIES 2 +#endif + #ifndef TPM_TIMEOUT_TRIES #define TPM_TIMEOUT_TRIES 1000000 #endif