diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2020-07-30 20:17:31 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2020-07-30 20:17:31 +0700 |
commit | 7c958e4e6a85239d7bc6a11fa60fbc68b6312b83 (patch) | |
tree | 04e202390351a47fd89af13dd03faaec96cf61c7 /crypto/src/openpgp/PgpPad.cs | |
parent | Encoder performance (diff) | |
download | BouncyCastle.NET-ed25519-7c958e4e6a85239d7bc6a11fa60fbc68b6312b83.tar.xz |
PGP updates from bc-java
Diffstat (limited to 'crypto/src/openpgp/PgpPad.cs')
-rw-r--r-- | crypto/src/openpgp/PgpPad.cs | 46 |
1 files changed, 33 insertions, 13 deletions
diff --git a/crypto/src/openpgp/PgpPad.cs b/crypto/src/openpgp/PgpPad.cs index 48f7f2f44..227e31019 100644 --- a/crypto/src/openpgp/PgpPad.cs +++ b/crypto/src/openpgp/PgpPad.cs @@ -11,35 +11,55 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp public static byte[] PadSessionData(byte[] sessionInfo) { - byte[] result = new byte[40]; - - Array.Copy(sessionInfo, 0, result, 0, sessionInfo.Length); + return PadSessionData(sessionInfo, true); + } - byte padValue = (byte)(result.Length - sessionInfo.Length); + public static byte[] PadSessionData(byte[] sessionInfo, bool obfuscate) + { + int length = sessionInfo.Length; + int paddedLength = ((length >> 3) + 1) << 3; - for (int i = sessionInfo.Length; i != result.Length; i++) + if (obfuscate) { - result[i] = padValue; + paddedLength = System.Math.Max(40, paddedLength); } + int padCount = paddedLength - length; + byte padByte = (byte)padCount; + + byte[] result = new byte[paddedLength]; + Array.Copy(sessionInfo, 0, result, 0, length); + for (int i = length; i < paddedLength; ++i) + { + result[i] = padByte; + } return result; } public static byte[] UnpadSessionData(byte[] encoded) { - byte padValue = encoded[encoded.Length - 1]; + int paddedLength = encoded.Length; + byte padByte = encoded[paddedLength - 1]; + int padCount = padByte; + int length = paddedLength - padCount; + int last = length - 1; - for (int i = encoded.Length - padValue; i != encoded.Length; i++) + int diff = 0; + for (int i = 0; i < paddedLength; ++i) { - if (encoded[i] != padValue) - throw new PgpException("bad padding found in session data"); + int mask = (last - i) >> 31; + diff |= (padByte ^ encoded[i]) & mask; } - byte[] taggedKey = new byte[encoded.Length - padValue]; + diff |= paddedLength & 7; + diff |= (40 - paddedLength) >> 31; - Array.Copy(encoded, 0, taggedKey, 0, taggedKey.Length); + if (diff != 0) + throw new PgpException("bad padding found in session data"); - return taggedKey; + byte[] result = new byte[length]; + Array.Copy(encoded, 0, result, 0, length); + return result; } } } |