|
1 |
| -using System; |
2 | 1 | using System.Runtime.InteropServices;
|
3 | 2 | using System.Security;
|
4 | 3 |
|
5 | 4 | namespace Discord.Audio
|
6 | 5 | {
|
7 | 6 | public unsafe static class SecretBox
|
8 | 7 | {
|
9 |
| - [DllImport("libsodium", EntryPoint = "crypto_secretbox_easy", CallingConvention = CallingConvention.Cdecl)] |
10 |
| - private static extern int SecretBoxEasy(byte* output, byte* input, long inputLength, byte[] nonce, byte[] secret); |
11 |
| - [DllImport("libsodium", EntryPoint = "crypto_secretbox_open_easy", CallingConvention = CallingConvention.Cdecl)] |
12 |
| - private static extern int SecretBoxOpenEasy(byte* output, byte* input, long inputLength, byte[] nonce, byte[] secret); |
| 8 | + [DllImport("libsodium", EntryPoint = "crypto_aead_xchacha20poly1305_ietf_encrypt", CallingConvention = CallingConvention.Cdecl)] |
| 9 | + private static extern int Encrypt(byte* ciphertext, out ulong ciphertextLength, byte* message, ulong messageLength, byte* ad, ulong adLength, byte* nsec, byte[] nonce, byte[] key); |
13 | 10 |
|
14 |
| - public static int Encrypt(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, byte[] nonce, byte[] secret) |
| 11 | + [DllImport("libsodium", EntryPoint = "crypto_aead_xchacha20poly1305_ietf_decrypt", CallingConvention = CallingConvention.Cdecl)] |
| 12 | + private static extern int Decrypt(byte* plaintext, out ulong plaintextLength, byte* nsec, byte* ciphertext, ulong ciphertextLength, byte* ad, ulong adLength, byte[] nonce, byte[] key); |
| 13 | + |
| 14 | + public static int Encrypt(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, byte[] header, byte[] nonce, byte[] key) |
15 | 15 | {
|
16 | 16 | fixed (byte* inPtr = input)
|
17 | 17 | fixed (byte* outPtr = output)
|
| 18 | + fixed (byte* adPtr = header) |
18 | 19 | {
|
19 |
| - int error = SecretBoxEasy(outPtr + outputOffset, inPtr + inputOffset, inputLength, nonce, secret); |
| 20 | + int error = Encrypt( |
| 21 | + outPtr + outputOffset, out ulong cipherLen, |
| 22 | + inPtr + inputOffset, (ulong)inputLength, |
| 23 | + adPtr, (ulong)header.Length, |
| 24 | + null, nonce, key |
| 25 | + ); |
| 26 | + |
20 | 27 | if (error != 0)
|
21 |
| - throw new SecurityException($"Sodium Error: {error}"); |
22 |
| - return inputLength + 16; |
| 28 | + throw new SecurityException($"Sodium AEAD Error: {error}"); |
| 29 | + |
| 30 | + return (int)cipherLen; |
23 | 31 | }
|
24 | 32 | }
|
25 |
| - public static int Decrypt(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, byte[] nonce, byte[] secret) |
| 33 | + |
| 34 | + public static int Decrypt(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, byte[] header, byte[] nonce, byte[] key) |
26 | 35 | {
|
27 | 36 | fixed (byte* inPtr = input)
|
28 | 37 | fixed (byte* outPtr = output)
|
| 38 | + fixed (byte* adPtr = header) |
29 | 39 | {
|
30 |
| - int error = SecretBoxOpenEasy(outPtr + outputOffset, inPtr + inputOffset, inputLength, nonce, secret); |
| 40 | + int error = Decrypt( |
| 41 | + outPtr + outputOffset, out ulong plainLen, |
| 42 | + null, |
| 43 | + inPtr + inputOffset, (ulong)inputLength, |
| 44 | + adPtr, (ulong)header.Length, |
| 45 | + nonce, key |
| 46 | + ); |
| 47 | + |
31 | 48 | if (error != 0)
|
32 |
| - throw new SecurityException($"Sodium Error: {error}"); |
33 |
| - return inputLength - 16; |
| 49 | + throw new SecurityException($"Sodium AEAD Decrypt Error: {error}"); |
| 50 | + |
| 51 | + return (int)plainLen; |
34 | 52 | }
|
35 | 53 | }
|
36 | 54 | }
|
|
0 commit comments