Skip to content

Commit

Permalink
fix: neats
Browse files Browse the repository at this point in the history
  • Loading branch information
tbrezot committed Apr 19, 2024
1 parent 1fbb914 commit 692de32
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 35 deletions.
45 changes: 21 additions & 24 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// TODO: there seems to be a bug in the `clippy` suggestion/lint, check it out later.
#![allow(clippy::needless_lifetimes)]

use std::{
collections::{HashMap, HashSet, LinkedList},
hash::Hash,
Expand All @@ -19,7 +16,6 @@ pub mod macros;

pub mod api;
pub mod primitives;

#[cfg(feature = "serialization")]
pub mod serialization;

Expand All @@ -30,26 +26,27 @@ mod tests;

use elgamal::{EcPoint, Scalar};

/// The length of the keys encapsulated by Covercrypt.
/// The length of the secret encapsulated by Covercrypt.
///
/// They are 32 bytes long to enable reaching 128 bits of post-quantum security
/// when using it with a sensible DEM.
pub const SEED_LENGTH: usize = 32;

/// The length of the KMAC key used to sign user secret keys.
/// The length of the key used to sign user secret keys.
///
/// It is only 16-byte long because no post-quantum security is needed for
/// now. An upgraded signature scheme can still be added later when quantum
/// computers become available.
const KMAC_KEY_LENGTH: usize = 16;
const SIGNING_KEY_LENGTH: usize = 16;

/// The length of the KMAC signature.
const KMAC_SIG_LENGTH: usize = 32;
const SIGNATURE_LENGTH: usize = 32;

/// KMAC signature is used to guarantee the integrity of the user secret keys.
type KmacSignature = [u8; KMAC_SIG_LENGTH];
type KmacSignature = [u8; SIGNATURE_LENGTH];

/// Length of the Covercrypt early abort tag
/// Length of the Covercrypt early abort tag. 128 bits are enough since we only want collision
/// resistance.
const TAG_LENGTH: usize = 16;

/// Covercrypt early abort tag is used during the decapsulation to verify the
Expand All @@ -62,26 +59,26 @@ pub const MIN_TRACING_LEVEL: usize = 1;
/// The Covercrypt subkeys hold the DH secret key associated to a coordinate.
/// Subkeys can be hybridized, in which case they also hold a PQ-KEM secret key.
#[derive(Clone, Debug, PartialEq, Eq)]
enum CoordinatePublicKey {
enum CoordinateSecretKey {
Hybridized {
postquantum_pk: postquantum::PublicKey,
elgamal_pk: EcPoint,
postquantum_sk: postquantum::SecretKey,
elgamal_sk: Scalar,
},
Classic {
elgamal_pk: EcPoint,
elgamal_sk: Scalar,
},
}

/// The Covercrypt subkeys hold the DH secret key associated to a coordinate.
/// Subkeys can be hybridized, in which case they also hold a PQ-KEM secret key.
/// The Covercrypt public keys hold the DH secret public key associated to a coordinate.
/// Subkeys can be hybridized, in which case they also hold a PQ-KEM public key.
#[derive(Clone, Debug, PartialEq, Eq)]
enum CoordinateSecretKey {
enum CoordinatePublicKey {
Hybridized {
postquantum_sk: postquantum::SecretKey,
elgamal_sk: Scalar,
postquantum_pk: postquantum::PublicKey,
elgamal_pk: EcPoint,
},
Classic {
elgamal_sk: Scalar,
elgamal_pk: EcPoint,
},
}

Expand Down Expand Up @@ -322,7 +319,7 @@ pub struct MasterSecretKey {
s: Scalar,
tsk: TracingSecretKey,
coordinate_keypairs: RevisionMap<Coordinate, CoordinateKeypair>,
signing_key: Option<SymmetricKey<KMAC_KEY_LENGTH>>,
signing_key: Option<SymmetricKey<SIGNING_KEY_LENGTH>>,
}

impl MasterSecretKey {
Expand Down Expand Up @@ -410,9 +407,9 @@ impl MasterSecretKey {
}

/// Returns the most recent public key associated to each coordinate.
fn get_latest_coordinate_pk<'a>(
&'a self,
) -> impl Iterator<Item = (Coordinate, CoordinatePublicKey)> + 'a {
fn get_latest_coordinate_pk(
&self,
) -> impl Iterator<Item = (Coordinate, CoordinatePublicKey)> + '_ {
self.coordinate_keypairs
.iter()
.filter_map(|(coordinate, keypairs)| {
Expand Down
8 changes: 4 additions & 4 deletions src/core/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use zeroize::Zeroize;

use super::{
elgamal, postquantum, CoordinateKeypair, CoordinatePublicKey, CoordinateSecretKey,
KmacSignature, TracingSecretKey, KMAC_KEY_LENGTH, KMAC_SIG_LENGTH, MIN_TRACING_LEVEL,
SEED_LENGTH, TAG_LENGTH,
KmacSignature, TracingSecretKey, MIN_TRACING_LEVEL, SEED_LENGTH, SIGNATURE_LENGTH,
SIGNING_KEY_LENGTH, TAG_LENGTH,
};
use crate::{
abe_policy::{AttributeStatus, Coordinate, EncryptionHint},
Expand Down Expand Up @@ -52,7 +52,7 @@ fn sign_usk(msk: &MasterSecretKey, usk: &UserSecretKey) -> Option<KmacSignature>
}
}
}
let mut res = [0; KMAC_SIG_LENGTH];
let mut res = [0; SIGNATURE_LENGTH];
kmac.into_xof().squeeze(&mut res);
Some(res)
} else {
Expand Down Expand Up @@ -88,7 +88,7 @@ pub fn setup(rng: &mut impl CryptoRngCore, tracing_level: usize) -> Result<Maste
s,
tsk,
coordinate_keypairs: RevisionMap::new(),
signing_key: Some(SymmetricKey::<KMAC_KEY_LENGTH>::new(rng)),
signing_key: Some(SymmetricKey::<SIGNING_KEY_LENGTH>::new(rng)),
})
}

Expand Down
14 changes: 7 additions & 7 deletions src/core/serialization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use std::collections::{HashMap, HashSet, LinkedList};

use cosmian_crypto_core::{
bytes_ser_de::{to_leb128_len, Deserializer, Serializable, Serializer},
FixedSizeCBytes, RandomFixedSizeCBytes, SymmetricKey,
FixedSizeCBytes, SymmetricKey,
};

use super::{
elgamal::{EcPoint, Scalar},
postquantum::{self, PublicKey},
CoordinateKeypair, CoordinatePublicKey, CoordinateSecretKey, TracingPublicKey,
TracingSecretKey, UserId, KMAC_KEY_LENGTH, KMAC_SIG_LENGTH, TAG_LENGTH,
TracingSecretKey, UserId, SIGNATURE_LENGTH, SIGNING_KEY_LENGTH, TAG_LENGTH,
};
use crate::{
abe_policy::Coordinate,
Expand Down Expand Up @@ -282,11 +282,11 @@ impl Serializable for MasterSecretKey {

println!("HEY");

let signing_key = if de.value().len() < KMAC_KEY_LENGTH {
let signing_key = if de.value().len() < SIGNING_KEY_LENGTH {
None
} else {
Some(SymmetricKey::try_from_bytes(
de.read_array::<KMAC_KEY_LENGTH>()?,
de.read_array::<SIGNING_KEY_LENGTH>()?,
)?)
};

Expand Down Expand Up @@ -429,10 +429,10 @@ impl Serializable for UserSecretKey {
.collect::<Result<_, _>>()?;
coordinate_keys.insert_new_chain(coordinate, new_chain);
}
let msk_signature = if de.value().len() < KMAC_SIG_LENGTH {
let msk_signature = if de.value().len() < SIGNATURE_LENGTH {
None
} else {
Some(de.read_array::<KMAC_SIG_LENGTH>()?)
Some(de.read_array::<SIGNATURE_LENGTH>()?)
};
Ok(Self {
id,
Expand Down Expand Up @@ -587,7 +587,7 @@ impl Serializable for CleartextHeader {

/// Tries to serialize the cleartext header.
fn write(&self, ser: &mut Serializer) -> Result<usize, Self::Error> {
let mut n = ser.write_array(self.symmetric_key.as_bytes())?;
let mut n = ser.write_array(&self.symmetric_key)?;
match &self.metadata {
Some(bytes) => n += ser.write_vec(bytes)?,
None => n += ser.write_vec(&[])?,
Expand Down

0 comments on commit 692de32

Please sign in to comment.