-
Notifications
You must be signed in to change notification settings - Fork 709
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bigint/rsa: Avoid constructing Montgomery constant for
d
.
We never multiply modulo `d` so avoid constructing `n0` for it. Although `n0` is almost free to construct now, that might change in the future. Also it is better to minimize the amount of processing on the secret value. ``` git difftool HEAD^1:src/arithmetic/bigint/modulus.rs src/arithmetic/bigint/modulusvalue.rs ```
- Loading branch information
1 parent
d68bd9c
commit 45ff856
Showing
5 changed files
with
107 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2015-2024 Brian Smith. | ||
// | ||
// Permission to use, copy, modify, and/or distribute this software for any | ||
// purpose with or without fee is hereby granted, provided that the above | ||
// copyright notice and this permission notice appear in all copies. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES | ||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY | ||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION | ||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN | ||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
|
||
use super::{ | ||
modulus::{MODULUS_MAX_LIMBS, MODULUS_MIN_LIMBS}, | ||
BoxedLimbs, Modulus, PublicModulus, | ||
}; | ||
use crate::{ | ||
bits::BitLength, | ||
error, | ||
limb::{self, Limb, LimbMask}, | ||
}; | ||
|
||
/// `OwnedModulus`, without the overhead of Montgomery multiplication support. | ||
pub(crate) struct OwnedModulusValue<M> { | ||
limbs: BoxedLimbs<M>, // Also `value >= 3`. | ||
|
||
len_bits: BitLength, | ||
} | ||
|
||
impl<M: PublicModulus> Clone for OwnedModulusValue<M> { | ||
fn clone(&self) -> Self { | ||
Self { | ||
limbs: self.limbs.clone(), | ||
len_bits: self.len_bits, | ||
} | ||
} | ||
} | ||
|
||
impl<M> OwnedModulusValue<M> { | ||
pub(crate) fn from_be_bytes(input: untrusted::Input) -> Result<Self, error::KeyRejected> { | ||
let n = BoxedLimbs::positive_minimal_width_from_be_bytes(input)?; | ||
if n.len() > MODULUS_MAX_LIMBS { | ||
return Err(error::KeyRejected::too_large()); | ||
} | ||
if n.len() < MODULUS_MIN_LIMBS { | ||
return Err(error::KeyRejected::unexpected_error()); | ||
} | ||
if limb::limbs_are_even_constant_time(&n) != LimbMask::False { | ||
return Err(error::KeyRejected::invalid_component()); | ||
} | ||
if limb::limbs_less_than_limb_constant_time(&n, 3) != LimbMask::False { | ||
return Err(error::KeyRejected::unexpected_error()); | ||
} | ||
|
||
let len_bits = limb::limbs_minimal_bits(&n); | ||
|
||
Ok(Self { limbs: n, len_bits }) | ||
} | ||
|
||
pub fn verify_less_than<L>(&self, l: &Modulus<L>) -> Result<(), error::Unspecified> { | ||
if self.len_bits() > l.len_bits() | ||
|| (self.limbs.len() == l.limbs().len() | ||
&& limb::limbs_less_than_limbs_consttime(&self.limbs, l.limbs()) != LimbMask::True) | ||
{ | ||
return Err(error::Unspecified); | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub fn len_bits(&self) -> BitLength { | ||
self.len_bits | ||
} | ||
|
||
#[inline] | ||
pub(super) fn limbs(&self) -> &[Limb] { | ||
&self.limbs | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters