Debian Codegraph @

mlspp/include/mls/treekem.h

raw symbols SCIP
1 #pragma once
2
3 #include "mls/common.h"
4 #include "mls/core_types.h"
5 #include "mls/crypto.h"
6 #include "mls/tree_math.h"
7 #include <tls/tls_syntax.h>
8
9 #define ENABLE_TREE_DUMP 1
10
11 namespace mlspp {
12
13 enum struct NodeType : uint8_t
14 {
15 reserved = 0x00,
16 leaf = 0x01,
17 parent = 0x02,
18 };
19
20 struct Node
21 {
22 var::variant<LeafNode, ParentNode> node;
23
24 const HPKEPublicKey& public_key() const;
25 std::optional<bytes> parent_hash() const;
26
27 TLS_SERIALIZABLE(node)
28 TLS_TRAITS(tls::variant<NodeType>)
29 };
30
31 struct OptionalNode
32 {
33 std::optional<Node> node;
34
35 bool blank() const { return !node.has_value(); }
36 bool leaf() const
37 {
38 return !blank() && var::holds_alternative<LeafNode>(opt::get(node).node);
39 }
40
41 LeafNode& leaf_node() { return var::get<LeafNode>(opt::get(node).node); }
42
43 const LeafNode& leaf_node() const
44 {
45 return var::get<LeafNode>(opt::get(node).node);
46 }
47
48 ParentNode& parent_node()
49 {
50 return var::get<ParentNode>(opt::get(node).node);
51 }
52
53 const ParentNode& parent_node() const
54 {
55 return var::get<ParentNode>(opt::get(node).node);
56 }
57
58 TLS_SERIALIZABLE(node)
59 };
60
61 struct TreeKEMPublicKey;
62
63 struct TreeKEMPrivateKey
64 {
65 CipherSuite suite;
66 LeafIndex index;
67 bytes update_secret;
68 std::map<NodeIndex, bytes> path_secrets;
69 std::map<NodeIndex, HPKEPrivateKey> private_key_cache;
70
71 static TreeKEMPrivateKey solo(CipherSuite suite,
72 LeafIndex index,
73 HPKEPrivateKey leaf_priv);
74 static TreeKEMPrivateKey create(const TreeKEMPublicKey& pub,
75 LeafIndex from,
76 const bytes& leaf_secret);
77 static TreeKEMPrivateKey joiner(const TreeKEMPublicKey& pub,
78 LeafIndex index,
79 HPKEPrivateKey leaf_priv,
80 NodeIndex intersect,
81 const std::optional<bytes>& path_secret);
82
83 void set_leaf_priv(HPKEPrivateKey priv);
84 std::tuple<NodeIndex, bytes, bool> shared_path_secret(LeafIndex to) const;
85
86 bool have_private_key(NodeIndex n) const;
87 std::optional<HPKEPrivateKey> private_key(NodeIndex n);
88 std::optional<HPKEPrivateKey> private_key(NodeIndex n) const;
89
90 void decap(LeafIndex from,
91 const TreeKEMPublicKey& pub,
92 const bytes& context,
93 const UpdatePath& path,
94 const std::vector<LeafIndex>& except);
95
96 void truncate(LeafCount size);
97
98 bool consistent(const TreeKEMPrivateKey& other) const;
99 bool consistent(const TreeKEMPublicKey& other) const;
100
101 #if ENABLE_TREE_DUMP
102 void dump() const;
103 #endif
104
105 // TODO(RLB) Make this private but exposed to test vectors
106 void implant(const TreeKEMPublicKey& pub,
107 NodeIndex start,
108 const bytes& path_secret);
109 };
110
111 struct TreeKEMPublicKey
112 {
113 CipherSuite suite;
114 LeafCount size{ 0 };
115 std::vector<OptionalNode> nodes;
116
117 explicit TreeKEMPublicKey(CipherSuite suite);
118
119 TreeKEMPublicKey() = default;
120 TreeKEMPublicKey(const TreeKEMPublicKey& other) = default;
121 TreeKEMPublicKey(TreeKEMPublicKey&& other) = default;
122 TreeKEMPublicKey& operator=(const TreeKEMPublicKey& other) = default;
123 TreeKEMPublicKey& operator=(TreeKEMPublicKey&& other) = default;
124
125 LeafIndex allocate_leaf();
126 LeafIndex add_leaf(const LeafNode& leaf);
127 void update_leaf(LeafIndex index, const LeafNode& leaf);
128 void blank_path(LeafIndex index);
129
130 TreeKEMPrivateKey update(LeafIndex from,
131 const bytes& leaf_secret,
132 const bytes& group_id,
133 const SignaturePrivateKey& sig_priv,
134 const LeafNodeOptions& opts);
135 UpdatePath encap(const TreeKEMPrivateKey& priv,
136 const bytes& context,
137 const std::vector<LeafIndex>& except) const;
138
139 void merge(LeafIndex from, const UpdatePath& path);
140 void set_hash_all();
141 const bytes& get_hash(NodeIndex index);
142 bytes root_hash() const;
143
144 bool parent_hash_valid(LeafIndex from, const UpdatePath& path) const;
145 bool parent_hash_valid() const;
146
147 bool has_leaf(LeafIndex index) const;
148 std::optional<LeafIndex> find(const LeafNode& leaf) const;
149 std::optional<LeafNode> leaf_node(LeafIndex index) const;
150 std::vector<NodeIndex> resolve(NodeIndex index) const;
151
152 template<typename UnaryPredicate>
153 bool all_leaves(const UnaryPredicate& pred) const
154 {
155 for (LeafIndex i{ 0 }; i < size; i.val++) {
156 const auto& node = node_at(i);
157 if (node.blank()) {
158 continue;
159 }
160
161 if (!pred(i, node.leaf_node())) {
162 return false;
163 }
164 }
165
166 return true;
167 }
168
169 template<typename UnaryPredicate>
170 bool any_leaf(const UnaryPredicate& pred) const
171 {
172 for (LeafIndex i{ 0 }; i < size; i.val++) {
173 const auto& node = node_at(i);
174 if (node.blank()) {
175 continue;
176 }
177
178 if (pred(i, node.leaf_node())) {
179 return true;
180 }
181 }
182
183 return false;
184 }
185
186 using FilteredDirectPath =
187 std::vector<std::tuple<NodeIndex, std::vector<NodeIndex>>>;
188 FilteredDirectPath filtered_direct_path(NodeIndex index) const;
189
190 void truncate();
191
192 OptionalNode& node_at(NodeIndex n);
193 const OptionalNode& node_at(NodeIndex n) const;
194 OptionalNode& node_at(LeafIndex n);
195 const OptionalNode& node_at(LeafIndex n) const;
196
197 TLS_SERIALIZABLE(nodes)
198
199 #if ENABLE_TREE_DUMP
200 void dump() const;
201 #endif
202
203 private:
204 std::map<NodeIndex, bytes> hashes;
205
206 void clear_hash_all();
207 void clear_hash_path(LeafIndex index);
208
209 bool has_parent_hash(NodeIndex child, const bytes& target_ph) const;
210
211 bytes parent_hash(const ParentNode& parent, NodeIndex copath_child) const;
212 std::vector<bytes> parent_hashes(
213 LeafIndex from,
214 const FilteredDirectPath& fdp,
215 const std::vector<UpdatePathNode>& path_nodes) const;
216
217 using TreeHashCache = std::map<NodeIndex, std::pair<size_t, bytes>>;
218 const bytes& original_tree_hash(TreeHashCache& cache,
219 NodeIndex index,
220 std::vector<LeafIndex> parent_except) const;
221 bytes original_parent_hash(TreeHashCache& cache,
222 NodeIndex parent,
223 NodeIndex sibling) const;
224
225 bool exists_in_tree(const HPKEPublicKey& key,
226 std::optional<LeafIndex> except) const;
227 bool exists_in_tree(const SignaturePublicKey& key,
228 std::optional<LeafIndex> except) const;
229
230 OptionalNode blank_node;
231
232 friend struct TreeKEMPrivateKey;
233 };
234
235 tls::ostream&
236 operator<<(tls::ostream& str, const TreeKEMPublicKey& obj);
237 tls::istream&
238 operator>>(tls::istream& str, TreeKEMPublicKey& obj);
239
240 struct LeafNodeHashInput;
241 struct ParentNodeHashInput;
242
243 } // namespace mlspp
244
245 namespace mlspp::tls {
246
247 TLS_VARIANT_MAP(mlspp::NodeType, mlspp::LeafNodeHashInput, leaf)
248 TLS_VARIANT_MAP(mlspp::NodeType,
249 mlspp::ParentNodeHashInput,
250 parent)
251
252 TLS_VARIANT_MAP(mlspp::NodeType, mlspp::LeafNode, leaf)
253 TLS_VARIANT_MAP(mlspp::NodeType, mlspp::ParentNode, parent)
254
255 } // namespace mlspp::tls