Mercurial > hg > ucis.core
annotate NaCl/APIv2.cs @ 111:df53bdd49507 default tip
Merge
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Fri, 07 Nov 2014 18:37:39 +0100 |
parents | 4714531734b3 |
children |
rev | line source |
---|---|
20 | 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 | 4 using curve25519xsalsa20poly1305impl = UCIS.NaCl.crypto_box.curve25519xsalsa20poly1305; |
80 | 5 using ed25519impl = UCIS.NaCl.crypto_sign.ed25519; |
20 | 6 using edwards25519sha512batchimpl = UCIS.NaCl.crypto_sign.edwards25519sha512batch; |
80 | 7 using sha512impl = UCIS.NaCl.crypto_hash.sha512; |
20 | 8 using xsalsa20poly1305impl = UCIS.NaCl.crypto_secretbox.xsalsa20poly1305; |
9 | |
10 namespace UCIS.NaCl.v2 { | |
11 public class curve25519keypair { | |
43
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
12 private Byte[] secretkey; |
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
13 private Byte[] publickey = null; |
20 | 14 |
15 public curve25519keypair() { | |
16 curve25519xsalsa20poly1305impl.crypto_box_keypair(out publickey, out secretkey); | |
17 } | |
18 public curve25519keypair(Byte[] secretkey) { | |
19 this.secretkey = secretkey; | |
20 } | |
43
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
21 public curve25519keypair(String secretkey) { |
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
22 this.secretkey = DecodeHexString(secretkey, curve25519xsalsa20poly1305impl.SECRETKEYBYTES); |
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
23 } |
20 | 24 public curve25519keypair(Byte[] secretkey, Byte[] publickey) { |
25 if (publickey.Length != curve25519xsalsa20poly1305impl.PUBLICKEYBYTES) throw new ArgumentOutOfRangeException("publickey"); | |
26 if (secretkey.Length != curve25519xsalsa20poly1305impl.SECRETKEYBYTES) throw new ArgumentOutOfRangeException("secretkey"); | |
27 this.secretkey = secretkey; | |
28 this.publickey = publickey; | |
29 } | |
43
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
30 public Byte[] PublicKey { |
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
31 get { |
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
32 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
|
33 return publickey; |
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
34 } |
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
35 } |
20 | 36 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
|
37 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
|
38 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
|
39 Byte[] bytes = new Byte[length]; |
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
40 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
|
41 return bytes; |
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
42 } |
20 | 43 } |
44 public class curve25519xsalsa20poly1305 : xsalsa20poly1305 { | |
43
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
45 public Byte[] PublicKey { get; private set; } |
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
46 public curve25519keypair SecretKey { get; private set; } |
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
47 public curve25519xsalsa20poly1305(String publickey, String secretkey) |
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
48 : this(curve25519keypair.DecodeHexString(publickey, curve25519xsalsa20poly1305impl.PUBLICKEYBYTES), new curve25519keypair(secretkey)) { |
20 | 49 } |
50 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
|
51 : this(publickey, new curve25519keypair(secretkey)) { |
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
52 } |
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
53 public curve25519xsalsa20poly1305(Byte[] publickey, curve25519keypair secretkey) |
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
54 : 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
|
55 this.PublicKey = publickey; |
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
56 this.SecretKey = secretkey; |
20 | 57 } |
58 } | |
59 public class xsalsa20poly1305 { | |
43
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
60 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
|
61 Byte[] nonce = new Byte[xsalsa20poly1305impl.NONCEBYTES]; |
20 | 62 |
63 public int SharedKeySize { get { return xsalsa20poly1305impl.KEYBYTES; } } | |
64 | |
65 public xsalsa20poly1305(Byte[] sharedkey) : this(sharedkey, null) { } | |
66 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
|
67 if (sharedkey == null) throw new ArgumentNullException("secretkey"); |
20 | 68 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
|
69 sharedkey.CopyTo(this.sharedkey, 0); |
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
70 if (nonce != null) this.Nonce = nonce; |
20 | 71 } |
72 | |
73 public Byte[] Nonce { | |
74 get { return this.nonce; } | |
43
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
75 set { NonceValue = value; } |
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
76 } |
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
77 public Byte[] NonceValue { |
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
78 get { return ArrayUtil.ToArray(nonce); } |
20 | 79 set { |
80 if (ReferenceEquals(value, null)) throw new ArgumentNullException("value"); | |
81 if (value.Length > xsalsa20poly1305impl.NONCEBYTES) throw new ArgumentOutOfRangeException("value", "Nonce is too big"); | |
82 value.CopyTo(nonce, 0); | |
83 Array.Clear(this.nonce, value.Length, this.nonce.Length - value.Length); | |
84 } | |
85 } | |
43
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
86 public Byte[] NonceBuffer { |
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
87 get { return this.nonce; } |
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
88 set { |
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
89 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
|
90 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
|
91 this.nonce = value; |
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
92 } |
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
93 } |
20 | 94 |
95 public Byte[] SharedKey { | |
96 get { return sharedkey; } | |
97 } | |
98 | |
99 public unsafe Byte[] Encrypt(Byte[] data) { | |
100 return Encrypt(data, 0, data.Length); | |
101 } | |
102 public unsafe Byte[] Encrypt(Byte[] data, int offset, int count) { | |
103 if (ReferenceEquals(data, null)) throw new ArgumentNullException("data"); | |
104 if (offset < 0) throw new ArgumentOutOfRangeException("offset", "Offset can not be negative"); | |
105 if (data.Length < offset + count) throw new ArgumentOutOfRangeException("count", "The specified range is outside of the array"); | |
106 Byte[] ret = new Byte[GetEncryptedSize(count)]; | |
107 fixed (Byte* mp = data, cp = ret, np = nonce, kp = sharedkey) { | |
108 if (xsalsa20poly1305impl.crypto_secretbox_nopad(cp, mp + offset, (ulong)count, np, kp) != 0) throw new InvalidOperationException("Encryption failed"); | |
109 } | |
110 return ret; | |
111 } | |
112 public unsafe int EncryptTo(Byte[] data, int offset, int count, Byte[] outdata, int outoffset, int outcount) { | |
113 if (ReferenceEquals(data, null)) throw new ArgumentNullException("data"); | |
114 if (offset < 0) throw new ArgumentOutOfRangeException("offset", "Offset can not be negative"); | |
115 if (data.Length < offset + count) throw new ArgumentOutOfRangeException("count", "The specified range is outside of the array"); | |
116 if (ReferenceEquals(outdata, null)) throw new ArgumentNullException("outdata"); | |
117 if (outoffset < 0) throw new ArgumentOutOfRangeException("outoffset", "Offset can not be negative"); | |
118 if (outdata.Length < outoffset + outcount) throw new ArgumentOutOfRangeException("outcount", "The specified range is outside of the array"); | |
119 int retcount = GetEncryptedSize(count); | |
120 if (outcount < retcount) throw new ArgumentOutOfRangeException("outcount", "The output buffer is too small"); | |
121 fixed (Byte* mp = data, cp = outdata, np = nonce, kp = sharedkey) { | |
122 if (xsalsa20poly1305impl.crypto_secretbox_nopad(cp + outoffset, mp + offset, (ulong)count, np, kp) != 0) throw new InvalidOperationException("Encryption failed"); | |
123 } | |
124 return outcount; | |
125 } | |
126 /*public unsafe void EncryptInplace(Byte[] data, int offset, int count) { | |
127 if (ReferenceEquals(data, null)) throw new ArgumentNullException("data"); | |
128 if (offset < 0) throw new ArgumentOutOfRangeException("offset", "Offset can not be negative"); | |
129 if (data.Length < offset + count) throw new ArgumentOutOfRangeException("count", "The specified range is outside of the array"); | |
130 if (count < 16) throw new ArgumentOutOfRangeException("count", "count should be at least 16"); | |
131 fixed (Byte* mp = data, np = nonce, kp = sharedkey) { | |
132 if (xsalsa20poly1305impl.crypto_secretbox_inplace_nopad(mp + offset, (ulong)count, np, kp) != 0) throw new InvalidOperationException("Encryption failed"); | |
133 } | |
134 }*/ | |
135 public int GetEncryptedSize(int size) { | |
136 return size + 16; | |
137 } | |
138 | |
139 public unsafe Byte[] Decrypt(Byte[] data) { | |
140 return Decrypt(data, 0, data.Length); | |
141 } | |
142 public unsafe Byte[] Decrypt(Byte[] data, int offset, int count) { | |
143 if (ReferenceEquals(data, null)) throw new ArgumentNullException("data"); | |
144 if (offset < 0) throw new ArgumentOutOfRangeException("offset", "Offset can not be negative"); | |
145 if (data.Length < offset + count) throw new ArgumentOutOfRangeException("count", "The specified range is outside of the array"); | |
146 if (count < 16) return null; | |
147 Byte[] ret = new Byte[GetDecryptedSize(count)]; | |
148 fixed (Byte* cp = data, mp = ret, np = nonce, kp = sharedkey) { | |
149 if (xsalsa20poly1305impl.crypto_secretbox_open_nopad(mp, cp + offset, (ulong)count, np, kp) != 0) return null; | |
150 } | |
151 return ret; | |
152 } | |
153 public unsafe int? DecryptTo(Byte[] data, int offset, int count, Byte[] outdata, int outoffset, int outcount) { | |
154 if (ReferenceEquals(data, null)) throw new ArgumentNullException("data"); | |
155 if (offset < 0) throw new ArgumentOutOfRangeException("offset", "Offset can not be negative"); | |
156 if (data.Length < offset + count) throw new ArgumentOutOfRangeException("count", "The specified range is outside of the array"); | |
157 if (count < 16) return null; | |
158 if (ReferenceEquals(outdata, null)) throw new ArgumentNullException("outdata"); | |
159 if (outoffset < 0) throw new ArgumentOutOfRangeException("outoffset", "Offset can not be negative"); | |
160 if (outdata.Length < outoffset + outcount) throw new ArgumentOutOfRangeException("outcount", "The specified range is outside of the array"); | |
161 int retcount = GetDecryptedSize(count); | |
162 if (outcount < retcount) throw new ArgumentOutOfRangeException("outcount", "The output buffer is too small"); | |
163 fixed (Byte* cp = data, mp = outdata, np = nonce, kp = sharedkey) { | |
164 if (xsalsa20poly1305impl.crypto_secretbox_open_nopad(mp + outoffset, cp + offset, (ulong)count, np, kp) != 0) return null; | |
165 } | |
166 return retcount; | |
167 } | |
168 public unsafe ArraySegment<Byte>? DecryptInplace(Byte[] data, int offset, int count) { | |
169 if (ReferenceEquals(data, null)) throw new ArgumentNullException("data"); | |
170 if (offset < 0) throw new ArgumentOutOfRangeException("offset", "Offset can not be negative"); | |
171 if (data.Length < offset + count) throw new ArgumentOutOfRangeException("count", "The specified range is outside of the array"); | |
172 if (count < 16) return null; | |
173 fixed (Byte* cp = data, np = nonce, kp = sharedkey) { | |
174 if (xsalsa20poly1305impl.crypto_secretbox_open_inplace_nopad(cp + offset, (ulong)count, np, kp) != 0) return null; | |
175 } | |
176 return new ArraySegment<byte>(data, offset + 16, count - 16); | |
177 } | |
178 public int GetDecryptedSize(int size) { | |
179 if (size < 16) return -1; | |
180 return size - 16; | |
181 } | |
182 | |
183 public Boolean Verify(Byte[] data) { | |
184 return Verify(data, 0, data.Length); | |
185 } | |
186 public unsafe Boolean Verify(Byte[] data, int offset, int count) { | |
187 if (ReferenceEquals(data, null)) throw new ArgumentNullException("data"); | |
188 if (offset < 0) throw new ArgumentOutOfRangeException("offset", "Offset can not be negative"); | |
189 if (data.Length < offset + count) throw new ArgumentOutOfRangeException("count", "The specified range is outside of the array"); | |
190 if (count < 16) return false; | |
191 Byte[] ret = new Byte[GetDecryptedSize(count)]; | |
192 fixed (Byte* cp = data, np = nonce, kp = sharedkey) { | |
193 return xsalsa20poly1305impl.crypto_secretbox_verify(cp + offset, (ulong)count, np, kp); | |
194 } | |
195 } | |
196 | |
197 public Byte[] GenerateRandomNonce() { | |
198 randombytes.generate(nonce); | |
199 return nonce; | |
200 } | |
201 public void IncrementNonceLE() { | |
202 for (int i = 0; i < nonce.Length && ++nonce[i] == 0; i++) ; | |
203 } | |
204 public void IncrementNonceBE() { | |
205 for (int i = nonce.Length - 1; i >= 0 && ++nonce[i] == 0; i--) ; | |
206 } | |
207 | |
208 public xsalsa20poly1305 Clone() { | |
209 return new xsalsa20poly1305(sharedkey, nonce); | |
210 } | |
211 } | |
212 public class edwards25519sha512batch { | |
73
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
213 public static Byte[] Sign(Byte[] message, Byte[] secretkey) { |
20 | 214 return edwards25519sha512batchimpl.crypto_sign(message, secretkey); |
215 } | |
73
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
216 public static int GetSignedSize(int size) { |
20 | 217 return size + 64; |
218 } | |
73
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
219 public static Byte[] Open(Byte[] signed, Byte[] publickey) { |
20 | 220 return edwards25519sha512batchimpl.crypto_sign_open(signed, publickey); |
221 } | |
73
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
222 public static unsafe Boolean Verify(Byte[] signed, Byte[] publickey) { |
20 | 223 if (publickey.Length != edwards25519sha512batchimpl.PUBLICKEYBYTES) throw new ArgumentException("publickey.Length != PUBLICKEYBYTES"); |
224 UInt64 mlen; | |
225 fixed (Byte* smp = signed, pkp = publickey) return edwards25519sha512batchimpl.crypto_sign_open(null, out mlen, smp, (ulong)signed.Length, pkp) == 0; | |
226 } | |
73
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
227 public static Byte[] Extract(Byte[] signed) { |
20 | 228 if (signed.Length < 64) return null; |
229 Byte[] ret = new Byte[signed.Length - 64]; | |
230 Buffer.BlockCopy(signed, 32, ret, 0, ret.Length); | |
231 return ret; | |
232 } | |
73
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
233 public static int GetExtractedSize(int size) { |
20 | 234 if (size < 64) return -1; |
235 return size - 64; | |
236 } | |
237 } | |
73
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
238 public class sha512 { |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
239 sha512impl.sha512state state = new sha512impl.sha512state(); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
240 public sha512() { |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
241 state.init(); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
242 } |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
243 public unsafe void Process(Byte[] buffer, int offset, int count) { |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
244 if (offset < 0 || count < 0 || offset + count > buffer.Length) throw new ArgumentException("buffer"); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
245 fixed (Byte* p = buffer) state.process(p + offset, count); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
246 } |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
247 public unsafe void GetHash(Byte[] hash, int offset) { |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
248 if (offset < 0 || offset + 64 > hash.Length) throw new ArgumentException("hash"); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
249 fixed (Byte* p = hash) state.finish(p + offset); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
250 } |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
251 public unsafe Byte[] GetHash() { |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
252 Byte[] hash = new Byte[64]; |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
253 GetHash(hash, 0); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
254 return hash; |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
255 } |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
256 public static unsafe void GetHash(Byte[] buffer, int offset, int count, Byte[] hash, int hashoffset) { |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
257 if (offset < 0 || offset + count > buffer.Length) throw new ArgumentException("buffer"); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
258 if (offset < 0 || offset + 64 > hash.Length) throw new ArgumentException("hash"); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
259 sha512impl.sha512state state = new sha512impl.sha512state(); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
260 state.init(); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
261 fixed (Byte* p = buffer) state.process(p + offset, count); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
262 fixed (Byte* p = hash) state.finish(p + offset); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
263 } |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
264 public static unsafe Byte[] GetHash(Byte[] buffer, int offset, int count) { |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
265 Byte[] hash = new Byte[64]; |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
266 GetHash(buffer,offset,count,hash,0); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
267 return hash; |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
268 } |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
269 } |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
270 public class ed25519keypair { |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
271 internal Byte[] key; |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
272 |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
273 public ed25519keypair() { |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
274 Byte[] pk; |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
275 ed25519impl.crypto_sign_keypair(out pk, out key); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
276 } |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
277 public ed25519keypair(Byte[] key) { |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
278 if (key.Length == 64) { |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
279 this.key = ArrayUtil.ToArray(key); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
280 }else { |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
281 Byte[] pk; |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
282 ed25519impl.crypto_sign_seed_keypair(out pk, out this.key, key); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
283 } |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
284 } |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
285 public ed25519keypair(String key) : this(curve25519keypair.DecodeHexString(key, key.Length)) { } |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
286 public Byte[] PublicKey { get { return ArrayUtil.Slice(key, 32, 32); } } |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
287 public Byte[] SecretKey { get { return ArrayUtil.Slice(key, 0, 32); } } |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
288 public Byte[] ExpandedKey { get { return ArrayUtil.ToArray(key); } } |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
289 |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
290 public Byte[] GetSignature(Byte[] message) { |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
291 return ed25519.GetSignature(message, key); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
292 } |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
293 public Byte[] GetSignature(Byte[] message, int offset, int count) { |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
294 return ed25519.GetSignature(new ArraySegment<Byte>(message, offset, count), key); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
295 } |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
296 public Byte[] SignMessage(Byte[] message) { |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
297 return ed25519.SignMessage(message, key); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
298 } |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
299 } |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
300 public class ed25519 { |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
301 public static unsafe Byte[] GetSignature(Byte[] message, Byte[] key) { |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
302 if (message == null) throw new ArgumentNullException("message"); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
303 if (key.Length != 64) throw new ArgumentException("key"); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
304 Byte[] sig = new Byte[64]; |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
305 fixed (Byte* sigp = sig, msgp = message, kp = key) ed25519impl.crypto_getsignature(sigp, msgp, message.Length, kp); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
306 return sig; |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
307 } |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
308 public static unsafe Byte[] GetSignature(ArraySegment<Byte> message, Byte[] key) { |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
309 if (message == null) throw new ArgumentNullException("message"); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
310 if (key.Length != 64) throw new ArgumentException("key"); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
311 if (message.Offset < 0 || message.Count < 0 || message.Offset + message.Count > message.Array.Length) throw new ArgumentException("message"); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
312 Byte[] sig = new Byte[64]; |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
313 fixed (Byte* sigp = sig, msgp = message.Array, kp = key) ed25519impl.crypto_getsignature(sigp, msgp + message.Offset, message.Count, kp); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
314 return sig; |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
315 } |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
316 public static unsafe Byte[] SignMessage(Byte[] message, Byte[] key) { |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
317 if (key.Length != 64) throw new ArgumentException("key"); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
318 Byte[] ret = new Byte[message.Length + 64]; |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
319 int smlen; |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
320 fixed (Byte* sm = ret, msgp = message, kp = key) ed25519impl.crypto_sign(sm, out smlen, msgp, message.Length, kp); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
321 return ret; |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
322 } |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
323 public static unsafe Boolean VerifySignature(Byte[] message, Byte[] signature, Byte[] pk) { |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
324 if (signature.Length < 64) throw new ArgumentException("signature"); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
325 if (pk.Length < 32) throw new ArgumentException("pk"); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
326 fixed (Byte* sp = signature, mp = message, kp = pk) return ed25519impl.crypto_sign_verify(sp, mp, message.Length, kp); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
327 } |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
328 public static unsafe Boolean VerifySignature(ArraySegment<Byte> message, ArraySegment<Byte> signature, Byte[] pk) { |
80 | 329 if (signature.Offset < 0 || signature.Count < 64 || signature.Offset + signature.Count > signature.Array.Length) throw new ArgumentException("signature"); |
330 if (message.Offset < 0 || message.Count < 0 || message.Offset + message.Count > message.Array.Length) throw new ArgumentException("message"); | |
73
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
331 if (pk.Length < 32) throw new ArgumentException("pk"); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
332 fixed (Byte* sp = signature.Array, mp = message.Array, kp = pk) return ed25519impl.crypto_sign_verify(sp + signature.Offset, mp + message.Offset, message.Count, kp); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
333 } |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
334 public static unsafe Boolean VerifySignedMessage(Byte[] signedmessage, Byte[] pk) { |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
335 if (signedmessage.Length < 64) throw new ArgumentException("signedmessage"); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
336 if (pk.Length < 32) throw new ArgumentException("pk"); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
337 fixed (Byte* mp = signedmessage, kp = pk) return ed25519impl.crypto_sign_verify(mp, mp + 64, signedmessage.Length - 64, kp); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
338 } |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
339 public static Byte[] ExtractSignedMessage(Byte[] signedmessage) { |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
340 return ArrayUtil.Slice(signedmessage, 64); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
341 } |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
342 public static Byte[] ExtractSignedMessage(ArraySegment<Byte> signedmessage) { |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
343 return ArrayUtil.Slice(signedmessage.Array, signedmessage.Offset + 64, signedmessage.Count - 64); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
344 } |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
345 public static ArraySegment<Byte> ExtractSignedMessageFast(Byte[] signedmessage) { |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
346 return new ArraySegment<Byte>(signedmessage, 64, signedmessage.Length - 64); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
347 } |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
348 public static ArraySegment<Byte> ExtractSignedMessageFast(ArraySegment<Byte> signedmessage) { |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
349 return new ArraySegment<Byte>(signedmessage.Array, signedmessage.Offset + 64, signedmessage.Count - 64); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
350 } |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
351 public static Byte[] OpenSignedMessage(Byte[] signedmessage, Byte[] pk) { |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
352 if (!VerifySignedMessage(signedmessage, pk)) return null; |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
353 return ExtractSignedMessage(signedmessage); |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
354 } |
6aca18ee4ec6
NaCl: improved ed25519 implementation, added simple API for ed25519 and sha512
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
355 } |
20 | 356 } |