summaryrefslogtreecommitdiff
path: root/dht/cbits
diff options
context:
space:
mode:
authorJames Crayne <jim.crayne@gmail.com>2019-09-28 13:43:29 -0400
committerJoe Crayne <joe@jerkface.net>2020-01-01 19:27:53 -0500
commit11987749fc6e6d3e53ea737d46d5ab13a16faeb8 (patch)
tree5716463275c2d3e902889db619908ded2a73971c /dht/cbits
parentadd2c76bced51fde5e9917e7449ef52be70faf87 (diff)
Factor out some new libraries
word64-map: Data.Word64Map network-addr: Network.Address tox-crypto: Crypto.Tox lifted-concurrent: Control.Concurrent.Lifted.Instrument Control.Concurrent.Async.Lifted.Instrument psq-wrap: Data.Wrapper.PSQInt Data.Wrapper.PSQ minmax-psq: Data.MinMaxPSQ tasks: Control.Concurrent.Tasks kad: Network.Kademlia Network.Kademlia.Bootstrap Network.Kademlia.Routing Network.Kademlia.CommonAPI Network.Kademlia.Persistence Network.Kademlia.Search
Diffstat (limited to 'dht/cbits')
-rw-r--r--dht/cbits/cryptonite_bitfn.h253
-rw-r--r--dht/cbits/cryptonite_salsa.c297
-rw-r--r--dht/cbits/cryptonite_salsa.h57
-rw-r--r--dht/cbits/cryptonite_xsalsa.c80
-rw-r--r--dht/cbits/cryptonite_xsalsa.h37
5 files changed, 724 insertions, 0 deletions
diff --git a/dht/cbits/cryptonite_bitfn.h b/dht/cbits/cryptonite_bitfn.h
new file mode 100644
index 00000000..3a00dd8a
--- /dev/null
+++ b/dht/cbits/cryptonite_bitfn.h
@@ -0,0 +1,253 @@
1/*
2 * Copyright (C) 2006-2014 Vincent Hanquez <vincent@snarc.org>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#ifndef BITFN_H
26#define BITFN_H
27#include <stdint.h>
28
29#ifndef NO_INLINE_ASM
30/**********************************************************/
31# if (defined(__i386__))
32# define ARCH_HAS_SWAP32
33static inline uint32_t bitfn_swap32(uint32_t a)
34{
35 asm ("bswap %0" : "=r" (a) : "0" (a));
36 return a;
37}
38/**********************************************************/
39# elif (defined(__arm__))
40# define ARCH_HAS_SWAP32
41static inline uint32_t bitfn_swap32(uint32_t a)
42{
43 uint32_t tmp = a;
44 asm volatile ("eor %1, %0, %0, ror #16\n"
45 "bic %1, %1, #0xff0000\n"
46 "mov %0, %0, ror #8\n"
47 "eor %0, %0, %1, lsr #8\n"
48 : "=r" (a), "=r" (tmp) : "0" (a), "1" (tmp));
49 return a;
50}
51/**********************************************************/
52# elif defined(__x86_64__)
53# define ARCH_HAS_SWAP32
54# define ARCH_HAS_SWAP64
55static inline uint32_t bitfn_swap32(uint32_t a)
56{
57 asm ("bswap %0" : "=r" (a) : "0" (a));
58 return a;
59}
60
61static inline uint64_t bitfn_swap64(uint64_t a)
62{
63 asm ("bswap %0" : "=r" (a) : "0" (a));
64 return a;
65}
66
67# endif
68#endif /* NO_INLINE_ASM */
69/**********************************************************/
70
71#ifndef ARCH_HAS_ROL32
72static inline uint32_t rol32(uint32_t word, uint32_t shift)
73{
74 return (word << shift) | (word >> (32 - shift));
75}
76#endif
77
78#ifndef ARCH_HAS_ROR32
79static inline uint32_t ror32(uint32_t word, uint32_t shift)
80{
81 return (word >> shift) | (word << (32 - shift));
82}
83#endif
84
85#ifndef ARCH_HAS_ROL64
86static inline uint64_t rol64(uint64_t word, uint32_t shift)
87{
88 return (word << shift) | (word >> (64 - shift));
89}
90#endif
91
92#ifndef ARCH_HAS_ROR64
93static inline uint64_t ror64(uint64_t word, uint32_t shift)
94{
95 return (word >> shift) | (word << (64 - shift));
96}
97#endif
98
99#ifndef ARCH_HAS_SWAP32
100static inline uint32_t bitfn_swap32(uint32_t a)
101{
102 return (a << 24) | ((a & 0xff00) << 8) | ((a >> 8) & 0xff00) | (a >> 24);
103}
104#endif
105
106#ifndef ARCH_HAS_ARRAY_SWAP32
107static inline void array_swap32(uint32_t *d, uint32_t *s, uint32_t nb)
108{
109 while (nb--)
110 *d++ = bitfn_swap32(*s++);
111}
112#endif
113
114#ifndef ARCH_HAS_SWAP64
115static inline uint64_t bitfn_swap64(uint64_t a)
116{
117 return ((uint64_t) bitfn_swap32((uint32_t) (a >> 32))) |
118 (((uint64_t) bitfn_swap32((uint32_t) a)) << 32);
119}
120#endif
121
122#ifndef ARCH_HAS_ARRAY_SWAP64
123static inline void array_swap64(uint64_t *d, uint64_t *s, uint32_t nb)
124{
125 while (nb--)
126 *d++ = bitfn_swap64(*s++);
127}
128#endif
129
130#ifndef ARCH_HAS_MEMORY_ZERO
131static inline void memory_zero(void *ptr, uint32_t len)
132{
133 uint32_t *ptr32 = ptr;
134 uint8_t *ptr8;
135 int i;
136
137 for (i = 0; i < len / 4; i++)
138 *ptr32++ = 0;
139 if (len % 4) {
140 ptr8 = (uint8_t *) ptr32;
141 for (i = len % 4; i >= 0; i--)
142 ptr8[i] = 0;
143 }
144}
145#endif
146
147#ifndef ARCH_HAS_ARRAY_COPY32
148static inline void array_copy32(uint32_t *d, uint32_t *s, uint32_t nb)
149{
150 while (nb--) *d++ = *s++;
151}
152#endif
153
154#ifndef ARCH_HAS_ARRAY_XOR32
155static inline void array_xor32(uint32_t *d, uint32_t *s, uint32_t nb)
156{
157 while (nb--) *d++ ^= *s++;
158}
159#endif
160
161#ifndef ARCH_HAS_ARRAY_COPY64
162static inline void array_copy64(uint64_t *d, uint64_t *s, uint32_t nb)
163{
164 while (nb--) *d++ = *s++;
165}
166#endif
167
168#ifdef __GNUC__
169#define bitfn_ntz(n) __builtin_ctz(n)
170#else
171#error "define ntz for your platform"
172#endif
173
174#ifdef __MINGW32__
175 # define LITTLE_ENDIAN 1234
176 # define BYTE_ORDER LITTLE_ENDIAN
177#elif defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__)
178 # include <sys/endian.h>
179#elif defined(__OpenBSD__) || defined(__SVR4)
180 # include <sys/types.h>
181#elif defined(__APPLE__)
182 # include <machine/endian.h>
183#elif defined( BSD ) && ( BSD >= 199103 )
184 # include <machine/endian.h>
185#elif defined( __QNXNTO__ ) && defined( __LITTLEENDIAN__ )
186 # define LITTLE_ENDIAN 1234
187 # define BYTE_ORDER LITTLE_ENDIAN
188#elif defined( __QNXNTO__ ) && defined( __BIGENDIAN__ )
189 # define BIG_ENDIAN 1234
190 # define BYTE_ORDER BIG_ENDIAN
191#elif defined( _AIX )
192 # include <sys/machine.h>
193#else
194 # include <endian.h>
195#endif
196/* big endian to cpu */
197#if LITTLE_ENDIAN == BYTE_ORDER
198
199# define be32_to_cpu(a) bitfn_swap32(a)
200# define cpu_to_be32(a) bitfn_swap32(a)
201# define le32_to_cpu(a) (a)
202# define cpu_to_le32(a) (a)
203# define be64_to_cpu(a) bitfn_swap64(a)
204# define cpu_to_be64(a) bitfn_swap64(a)
205# define le64_to_cpu(a) (a)
206# define cpu_to_le64(a) (a)
207
208# define cpu_to_le32_array(d, s, l) array_copy32(d, s, l)
209# define le32_to_cpu_array(d, s, l) array_copy32(d, s, l)
210# define cpu_to_be32_array(d, s, l) array_swap32(d, s, l)
211# define be32_to_cpu_array(d, s, l) array_swap32(d, s, l)
212
213# define cpu_to_le64_array(d, s, l) array_copy64(d, s, l)
214# define le64_to_cpu_array(d, s, l) array_copy64(d, s, l)
215# define cpu_to_be64_array(d, s, l) array_swap64(d, s, l)
216# define be64_to_cpu_array(d, s, l) array_swap64(d, s, l)
217
218# define ror32_be(a, s) rol32(a, s)
219# define rol32_be(a, s) ror32(a, s)
220
221# define ARCH_IS_LITTLE_ENDIAN
222
223#elif BIG_ENDIAN == BYTE_ORDER
224
225# define be32_to_cpu(a) (a)
226# define cpu_to_be32(a) (a)
227# define be64_to_cpu(a) (a)
228# define cpu_to_be64(a) (a)
229# define le64_to_cpu(a) bitfn_swap64(a)
230# define cpu_to_le64(a) bitfn_swap64(a)
231# define le32_to_cpu(a) bitfn_swap32(a)
232# define cpu_to_le32(a) bitfn_swap32(a)
233
234# define cpu_to_le32_array(d, s, l) array_swap32(d, s, l)
235# define le32_to_cpu_array(d, s, l) array_swap32(d, s, l)
236# define cpu_to_be32_array(d, s, l) array_copy32(d, s, l)
237# define be32_to_cpu_array(d, s, l) array_copy32(d, s, l)
238
239# define cpu_to_le64_array(d, s, l) array_swap64(d, s, l)
240# define le64_to_cpu_array(d, s, l) array_swap64(d, s, l)
241# define cpu_to_be64_array(d, s, l) array_copy64(d, s, l)
242# define be64_to_cpu_array(d, s, l) array_copy64(d, s, l)
243
244# define ror32_be(a, s) ror32(a, s)
245# define rol32_be(a, s) rol32(a, s)
246
247# define ARCH_IS_BIG_ENDIAN
248
249#else
250# error "endian not supported"
251#endif
252
253#endif /* !BITFN_H */
diff --git a/dht/cbits/cryptonite_salsa.c b/dht/cbits/cryptonite_salsa.c
new file mode 100644
index 00000000..0bd96607
--- /dev/null
+++ b/dht/cbits/cryptonite_salsa.c
@@ -0,0 +1,297 @@
1/*
2 * Copyright (c) 2014-2015 Vincent Hanquez <vincent@snarc.org>
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the names of his contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <stdint.h>
32#include <string.h>
33#include <stdio.h>
34#include "cryptonite_salsa.h"
35#include "cryptonite_bitfn.h"
36
37static const uint8_t sigma[16] = "expand 32-byte k";
38static const uint8_t tau[16] = "expand 16-byte k";
39
40#define QR(a,b,c,d) \
41 b ^= rol32(a+d, 7); \
42 c ^= rol32(b+a, 9); \
43 d ^= rol32(c+b, 13); \
44 a ^= rol32(d+c, 18);
45
46#define ALIGNED64(PTR) \
47 (((uintptr_t)(const void *)(PTR)) % 8 == 0)
48
49#define SALSA_CORE_LOOP \
50 for (i = rounds; i > 0; i -= 2) { \
51 QR (x0,x4,x8,x12); \
52 QR (x5,x9,x13,x1); \
53 QR (x10,x14,x2,x6); \
54 QR (x15,x3,x7,x11); \
55 QR (x0,x1,x2,x3); \
56 QR (x5,x6,x7,x4); \
57 QR (x10,x11,x8,x9); \
58 QR (x15,x12,x13,x14); \
59 }
60
61static inline uint32_t load32(const uint8_t *p)
62{
63 return le32_to_cpu(*((uint32_t *) p));
64}
65
66static void salsa_core(int rounds, block *out, const cryptonite_salsa_state *in)
67{
68 uint32_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
69 int i;
70
71 x0 = in->d[0]; x1 = in->d[1]; x2 = in->d[2]; x3 = in->d[3];
72 x4 = in->d[4]; x5 = in->d[5]; x6 = in->d[6]; x7 = in->d[7];
73 x8 = in->d[8]; x9 = in->d[9]; x10 = in->d[10]; x11 = in->d[11];
74 x12 = in->d[12]; x13 = in->d[13]; x14 = in->d[14]; x15 = in->d[15];
75
76 SALSA_CORE_LOOP;
77
78 x0 += in->d[0]; x1 += in->d[1]; x2 += in->d[2]; x3 += in->d[3];
79 x4 += in->d[4]; x5 += in->d[5]; x6 += in->d[6]; x7 += in->d[7];
80 x8 += in->d[8]; x9 += in->d[9]; x10 += in->d[10]; x11 += in->d[11];
81 x12 += in->d[12]; x13 += in->d[13]; x14 += in->d[14]; x15 += in->d[15];
82
83 out->d[0] = cpu_to_le32(x0);
84 out->d[1] = cpu_to_le32(x1);
85 out->d[2] = cpu_to_le32(x2);
86 out->d[3] = cpu_to_le32(x3);
87 out->d[4] = cpu_to_le32(x4);
88 out->d[5] = cpu_to_le32(x5);
89 out->d[6] = cpu_to_le32(x6);
90 out->d[7] = cpu_to_le32(x7);
91 out->d[8] = cpu_to_le32(x8);
92 out->d[9] = cpu_to_le32(x9);
93 out->d[10] = cpu_to_le32(x10);
94 out->d[11] = cpu_to_le32(x11);
95 out->d[12] = cpu_to_le32(x12);
96 out->d[13] = cpu_to_le32(x13);
97 out->d[14] = cpu_to_le32(x14);
98 out->d[15] = cpu_to_le32(x15);
99}
100
101void cryptonite_salsa_core_xor(int rounds, block *out, block *in)
102{
103 uint32_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
104 int i;
105
106#define LOAD(i) (out->d[i] ^= in->d[i])
107 x0 = LOAD(0); x1 = LOAD(1); x2 = LOAD(2); x3 = LOAD(3);
108 x4 = LOAD(4); x5 = LOAD(5); x6 = LOAD(6); x7 = LOAD(7);
109 x8 = LOAD(8); x9 = LOAD(9); x10 = LOAD(10); x11 = LOAD(11);
110 x12 = LOAD(12); x13 = LOAD(13); x14 = LOAD(14); x15 = LOAD(15);
111#undef LOAD
112
113 SALSA_CORE_LOOP;
114
115 out->d[0] += x0; out->d[1] += x1; out->d[2] += x2; out->d[3] += x3;
116 out->d[4] += x4; out->d[5] += x5; out->d[6] += x6; out->d[7] += x7;
117 out->d[8] += x8; out->d[9] += x9; out->d[10] += x10; out->d[11] += x11;
118 out->d[12] += x12; out->d[13] += x13; out->d[14] += x14; out->d[15] += x15;
119}
120
121/* only 2 valid values for keylen are 256 (32) and 128 (16) */
122void cryptonite_salsa_init_core(cryptonite_salsa_state *st,
123 uint32_t keylen, const uint8_t *key,
124 uint32_t ivlen, const uint8_t *iv)
125{
126 const uint8_t *constants = (keylen == 32) ? sigma : tau;
127 int i;
128
129 st->d[0] = load32(constants + 0);
130 st->d[5] = load32(constants + 4);
131 st->d[10] = load32(constants + 8);
132 st->d[15] = load32(constants + 12);
133
134 st->d[1] = load32(key + 0);
135 st->d[2] = load32(key + 4);
136 st->d[3] = load32(key + 8);
137 st->d[4] = load32(key + 12);
138 /* we repeat the key on 128 bits */
139 if (keylen == 32)
140 key += 16;
141 st->d[11] = load32(key + 0);
142 st->d[12] = load32(key + 4);
143 st->d[13] = load32(key + 8);
144 st->d[14] = load32(key + 12);
145
146 st->d[9] = 0;
147 switch (ivlen) {
148 case 8:
149 st->d[6] = load32(iv + 0);
150 st->d[7] = load32(iv + 4);
151 st->d[8] = 0;
152 break;
153 case 12:
154 st->d[6] = load32(iv + 0);
155 st->d[7] = load32(iv + 4);
156 st->d[8] = load32(iv + 8);
157 default:
158 return;
159 }
160}
161
162void cryptonite_salsa_init(cryptonite_salsa_context *ctx, uint8_t nb_rounds,
163 uint32_t keylen, const uint8_t *key,
164 uint32_t ivlen, const uint8_t *iv)
165{
166 memset(ctx, 0, sizeof(*ctx));
167 ctx->nb_rounds = nb_rounds;
168 cryptonite_salsa_init_core(&ctx->st, keylen, key, ivlen, iv);
169}
170
171void cryptonite_salsa_combine(uint8_t *dst, cryptonite_salsa_context *ctx, const uint8_t *src, uint32_t bytes)
172{
173 block out;
174 cryptonite_salsa_state *st;
175 int i;
176
177 if (!bytes)
178 return;
179
180 /* xor the previous buffer first (if any) */
181 if (ctx->prev_len > 0) {
182 int to_copy = (ctx->prev_len < bytes) ? ctx->prev_len : bytes;
183 for (i = 0; i < to_copy; i++)
184 dst[i] = src[i] ^ ctx->prev[ctx->prev_ofs+i];
185 memset(ctx->prev + ctx->prev_ofs, 0, to_copy);
186 ctx->prev_len -= to_copy;
187 ctx->prev_ofs += to_copy;
188 src += to_copy;
189 dst += to_copy;
190 bytes -= to_copy;
191 }
192
193 if (bytes == 0)
194 return;
195
196 st = &ctx->st;
197
198 /* xor new 64-bytes chunks and store the left over if any */
199 for (; bytes >= 64; bytes -= 64, src += 64, dst += 64) {
200 /* generate new chunk and update state */
201 salsa_core(ctx->nb_rounds, &out, st);
202 st->d[8] += 1;
203 if (st->d[8] == 0)
204 st->d[9] += 1;
205
206 for (i = 0; i < 64; ++i)
207 dst[i] = src[i] ^ out.b[i];
208 }
209
210 if (bytes > 0) {
211 /* generate new chunk and update state */
212 salsa_core(ctx->nb_rounds, &out, st);
213 st->d[8] += 1;
214 if (st->d[8] == 0)
215 st->d[9] += 1;
216
217 /* xor as much as needed */
218 for (i = 0; i < bytes; i++)
219 dst[i] = src[i] ^ out.b[i];
220
221 /* copy the left over in the buffer */
222 ctx->prev_len = 64 - bytes;
223 ctx->prev_ofs = i;
224 for (; i < 64; i++) {
225 ctx->prev[i] = out.b[i];
226 }
227 }
228}
229
230void cryptonite_salsa_generate(uint8_t *dst, cryptonite_salsa_context *ctx, uint32_t bytes)
231{
232 cryptonite_salsa_state *st;
233 block out;
234 int i;
235
236 if (!bytes)
237 return;
238
239 /* xor the previous buffer first (if any) */
240 if (ctx->prev_len > 0) {
241 int to_copy = (ctx->prev_len < bytes) ? ctx->prev_len : bytes;
242 for (i = 0; i < to_copy; i++)
243 dst[i] = ctx->prev[ctx->prev_ofs+i];
244 memset(ctx->prev + ctx->prev_ofs, 0, to_copy);
245 ctx->prev_len -= to_copy;
246 ctx->prev_ofs += to_copy;
247 dst += to_copy;
248 bytes -= to_copy;
249 }
250
251 if (bytes == 0)
252 return;
253
254 st = &ctx->st;
255
256 if (ALIGNED64(dst)) {
257 /* xor new 64-bytes chunks and store the left over if any */
258 for (; bytes >= 64; bytes -= 64, dst += 64) {
259 /* generate new chunk and update state */
260 salsa_core(ctx->nb_rounds, (block *) dst, st);
261 st->d[8] += 1;
262 if (st->d[8] == 0)
263 st->d[9] += 1;
264 }
265 } else {
266 /* xor new 64-bytes chunks and store the left over if any */
267 for (; bytes >= 64; bytes -= 64, dst += 64) {
268 /* generate new chunk and update state */
269 salsa_core(ctx->nb_rounds, &out, st);
270 st->d[8] += 1;
271 if (st->d[8] == 0)
272 st->d[9] += 1;
273
274 for (i = 0; i < 64; ++i)
275 dst[i] = out.b[i];
276 }
277 }
278
279 if (bytes > 0) {
280 /* generate new chunk and update state */
281 salsa_core(ctx->nb_rounds, &out, st);
282 st->d[8] += 1;
283 if (st->d[8] == 0)
284 st->d[9] += 1;
285
286 /* xor as much as needed */
287 for (i = 0; i < bytes; i++)
288 dst[i] = out.b[i];
289
290 /* copy the left over in the buffer */
291 ctx->prev_len = 64 - bytes;
292 ctx->prev_ofs = i;
293 for (; i < 64; i++)
294 ctx->prev[i] = out.b[i];
295 }
296}
297
diff --git a/dht/cbits/cryptonite_salsa.h b/dht/cbits/cryptonite_salsa.h
new file mode 100644
index 00000000..33e9cda9
--- /dev/null
+++ b/dht/cbits/cryptonite_salsa.h
@@ -0,0 +1,57 @@
1/*
2 * Copyright (c) 2014 Vincent Hanquez <vincent@snarc.org>
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the names of his contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30#ifndef CRYPTONITE_SALSA
31#define CRYPTONITE_SALSA
32
33typedef union {
34 uint64_t q[8];
35 uint32_t d[16];
36 uint8_t b[64];
37} block;
38
39typedef block cryptonite_salsa_state;
40
41typedef struct {
42 cryptonite_salsa_state st;
43 uint8_t prev[64];
44 uint8_t prev_ofs;
45 uint8_t prev_len;
46 uint8_t nb_rounds;
47} cryptonite_salsa_context;
48
49/* for scrypt */
50void cryptonite_salsa_core_xor(int rounds, block *out, block *in);
51
52void cryptonite_salsa_init_core(cryptonite_salsa_state *st, uint32_t keylen, const uint8_t *key, uint32_t ivlen, const uint8_t *iv);
53void cryptonite_salsa_init(cryptonite_salsa_context *ctx, uint8_t nb_rounds, uint32_t keylen, const uint8_t *key, uint32_t ivlen, const uint8_t *iv);
54void cryptonite_salsa_combine(uint8_t *dst, cryptonite_salsa_context *st, const uint8_t *src, uint32_t bytes);
55void cryptonite_salsa_generate(uint8_t *dst, cryptonite_salsa_context *st, uint32_t bytes);
56
57#endif
diff --git a/dht/cbits/cryptonite_xsalsa.c b/dht/cbits/cryptonite_xsalsa.c
new file mode 100644
index 00000000..6718cd7d
--- /dev/null
+++ b/dht/cbits/cryptonite_xsalsa.c
@@ -0,0 +1,80 @@
1/*
2 * Copyright (c) 2016 Brandon Hamilton <brandon.hamilton@gmail.com>
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the names of his contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30#include <stdint.h>
31#include <string.h>
32#include "cryptonite_xsalsa.h"
33#include "cryptonite_bitfn.h"
34
35static inline uint32_t load32(const uint8_t *p)
36{
37 return le32_to_cpu(*((uint32_t *) p));
38}
39
40/* XSalsa20 algorithm as described in https://cr.yp.to/snuffle/xsalsa-20081128.pdf */
41void cryptonite_xsalsa_init(cryptonite_salsa_context *ctx, uint8_t nb_rounds,
42 uint32_t keylen, const uint8_t *key,
43 uint32_t ivlen, const uint8_t *iv)
44{
45 memset(ctx, 0, sizeof(*ctx));
46 ctx->nb_rounds = nb_rounds;
47
48 /* Create initial 512-bit input block:
49 (x0, x5, x10, x15) is the Salsa20 constant
50 (x1, x2, x3, x4, x11, x12, x13, x14) is a 256-bit key
51 (x6, x7, x8, x9) is the first 128 bits of a 192-bit nonce
52 */
53 cryptonite_salsa_init_core(&ctx->st, keylen, key, 8, iv);
54 ctx->st.d[ 8] = load32(iv + 8);
55 ctx->st.d[ 9] = load32(iv + 12);
56
57 /* Compute (z0, z1, . . . , z15) = doubleround ^(r/2) (x0, x1, . . . , x15) */
58 block hSalsa;
59 memset(&hSalsa, 0, sizeof(block));
60 cryptonite_salsa_core_xor(nb_rounds, &hSalsa, &ctx->st);
61
62 /* Build a new 512-bit input block (x′0, x′1, . . . , x′15):
63 (x′0, x′5, x′10, x′15) is the Salsa20 constant
64 (x′1,x′2,x′3,x′4,x′11,x′12,x′13,x′14) = (z0,z5,z10,z15,z6,z7,z8,z9)
65 (x′6,x′7) is the last 64 bits of the 192-bit nonce
66 (x′8, x′9) is a 64-bit block counter.
67 */
68 ctx->st.d[ 1] = hSalsa.d[ 0] - ctx->st.d[ 0];
69 ctx->st.d[ 2] = hSalsa.d[ 5] - ctx->st.d[ 5];
70 ctx->st.d[ 3] = hSalsa.d[10] - ctx->st.d[10];
71 ctx->st.d[ 4] = hSalsa.d[15] - ctx->st.d[15];
72 ctx->st.d[11] = hSalsa.d[ 6] - ctx->st.d[ 6];
73 ctx->st.d[12] = hSalsa.d[ 7] - ctx->st.d[ 7];
74 ctx->st.d[13] = hSalsa.d[ 8] - ctx->st.d[ 8];
75 ctx->st.d[14] = hSalsa.d[ 9] - ctx->st.d[ 9];
76 ctx->st.d[ 6] = load32(iv + 16);
77 ctx->st.d[ 7] = load32(iv + 20);
78 ctx->st.d[ 8] = 0;
79 ctx->st.d[ 9] = 0;
80} \ No newline at end of file
diff --git a/dht/cbits/cryptonite_xsalsa.h b/dht/cbits/cryptonite_xsalsa.h
new file mode 100644
index 00000000..73233cee
--- /dev/null
+++ b/dht/cbits/cryptonite_xsalsa.h
@@ -0,0 +1,37 @@
1/*
2 * Copyright (c) 2016 Brandon Hamilton <brandon.hamilton@gmail.com>
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the names of his contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30#ifndef CRYPTONITE_XSALSA
31#define CRYPTONITE_XSALSA
32
33#include "cryptonite_salsa.h"
34
35void cryptonite_xsalsa_init(cryptonite_salsa_context *ctx, uint8_t nb_rounds, uint32_t keylen, const uint8_t *key, uint32_t ivlen, const uint8_t *iv);
36
37#endif