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 <GFp/poly1305.h>
19 
20 #include "internal.h"
21 #include "../internal.h"
22 
23 
24 #if defined(OPENSSL_POLY1305_NEON)
25 
26 #pragma GCC diagnostic ignored "-Wsign-conversion"
27 #pragma GCC diagnostic ignored "-Wcast-align"
28 
29 typedef struct {
30   uint32_t v[12];  // for alignment; only using 10
31 } fe1305x2;
32 
33 #define addmulmod GFp_poly1305_neon2_addmulmod
34 #define blocks GFp_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) { GFp_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   GFp_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 OPENSSL_STATIC_ASSERT(sizeof(struct poly1305_state_st) <= sizeof(poly1305_state),
188   "poly1305_state isn't large enough to hold aligned poly1305_state_st");
189 
GFp_poly1305_init_neon(poly1305_state * state,const uint8_t key[32])190 void GFp_poly1305_init_neon(poly1305_state *state, const uint8_t key[32]) {
191   struct poly1305_state_st *st = (struct poly1305_state_st *)(state);
192   fe1305x2 *const r = (fe1305x2 *)(st->data + (15 & (-(int)st->data)));
193   fe1305x2 *const h = r + 1;
194   fe1305x2 *const c = h + 1;
195   fe1305x2 *const precomp = c + 1;
196 
197   r->v[1] = r->v[0] = 0x3ffffff & load32(key);
198   r->v[3] = r->v[2] = 0x3ffff03 & (load32(key + 3) >> 2);
199   r->v[5] = r->v[4] = 0x3ffc0ff & (load32(key + 6) >> 4);
200   r->v[7] = r->v[6] = 0x3f03fff & (load32(key + 9) >> 6);
201   r->v[9] = r->v[8] = 0x00fffff & (load32(key + 12) >> 8);
202 
203   for (size_t j = 0; j < 10; j++) {
204     h->v[j] = 0;  // XXX: should fast-forward a bit
205   }
206 
207   addmulmod(precomp, r, r, &zero);                  // precompute r^2
208   addmulmod(precomp + 1, precomp, precomp, &zero);  // precompute r^4
209 
210   GFp_memcpy(st->key, key + 16, 16);
211   st->buf_used = 0;
212 }
213 
GFp_poly1305_update_neon(poly1305_state * state,const uint8_t * in,size_t in_len)214 void GFp_poly1305_update_neon(poly1305_state *state, const uint8_t *in,
215                               size_t in_len) {
216   struct poly1305_state_st *st = (struct poly1305_state_st *)(state);
217   fe1305x2 *const r = (fe1305x2 *)(st->data + (15 & (-(int)st->data)));
218   fe1305x2 *const h = r + 1;
219   fe1305x2 *const c = h + 1;
220   fe1305x2 *const precomp = c + 1;
221 
222   if (st->buf_used) {
223     size_t todo = 32 - st->buf_used;
224     if (todo > in_len) {
225       todo = in_len;
226     }
227     for (size_t i = 0; i < todo; i++) {
228       st->buf[st->buf_used + i] = in[i];
229     }
230     st->buf_used += todo;
231     in_len -= todo;
232     in += todo;
233 
234     if (st->buf_used == sizeof(st->buf) && in_len) {
235       addmulmod(h, h, precomp, &zero);
236       fe1305x2_frombytearray(c, st->buf, sizeof(st->buf));
237       for (size_t i = 0; i < 10; i++) {
238         h->v[i] += c->v[i];
239       }
240       st->buf_used = 0;
241     }
242   }
243 
244   while (in_len > 32) {
245     size_t tlen = 1048576;
246     if (in_len < tlen) {
247       tlen = in_len;
248     }
249     tlen -= blocks(h, precomp, in, tlen);
250     in_len -= tlen;
251     in += tlen;
252   }
253 
254   if (in_len) {
255     for (size_t i = 0; i < in_len; i++) {
256       st->buf[i] = in[i];
257     }
258     st->buf_used = in_len;
259   }
260 }
261 
GFp_poly1305_finish_neon(poly1305_state * state,uint8_t mac[16])262 void GFp_poly1305_finish_neon(poly1305_state *state, uint8_t mac[16]) {
263   struct poly1305_state_st *st = (struct poly1305_state_st *)(state);
264   fe1305x2 *const r = (fe1305x2 *)(st->data + (15 & (-(int)st->data)));
265   fe1305x2 *const h = r + 1;
266   fe1305x2 *const c = h + 1;
267   fe1305x2 *const precomp = c + 1;
268 
269   addmulmod(h, h, precomp, &zero);
270 
271   if (st->buf_used > 16) {
272     fe1305x2_frombytearray(c, st->buf, st->buf_used);
273     precomp->v[1] = r->v[1];
274     precomp->v[3] = r->v[3];
275     precomp->v[5] = r->v[5];
276     precomp->v[7] = r->v[7];
277     precomp->v[9] = r->v[9];
278     addmulmod(h, h, precomp, c);
279   } else if (st->buf_used > 0) {
280     fe1305x2_frombytearray(c, st->buf, st->buf_used);
281     r->v[1] = 1;
282     r->v[3] = 0;
283     r->v[5] = 0;
284     r->v[7] = 0;
285     r->v[9] = 0;
286     addmulmod(h, h, r, c);
287   }
288 
289   h->v[0] += h->v[1];
290   h->v[2] += h->v[3];
291   h->v[4] += h->v[5];
292   h->v[6] += h->v[7];
293   h->v[8] += h->v[9];
294   freeze(h);
295 
296   fe1305x2_frombytearray(c, st->key, 16);
297   c->v[8] ^= (1 << 24);
298 
299   h->v[0] += c->v[0];
300   h->v[2] += c->v[2];
301   h->v[4] += c->v[4];
302   h->v[6] += c->v[6];
303   h->v[8] += c->v[8];
304   fe1305x2_tobytearray(mac, h);
305 }
306 
307 #endif  // OPENSSL_POLY1305_NEON
308