diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-11-17 00:10:42 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-11-17 00:10:42 +0700 |
commit | bc9d472a78f0780ddde00f524a07a39c9fccdf5b (patch) | |
tree | 72c5051ebd169e61ab87a5caa96d2f6e07a8de62 /crypto/src/math/ec/rfc7748 | |
parent | PackageValidationBaselineVersion = 2.0.0 (diff) | |
download | BouncyCastle.NET-ed25519-bc9d472a78f0780ddde00f524a07a39c9fccdf5b.tar.xz |
EdDSA improvements
- better guards on context values - add Verify method to public keys - reduced allocation during verification
Diffstat (limited to 'crypto/src/math/ec/rfc7748')
-rw-r--r-- | crypto/src/math/ec/rfc7748/X25519Field.cs | 22 | ||||
-rw-r--r-- | crypto/src/math/ec/rfc7748/X448Field.cs | 20 |
2 files changed, 24 insertions, 18 deletions
diff --git a/crypto/src/math/ec/rfc7748/X25519Field.cs b/crypto/src/math/ec/rfc7748/X25519Field.cs index cddf03faa..241710fe9 100644 --- a/crypto/src/math/ec/rfc7748/X25519Field.cs +++ b/crypto/src/math/ec/rfc7748/X25519Field.cs @@ -1,4 +1,7 @@ using System; +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER +using System.Buffers.Binary; +#endif using System.Diagnostics; #if NETSTANDARD1_0_OR_GREATER || NETCOREAPP1_0_OR_GREATER using System.Runtime.CompilerServices; @@ -388,21 +391,21 @@ namespace Org.BouncyCastle.Math.EC.Rfc7748 private static uint Decode32(byte[] bs, int off) { +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + return BinaryPrimitives.ReadUInt32LittleEndian(bs.AsSpan(off)); +#else uint n = bs[off]; n |= (uint)bs[++off] << 8; n |= (uint)bs[++off] << 16; n |= (uint)bs[++off] << 24; return n; +#endif } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER private static uint Decode32(ReadOnlySpan<byte> bs) { - uint n = bs[0]; - n |= (uint)bs[1] << 8; - n |= (uint)bs[2] << 16; - n |= (uint)bs[3] << 24; - return n; + return BinaryPrimitives.ReadUInt32LittleEndian(bs); } #endif @@ -485,19 +488,20 @@ namespace Org.BouncyCastle.Math.EC.Rfc7748 private static void Encode32(uint n, byte[] bs, int off) { +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + BinaryPrimitives.WriteUInt32LittleEndian(bs.AsSpan(off), n); +#else bs[ off] = (byte)(n ); bs[++off] = (byte)(n >> 8); bs[++off] = (byte)(n >> 16); bs[++off] = (byte)(n >> 24); +#endif } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER private static void Encode32(uint n, Span<byte> bs) { - bs[0] = (byte)(n ); - bs[1] = (byte)(n >> 8); - bs[2] = (byte)(n >> 16); - bs[3] = (byte)(n >> 24); + BinaryPrimitives.WriteUInt32LittleEndian(bs, n); } #endif diff --git a/crypto/src/math/ec/rfc7748/X448Field.cs b/crypto/src/math/ec/rfc7748/X448Field.cs index 67e71afa8..1df837d3a 100644 --- a/crypto/src/math/ec/rfc7748/X448Field.cs +++ b/crypto/src/math/ec/rfc7748/X448Field.cs @@ -4,6 +4,7 @@ using System.Diagnostics; using System.Runtime.CompilerServices; #endif #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER +using System.Buffers.Binary; using System.Numerics; #endif #if NETCOREAPP3_0_OR_GREATER @@ -318,21 +319,21 @@ namespace Org.BouncyCastle.Math.EC.Rfc7748 private static uint Decode32(byte[] bs, int off) { +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + return BinaryPrimitives.ReadUInt32LittleEndian(bs.AsSpan(off)); +#else uint n = bs[off]; n |= (uint)bs[++off] << 8; n |= (uint)bs[++off] << 16; n |= (uint)bs[++off] << 24; return n; +#endif } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER private static uint Decode32(ReadOnlySpan<byte> bs) { - uint n = bs[0]; - n |= (uint)bs[1] << 8; - n |= (uint)bs[2] << 16; - n |= (uint)bs[3] << 24; - return n; + return BinaryPrimitives.ReadUInt32LittleEndian(bs); } #endif @@ -442,19 +443,20 @@ namespace Org.BouncyCastle.Math.EC.Rfc7748 private static void Encode32(uint n, byte[] bs, int off) { +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + BinaryPrimitives.WriteUInt32LittleEndian(bs.AsSpan(off), n); +#else bs[ off] = (byte)(n ); bs[++off] = (byte)(n >> 8); bs[++off] = (byte)(n >> 16); bs[++off] = (byte)(n >> 24); +#endif } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER private static void Encode32(uint n, Span<byte> bs) { - bs[0] = (byte)(n ); - bs[1] = (byte)(n >> 8); - bs[2] = (byte)(n >> 16); - bs[3] = (byte)(n >> 24); + BinaryPrimitives.WriteUInt32LittleEndian(bs, n); } #endif |