Module: HrrRbSsh::Transport::KexAlgorithm::EllipticCurveDiffieHellman

Includes:
Loggable, IvComputable
Included in:
EllipticCurveDiffieHellmanSha2Nistp256, EllipticCurveDiffieHellmanSha2Nistp384, EllipticCurveDiffieHellmanSha2Nistp521
Defined in:
lib/hrr_rb_ssh/transport/kex_algorithm/elliptic_curve_diffie_hellman.rb,
lib/hrr_rb_ssh/transport/kex_algorithm/elliptic_curve_diffie_hellman/h0.rb

Defined Under Namespace

Classes: H0

Instance Attribute Summary

Attributes included from Loggable

#log_key, #logger

Instance Method Summary collapse

Methods included from IvComputable

#build_key, #iv_c_to_s, #iv_s_to_c, #key_c_to_s, #key_s_to_c, #mac_c_to_s, #mac_s_to_c

Methods included from Loggable

#log_debug, #log_error, #log_fatal, #log_info, #log_warn

Instance Method Details

#hash(transport) ⇒ Object



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

#initialize(logger: nil) ⇒ Object



16
17
18
19
20
21
# 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

#receive_kexecdh_init(payload) ⇒ Object



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



90
91
92
# 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) ⇒ Object



81
82
83
84
85
86
87
88
# 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) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# 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_secretObject



42
43
44
# File 'lib/hrr_rb_ssh/transport/kex_algorithm/elliptic_curve_diffie_hellman.rb', line 42

def shared_secret
  @shared_secret
end

#sign(transport) ⇒ Object



61
62
63
64
# 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) ⇒ Object



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