1
//! Constant-time utilities.
2
use subtle::{Choice, ConstantTimeEq};
3

            
4
/// Convert a boolean into a Choice.
5
///
6
/// This isn't necessarily a good idea or constant-time.
7
12692
pub(crate) fn bool_to_choice(v: bool) -> Choice {
8
12692
    Choice::from(u8::from(v))
9
12692
}
10

            
11
/// Return true if two slices are equal.  Performs its operation in constant
12
/// time, but returns a bool instead of a subtle::Choice.
13
2090
pub(crate) fn bytes_eq(a: &[u8], b: &[u8]) -> bool {
14
2090
    let choice = a.ct_eq(b);
15
2090
    choice.unwrap_u8() == 1
16
2090
}
17

            
18
/// Returns true if all bytes of the input are zero (including if the slice is
19
/// empty). Executes in constant time for a given length of input.
20
4144
pub(crate) fn is_zero(x: &[u8]) -> bool {
21
4144
    // It's tempting to lift the Choice out of the fold loop, s.t. the loop does
22
4144
    // a simple bit-or of each byte, but then the compiler could theoretically
23
4144
    // exit the loop early if all bits become one (i.e. 0xff). (Granted this
24
4144
    // seems unlikely in practice)
25
4144
    x.iter()
26
10356
        .map(|b| bool_to_choice(*b == 0))
27
4144
        .fold(bool_to_choice(true), std::ops::BitAnd::bitand)
28
4144
        .unwrap_u8()
29
4144
        == 1
30
4144
}
31

            
32
#[cfg(test)]
33
mod test {
34
    // @@ begin test lint list maintained by maint/add_warning @@
35
    #![allow(clippy::bool_assert_comparison)]
36
    #![allow(clippy::clone_on_copy)]
37
    #![allow(clippy::dbg_macro)]
38
    #![allow(clippy::mixed_attributes_style)]
39
    #![allow(clippy::print_stderr)]
40
    #![allow(clippy::print_stdout)]
41
    #![allow(clippy::single_char_pattern)]
42
    #![allow(clippy::unwrap_used)]
43
    #![allow(clippy::unchecked_duration_subtraction)]
44
    #![allow(clippy::useless_vec)]
45
    #![allow(clippy::needless_pass_by_value)]
46
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
47
    #[test]
48
    fn test_bytes_eq() {
49
        use super::bytes_eq;
50
        assert!(bytes_eq(&b"123"[..], &b"1234"[..3]));
51
        assert!(!bytes_eq(&b"123"[..], &b"1234"[..]));
52
        assert!(bytes_eq(&b"45"[..], &b"45"[..]));
53
        assert!(!bytes_eq(&b"hi"[..], &b"45"[..]));
54
        assert!(bytes_eq(&b""[..], &b""[..]));
55
    }
56

            
57
    #[test]
58
    fn test_is_zero() {
59
        use super::is_zero;
60
        assert!(is_zero(&[]));
61
        assert!(is_zero(&[0]));
62
        assert!(is_zero(&[0, 0]));
63
        assert!(!is_zero(&[1, 0]));
64
        assert!(!is_zero(&[0, 1]));
65
        assert!(!is_zero(&[0, 1, 0]));
66
    }
67
}