Constant time
Whenever you interact with secrets and cryptographic parameters, you should use constant time functions to avoid leaking information via timing to an attacker. Such leaks can completely compromise security.
For example, you should increment counters/nonces, compare tags, compare passwords, and so on using this class.
Determines if two spans are equal in length and contain equal data.
ConstantTime.Equals(ReadOnlySpan<byte> a, ReadOnlySpan<byte> b)
a
has a length of 0.b
has a length of 0.Increments a span counter.
ConstantTime.Increment(Span<byte> buffer)
buffer
has a length of 0.Fills a span with the sum of two spans.
ConstantTime.Add(Span<byte> buffer, ReadOnlySpan<byte> a, ReadOnlySpan<byte> b)
buffer
has a length of 0.a
has a length of 0 or not equal to buffer.Length
.b
has a length of 0 or not equal to a.Length
.Fills a span with the result of subtracting the second span from the first span.
ConstantTime.Subtract(Span<byte> buffer, ReadOnlySpan<byte> a, ReadOnlySpan<byte> b)
buffer
has a length of 0.a
has a length of 0 or not equal to buffer.Length
.b
has a length of 0 or not equal to a.Length
.Determines if the contents of the first span is less than the second span.
ConstantTime.IsLessThan(ReadOnlySpan<byte> a, ReadOnlySpan<byte> b)
a
has a length of 0 or not equal to b.Length
.b
has a length of 0.Determines if the contents of the first span is greater than the second span.
ConstantTime.IsGreaterThan(ReadOnlySpan<byte> a, ReadOnlySpan<byte> b)
a
has a length of 0 or not equal to b.Length
.b
has a length of 0.Determines if a span only contains zeros.
ConstantTime.IsAllZeros(ReadOnlySpan<byte> buffer)
Tags MUST be compared in constant time using
ConstantTime.Equals()
. The VerifyTag()
functions do this for you.These constant time functions can also be used for non-secret values.
All of these functions use a little-endian format.
Last modified 10mo ago