@@ -41,6 +41,7 @@ namespace Shadowsocks.Controller | |||||
{ | { | ||||
#if DEBUG | #if DEBUG | ||||
return; | return; | ||||
#pragma warning disable CS0162 // 检测到无法访问的代码 | |||||
#endif | #endif | ||||
CheckUpdateTimer timer = new CheckUpdateTimer(delay); | CheckUpdateTimer timer = new CheckUpdateTimer(delay); | ||||
timer.AutoReset = false; | timer.AutoReset = false; | ||||
@@ -29,19 +29,19 @@ namespace Shadowsocks.Encryption.AEAD | |||||
} | } | ||||
#endregion | #endregion | ||||
public override int CipherEncrypt(Span<byte> plain, Span<byte> cipher) | |||||
public override int CipherEncrypt(ReadOnlySpan<byte> plain, Span<byte> cipher) | |||||
{ | { | ||||
using AesGcm aes = new AesGcm(sessionKey); | using AesGcm aes = new AesGcm(sessionKey); | ||||
aes.Encrypt(nonce.AsSpan(), plain, cipher.Slice(0, plain.Length), cipher.Slice(plain.Length, tagLen)); | aes.Encrypt(nonce.AsSpan(), plain, cipher.Slice(0, plain.Length), cipher.Slice(plain.Length, tagLen)); | ||||
return plain.Length + tagLen; | return plain.Length + tagLen; | ||||
} | } | ||||
public override int CipherDecrypt(Span<byte> plain, Span<byte> cipher) | |||||
public override int CipherDecrypt(Span<byte> plain, ReadOnlySpan<byte> cipher) | |||||
{ | { | ||||
int clen = cipher.Length - tagLen; | int clen = cipher.Length - tagLen; | ||||
using AesGcm aes = new AesGcm(sessionKey); | using AesGcm aes = new AesGcm(sessionKey); | ||||
Span<byte> ciphertxt = cipher.Slice(0, clen); | |||||
Span<byte> tag = cipher.Slice(clen); | |||||
ReadOnlySpan<byte> ciphertxt = cipher.Slice(0, clen); | |||||
ReadOnlySpan<byte> tag = cipher.Slice(clen); | |||||
aes.Decrypt(nonce.AsSpan(), ciphertxt, tag, plain.Slice(0, clen)); | aes.Decrypt(nonce.AsSpan(), ciphertxt, tag, plain.Slice(0, clen)); | ||||
return clen; | return clen; | ||||
} | } | ||||
@@ -104,8 +104,8 @@ namespace Shadowsocks.Encryption.AEAD | |||||
DeriveSessionKey(salt, masterKey, sessionKey); | DeriveSessionKey(salt, masterKey, sessionKey); | ||||
} | } | ||||
public abstract int CipherEncrypt(Span<byte> plain, Span<byte> cipher); | |||||
public abstract int CipherDecrypt(Span<byte> plain, Span<byte> cipher); | |||||
public abstract int CipherEncrypt(ReadOnlySpan<byte> plain, Span<byte> cipher); | |||||
public abstract int CipherDecrypt(Span<byte> plain, ReadOnlySpan<byte> cipher); | |||||
#region TCP | #region TCP | ||||
@@ -328,7 +328,7 @@ namespace Shadowsocks.Encryption.AEAD | |||||
#endregion | #endregion | ||||
private int ChunkEncrypt(Span<byte> plain, Span<byte> cipher) | |||||
private int ChunkEncrypt(ReadOnlySpan<byte> plain, Span<byte> cipher) | |||||
{ | { | ||||
if (plain.Length > ChunkLengthMask) | if (plain.Length > ChunkLengthMask) | ||||
{ | { | ||||
@@ -344,5 +344,25 @@ namespace Shadowsocks.Encryption.AEAD | |||||
return cipherLenSize + cipherDataSize; | return cipherLenSize + cipherDataSize; | ||||
} | } | ||||
public override int Encrypt(ReadOnlySpan<byte> plain, Span<byte> cipher) | |||||
{ | |||||
throw new NotImplementedException(); | |||||
} | |||||
public override int Decrypt(Span<byte> plain, ReadOnlySpan<byte> cipher) | |||||
{ | |||||
throw new NotImplementedException(); | |||||
} | |||||
public override int EncryptUDP(ReadOnlySpan<byte> plain, Span<byte> cipher) | |||||
{ | |||||
throw new NotImplementedException(); | |||||
} | |||||
public override int DecryptUDP(Span<byte> plain, ReadOnlySpan<byte> cipher) | |||||
{ | |||||
throw new NotImplementedException(); | |||||
} | |||||
} | } | ||||
} | } |
@@ -46,14 +46,14 @@ namespace Shadowsocks.Encryption.AEAD | |||||
} | } | ||||
} | } | ||||
public override int CipherEncrypt(Span<byte> plain, Span<byte> cipher) | |||||
public override int CipherEncrypt(ReadOnlySpan<byte> plain, Span<byte> cipher) | |||||
{ | { | ||||
byte[] ct = enc.Encrypt(plain, null, nonce); | byte[] ct = enc.Encrypt(plain, null, nonce); | ||||
ct.CopyTo(cipher); | ct.CopyTo(cipher); | ||||
return ct.Length; | return ct.Length; | ||||
} | } | ||||
public override int CipherDecrypt(Span<byte> plain, Span<byte> cipher) | |||||
public override int CipherDecrypt(Span<byte> plain, ReadOnlySpan<byte> cipher) | |||||
{ | { | ||||
byte[] pt = enc.Decrypt(cipher, null, nonce); | byte[] pt = enc.Decrypt(cipher, null, nonce); | ||||
pt.CopyTo(plain); | pt.CopyTo(plain); | ||||
@@ -1,4 +1,6 @@ | |||||
namespace Shadowsocks.Encryption | |||||
using System; | |||||
namespace Shadowsocks.Encryption | |||||
{ | { | ||||
public abstract class EncryptorBase : IEncryptor | public abstract class EncryptorBase : IEncryptor | ||||
{ | { | ||||
@@ -44,6 +46,11 @@ | |||||
return $"{instanceId}({Method},{Password})"; | return $"{instanceId}({Method},{Password})"; | ||||
} | } | ||||
public abstract int Encrypt(ReadOnlySpan<byte> plain, Span<byte> cipher); | |||||
public abstract int Decrypt(Span<byte> plain, ReadOnlySpan<byte> cipher); | |||||
public abstract int EncryptUDP(ReadOnlySpan<byte> plain, Span<byte> cipher); | |||||
public abstract int DecryptUDP(Span<byte> plain, ReadOnlySpan<byte> cipher); | |||||
public int AddressBufferLength { get; set; } = -1; | public int AddressBufferLength { get; set; } = -1; | ||||
} | } | ||||
} | } |
@@ -11,5 +11,9 @@ namespace Shadowsocks.Encryption | |||||
void Decrypt(byte[] buf, int length, byte[] outbuf, out int outlength); | void Decrypt(byte[] buf, int length, byte[] outbuf, out int outlength); | ||||
void EncryptUDP(byte[] buf, int length, byte[] outbuf, out int outlength); | void EncryptUDP(byte[] buf, int length, byte[] outbuf, out int outlength); | ||||
void DecryptUDP(byte[] buf, int length, byte[] outbuf, out int outlength); | void DecryptUDP(byte[] buf, int length, byte[] outbuf, out int outlength); | ||||
int Encrypt(ReadOnlySpan<byte> plain, Span<byte> cipher); | |||||
int Decrypt(Span<byte> plain, ReadOnlySpan<byte> cipher); | |||||
int EncryptUDP(ReadOnlySpan<byte> plain, Span<byte> cipher); | |||||
int DecryptUDP(Span<byte> plain, ReadOnlySpan<byte> cipher); | |||||
} | } | ||||
} | } |
@@ -23,19 +23,19 @@ namespace Shadowsocks.Encryption.Stream | |||||
b.Init(isEncrypt, new ParametersWithIV(new KeyParameter(key), iv)); | b.Init(isEncrypt, new ParametersWithIV(new KeyParameter(key), iv)); | ||||
} | } | ||||
protected override int CipherEncrypt(Span<byte> plain, Span<byte> cipher) | |||||
protected override int CipherEncrypt(ReadOnlySpan<byte> plain, Span<byte> cipher) | |||||
{ | { | ||||
CipherUpdate(plain, cipher); | CipherUpdate(plain, cipher); | ||||
return plain.Length; | return plain.Length; | ||||
} | } | ||||
protected override int CipherDecrypt(Span<byte> plain, Span<byte> cipher) | |||||
protected override int CipherDecrypt(Span<byte> plain, ReadOnlySpan<byte> cipher) | |||||
{ | { | ||||
CipherUpdate(cipher, plain); | CipherUpdate(cipher, plain); | ||||
return cipher.Length; | return cipher.Length; | ||||
} | } | ||||
private void CipherUpdate(Span<byte> i, Span<byte> o) | |||||
private void CipherUpdate(ReadOnlySpan<byte> i, Span<byte> o) | |||||
{ | { | ||||
Span<byte> ob = new byte[o.Length + 128]; | Span<byte> ob = new byte[o.Length + 128]; | ||||
i.CopyTo(cfbBuf.AsSpan(ptr)); | i.CopyTo(cfbBuf.AsSpan(ptr)); | ||||
@@ -24,17 +24,17 @@ namespace Shadowsocks.Encryption.Stream | |||||
{ | { | ||||
} | } | ||||
protected override int CipherDecrypt(Span<byte> plain, Span<byte> cipher) | |||||
protected override int CipherDecrypt(Span<byte> plain, ReadOnlySpan<byte> cipher) | |||||
{ | { | ||||
return CipherUpdate(cipher, plain, false); | return CipherUpdate(cipher, plain, false); | ||||
} | } | ||||
protected override int CipherEncrypt(Span<byte> plain, Span<byte> cipher) | |||||
protected override int CipherEncrypt(ReadOnlySpan<byte> plain, Span<byte> cipher) | |||||
{ | { | ||||
return CipherUpdate(plain, cipher, true); | return CipherUpdate(plain, cipher, true); | ||||
} | } | ||||
private int CipherUpdate(Span<byte> i, Span<byte> o, bool enc) | |||||
private int CipherUpdate(ReadOnlySpan<byte> i, Span<byte> o, bool enc) | |||||
{ | { | ||||
int len = i.Length; | int len = i.Length; | ||||
int pad = remain; | int pad = remain; | ||||
@@ -85,8 +85,8 @@ namespace Shadowsocks.Encryption.Stream | |||||
Array.Copy(iv, this.iv, ivLen); | Array.Copy(iv, this.iv, ivLen); | ||||
} | } | ||||
protected abstract int CipherEncrypt(Span<byte> plain, Span<byte> cipher); | |||||
protected abstract int CipherDecrypt(Span<byte> plain, Span<byte> cipher); | |||||
protected abstract int CipherEncrypt(ReadOnlySpan<byte> plain, Span<byte> cipher); | |||||
protected abstract int CipherDecrypt(Span<byte> plain, ReadOnlySpan<byte> cipher); | |||||
#region TCP | #region TCP | ||||
[MethodImpl(MethodImplOptions.Synchronized)] | [MethodImpl(MethodImplOptions.Synchronized)] | ||||
@@ -193,5 +193,25 @@ namespace Shadowsocks.Encryption.Stream | |||||
} | } | ||||
#endregion | #endregion | ||||
public override int Encrypt(ReadOnlySpan<byte> plain, Span<byte> cipher) | |||||
{ | |||||
throw new NotImplementedException(); | |||||
} | |||||
public override int Decrypt(Span<byte> plain, ReadOnlySpan<byte> cipher) | |||||
{ | |||||
throw new NotImplementedException(); | |||||
} | |||||
public override int EncryptUDP(ReadOnlySpan<byte> plain, Span<byte> cipher) | |||||
{ | |||||
throw new NotImplementedException(); | |||||
} | |||||
public override int DecryptUDP(Span<byte> plain, ReadOnlySpan<byte> cipher) | |||||
{ | |||||
throw new NotImplementedException(); | |||||
} | |||||
} | } | ||||
} | } |
@@ -10,13 +10,13 @@ namespace Shadowsocks.Encryption.Stream | |||||
{ | { | ||||
} | } | ||||
protected override int CipherDecrypt(Span<byte> plain, Span<byte> cipher) | |||||
protected override int CipherDecrypt(Span<byte> plain, ReadOnlySpan<byte> cipher) | |||||
{ | { | ||||
cipher.CopyTo(plain); | cipher.CopyTo(plain); | ||||
return cipher.Length; | return cipher.Length; | ||||
} | } | ||||
protected override int CipherEncrypt(Span<byte> plain, Span<byte> cipher) | |||||
protected override int CipherEncrypt(ReadOnlySpan<byte> plain, Span<byte> cipher) | |||||
{ | { | ||||
plain.CopyTo(cipher); | plain.CopyTo(cipher); | ||||
return plain.Length; | return plain.Length; | ||||
@@ -29,17 +29,17 @@ namespace Shadowsocks.Encryption.Stream | |||||
sbox = SBox(realkey); | sbox = SBox(realkey); | ||||
} | } | ||||
protected override int CipherEncrypt(Span<byte> plain, Span<byte> cipher) | |||||
protected override int CipherEncrypt(ReadOnlySpan<byte> plain, Span<byte> cipher) | |||||
{ | { | ||||
return CipherUpdate(plain, cipher); | return CipherUpdate(plain, cipher); | ||||
} | } | ||||
protected override int CipherDecrypt(Span<byte> plain, Span<byte> cipher) | |||||
protected override int CipherDecrypt(Span<byte> plain, ReadOnlySpan<byte> cipher) | |||||
{ | { | ||||
return CipherUpdate(cipher, plain); | return CipherUpdate(cipher, plain); | ||||
} | } | ||||
private int CipherUpdate(Span<byte> i, Span<byte> o) | |||||
private int CipherUpdate(ReadOnlySpan<byte> i, Span<byte> o) | |||||
{ | { | ||||
// don't know why we need third array, but it works... | // don't know why we need third array, but it works... | ||||
Span<byte> t = new byte[i.Length]; | Span<byte> t = new byte[i.Length]; | ||||