Skip to content

Commit

Permalink
montgomery: Use a distinct error for length checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
briansmith committed Dec 5, 2023
1 parent a40e939 commit 65553a1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/arithmetic/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ pub fn elem_exp_consttime<M>(
base: Elem<M, R>,
exponent: &PrivateExponent,
m: &Modulus<M>,
) -> Result<Elem<M, Unencoded>, error::Unspecified> {
) -> Result<Elem<M, Unencoded>, ImpossibleLengthError> {
use crate::{bssl, limb::Window};

const WINDOW_BITS: usize = 5;
Expand Down Expand Up @@ -490,7 +490,7 @@ pub fn elem_exp_consttime<M>(
base: Elem<M, R>,
exponent: &PrivateExponent,
m: &Modulus<M>,
) -> Result<Elem<M, Unencoded>, error::Unspecified> {
) -> Result<Elem<M, Unencoded>, ImpossibleLengthError> {
use crate::{cpu, limb::LIMB_BYTES};

// Pretty much all the math here requires CPU feature detection to have
Expand Down Expand Up @@ -629,7 +629,7 @@ pub fn elem_exp_consttime<M>(
mut i: Window,
num_limbs: usize,
cpu_features: cpu::Features,
) -> Result<(), error::Unspecified> {
) -> Result<(), ImpossibleLengthError> {
loop {
scatter(table, acc, i, num_limbs);
i *= 2;
Expand Down
33 changes: 25 additions & 8 deletions src/arithmetic/montgomery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ unsafe fn mul_mont(
m: &[Limb],
n0: &N0,
_: cpu::Features,
) -> Result<(), error::Unspecified> {
) -> Result<(), ImpossibleLengthError> {
if m.len() < MIN_LIMBS || m.len() > MAX_LIMBS {
return Err(error::Unspecified);
return Err(ImpossibleLengthError::new());

Check warning on line 138 in src/arithmetic/montgomery.rs

View check run for this annotation

Codecov / codecov/patch

src/arithmetic/montgomery.rs#L138

Added line #L138 was not covered by tests
}
bn_mul_mont(r, a, b, m.as_ptr(), n0, m.len());
Ok(())
Expand Down Expand Up @@ -273,9 +273,9 @@ pub(super) fn limbs_mont_mul(
m: &[Limb],
n0: &N0,
cpu_features: cpu::Features,
) -> Result<(), error::Unspecified> {
) -> Result<(), ImpossibleLengthError> {
if r.len() != m.len() || a.len() != m.len() {
return Err(error::Unspecified);
return Err(ImpossibleLengthError::new());

Check warning on line 278 in src/arithmetic/montgomery.rs

View check run for this annotation

Codecov / codecov/patch

src/arithmetic/montgomery.rs#L278

Added line #L278 was not covered by tests
}
unsafe { mul_mont(r.as_mut_ptr(), r.as_ptr(), a.as_ptr(), m, n0, cpu_features) }
}
Expand All @@ -289,9 +289,9 @@ pub(super) fn limbs_mont_product(
m: &[Limb],
n0: &N0,
cpu_features: cpu::Features,
) -> Result<(), error::Unspecified> {
) -> Result<(), ImpossibleLengthError> {
if r.len() != m.len() || a.len() != m.len() || b.len() != m.len() {
return Err(error::Unspecified);
return Err(ImpossibleLengthError::new());

Check warning on line 294 in src/arithmetic/montgomery.rs

View check run for this annotation

Codecov / codecov/patch

src/arithmetic/montgomery.rs#L294

Added line #L294 was not covered by tests
}
unsafe { mul_mont(r.as_mut_ptr(), a.as_ptr(), b.as_ptr(), m, n0, cpu_features) }
}
Expand All @@ -302,13 +302,30 @@ pub(super) fn limbs_mont_square(
m: &[Limb],
n0: &N0,
cpu_features: cpu::Features,
) -> Result<(), error::Unspecified> {
) -> Result<(), ImpossibleLengthError> {
if r.len() != m.len() {
return Err(error::Unspecified);
return Err(ImpossibleLengthError::new());

Check warning on line 307 in src/arithmetic/montgomery.rs

View check run for this annotation

Codecov / codecov/patch

src/arithmetic/montgomery.rs#L307

Added line #L307 was not covered by tests
}
unsafe { mul_mont(r.as_mut_ptr(), r.as_ptr(), r.as_ptr(), m, n0, cpu_features) }
}

/// `ImpossibleLengthError` should never occur.
#[derive(Debug)]

Check warning on line 313 in src/arithmetic/montgomery.rs

View check run for this annotation

Codecov / codecov/patch

src/arithmetic/montgomery.rs#L313

Added line #L313 was not covered by tests
pub struct ImpossibleLengthError(());

impl ImpossibleLengthError {
fn new() -> Self {
// unreachable!();
Self(())
}

Check warning on line 320 in src/arithmetic/montgomery.rs

View check run for this annotation

Codecov / codecov/patch

src/arithmetic/montgomery.rs#L317-L320

Added lines #L317 - L320 were not covered by tests
}

impl From<ImpossibleLengthError> for error::Unspecified {
fn from(_: ImpossibleLengthError) -> Self {
Self
}

Check warning on line 326 in src/arithmetic/montgomery.rs

View check run for this annotation

Codecov / codecov/patch

src/arithmetic/montgomery.rs#L324-L326

Added lines #L324 - L326 were not covered by tests
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
4 changes: 2 additions & 2 deletions src/rsa/keypair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use super::{
use crate::{
arithmetic::{
bigint,
montgomery::{R, RR, RRR},
montgomery::{ImpossibleLengthError, R, RR, RRR},
},
bits::BitLength,
cpu, digest,
Expand Down Expand Up @@ -482,7 +482,7 @@ fn elem_exp_consttime<M>(
c: &bigint::Elem<N>,
p: &PrivateCrtPrime<M>,
other_prime_len_bits: BitLength,
) -> Result<bigint::Elem<M>, error::Unspecified> {
) -> Result<bigint::Elem<M>, ImpossibleLengthError> {
let m = &p.modulus.modulus();
let c_mod_m = bigint::elem_reduced(c, m, other_prime_len_bits);
let c_mod_m = bigint::elem_mul(p.oneRRR.as_ref(), c_mod_m, m);
Expand Down

0 comments on commit 65553a1

Please sign in to comment.