1 |
|
|
/* $OpenBSD: poly1305-donna.c,v 1.3 2014/06/12 15:49:30 deraadt Exp $ */ |
2 |
|
|
/* |
3 |
|
|
* Public Domain poly1305 from Andrew Moon |
4 |
|
|
* Based on poly1305-donna.c, poly1305-donna-32.h and poly1305-donna.h from: |
5 |
|
|
* https://github.com/floodyberry/poly1305-donna |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
#include <stddef.h> |
9 |
|
|
|
10 |
|
|
static inline void poly1305_init(poly1305_context *ctx, |
11 |
|
|
const unsigned char key[32]); |
12 |
|
|
static inline void poly1305_update(poly1305_context *ctx, |
13 |
|
|
const unsigned char *m, size_t bytes); |
14 |
|
|
static inline void poly1305_finish(poly1305_context *ctx, |
15 |
|
|
unsigned char mac[16]); |
16 |
|
|
|
17 |
|
|
/* |
18 |
|
|
* poly1305 implementation using 32 bit * 32 bit = 64 bit multiplication |
19 |
|
|
* and 64 bit addition. |
20 |
|
|
*/ |
21 |
|
|
|
22 |
|
|
#define poly1305_block_size 16 |
23 |
|
|
|
24 |
|
|
/* 17 + sizeof(size_t) + 14*sizeof(unsigned long) */ |
25 |
|
|
typedef struct poly1305_state_internal_t { |
26 |
|
|
unsigned long r[5]; |
27 |
|
|
unsigned long h[5]; |
28 |
|
|
unsigned long pad[4]; |
29 |
|
|
size_t leftover; |
30 |
|
|
unsigned char buffer[poly1305_block_size]; |
31 |
|
|
unsigned char final; |
32 |
|
|
} poly1305_state_internal_t; |
33 |
|
|
|
34 |
|
|
/* interpret four 8 bit unsigned integers as a 32 bit unsigned integer in little endian */ |
35 |
|
|
static unsigned long |
36 |
|
|
U8TO32(const unsigned char *p) |
37 |
|
|
{ |
38 |
|
382992 |
return (((unsigned long)(p[0] & 0xff)) | |
39 |
|
191496 |
((unsigned long)(p[1] & 0xff) << 8) | |
40 |
|
191496 |
((unsigned long)(p[2] & 0xff) << 16) | |
41 |
|
95748 |
((unsigned long)(p[3] & 0xff) << 24)); |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
/* store a 32 bit unsigned integer as four 8 bit unsigned integers in little endian */ |
45 |
|
|
static void |
46 |
|
|
U32TO8(unsigned char *p, unsigned long v) |
47 |
|
|
{ |
48 |
|
13696 |
p[0] = (v) & 0xff; |
49 |
|
6848 |
p[1] = (v >> 8) & 0xff; |
50 |
|
6848 |
p[2] = (v >> 16) & 0xff; |
51 |
|
6848 |
p[3] = (v >> 24) & 0xff; |
52 |
|
6848 |
} |
53 |
|
|
|
54 |
|
|
static inline void |
55 |
|
|
poly1305_init(poly1305_context *ctx, const unsigned char key[32]) |
56 |
|
|
{ |
57 |
|
3424 |
poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx; |
58 |
|
|
|
59 |
|
|
/* r &= 0xffffffc0ffffffc0ffffffc0fffffff */ |
60 |
|
1712 |
st->r[0] = (U8TO32(&key[0])) & 0x3ffffff; |
61 |
|
1712 |
st->r[1] = (U8TO32(&key[3]) >> 2) & 0x3ffff03; |
62 |
|
1712 |
st->r[2] = (U8TO32(&key[6]) >> 4) & 0x3ffc0ff; |
63 |
|
1712 |
st->r[3] = (U8TO32(&key[9]) >> 6) & 0x3f03fff; |
64 |
|
1712 |
st->r[4] = (U8TO32(&key[12]) >> 8) & 0x00fffff; |
65 |
|
|
|
66 |
|
|
/* h = 0 */ |
67 |
|
1712 |
st->h[0] = 0; |
68 |
|
1712 |
st->h[1] = 0; |
69 |
|
1712 |
st->h[2] = 0; |
70 |
|
1712 |
st->h[3] = 0; |
71 |
|
1712 |
st->h[4] = 0; |
72 |
|
|
|
73 |
|
|
/* save pad for later */ |
74 |
|
1712 |
st->pad[0] = U8TO32(&key[16]); |
75 |
|
1712 |
st->pad[1] = U8TO32(&key[20]); |
76 |
|
1712 |
st->pad[2] = U8TO32(&key[24]); |
77 |
|
1712 |
st->pad[3] = U8TO32(&key[28]); |
78 |
|
|
|
79 |
|
1712 |
st->leftover = 0; |
80 |
|
1712 |
st->final = 0; |
81 |
|
1712 |
} |
82 |
|
|
|
83 |
|
|
static void |
84 |
|
|
poly1305_blocks(poly1305_state_internal_t *st, const unsigned char *m, size_t bytes) |
85 |
|
|
{ |
86 |
|
9948 |
const unsigned long hibit = (st->final) ? 0 : (1 << 24); /* 1 << 128 */ |
87 |
|
|
unsigned long r0, r1, r2, r3, r4; |
88 |
|
|
unsigned long s1, s2, s3, s4; |
89 |
|
|
unsigned long h0, h1, h2, h3, h4; |
90 |
|
|
unsigned long long d0, d1, d2, d3, d4; |
91 |
|
|
unsigned long c; |
92 |
|
|
|
93 |
|
4974 |
r0 = st->r[0]; |
94 |
|
4974 |
r1 = st->r[1]; |
95 |
|
4974 |
r2 = st->r[2]; |
96 |
|
4974 |
r3 = st->r[3]; |
97 |
|
4974 |
r4 = st->r[4]; |
98 |
|
|
|
99 |
|
4974 |
s1 = r1 * 5; |
100 |
|
4974 |
s2 = r2 * 5; |
101 |
|
4974 |
s3 = r3 * 5; |
102 |
|
4974 |
s4 = r4 * 5; |
103 |
|
|
|
104 |
|
4974 |
h0 = st->h[0]; |
105 |
|
4974 |
h1 = st->h[1]; |
106 |
|
4974 |
h2 = st->h[2]; |
107 |
|
4974 |
h3 = st->h[3]; |
108 |
|
4974 |
h4 = st->h[4]; |
109 |
|
|
|
110 |
✓✓ |
42084 |
while (bytes >= poly1305_block_size) { |
111 |
|
|
/* h += m[i] */ |
112 |
|
16068 |
h0 += (U8TO32(m + 0)) & 0x3ffffff; |
113 |
|
16068 |
h1 += (U8TO32(m + 3) >> 2) & 0x3ffffff; |
114 |
|
16068 |
h2 += (U8TO32(m + 6) >> 4) & 0x3ffffff; |
115 |
|
16068 |
h3 += (U8TO32(m + 9) >> 6) & 0x3ffffff; |
116 |
|
16068 |
h4 += (U8TO32(m + 12) >> 8) | hibit; |
117 |
|
|
|
118 |
|
|
/* h *= r */ |
119 |
|
32136 |
d0 = ((unsigned long long)h0 * r0) + |
120 |
|
32136 |
((unsigned long long)h1 * s4) + |
121 |
|
32136 |
((unsigned long long)h2 * s3) + |
122 |
|
32136 |
((unsigned long long)h3 * s2) + |
123 |
|
16068 |
((unsigned long long)h4 * s1); |
124 |
|
32136 |
d1 = ((unsigned long long)h0 * r1) + |
125 |
|
32136 |
((unsigned long long)h1 * r0) + |
126 |
|
32136 |
((unsigned long long)h2 * s4) + |
127 |
|
32136 |
((unsigned long long)h3 * s3) + |
128 |
|
16068 |
((unsigned long long)h4 * s2); |
129 |
|
32136 |
d2 = ((unsigned long long)h0 * r2) + |
130 |
|
32136 |
((unsigned long long)h1 * r1) + |
131 |
|
32136 |
((unsigned long long)h2 * r0) + |
132 |
|
32136 |
((unsigned long long)h3 * s4) + |
133 |
|
16068 |
((unsigned long long)h4 * s3); |
134 |
|
32136 |
d3 = ((unsigned long long)h0 * r3) + |
135 |
|
32136 |
((unsigned long long)h1 * r2) + |
136 |
|
32136 |
((unsigned long long)h2 * r1) + |
137 |
|
32136 |
((unsigned long long)h3 * r0) + |
138 |
|
16068 |
((unsigned long long)h4 * s4); |
139 |
|
32136 |
d4 = ((unsigned long long)h0 * r4) + |
140 |
|
32136 |
((unsigned long long)h1 * r3) + |
141 |
|
32136 |
((unsigned long long)h2 * r2) + |
142 |
|
32136 |
((unsigned long long)h3 * r1) + |
143 |
|
16068 |
((unsigned long long)h4 * r0); |
144 |
|
|
|
145 |
|
|
/* (partial) h %= p */ |
146 |
|
16068 |
c = (unsigned long)(d0 >> 26); |
147 |
|
16068 |
h0 = (unsigned long)d0 & 0x3ffffff; |
148 |
|
16068 |
d1 += c; |
149 |
|
16068 |
c = (unsigned long)(d1 >> 26); |
150 |
|
16068 |
h1 = (unsigned long)d1 & 0x3ffffff; |
151 |
|
16068 |
d2 += c; |
152 |
|
16068 |
c = (unsigned long)(d2 >> 26); |
153 |
|
16068 |
h2 = (unsigned long)d2 & 0x3ffffff; |
154 |
|
16068 |
d3 += c; |
155 |
|
16068 |
c = (unsigned long)(d3 >> 26); |
156 |
|
16068 |
h3 = (unsigned long)d3 & 0x3ffffff; |
157 |
|
16068 |
d4 += c; |
158 |
|
16068 |
c = (unsigned long)(d4 >> 26); |
159 |
|
16068 |
h4 = (unsigned long)d4 & 0x3ffffff; |
160 |
|
16068 |
h0 += c * 5; |
161 |
|
16068 |
c = (h0 >> 26); |
162 |
|
16068 |
h0 = h0 & 0x3ffffff; |
163 |
|
16068 |
h1 += c; |
164 |
|
|
|
165 |
|
16068 |
m += poly1305_block_size; |
166 |
|
16068 |
bytes -= poly1305_block_size; |
167 |
|
|
} |
168 |
|
|
|
169 |
|
4974 |
st->h[0] = h0; |
170 |
|
4974 |
st->h[1] = h1; |
171 |
|
4974 |
st->h[2] = h2; |
172 |
|
4974 |
st->h[3] = h3; |
173 |
|
4974 |
st->h[4] = h4; |
174 |
|
4974 |
} |
175 |
|
|
|
176 |
|
|
static inline void |
177 |
|
|
poly1305_update(poly1305_context *ctx, const unsigned char *m, size_t bytes) |
178 |
|
|
{ |
179 |
|
7960 |
poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx; |
180 |
|
|
size_t i; |
181 |
|
|
|
182 |
|
|
/* handle leftover */ |
183 |
✓✓ |
3980 |
if (st->leftover) { |
184 |
|
410 |
size_t want = (poly1305_block_size - st->leftover); |
185 |
✓✓ |
410 |
if (want > bytes) |
186 |
|
30 |
want = bytes; |
187 |
✓✓ |
6028 |
for (i = 0; i < want; i++) |
188 |
|
2604 |
st->buffer[st->leftover + i] = m[i]; |
189 |
|
410 |
bytes -= want; |
190 |
|
410 |
m += want; |
191 |
|
410 |
st->leftover += want; |
192 |
✓✓ |
410 |
if (st->leftover < poly1305_block_size) |
193 |
|
30 |
return; |
194 |
|
380 |
poly1305_blocks(st, st->buffer, poly1305_block_size); |
195 |
|
380 |
st->leftover = 0; |
196 |
✓✓ |
380 |
} |
197 |
|
|
|
198 |
|
|
/* process full blocks */ |
199 |
✓✓ |
3950 |
if (bytes >= poly1305_block_size) { |
200 |
|
3142 |
size_t want = (bytes & ~(poly1305_block_size - 1)); |
201 |
|
3142 |
poly1305_blocks(st, m, want); |
202 |
|
3142 |
m += want; |
203 |
|
3142 |
bytes -= want; |
204 |
|
3142 |
} |
205 |
|
|
|
206 |
|
|
/* store leftover */ |
207 |
✓✓ |
3950 |
if (bytes) { |
208 |
✓✓ |
33728 |
for (i = 0; i < bytes; i++) |
209 |
|
15032 |
st->buffer[st->leftover + i] = m[i]; |
210 |
|
1832 |
st->leftover += bytes; |
211 |
|
1832 |
} |
212 |
|
7930 |
} |
213 |
|
|
|
214 |
|
|
static inline void |
215 |
|
|
poly1305_finish(poly1305_context *ctx, unsigned char mac[16]) |
216 |
|
|
{ |
217 |
|
3424 |
poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx; |
218 |
|
|
unsigned long h0, h1, h2, h3, h4, c; |
219 |
|
|
unsigned long g0, g1, g2, g3, g4; |
220 |
|
|
unsigned long long f; |
221 |
|
|
unsigned long mask; |
222 |
|
|
|
223 |
|
|
/* process the remaining block */ |
224 |
✓✓ |
1712 |
if (st->leftover) { |
225 |
|
|
size_t i = st->leftover; |
226 |
|
1452 |
st->buffer[i++] = 1; |
227 |
✓✓ |
23352 |
for (; i < poly1305_block_size; i++) |
228 |
|
10224 |
st->buffer[i] = 0; |
229 |
|
1452 |
st->final = 1; |
230 |
|
1452 |
poly1305_blocks(st, st->buffer, poly1305_block_size); |
231 |
|
1452 |
} |
232 |
|
|
|
233 |
|
|
/* fully carry h */ |
234 |
|
1712 |
h0 = st->h[0]; |
235 |
|
1712 |
h1 = st->h[1]; |
236 |
|
1712 |
h2 = st->h[2]; |
237 |
|
1712 |
h3 = st->h[3]; |
238 |
|
1712 |
h4 = st->h[4]; |
239 |
|
|
|
240 |
|
1712 |
c = h1 >> 26; |
241 |
|
1712 |
h1 = h1 & 0x3ffffff; |
242 |
|
1712 |
h2 += c; |
243 |
|
1712 |
c = h2 >> 26; |
244 |
|
1712 |
h2 = h2 & 0x3ffffff; |
245 |
|
1712 |
h3 += c; |
246 |
|
1712 |
c = h3 >> 26; |
247 |
|
1712 |
h3 = h3 & 0x3ffffff; |
248 |
|
1712 |
h4 += c; |
249 |
|
1712 |
c = h4 >> 26; |
250 |
|
1712 |
h4 = h4 & 0x3ffffff; |
251 |
|
1712 |
h0 += c * 5; |
252 |
|
1712 |
c = h0 >> 26; |
253 |
|
1712 |
h0 = h0 & 0x3ffffff; |
254 |
|
1712 |
h1 += c; |
255 |
|
|
|
256 |
|
|
/* compute h + -p */ |
257 |
|
1712 |
g0 = h0 + 5; |
258 |
|
1712 |
c = g0 >> 26; |
259 |
|
1712 |
g0 &= 0x3ffffff; |
260 |
|
1712 |
g1 = h1 + c; |
261 |
|
1712 |
c = g1 >> 26; |
262 |
|
1712 |
g1 &= 0x3ffffff; |
263 |
|
1712 |
g2 = h2 + c; |
264 |
|
1712 |
c = g2 >> 26; |
265 |
|
1712 |
g2 &= 0x3ffffff; |
266 |
|
1712 |
g3 = h3 + c; |
267 |
|
1712 |
c = g3 >> 26; |
268 |
|
1712 |
g3 &= 0x3ffffff; |
269 |
|
1712 |
g4 = h4 + c - (1 << 26); |
270 |
|
|
|
271 |
|
|
/* select h if h < p, or h + -p if h >= p */ |
272 |
|
1712 |
mask = (g4 >> ((sizeof(unsigned long) * 8) - 1)) - 1; |
273 |
|
1712 |
g0 &= mask; |
274 |
|
1712 |
g1 &= mask; |
275 |
|
1712 |
g2 &= mask; |
276 |
|
1712 |
g3 &= mask; |
277 |
|
1712 |
g4 &= mask; |
278 |
|
1712 |
mask = ~mask; |
279 |
|
1712 |
h0 = (h0 & mask) | g0; |
280 |
|
1712 |
h1 = (h1 & mask) | g1; |
281 |
|
1712 |
h2 = (h2 & mask) | g2; |
282 |
|
1712 |
h3 = (h3 & mask) | g3; |
283 |
|
1712 |
h4 = (h4 & mask) | g4; |
284 |
|
|
|
285 |
|
|
/* h = h % (2^128) */ |
286 |
|
1712 |
h0 = ((h0) | (h1 << 26)) & 0xffffffff; |
287 |
|
1712 |
h1 = ((h1 >> 6) | (h2 << 20)) & 0xffffffff; |
288 |
|
1712 |
h2 = ((h2 >> 12) | (h3 << 14)) & 0xffffffff; |
289 |
|
1712 |
h3 = ((h3 >> 18) | (h4 << 8)) & 0xffffffff; |
290 |
|
|
|
291 |
|
|
/* mac = (h + pad) % (2^128) */ |
292 |
|
1712 |
f = (unsigned long long)h0 + st->pad[0]; |
293 |
|
|
h0 = (unsigned long)f; |
294 |
|
1712 |
f = (unsigned long long)h1 + st->pad[1] + (f >> 32); |
295 |
|
|
h1 = (unsigned long)f; |
296 |
|
1712 |
f = (unsigned long long)h2 + st->pad[2] + (f >> 32); |
297 |
|
|
h2 = (unsigned long)f; |
298 |
|
1712 |
f = (unsigned long long)h3 + st->pad[3] + (f >> 32); |
299 |
|
|
h3 = (unsigned long)f; |
300 |
|
|
|
301 |
|
1712 |
U32TO8(mac + 0, h0); |
302 |
|
1712 |
U32TO8(mac + 4, h1); |
303 |
|
1712 |
U32TO8(mac + 8, h2); |
304 |
|
1712 |
U32TO8(mac + 12, h3); |
305 |
|
|
|
306 |
|
|
/* zero out the state */ |
307 |
|
1712 |
st->h[0] = 0; |
308 |
|
1712 |
st->h[1] = 0; |
309 |
|
1712 |
st->h[2] = 0; |
310 |
|
1712 |
st->h[3] = 0; |
311 |
|
1712 |
st->h[4] = 0; |
312 |
|
1712 |
st->r[0] = 0; |
313 |
|
1712 |
st->r[1] = 0; |
314 |
|
1712 |
st->r[2] = 0; |
315 |
|
1712 |
st->r[3] = 0; |
316 |
|
1712 |
st->r[4] = 0; |
317 |
|
1712 |
st->pad[0] = 0; |
318 |
|
1712 |
st->pad[1] = 0; |
319 |
|
1712 |
st->pad[2] = 0; |
320 |
|
1712 |
st->pad[3] = 0; |
321 |
|
1712 |
} |