Skip to content

Commit

Permalink
add xchacha20 and xsalsa20 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Akkariiin committed Sep 18, 2017
1 parent 42a95b2 commit 8f5a4de
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions shadowsocks-csharp/Encryption/Sodium.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,15 @@ static Sodium()
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public extern static void crypto_stream_salsa20_xor_ic(byte[] c, byte[] m, ulong mlen, byte[] n, ulong ic, byte[] k);

[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public extern static void crypto_stream_xsalsa20_xor_ic(byte[] c, byte[] m, ulong mlen, byte[] n, ulong ic, byte[] k);

[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public extern static void crypto_stream_chacha20_xor_ic(byte[] c, byte[] m, ulong mlen, byte[] n, ulong ic, byte[] k);

[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public extern static void crypto_stream_xchacha20_xor_ic(byte[] c, byte[] m, ulong mlen, byte[] n, ulong ic, byte[] k);

[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public extern static int crypto_stream_chacha20_ietf_xor_ic(byte[] c, byte[] m, ulong mlen, byte[] n, uint ic, byte[] k);
}
Expand Down
10 changes: 10 additions & 0 deletions shadowsocks-csharp/Encryption/SodiumEncryptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class SodiumEncryptor
const int CIPHER_SALSA20 = 1;
const int CIPHER_CHACHA20 = 2;
const int CIPHER_CHACHA20_IETF = 3;
const int CIPHER_XSALSA20 = 4 + 1;
const int CIPHER_XCHACHA20 = 4 + 2;

const int SODIUM_BLOCK_SIZE = 64;

Expand Down Expand Up @@ -37,6 +39,12 @@ public SodiumEncryptor(string method, string password)
case CIPHER_CHACHA20:
encryptor_delegate = Sodium.crypto_stream_chacha20_xor_ic;
break;
case CIPHER_XSALSA20:
encryptor_delegate = Sodium.crypto_stream_xsalsa20_xor_ic;
break;
case CIPHER_XCHACHA20:
encryptor_delegate = Sodium.crypto_stream_xchacha20_xor_ic;
break;
case CIPHER_CHACHA20_IETF:
encryptor_delegate = crypto_stream_chacha20_ietf_xor_ic;
break;
Expand All @@ -46,6 +54,8 @@ public SodiumEncryptor(string method, string password)
private static Dictionary<string, EncryptorInfo> _ciphers = new Dictionary<string, EncryptorInfo> {
{"salsa20", new EncryptorInfo(32, 8, true, CIPHER_SALSA20)},
{"chacha20", new EncryptorInfo(32, 8, true, CIPHER_CHACHA20)},
{"xsalsa20", new EncryptorInfo(32, 24, true, CIPHER_XSALSA20)},
{"xchacha20", new EncryptorInfo(32, 24, true, CIPHER_XCHACHA20)},
{"chacha20-ietf", new EncryptorInfo(32, 12, true, CIPHER_CHACHA20_IETF)},
};

Expand Down

0 comments on commit 8f5a4de

Please sign in to comment.