1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
10 /* The implementation is based on:
11 * Public Domain poly1305 from Andrew Moon
12 * https://github.com/floodyberry/poly1305-donna
20 static void _poly1305_block(poly1305_state *st, const unsigned char *in, unsigned long inlen)
22 const unsigned long hibit = (st->final) ? 0 : (1UL << 24); /* 1 << 128 */
23 ulong32 r0,r1,r2,r3,r4;
25 ulong32 h0,h1,h2,h3,h4;
27 ulong64 d0,d1,d2,d3,d4;
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;
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);
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;
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
89 int poly1305_init(poly1305_state *st, const unsigned char *key, unsigned long keylen)
91 LTC_ARGCHK(st != NULL);
92 LTC_ARGCHK(key != NULL);
93 LTC_ARGCHK(keylen == 32);
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;
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);
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
127 int poly1305_process(poly1305_state *st, const unsigned char *in, unsigned long inlen)
131 if (inlen == 0) return CRYPT_OK; /* nothing to do */
132 LTC_ARGCHK(st != NULL);
133 LTC_ARGCHK(in != NULL);
135 /* handle 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];
142 st->leftover += want;
143 if (st->leftover < 16) return CRYPT_OK;
144 _poly1305_block(st, st->buffer, 16);
148 /* process full blocks */
150 unsigned long want = (inlen & ~(16 - 1));
151 _poly1305_block(st, in, want);
158 for (i = 0; i < inlen; i++) st->buffer[st->leftover + i] = in[i];
159 st->leftover += inlen;
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
171 int poly1305_done(poly1305_state *st, unsigned char *mac, unsigned long *maclen)
173 ulong32 h0,h1,h2,h3,h4,c;
174 ulong32 g0,g1,g2,g3,g4;
178 LTC_ARGCHK(st != NULL);
179 LTC_ARGCHK(mac != NULL);
180 LTC_ARGCHK(maclen != NULL);
181 LTC_ARGCHK(*maclen >= 16);
183 /* process the remaining block */
185 unsigned long i = st->leftover;
187 for (; i < 16; i++) st->buffer[i] = 0;
189 _poly1305_block(st, st->buffer, 16);
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;
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);
213 /* select h if h < p, or h + -p if h >= p */
214 mask = (g4 >> 31) - 1;
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;
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;
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;
239 STORE32L(h0, mac + 0);
240 STORE32L(h1, mac + 4);
241 STORE32L(h2, mac + 8);
242 STORE32L(h3, mac + 12);
244 /* zero out the state */
266 /* ref: $Format:%D$ */
267 /* git commit: $Format:%H$ */
268 /* commit time: $Format:%ai$ */