Debian Codegraph @

external/boringssl/crypto/poly1305/poly1305.c

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