diff options
author | jsing <> | 2014-05-14 14:46:35 +0000 |
---|---|---|
committer | jsing <> | 2014-05-14 14:46:35 +0000 |
commit | d80a509f5bc6ebb6406df372d83531863fa297e8 (patch) | |
tree | 2438466190276223517edaaed7b9d5a6c90b9c72 /src/lib | |
parent | 5dc223660bd1bba54e5ec9930856977e8a32fd40 (diff) | |
download | openbsd-d80a509f5bc6ebb6406df372d83531863fa297e8.tar.gz openbsd-d80a509f5bc6ebb6406df372d83531863fa297e8.tar.bz2 openbsd-d80a509f5bc6ebb6406df372d83531863fa297e8.zip |
Add poly1305 to libcrypto utilising Andrew Moon's public domain
implementation.
ok miod@
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/libcrypto/crypto/Makefile | 7 | ||||
-rw-r--r-- | src/lib/libcrypto/poly1305/poly1305-donna.c | 263 | ||||
-rw-r--r-- | src/lib/libcrypto/poly1305/poly1305.c | 37 | ||||
-rw-r--r-- | src/lib/libcrypto/poly1305/poly1305.h | 48 | ||||
-rw-r--r-- | src/lib/libssl/src/crypto/poly1305/poly1305-donna.c | 263 | ||||
-rw-r--r-- | src/lib/libssl/src/crypto/poly1305/poly1305.c | 37 | ||||
-rw-r--r-- | src/lib/libssl/src/crypto/poly1305/poly1305.h | 48 |
7 files changed, 702 insertions, 1 deletions
diff --git a/src/lib/libcrypto/crypto/Makefile b/src/lib/libcrypto/crypto/Makefile index ce04ac9e61..6d9eac1383 100644 --- a/src/lib/libcrypto/crypto/Makefile +++ b/src/lib/libcrypto/crypto/Makefile | |||
@@ -1,4 +1,4 @@ | |||
1 | # $OpenBSD: Makefile,v 1.32 2014/05/12 19:14:14 miod Exp $ | 1 | # $OpenBSD: Makefile,v 1.33 2014/05/14 14:46:35 jsing Exp $ |
2 | 2 | ||
3 | LIB= crypto | 3 | LIB= crypto |
4 | 4 | ||
@@ -198,6 +198,9 @@ SRCS+= p12_utl.c p12_npas.c pk12err.c p12_p8d.c p12_p8e.c | |||
198 | SRCS+= pk7_asn1.c pk7_lib.c pkcs7err.c pk7_doit.c pk7_smime.c pk7_attr.c | 198 | SRCS+= pk7_asn1.c pk7_lib.c pkcs7err.c pk7_doit.c pk7_smime.c pk7_attr.c |
199 | SRCS+= pk7_mime.c bio_pk7.c | 199 | SRCS+= pk7_mime.c bio_pk7.c |
200 | 200 | ||
201 | # poly1305/ | ||
202 | SRCS+= poly1305.c | ||
203 | |||
201 | # rand/ | 204 | # rand/ |
202 | SRCS+= randfile.c rand_lib.c rand_err.c | 205 | SRCS+= randfile.c rand_lib.c rand_err.c |
203 | 206 | ||
@@ -301,6 +304,7 @@ SRCS+= v3_asid.c v3_addr.c | |||
301 | ${LCRYPTO_SRC}/perlasm \ | 304 | ${LCRYPTO_SRC}/perlasm \ |
302 | ${LCRYPTO_SRC}/pkcs12 \ | 305 | ${LCRYPTO_SRC}/pkcs12 \ |
303 | ${LCRYPTO_SRC}/pkcs7 \ | 306 | ${LCRYPTO_SRC}/pkcs7 \ |
307 | ${LCRYPTO_SRC}/poly1305 \ | ||
304 | ${LCRYPTO_SRC}/rand \ | 308 | ${LCRYPTO_SRC}/rand \ |
305 | ${LCRYPTO_SRC}/rc2 \ | 309 | ${LCRYPTO_SRC}/rc2 \ |
306 | ${LCRYPTO_SRC}/rc4 \ | 310 | ${LCRYPTO_SRC}/rc4 \ |
@@ -363,6 +367,7 @@ HDRS=\ | |||
363 | crypto/pem/pem2.h \ | 367 | crypto/pem/pem2.h \ |
364 | crypto/pkcs12/pkcs12.h \ | 368 | crypto/pkcs12/pkcs12.h \ |
365 | crypto/pkcs7/pkcs7.h \ | 369 | crypto/pkcs7/pkcs7.h \ |
370 | crypto/poly1305/poly1305.h \ | ||
366 | crypto/rand/rand.h \ | 371 | crypto/rand/rand.h \ |
367 | crypto/rc2/rc2.h \ | 372 | crypto/rc2/rc2.h \ |
368 | crypto/rc4/rc4.h \ | 373 | crypto/rc4/rc4.h \ |
diff --git a/src/lib/libcrypto/poly1305/poly1305-donna.c b/src/lib/libcrypto/poly1305/poly1305-donna.c new file mode 100644 index 0000000000..642a30b376 --- /dev/null +++ b/src/lib/libcrypto/poly1305/poly1305-donna.c | |||
@@ -0,0 +1,263 @@ | |||
1 | /* | ||
2 | * Public Domain poly1305 from Andrew Moon | ||
3 | * Based on poly1305-donna.c, poly1305-donna-32.h and poly1305-donna.h from: | ||
4 | * https://github.com/floodyberry/poly1305-donna | ||
5 | */ | ||
6 | |||
7 | #include <stddef.h> | ||
8 | |||
9 | static inline void poly1305_init(poly1305_context *ctx, | ||
10 | const unsigned char key[32]); | ||
11 | static inline void poly1305_update(poly1305_context *ctx, | ||
12 | const unsigned char *m, size_t bytes); | ||
13 | static inline void poly1305_finish(poly1305_context *ctx, | ||
14 | unsigned char mac[16]); | ||
15 | |||
16 | /* | ||
17 | * poly1305 implementation using 32 bit * 32 bit = 64 bit multiplication | ||
18 | * and 64 bit addition. | ||
19 | */ | ||
20 | |||
21 | #define poly1305_block_size 16 | ||
22 | |||
23 | /* 17 + sizeof(size_t) + 14*sizeof(unsigned long) */ | ||
24 | typedef struct poly1305_state_internal_t { | ||
25 | unsigned long r[5]; | ||
26 | unsigned long h[5]; | ||
27 | unsigned long pad[4]; | ||
28 | size_t leftover; | ||
29 | unsigned char buffer[poly1305_block_size]; | ||
30 | unsigned char final; | ||
31 | } poly1305_state_internal_t; | ||
32 | |||
33 | /* interpret four 8 bit unsigned integers as a 32 bit unsigned integer in little endian */ | ||
34 | static unsigned long | ||
35 | U8TO32(const unsigned char *p) { | ||
36 | return | ||
37 | (((unsigned long)(p[0] & 0xff) ) | | ||
38 | ((unsigned long)(p[1] & 0xff) << 8) | | ||
39 | ((unsigned long)(p[2] & 0xff) << 16) | | ||
40 | ((unsigned long)(p[3] & 0xff) << 24)); | ||
41 | } | ||
42 | |||
43 | /* store a 32 bit unsigned integer as four 8 bit unsigned integers in little endian */ | ||
44 | static void | ||
45 | U32TO8(unsigned char *p, unsigned long v) { | ||
46 | p[0] = (v ) & 0xff; | ||
47 | p[1] = (v >> 8) & 0xff; | ||
48 | p[2] = (v >> 16) & 0xff; | ||
49 | p[3] = (v >> 24) & 0xff; | ||
50 | } | ||
51 | |||
52 | static inline void | ||
53 | poly1305_init(poly1305_context *ctx, const unsigned char key[32]) { | ||
54 | poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx; | ||
55 | |||
56 | /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */ | ||
57 | st->r[0] = (U8TO32(&key[ 0]) ) & 0x3ffffff; | ||
58 | st->r[1] = (U8TO32(&key[ 3]) >> 2) & 0x3ffff03; | ||
59 | st->r[2] = (U8TO32(&key[ 6]) >> 4) & 0x3ffc0ff; | ||
60 | st->r[3] = (U8TO32(&key[ 9]) >> 6) & 0x3f03fff; | ||
61 | st->r[4] = (U8TO32(&key[12]) >> 8) & 0x00fffff; | ||
62 | |||
63 | /* h = 0 */ | ||
64 | st->h[0] = 0; | ||
65 | st->h[1] = 0; | ||
66 | st->h[2] = 0; | ||
67 | st->h[3] = 0; | ||
68 | st->h[4] = 0; | ||
69 | |||
70 | /* save pad for later */ | ||
71 | st->pad[0] = U8TO32(&key[16]); | ||
72 | st->pad[1] = U8TO32(&key[20]); | ||
73 | st->pad[2] = U8TO32(&key[24]); | ||
74 | st->pad[3] = U8TO32(&key[28]); | ||
75 | |||
76 | st->leftover = 0; | ||
77 | st->final = 0; | ||
78 | } | ||
79 | |||
80 | static void | ||
81 | poly1305_blocks(poly1305_state_internal_t *st, const unsigned char *m, size_t bytes) { | ||
82 | const unsigned long hibit = (st->final) ? 0 : (1 << 24); /* 1 << 128 */ | ||
83 | unsigned long r0,r1,r2,r3,r4; | ||
84 | unsigned long s1,s2,s3,s4; | ||
85 | unsigned long h0,h1,h2,h3,h4; | ||
86 | unsigned long long d0,d1,d2,d3,d4; | ||
87 | unsigned long c; | ||
88 | |||
89 | r0 = st->r[0]; | ||
90 | r1 = st->r[1]; | ||
91 | r2 = st->r[2]; | ||
92 | r3 = st->r[3]; | ||
93 | r4 = st->r[4]; | ||
94 | |||
95 | s1 = r1 * 5; | ||
96 | s2 = r2 * 5; | ||
97 | s3 = r3 * 5; | ||
98 | s4 = r4 * 5; | ||
99 | |||
100 | h0 = st->h[0]; | ||
101 | h1 = st->h[1]; | ||
102 | h2 = st->h[2]; | ||
103 | h3 = st->h[3]; | ||
104 | h4 = st->h[4]; | ||
105 | |||
106 | while (bytes >= poly1305_block_size) { | ||
107 | /* h += m[i] */ | ||
108 | h0 += (U8TO32(m+ 0) ) & 0x3ffffff; | ||
109 | h1 += (U8TO32(m+ 3) >> 2) & 0x3ffffff; | ||
110 | h2 += (U8TO32(m+ 6) >> 4) & 0x3ffffff; | ||
111 | h3 += (U8TO32(m+ 9) >> 6) & 0x3ffffff; | ||
112 | h4 += (U8TO32(m+12) >> 8) | hibit; | ||
113 | |||
114 | /* h *= r */ | ||
115 | d0 = ((unsigned long long)h0 * r0) + ((unsigned long long)h1 * s4) + ((unsigned long long)h2 * s3) + ((unsigned long long)h3 * s2) + ((unsigned long long)h4 * s1); | ||
116 | d1 = ((unsigned long long)h0 * r1) + ((unsigned long long)h1 * r0) + ((unsigned long long)h2 * s4) + ((unsigned long long)h3 * s3) + ((unsigned long long)h4 * s2); | ||
117 | d2 = ((unsigned long long)h0 * r2) + ((unsigned long long)h1 * r1) + ((unsigned long long)h2 * r0) + ((unsigned long long)h3 * s4) + ((unsigned long long)h4 * s3); | ||
118 | d3 = ((unsigned long long)h0 * r3) + ((unsigned long long)h1 * r2) + ((unsigned long long)h2 * r1) + ((unsigned long long)h3 * r0) + ((unsigned long long)h4 * s4); | ||
119 | d4 = ((unsigned long long)h0 * r4) + ((unsigned long long)h1 * r3) + ((unsigned long long)h2 * r2) + ((unsigned long long)h3 * r1) + ((unsigned long long)h4 * r0); | ||
120 | |||
121 | /* (partial) h %= p */ | ||
122 | c = (unsigned long)(d0 >> 26); h0 = (unsigned long)d0 & 0x3ffffff; | ||
123 | d1 += c; c = (unsigned long)(d1 >> 26); h1 = (unsigned long)d1 & 0x3ffffff; | ||
124 | d2 += c; c = (unsigned long)(d2 >> 26); h2 = (unsigned long)d2 & 0x3ffffff; | ||
125 | d3 += c; c = (unsigned long)(d3 >> 26); h3 = (unsigned long)d3 & 0x3ffffff; | ||
126 | d4 += c; c = (unsigned long)(d4 >> 26); h4 = (unsigned long)d4 & 0x3ffffff; | ||
127 | h0 += c * 5; c = (h0 >> 26); h0 = h0 & 0x3ffffff; | ||
128 | h1 += c; | ||
129 | |||
130 | m += poly1305_block_size; | ||
131 | bytes -= poly1305_block_size; | ||
132 | } | ||
133 | |||
134 | st->h[0] = h0; | ||
135 | st->h[1] = h1; | ||
136 | st->h[2] = h2; | ||
137 | st->h[3] = h3; | ||
138 | st->h[4] = h4; | ||
139 | } | ||
140 | |||
141 | static inline void | ||
142 | poly1305_update(poly1305_context *ctx, const unsigned char *m, size_t bytes) { | ||
143 | poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx; | ||
144 | size_t i; | ||
145 | |||
146 | /* handle leftover */ | ||
147 | if (st->leftover) { | ||
148 | size_t want = (poly1305_block_size - st->leftover); | ||
149 | if (want > bytes) | ||
150 | want = bytes; | ||
151 | for (i = 0; i < want; i++) | ||
152 | st->buffer[st->leftover + i] = m[i]; | ||
153 | bytes -= want; | ||
154 | m += want; | ||
155 | st->leftover += want; | ||
156 | if (st->leftover < poly1305_block_size) | ||
157 | return; | ||
158 | poly1305_blocks(st, st->buffer, poly1305_block_size); | ||
159 | st->leftover = 0; | ||
160 | } | ||
161 | |||
162 | /* process full blocks */ | ||
163 | if (bytes >= poly1305_block_size) { | ||
164 | size_t want = (bytes & ~(poly1305_block_size - 1)); | ||
165 | poly1305_blocks(st, m, want); | ||
166 | m += want; | ||
167 | bytes -= want; | ||
168 | } | ||
169 | |||
170 | /* store leftover */ | ||
171 | if (bytes) { | ||
172 | for (i = 0; i < bytes; i++) | ||
173 | st->buffer[st->leftover + i] = m[i]; | ||
174 | st->leftover += bytes; | ||
175 | } | ||
176 | } | ||
177 | |||
178 | static inline void | ||
179 | poly1305_finish(poly1305_context *ctx, unsigned char mac[16]) { | ||
180 | poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx; | ||
181 | unsigned long h0,h1,h2,h3,h4,c; | ||
182 | unsigned long g0,g1,g2,g3,g4; | ||
183 | unsigned long long f; | ||
184 | unsigned long mask; | ||
185 | |||
186 | /* process the remaining block */ | ||
187 | if (st->leftover) { | ||
188 | size_t i = st->leftover; | ||
189 | st->buffer[i++] = 1; | ||
190 | for (; i < poly1305_block_size; i++) | ||
191 | st->buffer[i] = 0; | ||
192 | st->final = 1; | ||
193 | poly1305_blocks(st, st->buffer, poly1305_block_size); | ||
194 | } | ||
195 | |||
196 | /* fully carry h */ | ||
197 | h0 = st->h[0]; | ||
198 | h1 = st->h[1]; | ||
199 | h2 = st->h[2]; | ||
200 | h3 = st->h[3]; | ||
201 | h4 = st->h[4]; | ||
202 | |||
203 | c = h1 >> 26; h1 = h1 & 0x3ffffff; | ||
204 | h2 += c; c = h2 >> 26; h2 = h2 & 0x3ffffff; | ||
205 | h3 += c; c = h3 >> 26; h3 = h3 & 0x3ffffff; | ||
206 | h4 += c; c = h4 >> 26; h4 = h4 & 0x3ffffff; | ||
207 | h0 += c * 5; c = h0 >> 26; h0 = h0 & 0x3ffffff; | ||
208 | h1 += c; | ||
209 | |||
210 | /* compute h + -p */ | ||
211 | g0 = h0 + 5; c = g0 >> 26; g0 &= 0x3ffffff; | ||
212 | g1 = h1 + c; c = g1 >> 26; g1 &= 0x3ffffff; | ||
213 | g2 = h2 + c; c = g2 >> 26; g2 &= 0x3ffffff; | ||
214 | g3 = h3 + c; c = g3 >> 26; g3 &= 0x3ffffff; | ||
215 | g4 = h4 + c - (1 << 26); | ||
216 | |||
217 | /* select h if h < p, or h + -p if h >= p */ | ||
218 | mask = (g4 >> ((sizeof(unsigned long) * 8) - 1)) - 1; | ||
219 | g0 &= mask; | ||
220 | g1 &= mask; | ||
221 | g2 &= mask; | ||
222 | g3 &= mask; | ||
223 | g4 &= mask; | ||
224 | mask = ~mask; | ||
225 | h0 = (h0 & mask) | g0; | ||
226 | h1 = (h1 & mask) | g1; | ||
227 | h2 = (h2 & mask) | g2; | ||
228 | h3 = (h3 & mask) | g3; | ||
229 | h4 = (h4 & mask) | g4; | ||
230 | |||
231 | /* h = h % (2^128) */ | ||
232 | h0 = ((h0 ) | (h1 << 26)) & 0xffffffff; | ||
233 | h1 = ((h1 >> 6) | (h2 << 20)) & 0xffffffff; | ||
234 | h2 = ((h2 >> 12) | (h3 << 14)) & 0xffffffff; | ||
235 | h3 = ((h3 >> 18) | (h4 << 8)) & 0xffffffff; | ||
236 | |||
237 | /* mac = (h + pad) % (2^128) */ | ||
238 | f = (unsigned long long)h0 + st->pad[0] ; h0 = (unsigned long)f; | ||
239 | f = (unsigned long long)h1 + st->pad[1] + (f >> 32); h1 = (unsigned long)f; | ||
240 | f = (unsigned long long)h2 + st->pad[2] + (f >> 32); h2 = (unsigned long)f; | ||
241 | f = (unsigned long long)h3 + st->pad[3] + (f >> 32); h3 = (unsigned long)f; | ||
242 | |||
243 | U32TO8(mac + 0, h0); | ||
244 | U32TO8(mac + 4, h1); | ||
245 | U32TO8(mac + 8, h2); | ||
246 | U32TO8(mac + 12, h3); | ||
247 | |||
248 | /* zero out the state */ | ||
249 | st->h[0] = 0; | ||
250 | st->h[1] = 0; | ||
251 | st->h[2] = 0; | ||
252 | st->h[3] = 0; | ||
253 | st->h[4] = 0; | ||
254 | st->r[0] = 0; | ||
255 | st->r[1] = 0; | ||
256 | st->r[2] = 0; | ||
257 | st->r[3] = 0; | ||
258 | st->r[4] = 0; | ||
259 | st->pad[0] = 0; | ||
260 | st->pad[1] = 0; | ||
261 | st->pad[2] = 0; | ||
262 | st->pad[3] = 0; | ||
263 | } | ||
diff --git a/src/lib/libcrypto/poly1305/poly1305.c b/src/lib/libcrypto/poly1305/poly1305.c new file mode 100644 index 0000000000..96083fe7e9 --- /dev/null +++ b/src/lib/libcrypto/poly1305/poly1305.c | |||
@@ -0,0 +1,37 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> | ||
3 | * | ||
4 | * Permission to use, copy, modify, and distribute this software for any | ||
5 | * purpose with or without fee is hereby granted, provided that the above | ||
6 | * copyright notice and this permission notice appear in all copies. | ||
7 | * | ||
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
15 | */ | ||
16 | |||
17 | #include "poly1305.h" | ||
18 | #include "poly1305-donna.c" | ||
19 | |||
20 | void | ||
21 | CRYPTO_poly1305_init(poly1305_context *ctx, const unsigned char key[32]) | ||
22 | { | ||
23 | poly1305_init(ctx, key); | ||
24 | } | ||
25 | |||
26 | void | ||
27 | CRYPTO_poly1305_update(poly1305_context *ctx, const unsigned char *in, | ||
28 | size_t len) | ||
29 | { | ||
30 | poly1305_update(ctx, in, len); | ||
31 | } | ||
32 | |||
33 | void | ||
34 | CRYPTO_poly1305_finish(poly1305_context *ctx, unsigned char mac[16]) | ||
35 | { | ||
36 | poly1305_finish(ctx, mac); | ||
37 | } | ||
diff --git a/src/lib/libcrypto/poly1305/poly1305.h b/src/lib/libcrypto/poly1305/poly1305.h new file mode 100644 index 0000000000..a4ccdd5d80 --- /dev/null +++ b/src/lib/libcrypto/poly1305/poly1305.h | |||
@@ -0,0 +1,48 @@ | |||
1 | /* | ||
2 | * Copyright (c) Joel Sing <jsing@openbsd.org> | ||
3 | * | ||
4 | * Permission to use, copy, modify, and distribute this software for any | ||
5 | * purpose with or without fee is hereby granted, provided that the above | ||
6 | * copyright notice and this permission notice appear in all copies. | ||
7 | * | ||
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
15 | */ | ||
16 | |||
17 | #ifndef HEADER_POLY1305_H | ||
18 | #define HEADER_POLY1305_H | ||
19 | |||
20 | #include <openssl/opensslconf.h> | ||
21 | |||
22 | #if defined(OPENSSL_NO_POLY1305) | ||
23 | #error Poly1305 is disabled. | ||
24 | #endif | ||
25 | |||
26 | #include <stddef.h> | ||
27 | |||
28 | #ifdef __cplusplus | ||
29 | extern "C" { | ||
30 | #endif | ||
31 | |||
32 | typedef struct poly1305_context { | ||
33 | size_t aligner; | ||
34 | unsigned char opaque[136]; | ||
35 | } poly1305_context; | ||
36 | |||
37 | typedef struct poly1305_context poly1305_state; | ||
38 | |||
39 | void CRYPTO_poly1305_init(poly1305_context *ctx, const unsigned char key[32]); | ||
40 | void CRYPTO_poly1305_update(poly1305_context *ctx, const unsigned char *in, | ||
41 | size_t len); | ||
42 | void CRYPTO_poly1305_finish(poly1305_context *ctx, unsigned char mac[16]); | ||
43 | |||
44 | #ifdef __cplusplus | ||
45 | } | ||
46 | #endif | ||
47 | |||
48 | #endif /* HEADER_POLY1305_H */ | ||
diff --git a/src/lib/libssl/src/crypto/poly1305/poly1305-donna.c b/src/lib/libssl/src/crypto/poly1305/poly1305-donna.c new file mode 100644 index 0000000000..642a30b376 --- /dev/null +++ b/src/lib/libssl/src/crypto/poly1305/poly1305-donna.c | |||
@@ -0,0 +1,263 @@ | |||
1 | /* | ||
2 | * Public Domain poly1305 from Andrew Moon | ||
3 | * Based on poly1305-donna.c, poly1305-donna-32.h and poly1305-donna.h from: | ||
4 | * https://github.com/floodyberry/poly1305-donna | ||
5 | */ | ||
6 | |||
7 | #include <stddef.h> | ||
8 | |||
9 | static inline void poly1305_init(poly1305_context *ctx, | ||
10 | const unsigned char key[32]); | ||
11 | static inline void poly1305_update(poly1305_context *ctx, | ||
12 | const unsigned char *m, size_t bytes); | ||
13 | static inline void poly1305_finish(poly1305_context *ctx, | ||
14 | unsigned char mac[16]); | ||
15 | |||
16 | /* | ||
17 | * poly1305 implementation using 32 bit * 32 bit = 64 bit multiplication | ||
18 | * and 64 bit addition. | ||
19 | */ | ||
20 | |||
21 | #define poly1305_block_size 16 | ||
22 | |||
23 | /* 17 + sizeof(size_t) + 14*sizeof(unsigned long) */ | ||
24 | typedef struct poly1305_state_internal_t { | ||
25 | unsigned long r[5]; | ||
26 | unsigned long h[5]; | ||
27 | unsigned long pad[4]; | ||
28 | size_t leftover; | ||
29 | unsigned char buffer[poly1305_block_size]; | ||
30 | unsigned char final; | ||
31 | } poly1305_state_internal_t; | ||
32 | |||
33 | /* interpret four 8 bit unsigned integers as a 32 bit unsigned integer in little endian */ | ||
34 | static unsigned long | ||
35 | U8TO32(const unsigned char *p) { | ||
36 | return | ||
37 | (((unsigned long)(p[0] & 0xff) ) | | ||
38 | ((unsigned long)(p[1] & 0xff) << 8) | | ||
39 | ((unsigned long)(p[2] & 0xff) << 16) | | ||
40 | ((unsigned long)(p[3] & 0xff) << 24)); | ||
41 | } | ||
42 | |||
43 | /* store a 32 bit unsigned integer as four 8 bit unsigned integers in little endian */ | ||
44 | static void | ||
45 | U32TO8(unsigned char *p, unsigned long v) { | ||
46 | p[0] = (v ) & 0xff; | ||
47 | p[1] = (v >> 8) & 0xff; | ||
48 | p[2] = (v >> 16) & 0xff; | ||
49 | p[3] = (v >> 24) & 0xff; | ||
50 | } | ||
51 | |||
52 | static inline void | ||
53 | poly1305_init(poly1305_context *ctx, const unsigned char key[32]) { | ||
54 | poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx; | ||
55 | |||
56 | /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */ | ||
57 | st->r[0] = (U8TO32(&key[ 0]) ) & 0x3ffffff; | ||
58 | st->r[1] = (U8TO32(&key[ 3]) >> 2) & 0x3ffff03; | ||
59 | st->r[2] = (U8TO32(&key[ 6]) >> 4) & 0x3ffc0ff; | ||
60 | st->r[3] = (U8TO32(&key[ 9]) >> 6) & 0x3f03fff; | ||
61 | st->r[4] = (U8TO32(&key[12]) >> 8) & 0x00fffff; | ||
62 | |||
63 | /* h = 0 */ | ||
64 | st->h[0] = 0; | ||
65 | st->h[1] = 0; | ||
66 | st->h[2] = 0; | ||
67 | st->h[3] = 0; | ||
68 | st->h[4] = 0; | ||
69 | |||
70 | /* save pad for later */ | ||
71 | st->pad[0] = U8TO32(&key[16]); | ||
72 | st->pad[1] = U8TO32(&key[20]); | ||
73 | st->pad[2] = U8TO32(&key[24]); | ||
74 | st->pad[3] = U8TO32(&key[28]); | ||
75 | |||
76 | st->leftover = 0; | ||
77 | st->final = 0; | ||
78 | } | ||
79 | |||
80 | static void | ||
81 | poly1305_blocks(poly1305_state_internal_t *st, const unsigned char *m, size_t bytes) { | ||
82 | const unsigned long hibit = (st->final) ? 0 : (1 << 24); /* 1 << 128 */ | ||
83 | unsigned long r0,r1,r2,r3,r4; | ||
84 | unsigned long s1,s2,s3,s4; | ||
85 | unsigned long h0,h1,h2,h3,h4; | ||
86 | unsigned long long d0,d1,d2,d3,d4; | ||
87 | unsigned long c; | ||
88 | |||
89 | r0 = st->r[0]; | ||
90 | r1 = st->r[1]; | ||
91 | r2 = st->r[2]; | ||
92 | r3 = st->r[3]; | ||
93 | r4 = st->r[4]; | ||
94 | |||
95 | s1 = r1 * 5; | ||
96 | s2 = r2 * 5; | ||
97 | s3 = r3 * 5; | ||
98 | s4 = r4 * 5; | ||
99 | |||
100 | h0 = st->h[0]; | ||
101 | h1 = st->h[1]; | ||
102 | h2 = st->h[2]; | ||
103 | h3 = st->h[3]; | ||
104 | h4 = st->h[4]; | ||
105 | |||
106 | while (bytes >= poly1305_block_size) { | ||
107 | /* h += m[i] */ | ||
108 | h0 += (U8TO32(m+ 0) ) & 0x3ffffff; | ||
109 | h1 += (U8TO32(m+ 3) >> 2) & 0x3ffffff; | ||
110 | h2 += (U8TO32(m+ 6) >> 4) & 0x3ffffff; | ||
111 | h3 += (U8TO32(m+ 9) >> 6) & 0x3ffffff; | ||
112 | h4 += (U8TO32(m+12) >> 8) | hibit; | ||
113 | |||
114 | /* h *= r */ | ||
115 | d0 = ((unsigned long long)h0 * r0) + ((unsigned long long)h1 * s4) + ((unsigned long long)h2 * s3) + ((unsigned long long)h3 * s2) + ((unsigned long long)h4 * s1); | ||
116 | d1 = ((unsigned long long)h0 * r1) + ((unsigned long long)h1 * r0) + ((unsigned long long)h2 * s4) + ((unsigned long long)h3 * s3) + ((unsigned long long)h4 * s2); | ||
117 | d2 = ((unsigned long long)h0 * r2) + ((unsigned long long)h1 * r1) + ((unsigned long long)h2 * r0) + ((unsigned long long)h3 * s4) + ((unsigned long long)h4 * s3); | ||
118 | d3 = ((unsigned long long)h0 * r3) + ((unsigned long long)h1 * r2) + ((unsigned long long)h2 * r1) + ((unsigned long long)h3 * r0) + ((unsigned long long)h4 * s4); | ||
119 | d4 = ((unsigned long long)h0 * r4) + ((unsigned long long)h1 * r3) + ((unsigned long long)h2 * r2) + ((unsigned long long)h3 * r1) + ((unsigned long long)h4 * r0); | ||
120 | |||
121 | /* (partial) h %= p */ | ||
122 | c = (unsigned long)(d0 >> 26); h0 = (unsigned long)d0 & 0x3ffffff; | ||
123 | d1 += c; c = (unsigned long)(d1 >> 26); h1 = (unsigned long)d1 & 0x3ffffff; | ||
124 | d2 += c; c = (unsigned long)(d2 >> 26); h2 = (unsigned long)d2 & 0x3ffffff; | ||
125 | d3 += c; c = (unsigned long)(d3 >> 26); h3 = (unsigned long)d3 & 0x3ffffff; | ||
126 | d4 += c; c = (unsigned long)(d4 >> 26); h4 = (unsigned long)d4 & 0x3ffffff; | ||
127 | h0 += c * 5; c = (h0 >> 26); h0 = h0 & 0x3ffffff; | ||
128 | h1 += c; | ||
129 | |||
130 | m += poly1305_block_size; | ||
131 | bytes -= poly1305_block_size; | ||
132 | } | ||
133 | |||
134 | st->h[0] = h0; | ||
135 | st->h[1] = h1; | ||
136 | st->h[2] = h2; | ||
137 | st->h[3] = h3; | ||
138 | st->h[4] = h4; | ||
139 | } | ||
140 | |||
141 | static inline void | ||
142 | poly1305_update(poly1305_context *ctx, const unsigned char *m, size_t bytes) { | ||
143 | poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx; | ||
144 | size_t i; | ||
145 | |||
146 | /* handle leftover */ | ||
147 | if (st->leftover) { | ||
148 | size_t want = (poly1305_block_size - st->leftover); | ||
149 | if (want > bytes) | ||
150 | want = bytes; | ||
151 | for (i = 0; i < want; i++) | ||
152 | st->buffer[st->leftover + i] = m[i]; | ||
153 | bytes -= want; | ||
154 | m += want; | ||
155 | st->leftover += want; | ||
156 | if (st->leftover < poly1305_block_size) | ||
157 | return; | ||
158 | poly1305_blocks(st, st->buffer, poly1305_block_size); | ||
159 | st->leftover = 0; | ||
160 | } | ||
161 | |||
162 | /* process full blocks */ | ||
163 | if (bytes >= poly1305_block_size) { | ||
164 | size_t want = (bytes & ~(poly1305_block_size - 1)); | ||
165 | poly1305_blocks(st, m, want); | ||
166 | m += want; | ||
167 | bytes -= want; | ||
168 | } | ||
169 | |||
170 | /* store leftover */ | ||
171 | if (bytes) { | ||
172 | for (i = 0; i < bytes; i++) | ||
173 | st->buffer[st->leftover + i] = m[i]; | ||
174 | st->leftover += bytes; | ||
175 | } | ||
176 | } | ||
177 | |||
178 | static inline void | ||
179 | poly1305_finish(poly1305_context *ctx, unsigned char mac[16]) { | ||
180 | poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx; | ||
181 | unsigned long h0,h1,h2,h3,h4,c; | ||
182 | unsigned long g0,g1,g2,g3,g4; | ||
183 | unsigned long long f; | ||
184 | unsigned long mask; | ||
185 | |||
186 | /* process the remaining block */ | ||
187 | if (st->leftover) { | ||
188 | size_t i = st->leftover; | ||
189 | st->buffer[i++] = 1; | ||
190 | for (; i < poly1305_block_size; i++) | ||
191 | st->buffer[i] = 0; | ||
192 | st->final = 1; | ||
193 | poly1305_blocks(st, st->buffer, poly1305_block_size); | ||
194 | } | ||
195 | |||
196 | /* fully carry h */ | ||
197 | h0 = st->h[0]; | ||
198 | h1 = st->h[1]; | ||
199 | h2 = st->h[2]; | ||
200 | h3 = st->h[3]; | ||
201 | h4 = st->h[4]; | ||
202 | |||
203 | c = h1 >> 26; h1 = h1 & 0x3ffffff; | ||
204 | h2 += c; c = h2 >> 26; h2 = h2 & 0x3ffffff; | ||
205 | h3 += c; c = h3 >> 26; h3 = h3 & 0x3ffffff; | ||
206 | h4 += c; c = h4 >> 26; h4 = h4 & 0x3ffffff; | ||
207 | h0 += c * 5; c = h0 >> 26; h0 = h0 & 0x3ffffff; | ||
208 | h1 += c; | ||
209 | |||
210 | /* compute h + -p */ | ||
211 | g0 = h0 + 5; c = g0 >> 26; g0 &= 0x3ffffff; | ||
212 | g1 = h1 + c; c = g1 >> 26; g1 &= 0x3ffffff; | ||
213 | g2 = h2 + c; c = g2 >> 26; g2 &= 0x3ffffff; | ||
214 | g3 = h3 + c; c = g3 >> 26; g3 &= 0x3ffffff; | ||
215 | g4 = h4 + c - (1 << 26); | ||
216 | |||
217 | /* select h if h < p, or h + -p if h >= p */ | ||
218 | mask = (g4 >> ((sizeof(unsigned long) * 8) - 1)) - 1; | ||
219 | g0 &= mask; | ||
220 | g1 &= mask; | ||
221 | g2 &= mask; | ||
222 | g3 &= mask; | ||
223 | g4 &= mask; | ||
224 | mask = ~mask; | ||
225 | h0 = (h0 & mask) | g0; | ||
226 | h1 = (h1 & mask) | g1; | ||
227 | h2 = (h2 & mask) | g2; | ||
228 | h3 = (h3 & mask) | g3; | ||
229 | h4 = (h4 & mask) | g4; | ||
230 | |||
231 | /* h = h % (2^128) */ | ||
232 | h0 = ((h0 ) | (h1 << 26)) & 0xffffffff; | ||
233 | h1 = ((h1 >> 6) | (h2 << 20)) & 0xffffffff; | ||
234 | h2 = ((h2 >> 12) | (h3 << 14)) & 0xffffffff; | ||
235 | h3 = ((h3 >> 18) | (h4 << 8)) & 0xffffffff; | ||
236 | |||
237 | /* mac = (h + pad) % (2^128) */ | ||
238 | f = (unsigned long long)h0 + st->pad[0] ; h0 = (unsigned long)f; | ||
239 | f = (unsigned long long)h1 + st->pad[1] + (f >> 32); h1 = (unsigned long)f; | ||
240 | f = (unsigned long long)h2 + st->pad[2] + (f >> 32); h2 = (unsigned long)f; | ||
241 | f = (unsigned long long)h3 + st->pad[3] + (f >> 32); h3 = (unsigned long)f; | ||
242 | |||
243 | U32TO8(mac + 0, h0); | ||
244 | U32TO8(mac + 4, h1); | ||
245 | U32TO8(mac + 8, h2); | ||
246 | U32TO8(mac + 12, h3); | ||
247 | |||
248 | /* zero out the state */ | ||
249 | st->h[0] = 0; | ||
250 | st->h[1] = 0; | ||
251 | st->h[2] = 0; | ||
252 | st->h[3] = 0; | ||
253 | st->h[4] = 0; | ||
254 | st->r[0] = 0; | ||
255 | st->r[1] = 0; | ||
256 | st->r[2] = 0; | ||
257 | st->r[3] = 0; | ||
258 | st->r[4] = 0; | ||
259 | st->pad[0] = 0; | ||
260 | st->pad[1] = 0; | ||
261 | st->pad[2] = 0; | ||
262 | st->pad[3] = 0; | ||
263 | } | ||
diff --git a/src/lib/libssl/src/crypto/poly1305/poly1305.c b/src/lib/libssl/src/crypto/poly1305/poly1305.c new file mode 100644 index 0000000000..96083fe7e9 --- /dev/null +++ b/src/lib/libssl/src/crypto/poly1305/poly1305.c | |||
@@ -0,0 +1,37 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> | ||
3 | * | ||
4 | * Permission to use, copy, modify, and distribute this software for any | ||
5 | * purpose with or without fee is hereby granted, provided that the above | ||
6 | * copyright notice and this permission notice appear in all copies. | ||
7 | * | ||
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
15 | */ | ||
16 | |||
17 | #include "poly1305.h" | ||
18 | #include "poly1305-donna.c" | ||
19 | |||
20 | void | ||
21 | CRYPTO_poly1305_init(poly1305_context *ctx, const unsigned char key[32]) | ||
22 | { | ||
23 | poly1305_init(ctx, key); | ||
24 | } | ||
25 | |||
26 | void | ||
27 | CRYPTO_poly1305_update(poly1305_context *ctx, const unsigned char *in, | ||
28 | size_t len) | ||
29 | { | ||
30 | poly1305_update(ctx, in, len); | ||
31 | } | ||
32 | |||
33 | void | ||
34 | CRYPTO_poly1305_finish(poly1305_context *ctx, unsigned char mac[16]) | ||
35 | { | ||
36 | poly1305_finish(ctx, mac); | ||
37 | } | ||
diff --git a/src/lib/libssl/src/crypto/poly1305/poly1305.h b/src/lib/libssl/src/crypto/poly1305/poly1305.h new file mode 100644 index 0000000000..a4ccdd5d80 --- /dev/null +++ b/src/lib/libssl/src/crypto/poly1305/poly1305.h | |||
@@ -0,0 +1,48 @@ | |||
1 | /* | ||
2 | * Copyright (c) Joel Sing <jsing@openbsd.org> | ||
3 | * | ||
4 | * Permission to use, copy, modify, and distribute this software for any | ||
5 | * purpose with or without fee is hereby granted, provided that the above | ||
6 | * copyright notice and this permission notice appear in all copies. | ||
7 | * | ||
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
15 | */ | ||
16 | |||
17 | #ifndef HEADER_POLY1305_H | ||
18 | #define HEADER_POLY1305_H | ||
19 | |||
20 | #include <openssl/opensslconf.h> | ||
21 | |||
22 | #if defined(OPENSSL_NO_POLY1305) | ||
23 | #error Poly1305 is disabled. | ||
24 | #endif | ||
25 | |||
26 | #include <stddef.h> | ||
27 | |||
28 | #ifdef __cplusplus | ||
29 | extern "C" { | ||
30 | #endif | ||
31 | |||
32 | typedef struct poly1305_context { | ||
33 | size_t aligner; | ||
34 | unsigned char opaque[136]; | ||
35 | } poly1305_context; | ||
36 | |||
37 | typedef struct poly1305_context poly1305_state; | ||
38 | |||
39 | void CRYPTO_poly1305_init(poly1305_context *ctx, const unsigned char key[32]); | ||
40 | void CRYPTO_poly1305_update(poly1305_context *ctx, const unsigned char *in, | ||
41 | size_t len); | ||
42 | void CRYPTO_poly1305_finish(poly1305_context *ctx, unsigned char mac[16]); | ||
43 | |||
44 | #ifdef __cplusplus | ||
45 | } | ||
46 | #endif | ||
47 | |||
48 | #endif /* HEADER_POLY1305_H */ | ||