Clean up more byte packing/unpacking. (#40334)
authorKevin Jones <kevin@vcsjones.com>
Wed, 5 Aug 2020 05:01:13 +0000 (01:01 -0400)
committerGitHub <noreply@github.com>
Wed, 5 Aug 2020 05:01:13 +0000 (05:01 +0000)
src/libraries/Common/src/System/Security/Cryptography/DSACng.ImportExport.cs
src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/Helpers.cs
src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/PKCS1MaskGenerationMethod.cs
src/libraries/System.Security.Cryptography.Csp/src/System/Security/Cryptography/RSACryptoServiceProvider.Windows.cs

index 57e6261..61ec4f3 100644 (file)
@@ -3,6 +3,7 @@
 
 #nullable enable
 using Internal.Cryptography;
+using System.Buffers.Binary;
 using System.Diagnostics;
 
 using static Interop.BCrypt;
@@ -359,7 +360,7 @@ namespace System.Security.Cryptography
                             offset = sizeof(KeyBlobMagicNumber) + sizeof(int); // skip Magic and cbKey
 
                             // Read out a (V1) BCRYPT_DSA_KEY_BLOB structure.
-                            dsaParams.Counter = FromBigEndian(Interop.BCrypt.Consume(dsaBlob, ref offset, 4));
+                            dsaParams.Counter = BinaryPrimitives.ReadInt32BigEndian(Interop.BCrypt.Consume(dsaBlob, ref offset, 4));
                             dsaParams.Seed = Interop.BCrypt.Consume(dsaBlob, ref offset, Sha1HashOutputSize);
                             dsaParams.Q = Interop.BCrypt.Consume(dsaBlob, ref offset, Sha1HashOutputSize);
 
@@ -393,7 +394,7 @@ namespace System.Security.Cryptography
                             offset = sizeof(BCRYPT_DSA_KEY_BLOB_V2) - 4; //skip to Count[4]
 
                             // Read out a BCRYPT_DSA_KEY_BLOB_V2 structure.
-                            dsaParams.Counter = FromBigEndian(Interop.BCrypt.Consume(dsaBlob, ref offset, 4));
+                            dsaParams.Counter = BinaryPrimitives.ReadInt32BigEndian(Interop.BCrypt.Consume(dsaBlob, ref offset, 4));
 
                             Debug.Assert(offset == sizeof(BCRYPT_DSA_KEY_BLOB_V2), $"Expected offset = sizeof(BCRYPT_DSA_KEY_BLOB_V2), got {offset} != {sizeof(BCRYPT_DSA_KEY_BLOB_V2)}");
 
@@ -422,11 +423,6 @@ namespace System.Security.Cryptography
                 }
             }
 
-            private static int FromBigEndian(byte[] b)
-            {
-                return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3];
-            }
-
             /// <summary>
             ///     This function checks the magic value in the key blob header
             /// </summary>
index 8854940..dc900c0 100644 (file)
@@ -58,15 +58,5 @@ namespace Internal.Cryptography
             }
             return oddParityKey;
         }
-
-        internal static void ConvertIntToByteArray(uint value, byte[] dest)
-        {
-            Debug.Assert(dest != null);
-            Debug.Assert(dest.Length == 4);
-            dest[0] = (byte)((value & 0xFF000000) >> 24);
-            dest[1] = (byte)((value & 0xFF0000) >> 16);
-            dest[2] = (byte)((value & 0xFF00) >> 8);
-            dest[3] = (byte)(value & 0xFF);
-        }
     }
 }
index 6688025..4e24f7e 100644 (file)
@@ -1,6 +1,7 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
+using System.Buffers.Binary;
 using System.Diagnostics;
 using Internal.Cryptography;
 
@@ -38,7 +39,7 @@ namespace System.Security.Cryptography
                 for (int ib = 0; ib < rgbT.Length;)
                 {
                     //  Increment counter -- up to 2^32 * sizeof(Hash)
-                    Helpers.ConvertIntToByteArray(counter++, rgbCounter);
+                    BinaryPrimitives.WriteUInt32BigEndian(rgbCounter, counter++);
                     hasher.TransformBlock(rgbSeed, 0, rgbSeed.Length, rgbSeed, 0);
                     hasher.TransformFinalBlock(rgbCounter, 0, 4);
                     Debug.Assert(hasher.Hash != null);
index 7a22b93..e0a8cce 100644 (file)
@@ -1,6 +1,7 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
+using System.Buffers.Binary;
 using System.Diagnostics;
 using System.IO;
 using System.Runtime.Versioning;
@@ -195,7 +196,7 @@ namespace System.Security.Cryptography
             get
             {
                 byte[] keySize = CapiHelper.GetKeyParameter(SafeKeyHandle, Constants.CLR_KEYLEN);
-                _keySize = (keySize[0] | (keySize[1] << 8) | (keySize[2] << 16) | (keySize[3] << 24));
+                _keySize = BinaryPrimitives.ReadInt32LittleEndian(keySize);
                 return _keySize;
             }
         }