Simplify crypto_onetimeauth_poly1305() internal implementation API

This commit is contained in:
Frank Denis
2015-11-01 12:18:30 +01:00
parent 1d15772716
commit 68c52f2188
10 changed files with 41 additions and 100 deletions
+1 -2
View File
@@ -51,8 +51,7 @@ libsodium_la_SOURCES = \
crypto_hash/sha512/cp/hash_sha512.c \
crypto_onetimeauth/crypto_onetimeauth.c \
crypto_onetimeauth/poly1305/onetimeauth_poly1305.c \
crypto_onetimeauth/poly1305/onetimeauth_poly1305_api.c \
crypto_onetimeauth/poly1305/onetimeauth_poly1305_try.c \
crypto_onetimeauth/poly1305/onetimeauth_poly1305.h \
crypto_onetimeauth/poly1305/donna/poly1305_donna.h \
crypto_onetimeauth/poly1305/donna/poly1305_donna32.h \
crypto_onetimeauth/poly1305/donna/poly1305_donna64.h \
@@ -6,6 +6,7 @@
#else
# include "poly1305_donna32.h"
#endif
#include "../onetimeauth_poly1305.h"
static void
poly1305_update(poly1305_context *ctx, const unsigned char *m,
@@ -45,7 +46,7 @@ poly1305_update(poly1305_context *ctx, const unsigned char *m,
}
}
int
static int
crypto_onetimeauth_poly1305_donna(unsigned char *out, const unsigned char *m,
unsigned long long inlen,
const unsigned char *key)
@@ -58,7 +59,7 @@ crypto_onetimeauth_poly1305_donna(unsigned char *out, const unsigned char *m,
return 0;
}
int
static int
crypto_onetimeauth_poly1305_donna_init(crypto_onetimeauth_poly1305_state *state,
const unsigned char *key)
{
@@ -67,7 +68,7 @@ crypto_onetimeauth_poly1305_donna_init(crypto_onetimeauth_poly1305_state *state,
return 0;
}
int
static int
crypto_onetimeauth_poly1305_donna_update(crypto_onetimeauth_poly1305_state *state,
const unsigned char *in,
unsigned long long inlen)
@@ -77,7 +78,7 @@ crypto_onetimeauth_poly1305_donna_update(crypto_onetimeauth_poly1305_state *stat
return 0;
}
int
static int
crypto_onetimeauth_poly1305_donna_final(crypto_onetimeauth_poly1305_state *state,
unsigned char *out)
{
@@ -86,17 +87,8 @@ crypto_onetimeauth_poly1305_donna_final(crypto_onetimeauth_poly1305_state *state
return 0;
}
/* LCOV_EXCL_START */
const char *
crypto_onetimeauth_poly1305_donna_implementation_name(void)
{
return POLY1305_IMPLEMENTATION_NAME;
}
/* LCOV_EXCL_STOP */
struct crypto_onetimeauth_poly1305_implementation
crypto_onetimeauth_poly1305_donna_implementation = {
SODIUM_C99(.implementation_name =) crypto_onetimeauth_poly1305_donna_implementation_name,
SODIUM_C99(.onetimeauth =) crypto_onetimeauth_poly1305_donna,
SODIUM_C99(.onetimeauth_verify =) crypto_onetimeauth_poly1305_donna_verify,
SODIUM_C99(.onetimeauth_init =) crypto_onetimeauth_poly1305_donna_init,
@@ -10,26 +10,26 @@ typedef crypto_onetimeauth_poly1305_state poly1305_context;
extern struct crypto_onetimeauth_poly1305_implementation
crypto_onetimeauth_poly1305_donna_implementation;
const char *crypto_onetimeauth_poly1305_donna_implementation_name(void);
static const char *crypto_onetimeauth_poly1305_donna_implementation_name(void);
int crypto_onetimeauth_poly1305_donna(unsigned char *out,
const unsigned char *in,
unsigned long long inlen,
const unsigned char *k);
int crypto_onetimeauth_poly1305_donna_verify(const unsigned char *h,
static int crypto_onetimeauth_poly1305_donna(unsigned char *out,
const unsigned char *in,
unsigned long long inlen,
const unsigned char *k);
int crypto_onetimeauth_poly1305_donna_init(crypto_onetimeauth_poly1305_state *state,
const unsigned char *key);
static int crypto_onetimeauth_poly1305_donna_verify(const unsigned char *h,
const unsigned char *in,
unsigned long long inlen,
const unsigned char *k);
int crypto_onetimeauth_poly1305_donna_update(crypto_onetimeauth_poly1305_state *state,
const unsigned char *in,
unsigned long long inlen);
static int crypto_onetimeauth_poly1305_donna_init(crypto_onetimeauth_poly1305_state *state,
const unsigned char *key);
int crypto_onetimeauth_poly1305_donna_final(crypto_onetimeauth_poly1305_state *state,
unsigned char *out);
static int crypto_onetimeauth_poly1305_donna_update(crypto_onetimeauth_poly1305_state *state,
const unsigned char *in,
unsigned long long inlen);
static int crypto_onetimeauth_poly1305_donna_final(crypto_onetimeauth_poly1305_state *state,
unsigned char *out);
#endif /* __POLY1305_DONNA_H__ */
@@ -2,8 +2,6 @@
poly1305 implementation using 32 bit * 32 bit = 64 bit multiplication and 64 bit addition
*/
#define POLY1305_IMPLEMENTATION_NAME "donna32"
#if defined(_MSC_VER)
# define POLY1305_NOINLINE __declspec(noinline)
#elif defined(__GNUC__)
@@ -2,8 +2,6 @@
poly1305 implementation using 64 bit * 64 bit = 128 bit multiplication and 128 bit addition
*/
#define POLY1305_IMPLEMENTATION_NAME "donna64"
#if defined(__SIZEOF_INT128__)
typedef unsigned __int128 uint128_t;
#else
@@ -1,26 +1,11 @@
#include "crypto_onetimeauth_poly1305.h"
#include "donna/poly1305_donna.h"
#include "onetimeauth_poly1305.h"
/* LCOV_EXCL_START */
static const crypto_onetimeauth_poly1305_implementation *implementation =
&crypto_onetimeauth_poly1305_donna_implementation;
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();
}
/* LCOV_EXCL_STOP */
int
crypto_onetimeauth_poly1305(unsigned char *out, const unsigned char *in,
unsigned long long inlen, const unsigned char *k)
@@ -58,3 +43,20 @@ crypto_onetimeauth_poly1305_final(crypto_onetimeauth_poly1305_state *state,
{
return implementation->onetimeauth_final(state, out);
}
size_t
crypto_onetimeauth_poly1305_bytes(void) {
return crypto_onetimeauth_poly1305_BYTES;
}
size_t
crypto_onetimeauth_poly1305_keybytes(void) {
return crypto_onetimeauth_poly1305_KEYBYTES;
}
int
_crypto_onetimeauth_poly1305_pick_best_implementation(void)
{
implementation = &crypto_onetimeauth_poly1305_donna_implementation;
return 0;
}
@@ -1,11 +0,0 @@
#include "crypto_onetimeauth_poly1305.h"
size_t
crypto_onetimeauth_poly1305_bytes(void) {
return crypto_onetimeauth_poly1305_BYTES;
}
size_t
crypto_onetimeauth_poly1305_keybytes(void) {
return crypto_onetimeauth_poly1305_KEYBYTES;
}
@@ -1,13 +0,0 @@
#include <stdlib.h>
#include <string.h>
#include "crypto_onetimeauth.h"
#include "crypto_onetimeauth_poly1305.h"
#include "utils.h"
#include "donna/poly1305_donna.h"
crypto_onetimeauth_poly1305_implementation *
crypto_onetimeauth_pick_best_implementation(void)
{
return &crypto_onetimeauth_poly1305_donna_implementation;
}
@@ -54,32 +54,9 @@ SODIUM_EXPORT
int crypto_onetimeauth_poly1305_final(crypto_onetimeauth_poly1305_state *state,
unsigned char *out);
const char *crypto_onetimeauth_poly1305_implementation_name(void);
/* ------------------------------------------------------------------------- */
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);
int (*onetimeauth_init)(crypto_onetimeauth_poly1305_state *state,
const unsigned char *key);
int (*onetimeauth_update)(crypto_onetimeauth_poly1305_state *state,
const unsigned char *in,
unsigned long long inlen);
int (*onetimeauth_final)(crypto_onetimeauth_poly1305_state *state,
unsigned char *out);
} crypto_onetimeauth_poly1305_implementation;
int crypto_onetimeauth_poly1305_set_implementation(crypto_onetimeauth_poly1305_implementation *impl);
crypto_onetimeauth_poly1305_implementation *crypto_onetimeauth_pick_best_implementation(void);
int _crypto_onetimeauth_poly1305_pick_best_implementation(void);
#ifdef __cplusplus
}
+2 -3
View File
@@ -1,5 +1,6 @@
#include "core.h"
#include "crypto_generichash.h"
#include "crypto_onetimeauth.h"
#include "randombytes.h"
#include "runtime.h"
@@ -17,9 +18,7 @@ sodium_init(void)
randombytes_stir();
_sodium_alloc_init();
_crypto_generichash_blake2b_pick_best_implementation();
if (crypto_onetimeauth_pick_best_implementation() == NULL) {
return -1; /* LCOV_EXCL_LINE */
}
_crypto_onetimeauth_poly1305_pick_best_implementation();
initialized = 1;
return 0;