]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/if_wg/module/crypto/zinc/poly1305/poly1305-donna64.c
Import kernel WireGuard support
[FreeBSD/FreeBSD.git] / sys / dev / if_wg / module / crypto / zinc / poly1305 / poly1305-donna64.c
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /*
3  * Copyright (C) 2015-2019 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         u64 s[2];
15 };
16
17 static void poly1305_init_generic(void *ctx, const u8 key[16])
18 {
19         struct poly1305_internal *st = (struct poly1305_internal *)ctx;
20         u64 t0, t1;
21
22         /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
23         t0 = get_unaligned_le64(&key[0]);
24         t1 = get_unaligned_le64(&key[8]);
25
26         st->r[0] = t0 & 0xffc0fffffffULL;
27         st->r[1] = ((t0 >> 44) | (t1 << 20)) & 0xfffffc0ffffULL;
28         st->r[2] = ((t1 >> 24)) & 0x00ffffffc0fULL;
29
30         /* s = 20*r */
31         st->s[0] = st->r[1] * 20;
32         st->s[1] = st->r[2] * 20;
33
34         /* h = 0 */
35         st->h[0] = 0;
36         st->h[1] = 0;
37         st->h[2] = 0;
38 }
39
40 static void poly1305_blocks_generic(void *ctx, const u8 *input, size_t len,
41                                     const u32 padbit)
42 {
43         struct poly1305_internal *st = (struct poly1305_internal *)ctx;
44         const u64 hibit = ((u64)padbit) << 40;
45         u64 r0, r1, r2;
46         u64 s1, s2;
47         u64 h0, h1, h2;
48         u64 c;
49         u128 d0, d1, d2, d;
50
51         r0 = st->r[0];
52         r1 = st->r[1];
53         r2 = st->r[2];
54
55         h0 = st->h[0];
56         h1 = st->h[1];
57         h2 = st->h[2];
58
59         s1 = st->s[0];
60         s2 = st->s[1];
61
62         while (len >= POLY1305_BLOCK_SIZE) {
63                 u64 t0, t1;
64
65                 /* h += m[i] */
66                 t0 = get_unaligned_le64(&input[0]);
67                 t1 = get_unaligned_le64(&input[8]);
68
69                 h0 += t0 & 0xfffffffffffULL;
70                 h1 += ((t0 >> 44) | (t1 << 20)) & 0xfffffffffffULL;
71                 h2 += (((t1 >> 24)) & 0x3ffffffffffULL) | hibit;
72
73                 /* h *= r */
74                 d0 = (u128)h0 * r0;
75                 d = (u128)h1 * s2;
76                 d0 += d;
77                 d = (u128)h2 * s1;
78                 d0 += d;
79                 d1 = (u128)h0 * r1;
80                 d = (u128)h1 * r0;
81                 d1 += d;
82                 d = (u128)h2 * s2;
83                 d1 += d;
84                 d2 = (u128)h0 * r2;
85                 d = (u128)h1 * r1;
86                 d2 += d;
87                 d = (u128)h2 * r0;
88                 d2 += d;
89
90                 /* (partial) h %= p */
91                 c = (u64)(d0 >> 44);
92                 h0 = (u64)d0 & 0xfffffffffffULL;
93                 d1 += c;
94                 c = (u64)(d1 >> 44);
95                 h1 = (u64)d1 & 0xfffffffffffULL;
96                 d2 += c;
97                 c = (u64)(d2 >> 42);
98                 h2 = (u64)d2 & 0x3ffffffffffULL;
99                 h0 += c * 5;
100                 c = h0 >> 44;
101                 h0 = h0 & 0xfffffffffffULL;
102                 h1 += c;
103
104                 input += POLY1305_BLOCK_SIZE;
105                 len -= POLY1305_BLOCK_SIZE;
106         }
107
108         st->h[0] = h0;
109         st->h[1] = h1;
110         st->h[2] = h2;
111 }
112
113 static void poly1305_emit_generic(void *ctx, u8 mac[16], const u32 nonce[4])
114 {
115         struct poly1305_internal *st = (struct poly1305_internal *)ctx;
116         u64 h0, h1, h2, c;
117         u64 g0, g1, g2;
118         u64 t0, t1;
119
120         /* fully carry h */
121         h0 = st->h[0];
122         h1 = st->h[1];
123         h2 = st->h[2];
124
125         c = h1 >> 44;
126         h1 &= 0xfffffffffffULL;
127         h2 += c;
128         c = h2 >> 42;
129         h2 &= 0x3ffffffffffULL;
130         h0 += c * 5;
131         c = h0 >> 44;
132         h0 &= 0xfffffffffffULL;
133         h1 += c;
134         c = h1 >> 44;
135         h1 &= 0xfffffffffffULL;
136         h2 += c;
137         c = h2 >> 42;
138         h2 &= 0x3ffffffffffULL;
139         h0 += c * 5;
140         c = h0 >> 44;
141         h0 &= 0xfffffffffffULL;
142         h1 += c;
143
144         /* compute h + -p */
145         g0 = h0 + 5;
146         c  = g0 >> 44;
147         g0 &= 0xfffffffffffULL;
148         g1 = h1 + c;
149         c  = g1 >> 44;
150         g1 &= 0xfffffffffffULL;
151         g2 = h2 + c - (1ULL << 42);
152
153         /* select h if h < p, or h + -p if h >= p */
154         c = (g2 >> ((sizeof(u64) * 8) - 1)) - 1;
155         g0 &= c;
156         g1 &= c;
157         g2 &= c;
158         c  = ~c;
159         h0 = (h0 & c) | g0;
160         h1 = (h1 & c) | g1;
161         h2 = (h2 & c) | g2;
162
163         /* h = (h + nonce) */
164         t0 = ((u64)nonce[1] << 32) | nonce[0];
165         t1 = ((u64)nonce[3] << 32) | nonce[2];
166
167         h0 += t0 & 0xfffffffffffULL;
168         c = h0 >> 44;
169         h0 &= 0xfffffffffffULL;
170         h1 += (((t0 >> 44) | (t1 << 20)) & 0xfffffffffffULL) + c;
171         c = h1 >> 44;
172         h1 &= 0xfffffffffffULL;
173         h2 += (((t1 >> 24)) & 0x3ffffffffffULL) + c;
174         h2 &= 0x3ffffffffffULL;
175
176         /* mac = h % (2^128) */
177         h0 = h0 | (h1 << 44);
178         h1 = (h1 >> 20) | (h2 << 24);
179
180         put_unaligned_le64(h0, &mac[0]);
181         put_unaligned_le64(h1, &mac[8]);
182 }