mirror of
https://github.com/monero-project/monero.git
synced 2025-08-13 11:55:35 -04:00
add and use constant time 32 byte equality function
This commit is contained in:
parent
510dbf3329
commit
d2e26c23f3
9 changed files with 130 additions and 14 deletions
|
@ -33,19 +33,30 @@
|
|||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
#include <sodium/crypto_verify_32.h>
|
||||
|
||||
#define CRYPTO_MAKE_COMPARABLE(type) \
|
||||
namespace crypto { \
|
||||
inline bool operator==(const type &_v1, const type &_v2) { \
|
||||
return std::memcmp(&_v1, &_v2, sizeof(type)) == 0; \
|
||||
return !memcmp(&_v1, &_v2, sizeof(_v1)); \
|
||||
} \
|
||||
inline bool operator!=(const type &_v1, const type &_v2) { \
|
||||
return std::memcmp(&_v1, &_v2, sizeof(type)) != 0; \
|
||||
return !operator==(_v1, _v2); \
|
||||
} \
|
||||
}
|
||||
|
||||
#define CRYPTO_MAKE_HASHABLE(type) \
|
||||
CRYPTO_MAKE_COMPARABLE(type) \
|
||||
#define CRYPTO_MAKE_COMPARABLE_CONSTANT_TIME(type) \
|
||||
namespace crypto { \
|
||||
inline bool operator==(const type &_v1, const type &_v2) { \
|
||||
static_assert(sizeof(_v1) == 32, "constant time comparison is only implenmted for 32 bytes"); \
|
||||
return crypto_verify_32((const unsigned char*)&_v1, (const unsigned char*)&_v2) == 0; \
|
||||
} \
|
||||
inline bool operator!=(const type &_v1, const type &_v2) { \
|
||||
return !operator==(_v1, _v2); \
|
||||
} \
|
||||
}
|
||||
|
||||
#define CRYPTO_DEFINE_HASH_FUNCTIONS(type) \
|
||||
namespace crypto { \
|
||||
static_assert(sizeof(std::size_t) <= sizeof(type), "Size of " #type " must be at least that of size_t"); \
|
||||
inline std::size_t hash_value(const type &_v) { \
|
||||
|
@ -60,3 +71,12 @@ namespace std { \
|
|||
} \
|
||||
}; \
|
||||
}
|
||||
|
||||
#define CRYPTO_MAKE_HASHABLE(type) \
|
||||
CRYPTO_MAKE_COMPARABLE(type) \
|
||||
CRYPTO_DEFINE_HASH_FUNCTIONS(type)
|
||||
|
||||
#define CRYPTO_MAKE_HASHABLE_CONSTANT_TIME(type) \
|
||||
CRYPTO_MAKE_COMPARABLE_CONSTANT_TIME(type) \
|
||||
CRYPTO_DEFINE_HASH_FUNCTIONS(type)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue