Skip to content
This repository was archived by the owner on Dec 27, 2019. It is now read-only.

Commit 99db4f8

Browse files
committed
poly1305: switch to donna
1 parent 7a0e82f commit 99db4f8

3 files changed

Lines changed: 382 additions & 164 deletions

File tree

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
/* SPDX-License-Identifier: GPL-2.0
2+
*
3+
* Copyright (C) 2015-2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4+
*
5+
* This is based in part on Andrew Moon's poly1305-donna, which is in the
6+
* public domain.
7+
*/
8+
9+
struct poly1305_internal {
10+
u32 h[5];
11+
u32 r[5];
12+
};
13+
14+
static void poly1305_init_generic(void *ctx, const u8 key[16])
15+
{
16+
struct poly1305_internal *st = (struct poly1305_internal *)ctx;
17+
18+
/* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
19+
st->r[0] = (get_unaligned_le32(&key[0])) & 0x3ffffff;
20+
st->r[1] = (get_unaligned_le32(&key[3]) >> 2) & 0x3ffff03;
21+
st->r[2] = (get_unaligned_le32(&key[6]) >> 4) & 0x3ffc0ff;
22+
st->r[3] = (get_unaligned_le32(&key[9]) >> 6) & 0x3f03fff;
23+
st->r[4] = (get_unaligned_le32(&key[12]) >> 8) & 0x00fffff;
24+
25+
/* h = 0 */
26+
st->h[0] = 0;
27+
st->h[1] = 0;
28+
st->h[2] = 0;
29+
st->h[3] = 0;
30+
st->h[4] = 0;
31+
}
32+
33+
static void poly1305_blocks_generic(void *ctx, const u8 *input, size_t len,
34+
const u32 padbit)
35+
{
36+
struct poly1305_internal *st = (struct poly1305_internal *)ctx;
37+
const u32 hibit = padbit ? (1UL << 24) : 0;
38+
u32 r0, r1, r2, r3, r4;
39+
u32 s1, s2, s3, s4;
40+
u32 h0, h1, h2, h3, h4;
41+
u64 d0, d1, d2, d3, d4;
42+
u32 c;
43+
44+
r0 = st->r[0];
45+
r1 = st->r[1];
46+
r2 = st->r[2];
47+
r3 = st->r[3];
48+
r4 = st->r[4];
49+
50+
s1 = r1 * 5;
51+
s2 = r2 * 5;
52+
s3 = r3 * 5;
53+
s4 = r4 * 5;
54+
55+
h0 = st->h[0];
56+
h1 = st->h[1];
57+
h2 = st->h[2];
58+
h3 = st->h[3];
59+
h4 = st->h[4];
60+
61+
while (len >= POLY1305_BLOCK_SIZE) {
62+
/* h += m[i] */
63+
h0 += (get_unaligned_le32(&input[0])) & 0x3ffffff;
64+
h1 += (get_unaligned_le32(&input[3]) >> 2) & 0x3ffffff;
65+
h2 += (get_unaligned_le32(&input[6]) >> 4) & 0x3ffffff;
66+
h3 += (get_unaligned_le32(&input[9]) >> 6) & 0x3ffffff;
67+
h4 += (get_unaligned_le32(&input[12]) >> 8) | hibit;
68+
69+
/* h *= r */
70+
d0 = ((u64)h0 * r0) + ((u64)h1 * s4) +
71+
((u64)h2 * s3) + ((u64)h3 * s2) +
72+
((u64)h4 * s1);
73+
d1 = ((u64)h0 * r1) + ((u64)h1 * r0) +
74+
((u64)h2 * s4) + ((u64)h3 * s3) +
75+
((u64)h4 * s2);
76+
d2 = ((u64)h0 * r2) + ((u64)h1 * r1) +
77+
((u64)h2 * r0) + ((u64)h3 * s4) +
78+
((u64)h4 * s3);
79+
d3 = ((u64)h0 * r3) + ((u64)h1 * r2) +
80+
((u64)h2 * r1) + ((u64)h3 * r0) +
81+
((u64)h4 * s4);
82+
d4 = ((u64)h0 * r4) + ((u64)h1 * r3) +
83+
((u64)h2 * r2) + ((u64)h3 * r1) +
84+
((u64)h4 * r0);
85+
86+
/* (partial) h %= p */
87+
c = (u32)(d0 >> 26);
88+
h0 = (u32)d0 & 0x3ffffff;
89+
d1 += c;
90+
c = (u32)(d1 >> 26);
91+
h1 = (u32)d1 & 0x3ffffff;
92+
d2 += c;
93+
c = (u32)(d2 >> 26);
94+
h2 = (u32)d2 & 0x3ffffff;
95+
d3 += c;
96+
c = (u32)(d3 >> 26);
97+
h3 = (u32)d3 & 0x3ffffff;
98+
d4 += c;
99+
c = (u32)(d4 >> 26);
100+
h4 = (u32)d4 & 0x3ffffff;
101+
h0 += c * 5;
102+
c = (h0 >> 26);
103+
h0 = h0 & 0x3ffffff;
104+
h1 += c;
105+
106+
input += POLY1305_BLOCK_SIZE;
107+
len -= POLY1305_BLOCK_SIZE;
108+
}
109+
110+
st->h[0] = h0;
111+
st->h[1] = h1;
112+
st->h[2] = h2;
113+
st->h[3] = h3;
114+
st->h[4] = h4;
115+
}
116+
117+
static void poly1305_emit_generic(void *ctx, u8 mac[16], const u32 nonce[4])
118+
{
119+
struct poly1305_internal *st = (struct poly1305_internal *)ctx;
120+
u32 h0, h1, h2, h3, h4, c;
121+
u32 g0, g1, g2, g3, g4;
122+
u64 f;
123+
u32 mask;
124+
125+
/* fully carry h */
126+
h0 = st->h[0];
127+
h1 = st->h[1];
128+
h2 = st->h[2];
129+
h3 = st->h[3];
130+
h4 = st->h[4];
131+
132+
c = h1 >> 26;
133+
h1 = h1 & 0x3ffffff;
134+
h2 += c;
135+
c = h2 >> 26;
136+
h2 = h2 & 0x3ffffff;
137+
h3 += c;
138+
c = h3 >> 26;
139+
h3 = h3 & 0x3ffffff;
140+
h4 += c;
141+
c = h4 >> 26;
142+
h4 = h4 & 0x3ffffff;
143+
h0 += c * 5;
144+
c = h0 >> 26;
145+
h0 = h0 & 0x3ffffff;
146+
h1 += c;
147+
148+
/* compute h + -p */
149+
g0 = h0 + 5;
150+
c = g0 >> 26;
151+
g0 &= 0x3ffffff;
152+
g1 = h1 + c;
153+
c = g1 >> 26;
154+
g1 &= 0x3ffffff;
155+
g2 = h2 + c;
156+
c = g2 >> 26;
157+
g2 &= 0x3ffffff;
158+
g3 = h3 + c;
159+
c = g3 >> 26;
160+
g3 &= 0x3ffffff;
161+
g4 = h4 + c - (1UL << 26);
162+
163+
/* select h if h < p, or h + -p if h >= p */
164+
mask = (g4 >> ((sizeof(u32) * 8) - 1)) - 1;
165+
g0 &= mask;
166+
g1 &= mask;
167+
g2 &= mask;
168+
g3 &= mask;
169+
g4 &= mask;
170+
mask = ~mask;
171+
172+
h0 = (h0 & mask) | g0;
173+
h1 = (h1 & mask) | g1;
174+
h2 = (h2 & mask) | g2;
175+
h3 = (h3 & mask) | g3;
176+
h4 = (h4 & mask) | g4;
177+
178+
/* h = h % (2^128) */
179+
h0 = ((h0) | (h1 << 26)) & 0xffffffff;
180+
h1 = ((h1 >> 6) | (h2 << 20)) & 0xffffffff;
181+
h2 = ((h2 >> 12) | (h3 << 14)) & 0xffffffff;
182+
h3 = ((h3 >> 18) | (h4 << 8)) & 0xffffffff;
183+
184+
/* mac = (h + nonce) % (2^128) */
185+
f = (u64)h0 + nonce[0];
186+
h0 = (u32)f;
187+
f = (u64)h1 + nonce[1] + (f >> 32);
188+
h1 = (u32)f;
189+
f = (u64)h2 + nonce[2] + (f >> 32);
190+
h2 = (u32)f;
191+
f = (u64)h3 + nonce[3] + (f >> 32);
192+
h3 = (u32)f;
193+
194+
put_unaligned_le32(h0, &mac[0]);
195+
put_unaligned_le32(h1, &mac[4]);
196+
put_unaligned_le32(h2, &mac[8]);
197+
put_unaligned_le32(h3, &mac[12]);
198+
}
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/* SPDX-License-Identifier: GPL-2.0
2+
*
3+
* Copyright (C) 2015-2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4+
*
5+
* This is based in part on Andrew Moon's poly1305-donna, which is in the
6+
* public domain.
7+
*/
8+
9+
typedef __uint128_t u128;
10+
11+
struct poly1305_internal {
12+
u64 r[3];
13+
u64 h[3];
14+
};
15+
16+
static void poly1305_init_generic(void *ctx, const u8 key[16])
17+
{
18+
struct poly1305_internal *st = (struct poly1305_internal *)ctx;
19+
u64 t0, t1;
20+
21+
/* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
22+
t0 = get_unaligned_le64(&key[0]);
23+
t1 = get_unaligned_le64(&key[8]);
24+
25+
/* wiped after finalization */
26+
st->r[0] = (t0) &0xffc0fffffff;
27+
st->r[1] = ((t0 >> 44) | (t1 << 20)) & 0xfffffc0ffff;
28+
st->r[2] = ((t1 >> 24)) & 0x00ffffffc0f;
29+
30+
/* h = 0 */
31+
st->h[0] = 0;
32+
st->h[1] = 0;
33+
st->h[2] = 0;
34+
}
35+
36+
static void poly1305_blocks_generic(void *ctx, const u8 *input, size_t len,
37+
const u32 padbit)
38+
{
39+
struct poly1305_internal *st = (struct poly1305_internal *)ctx;
40+
const u64 hibit = padbit ? (1ULL << 40) : 0;
41+
u64 r0, r1, r2;
42+
u64 s1, s2;
43+
u64 h0, h1, h2;
44+
u64 c;
45+
u128 d0, d1, d2, d;
46+
47+
r0 = st->r[0];
48+
r1 = st->r[1];
49+
r2 = st->r[2];
50+
51+
h0 = st->h[0];
52+
h1 = st->h[1];
53+
h2 = st->h[2];
54+
55+
s1 = r1 * (5 << 2);
56+
s2 = r2 * (5 << 2);
57+
58+
while (len >= POLY1305_BLOCK_SIZE) {
59+
u64 t0, t1;
60+
61+
/* h += m[i] */
62+
t0 = get_unaligned_le64(&input[0]);
63+
t1 = get_unaligned_le64(&input[8]);
64+
65+
h0 += ((t0) &0xfffffffffff);
66+
h1 += (((t0 >> 44) | (t1 << 20)) & 0xfffffffffff);
67+
h2 += (((t1 >> 24)) & 0x3ffffffffff) | hibit;
68+
69+
/* h *= r */
70+
d0 = ((u128)h0 * r0);
71+
d = ((u128)h1 * s2);
72+
d0 += d;
73+
d = ((u128)h2 * s1);
74+
d0 += d;
75+
d1 = ((u128)h0 * r1);
76+
d = ((u128)h1 * r0);
77+
d1 += d;
78+
d = ((u128)h2 * s2);
79+
d1 += d;
80+
d2 = ((u128)h0 * r2);
81+
d = ((u128)h1 * r1);
82+
d2 += d;
83+
d = ((u128)h2 * r0);
84+
d2 += d;
85+
86+
/* (partial) h %= p */
87+
c = (u64)(d0 >> 44);
88+
h0 = (u64)d0 & 0xfffffffffff;
89+
d1 += c;
90+
c = (u64)(d1 >> 44);
91+
h1 = (u64)d1 & 0xfffffffffff;
92+
d2 += c;
93+
c = (u64)(d2 >> 42);
94+
h2 = (u64)d2 & 0x3ffffffffff;
95+
h0 += c * 5;
96+
c = (h0 >> 44);
97+
h0 = h0 & 0xfffffffffff;
98+
h1 += c;
99+
100+
input += POLY1305_BLOCK_SIZE;
101+
len -= POLY1305_BLOCK_SIZE;
102+
}
103+
104+
st->h[0] = h0;
105+
st->h[1] = h1;
106+
st->h[2] = h2;
107+
}
108+
109+
static void poly1305_emit_generic(void *ctx, u8 mac[16], const u32 nonce[4])
110+
{
111+
struct poly1305_internal *st = (struct poly1305_internal *)ctx;
112+
u64 h0, h1, h2, c;
113+
u64 g0, g1, g2;
114+
u64 t0, t1;
115+
116+
/* fully carry h */
117+
h0 = st->h[0];
118+
h1 = st->h[1];
119+
h2 = st->h[2];
120+
121+
c = (h1 >> 44);
122+
h1 &= 0xfffffffffff;
123+
h2 += c;
124+
c = (h2 >> 42);
125+
h2 &= 0x3ffffffffff;
126+
h0 += c * 5;
127+
c = (h0 >> 44);
128+
h0 &= 0xfffffffffff;
129+
h1 += c;
130+
c = (h1 >> 44);
131+
h1 &= 0xfffffffffff;
132+
h2 += c;
133+
c = (h2 >> 42);
134+
h2 &= 0x3ffffffffff;
135+
h0 += c * 5;
136+
c = (h0 >> 44);
137+
h0 &= 0xfffffffffff;
138+
h1 += c;
139+
140+
/* compute h + -p */
141+
g0 = h0 + 5;
142+
c = (g0 >> 44);
143+
g0 &= 0xfffffffffff;
144+
g1 = h1 + c;
145+
c = (g1 >> 44);
146+
g1 &= 0xfffffffffff;
147+
g2 = h2 + c - (1ULL << 42);
148+
149+
/* select h if h < p, or h + -p if h >= p */
150+
c = (g2 >> ((sizeof(u64) * 8) - 1)) - 1;
151+
g0 &= c;
152+
g1 &= c;
153+
g2 &= c;
154+
c = ~c;
155+
h0 = (h0 & c) | g0;
156+
h1 = (h1 & c) | g1;
157+
h2 = (h2 & c) | g2;
158+
159+
/* h = (h + nonce) */
160+
t0 = ((u64)nonce[1] << 32) | nonce[0];
161+
t1 = ((u64)nonce[3] << 32) | nonce[2];
162+
163+
h0 += ((t0) &0xfffffffffff);
164+
c = (h0 >> 44);
165+
h0 &= 0xfffffffffff;
166+
h1 += (((t0 >> 44) | (t1 << 20)) & 0xfffffffffff) + c;
167+
c = (h1 >> 44);
168+
h1 &= 0xfffffffffff;
169+
h2 += (((t1 >> 24)) & 0x3ffffffffff) + c;
170+
h2 &= 0x3ffffffffff;
171+
172+
/* mac = h % (2^128) */
173+
h0 = (h0 | (h1 << 44));
174+
h1 = ((h1 >> 20) | (h2 << 24));
175+
176+
put_unaligned_le64(h0, &mac[0]);
177+
put_unaligned_le64(h1, &mac[8]);
178+
}

0 commit comments

Comments
 (0)