ring/arithmetic/bigint/
private_exponent.rs

1// Copyright 2015-2023 Brian Smith.
2//
3// Permission to use, copy, modify, and/or distribute this software for any
4// purpose with or without fee is hereby granted, provided that the above
5// copyright notice and this permission notice appear in all copies.
6//
7// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15use super::{limb, BoxedLimbs, Limb, Modulus};
16use crate::error;
17use alloc::boxed::Box;
18
19pub struct PrivateExponent {
20    // Unlike most `[Limb]` we deal with, these are stored most significant
21    // word first.
22    limbs: Box<[Limb]>,
23}
24
25impl PrivateExponent {
26    // `p` is the modulus for which the exponent is in the interval [1, `p` - 1).
27    pub fn from_be_bytes_padded<M>(
28        input: untrusted::Input,
29        p: &Modulus<M>,
30    ) -> Result<Self, error::Unspecified> {
31        let mut dP = BoxedLimbs::from_be_bytes_padded_less_than(input, p)?;
32
33        // Proof that `dP < p - 1`:
34        //
35        // If `dP < p` then either `dP == p - 1` or `dP < p - 1`. Since `p` is
36        // odd, `p - 1` is even. `d` is odd, and an odd number modulo an even
37        // number is odd. Therefore `dP` must be odd. But then it cannot be
38        // `p - 1` and so we know `dP < p - 1`.
39        //
40        // Further we know `dP != 0` because `dP` is not even.
41        limb::limbs_reject_even_leak_bit(&dP)?;
42        dP.reverse();
43
44        Ok(Self {
45            limbs: dP.into_limbs(),
46        })
47    }
48
49    // Create a `PrivateExponent` with a value that we do not support in
50    // production use, to allow testing with additional test vectors.
51    #[cfg(test)]
52    pub fn from_be_bytes_for_test_only<M>(
53        input: untrusted::Input,
54        p: &Modulus<M>,
55    ) -> Result<Self, error::Unspecified> {
56        use crate::limb::LIMB_BYTES;
57
58        // Do exactly what `from_be_bytes_padded` does for any inputs it accepts.
59        if let r @ Ok(_) = Self::from_be_bytes_padded(input, p) {
60            return r;
61        }
62
63        let num_limbs = (input.len() + LIMB_BYTES - 1) / LIMB_BYTES;
64        let mut limbs = BoxedLimbs::<M>::zero(num_limbs);
65        limb::parse_big_endian_and_pad_consttime(input, &mut limbs)
66            .map_err(|error::Unspecified| error::KeyRejected::unexpected_error())?;
67        limbs.reverse();
68        Ok(Self {
69            limbs: limbs.into_limbs(),
70        })
71    }
72
73    #[inline]
74    pub(super) fn limbs(&self) -> &[Limb] {
75        &self.limbs
76    }
77}