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