Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/poly1305/poly1305_arm.c

raw
1 // Copyright (c) 2014, Google Inc.
2 // SPDX-License-Identifier: ISC
3
4 // This implementation was taken from the public domain, neon2 version in
5 // SUPERCOP by D. J. Bernstein and Peter Schwabe.
6
7 #include <openssl/poly1305.h>
8
9 #include <string.h>
10
11 #include "../internal.h"
12 #include "internal.h"
13
14
15 #if defined(OPENSSL_POLY1305_NEON)
16
17 typedef struct {
18 uint32_t v[12]; // for alignment; only using 10
19 } fe1305x2;
20
21 #define addmulmod openssl_poly1305_neon2_addmulmod
22 #define blocks openssl_poly1305_neon2_blocks
23
24 extern void addmulmod(fe1305x2 *r, const fe1305x2 *x, const fe1305x2 *y,
25 const fe1305x2 *c);
26
27 extern int blocks(fe1305x2 *h, const fe1305x2 *precomp, const uint8_t *in,
28 size_t inlen);
29
30 static void freeze(fe1305x2 *r) {
31 int i;
32
33 uint32_t x0 = r->v[0];
34 uint32_t x1 = r->v[2];
35 uint32_t x2 = r->v[4];
36 uint32_t x3 = r->v[6];
37 uint32_t x4 = r->v[8];
38 uint32_t y0;
39 uint32_t y1;
40 uint32_t y2;
41 uint32_t y3;
42 uint32_t y4;
43 uint32_t swap;
44
45 for (i = 0; i < 3; ++i) {
46 x1 += x0 >> 26;
47 x0 &= 0x3ffffff;
48 x2 += x1 >> 26;
49 x1 &= 0x3ffffff;
50 x3 += x2 >> 26;
51 x2 &= 0x3ffffff;
52 x4 += x3 >> 26;
53 x3 &= 0x3ffffff;
54 x0 += 5 * (x4 >> 26);
55 x4 &= 0x3ffffff;
56 }
57
58 y0 = x0 + 5;
59 y1 = x1 + (y0 >> 26);
60 y0 &= 0x3ffffff;
61 y2 = x2 + (y1 >> 26);
62 y1 &= 0x3ffffff;
63 y3 = x3 + (y2 >> 26);
64 y2 &= 0x3ffffff;
65 y4 = x4 + (y3 >> 26);
66 y3 &= 0x3ffffff;
67 swap = -(y4 >> 26);
68 y4 &= 0x3ffffff;
69
70 y0 ^= x0;
71 y1 ^= x1;
72 y2 ^= x2;
73 y3 ^= x3;
74 y4 ^= x4;
75
76 y0 &= swap;
77 y1 &= swap;
78 y2 &= swap;
79 y3 &= swap;
80 y4 &= swap;
81
82 y0 ^= x0;
83 y1 ^= x1;
84 y2 ^= x2;
85 y3 ^= x3;
86 y4 ^= x4;
87
88 r->v[0] = y0;
89 r->v[2] = y1;
90 r->v[4] = y2;
91 r->v[6] = y3;
92 r->v[8] = y4;
93 }
94
95 static void store32(uint8_t out[4], uint32_t v) { OPENSSL_memcpy(out, &v, 4); }
96
97 // load32 exists to avoid breaking strict aliasing rules in
98 // fe1305x2_frombytearray.
99 static uint32_t load32(const uint8_t t[4]) {
100 uint32_t tmp;
101 OPENSSL_memcpy(&tmp, t, sizeof(tmp));
102 return tmp;
103 }
104
105 static void fe1305x2_tobytearray(uint8_t r[16], fe1305x2 *x) {
106 uint32_t x0 = x->v[0];
107 uint32_t x1 = x->v[2];
108 uint32_t x2 = x->v[4];
109 uint32_t x3 = x->v[6];
110 uint32_t x4 = x->v[8];
111
112 x1 += x0 >> 26;
113 x0 &= 0x3ffffff;
114 x2 += x1 >> 26;
115 x1 &= 0x3ffffff;
116 x3 += x2 >> 26;
117 x2 &= 0x3ffffff;
118 x4 += x3 >> 26;
119 x3 &= 0x3ffffff;
120
121 store32(r, x0 + (x1 << 26));
122 store32(r + 4, (x1 >> 6) + (x2 << 20));
123 store32(r + 8, (x2 >> 12) + (x3 << 14));
124 store32(r + 12, (x3 >> 18) + (x4 << 8));
125 }
126
127 static void fe1305x2_frombytearray(fe1305x2 *r, const uint8_t *x, size_t xlen) {
128 size_t i;
129 uint8_t t[17];
130
131 for (i = 0; (i < 16) && (i < xlen); i++) {
132 t[i] = x[i];
133 }
134 xlen -= i;
135 x += i;
136 t[i++] = 1;
137 for (; i < 17; i++) {
138 t[i] = 0;
139 }
140
141 r->v[0] = 0x3ffffff & load32(t);
142 r->v[2] = 0x3ffffff & (load32(t + 3) >> 2);
143 r->v[4] = 0x3ffffff & (load32(t + 6) >> 4);
144 r->v[6] = 0x3ffffff & (load32(t + 9) >> 6);
145 r->v[8] = load32(t + 13);
146
147 if (xlen) {
148 for (i = 0; (i < 16) && (i < xlen); i++) {
149 t[i] = x[i];
150 }
151 t[i++] = 1;
152 for (; i < 17; i++) {
153 t[i] = 0;
154 }
155
156 r->v[1] = 0x3ffffff & load32(t);
157 r->v[3] = 0x3ffffff & (load32(t + 3) >> 2);
158 r->v[5] = 0x3ffffff & (load32(t + 6) >> 4);
159 r->v[7] = 0x3ffffff & (load32(t + 9) >> 6);
160 r->v[9] = load32(t + 13);
161 } else {
162 r->v[1] = r->v[3] = r->v[5] = r->v[7] = r->v[9] = 0;
163 }
164 }
165
166 static const alignas(16) fe1305x2 zero;
167
168 struct poly1305_state_st {
169 uint8_t data[sizeof(fe1305x2[5]) + 128];
170 uint8_t buf[32];
171 size_t buf_used;
172 uint8_t key[16];
173 };
174
175 OPENSSL_STATIC_ASSERT(
176 sizeof(struct poly1305_state_st) + 63 <= sizeof(poly1305_state),
177 _poly1305_state_isn_t_large_enough_to_hold_aligned_poly1305_state_st)
178
179 void CRYPTO_poly1305_init_neon(poly1305_state *state, const uint8_t key[32]) {
180 struct poly1305_state_st *st = poly1305_aligned_state(state);
181 fe1305x2 *const r = (fe1305x2 *)(st->data + (15 & (-(int)st->data)));
182 fe1305x2 *const h = r + 1;
183 fe1305x2 *const c = h + 1;
184 fe1305x2 *const precomp = c + 1;
185
186 r->v[1] = r->v[0] = 0x3ffffff & load32(key);
187 r->v[3] = r->v[2] = 0x3ffff03 & (load32(key + 3) >> 2);
188 r->v[5] = r->v[4] = 0x3ffc0ff & (load32(key + 6) >> 4);
189 r->v[7] = r->v[6] = 0x3f03fff & (load32(key + 9) >> 6);
190 r->v[9] = r->v[8] = 0x00fffff & (load32(key + 12) >> 8);
191
192 for (size_t j = 0; j < 10; j++) {
193 h->v[j] = 0; // XXX: should fast-forward a bit
194 }
195
196 addmulmod(precomp, r, r, &zero); // precompute r^2
197 addmulmod(precomp + 1, precomp, precomp, &zero); // precompute r^4
198
199 OPENSSL_memcpy(st->key, key + 16, 16);
200 st->buf_used = 0;
201 }
202
203 void CRYPTO_poly1305_update_neon(poly1305_state *state, const uint8_t *in,
204 size_t in_len) {
205 struct poly1305_state_st *st = poly1305_aligned_state(state);
206 fe1305x2 *const r = (fe1305x2 *)(st->data + (15 & (-(int)st->data)));
207 fe1305x2 *const h = r + 1;
208 fe1305x2 *const c = h + 1;
209 fe1305x2 *const precomp = c + 1;
210
211 if (st->buf_used) {
212 size_t todo = 32 - st->buf_used;
213 if (todo > in_len) {
214 todo = in_len;
215 }
216 for (size_t i = 0; i < todo; i++) {
217 st->buf[st->buf_used + i] = in[i];
218 }
219 st->buf_used += todo;
220 in_len -= todo;
221 in += todo;
222
223 if (st->buf_used == sizeof(st->buf) && in_len) {
224 addmulmod(h, h, precomp, &zero);
225 fe1305x2_frombytearray(c, st->buf, sizeof(st->buf));
226 for (size_t i = 0; i < 10; i++) {
227 h->v[i] += c->v[i];
228 }
229 st->buf_used = 0;
230 }
231 }
232
233 while (in_len > 32) {
234 size_t tlen = 1048576;
235 if (in_len < tlen) {
236 tlen = in_len;
237 }
238 tlen -= blocks(h, precomp, in, tlen);
239 in_len -= tlen;
240 in += tlen;
241 }
242
243 if (in_len) {
244 for (size_t i = 0; i < in_len; i++) {
245 st->buf[i] = in[i];
246 }
247 st->buf_used = in_len;
248 }
249 }
250
251 void CRYPTO_poly1305_finish_neon(poly1305_state *state, uint8_t mac[16]) {
252 struct poly1305_state_st *st = poly1305_aligned_state(state);
253 fe1305x2 *const r = (fe1305x2 *)(st->data + (15 & (-(int)st->data)));
254 fe1305x2 *const h = r + 1;
255 fe1305x2 *const c = h + 1;
256 fe1305x2 *const precomp = c + 1;
257
258 addmulmod(h, h, precomp, &zero);
259
260 if (st->buf_used > 16) {
261 fe1305x2_frombytearray(c, st->buf, st->buf_used);
262 precomp->v[1] = r->v[1];
263 precomp->v[3] = r->v[3];
264 precomp->v[5] = r->v[5];
265 precomp->v[7] = r->v[7];
266 precomp->v[9] = r->v[9];
267 addmulmod(h, h, precomp, c);
268 } else if (st->buf_used > 0) {
269 fe1305x2_frombytearray(c, st->buf, st->buf_used);
270 r->v[1] = 1;
271 r->v[3] = 0;
272 r->v[5] = 0;
273 r->v[7] = 0;
274 r->v[9] = 0;
275 addmulmod(h, h, r, c);
276 }
277
278 h->v[0] += h->v[1];
279 h->v[2] += h->v[3];
280 h->v[4] += h->v[5];
281 h->v[6] += h->v[7];
282 h->v[8] += h->v[9];
283 freeze(h);
284
285 fe1305x2_frombytearray(c, st->key, 16);
286 c->v[8] ^= (1 << 24);
287
288 h->v[0] += c->v[0];
289 h->v[2] += c->v[2];
290 h->v[4] += c->v[4];
291 h->v[6] += c->v[6];
292 h->v[8] += c->v[8];
293 fe1305x2_tobytearray(mac, h);
294 }
295
296 #endif // OPENSSL_POLY1305_NEON