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