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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use chacha20poly1305::{
aead,
aead::{Aead, NewAead},
};
use generic_array::GenericArray;
use secstr::{SecStr, SecUtf8};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use crate::pinentry::Pinentry;
pub type KdfParams = scrypt::Params;
lazy_static! {
pub static ref KDF_PARAMS_PROD: KdfParams = scrypt::Params::new(15, 8, 1).unwrap();
pub static ref KDF_PARAMS_TEST: KdfParams = scrypt::Params::new(4, 8, 1).unwrap();
}
type Nonce = GenericArray<u8, <chacha20poly1305::ChaCha20Poly1305 as aead::AeadCore>::NonceSize>;
const SALT_SIZE: usize = 24;
type Salt = [u8; SALT_SIZE];
pub trait Crypto: Sized {
type SecretBox;
type Error;
fn seal<K: AsRef<[u8]>>(&self, secret: K) -> Result<Self::SecretBox, Self::Error>;
fn unseal(&self, secret_box: Self::SecretBox) -> Result<SecStr, Self::Error>;
}
#[derive(Clone, Serialize, Deserialize)]
pub struct SecretBox {
nonce: Nonce,
salt: Salt,
sealed: Vec<u8>,
}
#[derive(Debug, Error)]
pub enum SecretBoxError<PinentryError: std::error::Error + 'static> {
#[error("Unable to decrypt secret box using the derived key")]
InvalidKey,
#[error("Error returned from underlying crypto")]
CryptoError,
#[error("Error getting passphrase")]
Pinentry(#[from] PinentryError),
}
#[derive(Clone)]
pub struct Pwhash<P> {
pinentry: P,
params: KdfParams,
}
impl<P> Pwhash<P> {
pub fn new(pinentry: P, params: KdfParams) -> Self {
Self { pinentry, params }
}
}
impl<P> Crypto for Pwhash<P>
where
P: Pinentry,
P::Error: std::error::Error + 'static,
{
type SecretBox = SecretBox;
type Error = SecretBoxError<P::Error>;
fn seal<K: AsRef<[u8]>>(&self, secret: K) -> Result<Self::SecretBox, Self::Error> {
use rand::RngCore;
let passphrase = self
.pinentry
.get_passphrase()
.map_err(SecretBoxError::Pinentry)?;
let mut rng = rand::thread_rng();
let mut nonce = [0; 12];
rng.fill_bytes(&mut nonce);
let mut salt: Salt = [0; SALT_SIZE];
rng.fill_bytes(&mut salt);
let nonce = *Nonce::from_slice(&nonce[..]);
let derived = derive_key(&salt, &passphrase, &self.params);
let key = chacha20poly1305::Key::from_slice(&derived[..]);
let cipher = chacha20poly1305::ChaCha20Poly1305::new(key);
let sealed = cipher
.encrypt(&nonce, secret.as_ref())
.map_err(|_| Self::Error::CryptoError)?;
Ok(SecretBox {
nonce,
salt,
sealed,
})
}
fn unseal(&self, secret_box: Self::SecretBox) -> Result<SecStr, Self::Error> {
let passphrase = self
.pinentry
.get_passphrase()
.map_err(SecretBoxError::Pinentry)?;
let derived = derive_key(&secret_box.salt, &passphrase, &self.params);
let key = chacha20poly1305::Key::from_slice(&derived[..]);
let cipher = chacha20poly1305::ChaCha20Poly1305::new(key);
cipher
.decrypt(&secret_box.nonce, secret_box.sealed.as_slice())
.map_err(|_| SecretBoxError::InvalidKey)
.map(SecStr::new)
}
}
fn derive_key(salt: &Salt, passphrase: &SecUtf8, params: &KdfParams) -> [u8; 32] {
let mut key = [0u8; 32];
scrypt::scrypt(passphrase.unsecure().as_bytes(), salt, params, &mut key)
.expect("Output length must not be zero");
key
}