/rust/registry/src/index.crates.io-6f17d22bba15001f/ring-0.17.14/src/arithmetic/bigint.rs
Line | Count | Source (jump to first uncovered line) |
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 | | |
15 | | //! Multi-precision integers. |
16 | | //! |
17 | | //! # Modular Arithmetic. |
18 | | //! |
19 | | //! Modular arithmetic is done in finite commutative rings ℤ/mℤ for some |
20 | | //! modulus *m*. We work in finite commutative rings instead of finite fields |
21 | | //! because the RSA public modulus *n* is not prime, which means ℤ/nℤ contains |
22 | | //! nonzero elements that have no multiplicative inverse, so ℤ/nℤ is not a |
23 | | //! finite field. |
24 | | //! |
25 | | //! In some calculations we need to deal with multiple rings at once. For |
26 | | //! example, RSA private key operations operate in the rings ℤ/nℤ, ℤ/pℤ, and |
27 | | //! ℤ/qℤ. Types and functions dealing with such rings are all parameterized |
28 | | //! over a type `M` to ensure that we don't wrongly mix up the math, e.g. by |
29 | | //! multiplying an element of ℤ/pℤ by an element of ℤ/qℤ modulo q. This follows |
30 | | //! the "unit" pattern described in [Static checking of units in Servo]. |
31 | | //! |
32 | | //! `Elem` also uses the static unit checking pattern to statically track the |
33 | | //! Montgomery factors that need to be canceled out in each value using it's |
34 | | //! `E` parameter. |
35 | | //! |
36 | | //! [Static checking of units in Servo]: |
37 | | //! https://blog.mozilla.org/research/2014/06/23/static-checking-of-units-in-servo/ |
38 | | |
39 | | use self::boxed_limbs::BoxedLimbs; |
40 | | pub(crate) use self::{ |
41 | | modulus::{Modulus, OwnedModulus}, |
42 | | modulusvalue::OwnedModulusValue, |
43 | | private_exponent::PrivateExponent, |
44 | | }; |
45 | | use super::{inout::AliasingSlices3, limbs512, montgomery::*, LimbSliceError, MAX_LIMBS}; |
46 | | use crate::{ |
47 | | bits::BitLength, |
48 | | c, |
49 | | error::{self, LenMismatchError}, |
50 | | limb::{self, Limb, LIMB_BITS}, |
51 | | polyfill::slice::{self, AsChunks}, |
52 | | }; |
53 | | use core::{ |
54 | | marker::PhantomData, |
55 | | num::{NonZeroU64, NonZeroUsize}, |
56 | | }; |
57 | | |
58 | | mod boxed_limbs; |
59 | | mod modulus; |
60 | | mod modulusvalue; |
61 | | mod private_exponent; |
62 | | |
63 | | pub trait PublicModulus {} |
64 | | |
65 | | // When we need to create a new `Elem`, first we create a `Storage` and then |
66 | | // move its `limbs` into the new element. When we want to recylce an `Elem`'s |
67 | | // memory allocation, we convert it back into a `Storage`. |
68 | | pub struct Storage<M> { |
69 | | limbs: BoxedLimbs<M>, |
70 | | } |
71 | | |
72 | | impl<M, E> From<Elem<M, E>> for Storage<M> { |
73 | 0 | fn from(elem: Elem<M, E>) -> Self { |
74 | 0 | Self { limbs: elem.limbs } |
75 | 0 | } Unexecuted instantiation: <ring::arithmetic::bigint::Storage<ring::rsa::keypair::P> as core::convert::From<ring::arithmetic::bigint::Elem<ring::rsa::keypair::P, ring::arithmetic::montgomery::RInverse>>>::from Unexecuted instantiation: <ring::arithmetic::bigint::Storage<ring::rsa::keypair::Q> as core::convert::From<ring::arithmetic::bigint::Elem<ring::rsa::keypair::Q, ring::arithmetic::montgomery::RInverse>>>::from |
76 | | } |
77 | | |
78 | | /// Elements of ℤ/mℤ for some modulus *m*. |
79 | | // |
80 | | // Defaulting `E` to `Unencoded` is a convenience for callers from outside this |
81 | | // submodule. However, for maximum clarity, we always explicitly use |
82 | | // `Unencoded` within the `bigint` submodule. |
83 | | pub struct Elem<M, E = Unencoded> { |
84 | | limbs: BoxedLimbs<M>, |
85 | | |
86 | | /// The number of Montgomery factors that need to be canceled out from |
87 | | /// `value` to get the actual value. |
88 | | encoding: PhantomData<E>, |
89 | | } |
90 | | |
91 | | impl<M, E> Elem<M, E> { |
92 | 0 | pub fn clone_into(&self, mut out: Storage<M>) -> Self { |
93 | 0 | out.limbs.copy_from_slice(&self.limbs); |
94 | 0 | Self { |
95 | 0 | limbs: out.limbs, |
96 | 0 | encoding: self.encoding, |
97 | 0 | } |
98 | 0 | } Unexecuted instantiation: <ring::arithmetic::bigint::Elem<ring::rsa::N, ring::arithmetic::montgomery::R>>::clone_into Unexecuted instantiation: <ring::arithmetic::bigint::Elem<ring::rsa::N, ring::arithmetic::montgomery::RR>>::clone_into |
99 | | } |
100 | | |
101 | | impl<M, E> Elem<M, E> { |
102 | | #[inline] |
103 | 0 | pub fn is_zero(&self) -> bool { |
104 | 0 | limb::limbs_are_zero(&self.limbs).leak() |
105 | 0 | } |
106 | | } |
107 | | |
108 | | /// Does a Montgomery reduction on `limbs` assuming they are Montgomery-encoded ('R') and assuming |
109 | | /// they are the same size as `m`, but perhaps not reduced mod `m`. The result will be |
110 | | /// fully reduced mod `m`. |
111 | | /// |
112 | | /// WARNING: Takes a `Storage` as an in/out value. |
113 | 0 | fn from_montgomery_amm<M>(mut in_out: Storage<M>, m: &Modulus<M>) -> Elem<M, Unencoded> { |
114 | 0 | let mut one = [0; MAX_LIMBS]; |
115 | 0 | one[0] = 1; |
116 | 0 | let one = &one[..m.limbs().len()]; |
117 | 0 | limbs_mul_mont( |
118 | 0 | (&mut in_out.limbs[..], one), |
119 | 0 | m.limbs(), |
120 | 0 | m.n0(), |
121 | 0 | m.cpu_features(), |
122 | 0 | ) |
123 | 0 | .unwrap_or_else(unwrap_impossible_limb_slice_error); |
124 | 0 | Elem { |
125 | 0 | limbs: in_out.limbs, |
126 | 0 | encoding: PhantomData, |
127 | 0 | } |
128 | 0 | } Unexecuted instantiation: ring::arithmetic::bigint::from_montgomery_amm::<ring::rsa::keypair::P> Unexecuted instantiation: ring::arithmetic::bigint::from_montgomery_amm::<ring::rsa::keypair::Q> |
129 | | |
130 | | #[cfg(any(test, not(target_arch = "x86_64")))] |
131 | | impl<M> Elem<M, R> { |
132 | | #[inline] |
133 | | pub fn into_unencoded(self, m: &Modulus<M>) -> Elem<M, Unencoded> { |
134 | | from_montgomery_amm(Storage::from(self), m) |
135 | | } |
136 | | } |
137 | | |
138 | | impl<M> Elem<M, Unencoded> { |
139 | 0 | pub fn from_be_bytes_padded( |
140 | 0 | input: untrusted::Input, |
141 | 0 | m: &Modulus<M>, |
142 | 0 | ) -> Result<Self, error::Unspecified> { |
143 | 0 | Ok(Self { |
144 | 0 | limbs: BoxedLimbs::from_be_bytes_padded_less_than(input, m)?, |
145 | 0 | encoding: PhantomData, |
146 | | }) |
147 | 0 | } Unexecuted instantiation: <ring::arithmetic::bigint::Elem<ring::rsa::N>>::from_be_bytes_padded Unexecuted instantiation: <ring::arithmetic::bigint::Elem<ring::rsa::keypair::P>>::from_be_bytes_padded |
148 | | |
149 | | #[inline] |
150 | 0 | pub fn fill_be_bytes(&self, out: &mut [u8]) { |
151 | 0 | // See Falko Strenzke, "Manger's Attack revisited", ICICS 2010. |
152 | 0 | limb::big_endian_from_limbs(&self.limbs, out) |
153 | 0 | } |
154 | | } |
155 | | |
156 | 0 | pub fn elem_mul_into<M, AF, BF>( |
157 | 0 | mut out: Storage<M>, |
158 | 0 | a: &Elem<M, AF>, |
159 | 0 | b: &Elem<M, BF>, |
160 | 0 | m: &Modulus<M>, |
161 | 0 | ) -> Elem<M, <(AF, BF) as ProductEncoding>::Output> |
162 | 0 | where |
163 | 0 | (AF, BF): ProductEncoding, |
164 | 0 | { |
165 | 0 | limbs_mul_mont( |
166 | 0 | (out.limbs.as_mut(), b.limbs.as_ref(), a.limbs.as_ref()), |
167 | 0 | m.limbs(), |
168 | 0 | m.n0(), |
169 | 0 | m.cpu_features(), |
170 | 0 | ) |
171 | 0 | .unwrap_or_else(unwrap_impossible_limb_slice_error); |
172 | 0 | Elem { |
173 | 0 | limbs: out.limbs, |
174 | 0 | encoding: PhantomData, |
175 | 0 | } |
176 | 0 | } |
177 | | |
178 | 0 | pub fn elem_mul<M, AF, BF>( |
179 | 0 | a: &Elem<M, AF>, |
180 | 0 | mut b: Elem<M, BF>, |
181 | 0 | m: &Modulus<M>, |
182 | 0 | ) -> Elem<M, <(AF, BF) as ProductEncoding>::Output> |
183 | 0 | where |
184 | 0 | (AF, BF): ProductEncoding, |
185 | 0 | { |
186 | 0 | limbs_mul_mont( |
187 | 0 | (&mut b.limbs[..], &a.limbs[..]), |
188 | 0 | m.limbs(), |
189 | 0 | m.n0(), |
190 | 0 | m.cpu_features(), |
191 | 0 | ) |
192 | 0 | .unwrap_or_else(unwrap_impossible_limb_slice_error); |
193 | 0 | Elem { |
194 | 0 | limbs: b.limbs, |
195 | 0 | encoding: PhantomData, |
196 | 0 | } |
197 | 0 | } Unexecuted instantiation: ring::arithmetic::bigint::elem_mul::<ring::rsa::N, ring::arithmetic::montgomery::R, ring::arithmetic::montgomery::R> Unexecuted instantiation: ring::arithmetic::bigint::elem_mul::<ring::rsa::N, ring::arithmetic::montgomery::R, ring::arithmetic::montgomery::Unencoded> Unexecuted instantiation: ring::arithmetic::bigint::elem_mul::<ring::rsa::N, ring::arithmetic::montgomery::RR, ring::arithmetic::montgomery::Unencoded> Unexecuted instantiation: ring::arithmetic::bigint::elem_mul::<ring::rsa::N, ring::arithmetic::montgomery::Unencoded, ring::arithmetic::montgomery::R> Unexecuted instantiation: ring::arithmetic::bigint::elem_mul::<ring::rsa::keypair::P, ring::arithmetic::montgomery::R, ring::arithmetic::montgomery::Unencoded> Unexecuted instantiation: ring::arithmetic::bigint::elem_mul::<ring::rsa::keypair::P, ring::arithmetic::montgomery::RR, ring::arithmetic::montgomery::RInverse> Unexecuted instantiation: ring::arithmetic::bigint::elem_mul::<ring::rsa::keypair::P, ring::arithmetic::montgomery::RR, ring::arithmetic::montgomery::Unencoded> |
198 | | |
199 | | // r *= 2. |
200 | 0 | fn elem_double<M, AF>(r: &mut Elem<M, AF>, m: &Modulus<M>) { |
201 | 0 | limb::limbs_double_mod(&mut r.limbs, m.limbs()) |
202 | 0 | .unwrap_or_else(unwrap_impossible_len_mismatch_error) |
203 | 0 | } Unexecuted instantiation: ring::arithmetic::bigint::elem_double::<ring::rsa::N, ring::arithmetic::montgomery::R> Unexecuted instantiation: ring::arithmetic::bigint::elem_double::<ring::rsa::keypair::P, ring::arithmetic::montgomery::R> Unexecuted instantiation: ring::arithmetic::bigint::elem_double::<ring::rsa::keypair::Q, ring::arithmetic::montgomery::R> |
204 | | |
205 | | // TODO: This is currently unused, but we intend to eventually use this to |
206 | | // reduce elements (x mod q) mod p in the RSA CRT. If/when we do so, we |
207 | | // should update the testing so it is reflective of that usage, instead of |
208 | | // the old usage. |
209 | 0 | pub fn elem_reduced_once<A, M>( |
210 | 0 | mut r: Storage<M>, |
211 | 0 | a: &Elem<A, Unencoded>, |
212 | 0 | m: &Modulus<M>, |
213 | 0 | other_modulus_len_bits: BitLength, |
214 | 0 | ) -> Elem<M, Unencoded> { |
215 | 0 | assert_eq!(m.len_bits(), other_modulus_len_bits); |
216 | 0 | r.limbs.copy_from_slice(&a.limbs); |
217 | 0 | limb::limbs_reduce_once(&mut r.limbs, m.limbs()) |
218 | 0 | .unwrap_or_else(unwrap_impossible_len_mismatch_error); |
219 | 0 | Elem { |
220 | 0 | limbs: r.limbs, |
221 | 0 | encoding: PhantomData, |
222 | 0 | } |
223 | 0 | } |
224 | | |
225 | | #[inline] |
226 | 0 | pub fn elem_reduced<Larger, Smaller>( |
227 | 0 | mut r: Storage<Smaller>, |
228 | 0 | a: &Elem<Larger, Unencoded>, |
229 | 0 | m: &Modulus<Smaller>, |
230 | 0 | other_prime_len_bits: BitLength, |
231 | 0 | ) -> Elem<Smaller, RInverse> { |
232 | 0 | // This is stricter than required mathematically but this is what we |
233 | 0 | // guarantee and this is easier to check. The real requirement is that |
234 | 0 | // that `a < m*R` where `R` is the Montgomery `R` for `m`. |
235 | 0 | assert_eq!(other_prime_len_bits, m.len_bits()); |
236 | | |
237 | | // `limbs_from_mont_in_place` requires this. |
238 | 0 | assert_eq!(a.limbs.len(), m.limbs().len() * 2); |
239 | | |
240 | 0 | let mut tmp = [0; MAX_LIMBS]; |
241 | 0 | let tmp = &mut tmp[..a.limbs.len()]; |
242 | 0 | tmp.copy_from_slice(&a.limbs); |
243 | 0 |
|
244 | 0 | limbs_from_mont_in_place(&mut r.limbs, tmp, m.limbs(), m.n0()); |
245 | 0 | Elem { |
246 | 0 | limbs: r.limbs, |
247 | 0 | encoding: PhantomData, |
248 | 0 | } |
249 | 0 | } Unexecuted instantiation: ring::arithmetic::bigint::elem_reduced::<ring::rsa::N, ring::rsa::keypair::P> Unexecuted instantiation: ring::arithmetic::bigint::elem_reduced::<ring::rsa::N, ring::rsa::keypair::Q> |
250 | | |
251 | | #[inline] |
252 | 0 | fn elem_squared<M, E>( |
253 | 0 | mut a: Elem<M, E>, |
254 | 0 | m: &Modulus<M>, |
255 | 0 | ) -> Elem<M, <(E, E) as ProductEncoding>::Output> |
256 | 0 | where |
257 | 0 | (E, E): ProductEncoding, |
258 | 0 | { |
259 | 0 | limbs_square_mont(&mut a.limbs, m.limbs(), m.n0(), m.cpu_features()) |
260 | 0 | .unwrap_or_else(unwrap_impossible_limb_slice_error); |
261 | 0 | Elem { |
262 | 0 | limbs: a.limbs, |
263 | 0 | encoding: PhantomData, |
264 | 0 | } |
265 | 0 | } Unexecuted instantiation: ring::arithmetic::bigint::elem_squared::<ring::rsa::N, ring::arithmetic::montgomery::R> Unexecuted instantiation: ring::arithmetic::bigint::elem_squared::<ring::rsa::keypair::P, ring::arithmetic::montgomery::R> Unexecuted instantiation: ring::arithmetic::bigint::elem_squared::<ring::rsa::keypair::P, ring::arithmetic::montgomery::RR> Unexecuted instantiation: ring::arithmetic::bigint::elem_squared::<ring::rsa::keypair::Q, ring::arithmetic::montgomery::R> Unexecuted instantiation: ring::arithmetic::bigint::elem_squared::<ring::rsa::keypair::Q, ring::arithmetic::montgomery::RR> |
266 | | |
267 | 0 | pub fn elem_widen<Larger, Smaller>( |
268 | 0 | mut r: Storage<Larger>, |
269 | 0 | a: Elem<Smaller, Unencoded>, |
270 | 0 | m: &Modulus<Larger>, |
271 | 0 | smaller_modulus_bits: BitLength, |
272 | 0 | ) -> Result<Elem<Larger, Unencoded>, error::Unspecified> { |
273 | 0 | if smaller_modulus_bits >= m.len_bits() { |
274 | 0 | return Err(error::Unspecified); |
275 | 0 | } |
276 | 0 | let (to_copy, to_zero) = r.limbs.split_at_mut(a.limbs.len()); |
277 | 0 | to_copy.copy_from_slice(&a.limbs); |
278 | 0 | to_zero.fill(0); |
279 | 0 | Ok(Elem { |
280 | 0 | limbs: r.limbs, |
281 | 0 | encoding: PhantomData, |
282 | 0 | }) |
283 | 0 | } Unexecuted instantiation: ring::arithmetic::bigint::elem_widen::<ring::rsa::N, ring::rsa::keypair::P> Unexecuted instantiation: ring::arithmetic::bigint::elem_widen::<ring::rsa::N, ring::rsa::keypair::Q> |
284 | | |
285 | | // TODO: Document why this works for all Montgomery factors. |
286 | 0 | pub fn elem_add<M, E>(mut a: Elem<M, E>, b: Elem<M, E>, m: &Modulus<M>) -> Elem<M, E> { |
287 | 0 | limb::limbs_add_assign_mod(&mut a.limbs, &b.limbs, m.limbs()) |
288 | 0 | .unwrap_or_else(unwrap_impossible_len_mismatch_error); |
289 | 0 | a |
290 | 0 | } |
291 | | |
292 | | // TODO: Document why this works for all Montgomery factors. |
293 | 0 | pub fn elem_sub<M, E>(mut a: Elem<M, E>, b: &Elem<M, E>, m: &Modulus<M>) -> Elem<M, E> { |
294 | 0 | prefixed_extern! { |
295 | 0 | // `r` and `a` may alias. |
296 | 0 | fn LIMBS_sub_mod( |
297 | 0 | r: *mut Limb, |
298 | 0 | a: *const Limb, |
299 | 0 | b: *const Limb, |
300 | 0 | m: *const Limb, |
301 | 0 | num_limbs: c::NonZero_size_t, |
302 | 0 | ); |
303 | 0 | } |
304 | 0 | let num_limbs = NonZeroUsize::new(m.limbs().len()).unwrap(); |
305 | 0 | (a.limbs.as_mut(), b.limbs.as_ref()) |
306 | 0 | .with_non_dangling_non_null_pointers_rab(num_limbs, |r, a, b| { |
307 | 0 | let m = m.limbs().as_ptr(); // Also non-dangling because num_limbs is non-zero. |
308 | 0 | unsafe { LIMBS_sub_mod(r, a, b, m, num_limbs) } |
309 | 0 | }) |
310 | 0 | .unwrap_or_else(unwrap_impossible_len_mismatch_error); |
311 | 0 | a |
312 | 0 | } |
313 | | |
314 | | // The value 1, Montgomery-encoded some number of times. |
315 | | pub struct One<M, E>(Elem<M, E>); |
316 | | |
317 | | impl<M> One<M, RR> { |
318 | | // Returns RR = = R**2 (mod n) where R = 2**r is the smallest power of |
319 | | // 2**LIMB_BITS such that R > m. |
320 | | // |
321 | | // Even though the assembly on some 32-bit platforms works with 64-bit |
322 | | // values, using `LIMB_BITS` here, rather than `N0::LIMBS_USED * LIMB_BITS`, |
323 | | // is correct because R**2 will still be a multiple of the latter as |
324 | | // `N0::LIMBS_USED` is either one or two. |
325 | 0 | pub(crate) fn newRR(mut out: Storage<M>, m: &Modulus<M>) -> Self { |
326 | 0 | // The number of limbs in the numbers involved. |
327 | 0 | let w = m.limbs().len(); |
328 | 0 |
|
329 | 0 | // The length of the numbers involved, in bits. R = 2**r. |
330 | 0 | let r = w * LIMB_BITS; |
331 | 0 |
|
332 | 0 | m.oneR(&mut out.limbs); |
333 | 0 | let mut acc: Elem<M, R> = Elem { |
334 | 0 | limbs: out.limbs, |
335 | 0 | encoding: PhantomData, |
336 | 0 | }; |
337 | 0 |
|
338 | 0 | // 2**t * R can be calculated by t doublings starting with R. |
339 | 0 | // |
340 | 0 | // Choose a t that divides r and where t doublings are cheaper than 1 squaring. |
341 | 0 | // |
342 | 0 | // We could choose other values of t than w. But if t < d then the exponentiation that |
343 | 0 | // follows would require multiplications. Normally d is 1 (i.e. the modulus length is a |
344 | 0 | // power of two: RSA 1024, 2048, 4097, 8192) or 3 (RSA 1536, 3072). |
345 | 0 | // |
346 | 0 | // XXX(perf): Currently t = w / 2 is slightly faster. TODO(perf): Optimize `elem_double` |
347 | 0 | // and re-run benchmarks to rebalance this. |
348 | 0 | let t = w; |
349 | 0 | let z = w.trailing_zeros(); |
350 | 0 | let d = w >> z; |
351 | 0 | debug_assert_eq!(w, d * (1 << z)); |
352 | 0 | debug_assert!(d <= t); |
353 | 0 | debug_assert!(t < r); |
354 | 0 | for _ in 0..t { |
355 | 0 | elem_double(&mut acc, m); |
356 | 0 | } |
357 | | |
358 | | // Because t | r: |
359 | | // |
360 | | // MontExp(2**t * R, r / t) |
361 | | // = (2**t)**(r / t) * R (mod m) by definition of MontExp. |
362 | | // = (2**t)**(1/t * r) * R (mod m) |
363 | | // = (2**(t * 1/t))**r * R (mod m) |
364 | | // = (2**1)**r * R (mod m) |
365 | | // = 2**r * R (mod m) |
366 | | // = R * R (mod m) |
367 | | // = RR |
368 | | // |
369 | | // Like BoringSSL, use t = w (`m.limbs.len()`) which ensures that the exponent is a power |
370 | | // of two. Consequently, there will be no multiplications in the Montgomery exponentiation; |
371 | | // there will only be lg(r / t) squarings. |
372 | | // |
373 | | // lg(r / t) |
374 | | // = lg((w * 2**b) / t) |
375 | | // = lg((t * 2**b) / t) |
376 | | // = lg(2**b) |
377 | | // = b |
378 | | // TODO(MSRV:1.67): const B: u32 = LIMB_BITS.ilog2(); |
379 | | const B: u32 = if cfg!(target_pointer_width = "64") { |
380 | | 6 |
381 | | } else if cfg!(target_pointer_width = "32") { |
382 | | 5 |
383 | | } else { |
384 | | panic!("unsupported target_pointer_width") |
385 | | }; |
386 | | #[allow(clippy::assertions_on_constants)] |
387 | | const _LIMB_BITS_IS_2_POW_B: () = assert!(LIMB_BITS == 1 << B); |
388 | 0 | debug_assert_eq!(r, t * (1 << B)); |
389 | 0 | for _ in 0..B { |
390 | 0 | acc = elem_squared(acc, m); |
391 | 0 | } |
392 | | |
393 | 0 | Self(Elem { |
394 | 0 | limbs: acc.limbs, |
395 | 0 | encoding: PhantomData, // PhantomData<RR> |
396 | 0 | }) |
397 | 0 | } Unexecuted instantiation: <ring::arithmetic::bigint::One<ring::rsa::N, ring::arithmetic::montgomery::RR>>::newRR Unexecuted instantiation: <ring::arithmetic::bigint::One<ring::rsa::keypair::P, ring::arithmetic::montgomery::RR>>::newRR Unexecuted instantiation: <ring::arithmetic::bigint::One<ring::rsa::keypair::Q, ring::arithmetic::montgomery::RR>>::newRR |
398 | | } |
399 | | |
400 | | impl<M> One<M, RRR> { |
401 | 0 | pub(crate) fn newRRR(One(oneRR): One<M, RR>, m: &Modulus<M>) -> Self { |
402 | 0 | Self(elem_squared(oneRR, m)) |
403 | 0 | } Unexecuted instantiation: <ring::arithmetic::bigint::One<ring::rsa::keypair::P, ring::arithmetic::montgomery::RRR>>::newRRR Unexecuted instantiation: <ring::arithmetic::bigint::One<ring::rsa::keypair::Q, ring::arithmetic::montgomery::RRR>>::newRRR |
404 | | } |
405 | | |
406 | | impl<M, E> AsRef<Elem<M, E>> for One<M, E> { |
407 | 0 | fn as_ref(&self) -> &Elem<M, E> { |
408 | 0 | &self.0 |
409 | 0 | } Unexecuted instantiation: <ring::arithmetic::bigint::One<ring::rsa::N, ring::arithmetic::montgomery::RR> as core::convert::AsRef<ring::arithmetic::bigint::Elem<ring::rsa::N, ring::arithmetic::montgomery::RR>>>::as_ref Unexecuted instantiation: <ring::arithmetic::bigint::One<ring::rsa::keypair::P, ring::arithmetic::montgomery::RR> as core::convert::AsRef<ring::arithmetic::bigint::Elem<ring::rsa::keypair::P, ring::arithmetic::montgomery::RR>>>::as_ref Unexecuted instantiation: <ring::arithmetic::bigint::One<ring::rsa::keypair::P, ring::arithmetic::montgomery::RRR> as core::convert::AsRef<ring::arithmetic::bigint::Elem<ring::rsa::keypair::P, ring::arithmetic::montgomery::RRR>>>::as_ref Unexecuted instantiation: <ring::arithmetic::bigint::One<ring::rsa::keypair::Q, ring::arithmetic::montgomery::RRR> as core::convert::AsRef<ring::arithmetic::bigint::Elem<ring::rsa::keypair::Q, ring::arithmetic::montgomery::RRR>>>::as_ref |
410 | | } |
411 | | |
412 | | impl<M: PublicModulus, E> One<M, E> { |
413 | 0 | pub fn clone_into(&self, out: Storage<M>) -> Self { |
414 | 0 | Self(self.0.clone_into(out)) |
415 | 0 | } |
416 | | } |
417 | | |
418 | | /// Calculates base**exponent (mod m). |
419 | | /// |
420 | | /// The run time is a function of the number of limbs in `m` and the bit |
421 | | /// length and Hamming Weight of `exponent`. The bounds on `m` are pretty |
422 | | /// obvious but the bounds on `exponent` are less obvious. Callers should |
423 | | /// document the bounds they place on the maximum value and maximum Hamming |
424 | | /// weight of `exponent`. |
425 | | // TODO: The test coverage needs to be expanded, e.g. test with the largest |
426 | | // accepted exponent and with the most common values of 65537 and 3. |
427 | 0 | pub(crate) fn elem_exp_vartime<M>( |
428 | 0 | out: Storage<M>, |
429 | 0 | base: Elem<M, R>, |
430 | 0 | exponent: NonZeroU64, |
431 | 0 | m: &Modulus<M>, |
432 | 0 | ) -> Elem<M, R> { |
433 | 0 | // Use what [Knuth] calls the "S-and-X binary method", i.e. variable-time |
434 | 0 | // square-and-multiply that scans the exponent from the most significant |
435 | 0 | // bit to the least significant bit (left-to-right). Left-to-right requires |
436 | 0 | // less storage compared to right-to-left scanning, at the cost of needing |
437 | 0 | // to compute `exponent.leading_zeros()`, which we assume to be cheap. |
438 | 0 | // |
439 | 0 | // As explained in [Knuth], exponentiation by squaring is the most |
440 | 0 | // efficient algorithm when the Hamming weight is 2 or less. It isn't the |
441 | 0 | // most efficient for all other, uncommon, exponent values but any |
442 | 0 | // suboptimality is bounded at least by the small bit length of `exponent` |
443 | 0 | // as enforced by its type. |
444 | 0 | // |
445 | 0 | // This implementation is slightly simplified by taking advantage of the |
446 | 0 | // fact that we require the exponent to be a positive integer. |
447 | 0 | // |
448 | 0 | // [Knuth]: The Art of Computer Programming, Volume 2: Seminumerical |
449 | 0 | // Algorithms (3rd Edition), Section 4.6.3. |
450 | 0 | let exponent = exponent.get(); |
451 | 0 | let mut acc = base.clone_into(out); |
452 | 0 | let mut bit = 1 << (64 - 1 - exponent.leading_zeros()); |
453 | 0 | debug_assert!((exponent & bit) != 0); |
454 | 0 | while bit > 1 { |
455 | 0 | bit >>= 1; |
456 | 0 | acc = elem_squared(acc, m); |
457 | 0 | if (exponent & bit) != 0 { |
458 | 0 | acc = elem_mul(&base, acc, m); |
459 | 0 | } |
460 | | } |
461 | 0 | acc |
462 | 0 | } |
463 | | |
464 | 0 | pub fn elem_exp_consttime<N, P>( |
465 | 0 | out: Storage<P>, |
466 | 0 | base: &Elem<N>, |
467 | 0 | oneRRR: &One<P, RRR>, |
468 | 0 | exponent: &PrivateExponent, |
469 | 0 | p: &Modulus<P>, |
470 | 0 | other_prime_len_bits: BitLength, |
471 | 0 | ) -> Result<Elem<P, Unencoded>, LimbSliceError> { |
472 | 0 | // `elem_exp_consttime_inner` is parameterized on `STORAGE_LIMBS` only so |
473 | 0 | // we can run tests with larger-than-supported-in-operation test vectors. |
474 | 0 | elem_exp_consttime_inner::<N, P, { ELEM_EXP_CONSTTIME_MAX_MODULUS_LIMBS * STORAGE_ENTRIES }>( |
475 | 0 | out, |
476 | 0 | base, |
477 | 0 | oneRRR, |
478 | 0 | exponent, |
479 | 0 | p, |
480 | 0 | other_prime_len_bits, |
481 | 0 | ) |
482 | 0 | } Unexecuted instantiation: ring::arithmetic::bigint::elem_exp_consttime::<ring::rsa::N, ring::rsa::keypair::P> Unexecuted instantiation: ring::arithmetic::bigint::elem_exp_consttime::<ring::rsa::N, ring::rsa::keypair::Q> |
483 | | |
484 | | // The maximum modulus size supported for `elem_exp_consttime` in normal |
485 | | // operation. |
486 | | const ELEM_EXP_CONSTTIME_MAX_MODULUS_LIMBS: usize = 2048 / LIMB_BITS; |
487 | | const _LIMBS_PER_CHUNK_DIVIDES_ELEM_EXP_CONSTTIME_MAX_MODULUS_LIMBS: () = |
488 | | assert!(ELEM_EXP_CONSTTIME_MAX_MODULUS_LIMBS % limbs512::LIMBS_PER_CHUNK == 0); |
489 | | const WINDOW_BITS: u32 = 5; |
490 | | const TABLE_ENTRIES: usize = 1 << WINDOW_BITS; |
491 | | const STORAGE_ENTRIES: usize = TABLE_ENTRIES + if cfg!(target_arch = "x86_64") { 3 } else { 0 }; |
492 | | |
493 | | #[cfg(not(target_arch = "x86_64"))] |
494 | | fn elem_exp_consttime_inner<N, M, const STORAGE_LIMBS: usize>( |
495 | | out: Storage<M>, |
496 | | base_mod_n: &Elem<N>, |
497 | | oneRRR: &One<M, RRR>, |
498 | | exponent: &PrivateExponent, |
499 | | m: &Modulus<M>, |
500 | | other_prime_len_bits: BitLength, |
501 | | ) -> Result<Elem<M, Unencoded>, LimbSliceError> { |
502 | | use crate::{bssl, limb::Window}; |
503 | | |
504 | | let base_rinverse: Elem<M, RInverse> = elem_reduced(out, base_mod_n, m, other_prime_len_bits); |
505 | | |
506 | | let num_limbs = m.limbs().len(); |
507 | | let m_chunked: AsChunks<Limb, { limbs512::LIMBS_PER_CHUNK }> = match slice::as_chunks(m.limbs()) |
508 | | { |
509 | | (m, []) => m, |
510 | | _ => { |
511 | | return Err(LimbSliceError::len_mismatch(LenMismatchError::new( |
512 | | num_limbs, |
513 | | ))) |
514 | | } |
515 | | }; |
516 | | let cpe = m_chunked.len(); // 512-bit chunks per entry. |
517 | | |
518 | | // This code doesn't have the strict alignment requirements that the x86_64 |
519 | | // version does, but uses the same aligned storage for convenience. |
520 | | assert!(STORAGE_LIMBS % (STORAGE_ENTRIES * limbs512::LIMBS_PER_CHUNK) == 0); // TODO: `const` |
521 | | let mut table = limbs512::AlignedStorage::<STORAGE_LIMBS>::zeroed(); |
522 | | let mut table = table |
523 | | .aligned_chunks_mut(TABLE_ENTRIES, cpe) |
524 | | .map_err(LimbSliceError::len_mismatch)?; |
525 | | |
526 | | // TODO: Rewrite the below in terms of `AsChunks`. |
527 | | let table = table.as_flattened_mut(); |
528 | | |
529 | | fn gather<M>(table: &[Limb], acc: &mut Elem<M, R>, i: Window) { |
530 | | prefixed_extern! { |
531 | | fn LIMBS_select_512_32( |
532 | | r: *mut Limb, |
533 | | table: *const Limb, |
534 | | num_limbs: c::size_t, |
535 | | i: Window, |
536 | | ) -> bssl::Result; |
537 | | } |
538 | | Result::from(unsafe { |
539 | | LIMBS_select_512_32(acc.limbs.as_mut_ptr(), table.as_ptr(), acc.limbs.len(), i) |
540 | | }) |
541 | | .unwrap(); |
542 | | } |
543 | | |
544 | | fn power<M>( |
545 | | table: &[Limb], |
546 | | mut acc: Elem<M, R>, |
547 | | m: &Modulus<M>, |
548 | | i: Window, |
549 | | mut tmp: Elem<M, R>, |
550 | | ) -> (Elem<M, R>, Elem<M, R>) { |
551 | | for _ in 0..WINDOW_BITS { |
552 | | acc = elem_squared(acc, m); |
553 | | } |
554 | | gather(table, &mut tmp, i); |
555 | | let acc = elem_mul(&tmp, acc, m); |
556 | | (acc, tmp) |
557 | | } |
558 | | |
559 | | fn entry(table: &[Limb], i: usize, num_limbs: usize) -> &[Limb] { |
560 | | &table[(i * num_limbs)..][..num_limbs] |
561 | | } |
562 | | fn entry_mut(table: &mut [Limb], i: usize, num_limbs: usize) -> &mut [Limb] { |
563 | | &mut table[(i * num_limbs)..][..num_limbs] |
564 | | } |
565 | | |
566 | | // table[0] = base**0 (i.e. 1). |
567 | | m.oneR(entry_mut(table, 0, num_limbs)); |
568 | | |
569 | | // table[1] = base*R == (base/R * RRR)/R |
570 | | limbs_mul_mont( |
571 | | ( |
572 | | entry_mut(table, 1, num_limbs), |
573 | | base_rinverse.limbs.as_ref(), |
574 | | oneRRR.as_ref().limbs.as_ref(), |
575 | | ), |
576 | | m.limbs(), |
577 | | m.n0(), |
578 | | m.cpu_features(), |
579 | | )?; |
580 | | for i in 2..TABLE_ENTRIES { |
581 | | let (src1, src2) = if i % 2 == 0 { |
582 | | (i / 2, i / 2) |
583 | | } else { |
584 | | (i - 1, 1) |
585 | | }; |
586 | | let (previous, rest) = table.split_at_mut(num_limbs * i); |
587 | | let src1 = entry(previous, src1, num_limbs); |
588 | | let src2 = entry(previous, src2, num_limbs); |
589 | | let dst = entry_mut(rest, 0, num_limbs); |
590 | | limbs_mul_mont((dst, src1, src2), m.limbs(), m.n0(), m.cpu_features())?; |
591 | | } |
592 | | |
593 | | let mut acc = Elem { |
594 | | limbs: base_rinverse.limbs, |
595 | | encoding: PhantomData, |
596 | | }; |
597 | | let tmp = m.alloc_zero(); |
598 | | let tmp = Elem { |
599 | | limbs: tmp.limbs, |
600 | | encoding: PhantomData, |
601 | | }; |
602 | | let (acc, _) = limb::fold_5_bit_windows( |
603 | | exponent.limbs(), |
604 | | |initial_window| { |
605 | | gather(&table, &mut acc, initial_window); |
606 | | (acc, tmp) |
607 | | }, |
608 | | |(acc, tmp), window| power(&table, acc, m, window, tmp), |
609 | | ); |
610 | | |
611 | | Ok(acc.into_unencoded(m)) |
612 | | } |
613 | | |
614 | | #[cfg(target_arch = "x86_64")] |
615 | 0 | fn elem_exp_consttime_inner<N, M, const STORAGE_LIMBS: usize>( |
616 | 0 | out: Storage<M>, |
617 | 0 | base_mod_n: &Elem<N>, |
618 | 0 | oneRRR: &One<M, RRR>, |
619 | 0 | exponent: &PrivateExponent, |
620 | 0 | m: &Modulus<M>, |
621 | 0 | other_prime_len_bits: BitLength, |
622 | 0 | ) -> Result<Elem<M, Unencoded>, LimbSliceError> { |
623 | | use super::limbs::x86_64::mont::{ |
624 | | gather5, mul_mont5, mul_mont_gather5_amm, power5_amm, scatter5, sqr_mont5, |
625 | | }; |
626 | | use crate::{ |
627 | | cpu::{ |
628 | | intel::{Adx, Bmi2}, |
629 | | GetFeature as _, |
630 | | }, |
631 | | limb::{LeakyWindow, Window}, |
632 | | polyfill::slice::AsChunksMut, |
633 | | }; |
634 | | |
635 | 0 | let n0 = m.n0(); |
636 | 0 |
|
637 | 0 | let cpu2 = m.cpu_features().get_feature(); |
638 | 0 | let cpu3 = m.cpu_features().get_feature(); |
639 | 0 |
|
640 | 0 | if base_mod_n.limbs.len() != m.limbs().len() * 2 { |
641 | 0 | return Err(LimbSliceError::len_mismatch(LenMismatchError::new( |
642 | 0 | base_mod_n.limbs.len(), |
643 | 0 | ))); |
644 | 0 | } |
645 | | |
646 | 0 | let m_original: AsChunks<Limb, 8> = match slice::as_chunks(m.limbs()) { |
647 | 0 | (m, []) => m, |
648 | 0 | _ => return Err(LimbSliceError::len_mismatch(LenMismatchError::new(8))), |
649 | | }; |
650 | 0 | let cpe = m_original.len(); // 512-bit chunks per entry |
651 | 0 |
|
652 | 0 | let oneRRR = &oneRRR.as_ref().limbs; |
653 | 0 | let oneRRR = match slice::as_chunks(oneRRR) { |
654 | 0 | (c, []) => c, |
655 | | _ => { |
656 | 0 | return Err(LimbSliceError::len_mismatch(LenMismatchError::new( |
657 | 0 | oneRRR.len(), |
658 | 0 | ))) |
659 | | } |
660 | | }; |
661 | | |
662 | | // The x86_64 assembly was written under the assumption that the input data |
663 | | // is aligned to `MOD_EXP_CTIME_ALIGN` bytes, which was/is 64 in OpenSSL. |
664 | | // Subsequently, it was changed such that, according to BoringSSL, they |
665 | | // only require 16 byte alignment. We enforce the old, stronger, alignment |
666 | | // unless/until we can see a benefit to reducing it. |
667 | | // |
668 | | // Similarly, OpenSSL uses the x86_64 assembly functions by giving it only |
669 | | // inputs `tmp`, `am`, and `np` that immediately follow the table. |
670 | | // According to BoringSSL, in older versions of the OpenSSL code, this |
671 | | // extra space was required for memory safety because the assembly code |
672 | | // would over-read the table; according to BoringSSL, this is no longer the |
673 | | // case. Regardless, the upstream code also contained comments implying |
674 | | // that this was also important for performance. For now, we do as OpenSSL |
675 | | // did/does. |
676 | | const MOD_EXP_CTIME_ALIGN: usize = 64; |
677 | | // Required by |
678 | | const _TABLE_ENTRIES_IS_32: () = assert!(TABLE_ENTRIES == 32); |
679 | | const _STORAGE_ENTRIES_HAS_3_EXTRA: () = assert!(STORAGE_ENTRIES == TABLE_ENTRIES + 3); |
680 | | |
681 | 0 | assert!(STORAGE_LIMBS % (STORAGE_ENTRIES * limbs512::LIMBS_PER_CHUNK) == 0); // TODO: `const` |
682 | 0 | let mut table = limbs512::AlignedStorage::<STORAGE_LIMBS>::zeroed(); |
683 | 0 | let mut table = table |
684 | 0 | .aligned_chunks_mut(STORAGE_ENTRIES, cpe) |
685 | 0 | .map_err(LimbSliceError::len_mismatch)?; |
686 | 0 | let (mut table, mut state) = table.split_at_mut(TABLE_ENTRIES * cpe); |
687 | 0 | assert_eq!((table.as_ptr() as usize) % MOD_EXP_CTIME_ALIGN, 0); |
688 | | |
689 | | // These are named `(tmp, am, np)` in BoringSSL. |
690 | 0 | let (mut acc, mut rest) = state.split_at_mut(cpe); |
691 | 0 | let (mut base_cached, mut m_cached) = rest.split_at_mut(cpe); |
692 | 0 |
|
693 | 0 | // "To improve cache locality" according to upstream. |
694 | 0 | m_cached |
695 | 0 | .as_flattened_mut() |
696 | 0 | .copy_from_slice(m_original.as_flattened()); |
697 | 0 | let m_cached = m_cached.as_ref(); |
698 | 0 |
|
699 | 0 | let out: Elem<M, RInverse> = elem_reduced(out, base_mod_n, m, other_prime_len_bits); |
700 | 0 | let base_rinverse = match slice::as_chunks(&out.limbs) { |
701 | 0 | (c, []) => c, |
702 | | _ => { |
703 | 0 | return Err(LimbSliceError::len_mismatch(LenMismatchError::new( |
704 | 0 | out.limbs.len(), |
705 | 0 | ))) |
706 | | } |
707 | | }; |
708 | | |
709 | | // base_cached = base*R == (base/R * RRR)/R |
710 | 0 | mul_mont5( |
711 | 0 | base_cached.as_mut(), |
712 | 0 | base_rinverse, |
713 | 0 | oneRRR, |
714 | 0 | m_cached, |
715 | 0 | n0, |
716 | 0 | cpu2, |
717 | 0 | )?; |
718 | 0 | let base_cached = base_cached.as_ref(); |
719 | 0 | let mut out = Storage::from(out); // recycle. |
720 | | |
721 | | // Fill in all the powers of 2 of `acc` into the table using only squaring and without any |
722 | | // gathering, storing the last calculated power into `acc`. |
723 | 0 | fn scatter_powers_of_2( |
724 | 0 | mut table: AsChunksMut<Limb, 8>, |
725 | 0 | mut acc: AsChunksMut<Limb, 8>, |
726 | 0 | m_cached: AsChunks<Limb, 8>, |
727 | 0 | n0: &N0, |
728 | 0 | mut i: LeakyWindow, |
729 | 0 | cpu: Option<(Adx, Bmi2)>, |
730 | 0 | ) -> Result<(), LimbSliceError> { |
731 | | loop { |
732 | 0 | scatter5(acc.as_ref(), table.as_mut(), i)?; |
733 | 0 | i *= 2; |
734 | 0 | if i >= TABLE_ENTRIES as LeakyWindow { |
735 | 0 | break; |
736 | 0 | } |
737 | 0 | sqr_mont5(acc.as_mut(), m_cached, n0, cpu)?; |
738 | | } |
739 | 0 | Ok(()) |
740 | 0 | } |
741 | | |
742 | | // All entries in `table` will be Montgomery encoded. |
743 | | |
744 | | // acc = table[0] = base**0 (i.e. 1). |
745 | 0 | m.oneR(acc.as_flattened_mut()); |
746 | 0 | scatter5(acc.as_ref(), table.as_mut(), 0)?; |
747 | | |
748 | | // acc = base**1 (i.e. base). |
749 | 0 | acc.as_flattened_mut() |
750 | 0 | .copy_from_slice(base_cached.as_flattened()); |
751 | 0 |
|
752 | 0 | // Fill in entries 1, 2, 4, 8, 16. |
753 | 0 | scatter_powers_of_2(table.as_mut(), acc.as_mut(), m_cached, n0, 1, cpu2)?; |
754 | | // Fill in entries 3, 6, 12, 24; 5, 10, 20, 30; 7, 14, 28; 9, 18; 11, 22; 13, 26; 15, 30; |
755 | | // 17; 19; 21; 23; 25; 27; 29; 31. |
756 | 0 | for i in (3..(TABLE_ENTRIES as LeakyWindow)).step_by(2) { |
757 | 0 | let power = Window::from(i - 1); |
758 | 0 | assert!(power < 32); // Not secret, |
759 | | unsafe { |
760 | 0 | mul_mont_gather5_amm( |
761 | 0 | acc.as_mut(), |
762 | 0 | base_cached, |
763 | 0 | table.as_ref(), |
764 | 0 | m_cached, |
765 | 0 | n0, |
766 | 0 | power, |
767 | 0 | cpu3, |
768 | 0 | ) |
769 | 0 | }?; |
770 | 0 | scatter_powers_of_2(table.as_mut(), acc.as_mut(), m_cached, n0, i, cpu2)?; |
771 | | } |
772 | | |
773 | 0 | let table = table.as_ref(); |
774 | 0 |
|
775 | 0 | let acc = limb::fold_5_bit_windows( |
776 | 0 | exponent.limbs(), |
777 | 0 | |initial_window| { |
778 | 0 | unsafe { gather5(acc.as_mut(), table, initial_window) } |
779 | 0 | .unwrap_or_else(unwrap_impossible_limb_slice_error); |
780 | 0 | acc |
781 | 0 | }, Unexecuted instantiation: ring::arithmetic::bigint::elem_exp_consttime_inner::<ring::rsa::N, ring::rsa::keypair::P, 1120>::{closure#0} Unexecuted instantiation: ring::arithmetic::bigint::elem_exp_consttime_inner::<ring::rsa::N, ring::rsa::keypair::Q, 1120>::{closure#0} |
782 | 0 | |mut acc, window| { |
783 | 0 | unsafe { power5_amm(acc.as_mut(), table, m_cached, n0, window, cpu3) } |
784 | 0 | .unwrap_or_else(unwrap_impossible_limb_slice_error); |
785 | 0 | acc |
786 | 0 | }, Unexecuted instantiation: ring::arithmetic::bigint::elem_exp_consttime_inner::<ring::rsa::N, ring::rsa::keypair::P, 1120>::{closure#1} Unexecuted instantiation: ring::arithmetic::bigint::elem_exp_consttime_inner::<ring::rsa::N, ring::rsa::keypair::Q, 1120>::{closure#1} |
787 | 0 | ); |
788 | 0 |
|
789 | 0 | // Reuse `base_rinverse`'s limbs to save an allocation. |
790 | 0 | out.limbs.copy_from_slice(acc.as_flattened()); |
791 | 0 | Ok(from_montgomery_amm(out, m)) |
792 | 0 | } Unexecuted instantiation: ring::arithmetic::bigint::elem_exp_consttime_inner::<ring::rsa::N, ring::rsa::keypair::P, 1120> Unexecuted instantiation: ring::arithmetic::bigint::elem_exp_consttime_inner::<ring::rsa::N, ring::rsa::keypair::Q, 1120> |
793 | | |
794 | | /// Verified a == b**-1 (mod m), i.e. a**-1 == b (mod m). |
795 | 0 | pub fn verify_inverses_consttime<M>( |
796 | 0 | a: &Elem<M, R>, |
797 | 0 | b: Elem<M, Unencoded>, |
798 | 0 | m: &Modulus<M>, |
799 | 0 | ) -> Result<(), error::Unspecified> { |
800 | 0 | let r = elem_mul(a, b, m); |
801 | 0 | limb::verify_limbs_equal_1_leak_bit(&r.limbs) |
802 | 0 | } |
803 | | |
804 | | #[inline] |
805 | 0 | pub fn elem_verify_equal_consttime<M, E>( |
806 | 0 | a: &Elem<M, E>, |
807 | 0 | b: &Elem<M, E>, |
808 | 0 | ) -> Result<(), error::Unspecified> { |
809 | 0 | let equal = limb::limbs_equal_limbs_consttime(&a.limbs, &b.limbs) |
810 | 0 | .unwrap_or_else(unwrap_impossible_len_mismatch_error); |
811 | 0 | if !equal.leak() { |
812 | 0 | return Err(error::Unspecified); |
813 | 0 | } |
814 | 0 | Ok(()) |
815 | 0 | } |
816 | | |
817 | | #[cold] |
818 | | #[inline(never)] |
819 | 0 | fn unwrap_impossible_len_mismatch_error<T>(LenMismatchError { .. }: LenMismatchError) -> T { |
820 | 0 | unreachable!() Unexecuted instantiation: ring::arithmetic::bigint::unwrap_impossible_len_mismatch_error::<ring::bb::boolmask::BoolMask> Unexecuted instantiation: ring::arithmetic::bigint::unwrap_impossible_len_mismatch_error::<()> |
821 | | } |
822 | | |
823 | | #[cold] |
824 | | #[inline(never)] |
825 | 0 | fn unwrap_impossible_limb_slice_error(err: LimbSliceError) { |
826 | 0 | match err { |
827 | 0 | LimbSliceError::LenMismatch(_) => unreachable!(), |
828 | 0 | LimbSliceError::TooShort(_) => unreachable!(), |
829 | 0 | LimbSliceError::TooLong(_) => unreachable!(), |
830 | | } |
831 | | } |
832 | | |
833 | | #[cfg(test)] |
834 | | mod tests { |
835 | | use super::*; |
836 | | use crate::cpu; |
837 | | use crate::testutil as test; |
838 | | |
839 | | // Type-level representation of an arbitrary modulus. |
840 | | struct M {} |
841 | | |
842 | | impl PublicModulus for M {} |
843 | | |
844 | | #[test] |
845 | | fn test_elem_exp_consttime() { |
846 | | let cpu_features = cpu::features(); |
847 | | test::run( |
848 | | test_vector_file!("../../crypto/fipsmodule/bn/test/mod_exp_tests.txt"), |
849 | | |section, test_case| { |
850 | | assert_eq!(section, ""); |
851 | | |
852 | | let m = consume_modulus::<M>(test_case, "M"); |
853 | | let m = m.modulus(cpu_features); |
854 | | let expected_result = consume_elem(test_case, "ModExp", &m); |
855 | | let base = consume_elem(test_case, "A", &m); |
856 | | let e = { |
857 | | let bytes = test_case.consume_bytes("E"); |
858 | | PrivateExponent::from_be_bytes_for_test_only(untrusted::Input::from(&bytes), &m) |
859 | | .expect("valid exponent") |
860 | | }; |
861 | | |
862 | | let oneRR = One::newRR(m.alloc_zero(), &m); |
863 | | let oneRRR = One::newRRR(oneRR, &m); |
864 | | |
865 | | // `base` in the test vectors is reduced (mod M) already but |
866 | | // the API expects the bsae to be (mod N) where N = M * P for |
867 | | // some other prime of the same length. Fake that here. |
868 | | // Pretend there's another prime of equal length. |
869 | | struct N {} |
870 | | let other_modulus_len_bits = m.len_bits(); |
871 | | let base: Elem<N> = { |
872 | | let mut limbs = BoxedLimbs::zero(base.limbs.len() * 2); |
873 | | limbs[..base.limbs.len()].copy_from_slice(&base.limbs); |
874 | | Elem { |
875 | | limbs, |
876 | | encoding: PhantomData, |
877 | | } |
878 | | }; |
879 | | |
880 | | let too_big = m.limbs().len() > ELEM_EXP_CONSTTIME_MAX_MODULUS_LIMBS; |
881 | | let actual_result = if !too_big { |
882 | | elem_exp_consttime( |
883 | | m.alloc_zero(), |
884 | | &base, |
885 | | &oneRRR, |
886 | | &e, |
887 | | &m, |
888 | | other_modulus_len_bits, |
889 | | ) |
890 | | } else { |
891 | | let actual_result = elem_exp_consttime( |
892 | | m.alloc_zero(), |
893 | | &base, |
894 | | &oneRRR, |
895 | | &e, |
896 | | &m, |
897 | | other_modulus_len_bits, |
898 | | ); |
899 | | // TODO: Be more specific with which error we expect? |
900 | | assert!(actual_result.is_err()); |
901 | | // Try again with a larger-than-normally-supported limit |
902 | | elem_exp_consttime_inner::<_, _, { (4096 / LIMB_BITS) * STORAGE_ENTRIES }>( |
903 | | m.alloc_zero(), |
904 | | &base, |
905 | | &oneRRR, |
906 | | &e, |
907 | | &m, |
908 | | other_modulus_len_bits, |
909 | | ) |
910 | | }; |
911 | | match actual_result { |
912 | | Ok(r) => assert_elem_eq(&r, &expected_result), |
913 | | Err(LimbSliceError::LenMismatch { .. }) => panic!(), |
914 | | Err(LimbSliceError::TooLong { .. }) => panic!(), |
915 | | Err(LimbSliceError::TooShort { .. }) => panic!(), |
916 | | }; |
917 | | |
918 | | Ok(()) |
919 | | }, |
920 | | ) |
921 | | } |
922 | | |
923 | | // TODO: fn test_elem_exp_vartime() using |
924 | | // "src/rsa/bigint_elem_exp_vartime_tests.txt". See that file for details. |
925 | | // In the meantime, the function is tested indirectly via the RSA |
926 | | // verification and signing tests. |
927 | | #[test] |
928 | | fn test_elem_mul() { |
929 | | let cpu_features = cpu::features(); |
930 | | test::run( |
931 | | test_vector_file!("../../crypto/fipsmodule/bn/test/mod_mul_tests.txt"), |
932 | | |section, test_case| { |
933 | | assert_eq!(section, ""); |
934 | | |
935 | | let m = consume_modulus::<M>(test_case, "M"); |
936 | | let m = m.modulus(cpu_features); |
937 | | let expected_result = consume_elem(test_case, "ModMul", &m); |
938 | | let a = consume_elem(test_case, "A", &m); |
939 | | let b = consume_elem(test_case, "B", &m); |
940 | | |
941 | | let b = into_encoded(m.alloc_zero(), b, &m); |
942 | | let a = into_encoded(m.alloc_zero(), a, &m); |
943 | | let actual_result = elem_mul(&a, b, &m); |
944 | | let actual_result = actual_result.into_unencoded(&m); |
945 | | assert_elem_eq(&actual_result, &expected_result); |
946 | | |
947 | | Ok(()) |
948 | | }, |
949 | | ) |
950 | | } |
951 | | |
952 | | #[test] |
953 | | fn test_elem_squared() { |
954 | | let cpu_features = cpu::features(); |
955 | | test::run( |
956 | | test_vector_file!("bigint_elem_squared_tests.txt"), |
957 | | |section, test_case| { |
958 | | assert_eq!(section, ""); |
959 | | |
960 | | let m = consume_modulus::<M>(test_case, "M"); |
961 | | let m = m.modulus(cpu_features); |
962 | | let expected_result = consume_elem(test_case, "ModSquare", &m); |
963 | | let a = consume_elem(test_case, "A", &m); |
964 | | |
965 | | let a = into_encoded(m.alloc_zero(), a, &m); |
966 | | let actual_result = elem_squared(a, &m); |
967 | | let actual_result = actual_result.into_unencoded(&m); |
968 | | assert_elem_eq(&actual_result, &expected_result); |
969 | | |
970 | | Ok(()) |
971 | | }, |
972 | | ) |
973 | | } |
974 | | |
975 | | #[test] |
976 | | fn test_elem_reduced() { |
977 | | let cpu_features = cpu::features(); |
978 | | test::run( |
979 | | test_vector_file!("bigint_elem_reduced_tests.txt"), |
980 | | |section, test_case| { |
981 | | assert_eq!(section, ""); |
982 | | |
983 | | struct M {} |
984 | | |
985 | | let m_ = consume_modulus::<M>(test_case, "M"); |
986 | | let m = m_.modulus(cpu_features); |
987 | | let expected_result = consume_elem(test_case, "R", &m); |
988 | | let a = |
989 | | consume_elem_unchecked::<M>(test_case, "A", expected_result.limbs.len() * 2); |
990 | | let other_modulus_len_bits = m_.len_bits(); |
991 | | |
992 | | let actual_result = elem_reduced(m.alloc_zero(), &a, &m, other_modulus_len_bits); |
993 | | let oneRR = One::newRR(m.alloc_zero(), &m); |
994 | | let actual_result = elem_mul(oneRR.as_ref(), actual_result, &m); |
995 | | assert_elem_eq(&actual_result, &expected_result); |
996 | | |
997 | | Ok(()) |
998 | | }, |
999 | | ) |
1000 | | } |
1001 | | |
1002 | | #[test] |
1003 | | fn test_elem_reduced_once() { |
1004 | | let cpu_features = cpu::features(); |
1005 | | test::run( |
1006 | | test_vector_file!("bigint_elem_reduced_once_tests.txt"), |
1007 | | |section, test_case| { |
1008 | | assert_eq!(section, ""); |
1009 | | |
1010 | | struct M {} |
1011 | | struct O {} |
1012 | | let m = consume_modulus::<M>(test_case, "m"); |
1013 | | let m = m.modulus(cpu_features); |
1014 | | let a = consume_elem_unchecked::<O>(test_case, "a", m.limbs().len()); |
1015 | | let expected_result = consume_elem::<M>(test_case, "r", &m); |
1016 | | let other_modulus_len_bits = m.len_bits(); |
1017 | | |
1018 | | let actual_result = |
1019 | | elem_reduced_once(m.alloc_zero(), &a, &m, other_modulus_len_bits); |
1020 | | assert_elem_eq(&actual_result, &expected_result); |
1021 | | |
1022 | | Ok(()) |
1023 | | }, |
1024 | | ) |
1025 | | } |
1026 | | |
1027 | | fn consume_elem<M>( |
1028 | | test_case: &mut test::TestCase, |
1029 | | name: &str, |
1030 | | m: &Modulus<M>, |
1031 | | ) -> Elem<M, Unencoded> { |
1032 | | let value = test_case.consume_bytes(name); |
1033 | | Elem::from_be_bytes_padded(untrusted::Input::from(&value), m).unwrap() |
1034 | | } |
1035 | | |
1036 | | fn consume_elem_unchecked<M>( |
1037 | | test_case: &mut test::TestCase, |
1038 | | name: &str, |
1039 | | num_limbs: usize, |
1040 | | ) -> Elem<M, Unencoded> { |
1041 | | let bytes = test_case.consume_bytes(name); |
1042 | | let mut limbs = BoxedLimbs::zero(num_limbs); |
1043 | | limb::parse_big_endian_and_pad_consttime(untrusted::Input::from(&bytes), &mut limbs) |
1044 | | .unwrap(); |
1045 | | Elem { |
1046 | | limbs, |
1047 | | encoding: PhantomData, |
1048 | | } |
1049 | | } |
1050 | | |
1051 | | fn consume_modulus<M>(test_case: &mut test::TestCase, name: &str) -> OwnedModulus<M> { |
1052 | | let value = test_case.consume_bytes(name); |
1053 | | OwnedModulus::from( |
1054 | | OwnedModulusValue::from_be_bytes(untrusted::Input::from(&value)).unwrap(), |
1055 | | ) |
1056 | | } |
1057 | | |
1058 | | fn assert_elem_eq<M, E>(a: &Elem<M, E>, b: &Elem<M, E>) { |
1059 | | if elem_verify_equal_consttime(a, b).is_err() { |
1060 | | panic!("{:x?} != {:x?}", &*a.limbs, &*b.limbs); |
1061 | | } |
1062 | | } |
1063 | | |
1064 | | fn into_encoded<M>(out: Storage<M>, a: Elem<M, Unencoded>, m: &Modulus<M>) -> Elem<M, R> { |
1065 | | let oneRR = One::newRR(out, m); |
1066 | | elem_mul(oneRR.as_ref(), a, m) |
1067 | | } |
1068 | | } |