Debian Codegraph @

mlspp/lib/mls_vectors/src/mls_vectors.cpp

raw symbols SCIP
1 #include <mls/key_schedule.h>
2 #include <mls/state.h>
3 #include <mls/tree_math.h>
4 #include <mls_vectors/mls_vectors.h>
5
6 #include <iostream> // XXX
7
8 namespace mls_vectors {
9
10 using namespace mlspp;
11
12 ///
13 /// Assertions for verifying test vectors
14 ///
15
16 template<typename T, std::enable_if_t<std::is_enum_v<T>, int> = 0>
17 std::ostream&
18 operator<<(std::ostream& str, const T& obj)
19 {
20 auto u = static_cast<std::underlying_type_t<T>>(obj);
21 return str << u;
22 }
23
24 static std::ostream&
25 operator<<(std::ostream& str, const NodeIndex& obj)
26 {
27 return str << obj.val;
28 }
29
30 static std::ostream&
31 operator<<(std::ostream& str, const NodeCount& obj)
32 {
33 return str << obj.val;
34 }
35
36 template<typename T>
37 static std::ostream&
38 operator<<(std::ostream& str, const std::optional<T>& obj)
39 {
40 if (!obj) {
41 return str << "(nullopt)";
42 }
43
44 return str << opt::get(obj);
45 }
46
47 static std::ostream&
48 operator<<(std::ostream& str, const std::vector<uint8_t>& obj)
49 {
50 return str << to_hex(obj);
51 }
52
53 template<typename T>
54 static std::ostream&
55 operator<<(std::ostream& str, const std::vector<T>& obj)
56 {
57 for (const auto& val : obj) {
58 str << val << " ";
59 }
60 return str;
61 }
62
63 static std::ostream&
64 operator<<(std::ostream& str, const GroupContent::RawContent& obj)
65 {
66 return var::visit(
67 overloaded{
68 [&](const Proposal&) -> std::ostream& { return str << "[Proposal]"; },
69 [&](const Commit&) -> std::ostream& { return str << "[Commit]"; },
70 [&](const ApplicationData&) -> std::ostream& {
71 return str << "[ApplicationData]";
72 },
73 },
74 obj);
75 }
76
77 template<typename T>
78 inline std::enable_if_t<T::_tls_serializable, std::ostream&>
79 operator<<(std::ostream& str, const T& obj)
80 {
81 return str << to_hex(tls::marshal(obj));
82 }
83
84 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
85 #define VERIFY(label, test) \
86 if (auto err = verify_bool(label, test)) { \
87 return err; \
88 }
89
90 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
91 #define VERIFY_EQUAL(label, actual, expected) \
92 if (auto err = verify_equal(label, actual, expected)) { \
93 return err; \
94 }
95
96 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
97 #define VERIFY_TLS_RTT(label, Type, expected) \
98 if (auto err = verify_round_trip<Type>(label, expected)) { \
99 return err; \
100 }
101
102 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
103 #define VERIFY_TLS_RTT_VAL(label, Type, expected, val) \
104 if (auto err = verify_round_trip<Type>(label, expected, val)) { \
105 return err; \
106 }
107
108 template<typename T>
109 static std::optional<std::string>
110 verify_bool(const std::string& label, const T& test)
111 {
112 if (test) {
113 return std::nullopt;
114 }
115
116 return label;
117 }
118
119 template<typename T, typename U>
120 static std::optional<std::string>
121 verify_equal(const std::string& label, const T& actual, const U& expected)
122 {
123 if (actual == expected) {
124 return std::nullopt;
125 }
126
127 auto ss = std::stringstream();
128 ss << "Error: " << label << " " << actual << " != " << expected;
129 return ss.str();
130 }
131
132 template<typename T>
133 static std::optional<std::string>
134 verify_round_trip(const std::string& label, const bytes& expected)
135 {
136 auto noop = [](const auto& /* unused */) { return true; };
137 return verify_round_trip<T>(label, expected, noop);
138 }
139
140 template<typename T, typename F>
141 static std::optional<std::string>
142 verify_round_trip(const std::string& label, const bytes& expected, const F& val)
143 {
144 auto obj = T{};
145 try {
146 obj = tls::get<T>(expected);
147 } catch (const std::exception& e) {
148 auto ss = std::stringstream();
149 ss << "Decode error: " << label << " " << e.what();
150 return ss.str();
151 }
152
153 if (!val(obj)) {
154 auto ss = std::stringstream();
155 ss << "Validation error: " << label;
156 return ss.str();
157 }
158
159 auto actual = tls::marshal(obj);
160 VERIFY_EQUAL(label, actual, expected);
161 return std::nullopt;
162 }
163
164 ///
165 /// PseudoRandom
166 ///
167
168 PseudoRandom::Generator::Generator(CipherSuite suite_in,
169 const std::string& label)
170 : suite(suite_in)
171 , seed(suite.hpke().kdf.extract({}, from_ascii(label)))
172 {
173 }
174
175 PseudoRandom::Generator::Generator(CipherSuite suite_in, bytes seed_in)
176 : suite(suite_in)
177 , seed(std::move(seed_in))
178 {
179 }
180
181 PseudoRandom::Generator
182 PseudoRandom::Generator::sub(const std::string& label) const
183 {
184 return { suite, suite.derive_secret(seed, label) };
185 }
186
187 bytes
188 PseudoRandom::Generator::secret(const std::string& label) const
189 {
190 return suite.derive_secret(seed, label);
191 }
192
193 bytes
194 PseudoRandom::Generator::generate(const std::string& label, size_t size) const
195 {
196 return suite.expand_with_label(seed, label, {}, size);
197 }
198
199 uint16_t
200 PseudoRandom::Generator::uint16(const std::string& label) const
201 {
202 auto data = generate(label, 2);
203 return tls::get<uint16_t>(data);
204 }
205
206 uint32_t
207 PseudoRandom::Generator::uint32(const std::string& label) const
208 {
209 auto data = generate(label, 4);
210 return tls::get<uint16_t>(data);
211 }
212
213 uint64_t
214 PseudoRandom::Generator::uint64(const std::string& label) const
215 {
216 auto data = generate(label, 8);
217 return tls::get<uint16_t>(data);
218 }
219
220 SignaturePrivateKey
221 PseudoRandom::Generator::signature_key(const std::string& label) const
222 {
223 auto data = generate(label, suite.secret_size());
224 return SignaturePrivateKey::derive(suite, data);
225 }
226
227 HPKEPrivateKey
228 PseudoRandom::Generator::hpke_key(const std::string& label) const
229 {
230 auto data = generate(label, suite.secret_size());
231 return HPKEPrivateKey::derive(suite, data);
232 }
233
234 size_t
235 PseudoRandom::Generator::output_length() const
236 {
237 return suite.secret_size();
238 }
239
240 PseudoRandom::PseudoRandom(CipherSuite suite, const std::string& label)
241 : prg(suite, label)
242 {
243 }
244
245 ///
246 /// TreeMathTestVector
247 ///
248
249 // XXX(RLB): This is a hack to get the tests working in the right format. In
250 // reality, the tree math functions should be updated to be fallible.
251 std::optional<mlspp::NodeIndex>
252 TreeMathTestVector::null_if_invalid(NodeIndex input, NodeIndex answer) const
253 {
254 // For some invalid cases (e.g., leaf.left()), we currently return the node
255 // itself instead of null
256 if (input == answer) {
257 return std::nullopt;
258 }
259
260 // NodeIndex::parent is irrespective of tree size, so we might step out of the
261 // tree under consideration.
262 if (answer.val >= n_nodes.val) {
263 return std::nullopt;
264 }
265
266 return answer;
267 }
268
269 TreeMathTestVector::TreeMathTestVector(uint32_t n_leaves_in)
270 : n_leaves(n_leaves_in)
271 , n_nodes(n_leaves)
272 , root(NodeIndex::root(n_leaves))
273 , left(n_nodes.val)
274 , right(n_nodes.val)
275 , parent(n_nodes.val)
276 , sibling(n_nodes.val)
277 {
278 for (NodeIndex x{ 0 }; x.val < n_nodes.val; x.val++) {
279 left[x.val] = null_if_invalid(x, x.left());
280 right[x.val] = null_if_invalid(x, x.right());
281 parent[x.val] = null_if_invalid(x, x.parent());
282 sibling[x.val] = null_if_invalid(x, x.sibling());
283 }
284 }
285
286 std::optional<std::string>
287 TreeMathTestVector::verify() const
288 {
289 VERIFY_EQUAL("n_nodes", n_nodes, NodeCount(n_leaves));
290 VERIFY_EQUAL("root", root, NodeIndex::root(n_leaves));
291
292 for (NodeIndex x{ 0 }; x.val < n_nodes.val; x.val++) {
293 VERIFY_EQUAL("left", null_if_invalid(x, x.left()), left[x.val]);
294 VERIFY_EQUAL("right", null_if_invalid(x, x.right()), right[x.val]);
295 VERIFY_EQUAL("parent", null_if_invalid(x, x.parent()), parent[x.val]);
296 VERIFY_EQUAL("sibling", null_if_invalid(x, x.sibling()), sibling[x.val]);
297 }
298
299 return std::nullopt;
300 }
301
302 ///
303 /// TreeMathTestVector
304 ///
305
306 CryptoBasicsTestVector::RefHash::RefHash(CipherSuite suite,
307 const PseudoRandom::Generator& prg)
308 : label("RefHash")
309 , value(prg.secret("value"))
310 , out(suite.raw_ref(from_ascii(label), value))
311 {
312 }
313
314 std::optional<std::string>
315 CryptoBasicsTestVector::RefHash::verify(CipherSuite suite) const
316 {
317 VERIFY_EQUAL("ref hash", out, suite.raw_ref(from_ascii(label), value));
318 return std::nullopt;
319 }
320
321 CryptoBasicsTestVector::ExpandWithLabel::ExpandWithLabel(
322 CipherSuite suite,
323 const PseudoRandom::Generator& prg)
324 : secret(prg.secret("secret"))
325 , label("ExpandWithLabel")
326 , context(prg.secret("context"))
327 , length(static_cast<uint16_t>(prg.output_length()))
328 , out(suite.expand_with_label(secret, label, context, length))
329 {
330 }
331
332 std::optional<std::string>
333 CryptoBasicsTestVector::ExpandWithLabel::verify(CipherSuite suite) const
334 {
335 VERIFY_EQUAL("expand with label",
336 out,
337 suite.expand_with_label(secret, label, context, length));
338 return std::nullopt;
339 }
340
341 CryptoBasicsTestVector::DeriveSecret::DeriveSecret(
342 CipherSuite suite,
343 const PseudoRandom::Generator& prg)
344 : secret(prg.secret("secret"))
345 , label("DeriveSecret")
346 , out(suite.derive_secret(secret, label))
347 {
348 }
349
350 std::optional<std::string>
351 CryptoBasicsTestVector::DeriveSecret::verify(CipherSuite suite) const
352 {
353 VERIFY_EQUAL("derive secret", out, suite.derive_secret(secret, label));
354 return std::nullopt;
355 }
356
357 CryptoBasicsTestVector::DeriveTreeSecret::DeriveTreeSecret(
358 CipherSuite suite,
359 const PseudoRandom::Generator& prg)
360 : secret(prg.secret("secret"))
361 , label("DeriveTreeSecret")
362 , generation(prg.uint32("generation"))
363 , length(static_cast<uint16_t>(prg.output_length()))
364 , out(suite.derive_tree_secret(secret, label, generation, length))
365 {
366 }
367
368 std::optional<std::string>
369 CryptoBasicsTestVector::DeriveTreeSecret::verify(CipherSuite suite) const
370 {
371 VERIFY_EQUAL("derive tree secret",
372 out,
373 suite.derive_tree_secret(secret, label, generation, length));
374 return std::nullopt;
375 }
376
377 CryptoBasicsTestVector::SignWithLabel::SignWithLabel(
378 CipherSuite suite,
379 const PseudoRandom::Generator& prg)
380 : priv(prg.signature_key("priv"))
381 , pub(priv.public_key)
382 , content(prg.secret("content"))
383 , label("SignWithLabel")
384 , signature(priv.sign(suite, label, content))
385 {
386 }
387
388 std::optional<std::string>
389 CryptoBasicsTestVector::SignWithLabel::verify(CipherSuite suite) const
390 {
391 VERIFY("verify with label", pub.verify(suite, label, content, signature));
392
393 auto new_signature = priv.sign(suite, label, content);
394 VERIFY("sign with label", pub.verify(suite, label, content, new_signature));
395
396 return std::nullopt;
397 }
398
399 CryptoBasicsTestVector::EncryptWithLabel::EncryptWithLabel(
400 CipherSuite suite,
401 const PseudoRandom::Generator& prg)
402 : priv(prg.hpke_key("priv"))
403 , pub(priv.public_key)
404 , label("EncryptWithLabel")
405 , context(prg.secret("context"))
406 , plaintext(prg.secret("plaintext"))
407 {
408 auto ct = pub.encrypt(suite, label, context, plaintext);
409 kem_output = ct.kem_output;
410 ciphertext = ct.ciphertext;
411 }
412
413 std::optional<std::string>
414 CryptoBasicsTestVector::EncryptWithLabel::verify(CipherSuite suite) const
415 {
416 auto ct = HPKECiphertext{ kem_output, ciphertext };
417 auto pt = priv.decrypt(suite, label, context, ct);
418 VERIFY_EQUAL("decrypt with label", pt, plaintext);
419
420 auto new_ct = pub.encrypt(suite, label, context, plaintext);
421 auto new_pt = priv.decrypt(suite, label, context, new_ct);
422 VERIFY_EQUAL("encrypt with label", new_pt, plaintext);
423
424 return std::nullopt;
425 }
426
427 CryptoBasicsTestVector::CryptoBasicsTestVector(CipherSuite suite)
428 : PseudoRandom(suite, "crypto-basics")
429 , cipher_suite(suite)
430 , ref_hash(suite, prg.sub("ref_hash"))
431 , expand_with_label(suite, prg.sub("expand_with_label"))
432 , derive_secret(suite, prg.sub("derive_secret"))
433 , derive_tree_secret(suite, prg.sub("derive_tree_secret"))
434 , sign_with_label(suite, prg.sub("sign_with_label"))
435 , encrypt_with_label(suite, prg.sub("encrypt_with_label"))
436 {
437 }
438
439 std::optional<std::string>
440 CryptoBasicsTestVector::verify() const
441 {
442 auto result = ref_hash.verify(cipher_suite);
443 if (result) {
444 return result;
445 }
446
447 result = expand_with_label.verify(cipher_suite);
448 if (result) {
449 return result;
450 }
451
452 result = derive_secret.verify(cipher_suite);
453 if (result) {
454 return result;
455 }
456
457 result = derive_tree_secret.verify(cipher_suite);
458 if (result) {
459 return result;
460 }
461
462 result = sign_with_label.verify(cipher_suite);
463 if (result) {
464 return result;
465 }
466
467 result = encrypt_with_label.verify(cipher_suite);
468 if (result) {
469 return result;
470 }
471
472 return std::nullopt;
473 }
474
475 ///
476 /// SecretTreeTestVector
477 ///
478
479 SecretTreeTestVector::SenderData::SenderData(mlspp::CipherSuite suite,
480 const PseudoRandom::Generator& prg)
481 : sender_data_secret(prg.secret("sender_data_secret"))
482 , ciphertext(prg.secret("ciphertext"))
483 {
484 auto key_and_nonce =
485 KeyScheduleEpoch::sender_data_keys(suite, sender_data_secret, ciphertext);
486 key = key_and_nonce.key;
487 nonce = key_and_nonce.nonce;
488 }
489
490 std::optional<std::string>
491 SecretTreeTestVector::SenderData::verify(mlspp::CipherSuite suite) const
492 {
493 auto key_and_nonce =
494 KeyScheduleEpoch::sender_data_keys(suite, sender_data_secret, ciphertext);
495 VERIFY_EQUAL("sender data key", key, key_and_nonce.key);
496 VERIFY_EQUAL("sender data nonce", nonce, key_and_nonce.nonce);
497 return std::nullopt;
498 }
499
500 SecretTreeTestVector::SecretTreeTestVector(
501 mlspp::CipherSuite suite,
502 uint32_t n_leaves,
503 const std::vector<uint32_t>& generations)
504 : PseudoRandom(suite, "secret-tree")
505 , cipher_suite(suite)
506 , sender_data(suite, prg.sub("sender_data"))
507 , encryption_secret(prg.secret("encryption_secret"))
508 {
509 auto src =
510 GroupKeySource(cipher_suite, LeafCount{ n_leaves }, encryption_secret);
511 leaves.resize(n_leaves);
512 auto zero_reuse_guard = ReuseGuard{ 0, 0, 0, 0 };
513 for (uint32_t i = 0; i < n_leaves; i++) {
514 auto leaf = LeafIndex{ i };
515
516 for (const auto generation : generations) {
517 auto hs =
518 src.get(ContentType::proposal, leaf, generation, zero_reuse_guard);
519 auto app =
520 src.get(ContentType::application, leaf, generation, zero_reuse_guard);
521
522 leaves.at(i).push_back(
523 RatchetStep{ generation, hs.key, hs.nonce, app.key, app.nonce });
524
525 src.erase(ContentType::proposal, leaf, generation);
526 src.erase(ContentType::application, leaf, generation);
527 }
528 }
529 }
530
531 std::optional<std::string>
532 SecretTreeTestVector::verify() const
533 {
534 auto sender_data_error = sender_data.verify(cipher_suite);
535 if (sender_data_error) {
536 return sender_data_error;
537 }
538
539 auto n_leaves = static_cast<uint32_t>(leaves.size());
540 auto src =
541 GroupKeySource(cipher_suite, LeafCount{ n_leaves }, encryption_secret);
542 auto zero_reuse_guard = ReuseGuard{ 0, 0, 0, 0 };
543 for (uint32_t i = 0; i < n_leaves; i++) {
544 auto leaf = LeafIndex{ i };
545
546 for (const auto& step : leaves[i]) {
547 auto generation = step.generation;
548
549 auto hs =
550 src.get(ContentType::proposal, leaf, generation, zero_reuse_guard);
551 VERIFY_EQUAL("hs key", hs.key, step.handshake_key);
552 VERIFY_EQUAL("hs nonce", hs.nonce, step.handshake_nonce);
553
554 auto app =
555 src.get(ContentType::application, leaf, generation, zero_reuse_guard);
556 VERIFY_EQUAL("app key", app.key, step.application_key);
557 VERIFY_EQUAL("app nonce", app.nonce, step.application_nonce);
558 }
559 }
560
561 return std::nullopt;
562 }
563
564 ///
565 /// KeyScheduleTestVector
566 ///
567
568 KeyScheduleTestVector::KeyScheduleTestVector(CipherSuite suite,
569 uint32_t n_epochs)
570 : PseudoRandom(suite, "key-schedule")
571 , cipher_suite(suite)
572 , group_id(prg.secret("group_id"))
573 , initial_init_secret(prg.secret("group_id"))
574 {
575 auto group_context = GroupContext{ suite, group_id, 0, {}, {}, {} };
576 auto epoch = KeyScheduleEpoch(cipher_suite);
577 epoch.init_secret = initial_init_secret;
578
579 for (uint64_t i = 0; i < n_epochs; i++) {
580 auto epoch_prg = prg.sub(to_hex(tls::marshal(i)));
581
582 group_context.tree_hash = epoch_prg.secret("tree_hash");
583 group_context.confirmed_transcript_hash =
584 epoch_prg.secret("confirmed_transcript_hash");
585 auto ctx = tls::marshal(group_context);
586
587 // TODO(RLB) Add Test case for externally-driven epoch change
588 auto commit_secret = epoch_prg.secret("commit_secret");
589 auto psk_secret = epoch_prg.secret("psk_secret");
590 epoch = epoch.next_raw(commit_secret, psk_secret, std::nullopt, ctx);
591
592 auto welcome_secret = KeyScheduleEpoch::welcome_secret_raw(
593 cipher_suite, epoch.joiner_secret, psk_secret);
594
595 auto exporter_prg = epoch_prg.sub("exporter");
596 auto exporter_label = to_hex(exporter_prg.secret("label"));
597 auto exporter_context = exporter_prg.secret("context");
598 auto exporter_length = cipher_suite.secret_size();
599 auto exported =
600 epoch.do_export(exporter_label, exporter_context, exporter_length);
601
602 epochs.push_back({ group_context.tree_hash,
603 commit_secret,
604 psk_secret,
605 group_context.confirmed_transcript_hash,
606
607 ctx,
608
609 epoch.joiner_secret,
610 welcome_secret,
611 epoch.init_secret,
612
613 epoch.sender_data_secret,
614 epoch.encryption_secret,
615 epoch.exporter_secret,
616 epoch.epoch_authenticator,
617 epoch.external_secret,
618 epoch.confirmation_key,
619 epoch.membership_key,
620 epoch.resumption_psk,
621
622 epoch.external_priv.public_key,
623
624 {
625 exporter_label,
626 exporter_context,
627 exporter_length,
628 exported,
629 } });
630
631 group_context.epoch += 1;
632 }
633 }
634
635 std::optional<std::string>
636 KeyScheduleTestVector::verify() const
637 {
638 auto group_context = GroupContext{ cipher_suite, group_id, 0, {}, {}, {} };
639 auto epoch = KeyScheduleEpoch(cipher_suite);
640 epoch.init_secret = initial_init_secret;
641
642 for (const auto& tve : epochs) {
643 group_context.tree_hash = tve.tree_hash;
644 group_context.confirmed_transcript_hash = tve.confirmed_transcript_hash;
645 auto ctx = tls::marshal(group_context);
646 VERIFY_EQUAL("group context", ctx, tve.group_context);
647
648 epoch =
649 epoch.next_raw(tve.commit_secret, tve.psk_secret, std::nullopt, ctx);
650
651 // Verify the rest of the epoch
652 VERIFY_EQUAL("joiner secret", epoch.joiner_secret, tve.joiner_secret);
653
654 auto welcome_secret = KeyScheduleEpoch::welcome_secret_raw(
655 cipher_suite, tve.joiner_secret, tve.psk_secret);
656 VERIFY_EQUAL("welcome secret", welcome_secret, tve.welcome_secret);
657
658 VERIFY_EQUAL(
659 "sender data secret", epoch.sender_data_secret, tve.sender_data_secret);
660 VERIFY_EQUAL(
661 "encryption secret", epoch.encryption_secret, tve.encryption_secret);
662 VERIFY_EQUAL("exporter secret", epoch.exporter_secret, tve.exporter_secret);
663 VERIFY_EQUAL("epoch authenticator",
664 epoch.epoch_authenticator,
665 tve.epoch_authenticator);
666 VERIFY_EQUAL("external secret", epoch.external_secret, tve.external_secret);
667 VERIFY_EQUAL(
668 "confirmation key", epoch.confirmation_key, tve.confirmation_key);
669 VERIFY_EQUAL("membership key", epoch.membership_key, tve.membership_key);
670 VERIFY_EQUAL("resumption psk", epoch.resumption_psk, tve.resumption_psk);
671 VERIFY_EQUAL("init secret", epoch.init_secret, tve.init_secret);
672
673 VERIFY_EQUAL(
674 "external pub", epoch.external_priv.public_key, tve.external_pub);
675
676 auto exported = epoch.do_export(
677 tve.exporter.label, tve.exporter.context, tve.exporter.length);
678 VERIFY_EQUAL("exported", exported, tve.exporter.secret);
679
680 group_context.epoch += 1;
681 }
682
683 return std::nullopt;
684 }
685
686 ///
687 /// MessageProtectionTestVector
688 ///
689
690 MessageProtectionTestVector::MessageProtectionTestVector(CipherSuite suite)
691 : PseudoRandom(suite, "message-protection")
692 , cipher_suite(suite)
693 , group_id(prg.secret("group_id"))
694 , epoch(prg.uint64("epoch"))
695 , tree_hash(prg.secret("tree_hash"))
696 , confirmed_transcript_hash(prg.secret("confirmed_transcript_hash"))
697 , signature_priv(prg.signature_key("signature_priv"))
698 , signature_pub(signature_priv.public_key)
699 , encryption_secret(prg.secret("encryption_secret"))
700 , sender_data_secret(prg.secret("sender_data_secret"))
701 , membership_key(prg.secret("membership_key"))
702 , proposal{ GroupContextExtensions{} }
703 , commit{ /* XXX(RLB) this is technically invalid, empty w/o path */ }
704 , application{ prg.secret("application") }
705 {
706 proposal_pub = protect_pub(proposal);
707 proposal_priv = protect_priv(proposal);
708
709 commit_pub = protect_pub(commit);
710 commit_priv = protect_priv(commit);
711
712 application_priv = protect_priv(ApplicationData{ application });
713 }
714
715 std::optional<std::string>
716 MessageProtectionTestVector::verify()
717 {
718 // Initialize fields that don't get set from JSON
719 prg = PseudoRandom::Generator(cipher_suite, "message-protection");
720 signature_priv.set_public_key(cipher_suite);
721
722 // Sanity check the key pairs
723 VERIFY_EQUAL("sig kp", signature_priv.public_key, signature_pub);
724
725 // Verify proposal unprotect as PublicMessage
726 auto proposal_pub_unprotected = unprotect(proposal_pub);
727 VERIFY("proposal pub unprotect auth", proposal_pub_unprotected);
728 VERIFY_EQUAL("proposal pub unprotect",
729 opt::get(proposal_pub_unprotected).content,
730 proposal);
731
732 // Verify proposal unprotect as PrivateMessage
733 auto proposal_priv_unprotected = unprotect(proposal_priv);
734 VERIFY("proposal priv unprotect auth", proposal_priv_unprotected);
735 VERIFY_EQUAL("proposal priv unprotect",
736 opt::get(proposal_priv_unprotected).content,
737 proposal);
738
739 // Verify commit unprotect as PublicMessage
740 auto commit_pub_unprotected = unprotect(commit_pub);
741 VERIFY("commit pub unprotect auth", commit_pub_unprotected);
742 VERIFY_EQUAL(
743 "commit pub unprotect", opt::get(commit_pub_unprotected).content, commit);
744
745 // Verify commit unprotect as PrivateMessage
746 auto commit_priv_unprotected = unprotect(commit_priv);
747 VERIFY("commit priv unprotect auth", commit_priv_unprotected);
748 VERIFY_EQUAL(
749 "commit priv unprotect", opt::get(commit_priv_unprotected).content, commit);
750
751 // Verify application data unprotect as PrivateMessage
752 auto app_unprotected = unprotect(application_priv);
753 VERIFY("app priv unprotect auth", app_unprotected);
754 VERIFY_EQUAL("app priv unprotect",
755 opt::get(app_unprotected).content,
756 ApplicationData{ application });
757
758 // Verify protect/unprotect round-trips
759 // XXX(RLB): Note that because (a) unprotect() deletes keys from the ratchet
760 // and (b) we are using the same ratchet to send and receive, we need to do
761 // these round-trip tests after all the unprotect tests are done. Otherwise
762 // the protect() calls here will re-use generations used the test vector, and
763 // then unprotect() will delete the keys, then when you go to decrypt the test
764 // vector object, you'll get "expired key". It might be good to have better
765 // safeguards around such reuse.
766 auto proposal_pub_protected = protect_pub(proposal);
767 auto proposal_pub_protected_unprotected = unprotect(proposal_pub_protected);
768 VERIFY("proposal pub protect/unprotect auth",
769 proposal_pub_protected_unprotected);
770 VERIFY_EQUAL("proposal pub protect/unprotect",
771 opt::get(proposal_pub_protected_unprotected).content,
772 proposal);
773
774 auto proposal_priv_protected = protect_priv(proposal);
775 auto proposal_priv_protected_unprotected = unprotect(proposal_priv_protected);
776 VERIFY("proposal priv protect/unprotect auth",
777 proposal_priv_protected_unprotected);
778 VERIFY_EQUAL("proposal priv protect/unprotect",
779 opt::get(proposal_priv_protected_unprotected).content,
780 proposal);
781
782 auto commit_pub_protected = protect_pub(commit);
783 auto commit_pub_protected_unprotected = unprotect(commit_pub_protected);
784 VERIFY("commit pub protect/unprotect auth", commit_pub_protected_unprotected);
785 VERIFY_EQUAL("commit pub protect/unprotect",
786 opt::get(commit_pub_protected_unprotected).content,
787 commit);
788
789 auto commit_priv_protected = protect_priv(commit);
790 auto commit_priv_protected_unprotected = unprotect(commit_priv_protected);
791 VERIFY("commit priv protect/unprotect auth",
792 commit_priv_protected_unprotected);
793 VERIFY_EQUAL("commit priv protect/unprotect",
794 opt::get(commit_priv_protected_unprotected).content,
795 commit);
796
797 auto app_protected = protect_priv(ApplicationData{ application });
798 auto app_protected_unprotected = unprotect(app_protected);
799 VERIFY("app priv protect/unprotect auth", app_protected_unprotected);
800 VERIFY_EQUAL("app priv protect/unprotect",
801 opt::get(app_protected_unprotected).content,
802 ApplicationData{ application });
803
804 return std::nullopt;
805 }
806
807 GroupKeySource
808 MessageProtectionTestVector::group_keys() const
809 {
810 return { cipher_suite, LeafCount{ 2 }, encryption_secret };
811 }
812
813 GroupContext
814 MessageProtectionTestVector::group_context() const
815 {
816 return GroupContext{
817 cipher_suite, group_id, epoch, tree_hash, confirmed_transcript_hash, {}
818 };
819 }
820
821 MLSMessage
822 MessageProtectionTestVector::protect_pub(
823 const mlspp::GroupContent::RawContent& raw_content) const
824 {
825 auto sender = Sender{ MemberSender{ LeafIndex{ 1 } } };
826 auto authenticated_data = bytes{};
827
828 auto content =
829 GroupContent{ group_id, epoch, sender, authenticated_data, raw_content };
830
831 auto auth_content = AuthenticatedContent::sign(WireFormat::mls_public_message,
832 content,
833 cipher_suite,
834 signature_priv,
835 group_context());
836 if (content.content_type() == ContentType::commit) {
837 auto confirmation_tag = prg.secret("confirmation_tag");
838 auth_content.set_confirmation_tag(confirmation_tag);
839 }
840
841 return PublicMessage::protect(
842 auth_content, cipher_suite, membership_key, group_context());
843 }
844
845 MLSMessage
846 MessageProtectionTestVector::protect_priv(
847 const mlspp::GroupContent::RawContent& raw_content)
848 {
849 auto sender = Sender{ MemberSender{ LeafIndex{ 1 } } };
850 auto authenticated_data = bytes{};
851 auto padding_size = size_t(0);
852
853 auto content =
854 GroupContent{ group_id, epoch, sender, authenticated_data, raw_content };
855
856 auto auth_content =
857 AuthenticatedContent::sign(WireFormat::mls_private_message,
858 content,
859 cipher_suite,
860 signature_priv,
861 group_context());
862 if (content.content_type() == ContentType::commit) {
863 auto confirmation_tag = prg.secret("confirmation_tag");
864 auth_content.set_confirmation_tag(confirmation_tag);
865 }
866
867 auto keys = group_keys();
868 return PrivateMessage::protect(
869 auth_content, cipher_suite, keys, sender_data_secret, padding_size);
870 }
871
872 std::optional<GroupContent>
873 MessageProtectionTestVector::unprotect(const MLSMessage& message)
874 {
875 auto do_unprotect =
876 overloaded{ [&](const PublicMessage& pt) {
877 return pt.unprotect(
878 cipher_suite, membership_key, group_context());
879 },
880 [&](const PrivateMessage& ct) {
881 auto keys = group_keys();
882 return ct.unprotect(cipher_suite, keys, sender_data_secret);
883 },
884 [](const auto& /* other */) -> std::optional<ValidatedContent> {
885 return std::nullopt;
886 } };
887
888 auto maybe_auth_content = var::visit(do_unprotect, message.message);
889 if (!maybe_auth_content) {
890 return std::nullopt;
891 }
892
893 auto val_content = opt::get(maybe_auth_content);
894 const auto& auth_content = val_content.authenticated_content();
895 if (!auth_content.verify(cipher_suite, signature_pub, group_context())) {
896 return std::nullopt;
897 }
898
899 return auth_content.content;
900 }
901
902 ///
903 /// PSKTestVector
904 ///
905 static std::vector<PSKWithSecret>
906 to_psk_w_secret(const std::vector<PSKSecretTestVector::PSK>& psks)
907 {
908 auto pskws = std::vector<PSKWithSecret>(psks.size());
909 std::transform(
910 std::begin(psks), std::end(psks), std::begin(pskws), [](const auto& psk) {
911 auto ext_id = ExternalPSK{ psk.psk_id };
912 auto id = PreSharedKeyID{ ext_id, psk.psk_nonce };
913 return PSKWithSecret{ id, psk.psk };
914 });
915
916 return pskws;
917 }
918
919 PSKSecretTestVector::PSKSecretTestVector(mlspp::CipherSuite suite,
920 size_t n_psks)
921 : PseudoRandom(suite, "psk_secret")
922 , cipher_suite(suite)
923 , psks(n_psks)
924 {
925 uint32_t i = 0;
926 for (auto& psk : psks) {
927 auto ix = to_hex(tls::marshal(i));
928 i += 1;
929
930 psk.psk_id = prg.secret("psk_id" + ix);
931 psk.psk_nonce = prg.secret("psk_nonce" + ix);
932 psk.psk = prg.secret("psk" + ix);
933 }
934
935 psk_secret =
936 KeyScheduleEpoch::make_psk_secret(cipher_suite, to_psk_w_secret(psks));
937 }
938
939 std::optional<std::string>
940 PSKSecretTestVector::verify() const
941 {
942 auto actual =
943 KeyScheduleEpoch::make_psk_secret(cipher_suite, to_psk_w_secret(psks));
944 VERIFY_EQUAL("psk secret", actual, psk_secret);
945
946 return std::nullopt;
947 }
948
949 ///
950 /// TranscriptTestVector
951 ///
952 TranscriptTestVector::TranscriptTestVector(CipherSuite suite)
953 : PseudoRandom(suite, "transcript")
954 , cipher_suite(suite)
955 , interim_transcript_hash_before(prg.secret("interim_transcript_hash_before"))
956 {
957 auto transcript = TranscriptHash(suite);
958 transcript.interim = interim_transcript_hash_before;
959
960 auto group_id = prg.secret("group_id");
961 auto epoch = prg.uint64("epoch");
962 auto group_context_obj =
963 GroupContext{ suite,
964 group_id,
965 epoch,
966 prg.secret("tree_hash_before"),
967 prg.secret("confirmed_transcript_hash_before"),
968 {} };
969 auto group_context = tls::marshal(group_context_obj);
970
971 auto init_secret = prg.secret("init_secret");
972 auto ks_epoch = KeyScheduleEpoch(suite, init_secret, group_context);
973
974 auto sig_priv = prg.signature_key("sig_priv");
975 auto leaf_index = LeafIndex{ 0 };
976
977 authenticated_content = AuthenticatedContent::sign(
978 WireFormat::mls_public_message,
979 GroupContent{
980 group_id, epoch, { MemberSender{ leaf_index } }, {}, Commit{} },
981 suite,
982 sig_priv,
983 group_context_obj);
984
985 transcript.update_confirmed(authenticated_content);
986
987 const auto confirmation_tag = ks_epoch.confirmation_tag(transcript.confirmed);
988 authenticated_content.set_confirmation_tag(confirmation_tag);
989
990 transcript.update_interim(authenticated_content);
991
992 // Store the required data
993 confirmation_key = ks_epoch.confirmation_key;
994 confirmed_transcript_hash_after = transcript.confirmed;
995 interim_transcript_hash_after = transcript.interim;
996 }
997
998 std::optional<std::string>
999 TranscriptTestVector::verify() const
1000 {
1001 auto transcript = TranscriptHash(cipher_suite);
1002 transcript.interim = interim_transcript_hash_before;
1003
1004 transcript.update(authenticated_content);
1005 VERIFY_EQUAL(
1006 "confirmed", transcript.confirmed, confirmed_transcript_hash_after);
1007 VERIFY_EQUAL("interim", transcript.interim, interim_transcript_hash_after);
1008
1009 auto confirmation_tag =
1010 cipher_suite.digest().hmac(confirmation_key, transcript.confirmed);
1011 VERIFY_EQUAL("confirmation tag",
1012 confirmation_tag,
1013 authenticated_content.auth.confirmation_tag);
1014
1015 return std::nullopt;
1016 }
1017
1018 ///
1019 /// WelcomeTestVector
1020 ///
1021 WelcomeTestVector::WelcomeTestVector(CipherSuite suite)
1022 : PseudoRandom(suite, "welcome")
1023 , cipher_suite(suite)
1024 , init_priv(prg.hpke_key("init_priv"))
1025 {
1026 auto joiner_secret = prg.secret("joiner_secret");
1027 auto group_id = prg.secret("group_id");
1028 auto epoch = epoch_t(prg.uint64("epoch"));
1029 auto tree_hash = prg.secret("tree_hash");
1030 auto confirmed_transcript_hash = prg.secret("confirmed_transcript_hash");
1031 auto enc_priv = prg.hpke_key("enc_priv");
1032 auto sig_priv = prg.signature_key("sig_priv");
1033 auto cred = Credential::basic(prg.secret("identity"));
1034
1035 auto signer_index = LeafIndex{ prg.uint32("signer") };
1036 auto signer_priv = prg.signature_key("signer_priv");
1037 signer_pub = signer_priv.public_key;
1038
1039 auto leaf_node = LeafNode{
1040 cipher_suite,
1041 enc_priv.public_key,
1042 sig_priv.public_key,
1043 cred,
1044 Capabilities::create_default(),
1045 Lifetime::create_default(),
1046 {},
1047 sig_priv,
1048 };
1049 auto key_package_obj = KeyPackage{
1050 cipher_suite, init_priv.public_key, leaf_node, {}, sig_priv,
1051 };
1052 key_package = key_package_obj;
1053
1054 auto group_context = GroupContext{
1055 cipher_suite, group_id, epoch, tree_hash, confirmed_transcript_hash, {}
1056 };
1057
1058 auto key_schedule = KeyScheduleEpoch::joiner(
1059 cipher_suite, joiner_secret, {}, tls::marshal(group_context));
1060 auto confirmation_tag =
1061 key_schedule.confirmation_tag(confirmed_transcript_hash);
1062
1063 auto group_info = GroupInfo{
1064 group_context,
1065 {},
1066 confirmation_tag,
1067 };
1068 group_info.sign(signer_index, signer_priv);
1069
1070 auto welcome_obj = Welcome(cipher_suite, joiner_secret, {}, group_info);
1071 welcome_obj.encrypt(key_package_obj, std::nullopt);
1072 welcome = welcome_obj;
1073 }
1074
1075 std::optional<std::string>
1076 WelcomeTestVector::verify() const
1077 {
1078 VERIFY_EQUAL(
1079 "kp format", key_package.wire_format(), WireFormat::mls_key_package);
1080 VERIFY_EQUAL(
1081 "welcome format", welcome.wire_format(), WireFormat::mls_welcome);
1082
1083 const auto& key_package_obj = var::get<KeyPackage>(key_package.message);
1084 const auto& welcome_obj = var::get<Welcome>(welcome.message);
1085
1086 VERIFY_EQUAL("kp suite", key_package_obj.cipher_suite, cipher_suite);
1087 VERIFY_EQUAL("welcome suite", welcome_obj.cipher_suite, cipher_suite);
1088
1089 auto maybe_kpi = welcome_obj.find(key_package_obj);
1090 VERIFY("found key package", maybe_kpi);
1091
1092 auto kpi = opt::get(maybe_kpi);
1093 auto group_secrets = welcome_obj.decrypt_secrets(kpi, init_priv);
1094 auto group_info = welcome_obj.decrypt(group_secrets.joiner_secret, {});
1095
1096 // Verify signature on GroupInfo
1097 VERIFY("group info verify", group_info.verify(signer_pub));
1098
1099 // Verify confirmation tag
1100 const auto& group_context = group_info.group_context;
1101 auto key_schedule = KeyScheduleEpoch::joiner(
1102 cipher_suite, group_secrets.joiner_secret, {}, tls::marshal(group_context));
1103 auto confirmation_tag =
1104 key_schedule.confirmation_tag(group_context.confirmed_transcript_hash);
1105
1106 return std::nullopt;
1107 }
1108
1109 ///
1110 /// TreeTestCase
1111 ///
1112
1113 std::array<TreeStructure, 14> all_tree_structures{
1114 TreeStructure::full_tree_2,
1115 TreeStructure::full_tree_3,
1116 TreeStructure::full_tree_4,
1117 TreeStructure::full_tree_5,
1118 TreeStructure::full_tree_6,
1119 TreeStructure::full_tree_7,
1120 TreeStructure::full_tree_8,
1121 TreeStructure::full_tree_32,
1122 TreeStructure::full_tree_33,
1123 TreeStructure::full_tree_34,
1124 TreeStructure::internal_blanks_no_skipping,
1125 TreeStructure::internal_blanks_with_skipping,
1126 TreeStructure::unmerged_leaves_no_skipping,
1127 TreeStructure::unmerged_leaves_with_skipping,
1128 };
1129
1130 std::array<TreeStructure, 11> treekem_test_tree_structures{
1131 // All cases except the big ones
1132 TreeStructure::full_tree_2,
1133 TreeStructure::full_tree_3,
1134 TreeStructure::full_tree_4,
1135 TreeStructure::full_tree_5,
1136 TreeStructure::full_tree_6,
1137 TreeStructure::full_tree_7,
1138 TreeStructure::full_tree_8,
1139 TreeStructure::internal_blanks_no_skipping,
1140 TreeStructure::internal_blanks_with_skipping,
1141 TreeStructure::unmerged_leaves_no_skipping,
1142 TreeStructure::unmerged_leaves_with_skipping,
1143 };
1144
1145 struct TreeTestCase
1146 {
1147 CipherSuite suite;
1148 PseudoRandom::Generator prg;
1149
1150 bytes group_id;
1151 uint32_t leaf_counter = 0;
1152 uint32_t path_counter = 0;
1153
1154 struct PrivateState
1155 {
1156 SignaturePrivateKey sig_priv;
1157 TreeKEMPrivateKey priv;
1158 std::vector<LeafIndex> senders;
1159 };
1160
1161 std::map<LeafIndex, PrivateState> privs;
1162 TreeKEMPublicKey pub;
1163
1164 TreeTestCase(CipherSuite suite_in, PseudoRandom::Generator prg_in)
1165 : suite(suite_in)
1166 , prg(std::move(prg_in))
1167 , group_id(prg.secret("group_id"))
1168 , pub(suite)
1169 {
1170 auto [where, enc_priv, sig_priv] = add_leaf();
1171 auto tree_priv = TreeKEMPrivateKey::solo(suite, where, enc_priv);
1172 auto priv_state = PrivateState{ sig_priv, tree_priv, { LeafIndex{ 0 } } };
1173 privs.insert_or_assign(where, priv_state);
1174 }
1175
1176 std::tuple<LeafIndex, HPKEPrivateKey, SignaturePrivateKey> add_leaf()
1177 {
1178 leaf_counter += 1;
1179 auto ix = to_hex(tls::marshal(leaf_counter));
1180 auto enc_priv = prg.hpke_key("encryption_key" + ix);
1181 auto sig_priv = prg.signature_key("signature_key" + ix);
1182 auto identity = prg.secret("identity" + ix);
1183
1184 auto credential = Credential::basic(identity);
1185 auto leaf_node = LeafNode{ suite,
1186 enc_priv.public_key,
1187 sig_priv.public_key,
1188 credential,
1189 Capabilities::create_default(),
1190 Lifetime::create_default(),
1191 {},
1192 sig_priv };
1193 auto where = pub.add_leaf(leaf_node);
1194 pub.set_hash_all();
1195 return { where, enc_priv, sig_priv };
1196 }
1197
1198 void commit(LeafIndex from,
1199 const std::vector<LeafIndex>& remove,
1200 bool add,
1201 std::optional<bytes> maybe_context)
1202 {
1203 // Remove members from the tree
1204 for (auto i : remove) {
1205 pub.blank_path(i);
1206 privs.erase(i);
1207 }
1208 pub.set_hash_all();
1209
1210 auto joiner = std::vector<LeafIndex>{};
1211 auto maybe_enc_priv = std::optional<HPKEPrivateKey>{};
1212 auto maybe_sig_priv = std::optional<SignaturePrivateKey>{};
1213 if (add) {
1214 auto [where, enc_priv, sig_priv] = add_leaf();
1215 joiner.push_back(where);
1216 maybe_enc_priv = enc_priv;
1217 maybe_sig_priv = sig_priv;
1218 }
1219
1220 auto path_secret = std::optional<bytes>{};
1221 if (maybe_context) {
1222 // Create an UpdatePath
1223 path_counter += 1;
1224 auto ix = to_hex(tls::marshal(path_counter));
1225 auto leaf_secret = prg.secret("leaf_secret" + ix);
1226 auto priv = privs.at(from);
1227
1228 auto context = opt::get(maybe_context);
1229 auto pub_before = pub;
1230 auto sender_priv =
1231 pub.update(from, leaf_secret, group_id, priv.sig_priv, {});
1232 auto path = pub.encap(sender_priv, context, joiner);
1233
1234 // Process the UpdatePath at all the members
1235 for (auto& pair : privs) {
1236 // XXX(RLB): It might seem like this could be done with a simple
1237 // destructuring assignment, either here or in the `for` clause above.
1238 // However, either of these options cause clang-tidy to segfault when
1239 // evaulating the "bugprone-unchecked-optional-access" lint.
1240 const auto& leaf = pair.first;
1241 auto& priv_state = pair.second;
1242 if (leaf == from) {
1243 priv_state =
1244 PrivateState{ priv_state.sig_priv, sender_priv, { from } };
1245 continue;
1246 }
1247
1248 priv_state.priv.decap(from, pub_before, context, path, joiner);
1249 priv_state.senders.push_back(from);
1250 }
1251
1252 // Look up the path secret for the joiner
1253 if (!joiner.empty()) {
1254 auto index = joiner.front();
1255 auto [overlap, shared_path_secret, ok] =
1256 sender_priv.shared_path_secret(index);
1257 silence_unused(overlap);
1258 silence_unused(ok);
1259
1260 path_secret = shared_path_secret;
1261 }
1262 }
1263
1264 // Add a private entry for the joiner if we added someone
1265 if (!joiner.empty()) {
1266 auto index = joiner.front();
1267 auto ancestor = index.ancestor(from);
1268 auto enc_priv = opt::get(maybe_enc_priv);
1269 auto sig_priv = opt::get(maybe_sig_priv);
1270 auto tree_priv =
1271 TreeKEMPrivateKey::joiner(pub, index, enc_priv, ancestor, path_secret);
1272 privs.insert_or_assign(index,
1273 PrivateState{ sig_priv, tree_priv, { from } });
1274 }
1275 }
1276
1277 static TreeTestCase full(CipherSuite suite,
1278 const PseudoRandom::Generator& prg,
1279 LeafCount leaves,
1280 const std::string& label)
1281 {
1282 auto tc = TreeTestCase{ suite, prg.sub(label) };
1283
1284 for (LeafIndex i{ 0 }; i.val < leaves.val - 1; i.val++) {
1285 tc.commit(
1286 i, {}, true, tc.prg.secret("context" + to_hex(tls::marshal(i))));
1287 }
1288
1289 return tc;
1290 }
1291
1292 static TreeTestCase with_structure(CipherSuite suite,
1293 const PseudoRandom::Generator& prg,
1294 TreeStructure tree_structure)
1295 {
1296 switch (tree_structure) {
1297 case TreeStructure::full_tree_2:
1298 return full(suite, prg, LeafCount{ 2 }, "full_tree_2");
1299
1300 case TreeStructure::full_tree_3:
1301 return full(suite, prg, LeafCount{ 3 }, "full_tree_3");
1302
1303 case TreeStructure::full_tree_4:
1304 return full(suite, prg, LeafCount{ 4 }, "full_tree_4");
1305
1306 case TreeStructure::full_tree_5:
1307 return full(suite, prg, LeafCount{ 5 }, "full_tree_5");
1308
1309 case TreeStructure::full_tree_6:
1310 return full(suite, prg, LeafCount{ 6 }, "full_tree_6");
1311
1312 case TreeStructure::full_tree_7:
1313 return full(suite, prg, LeafCount{ 7 }, "full_tree_7");
1314
1315 case TreeStructure::full_tree_8:
1316 return full(suite, prg, LeafCount{ 8 }, "full_tree_8");
1317
1318 case TreeStructure::full_tree_32:
1319 return full(suite, prg, LeafCount{ 32 }, "full_tree_32");
1320
1321 case TreeStructure::full_tree_33:
1322 return full(suite, prg, LeafCount{ 33 }, "full_tree_33");
1323
1324 case TreeStructure::full_tree_34:
1325 return full(suite, prg, LeafCount{ 34 }, "full_tree_34");
1326
1327 case TreeStructure::internal_blanks_no_skipping: {
1328 auto tc = TreeTestCase::full(
1329 suite, prg, LeafCount{ 8 }, "internal_blanks_no_skipping");
1330 auto context = tc.prg.secret("context");
1331 tc.commit(
1332 LeafIndex{ 0 }, { LeafIndex{ 2 }, LeafIndex{ 3 } }, true, context);
1333 return tc;
1334 }
1335
1336 case TreeStructure::internal_blanks_with_skipping: {
1337 auto tc = TreeTestCase::full(
1338 suite, prg, LeafCount{ 8 }, "internal_blanks_with_skipping");
1339 auto context = tc.prg.secret("context");
1340 tc.commit(LeafIndex{ 0 },
1341 { LeafIndex{ 1 }, LeafIndex{ 2 }, LeafIndex{ 3 } },
1342 false,
1343 context);
1344 return tc;
1345 }
1346
1347 case TreeStructure::unmerged_leaves_no_skipping: {
1348 auto tc = TreeTestCase::full(
1349 suite, prg, LeafCount{ 7 }, "unmerged_leaves_no_skipping");
1350 auto context = tc.prg.secret("context");
1351 tc.commit(LeafIndex{ 0 }, {}, true, std::nullopt);
1352 return tc;
1353 }
1354
1355 case TreeStructure::unmerged_leaves_with_skipping: {
1356 auto tc = TreeTestCase::full(
1357 suite, prg, LeafCount{ 1 }, "unmerged_leaves_with_skipping");
1358
1359 // 0 adds 1..6
1360 tc.commit(LeafIndex{ 0 }, {}, true, std::nullopt);
1361 tc.commit(LeafIndex{ 0 }, {}, true, std::nullopt);
1362 tc.commit(LeafIndex{ 0 }, {}, true, std::nullopt);
1363 tc.commit(LeafIndex{ 0 }, {}, true, std::nullopt);
1364 tc.commit(LeafIndex{ 0 }, {}, true, std::nullopt);
1365 tc.commit(LeafIndex{ 0 }, {}, true, std::nullopt);
1366
1367 // 0 reemoves 5
1368 tc.commit(LeafIndex{ 0 },
1369 { LeafIndex{ 5 } },
1370 false,
1371 tc.prg.secret("context_remove5"));
1372
1373 // 4 commits without any proupposals
1374 tc.commit(LeafIndex{ 4 }, {}, false, tc.prg.secret("context_update4"));
1375
1376 // 0 adds a new member
1377 tc.commit(LeafIndex{ 0 }, {}, true, std::nullopt);
1378
1379 return tc;
1380 }
1381
1382 default:
1383 throw InvalidParameterError("Unsupported tree structure");
1384 }
1385 }
1386 };
1387
1388 ///
1389 /// TreeHashTestVector
1390 ///
1391 TreeHashTestVector::TreeHashTestVector(mlspp::CipherSuite suite,
1392 TreeStructure tree_structure)
1393 : PseudoRandom(suite, "tree-hashes")
1394 , cipher_suite(suite)
1395 {
1396 auto tc = TreeTestCase::with_structure(suite, prg, tree_structure);
1397 tree = tc.pub;
1398 group_id = tc.group_id;
1399
1400 auto width = NodeCount(tree.size);
1401 for (NodeIndex i{ 0 }; i < width; i.val++) {
1402 tree_hashes.push_back(tree.get_hash(i));
1403 resolutions.push_back(tree.resolve(i));
1404 }
1405 }
1406
1407 std::optional<std::string>
1408 TreeHashTestVector::verify()
1409 {
1410 // Finish setting up the tree
1411 tree.suite = cipher_suite;
1412 tree.set_hash_all();
1413
1414 // Verify that each leaf node is properly signed
1415 for (LeafIndex i{ 0 }; i < tree.size; i.val++) {
1416 auto maybe_leaf = tree.leaf_node(i);
1417 if (!maybe_leaf) {
1418 continue;
1419 }
1420
1421 auto leaf = opt::get(maybe_leaf);
1422 auto leaf_valid = leaf.verify(cipher_suite, { { group_id, i } });
1423 VERIFY("leaf sig valid", leaf_valid);
1424 }
1425
1426 // Verify the tree hashes
1427 auto width = NodeCount{ tree.size };
1428 for (NodeIndex i{ 0 }; i < width; i.val++) {
1429 VERIFY_EQUAL("tree hash", tree.get_hash(i), tree_hashes.at(i.val));
1430 VERIFY_EQUAL("resolution", tree.resolve(i), resolutions.at(i.val));
1431 }
1432
1433 // Verify parent hashes
1434 VERIFY("parent hash valid", tree.parent_hash_valid());
1435
1436 // Verify the resolutions
1437 for (NodeIndex i{ 0 }; i < width; i.val++) {
1438 VERIFY_EQUAL("resolution", tree.resolve(i), resolutions[i.val]);
1439 }
1440
1441 return std::nullopt;
1442 }
1443
1444 ///
1445 /// TreeOperationsTestVector
1446 ///
1447
1448 const std::vector<TreeOperationsTestVector::Scenario>
1449 TreeOperationsTestVector::all_scenarios{
1450 Scenario::add_right_edge, Scenario::add_internal, Scenario::update,
1451 Scenario::remove_right_edge, Scenario::remove_internal,
1452 };
1453
1454 TreeOperationsTestVector::TreeOperationsTestVector(
1455 mlspp::CipherSuite suite,
1456 Scenario scenario)
1457 : PseudoRandom(suite, "tree-operations")
1458 , cipher_suite(suite)
1459 , proposal_sender(0)
1460 {
1461 auto init_priv = prg.hpke_key("init_key");
1462 auto enc_priv = prg.hpke_key("encryption_key");
1463 auto sig_priv = prg.signature_key("signature_key");
1464 auto identity = prg.secret("identity");
1465 auto credential = Credential::basic(identity);
1466 auto key_package = KeyPackage{
1467 suite,
1468 init_priv.public_key,
1469 { suite,
1470 enc_priv.public_key,
1471 sig_priv.public_key,
1472 credential,
1473 Capabilities::create_default(),
1474 Lifetime::create_default(),
1475 {},
1476 sig_priv },
1477 {},
1478 sig_priv,
1479 };
1480
1481 switch (scenario) {
1482 case Scenario::add_right_edge: {
1483 auto tc = TreeTestCase::full(suite, prg, LeafCount{ 8 }, "tc");
1484
1485 proposal = Proposal{ Add{ key_package } };
1486
1487 tree_before = tc.pub;
1488 tree_hash_before = tree_before.root_hash();
1489
1490 tree_after = tree_before;
1491 tree_after.add_leaf(key_package.leaf_node);
1492 break;
1493 }
1494
1495 case Scenario::add_internal: {
1496 auto tc = TreeTestCase::full(suite, prg, LeafCount{ 8 }, "tc");
1497
1498 proposal = Proposal{ Add{ key_package } };
1499
1500 tree_before = tc.pub;
1501 tree_before.blank_path(LeafIndex{ 4 });
1502 tree_before.set_hash_all();
1503 tree_hash_before = tree_before.root_hash();
1504
1505 tree_after = tree_before;
1506 tree_after.add_leaf(key_package.leaf_node);
1507 break;
1508 }
1509
1510 case Scenario::update: {
1511 auto tc = TreeTestCase::full(suite, prg, LeafCount{ 8 }, "tc");
1512
1513 proposal_sender = LeafIndex{ 3 };
1514 proposal = Proposal{ Update{ key_package.leaf_node } };
1515
1516 tree_before = tc.pub;
1517 tree_hash_before = tree_before.root_hash();
1518
1519 tree_after = tree_before;
1520 tree_after.update_leaf(proposal_sender, key_package.leaf_node);
1521 break;
1522 }
1523
1524 case Scenario::remove_right_edge: {
1525 auto tc = TreeTestCase::full(suite, prg, LeafCount{ 9 }, "tc");
1526
1527 auto removed = LeafIndex{ 8 };
1528 proposal = Proposal{ Remove{ removed } };
1529
1530 tree_before = tc.pub;
1531 tree_hash_before = tree_before.root_hash();
1532
1533 tree_after = tree_before;
1534 tree_after.blank_path(removed);
1535 tree_after.truncate();
1536 break;
1537 }
1538
1539 case Scenario::remove_internal: {
1540 auto tc = TreeTestCase::full(suite, prg, LeafCount{ 8 }, "tc");
1541
1542 auto removed = LeafIndex{ 4 };
1543 proposal = Proposal{ Remove{ removed } };
1544
1545 tree_before = tc.pub;
1546 tree_hash_before = tree_before.root_hash();
1547
1548 tree_after = tree_before;
1549 tree_after.blank_path(removed);
1550 tree_after.truncate();
1551 break;
1552 }
1553 }
1554
1555 tree_after.set_hash_all();
1556 tree_hash_after = tree_after.root_hash();
1557 }
1558
1559 std::optional<std::string>
1560 TreeOperationsTestVector::verify()
1561 {
1562 tree_before.suite = cipher_suite;
1563 tree_before.set_hash_all();
1564
1565 auto tree = tree_before;
1566 VERIFY_EQUAL("tree hash before", tree.root_hash(), tree_hash_before);
1567
1568 auto apply = overloaded{
1569 [&](const Add& add) { tree.add_leaf(add.key_package.leaf_node); },
1570
1571 [&](const Update& update) {
1572 tree.update_leaf(proposal_sender, update.leaf_node);
1573 },
1574
1575 [&](const Remove& remove) {
1576 tree.blank_path(remove.removed);
1577 tree.truncate();
1578 },
1579
1580 [](const auto& /* other */) {
1581 throw InvalidParameterError("invalid proposal type");
1582 },
1583 };
1584
1585 var::visit(apply, proposal.content);
1586 VERIFY_EQUAL("tree after", tree, tree_after);
1587
1588 tree.set_hash_all();
1589 VERIFY_EQUAL("tree hash after", tree.root_hash(), tree_hash_after);
1590
1591 return std::nullopt;
1592 }
1593
1594 ///
1595 /// TreeKEMTestVector
1596 ///
1597
1598 TreeKEMTestVector::TreeKEMTestVector(mlspp::CipherSuite suite,
1599 TreeStructure tree_structure)
1600 : PseudoRandom(suite, "treekem")
1601 , cipher_suite(suite)
1602 {
1603 auto tc = TreeTestCase::with_structure(cipher_suite, prg, tree_structure);
1604
1605 group_id = tc.group_id;
1606 epoch = prg.uint64("epoch");
1607 confirmed_transcript_hash = prg.secret("confirmed_transcript_hash");
1608
1609 ratchet_tree = tc.pub;
1610
1611 // Serialize out the private states
1612 for (LeafIndex index{ 0 }; index < ratchet_tree.size; index.val++) {
1613 if (tc.privs.count(index) == 0) {
1614 continue;
1615 }
1616
1617 auto priv_state = tc.privs.at(index);
1618 auto enc_priv = priv_state.priv.private_key_cache.at(NodeIndex(index));
1619 auto path_secrets = std::vector<PathSecret>{};
1620 for (const auto& [node, path_secret] : priv_state.priv.path_secrets) {
1621 if (node == NodeIndex(index)) {
1622 // No need to serialize a secret for the leaf node
1623 continue;
1624 }
1625
1626 path_secrets.push_back(PathSecret{ node, path_secret });
1627 }
1628
1629 leaves_private.push_back(LeafPrivateInfo{
1630 index,
1631 enc_priv,
1632 priv_state.sig_priv,
1633 path_secrets,
1634 });
1635 }
1636
1637 // Create test update paths
1638 for (LeafIndex sender{ 0 }; sender < ratchet_tree.size; sender.val++) {
1639 if (!tc.pub.has_leaf(sender)) {
1640 continue;
1641 }
1642
1643 auto leaf_secret = prg.secret("update_path" + to_hex(tls::marshal(sender)));
1644 const auto& sig_priv = tc.privs.at(sender).sig_priv;
1645
1646 auto pub = tc.pub;
1647 auto new_sender_priv =
1648 pub.update(sender, leaf_secret, group_id, sig_priv, {});
1649
1650 auto group_context = GroupContext{ cipher_suite,
1651 group_id,
1652 epoch,
1653 pub.root_hash(),
1654 confirmed_transcript_hash,
1655 {} };
1656 auto ctx = tls::marshal(group_context);
1657
1658 auto path = pub.encap(new_sender_priv, ctx, {});
1659
1660 auto path_secrets = std::vector<std::optional<bytes>>{};
1661 for (LeafIndex to{ 0 }; to < ratchet_tree.size; to.val++) {
1662 if (to == sender || !pub.has_leaf(to)) {
1663 path_secrets.emplace_back(std::nullopt);
1664 continue;
1665 }
1666
1667 auto [overlap, path_secret, ok] = new_sender_priv.shared_path_secret(to);
1668 silence_unused(overlap);
1669 silence_unused(ok);
1670
1671 path_secrets.emplace_back(path_secret);
1672 }
1673
1674 update_paths.push_back(UpdatePathInfo{
1675 sender,
1676 path,
1677 path_secrets,
1678 new_sender_priv.update_secret,
1679 pub.root_hash(),
1680 });
1681 }
1682 }
1683
1684 std::optional<std::string>
1685 TreeKEMTestVector::verify()
1686 {
1687 // Finish initializing the ratchet tree
1688 ratchet_tree.suite = cipher_suite;
1689 ratchet_tree.set_hash_all();
1690
1691 // Validate public state
1692 VERIFY("parent hash valid", ratchet_tree.parent_hash_valid());
1693
1694 for (LeafIndex i{ 0 }; i < ratchet_tree.size; i.val++) {
1695 auto maybe_leaf = ratchet_tree.leaf_node(i);
1696 if (!maybe_leaf) {
1697 continue;
1698 }
1699
1700 auto leaf = opt::get(maybe_leaf);
1701 VERIFY("leaf sig", leaf.verify(cipher_suite, { { group_id, i } }));
1702 }
1703
1704 // Import private keys
1705 std::map<LeafIndex, TreeKEMPrivateKey> tree_privs;
1706 std::map<LeafIndex, SignaturePrivateKey> sig_privs;
1707 for (const auto& info : leaves_private) {
1708 auto enc_priv = info.encryption_priv;
1709 auto sig_priv = info.signature_priv;
1710 enc_priv.set_public_key(cipher_suite);
1711 sig_priv.set_public_key(cipher_suite);
1712
1713 auto priv = TreeKEMPrivateKey{};
1714 priv.suite = cipher_suite;
1715 priv.index = info.index;
1716 priv.private_key_cache.insert_or_assign(NodeIndex(info.index), enc_priv);
1717
1718 for (const auto& entry : info.path_secrets) {
1719 priv.path_secrets.insert_or_assign(entry.node, entry.path_secret);
1720 }
1721
1722 VERIFY("priv consistent", priv.consistent(ratchet_tree));
1723
1724 tree_privs.insert_or_assign(info.index, priv);
1725 sig_privs.insert_or_assign(info.index, sig_priv);
1726 }
1727
1728 for (const auto& info : update_paths) {
1729 // Test decap of the existing group secrets
1730 const auto& from = info.sender;
1731 const auto& path = info.update_path;
1732 VERIFY("path parent hash valid",
1733 ratchet_tree.parent_hash_valid(from, path));
1734
1735 auto ratchet_tree_after = ratchet_tree;
1736 ratchet_tree_after.merge(from, path);
1737 ratchet_tree_after.set_hash_all();
1738 VERIFY_EQUAL(
1739 "tree hash after", ratchet_tree_after.root_hash(), info.tree_hash_after);
1740
1741 auto group_context = GroupContext{ cipher_suite,
1742 group_id,
1743 epoch,
1744 ratchet_tree_after.root_hash(),
1745 confirmed_transcript_hash,
1746 {} };
1747 auto ctx = tls::marshal(group_context);
1748
1749 for (LeafIndex to{ 0 }; to < ratchet_tree_after.size; to.val++) {
1750 if (to == from || !ratchet_tree_after.has_leaf(to)) {
1751 continue;
1752 }
1753
1754 auto priv = tree_privs.at(to);
1755 priv.decap(from, ratchet_tree_after, ctx, path, {});
1756 VERIFY_EQUAL("commit secret", priv.update_secret, info.commit_secret);
1757
1758 auto [overlap, path_secret, ok] = priv.shared_path_secret(from);
1759 silence_unused(overlap);
1760 silence_unused(ok);
1761 VERIFY_EQUAL("path secret", path_secret, info.path_secrets[to.val]);
1762 }
1763
1764 // Test encap/decap
1765 auto ratchet_tree_encap = ratchet_tree;
1766 auto leaf_secret = random_bytes(cipher_suite.secret_size());
1767 const auto& sig_priv = sig_privs.at(from);
1768 auto new_sender_priv =
1769 ratchet_tree_encap.update(from, leaf_secret, group_id, sig_priv, {});
1770 auto new_path = ratchet_tree_encap.encap(new_sender_priv, ctx, {});
1771 VERIFY("new path parent hash valid",
1772 ratchet_tree.parent_hash_valid(from, path));
1773
1774 for (LeafIndex to{ 0 }; to < ratchet_tree_encap.size; to.val++) {
1775 if (to == from || !ratchet_tree_encap.has_leaf(to)) {
1776 continue;
1777 }
1778
1779 auto priv = tree_privs.at(to);
1780 priv.decap(from, ratchet_tree_encap, ctx, new_path, {});
1781 VERIFY_EQUAL(
1782 "commit secret", priv.update_secret, new_sender_priv.update_secret);
1783 }
1784 }
1785
1786 return std::nullopt;
1787 }
1788
1789 ///
1790 /// MessagesTestVector
1791 ///
1792
1793 MessagesTestVector::MessagesTestVector()
1794 : PseudoRandom(CipherSuite::ID::X25519_AES128GCM_SHA256_Ed25519, "messages")
1795 {
1796 auto suite = CipherSuite{ CipherSuite::ID::X25519_AES128GCM_SHA256_Ed25519 };
1797 auto epoch = epoch_t(prg.uint64("epoch"));
1798 auto index = LeafIndex{ prg.uint32("index") };
1799 auto user_id = prg.secret("user_id");
1800 auto group_id = prg.secret("group_id");
1801 // auto opaque = bytes(32, 0xD3);
1802 // auto mac = bytes(32, 0xD5);
1803
1804 auto app_id_ext = ApplicationIDExtension{ prg.secret("app_id") };
1805 auto ext_list = ExtensionList{};
1806 ext_list.add(app_id_ext);
1807
1808 auto group_context = GroupContext{ suite,
1809 group_id,
1810 epoch,
1811 prg.secret("tree_hash"),
1812 prg.secret("confirmed_trasncript_hash"),
1813 ext_list };
1814
1815 auto version = ProtocolVersion::mls10;
1816 auto hpke_priv = prg.hpke_key("hpke_priv");
1817 auto hpke_priv_2 = prg.hpke_key("hpke_priv_2");
1818 auto hpke_pub = hpke_priv.public_key;
1819 auto hpke_pub_2 = hpke_priv_2.public_key;
1820 auto hpke_ct =
1821 HPKECiphertext{ prg.secret("kem_output"), prg.secret("ciphertext") };
1822 auto sig_priv = prg.signature_key("signature_priv");
1823 auto sig_priv_2 = prg.signature_key("signature_priv_2");
1824 auto sig_pub = sig_priv.public_key;
1825 auto sig_pub_2 = sig_priv_2.public_key;
1826
1827 // KeyPackage and extensions
1828 auto cred = Credential::basic(user_id);
1829 auto leaf_node = LeafNode{ suite,
1830 hpke_pub,
1831 sig_pub,
1832 cred,
1833 Capabilities::create_default(),
1834 Lifetime::create_default(),
1835 ext_list,
1836 sig_priv };
1837 auto leaf_node_2 = LeafNode{ suite,
1838 hpke_pub_2,
1839 sig_pub_2,
1840 cred,
1841 Capabilities::create_default(),
1842 Lifetime::create_default(),
1843 ext_list,
1844 sig_priv_2 };
1845 auto key_package_obj = KeyPackage{ suite, hpke_pub, leaf_node, {}, sig_priv };
1846
1847 auto leaf_node_update =
1848 leaf_node.for_update(suite, group_id, index, hpke_pub, {}, sig_priv);
1849 auto leaf_node_commit = leaf_node.for_commit(
1850 suite, group_id, index, hpke_pub, prg.secret("parent_hash"), {}, sig_priv);
1851
1852 auto sender = Sender{ MemberSender{ index } };
1853
1854 auto tree = TreeKEMPublicKey{ suite };
1855 tree.add_leaf(leaf_node);
1856 tree.add_leaf(leaf_node_2);
1857 auto ratchet_tree_obj = RatchetTreeExtension{ tree };
1858
1859 // Welcome and its substituents
1860 auto group_info_obj =
1861 GroupInfo{ group_context, ext_list, prg.secret("confirmation_tag") };
1862 auto joiner_secret = prg.secret("joiner_secret");
1863 auto path_secret = prg.secret("path_secret");
1864 auto psk_id = ExternalPSK{ prg.secret("psk_id") };
1865 auto psk_nonce = prg.secret("psk_nonce");
1866 auto group_secrets_obj = GroupSecrets{ joiner_secret,
1867 { { path_secret } },
1868 PreSharedKeys{ {
1869 { psk_id, psk_nonce },
1870 } } };
1871 auto welcome_obj = Welcome{ suite, joiner_secret, {}, group_info_obj };
1872 welcome_obj.encrypt(key_package_obj, path_secret);
1873
1874 // Proposals
1875 auto add = Add{ key_package_obj };
1876 auto update = Update{ leaf_node_update };
1877 auto remove = Remove{ index };
1878 auto pre_shared_key = PreSharedKey{ psk_id, psk_nonce };
1879 auto reinit = ReInit{ group_id, version, suite, {} };
1880 auto external_init = ExternalInit{ prg.secret("external_init") };
1881
1882 // Commit
1883 auto proposal_ref = ProposalRef{ 32, 0xa0 };
1884
1885 auto commit_obj = Commit{ {
1886 { proposal_ref },
1887 { Proposal{ add } },
1888 },
1889 UpdatePath{
1890 leaf_node_commit,
1891 {
1892 { hpke_pub, { hpke_ct, hpke_ct } },
1893 { hpke_pub, { hpke_ct, hpke_ct, hpke_ct } },
1894 },
1895 } };
1896
1897 // AuthenticatedContent with Application / Proposal / Commit
1898
1899 // PublicMessage
1900 auto membership_key = prg.secret("membership_key");
1901
1902 auto content_auth_proposal = AuthenticatedContent::sign(
1903 WireFormat::mls_public_message,
1904 { group_id, epoch, sender, {}, Proposal{ remove } },
1905 suite,
1906 sig_priv,
1907 group_context);
1908 auto public_message_proposal_obj = PublicMessage::protect(
1909 content_auth_proposal, suite, membership_key, group_context);
1910
1911 auto content_auth_commit =
1912 AuthenticatedContent::sign(WireFormat::mls_public_message,
1913 { group_id, epoch, sender, {}, commit_obj },
1914 suite,
1915 sig_priv,
1916 group_context);
1917 content_auth_commit.set_confirmation_tag(prg.secret("confirmation_tag"));
1918 auto public_message_commit_obj = PublicMessage::protect(
1919 content_auth_commit, suite, membership_key, group_context);
1920
1921 // PrivateMessage
1922 auto content_auth_application_obj = AuthenticatedContent::sign(
1923 WireFormat::mls_private_message,
1924 { group_id, epoch, sender, {}, ApplicationData{} },
1925 suite,
1926 sig_priv,
1927 group_context);
1928
1929 auto keys = GroupKeySource(
1930 suite, LeafCount{ index.val + 1 }, prg.secret("encryption_secret"));
1931 auto private_message_obj =
1932 PrivateMessage::protect(content_auth_application_obj,
1933 suite,
1934 keys,
1935 prg.secret("sender_data_secret"),
1936 10);
1937
1938 // Serialize out all the objects
1939 mls_welcome = tls::marshal(MLSMessage{ welcome_obj });
1940 mls_group_info = tls::marshal(MLSMessage{ group_info_obj });
1941 mls_key_package = tls::marshal(MLSMessage{ key_package_obj });
1942
1943 ratchet_tree = tls::marshal(ratchet_tree_obj);
1944 group_secrets = tls::marshal(group_secrets_obj);
1945
1946 add_proposal = tls::marshal(add);
1947 update_proposal = tls::marshal(update);
1948 remove_proposal = tls::marshal(remove);
1949 pre_shared_key_proposal = tls::marshal(pre_shared_key);
1950 re_init_proposal = tls::marshal(reinit);
1951 external_init_proposal = tls::marshal(external_init);
1952
1953 commit = tls::marshal(commit_obj);
1954
1955 public_message_proposal =
1956 tls::marshal(MLSMessage{ public_message_proposal_obj });
1957 public_message_commit = tls::marshal(MLSMessage{ public_message_commit_obj });
1958 private_message = tls::marshal(MLSMessage{ private_message_obj });
1959 }
1960
1961 std::optional<std::string>
1962 MessagesTestVector::verify() const
1963 {
1964 // TODO(RLB) Verify signatures
1965 // TODO(RLB) Verify content types in PublicMessage objects
1966 auto require_format = [](WireFormat format) {
1967 return
1968 [format](const MLSMessage& msg) { return msg.wire_format() == format; };
1969 };
1970
1971 VERIFY_TLS_RTT_VAL("Welcome",
1972 MLSMessage,
1973 mls_welcome,
1974 require_format(WireFormat::mls_welcome));
1975 VERIFY_TLS_RTT_VAL("GroupInfo",
1976 MLSMessage,
1977 mls_group_info,
1978 require_format(WireFormat::mls_group_info));
1979 VERIFY_TLS_RTT_VAL("KeyPackage",
1980 MLSMessage,
1981 mls_key_package,
1982 require_format(WireFormat::mls_key_package));
1983
1984 VERIFY_TLS_RTT("RatchetTree", RatchetTreeExtension, ratchet_tree);
1985 VERIFY_TLS_RTT("GroupSecrets", GroupSecrets, group_secrets);
1986
1987 VERIFY_TLS_RTT("Add", Add, add_proposal);
1988 VERIFY_TLS_RTT("Update", Update, update_proposal);
1989 VERIFY_TLS_RTT("Remove", Remove, remove_proposal);
1990 VERIFY_TLS_RTT("PreSharedKey", PreSharedKey, pre_shared_key_proposal);
1991 VERIFY_TLS_RTT("ReInit", ReInit, re_init_proposal);
1992 VERIFY_TLS_RTT("ExternalInit", ExternalInit, external_init_proposal);
1993
1994 VERIFY_TLS_RTT("Commit", Commit, commit);
1995
1996 VERIFY_TLS_RTT_VAL("Public(Proposal)",
1997 MLSMessage,
1998 public_message_proposal,
1999 require_format(WireFormat::mls_public_message));
2000 VERIFY_TLS_RTT_VAL("Public(Commit)",
2001 MLSMessage,
2002 public_message_commit,
2003 require_format(WireFormat::mls_public_message));
2004 VERIFY_TLS_RTT_VAL("PrivateMessage",
2005 MLSMessage,
2006 private_message,
2007 require_format(WireFormat::mls_private_message));
2008
2009 return std::nullopt;
2010 }
2011
2012 std::optional<std::string>
2013 PassiveClientTestVector::verify()
2014 {
2015 // Import everything
2016 signature_priv.set_public_key(cipher_suite);
2017 encryption_priv.set_public_key(cipher_suite);
2018 init_priv.set_public_key(cipher_suite);
2019
2020 const auto& key_package_raw = var::get<KeyPackage>(key_package.message);
2021 const auto& welcome_raw = var::get<Welcome>(welcome.message);
2022
2023 auto ext_psks = std::map<bytes, bytes>{};
2024 for (const auto& [id, psk] : external_psks) {
2025 ext_psks.insert_or_assign(id, psk);
2026 }
2027
2028 // Join the group and follow along
2029 auto state = State(init_priv,
2030 encryption_priv,
2031 signature_priv,
2032 key_package_raw,
2033 welcome_raw,
2034 ratchet_tree,
2035 ext_psks);
2036 VERIFY_EQUAL(
2037 "initial epoch", state.epoch_authenticator(), initial_epoch_authenticator);
2038
2039 for (const auto& tve : epochs) {
2040 for (const auto& proposal : tve.proposals) {
2041 state.handle(proposal);
2042 }
2043
2044 state = opt::get(state.handle(tve.commit));
2045 VERIFY_EQUAL(
2046 "epoch auth", state.epoch_authenticator(), tve.epoch_authenticator)
2047 }
2048
2049 return std::nullopt;
2050 }
2051
2052 } // namespace mls_vectors