Skip to content
Open
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
138 changes: 127 additions & 11 deletions wolfcrypt/benchmark/benchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -867,9 +867,14 @@ static WC_INLINE void bench_append_memory_info(char* buffer, size_t size,
#define BENCH_HMAC_SHA256 0x00000020
#define BENCH_HMAC_SHA384 0x00000040
#define BENCH_HMAC_SHA512 0x00000080
#define BENCH_HMAC_SHA3_256 0x00000400
#define BENCH_HMAC_SHA3_384 0x00000800
#define BENCH_HMAC_SHA3_512 0x00001000
#define BENCH_HMAC (BENCH_HMAC_MD5 | BENCH_HMAC_SHA | \
BENCH_HMAC_SHA224 | BENCH_HMAC_SHA256 | \
BENCH_HMAC_SHA384 | BENCH_HMAC_SHA512)
BENCH_HMAC_SHA384 | BENCH_HMAC_SHA512 | \
BENCH_HMAC_SHA3_256 | BENCH_HMAC_SHA3_384 | \
BENCH_HMAC_SHA3_512)
#define BENCH_PBKDF2 0x00000100
#define BENCH_SIPHASH 0x00000200

Expand Down Expand Up @@ -1208,6 +1213,17 @@ static const bench_alg bench_mac_opt[] = {
#ifdef WOLFSSL_SHA512
{ "-hmac-sha512", BENCH_HMAC_SHA512 },
#endif
#ifdef WOLFSSL_SHA3
#ifndef WOLFSSL_NOSHA3_256
{ "-hmac-sha3-256", BENCH_HMAC_SHA3_256 },
#endif
#ifndef WOLFSSL_NOSHA3_384
{ "-hmac-sha3-384", BENCH_HMAC_SHA3_384 },
#endif
#ifndef WOLFSSL_NOSHA3_512
{ "-hmac-sha3-512", BENCH_HMAC_SHA3_512 },
#endif
#endif
#ifndef NO_PWDBASED
{ "-pbkdf2", BENCH_PBKDF2 },
#endif
Expand Down Expand Up @@ -2375,6 +2391,9 @@ static const char* bench_result_words2[][6] = {
#endif

static int numBlocks = NUM_BLOCKS;
/* Set when the user passes -blocks, so benchmark_configure() honors that count
* verbatim instead of recomputing it from the block size. */
static int numBlocksSet = 0;
static word32 bench_size = BENCH_SIZE;
static int base2 = 1;
static int digest_stream = 1;
Expand Down Expand Up @@ -2472,6 +2491,7 @@ static void benchmark_static_init(int force)

/* Init static variables */
numBlocks = NUM_BLOCKS;
numBlocksSet = 0;
bench_size = BENCH_SIZE;
#if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
aesAuthAddSz = AES_AUTH_ADD_SZ;
Expand Down Expand Up @@ -4353,6 +4373,38 @@ static void* benchmarks_do(void* args)
#endif
}
#endif
#ifdef WOLFSSL_SHA3
#ifndef WOLFSSL_NOSHA3_256
if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA3_256)) {
#ifndef NO_SW_BENCH
bench_hmac_sha3_256(0);
#endif
#ifdef BENCH_DEVID
bench_hmac_sha3_256(1);
#endif
}
#endif
#ifndef WOLFSSL_NOSHA3_384
if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA3_384)) {
#ifndef NO_SW_BENCH
bench_hmac_sha3_384(0);
#endif
#ifdef BENCH_DEVID
bench_hmac_sha3_384(1);
#endif
}
#endif
#ifndef WOLFSSL_NOSHA3_512
if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA3_512)) {
#ifndef NO_SW_BENCH
bench_hmac_sha3_512(0);
#endif
#ifdef BENCH_DEVID
bench_hmac_sha3_512(1);
#endif
}
#endif
#endif /* WOLFSSL_SHA3 */
#ifndef NO_PWDBASED
if (bench_all || (bench_mac_algs & BENCH_PBKDF2)) {
bench_pbkdf2();
Expand Down Expand Up @@ -9188,6 +9240,11 @@ void bench_shake128(int useDeviceID)
#endif /* WOLFSSL_SHAKE128 */

#ifdef WOLFSSL_SHAKE256
/* SHAKE256 benchmark XOF output length. Defaults to the SHA3-256 rate block;
* override to a smaller size for hardware that limits XOF output length. */
#ifndef BENCH_SHAKE256_XOF_SZ
#define BENCH_SHAKE256_XOF_SZ WC_SHA3_256_BLOCK_SIZE
#endif
void bench_shake256(int useDeviceID)
{
WC_DECLARE_ARRAY(hash, wc_Shake, BENCH_MAX_PENDING,
Expand All @@ -9196,14 +9253,14 @@ void bench_shake256(int useDeviceID)
int ret = 0, i, count = 0, times, pending = 0;
DECLARE_MULTI_VALUE_STATS_VARS()
WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
WC_SHA3_256_BLOCK_SIZE, HEAP_HINT);
BENCH_SHAKE256_XOF_SZ, HEAP_HINT);

bench_stats_prepare();

WC_CALLOC_ARRAY(hash, wc_Shake, BENCH_MAX_PENDING,
sizeof(wc_Shake), HEAP_HINT);
WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
WC_SHA3_256_BLOCK_SIZE, HEAP_HINT);
BENCH_SHAKE256_XOF_SZ, HEAP_HINT);

if (digest_stream) {
/* init keys */
Expand Down Expand Up @@ -9245,7 +9302,7 @@ void bench_shake256(int useDeviceID)
if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
0, &times, numBlocks, &pending)) {
ret = wc_Shake256_Final(hash[i], digest[i],
WC_SHA3_256_BLOCK_SIZE);
BENCH_SHAKE256_XOF_SZ);
if (!bench_async_handle(&ret,
BENCH_ASYNC_GET_DEV(hash[i]), 0,
&times, &pending)) {
Expand All @@ -9270,7 +9327,7 @@ void bench_shake256(int useDeviceID)
ret = wc_Shake256_Update(hash[0], bench_plain, bench_size);
if (ret == 0)
ret = wc_Shake256_Final(hash[0], digest[0],
WC_SHA3_256_BLOCK_SIZE);
BENCH_SHAKE256_XOF_SZ);
if (ret != 0)
goto exit_shake256;
RECORD_MULTI_VALUE_STATS();
Expand Down Expand Up @@ -10080,6 +10137,56 @@ void bench_hmac_sha512(int useDeviceID)

#endif /* WOLFSSL_SHA512 */

#ifdef WOLFSSL_SHA3
#ifndef WOLFSSL_NOSHA3_256
void bench_hmac_sha3_256(int useDeviceID)
{
WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };

bench_hmac(useDeviceID, WC_SHA3_256, WC_SHA3_256_DIGEST_SIZE, key,
sizeof(key), "HMAC-SHA3-256");
}
#endif /* !WOLFSSL_NOSHA3_256 */

#ifndef WOLFSSL_NOSHA3_384
void bench_hmac_sha3_384(int useDeviceID)
{
WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };

bench_hmac(useDeviceID, WC_SHA3_384, WC_SHA3_384_DIGEST_SIZE, key,
sizeof(key), "HMAC-SHA3-384");
}
#endif /* !WOLFSSL_NOSHA3_384 */

#ifndef WOLFSSL_NOSHA3_512
void bench_hmac_sha3_512(int useDeviceID)
{
WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };

bench_hmac(useDeviceID, WC_SHA3_512, WC_SHA3_512_DIGEST_SIZE, key,
sizeof(key), "HMAC-SHA3-512");
}
#endif /* !WOLFSSL_NOSHA3_512 */
#endif /* WOLFSSL_SHA3 */

#ifndef NO_PWDBASED
void bench_pbkdf2(void)
{
Expand Down Expand Up @@ -17350,15 +17457,22 @@ void bench_mldsaKeySign(byte level)

#endif /* HAVE_GET_CYCLES */

/* Lower bound for the computed block count so a large block size still runs at
* least this many blocks. Default 1. */
#ifndef BENCH_MIN_BLOCKS
#define BENCH_MIN_BLOCKS 1
#endif
void benchmark_configure(word32 block_size)
{
/* must be greater than 0 */
if (block_size > 0) {
numBlocks = (int)((word32)numBlocks * bench_size / block_size);
/* integer division can truncate to 0 for large block sizes; keep at
* least one block so the algorithm actually runs. */
if (numBlocks < 1)
numBlocks = 1;
/* Recompute from constants (idempotent) unless -blocks set it, so
* sweeping several block sizes does not erode the total. */
if (!numBlocksSet) {
numBlocks = (int)(NUM_BLOCKS * BENCH_SIZE / block_size);
if (numBlocks < BENCH_MIN_BLOCKS)
numBlocks = BENCH_MIN_BLOCKS;
}
bench_size = block_size;
}
}
Expand Down Expand Up @@ -17700,8 +17814,10 @@ int wolfcrypt_benchmark_main(int argc, char** argv)
else if (string_matches(argv[1], "-blocks")) {
argc--;
argv++;
if (argc > 1)
if (argc > 1) {
numBlocks = XATOI(argv[1]);
numBlocksSet = 1;
}
}
#ifndef NO_FILESYSTEM
else if (string_matches(argv[1], "-hash_input")) {
Expand Down
3 changes: 3 additions & 0 deletions wolfcrypt/benchmark/benchmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ void bench_hmac_sha224(int useDeviceID);
void bench_hmac_sha256(int useDeviceID);
void bench_hmac_sha384(int useDeviceID);
void bench_hmac_sha512(int useDeviceID);
void bench_hmac_sha3_256(int useDeviceID);
void bench_hmac_sha3_384(int useDeviceID);
void bench_hmac_sha3_512(int useDeviceID);
void bench_siphash(void);
void bench_srtpkdf(void);
void bench_rsaKeyGen(int useDeviceID);
Expand Down
Loading