1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 
4 /* The implementation is based on:
5  * Public Domain poly1305 from Andrew Moon
6  * https://github.com/floodyberry/poly1305-donna
7  */
8 
9 #include "tomcrypt_private.h"
10 
11 #ifdef LTC_POLY1305
12 
13 /* internal only */
s_poly1305_block(poly1305_state * st,const unsigned char * in,unsigned long inlen)14 static void s_poly1305_block(poly1305_state *st, const unsigned char *in, unsigned long inlen)
15 {
16    const unsigned long hibit = (st->final) ? 0 : (1UL << 24); /* 1 << 128 */
17    ulong32 r0,r1,r2,r3,r4;
18    ulong32 s1,s2,s3,s4;
19    ulong32 h0,h1,h2,h3,h4;
20    ulong32 tmp;
21    ulong64 d0,d1,d2,d3,d4;
22    ulong32 c;
23 
24    r0 = st->r[0];
25    r1 = st->r[1];
26    r2 = st->r[2];
27    r3 = st->r[3];
28    r4 = st->r[4];
29 
30    s1 = r1 * 5;
31    s2 = r2 * 5;
32    s3 = r3 * 5;
33    s4 = r4 * 5;
34 
35    h0 = st->h[0];
36    h1 = st->h[1];
37    h2 = st->h[2];
38    h3 = st->h[3];
39    h4 = st->h[4];
40 
41    while (inlen >= 16) {
42       /* h += in[i] */
43       LOAD32L(tmp, in+ 0); h0 += (tmp     ) & 0x3ffffff;
44       LOAD32L(tmp, in+ 3); h1 += (tmp >> 2) & 0x3ffffff;
45       LOAD32L(tmp, in+ 6); h2 += (tmp >> 4) & 0x3ffffff;
46       LOAD32L(tmp, in+ 9); h3 += (tmp >> 6) & 0x3ffffff;
47       LOAD32L(tmp, in+12); h4 += (tmp >> 8) | hibit;
48 
49       /* h *= r */
50       d0 = ((ulong64)h0 * r0) + ((ulong64)h1 * s4) + ((ulong64)h2 * s3) + ((ulong64)h3 * s2) + ((ulong64)h4 * s1);
51       d1 = ((ulong64)h0 * r1) + ((ulong64)h1 * r0) + ((ulong64)h2 * s4) + ((ulong64)h3 * s3) + ((ulong64)h4 * s2);
52       d2 = ((ulong64)h0 * r2) + ((ulong64)h1 * r1) + ((ulong64)h2 * r0) + ((ulong64)h3 * s4) + ((ulong64)h4 * s3);
53       d3 = ((ulong64)h0 * r3) + ((ulong64)h1 * r2) + ((ulong64)h2 * r1) + ((ulong64)h3 * r0) + ((ulong64)h4 * s4);
54       d4 = ((ulong64)h0 * r4) + ((ulong64)h1 * r3) + ((ulong64)h2 * r2) + ((ulong64)h3 * r1) + ((ulong64)h4 * r0);
55 
56       /* (partial) h %= p */
57                     c = (ulong32)(d0 >> 26); h0 = (ulong32)d0 & 0x3ffffff;
58       d1 += c;      c = (ulong32)(d1 >> 26); h1 = (ulong32)d1 & 0x3ffffff;
59       d2 += c;      c = (ulong32)(d2 >> 26); h2 = (ulong32)d2 & 0x3ffffff;
60       d3 += c;      c = (ulong32)(d3 >> 26); h3 = (ulong32)d3 & 0x3ffffff;
61       d4 += c;      c = (ulong32)(d4 >> 26); h4 = (ulong32)d4 & 0x3ffffff;
62       h0 += c * 5;  c =          (h0 >> 26); h0 =          h0 & 0x3ffffff;
63       h1 += c;
64 
65       in += 16;
66       inlen -= 16;
67    }
68 
69    st->h[0] = h0;
70    st->h[1] = h1;
71    st->h[2] = h2;
72    st->h[3] = h3;
73    st->h[4] = h4;
74 }
75 
76 /**
77    Initialize an POLY1305 context.
78    @param st       The POLY1305 state
79    @param key      The secret key
80    @param keylen   The length of the secret key (octets)
81    @return CRYPT_OK if successful
82 */
poly1305_init(poly1305_state * st,const unsigned char * key,unsigned long keylen)83 int poly1305_init(poly1305_state *st, const unsigned char *key, unsigned long keylen)
84 {
85    LTC_ARGCHK(st  != NULL);
86    LTC_ARGCHK(key != NULL);
87    LTC_ARGCHK(keylen == 32);
88 
89    /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
90    LOAD32L(st->r[0], key +  0); st->r[0] = (st->r[0]     ) & 0x3ffffff;
91    LOAD32L(st->r[1], key +  3); st->r[1] = (st->r[1] >> 2) & 0x3ffff03;
92    LOAD32L(st->r[2], key +  6); st->r[2] = (st->r[2] >> 4) & 0x3ffc0ff;
93    LOAD32L(st->r[3], key +  9); st->r[3] = (st->r[3] >> 6) & 0x3f03fff;
94    LOAD32L(st->r[4], key + 12); st->r[4] = (st->r[4] >> 8) & 0x00fffff;
95 
96    /* h = 0 */
97    st->h[0] = 0;
98    st->h[1] = 0;
99    st->h[2] = 0;
100    st->h[3] = 0;
101    st->h[4] = 0;
102 
103    /* save pad for later */
104    LOAD32L(st->pad[0], key + 16);
105    LOAD32L(st->pad[1], key + 20);
106    LOAD32L(st->pad[2], key + 24);
107    LOAD32L(st->pad[3], key + 28);
108 
109    st->leftover = 0;
110    st->final = 0;
111    return CRYPT_OK;
112 }
113 
114 /**
115   Process data through POLY1305
116   @param st      The POLY1305 state
117   @param in      The data to send through HMAC
118   @param inlen   The length of the data to HMAC (octets)
119   @return CRYPT_OK if successful
120 */
poly1305_process(poly1305_state * st,const unsigned char * in,unsigned long inlen)121 int poly1305_process(poly1305_state *st, const unsigned char *in, unsigned long inlen)
122 {
123    unsigned long i;
124 
125    if (inlen == 0) return CRYPT_OK; /* nothing to do */
126    LTC_ARGCHK(st != NULL);
127    LTC_ARGCHK(in != NULL);
128 
129    /* handle leftover */
130    if (st->leftover) {
131       unsigned long want = (16 - st->leftover);
132       if (want > inlen) want = inlen;
133       for (i = 0; i < want; i++) st->buffer[st->leftover + i] = in[i];
134       inlen -= want;
135       in += want;
136       st->leftover += want;
137       if (st->leftover < 16) return CRYPT_OK;
138       s_poly1305_block(st, st->buffer, 16);
139       st->leftover = 0;
140    }
141 
142    /* process full blocks */
143    if (inlen >= 16) {
144       unsigned long want = (inlen & ~(16 - 1));
145       s_poly1305_block(st, in, want);
146       in += want;
147       inlen -= want;
148    }
149 
150    /* store leftover */
151    if (inlen) {
152       for (i = 0; i < inlen; i++) st->buffer[st->leftover + i] = in[i];
153       st->leftover += inlen;
154    }
155    return CRYPT_OK;
156 }
157 
158 /**
159    Terminate a POLY1305 session
160    @param st      The POLY1305 state
161    @param mac     [out] The destination of the POLY1305 authentication tag
162    @param maclen  [in/out]  The max size and resulting size of the POLY1305 authentication tag
163    @return CRYPT_OK if successful
164 */
poly1305_done(poly1305_state * st,unsigned char * mac,unsigned long * maclen)165 int poly1305_done(poly1305_state *st, unsigned char *mac, unsigned long *maclen)
166 {
167    ulong32 h0,h1,h2,h3,h4,c;
168    ulong32 g0,g1,g2,g3,g4;
169    ulong64 f;
170    ulong32 mask;
171 
172    LTC_ARGCHK(st     != NULL);
173    LTC_ARGCHK(mac    != NULL);
174    LTC_ARGCHK(maclen != NULL);
175    LTC_ARGCHK(*maclen >= 16);
176 
177    /* process the remaining block */
178    if (st->leftover) {
179       unsigned long i = st->leftover;
180       st->buffer[i++] = 1;
181       for (; i < 16; i++) st->buffer[i] = 0;
182       st->final = 1;
183       s_poly1305_block(st, st->buffer, 16);
184    }
185 
186    /* fully carry h */
187    h0 = st->h[0];
188    h1 = st->h[1];
189    h2 = st->h[2];
190    h3 = st->h[3];
191    h4 = st->h[4];
192 
193                 c = h1 >> 26; h1 = h1 & 0x3ffffff;
194    h2 +=     c; c = h2 >> 26; h2 = h2 & 0x3ffffff;
195    h3 +=     c; c = h3 >> 26; h3 = h3 & 0x3ffffff;
196    h4 +=     c; c = h4 >> 26; h4 = h4 & 0x3ffffff;
197    h0 += c * 5; c = h0 >> 26; h0 = h0 & 0x3ffffff;
198    h1 +=     c;
199 
200    /* compute h + -p */
201    g0 = h0 + 5; c = g0 >> 26; g0 &= 0x3ffffff;
202    g1 = h1 + c; c = g1 >> 26; g1 &= 0x3ffffff;
203    g2 = h2 + c; c = g2 >> 26; g2 &= 0x3ffffff;
204    g3 = h3 + c; c = g3 >> 26; g3 &= 0x3ffffff;
205    g4 = h4 + c - (1UL << 26);
206 
207    /* select h if h < p, or h + -p if h >= p */
208    mask = (g4 >> 31) - 1;
209    g0 &= mask;
210    g1 &= mask;
211    g2 &= mask;
212    g3 &= mask;
213    g4 &= mask;
214    mask = ~mask;
215    h0 = (h0 & mask) | g0;
216    h1 = (h1 & mask) | g1;
217    h2 = (h2 & mask) | g2;
218    h3 = (h3 & mask) | g3;
219    h4 = (h4 & mask) | g4;
220 
221    /* h = h % (2^128) */
222    h0 = ((h0      ) | (h1 << 26)) & 0xffffffff;
223    h1 = ((h1 >>  6) | (h2 << 20)) & 0xffffffff;
224    h2 = ((h2 >> 12) | (h3 << 14)) & 0xffffffff;
225    h3 = ((h3 >> 18) | (h4 <<  8)) & 0xffffffff;
226 
227    /* mac = (h + pad) % (2^128) */
228    f = (ulong64)h0 + st->pad[0]            ; h0 = (ulong32)f;
229    f = (ulong64)h1 + st->pad[1] + (f >> 32); h1 = (ulong32)f;
230    f = (ulong64)h2 + st->pad[2] + (f >> 32); h2 = (ulong32)f;
231    f = (ulong64)h3 + st->pad[3] + (f >> 32); h3 = (ulong32)f;
232 
233    STORE32L(h0, mac +  0);
234    STORE32L(h1, mac +  4);
235    STORE32L(h2, mac +  8);
236    STORE32L(h3, mac + 12);
237 
238    /* zero out the state */
239    st->h[0] = 0;
240    st->h[1] = 0;
241    st->h[2] = 0;
242    st->h[3] = 0;
243    st->h[4] = 0;
244    st->r[0] = 0;
245    st->r[1] = 0;
246    st->r[2] = 0;
247    st->r[3] = 0;
248    st->r[4] = 0;
249    st->pad[0] = 0;
250    st->pad[1] = 0;
251    st->pad[2] = 0;
252    st->pad[3] = 0;
253 
254    *maclen = 16;
255    return CRYPT_OK;
256 }
257 
258 #endif
259