-
-
Notifications
You must be signed in to change notification settings - Fork 940
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use System.Security.Cryptography in DesCipher and TripleDesCipher
Falls back to use BouncyCastle if BCL doesn't support
- Loading branch information
Showing
24 changed files
with
1,387 additions
and
913 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,6 @@ | |
using Renci.SshNet.Security; | ||
using Renci.SshNet.Security.Cryptography; | ||
using Renci.SshNet.Security.Cryptography.Ciphers; | ||
using Renci.SshNet.Security.Cryptography.Ciphers.Modes; | ||
|
||
namespace Renci.SshNet | ||
{ | ||
|
@@ -363,16 +362,16 @@ public ConnectionInfo(string host, int port, string username, ProxyTypes proxyTy | |
|
||
Encryptions = new Dictionary<string, CipherInfo> | ||
{ | ||
{ "aes128-ctr", new CipherInfo(128, (key, iv) => new AesCipher(key, iv, AesCipherMode.CTR, pkcs7Padding: false)) }, | ||
{ "aes192-ctr", new CipherInfo(192, (key, iv) => new AesCipher(key, iv, AesCipherMode.CTR, pkcs7Padding: false)) }, | ||
{ "aes256-ctr", new CipherInfo(256, (key, iv) => new AesCipher(key, iv, AesCipherMode.CTR, pkcs7Padding: false)) }, | ||
{ "aes128-ctr", new CipherInfo(128, (key, iv) => new AesCipher(key, iv, BlockCipherMode.CTR, pkcs7Padding: false)) }, | ||
{ "aes192-ctr", new CipherInfo(192, (key, iv) => new AesCipher(key, iv, BlockCipherMode.CTR, pkcs7Padding: false)) }, | ||
{ "aes256-ctr", new CipherInfo(256, (key, iv) => new AesCipher(key, iv, BlockCipherMode.CTR, pkcs7Padding: false)) }, | ||
{ "[email protected]", new CipherInfo(128, (key, iv) => new AesGcmCipher(key, iv, aadLength: 4), isAead: true) }, | ||
{ "[email protected]", new CipherInfo(256, (key, iv) => new AesGcmCipher(key, iv, aadLength: 4), isAead: true) }, | ||
{ "[email protected]", new CipherInfo(512, (key, iv) => new ChaCha20Poly1305Cipher(key, aadLength: 4), isAead: true) }, | ||
{ "aes128-cbc", new CipherInfo(128, (key, iv) => new AesCipher(key, iv, AesCipherMode.CBC, pkcs7Padding: false)) }, | ||
{ "aes192-cbc", new CipherInfo(192, (key, iv) => new AesCipher(key, iv, AesCipherMode.CBC, pkcs7Padding: false)) }, | ||
{ "aes256-cbc", new CipherInfo(256, (key, iv) => new AesCipher(key, iv, AesCipherMode.CBC, pkcs7Padding: false)) }, | ||
{ "3des-cbc", new CipherInfo(192, (key, iv) => new TripleDesCipher(key, new CbcCipherMode(iv), padding: null)) }, | ||
{ "aes128-cbc", new CipherInfo(128, (key, iv) => new AesCipher(key, iv, BlockCipherMode.CBC, pkcs7Padding: false)) }, | ||
{ "aes192-cbc", new CipherInfo(192, (key, iv) => new AesCipher(key, iv, BlockCipherMode.CBC, pkcs7Padding: false)) }, | ||
{ "aes256-cbc", new CipherInfo(256, (key, iv) => new AesCipher(key, iv, BlockCipherMode.CBC, pkcs7Padding: false)) }, | ||
{ "3des-cbc", new CipherInfo(192, (key, iv) => new TripleDesCipher(key, iv, BlockCipherMode.CBC, pkcs7Padding: false)) }, | ||
}; | ||
|
||
HmacAlgorithms = new Dictionary<string, HashInfo> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,6 @@ | |
using Renci.SshNet.Security; | ||
using Renci.SshNet.Security.Cryptography; | ||
using Renci.SshNet.Security.Cryptography.Ciphers; | ||
using Renci.SshNet.Security.Cryptography.Ciphers.Modes; | ||
|
||
namespace Renci.SshNet | ||
{ | ||
|
@@ -91,25 +90,25 @@ public Key Parse() | |
{ | ||
case "3des-cbc": | ||
ivLength = 8; | ||
cipherInfo = new CipherInfo(192, (key, iv) => new TripleDesCipher(key, new CbcCipherMode(iv), padding: null)); | ||
cipherInfo = new CipherInfo(192, (key, iv) => new TripleDesCipher(key, iv, BlockCipherMode.CBC, pkcs7Padding: false)); | ||
break; | ||
case "aes128-cbc": | ||
cipherInfo = new CipherInfo(128, (key, iv) => new AesCipher(key, iv, AesCipherMode.CBC, pkcs7Padding: false)); | ||
cipherInfo = new CipherInfo(128, (key, iv) => new AesCipher(key, iv, BlockCipherMode.CBC, pkcs7Padding: false)); | ||
break; | ||
case "aes192-cbc": | ||
cipherInfo = new CipherInfo(192, (key, iv) => new AesCipher(key, iv, AesCipherMode.CBC, pkcs7Padding: false)); | ||
cipherInfo = new CipherInfo(192, (key, iv) => new AesCipher(key, iv, BlockCipherMode.CBC, pkcs7Padding: false)); | ||
break; | ||
case "aes256-cbc": | ||
cipherInfo = new CipherInfo(256, (key, iv) => new AesCipher(key, iv, AesCipherMode.CBC, pkcs7Padding: false)); | ||
cipherInfo = new CipherInfo(256, (key, iv) => new AesCipher(key, iv, BlockCipherMode.CBC, pkcs7Padding: false)); | ||
break; | ||
case "aes128-ctr": | ||
cipherInfo = new CipherInfo(128, (key, iv) => new AesCipher(key, iv, AesCipherMode.CTR, pkcs7Padding: false)); | ||
cipherInfo = new CipherInfo(128, (key, iv) => new AesCipher(key, iv, BlockCipherMode.CTR, pkcs7Padding: false)); | ||
break; | ||
case "aes192-ctr": | ||
cipherInfo = new CipherInfo(192, (key, iv) => new AesCipher(key, iv, AesCipherMode.CTR, pkcs7Padding: false)); | ||
cipherInfo = new CipherInfo(192, (key, iv) => new AesCipher(key, iv, BlockCipherMode.CTR, pkcs7Padding: false)); | ||
break; | ||
case "aes256-ctr": | ||
cipherInfo = new CipherInfo(256, (key, iv) => new AesCipher(key, iv, AesCipherMode.CTR, pkcs7Padding: false)); | ||
cipherInfo = new CipherInfo(256, (key, iv) => new AesCipher(key, iv, BlockCipherMode.CTR, pkcs7Padding: false)); | ||
break; | ||
case "[email protected]": | ||
cipherInfo = new CipherInfo(128, (key, iv) => new AesGcmCipher(key, iv, aadLength: 0), isAead: true); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...ity/Cryptography/Ciphers/AesCipherMode.cs → ...y/Cryptography/Ciphers/BlockCipherMode.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/Renci.SshNet/Security/Cryptography/Ciphers/DesCipher.BclImpl.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System; | ||
using System.Security.Cryptography; | ||
|
||
using Renci.SshNet.Common; | ||
|
||
namespace Renci.SshNet.Security.Cryptography.Ciphers | ||
{ | ||
public partial class DesCipher | ||
{ | ||
private sealed class BclImpl : BlockCipher, IDisposable | ||
{ | ||
private readonly DES _des; | ||
private readonly ICryptoTransform _encryptor; | ||
private readonly ICryptoTransform _decryptor; | ||
|
||
public BclImpl( | ||
byte[] key, | ||
byte[] iv, | ||
System.Security.Cryptography.CipherMode mode, | ||
PaddingMode padding) | ||
: base(key, 8, mode: null, padding: null) | ||
{ | ||
var des = DES.Create(); | ||
des.Key = Key; | ||
des.IV = iv.Take(8); | ||
des.Mode = mode; | ||
des.Padding = padding; | ||
_des = des; | ||
_encryptor = _des.CreateEncryptor(); | ||
_decryptor = _des.CreateDecryptor(); | ||
} | ||
|
||
public override byte[] Encrypt(byte[] input, int offset, int length) | ||
{ | ||
if (_des.Padding != PaddingMode.None) | ||
{ | ||
return _encryptor.TransformFinalBlock(input, offset, length); | ||
} | ||
|
||
var output = new byte[length]; | ||
_ = _encryptor.TransformBlock(input, offset, length, output, 0); | ||
|
||
return output; | ||
} | ||
|
||
public override byte[] Decrypt(byte[] input, int offset, int length) | ||
{ | ||
if (_des.Padding != PaddingMode.None) | ||
{ | ||
return _decryptor.TransformFinalBlock(input, offset, length); | ||
} | ||
|
||
var output = new byte[length]; | ||
_ = _decryptor.TransformBlock(input, offset, length, output, 0); | ||
|
||
return output; | ||
} | ||
|
||
public override int EncryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) | ||
{ | ||
throw new NotImplementedException($"Invalid usage of {nameof(EncryptBlock)}."); | ||
} | ||
|
||
public override int DecryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) | ||
{ | ||
throw new NotImplementedException($"Invalid usage of {nameof(DecryptBlock)}."); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_des.Dispose(); | ||
_encryptor.Dispose(); | ||
_decryptor.Dispose(); | ||
} | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/Renci.SshNet/Security/Cryptography/Ciphers/DesCipher.BouncyCastleImpl.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Org.BouncyCastle.Crypto.Engines; | ||
using Org.BouncyCastle.Crypto.Parameters; | ||
|
||
namespace Renci.SshNet.Security.Cryptography.Ciphers | ||
{ | ||
public partial class DesCipher | ||
{ | ||
private sealed class BouncyCastleImpl : BlockCipher | ||
{ | ||
private KeyParameter _parameter; | ||
private DesEngine _encryptor; | ||
private DesEngine _decryptor; | ||
|
||
public BouncyCastleImpl(byte[] key, CipherMode mode, CipherPadding padding) | ||
: base(key, 8, mode, padding) | ||
{ | ||
} | ||
|
||
public override int EncryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) | ||
{ | ||
if (_encryptor == null) | ||
{ | ||
_parameter ??= new KeyParameter(Key); | ||
_encryptor = new DesEngine(); | ||
_encryptor.Init(forEncryption: true, _parameter); | ||
} | ||
|
||
return _encryptor.ProcessBlock(inputBuffer, inputOffset, outputBuffer, outputOffset); | ||
} | ||
|
||
public override int DecryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) | ||
{ | ||
if (_decryptor == null) | ||
{ | ||
_parameter ??= new KeyParameter(Key); | ||
_decryptor = new DesEngine(); | ||
_decryptor.Init(forEncryption: false, _parameter); | ||
} | ||
|
||
return _decryptor.ProcessBlock(inputBuffer, inputOffset, outputBuffer, outputOffset); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.