blib/lib/Net/SSH/Perl/Kex/C25519.pm untested

filter: Statementstmt 0.0% 0 / 93 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 115 SCAR 17.9
CC
2
Cov
0%
exchange
6.0
new
2.0
kex_hash
2.0
CRAP 6.0
1 package Net::SSH::Perl::Kex::C25519;
2 use strict;
3  
4 use Net::SSH::Perl::Buffer;
5 use Net::SSH::Perl::Packet;
6 use Net::SSH::Perl::Constants qw( :msg2 :kex );
7 use Net::SSH::Perl::Key;
8  
9 use Carp qw( croak );
10 use Crypt::Digest::SHA256 qw( sha256 );
11 use Scalar::Util qw(weaken);
12 use Crypt::PRNG qw( random_bytes );
13 use Crypt::Curve25519 qw( curve25519_secret_key
14 curve25519_public_key
15 curve25519_shared_secret );
16  
17 use base qw( Net::SSH::Perl::Kex );
18  
19 use constant CURVE25519_SIZE => 32;
20  
21 sub new {
22 my $class = shift;
23 my $ssh = shift;
24 my $kex = bless { ssh => $ssh }, $class;
25 weaken $kex->{ssh};
26 $kex;
27 }
28  
29 sub exchange {
30 my $kex = shift;
31 my $ssh = $kex->{ssh};
32 my $packet;
33  
34 $ssh->debug('Generating ephemeral key pair.');
35 my $rand = random_bytes(CURVE25519_SIZE);
36 my $c_sec_key = curve25519_secret_key($rand);
37 my $c_pub_key = curve25519_public_key($c_sec_key);
38  
39 $ssh->debug('Entering Curve 25519 Key Exchange.');
40 $packet = $ssh->packet_start(SSH2_MSG_KEX_ECDH_INIT);
41 $packet->put_str($c_pub_key);
42 $packet->send;
43  
44 $ssh->debug('Sent client public key, waiting for reply.');
45 $packet = Net::SSH::Perl::Packet->read_expect($ssh,
46 SSH2_MSG_KEX_ECDH_REPLY);
47  
48 my $host_key_blob = $packet->get_str;
49 my $s_host_key = Net::SSH::Perl::Key->new_from_blob($host_key_blob);
50 $ssh->debug("Received host key, type '" . $s_host_key->ssh_name . "'.");
51  
52 $ssh->check_host_key($s_host_key);
53  
54 my $s_pub_key = $packet->get_str;
55 my $signature = $packet->get_str;
56 my $shared_secret = curve25519_shared_secret($c_sec_key, $s_pub_key);
57  
58 my $hash = $kex->kex_hash(
59 $ssh->client_version_string,
60 $ssh->server_version_string,
61 $kex->client_kexinit,
62 $kex->server_kexinit,
63 $host_key_blob,
64 $c_pub_key,
65 $s_pub_key,
66 $shared_secret);
67  
68 $ssh->debug("Verifying server signature.");
69 croak "Key verification failed for server host key"
70 unless $s_host_key->verify($signature, $hash);
71  
72 $ssh->session_id($hash);
73  
74 $kex->derive_keys($hash, $shared_secret, $ssh->session_id);
75 }
76  
77 sub kex_hash {
78 my $kex = shift;
79 my($c_vs, $s_vs, $c_kexinit, $s_kexinit, $s_host_key_blob,
80 $c_pub_key, $s_pub_key, $shared_secret) = @_;
81 my $b = Net::SSH::Perl::Buffer->new( MP => 'SSH2' );
82 # version strings (V_C, V_S)
83 $b->put_str($c_vs);
84 $b->put_str($s_vs);
85 # client,server payload of SSH_MSG_KEXINIT (I_C, I_S)
86 $b->put_int32($c_kexinit->length + 1);
87 $b->put_int8(SSH2_MSG_KEXINIT);
88 $b->put_chars($c_kexinit->bytes);
89 $b->put_int32($s_kexinit->length + 1);
90 $b->put_int8(SSH2_MSG_KEXINIT);
91 $b->put_chars($s_kexinit->bytes);
92 # host key (K_S)
93 $b->put_str($s_host_key_blob);
94 # Q_C, Q_S, H
95 $b->put_str($c_pub_key);
96 $b->put_str($s_pub_key);
97 $b->put_bignum2_bytes($shared_secret);
98  
99 sha256($b->bytes);
100 }
101  
102 sub derive_key {
103 my($kex, $id, $need, $hash, $shared_secret, $session_id) = @_;
104 my $b = Net::SSH::Perl::Buffer->new( MP => 'SSH2' );
105 $b->put_bignum2_bytes($shared_secret);
106 my $digest = sha256($b->bytes, $hash, chr($id), $session_id);
107 for (my $have = 32; $need > $have; $have += 32) {
108 $digest .= sha256($b->bytes, $hash, $digest);
109 }
110 $digest;
111 }
112  
113 1;
114 __END__