Skip to content

Commit 912722d

Browse files
committed
rsa: Simplify the d < n check.
`d` is the only modulus for which we don't need to construct the value `RR`. By avoiding the construction of an `OwnedModulusValue` for it, we can improve the way we handle `RR` for N, P, Q, etc. in the future.
1 parent e1822d9 commit 912722d

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/rsa/keypair.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,12 @@ impl KeyPair {
316316
// First, validate `2**half_n_bits < d`. Since 2**half_n_bits has a bit
317317
// length of half_n_bits + 1, this check gives us 2**half_n_bits <= d,
318318
// and knowing d is odd makes the inequality strict.
319-
let d = bigint::modulus::ValidatedInput::try_from_be_bytes(d)
320-
.map_err(|_| KeyRejected::invalid_component())?;
321-
if !(n_bits.half_rounded_up() < d.len_bits()) {
322-
return Err(KeyRejected::inconsistent_components());
319+
{
320+
let d = bigint::modulus::ValidatedInput::try_from_be_bytes(d)
321+
.map_err(|_| KeyRejected::invalid_component())?;
322+
if !(n_bits.half_rounded_up() < d.len_bits()) {
323+
return Err(KeyRejected::inconsistent_components());
324+
}
323325
}
324326

325327
// 6.4.1.4.3 - Step 3.a (out of order).
@@ -345,11 +347,12 @@ impl KeyPair {
345347
return Err(KeyRejected::inconsistent_components());
346348
}
347349

348-
let d = d.build_value::<D>();
350+
// 6.4.1.4.3/6.4.1.2.1 - Step 6.
349351

350352
// XXX: This check should be `d < LCM(p - 1, q - 1)`, but we don't have
351-
// a good way of calculating LCM, so it is omitted, as explained above.
352-
d.verify_less_than(n)
353+
// a good way of calculating LCM, so just check the less strict condition
354+
// that `d < n`.
355+
let _d = bigint::Elem::from_be_bytes_padded(d, n)
353356
.map_err(|error::Unspecified| KeyRejected::inconsistent_components())?;
354357

355358
// Step 6.b is omitted as explained above.
@@ -522,8 +525,6 @@ enum P {}
522525

523526
enum Q {}
524527

525-
enum D {}
526-
527528
impl KeyPair {
528529
/// Computes the signature of `msg` and writes it into `signature`.
529530
///

0 commit comments

Comments
 (0)