From fe252b809326626d8744cdeb6548183db3b648f5 Mon Sep 17 00:00:00 2001 From: Joost Rijneveld Date: Wed, 1 Nov 2017 14:59:33 +0100 Subject: [PATCH] Move ull-byte-conversions to separate utils file --- Makefile | 6 +++--- hash.c | 2 +- utils.c | 30 ++++++++++++++++++++++++++++++ utils.h | 15 +++++++++++++++ wots.c | 2 +- xmss_commons.c | 30 +----------------------------- xmss_commons.h | 11 ----------- xmss_core.c | 1 + xmss_core_fast.c | 1 + 9 files changed, 53 insertions(+), 45 deletions(-) create mode 100644 utils.c create mode 100644 utils.h diff --git a/Makefile b/Makefile index 165a129..398a4d4 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,9 @@ CC = /usr/bin/gcc CFLAGS = -Wall -g -O3 -Wextra -Wpedantic -LDLIBS = -lcrypto +LDLIBS = -lcrypto -SOURCES = params.c hash.c fips202.c hash_address.c randombytes.c wots.c xmss.c xmss_core.c xmss_commons.c -HEADERS = params.h hash.h fips202.h hash_address.h randombytes.h wots.h xmss.h xmss_core.h xmss_commons.h +SOURCES = params.c hash.c fips202.c hash_address.c randombytes.c wots.c xmss.c xmss_core.c xmss_commons.c utils.c +HEADERS = params.h hash.h fips202.h hash_address.h randombytes.h wots.h xmss.h xmss_core.h xmss_commons.h utils.h SOURCES_FAST = $(subst xmss_core.c,xmss_core_fast.c,$(SOURCES)) HEADERS_FAST = $(subst xmss_core.c,xmss_core_fast.c,$(HEADERS)) diff --git a/hash.c b/hash.c index 0f9aa23..ae7c4b4 100644 --- a/hash.c +++ b/hash.c @@ -3,7 +3,7 @@ #include #include "hash_address.h" -#include "xmss_commons.h" +#include "utils.h" #include "params.h" #include "hash.h" #include "fips202.h" diff --git a/utils.c b/utils.c new file mode 100644 index 0000000..855f636 --- /dev/null +++ b/utils.c @@ -0,0 +1,30 @@ +#include "utils.h" + +/** + * Converts the value of 'in' to 'outlen' bytes in big-endian byte order. + */ +void ull_to_bytes(unsigned char *out, unsigned int outlen, + unsigned long long in) +{ + int i; + + /* Iterate over out in decreasing order, for big-endianness. */ + for (i = outlen - 1; i >= 0; i--) { + out[i] = in & 0xff; + in = in >> 8; + } +} + +/** + * Converts the inlen bytes in 'in' from big-endian byte order to an integer. + */ +unsigned long long bytes_to_ull(const unsigned char *in, unsigned int inlen) +{ + unsigned long long retval = 0; + unsigned int i; + + for (i = 0; i < inlen; i++) { + retval |= ((unsigned long long)in[i]) << (8*(inlen - 1 - i)); + } + return retval; +} diff --git a/utils.h b/utils.h new file mode 100644 index 0000000..32d00f1 --- /dev/null +++ b/utils.h @@ -0,0 +1,15 @@ +#ifndef XMSS_UTILS_H +#define XMSS_UTILS_H + +/** + * Converts the value of 'in' to 'outlen' bytes in big-endian byte order. + */ +void ull_to_bytes(unsigned char *out, unsigned int outlen, + unsigned long long in); + +/** + * Converts the inlen bytes in 'in' from big-endian byte order to an integer. + */ +unsigned long long bytes_to_ull(const unsigned char *in, unsigned int inlen); + +#endif diff --git a/wots.c b/wots.c index ad26580..a138305 100644 --- a/wots.c +++ b/wots.c @@ -1,7 +1,7 @@ #include #include -#include "xmss_commons.h" +#include "utils.h" #include "hash.h" #include "wots.h" #include "hash_address.h" diff --git a/xmss_commons.c b/xmss_commons.c index 6015cfa..a4460c2 100644 --- a/xmss_commons.c +++ b/xmss_commons.c @@ -6,37 +6,9 @@ #include "hash_address.h" #include "params.h" #include "wots.h" +#include "utils.h" #include "xmss_commons.h" -/** - * Converts the value of 'in' to 'outlen' bytes in big-endian byte order. - */ -void ull_to_bytes(unsigned char *out, unsigned int outlen, - unsigned long long in) -{ - int i; - - /* Iterate over out in decreasing order, for big-endianness. */ - for (i = outlen - 1; i >= 0; i--) { - out[i] = in & 0xff; - in = in >> 8; - } -} - -/** - * Converts the inlen bytes in 'in' from big-endian byte order to an integer. - */ -unsigned long long bytes_to_ull(const unsigned char *in, unsigned int inlen) -{ - unsigned long long retval = 0; - unsigned int i; - - for (i = 0; i < inlen; i++) { - retval |= ((unsigned long long)in[i]) << (8*(inlen - 1 - i)); - } - return retval; -} - /** * Computes the leaf at a given address. First generates the WOTS key pair, * then computes leaf using l_tree. As this happens position independent, we diff --git a/xmss_commons.h b/xmss_commons.h index 7a8f307..cdd4c4f 100644 --- a/xmss_commons.h +++ b/xmss_commons.h @@ -4,17 +4,6 @@ #include #include "params.h" -/** - * Converts the value of 'in' to 'len' bytes in big-endian byte order. - */ -void ull_to_bytes(unsigned char *out, unsigned int outlen, - unsigned long long in); - -/** - * Converts the inlen bytes in 'in' from big-endian byte order to an integer. - */ -unsigned long long bytes_to_ull(const unsigned char *in, unsigned int inlen); - /** * Computes the leaf at a given address. First generates the WOTS key pair, * then computes leaf using l_tree. As this happens position independent, we diff --git a/xmss_core.c b/xmss_core.c index 859d4bd..498b5ad 100644 --- a/xmss_core.c +++ b/xmss_core.c @@ -7,6 +7,7 @@ #include "params.h" #include "randombytes.h" #include "wots.h" +#include "utils.h" #include "xmss_commons.h" #include "xmss_core.h" diff --git a/xmss_core_fast.c b/xmss_core_fast.c index e56ead3..351db9f 100644 --- a/xmss_core_fast.c +++ b/xmss_core_fast.c @@ -7,6 +7,7 @@ #include "params.h" #include "randombytes.h" #include "wots.h" +#include "utils.h" #include "xmss_commons.h" #include "xmss_core.h"