annotate NaCl/APIv2.cs @ 54:ba4e2cb031e0

Added general purpose tar archive reader class
author Ivo Smits <Ivo@UCIS.nl>
date Wed, 02 Oct 2013 21:17:30 +0200
parents 29cf42a12c34
children 6aca18ee4ec6
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;
43
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
2 using System.Globalization;
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
3 using UCIS.Util;
20
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
4 using curve25519xsalsa20poly1305impl = UCIS.NaCl.crypto_box.curve25519xsalsa20poly1305;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
5 using edwards25519sha512batchimpl = UCIS.NaCl.crypto_sign.edwards25519sha512batch;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
6 using xsalsa20poly1305impl = UCIS.NaCl.crypto_secretbox.xsalsa20poly1305;
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 namespace UCIS.NaCl.v2 {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
9 public class curve25519keypair {
43
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
10 private Byte[] secretkey;
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
11 private Byte[] publickey = null;
20
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
12
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
13 public curve25519keypair() {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
14 curve25519xsalsa20poly1305impl.crypto_box_keypair(out publickey, out secretkey);
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
15 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
16 public curve25519keypair(Byte[] secretkey) {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
17 this.secretkey = secretkey;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
18 }
43
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
19 public curve25519keypair(String secretkey) {
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
20 this.secretkey = DecodeHexString(secretkey, curve25519xsalsa20poly1305impl.SECRETKEYBYTES);
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
21 }
20
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
22 public curve25519keypair(Byte[] secretkey, Byte[] publickey) {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
23 if (publickey.Length != curve25519xsalsa20poly1305impl.PUBLICKEYBYTES) throw new ArgumentOutOfRangeException("publickey");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
24 if (secretkey.Length != curve25519xsalsa20poly1305impl.SECRETKEYBYTES) throw new ArgumentOutOfRangeException("secretkey");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
25 this.secretkey = secretkey;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
26 this.publickey = publickey;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
27 }
43
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
28 public Byte[] PublicKey {
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
29 get {
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
30 if (publickey == null) publickey = curve25519xsalsa20poly1305impl.crypto_box_getpublickey(secretkey);
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
31 return publickey;
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
32 }
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
33 }
20
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
34 public Byte[] SecretKey { get { return secretkey; } }
43
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
35 internal static Byte[] DecodeHexString(String str, int length) {
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
36 if (str.Length != length * 2) throw new ArgumentException("str", "Incorrect key length");
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
37 Byte[] bytes = new Byte[length];
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
38 for (int i = 0; i < length; i++) bytes[i] = Byte.Parse(str.Substring(i * 2, 2), NumberStyles.HexNumber);
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
39 return bytes;
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
40 }
20
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
41 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
42 public class curve25519xsalsa20poly1305 : xsalsa20poly1305 {
43
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
43 public Byte[] PublicKey { get; private set; }
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
44 public curve25519keypair SecretKey { get; private set; }
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
45 public curve25519xsalsa20poly1305(String publickey, String secretkey)
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
46 : this(curve25519keypair.DecodeHexString(publickey, curve25519xsalsa20poly1305impl.PUBLICKEYBYTES), new curve25519keypair(secretkey)) {
20
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
47 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
48 public curve25519xsalsa20poly1305(Byte[] publickey, Byte[] secretkey)
43
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
49 : this(publickey, new curve25519keypair(secretkey)) {
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
50 }
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
51 public curve25519xsalsa20poly1305(Byte[] publickey, curve25519keypair secretkey)
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
52 : base(curve25519xsalsa20poly1305impl.crypto_box_beforenm(publickey, secretkey.SecretKey)) {
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
53 this.PublicKey = publickey;
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
54 this.SecretKey = secretkey;
20
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
55 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
56 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
57 public class xsalsa20poly1305 {
43
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
58 protected Byte[] sharedkey = new Byte[xsalsa20poly1305impl.KEYBYTES];
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
59 Byte[] nonce = new Byte[xsalsa20poly1305impl.NONCEBYTES];
20
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
60
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
61 public int SharedKeySize { get { return xsalsa20poly1305impl.KEYBYTES; } }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
62
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
63 public xsalsa20poly1305(Byte[] sharedkey) : this(sharedkey, null) { }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
64 public xsalsa20poly1305(Byte[] sharedkey, Byte[] nonce) {
43
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
65 if (sharedkey == null) throw new ArgumentNullException("secretkey");
20
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
66 if (sharedkey.Length != xsalsa20poly1305impl.KEYBYTES) throw new ArgumentOutOfRangeException("secretkey", "The key size does not match the expected key length");
43
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
67 sharedkey.CopyTo(this.sharedkey, 0);
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
68 if (nonce != null) this.Nonce = nonce;
20
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
69 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
70
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
71 public Byte[] Nonce {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
72 get { return this.nonce; }
43
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
73 set { NonceValue = value; }
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
74 }
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
75 public Byte[] NonceValue {
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
76 get { return ArrayUtil.ToArray(nonce); }
20
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
77 set {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
78 if (ReferenceEquals(value, null)) throw new ArgumentNullException("value");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
79 if (value.Length > xsalsa20poly1305impl.NONCEBYTES) throw new ArgumentOutOfRangeException("value", "Nonce is too big");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
80 value.CopyTo(nonce, 0);
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
81 Array.Clear(this.nonce, value.Length, this.nonce.Length - value.Length);
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
82 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
83 }
43
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
84 public Byte[] NonceBuffer {
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
85 get { return this.nonce; }
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
86 set {
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
87 if (ReferenceEquals(value, null)) throw new ArgumentNullException("value");
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
88 if (value.Length > xsalsa20poly1305impl.NONCEBYTES) throw new ArgumentOutOfRangeException("value", "Incorrect nonce length");
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
89 this.nonce = value;
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
90 }
29cf42a12c34 Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents: 20
diff changeset
91 }
20
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
92
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
93 public Byte[] SharedKey {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
94 get { return sharedkey; }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
95 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
96
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
97 public unsafe Byte[] Encrypt(Byte[] data) {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
98 return Encrypt(data, 0, data.Length);
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
99 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
100 public unsafe Byte[] Encrypt(Byte[] data, int offset, int count) {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
101 if (ReferenceEquals(data, null)) throw new ArgumentNullException("data");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
102 if (offset < 0) throw new ArgumentOutOfRangeException("offset", "Offset can not be negative");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
103 if (data.Length < offset + count) throw new ArgumentOutOfRangeException("count", "The specified range is outside of the array");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
104 Byte[] ret = new Byte[GetEncryptedSize(count)];
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
105 fixed (Byte* mp = data, cp = ret, np = nonce, kp = sharedkey) {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
106 if (xsalsa20poly1305impl.crypto_secretbox_nopad(cp, mp + offset, (ulong)count, np, kp) != 0) throw new InvalidOperationException("Encryption failed");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
107 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
108 return ret;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
109 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
110 public unsafe int EncryptTo(Byte[] data, int offset, int count, Byte[] outdata, int outoffset, int outcount) {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
111 if (ReferenceEquals(data, null)) throw new ArgumentNullException("data");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
112 if (offset < 0) throw new ArgumentOutOfRangeException("offset", "Offset can not be negative");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
113 if (data.Length < offset + count) throw new ArgumentOutOfRangeException("count", "The specified range is outside of the array");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
114 if (ReferenceEquals(outdata, null)) throw new ArgumentNullException("outdata");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
115 if (outoffset < 0) throw new ArgumentOutOfRangeException("outoffset", "Offset can not be negative");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
116 if (outdata.Length < outoffset + outcount) throw new ArgumentOutOfRangeException("outcount", "The specified range is outside of the array");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
117 int retcount = GetEncryptedSize(count);
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
118 if (outcount < retcount) throw new ArgumentOutOfRangeException("outcount", "The output buffer is too small");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
119 fixed (Byte* mp = data, cp = outdata, np = nonce, kp = sharedkey) {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
120 if (xsalsa20poly1305impl.crypto_secretbox_nopad(cp + outoffset, mp + offset, (ulong)count, np, kp) != 0) throw new InvalidOperationException("Encryption failed");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
121 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
122 return outcount;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
123 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
124 /*public unsafe void EncryptInplace(Byte[] data, int offset, int count) {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
125 if (ReferenceEquals(data, null)) throw new ArgumentNullException("data");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
126 if (offset < 0) throw new ArgumentOutOfRangeException("offset", "Offset can not be negative");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
127 if (data.Length < offset + count) throw new ArgumentOutOfRangeException("count", "The specified range is outside of the array");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
128 if (count < 16) throw new ArgumentOutOfRangeException("count", "count should be at least 16");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
129 fixed (Byte* mp = data, np = nonce, kp = sharedkey) {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
130 if (xsalsa20poly1305impl.crypto_secretbox_inplace_nopad(mp + offset, (ulong)count, np, kp) != 0) throw new InvalidOperationException("Encryption failed");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
131 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
132 }*/
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
133 public int GetEncryptedSize(int size) {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
134 return size + 16;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
135 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
136
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
137 public unsafe Byte[] Decrypt(Byte[] data) {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
138 return Decrypt(data, 0, data.Length);
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
139 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
140 public unsafe Byte[] Decrypt(Byte[] data, int offset, int count) {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
141 if (ReferenceEquals(data, null)) throw new ArgumentNullException("data");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
142 if (offset < 0) throw new ArgumentOutOfRangeException("offset", "Offset can not be negative");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
143 if (data.Length < offset + count) throw new ArgumentOutOfRangeException("count", "The specified range is outside of the array");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
144 if (count < 16) return null;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
145 Byte[] ret = new Byte[GetDecryptedSize(count)];
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
146 fixed (Byte* cp = data, mp = ret, np = nonce, kp = sharedkey) {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
147 if (xsalsa20poly1305impl.crypto_secretbox_open_nopad(mp, cp + offset, (ulong)count, np, kp) != 0) return null;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
148 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
149 return ret;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
150 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
151 public unsafe int? DecryptTo(Byte[] data, int offset, int count, Byte[] outdata, int outoffset, int outcount) {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
152 if (ReferenceEquals(data, null)) throw new ArgumentNullException("data");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
153 if (offset < 0) throw new ArgumentOutOfRangeException("offset", "Offset can not be negative");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
154 if (data.Length < offset + count) throw new ArgumentOutOfRangeException("count", "The specified range is outside of the array");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
155 if (count < 16) return null;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
156 if (ReferenceEquals(outdata, null)) throw new ArgumentNullException("outdata");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
157 if (outoffset < 0) throw new ArgumentOutOfRangeException("outoffset", "Offset can not be negative");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
158 if (outdata.Length < outoffset + outcount) throw new ArgumentOutOfRangeException("outcount", "The specified range is outside of the array");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
159 int retcount = GetDecryptedSize(count);
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
160 if (outcount < retcount) throw new ArgumentOutOfRangeException("outcount", "The output buffer is too small");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
161 fixed (Byte* cp = data, mp = outdata, np = nonce, kp = sharedkey) {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
162 if (xsalsa20poly1305impl.crypto_secretbox_open_nopad(mp + outoffset, cp + offset, (ulong)count, np, kp) != 0) return null;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
163 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
164 return retcount;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
165 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
166 public unsafe ArraySegment<Byte>? DecryptInplace(Byte[] data, int offset, int count) {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
167 if (ReferenceEquals(data, null)) throw new ArgumentNullException("data");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
168 if (offset < 0) throw new ArgumentOutOfRangeException("offset", "Offset can not be negative");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
169 if (data.Length < offset + count) throw new ArgumentOutOfRangeException("count", "The specified range is outside of the array");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
170 if (count < 16) return null;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
171 fixed (Byte* cp = data, np = nonce, kp = sharedkey) {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
172 if (xsalsa20poly1305impl.crypto_secretbox_open_inplace_nopad(cp + offset, (ulong)count, np, kp) != 0) return null;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
173 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
174 return new ArraySegment<byte>(data, offset + 16, count - 16);
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
175 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
176 public int GetDecryptedSize(int size) {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
177 if (size < 16) return -1;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
178 return size - 16;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
179 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
180
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
181 public Boolean Verify(Byte[] data) {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
182 return Verify(data, 0, data.Length);
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
183 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
184 public unsafe Boolean Verify(Byte[] data, int offset, int count) {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
185 if (ReferenceEquals(data, null)) throw new ArgumentNullException("data");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
186 if (offset < 0) throw new ArgumentOutOfRangeException("offset", "Offset can not be negative");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
187 if (data.Length < offset + count) throw new ArgumentOutOfRangeException("count", "The specified range is outside of the array");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
188 if (count < 16) return false;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
189 Byte[] ret = new Byte[GetDecryptedSize(count)];
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
190 fixed (Byte* cp = data, np = nonce, kp = sharedkey) {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
191 return xsalsa20poly1305impl.crypto_secretbox_verify(cp + offset, (ulong)count, np, kp);
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
192 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
193 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
194
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
195 public Byte[] GenerateRandomNonce() {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
196 randombytes.generate(nonce);
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
197 return nonce;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
198 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
199 public void IncrementNonceLE() {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
200 for (int i = 0; i < nonce.Length && ++nonce[i] == 0; i++) ;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
201 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
202 public void IncrementNonceBE() {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
203 for (int i = nonce.Length - 1; i >= 0 && ++nonce[i] == 0; i--) ;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
204 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
205
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
206 public xsalsa20poly1305 Clone() {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
207 return new xsalsa20poly1305(sharedkey, nonce);
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
208 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
209 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
210 public class edwards25519sha512batch {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
211 public Byte[] Sign(Byte[] message, Byte[] secretkey) {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
212 return edwards25519sha512batchimpl.crypto_sign(message, secretkey);
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
213 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
214 public int GetSignedSize(int size) {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
215 return size + 64;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
216 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
217 public Byte[] Open(Byte[] signed, Byte[] publickey) {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
218 return edwards25519sha512batchimpl.crypto_sign_open(signed, publickey);
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
219 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
220 public unsafe Boolean Verify(Byte[] signed, Byte[] publickey) {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
221 if (publickey.Length != edwards25519sha512batchimpl.PUBLICKEYBYTES) throw new ArgumentException("publickey.Length != PUBLICKEYBYTES");
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
222 UInt64 mlen;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
223 fixed (Byte* smp = signed, pkp = publickey) return edwards25519sha512batchimpl.crypto_sign_open(null, out mlen, smp, (ulong)signed.Length, pkp) == 0;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
224 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
225 public Byte[] Extract(Byte[] signed) {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
226 if (signed.Length < 64) return null;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
227 Byte[] ret = new Byte[signed.Length - 64];
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
228 Buffer.BlockCopy(signed, 32, ret, 0, ret.Length);
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
229 return ret;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
230 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
231 public int GetExtractedSize(int size) {
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
232 if (size < 64) return -1;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
233 return size - 64;
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
234 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
235 }
c873e3dd73fe Added NaCl cryptography code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
236 }