Skip to content

Commit 4d739b7

Browse files
authored
Merge pull request #281 from A6GibKm/remove-allocs-item-decrypt
client: encrypted_item: Remove unnecesary allocs
2 parents 433aaca + b9d956d commit 4d739b7

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

client/src/file/api/encrypted_item.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,23 @@ impl EncryptedItem {
1717
self.hashed_attributes.get(key).map(|b| b.as_slice()) == Some(blob)
1818
}
1919

20-
pub fn decrypt(mut self, key: &Key) -> Result<Item, Error> {
21-
let mac_tag = self.blob.split_off(self.blob.len() - crypto::mac_len());
20+
pub fn decrypt(self, key: &Key) -> Result<Item, Error> {
21+
let n = self.blob.len();
22+
let n_mac = crypto::mac_len();
23+
let n_iv = crypto::iv_len();
24+
25+
// The encrypted data, the iv, and the mac are concatenated into blob.
26+
let (encrypted_data_with_iv, mac_tag) = &self.blob.split_at(n - n_mac);
2227

2328
// verify item
24-
if !crypto::verify_mac(&self.blob, key, mac_tag)? {
29+
if !crypto::verify_mac(encrypted_data_with_iv, key, mac_tag)? {
2530
return Err(Error::MacError);
2631
}
2732

28-
let iv = self.blob.split_off(self.blob.len() - crypto::iv_len());
33+
let (encrypted_data, iv) = encrypted_data_with_iv.split_at(n - n_mac - n_iv);
2934

3035
// decrypt item
31-
let decrypted = crypto::decrypt(self.blob, key, iv)?;
36+
let decrypted = crypto::decrypt(encrypted_data, key, iv)?;
3237

3338
let item = Item::try_from(decrypted.as_slice())?;
3439

0 commit comments

Comments
 (0)