Class: Net::SSH::Transport::ChaCha20Poly1305Cipher

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/net/ssh/transport/chacha20_poly1305_cipher.rb

Overview

Implements the chacha20-poly1305@openssh cipher

Defined Under Namespace

Classes: ImplicitHMac

Instance Attribute Summary

Attributes included from Loggable

#logger

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#debug, #error, #fatal, #info, #lwarn

Constructor Details

#initialize(encrypt:, key:) ⇒ ChaCha20Poly1305Cipher

Returns a new instance of ChaCha20Poly1305Cipher.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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

Class Method Details

.block_sizeObject



107
108
109
# File 'lib/net/ssh/transport/chacha20_poly1305_cipher.rb', line 107

def self.block_size
  8
end

.key_lengthObject



111
112
113
# File 'lib/net/ssh/transport/chacha20_poly1305_cipher.rb', line 111

def self.key_length
  64
end

Instance Method Details

#block_sizeObject



91
92
93
# File 'lib/net/ssh/transport/chacha20_poly1305_cipher.rb', line 91

def block_size
  8
end

#implicit_macObject



103
104
105
# File 'lib/net/ssh/transport/chacha20_poly1305_cipher.rb', line 103

def implicit_mac
  return ImplicitHMac.new
end

#implicit_mac?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/net/ssh/transport/chacha20_poly1305_cipher.rb', line 99

def implicit_mac?
  true
end

#mac_lengthObject



87
88
89
# File 'lib/net/ssh/transport/chacha20_poly1305_cipher.rb', line 87

def mac_length
  16
end

#nameObject



95
96
97
# File 'lib/net/ssh/transport/chacha20_poly1305_cipher.rb', line 95

def name
  "[email protected]"
end

#read_and_mac(data, mac, sequence_number) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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) ⇒ Object



64
65
66
67
68
# 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) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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