libkeepass 0.2.0
KeePass key database importer/exporter library
Loading...
Searching...
No Matches
constant_time.hh File Reference

Constant-time byte comparisons for secrets and integrity values. More...

#include <array>
#include <cstddef>
#include <cstdint>
#include <openssl/crypto.h>

Go to the source code of this file.

Functions

bool keepass::detail::constant_time_eq (const void *a, std::size_t a_size, const void *b, std::size_t b_size) noexcept
 Compares two byte ranges in constant time.
 
template<std::size_t N>
bool keepass::detail::constant_time_eq (const std::array< uint8_t, N > &a, const std::array< uint8_t, N > &b) noexcept
 Compares two fixed-size byte arrays in constant time.
 

Detailed Description

Constant-time byte comparisons for secrets and integrity values.

Function Documentation

◆ constant_time_eq() [1/2]

template<std::size_t N>
bool keepass::detail::constant_time_eq ( const std::array< uint8_t, N > &  a,
const std::array< uint8_t, N > &  b 
)
inlinenoexcept

Compares two fixed-size byte arrays in constant time.

Parameters
aFirst array.
bSecond array.
Returns
Whether both arrays hold identical bytes.

◆ constant_time_eq() [2/2]

bool keepass::detail::constant_time_eq ( const void *  a,
std::size_t  a_size,
const void *  b,
std::size_t  b_size 
)
inlinenoexcept

Compares two byte ranges in constant time.

The comparison time does not depend on the content bytes: every pair of bytes is always visited, so an early-exit on the first differing byte cannot leak which position differs. The lengths are treated as public, so mismatched lengths return false immediately. It is safe to compare secrets (keys, derived keys) and integrity values (hashes, HMACs) with this helper.

Parameters
aFirst byte range.
a_sizeSize of the first byte range.
bSecond byte range.
b_sizeSize of the second byte range.
Returns
Whether both ranges are identical.