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