From 9ea67de935d307b413ba510be2de8731a13caa78 Mon Sep 17 00:00:00 2001 From: Loup Vaillant Date: Wed, 23 Oct 2019 23:39:48 +0200 Subject: [PATCH] Added Libhydrogen speed tests --- makefile | 28 +++++++++--- tests/externals/libhydrogen.pc | 11 +++++ tests/speed/speed-hydrogen.c | 78 ++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 6 deletions(-) create mode 100644 tests/externals/libhydrogen.pc create mode 100644 tests/speed/speed-hydrogen.c diff --git a/makefile b/makefile index ca28aed..684a6e2 100644 --- a/makefile +++ b/makefile @@ -13,10 +13,10 @@ else LINK_SHA512=lib/sha512.o endif -.PHONY: all library static-library dynamic-library \ - install install-doc \ - check test speed \ - clean uninstall \ +.PHONY: all library static-library dynamic-library \ + install install-doc pkg-config-libhydrogen \ + check test speed speed-sodium speed-tweetnacl \ + clean uninstall \ tarball all : library @@ -44,6 +44,10 @@ install-doc: mkdir -p $(MAN_DIR) cp -r doc/man/man3/*.3monocypher $(MAN_DIR) +pkg-config-libhydrogen: tests/externals/libhydrogen.pc + mkdir -p $(PKGCONFIG) + cp $< $(PKGCONFIG)/libhydrogen.pc + library: static-library dynamic-library static-library : lib/libmonocypher.a dynamic-library: lib/libmonocypher.so lib/libmonocypher.so.2 @@ -64,7 +68,8 @@ test : test.out speed : speed.out speed-sodium : speed-sodium.out speed-tweetnacl: speed-tweetnacl.out -test speed speed-sodium speed-tweetnacl: +speed-hydrogen : speed-hydrogen.out +test speed speed-sodium speed-tweetnacl speed-hydrogen: ./$< # Monocypher libraries @@ -101,15 +106,26 @@ lib/speed-sodium.o:$(SPEED)/speed-sodium.c $(TEST_COMMON) $(SPEED)/speed.h `pkg-config --cflags libsodium` \ -fPIC -c -o $@ $< +lib/speed-hydrogen.o:$(SPEED)/speed-hydrogen.c $(TEST_COMMON) $(SPEED)/speed.h + @mkdir -p $(@D) + $(CC) $(CFLAGS) \ + -I src -I src/optional -I tests \ + `pkg-config --cflags libhydrogen` \ + -fPIC -c -o $@ $< + # test & speed executables test.out : lib/test.o lib/monocypher.o lib/sha512.o speed.out: lib/speed.o lib/monocypher.o lib/sha512.o test.out speed.out: $(CC) $(CFLAGS) -I src -I src/optional -o $@ $^ speed-sodium.out: lib/speed-sodium.o - $(CC) $(CFLAGS) -o $@ $^ \ + $(CC) $(CFLAGS) -o $@ $^ \ `pkg-config --cflags libsodium` \ `pkg-config --libs libsodium` +speed-hydrogen.out: lib/speed-hydrogen.o + $(CC) $(CFLAGS) -o $@ $^ \ + `pkg-config --cflags libhydrogen` \ + `pkg-config --libs libhydrogen` lib/tweetnacl.o: tests/externals/tweetnacl.c tests/externals/tweetnacl.h $(CC) $(CFLAGS) -c -o $@ $< speed-tweetnacl.out: lib/speed-tweetnacl.o lib/tweetnacl.o diff --git a/tests/externals/libhydrogen.pc b/tests/externals/libhydrogen.pc new file mode 100644 index 0000000..932562a --- /dev/null +++ b/tests/externals/libhydrogen.pc @@ -0,0 +1,11 @@ +prefix=/usr/local +exec_prefix=${prefix} +libdir=${exec_prefix}/lib +includedir=${prefix}/include + +Name: libhydrogen +Version: git-f1f061d +Description: Small, easy-to-use, hard-to-misuse cryptographic library. + +Libs: -L${libdir} -lhydrogen +Cflags: -I${includedir} diff --git a/tests/speed/speed-hydrogen.c b/tests/speed/speed-hydrogen.c new file mode 100644 index 0000000..a32ee7d --- /dev/null +++ b/tests/speed/speed-hydrogen.c @@ -0,0 +1,78 @@ +#include "speed.h" +#include "utils.h" +#include "hydrogen.h" + +static u64 hydro_random(void) +{ + u8 out[SIZE]; + RANDOM_INPUT(key , 32); + RANDOM_INPUT(nonce, 8); + + TIMING_START { + hydro_random_buf_deterministic(out, SIZE, key); + } + TIMING_END; +} + +static u64 authenticated(void) +{ + u8 out[SIZE + hydro_secretbox_HEADERBYTES]; + RANDOM_INPUT(in , SIZE + 32); + RANDOM_INPUT(key, 32); + TIMING_START { + hydro_secretbox_encrypt(out, in, SIZE, 0, "Benchmark", key); + } + TIMING_END; +} + +static u64 hash(void) +{ + u8 hash[32]; + RANDOM_INPUT(in, SIZE); + + TIMING_START { + hydro_hash_hash(hash, 32, in, SIZE, "Benchmark", 0); + } + TIMING_END; +} + +static u64 sign(void) +{ + RANDOM_INPUT(message, 64); + hydro_sign_keypair key_pair; + hydro_sign_keygen(&key_pair); + uint8_t sig[hydro_sign_BYTES]; + + TIMING_START { + hydro_sign_create(sig, message, 64, "Benchmark", key_pair.sk); + } + TIMING_END; +} + +static u64 check(void) +{ + RANDOM_INPUT(message, 64); + hydro_sign_keypair key_pair; + hydro_sign_keygen(&key_pair); + uint8_t sig[hydro_sign_BYTES]; + hydro_sign_create(sig, message, 64, "Benchmark", key_pair.sk); + + TIMING_START { + if (hydro_sign_verify(sig, message, 64, "Benchmark", key_pair.pk)) { + printf("LibHydrogen verification failed\n"); + } + } + TIMING_END; +} + +int main() +{ + hydro_init(); + print("Random ",hydro_random() *MUL,"megabytes per second"); + print("Auth'd encryption",authenticated()*MUL,"megabytes per second"); + print("Hash ",hash() *MUL,"megabytes per second"); + print("sign ",sign() ,"signatures per second"); + print("check ",check() ,"checks per second"); + printf("\n"); + return 0; +} -- 2.47.3