Browse Source

buggy chacha20-ietf

pull/2865/head
Student Main 5 years ago
parent
commit
9675f80749
3 changed files with 72 additions and 0 deletions
  1. +8
    -0
      shadowsocks-csharp/Encryption/EncryptorFactory.cs
  2. +59
    -0
      shadowsocks-csharp/Encryption/Stream/StreamChachaNaClEncryptor.cs
  3. +5
    -0
      test/CryptographyTest.cs

+ 8
- 0
shadowsocks-csharp/Encryption/EncryptorFactory.cs View File

@@ -41,6 +41,14 @@ namespace Shadowsocks.Encryption
_registeredEncryptors.Add(method.Key, typeof(StreamAesBouncyCastleEncryptor));
}
}
foreach (var method in StreamChachaNaClEncryptor.SupportedCiphers())
{
if (!_registeredEncryptors.ContainsKey(method.Key))
{
ciphers.Add(method.Key, method.Value);
_registeredEncryptors.Add(method.Key, typeof(StreamChachaNaClEncryptor));
}
}
foreach (var method in AEADAesGcmNativeEncryptor.SupportedCiphers())


+ 59
- 0
shadowsocks-csharp/Encryption/Stream/StreamChachaNaClEncryptor.cs View File

@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Text;
using NaCl.Core;

namespace Shadowsocks.Encryption.Stream
{
public class StreamChachaNaClEncryptor : StreamEncryptor
{
ChaCha20 c;
public StreamChachaNaClEncryptor(string method, string password) : base(method, password)
{
c = new ChaCha20(key, 0);
}

protected override int CipherDecrypt(Span<byte> plain, Span<byte> cipher)
{
var p = c.Decrypt(cipher, iv);
p.CopyTo(plain);
return p.Length;
}

protected override int CipherEncrypt(Span<byte> plain, Span<byte> cipher)
{
var e = c.Encrypt(plain, iv);
e.CopyTo(cipher);
return e.Length;
}

protected override void cipherUpdate(bool isEncrypt, int length, byte[] buf, byte[] outbuf)
{
var i = buf.AsSpan().Slice(0, length);
if (isEncrypt)
{
CipherEncrypt(i, outbuf);
}
else
{
CipherDecrypt(outbuf, i);
}
}

#region Ciphers
private static readonly Dictionary<string, CipherInfo> _ciphers = new Dictionary<string, CipherInfo>
{
{ "chacha20-ietf", new CipherInfo("chacha20-ietf", 32, 12, CipherFamily.Chacha20) },
};
public static Dictionary<string, CipherInfo> SupportedCiphers()
{
return _ciphers;
}

protected override Dictionary<string, CipherInfo> getCiphers()
{
return _ciphers;
}
#endregion
}
}

+ 5
- 0
test/CryptographyTest.cs View File

@@ -165,5 +165,10 @@ namespace Shadowsocks.Test
{
TestEncryptionMethod(typeof(StreamAesBouncyCastleEncryptor), "aes-256-cfb");
}
[TestMethod]
public void TestStreamChachaNaClEncryption()
{
TestEncryptionMethod(typeof(StreamChachaNaClEncryptor), "chacha20-ietf");
}
}
}

Loading…
Cancel
Save