line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::SSH::Perl::Cipher::ChachaPoly; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
25
|
use strict; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
36
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
8
|
use Net::SSH::Perl::Cipher; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
33
|
|
6
|
1
|
|
|
1
|
|
7
|
use base qw( Net::SSH::Perl::Cipher ); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
89
|
|
7
|
1
|
|
|
1
|
|
8
|
use Carp qw( croak ); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
67
|
|
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
8
|
use constant POLY1305_KEYLEN => 32; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
113
|
|
10
|
1
|
|
|
1
|
|
7
|
use constant ONE => chr(1) . "\0" x 7; # NB little endian |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
61
|
|
11
|
1
|
|
|
1
|
|
7
|
use constant AADLEN => 4; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
100
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
unless (grep /^Net::SSH::Perl$/, @DynaLoader::dl_modules) { |
14
|
1
|
|
|
1
|
|
8
|
use XSLoader; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
905
|
|
15
|
|
|
|
|
|
|
XSLoader::load('Net::SSH::Perl'); |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub new { |
19
|
6
|
|
|
6
|
1
|
16
|
my $class = shift; |
20
|
6
|
|
|
|
|
18
|
my $ciph = bless { }, $class; |
21
|
6
|
50
|
|
|
|
33
|
$ciph->init(@_) if @_; |
22
|
6
|
|
|
|
|
20
|
$ciph; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
6
|
|
|
6
|
0
|
20
|
sub keysize { 64 } |
26
|
0
|
|
|
0
|
0
|
0
|
sub blocksize { 8 } |
27
|
6
|
|
|
6
|
0
|
19
|
sub authlen { 16 } |
28
|
0
|
|
|
0
|
0
|
0
|
sub ivlen { 0 } |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub init { |
31
|
6
|
|
|
6
|
0
|
12
|
my $ciph = shift; |
32
|
6
|
|
|
|
|
13
|
my $key = shift; |
33
|
|
|
|
|
|
|
|
34
|
6
|
|
|
|
|
18
|
my $size = $ciph->keysize/2; |
35
|
6
|
|
|
|
|
20
|
my $mainkey = substr($key,0,$size); |
36
|
6
|
|
|
|
|
15
|
my $headerkey = substr($key,$size,$size); |
37
|
6
|
|
|
|
|
48
|
$ciph->{main} = Crypt::OpenSSH::ChachaPoly->new($mainkey); |
38
|
6
|
|
|
|
|
35
|
$ciph->{header} = Crypt::OpenSSH::ChachaPoly->new($headerkey); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub _seqnr_bytes { |
42
|
6
|
|
|
6
|
|
16
|
my ($ciph, $seqnr) = @_; |
43
|
6
|
|
|
|
|
29
|
pack('N',0) . pack('N',$seqnr); # seqnr is only 32-bit |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub encrypt { |
47
|
3
|
|
|
3
|
1
|
4595
|
my($ciph, $data, $seqnr, $aadlen) = @_; |
48
|
3
|
|
50
|
|
|
25
|
$aadlen ||= 0; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# run chacha20 once to generate poly1305 key |
51
|
|
|
|
|
|
|
# the iv is the packet sequence number |
52
|
3
|
|
|
|
|
11
|
$seqnr = $ciph->_seqnr_bytes($seqnr); |
53
|
3
|
|
|
|
|
31
|
$ciph->{main}->ivsetup($seqnr,''); |
54
|
3
|
|
|
|
|
7
|
my $poly_key = "\0" x POLY1305_KEYLEN; |
55
|
3
|
|
|
|
|
82
|
$poly_key = $ciph->{main}->encrypt($poly_key); |
56
|
|
|
|
|
|
|
|
57
|
3
|
|
|
|
|
9
|
my $aadenc; |
58
|
3
|
50
|
|
|
|
10
|
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
|
|
|
|
|
14
|
$ciph->{main}->ivsetup($seqnr,ONE); |
66
|
3
|
|
|
|
|
25
|
my $enc = $aadenc . $ciph->{main}->encrypt(substr($data,$aadlen)); |
67
|
3
|
|
|
|
|
22
|
$enc .= $ciph->{main}->poly1305($enc,$poly_key); |
68
|
3
|
|
|
|
|
12
|
return $enc; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub decrypt { |
72
|
3
|
|
|
3
|
1
|
19
|
my($ciph, $data, $seqnr, $aadlen) = @_; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# run chacha20 once to generate poly1305 key |
75
|
|
|
|
|
|
|
# the iv is the packet sequence number |
76
|
3
|
|
|
|
|
9
|
$seqnr = $ciph->_seqnr_bytes($seqnr); |
77
|
3
|
|
|
|
|
13
|
$ciph->{main}->ivsetup($seqnr,''); |
78
|
3
|
|
|
|
|
8
|
my $poly_key = "\0" x POLY1305_KEYLEN; |
79
|
3
|
|
|
|
|
17
|
$poly_key = $ciph->{main}->encrypt($poly_key); |
80
|
|
|
|
|
|
|
|
81
|
3
|
|
|
|
|
10
|
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
|
|
|
|
|
9
|
delete $ciph->{length}; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# check tag before decrypting packet |
92
|
3
|
|
|
|
|
21
|
my $expected_tag = $ciph->{main}->poly1305(substr($data,0,$aadlen+$datalen), |
93
|
|
|
|
|
|
|
$poly_key); |
94
|
3
|
|
|
|
|
12
|
my $tag = substr($data,$aadlen+$datalen,$ciph->authlen); |
95
|
3
|
50
|
|
|
|
10
|
croak "Invalid poly1305 tag" |
96
|
|
|
|
|
|
|
if $expected_tag ne $tag; |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# set chacha's block counter to 1 |
99
|
3
|
|
|
|
|
13
|
$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__ |