diff options
author | joe <joe@jerkface.net> | 2017-07-08 22:10:40 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-07-08 22:10:40 -0400 |
commit | 8fe93b8e1d1d968bdf0b8a35335b060d92a9d7d7 (patch) | |
tree | 6e9a4b35f11de5ad0e4f422e0a6d268b5270befd /cbits/cryptonite_xsalsa.c | |
parent | f75d515bc0100e5ca372d592aa2f5f4ff2fc858c (diff) |
WIP: Tox encryption.
Diffstat (limited to 'cbits/cryptonite_xsalsa.c')
-rw-r--r-- | cbits/cryptonite_xsalsa.c | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/cbits/cryptonite_xsalsa.c b/cbits/cryptonite_xsalsa.c new file mode 100644 index 00000000..6718cd7d --- /dev/null +++ b/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 | |||
35 | static 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 */ | ||
41 | void 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 | ||