Class: Noise::ProtocolName

Inherits:
Data
  • Object
show all
Defined in:
lib/noise/protocol_name.rb

Overview

A Noise protocol name, split into the parts that name a handshake pattern and the three functions the protocol runs on.

A name has five parts joined with '_': the prefix 'Noise', the handshake pattern with its modifiers, the DH function, the cipher function and the hash function, for example 'Noise_NNpsk0_25519_ChaChaPoly_BLAKE2s'.

Two of those parts hold a list of their own. The pattern part is a pattern name followed by the modifiers applied to it, and the DH part is a list joined with '+', because a hybrid handshake names two DH functions at once ('Noise_NNhfs_25519+448_ChaChaPoly_BLAKE2s'). Both are split here, so that every rule about the shape of a name lives in this class alone.

Which functions exist is Noise::Protocol's question, not this one's: a name that asks for a cipher, hash or DH function this gem does not implement still parses. Modifiers are the one exception, because a modifier has to be understood before it can be held as a value, so an unknown one is rejected here.

The members are the names as written, not the functions themselves. Noise::Protocol resolves them, and calls its resolved functions cipher_fn, hash_fn and dh_fn.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cipher_nameObject (readonly)

Returns the value of attribute cipher_name

Returns:

  • (Object)

    the current value of cipher_name



23
24
25
# File 'lib/noise/protocol_name.rb', line 23

def cipher_name
  @cipher_name
end

#dh_namesObject (readonly)

Returns the value of attribute dh_names

Returns:

  • (Object)

    the current value of dh_names



23
24
25
# File 'lib/noise/protocol_name.rb', line 23

def dh_names
  @dh_names
end

#hash_nameObject (readonly)

Returns the value of attribute hash_name

Returns:

  • (Object)

    the current value of hash_name



23
24
25
# File 'lib/noise/protocol_name.rb', line 23

def hash_name
  @hash_name
end

#modifiersObject (readonly)

Returns the value of attribute modifiers

Returns:

  • (Object)

    the current value of modifiers



23
24
25
# File 'lib/noise/protocol_name.rb', line 23

def modifiers
  @modifiers
end

#nameObject (readonly)

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



23
24
25
# File 'lib/noise/protocol_name.rb', line 23

def name
  @name
end

#pattern_nameObject (readonly)

Returns the value of attribute pattern_name

Returns:

  • (Object)

    the current value of pattern_name



23
24
25
# File 'lib/noise/protocol_name.rb', line 23

def pattern_name
  @pattern_name
end

Class Method Details

.parse(name) ⇒ Noise::ProtocolName

Parses a protocol name.

Parameters:

  • name (String)

    for example 'Noise_XX_25519_ChaChaPoly_SHA256'.

Returns:

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/noise/protocol_name.rb', line 33

def self.parse(name)
  # 'Noise', the pattern with its modifiers, the DH functions, the cipher and the hash.
  parts = name.split('_')
  malformed!(name) unless parts.size == 5

  prefix, pattern_part, dh_part, cipher_name, hash_name = parts
  malformed!(name) unless prefix == 'Noise'

  # A pattern name is capitals, plus the digit 1 that deferred patterns use (X1K1). Whatever
  # follows it is the modifiers.
  matched = /\A([A-Z1]+)([^A-Z]*)\z/.match(pattern_part)
  malformed!(name) unless matched

  new(name: name, pattern_name: matched[1], modifiers: parse_modifiers(matched[2]),
      dh_names: split_dh(dh_part), cipher_name: cipher_name, hash_name: hash_name)
end

Instance Method Details

#to_sString

Returns the name this was parsed from.

Returns:

  • (String)

    the name this was parsed from.



74
75
76
# File 'lib/noise/protocol_name.rb', line 74

def to_s
  name
end