melos 0.0.2 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +71 -11
- data/docs/roadmap.md +302 -0
- data/docs/validation.md +172 -0
- data/lib/melos/capabilities.rb +18 -0
- data/lib/melos/client.rb +255 -0
- data/lib/melos/credential/x509.rb +91 -0
- data/lib/melos/crypto.rb +109 -15
- data/lib/melos/epoch_secrets.rb +166 -0
- data/lib/melos/errors.rb +18 -0
- data/lib/melos/external_sender.rb +78 -0
- data/lib/melos/group/validation.rb +310 -0
- data/lib/melos/group.rb +1087 -0
- data/lib/melos/key_package_bundle.rb +67 -0
- data/lib/melos/persistence.rb +45 -0
- data/lib/melos/private_tree.rb +70 -0
- data/lib/melos/ratchet_tree.rb +407 -0
- data/lib/melos/secret_tree.rb +130 -88
- data/lib/melos/struct/base.rb +46 -20
- data/lib/melos/struct/structs.rb +205 -87
- data/lib/melos/tree.rb +4 -120
- data/lib/melos/vec.rb +20 -35
- data/lib/melos/version.rb +1 -1
- data/lib/melos.rb +13 -2
- metadata +18 -22
- data/lib/melos/key_schedule.rb +0 -62
- data/lib/melos/struct/ratchet_tree.rb +0 -324
- data/test_vectors/crypto-basics.json +0 -303
- data/test_vectors/deserialization.json +0 -58
- data/test_vectors/key-schedule.json +0 -926
- data/test_vectors/message-protection.json +0 -142
- data/test_vectors/messages.json +0 -5702
- data/test_vectors/passive-client-handling-commit.json +0 -2683
- data/test_vectors/passive-client-random.json +0 -2657
- data/test_vectors/passive-client-welcome.json +0 -814
- data/test_vectors/psk_secret.json +0 -2382
- data/test_vectors/secret-tree.json +0 -4846
- data/test_vectors/transcript-hashes.json +0 -58
- data/test_vectors/tree-math.json +0 -8276
- data/test_vectors/tree-operations.json +0 -47
- data/test_vectors/tree-validation.json +0 -11839
- data/test_vectors/treekem.json +0 -14877
- data/test_vectors/welcome.json +0 -51
data/lib/melos/struct/structs.rb
CHANGED
|
@@ -33,6 +33,13 @@ class Melos::Struct::Credential < Melos::Struct::Base
|
|
|
33
33
|
[:identity, :select, ->(ctx){ctx[:credential_type] == Melos::Constants::CredentialType::BASIC}, :vec],
|
|
34
34
|
[:certificates, :select, ->(ctx){ctx[:credential_type] == Melos::Constants::CredentialType::X509}, :classes, Melos::Struct::Certificate]
|
|
35
35
|
]
|
|
36
|
+
|
|
37
|
+
def self.create_basic_credential(identity)
|
|
38
|
+
new_instance = self.allocate
|
|
39
|
+
new_instance.instance_variable_set(:@credential_type, Melos::Constants::CredentialType::BASIC)
|
|
40
|
+
new_instance.instance_variable_set(:@identity, identity)
|
|
41
|
+
new_instance
|
|
42
|
+
end
|
|
36
43
|
end
|
|
37
44
|
|
|
38
45
|
## 6.3.2
|
|
@@ -84,6 +91,26 @@ class Melos::Struct::Capabilities < Melos::Struct::Base
|
|
|
84
91
|
[:proposals, :vec], # vec of ProposalTypes (uint16)
|
|
85
92
|
[:credentials, :vec] # vec of CredentialTypes (uint16)
|
|
86
93
|
]
|
|
94
|
+
|
|
95
|
+
def self.import(capabilities)
|
|
96
|
+
new_instance = self.allocate
|
|
97
|
+
new_instance.instance_variable_set(:@versions, capabilities.versions.pack('S>*'))
|
|
98
|
+
new_instance.instance_variable_set(:@cipher_suites, capabilities.cipher_suites.pack('S>*'))
|
|
99
|
+
new_instance.instance_variable_set(:@extensions, capabilities.extensions.pack('S>*'))
|
|
100
|
+
new_instance.instance_variable_set(:@proposals, capabilities.proposals.pack('S>*'))
|
|
101
|
+
new_instance.instance_variable_set(:@credentials, capabilities.credentials.pack('S>*'))
|
|
102
|
+
new_instance
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def export
|
|
106
|
+
cap = Melos::Capabilities.new
|
|
107
|
+
cap.versions = versions.unpack('S>*')
|
|
108
|
+
cap.cipher_suites = cipher_suites.unpack('S>*')
|
|
109
|
+
cap.extensions = extensions.unpack('S>*')
|
|
110
|
+
cap.proposals = proposals.unpack('S>*')
|
|
111
|
+
cap.credentials = credentials.unpack('S>*')
|
|
112
|
+
cap
|
|
113
|
+
end
|
|
87
114
|
end
|
|
88
115
|
|
|
89
116
|
class Melos::Struct::Lifetime < Melos::Struct::Base
|
|
@@ -92,6 +119,13 @@ class Melos::Struct::Lifetime < Melos::Struct::Base
|
|
|
92
119
|
[:not_before, :uint64],
|
|
93
120
|
[:not_after, :uint64]
|
|
94
121
|
]
|
|
122
|
+
|
|
123
|
+
def self.create(not_before:, not_after:)
|
|
124
|
+
new_instance = self.allocate
|
|
125
|
+
new_instance.instance_variable_set(:@not_before, not_before)
|
|
126
|
+
new_instance.instance_variable_set(:@not_after, not_after)
|
|
127
|
+
new_instance
|
|
128
|
+
end
|
|
95
129
|
end
|
|
96
130
|
|
|
97
131
|
class Melos::Struct::Extension < Melos::Struct::Base
|
|
@@ -100,6 +134,13 @@ class Melos::Struct::Extension < Melos::Struct::Base
|
|
|
100
134
|
[:extension_type, :uint16], # ExtensionType = uint16
|
|
101
135
|
[:extension_data, :vec]
|
|
102
136
|
]
|
|
137
|
+
|
|
138
|
+
# 13.4: any list of extensions MUST NOT have more than one extension of a given type
|
|
139
|
+
def self.check_unique_types!(extensions, where)
|
|
140
|
+
duplicates = extensions.map(&:extension_type).tally.select { |_type, count| count > 1 }.keys
|
|
141
|
+
return if duplicates.empty?
|
|
142
|
+
raise Melos::ProtocolError.new("#{where} has more than one extension of type #{duplicates.map { '0x%04x' % _1 }.join(', ')}")
|
|
143
|
+
end
|
|
103
144
|
end
|
|
104
145
|
|
|
105
146
|
class Melos::Struct::LeafNode < Melos::Struct::Base
|
|
@@ -118,19 +159,19 @@ class Melos::Struct::LeafNode < Melos::Struct::Base
|
|
|
118
159
|
|
|
119
160
|
def leaf_node_tbs(group_id, leaf_index)
|
|
120
161
|
buf = ''
|
|
121
|
-
buf += Melos::Vec.
|
|
122
|
-
buf += Melos::Vec.
|
|
162
|
+
buf += Melos::Vec.string_to_vec(encryption_key)
|
|
163
|
+
buf += Melos::Vec.string_to_vec(signature_key)
|
|
123
164
|
buf += credential.raw
|
|
124
165
|
buf += capabilities.raw
|
|
125
166
|
buf += [leaf_node_source].pack('C')
|
|
126
167
|
if leaf_node_source == Melos::Constants::LeafNodeSource::KEY_PACKAGE
|
|
127
168
|
buf += lifetime.raw
|
|
128
169
|
elsif leaf_node_source == Melos::Constants::LeafNodeSource::COMMIT
|
|
129
|
-
buf += Melos::Vec.
|
|
170
|
+
buf += Melos::Vec.string_to_vec(parent_hash)
|
|
130
171
|
end
|
|
131
|
-
buf += Melos::Vec.
|
|
172
|
+
buf += Melos::Vec.string_to_vec(extensions.map(&:raw).join)
|
|
132
173
|
if leaf_node_source == Melos::Constants::LeafNodeSource::UPDATE || leaf_node_source == Melos::Constants::LeafNodeSource::COMMIT
|
|
133
|
-
buf += Melos::Vec.
|
|
174
|
+
buf += Melos::Vec.string_to_vec(group_id)
|
|
134
175
|
buf += [leaf_index].pack('L>') # uint32
|
|
135
176
|
end
|
|
136
177
|
buf
|
|
@@ -309,6 +350,26 @@ class Melos::Struct::PreSharedKeyID < Melos::Struct::Base
|
|
|
309
350
|
new_instance.instance_variable_set(:@psk_nonce, psk_nonce)
|
|
310
351
|
new_instance
|
|
311
352
|
end
|
|
353
|
+
|
|
354
|
+
# 8.4, 8.6: the resumption_psk of group psk_group_id at psk_epoch; usage is a Melos::Constants::ResumptionPSKUsage
|
|
355
|
+
def self.create_resumption(usage:, psk_group_id:, psk_epoch:, psk_nonce:)
|
|
356
|
+
new_instance = self.allocate
|
|
357
|
+
new_instance.instance_variable_set(:@psktype, Melos::Constants::PSKType::RESUMPTION)
|
|
358
|
+
new_instance.instance_variable_set(:@usage, usage)
|
|
359
|
+
new_instance.instance_variable_set(:@psk_group_id, psk_group_id)
|
|
360
|
+
new_instance.instance_variable_set(:@psk_epoch, psk_epoch)
|
|
361
|
+
new_instance.instance_variable_set(:@psk_nonce, psk_nonce)
|
|
362
|
+
new_instance
|
|
363
|
+
end
|
|
364
|
+
|
|
365
|
+
def resumption?
|
|
366
|
+
@psktype == Melos::Constants::PSKType::RESUMPTION
|
|
367
|
+
end
|
|
368
|
+
|
|
369
|
+
# 11.2, 11.3: a resumption PSK that links a reinitialized or branched group to its origin
|
|
370
|
+
def reinit_or_branch?
|
|
371
|
+
resumption? && [Melos::Constants::ResumptionPSKUsage::REINIT, Melos::Constants::ResumptionPSKUsage::BRANCH].include?(@usage)
|
|
372
|
+
end
|
|
312
373
|
end
|
|
313
374
|
|
|
314
375
|
class Melos::Struct::PSKLabel < Melos::Struct::Base
|
|
@@ -344,6 +405,31 @@ class Melos::Struct::KeyPackage < Melos::Struct::Base
|
|
|
344
405
|
def ref(suite)
|
|
345
406
|
Melos::Crypto.make_keypackage_ref(suite, self.raw)
|
|
346
407
|
end
|
|
408
|
+
|
|
409
|
+
def self.create(cipher_suite:, init_key:, leaf_node:)
|
|
410
|
+
new_instance = self.allocate
|
|
411
|
+
new_instance.instance_variable_set(:@version, 1)
|
|
412
|
+
new_instance.instance_variable_set(:@cipher_suite, cipher_suite)
|
|
413
|
+
new_instance.instance_variable_set(:@init_key, init_key)
|
|
414
|
+
new_instance.instance_variable_set(:@leaf_node, leaf_node)
|
|
415
|
+
new_instance.instance_variable_set(:@extensions, leaf_node.extensions)
|
|
416
|
+
new_instance.instance_variable_set(:@signature, '')
|
|
417
|
+
new_instance
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
def keypackage_tbs
|
|
421
|
+
[version].pack('S>') + [cipher_suite].pack('S>') + Melos::Vec.string_to_vec(init_key) + leaf_node.raw + Melos::Vec.string_to_vec(extensions.map(&:raw).join)
|
|
422
|
+
end
|
|
423
|
+
|
|
424
|
+
def sign(signature_private_key)
|
|
425
|
+
suite = Melos::Crypto::CipherSuite.new(@cipher_suite)
|
|
426
|
+
@signature = Melos::Crypto.sign_with_label(suite, signature_private_key, "KeyPackageTBS", keypackage_tbs)
|
|
427
|
+
end
|
|
428
|
+
|
|
429
|
+
def verify()
|
|
430
|
+
suite = Melos::Crypto::CipherSuite.new(@cipher_suite)
|
|
431
|
+
Melos::Crypto.verify_with_label(suite, leaf_node.signature_key, "KeyPackageTBS", keypackage_tbs, signature)
|
|
432
|
+
end
|
|
347
433
|
end
|
|
348
434
|
|
|
349
435
|
class Melos::Struct::KeyPackageTBS < Melos::Struct::Base
|
|
@@ -373,7 +459,7 @@ end
|
|
|
373
459
|
## 12.1.1 - 12.1.7
|
|
374
460
|
|
|
375
461
|
class Melos::Struct::Add < Melos::Struct::Base
|
|
376
|
-
|
|
462
|
+
attr_accessor :key_package
|
|
377
463
|
STRUCT = [
|
|
378
464
|
[:key_package, :class, Melos::Struct::KeyPackage]
|
|
379
465
|
]
|
|
@@ -387,7 +473,7 @@ class Melos::Struct::Update < Melos::Struct::Base
|
|
|
387
473
|
end
|
|
388
474
|
|
|
389
475
|
class Melos::Struct::Remove < Melos::Struct::Base
|
|
390
|
-
|
|
476
|
+
attr_accessor :removed
|
|
391
477
|
STRUCT = [
|
|
392
478
|
[:removed, :uint32]
|
|
393
479
|
]
|
|
@@ -434,10 +520,25 @@ class Melos::Struct::ExternalSender < Melos::Struct::Base
|
|
|
434
520
|
]
|
|
435
521
|
end
|
|
436
522
|
|
|
523
|
+
# the extension_data of the external_senders GroupContext extension: ExternalSender external_senders<V>
|
|
524
|
+
class Melos::Struct::ExternalSenders < Melos::Struct::Base
|
|
525
|
+
attr_reader :external_senders
|
|
526
|
+
STRUCT = [
|
|
527
|
+
[:external_senders, :classes, Melos::Struct::ExternalSender]
|
|
528
|
+
]
|
|
529
|
+
|
|
530
|
+
# the GroupContext extension listing these senders
|
|
531
|
+
def self.extension(external_senders)
|
|
532
|
+
Melos::Struct::Extension.create(
|
|
533
|
+
extension_type: Melos::Constants::ExtensionType::EXTERNAL_SENDERS, extension_data: create(external_senders: external_senders).raw
|
|
534
|
+
)
|
|
535
|
+
end
|
|
536
|
+
end
|
|
537
|
+
|
|
437
538
|
## 12.1
|
|
438
539
|
|
|
439
540
|
class Melos::Struct::Proposal < Melos::Struct::Base
|
|
440
|
-
|
|
541
|
+
attr_accessor :proposal_type, :add, :update, :remove, :psk, :reinit, :external_init, :group_context_extensions
|
|
441
542
|
STRUCT = [
|
|
442
543
|
[:proposal_type, :uint16],
|
|
443
544
|
[:add, :select, ->(ctx){ctx[:proposal_type] == Melos::Constants::ProposalType::ADD}, :class, Melos::Struct::Add],
|
|
@@ -463,6 +564,14 @@ class Melos::Struct::ProposalOrRef < Melos::Struct::Base
|
|
|
463
564
|
[:proposal, :select, ->(ctx){ctx[:type] == Melos::Constants::ProposalOrRefType::PROPOSAL}, :class, Melos::Struct::Proposal],
|
|
464
565
|
[:reference, :select, ->(ctx){ctx[:type] == Melos::Constants::ProposalOrRefType::REFERENCE}, :vec] # ProposalRef is a HashReference, which is a :vec
|
|
465
566
|
]
|
|
567
|
+
|
|
568
|
+
def self.create(type:, proposal:, reference:)
|
|
569
|
+
new_instance = self.allocate
|
|
570
|
+
new_instance.instance_variable_set(:@type, type)
|
|
571
|
+
new_instance.instance_variable_set(:@proposal, proposal)
|
|
572
|
+
new_instance.instance_variable_set(:@reference, reference)
|
|
573
|
+
new_instance
|
|
574
|
+
end
|
|
466
575
|
end
|
|
467
576
|
|
|
468
577
|
class Melos::Struct::Commit < Melos::Struct::Base
|
|
@@ -471,6 +580,13 @@ class Melos::Struct::Commit < Melos::Struct::Base
|
|
|
471
580
|
[:proposals, :classes, Melos::Struct::ProposalOrRef],
|
|
472
581
|
[:path, :optional, Melos::Struct::UpdatePath]
|
|
473
582
|
]
|
|
583
|
+
|
|
584
|
+
def self.create(proposals:, path:)
|
|
585
|
+
new_instance = self.allocate
|
|
586
|
+
new_instance.instance_variable_set(:@proposals, proposals)
|
|
587
|
+
new_instance.instance_variable_set(:@path, path)
|
|
588
|
+
new_instance
|
|
589
|
+
end
|
|
474
590
|
end
|
|
475
591
|
|
|
476
592
|
## 12.4.3
|
|
@@ -494,8 +610,8 @@ class Melos::Struct::GroupInfo < Melos::Struct::Base
|
|
|
494
610
|
)
|
|
495
611
|
end
|
|
496
612
|
|
|
497
|
-
def sign(suite,
|
|
498
|
-
Melos::Crypto.sign_with_label(suite, signer_private_key, "GroupInfoTBS", group_info_tbs.raw)
|
|
613
|
+
def sign(suite, signer_private_key)
|
|
614
|
+
@signature = Melos::Crypto.sign_with_label(suite, signer_private_key, "GroupInfoTBS", group_info_tbs.raw)
|
|
499
615
|
end
|
|
500
616
|
|
|
501
617
|
def verify(suite, signer_public_key)
|
|
@@ -575,8 +691,9 @@ class Melos::Struct::Node < Melos::Struct::Base
|
|
|
575
691
|
[:parent_node, :select, ->(ctx){ctx[:node_type] == Melos::Constants::NodeType::PARENT}, :class, Melos::Struct::ParentNode] # parent
|
|
576
692
|
]
|
|
577
693
|
|
|
694
|
+
# the parent_hash this node carries: nil for a leaf whose source is key_package or update (7.2)
|
|
578
695
|
def parent_hash_in_node
|
|
579
|
-
@
|
|
696
|
+
@parent_node ? @parent_node.parent_hash : @leaf_node.parent_hash
|
|
580
697
|
end
|
|
581
698
|
|
|
582
699
|
def public_encryption_key
|
|
@@ -628,38 +745,27 @@ class Melos::Struct::Sender < Melos::Struct::Base
|
|
|
628
745
|
instance.instance_variable_set(:@sender_index, sender_index)
|
|
629
746
|
instance
|
|
630
747
|
end
|
|
748
|
+
|
|
749
|
+
# a sender with no index: new_member_proposal or new_member_commit
|
|
750
|
+
def self.create_new_member(sender_type)
|
|
751
|
+
raise ArgumentError.new('not a new_member sender type') unless [Melos::Constants::SenderType::NEW_MEMBER_PROPOSAL, Melos::Constants::SenderType::NEW_MEMBER_COMMIT].include?(sender_type)
|
|
752
|
+
instance = self.allocate
|
|
753
|
+
instance.instance_variable_set(:@sender_type, sender_type)
|
|
754
|
+
instance
|
|
755
|
+
end
|
|
631
756
|
end
|
|
632
757
|
|
|
758
|
+
# The select in FramedContentAuthData depends on FramedContent.content_type, a field of
|
|
759
|
+
# the enclosing struct, so the parent passes it in as external context:
|
|
760
|
+
# [:auth, :class, Melos::Struct::FramedContentAuthData, ->(ctx){ {content_type: ctx[:content].content_type} }]
|
|
633
761
|
class Melos::Struct::FramedContentAuthData < Melos::Struct::Base
|
|
634
762
|
attr_reader :signature, :confirmation_tag, :content_type
|
|
635
763
|
|
|
636
764
|
STRUCT = [
|
|
637
|
-
[:signature, :vec]
|
|
765
|
+
[:signature, :vec],
|
|
766
|
+
[:confirmation_tag, :select, ->(ctx){ctx[:content_type] == Melos::Constants::ContentType::COMMIT}, :vec] # MAC is opaque <V>
|
|
638
767
|
]
|
|
639
768
|
|
|
640
|
-
# initialize from stream
|
|
641
|
-
def self.new_with_content_type(stream, content_type)
|
|
642
|
-
instance = self.allocate
|
|
643
|
-
context = instance.send(:deserialize, stream)
|
|
644
|
-
# custom part based on instance variable
|
|
645
|
-
if content_type == Melos::Constants::ContentType::COMMIT # commit
|
|
646
|
-
# read MAC(opaque <V>) confirmation_tag
|
|
647
|
-
value = Melos::Vec.parse_stringio(stream)
|
|
648
|
-
context << [:confirmation_tag, value]
|
|
649
|
-
end
|
|
650
|
-
context << [:content_type, content_type]
|
|
651
|
-
instance.send(:set_instance_vars, context)
|
|
652
|
-
instance
|
|
653
|
-
end
|
|
654
|
-
|
|
655
|
-
def raw
|
|
656
|
-
if @content_type == Melos::Constants::ContentType::COMMIT
|
|
657
|
-
Melos::Vec.from_string(@signature) + Melos::Vec.from_string(@confirmation_tag)
|
|
658
|
-
else
|
|
659
|
-
Melos::Vec.from_string(@signature)
|
|
660
|
-
end
|
|
661
|
-
end
|
|
662
|
-
|
|
663
769
|
def self.create(signature:, content_type:, confirmation_tag:)
|
|
664
770
|
instance = self.allocate
|
|
665
771
|
instance.instance_variable_set(:@signature, signature)
|
|
@@ -669,6 +775,12 @@ class Melos::Struct::FramedContentAuthData < Melos::Struct::Base
|
|
|
669
775
|
end
|
|
670
776
|
instance
|
|
671
777
|
end
|
|
778
|
+
|
|
779
|
+
# a commit's confirmation tag is computed after signing, once the next epoch's key schedule is known (8.2)
|
|
780
|
+
def confirmation_tag=(tag)
|
|
781
|
+
raise ArgumentError.new('only a commit carries a confirmation tag') unless @content_type == Melos::Constants::ContentType::COMMIT
|
|
782
|
+
@confirmation_tag = tag
|
|
783
|
+
end
|
|
672
784
|
end
|
|
673
785
|
|
|
674
786
|
class Melos::Struct::FramedContent < Melos::Struct::Base
|
|
@@ -719,7 +831,7 @@ class Melos::Struct::AuthenticatedContent < Melos::Struct::Base
|
|
|
719
831
|
STRUCT = [
|
|
720
832
|
[:wire_format, :uint16],
|
|
721
833
|
[:content, :class, Melos::Struct::FramedContent],
|
|
722
|
-
[:auth, :
|
|
834
|
+
[:auth, :class, Melos::Struct::FramedContentAuthData, ->(ctx){ {content_type: ctx[:content].content_type} }]
|
|
723
835
|
]
|
|
724
836
|
|
|
725
837
|
def content_tbm
|
|
@@ -750,15 +862,15 @@ class Melos::Struct::AuthenticatedContent < Melos::Struct::Base
|
|
|
750
862
|
new_instance
|
|
751
863
|
end
|
|
752
864
|
|
|
753
|
-
# populate auth with
|
|
754
|
-
def sign(suite, signature_private_key, group_context)
|
|
865
|
+
# populate auth with the signature (6.1); a commit's confirmation_tag may be given here or set on auth later
|
|
866
|
+
def sign(suite, signature_private_key, group_context, confirmation_tag: nil)
|
|
755
867
|
raise ArgumentError.new('Application data cannot be sent as a PublicMessage') if wire_format == Melos::Constants::WireFormat::MLS_PUBLIC_MESSAGE && content.content_type == Melos::Constants::ContentType::APPLICATION
|
|
756
868
|
content_tbs = content.content_tbs(Melos::Constants::Version::MLS10, wire_format, group_context)
|
|
757
869
|
signature = Melos::Crypto.sign_with_label(suite, signature_private_key, "FramedContentTBS", content_tbs)
|
|
758
870
|
@auth = Melos::Struct::FramedContentAuthData.create(
|
|
759
871
|
signature: signature,
|
|
760
872
|
content_type: content.content_type,
|
|
761
|
-
confirmation_tag:
|
|
873
|
+
confirmation_tag: confirmation_tag
|
|
762
874
|
)
|
|
763
875
|
end
|
|
764
876
|
end
|
|
@@ -769,7 +881,7 @@ class Melos::Struct::PublicMessage < Melos::Struct::Base
|
|
|
769
881
|
attr_reader :content, :auth, :membership_tag
|
|
770
882
|
STRUCT = [
|
|
771
883
|
[:content, :class, Melos::Struct::FramedContent],
|
|
772
|
-
[:auth, :
|
|
884
|
+
[:auth, :class, Melos::Struct::FramedContentAuthData, ->(ctx){ {content_type: ctx[:content].content_type} }],
|
|
773
885
|
[:membership_tag, :select, ->(ctx){ctx[:content].sender.sender_type == Melos::Constants::SenderType::MEMBER}, :vec] # MAC is opaque <V>
|
|
774
886
|
]
|
|
775
887
|
|
|
@@ -834,7 +946,7 @@ class Melos::Struct::PrivateMessage < Melos::Struct::Base
|
|
|
834
946
|
end
|
|
835
947
|
|
|
836
948
|
def self.sender_data_aad_impl(gid, ep, ct)
|
|
837
|
-
Melos::Vec.
|
|
949
|
+
Melos::Vec.string_to_vec(gid) + [ep].pack('Q>') + [ct].pack('C')
|
|
838
950
|
end
|
|
839
951
|
|
|
840
952
|
def private_content_aad
|
|
@@ -842,17 +954,21 @@ class Melos::Struct::PrivateMessage < Melos::Struct::Base
|
|
|
842
954
|
end
|
|
843
955
|
|
|
844
956
|
def self.private_content_aad_impl(gid, ep, ct, ad)
|
|
845
|
-
Melos::Vec.
|
|
957
|
+
Melos::Vec.string_to_vec(gid) + [ep].pack('Q>') + [ct].pack('C') + Melos::Vec.string_to_vec(ad)
|
|
846
958
|
end
|
|
847
959
|
|
|
848
960
|
def self.protect(authenticated_content, suite, secret_tree, sender_data_secret, padding_size)
|
|
849
961
|
leaf_index = authenticated_content.content.sender.leaf_index
|
|
850
962
|
content_type = authenticated_content.content.content_type
|
|
851
963
|
reuse_guard = SecureRandom.random_bytes(4)
|
|
852
|
-
key, nonce, generation =
|
|
964
|
+
key, nonce, generation = secret_tree.ratchet_and_get(content_type, leaf_index)
|
|
853
965
|
new_nonce = apply_nonce_reuse_guard(nonce, reuse_guard)
|
|
854
966
|
|
|
855
|
-
private_message_content_plain =
|
|
967
|
+
private_message_content_plain = Melos::Struct::PrivateMessageContent.create(
|
|
968
|
+
framed_content: authenticated_content.content,
|
|
969
|
+
auth: authenticated_content.auth,
|
|
970
|
+
padding_size: padding_size
|
|
971
|
+
).raw
|
|
856
972
|
aad = private_content_aad_impl(
|
|
857
973
|
authenticated_content.content.group_id,
|
|
858
974
|
authenticated_content.content.epoch,
|
|
@@ -885,10 +1001,12 @@ class Melos::Struct::PrivateMessage < Melos::Struct::Base
|
|
|
885
1001
|
|
|
886
1002
|
def unprotect(suite, secret_tree, sender_data_secret)
|
|
887
1003
|
sender_data = decrypt_sender_data(suite, sender_data_secret)
|
|
888
|
-
key, nonce, _ =
|
|
1004
|
+
key, nonce, _ = secret_tree.ratchet_until_and_get(content_type, sender_data.leaf_index, sender_data.generation)
|
|
889
1005
|
new_nonce = self.class.apply_nonce_reuse_guard(nonce, sender_data.reuse_guard)
|
|
890
1006
|
stream = StringIO.new(Melos::Crypto.aead_decrypt(suite, key, new_nonce, private_content_aad, ciphertext))
|
|
891
|
-
pmc = Melos::Struct::PrivateMessageContent.
|
|
1007
|
+
pmc = Melos::Struct::PrivateMessageContent.new(stream, content_type: content_type)
|
|
1008
|
+
# 6.3.1: the padding MUST be all zero bytes, otherwise the message is malformed
|
|
1009
|
+
raise Melos::ProtocolError.new('PrivateMessage padding is not all zero') unless pmc.padding_zero?
|
|
892
1010
|
|
|
893
1011
|
fc = Melos::Struct::FramedContent.create(
|
|
894
1012
|
group_id: group_id,
|
|
@@ -922,56 +1040,44 @@ class Melos::Struct::PrivateMessage < Melos::Struct::Base
|
|
|
922
1040
|
nonce_arr.pack('C*')
|
|
923
1041
|
end
|
|
924
1042
|
|
|
925
|
-
def self.serialize_private_message_content(framed_content, framed_content_auth_data, padding_size)
|
|
926
|
-
buf = ''
|
|
927
|
-
case framed_content.content_type
|
|
928
|
-
when Melos::Constants::ContentType::APPLICATION
|
|
929
|
-
buf += Melos::Vec.from_string(framed_content.application_data)
|
|
930
|
-
when Melos::Constants::ContentType::PROPOSAL
|
|
931
|
-
buf += framed_content.proposal.raw
|
|
932
|
-
when Melos::Constants::ContentType::COMMIT
|
|
933
|
-
buf += framed_content.commit.raw
|
|
934
|
-
end
|
|
935
|
-
buf += Melos::Vec.from_string(framed_content_auth_data.signature)
|
|
936
|
-
if framed_content.content_type == Melos::Constants::ContentType::COMMIT
|
|
937
|
-
buf += Melos::Vec.from_string(framed_content_auth_data.confirmation_tag)
|
|
938
|
-
end
|
|
939
|
-
buf += Melos::Crypto::Util.zero_vector(padding_size)
|
|
940
|
-
buf
|
|
941
|
-
end
|
|
942
1043
|
end
|
|
943
1044
|
|
|
1045
|
+
# The selects in PrivateMessageContent depend on PrivateMessage.content_type, a field of
|
|
1046
|
+
# the enclosing struct, so the caller passes it in:
|
|
1047
|
+
# Melos::Struct::PrivateMessageContent.new(stream, content_type: content_type)
|
|
944
1048
|
class Melos::Struct::PrivateMessageContent < Melos::Struct::Base
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
1049
|
+
attr_reader :application_data, :proposal, :commit, :auth, :padding, :content_type
|
|
1050
|
+
STRUCT = [
|
|
1051
|
+
[:application_data, :select, ->(ctx){ctx[:content_type] == Melos::Constants::ContentType::APPLICATION}, :vec],
|
|
1052
|
+
[:proposal, :select, ->(ctx){ctx[:content_type] == Melos::Constants::ContentType::PROPOSAL}, :class, Melos::Struct::Proposal],
|
|
1053
|
+
[:commit, :select, ->(ctx){ctx[:content_type] == Melos::Constants::ContentType::COMMIT}, :class, Melos::Struct::Commit],
|
|
1054
|
+
[:auth, :class, Melos::Struct::FramedContentAuthData, ->(ctx){ {content_type: ctx[:content_type]} }],
|
|
1055
|
+
[:padding, :padding] # the rest of the stream; zero bytes
|
|
1056
|
+
]
|
|
948
1057
|
|
|
949
|
-
def self.
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
case content_type
|
|
1058
|
+
def self.create(framed_content:, auth:, padding_size:)
|
|
1059
|
+
new_instance = self.allocate
|
|
1060
|
+
new_instance.instance_variable_set(:@content_type, framed_content.content_type)
|
|
1061
|
+
case framed_content.content_type
|
|
954
1062
|
when Melos::Constants::ContentType::APPLICATION
|
|
955
|
-
|
|
956
|
-
context << [:application_data, value]
|
|
1063
|
+
new_instance.instance_variable_set(:@application_data, framed_content.application_data)
|
|
957
1064
|
when Melos::Constants::ContentType::PROPOSAL
|
|
958
|
-
|
|
959
|
-
context << [:proposal, value]
|
|
1065
|
+
new_instance.instance_variable_set(:@proposal, framed_content.proposal)
|
|
960
1066
|
when Melos::Constants::ContentType::COMMIT
|
|
961
|
-
|
|
962
|
-
context << [:commit, value]
|
|
1067
|
+
new_instance.instance_variable_set(:@commit, framed_content.commit)
|
|
963
1068
|
end
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
context << [:padding, stream.read()]
|
|
968
|
-
instance.send(:set_instance_vars, context)
|
|
969
|
-
instance
|
|
1069
|
+
new_instance.instance_variable_set(:@auth, auth)
|
|
1070
|
+
new_instance.instance_variable_set(:@padding, Melos::Crypto::Util.zero_vector(padding_size))
|
|
1071
|
+
new_instance
|
|
970
1072
|
end
|
|
971
1073
|
|
|
972
1074
|
def content
|
|
973
1075
|
@application_data || @proposal || @commit
|
|
974
1076
|
end
|
|
1077
|
+
|
|
1078
|
+
def padding_zero?
|
|
1079
|
+
@padding.each_byte.all?(&:zero?)
|
|
1080
|
+
end
|
|
975
1081
|
end
|
|
976
1082
|
|
|
977
1083
|
## 8.2
|
|
@@ -1012,9 +1118,21 @@ class Melos::Struct::MLSMessage < Melos::Struct::Base
|
|
|
1012
1118
|
[:key_package, :select, ->(ctx){ctx[:wire_format] == Melos::Constants::WireFormat::MLS_KEY_PACKAGE}, :class, Melos::Struct::KeyPackage]
|
|
1013
1119
|
]
|
|
1014
1120
|
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1121
|
+
# wrap exactly one of the message types; the wire format follows from which one is given
|
|
1122
|
+
def self.create(public_message: nil, private_message: nil, welcome: nil, group_info: nil, key_package: nil)
|
|
1123
|
+
given = {
|
|
1124
|
+
public_message: [Melos::Constants::WireFormat::MLS_PUBLIC_MESSAGE, public_message],
|
|
1125
|
+
private_message: [Melos::Constants::WireFormat::MLS_PRIVATE_MESSAGE, private_message],
|
|
1126
|
+
welcome: [Melos::Constants::WireFormat::MLS_WELCOME, welcome],
|
|
1127
|
+
group_info: [Melos::Constants::WireFormat::MLS_GROUP_INFO, group_info],
|
|
1128
|
+
key_package: [Melos::Constants::WireFormat::MLS_KEY_PACKAGE, key_package]
|
|
1129
|
+
}.reject { |_, (_, value)| value.nil? }
|
|
1130
|
+
raise ArgumentError.new('exactly one message must be given') unless given.size == 1
|
|
1131
|
+
field, (wire_format, value) = given.first
|
|
1132
|
+
instance = allocate
|
|
1133
|
+
instance.instance_variable_set(:@version, Melos::Constants::Version::MLS10)
|
|
1134
|
+
instance.instance_variable_set(:@wire_format, wire_format)
|
|
1135
|
+
instance.instance_variable_set("@#{field}", value)
|
|
1136
|
+
instance
|
|
1019
1137
|
end
|
|
1020
1138
|
end
|
data/lib/melos/tree.rb
CHANGED
|
@@ -1,83 +1,7 @@
|
|
|
1
|
+
# Tree math over node indexes in the array representation of a left-balanced binary tree (RFC 9420 Appendix C).
|
|
2
|
+
# Functions that depend on the tree's contents live on Melos::RatchetTree.
|
|
1
3
|
class Melos::Tree
|
|
2
|
-
attr_accessor :array, :leaf_count
|
|
3
|
-
# attr_reader @array
|
|
4
|
-
|
|
5
|
-
def initialize
|
|
6
|
-
@array = []
|
|
7
|
-
@leaf_count = 0
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
def self.empty_tree(n_leaves)
|
|
11
|
-
instance = self.allocate
|
|
12
|
-
instance.leaf_count = n_leaves
|
|
13
|
-
instance.array = Array.new(node_width(n_leaves))
|
|
14
|
-
instance
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
def add(val)
|
|
18
|
-
raise ArgumentError.new('Cannot add nil element') if val.nil?
|
|
19
|
-
if @leaf_count == 0
|
|
20
|
-
# initialize tree with one node
|
|
21
|
-
@array = [val]
|
|
22
|
-
@leaf_count = 1
|
|
23
|
-
else
|
|
24
|
-
# find leftmost empty leaf
|
|
25
|
-
extend_tree = true
|
|
26
|
-
for k in 0 .. (@leaf_count - 1) do
|
|
27
|
-
if @array[k * 2].nil?
|
|
28
|
-
@array[k * 2] = val
|
|
29
|
-
extend_tree = false
|
|
30
|
-
break
|
|
31
|
-
end
|
|
32
|
-
end
|
|
33
|
-
if extend_tree
|
|
34
|
-
# if tree is full, extend
|
|
35
|
-
@leaf_count = @leaf_count * 2
|
|
36
|
-
for k in 0 .. @leaf_count - 2
|
|
37
|
-
@array[@leaf_count + k] = nil
|
|
38
|
-
end
|
|
39
|
-
@array[@leaf_count] = val
|
|
40
|
-
end
|
|
41
|
-
end
|
|
42
|
-
@array
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
def remove_leaf(leaf_idx)
|
|
46
|
-
raise ArgumentError.new('Cannot remove from empty tree') if @leaf_count == 0
|
|
47
|
-
remove_node(leaf_idx * 2)
|
|
48
|
-
# then if rigbt half of the tree is empty, truncate tree
|
|
49
|
-
# q: do we recursively shrink tree?
|
|
50
|
-
right_tree_empty = true
|
|
51
|
-
for i in 0 .. (@leaf_count / 2) - 1 do
|
|
52
|
-
if !@array[@leaf_count + 2 * i].nil?
|
|
53
|
-
right_tree_empty = false
|
|
54
|
-
break
|
|
55
|
-
end
|
|
56
|
-
end
|
|
57
|
-
if right_tree_empty
|
|
58
|
-
@array = @array.first(@leaf_count - 1)
|
|
59
|
-
@leaf_count = @leaf_count / 2
|
|
60
|
-
end
|
|
61
|
-
@array
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
def remove_node(node_idx)
|
|
65
|
-
@array[node_idx] = nil
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
def root
|
|
69
|
-
self.class.root(@leaf_count)
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
def leaf_at(leaf_index)
|
|
73
|
-
@array[leaf_index * 2]
|
|
74
|
-
end
|
|
75
|
-
|
|
76
4
|
class << self
|
|
77
|
-
def n_leaves(tree)
|
|
78
|
-
(tree.size + 1) / 2
|
|
79
|
-
end
|
|
80
|
-
|
|
81
5
|
def log2(x)
|
|
82
6
|
if x == 0
|
|
83
7
|
return 0
|
|
@@ -184,7 +108,7 @@ class Melos::Tree
|
|
|
184
108
|
raise ArgumentError.new('Failed to find common ancestor')
|
|
185
109
|
end
|
|
186
110
|
|
|
187
|
-
dxy.
|
|
111
|
+
dxy.min_by { level(_1) }
|
|
188
112
|
end
|
|
189
113
|
|
|
190
114
|
def overlap_with_filtered_direct_path(x, filtered_direct_path, n)
|
|
@@ -195,53 +119,13 @@ class Melos::Tree
|
|
|
195
119
|
raise ArgumentError.new('Failed to find overlap')
|
|
196
120
|
end
|
|
197
121
|
|
|
198
|
-
dxf.
|
|
122
|
+
dxf.min_by { level(_1) }
|
|
199
123
|
end
|
|
200
124
|
|
|
201
125
|
def leaf?(node_index)
|
|
202
126
|
node_index % 2 == 0
|
|
203
127
|
end
|
|
204
128
|
|
|
205
|
-
def truncate!(tree)
|
|
206
|
-
root = root(n_leaves(tree))
|
|
207
|
-
if tree[(root + 1)..]&.all?(nil)
|
|
208
|
-
tree.slice!(root..) # keep left half of tree
|
|
209
|
-
truncate!(tree) # then attempt to truncate again
|
|
210
|
-
end
|
|
211
|
-
# right half of tree has an element, so finish
|
|
212
|
-
end
|
|
213
|
-
|
|
214
|
-
def filtered_direct_path(tree, node_index)
|
|
215
|
-
n_l = n_leaves(tree)
|
|
216
|
-
direct_path(node_index, n_l).reject { resolution(tree, sibling_from_leaf(node_index, _1, n_l)) == [] }
|
|
217
|
-
end
|
|
218
|
-
|
|
219
|
-
def copath_nodes_of_filtered_direct_path(tree, node_index)
|
|
220
|
-
filtered_direct_path(tree, node_index).map do |a|
|
|
221
|
-
sibling_from_leaf(node_index, a, n_leaves(tree))
|
|
222
|
-
end
|
|
223
|
-
end
|
|
224
|
-
|
|
225
|
-
def resolution(tree, node_index)
|
|
226
|
-
node = tree[node_index]
|
|
227
|
-
if node.nil?
|
|
228
|
-
if Melos::Tree.leaf?(node_index)
|
|
229
|
-
# The resolution of a blank leaf node is the empty list.
|
|
230
|
-
[]
|
|
231
|
-
else
|
|
232
|
-
# The resolution of a blank intermediate node is the result of concatenating the resolution of its left child with the resolution of its right child, in that order.
|
|
233
|
-
resolution(tree, Melos::Tree.left(node_index)) + resolution(tree, Melos::Tree.right(node_index))
|
|
234
|
-
end
|
|
235
|
-
else
|
|
236
|
-
# The resolution of a non-blank node comprises the node itself, followed by its list of unmerged leaves, if any.
|
|
237
|
-
if node.parent_node
|
|
238
|
-
[node_index] + node.parent_node.unmerged_leaves.map { _1 * 2} # convert leaf index to node index
|
|
239
|
-
else
|
|
240
|
-
[node_index]
|
|
241
|
-
end
|
|
242
|
-
end
|
|
243
|
-
end
|
|
244
|
-
|
|
245
129
|
# def common_ancestor_direct(x, y)
|
|
246
130
|
# lx = level(x) + 1
|
|
247
131
|
# ly = level(y) + 1
|