use crate::{
encrypted::EncryptedSecret,
error::CgkaError,
id::TreeId,
keys::{HasShareKey, NodeKeys, ShareKeyMap},
treemath::TreeNodeIndex,
};
use alloc::{collections::BTreeMap, string::ToString, vec::Vec};
use keyhive_codec::{
error::DecodeError,
traits::{decode_exact, Decode, Encode, StreamDecode},
};
use keyhive_crypto::share_key::{ShareKey, ShareSecretKey};
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
pub struct SecretStore {
pub pk: ShareKey,
pub sk: BTreeMap<TreeNodeIndex, EncryptedSecret<ShareSecretKey>>,
pub encrypter_pk: ShareKey,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
pub struct PathNode {
pub pk: ShareKey,
pub sk: BTreeMap<TreeNodeIndex, EncryptedSecret<ShareSecretKey>>,
}
impl Encode for PathNode {
fn encode_into(&self, out: &mut Vec<u8>) {
self.pk.encode_into(out);
self.sk.encode_into(out);
}
}
impl Decode for PathNode {
fn decode(bytes: &[u8]) -> Result<Self, DecodeError> {
decode_exact(bytes)
}
}
impl StreamDecode for PathNode {
fn decode_next(bytes: &[u8]) -> Result<(Self, &[u8]), DecodeError> {
let (pk, rest) = ShareKey::decode_next(bytes)?;
let (sk, rest) = BTreeMap::decode_next(rest)?;
Ok((PathNode { pk, sk }, rest))
}
}
impl HasShareKey for SecretStore {
fn share_key(&self) -> ShareKey {
self.pk
}
}
impl SecretStore {
pub(crate) fn decrypt_with_sibling_secret(
&self,
doc_id: TreeId,
sks: &ShareKeyMap,
) -> Option<ShareSecretKey> {
self.sk.values().find_map(|encrypted| {
let plaintext = sks
.try_decrypt_encryption(doc_id, self.encrypter_pk, encrypted)
.ok()?;
ShareSecretKey::try_from_slice(&plaintext)
})
}
pub(crate) fn decrypt_with_child_secret(
&self,
doc_id: TreeId,
child_sk: &ShareSecretKey,
) -> Option<ShareSecretKey> {
self.sk
.values()
.find_map(|encrypted| encrypted.try_encrypter_decrypt(doc_id, child_sk).ok())
.and_then(|plaintext| ShareSecretKey::try_from_slice(&plaintext))
}
pub fn decrypt_secret(
&self,
doc_id: TreeId,
child_node_keys: &NodeKeys,
child_sks: &mut ShareKeyMap,
seen_idxs: &[TreeNodeIndex],
) -> Result<ShareSecretKey, CgkaError> {
let is_encrypter = child_node_keys.contains_key(&self.encrypter_pk)
|| child_sks.contains_key(&self.encrypter_pk);
let mut lookup_idx = seen_idxs.last().ok_or(CgkaError::EncryptedSecretNotFound)?;
if !self.sk.contains_key(lookup_idx) {
let mut found = false;
for idx in seen_idxs.iter().rev().skip(1) {
if self.sk.contains_key(idx) {
lookup_idx = idx;
found = true;
break;
}
}
if !found {
return Err(CgkaError::EncryptedSecretNotFound);
}
}
let encrypted = self
.sk
.get(lookup_idx)
.ok_or(CgkaError::EncryptedSecretNotFound)?;
let decrypted = if is_encrypter {
let secret_key = child_sks
.get(&self.encrypter_pk)
.ok_or(CgkaError::SecretKeyNotFound)?;
encrypted
.try_encrypter_decrypt(doc_id, secret_key)
.map_err(|e| CgkaError::Decryption(e.to_string()))?
} else {
child_sks.try_decrypt_encryption(doc_id, self.encrypter_pk, encrypted)?
};
ShareSecretKey::try_from_slice(&decrypted).ok_or(CgkaError::Conversion)
}
}