diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-10-04 19:17:38 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-10-04 19:17:38 +0700 |
commit | 83bc4307036193383a96e3002fb9696d66ce0f48 (patch) | |
tree | 714134b7872ece753180ceefe4de04851236e6de | |
parent | Fix warnings (diff) | |
download | BouncyCastle.NET-ed25519-83bc4307036193383a96e3002fb9696d66ce0f48.tar.xz |
Avoid some allocations around MPInteger
-rw-r--r-- | crypto/src/bcpg/BcpgInputStream.cs | 20 | ||||
-rw-r--r-- | crypto/src/bcpg/ECDHPublicBCPGKey.cs | 17 | ||||
-rw-r--r-- | crypto/src/bcpg/ECPublicBCPGKey.cs | 2 | ||||
-rw-r--r-- | crypto/src/bcpg/MPInteger.cs | 65 | ||||
-rw-r--r-- | crypto/src/openpgp/PgpEncryptedDataGenerator.cs | 2 |
5 files changed, 60 insertions, 46 deletions
diff --git a/crypto/src/bcpg/BcpgInputStream.cs b/crypto/src/bcpg/BcpgInputStream.cs index 5aa7ae306..7a19a90dd 100644 --- a/crypto/src/bcpg/BcpgInputStream.cs +++ b/crypto/src/bcpg/BcpgInputStream.cs @@ -62,22 +62,26 @@ namespace Org.BouncyCastle.Bcpg return Streams.ReadAll(this); } - public void ReadFully( - byte[] buffer, - int off, - int len) + public void ReadFully(byte[] buffer, int offset, int count) { - if (Streams.ReadFully(this, buffer, off, len) < len) + if (Streams.ReadFully(this, buffer, offset, count) < count) throw new EndOfStreamException(); } - public void ReadFully( - byte[] buffer) + public void ReadFully(byte[] buffer) { ReadFully(buffer, 0, buffer.Length); } - /// <summary>Returns the next packet tag in the stream.</summary> +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + public void ReadFully(Span<byte> buffer) + { + if (Streams.ReadFully(this, buffer) < buffer.Length) + throw new EndOfStreamException(); + } +#endif + + /// <summary>Returns the next packet tag in the stream.</summary> public PacketTag NextPacketTag() { if (!next) diff --git a/crypto/src/bcpg/ECDHPublicBCPGKey.cs b/crypto/src/bcpg/ECDHPublicBCPGKey.cs index dc225e31e..05c34ba8b 100644 --- a/crypto/src/bcpg/ECDHPublicBCPGKey.cs +++ b/crypto/src/bcpg/ECDHPublicBCPGKey.cs @@ -14,15 +14,18 @@ namespace Org.BouncyCastle.Bcpg private SymmetricKeyAlgorithmTag symAlgorithmId; /// <param name="bcpgIn">The stream to read the packet from.</param> - public ECDHPublicBcpgKey( - BcpgInputStream bcpgIn) + public ECDHPublicBcpgKey(BcpgInputStream bcpgIn) : base(bcpgIn) { int length = bcpgIn.ReadByte(); - byte[] kdfParameters = new byte[length]; - if (kdfParameters.Length != 3) - throw new InvalidOperationException("kdf parameters size of 3 expected."); + if (length != 3) + throw new InvalidOperationException("KDF parameters size of 3 expected."); +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + Span<byte> kdfParameters = stackalloc byte[length]; +#else + byte[] kdfParameters = new byte[length]; +#endif bcpgIn.ReadFully(kdfParameters); reserved = kdfParameters[0]; @@ -75,7 +78,7 @@ namespace Org.BouncyCastle.Bcpg private void VerifyHashAlgorithm() { - switch ((HashAlgorithmTag)hashFunctionId) + switch (hashFunctionId) { case HashAlgorithmTag.Sha256: case HashAlgorithmTag.Sha384: @@ -88,7 +91,7 @@ namespace Org.BouncyCastle.Bcpg private void VerifySymmetricKeyAlgorithm() { - switch ((SymmetricKeyAlgorithmTag)symAlgorithmId) + switch (symAlgorithmId) { case SymmetricKeyAlgorithmTag.Aes128: case SymmetricKeyAlgorithmTag.Aes192: diff --git a/crypto/src/bcpg/ECPublicBCPGKey.cs b/crypto/src/bcpg/ECPublicBCPGKey.cs index 4733ee6c9..560c6ad05 100644 --- a/crypto/src/bcpg/ECPublicBCPGKey.cs +++ b/crypto/src/bcpg/ECPublicBCPGKey.cs @@ -26,7 +26,7 @@ namespace Org.BouncyCastle.Bcpg DerObjectIdentifier oid, ECPoint point) { - this.point = new BigInteger(1, point.GetEncoded(false)); + this.point = MPInteger.ToMpiBigInteger(point); this.oid = oid; } diff --git a/crypto/src/bcpg/MPInteger.cs b/crypto/src/bcpg/MPInteger.cs index 441407244..2f564b00c 100644 --- a/crypto/src/bcpg/MPInteger.cs +++ b/crypto/src/bcpg/MPInteger.cs @@ -1,59 +1,66 @@ using System; -using System.IO; using Org.BouncyCastle.Math; +using Org.BouncyCastle.Math.EC; namespace Org.BouncyCastle.Bcpg { /// <remarks>A multiple precision integer</remarks> - public class MPInteger + public sealed class MPInteger : BcpgObject { - private readonly BigInteger val; + private readonly BigInteger m_val; - public MPInteger( - BcpgInputStream bcpgIn) + public MPInteger(BcpgInputStream bcpgIn) { if (bcpgIn == null) - throw new ArgumentNullException("bcpgIn"); + throw new ArgumentNullException(nameof(bcpgIn)); - int length = (bcpgIn.ReadByte() << 8) | bcpgIn.ReadByte(); - byte[] bytes = new byte[(length + 7) / 8]; + int lengthInBits = (bcpgIn.ReadByte() << 8) | bcpgIn.ReadByte(); + int lengthInBytes = (lengthInBits + 7) / 8; - bcpgIn.ReadFully(bytes); +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + if (lengthInBytes <= 512) + { + Span<byte> span = stackalloc byte[lengthInBytes]; + bcpgIn.ReadFully(span); + m_val = new BigInteger(1, span); + return; + } +#endif - this.val = new BigInteger(1, bytes); + byte[] bytes = new byte[lengthInBytes]; + bcpgIn.ReadFully(bytes); + m_val = new BigInteger(1, bytes); } - public MPInteger( - BigInteger val) + public MPInteger(BigInteger val) { if (val == null) - throw new ArgumentNullException("val"); + throw new ArgumentNullException(nameof(val)); if (val.SignValue < 0) - throw new ArgumentException("Values must be positive", "val"); + throw new ArgumentException("Values must be positive", nameof(val)); - this.val = val; + m_val = val; } - public BigInteger Value + public BigInteger Value => m_val; + + public override void Encode(BcpgOutputStream bcpgOut) { - get { return val; } + bcpgOut.WriteShort((short)m_val.BitLength); + bcpgOut.Write(m_val.ToByteArrayUnsigned()); } - public override void Encode( - BcpgOutputStream bcpgOut) + internal static BigInteger ToMpiBigInteger(ECPoint point) { - bcpgOut.WriteShort((short) val.BitLength); - bcpgOut.Write(val.ToByteArrayUnsigned()); +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + Span<byte> encoding = stackalloc byte[point.GetEncodedLength(false)]; + point.EncodeTo(false, encoding); + return new BigInteger(1, encoding); +#else + return new BigInteger(1, point.GetEncoded(false)); +#endif } - - internal static void Encode( - BcpgOutputStream bcpgOut, - BigInteger val) - { - bcpgOut.WriteShort((short) val.BitLength); - bcpgOut.Write(val.ToByteArrayUnsigned()); - } } } diff --git a/crypto/src/openpgp/PgpEncryptedDataGenerator.cs b/crypto/src/openpgp/PgpEncryptedDataGenerator.cs index f086aaf75..a86dce42d 100644 --- a/crypto/src/openpgp/PgpEncryptedDataGenerator.cs +++ b/crypto/src/openpgp/PgpEncryptedDataGenerator.cs @@ -146,7 +146,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp byte[] paddedSessionData = PgpPad.PadSessionData(sessionInfo, sessionKeyObfuscation); byte[] C = w.Wrap(paddedSessionData, 0, paddedSessionData.Length); - byte[] VB = new MPInteger(new BigInteger(1, ephPub.Q.GetEncoded(false))).GetEncoded(); + byte[] VB = new MPInteger(MPInteger.ToMpiBigInteger(ephPub.Q)).GetEncoded(); byte[] rv = new byte[VB.Length + 1 + C.Length]; |