-
Notifications
You must be signed in to change notification settings - Fork 0
/
SecureCompare.cs
61 lines (56 loc) · 2.16 KB
/
SecureCompare.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
namespace Recore.Security.Cryptography
{
/// <summary>
/// Provides methods for securely comparing objects.
/// </summary>
public static class SecureCompare
{
/// <summary>
/// Checks two byte arrays for equality without early termination.
/// </summary>
/// <remarks>
/// This method is used to guard against <b>timing attacks</b>.
///
/// When checking untrusted input against a secret,
/// using a regular element-by-element equality method
/// such as <see cref="string.Equals(string)"/> is insecure.
/// For example, suppose you are checking whether an incoming request's signature
/// matches what you expect.
/// In this case, you hash the request payload with your own private key
/// and compare that to the actual signature.
/// If the comparison stops at the first unmatched element in the sequence,
/// an attacker can time the comparison with a high-resolution timer
/// and infer how many elements they guessed correctly.
///
/// This method assumes that the length of the sequences are equal,
/// such as two strings processed by a hashing algorithm.
/// If the length of the sequence is considered a secret,
/// this method should not be used
/// as it will leak that information in a timing attack.
/// </remarks>
public static bool TimeInvariantEquals(byte[] lhs, byte[] rhs)
{
if (lhs is null)
{
throw new ArgumentNullException(nameof(lhs));
}
if (rhs is null)
{
throw new ArgumentNullException(nameof(rhs));
}
if (lhs.Length != rhs.Length)
{
return false;
}
// We can't use conditional branches here
// since that will change the execution time depending on the secret
bool areEqual = true;
for (int i = 0; i < lhs.Length; i++)
{
areEqual &= (lhs[i] == rhs[i]);
}
return areEqual;
}
}
}