Skip to content

Commit

Permalink
refacto: remove clones and clean function signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo Rosenkranz-Costa committed Jan 12, 2024
1 parent 61d73f3 commit 88eb173
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 18 deletions.
5 changes: 2 additions & 3 deletions src/core/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl Covercrypt {
/// - `access_policy` : describe the keys to prune
/// - `policy` : global policy
/// - `msk` : master secret key
pub fn prune_master_keys(
pub fn prune_master_secret_key(
&self,
access_policy: &AccessPolicy,
policy: &Policy,
Expand Down Expand Up @@ -141,8 +141,7 @@ impl Covercrypt {
///
/// - `usk` : the user key to refresh
/// - `msk` : master secret key
/// - `keep_old_accesses` : whether access to old partitions (i.e. before
/// rotation) should be kept
/// - `keep_old_rights` : whether or not to keep old decryption rights
pub fn refresh_user_secret_key(
&self,
usk: &mut UserSecretKey,
Expand Down
13 changes: 8 additions & 5 deletions src/core/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,19 +481,22 @@ pub fn refresh(
let first_usk_subkey = usk_subkeys.next()?;

let mut new_usk_subkeys = LinkedList::new();
// Add new master secret subkeys
for msk_subkey in msk_subkeys.by_ref() {
new_usk_subkeys.push_back(msk_subkey.clone());
if msk_subkey == &first_usk_subkey {
new_usk_subkeys.push_back(first_usk_subkey);
break;
}
new_usk_subkeys.push_back(msk_subkey.clone());
}
for next_usk_subkey in usk_subkeys {
if Some(&next_usk_subkey) != msk_subkeys.next() {
// Keep old matching subkeys between the master and user subkeys
for subkey in usk_subkeys {
if Some(&subkey) != msk_subkeys.next() {
break;
}
new_usk_subkeys.push_back(next_usk_subkey);
new_usk_subkeys.push_back(subkey);
}
Some((coordinate.clone(), new_usk_subkeys))
Some((coordinate, new_usk_subkeys))
})
})
.collect::<RevisionVec<_, _>>();
Expand Down
6 changes: 0 additions & 6 deletions src/data_struct/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,3 @@ Cons:
- following linked list pointers can be slower than iterating a regular vector

- serialization requires following each linked list

## Operations

### Master Secret Key

### User Secret Key
6 changes: 3 additions & 3 deletions src/data_struct/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{
fmt::{self, Debug},
hash::Hash,
marker::PhantomData,
mem::swap,
usize,
};

Expand Down Expand Up @@ -92,7 +93,7 @@ where
}

/// Updates the key for a given entry while retaining the current order.
pub fn update_key(&mut self, old_key: &K, new_key: K) -> Result<(), Error> {
pub fn update_key(&mut self, old_key: &K, mut new_key: K) -> Result<(), Error> {
// Get index from old_key
let index_entry = *self
.indices
Expand All @@ -107,8 +108,7 @@ where
// Remove old key from indices
let _ = self.indices.remove(old_key);
// Replace old_key with new_key inside entries
let replaced_key = std::mem::replace(&mut self.entries[index_entry].0, new_key);
assert_eq!(&replaced_key, old_key);
swap(&mut self.entries[index_entry].0, &mut new_key);
Ok(())
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ mod tests {
assert_eq!(msk.subkeys.count_elements(), 25);

// remove older subkeys for `Department::FIN`
cover_crypt.prune_master_keys(&rekey_access_policy, &policy, &mut msk)?;
cover_crypt.prune_master_secret_key(&rekey_access_policy, &policy, &mut msk)?;
assert_eq!(mpk.subkeys.len(), 20);
// we only keep the last subkeys in the secret key
assert_eq!(msk.subkeys.count_elements(), 20);
Expand Down

0 comments on commit 88eb173

Please sign in to comment.