· 6 years ago · Sep 06, 2019, 12:58 PM
1using System;
2using System.Runtime.InteropServices;
3using System.Text;
4
5// ref: https://github.com/jedisct1/libsodium/tree/master/src/libsodium/include/sodium/
6// ref: https://github.com/joshjdevl/libsodium-jni/blob/master/src/main/java/org/libsodium/jni/SodiumJNI.java
7// ref: https://github.com/adamcaudill/libsodium-net/blob/master/libsodium-net/SodiumLibrary.cs
8
9
10namespace unity.libsodium
11{
12 // crypto_generichash_state
13 [StructLayout(LayoutKind.Sequential, Size = 384)]
14 internal struct _HashState
15 {
16 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
17 public ulong[] h;
18
19 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
20 public ulong[] t;
21
22 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
23 public ulong[] f;
24
25 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
26 public byte[] buf;
27
28 public uint buflen;
29
30 public byte last_node;
31 }
32
33 /// <summary>
34 /// Native libsodium wrapper for unity
35 /// Functions are documented at https://download.libsodium.org/doc/
36 /// </summary>
37 public static class NativeLibsodium
38 {
39#if UNITY_IOS && !UNITY_EDITOR
40 const string DLL_NAME = "__Internal";
41#else
42 private const string DLL_NAME = "sodium";
43#endif
44
45 [DllImport(DLL_NAME)]
46 public static extern int sodium_init();
47
48 [DllImport(DLL_NAME)]
49 public static extern void randombytes_buf(byte[] buffer, int size);
50
51 [DllImport(DLL_NAME)]
52 public static extern int randombytes_uniform(int upperBound);
53
54 [DllImport(DLL_NAME)]
55 public static extern void sodium_increment(byte[] buffer, UIntPtr length);
56
57 [DllImport(DLL_NAME)]
58 public static extern int sodium_compare(byte[] a, byte[] b, UIntPtr length);
59
60 [DllImport(DLL_NAME)]
61 public static extern IntPtr sodium_version_string();
62
63 [DllImport(DLL_NAME)]
64 public static extern int crypto_hash(byte[] buffer, byte[] message, UIntPtr length);
65
66 [DllImport(DLL_NAME)]
67 public static extern int crypto_hash_sha512(byte[] buffer, byte[] message, UIntPtr length);
68
69 [DllImport(DLL_NAME)]
70 public static extern int crypto_hash_sha256(byte[] buffer, byte[] message, UIntPtr length);
71
72 [DllImport(DLL_NAME)]
73 public static extern int crypto_generichash(byte[] buffer, int bufferLength, byte[] message, long messageLength,
74 byte[] key, int keyLength);
75
76 [DllImport(DLL_NAME)]
77 public static extern int crypto_generichash_blake2b_salt_personal(byte[] buffer, int bufferLength,
78 byte[] message, long messageLength, byte[] key, int keyLength, byte[] salt, byte[] personal);
79
80 [DllImport(DLL_NAME)]
81 public static extern int crypto_onetimeauth(byte[] buffer, byte[] message, long messageLength, byte[] key);
82
83 [DllImport(DLL_NAME)]
84 public static extern int crypto_onetimeauth_verify(byte[] signature, byte[] message, long messageLength,
85 byte[] key);
86
87 [DllImport(DLL_NAME)]
88 public static extern int crypto_pwhash_str(byte[] buffer, byte[] password, long passwordLen, long opsLimit,
89 int memLimit);
90
91 [DllImport(DLL_NAME)]
92 public static extern int crypto_pwhash_str_verify(byte[] buffer, byte[] password, long passLength);
93
94 [DllImport(DLL_NAME)]
95 public static extern int crypto_pwhash(byte[] buffer, long bufferLen, byte[] password, long passwordLen,
96 byte[] salt, long opsLimit, int memLimit, int alg);
97
98 [DllImport(DLL_NAME)]
99 public static extern int crypto_pwhash_scryptsalsa208sha256_str(byte[] buffer, byte[] password,
100 long passwordLen, long opsLimit, int memLimit);
101
102 [DllImport(DLL_NAME)]
103 public static extern int crypto_pwhash_scryptsalsa208sha256(byte[] buffer, long bufferLen, byte[] password,
104 long passwordLen, byte[] salt, long opsLimit, int memLimit);
105
106 [DllImport(DLL_NAME)]
107 public static extern int crypto_pwhash_scryptsalsa208sha256_str_verify(byte[] buffer, byte[] password,
108 long passLength);
109
110 [DllImport(DLL_NAME)]
111 public static extern int crypto_sign_keypair(byte[] publicKey, byte[] secretKey);
112
113 [DllImport(DLL_NAME)]
114 public static extern int crypto_sign_seed_keypair(byte[] publicKey, byte[] secretKey, byte[] seed);
115
116 [DllImport(DLL_NAME)]
117 public static extern int crypto_sign(byte[] buffer, ref long bufferLength, byte[] message, long messageLength,
118 byte[] key);
119
120 [DllImport(DLL_NAME)]
121 public static extern int crypto_sign_open(byte[] buffer, ref long bufferLength, byte[] signedMessage,
122 long signedMessageLength, byte[] key);
123
124 [DllImport(DLL_NAME)]
125 public static extern int crypto_sign_detached(byte[] signature, ref long signatureLength, byte[] message,
126 long messageLength, byte[] key);
127
128 [DllImport(DLL_NAME)]
129 public static extern int crypto_sign_verify_detached(byte[] signature, byte[] message, long messageLength,
130 byte[] key);
131
132 [DllImport(DLL_NAME)]
133 public static extern int crypto_sign_ed25519_sk_to_seed(byte[] seed, byte[] secretKey);
134
135 [DllImport(DLL_NAME)]
136 public static extern int crypto_sign_ed25519_sk_to_pk(byte[] publicKey, byte[] secretKey);
137
138 [DllImport(DLL_NAME)]
139 public static extern int crypto_sign_ed25519_pk_to_curve25519(byte[] curve25519Pk, byte[] ed25519Pk);
140
141 [DllImport(DLL_NAME)]
142 public static extern int crypto_sign_ed25519_sk_to_curve25519(byte[] curve25519Sk, byte[] ed25519Sk);
143
144 [DllImport(DLL_NAME)]
145 public static extern int crypto_box_keypair(byte[] publicKey, byte[] secretKey);
146
147 [DllImport(DLL_NAME)]
148 public static extern int crypto_box_easy(byte[] buffer, byte[] message, long messageLength, byte[] nonce,
149 byte[] publicKey, byte[] secretKey);
150
151 [DllImport(DLL_NAME)]
152 public static extern int crypto_box_open_easy(byte[] buffer, byte[] cipherText, long cipherTextLength,
153 byte[] nonce, byte[] publicKey, byte[] secretKey);
154
155 [DllImport(DLL_NAME)]
156 public static extern int crypto_box_detached(byte[] cipher, byte[] mac, byte[] message, long messageLength,
157 byte[] nonce, byte[] pk, byte[] sk);
158
159 [DllImport(DLL_NAME)]
160 public static extern int crypto_box_open_detached(byte[] buffer, byte[] cipherText, byte[] mac,
161 long cipherTextLength, byte[] nonce, byte[] pk, byte[] sk);
162
163 [DllImport(DLL_NAME)]
164 public static extern int crypto_scalarmult_bytes();
165
166 [DllImport(DLL_NAME)]
167 public static extern int crypto_scalarmult_scalarbytes();
168
169 [DllImport(DLL_NAME)]
170 public static extern byte crypto_scalarmult_primitive();
171
172 [DllImport(DLL_NAME)]
173 public static extern int crypto_scalarmult_base(byte[] q, byte[] n);
174
175 [DllImport(DLL_NAME)]
176 public static extern int crypto_scalarmult(byte[] q, byte[] n, byte[] p);
177
178 [DllImport(DLL_NAME)]
179 public static extern int crypto_box_seal(byte[] buffer, byte[] message, long messageLength, byte[] pk);
180
181 [DllImport(DLL_NAME)]
182 public static extern int crypto_box_seal_open(byte[] buffer, byte[] cipherText, long cipherTextLength,
183 byte[] pk, byte[] sk);
184
185 [DllImport(DLL_NAME)]
186 public static extern int crypto_secretbox_easy(byte[] buffer, byte[] message, long messageLength, byte[] nonce,
187 byte[] key);
188
189 [DllImport(DLL_NAME)]
190 public static extern int crypto_secretbox_open_easy(byte[] buffer, byte[] cipherText, long cipherTextLength,
191 byte[] nonce, byte[] key);
192
193 [DllImport(DLL_NAME)]
194 public static extern int crypto_secretbox_detached(byte[] cipher, byte[] mac, byte[] message,
195 long messageLength, byte[] nonce, byte[] key);
196
197 [DllImport(DLL_NAME)]
198 public static extern int crypto_secretbox_open_detached(byte[] buffer, byte[] cipherText, byte[] mac,
199 long cipherTextLength, byte[] nonce, byte[] key);
200
201 [DllImport(DLL_NAME)]
202 public static extern int crypto_auth(byte[] buffer, byte[] message, long messageLength, byte[] key);
203
204 [DllImport(DLL_NAME)]
205 public static extern int crypto_auth_verify(byte[] signature, byte[] message, long messageLength, byte[] key);
206
207 [DllImport(DLL_NAME)]
208 public static extern int crypto_auth_hmacsha256(byte[] buffer, byte[] message, long messageLength, byte[] key);
209
210 [DllImport(DLL_NAME)]
211 public static extern int crypto_auth_hmacsha256_verify(byte[] signature, byte[] message, long messageLength,
212 byte[] key);
213
214 [DllImport(DLL_NAME)]
215 public static extern int crypto_auth_hmacsha512(byte[] signature, byte[] message, long messageLength,
216 byte[] key);
217
218 [DllImport(DLL_NAME)]
219 public static extern int crypto_auth_hmacsha512_verify(byte[] signature, byte[] message, long messageLength,
220 byte[] key);
221
222 [DllImport(DLL_NAME)]
223 public static extern int crypto_shorthash(byte[] buffer, byte[] message, long messageLength, byte[] key);
224
225 [DllImport(DLL_NAME)]
226 public static extern int crypto_stream_xor(byte[] buffer, byte[] message, long messageLength, byte[] nonce,
227 byte[] key);
228
229 [DllImport(DLL_NAME)]
230 public static extern int crypto_stream_chacha20_xor(byte[] buffer, byte[] message, long messageLength,
231 byte[] nonce, byte[] key);
232
233 [DllImport(DLL_NAME)]
234 public static extern IntPtr sodium_bin2hex(byte[] hex, int hexMaxlen, byte[] bin, int binLen);
235
236 [DllImport(DLL_NAME)]
237 public static extern int sodium_hex2bin(IntPtr bin, int binMaxlen, string hex, int hexLen, string ignore,
238 out int binLen, string hexEnd);
239
240 [DllImport(DLL_NAME)]
241 public static extern int crypto_aead_chacha20poly1305_encrypt(
242 IntPtr cipher, out long cipherLength, byte[] message, long messageLength, byte[] additionalData,
243 long additionalDataLength, byte[] nsec, byte[] nonce, byte[] key);
244
245 [DllImport(DLL_NAME)]
246 public static extern int crypto_aead_chacha20poly1305_decrypt(
247 IntPtr message, out long messageLength, byte[] nsec, byte[] cipher, long cipherLength,
248 byte[] additionalData,
249 long additionalDataLength, byte[] nonce, byte[] key);
250
251 [DllImport(DLL_NAME)]
252 public static extern int crypto_aead_aes256gcm_is_available();
253
254 [DllImport(DLL_NAME)]
255 public static extern int crypto_aead_aes256gcm_encrypt(
256 IntPtr cipher, out long cipherLength, byte[] message, long messageLength, byte[] additionalData,
257 long additionalDataLength, byte[] nsec, byte[] nonce, byte[] key);
258
259 [DllImport(DLL_NAME)]
260 public static extern int crypto_aead_aes256gcm_decrypt(
261 IntPtr message, out long messageLength, byte[] nsec, byte[] cipher, long cipherLength,
262 byte[] additionalData,
263 long additionalDataLength, byte[] nonce, byte[] key);
264
265 [DllImport(DLL_NAME)]
266 public static extern int crypto_generichash_init(IntPtr state, byte[] key, int keySize, int hashSize);
267
268 [DllImport(DLL_NAME)]
269 public static extern int crypto_generichash_update(IntPtr state, byte[] message, long messageLength);
270
271 [DllImport(DLL_NAME)]
272 public static extern int crypto_generichash_final(IntPtr state, byte[] buffer, int bufferLength);
273
274 [DllImport(DLL_NAME)]
275 public static extern int crypto_stream_chacha20_xor_ic(byte[] c, byte[] m, ulong mlen, byte[] n, ulong ic,
276 byte[] k);
277
278 [DllImport(DLL_NAME)]
279 public static extern int crypto_stream_chacha20_xor_ic_offset(byte[] c, ulong c_offset, byte[] m,
280 ulong m_offset, ulong mlen, byte[] n, ulong ic, byte[] k);
281
282 [DllImport(DLL_NAME)]
283 public static extern int crypto_stream_aes128ctr_xor(byte[] c, byte[] m, ulong inlen, byte[] n, byte[] k);
284
285 [DllImport(DLL_NAME)]
286 public static extern int crypto_stream_aes128ctr_beforenm(byte[] tbl, byte[] k);
287
288 [DllImport(DLL_NAME)]
289 public static extern int crypto_stream_aes128ctr_xor_afternm(byte[] c, byte[] m, ulong len, byte[] nonce,
290 byte[] tbl);
291
292 [DllImport(DLL_NAME)]
293 public static extern int crypto_stream_aes128ctr_xor_afternm_offset(byte[] c, ulong c_offset, byte[] m,
294 ulong m_offset, ulong len, byte[] nonce, byte[] tbl);
295
296 [DllImport(DLL_NAME)]
297 public static extern int crypto_kx_seed_keypair(byte[] publicKey, byte[] secretKey, byte[] seed);
298
299 [DllImport(DLL_NAME)]
300 public static extern int crypto_kx_keypair(byte[] publicKey, byte[] secretKey);
301
302 [DllImport(DLL_NAME)]
303 public static extern int crypto_kx_client_session_keys(byte[] rx, byte[] tx, byte[] client_pk, byte[] client_sk,
304 byte[] server_pk);
305
306 [DllImport(DLL_NAME)]
307 public static extern int crypto_kx_server_session_keys(byte[] rx, byte[] tx, byte[] server_pk, byte[] server_sk,
308 byte[] client_pk);
309 }
310
311}