Skip to content

Commit

Permalink
use composer for search and sortable keys
Browse files Browse the repository at this point in the history
  • Loading branch information
joernheinemann committed Jun 14, 2024
1 parent 620575c commit 6be3506
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
6 changes: 3 additions & 3 deletions crypto/src/group/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use sentc_crypto_common::group::{
};
use sentc_crypto_common::user::{UserPublicKeyData, UserVerifyKeyData};
use sentc_crypto_common::UserId;
use sentc_crypto_core::cryptomat::{CryptoAlg, SignK};
use sentc_crypto_core::cryptomat::{CryptoAlg, SearchableKeyComposer, SignK, SortableKeyComposer};
use sentc_crypto_core::{
group as core_group,
HmacKey as CoreHmacKey,
Expand Down Expand Up @@ -393,7 +393,7 @@ pub fn decrypt_group_hmac_key(group_key: &SymmetricKey, server_output: GroupHmac
{
let encrypted_hmac_key = Base64::decode_vec(&server_output.encrypted_hmac_key).map_err(|_| SdkUtilError::DerivedKeyWrongFormat)?;

let key = CoreHmacKey::decrypt_key_with_master_key(&group_key.key, &encrypted_hmac_key, &server_output.encrypted_hmac_alg)?;
let key = CoreHmacKey::decrypt_by_master_key(&group_key.key, &encrypted_hmac_key, &server_output.encrypted_hmac_alg)?;

Ok(HmacKey {
key_id: server_output.id,
Expand All @@ -405,7 +405,7 @@ pub fn decrypt_group_sortable_key(group_key: &SymmetricKey, server_output: Group
{
let encrypted_key = Base64::decode_vec(&server_output.encrypted_sortable_key).map_err(|_| SdkUtilError::DerivedKeyWrongFormat)?;

let key = CoreSortableKey::decrypt_key_with_master_key(&group_key.key, &encrypted_key, &server_output.encrypted_sortable_alg)?;
let key = CoreSortableKey::decrypt_by_master_key(&group_key.key, &encrypted_key, &server_output.encrypted_sortable_alg)?;

Ok(SortableKey {
key_id: server_output.id,
Expand Down
21 changes: 13 additions & 8 deletions crypto_core/src/alg/hmac/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloc::vec::Vec;

use crate::alg::hmac::hmac_sha256::HmacSha256Key;
use crate::cryptomat::{CryptoAlg, SearchableKey, SearchableKeyGen, SymKey};
use crate::cryptomat::{CryptoAlg, SearchableKey, SearchableKeyComposer, SearchableKeyGen, SymKey};
use crate::Error;

pub(crate) mod hmac_sha256;
Expand Down Expand Up @@ -29,13 +29,6 @@ impl HmacKey
}
}

pub fn decrypt_key_with_master_key<M: SymKey>(master_key: &M, encrypted_key: &[u8], alg_str: &str) -> Result<Self, Error>
{
let key = master_key.decrypt(encrypted_key)?;

Self::from_bytes(&key, alg_str)
}

pub fn hmac_sha256_from_bytes_owned(bytes: Vec<u8>) -> Result<Self, Error>
{
Ok(HmacKey::HmacSha256(bytes.try_into()?))
Expand Down Expand Up @@ -86,3 +79,15 @@ impl SearchableKeyGen for HmacKey
Ok(HmacSha256Key::generate()?.into())
}
}

impl SearchableKeyComposer for HmacKey
{
type Key = Self;

fn decrypt_by_master_key<M: SymKey>(master_key: &M, encrypted_key: &[u8], alg_str: &str) -> Result<Self::Key, Error>
{
let key = master_key.decrypt(encrypted_key)?;

Self::from_bytes(&key, alg_str)
}
}
21 changes: 13 additions & 8 deletions crypto_core/src/alg/sortable/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloc::vec::Vec;

use crate::alg::sortable::ope::OpeSortableKey;
use crate::cryptomat::{CryptoAlg, SortableKey, SortableKeyGen, SymKey};
use crate::cryptomat::{CryptoAlg, SortableKey, SortableKeyComposer, SortableKeyGen, SymKey};
use crate::Error;

pub(crate) mod ope;
Expand Down Expand Up @@ -29,13 +29,6 @@ impl SortKeys
}
}

pub fn decrypt_key_with_master_key<M: SymKey>(master_key: &M, encrypted_key: &[u8], alg_str: &str) -> Result<Self, Error>
{
let key = master_key.decrypt(encrypted_key)?;

Self::from_bytes(&key, alg_str)
}

pub fn ope_key_from_bytes_owned(bytes: Vec<u8>) -> Result<Self, Error>
{
Ok(SortKeys::Ope(bytes.try_into()?))
Expand Down Expand Up @@ -81,3 +74,15 @@ impl SortableKeyGen for SortKeys
Ok(OpeSortableKey::generate()?.into())
}
}

impl SortableKeyComposer for SortKeys
{
type Key = Self;

fn decrypt_by_master_key<M: SymKey>(master_key: &M, encrypted_key: &[u8], alg_str: &str) -> Result<Self::Key, Error>
{
let key = master_key.decrypt(encrypted_key)?;

Self::from_bytes(&key, alg_str)
}
}
14 changes: 14 additions & 0 deletions crypto_core/src/cryptomat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ pub trait SearchableKeyGen
fn generate() -> Result<Self::SearchableKey, Error>;
}

pub trait SearchableKeyComposer
{
type Key: SearchableKey;

fn decrypt_by_master_key<M: SymKey>(master_key: &M, encrypted_key: &[u8], alg_str: &str) -> Result<Self::Key, Error>;
}

//__________________________________________________________________________________________________
//sortable

Expand All @@ -190,6 +197,13 @@ pub trait SortableKeyGen
fn generate() -> Result<Self::SortableKey, Error>;
}

pub trait SortableKeyComposer
{
type Key: SortableKey;

fn decrypt_by_master_key<M: SymKey>(master_key: &M, encrypted_key: &[u8], alg_str: &str) -> Result<Self::Key, Error>;
}

//__________________________________________________________________________________________________
//pw hash

Expand Down

0 comments on commit 6be3506

Please sign in to comment.