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.
1415//! Constant-time operations.
1617use crate::{c, error};
1819/// 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`.
23pub fn verify_slices_are_equal(a: &[u8], b: &[u8]) -> Result<(), error::Unspecified> {
24if a.len() != b.len() {
25return Err(error::Unspecified);
26 }
27let result = unsafe { CRYPTO_memcmp(a.as_ptr(), b.as_ptr(), a.len()) };
28match result {
290 => Ok(()),
30_ => Err(error::Unspecified),
31 }
32}
3334prefixed_extern! {
35fn CRYPTO_memcmp(a: *const u8, b: *const u8, len: c::size_t) -> c::int;
36}
3738#[cfg(test)]
39mod tests {
40use crate::limb::LimbMask;
41use crate::{bssl, error, rand};
4243#[test]
44fn test_constant_time() -> Result<(), error::Unspecified> {
45prefixed_extern! {
46fn bssl_constant_time_test_main() -> bssl::Result;
47 }
48 Result::from(unsafe { bssl_constant_time_test_main() })
49 }
5051#[test]
52fn constant_time_conditional_memcpy() -> Result<(), error::Unspecified> {
53let rng = rand::SystemRandom::new();
54for _ in 0..100 {
55let mut out = rand::generate::<[u8; 256]>(&rng)?.expose();
56let input = rand::generate::<[u8; 256]>(&rng)?.expose();
5758// Mask to 16 bits to make zero more likely than it would otherwise be.
59let b = (rand::generate::<[u8; 1]>(&rng)?.expose()[0] & 0x0f) == 0;
6061let ref_in = input;
62let ref_out = if b { input } else { out };
6364prefixed_extern! {
65fn bssl_constant_time_test_conditional_memcpy(dst: &mut [u8; 256], src: &[u8; 256], b: LimbMask);
66 }
67unsafe {
68 bssl_constant_time_test_conditional_memcpy(
69&mut out,
70&input,
71if b { LimbMask::True } else { LimbMask::False },
72 )
73 }
74assert_eq!(ref_in, input);
75assert_eq!(ref_out, out);
76 }
7778Ok(())
79 }
8081#[test]
82fn constant_time_conditional_memxor() -> Result<(), error::Unspecified> {
83let rng = rand::SystemRandom::new();
84for _ in 0..256 {
85let mut out = rand::generate::<[u8; 256]>(&rng)?.expose();
86let input = rand::generate::<[u8; 256]>(&rng)?.expose();
8788// Mask to 16 bits to make zero more likely than it would otherwise be.
89let b = (rand::generate::<[u8; 1]>(&rng)?.expose()[0] & 0x0f) != 0;
9091let ref_in = input;
92let mut ref_out = out;
93if b {
94 ref_out
95 .iter_mut()
96 .zip(ref_in.iter())
97 .for_each(|(out, input)| {
98*out ^= input;
99 });
100 }
101102prefixed_extern! {
103fn bssl_constant_time_test_conditional_memxor(dst: &mut [u8; 256], src: &[u8; 256], b: LimbMask);
104 }
105unsafe {
106 bssl_constant_time_test_conditional_memxor(
107&mut out,
108&input,
109if b { LimbMask::True } else { LimbMask::False },
110 );
111 }
112113assert_eq!(ref_in, input);
114assert_eq!(ref_out, out);
115 }
116117Ok(())
118 }
119}