Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(278)

Side by Side Diff: nss/lib/freebl/poly1305.c

Issue 2078763002: Delete bundled copy of NSS and replace with README. (Closed) Base URL: https://chromium.googlesource.com/chromium/deps/nss@master
Patch Set: Delete bundled copy of NSS and replace with README. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « nss/lib/freebl/poly1305.h ('k') | nss/lib/freebl/poly1305-donna-x64-sse2-incremental-source.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 /* This implementation of poly1305 is by Andrew Moon
6 * (https://github.com/floodyberry/poly1305-donna) and released as public
7 * domain. */
8
9 #include <string.h>
10
11 #include "poly1305.h"
12
13 #if defined(_MSC_VER) && _MSC_VER < 1600
14 #include "prtypes.h"
15 typedef PRUint32 uint32_t;
16 typedef PRUint64 uint64_t;
17 #else
18 #include <stdint.h>
19 #endif
20
21 #if defined(NSS_X86) || defined(NSS_X64)
22 /* We can assume little-endian. */
23 static uint32_t U8TO32_LE(const unsigned char *m) {
24 uint32_t r;
25 memcpy(&r, m, sizeof(r));
26 return r;
27 }
28
29 static void U32TO8_LE(unsigned char *m, uint32_t v) {
30 memcpy(m, &v, sizeof(v));
31 }
32 #else
33 static uint32_t U8TO32_LE(const unsigned char *m) {
34 return (uint32_t)m[0] |
35 (uint32_t)m[1] << 8 |
36 (uint32_t)m[2] << 16 |
37 (uint32_t)m[3] << 24;
38 }
39
40 static void U32TO8_LE(unsigned char *m, uint32_t v) {
41 m[0] = v;
42 m[1] = v >> 8;
43 m[2] = v >> 16;
44 m[3] = v >> 24;
45 }
46 #endif
47
48 static uint64_t
49 mul32x32_64(uint32_t a, uint32_t b) {
50 return (uint64_t)a * b;
51 }
52
53 struct poly1305_state_st {
54 uint32_t r0,r1,r2,r3,r4;
55 uint32_t s1,s2,s3,s4;
56 uint32_t h0,h1,h2,h3,h4;
57 unsigned char buf[16];
58 unsigned int buf_used;
59 unsigned char key[16];
60 };
61
62 /* update updates |state| given some amount of input data. This function may
63 * only be called with a |len| that is not a multiple of 16 at the end of the
64 * data. Otherwise the input must be buffered into 16 byte blocks. */
65 static void update(struct poly1305_state_st *state, const unsigned char *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 unsigned char mp[16];
73
74 if (len < 16)
75 goto poly1305_donna_atmost15bytes;
76
77 poly1305_donna_16bytes:
78 t0 = U8TO32_LE(in);
79 t1 = U8TO32_LE(in+4);
80 t2 = U8TO32_LE(in+8);
81 t3 = U8TO32_LE(in+12);
82
83 in += 16;
84 len -= 16;
85
86 state->h0 += t0 & 0x3ffffff;
87 state->h1 += ((((uint64_t)t1 << 32) | t0) >> 26) & 0x3ffffff;
88 state->h2 += ((((uint64_t)t2 << 32) | t1) >> 20) & 0x3ffffff;
89 state->h3 += ((((uint64_t)t3 << 32) | t2) >> 14) & 0x3ffffff;
90 state->h4 += (t3 >> 8) | (1 << 24);
91
92 poly1305_donna_mul:
93 t[0] = mul32x32_64(state->h0,state->r0) +
94 mul32x32_64(state->h1,state->s4) +
95 mul32x32_64(state->h2,state->s3) +
96 mul32x32_64(state->h3,state->s2) +
97 mul32x32_64(state->h4,state->s1);
98 t[1] = mul32x32_64(state->h0,state->r1) +
99 mul32x32_64(state->h1,state->r0) +
100 mul32x32_64(state->h2,state->s4) +
101 mul32x32_64(state->h3,state->s3) +
102 mul32x32_64(state->h4,state->s2);
103 t[2] = mul32x32_64(state->h0,state->r2) +
104 mul32x32_64(state->h1,state->r1) +
105 mul32x32_64(state->h2,state->r0) +
106 mul32x32_64(state->h3,state->s4) +
107 mul32x32_64(state->h4,state->s3);
108 t[3] = mul32x32_64(state->h0,state->r3) +
109 mul32x32_64(state->h1,state->r2) +
110 mul32x32_64(state->h2,state->r1) +
111 mul32x32_64(state->h3,state->r0) +
112 mul32x32_64(state->h4,state->s4);
113 t[4] = mul32x32_64(state->h0,state->r4) +
114 mul32x32_64(state->h1,state->r3) +
115 mul32x32_64(state->h2,state->r2) +
116 mul32x32_64(state->h3,state->r1) +
117 mul32x32_64(state->h4,state->r0);
118
119 state->h0 = (uint32_t)t[0] & 0x3ffffff; c = (t[0] > > 26);
120 t[1] += c; state->h1 = (uint32_t)t[1] & 0x3ffffff; b = (uint32_t)(t[1] > > 26);
121 t[2] += b; state->h2 = (uint32_t)t[2] & 0x3ffffff; b = (uint32_t)(t[2] > > 26);
122 t[3] += b; state->h3 = (uint32_t)t[3] & 0x3ffffff; b = (uint32_t)(t[3] > > 26);
123 t[4] += b; state->h4 = (uint32_t)t[4] & 0x3ffffff; b = (uint32_t)(t[4] > > 26);
124 state->h0 += b * 5;
125
126 if (len >= 16)
127 goto poly1305_donna_16bytes;
128
129 /* final bytes */
130 poly1305_donna_atmost15bytes:
131 if (!len)
132 return;
133
134 for (j = 0; j < len; j++)
135 mp[j] = in[j];
136 mp[j++] = 1;
137 for (; j < 16; j++)
138 mp[j] = 0;
139 len = 0;
140
141 t0 = U8TO32_LE(mp+0);
142 t1 = U8TO32_LE(mp+4);
143 t2 = U8TO32_LE(mp+8);
144 t3 = U8TO32_LE(mp+12);
145
146 state->h0 += t0 & 0x3ffffff;
147 state->h1 += ((((uint64_t)t1 << 32) | t0) >> 26) & 0x3ffffff;
148 state->h2 += ((((uint64_t)t2 << 32) | t1) >> 20) & 0x3ffffff;
149 state->h3 += ((((uint64_t)t3 << 32) | t2) >> 14) & 0x3ffffff;
150 state->h4 += (t3 >> 8);
151
152 goto poly1305_donna_mul;
153 }
154
155 void Poly1305Init(poly1305_state *statep, const unsigned char key[32]) {
156 struct poly1305_state_st *state = (struct poly1305_state_st*) statep;
157 uint32_t t0,t1,t2,t3;
158
159 t0 = U8TO32_LE(key+0);
160 t1 = U8TO32_LE(key+4);
161 t2 = U8TO32_LE(key+8);
162 t3 = U8TO32_LE(key+12);
163
164 /* precompute multipliers */
165 state->r0 = t0 & 0x3ffffff; t0 >>= 26; t0 |= t1 << 6;
166 state->r1 = t0 & 0x3ffff03; t1 >>= 20; t1 |= t2 << 12;
167 state->r2 = t1 & 0x3ffc0ff; t2 >>= 14; t2 |= t3 << 18;
168 state->r3 = t2 & 0x3f03fff; t3 >>= 8;
169 state->r4 = t3 & 0x00fffff;
170
171 state->s1 = state->r1 * 5;
172 state->s2 = state->r2 * 5;
173 state->s3 = state->r3 * 5;
174 state->s4 = state->r4 * 5;
175
176 /* init state */
177 state->h0 = 0;
178 state->h1 = 0;
179 state->h2 = 0;
180 state->h3 = 0;
181 state->h4 = 0;
182
183 state->buf_used = 0;
184 memcpy(state->key, key + 16, sizeof(state->key));
185 }
186
187 void Poly1305Update(poly1305_state *statep, const unsigned char *in,
188 size_t in_len) {
189 unsigned int i;
190 struct poly1305_state_st *state = (struct poly1305_state_st*) statep;
191
192 if (state->buf_used) {
193 unsigned int todo = 16 - state->buf_used;
194 if (todo > in_len)
195 todo = in_len;
196 for (i = 0; i < todo; i++)
197 state->buf[state->buf_used + i] = in[i];
198 state->buf_used += todo;
199 in_len -= todo;
200 in += todo;
201
202 if (state->buf_used == 16) {
203 update(state, state->buf, 16);
204 state->buf_used = 0;
205 }
206 }
207
208 if (in_len >= 16) {
209 size_t todo = in_len & ~0xf;
210 update(state, in, todo);
211 in += todo;
212 in_len &= 0xf;
213 }
214
215 if (in_len) {
216 for (i = 0; i < in_len; i++)
217 state->buf[i] = in[i];
218 state->buf_used = in_len;
219 }
220 }
221
222 void Poly1305Finish(poly1305_state *statep, unsigned char mac[16]) {
223 struct poly1305_state_st *state = (struct poly1305_state_st*) statep;
224 uint64_t f0,f1,f2,f3;
225 uint32_t g0,g1,g2,g3,g4;
226 uint32_t b, nb;
227
228 if (state->buf_used)
229 update(state, state->buf, state->buf_used);
230
231 b = state->h0 >> 26; state->h0 = state->h0 & 0x3ffff ff;
232 state->h1 += b; b = state->h1 >> 26; state->h1 = state->h1 & 0x3ffff ff;
233 state->h2 += b; b = state->h2 >> 26; state->h2 = state->h2 & 0x3ffff ff;
234 state->h3 += b; b = state->h3 >> 26; state->h3 = state->h3 & 0x3ffff ff;
235 state->h4 += b; b = state->h4 >> 26; state->h4 = state->h4 & 0x3ffff ff;
236 state->h0 += b * 5;
237
238 g0 = state->h0 + 5; b = g0 >> 26; g0 &= 0x3ffffff;
239 g1 = state->h1 + b; b = g1 >> 26; g1 &= 0x3ffffff;
240 g2 = state->h2 + b; b = g2 >> 26; g2 &= 0x3ffffff;
241 g3 = state->h3 + b; b = g3 >> 26; g3 &= 0x3ffffff;
242 g4 = state->h4 + b - (1 << 26);
243
244 b = (g4 >> 31) - 1;
245 nb = ~b;
246 state->h0 = (state->h0 & nb) | (g0 & b);
247 state->h1 = (state->h1 & nb) | (g1 & b);
248 state->h2 = (state->h2 & nb) | (g2 & b);
249 state->h3 = (state->h3 & nb) | (g3 & b);
250 state->h4 = (state->h4 & nb) | (g4 & b);
251
252 f0 = ((state->h0 ) | (state->h1 << 26)) + (uint64_t)U8TO32_LE(&stat e->key[0]);
253 f1 = ((state->h1 >> 6) | (state->h2 << 20)) + (uint64_t)U8TO32_LE(&stat e->key[4]);
254 f2 = ((state->h2 >> 12) | (state->h3 << 14)) + (uint64_t)U8TO32_LE(&stat e->key[8]);
255 f3 = ((state->h3 >> 18) | (state->h4 << 8)) + (uint64_t)U8TO32_LE(&stat e->key[12]);
256
257 U32TO8_LE(&mac[ 0], (uint32_t)f0); f1 += (f0 >> 32);
258 U32TO8_LE(&mac[ 4], (uint32_t)f1); f2 += (f1 >> 32);
259 U32TO8_LE(&mac[ 8], (uint32_t)f2); f3 += (f2 >> 32);
260 U32TO8_LE(&mac[12], (uint32_t)f3);
261 }
OLDNEW
« no previous file with comments | « nss/lib/freebl/poly1305.h ('k') | nss/lib/freebl/poly1305-donna-x64-sse2-incremental-source.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698