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 
14 static const crypto_onetimeauth_poly1305_implementation *implementation =
15     &crypto_onetimeauth_poly1305_donna_implementation;
16 
17 int
18 crypto_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 
24 int
25 crypto_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 
33 int
34 crypto_onetimeauth_poly1305_init(crypto_onetimeauth_poly1305_state *state,
35                                  const unsigned char *key)
36 {
37     return implementation->onetimeauth_init(state, key);
38 }
39 
40 int
41 crypto_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 
48 int
49 crypto_onetimeauth_poly1305_final(crypto_onetimeauth_poly1305_state *state,
50                                   unsigned char *out)
51 {
52     return implementation->onetimeauth_final(state, out);
53 }
54 
55 size_t
56 crypto_onetimeauth_poly1305_bytes(void)
57 {
58     return crypto_onetimeauth_poly1305_BYTES;
59 }
60 
61 size_t
62 crypto_onetimeauth_poly1305_keybytes(void)
63 {
64     return crypto_onetimeauth_poly1305_KEYBYTES;
65 }
66 
67 size_t
68 crypto_onetimeauth_poly1305_statebytes(void)
69 {
70     return sizeof(crypto_onetimeauth_poly1305_state);
71 }
72 
73 void
74 crypto_onetimeauth_poly1305_keygen(
75     unsigned char k[crypto_onetimeauth_poly1305_KEYBYTES])
76 {
77     randombytes_buf(k, crypto_onetimeauth_poly1305_KEYBYTES);
78 }
79 
80 int
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