Module: HrrRbSsh::Transport::KexAlgorithm::DiffieHellman

Includes:
IvComputable
Included in:
DiffieHellmanGroup14Sha1, DiffieHellmanGroup14Sha256, DiffieHellmanGroup15Sha512, DiffieHellmanGroup16Sha512, DiffieHellmanGroup17Sha512, DiffieHellmanGroup18Sha512, DiffieHellmanGroup1Sha1
Defined in:
lib/hrr_rb_ssh/transport/kex_algorithm/diffie_hellman.rb,
lib/hrr_rb_ssh/transport/kex_algorithm/diffie_hellman/h0.rb

Defined Under Namespace

Modules: H0

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

Instance Method Details

#hash(transport) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/hrr_rb_ssh/transport/kex_algorithm/diffie_hellman.rb', line 49

def hash transport
  e = @e
  k = shared_secret
  f = pub_key

  h0_payload = {
    :'V_C' => transport.v_c,
    :'V_S' => transport.v_s,
    :'I_C' => transport.i_c,
    :'I_S' => transport.i_s,
    :'K_S' => transport.server_host_key_algorithm.server_public_host_key,
    :'e'   => e,
    :'f'   => f,
    :'k'   => k,
  }
  h0 = H0.encode h0_payload

  h = OpenSSL::Digest.digest self.class::DIGEST, h0

  h
end

#initializeObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/hrr_rb_ssh/transport/kex_algorithm/diffie_hellman.rb', line 15

def initialize
  @logger = Logger.new(self.class.name)
  @dh = OpenSSL::PKey::DH.new
  if @dh.respond_to?(:set_pqg)
    @dh.set_pqg OpenSSL::BN.new(self.class::P, 16), nil, OpenSSL::BN.new(self.class::G)
  else
    @dh.p = OpenSSL::BN.new(self.class::P, 16)
    @dh.g = OpenSSL::BN.new(self.class::G)
  end
  @dh.generate_key!
end

#pub_keyObject



45
46
47
# File 'lib/hrr_rb_ssh/transport/kex_algorithm/diffie_hellman.rb', line 45

def pub_key
  f = @dh.pub_key.to_i
end

#receive_kexdh_init(payload) ⇒ Object



78
79
80
81
# File 'lib/hrr_rb_ssh/transport/kex_algorithm/diffie_hellman.rb', line 78

def receive_kexdh_init payload
  message = Message::SSH_MSG_KEXDH_INIT.decode payload
  set_e message[:'e']
end

#send_kexdh_reply(transport) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/hrr_rb_ssh/transport/kex_algorithm/diffie_hellman.rb', line 83

def send_kexdh_reply transport
  message = {
    :'message number'                                => Message::SSH_MSG_KEXDH_REPLY::VALUE,
    :'server public host key and certificates (K_S)' => transport.server_host_key_algorithm.server_public_host_key,
    :'f'                                             => pub_key,
    :'signature of H'                                => sign(transport),
  }
  payload = Message::SSH_MSG_KEXDH_REPLY.encode message
  transport.send payload
end

#set_e(e) ⇒ Object



37
38
39
# File 'lib/hrr_rb_ssh/transport/kex_algorithm/diffie_hellman.rb', line 37

def set_e e
  @e = e
end

#shared_secretObject



41
42
43
# File 'lib/hrr_rb_ssh/transport/kex_algorithm/diffie_hellman.rb', line 41

def shared_secret
  k = OpenSSL::BN.new(@dh.compute_key(OpenSSL::BN.new(@e)), 2).to_i
end

#sign(transport) ⇒ Object



71
72
73
74
75
76
# File 'lib/hrr_rb_ssh/transport/kex_algorithm/diffie_hellman.rb', line 71

def sign transport
  h = hash transport
  s = transport.server_host_key_algorithm.sign h

  s
end

#start(transport, mode) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/hrr_rb_ssh/transport/kex_algorithm/diffie_hellman.rb', line 27

def start transport, mode
  case mode
  when Mode::SERVER
    receive_kexdh_init transport.receive
    send_kexdh_reply transport
  else
    raise "unsupported mode"
  end
end