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 of poly1305 is by Andrew Moon
16 // (https://github.com/floodyberry/poly1305-donna) and released as public
17 // domain.
18
19 #include <openssl/poly1305.h>
20
21 #include <string.h>
22
23 #include <openssl/cpu.h>
24
25 #include "internal.h"
26 #include "../internal.h"
27
28
29 #if !defined(BORINGSSL_HAS_UINT128) || !defined(OPENSSL_X86_64)
30
31 // We can assume little-endian.
U8TO32_LE(const uint8_t * m)32 static uint32_t U8TO32_LE(const uint8_t *m) {
33 uint32_t r;
34 OPENSSL_memcpy(&r, m, sizeof(r));
35 return r;
36 }
37
U32TO8_LE(uint8_t * m,uint32_t v)38 static void U32TO8_LE(uint8_t *m, uint32_t v) {
39 OPENSSL_memcpy(m, &v, sizeof(v));
40 }
41
mul32x32_64(uint32_t a,uint32_t b)42 static uint64_t mul32x32_64(uint32_t a, uint32_t b) { return (uint64_t)a * b; }
43
44 struct poly1305_state_st {
45 uint32_t r0, r1, r2, r3, r4;
46 uint32_t s1, s2, s3, s4;
47 uint32_t h0, h1, h2, h3, h4;
48 uint8_t buf[16];
49 size_t buf_used;
50 uint8_t key[16];
51 };
52
53 OPENSSL_STATIC_ASSERT(
54 sizeof(struct poly1305_state_st) + 63 <= sizeof(poly1305_state),
55 "poly1305_state isn't large enough to hold aligned poly1305_state_st");
56
poly1305_aligned_state(poly1305_state * state)57 static inline struct poly1305_state_st *poly1305_aligned_state(
58 poly1305_state *state) {
59 return align_pointer(state, 64);
60 }
61
62 // poly1305_blocks updates |state| given some amount of input data. This
63 // function may only be called with a |len| that is not a multiple of 16 at the
64 // end of the data. Otherwise the input must be buffered into 16 byte blocks.
poly1305_update(struct poly1305_state_st * state,const uint8_t * in,size_t len)65 static void poly1305_update(struct poly1305_state_st *state, const uint8_t *in,
66 size_t len) {
67 uint32_t t0, t1, t2, t3;
68 uint64_t t[5];
69 uint32_t b;
70 uint64_t c;
71 size_t j;
72 uint8_t mp[16];
73
74 if (len < 16) {
75 goto poly1305_donna_atmost15bytes;
76 }
77
78 poly1305_donna_16bytes:
79 t0 = U8TO32_LE(in);
80 t1 = U8TO32_LE(in + 4);
81 t2 = U8TO32_LE(in + 8);
82 t3 = U8TO32_LE(in + 12);
83
84 in += 16;
85 len -= 16;
86
87 state->h0 += t0 & 0x3ffffff;
88 state->h1 += ((((uint64_t)t1 << 32) | t0) >> 26) & 0x3ffffff;
89 state->h2 += ((((uint64_t)t2 << 32) | t1) >> 20) & 0x3ffffff;
90 state->h3 += ((((uint64_t)t3 << 32) | t2) >> 14) & 0x3ffffff;
91 state->h4 += (t3 >> 8) | (1 << 24);
92
93 poly1305_donna_mul:
94 t[0] = mul32x32_64(state->h0, state->r0) + mul32x32_64(state->h1, state->s4) +
95 mul32x32_64(state->h2, state->s3) + mul32x32_64(state->h3, state->s2) +
96 mul32x32_64(state->h4, state->s1);
97 t[1] = mul32x32_64(state->h0, state->r1) + mul32x32_64(state->h1, state->r0) +
98 mul32x32_64(state->h2, state->s4) + mul32x32_64(state->h3, state->s3) +
99 mul32x32_64(state->h4, state->s2);
100 t[2] = mul32x32_64(state->h0, state->r2) + mul32x32_64(state->h1, state->r1) +
101 mul32x32_64(state->h2, state->r0) + mul32x32_64(state->h3, state->s4) +
102 mul32x32_64(state->h4, state->s3);
103 t[3] = mul32x32_64(state->h0, state->r3) + mul32x32_64(state->h1, state->r2) +
104 mul32x32_64(state->h2, state->r1) + mul32x32_64(state->h3, state->r0) +
105 mul32x32_64(state->h4, state->s4);
106 t[4] = mul32x32_64(state->h0, state->r4) + mul32x32_64(state->h1, state->r3) +
107 mul32x32_64(state->h2, state->r2) + mul32x32_64(state->h3, state->r1) +
108 mul32x32_64(state->h4, state->r0);
109
110 state->h0 = (uint32_t)t[0] & 0x3ffffff;
111 c = (t[0] >> 26);
112 t[1] += c;
113 state->h1 = (uint32_t)t[1] & 0x3ffffff;
114 b = (uint32_t)(t[1] >> 26);
115 t[2] += b;
116 state->h2 = (uint32_t)t[2] & 0x3ffffff;
117 b = (uint32_t)(t[2] >> 26);
118 t[3] += b;
119 state->h3 = (uint32_t)t[3] & 0x3ffffff;
120 b = (uint32_t)(t[3] >> 26);
121 t[4] += b;
122 state->h4 = (uint32_t)t[4] & 0x3ffffff;
123 b = (uint32_t)(t[4] >> 26);
124 state->h0 += b * 5;
125
126 if (len >= 16) {
127 goto poly1305_donna_16bytes;
128 }
129
130 // final bytes
131 poly1305_donna_atmost15bytes:
132 if (!len) {
133 return;
134 }
135
136 for (j = 0; j < len; j++) {
137 mp[j] = in[j];
138 }
139 mp[j++] = 1;
140 for (; j < 16; j++) {
141 mp[j] = 0;
142 }
143 len = 0;
144
145 t0 = U8TO32_LE(mp + 0);
146 t1 = U8TO32_LE(mp + 4);
147 t2 = U8TO32_LE(mp + 8);
148 t3 = U8TO32_LE(mp + 12);
149
150 state->h0 += t0 & 0x3ffffff;
151 state->h1 += ((((uint64_t)t1 << 32) | t0) >> 26) & 0x3ffffff;
152 state->h2 += ((((uint64_t)t2 << 32) | t1) >> 20) & 0x3ffffff;
153 state->h3 += ((((uint64_t)t3 << 32) | t2) >> 14) & 0x3ffffff;
154 state->h4 += (t3 >> 8);
155
156 goto poly1305_donna_mul;
157 }
158
CRYPTO_poly1305_init(poly1305_state * statep,const uint8_t key[32])159 void CRYPTO_poly1305_init(poly1305_state *statep, const uint8_t key[32]) {
160 struct poly1305_state_st *state = poly1305_aligned_state(statep);
161 uint32_t t0, t1, t2, t3;
162
163 #if defined(OPENSSL_POLY1305_NEON)
164 if (CRYPTO_is_NEON_capable()) {
165 CRYPTO_poly1305_init_neon(statep, key);
166 return;
167 }
168 #endif
169
170 t0 = U8TO32_LE(key + 0);
171 t1 = U8TO32_LE(key + 4);
172 t2 = U8TO32_LE(key + 8);
173 t3 = U8TO32_LE(key + 12);
174
175 // precompute multipliers
176 state->r0 = t0 & 0x3ffffff;
177 t0 >>= 26;
178 t0 |= t1 << 6;
179 state->r1 = t0 & 0x3ffff03;
180 t1 >>= 20;
181 t1 |= t2 << 12;
182 state->r2 = t1 & 0x3ffc0ff;
183 t2 >>= 14;
184 t2 |= t3 << 18;
185 state->r3 = t2 & 0x3f03fff;
186 t3 >>= 8;
187 state->r4 = t3 & 0x00fffff;
188
189 state->s1 = state->r1 * 5;
190 state->s2 = state->r2 * 5;
191 state->s3 = state->r3 * 5;
192 state->s4 = state->r4 * 5;
193
194 // init state
195 state->h0 = 0;
196 state->h1 = 0;
197 state->h2 = 0;
198 state->h3 = 0;
199 state->h4 = 0;
200
201 state->buf_used = 0;
202 OPENSSL_memcpy(state->key, key + 16, sizeof(state->key));
203 }
204
CRYPTO_poly1305_update(poly1305_state * statep,const uint8_t * in,size_t in_len)205 void CRYPTO_poly1305_update(poly1305_state *statep, const uint8_t *in,
206 size_t in_len) {
207 struct poly1305_state_st *state = poly1305_aligned_state(statep);
208
209 #if defined(OPENSSL_POLY1305_NEON)
210 if (CRYPTO_is_NEON_capable()) {
211 CRYPTO_poly1305_update_neon(statep, in, in_len);
212 return;
213 }
214 #endif
215
216 if (state->buf_used) {
217 size_t todo = 16 - state->buf_used;
218 if (todo > in_len) {
219 todo = in_len;
220 }
221 for (size_t i = 0; i < todo; i++) {
222 state->buf[state->buf_used + i] = in[i];
223 }
224 state->buf_used += todo;
225 in_len -= todo;
226 in += todo;
227
228 if (state->buf_used == 16) {
229 poly1305_update(state, state->buf, 16);
230 state->buf_used = 0;
231 }
232 }
233
234 if (in_len >= 16) {
235 size_t todo = in_len & ~0xf;
236 poly1305_update(state, in, todo);
237 in += todo;
238 in_len &= 0xf;
239 }
240
241 if (in_len) {
242 for (size_t i = 0; i < in_len; i++) {
243 state->buf[i] = in[i];
244 }
245 state->buf_used = in_len;
246 }
247 }
248
CRYPTO_poly1305_finish(poly1305_state * statep,uint8_t mac[16])249 void CRYPTO_poly1305_finish(poly1305_state *statep, uint8_t mac[16]) {
250 struct poly1305_state_st *state = poly1305_aligned_state(statep);
251 uint64_t f0, f1, f2, f3;
252 uint32_t g0, g1, g2, g3, g4;
253 uint32_t b, nb;
254
255 #if defined(OPENSSL_POLY1305_NEON)
256 if (CRYPTO_is_NEON_capable()) {
257 CRYPTO_poly1305_finish_neon(statep, mac);
258 return;
259 }
260 #endif
261
262 if (state->buf_used) {
263 poly1305_update(state, state->buf, state->buf_used);
264 }
265
266 b = state->h0 >> 26;
267 state->h0 = state->h0 & 0x3ffffff;
268 state->h1 += b;
269 b = state->h1 >> 26;
270 state->h1 = state->h1 & 0x3ffffff;
271 state->h2 += b;
272 b = state->h2 >> 26;
273 state->h2 = state->h2 & 0x3ffffff;
274 state->h3 += b;
275 b = state->h3 >> 26;
276 state->h3 = state->h3 & 0x3ffffff;
277 state->h4 += b;
278 b = state->h4 >> 26;
279 state->h4 = state->h4 & 0x3ffffff;
280 state->h0 += b * 5;
281
282 g0 = state->h0 + 5;
283 b = g0 >> 26;
284 g0 &= 0x3ffffff;
285 g1 = state->h1 + b;
286 b = g1 >> 26;
287 g1 &= 0x3ffffff;
288 g2 = state->h2 + b;
289 b = g2 >> 26;
290 g2 &= 0x3ffffff;
291 g3 = state->h3 + b;
292 b = g3 >> 26;
293 g3 &= 0x3ffffff;
294 g4 = state->h4 + b - (1 << 26);
295
296 b = (g4 >> 31) - 1;
297 nb = ~b;
298 state->h0 = (state->h0 & nb) | (g0 & b);
299 state->h1 = (state->h1 & nb) | (g1 & b);
300 state->h2 = (state->h2 & nb) | (g2 & b);
301 state->h3 = (state->h3 & nb) | (g3 & b);
302 state->h4 = (state->h4 & nb) | (g4 & b);
303
304 f0 = ((state->h0) | (state->h1 << 26)) + (uint64_t)U8TO32_LE(&state->key[0]);
305 f1 = ((state->h1 >> 6) | (state->h2 << 20)) +
306 (uint64_t)U8TO32_LE(&state->key[4]);
307 f2 = ((state->h2 >> 12) | (state->h3 << 14)) +
308 (uint64_t)U8TO32_LE(&state->key[8]);
309 f3 = ((state->h3 >> 18) | (state->h4 << 8)) +
310 (uint64_t)U8TO32_LE(&state->key[12]);
311
312 U32TO8_LE(&mac[0], f0);
313 f1 += (f0 >> 32);
314 U32TO8_LE(&mac[4], f1);
315 f2 += (f1 >> 32);
316 U32TO8_LE(&mac[8], f2);
317 f3 += (f2 >> 32);
318 U32TO8_LE(&mac[12], f3);
319 }
320
321 #endif // !BORINGSSL_HAS_UINT128 || !OPENSSL_X86_64
322