Skip to content
Open
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
122 changes: 99 additions & 23 deletions examples/bench/bench.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,10 @@ static int bench_sym_aes(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* storageKey,
#ifdef WOLFTPM_MLDSA
/* Benchmark Pure ML-DSA: key gen, sign and verify (sign/verify sequence). */
static int bench_pqc_mldsa(WOLFTPM2_DEV* dev, double maxDuration,
double maxKeyGenDurSec)
double maxKeyGenDurSec, int level)
{
int rc, count;
int strength;
double start;
WOLFTPM2_KEY mldsaKey;
TPMT_PUBLIC publicTemplate;
Expand All @@ -287,10 +288,14 @@ static int bench_pqc_mldsa(WOLFTPM2_DEV* dev, double maxDuration,
XMEMSET(message, 0x11, sizeof(message));
XMEMSET(sig, 0, sizeof(sig));

/* The reported strength is the parameter set name, not the enum value. */
strength = (level == TPM_MLDSA_44) ? 44 :
(level == TPM_MLDSA_65) ? 65 : 87;

rc = wolfTPM2_GetKeyTemplate_MLDSA(&publicTemplate,
TPMA_OBJECT_sign | TPMA_OBJECT_fixedTPM | TPMA_OBJECT_fixedParent |
TPMA_OBJECT_sensitiveDataOrigin | TPMA_OBJECT_userWithAuth |
TPMA_OBJECT_noDA, TPM_MLDSA_65, 0);
TPMA_OBJECT_noDA, level, 0);
if (rc != 0) return rc;

bench_stats_start(&count, &start);
Expand All @@ -301,7 +306,7 @@ static int bench_pqc_mldsa(WOLFTPM2_DEV* dev, double maxDuration,
&publicTemplate, NULL, 0);
if (rc != 0) break;
} while (bench_stats_check(start, &count, maxKeyGenDurSec));
rc = bench_asym_done("ML-DSA", 65, "key gen", count, start, rc);
rc = bench_asym_done("ML-DSA", strength, "key gen", count, start, rc);
if (rc != 0)
return rc;
if (count == 0)
Expand All @@ -325,7 +330,7 @@ static int bench_pqc_mldsa(WOLFTPM2_DEV* dev, double maxDuration,
}
if (rc != 0) break;
} while (bench_stats_check(start, &count, maxDuration));
rc = bench_asym_done("ML-DSA", 65, "sign", count, start, rc);
rc = bench_asym_done("ML-DSA", strength, "sign", count, start, rc);
if (rc != 0) goto exit;
if (count == 0)
goto exit; /* no signature produced; nothing to verify */
Expand All @@ -338,7 +343,7 @@ static int bench_pqc_mldsa(WOLFTPM2_DEV* dev, double maxDuration,
message, (int)sizeof(message), sig, sigSz, &validation);
if (rc != 0) break;
} while (bench_stats_check(start, &count, maxDuration));
rc = bench_asym_done("ML-DSA", 65, "verify", count, start, rc);
rc = bench_asym_done("ML-DSA", strength, "verify", count, start, rc);
if (rc != 0) goto exit;

exit:
Expand All @@ -350,9 +355,10 @@ static int bench_pqc_mldsa(WOLFTPM2_DEV* dev, double maxDuration,
#ifdef WOLFTPM_MLKEM
/* Benchmark ML-KEM: key gen, encapsulate and decapsulate. */
static int bench_pqc_mlkem(WOLFTPM2_DEV* dev, double maxDuration,
double maxKeyGenDurSec)
double maxKeyGenDurSec, int level)
{
int rc, count;
int strength;
double start;
WOLFTPM2_KEY mlkemKey;
TPMT_PUBLIC publicTemplate;
Expand All @@ -364,10 +370,14 @@ static int bench_pqc_mlkem(WOLFTPM2_DEV* dev, double maxDuration,
XMEMSET(&mlkemKey, 0, sizeof(mlkemKey));
XMEMSET(&publicTemplate, 0, sizeof(publicTemplate));

/* The reported strength is the parameter set name, not the enum value. */
strength = (level == TPM_MLKEM_512) ? 512 :
(level == TPM_MLKEM_768) ? 768 : 1024;

rc = wolfTPM2_GetKeyTemplate_MLKEM(&publicTemplate,
TPMA_OBJECT_decrypt | TPMA_OBJECT_fixedTPM | TPMA_OBJECT_fixedParent |
TPMA_OBJECT_sensitiveDataOrigin | TPMA_OBJECT_userWithAuth |
TPMA_OBJECT_noDA, TPM_MLKEM_768);
TPMA_OBJECT_noDA, level);
if (rc != 0) return rc;

bench_stats_start(&count, &start);
Expand All @@ -378,7 +388,7 @@ static int bench_pqc_mlkem(WOLFTPM2_DEV* dev, double maxDuration,
&publicTemplate, NULL, 0);
if (rc != 0) break;
} while (bench_stats_check(start, &count, maxKeyGenDurSec));
rc = bench_asym_done("ML-KEM", 768, "key gen", count, start, rc);
rc = bench_asym_done("ML-KEM", strength, "key gen", count, start, rc);
if (rc != 0)
return rc;
if (count == 0)
Expand All @@ -392,7 +402,7 @@ static int bench_pqc_mlkem(WOLFTPM2_DEV* dev, double maxDuration,
secret, &secretSz);
if (rc != 0) break;
} while (bench_stats_check(start, &count, maxDuration));
rc = bench_asym_done("ML-KEM", 768, "encap", count, start, rc);
rc = bench_asym_done("ML-KEM", strength, "encap", count, start, rc);
if (rc != 0) goto exit;

bench_stats_start(&count, &start);
Expand All @@ -402,7 +412,7 @@ static int bench_pqc_mlkem(WOLFTPM2_DEV* dev, double maxDuration,
secret, &secretSz);
if (rc != 0) break;
} while (bench_stats_check(start, &count, maxDuration));
rc = bench_asym_done("ML-KEM", 768, "decap", count, start, rc);
rc = bench_asym_done("ML-KEM", strength, "decap", count, start, rc);
if (rc != 0) goto exit;

exit:
Expand Down Expand Up @@ -448,11 +458,42 @@ int TPM2_Wrapper_BenchArgs(void* userCtx, int argc, char *argv[])
int count;
int sha1Supported;
int rsaBits = WOLFTPM2_WRAP_RSA_KEY_BITS;

int skipDec = 0; /* skip a decrypt op whose paired encrypt was unsupported */
TPM_ALG_ID paramEncAlg = TPM_ALG_NULL;
WOLFTPM2_SESSION tpmSession;
double maxDuration = TPM2_BENCH_DURATION_SEC;
double maxKeyGenDurSec = TPM2_BENCH_DURATION_KEYGEN_SEC;
int j;
int k;
int eccBits;
static const int rsaSizes[] = {
2048,
#if MAX_RSA_KEY_BITS >= 3072
3072,
#endif
#if MAX_RSA_KEY_BITS >= 4096
4096,
#endif
};
TPM_ECC_CURVE eccCurve;
static const TPM_ECC_CURVE eccCurves[] = {
TPM_ECC_NIST_P256,
#if defined(HAVE_ECC384) && ECC_MIN_KEY_SZ <= 384
TPM_ECC_NIST_P384,
#endif
};
#if defined(WOLFTPM_MLDSA) || defined(WOLFTPM_MLKEM)
int i;
#endif
#ifdef WOLFTPM_MLDSA
static const int mldsaLevels[] = {
TPM_MLDSA_44, TPM_MLDSA_65, TPM_MLDSA_87 };
#endif
#ifdef WOLFTPM_MLKEM
static const int mlkemLevels[] = {
TPM_MLKEM_512, TPM_MLKEM_768, TPM_MLKEM_1024 };
#endif

/* Static, so a previous call with -iter must not leak into this one */
gBenchIter = 0;
Expand Down Expand Up @@ -607,11 +648,18 @@ int TPM2_Wrapper_BenchArgs(void* userCtx, int argc, char *argv[])
if (rc != 0 && (rc & RC_MAX_FMT1) != TPM_RC_HASH) goto exit;


for (k = 0; k < (int)(sizeof(rsaSizes)/sizeof(rsaSizes[0])); k++) {
rsaBits = rsaSizes[k];

/* Create RSA key for encrypt/decrypt */
rc = wolfTPM2_GetKeyTemplate_RSA(&publicTemplate,
TPMA_OBJECT_sensitiveDataOrigin | TPMA_OBJECT_userWithAuth |
TPMA_OBJECT_decrypt | TPMA_OBJECT_sign | TPMA_OBJECT_noDA);
if (rc != 0) goto exit;
/* The template helper takes no size, so it comes from
* WOLFTPM2_WRAP_RSA_KEY_BITS. Override it to walk the sizes the TPM
* structures can carry. */
publicTemplate.parameters.rsaDetail.keyBits = (TPMI_RSA_KEY_BITS)rsaBits;
bench_stats_start(&count, &start);
do {
if (count > 0) {
Expand All @@ -620,9 +668,16 @@ int TPM2_Wrapper_BenchArgs(void* userCtx, int argc, char *argv[])
}
rc = wolfTPM2_CreateAndLoadKey(&dev, &rsaKey, &storageKey.handle,
&publicTemplate, (byte*)gKeyAuth, sizeof(gKeyAuth)-1);
if (rc != 0) goto exit;
/* A part that does not implement this modulus reports itself here
* rather than ending the benchmark. */
if (rc != 0) break;
} while (bench_stats_check(start, &count, maxKeyGenDurSec));
bench_stats_asym_finish("RSA", rsaBits, "key gen", count, start);
rc = bench_asym_done("RSA", rsaBits, "key gen", count, start, rc);
if (rc != 0) goto exit;
if (count == 0) {
XMEMSET(&rsaKey, 0, sizeof(rsaKey));
continue; /* size unsupported; the rest needs the key */
}

/* Perform RSA encrypt / decrypt (no pad) */
/* No padding means the message is exactly the modulus width, which the
Expand Down Expand Up @@ -691,13 +746,18 @@ int TPM2_Wrapper_BenchArgs(void* userCtx, int argc, char *argv[])

rc = wolfTPM2_UnloadHandle(&dev, &rsaKey.handle);
if (rc != 0) goto exit;
} /* RSA sizes */


for (j = 0; j < (int)(sizeof(eccCurves)/sizeof(eccCurves[0])); j++) {
eccCurve = eccCurves[j];
eccBits = (eccCurve == TPM_ECC_NIST_P256) ? 256 : 384;

/* Create an ECC key for ECDSA */
rc = wolfTPM2_GetKeyTemplate_ECC(&publicTemplate,
TPMA_OBJECT_sensitiveDataOrigin | TPMA_OBJECT_userWithAuth |
TPMA_OBJECT_sign | TPMA_OBJECT_noDA,
TPM_ECC_NIST_P256, TPM_ALG_ECDSA);
eccCurve, TPM_ALG_ECDSA);
if (rc != 0) goto exit;
bench_stats_start(&count, &start);
do {
Expand All @@ -707,9 +767,16 @@ int TPM2_Wrapper_BenchArgs(void* userCtx, int argc, char *argv[])
}
rc = wolfTPM2_CreateAndLoadKey(&dev, &eccKey, &storageKey.handle,
&publicTemplate, (byte*)gKeyAuth, sizeof(gKeyAuth)-1);
if (rc != 0) goto exit;
/* A part that does not implement this curve reports itself here
* rather than ending the benchmark. */
if (rc != 0) break;
} while (bench_stats_check(start, &count, maxDuration));
bench_stats_asym_finish("ECC", 256, "key gen", count, start);
rc = bench_asym_done("ECC", eccBits, "key gen", count, start, rc);
if (rc != 0) goto exit;
if (count == 0) {
XMEMSET(&eccKey, 0, sizeof(eccKey));
continue; /* curve unsupported; the rest needs the key */
}

/* Perform sign / verify */
message.size = TPM_SHA256_DIGEST_SIZE; /* test message 0x11,0x11,etc */
Expand All @@ -722,7 +789,7 @@ int TPM2_Wrapper_BenchArgs(void* userCtx, int argc, char *argv[])
cipher.buffer, &cipher.size);
if (rc != 0) break;
} while (bench_stats_check(start, &count, maxDuration));
rc = bench_asym_done("ECDSA", 256, "sign", count, start, rc);
rc = bench_asym_done("ECDSA", eccBits, "sign", count, start, rc);
if (rc != 0) goto exit;

bench_stats_start(&count, &start);
Expand All @@ -731,7 +798,7 @@ int TPM2_Wrapper_BenchArgs(void* userCtx, int argc, char *argv[])
message.buffer, message.size);
if (rc != 0) break;
} while (bench_stats_check(start, &count, maxDuration));
rc = bench_asym_done("ECDSA", 256, "verify", count, start, rc);
rc = bench_asym_done("ECDSA", eccBits, "verify", count, start, rc);
if (rc != 0) goto exit;

rc = wolfTPM2_UnloadHandle(&dev, &eccKey.handle);
Expand All @@ -742,7 +809,7 @@ int TPM2_Wrapper_BenchArgs(void* userCtx, int argc, char *argv[])
rc = wolfTPM2_GetKeyTemplate_ECC(&publicTemplate,
TPMA_OBJECT_sensitiveDataOrigin | TPMA_OBJECT_userWithAuth |
TPMA_OBJECT_decrypt | TPMA_OBJECT_noDA,
TPM_ECC_NIST_P256, TPM_ALG_ECDH);
eccCurve, TPM_ALG_ECDH);
if (rc != 0) goto exit;
rc = wolfTPM2_CreateAndLoadKey(&dev, &eccKey, &storageKey.handle,
&publicTemplate, (byte*)gKeyAuth, sizeof(gKeyAuth)-1);
Expand All @@ -756,11 +823,12 @@ int TPM2_Wrapper_BenchArgs(void* userCtx, int argc, char *argv[])
cipher.buffer, &cipher.size);
if (rc != 0) break;
} while (bench_stats_check(start, &count, maxDuration));
rc = bench_asym_done("ECDHE", 256, "agree", count, start, rc);
rc = bench_asym_done("ECDHE", eccBits, "agree", count, start, rc);
if (rc != 0) goto exit;

rc = wolfTPM2_UnloadHandle(&dev, &eccKey.handle);
if (rc != 0) goto exit;
} /* ECC curves */

#if defined(WOLFTPM_MLDSA) || defined(WOLFTPM_MLKEM)
/* Post-quantum (TPM 2.0 v1.85). Skipped under parameter encryption: the
Expand All @@ -770,12 +838,20 @@ int TPM2_Wrapper_BenchArgs(void* userCtx, int argc, char *argv[])
if (paramEncAlg == TPM_ALG_NULL) {
wolfTPM2_UnloadHandle(&dev, &storageKey.handle);
#ifdef WOLFTPM_MLDSA
rc = bench_pqc_mldsa(&dev, maxDuration, maxKeyGenDurSec);
if (rc != 0) goto exit;
for (i = 0; i < (int)(sizeof(mldsaLevels)/sizeof(mldsaLevels[0]));
i++) {
rc = bench_pqc_mldsa(&dev, maxDuration, maxKeyGenDurSec,
mldsaLevels[i]);
if (rc != 0) goto exit;
}
#endif
#ifdef WOLFTPM_MLKEM
rc = bench_pqc_mlkem(&dev, maxDuration, maxKeyGenDurSec);
if (rc != 0) goto exit;
for (i = 0; i < (int)(sizeof(mlkemLevels)/sizeof(mlkemLevels[0]));
i++) {
rc = bench_pqc_mlkem(&dev, maxDuration, maxKeyGenDurSec,
mlkemLevels[i]);
if (rc != 0) goto exit;
}
#endif
}
#endif
Expand Down
Loading