2021-09-27 16:47:04 -07:00
|
|
|
use crate::{arithmetic::bigint, bits, cpu, error, rsa::N};
|
2021-09-20 15:11:04 -07:00
|
|
|
use core::ops::RangeInclusive;
|
|
|
|
|
2021-09-22 11:31:52 -07:00
|
|
|
/// The modulus (n) of an RSA public key.
|
2021-09-24 10:31:26 -07:00
|
|
|
#[derive(Clone)]
|
2021-09-28 11:44:07 -07:00
|
|
|
pub struct PublicModulus {
|
2023-11-02 16:49:28 -07:00
|
|
|
value: bigint::OwnedModulusWithOne<N>,
|
2021-09-20 15:11:04 -07:00
|
|
|
bits: bits::BitLength,
|
|
|
|
}
|
|
|
|
|
2021-09-28 12:20:55 -07:00
|
|
|
/*
|
2021-09-28 11:44:07 -07:00
|
|
|
impl core::fmt::Debug for PublicModulus {
|
2021-09-20 15:11:04 -07:00
|
|
|
fn fmt(&self, fmt: &mut ::core::fmt::Formatter) -> Result<(), ::core::fmt::Error> {
|
|
|
|
self.value.fmt(fmt)
|
|
|
|
}
|
2021-09-28 12:20:55 -07:00
|
|
|
}*/
|
2021-09-20 15:11:04 -07:00
|
|
|
|
2021-09-28 11:44:07 -07:00
|
|
|
impl PublicModulus {
|
2021-09-22 11:38:09 -07:00
|
|
|
pub(super) fn from_be_bytes(
|
2021-09-20 15:11:04 -07:00
|
|
|
n: untrusted::Input,
|
|
|
|
allowed_bit_lengths: RangeInclusive<bits::BitLength>,
|
2021-09-27 16:47:04 -07:00
|
|
|
cpu_features: cpu::Features,
|
2021-09-20 15:11:04 -07:00
|
|
|
) -> Result<Self, error::KeyRejected> {
|
2021-09-28 11:44:07 -07:00
|
|
|
// See `PublicKey::from_modulus_and_exponent` for background on the step
|
2021-09-20 15:11:04 -07:00
|
|
|
// numbering.
|
|
|
|
|
|
|
|
let min_bits = *allowed_bit_lengths.start();
|
|
|
|
let max_bits = *allowed_bit_lengths.end();
|
|
|
|
|
|
|
|
// `pkcs1_encode` depends on this not being small. Otherwise,
|
|
|
|
// `pkcs1_encode` would generate padding that is invalid (too few 0xFF
|
|
|
|
// bytes) for very small keys.
|
|
|
|
const MIN_BITS: bits::BitLength = bits::BitLength::from_usize_bits(1024);
|
|
|
|
|
|
|
|
// Step 3 / Step c for `n` (out of order).
|
2023-11-02 16:49:28 -07:00
|
|
|
let (value, bits) =
|
|
|
|
bigint::OwnedModulusWithOne::from_be_bytes_with_bit_length(n, cpu_features)?;
|
2021-09-20 15:11:04 -07:00
|
|
|
|
|
|
|
// Step 1 / Step a. XXX: SP800-56Br1 and SP800-89 require the length of
|
|
|
|
// the public modulus to be exactly 2048 or 3072 bits, but we are more
|
|
|
|
// flexible to be compatible with other commonly-used crypto libraries.
|
|
|
|
assert!(min_bits >= MIN_BITS);
|
|
|
|
let bits_rounded_up =
|
|
|
|
bits::BitLength::from_usize_bytes(bits.as_usize_bytes_rounded_up()).unwrap(); // TODO: safe?
|
|
|
|
if bits_rounded_up < min_bits {
|
|
|
|
return Err(error::KeyRejected::too_small());
|
|
|
|
}
|
|
|
|
if bits > max_bits {
|
|
|
|
return Err(error::KeyRejected::too_large());
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Self { value, bits })
|
|
|
|
}
|
|
|
|
|
2021-09-24 10:29:50 -07:00
|
|
|
/// The big-endian encoding of the modulus.
|
|
|
|
///
|
|
|
|
/// There are no leading zeros.
|
|
|
|
pub fn be_bytes(&self) -> impl ExactSizeIterator<Item = u8> + Clone + '_ {
|
|
|
|
self.value.be_bytes()
|
|
|
|
}
|
|
|
|
|
2021-09-22 11:32:27 -07:00
|
|
|
/// The length of the modulus in bits.
|
|
|
|
pub fn len_bits(&self) -> bits::BitLength {
|
2021-09-20 15:11:04 -07:00
|
|
|
self.bits
|
|
|
|
}
|
|
|
|
|
2023-11-02 16:49:28 -07:00
|
|
|
pub(super) fn value(&self) -> &bigint::OwnedModulusWithOne<N> {
|
2021-09-20 15:11:04 -07:00
|
|
|
&self.value
|
|
|
|
}
|
|
|
|
}
|