1
2#include "onetimeauth_poly1305.h"
3#include "crypto_onetimeauth_poly1305.h"
4#include "private/common.h"
5#include "private/implementations.h"
6#include "randombytes.h"
7#include "runtime.h"
8
9#include "donna/poly1305_donna.h"
10#if defined(HAVE_TI_MODE) && defined(HAVE_EMMINTRIN_H)
11# include "sse2/poly1305_sse2.h"
12#endif
13
14static const crypto_onetimeauth_poly1305_implementation *implementation =
15    &crypto_onetimeauth_poly1305_donna_implementation;
16
17int
18crypto_onetimeauth_poly1305(unsigned char *out, const unsigned char *in,
19                            unsigned long long inlen, const unsigned char *k)
20{
21    return implementation->onetimeauth(out, in, inlen, k);
22}
23
24int
25crypto_onetimeauth_poly1305_verify(const unsigned char *h,
26                                   const unsigned char *in,
27                                   unsigned long long   inlen,
28                                   const unsigned char *k)
29{
30    return implementation->onetimeauth_verify(h, in, inlen, k);
31}
32
33int
34crypto_onetimeauth_poly1305_init(crypto_onetimeauth_poly1305_state *state,
35                                 const unsigned char *key)
36{
37    return implementation->onetimeauth_init(state, key);
38}
39
40int
41crypto_onetimeauth_poly1305_update(crypto_onetimeauth_poly1305_state *state,
42                                   const unsigned char *in,
43                                   unsigned long long inlen)
44{
45    return implementation->onetimeauth_update(state, in, inlen);
46}
47
48int
49crypto_onetimeauth_poly1305_final(crypto_onetimeauth_poly1305_state *state,
50                                  unsigned char *out)
51{
52    return implementation->onetimeauth_final(state, out);
53}
54
55size_t
56crypto_onetimeauth_poly1305_bytes(void)
57{
58    return crypto_onetimeauth_poly1305_BYTES;
59}
60
61size_t
62crypto_onetimeauth_poly1305_keybytes(void)
63{
64    return crypto_onetimeauth_poly1305_KEYBYTES;
65}
66
67size_t
68crypto_onetimeauth_poly1305_statebytes(void)
69{
70    return sizeof(crypto_onetimeauth_poly1305_state);
71}
72
73void
74crypto_onetimeauth_poly1305_keygen(
75    unsigned char k[crypto_onetimeauth_poly1305_KEYBYTES])
76{
77    randombytes_buf(k, crypto_onetimeauth_poly1305_KEYBYTES);
78}
79
80int
81_crypto_onetimeauth_poly1305_pick_best_implementation(void)
82{
83    implementation = &crypto_onetimeauth_poly1305_donna_implementation;
84#if defined(HAVE_TI_MODE) && defined(HAVE_EMMINTRIN_H)
85    if (sodium_runtime_has_sse2()) {
86        implementation = &crypto_onetimeauth_poly1305_sse2_implementation;
87    }
88#endif
89    return 0;
90}
91