Skip to content

Commit

Permalink
Merge pull request #120 from cppalliance/destroy
Browse files Browse the repository at this point in the history
Add ability to destroy classes and automatically do so with C++20 or better
  • Loading branch information
mborland authored Nov 22, 2024
2 parents 0e3b658 + 16897c1 commit a0f4462
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 0 deletions.
14 changes: 14 additions & 0 deletions include/boost/crypt/drbg/detail/hmac_drbg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ class hmac_drbg

BOOST_CRYPT_GPU_ENABLED constexpr hmac_drbg() = default;

#ifdef BOOST_CRYPT_HAS_CXX20_CONSTEXPR
BOOST_CRYPT_GPU_ENABLED constexpr ~hmac_drbg() noexcept { destroy(); }
#endif

template <typename ForwardIter1, typename ForwardIter2, typename ForwardIter3 = const boost::crypt::uint8_t*>
BOOST_CRYPT_GPU_ENABLED constexpr auto init(ForwardIter1 entropy, boost::crypt::size_t entropy_size,
ForwardIter2 nonce = nullptr, boost::crypt::size_t nonce_size = 0,
Expand Down Expand Up @@ -143,8 +147,18 @@ class hmac_drbg
template <typename Container1, typename Container2, typename Container3>
BOOST_CRYPT_GPU_ENABLED constexpr auto generate(Container1& data, const Container2& additional_data_1, const Container3& additional_data_2) noexcept -> state;

BOOST_CRYPT_GPU_ENABLED constexpr auto destroy() noexcept;
};

template <typename HMACType, boost::crypt::size_t max_hasher_security, boost::crypt::size_t outlen, bool prediction_resistance>
constexpr auto hmac_drbg<HMACType, max_hasher_security, outlen, prediction_resistance>::destroy() noexcept
{
key_.fill(0x00);
value_.fill(0x00);
reseed_counter_ = 0U;
initialized_ = false;
}

template <typename HMACType, boost::crypt::size_t max_hasher_security, boost::crypt::size_t outlen, bool prediction_resistance>
template <typename ForwardIter1, typename ForwardIter2, typename ForwardIter3>
constexpr auto hmac_drbg<HMACType, max_hasher_security, outlen, prediction_resistance>::generate(
Expand Down
7 changes: 7 additions & 0 deletions include/boost/crypt/hash/detail/hasher_base_512.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class hasher_base_512

BOOST_CRYPT_GPU_ENABLED constexpr auto base_init() noexcept -> void;

#ifdef BOOST_CRYPT_HAS_CXX20_CONSTEXPR
BOOST_CRYPT_GPU_ENABLED constexpr ~hasher_base_512() noexcept { destroy(); }
#endif

template <typename ByteType>
BOOST_CRYPT_GPU_ENABLED constexpr auto process_byte(ByteType byte) noexcept -> state;

Expand Down Expand Up @@ -101,11 +105,14 @@ class hasher_base_512
#endif // BOOST_CRYPT_HAS_CUDA

BOOST_CRYPT_GPU_ENABLED constexpr auto get_base_digest() noexcept -> return_type;

BOOST_CRYPT_GPU_ENABLED constexpr auto destroy() noexcept -> void { base_init(); };
};

template <boost::crypt::size_t digest_size, boost::crypt::size_t intermediate_hash_size, typename Derived>
BOOST_CRYPT_GPU_ENABLED constexpr auto hasher_base_512<digest_size, intermediate_hash_size, Derived>::base_init() noexcept -> void
{
intermediate_hash_.fill(0);
buffer_.fill(0);
buffer_index_ = 0U;
low_ = 0U;
Expand Down
6 changes: 6 additions & 0 deletions include/boost/crypt/hash/detail/sha3_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ class sha3_base

BOOST_CRYPT_GPU_ENABLED constexpr sha3_base() noexcept { init(); };

#ifdef BOOST_CRYPT_HAS_CXX20_CONSTEXPR
BOOST_CRYPT_GPU_ENABLED constexpr ~sha3_base() noexcept { destroy(); }
#endif

BOOST_CRYPT_GPU_ENABLED constexpr auto init() noexcept -> void;

template <typename ByteType>
Expand Down Expand Up @@ -112,6 +116,8 @@ class sha3_base

template <typename Container>
BOOST_CRYPT_GPU_ENABLED constexpr auto get_digest(Container& container) noexcept -> boost::crypt::size_t;

BOOST_CRYPT_GPU_ENABLED constexpr auto destroy() noexcept -> void { init(); }
};

template <boost::crypt::size_t digest_size, bool is_xof>
Expand Down
7 changes: 7 additions & 0 deletions include/boost/crypt/hash/detail/sha512_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ class sha512_base final

BOOST_CRYPT_GPU_ENABLED constexpr sha512_base() noexcept { init(); }

#ifdef BOOST_CRYPT_HAS_CXX20_CONSTEXPR
BOOST_CRYPT_GPU_ENABLED constexpr ~sha512_base() noexcept { destroy(); }
#endif

BOOST_CRYPT_GPU_ENABLED constexpr auto init() noexcept -> void;

template <typename ByteType>
Expand Down Expand Up @@ -116,6 +120,8 @@ class sha512_base final
#endif // BOOST_CRYPT_HAS_CUDA

BOOST_CRYPT_GPU_ENABLED constexpr auto get_digest() noexcept -> return_type;

BOOST_CRYPT_GPU_ENABLED constexpr auto destroy() noexcept -> void { init(); }
};

template <boost::crypt::size_t digest_size>
Expand Down Expand Up @@ -574,6 +580,7 @@ constexpr auto sha512_base<digest_size>::init(const integral_constant<boost::cry
template <boost::crypt::size_t digest_size>
constexpr auto sha512_base<digest_size>::init() noexcept -> void
{
intermediate_hash_.fill(0);
buffer_.fill(0);
buffer_index_ = 0U;
low_ = 0U;
Expand Down
18 changes: 18 additions & 0 deletions include/boost/crypt/mac/hmac.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class hmac

BOOST_CRYPT_GPU_ENABLED constexpr hmac() noexcept = default;

#ifdef BOOST_CRYPT_HAS_CXX20_CONSTEXPR
BOOST_CRYPT_GPU_ENABLED constexpr ~hmac() noexcept { destroy(); }
#endif

template <typename ForwardIter>
BOOST_CRYPT_GPU_ENABLED constexpr hmac(ForwardIter key, boost::crypt::size_t size) noexcept { init(key, size); }

Expand Down Expand Up @@ -67,8 +71,22 @@ class hmac
BOOST_CRYPT_GPU_ENABLED constexpr auto get_outer_key() noexcept -> key_type;

BOOST_CRYPT_GPU_ENABLED constexpr auto get_inner_key() noexcept -> key_type;

BOOST_CRYPT_GPU_ENABLED constexpr auto destroy() noexcept -> void;
};

template <typename HasherType>
constexpr auto hmac<HasherType>::destroy() noexcept -> void
{
inner_key_.fill(0x00);
outer_key_.fill(0x00);
inner_hash_.destroy();
outer_hash_.destroy();
initialized_ = false;
computed_ = false;
corrupted_ = false;
}

template <typename HasherType>
constexpr auto hmac<HasherType>::get_inner_key() noexcept -> key_type
{
Expand Down
2 changes: 2 additions & 0 deletions test/test_hmac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ void basic_tests()
BOOST_TEST_EQ(res[i], soln[i]);
}
}

hmac_tester.destroy();
}

template <typename HasherType>
Expand Down
2 changes: 2 additions & 0 deletions test/test_hmac_drbg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ void sha1_pr()

#endif
#endif

rng.destroy();
}

int main()
Expand Down
2 changes: 2 additions & 0 deletions test/test_sha3_224.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ void test_class()

hasher.init();
}

hasher.destroy();
}

template <typename T>
Expand Down

0 comments on commit a0f4462

Please sign in to comment.