#![allow(box_pointers)]
use {bits, bssl, c, error, limb, untrusted};
use arithmetic::montgomery::*;
use core::marker::PhantomData;
use core::ops::{Deref, DerefMut};
use std;
#[cfg(any(test, feature = "rsa_signing"))]
use constant_time;
#[cfg(feature = "rsa_signing")]
use rand;
pub unsafe trait Prime {}
pub trait IsOne {
fn is_one(&self) -> bool;
}
pub struct Width<M> {
num_limbs: usize,
m: PhantomData<M>,
}
struct BoxedLimbs<M> {
limbs: std::boxed::Box<[limb::Limb]>,
m: PhantomData<M>,
}
impl<M> Deref for BoxedLimbs<M> {
type Target = [limb::Limb];
#[inline]
fn deref(&self) -> &Self::Target {
&self.limbs
}
}
impl<M> DerefMut for BoxedLimbs<M> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.limbs
}
}
impl<M> Clone for BoxedLimbs<M> {
fn clone(&self) -> Self {
Self {
limbs: self.limbs.clone(),
m: self.m.clone(),
}
}
}
impl<M> BoxedLimbs<M> {
fn positive_minimal_width_from_be_bytes(input: untrusted::Input)
-> Result<Self, error::Unspecified> {
if untrusted::Reader::new(input).peek(0) {
return Err(error::Unspecified);
}
let num_limbs = (input.len() + limb::LIMB_BYTES - 1) / limb::LIMB_BYTES;
let mut r = Self::zero(Width { num_limbs, m: PhantomData });
limb::parse_big_endian_and_pad_consttime(input, &mut r)?;
Ok(r)
}
#[cfg(feature = "rsa_signing")]
fn minimal_width_from_unpadded(limbs: &[limb::Limb]) -> Self {
debug_assert_ne!(limbs.last(), Some(&0));
use std::borrow::ToOwned;
Self {
limbs: limbs.to_owned().into_boxed_slice(),
m: PhantomData,
}
}
fn from_be_bytes_padded_less_than(input: untrusted::Input, m: &Modulus<M>)
-> Result<Self, error::Unspecified> {
let mut r = Self::zero(m.width());
limb::parse_big_endian_and_pad_consttime(input, &mut r)?;
if limb::limbs_less_than_limbs_consttime(&r, &m.limbs) !=
limb::LimbMask::True {
return Err(error::Unspecified);
}
Ok(r)
}
#[inline]
fn is_zero(&self) -> bool {
limb::limbs_are_zero_constant_time(&self.limbs) == limb::LimbMask::True
}
fn zero(width: Width<M>) -> Self {
use std::borrow::ToOwned;
Self {
limbs: vec![0; width.num_limbs].to_owned().into_boxed_slice(),
m: PhantomData,
}
}
fn width(&self) -> Width<M> {
Width {
num_limbs: self.limbs.len(),
m: PhantomData,
}
}
}
pub unsafe trait SmallerModulus<L> {}
pub unsafe trait SlightlySmallerModulus<L>: SmallerModulus<L> {}
pub unsafe trait NotMuchSmallerModulus<L>: SmallerModulus<L> {}
pub const MODULUS_MAX_LIMBS: usize = 8192 / limb::LIMB_BITS;
pub struct Modulus<M> {
limbs: BoxedLimbs<M>,
n0: N0,
}
impl<M> Modulus<M> {
pub fn from_be_bytes_with_bit_length(input: untrusted::Input)
-> Result<(Self, bits::BitLength), error::Unspecified>
{
let limbs = BoxedLimbs::positive_minimal_width_from_be_bytes(input)?;
let bits = minimal_limbs_bit_length(&limbs);
Ok((Self::from_boxed_limbs(limbs)?, bits))
}
#[cfg(feature = "rsa_signing")]
pub fn from(n: Nonnegative) -> Result<Self, error::Unspecified> {
Self::from_limbs(n.limbs())
}
#[cfg(feature = "rsa_signing")]
fn from_limbs(n: &[limb::Limb]) -> Result<Self, error::Unspecified> {
Self::from_boxed_limbs(BoxedLimbs::minimal_width_from_unpadded(n))
}
fn from_boxed_limbs(n: BoxedLimbs<M>) -> Result<Self, error::Unspecified> {
if n.len() > MODULUS_MAX_LIMBS {
return Err(error::Unspecified);
}
bssl::map_result(unsafe {
GFp_bn_mul_mont_check_num_limbs(n.len())
})?;
if limb::limbs_are_even_constant_time(&n) != limb::LimbMask::False {
return Err(error::Unspecified)
}
if limb::limbs_less_than_limb_constant_time(&n, 3) != limb::LimbMask::False {
return Err(error::Unspecified);
}
let n0 = {
let mut n_mod_r: u64 = u64::from(n[0]);
if N0_LIMBS_USED == 2 {
debug_assert_eq!(limb::LIMB_BITS, 32);
n_mod_r |= u64::from(n[1]) << 32;
}
unsafe { GFp_bn_neg_inv_mod_r_u64(n_mod_r) }
};
Ok(Modulus {
limbs: n,
n0: n0_from_u64(n0),
})
}
#[inline]
fn width(&self) -> Width<M> { self.limbs.width() }
pub fn zero<E>(&self) -> Elem<M, E> {
Elem {
limbs: BoxedLimbs::zero(self.width()),
encoding: PhantomData,
}
}
#[cfg(feature = "rsa_signing")]
fn one(&self) -> Elem<M, Unencoded> {
let mut r = self.zero();
r.limbs[0] = 1;
r
}
#[cfg(feature = "rsa_signing")]
pub fn to_elem<L>(&self, l: &Modulus<L>) -> Elem<L, Unencoded>
where M: SmallerModulus<L>
{
assert_eq!(self.width().num_limbs, l.width().num_limbs);
let limbs = self.limbs.clone();
Elem {
limbs: BoxedLimbs {
limbs: limbs.limbs,
m: PhantomData,
},
encoding: PhantomData,
}
}
}
pub trait ModMul<B, M> {
type Output;
fn mod_mul(&self, b: B, m: &Modulus<M>) -> Self::Output;
}
pub struct Elem<M, E = Unencoded> {
limbs: BoxedLimbs<M>,
encoding: PhantomData<E>,
}
impl<M, E> Clone for Elem<M, E> {
fn clone(&self) -> Self {
Elem {
limbs: self.limbs.clone(),
encoding: self.encoding.clone(),
}
}
}
impl<M, E> Elem<M, E> {
#[inline]
pub fn is_zero(&self) -> bool { self.limbs.is_zero() }
#[cfg(feature = "rsa_signing")]
pub fn take_storage<OtherF>(e: Elem<M, OtherF>) -> Elem<M, E> {
Elem {
limbs: e.limbs,
encoding: PhantomData,
}
}
}
impl<M, E: ReductionEncoding> Elem<M, E> {
fn decode_once(self, m: &Modulus<M>)
-> Elem<M, <E as ReductionEncoding>::Output>
{
let mut limbs = self.limbs;
let num_limbs = m.width().num_limbs;
let mut one = [0; MODULUS_MAX_LIMBS];
one[0] = 1;
let one = &one[..num_limbs]; unsafe {
GFp_bn_mul_mont(limbs.as_mut_ptr(), limbs.as_ptr(),
one.as_ptr(), m.limbs.as_ptr(), &m.n0, num_limbs)
}
Elem {
limbs,
encoding: PhantomData,
}
}
}
impl<M> Elem<M, R> {
#[inline]
pub fn into_unencoded(self, m: &Modulus<M>) -> Elem<M, Unencoded> {
self.decode_once(m)
}
}
impl<M> Elem<M, Unencoded> {
pub fn from_be_bytes_padded(input: untrusted::Input, m: &Modulus<M>)
-> Result<Self, error::Unspecified> {
Ok(Elem {
limbs: BoxedLimbs::from_be_bytes_padded_less_than(input, m)?,
encoding: PhantomData,
})
}
#[inline]
pub fn fill_be_bytes(&self, out: &mut [u8]) {
limb::big_endian_from_limbs_padded(&self.limbs, out)
}
#[cfg(feature = "rsa_signing")]
pub fn into_modulus<MM>(self) -> Result<Modulus<MM>, error::Unspecified> {
Modulus::from_limbs(&self.limbs)
}
}
#[cfg(feature = "rsa_signing")]
impl<M> IsOne for Elem<M, Unencoded> {
fn is_one(&self) -> bool {
limb::limbs_equal_limb_constant_time(&self.limbs, 1) ==
limb::LimbMask::True
}
}
#[cfg(feature = "rsa_signing")]
impl<AF, BF, M> ModMul<Elem<M, BF>, M> for Elem<M, AF>
where (AF, BF): ProductEncoding
{
type Output = Elem<M, <(AF, BF) as ProductEncoding>::Output>;
fn mod_mul(&self, b: Elem<M, BF>, m: &Modulus<M>)
-> <Self as ModMul<Elem<M, BF>, M>>::Output
{
elem_mul(self, b, m)
}
}
pub fn elem_mul<M, AF, BF>(a: &Elem<M, AF>, mut b: Elem<M, BF>, m: &Modulus<M>)
-> Elem<M, <(AF, BF) as ProductEncoding>::Output>
where (AF, BF): ProductEncoding {
unsafe {
GFp_bn_mul_mont(b.limbs.as_mut_ptr(), a.limbs.as_ptr(),
b.limbs.as_ptr(), m.limbs.as_ptr(), &m.n0,
m.limbs.len());
}
Elem {
limbs: b.limbs,
encoding: PhantomData,
}
}
#[cfg(feature = "rsa_signing")]
pub fn elem_set_to_product<M, AF, BF>(
r: &mut Elem<M, <(AF, BF) as ProductEncoding>::Output>,
a: &Elem<M, AF>, b: &Elem<M, BF>, m: &Modulus<M>)
where (AF, BF): ProductEncoding {
unsafe {
GFp_bn_mul_mont(r.limbs.as_mut_ptr(), a.limbs.as_ptr(),
b.limbs.as_ptr(), m.limbs.as_ptr(), &m.n0,
m.limbs.len())
}
}
#[cfg(feature = "rsa_signing")]
pub fn elem_reduced_once<Larger, Smaller: SlightlySmallerModulus<Larger>>(
a: &Elem<Larger, Unencoded>, m: &Modulus<Smaller>)
-> Elem<Smaller, Unencoded> {
let mut r = a.limbs.clone();
assert!(r.len() <= m.limbs.len());
limb::limbs_reduce_once_constant_time(&mut r, &m.limbs);
Elem {
limbs: BoxedLimbs {
limbs: r.limbs,
m: PhantomData,
},
encoding: PhantomData,
}
}
#[cfg(feature = "rsa_signing")]
#[inline]
pub fn elem_reduced<Larger, Smaller: NotMuchSmallerModulus<Larger>>(
a: &Elem<Larger, Unencoded>, m: &Modulus<Smaller>)
-> Result<Elem<Smaller, RInverse>, error::Unspecified> {
let mut tmp = [0; MODULUS_MAX_LIMBS];
let tmp = &mut tmp[..a.limbs.len()];
tmp.copy_from_slice(&a.limbs);
let mut r = m.zero();
bssl::map_result(unsafe {
GFp_bn_from_montgomery_in_place(r.limbs.as_mut_ptr(), r.limbs.len(),
tmp.as_mut_ptr(), tmp.len(),
m.limbs.as_ptr(), m.limbs.len(), &m.n0)
})?;
Ok(r)
}
pub fn elem_squared<M, E>(mut a: Elem<M, E>, m: &Modulus<M>)
-> Elem<M, <(E, E) as ProductEncoding>::Output>
where (E, E): ProductEncoding {
unsafe {
GFp_bn_mul_mont(a.limbs.as_mut_ptr(), a.limbs.as_ptr(),
a.limbs.as_ptr(), m.limbs.as_ptr(), &m.n0,
m.limbs.len());
};
Elem {
limbs: a.limbs,
encoding: PhantomData,
}
}
#[cfg(feature = "rsa_signing")]
pub fn elem_widen<Larger, Smaller: SmallerModulus<Larger>>(
a: Elem<Smaller, Unencoded>, m: &Modulus<Larger>)
-> Elem<Larger, Unencoded>
{
let mut r = m.zero();
r.limbs[..a.limbs.len()].copy_from_slice(&a.limbs);
r
}
#[cfg(feature = "rsa_signing")]
pub fn elem_add<M, E>(mut a: Elem<M, E>, b: Elem<M, E>, m: &Modulus<M>)
-> Elem<M, E>
{
unsafe {
LIMBS_add_mod(a.limbs.as_mut_ptr(), a.limbs.as_ptr(),
b.limbs.as_ptr(), m.limbs.as_ptr(), m.limbs.len())
}
a
}
#[cfg(feature = "rsa_signing")]
pub fn elem_sub<M, E>(mut a: Elem<M, E>, b: &Elem<M, E>, m: &Modulus<M>)
-> Elem<M, E>
{
unsafe {
LIMBS_sub_mod(a.limbs.as_mut_ptr(), a.limbs.as_ptr(), b.limbs.as_ptr(),
m.limbs.as_ptr(), m.limbs.len());
}
a
}
#[derive(Clone)]
pub struct One<M, E>(Elem<M, E>);
#[cfg(feature = "rsa_signing")]
impl<M> One<M, R> {
pub fn newR(oneRR: &One<M, RR>, m: &Modulus<M>) -> One<M, R> {
One(oneRR.0.clone().decode_once(m))
}
}
impl<M> One<M, RR> {
pub fn newRR(m: &Modulus<M>) -> One<M, RR> {
use limb::LIMB_BITS;
let m_bits = minimal_limbs_bit_length(&m.limbs).as_usize_bits();
let lg_RR = ((m_bits + (LIMB_BITS - 1)) / LIMB_BITS * LIMB_BITS) * 2;
let mut r = m.zero();
let bit = m_bits - 1;
r.limbs[bit / LIMB_BITS] = 1 << (bit % LIMB_BITS);
let num_limbs = r.limbs.len();
for _ in bit..lg_RR {
unsafe {
LIMBS_shl_mod(r.limbs.as_mut_ptr(), r.limbs.as_ptr(),
m.limbs.as_ptr(), num_limbs);
}
}
One(r)
}
}
#[cfg(feature = "rsa_signing")]
impl<M> One<M, RRR> {
pub fn newRRR(oneRR: One<M, RR>, m: &Modulus<M>) -> One<M, RRR> {
One(elem_squared(oneRR.0, &m))
}
}
impl<M, E> AsRef<Elem<M, E>> for One<M, E> {
fn as_ref(&self) -> &Elem<M, E> { &self.0 }
}
#[derive(Clone, Copy)]
pub struct PublicExponent(u64);
impl PublicExponent {
pub fn from_be_bytes(input: untrusted::Input, min_value: u64)
-> Result<Self, error::Unspecified> {
if input.len() > 5 {
return Err(error::Unspecified);
}
let value = input.read_all_mut(error::Unspecified, |input| {
if input.peek(0) {
return Err(error::Unspecified);
}
let mut value = 0u64;
loop {
let byte = input.read_byte()?;
value = (value << 8) | u64::from(byte);
if input.at_end() {
return Ok(value);
}
}
})?;
if value & 1 != 1 {
return Err(error::Unspecified);
}
if value < min_value {
return Err(error::Unspecified);
}
if value > PUBLIC_EXPONENT_MAX_VALUE {
return Err(error::Unspecified);
}
Ok(PublicExponent(value))
}
}
pub const PUBLIC_EXPONENT_MAX_VALUE: u64 = (1u64 << 33) - 1;
pub fn elem_exp_vartime<M>(
base: Elem<M, R>, PublicExponent(exponent): PublicExponent,
m: &Modulus<M>) -> Elem<M, R> {
debug_assert_eq!(exponent & 1, 1);
assert!(exponent <= PUBLIC_EXPONENT_MAX_VALUE);
let mut acc = base.clone();
let mut bit = 1 << (64 - 1 - exponent.leading_zeros());
debug_assert!((exponent & bit) != 0);
while bit > 1 {
bit >>= 1;
acc = elem_squared(acc, m);
if (exponent & bit) != 0 {
acc = elem_mul(&base, acc, m);
}
}
acc
}
#[cfg(feature = "rsa_signing")]
pub struct PrivateExponent<M> {
limbs: BoxedLimbs<M>,
}
#[cfg(feature = "rsa_signing")]
impl<M> PrivateExponent<M> {
pub fn from_be_bytes_padded(input: untrusted::Input, p: &Modulus<M>)
-> Result<Self, error::Unspecified> {
let dP = BoxedLimbs::from_be_bytes_padded_less_than(input, p)?;
if limb::limbs_are_even_constant_time(&dP) != limb::LimbMask::False {
return Err(error::Unspecified);
}
Ok(PrivateExponent {
limbs: dP,
})
}
}
#[cfg(feature = "rsa_signing")]
impl<M: Prime> PrivateExponent<M> {
fn for_flt(p: &Modulus<M>) -> Self {
let two = elem_add(p.one(), p.one(), p);
let p_minus_2 = elem_sub(p.zero(), &two, p);
PrivateExponent {
limbs: p_minus_2.limbs,
}
}
}
#[cfg(feature = "rsa_signing")]
pub fn elem_exp_consttime<M>(
base: Elem<M, R>, exponent: &PrivateExponent<M>, oneR: &One<M, R>,
m: &Modulus<M>) -> Result<Elem<M, Unencoded>, error::Unspecified> {
let mut r = Elem {
limbs: base.limbs,
encoding: PhantomData,
};
bssl::map_result(unsafe {
GFp_BN_mod_exp_mont_consttime(r.limbs.as_mut_ptr(), r.limbs.as_ptr(),
exponent.limbs.as_ptr(),
oneR.0.limbs.as_ptr(), m.limbs.as_ptr(),
m.limbs.len(), &m.n0)
})?;
#[cfg(not(target_arch = "x86_64"))]
let r = r.into_unencoded(m);
Ok(r)
}
#[cfg(feature = "rsa_signing")]
pub fn elem_inverse_consttime<M: Prime>(
a: Elem<M, R>,
m: &Modulus<M>,
oneR: &One<M, R>) -> Result<Elem<M, Unencoded>, error::Unspecified> {
elem_exp_consttime(a, &PrivateExponent::for_flt(&m), oneR, m)
}
#[cfg(feature = "rsa_signing")]
pub fn elem_randomize<E>(a: &mut Elem<super::N, E>, m: &Modulus<super::N>,
rng: &rand::SecureRandom)
-> Result<(), error::Unspecified> {
super::random::set_to_rand_mod(&mut a.limbs, &m.limbs, rng)
}
#[cfg(feature = "rsa_signing")]
pub fn verify_inverses_consttime<M, A, B>(a: &A, b: B, m: &Modulus<M>)
-> Result<(), error::Unspecified> where
A: ModMul<B, M>,
<A as ModMul<B, M>>::Output: IsOne
{
if a.mod_mul(b, m).is_one() {
Ok(())
} else {
Err(error::Unspecified)
}
}
#[cfg(feature = "rsa_signing")]
pub fn elem_set_to_inverse_blinded(
r: &mut Elem<super::N, R>, a: &Elem<super::N, Unencoded>,
n: &Modulus<super::N>, rng: &rand::SecureRandom)
-> Result<(), InversionError> {
let blinding_factor = {
let mut tmp = n.zero::<R>();
elem_randomize(&mut tmp, n, rng)?;
tmp
};
let to_blind = a.clone();
let blinded = elem_mul(&blinding_factor, to_blind, n);
let blinded_inverse = elem_inverse(blinded, n)?;
elem_set_to_product(r, &blinding_factor, &blinded_inverse, n);
Ok(())
}
#[cfg(feature = "rsa_signing")]
fn elem_inverse<M>(a: Elem<M, Unencoded>, m: &Modulus<M>)
-> Result<Elem<M, R>, InversionError> {
let inverse = nonnegative_mod_inverse(Nonnegative::from_limbs(&a.limbs)?,
&m.limbs)?;
let r: Elem<M, R> = Elem {
limbs: inverse.to_elem(&m)?.limbs,
encoding: PhantomData,
};
verify_inverses_consttime(&r, a, m)?;
Ok(r)
}
#[cfg(feature = "rsa_signing")]
fn nonnegative_mod_inverse(a: Nonnegative, m_limbs: &[limb::Limb])
-> Result<Nonnegative, InversionError> {
let m = Nonnegative::from_limbs(m_limbs)?;
use limb::*;
debug_assert!(greater_than(&m, &a));
if a.is_zero() {
return Err(InversionError::NoInverse);
}
fn halve(n: &mut Nonnegative) {
debug_assert!(n.is_even());
let mut carry = 0;
for limb in n.limbs_mut().iter_mut().rev() {
let original_value = *limb;
*limb = (original_value >> 1) | (carry << (LIMB_BITS - 1));
carry = original_value & 1;
}
n.0.shrunk_by_at_most_one_bit();
}
fn double(n: &mut Nonnegative) -> Result<(), InversionError> {
let mut carry = 0;
for limb in n.limbs_mut() {
let original_value = *limb;
*limb = (original_value << 1) | carry;
carry = original_value >> (LIMB_BITS - 1);
}
if carry != 0 {
n.0.grow_by_one_bit()?;
}
Ok(())
}
fn add_assign(r: &mut Nonnegative, a: &mut Nonnegative, m_limb_count: usize)
-> Result<(), error::Unspecified> {
let mut carry = 0;
r.0.make_limbs(m_limb_count, |r_limbs| {
a.0.make_limbs(m_limb_count, |a_limbs| {
carry = unsafe {
LIMBS_add_assign(r_limbs.as_mut_ptr(), a_limbs.as_ptr(),
m_limb_count)
};
Ok(())
})
})?;
if carry != 0 {
r.0.grow_by_one_bit()?
}
Ok(())
}
#[inline]
fn sub_assign(r: &mut Nonnegative, a: &mut Nonnegative, m_limb_count: usize)
-> Result<(), error::Unspecified> {
r.0.make_limbs(m_limb_count, |r_limbs| {
a.0.make_limbs(m_limb_count, |a_limbs| {
unsafe {
LIMBS_sub_assign(r_limbs.as_mut_ptr(), a_limbs.as_ptr(),
m_limb_count);
}
Ok(())
})
})
}
let mut u = a;
let mut v = Nonnegative::from_limbs(m_limbs)?; let mut x1 = Nonnegative::one()?;
let mut x2 = Nonnegative::zero()?;
let mut k = 0;
let m_limb_count = m_limbs.len();
while !v.is_zero() {
if v.is_even() {
halve(&mut v);
double(&mut x1)?;
} else if u.is_even() {
halve(&mut u);
double(&mut x2)?;
} else if !greater_than(&u, &v) {
sub_assign(&mut v, &mut u, m_limb_count)?;
halve(&mut v);
add_assign(&mut x2, &mut x1, m_limb_count)?;
double(&mut x1)?;
} else {
sub_assign(&mut u, &mut v, m_limb_count)?;
halve(&mut u);
add_assign(&mut x1, &mut x2, m_limb_count)?;
double(&mut x2)?;
}
k += 1;
}
if !u.is_one() {
return Err(InversionError::NoInverse);
}
if !greater_than(&m, &x1) {
debug_assert!(x1.limbs().len() <= m_limb_count + 1);
x1.0.make_limbs(m_limb_count, |x1_limbs| {
unsafe {
LIMBS_sub_assign(x1_limbs.as_mut_ptr(), m_limbs.as_ptr(),
m_limb_count);
}
Ok(())
})?;
}
assert!(greater_than(&m, &x1));
let n = minimal_limbs_bit_length(m.limbs()).as_usize_bits();
assert!(k >= n);
for _ in n..k {
let mut carry = 0;
if x1.is_odd() {
x1.0.make_limbs(m_limb_count, |x1_limbs| {
carry = unsafe {
LIMBS_add_assign(x1_limbs.as_mut_ptr(), m_limbs.as_ptr(),
m_limb_count)
};
Ok(())
})?;
}
halve(&mut x1);
if carry != 0 {
x1.0.make_limbs(m_limb_count, |limbs| {
*limbs.last_mut().unwrap() |= 1 << (LIMB_BITS - 1);
Ok(())
})?;
}
}
Ok(x1)
}
#[cfg(feature = "rsa_signing")]
pub enum InversionError {
NoInverse,
Unspecified
}
#[cfg(feature = "rsa_signing")]
impl From<error::Unspecified> for InversionError {
fn from(_: error::Unspecified) -> Self { InversionError::Unspecified }
}
#[cfg(any(test, feature = "rsa_signing"))]
pub fn elem_verify_equal_consttime<M, E>(a: &Elem<M, E>, b: &Elem<M, E>)
-> Result<(), error::Unspecified> {
constant_time::verify_slices_are_equal(limb::limbs_as_bytes(&a.limbs),
limb::limbs_as_bytes(&b.limbs))
}
#[cfg(feature = "rsa_signing")]
pub struct Nonnegative(BIGNUM);
#[cfg(feature = "rsa_signing")]
impl Nonnegative {
fn zero() -> Result<Self, error::Unspecified> {
let r = Nonnegative(BIGNUM::zero());
debug_assert!(r.is_zero());
Ok(r)
}
fn from_limbs(source: &[limb::Limb])
-> Result<Self, error::Unspecified>
{
let mut r = Self::zero()?;
r.0.make_limbs(source.len(), |limbs| {
limbs.copy_from_slice(source);
Ok(())
})?;
Ok(r)
}
fn one() -> Result<Self, error::Unspecified> {
let mut r = Self::zero()?;
r.0.make_limbs(1, |limbs| {
limbs[0] = 1;
Ok(())
})?;
Ok(r)
}
pub fn from_be_bytes_with_bit_length(input: untrusted::Input)
-> Result<(Self, bits::BitLength), error::Unspecified> {
let mut r = Self::zero()?;
r.0.make_limbs(
(input.len() + limb::LIMB_BYTES - 1) / limb::LIMB_BYTES, |limbs| {
limb::parse_big_endian_and_pad_consttime(input, limbs)
})?;
let r_bits = minimal_limbs_bit_length(r.limbs());
Ok((r, r_bits))
}
#[inline]
fn is_zero(&self) -> bool { self.limbs().is_empty() }
#[inline]
fn is_even(&self) -> bool { !self.is_odd() }
#[inline]
pub fn is_odd(&self) -> bool {
limb::limbs_are_even_constant_time(self.limbs()) == limb::LimbMask::False
}
#[inline]
fn limbs(&self) -> &[limb::Limb] { self.0.limbs() }
#[inline]
fn limbs_mut(&mut self) -> &mut [limb::Limb] { self.0.limbs_mut() }
pub fn verify_less_than(&self, other: &Self)
-> Result<(), error::Unspecified> {
if !greater_than(other, self) {
return Err(error::Unspecified);
}
Ok(())
}
pub fn to_elem<M>(&self, m: &Modulus<M>)
-> Result<Elem<M, Unencoded>, error::Unspecified> {
self.verify_less_than_modulus(&m)?;
let mut r = m.zero();
r.limbs[0..self.limbs().len()].copy_from_slice(self.limbs());
Ok(r)
}
pub fn verify_less_than_modulus<M>(&self, m: &Modulus<M>)
-> Result<(), error::Unspecified>
{
if self.limbs().len() > m.limbs.len() {
return Err(error::Unspecified);
}
if self.limbs().len() == m.limbs.len() {
if limb::limbs_less_than_limbs_consttime(self.limbs(), &m.limbs)
!= limb::LimbMask::True {
return Err(error::Unspecified)
}
}
return Ok(())
}
}
#[cfg(feature = "rsa_signing")]
impl IsOne for Nonnegative {
fn is_one(&self) -> bool {
limb::limbs_equal_limb_constant_time(self.limbs(), 1) ==
limb::LimbMask::True
}
}
fn minimal_limbs_bit_length(a: &[limb::Limb]) -> bits::BitLength {
let bits = match a.last() {
Some(limb) => {
assert_ne!(*limb, 0);
let high_bits = a.last().map_or(0, |high_limb| {
limb::LIMB_BITS - (high_limb.leading_zeros() as usize)
});
((a.len() - 1) * limb::LIMB_BITS) + high_bits
},
None => 0,
};
bits::BitLength::from_usize_bits(bits)
}
#[cfg(feature = "rsa_signing")]
fn greater_than(a: &Nonnegative, b: &Nonnegative) -> bool {
let a_limbs = a.limbs();
let b_limbs = b.limbs();
if a_limbs.len() == b_limbs.len() {
limb::limbs_less_than_limbs_vartime(b_limbs, a_limbs)
} else {
a_limbs.len() > b_limbs.len()
}
}
type N0 = [limb::Limb; N0_LIMBS];
const N0_LIMBS: usize = 2;
#[cfg(target_pointer_width = "64")]
const N0_LIMBS_USED: usize = 1;
#[cfg(target_pointer_width = "64")]
#[inline]
fn n0_from_u64(n0: u64) -> N0 {
[n0, 0]
}
#[cfg(target_pointer_width = "32")]
const N0_LIMBS_USED: usize = 2;
#[cfg(target_pointer_width = "32")]
#[inline]
fn n0_from_u64(n0: u64) -> N0 {
[n0 as limb::Limb, (n0 >> limb::LIMB_BITS) as limb::Limb]
}
#[cfg(feature = "rsa_signing")]
mod repr_c {
use {bssl, c, error, limb};
use core;
use libc;
#[repr(C)]
pub struct BIGNUM {
d: *mut limb::Limb,
top: c::int,
dmax: c::int,
}
impl Drop for BIGNUM {
fn drop(&mut self) {
unsafe {
let d: *mut limb::Limb = self.d;
libc::free(d as *mut libc::c_void)
}
}
}
impl BIGNUM {
pub fn zero() -> Self {
BIGNUM {
d: core::ptr::null_mut(),
top: 0,
dmax: 0,
}
}
#[inline]
pub fn limbs(&self) -> &[limb::Limb] {
unsafe {
core::slice::from_raw_parts(self.d, self.top as usize)
}
}
#[inline]
pub fn limbs_mut(&mut self) -> &mut [limb::Limb] {
unsafe {
core::slice::from_raw_parts_mut(self.d, self.top as usize)
}
}
pub fn grow_by_one_bit(&mut self) -> Result<(), error::Unspecified> {
let old_top = self.top;
let new_top = old_top + 1;
bssl::map_result(unsafe {
GFp_bn_wexpand(self, new_top)
})?;
self.top = new_top;
self.limbs_mut()[old_top as usize] = 1;
Ok(())
}
pub fn shrunk_by_at_most_one_bit(&mut self) {
if self.limbs().last().map_or(false, |last| *last == 0) {
self.top -= 1;
}
}
pub fn make_limbs<F>(&mut self, num_limbs: usize, f: F)
-> Result<(), error::Unspecified>
where F: FnOnce(&mut [limb::Limb])
-> Result<(), error::Unspecified> {
if num_limbs <= self.top as usize {
self.top = num_limbs as c::int;
} else {
let old_top = self.top as usize;
bssl::map_result(unsafe {
GFp_bn_wexpand(self, num_limbs as c::int)
})?;
self.top = num_limbs as c::int;
for limb in &mut self.limbs_mut()[old_top..] {
*limb = 0;
}
}
f(self.limbs_mut())?;
unsafe {
GFp_bn_correct_top(self)
}
Ok(())
}
}
extern {
fn GFp_bn_correct_top(r: &mut BIGNUM);
fn GFp_bn_wexpand(r: &mut BIGNUM, words: c::int) -> c::int;
}
}
#[cfg(feature = "rsa_signing")]
pub use self::repr_c::BIGNUM;
extern {
fn GFp_bn_mul_mont(r: *mut limb::Limb, a: *const limb::Limb,
b: *const limb::Limb, n: *const limb::Limb,
n0: &N0, num_limbs: c::size_t);
fn GFp_bn_mul_mont_check_num_limbs(num_limbs: c::size_t) -> c::int;
fn GFp_bn_neg_inv_mod_r_u64(n: u64) -> u64;
fn LIMBS_shl_mod(r: *mut limb::Limb, a: *const limb::Limb,
m: *const limb::Limb, num_limbs: c::size_t);
}
#[cfg(feature = "rsa_signing")]
extern {
fn GFp_bn_from_montgomery_in_place(r: *mut limb::Limb, num_r: c::size_t,
a: *mut limb::Limb, num_a: c::size_t,
n: *const limb::Limb, num_n: c::size_t,
n0: &N0) -> c::int;
fn GFp_BN_mod_exp_mont_consttime(r: *mut limb::Limb,
a_mont: *const limb::Limb,
p: *const limb::Limb,
one_mont: *const limb::Limb,
n: *const limb::Limb,
num_limbs: c::size_t, n0: &N0) -> c::int;
fn LIMBS_add_mod(r: *mut limb::Limb, a: *const limb::Limb,
b: *const limb::Limb, m: *const limb::Limb,
num_limbs: c::size_t);
fn LIMBS_sub_mod(r: *mut limb::Limb, a: *const limb::Limb,
b: *const limb::Limb, m: *const limb::Limb,
num_limbs: c::size_t);
fn LIMBS_add_assign(r: *mut limb::Limb, a: *const limb::Limb,
num_limbs: c::size_t) -> limb::Limb;
fn LIMBS_sub_assign(r: *mut limb::Limb, a: *const limb::Limb,
num_limbs: c::size_t);
}
#[cfg(test)]
mod tests {
use super::*;
use untrusted;
use test;
struct M {}
#[cfg(feature = "rsa_signing")]
#[test]
fn test_elem_exp_consttime() {
test::from_file("src/rsa/bigint_elem_exp_consttime_tests.txt",
|section, test_case| {
assert_eq!(section, "");
let m = consume_modulus::<M>(test_case, "M");
let expected_result = consume_elem(test_case, "ModExp", &m);
let base = consume_elem(test_case, "A", &m);
let e = {
let bytes = test_case.consume_bytes("E");
PrivateExponent::from_be_bytes_padded(
untrusted::Input::from(&bytes), &m).expect("valid exponent")
};
let base = into_encoded(base, &m);
let oneRR = One::newRR(&m);
let one = One::newR(&oneRR, &m);
let actual_result = elem_exp_consttime(base, &e, &one, &m).unwrap();
assert_elem_eq(&actual_result, &expected_result);
Ok(())
})
}
#[test]
fn test_elem_exp_vartime() {
test::from_file("src/rsa/bigint_elem_exp_vartime_tests.txt",
|section, test_case| {
assert_eq!(section, "");
let m = consume_modulus::<M>(test_case, "M");
let expected_result = consume_elem(test_case, "ModExp", &m);
let base = consume_elem(test_case, "A", &m);
let e = consume_public_exponent(test_case, "E");
let base = into_encoded(base, &m);
let actual_result = elem_exp_vartime(base, e, &m);
let actual_result = actual_result.into_unencoded(&m);
assert_elem_eq(&actual_result, &expected_result);
Ok(())
})
}
#[cfg(feature = "rsa_signing")]
#[test]
fn test_elem_inverse_invertible() {
test::from_file("src/rsa/bigint_elem_inverse_invertible_tests.txt",
|section, test_case| {
assert_eq!(section, "");
let m = consume_modulus::<M>(test_case, "M");
let a = consume_elem(test_case, "A", &m);
let expected_result = consume_elem(test_case, "R", &m);
let actual_result = match elem_inverse(a, &m) {
Ok(actual_result) => actual_result,
Err(InversionError::Unspecified) => unreachable!("Unspecified"),
Err(InversionError::NoInverse) => unreachable!("No Inverse"),
};
let actual_result = elem_mul(&m.one(), actual_result, &m);
assert_elem_eq(&actual_result, &expected_result);
Ok(())
})
}
#[cfg(feature = "rsa_signing")]
#[test]
fn test_elem_set_to_inverse_blinded_invertible() {
use super::super::N;
let rng = rand::SystemRandom::new();
test::from_file("src/rsa/bigint_elem_inverse_invertible_tests.txt",
|section, test_case| {
assert_eq!(section, "");
let n = consume_modulus::<N>(test_case, "M");
let a = consume_elem(test_case, "A", &n);
let expected_result = consume_elem(test_case, "R", &n);
let mut actual_result = n.zero();
assert!(elem_set_to_inverse_blinded(&mut actual_result, &a, &n,
&rng).is_ok());
let actual_result = elem_mul(&n.one(), actual_result, &n);
assert_elem_eq(&actual_result, &expected_result);
Ok(())
})
}
#[cfg(feature = "rsa_signing")]
#[test]
fn test_elem_inverse_noninvertible() {
test::from_file("src/rsa/bigint_elem_inverse_noninvertible_tests.txt",
|section, test_case| {
assert_eq!(section, "");
let m = consume_modulus::<M>(test_case, "M");
let a = consume_elem(test_case, "A", &m);
match elem_inverse(a, &m) {
Err(InversionError::NoInverse) => (),
Err(InversionError::Unspecified) => unreachable!("Unspecified"),
Ok(..) => unreachable!("No error"),
}
Ok(())
})
}
#[cfg(feature = "rsa_signing")]
#[test]
fn test_elem_set_to_inverse_blinded_noninvertible() {
use super::super::N;
let rng = rand::SystemRandom::new();
test::from_file("src/rsa/bigint_elem_inverse_noninvertible_tests.txt",
|section, test_case| {
assert_eq!(section, "");
let n = consume_modulus::<N>(test_case, "M");
let a = consume_elem(test_case, "A", &n);
let mut actual_result = n.zero();
match elem_set_to_inverse_blinded(&mut actual_result, &a, &n, &rng) {
Err(InversionError::NoInverse) => (),
Err(InversionError::Unspecified) => unreachable!("Unspecified"),
Ok(..) => unreachable!("No error"),
}
Ok(())
})
}
#[test]
fn test_elem_mul() {
test::from_file("src/rsa/bigint_elem_mul_tests.txt",
|section, test_case| {
assert_eq!(section, "");
let m = consume_modulus::<M>(test_case, "M");
let expected_result = consume_elem(test_case, "ModMul", &m);
let a = consume_elem(test_case, "A", &m);
let b = consume_elem(test_case, "B", &m);
let b = into_encoded(b, &m);
let a = into_encoded(a, &m);
let actual_result = elem_mul(&a, b, &m);
let actual_result = actual_result.into_unencoded(&m);
assert_elem_eq(&actual_result, &expected_result);
Ok(())
})
}
#[test]
fn test_elem_squared() {
test::from_file("src/rsa/bigint_elem_squared_tests.txt",
|section, test_case| {
assert_eq!(section, "");
let m = consume_modulus::<M>(test_case, "M");
let expected_result = consume_elem(test_case, "ModSquare", &m);
let a = consume_elem(test_case, "A", &m);
let a = into_encoded(a, &m);
let actual_result = elem_squared(a, &m);
let actual_result = actual_result.into_unencoded(&m);
assert_elem_eq(&actual_result, &expected_result);
Ok(())
})
}
#[cfg(feature = "rsa_signing")]
#[test]
fn test_elem_reduced() {
test::from_file("src/rsa/bigint_elem_reduced_tests.txt",
|section, test_case| {
assert_eq!(section, "");
struct MM {}
unsafe impl SmallerModulus<MM> for M {}
unsafe impl NotMuchSmallerModulus<MM> for M {}
let m = consume_modulus::<M>(test_case, "M");
let expected_result = consume_elem(test_case, "R", &m);
let a = consume_elem_unchecked::<MM>(
test_case, "A", expected_result.limbs.len() * 2);
let actual_result = elem_reduced(&a, &m).unwrap();
let oneRR = One::newRR(&m);
let actual_result = elem_mul(oneRR.as_ref(), actual_result, &m);
assert_elem_eq(&actual_result, &expected_result);
Ok(())
})
}
#[cfg(feature = "rsa_signing")]
#[test]
fn test_elem_reduced_once() {
test::from_file("src/rsa/bigint_elem_reduced_once_tests.txt",
|section, test_case| {
assert_eq!(section, "");
struct N {}
struct QQ {}
unsafe impl SmallerModulus<N> for QQ {}
unsafe impl SlightlySmallerModulus<N> for QQ {}
let qq = consume_modulus::<QQ>(test_case, "QQ");
let expected_result = consume_elem::<QQ>(test_case, "R", &qq);
let n = consume_modulus::<N>(test_case, "N");
let a = consume_elem::<N>(test_case, "A", &n);
let actual_result = elem_reduced_once(&a, &qq);
assert_elem_eq(&actual_result, &expected_result);
Ok(())
})
}
fn consume_elem<M>(test_case: &mut test::TestCase, name: &str, m: &Modulus<M>)
-> Elem<M, Unencoded> {
let value = test_case.consume_bytes(name);
Elem::from_be_bytes_padded(untrusted::Input::from(&value), m).unwrap()
}
#[cfg(feature = "rsa_signing")]
fn consume_elem_unchecked<M>(test_case: &mut test::TestCase, name: &str,
num_limbs: usize) -> Elem<M, Unencoded> {
let value = consume_nonnegative(test_case, name);
let mut limbs = BoxedLimbs::zero(Width { num_limbs, m: PhantomData });
limbs[0..value.limbs().len()].copy_from_slice(value.limbs());
Elem {
limbs,
encoding: PhantomData,
}
}
fn consume_modulus<M>(test_case: &mut test::TestCase, name: &str)
-> Modulus<M> {
let value = test_case.consume_bytes(name);
let (value, _) = Modulus::from_be_bytes_with_bit_length(
untrusted::Input::from(&value)).unwrap();
value
}
fn consume_public_exponent(test_case: &mut test::TestCase, name: &str)
-> PublicExponent {
let bytes = test_case.consume_bytes(name);
PublicExponent::from_be_bytes(
untrusted::Input::from(&bytes), 3).unwrap()
}
#[cfg(feature = "rsa_signing")]
fn consume_nonnegative(test_case: &mut test::TestCase, name: &str)
-> Nonnegative {
let bytes = test_case.consume_bytes(name);
let (r, _r_bits) = Nonnegative::from_be_bytes_with_bit_length(
untrusted::Input::from(&bytes)).unwrap();
r
}
fn assert_elem_eq<M, E>(a: &Elem<M, E>, b: &Elem<M, E>) {
elem_verify_equal_consttime(&a, b).unwrap()
}
fn into_encoded<M>(a: Elem<M, Unencoded>, m: &Modulus<M>) -> Elem<M, R> {
let oneRR = One::newRR(&m);
elem_mul(&oneRR.as_ref(), a, m)
}
}