Constant time
Purpose
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 retyped passwords, and so on using this class.
Usage
Equals
Returns whether two spans are equal in length and contain equal data.
bool equal = ConstantTime.Equals(ReadOnlySpan<byte> a, ReadOnlySpan<byte> b);Exceptions
a.Length is equal to 0.
b.Length is equal to 0.
Increment
Increments a span counter in little-endian format.
Exceptions
buffer.Length is equal to 0.
Add
Fills a span with the sum of two spans in little-endian format.
Exceptions
buffer.Length is equal to 0.
a.Length is equal to 0 or not equal to buffer.Length.
b.Length is equal to 0 or not equal to a.Length.
Subtract
Fills a span with the result of subtracting the second span from the first span in little-endian format.
Exceptions
buffer.Length is equal to 0.
a.Length is equal to 0 or not equal to buffer.Length.
b.Length is equal to 0 or not equal to a.Length.
IsLessThan
Returns whether the contents of the first span is less than the second span in little-endian format.
Exceptions
a.Length is equal to 0 or not equal to b.Length.
b.Length is equal to 0.
IsGreaterThan
Returns whether the contents of the first span is greater than the second span in little-endian format.
Exceptions
a.Length is equal to 0 or not equal to b.Length.
b.Length is equal to 0.
IsAllZeros
Returns whether a span only contains zeros.
Exceptions
buffer.Length is equal to 0.
Notes
These constant time functions can also be used for non-secret values. If in doubt, use constant time.
Last updated