Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/poly1305/poly1305.c

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