Class: Melos::Struct::LeafNode

Inherits:
Base
  • Object
show all
Defined in:
lib/melos/struct/structs.rb

Constant Summary collapse

STRUCT =
[
  [:encryption_key, :vec], # HPKEPublicKey = opaque <V>
  [:signature_key, :vec],  # SignaturePublicKey = opaque <V>
  [:credential, :class, Melos::Struct::Credential],
  [:capabilities, :class, Melos::Struct::Capabilities],
  [:leaf_node_source, :uint8], # LeafNodeSource = enum of uint8,
  [:lifetime, :select,    ->(ctx){ctx[:leaf_node_source] == Melos::Constants::LeafNodeSource::KEY_PACKAGE}, :class, Melos::Struct::Lifetime],
  [:parent_hash, :select, ->(ctx){ctx[:leaf_node_source] == Melos::Constants::LeafNodeSource::COMMIT},      :vec],
  [:extensions, :classes, Melos::Struct::Extension],
  [:signature, :vec]
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#deserialize_select_elem_with_context, #initialize, #raw

Constructor Details

This class inherits a constructor from Melos::Struct::Base

Instance Attribute Details

#capabilitiesObject (readonly)

Returns the value of attribute capabilities.



106
107
108
# File 'lib/melos/struct/structs.rb', line 106

def capabilities
  @capabilities
end

#credentialObject (readonly)

Returns the value of attribute credential.



106
107
108
# File 'lib/melos/struct/structs.rb', line 106

def credential
  @credential
end

#encryption_keyObject (readonly)

Returns the value of attribute encryption_key.



106
107
108
# File 'lib/melos/struct/structs.rb', line 106

def encryption_key
  @encryption_key
end

#extensionsObject (readonly)

Returns the value of attribute extensions.



106
107
108
# File 'lib/melos/struct/structs.rb', line 106

def extensions
  @extensions
end

#leaf_node_sourceObject (readonly)

Returns the value of attribute leaf_node_source.



106
107
108
# File 'lib/melos/struct/structs.rb', line 106

def leaf_node_source
  @leaf_node_source
end

#lifetimeObject (readonly)

Returns the value of attribute lifetime.



106
107
108
# File 'lib/melos/struct/structs.rb', line 106

def lifetime
  @lifetime
end

#parent_hashObject (readonly)

Returns the value of attribute parent_hash.



106
107
108
# File 'lib/melos/struct/structs.rb', line 106

def parent_hash
  @parent_hash
end

#signatureObject (readonly)

Returns the value of attribute signature.



106
107
108
# File 'lib/melos/struct/structs.rb', line 106

def signature
  @signature
end

#signature_keyObject (readonly)

Returns the value of attribute signature_key.



106
107
108
# File 'lib/melos/struct/structs.rb', line 106

def signature_key
  @signature_key
end

Class Method Details

.create(encryption_key:, signature_key:, credential:, capabilities:, leaf_node_source:, lifetime:, parent_hash:, extensions:, signature:) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/melos/struct/structs.rb', line 139

def self.create(
  encryption_key:, signature_key:, credential:, capabilities:,
  leaf_node_source:, lifetime:, parent_hash:, extensions:, signature:
)
  new_instance = self.allocate
  new_instance.instance_variable_set(:@encryption_key, encryption_key)
  new_instance.instance_variable_set(:@signature_key, signature_key)
  new_instance.instance_variable_set(:@credential, credential)
  new_instance.instance_variable_set(:@capabilities, capabilities)
  new_instance.instance_variable_set(:@leaf_node_source, leaf_node_source)
  new_instance.instance_variable_set(:@lifetime, lifetime)
  new_instance.instance_variable_set(:@parent_hash, parent_hash)
  new_instance.instance_variable_set(:@extensions, extensions)
  new_instance.instance_variable_set(:@signature, signature)
  new_instance
end

Instance Method Details

#leaf_node_tbs(group_id, leaf_index) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/melos/struct/structs.rb', line 119

def leaf_node_tbs(group_id, leaf_index)
  buf = ''
  buf += Melos::Vec.from_string(encryption_key)
  buf += Melos::Vec.from_string(signature_key)
  buf += credential.raw
  buf += capabilities.raw
  buf += [leaf_node_source].pack('C')
  if leaf_node_source == Melos::Constants::LeafNodeSource::KEY_PACKAGE
    buf += lifetime.raw
  elsif leaf_node_source == Melos::Constants::LeafNodeSource::COMMIT
    buf += Melos::Vec.from_string(parent_hash)
  end
  buf += Melos::Vec.from_string(extensions.map(&:raw).join)
  if leaf_node_source == Melos::Constants::LeafNodeSource::UPDATE || leaf_node_source == Melos::Constants::LeafNodeSource::COMMIT
    buf += Melos::Vec.from_string(group_id)
    buf += [leaf_index].pack('L>') # uint32
  end
  buf
end

#sign(suite, signature_private_key, group_id, leaf_index) ⇒ Object



156
157
158
# File 'lib/melos/struct/structs.rb', line 156

def sign(suite, signature_private_key, group_id, leaf_index)
  @signature = Melos::Crypto.sign_with_label(suite, signature_private_key, "LeafNodeTBS", leaf_node_tbs(group_id, leaf_index))
end

#verify(suite, group_id, leaf_index) ⇒ Object



160
161
162
# File 'lib/melos/struct/structs.rb', line 160

def verify(suite, group_id, leaf_index)
  Melos::Crypto.verify_with_label(suite, signature_key, "LeafNodeTBS", leaf_node_tbs(group_id, leaf_index), signature)
end