aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/cxx/include/aesxx
diff options
context:
space:
mode:
Diffstat (limited to 'cxx/include/aesxx')
-rw-r--r--cxx/include/aesxx/aes.hpp48
-rw-r--r--cxx/include/aesxx/box.hpp24
-rw-r--r--cxx/include/aesxx/error.hpp35
3 files changed, 58 insertions, 49 deletions
diff --git a/cxx/include/aesxx/aes.hpp b/cxx/include/aesxx/aes.hpp
index c85bd26..d1f68d2 100644
--- a/cxx/include/aesxx/aes.hpp
+++ b/cxx/include/aesxx/aes.hpp
@@ -42,37 +42,37 @@ namespace aes
template <>
void from_string<AES_AES128>(aes128::Block& dest, const char* src)
{
- aes_AES128_parse_block(&dest, src, ErrorDetailsThrowsInDestructor());
+ aes_AES128_parse_block(&dest, src, ErrorDetailsThrowsInDestructor{});
}
template <>
std::string to_string<AES_AES128>(const aes128::Block& src)
{
AES_AES128_BlockString str;
- aes_AES128_format_block(&str, &src, ErrorDetailsThrowsInDestructor());
- return { str.str };
+ aes_AES128_format_block(&str, &src, ErrorDetailsThrowsInDestructor{});
+ return {str.str};
}
template <>
std::string to_matrix_string<AES_AES128>(const aes128::Block& src)
{
AES_AES128_BlockMatrixString str;
- aes_AES128_format_block_as_matrix(&str, &src, ErrorDetailsThrowsInDestructor());
- return { str.str };
+ aes_AES128_format_block_as_matrix(&str, &src, ErrorDetailsThrowsInDestructor{});
+ return {str.str};
}
template <>
void from_string<AES_AES128>(aes128::Key& dest, const char* src)
{
- aes_AES128_parse_key(&dest, src, ErrorDetailsThrowsInDestructor());
+ aes_AES128_parse_key(&dest, src, ErrorDetailsThrowsInDestructor{});
}
template <>
std::string to_string<AES_AES128>(const aes128::Key& src)
{
AES_AES128_KeyString str;
- aes_AES128_format_key(&str, &src, ErrorDetailsThrowsInDestructor());
- return { str.str };
+ aes_AES128_format_key(&str, &src, ErrorDetailsThrowsInDestructor{});
+ return {str.str};
}
template <>
@@ -127,37 +127,37 @@ namespace aes
template <>
void from_string<AES_AES192>(aes192::Block& dest, const char* src)
{
- aes_AES192_parse_block(&dest, src, ErrorDetailsThrowsInDestructor());
+ aes_AES192_parse_block(&dest, src, ErrorDetailsThrowsInDestructor{});
}
template <>
std::string to_string<AES_AES192>(const aes192::Block& src)
{
AES_AES192_BlockString str;
- aes_AES192_format_block(&str, &src, ErrorDetailsThrowsInDestructor());
- return { str.str };
+ aes_AES192_format_block(&str, &src, ErrorDetailsThrowsInDestructor{});
+ return {str.str};
}
template <>
std::string to_matrix_string<AES_AES192>(const aes192::Block& src)
{
AES_AES192_BlockMatrixString str;
- aes_AES192_format_block_as_matrix(&str, &src, ErrorDetailsThrowsInDestructor());
- return { str.str };
+ aes_AES192_format_block_as_matrix(&str, &src, ErrorDetailsThrowsInDestructor{});
+ return {str.str};
}
template <>
void from_string<AES_AES192>(aes192::Key& dest, const char* src)
{
- aes_AES192_parse_key(&dest, src, ErrorDetailsThrowsInDestructor());
+ aes_AES192_parse_key(&dest, src, ErrorDetailsThrowsInDestructor{});
}
template <>
std::string to_string<AES_AES192>(const aes192::Key& src)
{
AES_AES192_KeyString str;
- aes_AES192_format_key(&str, &src, ErrorDetailsThrowsInDestructor());
- return { str.str };
+ aes_AES192_format_key(&str, &src, ErrorDetailsThrowsInDestructor{});
+ return {str.str};
}
template <>
@@ -212,37 +212,37 @@ namespace aes
template <>
void from_string<AES_AES256>(aes256::Block& dest, const char* src)
{
- aes_AES256_parse_block(&dest, src, ErrorDetailsThrowsInDestructor());
+ aes_AES256_parse_block(&dest, src, ErrorDetailsThrowsInDestructor{});
}
template <>
std::string to_string<AES_AES256>(const aes256::Block& src)
{
AES_AES256_BlockString str;
- aes_AES256_format_block(&str, &src, ErrorDetailsThrowsInDestructor());
- return { str.str };
+ aes_AES256_format_block(&str, &src, ErrorDetailsThrowsInDestructor{});
+ return {str.str};
}
template <>
std::string to_matrix_string<AES_AES256>(const aes256::Block& src)
{
AES_AES256_BlockMatrixString str;
- aes_AES256_format_block_as_matrix(&str, &src, ErrorDetailsThrowsInDestructor());
- return { str.str };
+ aes_AES256_format_block_as_matrix(&str, &src, ErrorDetailsThrowsInDestructor{});
+ return {str.str};
}
template <>
void from_string<AES_AES256>(aes256::Key& dest, const char* src)
{
- aes_AES256_parse_key(&dest, src, ErrorDetailsThrowsInDestructor());
+ aes_AES256_parse_key(&dest, src, ErrorDetailsThrowsInDestructor{});
}
template <>
std::string to_string<AES_AES256>(const aes256::Key& src)
{
AES_AES256_KeyString str;
- aes_AES256_format_key(&str, &src, ErrorDetailsThrowsInDestructor());
- return { str.str };
+ aes_AES256_format_key(&str, &src, ErrorDetailsThrowsInDestructor{});
+ return {str.str};
}
template <>
diff --git a/cxx/include/aesxx/box.hpp b/cxx/include/aesxx/box.hpp
index ced4c28..3d9529b 100644
--- a/cxx/include/aesxx/box.hpp
+++ b/cxx/include/aesxx/box.hpp
@@ -30,7 +30,7 @@ namespace aes
{
AES_BoxKeyString str;
aes_box_format_key(
- &str, algorithm, &src, ErrorDetailsThrowsInDestructor());
+ &str, algorithm, &src, ErrorDetailsThrowsInDestructor{});
return reinterpret_cast<const char*>(&str);
}
@@ -38,7 +38,7 @@ namespace aes
{
AES_BoxBlockString str;
aes_box_format_block(
- &str, algorithm, &src, ErrorDetailsThrowsInDestructor());
+ &str, algorithm, &src, ErrorDetailsThrowsInDestructor{});
return reinterpret_cast<const char*>(&str);
}
@@ -48,7 +48,7 @@ namespace aes
const char* src)
{
aes_box_parse_block(&dest, algorithm, src,
- ErrorDetailsThrowsInDestructor());
+ ErrorDetailsThrowsInDestructor{});
}
static void parse_block(
@@ -65,7 +65,7 @@ namespace aes
const char* src)
{
aes_box_parse_key(&dest, algorithm, src,
- ErrorDetailsThrowsInDestructor());
+ ErrorDetailsThrowsInDestructor{});
}
static void parse_key(
@@ -81,7 +81,7 @@ namespace aes
, mode(AES_ECB)
{
aes_box_init(&impl, algorithm, &key, mode, nullptr,
- ErrorDetailsThrowsInDestructor());
+ ErrorDetailsThrowsInDestructor{});
}
Box(Algorithm algorithm, const Key& key, Mode mode, const Block& iv)
@@ -89,21 +89,21 @@ namespace aes
, mode(mode)
{
aes_box_init(&impl, algorithm, &key, mode, &iv,
- ErrorDetailsThrowsInDestructor());
+ ErrorDetailsThrowsInDestructor{});
}
void encrypt_block(const Block& plaintext, Block& ciphertext)
{
aes_box_encrypt_block(
&impl, &plaintext, &ciphertext,
- ErrorDetailsThrowsInDestructor());
+ ErrorDetailsThrowsInDestructor{});
}
void decrypt_block(const Block& ciphertext, Block& plaintext)
{
aes_box_decrypt_block(
&impl, &ciphertext, &plaintext,
- ErrorDetailsThrowsInDestructor());
+ ErrorDetailsThrowsInDestructor{});
}
std::vector<unsigned char> encrypt_buffer(
@@ -118,7 +118,7 @@ namespace aes
src_size,
nullptr,
&dest_size,
- aes::ErrorDetailsThrowsInDestructor());
+ aes::ErrorDetailsThrowsInDestructor{});
std::vector<unsigned char> dest_buf;
dest_buf.resize(dest_size);
@@ -129,7 +129,7 @@ namespace aes
src_size,
dest_buf.data(),
&dest_size,
- aes::ErrorDetailsThrowsInDestructor());
+ aes::ErrorDetailsThrowsInDestructor{});
dest_buf.resize(dest_size);
return dest_buf;
@@ -147,7 +147,7 @@ namespace aes
src_size,
nullptr,
&dest_size,
- aes::ErrorDetailsThrowsInDestructor());
+ aes::ErrorDetailsThrowsInDestructor{});
std::vector<unsigned char> dest_buf;
dest_buf.resize(dest_size);
@@ -158,7 +158,7 @@ namespace aes
src_size,
dest_buf.data(),
&dest_size,
- aes::ErrorDetailsThrowsInDestructor());
+ aes::ErrorDetailsThrowsInDestructor{});
dest_buf.resize(dest_size);
return dest_buf;
diff --git a/cxx/include/aesxx/error.hpp b/cxx/include/aesxx/error.hpp
index 384a637..21f65d5 100644
--- a/cxx/include/aesxx/error.hpp
+++ b/cxx/include/aesxx/error.hpp
@@ -27,15 +27,20 @@ namespace aes
{
public:
Error(const AES_ErrorDetails& err_details)
- : std::runtime_error(format_error_message(err_details))
+ : std::runtime_error{format_error_message(err_details)}
{
- copy_call_stack(err_details);
+ fill_call_stack(err_details);
}
- void for_each_in_call_stack(const std::function<void (void*, const std::string&)>& callback) const
+ void for_each_addr(
+ const std::function<void (const void*, const std::string&)>& callback) const
{
aux::CallStackFormatter formatter;
- std::for_each(call_stack, call_stack + call_stack_size, [&formatter, &callback] (void* addr)
+
+ std::for_each(
+ call_stack,
+ call_stack + call_stack_len,
+ [&formatter, &callback] (const void* addr)
{
callback(addr, formatter.format_address(addr));
});
@@ -47,24 +52,28 @@ namespace aes
std::vector<char> buf;
buf.resize(aes_format_error(&err_details, NULL, 0));
aes_format_error(&err_details, buf.data(), buf.size());
- return { buf.begin(), buf.end() };
+ return {buf.begin(), buf.end()};
}
- void copy_call_stack(const AES_ErrorDetails& err_details)
+ void fill_call_stack(const AES_ErrorDetails& err_details)
{
- call_stack_size = err_details.call_stack_size;
- std::memcpy(call_stack, err_details.call_stack, call_stack_size * sizeof(void*));
+ call_stack_len = err_details.call_stack_len;
+
+ if (call_stack_len > AES_MAX_CALL_STACK_LENGTH)
+ call_stack_len = AES_MAX_CALL_STACK_LENGTH;
+
+ std::memcpy(call_stack, err_details.call_stack, call_stack_len * sizeof(const void*));
}
- void* call_stack[AES_MAX_CALL_STACK_LENGTH];
- std::size_t call_stack_size;
+ std::size_t call_stack_len = 0;
+ const void* call_stack[AES_MAX_CALL_STACK_LENGTH] = {nullptr};
};
- std::ostream& operator<<(std::ostream& os, const Error& e)
+ std::ostream& operator<<(std::ostream& os, const Error& error)
{
- os << "AES error: " << e.what() << '\n';
+ os << "AES error: " << error.what() << '\n';
os << "Call stack:\n";
- e.for_each_in_call_stack([&os] (void* addr, const std::string& name)
+ error.for_each_addr([&os] (const void* addr, const std::string& name)
{
os << '\t' << addr << ' ' << name << '\n';
});