Skip to content

Commit 3d17296

Browse files
committed
Better cover edge cases
1 parent 81237b3 commit 3d17296

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

include/boost/crypt2/aes/ecb.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ template <compat::size_t Extent>
108108
BOOST_CRYPT_GPU_ENABLED_CONSTEXPR auto ecb_impl<Nr>::encrypt_no_padding(
109109
compat::span<compat::byte, Extent> message) noexcept -> state
110110
{
111-
static_assert(Extent >= block_length_bytes, "Invalid block length");
111+
static_assert(Extent == compat::dynamic_extent || Extent % block_length_bytes == 0, "Invalid ciphertext length");
112112

113-
if (message.size() % block_length_bytes != 0)
113+
if (message.size() % block_length_bytes != 0 && !message.empty())
114114
{
115115
return state::incorrect_message_length;
116116
}
@@ -145,7 +145,9 @@ template <compat::size_t Extent>
145145
BOOST_CRYPT_GPU_ENABLED_CONSTEXPR auto ecb_impl<Nr>::decrypt_no_padding(
146146
compat::span<compat::byte, Extent> ciphertext) noexcept -> state
147147
{
148-
if (ciphertext.size() % block_length_bytes != 0)
148+
static_assert(Extent == compat::dynamic_extent || Extent % block_length_bytes == 0, "Invalid ciphertext length");
149+
150+
if (ciphertext.size() % block_length_bytes != 0 && !ciphertext.empty())
149151
{
150152
return state::incorrect_message_length;
151153
}

test/test_aes.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ void ecb_test()
116116
BOOST_TEST(gen2.init(key_span) == boost::crypt::state::success);
117117
BOOST_TEST(gen2.encrypt_no_padding(bad_message) == boost::crypt::state::incorrect_message_length);
118118
BOOST_TEST(gen2.encrypt_no_padding(bad_message_span) == boost::crypt::state::incorrect_message_length);
119+
120+
std::array<std::byte, 1000> incorrect_size {};
121+
std::span<std::byte, std::dynamic_extent> incorrect_size_span {incorrect_size};
122+
BOOST_TEST(gen2.decrypt_no_padding(incorrect_size_span) == boost::crypt::state::incorrect_message_length);
119123
}
120124

121125
/*

0 commit comments

Comments
 (0)