diff --git a/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Bignum.cs b/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Bignum.cs index 02f5f13a556862..d1f955bf759b8e 100644 --- a/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Bignum.cs +++ b/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Bignum.cs @@ -16,7 +16,7 @@ internal static partial class Crypto internal static extern void BigNumDestroy(IntPtr a); [DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_BigNumFromBinary")] - private static extern IntPtr BigNumFromBinary(byte[] s, int len); + private static extern unsafe IntPtr BigNumFromBinary(byte* s, int len); [DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_BigNumToBinary")] private static extern unsafe int BigNumToBinary(SafeBignumHandle a, byte* to); @@ -24,24 +24,22 @@ internal static partial class Crypto [DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_GetBigNumBytes")] private static extern int GetBigNumBytes(SafeBignumHandle a); - private static IntPtr CreateBignumPtr(byte[] bigEndianValue) + private static unsafe IntPtr CreateBignumPtr(ReadOnlySpan bigEndianValue) { - if (bigEndianValue == null) + fixed (byte* pBigEndianValue = bigEndianValue) { - return IntPtr.Zero; - } + IntPtr ret = BigNumFromBinary(pBigEndianValue, bigEndianValue.Length); - IntPtr ret = BigNumFromBinary(bigEndianValue, bigEndianValue.Length); + if (ret == IntPtr.Zero) + { + throw CreateOpenSslCryptographicException(); + } - if (ret == IntPtr.Zero) - { - throw CreateOpenSslCryptographicException(); + return ret; } - - return ret; } - internal static SafeBignumHandle CreateBignum(byte[] bigEndianValue) + internal static SafeBignumHandle CreateBignum(ReadOnlySpan bigEndianValue) { IntPtr handle = CreateBignumPtr(bigEndianValue); return new SafeBignumHandle(handle, true); diff --git a/src/libraries/Common/src/System/Security/Cryptography/RSAOpenSsl.cs b/src/libraries/Common/src/System/Security/Cryptography/RSAOpenSsl.cs index 8a135300080250..fbe13834f94fd3 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/RSAOpenSsl.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/RSAOpenSsl.cs @@ -27,7 +27,7 @@ public sealed partial class RSAOpenSsl : RSA private const int BitsPerByte = 8; // 65537 (0x10001) in big-endian form - private static readonly byte[] s_defaultExponent = { 0x01, 0x00, 0x01 }; + private static ReadOnlySpan DefaultExponent => new byte[] { 0x01, 0x00, 0x01 }; private Lazy _key; @@ -593,7 +593,7 @@ private SafeRsaHandle GenerateKey() try { - using (SafeBignumHandle exponent = Interop.Crypto.CreateBignum(s_defaultExponent)) + using (SafeBignumHandle exponent = Interop.Crypto.CreateBignum(DefaultExponent)) { // The documentation for RSA_generate_key_ex does not say that it returns only // 0 or 1, so the call marshals it back as a full Int32 and checks for a value