Skip to content

Commit

Permalink
Add OQS KEM to speed
Browse files Browse the repository at this point in the history
  • Loading branch information
Douglas Stebila committed Apr 10, 2018
1 parent 06196e7 commit cde6dd0
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
/out.*
/tmp.*

# OQS
/oqs

# Most *.c files under test/ are symlinks
/test/*.c
# Apart from these
Expand Down
12 changes: 6 additions & 6 deletions apps/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
DIR= apps
TOP= ..
CC= cc
INCLUDES= -I$(TOP) -I../include $(KRB5_INCLUDES)
INCLUDES= -I$(TOP) -I../include -I../oqs/include $(KRB5_INCLUDES)
CFLAG= -g -static
MAKEFILE= Makefile
PERL= perl
Expand All @@ -15,18 +15,18 @@ KRB5_INCLUDES=
LIBKRB5=

PEX_LIBS=
EX_LIBS=
EXE_EXT=
EX_LIBS=
EXE_EXT=

SHLIB_TARGET=

CFLAGS= -DMONOLITH $(INCLUDES) $(CFLAG)

GENERAL=Makefile makeapps.com install.com

DLIBCRYPTO=../libcrypto.a
DLIBCRYPTO=../libcrypto.a ../oqs/liboqs.a
DLIBSSL=../libssl.a
LIBCRYPTO=-L.. -lcrypto
LIBCRYPTO=-L.. -lcrypto -L../oqs -loqs
LIBSSL=-L.. -lssl

PROGRAM= openssl
Expand Down Expand Up @@ -91,7 +91,7 @@ req: sreq.o $(A_OBJ) $(DLIBCRYPTO)
LIBDEPS="$(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)" \
link_app.$${shlib_target}

sreq.o: req.c
sreq.o: req.c
$(CC) -c $(INCLUDES) $(CFLAG) -o sreq.o req.c

openssl-vms.cnf: openssl.cnf
Expand Down
185 changes: 185 additions & 0 deletions apps/speed.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
# define DSA_SECONDS 10
# define ECDSA_SECONDS 10
# define ECDH_SECONDS 10
# define OQSKEM_SECONDS 10

/* 11-Sep-92 Andrew Daviel Support for Silicon Graphics IRIX added */
/* 06-Apr-92 Luke Brennan Support for VMS and add extra signal calls */
Expand Down Expand Up @@ -191,6 +192,9 @@
# ifndef OPENSSL_NO_ECDH
# include <openssl/ecdh.h>
# endif
# ifndef OPENSSL_NO_OQSKEM
# include <oqs/oqs.h>
# endif
# include <openssl/modes.h>

# ifdef OPENSSL_FIPS
Expand Down Expand Up @@ -245,6 +249,7 @@ static int do_multi(int multi);
# define SIZE_NUM 5
# define RSA_NUM 4
# define DSA_NUM 3
# define OQSKEM_NUM 1

# define EC_NUM 16
# define MAX_ECDH_SIZE 256
Expand Down Expand Up @@ -274,6 +279,9 @@ static double ecdsa_results[EC_NUM][2];
# ifndef OPENSSL_NO_ECDH
static double ecdh_results[EC_NUM][1];
# endif
# ifndef OPENSSL_NO_OQSKEM
static double oqskem_results[OQSKEM_NUM][4];
# endif

# if defined(OPENSSL_NO_DSA) && !(defined(OPENSSL_NO_ECDSA) && defined(OPENSSL_NO_ECDH))
static const char rnd_seed[] =
Expand Down Expand Up @@ -545,6 +553,8 @@ int MAIN(int argc, char **argv)
# define R_EC_B409 14
# define R_EC_B571 15

# define R_OQSKEM_DEFAULT 0

# ifndef OPENSSL_NO_RSA
RSA *rsa_key[RSA_NUM];
long rsa_c[RSA_NUM][2];
Expand Down Expand Up @@ -618,6 +628,12 @@ int MAIN(int argc, char **argv)

# endif

# ifndef OPENSSL_NO_OQSKEM
static const char *test_oqskem_names[OQSKEM_NUM] = {
"default",
};
# endif

# ifndef OPENSSL_NO_ECDSA
unsigned char ecdsasig[256];
unsigned int ecdsasiglen;
Expand All @@ -634,13 +650,26 @@ int MAIN(int argc, char **argv)
long ecdh_c[EC_NUM][2];
# endif

# ifndef OPENSSL_NO_OQSKEM
OQS_KEM *oqskem_kem[OQSKEM_NUM];
unsigned char *oqskem_secret_key;
unsigned char *oqskem_public_key;
unsigned char *oqskem_ciphertext;
unsigned char *oqskem_shared_secret_e;
unsigned char *oqskem_shared_secret_d;
long oqskem_c[OQSKEM_NUM][3];
# endif

int rsa_doit[RSA_NUM];
int dsa_doit[DSA_NUM];
# ifndef OPENSSL_NO_ECDSA
int ecdsa_doit[EC_NUM];
# endif
# ifndef OPENSSL_NO_ECDH
int ecdh_doit[EC_NUM];
# endif
# ifndef OPENSSL_NO_OQSKEM
int oqskem_doit[OQSKEM_NUM];
# endif
int doit[ALGOR_NUM];
int pr_header = 0;
Expand Down Expand Up @@ -671,6 +700,16 @@ int MAIN(int argc, char **argv)
ecdh_b[i] = NULL;
}
# endif
# ifndef OPENSSL_NO_OQSKEM
for (i=0; i<OQSKEM_NUM; i++) {
oqskem_kem[i] = NULL;
}
oqskem_secret_key = NULL;
oqskem_public_key = NULL;
oqskem_ciphertext = NULL;
oqskem_shared_secret_d = NULL;
oqskem_shared_secret_e = NULL;
# endif
# ifndef OPENSSL_NO_RSA
for (i = 0; i < RSA_NUM; i++)
rsa_key[i] = NULL;
Expand Down Expand Up @@ -710,6 +749,10 @@ int MAIN(int argc, char **argv)
for (i = 0; i < EC_NUM; i++)
ecdh_doit[i] = 0;
# endif
# ifndef OPENSSL_NO_OQSKEM
for (i=0; i<OQSKEM_NUM; i++)
oqskem_doit[i]=0;
# endif

j = 0;
argc--;
Expand Down Expand Up @@ -1063,6 +1106,14 @@ int MAIN(int argc, char **argv)
for (i = 0; i < EC_NUM; i++)
ecdh_doit[i] = 1;
} else
# endif
# ifndef OPENSSL_NO_OQSKEM
if (strcmp(*argv, "oqskem_default") == 0)
oqskem_doit[R_OQSKEM_DEFAULT] = 2;
else if (strcmp(*argv, "oqskem") == 0) {
for (i = 0; i < OQSKEM_NUM; i++)
oqskem_doit[i] = 1;
} else
# endif
{
BIO_printf(bio_err, "Error: bad option or value\n");
Expand Down Expand Up @@ -1166,6 +1217,10 @@ int MAIN(int argc, char **argv)
"ecdhb163 ecdhb233 ecdhb283 ecdhb409 ecdhb571\n");
BIO_printf(bio_err, "ecdh\n");
# endif
# ifndef OPENSSL_NO_OQSKEM
BIO_printf(bio_err, "oqskem_default\n");
BIO_printf(bio_err, "oqskem\n");
# endif

# ifndef OPENSSL_NO_IDEA
BIO_printf(bio_err, "idea ");
Expand Down Expand Up @@ -1248,6 +1303,10 @@ int MAIN(int argc, char **argv)
# ifndef OPENSSL_NO_ECDH
for (i = 0; i < EC_NUM; i++)
ecdh_doit[i] = 1;
# endif
# ifndef OPENSSL_NO_OQSKEM
for (i = 0; i < OQSKEM_NUM; i++)
oqskem_doit[i] = 1;
# endif
}
for (i = 0; i < ALGOR_NUM; i++)
Expand Down Expand Up @@ -1531,6 +1590,12 @@ int MAIN(int argc, char **argv)
}
}
# endif
# ifndef OPENSSL_NO_OQSKEM
for (i = 0; i <= OQSKEM_NUM; i++) {
oqskem_c[i][0] = count/1000;
oqskem_c[i][1] = count/1000;
}
# endif

# define COND(d) (count < (d))
# define COUNT(d) (d)
Expand Down Expand Up @@ -2355,6 +2420,85 @@ int MAIN(int argc, char **argv)
if (rnd_fake)
RAND_cleanup();
# endif
# ifndef OPENSSL_NO_OQSKEM
if (RAND_status() != 1) {
RAND_seed(rnd_seed, sizeof rnd_seed);
rnd_fake = 1;
}
for (j = 0; j < OQSKEM_NUM; j++) {
if (!oqskem_doit[j])
continue;

if (j == R_OQSKEM_DEFAULT) {
oqskem_kem[j] = OQS_KEM_new(OQS_KEM_alg_default);
}
if (oqskem_kem[j] == NULL) {
BIO_printf(bio_err,"OQSKEM failure - OQS_KEM_new.\n");
ERR_print_errors(bio_err);
rsa_count=1;
} else {
oqskem_public_key = malloc(oqskem_kem[j]->length_public_key);
oqskem_secret_key = malloc(oqskem_kem[j]->length_secret_key);
oqskem_ciphertext = malloc(oqskem_kem[j]->length_ciphertext);
oqskem_shared_secret_d = malloc(oqskem_kem[j]->length_shared_secret);
oqskem_shared_secret_e = malloc(oqskem_kem[j]->length_shared_secret);

/* time OQSKEM keypair operation */
char lbl[1000];
sprintf(lbl, "OQS KEM %s", oqskem_kem[j]->method_name);
pkey_print_message(lbl, "keypair", oqskem_c[j][0], 0, OQSKEM_SECONDS);
Time_F(START);
for (count = 0, run = 1; COND(oqskem_c[j][0]); count++) {
OQS_KEM_keypair(oqskem_kem[j], oqskem_public_key, oqskem_secret_key);
}
d = Time_F(STOP);
sprintf(lbl, "%%ld OQS KEM %s keypair in %%.2fs\n", oqskem_kem[j]->method_name);
BIO_printf(bio_err, mr ? "+R9:%ld:%.2f\n" : lbl, count, d);
oqskem_results[j][0] = d / (double)count;
rsa_count = count;

/* time OQSKEM encaps operation */
sprintf(lbl, "OQS KEM %s", oqskem_kem[j]->method_name);
pkey_print_message(lbl, "encaps", oqskem_c[j][1], 0, OQSKEM_SECONDS);
Time_F(START);
for (count = 0, run = 1; COND(oqskem_c[j][1]); count++) {
OQS_KEM_encaps(oqskem_kem[j], oqskem_ciphertext, oqskem_shared_secret_e, oqskem_public_key);
}
d = Time_F(STOP);
sprintf(lbl, "%%ld OQS KEM %s encaps in %%.2fs\n", oqskem_kem[j]->method_name);
BIO_printf(bio_err, mr ? "+R10:%ld:%.2f\n" : lbl, count, d);
oqskem_results[j][1] = d / (double)count;
rsa_count = count;

/* time OQSKEM decaps operation */
sprintf(lbl, "OQS KEM %s", oqskem_kem[j]->method_name);
pkey_print_message(lbl, "decaps", oqskem_c[j][2], 0, OQSKEM_SECONDS);
Time_F(START);
for (count = 0, run = 1; COND(oqskem_c[j][2]); count++) {
OQS_KEM_decaps(oqskem_kem[j], oqskem_shared_secret_d, oqskem_ciphertext, oqskem_secret_key);
}
d = Time_F(STOP);
sprintf(lbl, "%%ld OQS KEM %s decaps in %%.2fs\n", oqskem_kem[j]->method_name);
BIO_printf(bio_err, mr ? "+R11:%ld:%.2f\n" : lbl, count, d);
oqskem_results[j][2] = d / (double)count;
rsa_count = count;
OQS_MEM_insecure_free(oqskem_public_key);
OQS_MEM_insecure_free(oqskem_ciphertext);
OQS_MEM_secure_free(oqskem_secret_key, oqskem_kem[j]->length_secret_key);
OQS_MEM_secure_free(oqskem_shared_secret_d, oqskem_kem[j]->length_shared_secret);
OQS_MEM_secure_free(oqskem_shared_secret_e, oqskem_kem[j]->length_shared_secret);
}
OQS_KEM_free(oqskem_kem[j]);

if (rsa_count <= 1) {
/* if longer than 10s, don't do any more */
for (j++; j < OQSKEM_NUM; j++)
oqskem_doit[j] = 0;
}
}
if (rnd_fake)
RAND_cleanup();
# endif
# ifndef NO_FORK
show_res:
# endif
Expand Down Expand Up @@ -2494,6 +2638,29 @@ int MAIN(int argc, char **argv)
}
# endif

# ifndef OPENSSL_NO_OQSKEM
j = 1;
for (k = 0; k < OQSKEM_NUM; k++) {
if (!oqskem_doit[k])
continue;
if (j && !mr) {
printf(" keypair keypair/s encaps encaps/s decaps decaps/s\n");
j = 0;
}
if (mr)
fprintf(stdout,"+F6:%u:%s:%f:%f:%f\n",
k, test_oqskem_names[k],
oqskem_results[k][0], oqskem_results[k][1], oqskem_results[k][2]);

else
fprintf(stdout,"oqskem (%-15s) %8.6fms %8.1f %8.6fms %8.1f %8.6fms %8.1f\n",
test_oqskem_names[k],
oqskem_results[k][0] * 1000, 1.0/oqskem_results[k][0],
oqskem_results[k][1] * 1000, 1.0/oqskem_results[k][1],
oqskem_results[k][2] * 1000, 1.0/oqskem_results[k][2]);
}
# endif

mret = 0;

end:
Expand Down Expand Up @@ -2757,6 +2924,24 @@ static int do_multi(int multi)
}
# endif

# ifndef OPENSSL_NO_OQSKEM
else if (!strncmp(buf,"+F6:",4)) {
int k;
double d;

p = buf + 4;
k = atoi(sstrsep(&p, sep));
sstrsep(&p, sep);

d = atof(sstrsep(&p, sep));
if(n)
oqskem_results[k][0] = 1 / (1 / oqskem_results[k][0] + 1 / d);
else
oqskem_results[k][0] = d;

}
# endif

else if (!strncmp(buf, "+H:", 3)) {
} else
fprintf(stderr, "Unknown type '%s' from child %d\n", buf, n);
Expand Down

0 comments on commit cde6dd0

Please sign in to comment.