ffsend_api/crypto/
hkdf.rs1extern crate hkdf;
2extern crate sha2;
3
4#[cfg(feature = "crypto-ring")]
5use std::num::NonZeroU32;
6
7use self::hkdf::Hkdf;
8use self::sha2::Sha256;
9#[cfg(feature = "crypto-openssl")]
10use openssl::{hash::MessageDigest, pkcs5::pbkdf2_hmac};
11#[cfg(feature = "crypto-ring")]
12use ring::pbkdf2;
13use url::Url;
14
15const KEY_AUTH_SIZE: usize = 64;
17
18const KEY_AUTH_ITERATIONS: usize = 100;
21
22pub fn hkdf(salt: Option<&[u8]>, length: usize, ikm: &[u8], info: Option<&[u8]>) -> Vec<u8> {
34 let info = info.unwrap_or(&[]);
36 let mut okm = vec![0u8; length];
37
38 Hkdf::<Sha256>::extract(salt, &ikm)
40 .1
41 .expand(&info, &mut okm)
42 .unwrap();
43
44 okm
45}
46
47pub fn derive_file_key(secret: &[u8]) -> Vec<u8> {
49 hkdf(None, 16, secret, Some(b"encryption"))
50}
51
52pub fn derive_meta_key(secret: &[u8]) -> Vec<u8> {
54 hkdf(None, 16, secret, Some(b"metadata"))
55}
56
57pub fn derive_auth_key(secret: &[u8], password: Option<&str>, url: Option<&Url>) -> Vec<u8> {
62 assert_eq!(
64 password.is_none(),
65 url.is_none(),
66 "unable to derive authentication key, missing password or URL",
67 );
68
69 if password.is_none() {
71 return hkdf(None, KEY_AUTH_SIZE, secret, Some(b"authentication"));
72 }
73
74 let mut key = vec![0u8; KEY_AUTH_SIZE];
76
77 #[cfg(feature = "crypto-openssl")]
79 pbkdf2_hmac(
80 password.unwrap().as_bytes(),
81 url.unwrap().as_str().as_bytes(),
82 KEY_AUTH_ITERATIONS,
83 MessageDigest::sha256(),
84 &mut key,
85 )
86 .expect("failed to derive passworded authentication key");
87
88 #[cfg(feature = "crypto-ring")]
89 pbkdf2::derive(
90 pbkdf2::PBKDF2_HMAC_SHA256,
91 NonZeroU32::new(KEY_AUTH_ITERATIONS as u32)
92 .expect("key authentication iteration count cannot be 0"),
93 url.unwrap().as_str().as_bytes(),
94 password.unwrap().as_bytes(),
95 &mut key,
96 );
97
98 key
99}