module HrrRbSsh::Transport::KexAlgorithm::EllipticCurveDiffieHellman

Public Class Methods

new(logger: nil) click to toggle source
# File lib/hrr_rb_ssh/transport/kex_algorithm/elliptic_curve_diffie_hellman.rb, line 16
def initialize logger: nil
  self.logger = logger
  @dh = OpenSSL::PKey::EC.new(self.class::CURVE_NAME)
  @dh.generate_key
  @public_key = @dh.public_key.to_bn.to_i
end

Public Instance Methods

hash(transport) click to toggle source
# File lib/hrr_rb_ssh/transport/kex_algorithm/elliptic_curve_diffie_hellman.rb, line 46
def hash transport
  h0_payload = {
    :'V_C' => transport.v_c,
    :'V_S' => transport.v_s,
    :'I_C' => transport.i_c,
    :'I_S' => transport.i_s,
    :'K_S' => @k_s,
    :'Q_C' => @q_c,
    :'Q_S' => @q_s,
    :'K'   => @shared_secret,
  }
  h0 = H0.new(logger: logger).encode h0_payload
  h  = OpenSSL::Digest.digest self.class::DIGEST, h0
end
receive_kexecdh_init(payload) click to toggle source
# File lib/hrr_rb_ssh/transport/kex_algorithm/elliptic_curve_diffie_hellman.rb, line 66
def receive_kexecdh_init payload
  Message::SSH_MSG_KEXECDH_INIT.new(logger: logger).decode payload
end
receive_kexecdh_reply(payload) click to toggle source
# File lib/hrr_rb_ssh/transport/kex_algorithm/elliptic_curve_diffie_hellman.rb, line 90
def receive_kexecdh_reply payload
  Message::SSH_MSG_KEXECDH_REPLY.new(logger: logger).decode payload
end
send_kexecdh_init(transport) click to toggle source
# File lib/hrr_rb_ssh/transport/kex_algorithm/elliptic_curve_diffie_hellman.rb, line 81
def send_kexecdh_init transport
  message = {
    :'message number' => Message::SSH_MSG_KEXECDH_INIT::VALUE,
    :'Q_C'            => @q_c,
  }
  payload = Message::SSH_MSG_KEXECDH_INIT.new(logger: logger).encode message
  transport.send payload
end
send_kexecdh_reply(transport) click to toggle source
# File lib/hrr_rb_ssh/transport/kex_algorithm/elliptic_curve_diffie_hellman.rb, line 70
def send_kexecdh_reply transport
  message = {
    :'message number' => Message::SSH_MSG_KEXECDH_REPLY::VALUE,
    :'K_S'            => @k_s,
    :'Q_S'            => @q_s,
    :'signature of H' => sign(transport),
  }
  payload = Message::SSH_MSG_KEXECDH_REPLY.new(logger: logger).encode message
  transport.send payload
end
shared_secret() click to toggle source
# File lib/hrr_rb_ssh/transport/kex_algorithm/elliptic_curve_diffie_hellman.rb, line 42
def shared_secret
  @shared_secret
end
sign(transport) click to toggle source
# File lib/hrr_rb_ssh/transport/kex_algorithm/elliptic_curve_diffie_hellman.rb, line 61
def sign transport
  h = hash transport
  s = transport.server_host_key_algorithm.sign h
end
start(transport) click to toggle source
# File lib/hrr_rb_ssh/transport/kex_algorithm/elliptic_curve_diffie_hellman.rb, line 23
def start transport
  case transport.mode
  when Mode::SERVER
    @k_s = transport.server_host_key_algorithm.server_public_host_key
    @q_s = @public_key
    message = receive_kexecdh_init transport.receive
    @q_c = message[:'Q_C']
    @shared_secret = OpenSSL::BN.new(@dh.dh_compute_key(OpenSSL::PKey::EC::Point.new(OpenSSL::PKey::EC.new(self.class::CURVE_NAME).group, OpenSSL::BN.new(@q_c))), 2).to_i
    send_kexecdh_reply transport
  when Mode::CLIENT
    @q_c = @public_key
    send_kexecdh_init transport
    message = receive_kexecdh_reply transport.receive
    @k_s = message[:'K_S']
    @q_s = message[:'Q_S']
    @shared_secret = OpenSSL::BN.new(@dh.dh_compute_key(OpenSSL::PKey::EC::Point.new(OpenSSL::PKey::EC.new(self.class::CURVE_NAME).group, OpenSSL::BN.new(@q_s))), 2).to_i
  end
end