Skip to main content

oo7/crypto/
mod.rs

1//! Cryptographic primitives using either native crates or openssl.
2#[cfg(feature = "native_crypto")]
3mod native;
4#[cfg(all(feature = "native_crypto", not(feature = "unstable")))]
5pub(crate) use native::*;
6#[cfg(all(feature = "native_crypto", feature = "unstable"))]
7#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
8pub use native::*;
9
10mod error;
11pub use error::Error;
12
13#[cfg(feature = "openssl_crypto")]
14mod openssl;
15#[cfg(all(feature = "openssl_crypto", not(feature = "unstable")))]
16pub(crate) use self::openssl::*;
17#[cfg(all(feature = "openssl_crypto", feature = "unstable"))]
18#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
19pub use self::openssl::*;
20
21#[cfg(test)]
22mod test {
23    use super::*;
24    use crate::Key;
25
26    #[test]
27    fn test_encrypt() {
28        let data = b"some data";
29        let expected_encrypted = &[
30            241, 233, 175, 173, 142, 44, 63, 240, 77, 154, 211, 233, 217, 170, 49, 142,
31        ];
32        let aes_key = Key::new(vec![
33            132, 3, 113, 222, 81, 209, 49, 43, 81, 232, 243, 46, 1, 103, 184, 42,
34        ]);
35        let aes_iv = &[
36            78, 82, 67, 158, 214, 102, 48, 109, 84, 107, 94, 54, 225, 29, 186, 246,
37        ];
38
39        let encrypted = encrypt(data, &aes_key, aes_iv).unwrap();
40        assert_eq!(encrypted, expected_encrypted);
41
42        let decrypted = decrypt(&encrypted, &aes_key, aes_iv).unwrap();
43        assert_eq!(decrypted.to_vec(), data);
44    }
45
46    #[test]
47    fn test_legacy_derive_key_and_iv() {
48        let expected_key = &[
49            0x1f, 0x35, 0x38, 0x40, 0xf2, 0x95, 0x73, 0x30, 0xa6, 0xcb, 0x01, 0xf9, 0x53, 0xba,
50            0x22, 0x12,
51        ];
52        let expected_iv = &[
53            0x7f, 0xf5, 0x65, 0xb2, 0x31, 0xa5, 0x77, 0x32, 0xf8, 0xd3, 0xd0, 0xa6, 0x45, 0x1c,
54            0x39, 0x97,
55        ];
56        let salt = &[0x92, 0xf4, 0xc0, 0x34, 0x0f, 0x5f, 0x36, 0xf9];
57        let iteration_count = 1782;
58        let password = b"test";
59        let (key, iv) = legacy_derive_key_and_iv(password, Ok(()), salt, iteration_count).unwrap();
60        assert_eq!(key.as_ref(), &expected_key[..]);
61        assert_eq!(iv, &expected_iv[..]);
62    }
63}