From 2a3e60f45d6d91f1c3bfcff45f38706cc07ce773 Mon Sep 17 00:00:00 2001 From: Rene Meusel Date: Tue, 23 Jan 2024 10:29:02 +0100 Subject: [PATCH] StrongAdapter<> for bitvector --- .../pubkey/classic_mceliece/cmce_decaps.cpp | 3 +- .../classic_mceliece/cmce_field_ordering.cpp | 4 +- .../classic_mceliece/cmce_keys_internal.cpp | 2 +- .../pubkey/classic_mceliece/cmce_matrix.cpp | 4 +- src/lib/utils/bitvector.h | 218 ++++++++++++++---- src/tests/test_utils_bitvector.cpp | 63 ++++- 6 files changed, 236 insertions(+), 58 deletions(-) diff --git a/src/lib/pubkey/classic_mceliece/cmce_decaps.cpp b/src/lib/pubkey/classic_mceliece/cmce_decaps.cpp index 790c22a3ba5..08230b9282c 100644 --- a/src/lib/pubkey/classic_mceliece/cmce_decaps.cpp +++ b/src/lib/pubkey/classic_mceliece/cmce_decaps.cpp @@ -92,7 +92,8 @@ std::pair, secure_bitvector> Classic_McEliece_Decryptor::decod BOTAN_ASSERT(big_c.size() == m_key->params().m() * m_key->params().t(), "Correct ciphertext input size"); big_c.resize(m_key->params().n()); - auto syndrome = compute_goppa_syndrome(m_key->params(), m_key->g(), m_key->field_ordering(), big_c.as_locked()); + auto syndrome = + compute_goppa_syndrome(m_key->params(), m_key->g(), m_key->field_ordering(), big_c.as()); auto locator = berlekamp_massey(m_key->params(), syndrome); std::vector images; diff --git a/src/lib/pubkey/classic_mceliece/cmce_field_ordering.cpp b/src/lib/pubkey/classic_mceliece/cmce_field_ordering.cpp index 386d3f37a91..c80969066dc 100644 --- a/src/lib/pubkey/classic_mceliece/cmce_field_ordering.cpp +++ b/src/lib/pubkey/classic_mceliece/cmce_field_ordering.cpp @@ -310,8 +310,8 @@ void Classic_McEliece_Field_Ordering::permute_with_pivots(const Classic_McEliece for(size_t p_idx = 1; p_idx <= Classic_McEliece_Parameters::mu(); ++p_idx) { size_t p_counter = 0; for(size_t col = 0; col < Classic_McEliece_Parameters::nu(); ++col) { - auto mask_is_pivot_set = CT::Mask::expand(pivots.get().at(col).as()); - p_counter += CT::Mask::expand(pivots.get().at(col).as()).if_set_return(1); + auto mask_is_pivot_set = CT::Mask::expand(pivots.at(col).as()); + p_counter += CT::Mask::expand(pivots.at(col).as()).if_set_return(1); auto mask_is_current_pivot = CT::Mask::is_equal(p_idx, p_counter); (mask_is_pivot_set & mask_is_current_pivot) .conditional_swap(m_pi.get().at(col_offset + col), m_pi.get().at(col_offset + p_idx - 1)); diff --git a/src/lib/pubkey/classic_mceliece/cmce_keys_internal.cpp b/src/lib/pubkey/classic_mceliece/cmce_keys_internal.cpp index 56d0cd42bf2..6e0bc36897c 100644 --- a/src/lib/pubkey/classic_mceliece/cmce_keys_internal.cpp +++ b/src/lib/pubkey/classic_mceliece/cmce_keys_internal.cpp @@ -108,7 +108,7 @@ std::shared_ptr Classic_McEliece_PublicKeyIn throw Decoding_Error("Cannot create public key from private key. Private key is invalid."); } auto& [pk_matrix, pivot] = pk_matrix_and_pivot.value(); - if(!pivot.get().subvector(0, pivot.size() / 2).all() || !pivot.get().subvector(pivot.size() / 2).none()) { + if(!pivot.subvector(0, pivot.size() / 2).all() || !pivot.subvector(pivot.size() / 2).none()) { // There should not be a pivot other than 0xff ff ff ff 00 00 00 00. Otherwise // the gauss algorithm failed effectively. throw Decoding_Error("Cannot create public key from private key. Private key is invalid."); diff --git a/src/lib/pubkey/classic_mceliece/cmce_matrix.cpp b/src/lib/pubkey/classic_mceliece/cmce_matrix.cpp index 3910afaa6ca..eea7185f77d 100644 --- a/src/lib/pubkey/classic_mceliece/cmce_matrix.cpp +++ b/src/lib/pubkey/classic_mceliece/cmce_matrix.cpp @@ -121,7 +121,7 @@ std::optional move_columns(std::vector& mat, for(auto pivot_idx : pivot_indices) { for(size_t i = 0; i < Classic_McEliece_Parameters::nu(); ++i) { auto mask_is_at_current_idx = Botan::CT::Mask::is_equal(i, pivot_idx); - pivots.get().at(i) = mask_is_at_current_idx.select(1, pivots.get().at(i).as()); + pivots.at(i) = mask_is_at_current_idx.select(1, pivots.at(i).as()); } } @@ -252,6 +252,6 @@ bitvector Classic_McEliece_Matrix::mul(const Classic_McEliece_Parameters& params } BOTAN_ASSERT_NOMSG(pk_slicer.empty()); - return s.as_unlocked(); + return s.as(); } } // namespace Botan diff --git a/src/lib/utils/bitvector.h b/src/lib/utils/bitvector.h index 2c127191722..5a6c6020ab5 100644 --- a/src/lib/utils/bitvector.h +++ b/src/lib/utils/bitvector.h @@ -2,8 +2,8 @@ * An abstraction for an arbitrarily large bitvector that can * optionally use the secure_allocator. * - * (C) 2023 Jack Lloyd - * (C) 2023 René Meusel, Rohde & Schwarz Cybersecurity + * (C) 2023-2024 Jack Lloyd + * (C) 2023-2024 René Meusel, Rohde & Schwarz Cybersecurity * * Botan is released under the Simplified BSD License (see license.txt) */ @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -29,6 +30,21 @@ namespace Botan { +template