#![allow(non_snake_case)]
use {bits, der, limb, error};
use untrusted;
mod padding;
#[cfg(feature = "rsa_signing")]
pub use self::padding::RSAEncoding;
pub use self::padding::{
RSA_PKCS1_SHA256,
RSA_PKCS1_SHA384,
RSA_PKCS1_SHA512,
RSA_PSS_SHA256,
RSA_PSS_SHA384,
RSA_PSS_SHA512
};
const PUBLIC_KEY_PUBLIC_MODULUS_MAX_LEN: usize =
bigint::MODULUS_MAX_LIMBS * limb::LIMB_BYTES;
#[cfg(feature = "rsa_signing")]
const PRIVATE_KEY_PUBLIC_MODULUS_MAX_BITS: bits::BitLength =
bits::BitLength(4096);
#[cfg(feature = "rsa_signing")]
const PRIVATE_KEY_PUBLIC_MODULUS_MAX_LIMBS: usize =
(4096 + limb::LIMB_BITS - 1) / limb::LIMB_BITS;
pub struct RSAParameters {
padding_alg: &'static padding::RSAVerification,
min_bits: bits::BitLength,
id: RSAParametersID,
}
#[allow(non_camel_case_types)]
enum RSAParametersID {
RSA_PKCS1_2048_8192_SHA1,
RSA_PKCS1_2048_8192_SHA256,
RSA_PKCS1_2048_8192_SHA384,
RSA_PKCS1_2048_8192_SHA512,
RSA_PKCS1_3072_8192_SHA384,
RSA_PSS_2048_8192_SHA256,
RSA_PSS_2048_8192_SHA384,
RSA_PSS_2048_8192_SHA512,
}
fn parse_public_key(input: untrusted::Input)
-> Result<(untrusted::Input, untrusted::Input),
error::Unspecified> {
input.read_all(error::Unspecified, |input| {
der::nested(input, der::Tag::Sequence, error::Unspecified, |input| {
let n = der::positive_integer(input)?;
let e = der::positive_integer(input)?;
Ok((n, e))
})
})
}
fn check_public_modulus_and_exponent(
n: untrusted::Input, e: untrusted::Input, n_min_bits: bits::BitLength,
n_max_bits: bits::BitLength, e_min_value: u64)
-> Result<(bigint::Modulus<N>, bits::BitLength,
bigint::PublicExponent), error::Unspecified> {
let (n, n_bits) = bigint::Modulus::from_be_bytes_with_bit_length(n)?;
const N_MIN_BITS: bits::BitLength = bits::BitLength(2048);
assert!(n_min_bits >= N_MIN_BITS);
let n_bits_rounded_up =
bits::BitLength::from_usize_bytes(n_bits.as_usize_bytes_rounded_up())?;
if n_bits_rounded_up < n_min_bits {
return Err(error::Unspecified);
}
if n_bits > n_max_bits {
return Err(error::Unspecified);
}
debug_assert!(e_min_value >= 3);
debug_assert!(e_min_value & 1 == 1); debug_assert!(e_min_value <= bigint::PUBLIC_EXPONENT_MAX_VALUE);
let e = bigint::PublicExponent::from_be_bytes(e, e_min_value)?;
Ok((n, n_bits, e))
}
#[derive(Copy, Clone)]
pub enum N {}
pub mod verification;
#[cfg(feature = "rsa_signing")]
pub mod signing;
mod bigint;
#[cfg(feature = "rsa_signing")]
mod blinding;
#[cfg(feature = "rsa_signing")]
mod random;