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
use crate::errors::DecryptError;
use std::io::{Read, Write};
use crate::{chapoly_decrypt_noise, noise_decrypt, scrypt, PrivateKey, PublicKey};
use crate::{AsymFileFormat, PassFileFormat};
use crate::{CHUNK_SIZE, SCRYPT_N, SCRYPT_P, SCRYPT_R};
const TAG_SIZE: usize = 16;
pub fn key_decrypt<T: Read, U: Write>(
ciphertext: &mut T,
plaintext: &mut U,
recipient: &PrivateKey,
file_format: AsymFileFormat,
) -> Result<PublicKey, DecryptError> {
let _file_format = file_format;
let mut prologue = [0u8; 4];
ciphertext.read_exact(&mut prologue)?;
let mut handshake_message = [0u8; 128];
ciphertext.read_exact(&mut handshake_message)?;
let (payload_key, sender_public) = noise_decrypt(recipient, &prologue, &handshake_message)?;
decrypt_chunks(ciphertext, plaintext, payload_key, None, CHUNK_SIZE)?;
plaintext.flush()?;
Ok(sender_public)
}
pub fn pass_decrypt<T: Read, U: Write>(
ciphertext: &mut T,
plaintext: &mut U,
password: &[u8],
file_format: PassFileFormat,
) -> Result<(), DecryptError> {
let _file_format = file_format;
let mut pass_magic_num = [0u8; 4];
ciphertext.read_exact(&mut pass_magic_num)?;
let mut salt = [0u8; 32];
ciphertext.read_exact(&mut salt)?;
let key = scrypt(password, &salt, SCRYPT_N, SCRYPT_R, SCRYPT_P, 32);
let key: [u8; 32] = key.as_slice().try_into().unwrap();
let aad = Some(&pass_magic_num[..]);
decrypt_chunks(ciphertext, plaintext, key, aad, CHUNK_SIZE)?;
plaintext.flush()?;
Ok(())
}
pub fn decrypt_chunks<T: Read, U: Write>(
ciphertext: &mut T,
plaintext: &mut U,
key: [u8; 32],
aad: Option<&[u8]>,
chunk_size: u32,
) -> Result<(), DecryptError> {
let mut chunk_number: u64 = 0;
let mut done = false;
let cs: usize = chunk_size.try_into().unwrap();
let mut buffer = vec![0; cs + TAG_SIZE];
let mut auth_data = match aad {
Some(aad) => vec![0; aad.len() + 8],
None => vec![0; 8],
};
loop {
let mut chunk_header = [0u8; 16];
ciphertext.read_exact(&mut chunk_header)?;
let last_chunk_indicator_bytes: [u8; 4] = chunk_header[8..12].try_into().unwrap();
let ciphertext_length_bytes: [u8; 4] = chunk_header[12..].try_into().unwrap();
let last_chunk_indicator = u32::from_be_bytes(last_chunk_indicator_bytes);
let ciphertext_length = u32::from_be_bytes(ciphertext_length_bytes);
if ciphertext_length > chunk_size {
return Err(DecryptError::ChunkLen);
}
let ct_len: usize = ciphertext_length.try_into().unwrap();
ciphertext.read_exact(&mut buffer[..ct_len + TAG_SIZE])?;
match aad {
Some(aad) => {
let aad_len = aad.len();
auth_data[..aad_len].copy_from_slice(aad);
auth_data[aad_len..aad_len + 4].copy_from_slice(&last_chunk_indicator_bytes);
auth_data[aad_len + 4..].copy_from_slice(&ciphertext_length_bytes);
}
None => {
auth_data[..4].copy_from_slice(&last_chunk_indicator_bytes);
auth_data[4..].copy_from_slice(&ciphertext_length_bytes);
}
}
let ct = &buffer[..ct_len + TAG_SIZE];
let pt_chunk = chapoly_decrypt_noise(&key, chunk_number, auth_data.as_slice(), ct)?;
if last_chunk_indicator == 1 {
done = true;
let check = ciphertext.read(&mut [0u8; 1])?;
if check != 0 {
return Err(DecryptError::UnexpectedData);
}
}
plaintext.write_all(pt_chunk.as_slice())?;
if done {
break;
}
chunk_number += 1;
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::CHUNK_SIZE;
use super::{key_decrypt, pass_decrypt};
use super::{PrivateKey, PublicKey};
use crate::encrypt::{key_encrypt, pass_encrypt};
use crate::sha256;
use crate::{AsymFileFormat, PassFileFormat};
use std::io::Read;
#[allow(dead_code)]
struct KeyData {
alice_private: PrivateKey,
alice_public: PublicKey,
bob_private: PrivateKey,
bob_public: PublicKey,
}
#[test]
fn test_decrypt_small() {
let expected_plaintext = b"Hello, world!";
let key_data = get_key_data();
let expected_sender = key_data.alice_public;
let recipient = key_data.bob_private;
let ciphertext = encrypt_small();
let mut plaintext = Vec::new();
let sender_public = key_decrypt(
&mut ciphertext.as_slice(),
&mut plaintext,
&recipient,
AsymFileFormat::V1,
)
.unwrap();
assert_eq!(&expected_plaintext[..], plaintext.as_slice());
assert_eq!(expected_sender.as_bytes(), sender_public.as_bytes());
}
fn encrypt_small() -> Vec<u8> {
let ephemeral_private =
hex::decode("fdbc28d8f4c2a97013e460836cece7a4bdf59df0cb4b3a185146d13615884f38")
.unwrap();
let payload_key =
hex::decode("a9f9ddef54d0432ec067b75aef26c3db5419ade3b016339743ca1812d89188b2")
.unwrap();
let key_data = get_key_data();
let sender = PrivateKey::from(key_data.alice_private.as_bytes());
let recipient = PublicKey::from(key_data.bob_public.as_bytes());
let ephemeral = PrivateKey::from(ephemeral_private.as_slice());
let payload_key: [u8; 32] = payload_key.as_slice().try_into().unwrap();
let plaintext_data = b"Hello, world!";
let mut plaintext = Vec::new();
plaintext.extend_from_slice(plaintext_data);
let mut ciphertext = Vec::new();
key_encrypt(
&mut plaintext.as_slice(),
&mut ciphertext,
&sender,
&recipient,
Some(&ephemeral),
Some(payload_key),
AsymFileFormat::V1,
)
.unwrap();
ciphertext
}
#[test]
fn test_decrypt_one_chunk() {
let expected_hash =
hex::decode("916b144867c340614f515c7b0e5415c74832d899c05264ded2a277a6e81d81ff")
.unwrap();
let key_data = get_key_data();
let expected_sender = key_data.alice_public;
let recipient = key_data.bob_private;
let ciphertext = encrypt_one_chunk();
let mut plaintext = Vec::new();
let sender_public = key_decrypt(
&mut ciphertext.as_slice(),
&mut plaintext,
&recipient,
AsymFileFormat::V1,
)
.unwrap();
let got_hash = sha256(plaintext.as_slice());
assert_eq!(expected_hash.as_slice(), &got_hash[..]);
assert_eq!(expected_sender.as_bytes(), sender_public.as_bytes());
}
fn encrypt_one_chunk() -> Vec<u8> {
let ephemeral_private =
hex::decode("fdf2b46d965e4bb85d856971d657fdd6dc1fe8993f27587980e4f07f6409927f")
.unwrap();
let ephemeral_private = PrivateKey::from(ephemeral_private.as_slice());
let payload_key =
hex::decode("a300f423e416610a5dd87442f4edc21325f2b3211c4c69f0e0c541cf6cf4eca6")
.unwrap();
let payload_key: [u8; 32] = payload_key.as_slice().try_into().unwrap();
let key_data = get_key_data();
let chunk_size: usize = CHUNK_SIZE.try_into().unwrap();
let mut plaintext = vec![0; chunk_size];
std::io::repeat(0x01).read_exact(&mut plaintext).unwrap();
let mut ciphertext = Vec::new();
key_encrypt(
&mut plaintext.as_slice(),
&mut ciphertext,
&key_data.alice_private,
&key_data.bob_public,
Some(&ephemeral_private),
Some(payload_key),
AsymFileFormat::V1,
)
.unwrap();
ciphertext
}
#[test]
fn test_decrypt_two_chunks() {
let expected_hash =
hex::decode("6cb0ccb39028c57dd7db638d27c88fd1acc1794c8582fefe0949c091a2035ac7")
.unwrap();
let key_data = get_key_data();
let expected_sender = key_data.alice_public;
let recipient = key_data.bob_private;
let ciphertext = encrypt_two_chunks();
let mut plaintext = Vec::new();
let sender_public = key_decrypt(
&mut ciphertext.as_slice(),
&mut plaintext,
&recipient,
AsymFileFormat::V1,
)
.unwrap();
let got_hash = sha256(plaintext.as_slice());
assert_eq!(expected_hash.as_slice(), &got_hash[..]);
assert_eq!(expected_sender.as_bytes(), sender_public.as_bytes());
}
fn encrypt_two_chunks() -> Vec<u8> {
let ephemeral_private =
hex::decode("90ecf9d1dca6ed1e6997585228513a73d4db36bd7dd7c758acb55a6d333bb2fb")
.unwrap();
let ephemeral_private = PrivateKey::from(ephemeral_private.as_slice());
let payload_key =
hex::decode("d3387376438daeb6f7543e815cbde249810e341c1ccab192025b909b9ea4ebe7")
.unwrap();
let payload_key: [u8; 32] = payload_key.as_slice().try_into().unwrap();
let key_data = get_key_data();
let chunk_size: usize = CHUNK_SIZE.try_into().unwrap();
let mut plaintext = vec![0; chunk_size + 1];
std::io::repeat(0x02).read_exact(&mut plaintext).unwrap();
let mut ciphertext = Vec::new();
key_encrypt(
&mut plaintext.as_slice(),
&mut ciphertext,
&key_data.alice_private,
&key_data.bob_public,
Some(&ephemeral_private),
Some(payload_key),
AsymFileFormat::V1,
)
.unwrap();
ciphertext
}
#[test]
fn test_pass_decrypt() {
let expected_pt = b"Be sure to drink your Ovaltine";
let pass = b"hackme";
let ciphertext = pass_encrypt_util();
let mut plaintext = Vec::new();
pass_decrypt(
&mut ciphertext.as_slice(),
&mut plaintext,
pass,
PassFileFormat::V1,
)
.unwrap();
assert_eq!(&expected_pt[..], plaintext.as_slice());
}
fn pass_encrypt_util() -> Vec<u8> {
let salt = hex::decode("b3e94eb6bba5bc462aab92fd86eb9d9f939320a60ae46e690907918ef2ee3aec")
.unwrap();
let salt: [u8; 32] = salt.try_into().unwrap();
let pass = b"hackme";
let plaintext = b"Be sure to drink your Ovaltine";
let mut pt = Vec::new();
pt.extend_from_slice(plaintext);
let mut ciphertext = Vec::new();
pass_encrypt(
&mut pt.as_slice(),
&mut ciphertext,
pass,
salt,
PassFileFormat::V1,
)
.unwrap();
ciphertext
}
fn get_key_data() -> KeyData {
let alice_private =
hex::decode("46acb4ad2a6ffb9d70245798634ad0d5caf7a9738e5f3b60905dee7a7b973bd5")
.unwrap();
let alice_private = PrivateKey::from(alice_private.as_slice());
let alice_public =
hex::decode("3cf3637b4dfdc4596544a936b3983fca09324505f39568d4b8537bc01a92cf6d")
.unwrap();
let alice_public = PublicKey::from(alice_public.as_slice());
let bob_private =
hex::decode("461299525a53333e8597a2b065703ec751356f8462d2704e630c108037567bd4")
.unwrap();
let bob_private = PrivateKey::from(bob_private.as_slice());
let bob_public =
hex::decode("98459724b39e6b9e90b60d214df2887093e224b163714e07e527a4d37edc2d03")
.unwrap();
let bob_public = PublicKey::from(bob_public.as_slice());
KeyData {
alice_private,
alice_public,
bob_private,
bob_public,
}
}
}