utils: Add a constant time chunk_equals() variant for cryptographic purposes

This commit is contained in:
Martin Willi
2015-04-14 12:02:48 +02:00
parent 71afe0a556
commit 9d6e952201
3 changed files with 87 additions and 1 deletions
+13
View File
@@ -309,6 +309,19 @@ static inline bool chunk_equals(chunk_t a, chunk_t b)
a.len == b.len && memeq(a.ptr, b.ptr, a.len);
}
/**
* Compare two chunks for equality, constant time for cryptographic purposes.
*
* Note that this function is constant time only for chunks with the same
* length, i.e. it does not protect against guessing the length of one of the
* chunks.
*/
static inline bool chunk_equals_const(chunk_t a, chunk_t b)
{
return a.ptr != NULL && b.ptr != NULL &&
a.len == b.len && memeq_const(a.ptr, b.ptr, a.len);
}
/**
* Compare two chunks (given as pointers) for equality (useful as callback),
* NULL chunks are never equal.