From 57fe52c7c965037770febce691a3aade8e16975b Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Thu, 11 May 2017 10:42:15 +0100 Subject: [PATCH] progs/perftest.c: Allow setting the public exponent in RSA tests. --- progs/perftest.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/progs/perftest.c b/progs/perftest.c index b2722a4e..80f060aa 100644 --- a/progs/perftest.c +++ b/progs/perftest.c @@ -56,6 +56,8 @@ #include "mprand.h" #include "fibrand.h" #include "rsa.h" +#include "mpint.h" +#include "mptext.h" #include "mpmont.h" #include "mpbarrett.h" #include "dh.h" @@ -84,6 +86,7 @@ typedef struct opts { unsigned n; /* Number of factors */ unsigned i; /* Number of intervals (or zero) */ double t; /* Time for each interval (secs) */ + mp *e; /* Public exponent */ unsigned f; /* Flags */ #define OF_NOCHECK 1u /* Don't do group checking */ } opts; @@ -371,7 +374,8 @@ static void *rsapriv_init(opts *o) rsapriv_ctx *c = CREATE(rsapriv_ctx); if (!o->fbits) o->fbits = 1024; - rsa_gen(&c->rp, o->fbits, &rand_global, 0, pgen_evspin, 0); + if (!o->e) o->e = mp_fromulong(MP_NEW, 65537); + rsa_gen_e(&c->rp, o->fbits, o->e, &rand_global, 0, pgen_evspin, 0); rsa_privcreate(&c->rpc, &c->rp, 0); c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0); return (c); @@ -382,7 +386,8 @@ static void *rsaprivblind_init(opts *o) rsapriv_ctx *c = CREATE(rsapriv_ctx); if (!o->fbits) o->fbits = 1024; - rsa_gen(&c->rp, o->fbits, &rand_global, 0, pgen_evspin, 0); + if (!o->e) o->e = mp_fromulong(MP_NEW, 65537); + rsa_gen_e(&c->rp, o->fbits, o->e, &rand_global, 0, pgen_evspin, 0); rsa_privcreate(&c->rpc, &c->rp, fibrand_create(0)); c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0); return (c); @@ -407,7 +412,8 @@ static void *rsapub_init(opts *o) rsa_priv rp; if (!o->fbits) o->fbits = 1024; - rsa_gen(&rp, o->fbits, &rand_global, 0, pgen_evspin, 0); + if (!o->e) o->e = mp_fromulong(MP_NEW, 65537); + rsa_gen_e(&rp, o->fbits, o->e, &rand_global, 0, pgen_evspin, 0); c->rp.n = MP_COPY(rp.n); c->rp.e = MP_COPY(rp.e); rsa_privfree(&rp); @@ -650,6 +656,14 @@ static unsigned uarg(const char *what, const char *p) return (u); } +static mp *mparg(const char *what, const char *p) +{ + char *q; + mp *x = mp_readstring(MP_NEW, p, &q, 0); + if (!x || *q) die(1, "bad %s `%s'", what, p); + return (x); +} + static double farg(const char *what, const char *p) { char *q; @@ -687,12 +701,13 @@ int main(int argc, char *argv[]) { "group-bits", OPTF_ARGREQ, 0, 'B' }, { "factors", OPTF_ARGREQ, 0, 'n' }, { "intervals", OPTF_ARGREQ, 0, 'i' }, + { "public-exponent", OPTF_ARGREQ, 0, 'e' }, { "time", OPTF_ARGREQ, 0, 't' }, { "no-check", 0, 0, 'q' }, { 0, 0, 0, 0 } }; - i = mdwopt(argc, argv, "hvulC:b:B:n:i:t:q", opts, 0, 0, 0); + i = mdwopt(argc, argv, "hvulC:b:B:n:i:e:t:q", opts, 0, 0, 0); if (i < 0) break; switch (i) { case 'h': help(stdout); exit(0); @@ -703,6 +718,11 @@ int main(int argc, char *argv[]) case 'b': o.fbits = uarg("field bits", optarg); break; case 'B': o.gbits = uarg("subgroup bits", optarg); break; case 'n': o.n = uarg("factor count", optarg); break; + case 'e': + mp_drop(o.e); o.e = mparg("public exponent", optarg); + if (MP_CMP(o.e, <, MP_THREE) || MP_EVENP(o.e)) + die(1, "invalid public exponent"); + break; case 'i': o.i = uarg("interval count", optarg); break; case 't': o.t = farg("interval length", optarg); break; case 'q': o.f |= OF_NOCHECK; break; -- 2.11.0