melos 0.1.0 → 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.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +18 -10
  3. data/docs/roadmap.md +302 -0
  4. data/docs/validation.md +22 -18
  5. data/lib/melos/capabilities.rb +5 -2
  6. data/lib/melos/client.rb +184 -14
  7. data/lib/melos/credential/x509.rb +91 -0
  8. data/lib/melos/crypto.rb +25 -0
  9. data/lib/melos/epoch_secrets.rb +61 -8
  10. data/lib/melos/errors.rb +18 -0
  11. data/lib/melos/external_sender.rb +78 -0
  12. data/lib/melos/group/validation.rb +123 -50
  13. data/lib/melos/group.rb +597 -104
  14. data/lib/melos/key_package_bundle.rb +8 -0
  15. data/lib/melos/persistence.rb +45 -0
  16. data/lib/melos/private_tree.rb +23 -4
  17. data/lib/melos/ratchet_tree.rb +6 -6
  18. data/lib/melos/secret_tree.rb +87 -28
  19. data/lib/melos/struct/structs.rb +45 -2
  20. data/lib/melos/vec.rb +6 -6
  21. data/lib/melos/version.rb +1 -1
  22. data/lib/melos.rb +4 -0
  23. metadata +6 -17
  24. data/test_vectors/crypto-basics.json +0 -303
  25. data/test_vectors/deserialization.json +0 -58
  26. data/test_vectors/key-schedule.json +0 -926
  27. data/test_vectors/message-protection.json +0 -142
  28. data/test_vectors/messages.json +0 -5702
  29. data/test_vectors/passive-client-handling-commit.json +0 -2683
  30. data/test_vectors/passive-client-random.json +0 -2657
  31. data/test_vectors/passive-client-welcome.json +0 -814
  32. data/test_vectors/psk_secret.json +0 -2382
  33. data/test_vectors/secret-tree.json +0 -4846
  34. data/test_vectors/transcript-hashes.json +0 -58
  35. data/test_vectors/tree-math.json +0 -8276
  36. data/test_vectors/tree-operations.json +0 -47
  37. data/test_vectors/tree-validation.json +0 -11839
  38. data/test_vectors/treekem.json +0 -14877
  39. data/test_vectors/welcome.json +0 -51
@@ -47,6 +47,14 @@ class Melos::KeyPackageBundle
47
47
  Melos::Crypto.serialize_private_key(suite, encryption_key_pair))
48
48
  end
49
49
 
50
+ def to_h
51
+ { key_package: @key_package.raw, init_private_key: @init_private_key, encryption_private_key: @encryption_private_key }
52
+ end
53
+
54
+ def self.from_h(suite, hash)
55
+ new(suite, Melos::Struct::KeyPackage.new(hash[:key_package]), hash[:init_private_key], hash[:encryption_private_key])
56
+ end
57
+
50
58
  # 5.2: the KeyPackageRef a Welcome uses to address this KeyPackage
51
59
  def ref
52
60
  @key_package.ref(@suite)
@@ -0,0 +1,45 @@
1
+ require 'json'
2
+
3
+ # Turning the state Hashes of Melos::Client#to_h and Melos::Group#to_h into JSON and back.
4
+ # Those Hashes have Symbol keys, and their Strings are binary; here every String becomes hex and every
5
+ # key a JSON string, and the reverse on load. Marshal, MessagePack or anything else that keeps
6
+ # binary Strings works on the Hashes as they are.
7
+ #
8
+ # The state holds private keys and epoch secrets. Encrypting it at rest is the application's job.
9
+ module Melos::Persistence
10
+ extend self
11
+
12
+ def dump_json(hash)
13
+ JSON.generate(encode(hash))
14
+ end
15
+
16
+ def load_json(json)
17
+ decode(JSON.parse(json))
18
+ end
19
+
20
+ def dump_client(client) = dump_json(client.to_h)
21
+ def load_client(json, **group_options) = Melos::Client.from_h(load_json(json), **group_options)
22
+ def dump_group(group) = dump_json(group.to_h)
23
+ def load_group(json, **options) = Melos::Group.from_h(load_json(json), **options)
24
+
25
+ private
26
+
27
+ def encode(value)
28
+ case value
29
+ when Hash then value.to_h { |key, v| [key.to_s, encode(v)] }
30
+ when Array then value.map { encode(_1) }
31
+ when String then value.unpack1('H*')
32
+ when Integer, true, false, nil then value
33
+ else raise ArgumentError.new("cannot encode #{value.class}")
34
+ end
35
+ end
36
+
37
+ def decode(value)
38
+ case value
39
+ when Hash then value.to_h { |key, v| [key.to_sym, decode(v)] }
40
+ when Array then value.map { decode(_1) }
41
+ when String then [value].pack('H*')
42
+ else value
43
+ end
44
+ end
45
+ end
@@ -28,6 +28,15 @@ class Melos::PrivateTree
28
28
  self.class.new(@keys.dup)
29
29
  end
30
30
 
31
+ # [[node_index, private key], ...]
32
+ def to_a
33
+ @keys.sort.to_a
34
+ end
35
+
36
+ def self.from_a(pairs)
37
+ new(pairs.to_h)
38
+ end
39
+
31
40
  # 7.4: starting with path_secret at start_node_index, derive the node private key there and at
32
41
  # every node up its filtered direct path on ratchet_tree, each from the next path secret
33
42
  def set_path_secret(ratchet_tree, start_node_index, path_secret)
@@ -41,11 +50,21 @@ class Melos::PrivateTree
41
50
  end
42
51
  end
43
52
 
53
+ # 9.2, 7.5: drop every private key whose node is now blank, gone, or carries a different public key
54
+ def prune!(ratchet_tree)
55
+ @keys.select! { |node_index, private_key| matches_node?(ratchet_tree, node_index, private_key) }
56
+ self
57
+ end
58
+
44
59
  # every private key here belongs to a non-blank node of ratchet_tree whose public key it matches
45
60
  def consistent?(ratchet_tree)
46
- @keys.all? do |node_index, private_key|
47
- node = ratchet_tree[node_index]
48
- !node.nil? && Melos::Crypto.encapsulation_key_pair_corresponds?(ratchet_tree.suite, private_key, node.public_encryption_key)
49
- end
61
+ @keys.all? { |node_index, private_key| matches_node?(ratchet_tree, node_index, private_key) }
62
+ end
63
+
64
+ private
65
+
66
+ def matches_node?(ratchet_tree, node_index, private_key)
67
+ node = ratchet_tree[node_index]
68
+ !node.nil? && Melos::Crypto.encapsulation_key_pair_corresponds?(ratchet_tree.suite, private_key, node.public_encryption_key)
50
69
  end
51
70
  end
@@ -37,7 +37,7 @@ class Melos::RatchetTree
37
37
  presence = data_stream.read(1).unpack1('C')
38
38
  nodes << (presence == 1 ? Melos::Struct::Node.new(data_stream) : nil)
39
39
  end
40
- raise ArgumentError.new('last node of ratchet_tree must not be blank') if !nodes.empty? && nodes.last.nil?
40
+ raise Melos::ProtocolError.new('last node of ratchet_tree must not be blank') if !nodes.empty? && nodes.last.nil?
41
41
  new(suite, nodes)
42
42
  end
43
43
 
@@ -250,7 +250,7 @@ class Melos::RatchetTree
250
250
  def merge_update_path(leaf_index, update_path)
251
251
  node_index = leaf_index * 2
252
252
  fdp = filtered_direct_path(node_index)
253
- raise ArgumentError.new('malformed update path') unless fdp.count == update_path.nodes.count
253
+ raise Melos::ProtocolError.new('malformed update path') unless fdp.count == update_path.nodes.count
254
254
  parent_hashes = calculate_parent_hashes(leaf_index, update_path.nodes)
255
255
  fdp.each_with_index do |fdp_node_index, i|
256
256
  parent_node = Melos::Struct::ParentNode.create(
@@ -342,16 +342,16 @@ class Melos::RatchetTree
342
342
  # Returns the decrypted path secret, for #calculate_commit_secret.
343
343
  def decrypt_path_secret(private_tree, update_path, sender_leaf_index, receiver_leaf_index, group_context, leaves_to_remove = [])
344
344
  fdp = filtered_direct_path(sender_leaf_index * 2)
345
- raise ArgumentError.new('malformed update path') unless fdp.count == update_path.nodes.count
345
+ raise Melos::ProtocolError.new('malformed update path') unless fdp.count == update_path.nodes.count
346
346
  overlap_node_index = Melos::Tree.overlap_with_filtered_direct_path(receiver_leaf_index * 2, fdp, n_leaves)
347
347
  overlap_index = fdp.index(overlap_node_index)
348
348
  copath_node_index = copath_nodes_of_filtered_direct_path(sender_leaf_index * 2)[overlap_index]
349
349
  resolution = resolution(copath_node_index) - leaves_to_remove.map { _1 * 2 } # leaf index -> node index
350
350
  update_path_node = update_path.nodes[overlap_index]
351
- raise ArgumentError.new('# of resolution of copath node does not match with # of encrypted path secrets') unless update_path_node.encrypted_path_secret.count == resolution.count
351
+ raise Melos::ProtocolError.new('# of resolution of copath node does not match with # of encrypted path secrets') unless update_path_node.encrypted_path_secret.count == resolution.count
352
352
 
353
353
  priv_index = resolution.rindex { private_tree[_1] }
354
- raise ArgumentError.new("priv key not found in tree (sender leaf index: #{sender_leaf_index})") if priv_index.nil?
354
+ raise Melos::ProtocolError.new("priv key not found in tree (sender leaf index: #{sender_leaf_index})") if priv_index.nil?
355
355
  encrypted_path_secret = update_path_node.encrypted_path_secret[priv_index]
356
356
  path_secret = Melos::Crypto.decrypt_with_label(
357
357
  @suite, private_tree[resolution[priv_index]], "UpdatePathNode", group_context.raw,
@@ -365,7 +365,7 @@ class Melos::RatchetTree
365
365
  # 7.4: the commit secret is the path secret one derivation past the last node of the sender's filtered direct path
366
366
  def calculate_commit_secret(update_path, sender_leaf_index, receiver_leaf_index, path_secret)
367
367
  fdp = filtered_direct_path(sender_leaf_index * 2)
368
- raise ArgumentError.new('malformed update path') unless fdp.count == update_path.nodes.count
368
+ raise Melos::ProtocolError.new('malformed update path') unless fdp.count == update_path.nodes.count
369
369
  overlap_node_index = Melos::Tree.overlap_with_filtered_direct_path(receiver_leaf_index * 2, fdp, n_leaves)
370
370
  secret = path_secret
371
371
  (fdp.count - 1 - fdp.index(overlap_node_index)).times do
@@ -6,16 +6,43 @@ require_relative 'constants'
6
6
  # encryption_secret, with the same shape as the ratchet tree. Only the two hash ratchets
7
7
  # at each leaf are kept; the node secrets above them are discarded once derived.
8
8
  class Melos::SecretTree
9
- # 9.1: a hash ratchet over one leaf's secret. Each generation yields a key and nonce
10
- # and steps the secret forward; older secrets are forgotten.
9
+ # 15.3: how far one message may move a receiving ratchet forward, and how many keys of skipped
10
+ # generations a ratchet keeps for messages that arrive out of order. Both bound the work and memory
11
+ # a single message can cost; the RFC leaves the values to the application.
12
+ DEFAULT_MAX_FORWARD_DISTANCE = 1000
13
+ DEFAULT_OUT_OF_ORDER_TOLERANCE = 100
14
+
15
+ # 9.1: a hash ratchet over one leaf's secret. Each generation yields a key and nonce once and
16
+ # steps the secret forward; per 9.2 neither the secret nor the key is kept once handed out.
17
+ # When a receiver skips ahead, the keys of the generations it skipped are kept (at most
18
+ # out_of_order_tolerance of them, oldest dropped first) so that late messages still decrypt (9.1, 15.3).
11
19
  class Ratchet
12
- attr_reader :next_generation
20
+ attr_reader :next_generation, :max_forward_distance, :out_of_order_tolerance
13
21
 
14
- def initialize(suite, secret)
22
+ def initialize(suite, secret, max_forward_distance: DEFAULT_MAX_FORWARD_DISTANCE, out_of_order_tolerance: DEFAULT_OUT_OF_ORDER_TOLERANCE)
15
23
  @suite = suite
16
24
  @secret = secret
17
25
  @next_generation = 0
18
- @current = nil # [key, nonce, generation] of the last generation derived
26
+ @skipped = {} # generation -> [key, nonce], in increasing generation order
27
+ @max_forward_distance = max_forward_distance
28
+ @out_of_order_tolerance = out_of_order_tolerance
29
+ end
30
+
31
+ # generations below next_generation whose keys are still held
32
+ def skipped_generations
33
+ @skipped.keys
34
+ end
35
+
36
+ # the ratchet's whole state (for persistence), as a Hash of binary strings and integers
37
+ def to_h
38
+ { secret: @secret, next_generation: @next_generation, skipped: @skipped.map { |generation, (key, nonce)| [generation, key, nonce] } }
39
+ end
40
+
41
+ def self.from_h(suite, hash, **options)
42
+ ratchet = new(suite, hash[:secret], **options)
43
+ ratchet.instance_variable_set(:@next_generation, hash[:next_generation])
44
+ ratchet.instance_variable_set(:@skipped, hash[:skipped].to_h { |generation, key, nonce| [generation, [key, nonce]] })
45
+ ratchet
19
46
  end
20
47
 
21
48
  # derive the key and nonce of the current generation and step the ratchet
@@ -25,30 +52,62 @@ class Melos::SecretTree
25
52
  key = Melos::Crypto.derive_tree_secret(@suite, @secret, "key", generation, @suite.hpke.n_k)
26
53
  @secret = Melos::Crypto.derive_tree_secret(@suite, @secret, "secret", generation, @suite.kdf.n_h)
27
54
  @next_generation += 1
28
- @current = [key, nonce, generation]
55
+ [key, nonce, generation]
29
56
  end
30
57
 
31
- # the key and nonce of the given generation, ratcheting forward as needed.
32
- # TODO (9.1): keep a window of unconsumed generations so that out-of-order messages can be decrypted
58
+ # the key and nonce of the given generation, ratcheting forward as needed. An earlier generation
59
+ # is served once from the skipped keys; one that was already used, or dropped from the window,
60
+ # cannot be derived again (StateError). A jump further than max_forward_distance is refused (ProtocolError).
33
61
  def ratchet_until(generation)
34
- if generation < @next_generation - 1
35
- raise ArgumentError.new("generation #{generation} has already been consumed (next generation is #{@next_generation})")
62
+ if generation < @next_generation
63
+ key_and_nonce = @skipped.delete(generation)
64
+ return [*key_and_nonce, generation] if key_and_nonce
65
+ raise Melos::StateError.new("generation #{generation} has already been consumed or is no longer kept (next generation is #{@next_generation})")
66
+ end
67
+ if generation - @next_generation > @max_forward_distance
68
+ raise Melos::ProtocolError.new("generation #{generation} is more than #{@max_forward_distance} ahead of #{@next_generation}")
36
69
  end
37
- ratchet while @next_generation <= generation
38
- @current
70
+ while @next_generation < generation
71
+ key, nonce, skipped = ratchet
72
+ @skipped[skipped] = [key, nonce]
73
+ end
74
+ @skipped.shift while @skipped.size > @out_of_order_tolerance
75
+ ratchet
39
76
  end
40
77
  end
41
78
 
42
79
  attr_reader :suite, :n_leaves
43
80
 
44
- def initialize(suite, n_leaves, encryption_secret)
81
+ # options: max_forward_distance:, out_of_order_tolerance: for every ratchet (see Ratchet)
82
+ def initialize(suite, n_leaves, encryption_secret, **options)
45
83
  @suite = suite
46
84
  @n_leaves = n_leaves
85
+ @options = options
47
86
  @handshake_ratchets = [] # by leaf index
48
87
  @application_ratchets = [] # by leaf index
49
88
  populate(Melos::Tree.root(n_leaves), encryption_secret)
50
89
  end
51
90
 
91
+ # the ratchet positions, skipped keys and options, as a Hash of binary strings and integers (6.3.1:
92
+ # a client that restarts without its ratchet state would reuse nonces)
93
+ def to_h
94
+ {
95
+ n_leaves: @n_leaves, options: @options,
96
+ handshake: @handshake_ratchets.map(&:to_h), application: @application_ratchets.map(&:to_h)
97
+ }
98
+ end
99
+
100
+ def self.from_h(suite, hash)
101
+ tree = allocate
102
+ options = hash[:options] || {}
103
+ tree.instance_variable_set(:@suite, suite)
104
+ tree.instance_variable_set(:@n_leaves, hash[:n_leaves])
105
+ tree.instance_variable_set(:@options, options)
106
+ tree.instance_variable_set(:@handshake_ratchets, hash[:handshake].map { Ratchet.from_h(suite, _1, **options) })
107
+ tree.instance_variable_set(:@application_ratchets, hash[:application].map { Ratchet.from_h(suite, _1, **options) })
108
+ tree
109
+ end
110
+
52
111
  # sender side: the key and nonce of the next generation for this leaf and content type
53
112
  # returns [key, nonce, generation]
54
113
  def ratchet_and_get(content_type, leaf_index)
@@ -61,14 +120,27 @@ class Melos::SecretTree
61
120
  ratchet_for(content_type, leaf_index).ratchet_until(generation)
62
121
  end
63
122
 
123
+ # 9.1: handshake messages (proposals and commits) and application messages use separate ratchets
124
+ def ratchet_for(content_type, leaf_index)
125
+ ratchets = case content_type
126
+ when Melos::Constants::ContentType::PROPOSAL, Melos::Constants::ContentType::COMMIT
127
+ @handshake_ratchets
128
+ when Melos::Constants::ContentType::APPLICATION
129
+ @application_ratchets
130
+ else
131
+ raise ArgumentError.new("unknown content type #{content_type}")
132
+ end
133
+ ratchets[leaf_index] or raise Melos::ProtocolError.new("no leaf #{leaf_index} in secret tree with #{@n_leaves} leaves")
134
+ end
135
+
64
136
  private
65
137
 
66
138
  # 9: derive the secrets of the subtree under node_index down to the leaves
67
139
  def populate(node_index, secret)
68
140
  if Melos::Tree.leaf?(node_index)
69
141
  leaf_index = node_index / 2
70
- @handshake_ratchets[leaf_index] = Ratchet.new(@suite, Melos::Crypto.expand_with_label(@suite, secret, "handshake", "", @suite.kdf.n_h))
71
- @application_ratchets[leaf_index] = Ratchet.new(@suite, Melos::Crypto.expand_with_label(@suite, secret, "application", "", @suite.kdf.n_h))
142
+ @handshake_ratchets[leaf_index] = Ratchet.new(@suite, Melos::Crypto.expand_with_label(@suite, secret, "handshake", "", @suite.kdf.n_h), **@options)
143
+ @application_ratchets[leaf_index] = Ratchet.new(@suite, Melos::Crypto.expand_with_label(@suite, secret, "application", "", @suite.kdf.n_h), **@options)
72
144
  else
73
145
  left_secret = Melos::Crypto.expand_with_label(@suite, secret, "tree", "left", @suite.kdf.n_h)
74
146
  right_secret = Melos::Crypto.expand_with_label(@suite, secret, "tree", "right", @suite.kdf.n_h)
@@ -76,17 +148,4 @@ class Melos::SecretTree
76
148
  populate(Melos::Tree.right(node_index), right_secret)
77
149
  end
78
150
  end
79
-
80
- # 9.1: handshake messages (proposals and commits) and application messages use separate ratchets
81
- def ratchet_for(content_type, leaf_index)
82
- ratchets = case content_type
83
- when Melos::Constants::ContentType::PROPOSAL, Melos::Constants::ContentType::COMMIT
84
- @handshake_ratchets
85
- when Melos::Constants::ContentType::APPLICATION
86
- @application_ratchets
87
- else
88
- raise ArgumentError.new("unknown content type #{content_type}")
89
- end
90
- ratchets[leaf_index] or raise ArgumentError.new("no leaf #{leaf_index} in secret tree with #{@n_leaves} leaves")
91
- end
92
151
  end
@@ -139,7 +139,7 @@ class Melos::Struct::Extension < Melos::Struct::Base
139
139
  def self.check_unique_types!(extensions, where)
140
140
  duplicates = extensions.map(&:extension_type).tally.select { |_type, count| count > 1 }.keys
141
141
  return if duplicates.empty?
142
- raise ArgumentError.new("#{where} has more than one extension of type #{duplicates.map { '0x%04x' % _1 }.join(', ')}")
142
+ raise Melos::ProtocolError.new("#{where} has more than one extension of type #{duplicates.map { '0x%04x' % _1 }.join(', ')}")
143
143
  end
144
144
  end
145
145
 
@@ -350,6 +350,26 @@ class Melos::Struct::PreSharedKeyID < Melos::Struct::Base
350
350
  new_instance.instance_variable_set(:@psk_nonce, psk_nonce)
351
351
  new_instance
352
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
353
373
  end
354
374
 
355
375
  class Melos::Struct::PSKLabel < Melos::Struct::Base
@@ -500,6 +520,21 @@ class Melos::Struct::ExternalSender < Melos::Struct::Base
500
520
  ]
501
521
  end
502
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
+
503
538
  ## 12.1
504
539
 
505
540
  class Melos::Struct::Proposal < Melos::Struct::Base
@@ -710,6 +745,14 @@ class Melos::Struct::Sender < Melos::Struct::Base
710
745
  instance.instance_variable_set(:@sender_index, sender_index)
711
746
  instance
712
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
713
756
  end
714
757
 
715
758
  # The select in FramedContentAuthData depends on FramedContent.content_type, a field of
@@ -963,7 +1006,7 @@ class Melos::Struct::PrivateMessage < Melos::Struct::Base
963
1006
  stream = StringIO.new(Melos::Crypto.aead_decrypt(suite, key, new_nonce, private_content_aad, ciphertext))
964
1007
  pmc = Melos::Struct::PrivateMessageContent.new(stream, content_type: content_type)
965
1008
  # 6.3.1: the padding MUST be all zero bytes, otherwise the message is malformed
966
- raise ArgumentError.new('PrivateMessage padding is not all zero') unless pmc.padding_zero?
1009
+ raise Melos::ProtocolError.new('PrivateMessage padding is not all zero') unless pmc.padding_zero?
967
1010
 
968
1011
  fc = Melos::Struct::FramedContent.create(
969
1012
  group_id: group_id,
data/lib/melos/vec.rb CHANGED
@@ -2,7 +2,7 @@ module Melos::Vec
2
2
  extend self
3
3
 
4
4
  def read_varint(data)
5
- raise ArgumentError.new('variable-length vector is truncated') if data.nil? || data.empty?
5
+ raise Melos::ProtocolError.new('variable-length vector is truncated') if data.nil? || data.empty?
6
6
  byte = 0
7
7
  v = data[byte].ord
8
8
  prefix = v >> 6
@@ -11,7 +11,7 @@ module Melos::Vec
11
11
  v = v & 0x3f
12
12
  (length - 1).times do
13
13
  byte += 1
14
- raise ArgumentError.new('variable-length vector is truncated') if data[byte].nil?
14
+ raise Melos::ProtocolError.new('variable-length vector is truncated') if data[byte].nil?
15
15
  v = (v << 8) + data[byte].ord
16
16
  end
17
17
 
@@ -46,14 +46,14 @@ module Melos::Vec
46
46
  # sig { params(stream: StringIO).returns(String) }
47
47
  def parse_stream(stream)
48
48
  prefix = prefix_from_stream(stream)
49
- raise ArgumentError.new('invalid header') unless (0..2).include?(prefix) # 2.1.2: prefix 3 is invalid
49
+ raise Melos::ProtocolError.new('invalid header') unless (0..2).include?(prefix) # 2.1.2: prefix 3 is invalid
50
50
  header_length = 2 ** prefix
51
51
  length = length_from_stream(stream)
52
52
  # 2.1.2: the length MUST be encoded in the minimum number of bytes; a longer encoding is rejected
53
- raise ArgumentError.new('variable-length header is not minimal') unless write_varint(length).bytesize == header_length
53
+ raise Melos::ProtocolError.new('variable-length header is not minimal') unless write_varint(length).bytesize == header_length
54
54
  stream.pos += header_length
55
55
  str = stream.read(length) || ''
56
- raise ArgumentError.new('variable-length vector is truncated') if str.bytesize < length
56
+ raise Melos::ProtocolError.new('variable-length vector is truncated') if str.bytesize < length
57
57
  str
58
58
  end
59
59
 
@@ -62,7 +62,7 @@ module Melos::Vec
62
62
  def prefix_from_stream(stringio)
63
63
  pos = stringio.pos
64
64
  first = stringio.getbyte
65
- raise ArgumentError.new('variable-length vector is truncated') if first.nil?
65
+ raise Melos::ProtocolError.new('variable-length vector is truncated') if first.nil?
66
66
  prefix_ = first >> 6
67
67
  stringio.seek(pos)
68
68
  prefix_
data/lib/melos/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Melos
4
- VERSION = "0.1.0"
4
+ VERSION = "0.9.0"
5
5
  end
data/lib/melos.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'stringio'
2
+ require_relative 'melos/errors'
2
3
 
3
4
  module Melos
4
5
  MLS_VERSION = 0x01 # mls10
@@ -16,6 +17,9 @@ require_relative 'melos/secret_tree'
16
17
  require_relative 'melos/capabilities'
17
18
  require_relative 'melos/key_package_bundle'
18
19
  require_relative 'melos/client'
20
+ require_relative 'melos/external_sender'
21
+ require_relative 'melos/persistence'
22
+ require_relative 'melos/credential/x509'
19
23
  require_relative 'melos/struct/base'
20
24
  require_relative 'melos/struct/structs'
21
25
  require_relative 'melos/ratchet_tree'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: melos
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Kajiwara
@@ -53,16 +53,21 @@ files:
53
53
  - LICENSE.txt
54
54
  - README.md
55
55
  - Rakefile
56
+ - docs/roadmap.md
56
57
  - docs/validation.md
57
58
  - lib/melos.rb
58
59
  - lib/melos/capabilities.rb
59
60
  - lib/melos/client.rb
60
61
  - lib/melos/constants.rb
62
+ - lib/melos/credential/x509.rb
61
63
  - lib/melos/crypto.rb
62
64
  - lib/melos/epoch_secrets.rb
65
+ - lib/melos/errors.rb
66
+ - lib/melos/external_sender.rb
63
67
  - lib/melos/group.rb
64
68
  - lib/melos/group/validation.rb
65
69
  - lib/melos/key_package_bundle.rb
70
+ - lib/melos/persistence.rb
66
71
  - lib/melos/private_tree.rb
67
72
  - lib/melos/psk.rb
68
73
  - lib/melos/ratchet_tree.rb
@@ -74,22 +79,6 @@ files:
74
79
  - lib/melos/vec.rb
75
80
  - lib/melos/version.rb
76
81
  - sig/melos.rbs
77
- - test_vectors/crypto-basics.json
78
- - test_vectors/deserialization.json
79
- - test_vectors/key-schedule.json
80
- - test_vectors/message-protection.json
81
- - test_vectors/messages.json
82
- - test_vectors/passive-client-handling-commit.json
83
- - test_vectors/passive-client-random.json
84
- - test_vectors/passive-client-welcome.json
85
- - test_vectors/psk_secret.json
86
- - test_vectors/secret-tree.json
87
- - test_vectors/transcript-hashes.json
88
- - test_vectors/tree-math.json
89
- - test_vectors/tree-operations.json
90
- - test_vectors/tree-validation.json
91
- - test_vectors/treekem.json
92
- - test_vectors/welcome.json
93
82
  homepage: https://github.com/sylph01/melos
94
83
  licenses:
95
84
  - MIT