poly1305.c
Go to the documentation of this file.
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.
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 
38 static void U32TO8_LE(uint8_t *m, uint32_t v) {
39  OPENSSL_memcpy(m, &v, sizeof(v));
40 }
41 
42 static uint64_t mul32x32_64(uint32_t a, uint32_t b) { return (uint64_t)a * b; }
43 
48  uint8_t buf[16];
49  size_t buf_used;
50  uint8_t key[16];
51 };
52 
54  sizeof(struct poly1305_state_st) + 63 <= sizeof(poly1305_state),
55  "poly1305_state isn't large enough to hold aligned poly1305_state_st");
56 
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.
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 
159 void CRYPTO_poly1305_init(poly1305_state *statep, const uint8_t key[32]) {
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 
206  size_t in_len) {
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;
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 
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
poly1305_state_st::s1
uint32_t s1
Definition: poly1305.c:46
poly1305_update
static void poly1305_update(struct poly1305_state_st *state, const uint8_t *in, size_t len)
Definition: poly1305.c:65
poly1305_state_st::r3
uint32_t r3
Definition: poly1305.c:45
xds_manager.f1
f1
Definition: xds_manager.py:42
internal.h
CRYPTO_poly1305_finish
void CRYPTO_poly1305_finish(poly1305_state *statep, uint8_t mac[16])
Definition: poly1305.c:249
string.h
poly1305_state_st::s3
uint32_t s3
Definition: poly1305.c:46
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
CRYPTO_poly1305_update
void CRYPTO_poly1305_update(poly1305_state *statep, const uint8_t *in, size_t in_len)
Definition: poly1305.c:205
align_pointer
static void * align_pointer(void *ptr, size_t alignment)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:230
poly1305_state_st::h3
uint32_t h3
Definition: poly1305.c:47
poly1305_state_st::h1
uint32_t h1
Definition: poly1305.c:47
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
poly1305_state_st
Definition: poly1305.c:44
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
poly1305_aligned_state
static struct poly1305_state_st * poly1305_aligned_state(poly1305_state *state)
Definition: poly1305.c:57
poly1305_state_st::r4
uint32_t r4
Definition: poly1305.c:45
mul32x32_64
static uint64_t mul32x32_64(uint32_t a, uint32_t b)
Definition: poly1305.c:42
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
U32TO8_LE
static void U32TO8_LE(uint8_t *m, uint32_t v)
Definition: poly1305.c:38
in
const char * in
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:391
poly1305_state_st::r0
uint32_t r0
Definition: poly1305.c:45
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
poly1305_state_st::h0
uint32_t h0
Definition: poly1305.c:47
poly1305.h
t0
static int64_t t0
Definition: bloaty/third_party/re2/util/benchmark.cc:44
U8TO32_LE
static uint32_t U8TO32_LE(const uint8_t *m)
Definition: poly1305.c:32
setup.v
v
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
OPENSSL_memcpy
static void * OPENSSL_memcpy(void *dst, const void *src, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:819
poly1305_state_st::r1
uint32_t r1
Definition: poly1305.c:45
poly1305_state_st::buf_used
size_t buf_used
Definition: poly1305.c:49
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
poly1305_state_st::r2
uint32_t r2
Definition: poly1305.c:45
poly1305_state_st::key
uint8_t key[16]
Definition: poly1305.c:50
key
const char * key
Definition: hpack_parser_table.cc:164
poly1305_state_st::h4
uint32_t h4
Definition: poly1305.c:47
fix_build_deps.r
r
Definition: fix_build_deps.py:491
CRYPTO_poly1305_init
void CRYPTO_poly1305_init(poly1305_state *statep, const uint8_t key[32])
Definition: poly1305.c:159
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
cpu.h
std::tr1::f3
const T1 const T2 const T3 & f3
Definition: cares/cares/test/gmock-1.8.0/gtest/gtest.h:1638
poly1305_state
uint8_t poly1305_state[512]
Definition: poly1305.h:25
xds_manager.f2
f2
Definition: xds_manager.py:85
poly1305_state_st::s4
uint32_t s4
Definition: poly1305.c:46
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
OPENSSL_STATIC_ASSERT
OPENSSL_STATIC_ASSERT(sizeof(struct poly1305_state_st)+63<=sizeof(poly1305_state), "poly1305_state isn't large enough to hold aligned poly1305_state_st")
regress.m
m
Definition: regress/regress.py:25
t1
Table t1
Definition: abseil-cpp/absl/container/internal/raw_hash_set_allocator_test.cc:185
mkowners.todo
todo
Definition: mkowners.py:209
poly1305_state_st::h2
uint32_t h2
Definition: poly1305.c:47
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
poly1305_state_st::s2
uint32_t s2
Definition: poly1305.c:46


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:44