Skip to content

Commit

Permalink
Derive the name segment directly from the key
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Jun 30, 2023
1 parent 76627f1 commit 122fa2a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
9 changes: 7 additions & 2 deletions wnfs/src/private/node/header.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{PrivateNodeHeaderSerializable, TemporalKey};
use super::{PrivateNodeHeaderSerializable, TemporalKey, REVISION_SEGMENT_DSS};
use crate::{error::FsError, private::RevisionRef};
use anyhow::{bail, Result};
use libipld_core::cid::Cid;
Expand Down Expand Up @@ -130,6 +130,11 @@ impl PrivateNodeHeader {
TemporalKey::from(&self.ratchet)
}

pub(crate) fn derive_revision_segment(&self) -> NameSegment {
let hasher = self.ratchet.derive_key(REVISION_SEGMENT_DSS);
NameSegment::from_digest(hasher)
}

/// Gets the revision name for this node.
///
/// It's this node's name with a last segment added that's
Expand Down Expand Up @@ -159,7 +164,7 @@ impl PrivateNodeHeader {
/// ```
pub fn get_revision_name(&self) -> Name {
self.name
.with_segments_added(Some(self.derive_temporal_key().to_revision_segment()))
.with_segments_added(Some(self.derive_revision_segment()))
}

/// Gets the name for this node.
Expand Down
23 changes: 9 additions & 14 deletions wnfs/src/private/node/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use sha3::{Digest, Sha3_256};
use skip_ratchet::Ratchet;
use std::fmt::Debug;
use wnfs_hamt::Hasher;
use wnfs_nameaccumulator::NameSegment;

//--------------------------------------------------------------------------------------------------
// Type Definitions
Expand All @@ -32,8 +31,14 @@ pub struct TemporalKey(pub AesKey);
// Constants
//--------------------------------------------------------------------------------------------------

const REVISION_SEGMENT_DOMAIN_SEPARATION_STRING: &str = "wnfs/segment deriv from temporal";
const TEMPORAL_KEY_DOMAIN_SEPARATION_STRING: &str = "wnfs/temporal deriv from ratchet";
/// The revision segment derivation domain separation string
/// used for salting the hashing function when turning
/// node names into revisioned node names.
pub(crate) const REVISION_SEGMENT_DSS: &str = "wnfs/segment deriv from temporal";
/// The temporal key derivation domain seperation string
/// used for salting the hashing function when deriving
/// symmetric keys from ratchets.
pub(crate) const TEMPORAL_KEY_DSS: &str = "wnfs/temporal deriv from ratchet";

//--------------------------------------------------------------------------------------------------
// Implementations
Expand Down Expand Up @@ -70,13 +75,6 @@ impl TemporalKey {
.unwrap_with_padding_vec(ciphertext)
.map_err(|e| AesError::UnableToEncrypt(format!("{e}")))?)
}

pub(crate) fn to_revision_segment(&self) -> NameSegment {
let mut hasher = Sha3_256::new();
hasher.update(REVISION_SEGMENT_DOMAIN_SEPARATION_STRING);
hasher.update(self.0.as_bytes());
NameSegment::from_digest(hasher)
}
}

impl SnapshotKey {
Expand Down Expand Up @@ -186,10 +184,7 @@ impl From<[u8; KEY_BYTE_SIZE]> for TemporalKey {
impl From<&Ratchet> for TemporalKey {
fn from(ratchet: &Ratchet) -> Self {
Self::from(AesKey::new(
ratchet
.derive_key(TEMPORAL_KEY_DOMAIN_SEPARATION_STRING)
.finalize()
.into(),
ratchet.derive_key(TEMPORAL_KEY_DSS).finalize().into(),
))
}
}
Expand Down

0 comments on commit 122fa2a

Please sign in to comment.