Skip to content

Commit 199b533

Browse files
committed
Change decrypt_in_place to return a Result.
1 parent 1e2b840 commit 199b533

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/crypto/chacha20poly1305.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,14 @@ mod real_chachapoly {
7676
self.finish_and_get_tag(out_tag);
7777
}
7878

79-
pub fn decrypt_inplace(&mut self, input_output: &mut [u8], tag: &[u8]) -> bool {
79+
pub fn decrypt_inplace(&mut self, input_output: &mut [u8], tag: &[u8]) -> Result<(), ()> {
8080
assert!(self.finished == false);
8181
self.decrypt_in_place(input_output);
82-
self.finish_and_check_tag(tag)
82+
if self.finish_and_check_tag(tag) {
83+
Ok(())
84+
} else {
85+
Err(())
86+
}
8387
}
8488

8589
// Encrypt `input_output` in-place. To finish and calculate the tag, use `finish_and_get_tag`

src/util/storable_builder.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ impl<T: EntropySource> StorableBuilder<T> {
6767
let mut cipher =
6868
ChaCha20Poly1305::new(&self.data_encryption_key, &encryption_metadata.nonce, &[]);
6969

70-
if cipher.decrypt_inplace(&mut storable.data, encryption_metadata.tag.borrow()) {
71-
let data_blob = PlaintextBlob::decode(&storable.data[..])
72-
.map_err(|e| Error::new(ErrorKind::InvalidData, e))?;
73-
Ok((data_blob.value, data_blob.version))
74-
} else {
75-
Err(Error::new(ErrorKind::InvalidData, "Invalid Tag"))
76-
}
70+
cipher
71+
.decrypt_inplace(&mut storable.data, encryption_metadata.tag.borrow())
72+
.map_err(|_| Error::new(ErrorKind::InvalidData, "Invalid Tag"))?;
73+
74+
let data_blob = PlaintextBlob::decode(&storable.data[..])
75+
.map_err(|e| Error::new(ErrorKind::InvalidData, e))?;
76+
Ok((data_blob.value, data_blob.version))
7777
}
7878
}
7979

0 commit comments

Comments
 (0)