From f3e0d9a140db38c86ea2ade4dc919be8403c18a0 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Thu, 18 Apr 2013 12:41:09 +0200 Subject: [PATCH] Make the crypto_onetimeauth_poly1305() implementation switchable at runtime --- src/libsodium/Makefile.am | 1 + .../poly1305/53/crypto_onetimeauth.h | 9 +--- .../poly1305/onetimeauth_poly1305.c | 43 +++++++++++++++++++ .../poly1305/ref/auth_poly1305.c | 6 +++ .../poly1305/ref/crypto_onetimeauth.h | 9 +--- src/libsodium/include/Makefile.am | 1 + .../sodium/crypto_onetimeauth_poly1305.h | 38 +++++++++++++--- .../sodium/crypto_onetimeauth_poly1305_ref.h | 24 +++++++++++ 8 files changed, 110 insertions(+), 21 deletions(-) create mode 100644 src/libsodium/crypto_onetimeauth/poly1305/onetimeauth_poly1305.c create mode 100644 src/libsodium/include/sodium/crypto_onetimeauth_poly1305_ref.h diff --git a/src/libsodium/Makefile.am b/src/libsodium/Makefile.am index f70abe9d..97c2bca0 100644 --- a/src/libsodium/Makefile.am +++ b/src/libsodium/Makefile.am @@ -52,6 +52,7 @@ libsodium_la_SOURCES = \ crypto_stream/xsalsa20/ref/crypto_stream.h \ crypto_stream/xsalsa20/ref/stream_xsalsa20.c \ crypto_stream/xsalsa20/ref/xor_xsalsa20.c \ + crypto_onetimeauth/poly1305/onetimeauth_poly1305.c \ crypto_onetimeauth/poly1305/ref/auth_poly1305.c \ crypto_onetimeauth/poly1305/ref/crypto_onetimeauth.h \ crypto_onetimeauth/poly1305/ref/verify_poly1305.c \ diff --git a/src/libsodium/crypto_onetimeauth/poly1305/53/crypto_onetimeauth.h b/src/libsodium/crypto_onetimeauth/poly1305/53/crypto_onetimeauth.h index 450e122d..bd052c69 100644 --- a/src/libsodium/crypto_onetimeauth/poly1305/53/crypto_onetimeauth.h +++ b/src/libsodium/crypto_onetimeauth/poly1305/53/crypto_onetimeauth.h @@ -3,12 +3,7 @@ #include "crypto_onetimeauth_poly1305.h" -#define crypto_onetimeauth crypto_onetimeauth_poly1305 -#define crypto_onetimeauth_verify crypto_onetimeauth_poly1305_verify -#define crypto_onetimeauth_BYTES crypto_onetimeauth_poly1305_BYTES -#define crypto_onetimeauth_KEYBYTES crypto_onetimeauth_poly1305_KEYBYTES -#define crypto_onetimeauth_PRIMITIVE "poly1305" -#define crypto_onetimeauth_IMPLEMENTATION crypto_onetimeauth_poly1305_IMPLEMENTATION -#define crypto_onetimeauth_VERSION crypto_onetimeauth_poly1305_VERSION +#define crypto_onetimeauth crypto_onetimeauth_poly1305_53 +#define crypto_onetimeauth_verify crypto_onetimeauth_poly1305_53_verify #endif diff --git a/src/libsodium/crypto_onetimeauth/poly1305/onetimeauth_poly1305.c b/src/libsodium/crypto_onetimeauth/poly1305/onetimeauth_poly1305.c new file mode 100644 index 00000000..8bc0523b --- /dev/null +++ b/src/libsodium/crypto_onetimeauth/poly1305/onetimeauth_poly1305.c @@ -0,0 +1,43 @@ + +#include + +#include +#include +#include + +#include "crypto_onetimeauth_poly1305.h" +#include "crypto_onetimeauth_poly1305_ref.h" + +static crypto_onetimeauth_poly1305_implementation implementation = { + .implementation_name = crypto_onetimeauth_poly1305_ref_implementation_name, + .onetimeauth = crypto_onetimeauth_poly1305_ref, + .onetimeauth_verify = crypto_onetimeauth_poly1305_ref_verify +}; + +int +crypto_onetimeauth_poly1305_set_implementation(crypto_onetimeauth_poly1305_implementation *impl) +{ + implementation = *impl; + + return 0; +} + +const char * +crypto_onetimeauth_poly1305_implementation_name(void) +{ + return implementation.implementation_name(); +} + +int +crypto_onetimeauth_poly1305(unsigned char *out, const unsigned char *in, + unsigned long long inlen, const unsigned char *k) +{ + return implementation.onetimeauth(out, in, inlen, k); +} + +int +crypto_onetimeauth_poly1305_verify(const unsigned char *h, const unsigned char *in, + unsigned long long inlen, const unsigned char *k) +{ + return implementation.onetimeauth_verify(h, in, inlen, k); +} diff --git a/src/libsodium/crypto_onetimeauth/poly1305/ref/auth_poly1305.c b/src/libsodium/crypto_onetimeauth/poly1305/ref/auth_poly1305.c index 06cf115d..7485295a 100644 --- a/src/libsodium/crypto_onetimeauth/poly1305/ref/auth_poly1305.c +++ b/src/libsodium/crypto_onetimeauth/poly1305/ref/auth_poly1305.c @@ -102,3 +102,9 @@ int crypto_onetimeauth(unsigned char *out,const unsigned char *in,unsigned long for (j = 0;j < 16;++j) out[j] = h[j]; return 0; } + +const char * +crypto_onetimeauth_poly1305_ref_implementation_name(void) +{ + return "ref"; +} diff --git a/src/libsodium/crypto_onetimeauth/poly1305/ref/crypto_onetimeauth.h b/src/libsodium/crypto_onetimeauth/poly1305/ref/crypto_onetimeauth.h index 450e122d..30f27f43 100644 --- a/src/libsodium/crypto_onetimeauth/poly1305/ref/crypto_onetimeauth.h +++ b/src/libsodium/crypto_onetimeauth/poly1305/ref/crypto_onetimeauth.h @@ -3,12 +3,7 @@ #include "crypto_onetimeauth_poly1305.h" -#define crypto_onetimeauth crypto_onetimeauth_poly1305 -#define crypto_onetimeauth_verify crypto_onetimeauth_poly1305_verify -#define crypto_onetimeauth_BYTES crypto_onetimeauth_poly1305_BYTES -#define crypto_onetimeauth_KEYBYTES crypto_onetimeauth_poly1305_KEYBYTES -#define crypto_onetimeauth_PRIMITIVE "poly1305" -#define crypto_onetimeauth_IMPLEMENTATION crypto_onetimeauth_poly1305_IMPLEMENTATION -#define crypto_onetimeauth_VERSION crypto_onetimeauth_poly1305_VERSION +#define crypto_onetimeauth crypto_onetimeauth_poly1305_ref +#define crypto_onetimeauth_verify crypto_onetimeauth_poly1305_ref_verify #endif diff --git a/src/libsodium/include/Makefile.am b/src/libsodium/include/Makefile.am index fbedf106..ab888c28 100644 --- a/src/libsodium/include/Makefile.am +++ b/src/libsodium/include/Makefile.am @@ -18,6 +18,7 @@ SODIUM_EXPORT = \ sodium/crypto_hashblocks_sha512.h \ sodium/crypto_onetimeauth.h \ sodium/crypto_onetimeauth_poly1305.h \ + sodium/crypto_onetimeauth_poly1305_ref.h \ sodium/crypto_scalarmult_curve25519.h \ sodium/crypto_secretbox.h \ sodium/crypto_secretbox_xsalsa20poly1305.h \ diff --git a/src/libsodium/include/sodium/crypto_onetimeauth_poly1305.h b/src/libsodium/include/sodium/crypto_onetimeauth_poly1305.h index 7e88177c..791dc1d3 100644 --- a/src/libsodium/include/sodium/crypto_onetimeauth_poly1305.h +++ b/src/libsodium/include/sodium/crypto_onetimeauth_poly1305.h @@ -6,16 +6,40 @@ #ifdef __cplusplus extern "C" { #endif -extern int crypto_onetimeauth_poly1305_ref(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *); -extern int crypto_onetimeauth_poly1305_ref_verify(const unsigned char *,const unsigned char *,unsigned long long,const unsigned char *); -extern int crypto_onetimeauth_poly1305_53(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *); -extern int crypto_onetimeauth_poly1305_53_verify(const unsigned char *,const unsigned char *,unsigned long long,const unsigned char *); +#include + +#include +#include + +typedef struct crypto_onetimeauth_poly1305_implementation { + const char *(*implementation_name)(void); + int (*onetimeauth)(unsigned char *out, + const unsigned char *in, + unsigned long long inlen, + const unsigned char *k); + int (*onetimeauth_verify)(const unsigned char *h, + const unsigned char *in, + unsigned long long inlen, + const unsigned char *k); +} crypto_onetimeauth_poly1305_implementation; + +const char *crypto_onetimeauth_poly1305_ref_implementation_name(void); + +int crypto_onetimeauth_poly1305_set_implementation(crypto_onetimeauth_poly1305_implementation *impl); + +int crypto_onetimeauth_poly1305(unsigned char *out, + const unsigned char *in, + unsigned long long inlen, + const unsigned char *k); + +int crypto_onetimeauth_poly1305_verify(const unsigned char *h, + const unsigned char *in, + unsigned long long inlen, + const unsigned char *k); + #ifdef __cplusplus } #endif -#define crypto_onetimeauth_poly1305 crypto_onetimeauth_poly1305_ref -#define crypto_onetimeauth_poly1305_verify crypto_onetimeauth_poly1305_ref_verify - #endif diff --git a/src/libsodium/include/sodium/crypto_onetimeauth_poly1305_ref.h b/src/libsodium/include/sodium/crypto_onetimeauth_poly1305_ref.h new file mode 100644 index 00000000..6bb61562 --- /dev/null +++ b/src/libsodium/include/sodium/crypto_onetimeauth_poly1305_ref.h @@ -0,0 +1,24 @@ +#ifndef crypto_onetimeauth_poly1305_ref_H +#define crypto_onetimeauth_poly1305_ref_H + +#include "crypto_onetimeauth_poly1305.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int crypto_onetimeauth_poly1305_ref(unsigned char *out, + const unsigned char *in, + unsigned long long inlen, + const unsigned char *k); + +int crypto_onetimeauth_poly1305_ref_verify(const unsigned char *h, + const unsigned char *in, + unsigned long long inlen, + const unsigned char *k); + +#ifdef __cplusplus +} +#endif + +#endif