line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::SSH::Perl::Cipher::ChachaPoly; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
11
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
33
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
6
|
use Net::SSH::Perl::Cipher; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
32
|
|
6
|
1
|
|
|
1
|
|
5
|
use base qw( Net::SSH::Perl::Cipher ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
148
|
|
7
|
1
|
|
|
1
|
|
14
|
use Carp qw( croak ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
67
|
|
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
7
|
use constant POLY1305_KEYLEN => 32; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
158
|
|
10
|
1
|
|
|
1
|
|
21
|
use constant ONE => chr(1) . "\0" x 7; # NB little endian |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
71
|
|
11
|
1
|
|
|
1
|
|
8
|
use constant AADLEN => 4; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
95
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
unless (grep /^Net::SSH::Perl$/, @DynaLoader::dl_modules) { |
14
|
1
|
|
|
1
|
|
12
|
use XSLoader; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
864
|
|
15
|
|
|
|
|
|
|
XSLoader::load('Net::SSH::Perl'); |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub new { |
19
|
6
|
|
|
6
|
1
|
18
|
my $class = shift; |
20
|
6
|
|
|
|
|
17
|
my $ciph = bless { }, $class; |
21
|
6
|
50
|
|
|
|
31
|
$ciph->init(@_) if @_; |
22
|
6
|
|
|
|
|
15
|
$ciph; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
6
|
|
|
6
|
0
|
16
|
sub keysize { 64 } |
26
|
0
|
|
|
0
|
0
|
0
|
sub blocksize { 8 } |
27
|
6
|
|
|
6
|
0
|
12
|
sub authlen { 16 } |
28
|
0
|
|
|
0
|
0
|
0
|
sub ivlen { 0 } |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub init { |
31
|
6
|
|
|
6
|
0
|
11
|
my $ciph = shift; |
32
|
6
|
|
|
|
|
12
|
my $key = shift; |
33
|
|
|
|
|
|
|
|
34
|
6
|
|
|
|
|
12
|
my $size = $ciph->keysize/2; |
35
|
6
|
|
|
|
|
15
|
my $mainkey = substr($key,0,$size); |
36
|
6
|
|
|
|
|
10
|
my $headerkey = substr($key,$size,$size); |
37
|
6
|
|
|
|
|
48
|
$ciph->{main} = Crypt::OpenSSH::ChachaPoly->new($mainkey); |
38
|
6
|
|
|
|
|
25
|
$ciph->{header} = Crypt::OpenSSH::ChachaPoly->new($headerkey); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub _seqnr_bytes { |
42
|
6
|
|
|
6
|
|
12
|
my ($ciph, $seqnr) = @_; |
43
|
6
|
|
|
|
|
25
|
pack('N',0) . pack('N',$seqnr); # seqnr is only 32-bit |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub encrypt { |
47
|
3
|
|
|
3
|
1
|
1923
|
my($ciph, $data, $seqnr, $aadlen) = @_; |
48
|
3
|
|
50
|
|
|
23
|
$aadlen ||= 0; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# run chacha20 once to generate poly1305 key |
51
|
|
|
|
|
|
|
# the iv is the packet sequence number |
52
|
3
|
|
|
|
|
10
|
$seqnr = $ciph->_seqnr_bytes($seqnr); |
53
|
3
|
|
|
|
|
34
|
$ciph->{main}->ivsetup($seqnr,''); |
54
|
3
|
|
|
|
|
7
|
my $poly_key = "\0" x POLY1305_KEYLEN; |
55
|
3
|
|
|
|
|
20
|
$poly_key = $ciph->{main}->encrypt($poly_key); |
56
|
|
|
|
|
|
|
|
57
|
3
|
|
|
|
|
6
|
my $aadenc; |
58
|
3
|
50
|
|
|
|
9
|
if ($aadlen) { |
59
|
|
|
|
|
|
|
# encrypt packet length from first four bytes of data |
60
|
0
|
|
|
|
|
0
|
$ciph->{header}->ivsetup($seqnr,''); |
61
|
0
|
|
|
|
|
0
|
$aadenc = $ciph->{header}->encrypt(substr($data,0,$aadlen)); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# set chacha's block counter to 1 |
65
|
3
|
|
|
|
|
10
|
$ciph->{main}->ivsetup($seqnr,ONE); |
66
|
3
|
|
|
|
|
22
|
my $enc = $aadenc . $ciph->{main}->encrypt(substr($data,$aadlen)); |
67
|
3
|
|
|
|
|
16
|
$enc .= $ciph->{main}->poly1305($enc,$poly_key); |
68
|
3
|
|
|
|
|
10
|
return $enc; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub decrypt { |
72
|
3
|
|
|
3
|
1
|
21
|
my($ciph, $data, $seqnr, $aadlen) = @_; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# run chacha20 once to generate poly1305 key |
75
|
|
|
|
|
|
|
# the iv is the packet sequence number |
76
|
3
|
|
|
|
|
7
|
$seqnr = $ciph->_seqnr_bytes($seqnr); |
77
|
3
|
|
|
|
|
10
|
$ciph->{main}->ivsetup($seqnr,''); |
78
|
3
|
|
|
|
|
6
|
my $poly_key = "\0" x POLY1305_KEYLEN; |
79
|
3
|
|
|
|
|
14
|
$poly_key = $ciph->{main}->encrypt($poly_key); |
80
|
|
|
|
|
|
|
|
81
|
3
|
|
|
|
|
8
|
my $datalen = length($data) - $ciph->authlen; |
82
|
3
|
50
|
|
|
|
8
|
if ($aadlen) { |
83
|
|
|
|
|
|
|
$datalen = unpack('N', $ciph->{length} || |
84
|
0
|
|
0
|
|
|
0
|
eval { |
85
|
|
|
|
|
|
|
$ciph->{header}->ivsetup($seqnr,''); |
86
|
|
|
|
|
|
|
$ciph->{header}->decrypt(substr($data,0,$aadlen)) |
87
|
|
|
|
|
|
|
}); |
88
|
|
|
|
|
|
|
} |
89
|
3
|
|
|
|
|
6
|
delete $ciph->{length}; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# check tag before decrypting packet |
92
|
3
|
|
|
|
|
16
|
my $expected_tag = $ciph->{main}->poly1305(substr($data,0,$aadlen+$datalen), |
93
|
|
|
|
|
|
|
$poly_key); |
94
|
3
|
|
|
|
|
10
|
my $tag = substr($data,$aadlen+$datalen,$ciph->authlen); |
95
|
3
|
50
|
|
|
|
18
|
croak "Invalid poly1305 tag" |
96
|
|
|
|
|
|
|
if $expected_tag ne $tag; |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# set chacha's block counter to 1 |
99
|
3
|
|
|
|
|
11
|
$ciph->{main}->ivsetup($seqnr,ONE); |
100
|
|
|
|
|
|
|
# return payload only |
101
|
3
|
|
|
|
|
25
|
$ciph->{main}->decrypt(substr($data,$aadlen,$datalen)); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub get_length { |
105
|
0
|
|
|
0
|
0
|
|
my($ciph, $data, $seqnr) = @_; |
106
|
|
|
|
|
|
|
|
107
|
0
|
0
|
|
|
|
|
return if length($data) < AADLEN; |
108
|
0
|
|
|
|
|
|
$seqnr = $ciph->_seqnr_bytes($seqnr); |
109
|
0
|
|
|
|
|
|
$ciph->{header}->ivsetup($seqnr,''); |
110
|
|
|
|
|
|
|
# save it so we do not have to decrypt again later in decrypt() |
111
|
0
|
|
|
|
|
|
$ciph->{length} = $ciph->{header}->decrypt(substr($data,0,AADLEN)); |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
1; |
115
|
|
|
|
|
|
|
__END__ |