From 9d07e2a9ea4e4b9994ecbf8fe4f886d9223916e2 Mon Sep 17 00:00:00 2001 From: Fabian Albert Date: Thu, 28 Sep 2023 12:43:43 +0200 Subject: [PATCH] use checked_cast_to --- src/lib/pubkey/hss_lms/hss.cpp | 5 +++-- src/lib/pubkey/hss_lms/hss.h | 3 ++- src/lib/pubkey/hss_lms/lm_ots.cpp | 16 ++++++++-------- src/lib/pubkey/hss_lms/lms.cpp | 13 +++++++------ 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/lib/pubkey/hss_lms/hss.cpp b/src/lib/pubkey/hss_lms/hss.cpp index 820f5fefef2..42b9f5516c9 100644 --- a/src/lib/pubkey/hss_lms/hss.cpp +++ b/src/lib/pubkey/hss_lms/hss.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -89,8 +90,8 @@ HSS_LMS_Params::HSS_LMS_Params(std::string_view algo_params) { SCAN_Name scan_layer(scan.arg(i)); BOTAN_ARG_CHECK(scan_layer.algo_name() == "HW", "Invalid name for layer parameters"); BOTAN_ARG_CHECK(scan_layer.arg_count() == 2, "Invalid number of layer parameters"); - const auto h = scan_layer.arg_as_integer(0); - const auto w = static_cast(scan_layer.arg_as_integer(1)); + const auto h = checked_cast_to(scan_layer.arg_as_integer(0)); + const auto w = checked_cast_to(scan_layer.arg_as_integer(1)); m_lms_lmots_params.push_back({LMS_Params::create_or_throw(hash, h), LMOTS_Params::create_or_throw(hash, w)}); } m_max_sig_count = calc_max_sig_count(); diff --git a/src/lib/pubkey/hss_lms/hss.h b/src/lib/pubkey/hss_lms/hss.h index 8099ff2230f..a13147fa4ba 100644 --- a/src/lib/pubkey/hss_lms/hss.h +++ b/src/lib/pubkey/hss_lms/hss.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -99,7 +100,7 @@ class BOTAN_TEST_API HSS_LMS_Params final { /** * @brief Returns the number of layers the HSS tree has. */ - HSS_Level L() const { return HSS_Level(static_cast(m_lms_lmots_params.size())); } + HSS_Level L() const { return HSS_Level(checked_cast_to(m_lms_lmots_params.size())); } /** * @brief The maximal number of signatures allowed for these HSS parameters diff --git a/src/lib/pubkey/hss_lms/lm_ots.cpp b/src/lib/pubkey/hss_lms/lm_ots.cpp index a4516e494a8..f0486c8258f 100644 --- a/src/lib/pubkey/hss_lms/lm_ots.cpp +++ b/src/lib/pubkey/hss_lms/lm_ots.cpp @@ -110,22 +110,22 @@ std::vector gen_Q_with_cksm(const LMOTS_Params& params, } // namespace LMOTS_Params LMOTS_Params::create_or_throw(LMOTS_Algorithm_Type type) { - uint8_t type_value = static_cast(type); + uint8_t type_value = checked_cast_to(type); if(type >= LMOTS_Algorithm_Type::SHA256_N32_W1 && type <= LMOTS_Algorithm_Type::SHA256_N32_W8) { - uint8_t w = 1 << (type_value - static_cast(LMOTS_Algorithm_Type::SHA256_N32_W1)); + uint8_t w = 1 << (type_value - checked_cast_to(LMOTS_Algorithm_Type::SHA256_N32_W1)); return LMOTS_Params(type, "SHA-256", w); } if(type >= LMOTS_Algorithm_Type::SHA256_N24_W1 && type <= LMOTS_Algorithm_Type::SHA256_N24_W8) { - uint8_t w = 1 << (type_value - static_cast(LMOTS_Algorithm_Type::SHA256_N24_W1)); + uint8_t w = 1 << (type_value - checked_cast_to(LMOTS_Algorithm_Type::SHA256_N24_W1)); return LMOTS_Params(type, "Truncated(SHA-256,192)", w); } if(type >= LMOTS_Algorithm_Type::SHAKE_N32_W1 && type <= LMOTS_Algorithm_Type::SHAKE_N32_W8) { - uint8_t w = 1 << (type_value - static_cast(LMOTS_Algorithm_Type::SHAKE_N32_W1)); + uint8_t w = 1 << (type_value - checked_cast_to(LMOTS_Algorithm_Type::SHAKE_N32_W1)); return LMOTS_Params(type, "SHAKE-256(256)", w); } if(type >= LMOTS_Algorithm_Type::SHAKE_N24_W1 && type <= LMOTS_Algorithm_Type::SHAKE_N24_W8) { - uint8_t w = 1 << (type_value - static_cast(LMOTS_Algorithm_Type::SHAKE_N24_W1)); + uint8_t w = 1 << (type_value - checked_cast_to(LMOTS_Algorithm_Type::SHAKE_N24_W1)); return LMOTS_Params(type, "SHAKE-256(192)", w); } @@ -148,7 +148,7 @@ LMOTS_Params LMOTS_Params::create_or_throw(std::string_view hash_name, uint8_t w } else { throw Decoding_Error("Unsupported hash function"); } - auto type = static_cast(static_cast(base_type) + type_offset); + auto type = checked_cast_to(checked_cast_to(base_type) + type_offset); return LMOTS_Params(type, hash_name, w); } @@ -159,8 +159,8 @@ LMOTS_Params::LMOTS_Params(LMOTS_Algorithm_Type algorithm_type, std::string_view // RFC 8553 Appendix B - Parameter Computation auto u = ceil_division(8 * m_n, m_w); // ceil(8*n/w) auto v = ceil_division(high_bit(((1 << m_w) - 1) * u), m_w); // ceil((floor(lg[(2^w - 1) * u]) + 1) / w) - m_ls = static_cast(16 - (v * w)); - m_p = static_cast(u + v); + m_ls = checked_cast_to(16 - (v * w)); + m_p = checked_cast_to(u + v); } LMOTS_Signature::LMOTS_Signature(LMOTS_Algorithm_Type lmots_type, diff --git a/src/lib/pubkey/hss_lms/lms.cpp b/src/lib/pubkey/hss_lms/lms.cpp index e0699fddaea..d5c6fdcbd56 100644 --- a/src/lib/pubkey/hss_lms/lms.cpp +++ b/src/lib/pubkey/hss_lms/lms.cpp @@ -9,6 +9,7 @@ #include #include +#include namespace Botan { namespace { @@ -119,22 +120,22 @@ void lms_treehash(StrongSpan out_root, } // namespace LMS_Params LMS_Params::create_or_throw(LMS_Algorithm_Type type) { - uint8_t type_value = static_cast(type); + uint8_t type_value = checked_cast_to(type); if(type >= LMS_Algorithm_Type::SHA256_M32_H5 && type <= LMS_Algorithm_Type::SHA256_M32_H25) { - uint8_t h = 5 * (type_value - static_cast(LMS_Algorithm_Type::SHA256_M32_H5) + 1); + uint8_t h = 5 * (type_value - checked_cast_to(LMS_Algorithm_Type::SHA256_M32_H5) + 1); return LMS_Params(type, "SHA-256", h); } if(type >= LMS_Algorithm_Type::SHA256_M24_H5 && type <= LMS_Algorithm_Type::SHA256_M24_H25) { - uint8_t h = 5 * (type_value - static_cast(LMS_Algorithm_Type::SHA256_M24_H5) + 1); + uint8_t h = 5 * (type_value - checked_cast_to(LMS_Algorithm_Type::SHA256_M24_H5) + 1); return LMS_Params(type, "Truncated(SHA-256,192)", h); } if(type >= LMS_Algorithm_Type::SHAKE_M32_H5 && type <= LMS_Algorithm_Type::SHAKE_M32_H25) { - uint8_t h = 5 * (type_value - static_cast(LMS_Algorithm_Type::SHAKE_M32_H5) + 1); + uint8_t h = 5 * (type_value - checked_cast_to(LMS_Algorithm_Type::SHAKE_M32_H5) + 1); return LMS_Params(type, "SHAKE-256(256)", h); } if(type >= LMS_Algorithm_Type::SHAKE_M24_H5 && type <= LMS_Algorithm_Type::SHAKE_M24_H25) { - uint8_t h = 5 * (type_value - static_cast(LMS_Algorithm_Type::SHAKE_M24_H5) + 1); + uint8_t h = 5 * (type_value - checked_cast_to(LMS_Algorithm_Type::SHAKE_M24_H5) + 1); return LMS_Params(type, "SHAKE-256(192)", h); } @@ -157,7 +158,7 @@ LMS_Params LMS_Params::create_or_throw(std::string_view hash_name, size_t h) { } else { throw Decoding_Error("Unsupported hash function"); } - auto type = static_cast(static_cast(base_type) + type_offset); + auto type = checked_cast_to(checked_cast_to(base_type) + type_offset); return LMS_Params(type, hash_name, h); }