| 1 | package Net::SSH::Perl::Key::Ed25519; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Net::SSH::Perl::Buffer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Crypt::Digest::SHA512 qw( sha512 ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use base qw( Net::SSH::Perl::Key ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Crypt::PRNG qw( random_bytes ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Crypt::Misc qw( decode_b64 encode_b64 ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use constant MARK_BEGIN => "-----BEGIN OPENSSH PRIVATE KEY-----\n"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use constant MARK_END => "-----END OPENSSH PRIVATE KEY-----\n"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use constant AUTH_MAGIC => "openssh-key-v1\0"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use constant ED25519_SK_SZ => 64; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use constant ED25519_PK_SZ => 32; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use constant SALT_LEN => 16; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use constant DEFAULT_ROUNDS => 16; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use constant DEFAULT_CIPHERNAME => 'aes256-cbc'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use constant KDFNAME => 'bcrypt'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | unless (grep /^Net::SSH::Perl$/, @DynaLoader::dl_modules) { | ||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use XSLoader; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | XSLoader::load('Net::SSH::Perl'); | ||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | ▶ | sub ssh_name { 'ssh-ed25519' } | ||||||||||||||||||||||||||
|
Subroutine
ssh_name: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | sub init { | ||||||||||||||||||||||||||||
| 31 | 0 | ▶ | my $key = shift; | ||||||||||||||||||||||||||
|
Subroutine
init: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 32 | 0 | my($blob) = @_; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | if ($blob) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 0 | my $b = Net::SSH::Perl::Buffer->new( MP => 'SSH2' ); | |||||||||||||||||||||||||||
| 36 | 0 | $b->append($blob); | |||||||||||||||||||||||||||
| 37 | 0 | my $ktype = $b->get_str; | |||||||||||||||||||||||||||
| 38 | 0 | ▶ | croak __PACKAGE__, "->init: cannot handle type '$ktype'" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | unless $ktype eq $key->ssh_name; | ||||||||||||||||||||||||||||
| 40 | 0 | $key->{pub} = $b->get_str; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | sub keygen { | ||||||||||||||||||||||||||||
| 45 | 0 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
keygen: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 46 | 0 | my $key = __PACKAGE__->new(undef); | |||||||||||||||||||||||||||
| 47 | 0 | my $secret = random_bytes(ED25519_PK_SZ); | |||||||||||||||||||||||||||
| 48 | 0 | ($key->{pub},$key->{priv}) = ed25519_generate_keypair($secret); | |||||||||||||||||||||||||||
| 49 | 0 | $key; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | sub read_private { | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
read_private: not called CC=24 SCAR=63.3
| |||||||||||||||||||||||||||||
| 54 | 0 | my($key_file, $passphrase) = @_; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | local *FH; | |||||||||||||||||||||||||||
| 57 | 0 | ▶ | open FH, $key_file or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | my $content = do { local $/; <FH> }; | |||||||||||||||||||||||||||
| 59 | 0 | close FH; | |||||||||||||||||||||||||||
| 60 | 0 | $content = substr($content,length(MARK_BEGIN), | |||||||||||||||||||||||||||
| 61 | length($content)-length(MARK_END)-length(MARK_BEGIN)); | ||||||||||||||||||||||||||||
| 62 | 0 | my $blob = decode_b64($content); | |||||||||||||||||||||||||||
| 63 | 0 | my $str = AUTH_MAGIC; | |||||||||||||||||||||||||||
| 64 | 0 | ▶ | croak "Invalid key format" unless $blob =~ /^${str}/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | my $b = Net::SSH::Perl::Buffer->new( MP => 'SSH2' ); | |||||||||||||||||||||||||||
| 67 | 0 | $b->append($blob); | |||||||||||||||||||||||||||
| 68 | 0 | $b->consume(length(AUTH_MAGIC)); | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | my $ciphername = $b->get_str; | |||||||||||||||||||||||||||
| 71 | 0 | my $kdfname = $b->get_str; | |||||||||||||||||||||||||||
| 72 | 0 | my $kdfoptions = $b->get_str; | |||||||||||||||||||||||||||
| 73 | 0 | my $nkeys = $b->get_int32; | |||||||||||||||||||||||||||
| 74 | 0 | my $pub_key = $b->get_str; | |||||||||||||||||||||||||||
| 75 | 0 | my $encrypted = $b->get_str; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | croak 'Wrong passphrase' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $passphrase and $ciphername ne "none"
MC/DC0%0/2
not $passphrase and $ciphername ne "none"
not $passphrase
$ciphername ne "none"
Truth table0%0/3
not(A) and B
A= not $passphraseB= $ciphername ne "none"
| |||||||||||||||||||||||||||||
| 78 | if !$passphrase && $ciphername ne 'none'; | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | croak 'Unknown cipher' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$kdfname ne "none" and $kdfname ne "bcrypt"
MC/DC0%0/2
$kdfname ne "none" and $kdfname ne "bcrypt"
$kdfname ne "none"
$kdfname ne "bcrypt"
Truth table0%0/3
A and B
A= $kdfname ne "none"B= $kdfname ne "bcrypt"
| |||||||||||||||||||||||||||||
| 81 | if $kdfname ne 'none' && $kdfname ne KDFNAME; | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | croak 'Invalid format' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$kdfname ne "none" and $ciphername eq "none"
MC/DC0%0/2
$kdfname ne "none" and $ciphername eq "none"
$kdfname ne "none"
$ciphername eq "none"
Truth table0%0/3
A and B
A= $kdfname ne "none"B= $ciphername eq "none"
| |||||||||||||||||||||||||||||
| 84 | if $kdfname ne 'none' && $ciphername eq 'none'; | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | croak 'Invalid format: nkeys > 1' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | if $nkeys != 1; | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | my $decrypted; | |||||||||||||||||||||||||||
| 90 | 0 | ▶ | if ($ciphername eq 'none') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | $decrypted = $encrypted; | |||||||||||||||||||||||||||
| 92 | } else { | ||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if ($kdfname eq KDFNAME) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 1 | ▶ | use Net::SSH::Perl::Cipher; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 95 | 0 | my $cipher = eval { Net::SSH::Perl::Cipher->new($ciphername) }; | |||||||||||||||||||||||||||
| 96 | 0 | ▶ | croak "Cannot load cipher $ciphername" unless $cipher; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | croak 'Invalid format' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
length $encrypted < $cipher->blocksize or length($encrypted) % $cipher->blocksize
MC/DC0%0/2
length $encrypted < $cipher->blocksize or length($encrypted) % $cipher->blocksize
length $encrypted < $cipher->blocksize
length($encrypted) % $cipher->blocksize
Truth table0%0/3
A or B
A= length $encrypted < $cipher->blocksizeB= length($encrypted) % $cipher->blocksize
| |||||||||||||||||||||||||||||
| 98 | if length($encrypted) < $cipher->blocksize || | ||||||||||||||||||||||||||||
| 99 | length($encrypted) % $cipher->blocksize; | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | my $keylen = $cipher->keysize; | |||||||||||||||||||||||||||
| 102 | 0 | my $ivlen = $cipher->ivlen; | |||||||||||||||||||||||||||
| 103 | 0 | my $authlen = $cipher->authlen; | |||||||||||||||||||||||||||
| 104 | 0 | my $tag = $b->bytes($b->offset,$authlen); | |||||||||||||||||||||||||||
| 105 | 0 | ▶ | croak 'Invalid format' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | if length($tag) != $authlen; | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | $b->empty; | |||||||||||||||||||||||||||
| 109 | 0 | $b->append($kdfoptions); | |||||||||||||||||||||||||||
| 110 | 0 | my $salt = $b->get_str; | |||||||||||||||||||||||||||
| 111 | 0 | ▶ | croak "Invalid format" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | if length($salt) != SALT_LEN; | ||||||||||||||||||||||||||||
| 113 | 0 | my $rounds = $b->get_int32; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | my $km = bcrypt_pbkdf($passphrase, $salt, $keylen+$ivlen, $rounds); | |||||||||||||||||||||||||||
| 116 | 0 | my $key = substr($km,0,$keylen); | |||||||||||||||||||||||||||
| 117 | 0 | my $iv = substr($km,$keylen,$ivlen); | |||||||||||||||||||||||||||
| 118 | 0 | $cipher->init($key,$iv); | |||||||||||||||||||||||||||
| 119 | 0 | $decrypted = $cipher->decrypt($encrypted . $tag); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | $b->empty; | |||||||||||||||||||||||||||
| 124 | 0 | $b->append($decrypted); | |||||||||||||||||||||||||||
| 125 | 0 | my $check1 = $b->get_int32; | |||||||||||||||||||||||||||
| 126 | 0 | my $check2 = $b->get_int32; | |||||||||||||||||||||||||||
| 127 | 0 | ▶ | croak 'Wrong passphrase (check mismatch)' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$check1 != $check2 or not defined $check1
MC/DC0%0/2
$check1 != $check2 or not defined $check1
$check1 != $check2
not defined $check1
Truth table0%0/3
A or not(B)
A= $check1 != $check2B= not defined $check1
| |||||||||||||||||||||||||||||
| 128 | if $check1 != $check2 || ! defined $check1; | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | my $type = $b->get_str; | |||||||||||||||||||||||||||
| 131 | 0 | ▶ | croak 'Wrong key type' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | unless $type eq $class->ssh_name; | ||||||||||||||||||||||||||||
| 133 | 0 | $pub_key = $b->get_str; | |||||||||||||||||||||||||||
| 134 | 0 | my $priv_key = $b->get_str; | |||||||||||||||||||||||||||
| 135 | 0 | ▶ | croak 'Invalid format' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
length $pub_key != 32 or length $priv_key != 64
MC/DC0%0/2
length $pub_key != 32 or length $priv_key != 64
length $pub_key != 32
length $priv_key != 64
Truth table0%0/3
A or B
A= length $pub_key != 32B= length $priv_key != 64
| |||||||||||||||||||||||||||||
| 136 | if length($pub_key) != ED25519_PK_SZ || | ||||||||||||||||||||||||||||
| 137 | length($priv_key) != ED25519_SK_SZ; | ||||||||||||||||||||||||||||
| 138 | 0 | my $comment = $b->get_str; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | # check padding | ||||||||||||||||||||||||||||
| 141 | 0 | my $padnum = 0; | |||||||||||||||||||||||||||
| 142 | 0 | ▶ | while ($b->offset < $b->length) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | croak "Invalid format" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | if ord($b->get_char) != ++$padnum; | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | my $key = __PACKAGE__->new(undef); | |||||||||||||||||||||||||||
| 148 | 0 | $key->comment($comment); | |||||||||||||||||||||||||||
| 149 | 0 | $key->{pub} = $pub_key; | |||||||||||||||||||||||||||
| 150 | 0 | $key->{priv} = $priv_key; | |||||||||||||||||||||||||||
| 151 | 0 | $key; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | sub write_private { | ||||||||||||||||||||||||||||
| 155 | 0 | ▶ | my $key = shift; | ||||||||||||||||||||||||||
|
Subroutine
write_private: not called CC=11 SCAR=47.7
| |||||||||||||||||||||||||||||
| 156 | 0 | my($key_file, $passphrase, $ciphername, $rounds) = @_; | |||||||||||||||||||||||||||
| 157 | 0 | my ($kdfoptions, $kdfname, $blocksize, $cipher, $authlen, $tag); | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 0 | ▶ | if ($passphrase) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 0 | ▶ | $ciphername ||= DEFAULT_CIPHERNAME; | ||||||||||||||||||||||||||
|
Condition0%0/2
$ciphername ||= "aes256-cbc"
MC/DC0%0/1
$ciphername ||= "aes256-cbc"
$ciphername
Truth table0%0/2
A
A= $ciphername
| |||||||||||||||||||||||||||||
| 161 | 1 | ▶ | use Net::SSH::Perl::Cipher; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 162 | 0 | $cipher = eval { Net::SSH::Perl::Cipher->new($ciphername) }; | |||||||||||||||||||||||||||
| 163 | 0 | ▶ | croak "Cannot load cipher $ciphername" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | unless $cipher; | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | # cipher init params | ||||||||||||||||||||||||||||
| 167 | 0 | $kdfname = KDFNAME; | |||||||||||||||||||||||||||
| 168 | 0 | $blocksize = $cipher->blocksize; | |||||||||||||||||||||||||||
| 169 | 0 | my $keylen = $cipher->keysize; | |||||||||||||||||||||||||||
| 170 | 0 | my $ivlen = $cipher->ivlen; | |||||||||||||||||||||||||||
| 171 | 0 | ▶ | $rounds ||= DEFAULT_ROUNDS; | ||||||||||||||||||||||||||
|
Condition0%0/2
$rounds ||= 16
MC/DC0%0/1
$rounds ||= 16
$rounds
Truth table0%0/2
A
A= $rounds
| |||||||||||||||||||||||||||||
| 172 | 0 | my $salt = random_bytes(SALT_LEN); | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 0 | my $kdf = Net::SSH::Perl::Buffer->new( MP => 'SSH2' ); | |||||||||||||||||||||||||||
| 175 | 0 | $kdf->put_str($salt); | |||||||||||||||||||||||||||
| 176 | 0 | $kdf->put_int32($rounds); | |||||||||||||||||||||||||||
| 177 | 0 | $kdfoptions = $kdf->bytes; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | # get key material | ||||||||||||||||||||||||||||
| 180 | 0 | my $km = bcrypt_pbkdf($passphrase, $salt, $keylen+$ivlen, $rounds); | |||||||||||||||||||||||||||
| 181 | 0 | my $key = substr($km,0,$keylen); | |||||||||||||||||||||||||||
| 182 | 0 | my $iv = substr($km,$keylen,$ivlen); | |||||||||||||||||||||||||||
| 183 | 0 | $cipher->init($key,$iv); | |||||||||||||||||||||||||||
| 184 | 0 | $authlen = $cipher->authlen; | |||||||||||||||||||||||||||
| 185 | } else { | ||||||||||||||||||||||||||||
| 186 | 0 | $ciphername = 'none'; | |||||||||||||||||||||||||||
| 187 | 0 | $kdfname = 'none'; | |||||||||||||||||||||||||||
| 188 | 0 | $blocksize = 8; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | 0 | my $b = Net::SSH::Perl::Buffer->new( MP => 'SSH2' ); | |||||||||||||||||||||||||||
| 191 | 0 | $b->put_char(AUTH_MAGIC); | |||||||||||||||||||||||||||
| 192 | 0 | $b->put_str($ciphername); | |||||||||||||||||||||||||||
| 193 | 0 | $b->put_str($kdfname); | |||||||||||||||||||||||||||
| 194 | 0 | $b->put_str($kdfoptions); | |||||||||||||||||||||||||||
| 195 | 0 | $b->put_int32(1); # one key | |||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | # public key | ||||||||||||||||||||||||||||
| 198 | 0 | my $pub = Net::SSH::Perl::Buffer->new( MP => 'SSH2' ); | |||||||||||||||||||||||||||
| 199 | 0 | $pub->put_str($key->ssh_name); | |||||||||||||||||||||||||||
| 200 | 0 | $pub->put_str($key->{pub}); | |||||||||||||||||||||||||||
| 201 | 0 | $b->put_str($pub->bytes); | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | # create private key blob | ||||||||||||||||||||||||||||
| 204 | 0 | my $kb = Net::SSH::Perl::Buffer->new( MP => 'SSH2' ); | |||||||||||||||||||||||||||
| 205 | 0 | my $checkint = int(rand(0xffffffff)); | |||||||||||||||||||||||||||
| 206 | 0 | $kb->put_int32($checkint); | |||||||||||||||||||||||||||
| 207 | 0 | $kb->put_int32($checkint); | |||||||||||||||||||||||||||
| 208 | 0 | $kb->put_str($key->ssh_name); | |||||||||||||||||||||||||||
| 209 | 0 | $kb->put_str($key->{pub}); | |||||||||||||||||||||||||||
| 210 | 0 | $kb->put_str($key->{priv}); | |||||||||||||||||||||||||||
| 211 | 0 | $kb->put_str($key->comment); | |||||||||||||||||||||||||||
| 212 | 0 | ▶ | if (my $r = length($kb->bytes) % $blocksize) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | 0 | $kb->put_char(chr($_)) foreach (1..$blocksize-$r); | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | 0 | ▶ | my $bytes = $cipher ? $cipher->encrypt($kb->bytes) : $kb->bytes; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | 0 | ▶ | $tag = substr($bytes,-$authlen,$authlen,'') if $authlen; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | 0 | $b->put_str($bytes); | |||||||||||||||||||||||||||
| 218 | 0 | ▶ | $b->put_chars($tag) if $tag; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 0 | local *FH; | |||||||||||||||||||||||||||
| 221 | 0 | ▶ | open FH, ">$key_file" or die "Cannot write key file"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 0 | print FH MARK_BEGIN; | |||||||||||||||||||||||||||
| 223 | 0 | print FH encode_b64($b->bytes),"\n"; | |||||||||||||||||||||||||||
| 224 | 0 | print FH MARK_END; | |||||||||||||||||||||||||||
| 225 | 0 | close FH; | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | sub sign { | ||||||||||||||||||||||||||||
| 229 | 0 | ▶ | my $key = shift; | ||||||||||||||||||||||||||
|
Subroutine
sign: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 230 | 0 | my $data = shift; | |||||||||||||||||||||||||||
| 231 | 0 | my $sig = ed25519_sign_message($data, $key->{priv}); | |||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 0 | my $b = Net::SSH::Perl::Buffer->new( MP => 'SSH2' ); | |||||||||||||||||||||||||||
| 234 | 0 | $b->put_str($key->ssh_name); | |||||||||||||||||||||||||||
| 235 | 0 | $b->put_str($sig); | |||||||||||||||||||||||||||
| 236 | 0 | $b->bytes; | |||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | sub verify { | ||||||||||||||||||||||||||||
| 240 | 0 | ▶ | my $key = shift; | ||||||||||||||||||||||||||
|
Subroutine
verify: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 241 | 0 | my($signature, $data) = @_; | |||||||||||||||||||||||||||
| 242 | 0 | my $sigblob; | |||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 0 | my $b = Net::SSH::Perl::Buffer->new( MP => 'SSH2' ); | |||||||||||||||||||||||||||
| 245 | 0 | $b->append($signature); | |||||||||||||||||||||||||||
| 246 | 0 | my $ktype = $b->get_str; | |||||||||||||||||||||||||||
| 247 | 0 | ▶ | croak "Can't verify type ", $ktype unless $ktype eq $key->ssh_name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 248 | 0 | $sigblob = $b->get_str; | |||||||||||||||||||||||||||
| 249 | 0 | ▶ | croak "Invalid format" unless length($sigblob) == 64; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | 0 | ed25519_verify_message($data,$key->{pub},$sigblob); | |||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | sub equal { | ||||||||||||||||||||||||||||
| 255 | 0 | ▶ | my($keyA, $keyB) = @_; | ||||||||||||||||||||||||||
|
Subroutine
equal: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 256 | $keyA->{pub} && $keyB->{pub} && | ||||||||||||||||||||||||||||
| 257 | 0 | ▶ | $keyA->{pub} eq $keyB->{pub}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$keyA->{"pub"} and $keyB->{"pub"}
MC/DC0%0/2
$keyA->{"pub"} and $keyB->{"pub"}
$keyA->{"pub"}
$keyB->{"pub"}
Truth table0%0/3
A and B
A= $keyA->{"pub"}B= $keyB->{"pub"}
| |||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | sub as_blob { | ||||||||||||||||||||||||||||
| 261 | 0 | ▶ | my $key = shift; | ||||||||||||||||||||||||||
|
Subroutine
as_blob: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 262 | 0 | my $b = Net::SSH::Perl::Buffer->new( MP => 'SSH2' ); | |||||||||||||||||||||||||||
| 263 | 0 | $b->put_str($key->ssh_name); | |||||||||||||||||||||||||||
| 264 | 0 | $b->put_str($key->{pub}); | |||||||||||||||||||||||||||
| 265 | 0 | $b->bytes; | |||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 0 | ▶ | sub fingerprint_raw { $_[0]->as_blob } | ||||||||||||||||||||||||||
|
Subroutine
fingerprint_raw: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | sub bcrypt_hash { | ||||||||||||||||||||||||||||
| 271 | 0 | ▶ | my ($sha2pass, $sha2salt) = @_; | ||||||||||||||||||||||||||
|
Subroutine
bcrypt_hash: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 272 | 0 | my $ciphertext = 'OxychromaticBlowfishSwatDynamite'; | |||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | 0 | my $ctx = bf_init(); | |||||||||||||||||||||||||||
| 275 | 0 | bf_expandstate($ctx,$sha2salt,$sha2pass); | |||||||||||||||||||||||||||
| 276 | 0 | ▶ | for (my $i=0; $i<64; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 277 | 0 | bf_expand0state($ctx,$sha2salt); | |||||||||||||||||||||||||||
| 278 | 0 | bf_expand0state($ctx,$sha2pass); | |||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||
| 280 | # iterate 64 times | ||||||||||||||||||||||||||||
| 281 | 0 | bf_encrypt_iterate($ctx,$ciphertext,64); | |||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | sub bcrypt_pbkdf { | ||||||||||||||||||||||||||||
| 285 | 0 | ▶ | my ($pass, $salt, $keylen, $rounds) = @_; | ||||||||||||||||||||||||||
|
Subroutine
bcrypt_pbkdf: not called CC=9 SCAR=43.2
| |||||||||||||||||||||||||||||
| 286 | 0 | my $out; | |||||||||||||||||||||||||||
| 287 | 1 | ▶ | use constant BCRYPT_HASHSIZE => 32; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 288 | 0 | my $key = "\0" x $keylen; | |||||||||||||||||||||||||||
| 289 | 0 | my $origkeylen = $keylen; | |||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | 0 | ▶ | return if $rounds < 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 292 | 0 | ▶ | return unless $pass && $salt; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$pass and $salt
MC/DC0%0/2
$pass and $salt
$pass
$salt
Truth table0%0/3
A and B
A= $passB= $salt
| |||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 0 | my $stride = int(($keylen + BCRYPT_HASHSIZE - 1) / BCRYPT_HASHSIZE); | |||||||||||||||||||||||||||
| 295 | 0 | my $amt = int(($keylen + $stride - 1) / $stride); | |||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | 0 | my $sha2pass = sha512($pass); | |||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | 0 | ▶ | for (my $count = 1; $keylen > 1; $count++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | 0 | my $countsalt = pack('N',$count & 0xffffffff); | |||||||||||||||||||||||||||
| 301 | # first round, salt is salt | ||||||||||||||||||||||||||||
| 302 | 0 | my $sha2salt = sha512($salt . $countsalt); | |||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | 0 | my $tmpout = $out = bcrypt_hash($sha2pass, $sha2salt); | |||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | 0 | ▶ | for (my $i=1; $i < $rounds; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 307 | # subsequent rounds, salt is previous output | ||||||||||||||||||||||||||||
| 308 | 0 | $sha2salt = sha512($tmpout); | |||||||||||||||||||||||||||
| 309 | 0 | $tmpout = bcrypt_hash($sha2pass,$sha2salt); | |||||||||||||||||||||||||||
| 310 | 0 | $out ^= $tmpout; | |||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | # pbkdf2 deviation: output the key material non-linearly. | ||||||||||||||||||||||||||||
| 314 | 0 | ▶ | $amt = $amt<$keylen ? $amt : $keylen; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 315 | 0 | my $i; | |||||||||||||||||||||||||||
| 316 | 0 | ▶ | for ($i=0; $i<$amt; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 317 | 0 | my $dest = $i * $stride + ($count - 1); | |||||||||||||||||||||||||||
| 318 | 0 | ▶ | last if $dest >= $origkeylen; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 319 | 0 | substr($key,$dest,1,substr($out,$i,1)); | |||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||
| 321 | 0 | $keylen -= $i; | |||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||
| 323 | 0 | return $key; | |||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | 1; | ||||||||||||||||||||||||||||
| 327 | __END__ | ||||||||||||||||||||||||||||