1 /* Copyright (c) 2014, Google Inc.
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.
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. */
15 // This implementation was taken from the public domain, neon2 version in
16 // SUPERCOP by D. J. Bernstein and Peter Schwabe.
18 #include <openssl/poly1305.h>
22 #include "../internal.h"
26 #if defined(OPENSSL_POLY1305_NEON)
29 uint32_t v[12]; // for alignment; only using 10
32 #define addmulmod openssl_poly1305_neon2_addmulmod
33 #define blocks openssl_poly1305_neon2_blocks
35 extern void addmulmod(fe1305x2 *r, const fe1305x2 *x, const fe1305x2 *y,
38 extern int blocks(fe1305x2 *h, const fe1305x2 *precomp, const uint8_t *in,
41 static void freeze(fe1305x2 *r) {
44 uint32_t x0 = r->v[0];
45 uint32_t x1 = r->v[2];
46 uint32_t x2 = r->v[4];
47 uint32_t x3 = r->v[6];
48 uint32_t x4 = r->v[8];
56 for (i = 0; i < 3; ++i) {
106 static void fe1305x2_tobytearray(uint8_t *r, fe1305x2 *x) {
107 uint32_t x0 = x->v[0];
108 uint32_t x1 = x->v[2];
109 uint32_t x2 = x->v[4];
110 uint32_t x3 = x->v[6];
111 uint32_t x4 = x->v[8];
122 *(uint32_t *)r = x0 + (x1 << 26);
123 *(uint32_t *)(r + 4) = (x1 >> 6) + (x2 << 20);
124 *(uint32_t *)(r + 8) = (x2 >> 12) + (x3 << 14);
125 *(uint32_t *)(r + 12) = (x3 >> 18) + (x4 << 8);
128 // load32 exists to avoid breaking strict aliasing rules in
129 // fe1305x2_frombytearray.
130 static uint32_t load32(uint8_t *t) {
132 OPENSSL_memcpy(&tmp, t, sizeof(tmp));
136 static void fe1305x2_frombytearray(fe1305x2 *r, const uint8_t *x,
137 unsigned long long xlen) {
141 for (i = 0; (i < 16) && (i < xlen); i++) {
147 for (; i < 17; i++) {
151 r->v[0] = 0x3ffffff & load32(t);
152 r->v[2] = 0x3ffffff & (load32(t + 3) >> 2);
153 r->v[4] = 0x3ffffff & (load32(t + 6) >> 4);
154 r->v[6] = 0x3ffffff & (load32(t + 9) >> 6);
155 r->v[8] = load32(t + 13);
158 for (i = 0; (i < 16) && (i < xlen); i++) {
162 for (; i < 17; i++) {
166 r->v[1] = 0x3ffffff & load32(t);
167 r->v[3] = 0x3ffffff & (load32(t + 3) >> 2);
168 r->v[5] = 0x3ffffff & (load32(t + 6) >> 4);
169 r->v[7] = 0x3ffffff & (load32(t + 9) >> 6);
170 r->v[9] = load32(t + 13);
172 r->v[1] = r->v[3] = r->v[5] = r->v[7] = r->v[9] = 0;
176 static const alignas(16) fe1305x2 zero;
178 struct poly1305_state_st {
179 uint8_t data[sizeof(fe1305x2[5]) + 128];
181 unsigned int buf_used;
185 void CRYPTO_poly1305_init_neon(poly1305_state *state, const uint8_t key[32]) {
186 struct poly1305_state_st *st = (struct poly1305_state_st *)(state);
187 fe1305x2 *const r = (fe1305x2 *)(st->data + (15 & (-(int)st->data)));
188 fe1305x2 *const h = r + 1;
189 fe1305x2 *const c = h + 1;
190 fe1305x2 *const precomp = c + 1;
193 r->v[1] = r->v[0] = 0x3ffffff & *(uint32_t *)key;
194 r->v[3] = r->v[2] = 0x3ffff03 & ((*(uint32_t *)(key + 3)) >> 2);
195 r->v[5] = r->v[4] = 0x3ffc0ff & ((*(uint32_t *)(key + 6)) >> 4);
196 r->v[7] = r->v[6] = 0x3f03fff & ((*(uint32_t *)(key + 9)) >> 6);
197 r->v[9] = r->v[8] = 0x00fffff & ((*(uint32_t *)(key + 12)) >> 8);
199 for (j = 0; j < 10; j++) {
200 h->v[j] = 0; // XXX: should fast-forward a bit
203 addmulmod(precomp, r, r, &zero); // precompute r^2
204 addmulmod(precomp + 1, precomp, precomp, &zero); // precompute r^4
206 OPENSSL_memcpy(st->key, key + 16, 16);
210 void CRYPTO_poly1305_update_neon(poly1305_state *state, const uint8_t *in,
212 struct poly1305_state_st *st = (struct poly1305_state_st *)(state);
213 fe1305x2 *const r = (fe1305x2 *)(st->data + (15 & (-(int)st->data)));
214 fe1305x2 *const h = r + 1;
215 fe1305x2 *const c = h + 1;
216 fe1305x2 *const precomp = c + 1;
220 unsigned int todo = 32 - st->buf_used;
224 for (i = 0; i < todo; i++) {
225 st->buf[st->buf_used + i] = in[i];
227 st->buf_used += todo;
231 if (st->buf_used == sizeof(st->buf) && in_len) {
232 addmulmod(h, h, precomp, &zero);
233 fe1305x2_frombytearray(c, st->buf, sizeof(st->buf));
234 for (i = 0; i < 10; i++) {
241 while (in_len > 32) {
242 unsigned int tlen = 1048576;
246 tlen -= blocks(h, precomp, in, tlen);
252 for (i = 0; i < in_len; i++) {
255 st->buf_used = in_len;
259 void CRYPTO_poly1305_finish_neon(poly1305_state *state, uint8_t mac[16]) {
260 struct poly1305_state_st *st = (struct poly1305_state_st *)(state);
261 fe1305x2 *const r = (fe1305x2 *)(st->data + (15 & (-(int)st->data)));
262 fe1305x2 *const h = r + 1;
263 fe1305x2 *const c = h + 1;
264 fe1305x2 *const precomp = c + 1;
266 addmulmod(h, h, precomp, &zero);
268 if (st->buf_used > 16) {
269 fe1305x2_frombytearray(c, st->buf, st->buf_used);
270 precomp->v[1] = r->v[1];
271 precomp->v[3] = r->v[3];
272 precomp->v[5] = r->v[5];
273 precomp->v[7] = r->v[7];
274 precomp->v[9] = r->v[9];
275 addmulmod(h, h, precomp, c);
276 } else if (st->buf_used > 0) {
277 fe1305x2_frombytearray(c, st->buf, st->buf_used);
283 addmulmod(h, h, r, c);
293 fe1305x2_frombytearray(c, st->key, 16);
294 c->v[8] ^= (1 << 24);
301 fe1305x2_tobytearray(mac, h);
304 #endif // OPENSSL_POLY1305_NEON