Use ReadOnlySpan for RSA DefaultExponent
authorKevin Jones <kevin@vcsjones.com>
Fri, 27 Mar 2020 16:06:53 +0000 (12:06 -0400)
committerGitHub <noreply@github.com>
Fri, 27 Mar 2020 16:06:53 +0000 (09:06 -0700)
src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Bignum.cs
src/libraries/Common/src/System/Security/Cryptography/RSAOpenSsl.cs

index 02f5f13a5568629e77baf5acb4bf9cf408a687ef..d1f955bf759b8ec562599447c371486f8ae24864 100644 (file)
@@ -16,7 +16,7 @@ internal static partial class Interop
         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 Interop
         [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<byte> 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<byte> bigEndianValue)
         {
             IntPtr handle = CreateBignumPtr(bigEndianValue);
             return new SafeBignumHandle(handle, true);
index 8a13530008025087f99a3feae93c6d53464add17..fbe13834f94fd3ee8b4d879e3977d0098b860564 100644 (file)
@@ -27,7 +27,7 @@ namespace System.Security.Cryptography
         private const int BitsPerByte = 8;
 
         // 65537 (0x10001) in big-endian form
-        private static readonly byte[] s_defaultExponent = { 0x01, 0x00, 0x01 };
+        private static ReadOnlySpan<byte> DefaultExponent => new byte[] { 0x01, 0x00, 0x01 };
 
         private Lazy<SafeRsaHandle> _key;
 
@@ -593,7 +593,7 @@ namespace System.Security.Cryptography
 
             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