Added option to benchmark RSA sign/verify instead of enc/dec

pull/1325/head
Sean Parkinson 2018-01-18 10:23:02 +10:00
parent 0b8c069119
commit f2079ca792
1 changed files with 121 additions and 93 deletions

View File

@ -689,6 +689,10 @@ static THREAD_LS_T int devId = INVALID_DEVID;
#endif #endif
static int base2 = 1; static int base2 = 1;
static int digest_stream = 1; static int digest_stream = 1;
#ifndef NO_RSA
/* Don't measure RSA sign/verify by default */
static int rsa_sign_verify = 0;
#endif
/* for compatibility */ /* for compatibility */
#define BENCH_SIZE bench_size #define BENCH_SIZE bench_size
@ -3681,109 +3685,126 @@ void bench_rsa(int doAsync)
} }
} }
#ifndef BENCHMARK_RSA_SIGN_VERIFY if (!rsa_sign_verify) {
/* begin public RSA */ /* begin public RSA */
bench_stats_start(&count, &start); bench_stats_start(&count, &start);
do { do {
for (times = 0; times < ntimes || pending > 0; ) { for (times = 0; times < ntimes || pending > 0; ) {
bench_async_poll(&pending); bench_async_poll(&pending);
/* while free pending slots in queue, submit ops */ /* while free pending slots in queue, submit ops */
for (i = 0; i < BENCH_MAX_PENDING; i++) { for (i = 0; i < BENCH_MAX_PENDING; i++) {
if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]), 1, &times, ntimes, &pending)) { if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]),
ret = wc_RsaPublicEncrypt(message, (word32)len, enc[i], 1, &times, ntimes, &pending)) {
RSA_BUF_SIZE, &rsaKey[i], &rng); ret = wc_RsaPublicEncrypt(message, (word32)len, enc[i],
if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]), 1, &times, &pending)) { RSA_BUF_SIZE, &rsaKey[i],
goto exit_rsa_pub; &rng);
if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(
&rsaKey[i]), 1, &times, &pending)) {
goto exit_rsa_pub;
}
} }
} } /* for i */
} /* for i */ } /* for times */
} /* for times */ count += times;
count += times; } while (bench_stats_sym_check(start));
} while (bench_stats_sym_check(start));
exit_rsa_pub: exit_rsa_pub:
bench_stats_asym_finish("RSA", rsaKeySz, "public", doAsync, count, start, ret); bench_stats_asym_finish("RSA", rsaKeySz, "public", doAsync, count,
start, ret);
if (ret < 0) { if (ret < 0) {
goto exit; goto exit;
} }
/* capture resulting encrypt length */ /* capture resulting encrypt length */
idx = (word32)(rsaKeySz/8); idx = (word32)(rsaKeySz/8);
/* begin private async RSA */ /* begin private async RSA */
bench_stats_start(&count, &start); bench_stats_start(&count, &start);
do { do {
for (times = 0; times < ntimes || pending > 0; ) { for (times = 0; times < ntimes || pending > 0; ) {
bench_async_poll(&pending); bench_async_poll(&pending);
/* while free pending slots in queue, submit ops */ /* while free pending slots in queue, submit ops */
for (i = 0; i < BENCH_MAX_PENDING; i++) { for (i = 0; i < BENCH_MAX_PENDING; i++) {
if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]), 1, &times, ntimes, &pending)) { if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]),
ret = wc_RsaPrivateDecrypt(enc[i], idx, out[i], 1, &times, ntimes, &pending)) {
RSA_BUF_SIZE, &rsaKey[i]); ret = wc_RsaPrivateDecrypt(enc[i], idx, out[i],
if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]), 1, &times, &pending)) { RSA_BUF_SIZE, &rsaKey[i]);
goto exit; if (!bench_async_handle(&ret,
BENCH_ASYNC_GET_DEV(&rsaKey[i]),
1, &times, &pending)) {
goto exit;
}
} }
} } /* for i */
} /* for i */ } /* for times */
} /* for times */ count += times;
count += times; } while (bench_stats_sym_check(start));
} while (bench_stats_sym_check(start));
exit: exit:
bench_stats_asym_finish("RSA", rsaKeySz, "private", doAsync, count, start, ret); bench_stats_asym_finish("RSA", rsaKeySz, "private", doAsync, count,
#else start, ret);
/* begin public RSA */
bench_stats_start(&count, &start);
do {
for (times = 0; times < ntimes || pending > 0; ) {
bench_async_poll(&pending);
/* while free pending slots in queue, submit ops */
for (i = 0; i < BENCH_MAX_PENDING; i++) {
if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]), 1, &times, ntimes, &pending)) {
ret = wc_RsaSSL_Sign(message, len, enc[i],
RSA_BUF_SIZE, &rsaKey[i], &rng);
if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]), 1, &times, &pending)) {
goto exit_rsa_pub;
}
}
} /* for i */
} /* for times */
count += times;
} while (bench_stats_sym_check(start));
exit_rsa_pub:
bench_stats_asym_finish("RSA", rsaKeySz, "private", doAsync, count, start, ret);
if (ret < 0) {
goto exit;
} }
else {
/* begin RSA sign */
bench_stats_start(&count, &start);
do {
for (times = 0; times < ntimes || pending > 0; ) {
bench_async_poll(&pending);
/* capture resulting encrypt length */ /* while free pending slots in queue, submit ops */
idx = rsaKeySz/8; for (i = 0; i < BENCH_MAX_PENDING; i++) {
if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]),
/* begin private async RSA */ 1, &times, ntimes, &pending)) {
bench_stats_start(&count, &start); ret = wc_RsaSSL_Sign(message, len, enc[i],
do { RSA_BUF_SIZE, &rsaKey[i], &rng);
for (times = 0; times < ntimes || pending > 0; ) { if (!bench_async_handle(&ret,
bench_async_poll(&pending); BENCH_ASYNC_GET_DEV(&rsaKey[i]),
1, &times, &pending)) {
/* while free pending slots in queue, submit ops */ goto exit_rsa_sign;
for (i = 0; i < BENCH_MAX_PENDING; i++) { }
if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]), 1, &times, ntimes, &pending)) {
ret = wc_RsaSSL_Verify(enc[i], idx, out[i],
RSA_BUF_SIZE, &rsaKey[i]);
if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]), 1, &times, &pending)) {
goto exit;
} }
} } /* for i */
} /* for i */ } /* for times */
} /* for times */ count += times;
count += times; } while (bench_stats_sym_check(start));
} while (bench_stats_sym_check(start)); exit_rsa_sign:
exit: bench_stats_asym_finish("RSA", rsaKeySz, "sign", doAsync, count, start,
bench_stats_asym_finish("RSA", rsaKeySz, "public", doAsync, count, start, ret); ret);
#endif
if (ret < 0) {
goto exit;
}
/* capture resulting encrypt length */
idx = rsaKeySz/8;
/* begin RSA verify */
bench_stats_start(&count, &start);
do {
for (times = 0; times < ntimes || pending > 0; ) {
bench_async_poll(&pending);
/* while free pending slots in queue, submit ops */
for (i = 0; i < BENCH_MAX_PENDING; i++) {
if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]),
1, &times, ntimes, &pending)) {
ret = wc_RsaSSL_Verify(enc[i], idx, out[i],
RSA_BUF_SIZE, &rsaKey[i]);
if (!bench_async_handle(&ret,
BENCH_ASYNC_GET_DEV(&rsaKey[i]),
1, &times, &pending)) {
goto exit_rsa_verify;
}
}
} /* for i */
} /* for times */
count += times;
} while (bench_stats_sym_check(start));
exit_rsa_verify:
bench_stats_asym_finish("RSA", rsaKeySz, "verify", doAsync, count,
start, ret);
}
/* cleanup */ /* cleanup */
for (i = 0; i < BENCH_MAX_PENDING; i++) { for (i = 0; i < BENCH_MAX_PENDING; i++) {
@ -3934,7 +3955,7 @@ exit_dh_gen:
count += times; count += times;
} while (bench_stats_sym_check(start)); } while (bench_stats_sym_check(start));
exit: exit:
bench_stats_asym_finish("DH", dhKeySz, "key agree", doAsync, count, start, ret); bench_stats_asym_finish("DH", dhKeySz, "agree", doAsync, count, start, ret);
/* cleanup */ /* cleanup */
for (i = 0; i < BENCH_MAX_PENDING; i++) { for (i = 0; i < BENCH_MAX_PENDING; i++) {
@ -4520,7 +4541,7 @@ void bench_curve25519KeyAgree(void)
count += i; count += i;
} while (bench_stats_sym_check(start)); } while (bench_stats_sym_check(start));
exit: exit:
bench_stats_asym_finish("CURVE", 25519, "key agree", 0, count, start, ret); bench_stats_asym_finish("CURVE", 25519, "agree", 0, count, start, ret);
wc_curve25519_free(&genKey2); wc_curve25519_free(&genKey2);
wc_curve25519_free(&genKey); wc_curve25519_free(&genKey);
@ -4797,6 +4818,9 @@ static void Usage(void)
printf("-no_aad No additional authentication data passed.\n"); printf("-no_aad No additional authentication data passed.\n");
#endif #endif
printf("-dgst_full Full digest operation performed.\n"); printf("-dgst_full Full digest operation performed.\n");
#ifndef NO_RSA
printf("-rsa_sign Measure RSA sign/verify instead of encrypt/decrypt.\n");
#endif
#ifndef WOLFSSL_BENCHMARK_ALL #ifndef WOLFSSL_BENCHMARK_ALL
printf("-<alg> Algorithm to benchmark. Available algorithms " printf("-<alg> Algorithm to benchmark. Available algorithms "
"include:\n"); "include:\n");
@ -4858,6 +4882,10 @@ int main(int argc, char** argv)
#endif #endif
else if (string_matches(argv[1], "-dgst_full")) else if (string_matches(argv[1], "-dgst_full"))
digest_stream = 0; digest_stream = 0;
#ifndef NO_RSA
else if (string_matches(argv[1], "-rsa_sign"))
rsa_sign_verify = 1;
#endif
else if (argv[1][0] == '-') { else if (argv[1][0] == '-') {
optMatched = 0; optMatched = 0;
#ifndef WOLFSSL_BENCHMARK_ALL #ifndef WOLFSSL_BENCHMARK_ALL