Skip to content

Commit

Permalink
Merge pull request #114 from cppalliance/constexp_hmac_drbg
Browse files Browse the repository at this point in the history
Constexpr HMAC_DRBG
  • Loading branch information
mborland authored Nov 15, 2024
2 parents 52a2ac4 + 0c59fa3 commit da4ff38
Show file tree
Hide file tree
Showing 6 changed files with 260 additions and 375 deletions.
434 changes: 80 additions & 354 deletions include/boost/crypt/drbg/detail/hmac_drbg.hpp

Large diffs are not rendered by default.

22 changes: 18 additions & 4 deletions include/boost/crypt/hash/detail/hasher_base_512.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,19 @@ BOOST_CRYPT_GPU_ENABLED constexpr auto hasher_base_512<digest_size, intermediate
return state::state_error;
}

while (size--)
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wsign-conversion"
#elif defined(__GNUC__) && __GNUC__ >= 5
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds="
#pragma GCC diagnostic ignored "-Wrestrict"
#pragma GCC diagnostic ignored "-Wsign-conversion"
#endif

for (boost::crypt::size_t i {}; i < size; ++i)
{
buffer_[buffer_index_++] = static_cast<boost::crypt::uint8_t>(static_cast<boost::crypt::uint8_t>(*data) &
buffer_[buffer_index_++] = static_cast<boost::crypt::uint8_t>(static_cast<boost::crypt::uint8_t>(data[i]) &
static_cast<boost::crypt::uint8_t>(0xFF));
low_ += 8U;

Expand All @@ -316,10 +326,14 @@ BOOST_CRYPT_GPU_ENABLED constexpr auto hasher_base_512<digest_size, intermediate
{
process_message_block();
}

++data;
}

#ifdef __clang__
#pragma clang diagnostic pop
#elif defined(__GNUC__) && __GNUC__ >= 5
#pragma GCC diagnostic pop
#endif

return state::success;
}

Expand Down
22 changes: 18 additions & 4 deletions include/boost/crypt/hash/detail/sha512_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,19 @@ constexpr auto sha512_base<digest_size>::update(ForwardIter data, boost::crypt::
return state::state_error;
}

while (size--)
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wsign-conversion"
#elif defined(__GNUC__) && __GNUC__ >= 5
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds="
#pragma GCC diagnostic ignored "-Wrestrict"
#pragma GCC diagnostic ignored "-Wsign-conversion"
#endif

for (boost::crypt::size_t i {}; i < size; ++i)
{
buffer_[buffer_index_++] = static_cast<boost::crypt::uint8_t>(static_cast<boost::crypt::uint8_t>(*data) &
buffer_[buffer_index_++] = static_cast<boost::crypt::uint8_t>(static_cast<boost::crypt::uint8_t>(data[i]) &
static_cast<boost::crypt::uint8_t>(0xFF));
low_ += 8U;

Expand All @@ -494,10 +504,14 @@ constexpr auto sha512_base<digest_size>::update(ForwardIter data, boost::crypt::
{
process_message_block();
}

++data;
}

#ifdef __clang__
#pragma clang diagnostic pop
#elif defined(__GNUC__) && __GNUC__ >= 5
#pragma GCC diagnostic pop
#endif

return state::success;
}

Expand Down
4 changes: 2 additions & 2 deletions include/boost/crypt/hash/md5.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,14 @@ BOOST_CRYPT_GPU_ENABLED constexpr auto md5_hasher::get_digest() noexcept -> retu

if (available < 8U)
{
fill_array(buffer_.begin() + used, buffer_.end(), static_cast<boost::crypt::uint8_t>(0));
fill_array(buffer_.begin() + static_cast<boost::crypt::ptrdiff_t>(used), buffer_.end(), static_cast<boost::crypt::uint8_t>(0));
process_message_block();
used = 0;
buffer_.fill(0);
}
else
{
fill_array(buffer_.begin() + used, buffer_.end() - 8, static_cast<boost::crypt::uint8_t>(0));
fill_array(buffer_.begin() + static_cast<boost::crypt::ptrdiff_t>(used), buffer_.end() - 8, static_cast<boost::crypt::uint8_t>(0));
}

const auto total_bits {(static_cast<uint64_t>(high_) << 32) | low_};
Expand Down
147 changes: 139 additions & 8 deletions include/boost/crypt/utility/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,108 @@ template <typename T, boost::crypt::size_t N>
class array
{
public:

class iterator {
public:
using value_type = T;
using pointer = T*;
using reference = T&;
using difference_type = boost::crypt::ptrdiff_t;
#ifndef BOOST_CRYPT_HAS_CUDA
using iterator_category = std::random_access_iterator_tag;
#else
using iterator_category = cuda::std::random_access_iterator_tag;
#endif

BOOST_CRYPT_GPU_ENABLED constexpr iterator() noexcept : ptr_(nullptr) {}
BOOST_CRYPT_GPU_ENABLED constexpr explicit iterator(pointer ptr) noexcept : ptr_(ptr) {}

// Iterator operations
BOOST_CRYPT_GPU_ENABLED constexpr auto operator*() noexcept -> reference { return *ptr_; }
BOOST_CRYPT_GPU_ENABLED constexpr auto operator*() const noexcept -> reference { return *ptr_; }
BOOST_CRYPT_GPU_ENABLED constexpr auto operator->() noexcept -> pointer { return ptr_; }
BOOST_CRYPT_GPU_ENABLED constexpr auto operator->() const noexcept -> pointer { return ptr_; }
BOOST_CRYPT_GPU_ENABLED constexpr auto operator[](difference_type n) noexcept -> reference { return ptr_[n]; }
BOOST_CRYPT_GPU_ENABLED constexpr auto operator[](difference_type n) const noexcept -> reference { return ptr_[n]; }

// Increment/Decrement
BOOST_CRYPT_GPU_ENABLED constexpr auto operator++() noexcept -> iterator& { ++ptr_; return *this; }
BOOST_CRYPT_GPU_ENABLED constexpr auto operator++(int) noexcept -> iterator { iterator tmp(*this); ++ptr_; return tmp; }
BOOST_CRYPT_GPU_ENABLED constexpr auto operator--() noexcept -> iterator& { --ptr_; return *this; }
BOOST_CRYPT_GPU_ENABLED constexpr auto operator--(int) noexcept -> iterator { iterator tmp(*this); --ptr_; return tmp; }

// Arithmetic operations
BOOST_CRYPT_GPU_ENABLED constexpr auto operator+=(difference_type n) noexcept -> iterator& { ptr_ += n; return *this; }
BOOST_CRYPT_GPU_ENABLED constexpr auto operator-=(difference_type n) noexcept -> iterator& { ptr_ -= n; return *this; }

// Comparison operators
BOOST_CRYPT_GPU_ENABLED constexpr auto operator==(const iterator& other) const noexcept -> bool { return ptr_ == other.ptr_; }
BOOST_CRYPT_GPU_ENABLED constexpr auto operator!=(const iterator& other) const noexcept -> bool { return ptr_ != other.ptr_; }
BOOST_CRYPT_GPU_ENABLED constexpr auto operator<(const iterator& other) const noexcept -> bool { return ptr_ < other.ptr_; }
BOOST_CRYPT_GPU_ENABLED constexpr auto operator>(const iterator& other) const noexcept -> bool { return ptr_ > other.ptr_; }
BOOST_CRYPT_GPU_ENABLED constexpr auto operator<=(const iterator& other) const noexcept -> bool { return ptr_ <= other.ptr_; }
BOOST_CRYPT_GPU_ENABLED constexpr auto operator>=(const iterator& other) const noexcept -> bool { return ptr_ >= other.ptr_; }

BOOST_CRYPT_GPU_ENABLED friend constexpr auto operator+(iterator it, difference_type n) noexcept -> iterator { return it += n; }
BOOST_CRYPT_GPU_ENABLED friend constexpr auto operator+(difference_type n, iterator it) noexcept -> iterator { return it += n; }
BOOST_CRYPT_GPU_ENABLED friend constexpr auto operator-(iterator it, difference_type n) noexcept -> iterator { return it -= n; }
BOOST_CRYPT_GPU_ENABLED friend constexpr auto operator-(const iterator& lhs, const iterator& rhs) noexcept -> difference_type
{ return lhs.operator->() - rhs.operator->(); }

private:
pointer ptr_;
};

class const_iterator {
public:
using value_type = const T;
using pointer = const T*;
using reference = const T&;
using difference_type = boost::crypt::ptrdiff_t;
#ifndef BOOST_CRYPT_HAS_CUDA
using iterator_category = std::random_access_iterator_tag;
#else
using iterator_category = cuda::std::random_access_iterator_tag;
#endif

BOOST_CRYPT_GPU_ENABLED constexpr const_iterator() noexcept : ptr_(nullptr) {}
BOOST_CRYPT_GPU_ENABLED constexpr explicit const_iterator(pointer ptr) noexcept : ptr_(ptr) {}

// Iterator operations
BOOST_CRYPT_GPU_ENABLED constexpr auto operator*() const noexcept -> reference { return *ptr_; }
BOOST_CRYPT_GPU_ENABLED constexpr auto operator->() const noexcept -> pointer { return ptr_; }
BOOST_CRYPT_GPU_ENABLED constexpr auto operator[](difference_type n) const noexcept -> reference { return ptr_[n]; }

// Increment/Decrement
BOOST_CRYPT_GPU_ENABLED constexpr auto operator++() noexcept -> const_iterator& { ++ptr_; return *this; }
BOOST_CRYPT_GPU_ENABLED constexpr auto operator++(int) noexcept -> const_iterator { const_iterator tmp(*this); ++ptr_; return tmp; }
BOOST_CRYPT_GPU_ENABLED constexpr auto operator--() noexcept -> const_iterator& { --ptr_; return *this; }
BOOST_CRYPT_GPU_ENABLED constexpr auto operator--(int) noexcept -> const_iterator { const_iterator tmp(*this); --ptr_; return tmp; }

// Arithmetic operations
BOOST_CRYPT_GPU_ENABLED constexpr auto operator+=(difference_type n) noexcept -> const_iterator& { ptr_ += n; return *this; }
BOOST_CRYPT_GPU_ENABLED constexpr auto operator-=(difference_type n) noexcept -> const_iterator& { ptr_ -= n; return *this; }

// Comparison operators
BOOST_CRYPT_GPU_ENABLED constexpr auto operator==(const const_iterator& other) const noexcept -> bool { return ptr_ == other.ptr_; }
BOOST_CRYPT_GPU_ENABLED constexpr auto operator!=(const const_iterator& other) const noexcept -> bool { return ptr_ != other.ptr_; }
BOOST_CRYPT_GPU_ENABLED constexpr auto operator<(const const_iterator& other) const noexcept -> bool { return ptr_ < other.ptr_; }
BOOST_CRYPT_GPU_ENABLED constexpr auto operator>(const const_iterator& other) const noexcept -> bool { return ptr_ > other.ptr_; }
BOOST_CRYPT_GPU_ENABLED constexpr auto operator<=(const const_iterator& other) const noexcept -> bool { return ptr_ <= other.ptr_; }
BOOST_CRYPT_GPU_ENABLED constexpr auto operator>=(const const_iterator& other) const noexcept -> bool { return ptr_ >= other.ptr_; }

BOOST_CRYPT_GPU_ENABLED friend constexpr auto operator+(const_iterator it, difference_type n) noexcept -> const_iterator { return it += n; }
BOOST_CRYPT_GPU_ENABLED friend constexpr auto operator+(difference_type n, const_iterator it) noexcept -> const_iterator { return it += n; }
BOOST_CRYPT_GPU_ENABLED friend constexpr auto operator-(const_iterator it, difference_type n) noexcept -> const_iterator { return it -= n; }
BOOST_CRYPT_GPU_ENABLED friend constexpr auto operator-(const const_iterator& lhs, const const_iterator& rhs) noexcept -> difference_type
{ return lhs.operator->() - rhs.operator->(); }

private:
pointer ptr_;
};

using reference = T&;
using const_reference = const T&;
using iterator = T*;
using const_iterator = const T*;
using size_type = boost::crypt::size_t;
using difference_type = boost::crypt::ptrdiff_t;
using value_type = T;
Expand All @@ -36,12 +134,12 @@ class array
T elements[N];

// Iterators
BOOST_CRYPT_GPU_ENABLED constexpr auto begin() noexcept -> iterator { return elements; }
BOOST_CRYPT_GPU_ENABLED constexpr auto begin() const noexcept -> const_iterator { return elements; }
BOOST_CRYPT_GPU_ENABLED constexpr auto cbegin() const noexcept -> const_iterator { return elements; }
BOOST_CRYPT_GPU_ENABLED constexpr auto end() noexcept -> iterator { return elements + N; }
BOOST_CRYPT_GPU_ENABLED constexpr auto end() const noexcept -> const_iterator { return elements + N; }
BOOST_CRYPT_GPU_ENABLED constexpr auto cend() const noexcept -> const_iterator { return elements + N; }
BOOST_CRYPT_GPU_ENABLED constexpr auto begin() noexcept -> iterator { return iterator{elements}; }
BOOST_CRYPT_GPU_ENABLED constexpr auto begin() const noexcept -> const_iterator { return const_iterator{elements}; }
BOOST_CRYPT_GPU_ENABLED constexpr auto cbegin() const noexcept -> const_iterator { return const_iterator{elements}; }
BOOST_CRYPT_GPU_ENABLED constexpr auto end() noexcept -> iterator { return iterator{elements + N}; }
BOOST_CRYPT_GPU_ENABLED constexpr auto end() const noexcept -> const_iterator { return const_iterator{elements + N}; }
BOOST_CRYPT_GPU_ENABLED constexpr auto cend() const noexcept -> const_iterator { return const_iterator{elements + N}; }

// Sizing
BOOST_CRYPT_GPU_ENABLED constexpr auto size() const noexcept -> size_type { return N; }
Expand Down Expand Up @@ -115,6 +213,17 @@ class array

return new_array;
}

constexpr operator std::array<T, N>() const noexcept
{
std::array<T, N> new_array{};
for (boost::crypt::size_t i {}; i < N; ++i)
{
new_array[i] = elements[i];
}

return new_array;
}
#endif
};

Expand Down Expand Up @@ -172,4 +281,26 @@ class tuple_size<boost::crypt::array<T, N>> : public boost::crypt::integral_cons
} // namespace crypt
} // namespace boost

#ifndef BOOST_CRYPT_HAS_CUDA

namespace std {

template <typename T, boost::crypt::size_t N>
struct iterator_traits<boost::crypt::array<T, N>>
{
using value_type = typename boost::crypt::array<T, N>::value_type;
using pointer = typename boost::crypt::array<T, N>::pointer;
using reference = typename boost::crypt::array<T, N>::reference;
using difference_type = typename boost::crypt::array<T, N>::difference_type;
#ifndef BOOST_CRYPT_HAS_CUDA
using iterator_category = std::random_access_iterator_tag;
#else
using iterator_category = cuda::std::random_access_iterator_tag;
#endif
};

} // namespace std

#endif // BOOST_CRYPT_HAS_CUDA

#endif // BOOST_CRYPT_UTILITIES_ARRAY_HPP
6 changes: 3 additions & 3 deletions test/test_hmac_drbg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ void sha1_additional_input()
void sha1_pr()
{
boost::crypt::sha1_hmac_drbg_pr rng;
constexpr boost::crypt::array<boost::crypt::uint8_t, 16> entropy = {
constexpr std::array<boost::crypt::uint8_t, 16> entropy = {
0xa0, 0xc9, 0xab, 0x58, 0xf1, 0xe2, 0xe5, 0xa4, 0xde, 0x3e, 0xbd, 0x4f, 0xf7, 0x3e, 0x9c, 0x5b
};
constexpr boost::crypt::array<boost::crypt::uint8_t, 8> nonce = {
constexpr std::array<boost::crypt::uint8_t, 8> nonce = {
0x64, 0xef, 0xd8, 0xca, 0x02, 0x8c, 0xf8, 0x11
};

Expand All @@ -171,7 +171,7 @@ void sha1_pr()

boost::crypt::array<boost::crypt::uint8_t, 80> return_bits {};

BOOST_TEST(rng.init(entropy, entropy.size(), nonce, nonce.size()) == boost::crypt::state::success);
BOOST_TEST(rng.init(entropy.begin(), entropy.size(), nonce.begin(), nonce.size()) == boost::crypt::state::success);

BOOST_TEST(rng.generate(return_bits.begin(), 640U, entropy_gen_1.begin(), entropy_gen_1.size()) == boost::crypt::state::success);

Expand Down

0 comments on commit da4ff38

Please sign in to comment.