1 // Copyright 2015-2016 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 AUTHORS DISCLAIM ALL WARRANTIES
8 // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS 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 //! Constant-time operations.
16 
17 use crate::{c, error};
18 
19 /// Returns `Ok(())` if `a == b` and `Err(error::Unspecified)` otherwise.
20 /// The comparison of `a` and `b` is done in constant time with respect to the
21 /// contents of each, but NOT in constant time with respect to the lengths of
22 /// `a` and `b`.
verify_slices_are_equal(a: &[u8], b: &[u8]) -> Result<(), error::Unspecified>23 pub fn verify_slices_are_equal(a: &[u8], b: &[u8]) -> Result<(), error::Unspecified> {
24     if a.len() != b.len() {
25         return Err(error::Unspecified);
26     }
27     let result = unsafe { OPENSSL_memcmp(a.as_ptr(), b.as_ptr(), a.len()) };
28     match result {
29         0 => Ok(()),
30         _ => Err(error::Unspecified),
31     }
32 }
33 
34 prefixed_extern! {
35     fn OPENSSL_memcmp(a: *const u8, b: *const u8, len: c::size_t) -> c::int;
36 }
37 
38 #[cfg(test)]
39 mod tests {
40     use crate::limb::LimbMask;
41     use crate::{bssl, error, rand};
42 
43     #[test]
test_constant_time() -> Result<(), error::Unspecified>44     fn test_constant_time() -> Result<(), error::Unspecified> {
45         prefixed_extern! {
46             fn bssl_constant_time_test_main() -> bssl::Result;
47         }
48         Result::from(unsafe { bssl_constant_time_test_main() })
49     }
50 
51     #[test]
constant_time_conditional_memcpy() -> Result<(), error::Unspecified>52     fn constant_time_conditional_memcpy() -> Result<(), error::Unspecified> {
53         let rng = rand::SystemRandom::new();
54         for _ in 0..100 {
55             let mut out = rand::generate::<[u8; 256]>(&rng)?.expose();
56             let input = rand::generate::<[u8; 256]>(&rng)?.expose();
57 
58             // Mask to 16 bits to make zero more likely than it would otherwise be.
59             let b = (rand::generate::<[u8; 1]>(&rng)?.expose()[0] & 0x0f) == 0;
60 
61             let ref_in = input;
62             let ref_out = if b { input } else { out };
63 
64             prefixed_extern! {
65                 fn bssl_constant_time_test_conditional_memcpy(dst: &mut [u8; 256], src: &[u8; 256], b: LimbMask);
66             }
67             unsafe {
68                 bssl_constant_time_test_conditional_memcpy(
69                     &mut out,
70                     &input,
71                     if b { LimbMask::True } else { LimbMask::False },
72                 )
73             }
74             assert_eq!(ref_in, input);
75             assert_eq!(ref_out, out);
76         }
77 
78         Ok(())
79     }
80 
81     #[test]
constant_time_conditional_memxor() -> Result<(), error::Unspecified>82     fn constant_time_conditional_memxor() -> Result<(), error::Unspecified> {
83         let rng = rand::SystemRandom::new();
84         for _ in 0..256 {
85             let mut out = rand::generate::<[u8; 256]>(&rng)?.expose();
86             let input = rand::generate::<[u8; 256]>(&rng)?.expose();
87 
88             // Mask to 16 bits to make zero more likely than it would otherwise be.
89             let b = (rand::generate::<[u8; 1]>(&rng)?.expose()[0] & 0x0f) != 0;
90 
91             let ref_in = input;
92             let mut ref_out = out;
93             if b {
94                 ref_out
95                     .iter_mut()
96                     .zip(ref_in.iter())
97                     .for_each(|(out, input)| {
98                         *out ^= input;
99                     });
100             }
101 
102             prefixed_extern! {
103                 fn bssl_constant_time_test_conditional_memxor(dst: &mut [u8; 256], src: &[u8; 256], b: LimbMask);
104             }
105             unsafe {
106                 bssl_constant_time_test_conditional_memxor(
107                     &mut out,
108                     &input,
109                     if b { LimbMask::True } else { LimbMask::False },
110                 );
111             }
112 
113             assert_eq!(ref_in, input);
114             assert_eq!(ref_out, out);
115         }
116 
117         Ok(())
118     }
119 }
120