Skip to content

Commit 9a393e1

Browse files
committed
montgomery: Use a distinct error for length checks.
1 parent 6d11f92 commit 9a393e1

File tree

5 files changed

+50
-13
lines changed

5 files changed

+50
-13
lines changed

src/arithmetic.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ mod constant;
1717
#[cfg(feature = "alloc")]
1818
pub mod bigint;
1919

20+
mod error;
2021
pub mod montgomery;
2122

2223
mod n0;
2324
pub use constant::limbs_from_hex;
25+
pub(crate) use error::ImpossibleLengthError;

src/arithmetic/bigint.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub(crate) use self::{
4141
modulus::{Modulus, OwnedModulus, MODULUS_MAX_LIMBS},
4242
private_exponent::PrivateExponent,
4343
};
44+
use super::ImpossibleLengthError;
4445
use crate::{
4546
arithmetic::montgomery::*,
4647
bits::BitLength,
@@ -404,7 +405,7 @@ pub fn elem_exp_consttime<M>(
404405
base: Elem<M, R>,
405406
exponent: &PrivateExponent,
406407
m: &Modulus<M>,
407-
) -> Result<Elem<M, Unencoded>, error::Unspecified> {
408+
) -> Result<Elem<M, Unencoded>, ImpossibleLengthError> {
408409
use crate::{bssl, limb::Window};
409410

410411
const WINDOW_BITS: usize = 5;
@@ -490,7 +491,7 @@ pub fn elem_exp_consttime<M>(
490491
base: Elem<M, R>,
491492
exponent: &PrivateExponent,
492493
m: &Modulus<M>,
493-
) -> Result<Elem<M, Unencoded>, error::Unspecified> {
494+
) -> Result<Elem<M, Unencoded>, ImpossibleLengthError> {
494495
use crate::{cpu, limb::LIMB_BYTES};
495496

496497
// Pretty much all the math here requires CPU feature detection to have
@@ -629,7 +630,7 @@ pub fn elem_exp_consttime<M>(
629630
mut i: Window,
630631
num_limbs: usize,
631632
cpu_features: cpu::Features,
632-
) -> Result<(), error::Unspecified> {
633+
) -> Result<(), ImpossibleLengthError> {
633634
loop {
634635
scatter(table, acc, i, num_limbs);
635636
i *= 2;

src/arithmetic/error.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2023 Brian Smith.
2+
//
3+
// Permission to use, copy, modify, and/or distribute this software for any
4+
// purpose with or without fee is hereby granted, provided that the above
5+
// copyright notice and this permission notice appear in all copies.
6+
//
7+
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
8+
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
10+
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12+
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13+
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14+
15+
use crate::error;
16+
17+
/// `ImpossibleLengthError` should never occur.
18+
#[derive(Debug)]
19+
pub struct ImpossibleLengthError(());
20+
21+
impl ImpossibleLengthError {
22+
pub(super) fn new() -> Self {
23+
// unreachable!();
24+
Self(())
25+
}
26+
}
27+
28+
impl From<ImpossibleLengthError> for error::Unspecified {
29+
fn from(_: ImpossibleLengthError) -> Self {
30+
Self
31+
}
32+
}

src/arithmetic/montgomery.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1414

1515
pub use super::n0::N0;
16-
use crate::{cpu, error};
16+
use super::ImpossibleLengthError;
17+
use crate::cpu;
1718

1819
// Indicates that the element is not encoded; there is no *R* factor
1920
// that needs to be canceled out.
@@ -133,9 +134,9 @@ unsafe fn mul_mont(
133134
m: &[Limb],
134135
n0: &N0,
135136
_: cpu::Features,
136-
) -> Result<(), error::Unspecified> {
137+
) -> Result<(), ImpossibleLengthError> {
137138
if m.len() < MIN_LIMBS || m.len() > MAX_LIMBS {
138-
return Err(error::Unspecified);
139+
return Err(ImpossibleLengthError::new());
139140
}
140141
bn_mul_mont(r, a, b, m.as_ptr(), n0, m.len());
141142
Ok(())
@@ -273,9 +274,9 @@ pub(super) fn limbs_mont_mul(
273274
m: &[Limb],
274275
n0: &N0,
275276
cpu_features: cpu::Features,
276-
) -> Result<(), error::Unspecified> {
277+
) -> Result<(), ImpossibleLengthError> {
277278
if r.len() != m.len() || a.len() != m.len() {
278-
return Err(error::Unspecified);
279+
return Err(ImpossibleLengthError::new());
279280
}
280281
unsafe { mul_mont(r.as_mut_ptr(), r.as_ptr(), a.as_ptr(), m, n0, cpu_features) }
281282
}
@@ -289,9 +290,9 @@ pub(super) fn limbs_mont_product(
289290
m: &[Limb],
290291
n0: &N0,
291292
cpu_features: cpu::Features,
292-
) -> Result<(), error::Unspecified> {
293+
) -> Result<(), ImpossibleLengthError> {
293294
if r.len() != m.len() || a.len() != m.len() || b.len() != m.len() {
294-
return Err(error::Unspecified);
295+
return Err(ImpossibleLengthError::new());
295296
}
296297
unsafe { mul_mont(r.as_mut_ptr(), a.as_ptr(), b.as_ptr(), m, n0, cpu_features) }
297298
}
@@ -302,9 +303,9 @@ pub(super) fn limbs_mont_square(
302303
m: &[Limb],
303304
n0: &N0,
304305
cpu_features: cpu::Features,
305-
) -> Result<(), error::Unspecified> {
306+
) -> Result<(), ImpossibleLengthError> {
306307
if r.len() != m.len() {
307-
return Err(error::Unspecified);
308+
return Err(ImpossibleLengthError::new());
308309
}
309310
unsafe { mul_mont(r.as_mut_ptr(), r.as_ptr(), r.as_ptr(), m, n0, cpu_features) }
310311
}

src/rsa/keypair.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use crate::{
2121
arithmetic::{
2222
bigint,
2323
montgomery::{R, RR, RRR},
24+
ImpossibleLengthError,
2425
},
2526
bits::BitLength,
2627
cpu, digest,
@@ -487,7 +488,7 @@ fn elem_exp_consttime<M>(
487488
p: &PrivateCrtPrime<M>,
488489
other_prime_len_bits: BitLength,
489490
cpu_features: cpu::Features,
490-
) -> Result<bigint::Elem<M>, error::Unspecified> {
491+
) -> Result<bigint::Elem<M>, ImpossibleLengthError> {
491492
let m = &p.modulus.modulus(cpu_features);
492493
let c_mod_m = bigint::elem_reduced(c, m, other_prime_len_bits);
493494
let c_mod_m = bigint::elem_mul(p.oneRRR.as_ref(), c_mod_m, m);

0 commit comments

Comments
 (0)