class Net::SSH::Transport::ChaCha20Poly1305Cipher

Implements the chacha20-poly1305@openssh cipher

Public Class Methods

block_size() click to toggle source
# File lib/net/ssh/transport/chacha20_poly1305_cipher.rb, line 107
def self.block_size
  8
end
key_length() click to toggle source
# File lib/net/ssh/transport/chacha20_poly1305_cipher.rb, line 111
def self.key_length
  64
end
new(encrypt:, key:) click to toggle source
# File lib/net/ssh/transport/chacha20_poly1305_cipher.rb, line 23
def initialize(encrypt:, key:)
  @chacha_hdr = OpenSSL::Cipher.new("chacha20")
  key_len = @chacha_hdr.key_len
  @chacha_main = OpenSSL::Cipher.new("chacha20")
  @poly = RbNaCl::OneTimeAuths::Poly1305
  if key.size < key_len * 2
    error { "chacha20_poly1305: keylength doesn't match" }
    raise "chacha20_poly1305: keylength doesn't match"
  end
  if encrypt
    @chacha_hdr.encrypt
    @chacha_main.encrypt
  else
    @chacha_hdr.decrypt
    @chacha_main.decrypt
  end
  main_key = key[0...key_len]
  @chacha_main.key = main_key
  hdr_key = key[key_len...(2 * key_len)]
  @chacha_hdr.key = hdr_key
end

Public Instance Methods

block_size() click to toggle source
# File lib/net/ssh/transport/chacha20_poly1305_cipher.rb, line 91
def block_size
  8
end
implicit_mac() click to toggle source
# File lib/net/ssh/transport/chacha20_poly1305_cipher.rb, line 103
def implicit_mac
  return ImplicitHMac.new
end
implicit_mac?() click to toggle source
# File lib/net/ssh/transport/chacha20_poly1305_cipher.rb, line 99
def implicit_mac?
  true
end
mac_length() click to toggle source
# File lib/net/ssh/transport/chacha20_poly1305_cipher.rb, line 87
def mac_length
  16
end
name() click to toggle source
# File lib/net/ssh/transport/chacha20_poly1305_cipher.rb, line 95
def name
  "chacha20-poly1305@openssh.com"
end
read_and_mac(data, mac, sequence_number) click to toggle source
# File lib/net/ssh/transport/chacha20_poly1305_cipher.rb, line 70
def read_and_mac(data, mac, sequence_number)
  iv_data = [0, 0, 0, sequence_number].pack("NNNN")
  @chacha_main.iv = iv_data
  poly_key = @chacha_main.update(([0] * 32).pack('C32'))

  iv_data[0] = 1.chr
  @chacha_main.iv = iv_data
  unencrypted_data = @chacha_main.update(data[4..])
  begin
    ok = @poly.verify(poly_key, mac, data[0..])
    raise Net::SSH::Exception, "corrupted hmac detected #{name}" unless ok
  rescue RbNaCl::BadAuthenticatorError
    raise Net::SSH::Exception, "corrupted hmac detected #{name}"
  end
  return unencrypted_data
end
read_length(data, sequence_number) click to toggle source
# File lib/net/ssh/transport/chacha20_poly1305_cipher.rb, line 64
def read_length(data, sequence_number)
  iv_data = [0, 0, 0, sequence_number].pack("NNNN")
  @chacha_hdr.iv = iv_data
  @chacha_hdr.update(data).unpack1("N")
end
update_cipher_mac(payload, sequence_number) click to toggle source
# File lib/net/ssh/transport/chacha20_poly1305_cipher.rb, line 45
def update_cipher_mac(payload, sequence_number)
  iv_data = [0, 0, 0, sequence_number].pack("NNNN")
  @chacha_main.iv = iv_data
  poly_key = @chacha_main.update(([0] * 32).pack('C32'))

  packet_length = payload.size
  length_data = [packet_length].pack("N")
  @chacha_hdr.iv = iv_data
  packet = @chacha_hdr.update(length_data)

  iv_data[0] = 1.chr
  @chacha_main.iv = iv_data
  unencrypted_data = payload
  packet += @chacha_main.update(unencrypted_data)

  packet += @poly.auth(poly_key, packet)
  return packet
end