| 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 | unless (grep /^Net::SSH::Perl$/, @DynaLoader::dl_modules) { | ||||||||||||||||||||
| 14 | 1 | ▶ | use XSLoader; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | XSLoader::load('Net::SSH::Perl'); | ||||||||||||||||||||
| 16 | } | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | sub new { | ||||||||||||||||||||
| 19 | 0 | ▶ | my $class = shift; | ||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 20 | 0 | my $ciph = bless { }, $class; | |||||||||||||||||||
| 21 | 0 | ▶ | $ciph->init(@_) if @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 22 | 0 | $ciph; | |||||||||||||||||||
| 23 | } | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 0 | ▶ | sub keysize { 64 } | ||||||||||||||||||
|
Subroutine
keysize: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 26 | 0 | ▶ | sub blocksize { 8 } | ||||||||||||||||||
|
Subroutine
blocksize: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 27 | 0 | ▶ | sub authlen { 16 } | ||||||||||||||||||
|
Subroutine
authlen: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 28 | 0 | ▶ | sub ivlen { 0 } | ||||||||||||||||||
|
Subroutine
ivlen: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | sub init { | ||||||||||||||||||||
| 31 | 0 | ▶ | my $ciph = shift; | ||||||||||||||||||
|
Subroutine
init: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 32 | 0 | my $key = shift; | |||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 0 | my $size = $ciph->keysize/2; | |||||||||||||||||||
| 35 | 0 | my $mainkey = substr($key,0,$size); | |||||||||||||||||||
| 36 | 0 | my $headerkey = substr($key,$size,$size); | |||||||||||||||||||
| 37 | 0 | $ciph->{main} = Crypt::OpenSSH::ChachaPoly->new($mainkey); | |||||||||||||||||||
| 38 | 0 | $ciph->{header} = Crypt::OpenSSH::ChachaPoly->new($headerkey); | |||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | sub _seqnr_bytes { | ||||||||||||||||||||
| 42 | 0 | ▶ | my ($ciph, $seqnr) = @_; | ||||||||||||||||||
|
Subroutine
_seqnr_bytes: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 43 | 0 | pack('N',0) . pack('N',$seqnr); # seqnr is only 32-bit | |||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | sub encrypt { | ||||||||||||||||||||
| 47 | 0 | ▶ | my($ciph, $data, $seqnr, $aadlen) = @_; | ||||||||||||||||||
|
Subroutine
encrypt: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 48 | 0 | ▶ | $aadlen ||= 0; | ||||||||||||||||||
|
Condition0%0/2
$aadlen ||= 0
MC/DC0%0/1
$aadlen ||= 0
$aadlen
Truth table0%0/2
A
A= $aadlen
| |||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | # run chacha20 once to generate poly1305 key | ||||||||||||||||||||
| 51 | # the iv is the packet sequence number | ||||||||||||||||||||
| 52 | 0 | $seqnr = $ciph->_seqnr_bytes($seqnr); | |||||||||||||||||||
| 53 | 0 | $ciph->{main}->ivsetup($seqnr,''); | |||||||||||||||||||
| 54 | 0 | my $poly_key = "\0" x POLY1305_KEYLEN; | |||||||||||||||||||
| 55 | 0 | $poly_key = $ciph->{main}->encrypt($poly_key); | |||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 0 | my $aadenc; | |||||||||||||||||||
| 58 | 0 | ▶ | if ($aadlen) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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 | 0 | $ciph->{main}->ivsetup($seqnr,ONE); | |||||||||||||||||||
| 66 | 0 | my $enc = $aadenc . $ciph->{main}->encrypt(substr($data,$aadlen)); | |||||||||||||||||||
| 67 | 0 | $enc .= $ciph->{main}->poly1305($enc,$poly_key); | |||||||||||||||||||
| 68 | 0 | return $enc; | |||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | sub decrypt { | ||||||||||||||||||||
| 72 | 0 | ▶ | my($ciph, $data, $seqnr, $aadlen) = @_; | ||||||||||||||||||
|
Subroutine
decrypt: not called CC=4 SCAR=30.0
| |||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | # run chacha20 once to generate poly1305 key | ||||||||||||||||||||
| 75 | # the iv is the packet sequence number | ||||||||||||||||||||
| 76 | 0 | $seqnr = $ciph->_seqnr_bytes($seqnr); | |||||||||||||||||||
| 77 | 0 | $ciph->{main}->ivsetup($seqnr,''); | |||||||||||||||||||
| 78 | 0 | my $poly_key = "\0" x POLY1305_KEYLEN; | |||||||||||||||||||
| 79 | 0 | $poly_key = $ciph->{main}->encrypt($poly_key); | |||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 0 | my $datalen = length($data) - $ciph->authlen; | |||||||||||||||||||
| 82 | 0 | ▶ | if ($aadlen) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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))
}
}
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))
}
}
| |||||||||||||||||||||
| 85 | 0 | $ciph->{header}->ivsetup($seqnr,''); | |||||||||||||||||||
| 86 | 0 | $ciph->{header}->decrypt(substr($data,0,$aadlen)) | |||||||||||||||||||
| 87 | }); | ||||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | 0 | delete $ciph->{length}; | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | # check tag before decrypting packet | ||||||||||||||||||||
| 92 | 0 | my $expected_tag = $ciph->{main}->poly1305(substr($data,0,$aadlen+$datalen), | |||||||||||||||||||
| 93 | $poly_key); | ||||||||||||||||||||
| 94 | 0 | my $tag = substr($data,$aadlen+$datalen,$ciph->authlen); | |||||||||||||||||||
| 95 | 0 | ▶ | croak "Invalid poly1305 tag" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | if $expected_tag ne $tag; | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | # set chacha's block counter to 1 | ||||||||||||||||||||
| 99 | 0 | $ciph->{main}->ivsetup($seqnr,ONE); | |||||||||||||||||||
| 100 | # return payload only | ||||||||||||||||||||
| 101 | 0 | $ciph->{main}->decrypt(substr($data,$aadlen,$datalen)); | |||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | sub get_length { | ||||||||||||||||||||
| 105 | 0 | ▶ | my($ciph, $data, $seqnr) = @_; | ||||||||||||||||||
|
Subroutine
get_length: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 0 | ▶ | return if length($data) < AADLEN; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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__ | ||||||||||||||||||||