GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libcrypto/crypto/../../libssl/src/crypto/poly1305/poly1305-donna.c Lines: 189 189 100.0 %
Date: 2016-12-06 Branches: 22 22 100.0 %

Line Branch Exec Source
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
36952
{
38
36952
	return (((unsigned long)(p[0] & 0xff)) |
39
	    ((unsigned long)(p[1] & 0xff) <<  8) |
40
	    ((unsigned long)(p[2] & 0xff) << 16) |
41
	    ((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
2012
{
48
2012
	p[0] = (v) & 0xff;
49
2012
	p[1] = (v >>  8) & 0xff;
50
2012
	p[2] = (v >> 16) & 0xff;
51
2012
	p[3] = (v >> 24) & 0xff;
52
2012
}
53
54
static inline void
55
poly1305_init(poly1305_context *ctx, const unsigned char key[32])
56
503
{
57
503
	poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx;
58
59
	/* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
60
503
	st->r[0] = (U8TO32(&key[0])) & 0x3ffffff;
61
503
	st->r[1] = (U8TO32(&key[3]) >> 2) & 0x3ffff03;
62
503
	st->r[2] = (U8TO32(&key[6]) >> 4) & 0x3ffc0ff;
63
503
	st->r[3] = (U8TO32(&key[9]) >> 6) & 0x3f03fff;
64
503
	st->r[4] = (U8TO32(&key[12]) >> 8) & 0x00fffff;
65
66
	/* h = 0 */
67
503
	st->h[0] = 0;
68
503
	st->h[1] = 0;
69
503
	st->h[2] = 0;
70
503
	st->h[3] = 0;
71
503
	st->h[4] = 0;
72
73
	/* save pad for later */
74
503
	st->pad[0] = U8TO32(&key[16]);
75
503
	st->pad[1] = U8TO32(&key[20]);
76
503
	st->pad[2] = U8TO32(&key[24]);
77
503
	st->pad[3] = U8TO32(&key[28]);
78
79
503
	st->leftover = 0;
80
503
	st->final = 0;
81
503
}
82
83
static void
84
poly1305_blocks(poly1305_state_internal_t *st, const unsigned char *m, size_t bytes)
85
1821
{
86
1821
	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
1821
	r0 = st->r[0];
94
1821
	r1 = st->r[1];
95
1821
	r2 = st->r[2];
96
1821
	r3 = st->r[3];
97
1821
	r4 = st->r[4];
98
99
1821
	s1 = r1 * 5;
100
1821
	s2 = r2 * 5;
101
1821
	s3 = r3 * 5;
102
1821
	s4 = r4 * 5;
103
104
1821
	h0 = st->h[0];
105
1821
	h1 = st->h[1];
106
1821
	h2 = st->h[2];
107
1821
	h3 = st->h[3];
108
1821
	h4 = st->h[4];
109
110
10127
	while (bytes >= poly1305_block_size) {
111
		/* h += m[i] */
112
6485
		h0 += (U8TO32(m + 0)) & 0x3ffffff;
113
6485
		h1 += (U8TO32(m + 3) >> 2) & 0x3ffffff;
114
6485
		h2 += (U8TO32(m + 6) >> 4) & 0x3ffffff;
115
6485
		h3 += (U8TO32(m + 9) >> 6) & 0x3ffffff;
116
6485
		h4 += (U8TO32(m + 12) >> 8) | hibit;
117
118
		/* h *= r */
119
6485
		d0 = ((unsigned long long)h0 * r0) +
120
		    ((unsigned long long)h1 * s4) +
121
		    ((unsigned long long)h2 * s3) +
122
		    ((unsigned long long)h3 * s2) +
123
		    ((unsigned long long)h4 * s1);
124
6485
		d1 = ((unsigned long long)h0 * r1) +
125
		    ((unsigned long long)h1 * r0) +
126
		    ((unsigned long long)h2 * s4) +
127
		    ((unsigned long long)h3 * s3) +
128
		    ((unsigned long long)h4 * s2);
129
6485
		d2 = ((unsigned long long)h0 * r2) +
130
		    ((unsigned long long)h1 * r1) +
131
		    ((unsigned long long)h2 * r0) +
132
		    ((unsigned long long)h3 * s4) +
133
		    ((unsigned long long)h4 * s3);
134
6485
		d3 = ((unsigned long long)h0 * r3) +
135
		    ((unsigned long long)h1 * r2) +
136
		    ((unsigned long long)h2 * r1) +
137
		    ((unsigned long long)h3 * r0) +
138
		    ((unsigned long long)h4 * s4);
139
6485
		d4 = ((unsigned long long)h0 * r4) +
140
		    ((unsigned long long)h1 * r3) +
141
		    ((unsigned long long)h2 * r2) +
142
		    ((unsigned long long)h3 * r1) +
143
		    ((unsigned long long)h4 * r0);
144
145
		/* (partial) h %= p */
146
6485
		c = (unsigned long)(d0 >> 26);
147
6485
		h0 = (unsigned long)d0 & 0x3ffffff;
148
6485
		d1 += c;
149
6485
		c = (unsigned long)(d1 >> 26);
150
6485
		h1 = (unsigned long)d1 & 0x3ffffff;
151
6485
		d2 += c;
152
6485
		c = (unsigned long)(d2 >> 26);
153
6485
		h2 = (unsigned long)d2 & 0x3ffffff;
154
6485
		d3 += c;
155
6485
		c = (unsigned long)(d3 >> 26);
156
6485
		h3 = (unsigned long)d3 & 0x3ffffff;
157
6485
		d4 += c;
158
6485
		c = (unsigned long)(d4 >> 26);
159
6485
		h4 = (unsigned long)d4 & 0x3ffffff;
160
6485
		h0 += c * 5;
161
6485
		c = (h0 >> 26);
162
6485
		h0 = h0 & 0x3ffffff;
163
6485
		h1 += c;
164
165
6485
		m += poly1305_block_size;
166
6485
		bytes -= poly1305_block_size;
167
	}
168
169
1821
	st->h[0] = h0;
170
1821
	st->h[1] = h1;
171
1821
	st->h[2] = h2;
172
1821
	st->h[3] = h3;
173
1821
	st->h[4] = h4;
174
1821
}
175
176
static inline void
177
poly1305_update(poly1305_context *ctx, const unsigned char *m, size_t bytes)
178
1518
{
179
1518
	poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx;
180
	size_t i;
181
182
	/* handle leftover */
183
1518
	if (st->leftover) {
184
642
		size_t want = (poly1305_block_size - st->leftover);
185
642
		if (want > bytes)
186
164
			want = bytes;
187
5077
		for (i = 0; i < want; i++)
188
4435
			st->buffer[st->leftover + i] = m[i];
189
642
		bytes -= want;
190
642
		m += want;
191
642
		st->leftover += want;
192
642
		if (st->leftover < poly1305_block_size)
193
164
			return;
194
478
		poly1305_blocks(st, st->buffer, poly1305_block_size);
195
478
		st->leftover = 0;
196
	}
197
198
	/* process full blocks */
199
1354
	if (bytes >= poly1305_block_size) {
200
939
		size_t want = (bytes & ~(poly1305_block_size - 1));
201
939
		poly1305_blocks(st, m, want);
202
939
		m += want;
203
939
		bytes -= want;
204
	}
205
206
	/* store leftover */
207
1354
	if (bytes) {
208
7335
		for (i = 0; i < bytes; i++)
209
6453
			st->buffer[st->leftover + i] = m[i];
210
882
		st->leftover += bytes;
211
	}
212
}
213
214
static inline void
215
poly1305_finish(poly1305_context *ctx, unsigned char mac[16])
216
503
{
217
503
	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
503
	if (st->leftover) {
225
404
		size_t i = st->leftover;
226
404
		st->buffer[i++] = 1;
227
3224
		for (; i < poly1305_block_size; i++)
228
2820
			st->buffer[i] = 0;
229
404
		st->final = 1;
230
404
		poly1305_blocks(st, st->buffer, poly1305_block_size);
231
	}
232
233
	/* fully carry h */
234
503
	h0 = st->h[0];
235
503
	h1 = st->h[1];
236
503
	h2 = st->h[2];
237
503
	h3 = st->h[3];
238
503
	h4 = st->h[4];
239
240
503
	c = h1 >> 26;
241
503
	h1 = h1 & 0x3ffffff;
242
503
	h2 += c;
243
503
	c = h2 >> 26;
244
503
	h2 = h2 & 0x3ffffff;
245
503
	h3 += c;
246
503
	c = h3 >> 26;
247
503
	h3 = h3 & 0x3ffffff;
248
503
	h4 += c;
249
503
	c = h4 >> 26;
250
503
	h4 = h4 & 0x3ffffff;
251
503
	h0 += c * 5;
252
503
	c = h0 >> 26;
253
503
	h0 = h0 & 0x3ffffff;
254
503
	h1 += c;
255
256
	/* compute h + -p */
257
503
	g0 = h0 + 5;
258
503
	c = g0 >> 26;
259
503
	g0 &= 0x3ffffff;
260
503
	g1 = h1 + c;
261
503
	c = g1 >> 26;
262
503
	g1 &= 0x3ffffff;
263
503
	g2 = h2 + c;
264
503
	c = g2 >> 26;
265
503
	g2 &= 0x3ffffff;
266
503
	g3 = h3 + c;
267
503
	c = g3 >> 26;
268
503
	g3 &= 0x3ffffff;
269
503
	g4 = h4 + c - (1 << 26);
270
271
	/* select h if h < p, or h + -p if h >= p */
272
503
	mask = (g4 >> ((sizeof(unsigned long) * 8) - 1)) - 1;
273
503
	g0 &= mask;
274
503
	g1 &= mask;
275
503
	g2 &= mask;
276
503
	g3 &= mask;
277
503
	g4 &= mask;
278
503
	mask = ~mask;
279
503
	h0 = (h0 & mask) | g0;
280
503
	h1 = (h1 & mask) | g1;
281
503
	h2 = (h2 & mask) | g2;
282
503
	h3 = (h3 & mask) | g3;
283
503
	h4 = (h4 & mask) | g4;
284
285
	/* h = h % (2^128) */
286
503
	h0 = ((h0) | (h1 << 26)) & 0xffffffff;
287
503
	h1 = ((h1 >>  6) | (h2 << 20)) & 0xffffffff;
288
503
	h2 = ((h2 >> 12) | (h3 << 14)) & 0xffffffff;
289
503
	h3 = ((h3 >> 18) | (h4 <<  8)) & 0xffffffff;
290
291
	/* mac = (h + pad) % (2^128) */
292
503
	f = (unsigned long long)h0 + st->pad[0];
293
503
	h0 = (unsigned long)f;
294
503
	f = (unsigned long long)h1 + st->pad[1] + (f >> 32);
295
503
	h1 = (unsigned long)f;
296
503
	f = (unsigned long long)h2 + st->pad[2] + (f >> 32);
297
503
	h2 = (unsigned long)f;
298
503
	f = (unsigned long long)h3 + st->pad[3] + (f >> 32);
299
503
	h3 = (unsigned long)f;
300
301
503
	U32TO8(mac +  0, h0);
302
503
	U32TO8(mac +  4, h1);
303
503
	U32TO8(mac +  8, h2);
304
503
	U32TO8(mac + 12, h3);
305
306
	/* zero out the state */
307
503
	st->h[0] = 0;
308
503
	st->h[1] = 0;
309
503
	st->h[2] = 0;
310
503
	st->h[3] = 0;
311
503
	st->h[4] = 0;
312
503
	st->r[0] = 0;
313
503
	st->r[1] = 0;
314
503
	st->r[2] = 0;
315
503
	st->r[3] = 0;
316
503
	st->r[4] = 0;
317
503
	st->pad[0] = 0;
318
503
	st->pad[1] = 0;
319
503
	st->pad[2] = 0;
320
503
	st->pad[3] = 0;
321
503
}