Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add missing serialization impls for structs Tokens, TokenWithEncryptedValueList and TokenToEncryptedValueMap. #88

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,10 @@ repos:
rev: v1.0.16
hooks:
- id: stable-cargo-format
- id: cargo-upgrade
- id: cargo-update
# - id: dprint-toml-fix
# - id: cargo-upgrade
# - id: cargo-update
- id: cargo-machete
- id: cargo-outdated
- id: cargo-udeps
- id: cargo-tests-all
- id: cargo-test-doc
- id: clippy-autofix-all
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to this project will be documented in this file.

## [6.0.1] - 2024-10-17

### Features

- Add missing serialization impls for structs `Tokens`, `TokenWithEncryptedValueList` and `TokenToEncryptedValueMap`.

## [6.0.0] - 2023-11-21

### Features
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cosmian_findex"
version = "6.0.0"
version = "6.0.1"
authors = [
"Chloé Hébant <[email protected]>",
"Bruno Grieder <[email protected]>",
Expand Down Expand Up @@ -31,6 +31,7 @@ base64 = "0.21.5"
cosmian_crypto_core = { version = "9.3.0", default-features = false, features = [
"aes",
"sha3",
"ser",
] }
# Once available in stable Rust, use `!` std primitive
# <https://doc.rust-lang.org/std/primitive.never.html>
Expand Down
2 changes: 1 addition & 1 deletion src/edx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ pub mod in_memory {
use crate::{error::DbInterfaceErrorTrait, EncryptedValue};

#[derive(Debug)]
pub struct InMemoryDbError(String);
pub struct InMemoryDbError(pub String);

impl From<CryptoCoreError> for InMemoryDbError {
fn from(value: CryptoCoreError) -> Self {
Expand Down
102 changes: 99 additions & 3 deletions src/edx/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ use std::{

use base64::engine::{general_purpose::STANDARD, Engine};
use cosmian_crypto_core::{
reexport::rand_core::CryptoRngCore, Aes256Gcm, DemInPlace, FixedSizeCBytes, Instantiable,
Nonce, RandomFixedSizeCBytes, SymmetricKey,
bytes_ser_de::{to_leb128_len, Deserializer, Serializable, Serializer},
reexport::rand_core::CryptoRngCore,
Aes256Gcm, DemInPlace, FixedSizeCBytes, Instantiable, Nonce, RandomFixedSizeCBytes,
SymmetricKey,
};
use zeroize::{Zeroize, ZeroizeOnDrop};

Expand Down Expand Up @@ -114,6 +116,32 @@ impl From<Tokens> for HashSet<Token> {
}
}

impl Serializable for Tokens {
type Error = CoreError;

fn length(&self) -> usize {
self.iter().map(|token| token.0.len()).sum()
}

fn write(&self, ser: &mut Serializer) -> Result<usize, Self::Error> {
let mut n = ser.write_leb128_u64(self.len() as u64)?;
for token in self.iter() {
n += ser.write_array(token)?;
}
Ok(n)
}

fn read(de: &mut Deserializer) -> Result<Self, Self::Error> {
let length = de.read_leb128_u64()? as usize;
let mut res = HashSet::with_capacity(length);
for _ in 0..length {
let token = de.read_array::<{ Token::LENGTH }>()?;
res.insert(Token::from(token));
}
Ok(Self(res))
}
}

/// Seed used to derive a key.
#[derive(Debug)]
pub struct Seed<const LENGTH: usize>([u8; LENGTH]);
Expand Down Expand Up @@ -206,7 +234,7 @@ impl<const VALUE_LENGTH: usize> TryFrom<&[u8]> for EncryptedValue<VALUE_LENGTH>
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
if value.len() != Self::LENGTH {
return Err(Self::Error::Conversion(format!(
"incorrect length for encrypted value: {} bytes give, {} bytes expected",
"incorrect length for encrypted value: {} bytes given, {} bytes expected",
value.len(),
Self::LENGTH
)));
Expand Down Expand Up @@ -330,6 +358,40 @@ impl<const VALUE_LENGTH: usize> IntoIterator for TokenWithEncryptedValueList<VAL
}
}

impl<const VALUE_LENGTH: usize> Serializable for TokenWithEncryptedValueList<VALUE_LENGTH> {
type Error = CoreError;

fn length(&self) -> usize {
self.iter()
.map(|(_, _)| {
Token::LENGTH
+ to_leb128_len(EncryptedValue::<VALUE_LENGTH>::LENGTH)
+ EncryptedValue::<VALUE_LENGTH>::LENGTH
})
.sum()
}

fn write(&self, ser: &mut Serializer) -> Result<usize, Self::Error> {
let mut n = ser.write_leb128_u64(self.len() as u64)?;
for (token, encrypted_value) in self.iter() {
n += ser.write_array(token)?;
n += ser.write_vec(&<Vec<u8>>::from(encrypted_value))?;
}
Ok(n)
}

fn read(de: &mut cosmian_crypto_core::bytes_ser_de::Deserializer) -> Result<Self, Self::Error> {
let length = de.read_leb128_u64()? as usize;
let mut res = Vec::with_capacity(length);
for _ in 0..length {
let token = de.read_array::<{ Token::LENGTH }>()?;
let encrypted_value = EncryptedValue::try_from(de.read_vec()?.as_slice())?;
res.push((Token::from(token), encrypted_value));
}
Ok(Self(res))
}
}

#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct TokenToEncryptedValueMap<const VALUE_LENGTH: usize>(
pub HashMap<Token, EncryptedValue<VALUE_LENGTH>>,
Expand Down Expand Up @@ -391,3 +453,37 @@ impl<const VALUE_LENGTH: usize> IntoIterator for TokenToEncryptedValueMap<VALUE_
self.0.into_iter()
}
}

impl<const VALUE_LENGTH: usize> Serializable for TokenToEncryptedValueMap<VALUE_LENGTH> {
type Error = CoreError;

fn length(&self) -> usize {
self.iter()
.map(|(_, _)| {
Token::LENGTH
+ to_leb128_len(EncryptedValue::<VALUE_LENGTH>::LENGTH)
+ EncryptedValue::<VALUE_LENGTH>::LENGTH
})
.sum()
}

fn write(&self, ser: &mut Serializer) -> Result<usize, Self::Error> {
let mut n = ser.write_leb128_u64(self.len() as u64)?;
for (token, encrypted_value) in self.iter() {
n += ser.write_array(token)?;
n += ser.write_vec(&<Vec<u8>>::from(encrypted_value))?;
}
Ok(n)
}

fn read(de: &mut cosmian_crypto_core::bytes_ser_de::Deserializer) -> Result<Self, Self::Error> {
let length = de.read_leb128_u64()? as usize;
let mut res = HashMap::with_capacity(length);
for _ in 0..length {
let token = de.read_array::<{ Token::LENGTH }>()?;
let encrypted_value = EncryptedValue::try_from(de.read_vec()?.as_slice())?;
res.insert(Token::from(token), encrypted_value);
}
Ok(Self(res))
}
}
2 changes: 1 addition & 1 deletion src/findex_mm/compact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl<
let rng = &mut *rng.lock().expect("could not lock mutex");
for (token, entry) in continuation.entries {
old_entries.insert(token);
if remaining_entry_tokens.get(&token).is_some() {
if remaining_entry_tokens.contains(&token) {
new_entries.insert(
self.entry_table
.tokenize(new_key, &entry.tag_hash, Some(new_label)),
Expand Down
Loading