Skip to content

Commit 0f98ada

Browse files
committed
Borrow PadCount method from BouncyCastle
1 parent 47cfe86 commit 0f98ada

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

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

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

3-
using Org.BouncyCastle.Crypto.Paddings;
4-
53
using Renci.SshNet.Common;
64
using Renci.SshNet.Security.Cryptography.Ciphers;
75
using Renci.SshNet.Security.Cryptography.Ciphers.Modes;
8-
using Renci.SshNet.Security.Cryptography.Ciphers.Paddings;
96

107
namespace Renci.SshNet.Security.Cryptography
118
{
@@ -180,9 +177,9 @@ public override byte[] Decrypt(byte[] input, int offset, int length)
180177
throw new InvalidOperationException("Encryption error.");
181178
}
182179

183-
if (_padding is PKCS7Padding)
180+
if (_padding is not null)
184181
{
185-
paddingLength = new Pkcs7Padding().PadCount(output);
182+
paddingLength = _padding.PadCount(output);
186183
}
187184

188185
if (paddingLength > 0)

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,12 @@ public byte[] Pad(byte[] input, int paddinglength)
5454
/// The padded data array.
5555
/// </returns>
5656
public abstract byte[] Pad(byte[] input, int offset, int length, int paddinglength);
57+
58+
/// <summary>
59+
/// Gets the padd count from the specified input.
60+
/// </summary>
61+
/// <param name="input">The input.</param>
62+
/// <returns>The padd count.</returns>
63+
public abstract int PadCount(byte[] input);
5764
}
5865
}

src/Renci.SshNet/Security/Cryptography/Ciphers/Paddings/PKCS7Padding.cs

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

3+
using Renci.SshNet.Common;
4+
35
namespace Renci.SshNet.Security.Cryptography.Ciphers.Paddings
46
{
57
/// <summary>
@@ -45,5 +47,26 @@ public override byte[] Pad(byte[] input, int offset, int length, int paddingleng
4547

4648
return output;
4749
}
50+
51+
/// <inheritdoc/>
52+
public override int PadCount(byte[] input)
53+
{
54+
var padValue = input[input.Length - 1];
55+
int count = padValue;
56+
var position = input.Length - count;
57+
58+
var failed = (position | (count - 1)) >> 31;
59+
for (var i = 0; i < input.Length; ++i)
60+
{
61+
failed |= (input[i] ^ padValue) & ~((i - position) >> 31);
62+
}
63+
64+
if (failed != 0)
65+
{
66+
throw new SshException("pad block corrupted");
67+
}
68+
69+
return count;
70+
}
4871
}
4972
}

0 commit comments

Comments
 (0)