20
|
1 ???using System; |
|
2 |
|
3 namespace UCIS.NaCl.crypto_secretbox { |
|
4 unsafe static class xsalsa20poly1305 { |
|
5 public const int KEYBYTES = 32; |
|
6 public const int NONCEBYTES = 24; |
|
7 public const int ZEROBYTES = 32; |
|
8 public const int BOXZEROBYTES = 16; |
|
9 |
|
10 static public int crypto_secretbox(Byte* c, Byte* m, UInt64 mlen, Byte* n, Byte* k) { |
|
11 if (mlen < 32) return -1; |
|
12 crypto_stream.xsalsa20.crypto_stream_xor(c, m, mlen, n, k); |
|
13 crypto_onetimeauth.poly1305.crypto_onetimeauth(c + 16, c + 32, mlen - 32, c); |
|
14 for (int i = 0; i < 16; ++i) c[i] = 0; |
|
15 return 0; |
|
16 } |
|
17 |
|
18 static public int crypto_secretbox_open(Byte* m, Byte* c, UInt64 clen, Byte* n, Byte* k) { |
|
19 if (clen < 32) return -1; |
|
20 Byte[] subkey = new Byte[32]; |
|
21 fixed (Byte* subkeyp = subkey) { |
|
22 crypto_stream.xsalsa20.crypto_stream(subkeyp, 32, n, k); |
|
23 if (crypto_onetimeauth.poly1305.crypto_onetimeauth_verify(c + 16, c + 32, clen - 32, subkeyp) != 0) return -1; |
|
24 } |
|
25 crypto_stream.xsalsa20.crypto_stream_xor(m, c, clen, n, k); |
|
26 for (int i = 0; i < 32; ++i) m[i] = 0; |
|
27 return 0; |
|
28 } |
|
29 |
|
30 static internal int crypto_secretbox_nopad(Byte* c, Byte* m, UInt64 mlen, Byte* n, Byte* k) { |
|
31 if (mlen < 0) return -1; |
|
32 Byte* mc32 = stackalloc Byte[32]; |
|
33 for (int i = 0; i < 32; i += 4) *(int*)(mc32 + i) = 0; |
|
34 crypto_stream.xsalsa20.crypto_stream_xor_split(mc32, 32, c + 16, m, mlen, n, k); |
|
35 crypto_onetimeauth.poly1305.crypto_onetimeauth(mc32 + 16, c + 16, mlen, mc32); |
|
36 for (int i = 0; i < 16; ++i) c[i] = mc32[i + 16]; |
|
37 return 0; |
|
38 } |
|
39 |
|
40 static internal Boolean crypto_secretbox_verify(Byte* c, UInt64 clen, Byte* n, Byte* k) { |
|
41 if (clen < 16) return false; |
|
42 Byte* subkey = stackalloc Byte[32]; |
|
43 for (int i = 0; i < 32; i += 4) *(int*)(subkey + i) = 0; |
|
44 crypto_stream.xsalsa20.crypto_stream(subkey, 32, n, k); |
|
45 return crypto_onetimeauth.poly1305.crypto_onetimeauth_verify(c, c + 16, clen - 16, subkey) == 0; |
|
46 } |
|
47 |
|
48 static internal int crypto_secretbox_open_nopad(Byte* m, Byte* c, UInt64 clen, Byte* n, Byte* k) { |
|
49 if (!crypto_secretbox_verify(c, clen, n, k)) return -1; |
|
50 if (clen < 16) return -1; |
|
51 Byte* mc32 = stackalloc Byte[32]; |
|
52 for (int i = 0; i < 16; i += 4) *(int*)(mc32 + i) = 0; |
|
53 for (int i = 0; i < 16; i += 4) *(int*)(mc32 + i + 16) = *(int*)(c + i); |
|
54 crypto_stream.xsalsa20.crypto_stream_xor_split(mc32, 32, m, c + 16, clen - 16, n, k); |
|
55 return 0; |
|
56 } |
|
57 |
|
58 static internal int crypto_secretbox_inplace_nopad(Byte* c, UInt64 mlen, Byte* n, Byte* k) { |
|
59 if (mlen < 0) return -1; |
|
60 Byte* mc16 = stackalloc Byte[16]; |
|
61 for (int i = 0; i < 16; i += 4) *(int*)(mc16 + i) = 0; |
|
62 crypto_stream.xsalsa20.crypto_stream_xor_split(mc16, 16, c, c, mlen, n, k); |
|
63 crypto_onetimeauth.poly1305.crypto_onetimeauth(c, c + 16, mlen, mc16); |
|
64 return 0; |
|
65 } |
|
66 |
|
67 static internal int crypto_secretbox_open_inplace_nopad(Byte* c, UInt64 clen, Byte* n, Byte* k) { |
|
68 if (clen < 16) return -1; |
|
69 Byte* subkey = stackalloc Byte[32]; |
|
70 for (int i = 0; i < 32; i += 4) *(int*)(subkey + i) = 0; |
|
71 crypto_stream.xsalsa20.crypto_stream(subkey, 32, n, k); |
|
72 if (crypto_onetimeauth.poly1305.crypto_onetimeauth_verify(c, c + 16, clen - 16, subkey) != 0) return -1; |
|
73 crypto_stream.xsalsa20.crypto_stream_xor_split(null, 16, c, c, clen, n, k); |
|
74 return 0; |
|
75 } |
|
76 } |
|
77 } |