Skip to content

Commit

Permalink
fix some errors after rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
phochard committed Oct 28, 2024
1 parent e7f1d30 commit ae73e47
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 805 deletions.
5 changes: 3 additions & 2 deletions examples/runme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ fn main() {
// Encrypt
let (_, encrypted_header) = EncryptedHeader::generate(
&cover_crypt,
&policy,
&mpk,
&access_policy.clone(),
None,
Expand All @@ -73,13 +74,13 @@ fn main() {
//
// Rekey the user access policy.
let mpk = cover_crypt
.rekey(&access_policy, &mut msk)
.rekey(&access_policy, &policy, &mut msk)
.unwrap();

let enc_policy = AccessPolicy::parse("Security Level::Top Secret").unwrap();
// Encrypt with rotated attribute
let (_, new_encrypted_header) =
EncryptedHeader::generate(&cover_crypt, &mpk, &access_policy, None, None).unwrap();
EncryptedHeader::generate(&cover_crypt, &policy, &mpk, &enc_policy, None, None).unwrap();

// user cannot decrypt the newly encrypted header
assert!(new_encrypted_header
Expand Down
4 changes: 0 additions & 4 deletions src/abe_policy/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ use super::{
};
use crate::Error;

use super::{
AccessPolicy, Attribute, AttributeParameters, AttributeStatus, Dimension, DimensionBuilder, EncryptionHint, Partition, Policy, PolicyVersion
};

impl Display for Policy {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{self:?}")
Expand Down
123 changes: 72 additions & 51 deletions src/core/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,61 +82,62 @@ impl Covercrypt {
/// - adds the policy coordinates that don't belong yet to the MSK,
/// generating new keys.
///
/// - `msk` : master secret key
/// - `mpk` : master public key
/// The new MPK holds the latest public keys of each coordinates of the new policy.
pub fn update_master_keys(
&self,
policy: &Policy,
msk: &mut MasterSecretKey,
) -> Result<MasterPublicKey, Error> {
update_coordinate_keys(
&mut *self.rng.lock().expect("Mutex lock failed!"),
msk,
mpk,
msk.policy.generate_all_partitions()?,
)
policy.generate_universal_coordinates()?,
)?;
msk.mpk()
}

/// Generate new keys associated to the given access policy in the master
/// keys. User keys will need to be refreshed after this step.
/// - `access_policy` : describe the keys to renew
/// - `msk` : master secret key
/// - `mpk` : master public key
pub fn rekey_master_keys(
/// Generates new keys for each coordinate in the semantic space of the
/// given access policy and update the given master keys.
///
/// All user keys need to be refreshed.
// TODO document error cases.
pub fn rekey(
&self,
access_policy: &AccessPolicy,
ap: &AccessPolicy,
policy: &Policy,
msk: &mut MasterSecretKey,
) -> Result<MasterPublicKey, Error> {
rekey(
&mut *self.rng.lock().expect("Mutex lock failed!"),
msk,
mpk,
mpk.policy.access_policy_to_partitions(access_policy, false)?,
)
policy.generate_semantic_space_coordinates(ap)?,
)?;
msk.mpk()
}

/// Removes old keys associated to the given master keys from the master
/// keys. This will permanently remove access to old ciphers.
/// - `access_policy` : describe the keys to prune
/// - `msk` : master secret key
/// Removes all but the latest secret of each coordinate in the semantic
/// space of the given access policy from the given master keys.
///
/// This action is *irreversible*, and all user keys need to be refreshed.
// TODO document error cases.
pub fn prune_master_secret_key(
&self,
access_policy: &AccessPolicy,
policy: &Policy,
msk: &mut MasterSecretKey,
) -> Result<MasterPublicKey, Error> {
prune(
msk,
&msk.policy.access_policy_to_partitions(access_policy, false)?,
)
&policy.generate_semantic_space_coordinates(access_policy)?,
);
msk.mpk()
}

/// Generates a USK associated to the given access policy.
///
/// A new user secret key only has the latest keys corresponding to its
/// access policy.
///
/// - `msk` : master secret key
/// - `access_policy` : user access policy
/// - `policy` : global policy
/// It will be given the latest secret of each coordinate in the semantic
/// space of its access policy.
// TODO document error cases.
pub fn generate_user_secret_key(
&self,
msk: &mut MasterSecretKey,
Expand All @@ -146,7 +147,7 @@ impl Covercrypt {
usk_keygen(
&mut *self.rng.lock().expect("Mutex lock failed!"),
msk,
&policy.access_policy_to_partitions(access_policy, true)?,
policy.generate_semantic_space_coordinates(access_policy)?,
)
}

Expand Down Expand Up @@ -178,17 +179,39 @@ pub trait CovercryptKEM {
/// Generates an encapsulation for the given access
/// policy.
///
/// - `pk` : public key
/// - `encryption_policy` : encryption policy used for the encapsulation
pub fn encaps(
/// # Error
///
/// Returns an error if the access policy is not valid.
fn encaps(
&self,
pk: &MasterPublicKey,
access_policy: &AccessPolicy,
) -> Result<(SymmetricKey<SYM_KEY_LENGTH>, Encapsulation), Error> {
mpk: &MasterPublicKey,
policy: &Policy,
ap: &AccessPolicy,
) -> Result<(Secret<SEED_LENGTH>, Encapsulation), Error>;

/// Attempts opening the given encapsulation using the given
/// user secret key.
///
/// Returns the encapsulated symmetric key if the user key holds
/// the correct rights.
fn decaps(
&self,
usk: &UserSecretKey,
enc: &Encapsulation,
) -> Result<Option<Secret<SEED_LENGTH>>, Error>;
}

impl CovercryptKEM for Covercrypt {
fn encaps(
&self,
mpk: &MasterPublicKey,
policy: &Policy,
ap: &AccessPolicy,
) -> Result<(Secret<SEED_LENGTH>, Encapsulation), Error> {
encaps(
&mut *self.rng.lock().expect("Mutex lock failed!"),
pk,
&pk.policy.access_policy_to_partitions(access_policy, false)?,
mpk,
&policy.generate_point_coordinates(ap)?,
)
}

Expand All @@ -204,21 +227,19 @@ pub trait CovercryptKEM {
pub trait CovercryptPKE<Aead, const KEY_LENGTH: usize> {
/// Encrypts the given plaintext using Covercrypt and the given DEM.
///
/// - `cover_crypt` : `Covercrypt` object
/// - `public_key` : `Covercrypt` public key
/// - `encryption_policy` : access policy used for the encapsulation
/// - `header_metadata` : additional data symmetrically encrypted in the
/// header
/// - `authentication_data` : authentication data used in the DEM encryption
pub fn generate(
cover_crypt: &Covercrypt,
public_key: &MasterPublicKey,
encryption_policy: &AccessPolicy,
metadata: Option<&[u8]>,
authentication_data: Option<&[u8]>,
) -> Result<(SymmetricKey<SYM_KEY_LENGTH>, Self), Error> {
let (symmetric_key, encapsulation) =
cover_crypt.encaps(public_key, encryption_policy)?;
/// Creates a Covercrypt encapsulation of a 256-bit seed, and use it with
/// the given authentication data to encrypt the plaintext.
///
/// # Error
///
/// Returns an error if the access policy is not valid.
fn encrypt(
&self,
mpk: &MasterPublicKey,
policy: &Policy,
ap: &AccessPolicy,
plaintext: &[u8],
) -> Result<(Encapsulation, Vec<u8>), Error>;

/// Attempts decrypting the given ciphertext using the Covercrypt KEM and the DEM.
///
Expand Down
2 changes: 1 addition & 1 deletion src/core/encrypted_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,4 @@ impl EncryptedHeader {
pub struct CleartextHeader {
pub seed: Secret<SEED_LENGTH>,
pub metadata: Option<Vec<u8>>,
}
}
28 changes: 16 additions & 12 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
use cosmian_crypto_core::{reexport::rand_core::CryptoRngCore, Aes256Gcm, SymmetricKey};

use crate::{
abe_policy::{Partition, Policy},
abe_policy::{Coordinate, Policy},
data_struct::{RevisionMap, RevisionVec},
Error,
};
Expand Down Expand Up @@ -136,12 +136,15 @@ impl CoordinatePublicKey {
#[derive(Clone, Debug, PartialEq, Eq, Hash, Default)]
struct UserId(LinkedList<Scalar>);

#[derive(Debug, PartialEq, Eq)]
pub struct MasterPublicKey {
g1: R25519PublicKey,
g2: R25519PublicKey,
pub(crate) subkeys: HashMap<Partition, PublicSubkey>,
policy: Policy,
impl UserId {
/// Returns the tracing level of the USK.
fn tracing_level(&self) -> usize {
self.0.len() - 1
}

fn iter(&self) -> impl Iterator<Item = &Scalar> {
self.0.iter()
}
}

/// Covercrypt tracing secret key.
Expand Down Expand Up @@ -257,11 +260,10 @@ impl TracingPublicKey {
/// - an optional key for symmetric USK-signing.
#[derive(Debug, PartialEq, Eq)]
pub struct MasterSecretKey {
s: R25519PrivateKey,
s1: R25519PrivateKey,
s2: R25519PrivateKey,
pub(crate) subkeys: RevisionMap<Partition, SecretSubkey>,
kmac_key: Option<SymmetricKey<KMAC_KEY_LENGTH>>,
s: Scalar,
tsk: TracingSecretKey,
coordinate_secrets: RevisionMap<Coordinate, (bool, CoordinateSecretKey)>,
signing_key: Option<SymmetricKey<SIGNING_KEY_LENGTH>>,
policy: Policy,
}

Expand Down Expand Up @@ -367,6 +369,7 @@ impl MasterSecretKey {
})
})
.collect(),
policy: self.policy.clone(),
})
}
}
Expand All @@ -380,6 +383,7 @@ impl MasterSecretKey {
pub struct MasterPublicKey {
tpk: TracingPublicKey,
coordinate_keys: HashMap<Coordinate, CoordinatePublicKey>,
policy: Policy,
}

impl MasterPublicKey {
Expand Down
3 changes: 2 additions & 1 deletion src/core/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use super::{
SEED_LENGTH, SIGNATURE_LENGTH, SIGNING_KEY_LENGTH, TAG_LENGTH,
};
use crate::{
abe_policy::{AttributeStatus, Coordinate, EncryptionHint},
abe_policy::{AttributeStatus, Coordinate, EncryptionHint, Policy},
core::{Encapsulation, MasterPublicKey, MasterSecretKey, SeedEncapsulation, UserSecretKey},
data_struct::{RevisionMap, RevisionVec},
Error,
Expand Down Expand Up @@ -78,6 +78,7 @@ pub fn setup(rng: &mut impl CryptoRngCore, tracing_level: usize) -> Result<Maste
tsk,
coordinate_secrets: RevisionMap::new(),
signing_key: Some(SymmetricKey::<SIGNING_KEY_LENGTH>::new(rng)),
policy: Policy::new(),
})
}

Expand Down
Loading

0 comments on commit ae73e47

Please sign in to comment.