Skip to content

Commit

Permalink
wip: copying hasher state
Browse files Browse the repository at this point in the history
  • Loading branch information
tbrezot committed Feb 10, 2025
1 parent 58f6076 commit 59ec744
Show file tree
Hide file tree
Showing 5 changed files with 400 additions and 245 deletions.
32 changes: 16 additions & 16 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use super::{
};
use crate::{
core::{
primitives::{decaps, encaps, full_decaps, refresh, rekey, setup},
primitives::{decaps, encaps, refresh, rekey, setup},
MasterPublicKey, MasterSecretKey, UserSecretKey, XEnc, SHARED_SECRET_LENGTH,
},
traits::{KemAc, PkeAc},
Expand Down Expand Up @@ -147,21 +147,21 @@ impl Covercrypt {
)
}

/// Returns a new encapsulation with the same rights as the one given, along
/// with a freshly generated shared secret.
pub fn recaps(
&self,
msk: &MasterSecretKey,
mpk: &MasterPublicKey,
encapsulation: &XEnc,
) -> Result<(Secret<32>, XEnc), Error> {
let (_ss, rights) = full_decaps(msk, encapsulation)?;
encaps(
&mut *self.rng.lock().expect("Mutex lock failed!"),
mpk,
&rights,
)
}
// /// Returns a new encapsulation with the same rights as the one given, along
// /// with a freshly generated shared secret.
// pub fn recaps(
// &self,
// msk: &MasterSecretKey,
// mpk: &MasterPublicKey,
// encapsulation: &XEnc,
// ) -> Result<(Secret<32>, XEnc), Error> {
// let (_ss, rights) = full_decaps(msk, encapsulation)?;
// encaps(
// &mut *self.rng.lock().expect("Mutex lock failed!"),
// mpk,
// &rights,
// )
// }
}

impl KemAc<SHARED_SECRET_LENGTH> for Covercrypt {
Expand Down
68 changes: 31 additions & 37 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,6 @@ impl RightPublicKey {
Self::Classic { .. } => false,
}
}

pub fn assert_homogeneity(subkeys: &[&Self]) -> Result<(), Error> {
let is_homogeneous = subkeys
.iter()
.all(|cpk| cpk.is_hybridized() == subkeys[0].is_hybridized());

if is_homogeneous {
Ok(())
} else {
Err(Error::OperationNotPermitted(
"classic and hybridized access policies cannot be mixed".to_string(),
))
}
}
}

/// Covercrypt user IDs are used to make user keys unique and traceable.
Expand Down Expand Up @@ -265,7 +251,7 @@ impl TracingSecretKey {
let mut markers: LinkedList<Scalar> = self
.tracers
.iter()
.zip(0..self.tracers.len() - 1)
.take(self.tracers.len() - 1)
.map(|_| Scalar::new(rng))
.collect();

Expand Down Expand Up @@ -417,17 +403,31 @@ impl MasterPublicKey {
self.tpk.0.iter().map(|gi| gi * r).collect()
}

fn select_subkeys(&self, targets: &HashSet<Right>) -> Result<Vec<&RightPublicKey>, Error> {
/// Returns the subkeys associated with the given rights in this public key,
/// alongside a boolean value that is true if all of them are hybridized.
fn select_subkeys(
&self,
targets: &HashSet<Right>,
) -> Result<(bool, Vec<&RightPublicKey>), Error> {
// This mutable variable is set to false if at least one sub-key is not
// hybridized.
let mut is_hybridized = true;

let subkeys = targets
.iter()
.map(|r| {
self.encryption_keys
let subkey = self
.encryption_keys
.get(r)
.ok_or_else(|| Error::KeyError(format!("no public key for right '{r:#?}'")))
.ok_or_else(|| Error::KeyError(format!("no public key for right '{r:#?}'")))?;
if !subkey.is_hybridized() {
is_hybridized = false;
}
Ok(subkey)
})
.collect::<Result<Vec<_>, _>>()?;
RightPublicKey::assert_homogeneity(&subkeys)?;
Ok(subkeys)
.collect::<Result<_, Error>>()?;

Ok((is_hybridized, subkeys))
}
}

Expand Down Expand Up @@ -461,19 +461,10 @@ impl UserSecretKey {
}
}

/// Encapsulation of a `SHARED_SECRET_LENGTH`-byte secret for a given right.
///
/// In case the security level of the associated right was set to post-quantum secure, the key
/// encapsulation is hybridized. This implies a significant size overhead.
#[derive(Debug, Clone, Hash, PartialEq)]
enum Encapsulation {
Classic {
F: [u8; SHARED_SECRET_LENGTH],
},
Hybridized {
E: kem::Encapsulation512,
F: [u8; SHARED_SECRET_LENGTH],
},
#[derive(Debug, Clone, PartialEq)]
enum Encapsulations {
HEncs(Vec<(kem::Encapsulation512, [u8; SHARED_SECRET_LENGTH])>),
CEncs(Vec<[u8; SHARED_SECRET_LENGTH]>),
}

/// Covercrypt encapsulation.
Expand All @@ -488,7 +479,7 @@ enum Encapsulation {
pub struct XEnc {
tag: Tag,
c: Vec<EcPoint>,
encapsulations: Vec<Encapsulation>,
encapsulations: Encapsulations,
}

impl XEnc {
Expand All @@ -497,8 +488,11 @@ impl XEnc {
self.c.len() - 1
}

#[cfg(feature = "test-utils")]
#[cfg(any(test, feature = "test-utils"))]
pub fn count(&self) -> usize {
self.encapsulations.len()
match &self.encapsulations {
Encapsulations::HEncs(vec) => vec.len(),
Encapsulations::CEncs(vec) => vec.len(),
}
}
}
Loading

0 comments on commit 59ec744

Please sign in to comment.