Skip to content

Commit 47e324d

Browse files
committed
Drop DesCipher; Replace PKCS7Padding with BouncyCastle's implementation.
1 parent a7b10e5 commit 47e324d

22 files changed

+38
-896
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ The main types provided by this library are:
121121
Private keys in OpenSSL traditional PEM format can be encrypted using one of the following cipher methods:
122122
* DES-EDE3-CBC
123123
* DES-EDE3-CFB
124-
* DES-CBC
125124
* AES-128-CBC
126125
* AES-192-CBC
127126
* AES-256-CBC

src/Renci.SshNet/PrivateKeyFile.PKCS1.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ public Key Parse()
5454
case "DES-EDE3-CFB":
5555
cipher = new CipherInfo(192, (key, iv) => new TripleDesCipher(key, iv, BlockCipherMode.CFB, pkcs7Padding: false));
5656
break;
57-
case "DES-CBC":
58-
cipher = new CipherInfo(64, (key, iv) => new DesCipher(key, iv, BlockCipherMode.CBC, pkcs7Padding: true));
59-
break;
6057
case "AES-128-CBC":
6158
cipher = new CipherInfo(128, (key, iv) => new AesCipher(key, iv, BlockCipherMode.CBC, pkcs7Padding: true));
6259
break;

src/Renci.SshNet/PrivateKeyFile.PuTTY.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public Key Parse()
111111
throw new SshException("PuTTY key file version " + _version + " is not supported");
112112
}
113113

114-
using (var cipher = new AesCipher(cipherKey, cipherIV, AesCipherMode.CBC, pkcs7Padding: false))
114+
using (var cipher = new AesCipher(cipherKey, cipherIV, BlockCipherMode.CBC, pkcs7Padding: false))
115115
{
116116
privateKey = cipher.Decrypt(_data);
117117
}

src/Renci.SshNet/PrivateKeyFile.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ namespace Renci.SshNet
4848
/// <description>DES-EDE3-CFB</description>
4949
/// </item>
5050
/// <item>
51-
/// <description>DES-CBC</description>
52-
/// </item>
53-
/// <item>
5451
/// <description>AES-128-CBC</description>
5552
/// </item>
5653
/// <item>

src/Renci.SshNet/Security/Cryptography/BlockCipher.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22

3+
using Org.BouncyCastle.Crypto.Paddings;
4+
35
using Renci.SshNet.Common;
46
using Renci.SshNet.Security.Cryptography.Ciphers;
57
using Renci.SshNet.Security.Cryptography.Ciphers.Modes;
@@ -13,7 +15,7 @@ public abstract class BlockCipher : SymmetricCipher
1315
{
1416
private readonly CipherMode _mode;
1517

16-
private readonly CipherPadding _padding;
18+
private readonly IBlockCipherPadding _padding;
1719

1820
/// <summary>
1921
/// Gets the size of the block in bytes.
@@ -56,7 +58,7 @@ public byte BlockSize
5658
/// <param name="mode">Cipher mode.</param>
5759
/// <param name="padding">Cipher padding.</param>
5860
/// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception>
59-
protected BlockCipher(byte[] key, byte blockSize, CipherMode mode, CipherPadding padding)
61+
protected BlockCipher(byte[] key, byte blockSize, CipherMode mode, IBlockCipherPadding padding)
6062
: base(key)
6163
{
6264
_blockSize = blockSize;
@@ -81,7 +83,9 @@ public override byte[] Encrypt(byte[] input, int offset, int length)
8183
if (_padding is not null)
8284
{
8385
paddingLength = _blockSize - (length % _blockSize);
84-
input = _padding.Pad(input, offset, length, paddingLength);
86+
input = input.Take(offset, length);
87+
Array.Resize(ref input, length + paddingLength);
88+
_ = _padding.AddPadding(input, length);
8589
length += paddingLength;
8690
offset = 0;
8791
}

src/Renci.SshNet/Security/Cryptography/Ciphers/AesCipher.BlockImpl.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Security.Cryptography;
33

4+
using Org.BouncyCastle.Crypto.Paddings;
5+
46
namespace Renci.SshNet.Security.Cryptography.Ciphers
57
{
68
public partial class AesCipher
@@ -11,7 +13,7 @@ private sealed class BlockImpl : BlockCipher, IDisposable
1113
private readonly ICryptoTransform _encryptor;
1214
private readonly ICryptoTransform _decryptor;
1315

14-
public BlockImpl(byte[] key, CipherMode mode, CipherPadding padding)
16+
public BlockImpl(byte[] key, CipherMode mode, IBlockCipherPadding padding)
1517
: base(key, 16, mode, padding)
1618
{
1719
var aes = Aes.Create();

src/Renci.SshNet/Security/Cryptography/Ciphers/AesCipher.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using System;
22
using System.Security.Cryptography;
33

4+
using Org.BouncyCastle.Crypto.Paddings;
5+
46
using Renci.SshNet.Security.Cryptography.Ciphers.Modes;
5-
using Renci.SshNet.Security.Cryptography.Ciphers.Paddings;
67

78
namespace Renci.SshNet.Security.Cryptography.Ciphers
89
{
@@ -28,13 +29,13 @@ public AesCipher(byte[] key, byte[] iv, BlockCipherMode mode, bool pkcs7Padding
2829
if (mode == BlockCipherMode.OFB)
2930
{
3031
// OFB is not supported on modern .NET
31-
_impl = new BlockImpl(key, new OfbCipherMode(iv), pkcs7Padding ? new PKCS7Padding() : null);
32+
_impl = new BlockImpl(key, new OfbCipherMode(iv), pkcs7Padding ? new Pkcs7Padding() : null);
3233
}
3334
#if !NET6_0_OR_GREATER
3435
else if (mode == BlockCipherMode.CFB)
3536
{
3637
// CFB not supported on NetStandard 2.1
37-
_impl = new BlockImpl(key, new CfbCipherMode(iv), pkcs7Padding ? new PKCS7Padding() : null);
38+
_impl = new BlockImpl(key, new CfbCipherMode(iv), pkcs7Padding ? new Pkcs7Padding() : null);
3839
}
3940
#endif
4041
else if (mode == BlockCipherMode.CTR)

src/Renci.SshNet/Security/Cryptography/Ciphers/CipherPadding.cs

Lines changed: 0 additions & 65 deletions
This file was deleted.

src/Renci.SshNet/Security/Cryptography/Ciphers/DesCipher.BclImpl.cs

Lines changed: 0 additions & 89 deletions
This file was deleted.

src/Renci.SshNet/Security/Cryptography/Ciphers/DesCipher.BouncyCastleImpl.cs

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/Renci.SshNet/Security/Cryptography/Ciphers/DesCipher.cs

Lines changed: 0 additions & 71 deletions
This file was deleted.

0 commit comments

Comments
 (0)