From 2a045404e3a3187bd9dd5763a286b3e39d1fa47e Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Tue, 9 Feb 2016 10:11:51 -0500 Subject: Fix Coverity findings in McEliece code Initialize variables in constructor in gf2m_decomp_rootfind_state Add asserts on the degree where a positive value was assumed. How polyn_gf2m handles the degree needs some work but this should do for now. --- src/lib/pubkey/mce/gf2m_rootfind_dcmp.cpp | 8 +++++--- src/lib/pubkey/mce/goppa_code.cpp | 5 +++-- src/lib/pubkey/mce/polyn_gf2m.cpp | 21 ++++++++++++--------- 3 files changed, 20 insertions(+), 14 deletions(-) (limited to 'src/lib') diff --git a/src/lib/pubkey/mce/gf2m_rootfind_dcmp.cpp b/src/lib/pubkey/mce/gf2m_rootfind_dcmp.cpp index 008da99c1..acae036db 100644 --- a/src/lib/pubkey/mce/gf2m_rootfind_dcmp.cpp +++ b/src/lib/pubkey/mce/gf2m_rootfind_dcmp.cpp @@ -95,7 +95,7 @@ secure_vector find_roots_gf2m_decomp(const polyn_gf2m & polyn, u32bit code } gf2m_decomp_rootfind_state::gf2m_decomp_rootfind_state(const polyn_gf2m & polyn, u32bit the_code_length) : - code_length(the_code_length) + code_length(the_code_length), m_j(0), m_j_gray(0) { gf2m coeff_3; gf2m coeff_head; @@ -105,7 +105,7 @@ gf2m_decomp_rootfind_state::gf2m_decomp_rootfind_state(const polyn_gf2m & polyn, { throw Internal_Error("Unexpected degree in gf2m_decomp_rootfind_state"); } - this->m_j = 0; + coeff_3 = polyn.get_coef( 3); coeff_head = polyn.get_coef( deg_sigma); /* dummy value for SCA CM */ if(coeff_3 != 0) @@ -275,7 +275,9 @@ gf2m gf2m_decomp_rootfind_state::calc_Fxj_j_neq_0( const polyn_gf2m & sigma, gf2 secure_vector gf2m_decomp_rootfind_state::find_roots(const polyn_gf2m & sigma) { - secure_vector result(sigma.get_degree()); + const int sigma_degree = sigma.get_degree(); + BOTAN_ASSERT(sigma_degree > 0, "Valid sigma"); + secure_vector result(sigma_degree); u32bit root_pos = 0; this->calc_Ai_zero(sigma); diff --git a/src/lib/pubkey/mce/goppa_code.cpp b/src/lib/pubkey/mce/goppa_code.cpp index 02908aa4f..2657beee9 100644 --- a/src/lib/pubkey/mce/goppa_code.cpp +++ b/src/lib/pubkey/mce/goppa_code.cpp @@ -96,8 +96,9 @@ secure_vector goppa_decode(const polyn_gf2m & syndrom_polyn, sigma.set_coef(2*i, sp_field->gf_square(u.get_coef(i))); } - const size_t v_deg = v.get_degree(); - for(size_t i = 0; i <= v_deg; ++i) + const int v_deg = v.get_degree(); + BOTAN_ASSERT(v_deg > 0, "Valid degree"); + for(int i = 0; i <= v_deg; ++i) { sigma.set_coef(2*i+1, sp_field->gf_square(v.get_coef(i))); } diff --git a/src/lib/pubkey/mce/polyn_gf2m.cpp b/src/lib/pubkey/mce/polyn_gf2m.cpp index ec60213db..01a62da7d 100644 --- a/src/lib/pubkey/mce/polyn_gf2m.cpp +++ b/src/lib/pubkey/mce/polyn_gf2m.cpp @@ -268,7 +268,11 @@ void polyn_gf2m::remainder(polyn_gf2m &p, const polyn_gf2m & g) if (d >= 0) { gf2m la = msp_field->gf_inv_rn(g.get_lead_coef()); - for (i = p.get_degree(); d >= 0; --i, --d) { + const int p_degree = p.get_degree(); + + BOTAN_ASSERT(p_degree > 0, "Valid polynomial"); + + for (i = p_degree; d >= 0; --i, --d) { if (p[i] != 0) { gf2m lb = msp_field->gf_mul_rrn(la, p[i]); for (j = 0; j < g.get_degree(); ++j) @@ -455,6 +459,8 @@ std::pair polyn_gf2m::eea_with_coefficients( const polyn // r0 <- g, r1 <- p, u0 <- 0, u1 <- 1 dr = g.get_degree(); + BOTAN_ASSERT(dr > 3, "Valid polynomial"); + polyn_gf2m r0(dr, g.msp_field); polyn_gf2m r1(dr - 1, g.msp_field); polyn_gf2m u0(dr - 1, g.msp_field); @@ -656,18 +662,15 @@ polyn_gf2m::polyn_gf2m(int t, Botan::RandomNumberGenerator& rng, std::shared_ptr void polyn_gf2m::poly_shiftmod( const polyn_gf2m & g) { - int i, t; - gf2m a; - - if(g.get_degree() <= 0) + if(g.get_degree() <= 1) { - throw Invalid_Argument("shiftmod cannot be called on polynomials of degree 0 or less"); + throw Invalid_Argument("shiftmod cannot be called on polynomials of degree 1 or less"); } std::shared_ptr msp_field = g.msp_field; - t = g.get_degree(); - a = msp_field->gf_div(this->coeff[t-1], g.coeff[t]); - for (i = t - 1; i > 0; --i) + int t = g.get_degree(); + gf2m a = msp_field->gf_div(this->coeff[t-1], g.coeff[t]); + for (int i = t - 1; i > 0; --i) { this->coeff[i] = this->coeff[i - 1] ^ this->msp_field->gf_mul(a, g.coeff[i]); } -- cgit v1.2.3