1 /* Copyright 2014 The BoringSSL Authors
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15 // This implementation was taken from the public domain, neon2 version in
16 // SUPERCOP by D. J. Bernstein and Peter Schwabe.
17
18 #include <openssl/poly1305.h>
19
20 #include <assert.h>
21 #include <string.h>
22
23 #include "../internal.h"
24 #include "internal.h"
25
26
27 #if defined(OPENSSL_POLY1305_NEON)
28
29 typedef struct {
30 uint32_t v[12]; // for alignment; only using 10
31 } fe1305x2;
32
33 #define addmulmod openssl_poly1305_neon2_addmulmod
34 #define blocks openssl_poly1305_neon2_blocks
35
36 extern "C" {
37 extern void addmulmod(fe1305x2 *r, const fe1305x2 *x, const fe1305x2 *y,
38 const fe1305x2 *c);
39
40 extern int blocks(fe1305x2 *h, const fe1305x2 *precomp, const uint8_t *in,
41 size_t inlen);
42 }
43
freeze(fe1305x2 * r)44 static void freeze(fe1305x2 *r) {
45 int i;
46
47 uint32_t x0 = r->v[0];
48 uint32_t x1 = r->v[2];
49 uint32_t x2 = r->v[4];
50 uint32_t x3 = r->v[6];
51 uint32_t x4 = r->v[8];
52 uint32_t y0;
53 uint32_t y1;
54 uint32_t y2;
55 uint32_t y3;
56 uint32_t y4;
57 uint32_t swap;
58
59 for (i = 0; i < 3; ++i) {
60 x1 += x0 >> 26;
61 x0 &= 0x3ffffff;
62 x2 += x1 >> 26;
63 x1 &= 0x3ffffff;
64 x3 += x2 >> 26;
65 x2 &= 0x3ffffff;
66 x4 += x3 >> 26;
67 x3 &= 0x3ffffff;
68 x0 += 5 * (x4 >> 26);
69 x4 &= 0x3ffffff;
70 }
71
72 y0 = x0 + 5;
73 y1 = x1 + (y0 >> 26);
74 y0 &= 0x3ffffff;
75 y2 = x2 + (y1 >> 26);
76 y1 &= 0x3ffffff;
77 y3 = x3 + (y2 >> 26);
78 y2 &= 0x3ffffff;
79 y4 = x4 + (y3 >> 26);
80 y3 &= 0x3ffffff;
81 swap = -(y4 >> 26);
82 y4 &= 0x3ffffff;
83
84 y0 ^= x0;
85 y1 ^= x1;
86 y2 ^= x2;
87 y3 ^= x3;
88 y4 ^= x4;
89
90 y0 &= swap;
91 y1 &= swap;
92 y2 &= swap;
93 y3 &= swap;
94 y4 &= swap;
95
96 y0 ^= x0;
97 y1 ^= x1;
98 y2 ^= x2;
99 y3 ^= x3;
100 y4 ^= x4;
101
102 r->v[0] = y0;
103 r->v[2] = y1;
104 r->v[4] = y2;
105 r->v[6] = y3;
106 r->v[8] = y4;
107 }
108
store32(uint8_t out[4],uint32_t v)109 static void store32(uint8_t out[4], uint32_t v) { OPENSSL_memcpy(out, &v, 4); }
110
111 // load32 exists to avoid breaking strict aliasing rules in
112 // fe1305x2_frombytearray.
load32(const uint8_t t[4])113 static uint32_t load32(const uint8_t t[4]) {
114 uint32_t tmp;
115 OPENSSL_memcpy(&tmp, t, sizeof(tmp));
116 return tmp;
117 }
118
fe1305x2_tobytearray(uint8_t r[16],fe1305x2 * x)119 static void fe1305x2_tobytearray(uint8_t r[16], fe1305x2 *x) {
120 uint32_t x0 = x->v[0];
121 uint32_t x1 = x->v[2];
122 uint32_t x2 = x->v[4];
123 uint32_t x3 = x->v[6];
124 uint32_t x4 = x->v[8];
125
126 x1 += x0 >> 26;
127 x0 &= 0x3ffffff;
128 x2 += x1 >> 26;
129 x1 &= 0x3ffffff;
130 x3 += x2 >> 26;
131 x2 &= 0x3ffffff;
132 x4 += x3 >> 26;
133 x3 &= 0x3ffffff;
134
135 store32(r, x0 + (x1 << 26));
136 store32(r + 4, (x1 >> 6) + (x2 << 20));
137 store32(r + 8, (x2 >> 12) + (x3 << 14));
138 store32(r + 12, (x3 >> 18) + (x4 << 8));
139 }
140
fe1305x2_frombytearray(fe1305x2 * r,const uint8_t * x,size_t xlen)141 static void fe1305x2_frombytearray(fe1305x2 *r, const uint8_t *x, size_t xlen) {
142 size_t i;
143 uint8_t t[17];
144
145 for (i = 0; (i < 16) && (i < xlen); i++) {
146 t[i] = x[i];
147 }
148 xlen -= i;
149 x += i;
150 t[i++] = 1;
151 for (; i < 17; i++) {
152 t[i] = 0;
153 }
154
155 r->v[0] = 0x3ffffff & load32(t);
156 r->v[2] = 0x3ffffff & (load32(t + 3) >> 2);
157 r->v[4] = 0x3ffffff & (load32(t + 6) >> 4);
158 r->v[6] = 0x3ffffff & (load32(t + 9) >> 6);
159 r->v[8] = load32(t + 13);
160
161 if (xlen) {
162 for (i = 0; (i < 16) && (i < xlen); i++) {
163 t[i] = x[i];
164 }
165 t[i++] = 1;
166 for (; i < 17; i++) {
167 t[i] = 0;
168 }
169
170 r->v[1] = 0x3ffffff & load32(t);
171 r->v[3] = 0x3ffffff & (load32(t + 3) >> 2);
172 r->v[5] = 0x3ffffff & (load32(t + 6) >> 4);
173 r->v[7] = 0x3ffffff & (load32(t + 9) >> 6);
174 r->v[9] = load32(t + 13);
175 } else {
176 r->v[1] = r->v[3] = r->v[5] = r->v[7] = r->v[9] = 0;
177 }
178 }
179
180 static const fe1305x2 zero alignas(16) = {0};
181
182 struct poly1305_state_st {
183 uint8_t data[sizeof(fe1305x2[5]) + 128];
184 uint8_t buf[32];
185 size_t buf_used;
186 uint8_t key[16];
187 };
188
189 static_assert(
190 sizeof(struct poly1305_state_st) + 63 <= sizeof(poly1305_state),
191 "poly1305_state isn't large enough to hold aligned poly1305_state_st.");
192
CRYPTO_poly1305_init_neon(poly1305_state * state,const uint8_t key[32])193 void CRYPTO_poly1305_init_neon(poly1305_state *state, const uint8_t key[32]) {
194 struct poly1305_state_st *st = (struct poly1305_state_st *)(state);
195 fe1305x2 *const r = (fe1305x2 *)(st->data + (15 & (-(int)st->data)));
196 fe1305x2 *const h = r + 1;
197 fe1305x2 *const c = h + 1;
198 fe1305x2 *const precomp = c + 1;
199
200 r->v[1] = r->v[0] = 0x3ffffff & load32(key);
201 r->v[3] = r->v[2] = 0x3ffff03 & (load32(key + 3) >> 2);
202 r->v[5] = r->v[4] = 0x3ffc0ff & (load32(key + 6) >> 4);
203 r->v[7] = r->v[6] = 0x3f03fff & (load32(key + 9) >> 6);
204 r->v[9] = r->v[8] = 0x00fffff & (load32(key + 12) >> 8);
205
206 for (size_t j = 0; j < 10; j++) {
207 h->v[j] = 0; // XXX: should fast-forward a bit
208 }
209
210 addmulmod(precomp, r, r, &zero); // precompute r^2
211 addmulmod(precomp + 1, precomp, precomp, &zero); // precompute r^4
212
213 OPENSSL_memcpy(st->key, key + 16, 16);
214 st->buf_used = 0;
215 }
216
CRYPTO_poly1305_update_neon(poly1305_state * state,const uint8_t * in,size_t in_len)217 void CRYPTO_poly1305_update_neon(poly1305_state *state, const uint8_t *in,
218 size_t in_len) {
219 struct poly1305_state_st *st = (struct poly1305_state_st *)(state);
220 fe1305x2 *const r = (fe1305x2 *)(st->data + (15 & (-(int)st->data)));
221 fe1305x2 *const h = r + 1;
222 fe1305x2 *const c = h + 1;
223 fe1305x2 *const precomp = c + 1;
224
225 if (st->buf_used) {
226 size_t todo = 32 - st->buf_used;
227 if (todo > in_len) {
228 todo = in_len;
229 }
230 for (size_t i = 0; i < todo; i++) {
231 st->buf[st->buf_used + i] = in[i];
232 }
233 st->buf_used += todo;
234 in_len -= todo;
235 in += todo;
236
237 if (st->buf_used == sizeof(st->buf) && in_len) {
238 addmulmod(h, h, precomp, &zero);
239 fe1305x2_frombytearray(c, st->buf, sizeof(st->buf));
240 for (size_t i = 0; i < 10; i++) {
241 h->v[i] += c->v[i];
242 }
243 st->buf_used = 0;
244 }
245 }
246
247 while (in_len > 32) {
248 size_t tlen = 1048576;
249 if (in_len < tlen) {
250 tlen = in_len;
251 }
252 tlen -= blocks(h, precomp, in, tlen);
253 in_len -= tlen;
254 in += tlen;
255 }
256
257 if (in_len) {
258 for (size_t i = 0; i < in_len; i++) {
259 st->buf[i] = in[i];
260 }
261 st->buf_used = in_len;
262 }
263 }
264
CRYPTO_poly1305_finish_neon(poly1305_state * state,uint8_t mac[16])265 void CRYPTO_poly1305_finish_neon(poly1305_state *state, uint8_t mac[16]) {
266 struct poly1305_state_st *st = (struct poly1305_state_st *)(state);
267 fe1305x2 *const r = (fe1305x2 *)(st->data + (15 & (-(int)st->data)));
268 fe1305x2 *const h = r + 1;
269 fe1305x2 *const c = h + 1;
270 fe1305x2 *const precomp = c + 1;
271
272 addmulmod(h, h, precomp, &zero);
273
274 if (st->buf_used > 16) {
275 fe1305x2_frombytearray(c, st->buf, st->buf_used);
276 precomp->v[1] = r->v[1];
277 precomp->v[3] = r->v[3];
278 precomp->v[5] = r->v[5];
279 precomp->v[7] = r->v[7];
280 precomp->v[9] = r->v[9];
281 addmulmod(h, h, precomp, c);
282 } else if (st->buf_used > 0) {
283 fe1305x2_frombytearray(c, st->buf, st->buf_used);
284 r->v[1] = 1;
285 r->v[3] = 0;
286 r->v[5] = 0;
287 r->v[7] = 0;
288 r->v[9] = 0;
289 addmulmod(h, h, r, c);
290 }
291
292 h->v[0] += h->v[1];
293 h->v[2] += h->v[3];
294 h->v[4] += h->v[5];
295 h->v[6] += h->v[7];
296 h->v[8] += h->v[9];
297 freeze(h);
298
299 fe1305x2_frombytearray(c, st->key, 16);
300 c->v[8] ^= (1 << 24);
301
302 h->v[0] += c->v[0];
303 h->v[2] += c->v[2];
304 h->v[4] += c->v[4];
305 h->v[6] += c->v[6];
306 h->v[8] += c->v[8];
307 fe1305x2_tobytearray(mac, h);
308 }
309
310 #endif // OPENSSL_POLY1305_NEON
311