xref: /freebsd/sys/contrib/libsodium/src/libsodium/crypto_onetimeauth/poly1305/onetimeauth_poly1305.c (revision 3611ec604864a7d4dcc9a3ea898c80eb35eef8a0)
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
crypto_onetimeauth_poly1305(unsigned char * out,const unsigned char * in,unsigned long long inlen,const unsigned char * k)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
crypto_onetimeauth_poly1305_verify(const unsigned char * h,const unsigned char * in,unsigned long long inlen,const unsigned char * k)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
crypto_onetimeauth_poly1305_init(crypto_onetimeauth_poly1305_state * state,const unsigned char * key)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
crypto_onetimeauth_poly1305_update(crypto_onetimeauth_poly1305_state * state,const unsigned char * in,unsigned long long inlen)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
crypto_onetimeauth_poly1305_final(crypto_onetimeauth_poly1305_state * state,unsigned char * out)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
crypto_onetimeauth_poly1305_bytes(void)56 crypto_onetimeauth_poly1305_bytes(void)
57 {
58     return crypto_onetimeauth_poly1305_BYTES;
59 }
60 
61 size_t
crypto_onetimeauth_poly1305_keybytes(void)62 crypto_onetimeauth_poly1305_keybytes(void)
63 {
64     return crypto_onetimeauth_poly1305_KEYBYTES;
65 }
66 
67 size_t
crypto_onetimeauth_poly1305_statebytes(void)68 crypto_onetimeauth_poly1305_statebytes(void)
69 {
70     return sizeof(crypto_onetimeauth_poly1305_state);
71 }
72 
73 void
crypto_onetimeauth_poly1305_keygen(unsigned char k[crypto_onetimeauth_poly1305_KEYBYTES])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
_crypto_onetimeauth_poly1305_pick_best_implementation(void)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