-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdecrypt.rs
668 lines (612 loc) · 20.5 KB
/
decrypt.rs
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
use std::{num::NonZeroU32, sync::Arc};
use anyhow::Result;
use bytes::Bytes;
use crossbeam_channel::{unbounded, Receiver};
use rayon::prelude::*;
use zstd::stream::{copy_encode, decode_all, encode_all};
pub use zstd::compression_level_range;
/// The maximum compression level allowed by zstd
#[must_use]
pub fn max_compression_level() -> i32 {
*compression_level_range().end()
}
use crate::{
backend::{FileType, ReadBackend, WriteBackend},
crypto::{hasher::hash, CryptoKey},
error::{CryptBackendErrorKind, RusticErrorKind},
id::Id,
repofile::RepoFile,
Progress, RusticResult,
};
/// A backend that can decrypt data.
/// This is a trait that is implemented by all backends that can decrypt data.
/// It is implemented for all backends that implement `DecryptWriteBackend` and `DecryptReadBackend`.
/// This trait is used by the `Repository` to decrypt data.
pub trait DecryptFullBackend: DecryptWriteBackend + DecryptReadBackend {}
impl<T: DecryptWriteBackend + DecryptReadBackend> DecryptFullBackend for T {}
pub trait DecryptReadBackend: ReadBackend + Clone + 'static {
/// Decrypts the given data.
///
/// # Arguments
///
/// * `data` - The data to decrypt.
///
/// # Errors
///
/// If the data could not be decrypted.
fn decrypt(&self, data: &[u8]) -> RusticResult<Vec<u8>>;
/// Reads the given file.
///
/// # Arguments
///
/// * `tpe` - The type of the file.
/// * `id` - The id of the file.
///
/// # Errors
///
/// If the file could not be read.
fn read_encrypted_full(&self, tpe: FileType, id: &Id) -> RusticResult<Bytes>;
/// Reads the given file from partial data.
///
/// # Arguments
///
/// * `data` - The partial data to decrypt.
/// * `uncompressed_length` - The length of the uncompressed data.
///
/// # Errors
///
/// * [`CryptBackendErrorKind::DecodingZstdCompressedDataFailed`] - If the data could not be decoded.
/// * [`CryptBackendErrorKind::LengthOfUncompressedDataDoesNotMatch`] - If the length of the uncompressed data does not match the given length.
///
/// [`CryptBackendErrorKind::DecodingZstdCompressedDataFailed`]: crate::error::CryptBackendErrorKind::DecodingZstdCompressedDataFailed
/// [`CryptBackendErrorKind::LengthOfUncompressedDataDoesNotMatch`]: crate::error::CryptBackendErrorKind::LengthOfUncompressedDataDoesNotMatch
fn read_encrypted_from_partial(
&self,
data: &[u8],
uncompressed_length: Option<NonZeroU32>,
) -> RusticResult<Bytes> {
let mut data = self.decrypt(data)?;
if let Some(length) = uncompressed_length {
data = decode_all(&*data)
.map_err(CryptBackendErrorKind::DecodingZstdCompressedDataFailed)?;
if data.len() != length.get() as usize {
return Err(CryptBackendErrorKind::LengthOfUncompressedDataDoesNotMatch.into());
}
}
Ok(data.into())
}
/// Reads the given file with the given offset and length.
///
/// # Arguments
///
/// * `tpe` - The type of the file.
/// * `id` - The id of the file.
/// * `cacheable` - Whether the file should be cached.
/// * `offset` - The offset to read from.
/// * `length` - The length to read.
/// * `uncompressed_length` - The length of the uncompressed data.
///
/// # Errors
///
/// If the file could not be read.
fn read_encrypted_partial(
&self,
tpe: FileType,
id: &Id,
cacheable: bool,
offset: u32,
length: u32,
uncompressed_length: Option<NonZeroU32>,
) -> RusticResult<Bytes> {
self.read_encrypted_from_partial(
&self
.read_partial(tpe, id, cacheable, offset, length)
.map_err(RusticErrorKind::Backend)?,
uncompressed_length,
)
}
/// Gets the given file.
///
/// # Arguments
///
/// * `id` - The id of the file.
///
/// # Errors
///
/// If the file could not be read.
fn get_file<F: RepoFile>(&self, id: &Id) -> RusticResult<F> {
let data = self.read_encrypted_full(F::TYPE, id)?;
Ok(serde_json::from_slice(&data)
.map_err(CryptBackendErrorKind::DeserializingFromBytesOfJsonTextFailed)?)
}
/// Streams all files.
///
/// Note: The result comes in arbitrary order!
///
/// # Arguments
///
/// * `p` - The progress bar.
///
/// # Errors
///
/// If the files could not be read.
fn stream_all<F: RepoFile>(
&self,
p: &impl Progress,
) -> RusticResult<Receiver<RusticResult<(Id, F)>>> {
let list = self.list(F::TYPE).map_err(RusticErrorKind::Backend)?;
self.stream_list(&list, p)
}
/// Streams a list of files.
///
/// Note: The result comes in arbitrary order!
///
/// # Arguments
///
/// * `list` - The list of files to stream.
/// * `p` - The progress bar.
///
/// # Errors
///
/// If the files could not be read.
fn stream_list<F: RepoFile>(
&self,
list: &[Id],
p: &impl Progress,
) -> RusticResult<Receiver<RusticResult<(Id, F)>>> {
p.set_length(list.len() as u64);
let (tx, rx) = unbounded();
list.into_par_iter()
.for_each_with((self, p, tx), |(be, p, tx), id| {
let file = be.get_file::<F>(id).map(|file| (*id, file));
p.inc(1);
tx.send(file).unwrap();
});
Ok(rx)
}
}
pub trait DecryptWriteBackend: WriteBackend + Clone + 'static {
/// The type of the key.
type Key: CryptoKey;
/// Gets the key.
fn key(&self) -> &Self::Key;
/// Writes the given data to the backend and returns the id of the data.
///
/// # Arguments
///
/// * `tpe` - The type of the file.
/// * `data` - The data to write.
///
/// # Errors
///
/// If the data could not be written.
///
/// # Returns
///
/// The hash of the written data.
fn hash_write_full(&self, tpe: FileType, data: &[u8]) -> RusticResult<Id>;
/// Process some blob data.
/// This compresses and encrypts the data as requested
///
/// # Returns
///
/// The processed data, the original data length and when compression is used, the uncomressed length
fn process_data(&self, data: &[u8]) -> RusticResult<(Vec<u8>, u32, Option<NonZeroU32>)>;
/// Writes the given data to the backend without compression and returns the id of the data.
///
/// # Arguments
///
/// * `tpe` - The type of the file.
/// * `data` - The data to write.
///
/// # Errors
///
/// If the data could not be written.
///
/// # Returns
///
/// The hash of the written data.
fn hash_write_full_uncompressed(&self, tpe: FileType, data: &[u8]) -> RusticResult<Id> {
let data = self.key().encrypt_data(data)?;
let id = hash(&data);
self.write_bytes(tpe, &id, false, data.into())
.map_err(RusticErrorKind::Backend)?;
Ok(id)
}
/// Saves the given file.
///
/// # Arguments
///
/// * `file` - The file to save.
///
/// # Errors
///
/// * [`CryptBackendErrorKind::SerializingToJsonByteVectorFailed`] - If the file could not be serialized to json.
///
/// # Returns
///
/// The id of the file.
///
/// [`CryptBackendErrorKind::SerializingToJsonByteVectorFailed`]: crate::error::CryptBackendErrorKind::SerializingToJsonByteVectorFailed
fn save_file<F: RepoFile>(&self, file: &F) -> RusticResult<Id> {
let data = serde_json::to_vec(file)
.map_err(CryptBackendErrorKind::SerializingToJsonByteVectorFailed)?;
self.hash_write_full(F::TYPE, &data)
}
/// Saves the given file uncompressed.
///
/// # Arguments
///
/// * `file` - The file to save.
///
/// # Errors
///
/// * [`CryptBackendErrorKind::SerializingToJsonByteVectorFailed`] - If the file could not be serialized to json.
///
/// # Returns
///
/// The id of the file.
///
/// [`CryptBackendErrorKind::SerializingToJsonByteVectorFailed`]: crate::error::CryptBackendErrorKind::SerializingToJsonByteVectorFailed
fn save_file_uncompressed<F: RepoFile>(&self, file: &F) -> RusticResult<Id> {
let data = serde_json::to_vec(file)
.map_err(CryptBackendErrorKind::SerializingToJsonByteVectorFailed)?;
self.hash_write_full_uncompressed(F::TYPE, &data)
}
/// Saves the given list of files.
///
/// # Arguments
///
/// * `list` - The list of files to save.
/// * `p` - The progress bar.
///
/// # Errors
///
/// * [`CryptBackendErrorKind::SerializingToJsonByteVectorFailed`] - If the file could not be serialized to json.
fn save_list<'a, F: RepoFile, I: ExactSizeIterator<Item = &'a F> + Send>(
&self,
list: I,
p: impl Progress,
) -> RusticResult<()> {
p.set_length(list.len() as u64);
list.par_bridge().try_for_each(|file| -> RusticResult<_> {
_ = self.save_file(file)?;
p.inc(1);
Ok(())
})?;
p.finish();
Ok(())
}
/// Deletes the given list of files.
///
/// # Arguments
///
/// * `tpe` - The type of the files.
/// * `cacheable` - Whether the files should be cached.
/// * `list` - The list of files to delete.
/// * `p` - The progress bar.
///
/// # Panics
///
/// If the files could not be deleted.
fn delete_list<'a, I: ExactSizeIterator<Item = &'a Id> + Send>(
&self,
tpe: FileType,
cacheable: bool,
list: I,
p: impl Progress,
) -> RusticResult<()> {
p.set_length(list.len() as u64);
list.par_bridge().try_for_each(|id| -> RusticResult<_> {
// TODO: Don't panic on file not being able to be deleted.
self.remove(tpe, id, cacheable).unwrap();
p.inc(1);
Ok(())
})?;
p.finish();
Ok(())
}
/// Sets the compression level to use for zstd.
///
/// # Arguments
///
/// * `zstd` - The compression level to use for zstd. TODO: What happens if this is None? What are defaults?
fn set_zstd(&mut self, zstd: Option<i32>);
fn set_extra_verify(&mut self, extra_check: bool);
}
/// A backend that can decrypt data.
///
/// # Type Parameters
///
/// * `C` - The type of the key to decrypt the backend with.
#[derive(Debug, Clone)]
pub struct DecryptBackend<C: CryptoKey> {
/// The backend to decrypt.
be: Arc<dyn WriteBackend>,
/// The key to decrypt the backend with.
key: C,
/// The compression level to use for zstd.
zstd: Option<i32>,
/// Whether to do an extra verification by decompressing and decrypting the data
extra_verify: bool,
}
impl<C: CryptoKey> DecryptBackend<C> {
/// Creates a new decrypt backend.
///
/// # Type Parameters
///
/// * `C` - The type of the key to decrypt the backend with.
///
/// # Arguments
///
/// * `be` - The backend to decrypt.
/// * `key` - The key to decrypt the backend with.
///
/// # Returns
///
/// The new decrypt backend.
pub fn new(be: Arc<dyn WriteBackend>, key: C) -> Self {
Self {
be,
key,
// zstd and extra_verify are directly set, where needed.
zstd: None,
extra_verify: false,
}
}
/// Decrypt and potentially decompress an already read repository file
fn decrypt_file(&self, data: &[u8]) -> RusticResult<Vec<u8>> {
let decrypted = self.decrypt(data)?;
Ok(match decrypted.first() {
Some(b'{' | b'[') => decrypted, // not compressed
Some(2) => decode_all(&decrypted[1..])
.map_err(CryptBackendErrorKind::DecodingZstdCompressedDataFailed)?, // 2 indicates compressed data following
_ => return Err(CryptBackendErrorKind::DecryptionNotSupportedForBackend)?,
})
}
/// encrypt and potentially compress a repository file
fn encrypt_file(&self, data: &[u8]) -> RusticResult<Vec<u8>> {
let data_encrypted = match self.zstd {
Some(level) => {
let mut out = vec![2_u8];
copy_encode(data, &mut out, level)
.map_err(CryptBackendErrorKind::CopyEncodingDataFailed)?;
self.key().encrypt_data(&out)?
}
None => self.key().encrypt_data(data)?,
};
Ok(data_encrypted)
}
fn very_file(&self, data_encrypted: &[u8], data: &[u8]) -> RusticResult<()> {
if self.extra_verify {
let check_data = self.decrypt_file(data_encrypted)?;
if data != check_data {
return Err(CryptBackendErrorKind::ExtraVerificationFailed.into());
}
}
Ok(())
}
/// encrypt and potentially compress some data
fn encrypt_data(&self, data: &[u8]) -> RusticResult<(Vec<u8>, u32, Option<NonZeroU32>)> {
let data_len: u32 = data
.len()
.try_into()
.map_err(CryptBackendErrorKind::IntConversionFailed)?;
let (data_encrypted, uncompressed_length) = match self.zstd {
None => (self.key.encrypt_data(data)?, None),
// compress if requested
Some(level) => (
self.key.encrypt_data(&encode_all(data, level)?)?,
NonZeroU32::new(data_len),
),
};
Ok((data_encrypted, data_len, uncompressed_length))
}
fn very_data(
&self,
data_encrypted: &[u8],
uncompressed_length: Option<NonZeroU32>,
data: &[u8],
) -> RusticResult<()> {
if self.extra_verify {
let data_check =
self.read_encrypted_from_partial(data_encrypted, uncompressed_length)?;
if data != data_check {
return Err(CryptBackendErrorKind::ExtraVerificationFailed.into());
}
}
Ok(())
}
}
impl<C: CryptoKey> DecryptWriteBackend for DecryptBackend<C> {
/// The type of the key.
type Key = C;
/// Gets the key.
fn key(&self) -> &Self::Key {
&self.key
}
/// Writes the given data to the backend and returns the id of the data.
///
/// # Arguments
///
/// * `tpe` - The type of the file.
/// * `data` - The data to write.
///
/// # Errors
///
/// * [`CryptBackendErrorKind::CopyEncodingDataFailed`] - If the data could not be encoded.
///
/// # Returns
///
/// The id of the data.
///
/// [`CryptBackendErrorKind::CopyEncodingDataFailed`]: crate::error::CryptBackendErrorKind::CopyEncodingDataFailed
fn hash_write_full(&self, tpe: FileType, data: &[u8]) -> RusticResult<Id> {
let data_encrypted = self.encrypt_file(data)?;
self.very_file(&data_encrypted, data)?;
let id = hash(&data_encrypted);
self.write_bytes(tpe, &id, false, data_encrypted.into())
.map_err(RusticErrorKind::Backend)?;
Ok(id)
}
fn process_data(&self, data: &[u8]) -> RusticResult<(Vec<u8>, u32, Option<NonZeroU32>)> {
let (data_encrypted, data_len, uncompressed_length) = self.encrypt_data(data)?;
self.very_data(&data_encrypted, uncompressed_length, data)?;
Ok((data_encrypted, data_len, uncompressed_length))
}
/// Sets the compression level to use for zstd.
///
/// # Arguments
///
/// * `zstd` - The compression level to use for zstd.
fn set_zstd(&mut self, zstd: Option<i32>) {
self.zstd = zstd;
}
/// Sets `extra_check`, i.e. whether to do an extra check after compressing/encrypting
///
/// # Arguments
///
/// * `extra_echeck` - The compression level to use for zstd.
fn set_extra_verify(&mut self, extra_verify: bool) {
self.extra_verify = extra_verify;
}
}
impl<C: CryptoKey> DecryptReadBackend for DecryptBackend<C> {
/// Decrypts the given data.
///
/// # Arguments
///
/// * `data` - The data to decrypt.
///
/// # Returns
///
/// A vector containing the decrypted data.
fn decrypt(&self, data: &[u8]) -> RusticResult<Vec<u8>> {
self.key.decrypt_data(data)
}
/// Reads encrypted data from the backend.
///
/// # Arguments
///
/// * `tpe` - The type of the file.
/// * `id` - The id of the file.
///
/// # Errors
///
/// * [`CryptBackendErrorKind::DecryptionNotSupportedForBackend`] - If the backend does not support decryption.
/// * [`CryptBackendErrorKind::DecodingZstdCompressedDataFailed`] - If the data could not be decoded.
///
/// [`CryptBackendErrorKind::DecryptionNotSupportedForBackend`]: crate::error::CryptBackendErrorKind::DecryptionNotSupportedForBackend
/// [`CryptBackendErrorKind::DecodingZstdCompressedDataFailed`]: crate::error::CryptBackendErrorKind::DecodingZstdCompressedDataFailed
fn read_encrypted_full(&self, tpe: FileType, id: &Id) -> RusticResult<Bytes> {
self.decrypt_file(&self.read_full(tpe, id).map_err(RusticErrorKind::Backend)?)
.map(Into::into)
}
}
impl<C: CryptoKey> ReadBackend for DecryptBackend<C> {
fn location(&self) -> String {
self.be.location()
}
fn list(&self, tpe: FileType) -> Result<Vec<Id>> {
self.be.list(tpe)
}
fn list_with_size(&self, tpe: FileType) -> Result<Vec<(Id, u32)>> {
self.be.list_with_size(tpe)
}
fn read_full(&self, tpe: FileType, id: &Id) -> Result<Bytes> {
self.be.read_full(tpe, id)
}
fn read_partial(
&self,
tpe: FileType,
id: &Id,
cacheable: bool,
offset: u32,
length: u32,
) -> Result<Bytes> {
self.be.read_partial(tpe, id, cacheable, offset, length)
}
}
impl<C: CryptoKey> WriteBackend for DecryptBackend<C> {
fn create(&self) -> Result<()> {
self.be.create()
}
fn write_bytes(&self, tpe: FileType, id: &Id, cacheable: bool, buf: Bytes) -> Result<()> {
self.be.write_bytes(tpe, id, cacheable, buf)
}
fn remove(&self, tpe: FileType, id: &Id, cacheable: bool) -> Result<()> {
self.be.remove(tpe, id, cacheable)
}
}
#[cfg(test)]
mod tests {
use crate::{backend::MockBackend, crypto::aespoly1305::Key};
use anyhow::Result;
use super::*;
fn init() -> (DecryptBackend<Key>, &'static [u8]) {
let be = Arc::new(MockBackend::new());
let key = Key::new();
let mut be = DecryptBackend::new(be, key);
be.set_zstd(Some(0));
let data = b"{test}"; // Note we use braces as this should be detected as valid json
(be, data)
}
#[test]
fn verify_encrypt_file_ok() -> Result<()> {
let (mut be, data) = init();
be.set_extra_verify(true);
let data_encrypted = be.encrypt_file(data)?;
be.very_file(&data_encrypted, data)?;
Ok(())
}
#[test]
fn verify_encrypt_file_no_test() -> Result<()> {
let (be, data) = init();
let mut data_encrypted = be.encrypt_file(data)?;
// modify some data
data_encrypted[5] = !data_encrypted[5];
// won't be detected
be.very_file(&data_encrypted, data)?;
Ok(())
}
#[test]
fn verify_encrypt_file_nok() -> Result<()> {
let (mut be, data) = init();
be.set_extra_verify(true);
let mut data_encrypted = be.encrypt_file(data)?;
// modify some data
data_encrypted[5] = !data_encrypted[5];
// will be detected
assert!(be.very_file(&data_encrypted, data).is_err());
Ok(())
}
#[test]
fn verify_encrypt_data_ok() -> Result<()> {
let (mut be, data) = init();
be.set_extra_verify(true);
let (data_encrypted, _, ul) = be.encrypt_data(data)?;
be.very_data(&data_encrypted, ul, data)?;
Ok(())
}
#[test]
fn verify_encrypt_data_no_test() -> Result<()> {
let (be, data) = init();
let (mut data_encrypted, _, ul) = be.encrypt_data(data)?;
// modify some data
data_encrypted[0] = !data_encrypted[0];
// won't be detected
be.very_data(&data_encrypted, ul, data)?;
Ok(())
}
#[test]
fn verify_encrypt_data_nok() -> Result<()> {
let (mut be, data) = init();
be.set_extra_verify(true);
let (mut data_encrypted, _, ul) = be.encrypt_data(data)?;
// modify some data
data_encrypted[5] = !data_encrypted[5];
// will be detected
assert!(be.very_data(&data_encrypted, ul, data).is_err());
Ok(())
}
}