Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions .github/workflows/make-test-swtpm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,26 @@ jobs:
- name: make not provisioning
run: make

# test with symmetric encryption
- name: configure symmetric
run: ./configure --enable-swtpm CFLAGS="-DWOLFTPM_USE_SYMMETRIC"
- name: make symmetric
run: make
- name: make check symmetric
run: |
make check
WOLFSSL_PATH=./wolfssl ./examples/run_examples.sh

# test with software ecdhe
- name: configure swecdhe
run: ./configure --enable-swtpm CFLAGS="-DWOLFTPM2_USE_SW_ECDHE"
- name: make swecdhe
run: make
- name: make check swecdhe
run: |
make check
WOLFSSL_PATH=./wolfssl ./examples/run_examples.sh

# test without ECC
- name: wolfssl no ECC
working-directory: ./wolfssl
Expand Down Expand Up @@ -240,16 +260,6 @@ jobs:
make check
WOLFSSL_PATH=./wolfssl NO_PUBASPRIV=1 ./examples/run_examples.sh

# test with symmetric encryption
- name: configure symmetric
run: ./configure --enable-swtpm CFLAGS="-DWOLFTPM_USE_SYMMETRIC"
- name: make symmetric
run: make
- name: make check symmetric
run: |
make check
WOLFSSL_PATH=./wolfssl ./examples/run_examples.sh

# capture logs on failure
- name: Upload failure logs
if: failure()
Expand Down
64 changes: 40 additions & 24 deletions examples/wrap/wrap_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ int TPM2_Wrapper_TestArgs(void* userCtx, int argc, char *argv[])
TPM_ALG_ID paramEncAlg = TPM_ALG_NULL;
WOLFTPM2_SESSION tpmSession;

XMEMSET(&rsaKey, 0, sizeof(rsaKey));
XMEMSET(&eccKey, 0, sizeof(eccKey));
XMEMSET(&aesKey, 0, sizeof(aesKey));
XMEMSET(&publicKey, 0, sizeof(publicKey));
#ifndef WOLFTPM2_NO_WOLFCRYPT
Expand Down Expand Up @@ -172,11 +174,9 @@ int TPM2_Wrapper_TestArgs(void* userCtx, int argc, char *argv[])
/* Setup the wolf crypto device callback */
XMEMSET(&tpmCtx, 0, sizeof(tpmCtx));
#ifndef NO_RSA
XMEMSET(&rsaKey, 0, sizeof(rsaKey));
tpmCtx.rsaKey = &rsaKey;
#endif
#ifdef HAVE_ECC
XMEMSET(&eccKey, 0, sizeof(eccKey));
tpmCtx.eccKey = &eccKey;
#endif
tpmCtx.storageKey = &storageKey;
Expand Down Expand Up @@ -448,21 +448,30 @@ int TPM2_Wrapper_TestArgs(void* userCtx, int argc, char *argv[])
(word32)sizeof(kRsaKeyPrivDer));
PRIVATE_KEY_LOCK();
if (rc != 0) goto exit;
rc = wolfTPM2_RsaKey_WolfToTpm_ex(&dev, &storageKey, &wolfRsaPrivKey,
&rsaKey);
wc_FreeRsaKey(&wolfRsaPrivKey);
if (rc != 0 && rc != NOT_COMPILED_IN) {
XMEMSET(&testKey, 0, sizeof(testKey));
rc = wolfTPM2_CreateRsaKeyBlob(&dev, &storageKey, &wolfRsaPrivKey,
&testKey);
wc_FreeRsaKey(&wolfRsaPrivKey); /* free wolf key */

if (rc == 0) {
printf("RSA Private Key Blob created (private = %d bytes)\n",
testKey.priv.size);

rc = wolfTPM2_LoadKey(&dev, &testKey, &storageKey.handle);
if (rc != 0) goto exit;
printf("RSA Private Key Loaded into TPM: Handle 0x%x\n",
(word32)rsaKey.handle.hndl);

/* Use TPM Handle... */

rc = wolfTPM2_UnloadHandle(&dev, &testKey.handle);
if (rc != 0) goto exit;
}
else if (rc != NOT_COMPILED_IN) {
/* NOT_COMPILED_IN here likely means that AES-CFB is not enabled for
* encrypting secrets */
goto exit;
}
printf("RSA Private Key Loaded into TPM: Handle 0x%x\n",
(word32)rsaKey.handle.hndl);

/* Use TPM Handle... */

rc = wolfTPM2_UnloadHandle(&dev, &rsaKey.handle);
if (rc != 0) goto exit;
#endif /* !WOLFTPM2_NO_WOLFCRYPT && !NO_RSA && !NO_ASN */

/* Load raw RSA private key into TPM */
Expand Down Expand Up @@ -682,22 +691,29 @@ int TPM2_Wrapper_TestArgs(void* userCtx, int argc, char *argv[])
rc = wc_EccPrivateKeyDecode(kEccKeyPrivDer, &idx, &wolfEccPrivKey,
(word32)sizeof(kEccKeyPrivDer));
if (rc != 0) goto exit;
rc = wolfTPM2_EccKey_WolfToTpm_ex(&dev, &storageKey, &wolfEccPrivKey,
&eccKey);
wc_ecc_free(&wolfEccPrivKey);
if (rc != 0 && rc != NOT_COMPILED_IN) {
XMEMSET(&testKey, 0, sizeof(testKey));
rc = wolfTPM2_CreateEccKeyBlob(&dev, &storageKey, &wolfEccPrivKey,
&testKey);
wc_ecc_free(&wolfEccPrivKey); /* free wolf key */
if (rc == 0) {
printf("ECC Private Key Blob created (private = %d bytes)\n",
testKey.priv.size);
rc = wolfTPM2_LoadKey(&dev, &testKey, &storageKey.handle);
if (rc != 0) goto exit;
printf("ECC Private Key Loaded into TPM: Handle 0x%x\n",
(word32)testKey.handle.hndl);

/* Use TPM Handle... */

rc = wolfTPM2_UnloadHandle(&dev, &testKey.handle);
if (rc != 0) goto exit;
}
else if (rc != NOT_COMPILED_IN) {
/* NOT_COMPILED_IN here likely means the WOLFSSL_PUBLIC_MP is enabled
* exposing the mp_ math API's or AES CFB is not enabled.
* Both are needed for encrypting secrets */
goto exit;
}
printf("ECC Private Key Loaded into TPM: Handle 0x%x\n",
(word32)eccKey.handle.hndl);

/* Use TPM Handle... */

rc = wolfTPM2_UnloadHandle(&dev, &eccKey.handle);
if (rc != 0) goto exit;
#endif /* !WOLFTPM2_NO_WOLFCRYPT && HAVE_ECC && !NO_ASN */

/* Load raw ECC private key into TPM */
Expand Down
92 changes: 68 additions & 24 deletions src/tpm2_cryptocb.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,80 +177,123 @@ int wolfTPM2_CryptoDevCb(int devId, wc_CryptoInfo* info, void* ctx)
#endif /* !NO_RSA */
#ifdef HAVE_ECC
if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
#ifdef WOLFTPM2_USE_SW_ECDHE
rc = exit_rc;
#else
int curve_id;
WOLFTPM2_KEY* key;

#ifdef WOLFTPM2_USE_SW_ECDHE
if (tlsCtx->ecdhKey == NULL) {
return exit_rc;
}
#endif

if ( tlsCtx->eccKey == NULL
&& tlsCtx->ecdsaKey == NULL
&& tlsCtx->ecdhKey == NULL
) {
#ifdef DEBUG_WOLFTPM
printf("No crypto callback key pointer set!\n");
#endif
return BAD_FUNC_ARG;
}

/* Make sure an ECDH key has been set and curve is supported */
curve_id = info->pk.eckg.curveId;
if (curve_id == 0 && info->pk.eckg.key->dp != NULL) {
curve_id = info->pk.eckg.key->dp->id; /* use dp */
}
rc = TPM2_GetTpmCurve(curve_id);
if (rc < 0 || (tlsCtx->ecdhKey == NULL && tlsCtx->eccKey == NULL)) {
if (rc < 0) {
return exit_rc;
}
curve_id = rc;
rc = 0;

/* If ecdhKey is NULL then it is a signing key */
if (tlsCtx->ecdhKey == NULL) {
#ifndef WOLFTPM2_USE_SW_ECDHE
if (tlsCtx->ecdhKey == NULL)
#endif
{
/* Create an ECC key for ECDSA - if one isn't already created */
key = tlsCtx->eccKey;
key = (tlsCtx->ecdsaKey != NULL) ?
(WOLFTPM2_KEY*)tlsCtx->ecdsaKey : tlsCtx->eccKey;
if (key->handle.hndl == 0 ||
key->handle.hndl == TPM_RH_NULL
) {
TPMT_PUBLIC publicTemplate;
XMEMSET(&publicTemplate, 0, sizeof(publicTemplate));
TPMI_ALG_HASH hashAlg;

if (curve_id == TPM_ECC_NIST_P521)
hashAlg = TPM_ALG_SHA512;
else if (curve_id == TPM_ECC_NIST_P384)
hashAlg = TPM_ALG_SHA384;
else
hashAlg = TPM_ALG_SHA256;

rc = wolfTPM2_GetKeyTemplate_ECC(&publicTemplate,
XMEMSET(&publicTemplate, 0, sizeof(publicTemplate));
rc = wolfTPM2_GetKeyTemplate_ECC_ex(&publicTemplate, hashAlg,
TPMA_OBJECT_sensitiveDataOrigin | TPMA_OBJECT_userWithAuth |
TPMA_OBJECT_sign | TPMA_OBJECT_noDA,
curve_id, TPM_ALG_ECDSA);
curve_id, TPM_ALG_ECDSA, hashAlg);
if (rc == 0) {
publicTemplate.nameAlg = TPM_ALG_SHA256; /* make sure its SHA256 */
rc = wolfTPM2_CreateAndLoadKey(tlsCtx->dev, key,
&tlsCtx->storageKey->handle, &publicTemplate,
(byte*)key->handle.auth.buffer,
key->handle.auth.size);
if (tlsCtx->ecdsaKey != NULL) {
/* Use create key and load key directly instead to make
* sure the private portion is populated */
rc = wolfTPM2_CreateKey(tlsCtx->dev, tlsCtx->ecdsaKey,
&tlsCtx->storageKey->handle, &publicTemplate,
(byte*)key->handle.auth.buffer,
key->handle.auth.size);
if (rc == TPM_RC_SUCCESS) {
rc = wolfTPM2_LoadKey(tlsCtx->dev, tlsCtx->ecdsaKey,
&tlsCtx->storageKey->handle);
}
}
else {
/* Create and load key - encrypted private is not exported */
rc = wolfTPM2_CreateAndLoadKey(tlsCtx->dev, tlsCtx->eccKey,
&tlsCtx->storageKey->handle, &publicTemplate,
(byte*)key->handle.auth.buffer,
key->handle.auth.size);
}
}
}
}
#ifndef WOLFTPM2_USE_SW_ECDHE
else {
/* Generate ephemeral key - if one isn't already created */
key = tlsCtx->ecdhKey;
if (key->handle.hndl == 0 ||
key->handle.hndl == TPM_RH_NULL) {
rc = wolfTPM2_ECDHGenKey(tlsCtx->dev, key, curve_id,
NULL, 0 /* no auth for ephemeral key */
rc = wolfTPM2_ECDHGenKey(tlsCtx->dev, tlsCtx->ecdhKey,
curve_id, NULL, 0 /* no auth for ephemeral key */
);
}
}
#endif

if (rc == 0) {
/* Export public key info to wolf ecc_key */
rc = wolfTPM2_EccKey_TpmToWolf(tlsCtx->dev, key,
info->pk.eckg.key);
if (rc != 0) {
/* if failure, release key */
wolfTPM2_UnloadHandle(tlsCtx->dev, &tlsCtx->ecdhKey->handle);
wolfTPM2_UnloadHandle(tlsCtx->dev, &key->handle);
}
}
else if (rc & TPM_RC_CURVE) {
/* if the curve is not supported on TPM, then fall-back to software */
rc = exit_rc;
/* Make sure ECDHE key indicates nothing loaded */
/* Make sure key indicates nothing loaded */
key->handle.hndl = TPM_RH_NULL;
}
#endif /* WOLFTPM2_USE_SW_ECDHE */
}
else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
byte sigRS[MAX_ECC_BYTES*2];
word32 rsLen = sizeof(sigRS), keySz;
word32 inlen = info->pk.eccsign.inlen;
WOLFTPM2_KEY* key = (tlsCtx->ecdsaKey != NULL) ?
(WOLFTPM2_KEY*)tlsCtx->ecdsaKey : tlsCtx->eccKey;

if (tlsCtx->eccKey == NULL) {
if (key == NULL) {
/* TPM key not setup, fallback to software */
return exit_rc;
}
Expand All @@ -260,13 +303,13 @@ int wolfTPM2_CryptoDevCb(int devId, wc_CryptoInfo* info, void* ctx)
if (keySz == 0) {
/* if not populated fallback to key size for TPM key */
keySz = TPM2_GetCurveSize(
tlsCtx->eccKey->pub.publicArea.parameters.eccDetail.curveID);
key->pub.publicArea.parameters.eccDetail.curveID);
}
/* truncate input to match key size */
if (inlen > keySz)
inlen = keySz;

rc = wolfTPM2_SignHash(tlsCtx->dev, tlsCtx->eccKey,
rc = wolfTPM2_SignHash(tlsCtx->dev, key,
info->pk.eccsign.in, inlen, sigRS, (int*)&rsLen);
if (rc == 0) {
byte *r, *s;
Expand Down Expand Up @@ -335,8 +378,9 @@ int wolfTPM2_CryptoDevCb(int devId, wc_CryptoInfo* info, void* ctx)
TPM2B_ECC_POINT pubPoint;

/* Make sure an ECDH key has been set */
if (tlsCtx->ecdhKey == NULL || tlsCtx->eccKey == NULL ||
tlsCtx->ecdhKey->handle.hndl == TPM_RH_NULL) {
if (tlsCtx->ecdhKey == NULL ||
tlsCtx->ecdhKey->handle.hndl == TPM_RH_NULL ||
tlsCtx->ecdhKey->handle.hndl == 0) {
return exit_rc;
}

Expand Down
Loading