From 2d28fafa7fe1becdada43f939b5121946468052c Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Thu, 6 Oct 2022 12:19:49 +0700 Subject: Refactor stackalloc usage --- crypto/src/math/ec/ECAlgorithms.cs | 5 ++++- crypto/src/math/ec/rfc8032/Ed25519.cs | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) (limited to 'crypto/src/math/ec') diff --git a/crypto/src/math/ec/ECAlgorithms.cs b/crypto/src/math/ec/ECAlgorithms.cs index fcfab06f7..3059ca3b3 100644 --- a/crypto/src/math/ec/ECAlgorithms.cs +++ b/crypto/src/math/ec/ECAlgorithms.cs @@ -216,7 +216,10 @@ namespace Org.BouncyCastle.Math.EC throw new ArgumentException("Point must be on the same curve", nameof(p)); #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER - Span encoding = stackalloc byte[p.GetEncodedLength(false)]; + int encodedLength = p.GetEncodedLength(false); + Span encoding = encodedLength <= 512 + ? stackalloc byte[encodedLength] + : new byte[encodedLength]; p.EncodeTo(false, encoding); return c.DecodePoint(encoding); #else diff --git a/crypto/src/math/ec/rfc8032/Ed25519.cs b/crypto/src/math/ec/rfc8032/Ed25519.cs index d6bf461cf..f3b63f3b3 100644 --- a/crypto/src/math/ec/rfc8032/Ed25519.cs +++ b/crypto/src/math/ec/rfc8032/Ed25519.cs @@ -433,7 +433,10 @@ namespace Org.BouncyCastle.Math.EC.Rfc8032 public static void GeneratePublicKey(ReadOnlySpan sk, Span pk) { IDigest d = CreateDigest(); - Span h = stackalloc byte[d.GetDigestSize()]; + int digestSize = d.GetDigestSize(); + Span h = digestSize <= 128 + ? stackalloc byte[digestSize] + : new byte[digestSize]; d.BlockUpdate(sk[..SecretKeySize]); d.DoFinal(h); -- cgit 1.5.1