annotate NaCl/crypto_stream/xsalsa20.cs @ 20:c873e3dd73fe

Added NaCl cryptography code
author Ivo Smits <Ivo@UCIS.nl>
date Mon, 15 Apr 2013 00:43:48 +0200
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
1 ???using System;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
2
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
3 namespace UCIS.NaCl.crypto_stream {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
4 static unsafe class xsalsa20 {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
5 public const int KEYBYTES = 32;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
6 public const int NONCEBYTES = 24;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
7
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
8 //Never written to
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
9 static Byte[] sigma = new Byte[16] {(Byte)'e', (Byte)'x', (Byte)'p', (Byte)'a', //[16] = "expand 32-byte k";
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
10 (Byte)'n', (Byte)'d', (Byte)' ', (Byte)'3',
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
11 (Byte)'2', (Byte)'-', (Byte)'b', (Byte)'y',
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
12 (Byte)'t', (Byte)'e', (Byte)' ', (Byte)'k', };
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
13
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
14 public static void crypto_stream(Byte* c, int clen, Byte* n, Byte* k) {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
15 Byte* subkey = stackalloc Byte[32];
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
16 crypto_core.hsalsa20.crypto_core(subkey, n, k, sigma);
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
17 salsa20.crypto_stream(c, clen, n + 16, subkey);
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
18 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
19
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
20 public static void crypto_stream_xor(Byte* c, Byte* m, UInt64 mlen, Byte* n, Byte* k) {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
21 Byte* subkey = stackalloc Byte[32];
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
22 crypto_core.hsalsa20.crypto_core(subkey, n, k, sigma);
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
23 salsa20.crypto_stream_xor(c, m, (int)mlen, n + 16, subkey);
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
24 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
25
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
26 internal static void crypto_stream_xor_split(Byte* mcpad, int padbytes, Byte* c, Byte* m, UInt64 mlen, Byte* n, Byte* k) {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
27 Byte* subkey = stackalloc Byte[32];
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
28 crypto_core.hsalsa20.crypto_core(subkey, n, k, sigma);
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
29 salsa20.crypto_stream_xor_split(mcpad, padbytes, c, m, (int)mlen, n + 16, subkey);
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
30 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
31 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
32 }