Botan 3.6.1
Crypto and TLS for C&
mem_ops.cpp
Go to the documentation of this file.
1/*
2* (C) 2017 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/mem_ops.h>
8
9#include <botan/internal/ct_utils.h>
10
11namespace Botan {
12
13uint8_t ct_compare_u8(const uint8_t x[], const uint8_t y[], size_t len) {
14 return CT::is_equal(x, y, len).value();
15}
16
17bool constant_time_compare(std::span<const uint8_t> x, std::span<const uint8_t> y) {
18 const auto min_size = CT::Mask<size_t>::is_lte(x.size(), y.size()).select(x.size(), y.size());
19 const auto equal_size = CT::Mask<size_t>::is_equal(x.size(), y.size());
20 const auto equal_content = CT::Mask<size_t>::expand(CT::is_equal(x.data(), y.data(), min_size));
21 return (equal_content & equal_size).as_bool();
22}
23
24} // namespace Botan
static constexpr Mask< T > is_lte(T x, T y)
Definition ct_utils.h:455
static constexpr Mask< T > expand(T v)
Definition ct_utils.h:389
static constexpr Mask< T > is_equal(T x, T y)
Definition ct_utils.h:434
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
Definition ct_utils.h:759
uint8_t ct_compare_u8(const uint8_t x[], const uint8_t y[], size_t len)
Definition mem_ops.cpp:13
bool constant_time_compare(std::span< const uint8_t > x, std::span< const uint8_t > y)
Definition mem_ops.cpp:17