Mercurial > trustbridge > nss-cmake-static
comparison nss/lib/freebl/poly1305/poly1305.c @ 0:1e5118fa0cb1
This is NSS with a Cmake Buildsyste
To compile a static NSS library for Windows we've used the
Chromium-NSS fork and added a Cmake buildsystem to compile
it statically for Windows. See README.chromium for chromium
changes and README.trustbridge for our modifications.
author | Andre Heinecke <andre.heinecke@intevation.de> |
---|---|
date | Mon, 28 Jul 2014 10:47:06 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:1e5118fa0cb1 |
---|---|
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 #include <stdint.h> | |
11 | |
12 #include "poly1305.h" | |
13 | |
14 #if defined(NSS_X86) || defined(NSS_X64) | |
15 /* We can assume little-endian. */ | |
16 static uint32_t U8TO32_LE(const unsigned char *m) { | |
17 uint32_t r; | |
18 memcpy(&r, m, sizeof(r)); | |
19 return r; | |
20 } | |
21 | |
22 static void U32TO8_LE(unsigned char *m, uint32_t v) { | |
23 memcpy(m, &v, sizeof(v)); | |
24 } | |
25 #else | |
26 static uint32_t U8TO32_LE(const unsigned char *m) { | |
27 return (uint32_t)m[0] | | |
28 (uint32_t)m[1] << 8 | | |
29 (uint32_t)m[2] << 16 | | |
30 (uint32_t)m[3] << 24; | |
31 } | |
32 | |
33 static void U32TO8_LE(unsigned char *m, uint32_t v) { | |
34 m[0] = v; | |
35 m[1] = v >> 8; | |
36 m[2] = v >> 16; | |
37 m[3] = v >> 24; | |
38 } | |
39 #endif | |
40 | |
41 static uint64_t | |
42 mul32x32_64(uint32_t a, uint32_t b) { | |
43 return (uint64_t)a * b; | |
44 } | |
45 | |
46 struct poly1305_state_st { | |
47 uint32_t r0,r1,r2,r3,r4; | |
48 uint32_t s1,s2,s3,s4; | |
49 uint32_t h0,h1,h2,h3,h4; | |
50 unsigned char buf[16]; | |
51 unsigned int buf_used; | |
52 unsigned char key[16]; | |
53 }; | |
54 | |
55 /* update updates |state| given some amount of input data. This function may | |
56 * only be called with a |len| that is not a multiple of 16 at the end of the | |
57 * data. Otherwise the input must be buffered into 16 byte blocks. */ | |
58 static void update(struct poly1305_state_st *state, const unsigned char *in, | |
59 size_t len) { | |
60 uint32_t t0,t1,t2,t3; | |
61 uint64_t t[5]; | |
62 uint32_t b; | |
63 uint64_t c; | |
64 size_t j; | |
65 unsigned char mp[16]; | |
66 | |
67 if (len < 16) | |
68 goto poly1305_donna_atmost15bytes; | |
69 | |
70 poly1305_donna_16bytes: | |
71 t0 = U8TO32_LE(in); | |
72 t1 = U8TO32_LE(in+4); | |
73 t2 = U8TO32_LE(in+8); | |
74 t3 = U8TO32_LE(in+12); | |
75 | |
76 in += 16; | |
77 len -= 16; | |
78 | |
79 state->h0 += t0 & 0x3ffffff; | |
80 state->h1 += ((((uint64_t)t1 << 32) | t0) >> 26) & 0x3ffffff; | |
81 state->h2 += ((((uint64_t)t2 << 32) | t1) >> 20) & 0x3ffffff; | |
82 state->h3 += ((((uint64_t)t3 << 32) | t2) >> 14) & 0x3ffffff; | |
83 state->h4 += (t3 >> 8) | (1 << 24); | |
84 | |
85 poly1305_donna_mul: | |
86 t[0] = mul32x32_64(state->h0,state->r0) + | |
87 mul32x32_64(state->h1,state->s4) + | |
88 mul32x32_64(state->h2,state->s3) + | |
89 mul32x32_64(state->h3,state->s2) + | |
90 mul32x32_64(state->h4,state->s1); | |
91 t[1] = mul32x32_64(state->h0,state->r1) + | |
92 mul32x32_64(state->h1,state->r0) + | |
93 mul32x32_64(state->h2,state->s4) + | |
94 mul32x32_64(state->h3,state->s3) + | |
95 mul32x32_64(state->h4,state->s2); | |
96 t[2] = mul32x32_64(state->h0,state->r2) + | |
97 mul32x32_64(state->h1,state->r1) + | |
98 mul32x32_64(state->h2,state->r0) + | |
99 mul32x32_64(state->h3,state->s4) + | |
100 mul32x32_64(state->h4,state->s3); | |
101 t[3] = mul32x32_64(state->h0,state->r3) + | |
102 mul32x32_64(state->h1,state->r2) + | |
103 mul32x32_64(state->h2,state->r1) + | |
104 mul32x32_64(state->h3,state->r0) + | |
105 mul32x32_64(state->h4,state->s4); | |
106 t[4] = mul32x32_64(state->h0,state->r4) + | |
107 mul32x32_64(state->h1,state->r3) + | |
108 mul32x32_64(state->h2,state->r2) + | |
109 mul32x32_64(state->h3,state->r1) + | |
110 mul32x32_64(state->h4,state->r0); | |
111 | |
112 state->h0 = (uint32_t)t[0] & 0x3ffffff; c = (t[0] >> 26); | |
113 t[1] += c; state->h1 = (uint32_t)t[1] & 0x3ffffff; b = (uint32_t)(t[1] >> 26); | |
114 t[2] += b; state->h2 = (uint32_t)t[2] & 0x3ffffff; b = (uint32_t)(t[2] >> 26); | |
115 t[3] += b; state->h3 = (uint32_t)t[3] & 0x3ffffff; b = (uint32_t)(t[3] >> 26); | |
116 t[4] += b; state->h4 = (uint32_t)t[4] & 0x3ffffff; b = (uint32_t)(t[4] >> 26); | |
117 state->h0 += b * 5; | |
118 | |
119 if (len >= 16) | |
120 goto poly1305_donna_16bytes; | |
121 | |
122 /* final bytes */ | |
123 poly1305_donna_atmost15bytes: | |
124 if (!len) | |
125 return; | |
126 | |
127 for (j = 0; j < len; j++) | |
128 mp[j] = in[j]; | |
129 mp[j++] = 1; | |
130 for (; j < 16; j++) | |
131 mp[j] = 0; | |
132 len = 0; | |
133 | |
134 t0 = U8TO32_LE(mp+0); | |
135 t1 = U8TO32_LE(mp+4); | |
136 t2 = U8TO32_LE(mp+8); | |
137 t3 = U8TO32_LE(mp+12); | |
138 | |
139 state->h0 += t0 & 0x3ffffff; | |
140 state->h1 += ((((uint64_t)t1 << 32) | t0) >> 26) & 0x3ffffff; | |
141 state->h2 += ((((uint64_t)t2 << 32) | t1) >> 20) & 0x3ffffff; | |
142 state->h3 += ((((uint64_t)t3 << 32) | t2) >> 14) & 0x3ffffff; | |
143 state->h4 += (t3 >> 8); | |
144 | |
145 goto poly1305_donna_mul; | |
146 } | |
147 | |
148 void Poly1305Init(poly1305_state *statep, const unsigned char key[32]) { | |
149 struct poly1305_state_st *state = (struct poly1305_state_st*) statep; | |
150 uint32_t t0,t1,t2,t3; | |
151 | |
152 t0 = U8TO32_LE(key+0); | |
153 t1 = U8TO32_LE(key+4); | |
154 t2 = U8TO32_LE(key+8); | |
155 t3 = U8TO32_LE(key+12); | |
156 | |
157 /* precompute multipliers */ | |
158 state->r0 = t0 & 0x3ffffff; t0 >>= 26; t0 |= t1 << 6; | |
159 state->r1 = t0 & 0x3ffff03; t1 >>= 20; t1 |= t2 << 12; | |
160 state->r2 = t1 & 0x3ffc0ff; t2 >>= 14; t2 |= t3 << 18; | |
161 state->r3 = t2 & 0x3f03fff; t3 >>= 8; | |
162 state->r4 = t3 & 0x00fffff; | |
163 | |
164 state->s1 = state->r1 * 5; | |
165 state->s2 = state->r2 * 5; | |
166 state->s3 = state->r3 * 5; | |
167 state->s4 = state->r4 * 5; | |
168 | |
169 /* init state */ | |
170 state->h0 = 0; | |
171 state->h1 = 0; | |
172 state->h2 = 0; | |
173 state->h3 = 0; | |
174 state->h4 = 0; | |
175 | |
176 state->buf_used = 0; | |
177 memcpy(state->key, key + 16, sizeof(state->key)); | |
178 } | |
179 | |
180 void Poly1305Update(poly1305_state *statep, const unsigned char *in, | |
181 size_t in_len) { | |
182 unsigned int i; | |
183 struct poly1305_state_st *state = (struct poly1305_state_st*) statep; | |
184 | |
185 if (state->buf_used) { | |
186 unsigned int todo = 16 - state->buf_used; | |
187 if (todo > in_len) | |
188 todo = in_len; | |
189 for (i = 0; i < todo; i++) | |
190 state->buf[state->buf_used + i] = in[i]; | |
191 state->buf_used += todo; | |
192 in_len -= todo; | |
193 in += todo; | |
194 | |
195 if (state->buf_used == 16) { | |
196 update(state, state->buf, 16); | |
197 state->buf_used = 0; | |
198 } | |
199 } | |
200 | |
201 if (in_len >= 16) { | |
202 size_t todo = in_len & ~0xf; | |
203 update(state, in, todo); | |
204 in += todo; | |
205 in_len &= 0xf; | |
206 } | |
207 | |
208 if (in_len) { | |
209 for (i = 0; i < in_len; i++) | |
210 state->buf[i] = in[i]; | |
211 state->buf_used = in_len; | |
212 } | |
213 } | |
214 | |
215 void Poly1305Finish(poly1305_state *statep, unsigned char mac[16]) { | |
216 struct poly1305_state_st *state = (struct poly1305_state_st*) statep; | |
217 uint64_t f0,f1,f2,f3; | |
218 uint32_t g0,g1,g2,g3,g4; | |
219 uint32_t b, nb; | |
220 | |
221 if (state->buf_used) | |
222 update(state, state->buf, state->buf_used); | |
223 | |
224 b = state->h0 >> 26; state->h0 = state->h0 & 0x3ffffff; | |
225 state->h1 += b; b = state->h1 >> 26; state->h1 = state->h1 & 0x3ffffff; | |
226 state->h2 += b; b = state->h2 >> 26; state->h2 = state->h2 & 0x3ffffff; | |
227 state->h3 += b; b = state->h3 >> 26; state->h3 = state->h3 & 0x3ffffff; | |
228 state->h4 += b; b = state->h4 >> 26; state->h4 = state->h4 & 0x3ffffff; | |
229 state->h0 += b * 5; | |
230 | |
231 g0 = state->h0 + 5; b = g0 >> 26; g0 &= 0x3ffffff; | |
232 g1 = state->h1 + b; b = g1 >> 26; g1 &= 0x3ffffff; | |
233 g2 = state->h2 + b; b = g2 >> 26; g2 &= 0x3ffffff; | |
234 g3 = state->h3 + b; b = g3 >> 26; g3 &= 0x3ffffff; | |
235 g4 = state->h4 + b - (1 << 26); | |
236 | |
237 b = (g4 >> 31) - 1; | |
238 nb = ~b; | |
239 state->h0 = (state->h0 & nb) | (g0 & b); | |
240 state->h1 = (state->h1 & nb) | (g1 & b); | |
241 state->h2 = (state->h2 & nb) | (g2 & b); | |
242 state->h3 = (state->h3 & nb) | (g3 & b); | |
243 state->h4 = (state->h4 & nb) | (g4 & b); | |
244 | |
245 f0 = ((state->h0 ) | (state->h1 << 26)) + (uint64_t)U8TO32_LE(&state->key[0]); | |
246 f1 = ((state->h1 >> 6) | (state->h2 << 20)) + (uint64_t)U8TO32_LE(&state->key[4]); | |
247 f2 = ((state->h2 >> 12) | (state->h3 << 14)) + (uint64_t)U8TO32_LE(&state->key[8]); | |
248 f3 = ((state->h3 >> 18) | (state->h4 << 8)) + (uint64_t)U8TO32_LE(&state->key[12]); | |
249 | |
250 U32TO8_LE(&mac[ 0], f0); f1 += (f0 >> 32); | |
251 U32TO8_LE(&mac[ 4], f1); f2 += (f1 >> 32); | |
252 U32TO8_LE(&mac[ 8], f2); f3 += (f2 >> 32); | |
253 U32TO8_LE(&mac[12], f3); | |
254 } |