diff options
Diffstat (limited to 'src/lib/pubkey/mce')
-rw-r--r-- | src/lib/pubkey/mce/code_based_key_gen.cpp | 56 | ||||
-rw-r--r-- | src/lib/pubkey/mce/code_based_util.h | 8 | ||||
-rw-r--r-- | src/lib/pubkey/mce/gf2m_rootfind_dcmp.cpp | 48 | ||||
-rw-r--r-- | src/lib/pubkey/mce/gf2m_small_m.cpp | 10 | ||||
-rw-r--r-- | src/lib/pubkey/mce/gf2m_small_m.h | 8 | ||||
-rw-r--r-- | src/lib/pubkey/mce/goppa_code.cpp | 62 | ||||
-rw-r--r-- | src/lib/pubkey/mce/mce_internal.h | 28 | ||||
-rw-r--r-- | src/lib/pubkey/mce/mceliece.cpp | 52 | ||||
-rw-r--r-- | src/lib/pubkey/mce/mceliece.h | 42 | ||||
-rw-r--r-- | src/lib/pubkey/mce/mceliece_key.cpp | 82 | ||||
-rw-r--r-- | src/lib/pubkey/mce/polyn_gf2m.cpp | 70 | ||||
-rw-r--r-- | src/lib/pubkey/mce/polyn_gf2m.h | 22 |
12 files changed, 244 insertions, 244 deletions
diff --git a/src/lib/pubkey/mce/code_based_key_gen.cpp b/src/lib/pubkey/mce/code_based_key_gen.cpp index 839ebc977..4d68c875a 100644 --- a/src/lib/pubkey/mce/code_based_key_gen.cpp +++ b/src/lib/pubkey/mce/code_based_key_gen.cpp @@ -22,27 +22,27 @@ namespace { struct binary_matrix { public: - binary_matrix(u32bit m_rown, u32bit m_coln); + binary_matrix(uint32_t m_rown, uint32_t m_coln); - void row_xor(u32bit a, u32bit b); + void row_xor(uint32_t a, uint32_t b); secure_vector<int> row_reduced_echelon_form(); /** * return the coefficient out of F_2 */ - u32bit coef(u32bit i, u32bit j) + uint32_t coef(uint32_t i, uint32_t j) { return (m_elem[(i) * m_rwdcnt + (j) / 32] >> (j % 32)) & 1; }; - void set_coef_to_one(u32bit i, u32bit j) + void set_coef_to_one(uint32_t i, uint32_t j) { - m_elem[(i) * m_rwdcnt + (j) / 32] |= (static_cast<u32bit>(1) << ((j) % 32)) ; + m_elem[(i) * m_rwdcnt + (j) / 32] |= (static_cast<uint32_t>(1) << ((j) % 32)) ; }; - void toggle_coeff(u32bit i, u32bit j) + void toggle_coeff(uint32_t i, uint32_t j) { - m_elem[(i) * m_rwdcnt + (j) / 32] ^= (static_cast<u32bit>(1) << ((j) % 32)) ; + m_elem[(i) * m_rwdcnt + (j) / 32] ^= (static_cast<uint32_t>(1) << ((j) % 32)) ; } void set_to_zero() @@ -51,23 +51,23 @@ struct binary_matrix } //private: - u32bit m_rown; // number of rows. - u32bit m_coln; // number of columns. - u32bit m_rwdcnt; // number of words in a row - std::vector<u32bit> m_elem; + uint32_t m_rown; // number of rows. + uint32_t m_coln; // number of columns. + uint32_t m_rwdcnt; // number of words in a row + std::vector<uint32_t> m_elem; }; -binary_matrix::binary_matrix (u32bit rown, u32bit coln) +binary_matrix::binary_matrix (uint32_t rown, uint32_t coln) { m_coln = coln; m_rown = rown; m_rwdcnt = 1 + ((m_coln - 1) / 32); - m_elem = std::vector<u32bit>(m_rown * m_rwdcnt); + m_elem = std::vector<uint32_t>(m_rown * m_rwdcnt); } -void binary_matrix::row_xor(u32bit a, u32bit b) +void binary_matrix::row_xor(uint32_t a, uint32_t b) { - u32bit i; + uint32_t i; for(i=0;i<m_rwdcnt;i++) { m_elem[a*m_rwdcnt+i]^=m_elem[b*m_rwdcnt+i]; @@ -77,7 +77,7 @@ void binary_matrix::row_xor(u32bit a, u32bit b) //the matrix is reduced from LSB...(from right) secure_vector<int> binary_matrix::row_reduced_echelon_form() { - u32bit i, failcnt, findrow, max=m_coln - 1; + uint32_t i, failcnt, findrow, max=m_coln - 1; secure_vector<int> perm(m_coln); for(i=0;i<m_coln;i++) @@ -89,7 +89,7 @@ secure_vector<int> binary_matrix::row_reduced_echelon_form() for(i=0;i<m_rown;i++,max--) { findrow=0; - for(u32bit j=i;j<m_rown;j++) + for(uint32_t j=i;j<m_rown;j++) { if(coef(j,max)) { @@ -115,7 +115,7 @@ secure_vector<int> binary_matrix::row_reduced_echelon_form() else { perm[i+m_coln - m_rown] = max; - for(u32bit j=i+1;j<m_rown;j++)//fill the column downwards with 0's + for(uint32_t j=i+1;j<m_rown;j++)//fill the column downwards with 0's { if(coef(j,(max))) { @@ -137,7 +137,7 @@ secure_vector<int> binary_matrix::row_reduced_echelon_form() void randomize_support(std::vector<gf2m>& L, RandomNumberGenerator& rng) { - for(u32bit i = 0; i != L.size(); ++i) + for(uint32_t i = 0; i != L.size(); ++i) { gf2m rnd = random_gf2m(rng); @@ -146,7 +146,7 @@ void randomize_support(std::vector<gf2m>& L, RandomNumberGenerator& rng) } } -std::unique_ptr<binary_matrix> generate_R(std::vector<gf2m> &L, polyn_gf2m* g, std::shared_ptr<GF2m_Field> sp_field, u32bit code_length, u32bit t ) +std::unique_ptr<binary_matrix> generate_R(std::vector<gf2m> &L, polyn_gf2m* g, std::shared_ptr<GF2m_Field> sp_field, uint32_t code_length, uint32_t t ) { //L- Support //t- Number of errors @@ -154,7 +154,7 @@ std::unique_ptr<binary_matrix> generate_R(std::vector<gf2m> &L, polyn_gf2m* g, s //m- The extension degree of the GF //g- The generator polynomial. gf2m x,y; - u32bit i,j,k,r,n; + uint32_t i,j,k,r,n; std::vector<int> Laux(code_length); n=code_length; r=t*sp_field->get_extension_degree(); @@ -210,12 +210,12 @@ std::unique_ptr<binary_matrix> generate_R(std::vector<gf2m> &L, polyn_gf2m* g, s } } -McEliece_PrivateKey generate_mceliece_key( RandomNumberGenerator & rng, u32bit ext_deg, u32bit code_length, u32bit t) +McEliece_PrivateKey generate_mceliece_key( RandomNumberGenerator & rng, uint32_t ext_deg, uint32_t code_length, uint32_t t) { - u32bit i, j, k, l; + uint32_t i, j, k, l; std::unique_ptr<binary_matrix> R; - u32bit codimension = t * ext_deg; + uint32_t codimension = t * ext_deg; if(code_length <= codimension) { throw Invalid_Argument("invalid McEliece parameters"); @@ -256,15 +256,15 @@ McEliece_PrivateKey generate_mceliece_key( RandomNumberGenerator & rng, u32bit e // speed up the syndrome computation) // // - std::vector<u32bit> H(bit_size_to_32bit_size(codimension) * code_length ); - u32bit* sk = H.data(); + std::vector<uint32_t> H(bit_size_to_32bit_size(codimension) * code_length ); + uint32_t* sk = H.data(); for (i = 0; i < code_length; ++i) { for (l = 0; l < t; ++l) { k = (l * ext_deg) / 32; j = (l * ext_deg) % 32; - sk[k] ^= static_cast<u32bit>(F[i].get_coef(l)) << j; + sk[k] ^= static_cast<uint32_t>(F[i].get_coef(l)) << j; if (j + ext_deg > 32) { sk[k + 1] ^= F[i].get_coef( l) >> (32 - j); @@ -281,7 +281,7 @@ McEliece_PrivateKey generate_mceliece_key( RandomNumberGenerator & rng, u32bit e { Linv[L[i]] = i; } - std::vector<byte> pubmat (R->m_elem.size() * 4); + std::vector<uint8_t> pubmat (R->m_elem.size() * 4); for(i = 0; i < R->m_elem.size(); i++) { store_le(R->m_elem[i], &pubmat[i*4]); diff --git a/src/lib/pubkey/mce/code_based_util.h b/src/lib/pubkey/mce/code_based_util.h index 9b5395f41..ccc94c91b 100644 --- a/src/lib/pubkey/mce/code_based_util.h +++ b/src/lib/pubkey/mce/code_based_util.h @@ -22,9 +22,9 @@ namespace Botan { * @return the mask 0xFFFF if tst is non-zero and 0 otherwise */ template<typename T> -u16bit expand_mask_16bit(T tst) +uint16_t expand_mask_16bit(T tst) { - const u16bit result = (tst != 0); + const uint16_t result = (tst != 0); return ~(result - 1); } @@ -42,12 +42,12 @@ inline gf2m lex_to_gray(gf2m lex) return (lex >> 1) ^ lex; } -inline u32bit bit_size_to_byte_size(u32bit bit_size) +inline uint32_t bit_size_to_byte_size(uint32_t bit_size) { return (bit_size - 1) / 8 + 1; } -inline u32bit bit_size_to_32bit_size(u32bit bit_size) +inline uint32_t bit_size_to_32bit_size(uint32_t bit_size) { return (bit_size - 1) / 32 + 1; } diff --git a/src/lib/pubkey/mce/gf2m_rootfind_dcmp.cpp b/src/lib/pubkey/mce/gf2m_rootfind_dcmp.cpp index 74cb1c64b..a35fc7458 100644 --- a/src/lib/pubkey/mce/gf2m_rootfind_dcmp.cpp +++ b/src/lib/pubkey/mce/gf2m_rootfind_dcmp.cpp @@ -15,11 +15,11 @@ namespace Botan { namespace { -u32bit patch_root_array(gf2m* res_root_arr, - u32bit res_root_arr_len, - u32bit root_pos) +uint32_t patch_root_array(gf2m* res_root_arr, + uint32_t res_root_arr_len, + uint32_t root_pos) { - volatile u32bit i; + volatile uint32_t i; volatile gf2m patch_elem = 0x01; volatile gf2m cond_mask = (root_pos == res_root_arr_len); cond_mask = expand_mask_16bit(cond_mask); @@ -37,18 +37,18 @@ u32bit patch_root_array(gf2m* res_root_arr, class gf2m_decomp_rootfind_state { public: - gf2m_decomp_rootfind_state(const polyn_gf2m & p_polyn, u32bit code_length); + gf2m_decomp_rootfind_state(const polyn_gf2m & p_polyn, uint32_t code_length); void calc_LiK(const polyn_gf2m & sigma); gf2m calc_Fxj_j_neq_0( const polyn_gf2m & sigma, gf2m j_gray); void calc_next_Aij(); void calc_Ai_zero(const polyn_gf2m & sigma); secure_vector<gf2m> find_roots(const polyn_gf2m & sigma); - u32bit get_code_length() const { return code_length; }; - u32bit code_length; + uint32_t get_code_length() const { return code_length; }; + uint32_t code_length; secure_vector<gf2m> m_Lik; // size is outer_summands * m secure_vector<gf2m> m_Aij; // ... - u32bit m_outer_summands; + uint32_t m_outer_summands; gf2m m_j; gf2m m_j_gray; gf2m m_sigma_3_l; @@ -73,9 +73,9 @@ gf2m brootf_decomp__gray_to_lex(gf2m gray) /** * calculates ceil((t-4)/5) = outer_summands - 1 */ -u32bit brootf_decomp__calc_sum_limit(u32bit t) +uint32_t brootf_decomp__calc_sum_limit(uint32_t t) { - u32bit result; + uint32_t result; if(t < 4) { return 0; @@ -86,7 +86,7 @@ u32bit brootf_decomp__calc_sum_limit(u32bit t) return result; } -gf2m_decomp_rootfind_state::gf2m_decomp_rootfind_state(const polyn_gf2m & polyn, u32bit the_code_length) : +gf2m_decomp_rootfind_state::gf2m_decomp_rootfind_state(const polyn_gf2m & polyn, uint32_t the_code_length) : code_length(the_code_length), m_j(0), m_j_gray(0) { gf2m coeff_3; @@ -119,7 +119,7 @@ gf2m_decomp_rootfind_state::gf2m_decomp_rootfind_state(const polyn_gf2m & polyn, void gf2m_decomp_rootfind_state::calc_Ai_zero(const polyn_gf2m & sigma) { - u32bit i; + uint32_t i; /* * this function assumes this the first gray code element is zero */ @@ -138,9 +138,9 @@ void gf2m_decomp_rootfind_state::calc_next_Aij() * first thing, we declare Aij Aij_minusone and increase j. * Case j=0 upon function entry also included, then Aij contains A_{i,j=0}. */ - u32bit i; + uint32_t i; gf2m diff, new_j_gray; - u32bit Lik_pos_base; + uint32_t Lik_pos_base; this->m_j++; @@ -190,11 +190,11 @@ void gf2m_decomp_rootfind_state::calc_next_Aij() void gf2m_decomp_rootfind_state::calc_LiK(const polyn_gf2m & sigma) { std::shared_ptr<GF2m_Field> sp_field = sigma.get_sp_field(); - u32bit i, k, d; + uint32_t i, k, d; d = sigma.get_degree(); for(k = 0; k < sp_field->get_extension_degree(); k++) { - u32bit Lik_pos_base = k * this->m_outer_summands; + uint32_t Lik_pos_base = k * this->m_outer_summands; gf2m alpha_l_k_tt2_ttj[4]; alpha_l_k_tt2_ttj[0] = sp_field->gf_l_from_n(static_cast<gf2m>(1) << k); alpha_l_k_tt2_ttj[1] = sp_field->gf_mul_rrr(alpha_l_k_tt2_ttj[0], alpha_l_k_tt2_ttj[0]); @@ -203,14 +203,14 @@ void gf2m_decomp_rootfind_state::calc_LiK(const polyn_gf2m & sigma) alpha_l_k_tt2_ttj[3] = sp_field->gf_mul_rrr(alpha_l_k_tt2_ttj[2], alpha_l_k_tt2_ttj[2]); for(i = 0; i < this->m_outer_summands; i++) { - u32bit j; - u32bit five_i = 5*i; - u32bit Lik_pos = Lik_pos_base + i; + uint32_t j; + uint32_t five_i = 5*i; + uint32_t Lik_pos = Lik_pos_base + i; this->m_Lik[Lik_pos] = 0; for(j = 0; j <= 3; j++) { gf2m f, x; - u32bit f_ind = five_i + (static_cast<u32bit>(1) << j); + uint32_t f_ind = five_i + (static_cast<uint32_t>(1) << j); if(f_ind > d) { break; @@ -228,7 +228,7 @@ gf2m gf2m_decomp_rootfind_state::calc_Fxj_j_neq_0( const polyn_gf2m & sigma, gf2 { //needs the A_{ij} to compute F(x)_j gf2m sum = 0; - u32bit i; + uint32_t i; std::shared_ptr<GF2m_Field> sp_field = sigma.get_sp_field(); const gf2m jl_gray = sp_field->gf_l_from_n(j_gray); gf2m xl_j_tt_5 = sp_field->gf_square_rr(jl_gray); @@ -270,7 +270,7 @@ secure_vector<gf2m> gf2m_decomp_rootfind_state::find_roots(const polyn_gf2m & si const int sigma_degree = sigma.get_degree(); BOTAN_ASSERT(sigma_degree > 0, "Valid sigma"); secure_vector<gf2m> result(sigma_degree); - u32bit root_pos = 0; + uint32_t root_pos = 0; this->calc_Ai_zero(sigma); this->calc_LiK(sigma); @@ -293,7 +293,7 @@ secure_vector<gf2m> gf2m_decomp_rootfind_state::find_roots(const polyn_gf2m & si root_pos++; } - if(this->m_j + static_cast<u32bit>(1) == this->get_code_length()) + if(this->m_j + static_cast<uint32_t>(1) == this->get_code_length()) { break; } @@ -308,7 +308,7 @@ secure_vector<gf2m> gf2m_decomp_rootfind_state::find_roots(const polyn_gf2m & si } // end anonymous namespace -secure_vector<gf2m> find_roots_gf2m_decomp(const polyn_gf2m & polyn, u32bit code_length) +secure_vector<gf2m> find_roots_gf2m_decomp(const polyn_gf2m & polyn, uint32_t code_length) { gf2m_decomp_rootfind_state state(polyn, code_length); return state.find_roots(polyn); diff --git a/src/lib/pubkey/mce/gf2m_small_m.cpp b/src/lib/pubkey/mce/gf2m_small_m.cpp index e74e5c71f..95187c7af 100644 --- a/src/lib/pubkey/mce/gf2m_small_m.cpp +++ b/src/lib/pubkey/mce/gf2m_small_m.cpp @@ -94,14 +94,14 @@ const std::vector<gf2m>& log_table(size_t deg) } -u32bit encode_gf2m(gf2m to_enc, byte* mem) +uint32_t encode_gf2m(gf2m to_enc, uint8_t* mem) { mem[0] = to_enc >> 8; mem[1] = to_enc & 0xFF; return sizeof(to_enc); } -gf2m decode_gf2m(const byte* mem) +gf2m decode_gf2m(const uint8_t* mem) { gf2m result; result = mem[0] << 8; @@ -118,9 +118,9 @@ GF2m_Field::GF2m_Field(size_t extdeg) : m_gf_extension_degree(extdeg), gf2m GF2m_Field::gf_div(gf2m x, gf2m y) const { - const s32bit sub_res = static_cast<s32bit>(gf_log(x) - static_cast<s32bit>(gf_log(y))); - const s32bit modq_res = static_cast<s32bit>(_gf_modq_1(sub_res)); - const s32bit div_res = static_cast<s32bit>(x) ? static_cast<s32bit>(gf_exp(modq_res)) : 0; + const int32_t sub_res = static_cast<int32_t>(gf_log(x) - static_cast<int32_t>(gf_log(y))); + const int32_t modq_res = static_cast<int32_t>(_gf_modq_1(sub_res)); + const int32_t div_res = static_cast<int32_t>(x) ? static_cast<int32_t>(gf_exp(modq_res)) : 0; return static_cast<gf2m>(div_res); } diff --git a/src/lib/pubkey/mce/gf2m_small_m.h b/src/lib/pubkey/mce/gf2m_small_m.h index 595ef3999..d49325def 100644 --- a/src/lib/pubkey/mce/gf2m_small_m.h +++ b/src/lib/pubkey/mce/gf2m_small_m.h @@ -17,7 +17,7 @@ namespace Botan { -typedef u16bit gf2m; +typedef uint16_t gf2m; /** * GF(2^m) field for m = [2...16] @@ -196,7 +196,7 @@ class BOTAN_DLL GF2m_Field } private: - gf2m _gf_modq_1(s32bit d) const + gf2m _gf_modq_1(int32_t d) const { /* residual modulo q-1 when -q < d < 0, we get (q-1+d) @@ -211,9 +211,9 @@ class BOTAN_DLL GF2m_Field const std::vector<gf2m>& m_gf_exp_table; }; -u32bit encode_gf2m(gf2m to_enc, byte* mem); +uint32_t encode_gf2m(gf2m to_enc, uint8_t* mem); -gf2m decode_gf2m(const byte* mem); +gf2m decode_gf2m(const uint8_t* mem); } diff --git a/src/lib/pubkey/mce/goppa_code.cpp b/src/lib/pubkey/mce/goppa_code.cpp index cbec6302a..97cdf947e 100644 --- a/src/lib/pubkey/mce/goppa_code.cpp +++ b/src/lib/pubkey/mce/goppa_code.cpp @@ -16,11 +16,11 @@ namespace Botan { namespace { -void matrix_arr_mul(std::vector<u32bit> matrix, - u32bit numo_rows, - u32bit words_per_row, - const byte* input_vec, - u32bit* output_vec, u32bit output_vec_len) +void matrix_arr_mul(std::vector<uint32_t> matrix, + uint32_t numo_rows, + uint32_t words_per_row, + const uint8_t* input_vec, + uint32_t* output_vec, uint32_t output_vec_len) { for(size_t j = 0; j < numo_rows; j++) { @@ -43,8 +43,8 @@ secure_vector<gf2m> goppa_decode(const polyn_gf2m & syndrom_polyn, const std::vector<gf2m> & Linv) { gf2m a; - u32bit code_length = Linv.size(); - u32bit t = g.get_degree(); + uint32_t code_length = Linv.size(); + uint32_t t = g.get_degree(); std::shared_ptr<GF2m_Field> sp_field = g.get_sp_field(); @@ -63,13 +63,13 @@ secure_vector<gf2m> goppa_decode(const polyn_gf2m & syndrom_polyn, // compute S square root of h (using sqrtmod) polyn_gf2m S(t - 1, g.get_sp_field()); - for(u32bit i=0;i<t;i++) + for(uint32_t i=0;i<t;i++) { a = sp_field->gf_sqrt(h.get_coef(i)); if(i & 1) { - for(u32bit j=0;j<t;j++) + for(uint32_t j=0;j<t;j++) { S.add_to_coef( j, sp_field->gf_mul(a, sqrtmod[i/2].get_coef(j))); } @@ -107,7 +107,7 @@ secure_vector<gf2m> goppa_decode(const polyn_gf2m & syndrom_polyn, size_t d = res.size(); secure_vector<gf2m> result(d); - for(u32bit i = 0; i < d; ++i) + for(uint32_t i = 0; i < d; ++i) { gf2m current = res[i]; @@ -124,18 +124,18 @@ secure_vector<gf2m> goppa_decode(const polyn_gf2m & syndrom_polyn, } } -void mceliece_decrypt(secure_vector<byte>& plaintext_out, - secure_vector<byte>& error_mask_out, - const secure_vector<byte>& ciphertext, +void mceliece_decrypt(secure_vector<uint8_t>& plaintext_out, + secure_vector<uint8_t>& error_mask_out, + const secure_vector<uint8_t>& ciphertext, const McEliece_PrivateKey& key) { mceliece_decrypt(plaintext_out, error_mask_out, ciphertext.data(), ciphertext.size(), key); } void mceliece_decrypt( - secure_vector<byte>& plaintext, - secure_vector<byte> & error_mask, - const byte ciphertext[], + secure_vector<uint8_t>& plaintext, + secure_vector<uint8_t> & error_mask, + const uint8_t ciphertext[], size_t ciphertext_len, const McEliece_PrivateKey & key) { @@ -143,7 +143,7 @@ void mceliece_decrypt( plaintext = mceliece_decrypt(error_pos, ciphertext, ciphertext_len, key); const size_t code_length = key.get_code_length(); - secure_vector<byte> result((code_length+7)/8); + secure_vector<uint8_t> result((code_length+7)/8); for(auto&& pos : error_pos) { if(pos > code_length) @@ -160,40 +160,40 @@ void mceliece_decrypt( * @p p_err_pos_len must point to the available length of @p error_pos on input, the * function will set it to the actual number of errors returned in the @p error_pos * array */ -secure_vector<byte> mceliece_decrypt( +secure_vector<uint8_t> mceliece_decrypt( secure_vector<gf2m> & error_pos, - const byte *ciphertext, u32bit ciphertext_len, + const uint8_t *ciphertext, uint32_t ciphertext_len, const McEliece_PrivateKey & key) { - u32bit dimension = key.get_dimension(); - u32bit codimension = key.get_codimension(); - u32bit t = key.get_goppa_polyn().get_degree(); + uint32_t dimension = key.get_dimension(); + uint32_t codimension = key.get_codimension(); + uint32_t t = key.get_goppa_polyn().get_degree(); polyn_gf2m syndrome_polyn(key.get_goppa_polyn().get_sp_field()); // init as zero polyn const unsigned unused_pt_bits = dimension % 8; - const byte unused_pt_bits_mask = (1 << unused_pt_bits) - 1; + const uint8_t unused_pt_bits_mask = (1 << unused_pt_bits) - 1; if(ciphertext_len != (key.get_code_length()+7)/8) { throw Invalid_Argument("wrong size of McEliece ciphertext"); } - u32bit cleartext_len = (key.get_message_word_bit_length()+7)/8; + uint32_t cleartext_len = (key.get_message_word_bit_length()+7)/8; if(cleartext_len != bit_size_to_byte_size(dimension)) { throw Invalid_Argument("mce-decryption: wrong length of cleartext buffer"); } - secure_vector<u32bit> syndrome_vec(bit_size_to_32bit_size(codimension)); + secure_vector<uint32_t> syndrome_vec(bit_size_to_32bit_size(codimension)); matrix_arr_mul(key.get_H_coeffs(), key.get_code_length(), bit_size_to_32bit_size(codimension), ciphertext, syndrome_vec.data(), syndrome_vec.size()); - secure_vector<byte> syndrome_byte_vec(bit_size_to_byte_size(codimension)); - u32bit syndrome_byte_vec_size = syndrome_byte_vec.size(); - for(u32bit i = 0; i < syndrome_byte_vec_size; i++) + secure_vector<uint8_t> syndrome_byte_vec(bit_size_to_byte_size(codimension)); + uint32_t syndrome_byte_vec_size = syndrome_byte_vec.size(); + for(uint32_t i = 0; i < syndrome_byte_vec_size; i++) { syndrome_byte_vec[i] = syndrome_vec[i/4] >> (8* (i % 4)); } @@ -203,12 +203,12 @@ secure_vector<byte> mceliece_decrypt( syndrome_polyn.get_degree(); error_pos = goppa_decode(syndrome_polyn, key.get_goppa_polyn(), key.get_sqrtmod(), key.get_Linv()); - u32bit nb_err = error_pos.size(); + uint32_t nb_err = error_pos.size(); - secure_vector<byte> cleartext(cleartext_len); + secure_vector<uint8_t> cleartext(cleartext_len); copy_mem(cleartext.data(), ciphertext, cleartext_len); - for(u32bit i = 0; i < nb_err; i++) + for(uint32_t i = 0; i < nb_err; i++) { gf2m current = error_pos[i]; diff --git a/src/lib/pubkey/mce/mce_internal.h b/src/lib/pubkey/mce/mce_internal.h index fb995e758..81fa970bb 100644 --- a/src/lib/pubkey/mce/mce_internal.h +++ b/src/lib/pubkey/mce/mce_internal.h @@ -19,32 +19,32 @@ namespace Botan { -void mceliece_decrypt(secure_vector<byte>& plaintext_out, - secure_vector<byte>& error_mask_out, - const byte ciphertext[], +void mceliece_decrypt(secure_vector<uint8_t>& plaintext_out, + secure_vector<uint8_t>& error_mask_out, + const uint8_t ciphertext[], size_t ciphertext_len, const McEliece_PrivateKey& key); -void mceliece_decrypt(secure_vector<byte>& plaintext_out, - secure_vector<byte>& error_mask_out, - const secure_vector<byte>& ciphertext, +void mceliece_decrypt(secure_vector<uint8_t>& plaintext_out, + secure_vector<uint8_t>& error_mask_out, + const secure_vector<uint8_t>& ciphertext, const McEliece_PrivateKey& key); -secure_vector<byte> mceliece_decrypt( +secure_vector<uint8_t> mceliece_decrypt( secure_vector<gf2m> & error_pos, - const byte *ciphertext, u32bit ciphertext_len, + const uint8_t *ciphertext, uint32_t ciphertext_len, const McEliece_PrivateKey & key); -void mceliece_encrypt(secure_vector<byte>& ciphertext_out, - secure_vector<byte>& error_mask_out, - const secure_vector<byte>& plaintext, +void mceliece_encrypt(secure_vector<uint8_t>& ciphertext_out, + secure_vector<uint8_t>& error_mask_out, + const secure_vector<uint8_t>& plaintext, const McEliece_PublicKey& key, RandomNumberGenerator& rng); McEliece_PrivateKey generate_mceliece_key(RandomNumberGenerator &rng, - u32bit ext_deg, - u32bit code_length, - u32bit t); + uint32_t ext_deg, + uint32_t code_length, + uint32_t t); } diff --git a/src/lib/pubkey/mce/mceliece.cpp b/src/lib/pubkey/mce/mceliece.cpp index 7617ff11f..fd985c032 100644 --- a/src/lib/pubkey/mce/mceliece.cpp +++ b/src/lib/pubkey/mce/mceliece.cpp @@ -19,10 +19,10 @@ namespace Botan { namespace { -secure_vector<byte> concat_vectors(const secure_vector<byte>& a, const secure_vector<byte>& b, - u32bit dimension, u32bit codimension) +secure_vector<uint8_t> concat_vectors(const secure_vector<uint8_t>& a, const secure_vector<uint8_t>& b, + uint32_t dimension, uint32_t codimension) { - secure_vector<byte> x(bit_size_to_byte_size(dimension) + bit_size_to_byte_size(codimension)); + secure_vector<uint8_t> x(bit_size_to_byte_size(dimension) + bit_size_to_byte_size(codimension)); const size_t final_bits = dimension % 8; @@ -35,31 +35,31 @@ secure_vector<byte> concat_vectors(const secure_vector<byte>& a, const secure_ve else { copy_mem(&x[0], a.data(), (dimension / 8)); - u32bit l = dimension / 8; - x[l] = static_cast<byte>(a[l] & ((1 << final_bits) - 1)); + uint32_t l = dimension / 8; + x[l] = static_cast<uint8_t>(a[l] & ((1 << final_bits) - 1)); - for(u32bit k = 0; k < codimension / 8; ++k) + for(uint32_t k = 0; k < codimension / 8; ++k) { - x[l] ^= static_cast<byte>(b[k] << final_bits); + x[l] ^= static_cast<uint8_t>(b[k] << final_bits); ++l; - x[l] = static_cast<byte>(b[k] >> (8 - final_bits)); + x[l] = static_cast<uint8_t>(b[k] >> (8 - final_bits)); } - x[l] ^= static_cast<byte>(b[codimension/8] << final_bits); + x[l] ^= static_cast<uint8_t>(b[codimension/8] << final_bits); } return x; } -secure_vector<byte> mult_by_pubkey(const secure_vector<byte>& cleartext, - std::vector<byte> const& public_matrix, - u32bit code_length, u32bit t) +secure_vector<uint8_t> mult_by_pubkey(const secure_vector<uint8_t>& cleartext, + std::vector<uint8_t> const& public_matrix, + uint32_t code_length, uint32_t t) { - const u32bit ext_deg = ceil_log2(code_length); - const u32bit codimension = ext_deg * t; - const u32bit dimension = code_length - codimension; - secure_vector<byte> cR(bit_size_to_32bit_size(codimension) * sizeof(u32bit)); + const uint32_t ext_deg = ceil_log2(code_length); + const uint32_t codimension = ext_deg * t; + const uint32_t dimension = code_length - codimension; + secure_vector<uint8_t> cR(bit_size_to_32bit_size(codimension) * sizeof(uint32_t)); - const byte* pt = public_matrix.data(); + const uint8_t* pt = public_matrix.data(); for(size_t i = 0; i < dimension / 8; ++i) { @@ -82,16 +82,16 @@ secure_vector<byte> mult_by_pubkey(const secure_vector<byte>& cleartext, pt += cR.size(); } - secure_vector<byte> ciphertext = concat_vectors(cleartext, cR, dimension, codimension); + secure_vector<uint8_t> ciphertext = concat_vectors(cleartext, cR, dimension, codimension); ciphertext.resize((code_length+7)/8); return ciphertext; } -secure_vector<byte> create_random_error_vector(unsigned code_length, +secure_vector<uint8_t> create_random_error_vector(unsigned code_length, unsigned error_weight, RandomNumberGenerator& rng) { - secure_vector<byte> result((code_length+7)/8); + secure_vector<uint8_t> result((code_length+7)/8); size_t bits_set = 0; @@ -101,7 +101,7 @@ secure_vector<byte> create_random_error_vector(unsigned code_length, const size_t byte_pos = x / 8, bit_pos = x % 8; - const byte mask = (1 << bit_pos); + const uint8_t mask = (1 << bit_pos); if(result[byte_pos] & mask) continue; // already set this bit @@ -115,15 +115,15 @@ secure_vector<byte> create_random_error_vector(unsigned code_length, } -void mceliece_encrypt(secure_vector<byte>& ciphertext_out, - secure_vector<byte>& error_mask_out, - const secure_vector<byte>& plaintext, +void mceliece_encrypt(secure_vector<uint8_t>& ciphertext_out, + secure_vector<uint8_t>& error_mask_out, + const secure_vector<uint8_t>& plaintext, const McEliece_PublicKey& key, RandomNumberGenerator& rng) { - secure_vector<byte> error_mask = create_random_error_vector(key.get_code_length(), key.get_t(), rng); + secure_vector<uint8_t> error_mask = create_random_error_vector(key.get_code_length(), key.get_t(), rng); - secure_vector<byte> ciphertext = mult_by_pubkey(plaintext, key.get_public_matrix(), + secure_vector<uint8_t> ciphertext = mult_by_pubkey(plaintext, key.get_public_matrix(), key.get_code_length(), key.get_t()); ciphertext ^= error_mask; diff --git a/src/lib/pubkey/mce/mceliece.h b/src/lib/pubkey/mce/mceliece.h index 0731e0c68..58c242360 100644 --- a/src/lib/pubkey/mce/mceliece.h +++ b/src/lib/pubkey/mce/mceliece.h @@ -21,9 +21,9 @@ namespace Botan { class BOTAN_DLL McEliece_PublicKey : public virtual Public_Key { public: - explicit McEliece_PublicKey(const std::vector<byte>& key_bits); + explicit McEliece_PublicKey(const std::vector<uint8_t>& key_bits); - McEliece_PublicKey(std::vector<byte> const& pub_matrix, u32bit the_t, u32bit the_code_length) : + McEliece_PublicKey(std::vector<uint8_t> const& pub_matrix, uint32_t the_t, uint32_t the_code_length) : m_public_matrix(pub_matrix), m_t(the_t), m_code_length(the_code_length) @@ -31,7 +31,7 @@ class BOTAN_DLL McEliece_PublicKey : public virtual Public_Key McEliece_PublicKey(const McEliece_PublicKey& other); - secure_vector<byte> random_plaintext_element(RandomNumberGenerator& rng) const; + secure_vector<uint8_t> random_plaintext_element(RandomNumberGenerator& rng) const; std::string algo_name() const override { return "McEliece"; } @@ -40,15 +40,15 @@ class BOTAN_DLL McEliece_PublicKey : public virtual Public_Key size_t key_length() const override; size_t estimated_strength() const override; - std::vector<byte> public_key_bits() const override; + std::vector<uint8_t> public_key_bits() const override; bool check_key(RandomNumberGenerator&, bool) const override { return true; } - u32bit get_t() const { return m_t; } - u32bit get_code_length() const { return m_code_length; } - u32bit get_message_word_bit_length() const; - const std::vector<byte>& get_public_matrix() const { return m_public_matrix; } + uint32_t get_t() const { return m_t; } + uint32_t get_code_length() const { return m_code_length; } + uint32_t get_message_word_bit_length() const; + const std::vector<uint8_t>& get_public_matrix() const { return m_public_matrix; } bool operator==(const McEliece_PublicKey& other) const; bool operator!=(const McEliece_PublicKey& other) const { return !(*this == other); } @@ -61,9 +61,9 @@ class BOTAN_DLL McEliece_PublicKey : public virtual Public_Key protected: McEliece_PublicKey() : m_t(0), m_code_length(0) {} - std::vector<byte> m_public_matrix; - u32bit m_t; - u32bit m_code_length; + std::vector<uint8_t> m_public_matrix; + uint32_t m_t; + uint32_t m_code_length; }; class BOTAN_DLL McEliece_PrivateKey : public virtual McEliece_PublicKey, @@ -85,26 +85,26 @@ class BOTAN_DLL McEliece_PrivateKey : public virtual McEliece_PublicKey, */ McEliece_PrivateKey(RandomNumberGenerator& rng, size_t code_length, size_t t); - explicit McEliece_PrivateKey(const secure_vector<byte>& key_bits); + explicit McEliece_PrivateKey(const secure_vector<uint8_t>& key_bits); McEliece_PrivateKey(polyn_gf2m const& goppa_polyn, - std::vector<u32bit> const& parity_check_matrix_coeffs, + std::vector<uint32_t> const& parity_check_matrix_coeffs, std::vector<polyn_gf2m> const& square_root_matrix, std::vector<gf2m> const& inverse_support, - std::vector<byte> const& public_matrix ); + std::vector<uint8_t> const& public_matrix ); bool check_key(RandomNumberGenerator& rng, bool strong) const override; polyn_gf2m const& get_goppa_polyn() const { return m_g; } - std::vector<u32bit> const& get_H_coeffs() const { return m_coeffs; } + std::vector<uint32_t> const& get_H_coeffs() const { return m_coeffs; } std::vector<gf2m> const& get_Linv() const { return m_Linv; } std::vector<polyn_gf2m> const& get_sqrtmod() const { return m_sqrtmod; } - inline u32bit get_dimension() const { return m_dimension; } + inline uint32_t get_dimension() const { return m_dimension; } - inline u32bit get_codimension() const { return m_codimension; } + inline uint32_t get_codimension() const { return m_codimension; } - secure_vector<byte> private_key_bits() const override; + secure_vector<uint8_t> private_key_bits() const override; bool operator==(const McEliece_PrivateKey & other) const; @@ -118,10 +118,10 @@ class BOTAN_DLL McEliece_PrivateKey : public virtual McEliece_PublicKey, polyn_gf2m m_g; std::vector<polyn_gf2m> m_sqrtmod; std::vector<gf2m> m_Linv; - std::vector<u32bit> m_coeffs; + std::vector<uint32_t> m_coeffs; - u32bit m_codimension; - u32bit m_dimension; + uint32_t m_codimension; + uint32_t m_dimension; }; /** diff --git a/src/lib/pubkey/mce/mceliece_key.cpp b/src/lib/pubkey/mce/mceliece_key.cpp index 409688153..798f7a91f 100644 --- a/src/lib/pubkey/mce/mceliece_key.cpp +++ b/src/lib/pubkey/mce/mceliece_key.cpp @@ -21,10 +21,10 @@ namespace Botan { McEliece_PrivateKey::McEliece_PrivateKey(polyn_gf2m const& goppa_polyn, - std::vector<u32bit> const& parity_check_matrix_coeffs, + std::vector<uint32_t> const& parity_check_matrix_coeffs, std::vector<polyn_gf2m> const& square_root_matrix, std::vector<gf2m> const& inverse_support, - std::vector<byte> const& public_matrix) : + std::vector<uint8_t> const& public_matrix) : McEliece_PublicKey(public_matrix, goppa_polyn.get_degree(), inverse_support.size()), m_g(goppa_polyn), m_sqrtmod(square_root_matrix), @@ -37,27 +37,27 @@ McEliece_PrivateKey::McEliece_PrivateKey(polyn_gf2m const& goppa_polyn, McEliece_PrivateKey::McEliece_PrivateKey(RandomNumberGenerator& rng, size_t code_length, size_t t) { - u32bit ext_deg = ceil_log2(code_length); + uint32_t ext_deg = ceil_log2(code_length); *this = generate_mceliece_key(rng, ext_deg, code_length, t); } -u32bit McEliece_PublicKey::get_message_word_bit_length() const +uint32_t McEliece_PublicKey::get_message_word_bit_length() const { - u32bit codimension = ceil_log2(m_code_length) * m_t; + uint32_t codimension = ceil_log2(m_code_length) * m_t; return m_code_length - codimension; } -secure_vector<byte> McEliece_PublicKey::random_plaintext_element(RandomNumberGenerator& rng) const +secure_vector<uint8_t> McEliece_PublicKey::random_plaintext_element(RandomNumberGenerator& rng) const { const size_t bits = get_message_word_bit_length(); - secure_vector<byte> plaintext((bits+7)/8); + secure_vector<uint8_t> plaintext((bits+7)/8); rng.randomize(plaintext.data(), plaintext.size()); // unset unused bits in the last plaintext byte - if(u32bit used = bits % 8) + if(uint32_t used = bits % 8) { - const byte mask = (1 << used) - 1; + const uint8_t mask = (1 << used) - 1; plaintext[plaintext.size() - 1] &= mask; } @@ -66,10 +66,10 @@ secure_vector<byte> McEliece_PublicKey::random_plaintext_element(RandomNumberGen AlgorithmIdentifier McEliece_PublicKey::algorithm_identifier() const { - return AlgorithmIdentifier(get_oid(), std::vector<byte>()); + return AlgorithmIdentifier(get_oid(), std::vector<uint8_t>()); } -std::vector<byte> McEliece_PublicKey::public_key_bits() const +std::vector<uint8_t> McEliece_PublicKey::public_key_bits() const { return DER_Encoder() .start_cons(SEQUENCE) @@ -99,7 +99,7 @@ size_t McEliece_PublicKey::estimated_strength() const return mceliece_work_factor(m_code_length, m_t); } -McEliece_PublicKey::McEliece_PublicKey(const std::vector<byte>& key_bits) +McEliece_PublicKey::McEliece_PublicKey(const std::vector<uint8_t>& key_bits) { BER_Decoder dec(key_bits); size_t n; @@ -115,7 +115,7 @@ McEliece_PublicKey::McEliece_PublicKey(const std::vector<byte>& key_bits) m_code_length = n; } -secure_vector<byte> McEliece_PrivateKey::private_key_bits() const +secure_vector<uint8_t> McEliece_PrivateKey::private_key_bits() const { DER_Encoder enc; enc.start_cons(SEQUENCE) @@ -126,20 +126,20 @@ secure_vector<byte> McEliece_PrivateKey::private_key_bits() const .encode(m_public_matrix, OCTET_STRING) .encode(m_g.encode(), OCTET_STRING); // g as octet string enc.start_cons(SEQUENCE); - for(u32bit i = 0; i < m_sqrtmod.size(); i++) + for(uint32_t i = 0; i < m_sqrtmod.size(); i++) { enc.encode(m_sqrtmod[i].encode(), OCTET_STRING); } enc.end_cons(); - secure_vector<byte> enc_support; - for(u32bit i = 0; i < m_Linv.size(); i++) + secure_vector<uint8_t> enc_support; + for(uint32_t i = 0; i < m_Linv.size(); i++) { enc_support.push_back(m_Linv[i] >> 8); enc_support.push_back(m_Linv[i]); } enc.encode(enc_support, OCTET_STRING); - secure_vector<byte> enc_H; - for(u32bit i = 0; i < m_coeffs.size(); i++) + secure_vector<uint8_t> enc_H; + for(uint32_t i = 0; i < m_coeffs.size(); i++) { enc_H.push_back(m_coeffs[i] >> 24); enc_H.push_back(m_coeffs[i] >> 16); @@ -153,14 +153,14 @@ secure_vector<byte> McEliece_PrivateKey::private_key_bits() const bool McEliece_PrivateKey::check_key(RandomNumberGenerator& rng, bool) const { - const secure_vector<byte> plaintext = this->random_plaintext_element(rng); + const secure_vector<uint8_t> plaintext = this->random_plaintext_element(rng); - secure_vector<byte> ciphertext; - secure_vector<byte> errors; + secure_vector<uint8_t> ciphertext; + secure_vector<uint8_t> errors; mceliece_encrypt(ciphertext, errors, plaintext, *this, rng); - secure_vector<byte> plaintext_out; - secure_vector<byte> errors_out; + secure_vector<uint8_t> plaintext_out; + secure_vector<uint8_t> errors_out; mceliece_decrypt(plaintext_out, errors_out, ciphertext, *this); if(errors != errors_out || plaintext != plaintext_out) @@ -169,10 +169,10 @@ bool McEliece_PrivateKey::check_key(RandomNumberGenerator& rng, bool) const return true; } -McEliece_PrivateKey::McEliece_PrivateKey(const secure_vector<byte>& key_bits) +McEliece_PrivateKey::McEliece_PrivateKey(const secure_vector<uint8_t>& key_bits) { size_t n, t; - secure_vector<byte> g_enc; + secure_vector<uint8_t> g_enc; BER_Decoder dec_base(key_bits); BER_Decoder dec = dec_base.start_cons(SEQUENCE) .start_cons(SEQUENCE) @@ -185,7 +185,7 @@ McEliece_PrivateKey::McEliece_PrivateKey(const secure_vector<byte>& key_bits) if(t == 0 || n == 0) throw Decoding_Error("invalid McEliece parameters"); - u32bit ext_deg = ceil_log2(n); + uint32_t ext_deg = ceil_log2(n); m_code_length = n; m_t = t; m_codimension = (ext_deg * t); @@ -198,9 +198,9 @@ McEliece_PrivateKey::McEliece_PrivateKey(const secure_vector<byte>& key_bits) throw Decoding_Error("degree of decoded Goppa polynomial is incorrect"); } BER_Decoder dec2 = dec.start_cons(SEQUENCE); - for(u32bit i = 0; i < t/2; i++) + for(uint32_t i = 0; i < t/2; i++) { - secure_vector<byte> sqrt_enc; + secure_vector<uint8_t> sqrt_enc; dec2.decode(sqrt_enc, OCTET_STRING); while(sqrt_enc.size() < (t*2)) { @@ -214,7 +214,7 @@ McEliece_PrivateKey::McEliece_PrivateKey(const secure_vector<byte>& key_bits) } m_sqrtmod.push_back(polyn_gf2m(sqrt_enc, sp_field)); } - secure_vector<byte> enc_support; + secure_vector<uint8_t> enc_support; BER_Decoder dec3 = dec2.end_cons() .decode(enc_support, OCTET_STRING); if(enc_support.size() % 2) @@ -225,12 +225,12 @@ McEliece_PrivateKey::McEliece_PrivateKey(const secure_vector<byte>& key_bits) { throw Decoding_Error("encoded support has length different from code length"); } - for(u32bit i = 0; i < n*2; i+=2) + for(uint32_t i = 0; i < n*2; i+=2) { gf2m el = (enc_support[i] << 8) | enc_support[i+1]; m_Linv.push_back(el); } - secure_vector<byte> enc_H; + secure_vector<uint8_t> enc_H; dec3.decode(enc_H, OCTET_STRING) .end_cons(); if(enc_H.size() % 4) @@ -242,9 +242,9 @@ McEliece_PrivateKey::McEliece_PrivateKey(const secure_vector<byte>& key_bits) throw Decoding_Error("encoded parity check matrix has wrong length"); } - for(u32bit i = 0; i < enc_H.size(); i+=4) + for(uint32_t i = 0; i < enc_H.size(); i+=4) { - u32bit coeff = (enc_H[i] << 24) | (enc_H[i+1] << 16) | (enc_H[i+2] << 8) | enc_H[i+3]; + uint32_t coeff = (enc_H[i] << 24) | (enc_H[i+1] << 16) | (enc_H[i+2] << 8) | enc_H[i+3]; m_coeffs.push_back(coeff); } @@ -310,13 +310,13 @@ class MCE_KEM_Encryptor : public PK_Ops::KEM_Encryption_with_KDF KEM_Encryption_with_KDF(kdf), m_key(key) {} private: - void raw_kem_encrypt(secure_vector<byte>& out_encapsulated_key, - secure_vector<byte>& raw_shared_key, + void raw_kem_encrypt(secure_vector<uint8_t>& out_encapsulated_key, + secure_vector<uint8_t>& raw_shared_key, Botan::RandomNumberGenerator& rng) override { - secure_vector<byte> plaintext = m_key.random_plaintext_element(rng); + secure_vector<uint8_t> plaintext = m_key.random_plaintext_element(rng); - secure_vector<byte> ciphertext, error_mask; + secure_vector<uint8_t> ciphertext, error_mask; mceliece_encrypt(ciphertext, error_mask, plaintext, m_key, rng); raw_shared_key.clear(); @@ -338,13 +338,13 @@ class MCE_KEM_Decryptor : public PK_Ops::KEM_Decryption_with_KDF KEM_Decryption_with_KDF(kdf), m_key(key) {} private: - secure_vector<byte> - raw_kem_decrypt(const byte encap_key[], size_t len) override + secure_vector<uint8_t> + raw_kem_decrypt(const uint8_t encap_key[], size_t len) override { - secure_vector<byte> plaintext, error_mask; + secure_vector<uint8_t> plaintext, error_mask; mceliece_decrypt(plaintext, error_mask, encap_key, len, m_key); - secure_vector<byte> output; + secure_vector<uint8_t> output; output.reserve(plaintext.size() + error_mask.size()); output.insert(output.end(), plaintext.begin(), plaintext.end()); output.insert(output.end(), error_mask.begin(), error_mask.end()); diff --git a/src/lib/pubkey/mce/polyn_gf2m.cpp b/src/lib/pubkey/mce/polyn_gf2m.cpp index 2815181c1..f4fd88f5d 100644 --- a/src/lib/pubkey/mce/polyn_gf2m.cpp +++ b/src/lib/pubkey/mce/polyn_gf2m.cpp @@ -30,7 +30,7 @@ gf2m generate_gf2m_mask(gf2m a) /** * number of leading zeros */ -unsigned nlz_16bit(u16bit x) +unsigned nlz_16bit(uint16_t x) { unsigned n; if(x == 0) return 16; @@ -47,8 +47,8 @@ int polyn_gf2m::calc_degree_secure() const { int i = this->coeff.size() - 1; int result = 0; - u32bit found_mask = 0; - u32bit tracker_mask = 0xffff; + uint32_t found_mask = 0; + uint32_t tracker_mask = 0xffff; for( ; i >= 0; i--) { found_mask = expand_mask_16bit(this->coeff[i]); @@ -63,9 +63,9 @@ int polyn_gf2m::calc_degree_secure() const gf2m random_gf2m(RandomNumberGenerator& rng) { - byte b[2]; + uint8_t b[2]; rng.randomize(b, sizeof(b)); - return make_u16bit(b[1], b[0]); + return make_uint16(b[1], b[0]); } gf2m random_code_element(unsigned code_length, RandomNumberGenerator& rng) @@ -118,12 +118,12 @@ std::string polyn_gf2m::to_string() const /** * doesn't save coefficients: */ -void polyn_gf2m::realloc(u32bit new_size) +void polyn_gf2m::realloc(uint32_t new_size) { this->coeff = secure_vector<gf2m>(new_size); } -polyn_gf2m::polyn_gf2m(const byte* mem, u32bit mem_len, std::shared_ptr<GF2m_Field> sp_field) +polyn_gf2m::polyn_gf2m(const uint8_t* mem, uint32_t mem_len, std::shared_ptr<GF2m_Field> sp_field) :msp_field(sp_field) { if(mem_len % sizeof(gf2m)) @@ -131,15 +131,15 @@ polyn_gf2m::polyn_gf2m(const byte* mem, u32bit mem_len, std::shared_ptr<GF2m_Fie throw new Botan::Decoding_Error("illegal length of memory to decode "); } - u32bit size = (mem_len / sizeof(this->coeff[0])) ; + uint32_t size = (mem_len / sizeof(this->coeff[0])) ; this->coeff = secure_vector<gf2m>(size); this->m_deg = -1; - for(u32bit i = 0; i < size; i++) + for(uint32_t i = 0; i < size; i++) { this->coeff[i] = decode_gf2m(mem); mem += sizeof(this->coeff[0]); } - for(u32bit i = 0; i < size; i++) + for(uint32_t i = 0; i < size; i++) { if(this->coeff[i] >= (1 << sp_field->get_extension_degree())) { @@ -156,12 +156,12 @@ polyn_gf2m::polyn_gf2m( std::shared_ptr<GF2m_Field> sp_field ) msp_field(sp_field) {} -polyn_gf2m::polyn_gf2m(int degree, const unsigned char* mem, u32bit mem_byte_len, std::shared_ptr<GF2m_Field> sp_field) +polyn_gf2m::polyn_gf2m(int degree, const unsigned char* mem, uint32_t mem_byte_len, std::shared_ptr<GF2m_Field> sp_field) :msp_field(sp_field) { - u32bit j, k, l; + uint32_t j, k, l; gf2m a; - u32bit polyn_size; + uint32_t polyn_size; polyn_size = degree + 1; if(polyn_size * sp_field->get_extension_degree() > 8 * mem_byte_len) { @@ -191,12 +191,12 @@ polyn_gf2m::polyn_gf2m(int degree, const unsigned char* mem, u32bit mem_byte_le } #if 0 -void polyn_gf2m::encode(u32bit min_numo_coeffs, byte* mem, u32bit mem_len) const +void polyn_gf2m::encode(uint32_t min_numo_coeffs, uint8_t* mem, uint32_t mem_len) const { - u32bit i; - u32bit numo_coeffs, needed_size; + uint32_t i; + uint32_t numo_coeffs, needed_size; this->get_degree(); - numo_coeffs = (min_numo_coeffs > static_cast<u32bit>(this->m_deg+1)) ? min_numo_coeffs : this->m_deg+1; + numo_coeffs = (min_numo_coeffs > static_cast<uint32_t>(this->m_deg+1)) ? min_numo_coeffs : this->m_deg+1; needed_size = sizeof(this->coeff[0]) * numo_coeffs; if(mem_len < needed_size) { @@ -206,7 +206,7 @@ void polyn_gf2m::encode(u32bit min_numo_coeffs, byte* mem, u32bit mem_len) const for(i = 0; i < numo_coeffs; i++) { gf2m to_enc; - if(i >= static_cast<u32bit>(this->m_deg+1)) + if(i >= static_cast<uint32_t>(this->m_deg+1)) { /* encode a zero */ to_enc = 0; @@ -295,10 +295,10 @@ std::vector<polyn_gf2m> polyn_gf2m::sqmod_init(const polyn_gf2m & g) if(signed_deg <= 0) throw Invalid_Argument("cannot compute sqmod for such low degree"); - const u32bit d = static_cast<u32bit>(signed_deg); - u32bit t = g.m_deg; + const uint32_t d = static_cast<uint32_t>(signed_deg); + uint32_t t = g.m_deg; // create t zero polynomials - u32bit i; + uint32_t i; for (i = 0; i < t; ++i) { sq.push_back(polyn_gf2m(t+1, g.get_sp_field())); @@ -428,16 +428,16 @@ void polyn_gf2m::degppf(const polyn_gf2m & g, int* p_result) } -void polyn_gf2m::patchup_deg_secure( u32bit trgt_deg, volatile gf2m patch_elem) +void polyn_gf2m::patchup_deg_secure( uint32_t trgt_deg, volatile gf2m patch_elem) { - u32bit i; + uint32_t i; if(this->coeff.size() < trgt_deg) { return; } for(i = 0; i < this->coeff.size(); i++) { - u32bit equal, equal_mask; + uint32_t equal, equal_mask; this->coeff[i] |= patch_elem; equal = (i == trgt_deg); equal_mask = expand_mask_16bit(equal); @@ -539,7 +539,7 @@ std::pair<polyn_gf2m, polyn_gf2m> polyn_gf2m::eea_with_coefficients( const polyn if(break_deg == 1) /* syndrome inversion */ { volatile gf2m fake_elem = 0x00; - volatile u32bit trgt_deg = 0; + volatile uint32_t trgt_deg = 0; r0.calc_degree_secure(); u0.calc_degree_secure(); /** @@ -559,7 +559,7 @@ std::pair<polyn_gf2m, polyn_gf2m> polyn_gf2m::eea_with_coefficients( const polyn */ if(u0.get_degree() == 4) { - u32bit mask = 0; + uint32_t mask = 0; /** * Condition that the EEA would break now */ @@ -587,7 +587,7 @@ std::pair<polyn_gf2m, polyn_gf2m> polyn_gf2m::eea_with_coefficients( const polyn } else if(u0.get_degree() == 6) { - u32bit mask = 0; + uint32_t mask = 0; int cond_r= r0.get_degree() == 0; int cond_u1 = msp_field->gf_mul(u0.coeff[1], msp_field->gf_inv(r0.coeff[0])) == 1; int cond_u3 = u0.coeff[3] == 0; @@ -601,7 +601,7 @@ std::pair<polyn_gf2m, polyn_gf2m> polyn_gf2m::eea_with_coefficients( const polyn } else if(u0.get_degree() == 8) { - u32bit mask = 0; + uint32_t mask = 0; int cond_r= r0.get_degree() == 0; int cond_u1 = msp_field->gf_mul(u0[1], msp_field->gf_inv(r0[0])) == 1; int cond_u3 = u0.coeff[3] == 0; @@ -677,8 +677,8 @@ void polyn_gf2m::poly_shiftmod( const polyn_gf2m & g) std::vector<polyn_gf2m> polyn_gf2m::sqrt_mod_init(const polyn_gf2m & g) { - u32bit i, t; - u32bit nb_polyn_sqrt_mat; + uint32_t i, t; + uint32_t nb_polyn_sqrt_mat; std::shared_ptr<GF2m_Field> msp_field = g.msp_field; std::vector<polyn_gf2m> result; t = g.get_degree(); @@ -753,14 +753,14 @@ std::vector<polyn_gf2m> syndrome_init(polyn_gf2m const& generator, std::vector<g return result; } -polyn_gf2m::polyn_gf2m(const secure_vector<byte>& encoded, std::shared_ptr<GF2m_Field> sp_field ) +polyn_gf2m::polyn_gf2m(const secure_vector<uint8_t>& encoded, std::shared_ptr<GF2m_Field> sp_field ) :msp_field(sp_field) { if(encoded.size() % 2) { throw Decoding_Error("encoded polynomial has odd length"); } - for(u32bit i = 0; i < encoded.size(); i += 2) + for(uint32_t i = 0; i < encoded.size(); i += 2) { gf2m el = (encoded[i] << 8) | encoded[i + 1]; coeff.push_back(el); @@ -768,9 +768,9 @@ polyn_gf2m::polyn_gf2m(const secure_vector<byte>& encoded, std::shared_ptr<GF2m_ get_degree(); } -secure_vector<byte> polyn_gf2m::encode() const +secure_vector<uint8_t> polyn_gf2m::encode() const { - secure_vector<byte> result; + secure_vector<uint8_t> result; if(m_deg < 1) { @@ -779,7 +779,7 @@ secure_vector<byte> polyn_gf2m::encode() const return result; } - u32bit len = m_deg+1; + uint32_t len = m_deg+1; for(unsigned i = 0; i < len; i++) { // "big endian" encoding of the GF(2^m) elements diff --git a/src/lib/pubkey/mce/polyn_gf2m.h b/src/lib/pubkey/mce/polyn_gf2m.h index 73e495fba..0782406ea 100644 --- a/src/lib/pubkey/mce/polyn_gf2m.h +++ b/src/lib/pubkey/mce/polyn_gf2m.h @@ -33,7 +33,7 @@ struct polyn_gf2m :m_deg(-1) {}; - polyn_gf2m(const secure_vector<byte>& encoded, std::shared_ptr<GF2m_Field> sp_field ); + polyn_gf2m(const secure_vector<uint8_t>& encoded, std::shared_ptr<GF2m_Field> sp_field ); polyn_gf2m& operator=(const polyn_gf2m&) = default; @@ -57,7 +57,7 @@ struct polyn_gf2m void swap(polyn_gf2m& other); - secure_vector<byte> encode() const; + secure_vector<uint8_t> encode() const; /** * create zero polynomial with reservation of space for a degree d polynomial */ @@ -82,14 +82,14 @@ struct polyn_gf2m gf2m get_lead_coef() const { return coeff[m_deg]; } - gf2m get_coef(u32bit i) const { return coeff[i]; } + gf2m get_coef(uint32_t i) const { return coeff[i]; } - inline void set_coef(u32bit i, gf2m v) + inline void set_coef(uint32_t i, gf2m v) { coeff[i] = v; }; - inline void add_to_coef(u32bit i, gf2m v) + inline void add_to_coef(uint32_t i, gf2m v) { coeff[i] = coeff[i] ^ v; } @@ -97,14 +97,14 @@ struct polyn_gf2m std::string to_string() const; /** decode a polynomial from memory: **/ - polyn_gf2m(const byte* mem, u32bit mem_len, std::shared_ptr<GF2m_Field> sp_field); + polyn_gf2m(const uint8_t* mem, uint32_t mem_len, std::shared_ptr<GF2m_Field> sp_field); // remove one! ^v! /** * create a polynomial from memory area (encoded) */ - polyn_gf2m(int degree, const unsigned char* mem, u32bit mem_byte_len, std::shared_ptr<GF2m_Field> sp_field); + polyn_gf2m(int degree, const unsigned char* mem, uint32_t mem_byte_len, std::shared_ptr<GF2m_Field> sp_field); - void encode(u32bit min_numo_coeffs, byte* mem, u32bit mem_len) const; + void encode(uint32_t min_numo_coeffs, uint8_t* mem, uint32_t mem_len) const; int get_degree() const; @@ -130,14 +130,14 @@ struct polyn_gf2m const polyn_gf2m & g, int break_deg); - void patchup_deg_secure( u32bit trgt_deg, volatile gf2m patch_elem); + void patchup_deg_secure( uint32_t trgt_deg, volatile gf2m patch_elem); private: void set_degree(int d) { m_deg = d; } void poly_shiftmod( const polyn_gf2m & g); - void realloc(u32bit new_size); + void realloc(uint32_t new_size); static polyn_gf2m gcd(polyn_gf2m const& p1, polyn_gf2m const& p2); /** @@ -166,7 +166,7 @@ std::vector<polyn_gf2m> syndrome_init(polyn_gf2m const& generator, std::vector<g * Find the roots of a polynomial over GF(2^m) using the method by Federenko * et al. */ -secure_vector<gf2m> find_roots_gf2m_decomp(const polyn_gf2m & polyn, u32bit code_length); +secure_vector<gf2m> find_roots_gf2m_decomp(const polyn_gf2m & polyn, uint32_t code_length); } |