1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use crate::{
collections::Map,
operation::{CgkaOperation, CgkaOperationGraph},
pcs_key::PcsKey,
};
use alloc::vec::Vec;
use keyhive_crypto::digest::Digest;
/// A root secret checked against the update that produced it.
#[derive(Debug)]
pub(crate) struct VerifiedRootSecret {
op_hash: Digest<CgkaOperation>,
secret: PcsKey,
}
impl VerifiedRootSecret {
/// Check that `secret` is the root secret the update `op_hash` produced. Its
/// public key must be the sole share key at the root of that update's path.
///
/// Returns `None` if `graph` has no such key for `op_hash` or `secret` does
/// not match it.
pub(crate) fn verify(
graph: &CgkaOperationGraph,
op_hash: Digest<CgkaOperation>,
secret: PcsKey,
) -> Option<Self> {
(graph.update_root_share_key(&op_hash)? == secret.0.share_key())
.then_some(Self { op_hash, secret })
}
}
/// Root secrets by the update that produced them.
///
/// Entries are added only by [`RootSecrets::insert`], which takes a
/// [`VerifiedRootSecret`], or copied from another [`RootSecrets`] by
/// [`RootSecrets::merge_from`], ensuring every entry was checked against
/// its update.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) struct RootSecrets(Map<Digest<CgkaOperation>, PcsKey>);
impl RootSecrets {
/// Record a verified root secret for its update.
pub(crate) fn insert(&mut self, verified: VerifiedRootSecret) {
self.0.insert(verified.op_hash, verified.secret);
}
/// The root secret recorded for `op_hash`, borrowed.
pub(crate) fn get_ref(&self, op_hash: &Digest<CgkaOperation>) -> Option<&PcsKey> {
self.0.get(op_hash)
}
/// A copy of the root secret recorded for `op_hash`, which is wiped when
/// dropped too. Prefer [`Self::get_ref`] or [`Self::contains`] unless an
/// owned key is needed.
pub(crate) fn get(&self, op_hash: &Digest<CgkaOperation>) -> Option<PcsKey> {
self.get_ref(op_hash).cloned()
}
/// Whether a root secret is recorded for `op_hash`, without copying it.
pub(crate) fn contains(&self, op_hash: &Digest<CgkaOperation>) -> bool {
self.get_ref(op_hash).is_some()
}
/// Every recorded update with its root secret.
pub(crate) fn iter(&self) -> impl Iterator<Item = (&Digest<CgkaOperation>, &PcsKey)> {
self.0.iter()
}
/// Add every secret `other` has recorded, returning the ones not recorded
/// here before.
pub(crate) fn merge_from(&mut self, other: &Self) -> Vec<(Digest<CgkaOperation>, PcsKey)> {
let mut added = Vec::new();
for (op_hash, secret) in other.iter() {
if self.0.get(op_hash) != Some(secret) {
self.0.insert(*op_hash, secret.clone());
added.push((*op_hash, secret.clone()));
}
}
added
}
#[cfg(test)]
pub(crate) fn clear(&mut self) {
self.0.clear();
}
#[cfg(test)]
pub(crate) fn remove(&mut self, op_hash: &Digest<CgkaOperation>) {
self.0.remove(op_hash);
}
}