Skip to content

Commit

Permalink
prefer to use aes-256-gcm in libsodium
Browse files Browse the repository at this point in the history
which is hardware accelerated compared to AES-GCM in mbedtls

We have to drop this commit when mbedtls support hw-accelerated GCM
when compiling with MSVC

Signed-off-by: Syrone Wong <[email protected]>
  • Loading branch information
wongsyrone committed May 31, 2017
1 parent 816651e commit 5d75e19
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
Binary file modified shadowsocks-csharp/Data/libsscrypto.dll.gz
Binary file not shown.
16 changes: 16 additions & 0 deletions shadowsocks-csharp/Encryption/AEAD/AEADSodiumEncryptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class AEADSodiumEncryptor
: AEADEncryptor, IDisposable
{
private const int CIPHER_CHACHA20IETFPOLY1305 = 1;
private const int CIPHER_AES256GCM = 2;

private byte[] _sodiumEncSubkey;
private byte[] _sodiumDecSubkey;
Expand All @@ -24,6 +25,7 @@ public AEADSodiumEncryptor(string method, string password)
private static Dictionary<string, EncryptorInfo> _ciphers = new Dictionary<string, EncryptorInfo>
{
{"chacha20-ietf-poly1305", new EncryptorInfo(32, 32, 12, 16, CIPHER_CHACHA20IETFPOLY1305)},
{"aes-256-gcm", new EncryptorInfo(32, 32, 12, 16, CIPHER_AES256GCM)},
};

public static List<string> SupportedCiphers()
Expand Down Expand Up @@ -63,6 +65,13 @@ public override int cipherEncrypt(byte[] plaintext, uint plen, byte[] ciphertext
null, _encNonce,
_sodiumEncSubkey);
break;
case CIPHER_AES256GCM:
ret = Sodium.crypto_aead_aes256gcm_encrypt(ciphertext, ref encClen,
plaintext, (ulong)plen,
null, 0,
null, _encNonce,
_sodiumEncSubkey);
break;
default:
throw new System.Exception("not implemented");
}
Expand Down Expand Up @@ -91,6 +100,13 @@ public override int cipherDecrypt(byte[] ciphertext, uint clen, byte[] plaintext
null, 0,
_decNonce, _sodiumDecSubkey);
break;
case CIPHER_AES256GCM:
ret = Sodium.crypto_aead_aes256gcm_decrypt(plaintext, ref decPlen,
null,
ciphertext, (ulong)clen,
null, 0,
_decNonce, _sodiumDecSubkey);
break;
default:
throw new System.Exception("not implemented");
}
Expand Down
16 changes: 14 additions & 2 deletions shadowsocks-csharp/Encryption/EncryptorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ public static class EncryptorFactory

static EncryptorFactory()
{
var AEADMbedTLSEncryptorSupportedCiphers = AEADMbedTLSEncryptor.SupportedCiphers();
var AEADSodiumEncryptorSupportedCiphers = AEADSodiumEncryptor.SupportedCiphers();
if (Sodium.AES256GCMAvailable)
{
// prefer to aes-256-gcm in libsodium
AEADMbedTLSEncryptorSupportedCiphers.Remove("aes-256-gcm");
}
else
{
AEADSodiumEncryptorSupportedCiphers.Remove("aes-256-gcm");
}

foreach (string method in StreamMbedTLSEncryptor.SupportedCiphers())
{
_registeredEncryptors.Add(method, typeof(StreamMbedTLSEncryptor));
Expand All @@ -22,11 +34,11 @@ static EncryptorFactory()
{
_registeredEncryptors.Add(method, typeof(StreamSodiumEncryptor));
}
foreach (string method in AEADMbedTLSEncryptor.SupportedCiphers())
foreach (string method in AEADMbedTLSEncryptorSupportedCiphers)
{
_registeredEncryptors.Add(method, typeof(AEADMbedTLSEncryptor));
}
foreach (string method in AEADSodiumEncryptor.SupportedCiphers())
foreach (string method in AEADSodiumEncryptorSupportedCiphers)
{
_registeredEncryptors.Add(method, typeof(AEADSodiumEncryptor));
}
Expand Down
16 changes: 16 additions & 0 deletions shadowsocks-csharp/Encryption/Sodium.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public static class Sodium
private static bool _initialized = false;
private static readonly object _initLock = new object();

public static bool AES256GCMAvailable { get; private set; } = false;

static Sodium()
{
string dllPath = Utils.GetTempPath(DLLNAME);
Expand Down Expand Up @@ -42,6 +44,9 @@ static Sodium()
{
_initialized = true;
}

AES256GCMAvailable = crypto_aead_aes256gcm_is_available() == 1;
Logging.Debug($"sodium: AES256GCMAvailable is {AES256GCMAvailable}");
}
}
}
Expand All @@ -52,6 +57,9 @@ static Sodium()
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
private static extern int sodium_init();

[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
private static extern int crypto_aead_aes256gcm_is_available();

#region AEAD

[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
Expand All @@ -65,6 +73,14 @@ public static extern int crypto_aead_chacha20poly1305_ietf_encrypt(byte[] c, ref
public static extern int crypto_aead_chacha20poly1305_ietf_decrypt(byte[] m, ref ulong mlen_p,
byte[] nsec, byte[] c, ulong clen, byte[] ad, ulong adlen, byte[] npub, byte[] k);

[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern int crypto_aead_aes256gcm_encrypt(byte[] c, ref ulong clen_p, byte[] m, ulong mlen,
byte[] ad, ulong adlen, byte[] nsec, byte[] npub, byte[] k);

[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern int crypto_aead_aes256gcm_decrypt(byte[] m, ref ulong mlen_p, byte[] nsec, byte[] c,
ulong clen, byte[] ad, ulong adlen, byte[] npub, byte[] k);

#endregion

#region Stream
Expand Down

0 comments on commit 5d75e19

Please sign in to comment.