blib/lib/Net/SSH/Perl/Cipher/ChachaPoly.pm

filter: Statementstmt 84.8% 67 / 79 Branchbran 41.6% 5 / 12 Conditioncond 20.0% 1 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 83.3% 15 / 18 Podpod 33.3% 3 / 9 totaltotal 72.2% 91 / 126 CC 8 SCAR 8.8
CC
8
Cov
76%
get_length
6.0
decrypt
4.7
encrypt
3.1
CRAP 2.4
« blib/lib/Net/SSH/Perl/Kex/C25519.pm ↑ Summary blib/lib/Net/SSH/Perl/Kex/DH1.pm »
1 package Net::SSH::Perl::Cipher::ChachaPoly;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Net::SSH::Perl::Cipher;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use base qw( Net::SSH::Perl::Cipher );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use constant POLY1305_KEYLEN => 32;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use constant ONE => chr(1) . "\0" x 7; # NB little endian
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use constant AADLEN => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶unless (grep /^Net::SSH::Perl$/, @DynaLoader::dl_modules) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /^Net::SSH::Perl$/, @DynaLoader::dl_modules) 1 0 1
14 1 ▶ use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 XSLoader::load('Net::SSH::Perl');
16 }
17  
18 6 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
19 6 my $class = shift;
20 6 my $ciph = bless { }, $class;
21 6 ▶ $ciph->init(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 6 0 6
22 6 $ciph;
23 }
24  
25 6 ▶sub keysize { 64 }
Subroutine keysize: called CC=1 SCAR=0.0
Pod undocumented
26 0 ▶sub blocksize { 8 }
Subroutine blocksize: not called CC=1 SCAR=6.9
Pod undocumented
27 6 ▶sub authlen { 16 }
Subroutine authlen: called CC=1 SCAR=0.0
Pod undocumented
28 0 ▶sub ivlen { 0 }
Subroutine ivlen: not called CC=1 SCAR=6.9
Pod undocumented
29  
30 6 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
31 6 my $ciph = shift;
32 6 my $key = shift;
33  
34 6 my $size = $ciph->keysize/2;
35 6 my $mainkey = substr($key,0,$size);
36 6 my $headerkey = substr($key,$size,$size);
37 6 $ciph->{main} = Crypt::OpenSSH::ChachaPoly->new($mainkey);
38 6 $ciph->{header} = Crypt::OpenSSH::ChachaPoly->new($headerkey);
39 }
40  
41 6 ▶sub _seqnr_bytes {
Subroutine _seqnr_bytes: called CC=1 SCAR=0.0
42 6 my ($ciph, $seqnr) = @_;
43 6 pack('N',0) . pack('N',$seqnr); # seqnr is only 32-bit
44 }
45  
46 3 ▶sub encrypt {
Subroutine encrypt: called CC=3 SCAR=11.3
Pod documented
47 3 my($ciph, $data, $seqnr, $aadlen) = @_;
48 3 ▶ $aadlen ||= 0;
Condition50%1/2
$aadlen ||= 0
l!l
03
MC/DC0%0/1
$aadlen ||= 0
$aadlen
Truth table50%1/2
A
A= $aadlen
Aresult
11
00
49  
50 # run chacha20 once to generate poly1305 key
51 # the iv is the packet sequence number
52 3 $seqnr = $ciph->_seqnr_bytes($seqnr);
53 3 $ciph->{main}->ivsetup($seqnr,'');
54 3 my $poly_key = "\0" x POLY1305_KEYLEN;
55 3 $poly_key = $ciph->{main}->encrypt($poly_key);
56  
57 3 my $aadenc;
58 3 ▶ if ($aadlen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($aadlen) 0 3 3
59 # encrypt packet length from first four bytes of data
60 0 $ciph->{header}->ivsetup($seqnr,'');
61 0 $aadenc = $ciph->{header}->encrypt(substr($data,0,$aadlen));
62 }
63  
64 # set chacha's block counter to 1
65 3 $ciph->{main}->ivsetup($seqnr,ONE);
66 3 my $enc = $aadenc . $ciph->{main}->encrypt(substr($data,$aadlen));
67 3 $enc .= $ciph->{main}->poly1305($enc,$poly_key);
68 3 return $enc;
69 }
70  
71 3 ▶sub decrypt {
Subroutine decrypt: called CC=4 SCAR=15.4
Pod documented
72 3 my($ciph, $data, $seqnr, $aadlen) = @_;
73  
74 # run chacha20 once to generate poly1305 key
75 # the iv is the packet sequence number
76 3 $seqnr = $ciph->_seqnr_bytes($seqnr);
77 3 $ciph->{main}->ivsetup($seqnr,'');
78 3 my $poly_key = "\0" x POLY1305_KEYLEN;
79 3 $poly_key = $ciph->{main}->encrypt($poly_key);
80  
81 3 my $datalen = length($data) - $ciph->authlen;
82 3 ▶ if ($aadlen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($aadlen) 0 3 3
83 $datalen = unpack('N', $ciph->{length} ||
84 0 ▶ eval {
Condition0%0/3
$ciph->{'length'} || eval { do { $ciph->{'header'}->ivsetup($seqnr, ''); $ciph->{'header'}->decrypt(substr($data, 0, $aadlen)) } }
l!l&&r!l&&!r
000
MC/DC0%0/2
$ciph->{'length'} || eval { do { $ciph->{'header'}->ivsetup($seqnr, ''); $ciph->{'header'}->decrypt(substr($data, 0, $aadlen)) } }
$ciph->{'length'} eval { do { $ciph->{'header'}->ivsetup($seqnr, ''); $ciph->{'header'}->decrypt(substr($data, 0, $aadlen)) } }
Truth table0%0/3
A || B
A= $ciph->{'length'}B= eval { do { $ciph->{'header'}->ivsetup($seqnr, ''); $ciph->{'header'}->decrypt(substr($data, 0, $aadlen)) } }
ABresult
1X1
011
000
85 0 $ciph->{header}->ivsetup($seqnr,'');
86 0 $ciph->{header}->decrypt(substr($data,0,$aadlen))
87 });
88 }
89 3 delete $ciph->{length};
90  
91 # check tag before decrypting packet
92 3 my $expected_tag = $ciph->{main}->poly1305(substr($data,0,$aadlen+$datalen),
93 $poly_key);
94 3 my $tag = substr($data,$aadlen+$datalen,$ciph->authlen);
95 3 ▶ croak "Invalid poly1305 tag"
Branch: 1 branch
BranchTrueFalseTotal
if $expected_tag ne $tag 0 3 3
96 if $expected_tag ne $tag;
97  
98 # set chacha's block counter to 1
99 3 $ciph->{main}->ivsetup($seqnr,ONE);
100 # return payload only
101 3 $ciph->{main}->decrypt(substr($data,$aadlen,$datalen));
102 }
103  
104 0 ▶sub get_length {
Subroutine get_length: not called CC=2 SCAR=17.9
Pod undocumented
105 0 my($ciph, $data, $seqnr) = @_;
106  
107 0 ▶ return if length($data) < AADLEN;
Branch: 1 branch
BranchTrueFalseTotal
if length $data < 4 0 0 0
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 1;
115 __END__
« blib/lib/Net/SSH/Perl/Kex/C25519.pm ↑ Summary blib/lib/Net/SSH/Perl/Kex/DH1.pm »