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, UnsupportedError

Constant Summary collapse

POLY1305_ALGORITHM =
"POLY1305"
POLY1305_KEY_BYTES =
32
POLY1305_TAG_BYTES =
16
NAME =
"chacha20-poly1305@openssh.com"
ZERO_BLOCK =
"\x00".b * POLY1305_KEY_BYTES
ZERO_IV =
"\x00".b * 16

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.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/net/ssh/transport/chacha20_poly1305_cipher.rb', line 32

def initialize(encrypt:, key:)
  @chacha_hdr = OpenSSL::Cipher.new("chacha20")
  key_len = @chacha_hdr.key_len
  @chacha_main = OpenSSL::Cipher.new("chacha20")
  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

.auth_lengthObject



128
129
130
# File 'lib/net/ssh/transport/chacha20_poly1305_cipher.rb', line 128

def self.auth_length
  POLY1305_TAG_BYTES
end

.block_sizeObject



116
117
118
# File 'lib/net/ssh/transport/chacha20_poly1305_cipher.rb', line 116

def self.block_size
  8
end

.decrypt_private_key(ciphertext, auth_tag, key, _initialization_vector) ⇒ Object

Raises:

  • (ArgumentError)


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/net/ssh/transport/chacha20_poly1305_cipher.rb', line 132

def self.decrypt_private_key(ciphertext, auth_tag, key, _initialization_vector)
  raise ArgumentError, "chacha20_poly1305: keylength doesn't match" unless
    key.respond_to?(:bytesize) && key.bytesize == key_length

  ciphertext = binary_string(ciphertext)
  chacha = OpenSSL::Cipher.new("chacha20")
  chacha.decrypt
  chacha.key = binary_string(key[0...POLY1305_KEY_BYTES])

  iv_data = ZERO_IV.dup
  chacha.iv = iv_data
  poly_key = chacha.update(ZERO_BLOCK)

  valid_mac = OpenSSL.fixed_length_secure_compare(poly1305_auth(poly_key, ciphertext), auth_tag)
  raise Net::SSH::Exception, "corrupted hmac detected #{NAME}" unless valid_mac

  iv_data.setbyte(0, 1)
  chacha.iv = iv_data
  chacha.update(ciphertext)
end

.ensure_supported!Object



153
154
155
156
157
158
159
160
161
162
# File 'lib/net/ssh/transport/chacha20_poly1305_cipher.rb', line 153

def self.ensure_supported!
  raise UnsupportedError, "OpenSSL::PKey raw private key APIs are unavailable" unless OpenSSL::PKey.respond_to?(:new_raw_private_key)

  OpenSSL::Cipher.new("chacha20")

  tag = poly1305_auth("\x00" * POLY1305_KEY_BYTES, "")
  raise UnsupportedError, "OpenSSL Poly1305 authentication failed" unless tag.bytesize == POLY1305_TAG_BYTES
rescue OpenSSL::Cipher::CipherError, OpenSSL::PKey::PKeyError => e
  raise UnsupportedError, e.message
end

.iv_lenObject



124
125
126
# File 'lib/net/ssh/transport/chacha20_poly1305_cipher.rb', line 124

def self.iv_len
  0
end

.key_lengthObject



120
121
122
# File 'lib/net/ssh/transport/chacha20_poly1305_cipher.rb', line 120

def self.key_length
  64
end

.poly1305_auth(poly_key, data) ⇒ Object



164
165
166
167
# File 'lib/net/ssh/transport/chacha20_poly1305_cipher.rb', line 164

def self.poly1305_auth(poly_key, data)
  validate_poly1305_key!(poly_key)
  OpenSSL::PKey.new_raw_private_key(POLY1305_ALGORITHM, binary_string(poly_key)).sign(nil, binary_string(data))
end

.validate_poly1305_key!(poly_key) ⇒ Object

Raises:

  • (ArgumentError)


169
170
171
# File 'lib/net/ssh/transport/chacha20_poly1305_cipher.rb', line 169

def self.validate_poly1305_key!(poly_key)
  raise ArgumentError, "invalid Poly1305 key" unless poly_key.respond_to?(:bytesize) && poly_key.bytesize == POLY1305_KEY_BYTES
end

Instance Method Details

#block_sizeObject



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

def block_size
  8
end

#implicit_macObject



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

def implicit_mac
  return ImplicitHMac.new
end

#implicit_mac?Boolean

Returns:

  • (Boolean)


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

def implicit_mac?
  true
end

#mac_lengthObject



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

def mac_length
  POLY1305_TAG_BYTES
end

#nameObject



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

def name
  NAME
end

#read_and_mac(data, mac, sequence_number) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/net/ssh/transport/chacha20_poly1305_cipher.rb', line 78

def read_and_mac(data, mac, sequence_number)
  iv_data = packet_iv(sequence_number)
  @chacha_main.iv = iv_data
  poly_key = @chacha_main.update(ZERO_BLOCK)

  iv_data[0] = 1.chr
  @chacha_main.iv = iv_data
  unencrypted_data = @chacha_main.update(data[4..])

  expected_mac = self.class.poly1305_auth(poly_key, data[0..])
  valid_mac = mac.respond_to?(:bytesize) &&
              mac.bytesize == POLY1305_TAG_BYTES &&
              OpenSSL.fixed_length_secure_compare(expected_mac, mac)
  raise Net::SSH::Exception, "corrupted hmac detected #{name}" unless valid_mac

  return unencrypted_data
end

#read_length(data, sequence_number) ⇒ Object



72
73
74
75
76
# File 'lib/net/ssh/transport/chacha20_poly1305_cipher.rb', line 72

def read_length(data, sequence_number)
  iv_data = packet_iv(sequence_number)
  @chacha_hdr.iv = iv_data
  @chacha_hdr.update(data).unpack1("N")
end

#update_cipher_mac(payload, sequence_number) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/net/ssh/transport/chacha20_poly1305_cipher.rb', line 53

def update_cipher_mac(payload, sequence_number)
  iv_data = packet_iv(sequence_number)
  @chacha_main.iv = iv_data
  poly_key = @chacha_main.update(ZERO_BLOCK)

  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 += self.class.poly1305_auth(poly_key, packet)
  return packet
end