vitaminc_protected/
conversions.rs

1use crate::{Controlled, Protected};
2use zeroize::Zeroize;
3// TODO: Feature flag?
4use digest::generic_array::{ArrayLength, GenericArray};
5
6impl<T: Zeroize> From<T> for Protected<T> {
7    fn from(x: T) -> Self {
8        Self::init_from_inner(x)
9    }
10}
11
12impl<const N: usize> From<[char; N]> for Protected<String> {
13    fn from(x: [char; N]) -> Self {
14        Self::new(x.iter().collect())
15    }
16}
17
18impl<const N: usize, U> From<GenericArray<u8, U>> for Protected<[u8; N]>
19where
20    U: ArrayLength<u8>,
21    [u8; N]: From<GenericArray<u8, U>>,
22{
23    fn from(x: GenericArray<u8, U>) -> Self {
24        Self::init_from_inner(x.into())
25    }
26}
27
28#[cfg(test)]
29mod tests {
30    use super::*;
31    use crate::Controlled;
32    use digest::consts::U48;
33    use std::num::NonZeroU8;
34
35    macro_rules! test_integer_into_protected {
36        ($($t:ty)*) => ($(
37            // TODO: Use proptests or kani proof
38            let x: $t = 0;
39            let y: Protected<_> = x.into();
40            assert_eq!(y.risky_unwrap(), x);
41        )*)
42    }
43
44    macro_rules! test_array_into_protected {
45        ($t:ty; $($size:expr),*) => ($(
46            // TODO: Use proptests or kani proof
47            let x: [$t; $size] = [0; $size];
48            let y: Protected<_> = x.into();
49            assert_eq!(y.risky_unwrap(), x);
50        )*);
51    }
52
53    #[test]
54    fn test_into_protected() {
55        test_integer_into_protected!(u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize);
56        test_array_into_protected!(u8; 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64);
57        test_array_into_protected!(u16; 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64);
58        test_array_into_protected!(u32; 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64);
59        test_array_into_protected!(u64; 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64);
60        test_array_into_protected!(u128; 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64);
61        test_array_into_protected!(usize; 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64);
62        test_array_into_protected!(i8; 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64);
63        test_array_into_protected!(i16; 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64);
64        test_array_into_protected!(i32; 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64);
65        test_array_into_protected!(i64; 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64);
66        test_array_into_protected!(i128; 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64);
67        test_array_into_protected!(isize; 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64);
68    }
69
70    #[test]
71    fn test_non_zero_into_protected() {
72        let x: NonZeroU8 = NonZeroU8::new(1).unwrap();
73        let y: Protected<_> = x.into();
74        assert_eq!(y.risky_unwrap(), x);
75    }
76
77    #[test]
78    fn test_string_into_protected() {
79        let x: String = "hello".into();
80        let y: Protected<_> = x.into();
81        assert_eq!(&y.risky_unwrap(), "hello");
82    }
83
84    #[test]
85    fn test_char_array_into_string() {
86        let x: [char; 10] = ['c', 'o', 'o', 'k', 'i', 'e', 's', '!', '!', '!'];
87        let y: Protected<String> = x.into();
88        assert_eq!(y.risky_unwrap(), "cookies!!!");
89    }
90
91    #[test]
92    fn test_from_generic_array() {
93        let x: GenericArray<u8, digest::generic_array::typenum::U3> =
94            digest::generic_array::arr![u8; 1, 2, 3];
95        let y: Protected<[u8; 3]> = x.into();
96        assert_eq!(y.risky_unwrap(), [1, 2, 3]);
97    }
98
99    #[test]
100    fn test_from_generic_array_48() {
101        let x: GenericArray<u8, U48> = digest::generic_array::arr![u8; 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48];
102        let y: Protected<[u8; 48]> = x.into();
103        assert_eq!(
104            y.risky_unwrap(),
105            [
106                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
107                24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
108                45, 46, 47, 48
109            ]
110        );
111    }
112}