Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/DataProtection/Cryptography.Internal/src/CryptoUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,22 @@ public static bool TimeConstantBuffersAreEqual(byte* bufA, byte* bufB, uint coun
#endif
}

#if NET || NETSTANDARD2_1_OR_GREATER
// CryptographicOperations.ZeroMemory is itself [NoInlining | NoOptimization];
// inlining this wrapper just leaves a direct call
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
#endif
public static void ZeroMemory(byte[] buffer)
{
#if NET || NETSTANDARD2_1_OR_GREATER
CryptographicOperations.ZeroMemory(buffer);
#else
Array.Clear(buffer, 0, buffer.Length);
#endif
}

[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
public static bool TimeConstantBuffersAreEqual(ReadOnlySpan<byte> bufA, ReadOnlySpan<byte> bufB)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private static KeyedHashAlgorithm PrfToManagedHmacAlgorithm(KeyDerivationPrf prf
finally
{
// The HMAC ctor makes a duplicate of this key; we clear original buffer to limit exposure to the GC.
Array.Clear(passwordBytes, 0, passwordBytes.Length);
CryptoUtil.ZeroMemory(passwordBytes);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Xml.Linq;
using Microsoft.AspNetCore.Cryptography;

namespace Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel;

Expand Down Expand Up @@ -30,7 +31,7 @@ public static XElement ToMasterKeyElement(this ISecret secret)
}
finally
{
Array.Clear(unprotectedSecretRawBytes, 0, unprotectedSecretRawBytes.Length);
CryptoUtil.ZeroMemory(unprotectedSecretRawBytes);
}
}

Expand All @@ -56,7 +57,7 @@ public static Secret ToSecret(this string base64String)
}
finally
{
Array.Clear(unprotectedSecret, 0, unprotectedSecret.Length);
CryptoUtil.ZeroMemory(unprotectedSecret);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static byte[] ProtectWithDpapi(ISecret secret, bool protectToLocalMachine
finally
{
// To limit exposure to the GC.
Array.Clear(plaintextSecret, 0, plaintextSecret.Length);
CryptoUtil.ZeroMemory(plaintextSecret);
}
}
}
Expand Down Expand Up @@ -136,7 +136,7 @@ public static byte[] ProtectWithDpapiNG(ISecret secret, NCryptDescriptorHandle p
finally
{
// Limits secret exposure to garbage collector.
Array.Clear(plaintextSecret, 0, plaintextSecret.Length);
CryptoUtil.ZeroMemory(plaintextSecret);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,8 @@ public byte[] Encrypt(ArraySegment<byte> plaintext, ArraySegment<byte> additiona
}
finally
{
Array.Clear(keyModifier, 0, keyModifierLength);
Array.Clear(decryptedKdk, 0, decryptedKdk.Length);
CryptoUtil.ZeroMemory(keyModifier);
CryptoUtil.ZeroMemory(decryptedKdk);
}
}
}
Expand Down Expand Up @@ -453,7 +453,7 @@ private void CalculateAndValidateMac(
}
finally
{
Array.Clear(correctHashArray, 0, correctHashArray.Length);
CryptoUtil.ZeroMemory(correctHashArray);
}
}
#endif
Expand Down Expand Up @@ -573,7 +573,7 @@ public byte[] Decrypt(ArraySegment<byte> protectedPayload, ArraySegment<byte> ad
{
// delete since these contain secret material
validationSubkey.Clear();
Array.Clear(decryptedKdk, 0, decryptedKdk.Length);
CryptoUtil.ZeroMemory(decryptedKdk);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public static Secret ToSecret(this XElement element)
}
finally
{
Array.Clear(underlyingBuffer, 0, underlyingBuffer.Length);
CryptoUtil.ZeroMemory(underlyingBuffer);
}
}
}
Expand All @@ -194,7 +194,7 @@ public static XElement ToXElement(this Secret secret)
}
finally
{
Array.Clear(plaintextSecret, 0, plaintextSecret.Length);
CryptoUtil.ZeroMemory(plaintextSecret);
}
}
}
Expand Down
Loading