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/secret_tree.rb
CHANGED
|
@@ -1,109 +1,151 @@
|
|
|
1
1
|
require_relative 'tree'
|
|
2
2
|
require_relative 'crypto'
|
|
3
|
+
require_relative 'constants'
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
# The secret tree (RFC 9420 Section 9): the per-epoch tree of secrets derived from the
|
|
6
|
+
# encryption_secret, with the same shape as the ratchet tree. Only the two hash ratchets
|
|
7
|
+
# at each leaf are kept; the node secrets above them are discarded once derived.
|
|
8
|
+
class Melos::SecretTree
|
|
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).
|
|
19
|
+
class Ratchet
|
|
20
|
+
attr_reader :next_generation, :max_forward_distance, :out_of_order_tolerance
|
|
21
|
+
|
|
22
|
+
def initialize(suite, secret, max_forward_distance: DEFAULT_MAX_FORWARD_DISTANCE, out_of_order_tolerance: DEFAULT_OUT_OF_ORDER_TOLERANCE)
|
|
23
|
+
@suite = suite
|
|
24
|
+
@secret = secret
|
|
25
|
+
@next_generation = 0
|
|
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
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# derive the key and nonce of the current generation and step the ratchet
|
|
49
|
+
def ratchet
|
|
50
|
+
generation = @next_generation
|
|
51
|
+
nonce = Melos::Crypto.derive_tree_secret(@suite, @secret, "nonce", generation, @suite.hpke.n_n)
|
|
52
|
+
key = Melos::Crypto.derive_tree_secret(@suite, @secret, "key", generation, @suite.hpke.n_k)
|
|
53
|
+
@secret = Melos::Crypto.derive_tree_secret(@suite, @secret, "secret", generation, @suite.kdf.n_h)
|
|
54
|
+
@next_generation += 1
|
|
55
|
+
[key, nonce, generation]
|
|
56
|
+
end
|
|
57
|
+
|
|
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).
|
|
61
|
+
def ratchet_until(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}")
|
|
69
|
+
end
|
|
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
|
|
76
|
+
end
|
|
9
77
|
end
|
|
10
78
|
|
|
11
|
-
|
|
12
|
-
|
|
79
|
+
attr_reader :suite, :n_leaves
|
|
80
|
+
|
|
81
|
+
# options: max_forward_distance:, out_of_order_tolerance: for every ratchet (see Ratchet)
|
|
82
|
+
def initialize(suite, n_leaves, encryption_secret, **options)
|
|
83
|
+
@suite = suite
|
|
84
|
+
@n_leaves = n_leaves
|
|
85
|
+
@options = options
|
|
86
|
+
@handshake_ratchets = [] # by leaf index
|
|
87
|
+
@application_ratchets = [] # by leaf index
|
|
88
|
+
populate(Melos::Tree.root(n_leaves), encryption_secret)
|
|
13
89
|
end
|
|
14
90
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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)
|
|
21
97
|
}
|
|
22
|
-
unless Melos::Tree.leaf?(index)
|
|
23
|
-
left_secret = Melos::Crypto.expand_with_label(suite, secret, "tree", "left", suite.kdf.n_h)
|
|
24
|
-
right_secret = Melos::Crypto.expand_with_label(suite, secret, "tree", "right", suite.kdf.n_h)
|
|
25
|
-
populate_tree_impl(suite, tree, Melos::Tree.left(index), left_secret)
|
|
26
|
-
populate_tree_impl(suite, tree, Melos::Tree.right(index), right_secret)
|
|
27
|
-
end
|
|
28
98
|
end
|
|
29
99
|
|
|
30
|
-
def self.
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
]
|
|
40
|
-
else
|
|
41
|
-
ratchet_application_until(suite, tree, leaf_index, generation)
|
|
42
|
-
[
|
|
43
|
-
tree.leaf_at(leaf_index)['application_key'],
|
|
44
|
-
tree.leaf_at(leaf_index)['application_nonce'],
|
|
45
|
-
generation
|
|
46
|
-
]
|
|
47
|
-
end
|
|
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
|
|
48
109
|
end
|
|
49
110
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
ratchet_handshake(suite, tree, leaf_index)
|
|
55
|
-
[
|
|
56
|
-
tree.leaf_at(leaf_index)['handshake_key'],
|
|
57
|
-
tree.leaf_at(leaf_index)['handshake_nonce'],
|
|
58
|
-
tree.leaf_at(leaf_index)['next_handshake_ratchet_secret_generation'] - 1, # returns current generation
|
|
59
|
-
]
|
|
60
|
-
else
|
|
61
|
-
ratchet_application(suite, tree, leaf_index)
|
|
62
|
-
[
|
|
63
|
-
tree.leaf_at(leaf_index)['application_key'],
|
|
64
|
-
tree.leaf_at(leaf_index)['application_nonce'],
|
|
65
|
-
tree.leaf_at(leaf_index)['next_application_ratchet_secret_generation'] - 1, # returns current generation
|
|
66
|
-
]
|
|
67
|
-
end
|
|
111
|
+
# sender side: the key and nonce of the next generation for this leaf and content type
|
|
112
|
+
# returns [key, nonce, generation]
|
|
113
|
+
def ratchet_and_get(content_type, leaf_index)
|
|
114
|
+
ratchet_for(content_type, leaf_index).ratchet
|
|
68
115
|
end
|
|
69
116
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
117
|
+
# receiver side: the key and nonce of a given generation for this leaf and content type
|
|
118
|
+
# returns [key, nonce, generation]
|
|
119
|
+
def ratchet_until_and_get(content_type, leaf_index, generation)
|
|
120
|
+
ratchet_for(content_type, leaf_index).ratchet_until(generation)
|
|
74
121
|
end
|
|
75
122
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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")
|
|
82
134
|
end
|
|
83
135
|
|
|
84
|
-
|
|
85
|
-
node_index = leaf_index * 2
|
|
86
|
-
generation = tree.array[node_index]['next_application_ratchet_secret_generation']
|
|
87
|
-
application_ratchet_secret = tree.array[node_index]['application_ratchet_secret']
|
|
88
|
-
application_nonce = Melos::Crypto.derive_tree_secret(suite, application_ratchet_secret, "nonce", generation, suite.hpke.n_n)
|
|
89
|
-
application_key = Melos::Crypto.derive_tree_secret(suite, application_ratchet_secret, "key", generation, suite.hpke.n_k)
|
|
90
|
-
next_application_ratchet_secret = Melos::Crypto.derive_tree_secret(suite, application_ratchet_secret, "secret", generation, suite.kdf.n_h)
|
|
91
|
-
tree.array[node_index]['next_application_ratchet_secret_generation'] = generation + 1
|
|
92
|
-
tree.array[node_index]['application_ratchet_secret'] = next_application_ratchet_secret
|
|
93
|
-
tree.array[node_index]['application_nonce'] = application_nonce
|
|
94
|
-
tree.array[node_index]['application_key'] = application_key
|
|
95
|
-
end
|
|
136
|
+
private
|
|
96
137
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
138
|
+
# 9: derive the secrets of the subtree under node_index down to the leaves
|
|
139
|
+
def populate(node_index, secret)
|
|
140
|
+
if Melos::Tree.leaf?(node_index)
|
|
141
|
+
leaf_index = node_index / 2
|
|
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)
|
|
144
|
+
else
|
|
145
|
+
left_secret = Melos::Crypto.expand_with_label(@suite, secret, "tree", "left", @suite.kdf.n_h)
|
|
146
|
+
right_secret = Melos::Crypto.expand_with_label(@suite, secret, "tree", "right", @suite.kdf.n_h)
|
|
147
|
+
populate(Melos::Tree.left(node_index), left_secret)
|
|
148
|
+
populate(Melos::Tree.right(node_index), right_secret)
|
|
149
|
+
end
|
|
108
150
|
end
|
|
109
151
|
end
|
data/lib/melos/struct/base.rb
CHANGED
|
@@ -1,22 +1,36 @@
|
|
|
1
1
|
module Melos::Struct; end
|
|
2
2
|
|
|
3
3
|
class Melos::Struct::Base
|
|
4
|
-
|
|
4
|
+
# external: values this struct's select fields depend on but which belong to an
|
|
5
|
+
# enclosing struct (e.g. FramedContent.content_type for FramedContentAuthData).
|
|
6
|
+
# They are seeded into the parse context and set as instance variables, but are
|
|
7
|
+
# not part of STRUCT and so are never serialized.
|
|
8
|
+
def initialize(stream, **external)
|
|
5
9
|
stream = StringIO.new(stream) if stream.is_a?(String)
|
|
6
|
-
context = deserialize(stream)
|
|
10
|
+
context = deserialize(stream, external)
|
|
7
11
|
set_instance_vars(context)
|
|
8
12
|
self
|
|
9
13
|
end
|
|
10
14
|
|
|
15
|
+
# build an instance from its field values. Structs with defaults or fields that
|
|
16
|
+
# depend on an enclosing struct define their own create.
|
|
17
|
+
def self.create(**fields)
|
|
18
|
+
unknown = fields.keys - self::STRUCT.map(&:first)
|
|
19
|
+
raise ArgumentError.new("#{name}: unknown fields #{unknown.join(', ')}") unless unknown.empty?
|
|
20
|
+
instance = allocate
|
|
21
|
+
fields.each { |field, value| instance.instance_variable_set("@#{field}", value) }
|
|
22
|
+
instance
|
|
23
|
+
end
|
|
24
|
+
|
|
11
25
|
def raw
|
|
12
26
|
buf = ''
|
|
27
|
+
context = field_context
|
|
13
28
|
self.class::STRUCT.each do |elem|
|
|
29
|
+
value = self.instance_variable_get("@#{elem[0]}")
|
|
14
30
|
case elem[1]
|
|
15
31
|
when :select
|
|
16
|
-
|
|
17
|
-
buf += serialize_select_elem(value, elem[3])
|
|
32
|
+
buf += serialize_select_elem(elem[0], value, elem[2], elem[3], context)
|
|
18
33
|
else
|
|
19
|
-
value = self.instance_variable_get("@#{elem[0]}")
|
|
20
34
|
buf += serialize_elem(value, elem[1], elem[2])
|
|
21
35
|
end
|
|
22
36
|
end
|
|
@@ -36,15 +50,19 @@ class Melos::Struct::Base
|
|
|
36
50
|
end
|
|
37
51
|
|
|
38
52
|
private
|
|
39
|
-
|
|
40
|
-
|
|
53
|
+
# external is a Hash of values seeded into the context before parsing (see #initialize)
|
|
54
|
+
def deserialize(stream, external = {})
|
|
55
|
+
context = external.to_a
|
|
41
56
|
self.class::STRUCT.each do |elem|
|
|
42
57
|
case elem[1]
|
|
43
58
|
when :select
|
|
44
59
|
value = deserialize_select_elem_with_context(stream, context.to_h, elem[2], elem[3], elem[4])
|
|
45
60
|
context << [elem[0], value]
|
|
46
|
-
when :
|
|
47
|
-
|
|
61
|
+
when :class
|
|
62
|
+
# [:name, :class, Klass, ->(ctx){ {external_name: value} }]
|
|
63
|
+
# the optional 4th element derives external context for the child struct from this struct's context
|
|
64
|
+
external_for_child = elem[3] ? elem[3].(context.to_h) : {}
|
|
65
|
+
value = elem[2].new(stream, **external_for_child)
|
|
48
66
|
context << [elem[0], value]
|
|
49
67
|
else
|
|
50
68
|
value = deserialize_elem(stream, elem[1], elem[2])
|
|
@@ -71,9 +89,9 @@ class Melos::Struct::Base
|
|
|
71
89
|
when :uint64
|
|
72
90
|
value = stream.read(8).unpack1('Q>')
|
|
73
91
|
when :vec
|
|
74
|
-
value = Melos::Vec.
|
|
92
|
+
value = Melos::Vec.parse_stream(stream)
|
|
75
93
|
when :vec_of_type
|
|
76
|
-
data = Melos::Vec.
|
|
94
|
+
data = Melos::Vec.parse_stream(stream)
|
|
77
95
|
value = []
|
|
78
96
|
data_stream = StringIO.new(data)
|
|
79
97
|
while (!data_stream.eof?)
|
|
@@ -83,7 +101,7 @@ class Melos::Struct::Base
|
|
|
83
101
|
when :class
|
|
84
102
|
value = type_param.send(:new, stream)
|
|
85
103
|
when :classes
|
|
86
|
-
data = Melos::Vec.
|
|
104
|
+
data = Melos::Vec.parse_stream(stream)
|
|
87
105
|
value = []
|
|
88
106
|
data_stream = StringIO.new(data)
|
|
89
107
|
while (!data_stream.eof?)
|
|
@@ -119,13 +137,13 @@ class Melos::Struct::Base
|
|
|
119
137
|
when :uint64
|
|
120
138
|
[value].pack('Q>')
|
|
121
139
|
when :vec
|
|
122
|
-
Melos::Vec.
|
|
140
|
+
Melos::Vec.string_to_vec(value)
|
|
123
141
|
when :vec_of_type
|
|
124
|
-
Melos::Vec.
|
|
125
|
-
when :class
|
|
142
|
+
Melos::Vec.string_to_vec(value.map { serialize_elem(_1, type_param, nil) }.join)
|
|
143
|
+
when :class
|
|
126
144
|
value.raw
|
|
127
145
|
when :classes
|
|
128
|
-
Melos::Vec.
|
|
146
|
+
Melos::Vec.string_to_vec(value.map(&:raw).join)
|
|
129
147
|
when :optional
|
|
130
148
|
if value.nil?
|
|
131
149
|
[0].pack('C')
|
|
@@ -140,11 +158,19 @@ class Melos::Struct::Base
|
|
|
140
158
|
end
|
|
141
159
|
end
|
|
142
160
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
161
|
+
# every instance variable as a Hash, the same shape as the parse context,
|
|
162
|
+
# so that select predicates can be evaluated at serialization time
|
|
163
|
+
def field_context
|
|
164
|
+
instance_variables.to_h { [_1.to_s.delete_prefix('@').to_sym, instance_variable_get(_1)] }
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# a select field is serialized only when its predicate holds; it must then be present
|
|
168
|
+
def serialize_select_elem(name, value, predicate, type, context)
|
|
169
|
+
if predicate.(context)
|
|
170
|
+
raise ArgumentError.new("#{self.class.name}##{name} is required by its select predicate but is nil") if value.nil?
|
|
147
171
|
serialize_elem(value, type, nil)
|
|
172
|
+
else
|
|
173
|
+
''
|
|
148
174
|
end
|
|
149
175
|
end
|
|
150
176
|
end
|