42e21e3362ba3915113c704669bce5aa5b062971
[trex.git] /
1
2 #include "utils.h"
3 #include "poly1305_donna.h"
4 #ifdef HAVE_TI_MODE
5 # include "poly1305_donna64.h"
6 #else
7 # include "poly1305_donna32.h"
8 #endif
9
10 static void
11 poly1305_update(poly1305_context *ctx, const unsigned char *m,
12                 unsigned long long bytes) {
13     poly1305_state_internal_t *st = (poly1305_state_internal_t *)(void *)ctx;
14     unsigned long long i;
15
16     /* handle leftover */
17     if (st->leftover) {
18         unsigned long long want = (poly1305_block_size - st->leftover);
19         if (want > bytes)
20             want = bytes;
21         for (i = 0; i < want; i++)
22             st->buffer[st->leftover + i] = m[i];
23         bytes -= want;
24         m += want;
25         st->leftover += want;
26         if (st->leftover < poly1305_block_size)
27             return;
28         poly1305_blocks(st, st->buffer, poly1305_block_size);
29         st->leftover = 0;
30     }
31
32     /* process full blocks */
33     if (bytes >= poly1305_block_size) {
34         unsigned long long want = (bytes & ~(poly1305_block_size - 1));
35         poly1305_blocks(st, m, want);
36         m += want;
37         bytes -= want;
38     }
39
40     /* store leftover */
41     if (bytes) {
42         for (i = 0; i < bytes; i++)
43             st->buffer[st->leftover + i] = m[i];
44         st->leftover += bytes;
45     }
46 }
47
48 int
49 crypto_onetimeauth_poly1305_donna(unsigned char *out, const unsigned char *m,
50                                   unsigned long long inlen,
51                                   const unsigned char *key)
52 {
53     poly1305_context ctx;
54     poly1305_init(&ctx, key);
55     poly1305_update(&ctx, m, inlen);
56     poly1305_finish(&ctx, out);
57
58     return 0;
59 }
60
61 int
62 crypto_onetimeauth_poly1305_donna_init(crypto_onetimeauth_poly1305_state *state,
63                                        const unsigned char *key)
64 {
65     poly1305_init((poly1305_context *) state, key);
66
67     return 0;
68 }
69
70 int
71 crypto_onetimeauth_poly1305_donna_update(crypto_onetimeauth_poly1305_state *state,
72                                          const unsigned char *in,
73                                          unsigned long long inlen)
74 {
75     poly1305_update((poly1305_context *) state, in, inlen);
76
77     return 0;
78 }
79
80 int
81 crypto_onetimeauth_poly1305_donna_final(crypto_onetimeauth_poly1305_state *state,
82                                         unsigned char *out)
83 {
84     poly1305_finish((poly1305_context *) state, out);
85
86     return 0;
87 }
88
89 /* LCOV_EXCL_START */
90 const char *
91 crypto_onetimeauth_poly1305_donna_implementation_name(void)
92 {
93     return POLY1305_IMPLEMENTATION_NAME;
94 }
95 /* LCOV_EXCL_STOP */
96
97 struct crypto_onetimeauth_poly1305_implementation
98 crypto_onetimeauth_poly1305_donna_implementation = {
99     SODIUM_C99(.implementation_name =) crypto_onetimeauth_poly1305_donna_implementation_name,
100     SODIUM_C99(.onetimeauth =) crypto_onetimeauth_poly1305_donna,
101     SODIUM_C99(.onetimeauth_verify =) crypto_onetimeauth_poly1305_donna_verify,
102     SODIUM_C99(.onetimeauth_init =) crypto_onetimeauth_poly1305_donna_init,
103     SODIUM_C99(.onetimeauth_update =) crypto_onetimeauth_poly1305_donna_update,
104     SODIUM_C99(.onetimeauth_final =) crypto_onetimeauth_poly1305_donna_final
105 };