This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FixedTimeEquals and other crypto helper routines as public API
- Loading branch information
Showing
19 changed files
with
494 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...urity.Cryptography.Primitives/src/System/Security/Cryptography/CryptographicOperations.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Runtime.CompilerServices; | ||
|
||
namespace System.Security.Cryptography | ||
{ | ||
public static class CryptographicOperations | ||
{ | ||
/// <summary> | ||
/// Determine the equality of two byte sequences in an amount of time which depends on | ||
/// the length of the sequences, but not the values. | ||
/// </summary> | ||
/// <param name="left">The first buffer to compare.</param> | ||
/// <param name="right">The second buffer to compare.</param> | ||
/// <returns> | ||
/// <c>true</c> if <paramref name="left"/> and <paramref name="right"/> have the same | ||
/// values for <see cref="ReadOnlySpan{T}.Length"/> and the same contents, <c>false</c> | ||
/// otherwise. | ||
/// </returns> | ||
/// <remarks> | ||
/// This method compares two buffers' contents for equality in a manner which does not | ||
/// leak timing information, making it ideal for use within cryptographic routines. | ||
/// This method will short-circuit and return <c>false</c> only if <paramref name="left"/> | ||
/// and <paramref name="right"/> have different lengths. | ||
/// | ||
/// Fixed-time behavior is guaranteed in all other cases, including if <paramref name="left"/> | ||
/// and <paramref name="right"/> reference the same address. | ||
/// </remarks> | ||
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)] | ||
public static bool FixedTimeEquals(ReadOnlySpan<byte> left, ReadOnlySpan<byte> right) | ||
{ | ||
// NoOptimization because we want this method to be exactly as non-short-circuiting | ||
// as written. | ||
// | ||
// NoInlining because the NoOptimization would get lost if the method got inlined. | ||
|
||
if (left.Length != right.Length) | ||
{ | ||
return false; | ||
} | ||
|
||
int length = left.Length; | ||
int accum = 0; | ||
|
||
for (int i = 0; i < length; i++) | ||
{ | ||
accum |= left[i] - right[i]; | ||
} | ||
|
||
return accum == 0; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)] | ||
public static void ZeroMemory(Span<byte> buffer) | ||
{ | ||
// NoOptimize to prevent the optimizer from deciding this call is unnecessary | ||
// NoInlining to prevent the inliner from forgetting that the method was no-optimize | ||
buffer.Clear(); | ||
} | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
src/System.Security.Cryptography.Primitives/tests/FixedTimeEqualsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Buffers; | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using Xunit; | ||
|
||
namespace System.Security.Cryptography.Primitives.Tests | ||
{ | ||
public static class FixedTimeEqualsTests | ||
{ | ||
[Theory] | ||
[InlineData(0)] | ||
[InlineData(1)] | ||
[InlineData(128 / 8)] | ||
[InlineData(256 / 8)] | ||
[InlineData(512 / 8)] | ||
[InlineData(96)] | ||
[InlineData(1024)] | ||
public static void EqualReturnsTrue(int byteLength) | ||
{ | ||
byte[] rented = ArrayPool<byte>.Shared.Rent(byteLength); | ||
Span<byte> testSpan = new Span<byte>(rented, 0, byteLength); | ||
RandomNumberGenerator.Fill(testSpan); | ||
|
||
byte[] rented2 = ArrayPool<byte>.Shared.Rent(byteLength); | ||
Span<byte> testSpan2 = new Span<byte>(rented2, 0, byteLength); | ||
|
||
testSpan.CopyTo(testSpan2); | ||
|
||
bool isEqual = CryptographicOperations.FixedTimeEquals(testSpan, testSpan2); | ||
|
||
ArrayPool<byte>.Shared.Return(rented); | ||
ArrayPool<byte>.Shared.Return(rented2); | ||
|
||
Assert.True(isEqual); | ||
} | ||
|
||
[Theory] | ||
[InlineData(1)] | ||
[InlineData(128 / 8)] | ||
[InlineData(256 / 8)] | ||
[InlineData(512 / 8)] | ||
[InlineData(96)] | ||
[InlineData(1024)] | ||
public static void UnequalReturnsFalse(int byteLength) | ||
{ | ||
byte[] rented = ArrayPool<byte>.Shared.Rent(byteLength); | ||
Span<byte> testSpan = new Span<byte>(rented, 0, byteLength); | ||
RandomNumberGenerator.Fill(testSpan); | ||
|
||
byte[] rented2 = ArrayPool<byte>.Shared.Rent(byteLength); | ||
Span<byte> testSpan2 = new Span<byte>(rented2, 0, byteLength); | ||
|
||
testSpan.CopyTo(testSpan2); | ||
testSpan[testSpan[0] % testSpan.Length] ^= 0xFF; | ||
|
||
bool isEqual = CryptographicOperations.FixedTimeEquals(testSpan, testSpan2); | ||
|
||
ArrayPool<byte>.Shared.Return(rented); | ||
ArrayPool<byte>.Shared.Return(rented2); | ||
|
||
Assert.False(isEqual); | ||
} | ||
|
||
[Theory] | ||
[InlineData(1)] | ||
[InlineData(128 / 8)] | ||
[InlineData(256 / 8)] | ||
[InlineData(512 / 8)] | ||
[InlineData(96)] | ||
[InlineData(1024)] | ||
public static void DifferentLengthsReturnFalse(int byteLength) | ||
{ | ||
byte[] rented = ArrayPool<byte>.Shared.Rent(byteLength); | ||
Span<byte> testSpan = new Span<byte>(rented, 0, byteLength); | ||
RandomNumberGenerator.Fill(testSpan); | ||
|
||
byte[] rented2 = ArrayPool<byte>.Shared.Rent(byteLength); | ||
Span<byte> testSpan2 = new Span<byte>(rented2, 0, byteLength); | ||
|
||
testSpan.CopyTo(testSpan2); | ||
|
||
bool isEqualA = CryptographicOperations.FixedTimeEquals(testSpan, testSpan2.Slice(0, byteLength - 1)); | ||
bool isEqualB = CryptographicOperations.FixedTimeEquals(testSpan.Slice(0, byteLength - 1), testSpan2); | ||
|
||
ArrayPool<byte>.Shared.Return(rented); | ||
ArrayPool<byte>.Shared.Return(rented2); | ||
|
||
Assert.False(isEqualA, "value, value missing last byte"); | ||
Assert.False(isEqualB, "value missing last byte, value"); | ||
} | ||
|
||
[Fact] | ||
public static void HasCorrectMethodImpl() | ||
{ | ||
Type t = typeof(CryptographicOperations); | ||
MethodInfo mi = t.GetMethod(nameof(CryptographicOperations.FixedTimeEquals)); | ||
|
||
// This method cannot be optimized, or it loses its fixed time guarantees. | ||
// It cannot be inlined, or it loses its no-optimization guarantee. | ||
Assert.Equal( | ||
MethodImplAttributes.NoInlining | MethodImplAttributes.NoOptimization, | ||
mi.MethodImplementationFlags); | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/System.Security.Cryptography.Primitives/tests/Performance/Configurations.props
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<BuildConfigurations> | ||
netcoreapp; | ||
</BuildConfigurations> | ||
</PropertyGroup> | ||
</Project> |
Oops, something went wrong.