Skip to content

Commit

Permalink
fix: decrypt test example
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuthor committed Mar 1, 2023
1 parent 305e20a commit d0a531c
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 104 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ repos:
- id: cargo-check

- repo: https://github.com/Cosmian/git-hooks.git
rev: v1.0.6
rev: v1.0.7
hooks:
- id: cargo-update
- id: cargo-outdated
Expand Down
19 changes: 3 additions & 16 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,16 @@ All notable changes to this project will be documented in this file.
### Refactor

- [**breaking**] Move all interfaces (FFI, Wasm, pyo3) to `cloudproof_rust` repository
- remove unneeded dependencies
- remove inline macros
- remove serde wherever possible

### Testing

- Re-expose non-regression mechanism

---

## [11.0.0] - 2023-02-28


### Changed

- use 16-bytes tag

### Removed

- unneeded dependencies
- inline macros
- serde whereever possible
- interfaces

---

## [10.0.0] - 2023-02-02

### Documentation
Expand Down
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,3 @@ tiny-keccak = { version = "2.0.2", features = ["shake"] }

[dev-dependencies]
criterion = { version = "0.4", features = ["html_reports"], default_features = false }

12 changes: 6 additions & 6 deletions benches/BENCHMARKS_classic.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
## Table of Contents

- [Benchmark Results](#benchmark-results)
- [Header encryption](#header-encryption)
- [Header encryption + decryption](#header-encryption-+-decryption)
- [Key serialization](#key-serialization)
- [Header serialization](#header-serialization)
- [Header encryption](#header-encryption)
- [Header encryption + decryption](#header-encryption-and-decryption)
- [Key serialization](#key-serialization)
- [Header serialization](#header-serialization)

## Benchmark Results

Expand All @@ -16,7 +16,7 @@
|:-------|:------------------------------------|:------------------------------------|:------------------------------------|:------------------------------------|:------------------------------------ |
| | `293.43 us` (✅ **1.00x**) | `367.53 us` (*1.25x slower*) | `450.77 us` (*1.54x slower*) | `588.11 us` (*2.00x slower*) | `627.62 us` (*2.14x slower*) |

### Header encryption + decryption
### Header encryption and decryption

| | `ciphertexts with 1 partition(s), usk with 1 partitions` | `ciphertexts with 2 partition(s), usk with 1 partitions` | `ciphertexts with 3 partition(s), usk with 1 partitions` | `ciphertexts with 4 partition(s), usk with 1 partitions` | `ciphertexts with 5 partition(s), usk with 1 partitions` | `ciphertexts with 1 partition(s), usk with 2 partitions` | `ciphertexts with 2 partition(s), usk with 2 partitions` | `ciphertexts with 3 partition(s), usk with 2 partitions` | `ciphertexts with 4 partition(s), usk with 2 partitions` | `ciphertexts with 5 partition(s), usk with 2 partitions` | `ciphertexts with 1 partition(s), usk with 3 partitions` | `ciphertexts with 2 partition(s), usk with 3 partitions` | `ciphertexts with 3 partition(s), usk with 3 partitions` | `ciphertexts with 4 partition(s), usk with 3 partitions` | `ciphertexts with 5 partition(s), usk with 3 partitions` |
|:-------|:------------------------------------------------------------------|:------------------------------------------------------------------|:------------------------------------------------------------------|:------------------------------------------------------------------|:------------------------------------------------------------------|:------------------------------------------------------------------|:------------------------------------------------------------------|:------------------------------------------------------------------|:------------------------------------------------------------------|:------------------------------------------------------------------|:------------------------------------------------------------------|:------------------------------------------------------------------|:------------------------------------------------------------------|:------------------------------------------------------------------|:------------------------------------------------------------------ |
Expand All @@ -26,7 +26,7 @@

| | `MSK` | `MPK` | `USK 1 partition` |
|:-------|:--------------------------|:----------------------------------|:-------------------------------- |
| | `443.45 ns` (✅ **1.00x**) | `85.07 us` (*191.84x slower*) | `58.34 ns` (🚀 **7.60x faster**) |
| | `443.45 ns` (✅ **1.00x**) | `85.07 us` (*191.84x slower*) | `58.34 ns` (**7.60x faster**) |

### Header serialization

Expand Down
12 changes: 6 additions & 6 deletions benches/BENCHMARKS_hybridized.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
## Table of Contents

- [Benchmark Results](#benchmark-results)
- [Header encryption](#header-encryption)
- [Header encryption + decryption](#header-encryption-+-decryption)
- [Key serialization](#key-serialization)
- [Header serialization](#header-serialization)
- [Header encryption](#header-encryption)
- [Header encryption + decryption](#header-encryption-and-decryption)
- [Key serialization](#key-serialization)
- [Header serialization](#header-serialization)

## Benchmark Results

Expand All @@ -16,7 +16,7 @@
|:--|:---------------------------|:-----------------------------|:-----------------------------|:-----------------------------|:---------------------------|
| | `361.16 us` (✅ **1.00x**) | `508.30 us` (*1.41x slower*) | `693.84 us` (*1.92x slower*) | `851.50 us` (*2.36x slower*) | `1.01 ms` (*2.79x slower*) |

### Header encryption + decryption
### Header encryption and decryption

| | `ciphertexts with 1 partition(s), usk with 1 partitions` | `ciphertexts with 2 partition(s), usk with 1 partitions` | `ciphertexts with 3 partition(s), usk with 1 partitions` | `ciphertexts with 4 partition(s), usk with 1 partitions` | `ciphertexts with 5 partition(s), usk with 1 partitions` | `ciphertexts with 1 partition(s), usk with 2 partitions` | `ciphertexts with 2 partition(s), usk with 2 partitions` | `ciphertexts with 3 partition(s), usk with 2 partitions` | `ciphertexts with 4 partition(s), usk with 2 partitions` | `ciphertexts with 5 partition(s), usk with 2 partitions` | `ciphertexts with 1 partition(s), usk with 3 partitions` | `ciphertexts with 2 partition(s), usk with 3 partitions` | `ciphertexts with 3 partition(s), usk with 3 partitions` | `ciphertexts with 4 partition(s), usk with 3 partitions` | `ciphertexts with 5 partition(s), usk with 3 partitions` |
|:--|:---------------------------------------------------------|:---------------------------------------------------------|:---------------------------------------------------------|:---------------------------------------------------------|:---------------------------------------------------------|:---------------------------------------------------------|:---------------------------------------------------------|:---------------------------------------------------------|:---------------------------------------------------------|:---------------------------------------------------------|:---------------------------------------------------------|:---------------------------------------------------------|:---------------------------------------------------------|:---------------------------------------------------------|:---------------------------------------------------------|
Expand All @@ -26,7 +26,7 @@

| | `MSK` | `MPK` | `USK 1 partition` |
|:--|:--------------------------|:-----------------------------|:----------------------------------|
| | `978.19 ns` (✅ **1.00x**) | `86.63 us` (*88.56x slower*) | `141.37 ns` (🚀 **6.92x faster**) |
| | `978.19 ns` (✅ **1.00x**) | `86.63 us` (*88.56x slower*) | `141.37 ns` (**6.92x faster**) |

### Header serialization

Expand Down
20 changes: 11 additions & 9 deletions benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ fn policy() -> Result<Policy, Error> {
fn get_access_policies() -> (Vec<AccessPolicy>, Vec<AccessPolicy>) {
// Access policy with 1 partition
#[allow(unused_mut)]
let mut access_policies = vec![
AccessPolicy::from_boolean_expression("Department::FIN && Security Level::Protected")
.unwrap(),
];
let mut access_policies =
vec![
AccessPolicy::from_boolean_expression("Department::FIN && Security Level::Protected")
.unwrap(),
];

#[cfg(feature = "full_bench")]
{
Expand Down Expand Up @@ -122,10 +123,11 @@ fn get_access_policies() -> (Vec<AccessPolicy>, Vec<AccessPolicy>) {
// The intersection between the user access policies and the encryption
// policies is always "Department::FIN && Security Level::Protected" only.
#[allow(unused_mut)]
let mut user_access_policies = vec![
AccessPolicy::from_boolean_expression("Department::FIN && Security Level::Protected")
.unwrap(),
];
let mut user_access_policies =
vec![
AccessPolicy::from_boolean_expression("Department::FIN && Security Level::Protected")
.unwrap(),
];

#[cfg(feature = "full_bench")]
{
Expand Down Expand Up @@ -279,7 +281,7 @@ fn bench_header_decryption(c: &mut Criterion) {
.expect("cannot generate user private key")
})
.collect();
let mut group = c.benchmark_group("Header encryption + decryption");
let mut group = c.benchmark_group("Header encryption and decryption");
for (n_partitions_usk, usk) in user_decryption_keys.iter().enumerate() {
for (n_partition_ct, access_policy) in access_policies.iter().enumerate() {
group.bench_function(
Expand Down
1 change: 1 addition & 0 deletions benches/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ cargo criterion --features full_bench --message-format=json | criterion-table >b
cargo criterion --features full_bench,hybridized_bench --message-format=json | criterion-table >benches/BENCHMARKS_hybridized.md

sed -i "s/❌ //g" benches/BENCHMARKS*.md
sed -i "s/🚀 //g" benches/BENCHMARKS*.md
# sed -i "s/✅ //g" benches/BENCHMARKS*.md
4 changes: 2 additions & 2 deletions examples/decrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ fn main() {
engine::{GeneralPurpose, GeneralPurposeConfig},
Engine,
};
use cosmian_cover_crypt::{
api::EncryptedHeader, interfaces::statics::UserSecretKey, CoverCryptStruct,
use cosmian_cover_crypt::statics::{
CoverCryptX25519Aes256, EncryptedHeader, UserSecretKey,
};
use cosmian_crypto_core::bytes_ser_de::Serializable;

Expand Down
16 changes: 6 additions & 10 deletions examples/runme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,19 @@ fn main() {
EncryptedHeader::generate(&cover_crypt, &policy, &mpk, &access_policy, None, None).unwrap();

// user cannot decrypt the newly encrypted header
assert!(
new_encrypted_header
.decrypt(&cover_crypt, &usk, None)
.is_err()
);
assert!(new_encrypted_header
.decrypt(&cover_crypt, &usk, None)
.is_err());

// refresh user secret key, do not grant old encryption access
cover_crypt
.refresh_user_secret_key(&mut usk, &access_policy, &msk, &policy, false)
.unwrap();

// The user with refreshed key is able to decrypt the newly encrypted header.
assert!(
new_encrypted_header
.decrypt(&cover_crypt, &usk, None)
.is_ok()
);
assert!(new_encrypted_header
.decrypt(&cover_crypt, &usk, None)
.is_ok());

// But it cannot decrypt old ciphertexts
assert!(encrypted_header.decrypt(&cover_crypt, &usk, None).is_err());
Expand Down
25 changes: 17 additions & 8 deletions src/core/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,23 @@ pub struct EncryptedHeader<
}

impl<
const TAG_LENGTH: usize,
const SYM_KEY_LENGTH: usize,
const PK_LENGTH: usize,
const SK_LENGTH: usize,
KeyPair,
DEM,
CoverCryptScheme,
> EncryptedHeader<TAG_LENGTH, SYM_KEY_LENGTH, PK_LENGTH, SK_LENGTH, KeyPair, DEM, CoverCryptScheme>
const TAG_LENGTH: usize,
const SYM_KEY_LENGTH: usize,
const PK_LENGTH: usize,
const SK_LENGTH: usize,
KeyPair,
DEM,
CoverCryptScheme,
>
EncryptedHeader<
TAG_LENGTH,
SYM_KEY_LENGTH,
PK_LENGTH,
SK_LENGTH,
KeyPair,
DEM,
CoverCryptScheme,
>
where
KeyPair: DhKeyPair<PK_LENGTH, SK_LENGTH>,
DEM: Dem<SYM_KEY_LENGTH>,
Expand Down
27 changes: 14 additions & 13 deletions src/core/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,12 @@ impl<const SYM_KEY_LENGTH: usize> Serializable for KeyEncapsulation<SYM_KEY_LENG
}

impl<
const TAG_LENGTH: usize,
const ENCAPSULATION_LENGTH: usize,
const PUBLIC_KEY_LENGTH: usize,
DhPublicKey: KeyTrait<PUBLIC_KEY_LENGTH>,
> Serializable for Encapsulation<TAG_LENGTH, ENCAPSULATION_LENGTH, PUBLIC_KEY_LENGTH, DhPublicKey>
const TAG_LENGTH: usize,
const ENCAPSULATION_LENGTH: usize,
const PUBLIC_KEY_LENGTH: usize,
DhPublicKey: KeyTrait<PUBLIC_KEY_LENGTH>,
> Serializable
for Encapsulation<TAG_LENGTH, ENCAPSULATION_LENGTH, PUBLIC_KEY_LENGTH, DhPublicKey>
{
type Error = Error;

Expand Down Expand Up @@ -260,14 +261,14 @@ impl<
}

impl<
const TAG_LENGTH: usize,
const SYM_KEY_LENGTH: usize,
const PK_LENGTH: usize,
const SK_LENGTH: usize,
KeyPair,
DEM,
CoverCryptScheme,
> Serializable
const TAG_LENGTH: usize,
const SYM_KEY_LENGTH: usize,
const PK_LENGTH: usize,
const SK_LENGTH: usize,
KeyPair,
DEM,
CoverCryptScheme,
> Serializable
for EncryptedHeader<
TAG_LENGTH,
SYM_KEY_LENGTH,
Expand Down
20 changes: 8 additions & 12 deletions src/test_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ mod tests {
#[test]
fn read_policy() {
// Can read a `Policy`
let policy_str = include_bytes!("../../tests_data/policy.json");
let policy_str = include_bytes!("./tests_data/policy.json");
Policy::try_from(policy_str.as_slice()).unwrap();

// Can read a `LegacyPolicy`
let legacy_policy_str = include_bytes!("../../tests_data/legacy_policy.json");
let legacy_policy_str = include_bytes!("./tests_data/legacy_policy.json");
serde_json::from_slice::<LegacyPolicy>(legacy_policy_str).unwrap();

// Can read `LegacyPolicy` as `Policy`
Expand Down Expand Up @@ -220,11 +220,9 @@ mod tests {
)?;

// Decryption fails without refreshing the user key
assert!(
encrypted_header
.decrypt(&cover_crypt, &top_secret_fin_usk, None)
.is_err()
);
assert!(encrypted_header
.decrypt(&cover_crypt, &top_secret_fin_usk, None)
.is_err());

cover_crypt.refresh_user_secret_key(
&mut top_secret_fin_usk,
Expand All @@ -237,11 +235,9 @@ mod tests {
)?;

// The refreshed key can decrypt the header
assert!(
encrypted_header
.decrypt(&cover_crypt, &top_secret_fin_usk, None)
.is_ok()
);
assert!(encrypted_header
.decrypt(&cover_crypt, &top_secret_fin_usk, None)
.is_ok());

Ok(())
}
Expand Down
36 changes: 16 additions & 20 deletions src/test_utils/non_regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,14 @@ impl NonRegressionTestVector {
// top_secret_fin_key
self.low_secret_fin_test_vector
.decrypt(&self.top_secret_fin_key.key)?;
assert!(
self.low_secret_mkg_test_vector
.decrypt(&self.top_secret_fin_key.key)
.is_err()
);
assert!(
self.top_secret_mkg_test_vector
.decrypt(&self.top_secret_fin_key.key)
.is_err()
);
assert!(self
.low_secret_mkg_test_vector
.decrypt(&self.top_secret_fin_key.key)
.is_err());
assert!(self
.top_secret_mkg_test_vector
.decrypt(&self.top_secret_fin_key.key)
.is_err());

// top_secret_mkg_fin_key
self.low_secret_fin_test_vector
Expand All @@ -245,18 +243,16 @@ impl NonRegressionTestVector {
self.top_secret_mkg_test_vector
.decrypt(&self.top_secret_mkg_fin_key.key)?;

assert!(
self.low_secret_fin_test_vector
.decrypt(&self.medium_secret_mkg_key.key)
.is_err()
);
assert!(self
.low_secret_fin_test_vector
.decrypt(&self.medium_secret_mkg_key.key)
.is_err());
self.low_secret_mkg_test_vector
.decrypt(&self.medium_secret_mkg_key.key)?;
assert!(
self.top_secret_mkg_test_vector
.decrypt(&self.medium_secret_mkg_key.key)
.is_err()
);
assert!(self
.top_secret_mkg_test_vector
.decrypt(&self.medium_secret_mkg_key.key)
.is_err());
Ok(())
}
}
Expand Down

0 comments on commit d0a531c

Please sign in to comment.