-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathencryption.rs
More file actions
397 lines (335 loc) · 11.1 KB
/
encryption.rs
File metadata and controls
397 lines (335 loc) · 11.1 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
use crate::{
e,
file::update_file,
globals::{g_get_derivation, g_get_password, g_set_derivation, g_set_password},
input::{ask_string, get_input},
notes::get_notes,
other::exit,
p, s,
};
use argon2::{Algorithm, Argon2, Params, Version};
use chacha20poly1305::{
aead::{Aead, Payload},
ChaCha20Poly1305, KeyInit, XChaCha20Poly1305,
};
use rand::{rngs::OsRng, RngCore};
// Lets the user change password or derivation
pub fn change_security() {
p!("(1) Change Password");
p!("(2) Change Key Derivation");
let ans = ask_string("Choice", "", true);
let mut save = true;
match &ans[..] {
"1" => change_password(),
"2" => change_key_derivation(),
_ => save = false,
};
if save {
update_file(get_notes(false))
}
}
// Change key derivation method
pub fn change_key_derivation() {
let og_deriv = g_get_derivation();
p!("This will change the file's key derivation method");
get_key_derivation();
if og_deriv == 0 && g_get_derivation() != 0 {
change_password();
}
}
// Lets the user decide between fast or secure derivation
pub fn get_key_derivation() {
loop {
p!("Key Derivation:");
p!("1) Interactive (Faster)");
p!("2) Sensitive (More Secure)");
p!("0) Plain (No Encryption)");
let derivation = ask_string("Choice", "", true);
if derivation == "1" || derivation == "2" || derivation == "0" {
g_set_derivation(derivation.parse::<usize>().unwrap());
break;
}
}
}
// Changes the password and updates the file with it
pub fn change_password() {
p!("This will change the file's password.");
if !get_password(true).is_empty() {
update_file(get_notes(false))
}
}
// Gets the file's password saved globally
// Or asks the user for the file's password
// Or changes the file's password
pub fn get_password(change: bool) -> String {
if g_get_derivation() == 0 {
return s!("none");
}
let mut pw = g_get_password();
if pw.is_empty() || change {
let mut password: String;
if change {
loop {
password = get_input("New Password", "", true);
if password.is_empty() {
return s!();
}
let confirmation = get_input("Confirm Password", "", true);
if password != confirmation {
e!("Error: Passwords Don't Match.")
} else {
break;
}
}
} else {
password = get_input("Password", "", true);
}
pw = s!(password);
g_set_password(password);
}
pw
}
// Get a key from the password for V4
fn key_from_pw(password: &str, salt: &[u8], derivation: u8) -> Result<[u8; 32], ()> {
let mut key = [0u8; 32];
match derivation {
0 => {
return Ok(key);
}
1 => {
let params = Params::new(65536, 2, 1, Some(32)).map_err(|_| ())?;
let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
argon2
.hash_password_into(password.as_bytes(), salt, &mut key)
.map_err(|_| ())?;
}
2 => {
let params = Params::new(1048576, 4, 1, Some(32)).map_err(|_| ())?;
let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
argon2
.hash_password_into(password.as_bytes(), salt, &mut key)
.map_err(|_| ())?;
}
_ => {
e!("Wrong key derivation.");
exit()
}
};
Ok(key)
}
// Fallback logic for V2 and V3 using sodiumoxide
fn legacy_decrypt(
version: u8,
derivation: u8,
salt_bytes: &[u8],
nonce_bytes: &[u8],
ciphertext: &[u8],
password: &str,
) -> Result<Vec<u8>, ()> {
use sodiumoxide::crypto::aead;
use sodiumoxide::crypto::aead::chacha20poly1305_ietf as aead_v2;
use sodiumoxide::crypto::pwhash;
let salt = pwhash::Salt::from_slice(salt_bytes).ok_or(())?;
let mut key = aead::Key([0; aead::KEYBYTES]);
match derivation {
0 => return Ok(ciphertext.to_vec()),
1 => {
pwhash::derive_key_interactive(&mut key.0, password.as_bytes(), &salt)
.map_err(|_| ())?;
}
2 => {
pwhash::derive_key_sensitive(&mut key.0, password.as_bytes(), &salt)
.map_err(|_| ())?;
}
_ => return Err(()),
}
if version < 3 {
let key_v2 = aead_v2::Key(key.0);
let nonce = aead_v2::Nonce::from_slice(nonce_bytes).ok_or(())?;
aead_v2::open(ciphertext, Some(&salt.0), &nonce, &key_v2).map_err(|_| ())
} else {
let nonce = aead::Nonce::from_slice(nonce_bytes).ok_or(())?;
let mut header_ad = vec![version, derivation];
header_ad.extend_from_slice(&salt.0);
header_ad.extend_from_slice(&nonce.0);
aead::open(ciphertext, Some(header_ad.as_slice()), &nonce, &key).map_err(|_| ())
}
}
// Enum for nonce types (v2 uses 12-byte, v3+ uses 24-byte)
enum NonceType {
V2([u8; 12]),
V3([u8; 24]),
}
impl NonceType {
fn as_bytes(&self) -> &[u8] {
match self {
NonceType::V2(n) => n,
NonceType::V3(n) => n,
}
}
}
// Struct for encrypted data
struct EncryptedData {
version: u8,
derivation: u8,
salt: Vec<u8>,
nonce: NonceType,
ciphertext: Vec<u8>,
}
// Implement the encryption struct
impl EncryptedData {
fn new(plaintext: &str, password: &str) -> Result<Self, ()> {
let version = 4;
let derivation = g_get_derivation() as u8;
let mut salt = [0u8; 16];
OsRng.fill_bytes(&mut salt);
let mut nonce_bytes = [0u8; 24];
OsRng.fill_bytes(&mut nonce_bytes);
let key = key_from_pw(password, &salt, derivation)?;
let ciphertext = if derivation == 0 {
plaintext.as_bytes().to_vec()
} else {
let cipher = XChaCha20Poly1305::new(&key.into());
let xnonce = chacha20poly1305::XNonce::from(nonce_bytes);
let header_ad = if Self::use_authenticated_header(version) {
Self::associated_data(version, derivation, &salt, &nonce_bytes)
} else {
salt.to_vec()
};
let payload = Payload {
msg: plaintext.as_bytes(),
aad: &header_ad,
};
cipher.encrypt(&xnonce, payload).map_err(|_| ())?
};
Ok(EncryptedData {
version: version,
derivation: derivation,
salt: salt.to_vec(),
nonce: NonceType::V3(nonce_bytes),
ciphertext: ciphertext,
})
}
fn decrypt(&self, password: &str) -> Result<Vec<u8>, ()> {
if self.derivation == 0 {
return Ok(self.ciphertext.clone());
}
if self.version < 4 {
return legacy_decrypt(
self.version,
self.derivation,
&self.salt,
self.nonce.as_bytes(),
&self.ciphertext,
password,
);
}
let key = key_from_pw(password, &self.salt, self.derivation)?;
match &self.nonce {
NonceType::V2(nonce_bytes) => {
let cipher = ChaCha20Poly1305::new(&key.into());
let n = chacha20poly1305::Nonce::from_slice(nonce_bytes);
let payload = Payload {
msg: self.ciphertext.as_slice(),
aad: &self.salt,
};
cipher.decrypt(n, payload).map_err(|_| ())
}
NonceType::V3(nonce_bytes) => {
let cipher = XChaCha20Poly1305::new(&key.into());
let n = chacha20poly1305::XNonce::from_slice(nonce_bytes);
let header_ad = if Self::use_authenticated_header(self.version) {
Self::associated_data(
self.version,
self.derivation,
&self.salt,
nonce_bytes,
)
} else {
self.salt.clone()
};
let payload = Payload {
msg: self.ciphertext.as_slice(),
aad: &header_ad,
};
cipher.decrypt(n, payload).map_err(|_| ())
}
}
}
fn use_authenticated_header(version: u8) -> bool {
version >= 3
}
fn associated_data(version: u8, derivation: u8, salt: &[u8], nonce: &[u8]) -> Vec<u8> {
let mut data = vec![version, derivation];
data.extend_from_slice(salt);
data.extend_from_slice(nonce);
data
}
fn to_bytes(&self) -> Vec<u8> {
let mut bytes: Vec<u8> = vec![self.version, self.derivation];
bytes.extend(self.salt.iter());
bytes.extend(self.nonce.as_bytes().iter());
bytes.extend(self.ciphertext.iter());
bytes
}
fn from_bytes(bytes: &[u8]) -> Result<Self, base64::DecodeError> {
let n = 2;
let version = bytes[0];
let derivation = bytes[1];
let sbytes = if version < 4 {
sodiumoxide::crypto::pwhash::SALTBYTES
} else {
16
};
let (nonce, nbytes) = if version < 3 {
let nbytes = 12;
if bytes.len() < (n + sbytes + nbytes) {
return Err(base64::DecodeError::InvalidLength);
}
let mut nonce_bytes = [0u8; 12];
nonce_bytes.copy_from_slice(&bytes[(n + sbytes)..(n + sbytes + nbytes)]);
(NonceType::V2(nonce_bytes), nbytes)
} else {
let nbytes = 24;
if bytes.len() < (n + sbytes + nbytes) {
return Err(base64::DecodeError::InvalidLength);
}
let mut nonce_bytes = [0u8; 24];
nonce_bytes.copy_from_slice(&bytes[(n + sbytes)..(n + sbytes + nbytes)]);
(NonceType::V3(nonce_bytes), nbytes)
};
let ciphertext: Vec<u8> = bytes[(n + sbytes + nbytes)..]
.iter()
.map(|x| x.to_owned())
.collect();
let mut salt = vec![0u8; sbytes];
salt.copy_from_slice(&bytes[n..(n + sbytes)]);
Ok(EncryptedData {
version: version,
derivation: derivation,
salt: salt,
nonce: nonce,
ciphertext: ciphertext,
})
}
}
// Encrypt the notes text
pub fn encrypt_text(plaintext: &str) -> Vec<u8> {
let password = get_password(false);
let ciphertext = EncryptedData::new(plaintext, &password).unwrap();
ciphertext.to_bytes()
}
// Decrypt the file's bytes
pub fn decrypt_bytes(bytes: &[u8]) -> String {
let ciphertext = EncryptedData::from_bytes(bytes).unwrap();
g_set_derivation(ciphertext.derivation as usize);
let password = get_password(false);
let plaintext = match ciphertext.decrypt(&password) {
Ok(text) => text,
Err(_) => {
e!("Can't decrypt the file.");
exit()
}
};
String::from_utf8(plaintext).unwrap()
}