blib/lib/Net/SSH/Perl/Key/Ed25519.pm

filter: Statementstmt 72.8% 188 / 258 Branchbran 46.0% 35 / 76 Conditioncond 25.0% 7 / 28 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 87.5% 28 / 32 Podpod 58.3% 7 / 12 totaltotal 62.5% 265 / 424 CC 49 SCAR 33.3
CC
49
Cov
64%
write_private
118.4
read_private
31.7
equal
12.0
CRAP 28.0
« blib/lib/Net/SSH/Perl/Auth/RSA.pm ↑ Summary blib/lib/Net/SSH/Perl/Subsystem/Server.pm »
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 1 ▶unless (grep /^Net::SSH::Perl$/, @DynaLoader::dl_modules) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /^Net::SSH::Perl$/, @DynaLoader::dl_modules) 1 0 1
24 1 ▶ use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 XSLoader::load('Net::SSH::Perl');
26 }
27  
28 3 ▶sub ssh_name { 'ssh-ed25519' }
Subroutine ssh_name: called CC=1 SCAR=0.0
Pod undocumented
29  
30 3 ▶sub init {
Subroutine init: called CC=3 SCAR=17.3
Pod undocumented
31 3 my $key = shift;
32 3 my($blob) = @_;
33  
34 3 ▶ if ($blob) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blob) 0 3 3
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
BranchTrueFalseTotal
unless $ktype eq $key->ssh_name 0 0 0
39 unless $ktype eq $key->ssh_name;
40 0 $key->{pub} = $b->get_str;
41 }
42 }
43  
44 1 ▶sub keygen {
Subroutine keygen: called CC=1 SCAR=0.0
Pod documented
45 1 my $class = shift;
46 1 my $key = __PACKAGE__->new(undef);
47 1 my $secret = random_bytes(ED25519_PK_SZ);
48 1 ($key->{pub},$key->{priv}) = ed25519_generate_keypair($secret);
49 1 $key;
50 }
51  
52 1 ▶sub read_private {
Subroutine read_private: called CC=24 SCAR=34.6
Pod documented
53 1 my $class = shift;
54 1 my($key_file, $passphrase) = @_;
55  
56 1 local *FH;
57 1 ▶ open FH, $key_file or return;
Branch: 1 branch
BranchTrueFalseTotal
open FH, $key_file or return 1 0 1
58 1 my $content = do { local $/; <FH> };
59 1 close FH;
60 1 $content = substr($content,length(MARK_BEGIN),
61 length($content)-length(MARK_END)-length(MARK_BEGIN));
62 1 my $blob = decode_b64($content);
63 1 my $str = AUTH_MAGIC;
64 1 ▶ croak "Invalid key format" unless $blob =~ /^${str}/;
Branch: 1 branch
BranchTrueFalseTotal
unless $blob =~ /^$str/ 0 1 1
65  
66 1 my $b = Net::SSH::Perl::Buffer->new( MP => 'SSH2' );
67 1 $b->append($blob);
68 1 $b->consume(length(AUTH_MAGIC));
69  
70 1 my $ciphername = $b->get_str;
71 1 my $kdfname = $b->get_str;
72 1 my $kdfoptions = $b->get_str;
73 1 my $nkeys = $b->get_int32;
74 1 my $pub_key = $b->get_str;
75 1 my $encrypted = $b->get_str;
76  
77 1 ▶ croak 'Wrong passphrase'
Branch: 1 branch
BranchTrueFalseTotal
if not $passphrase and $ciphername ne "none" 0 1 1
Condition33%1/3
not $passphrase and $ciphername ne "none"
!ll&&!rl&&r
100
MC/DC0%0/2
not $passphrase and $ciphername ne "none"
not $passphrase $ciphername ne "none"
Truth table33%1/3
not(A) and B
A= not $passphraseB= $ciphername ne "none"
ABresult
0X0
100
111
78 if !$passphrase && $ciphername ne 'none';
79  
80 1 ▶ croak 'Unknown cipher'
Branch: 1 branch
BranchTrueFalseTotal
if $kdfname ne "none" and $kdfname ne "bcrypt" 0 1 1
Condition33%1/3
$kdfname ne "none" and $kdfname ne "bcrypt"
!ll&&!rl&&r
010
MC/DC0%0/2
$kdfname ne "none" and $kdfname ne "bcrypt"
$kdfname ne "none" $kdfname ne "bcrypt"
Truth table33%1/3
A and B
A= $kdfname ne "none"B= $kdfname ne "bcrypt"
ABresult
0X0
100
111
81 if $kdfname ne 'none' && $kdfname ne KDFNAME;
82  
83 1 ▶ croak 'Invalid format'
Branch: 1 branch
BranchTrueFalseTotal
if $kdfname ne "none" and $ciphername eq "none" 0 1 1
Condition33%1/3
$kdfname ne "none" and $ciphername eq "none"
!ll&&!rl&&r
010
MC/DC0%0/2
$kdfname ne "none" and $ciphername eq "none"
$kdfname ne "none" $ciphername eq "none"
Truth table33%1/3
A and B
A= $kdfname ne "none"B= $ciphername eq "none"
ABresult
0X0
100
111
84 if $kdfname ne 'none' && $ciphername eq 'none';
85  
86 1 ▶ croak 'Invalid format: nkeys > 1'
Branch: 1 branch
BranchTrueFalseTotal
if $nkeys != 1 0 1 1
87 if $nkeys != 1;
88  
89 1 my $decrypted;
90 1 ▶ if ($ciphername eq 'none') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ciphername eq 'none') { } 0 1 1
91 0 $decrypted = $encrypted;
92 } else {
93 1 ▶ if ($kdfname eq KDFNAME) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kdfname eq "bcrypt") 1 0 1
94 1 ▶ use Net::SSH::Perl::Cipher;
Subroutine BEGIN: called CC=1 SCAR=0.0
95 1 my $cipher = eval { Net::SSH::Perl::Cipher->new($ciphername) };
96 1 ▶ croak "Cannot load cipher $ciphername" unless $cipher;
Branch: 1 branch
BranchTrueFalseTotal
unless $cipher 0 1 1
97 1 ▶ croak 'Invalid format'
Branch: 1 branch
BranchTrueFalseTotal
if length $encrypted < $cipher->blocksize or length($encrypted) % $cipher->blocksize 0 1 1
Condition33%1/3
length $encrypted < $cipher->blocksize or length($encrypted) % $cipher->blocksize
l!l&&r!l&&!r
001
MC/DC0%0/2
length $encrypted < $cipher->blocksize or length($encrypted) % $cipher->blocksize
length $encrypted < $cipher->blocksize length($encrypted) % $cipher->blocksize
Truth table33%1/3
A or B
A= length $encrypted < $cipher->blocksizeB= length($encrypted) % $cipher->blocksize
ABresult
1X1
011
000
98 if length($encrypted) < $cipher->blocksize ||
99 length($encrypted) % $cipher->blocksize;
100  
101 1 my $keylen = $cipher->keysize;
102 1 my $ivlen = $cipher->ivlen;
103 1 my $authlen = $cipher->authlen;
104 1 my $tag = $b->bytes($b->offset,$authlen);
105 1 ▶ croak 'Invalid format'
Branch: 1 branch
BranchTrueFalseTotal
if length $tag != $authlen 0 1 1
106 if length($tag) != $authlen;
107  
108 1 $b->empty;
109 1 $b->append($kdfoptions);
110 1 my $salt = $b->get_str;
111 1 ▶ croak "Invalid format"
Branch: 1 branch
BranchTrueFalseTotal
if length $salt != 16 0 1 1
112 if length($salt) != SALT_LEN;
113 1 my $rounds = $b->get_int32;
114  
115 1 my $km = bcrypt_pbkdf($passphrase, $salt, $keylen+$ivlen, $rounds);
116 1 my $key = substr($km,0,$keylen);
117 1 my $iv = substr($km,$keylen,$ivlen);
118 1 $cipher->init($key,$iv);
119 1 $decrypted = $cipher->decrypt($encrypted . $tag);
120 }
121 }
122  
123 1 $b->empty;
124 1 $b->append($decrypted);
125 1 my $check1 = $b->get_int32;
126 1 my $check2 = $b->get_int32;
127 1 ▶ croak 'Wrong passphrase (check mismatch)'
Branch: 1 branch
BranchTrueFalseTotal
if $check1 != $check2 or not defined $check1 0 1 1
Condition33%1/3
$check1 != $check2 or not defined $check1
l!l&&r!l&&!r
001
MC/DC0%0/2
$check1 != $check2 or not defined $check1
$check1 != $check2 not defined $check1
Truth table33%1/3
A or not(B)
A= $check1 != $check2B= not defined $check1
ABresult
1X1
011
000
128 if $check1 != $check2 || ! defined $check1;
129  
130 1 my $type = $b->get_str;
131 1 ▶ croak 'Wrong key type'
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq $class->ssh_name 0 1 1
132 unless $type eq $class->ssh_name;
133 1 $pub_key = $b->get_str;
134 1 my $priv_key = $b->get_str;
135 1 ▶ croak 'Invalid format'
Branch: 1 branch
BranchTrueFalseTotal
if length $pub_key != 32 or length $priv_key != 64 0 1 1
Condition33%1/3
length $pub_key != 32 or length $priv_key != 64
l!l&&r!l&&!r
001
MC/DC0%0/2
length $pub_key != 32 or length $priv_key != 64
length $pub_key != 32 length $priv_key != 64
Truth table33%1/3
A or B
A= length $pub_key != 32B= length $priv_key != 64
ABresult
1X1
011
000
136 if length($pub_key) != ED25519_PK_SZ ||
137 length($priv_key) != ED25519_SK_SZ;
138 1 my $comment = $b->get_str;
139  
140 # check padding
141 1 my $padnum = 0;
142 1 ▶ while ($b->offset < $b->length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b->offset < $b->length) 15 1 16
143 15 ▶ croak "Invalid format"
Branch: 1 branch
BranchTrueFalseTotal
if ord $b->get_char != ++$padnum 0 15 15
144 if ord($b->get_char) != ++$padnum;
145 }
146  
147 1 my $key = __PACKAGE__->new(undef);
148 1 $key->comment($comment);
149 1 $key->{pub} = $pub_key;
150 1 $key->{priv} = $priv_key;
151 1 $key;
152 }
153  
154 0 ▶sub write_private {
Subroutine write_private: not called CC=11 SCAR=47.7
Pod documented
155 0 my $key = shift;
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
BranchTrueFalseTotal
if ($passphrase) { } 0 0 0
160 0 ▶ $ciphername ||= DEFAULT_CIPHERNAME;
Condition0%0/2
$ciphername ||= "aes256-cbc"
l!l
00
MC/DC0%0/1
$ciphername ||= "aes256-cbc"
$ciphername
Truth table0%0/2
A
A= $ciphername
Aresult
11
00
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
BranchTrueFalseTotal
unless $cipher 0 0 0
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
l!l
00
MC/DC0%0/1
$rounds ||= 16
$rounds
Truth table0%0/2
A
A= $rounds
Aresult
11
00
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
BranchTrueFalseTotal
if (my $r = length($kb->bytes) % $blocksize) 0 0 0
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
BranchTrueFalseTotal
$cipher ? : 0 0 0
216 0 ▶ $tag = substr($bytes,-$authlen,$authlen,'') if $authlen;
Branch: 1 branch
BranchTrueFalseTotal
if $authlen 0 0 0
217 0 $b->put_str($bytes);
218 0 ▶ $b->put_chars($tag) if $tag;
Branch: 1 branch
BranchTrueFalseTotal
if $tag 0 0 0
219  
220 0 local *FH;
221 0 ▶ open FH, ">$key_file" or die "Cannot write key file";
Branch: 1 branch
BranchTrueFalseTotal
open FH, ">$key_file" or die "Cannot write key file" 0 0 0
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 1 ▶sub sign {
Subroutine sign: called CC=1 SCAR=0.0
Pod documented
229 1 my $key = shift;
230 1 my $data = shift;
231 1 my $sig = ed25519_sign_message($data, $key->{priv});
232  
233 1 my $b = Net::SSH::Perl::Buffer->new( MP => 'SSH2' );
234 1 $b->put_str($key->ssh_name);
235 1 $b->put_str($sig);
236 1 $b->bytes;
237 }
238  
239 1 ▶sub verify {
Subroutine verify: called CC=3 SCAR=11.1
Pod documented
240 1 my $key = shift;
241 1 my($signature, $data) = @_;
242 1 my $sigblob;
243  
244 1 my $b = Net::SSH::Perl::Buffer->new( MP => 'SSH2' );
245 1 $b->append($signature);
246 1 my $ktype = $b->get_str;
247 1 ▶ croak "Can't verify type ", $ktype unless $ktype eq $key->ssh_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $ktype eq $key->ssh_name 0 1 1
248 1 $sigblob = $b->get_str;
249 1 ▶ croak "Invalid format" unless length($sigblob) == 64;
Branch: 1 branch
BranchTrueFalseTotal
unless length $sigblob == 64 0 1 1
250  
251 1 ed25519_verify_message($data,$key->{pub},$sigblob);
252 }
253  
254 0 ▶sub equal {
Subroutine equal: not called CC=3 SCAR=24.8
Pod documented
255 0 my($keyA, $keyB) = @_;
256 $keyA->{pub} && $keyB->{pub} &&
257 0 ▶ $keyA->{pub} eq $keyB->{pub};
Branch: 1 branch
BranchTrueFalseTotal
$keyA->{"pub"} and $keyB->{"pub"} and $keyA->{"pub"} eq $keyB->{"pub"} 0 0 0
Condition0%0/3
$keyA->{"pub"} and $keyB->{"pub"}
!ll&&!rl&&r
000
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"}
ABresult
0X0
100
111
258 }
259  
260 0 ▶sub as_blob {
Subroutine as_blob: not called CC=1 SCAR=6.9
Pod documented
261 0 my $key = shift;
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
Pod undocumented
269  
270 32 ▶sub bcrypt_hash {
Subroutine bcrypt_hash: called CC=2 SCAR=6.9
Pod undocumented
271 32 my ($sha2pass, $sha2salt) = @_;
272 32 my $ciphertext = 'OxychromaticBlowfishSwatDynamite';
273  
274 32 my $ctx = bf_init();
275 32 bf_expandstate($ctx,$sha2salt,$sha2pass);
276 32 ▶ for (my $i=0; $i<64; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 64) 2048 32 2080
277 2048 bf_expand0state($ctx,$sha2salt);
278 2048 bf_expand0state($ctx,$sha2pass);
279 }
280 # iterate 64 times
281 32 bf_encrypt_iterate($ctx,$ciphertext,64);
282 }
283  
284 1 ▶sub bcrypt_pbkdf {
Subroutine bcrypt_pbkdf: called CC=9 SCAR=22.1
Pod undocumented
285 1 my ($pass, $salt, $keylen, $rounds) = @_;
286 1 my $out;
287 1 ▶ use constant BCRYPT_HASHSIZE => 32;
Subroutine BEGIN: called CC=1 SCAR=0.0
288 1 my $key = "\0" x $keylen;
289 1 my $origkeylen = $keylen;
290  
291 1 ▶ return if $rounds < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $rounds < 1 0 1 1
292 1 ▶ return unless $pass && $salt;
Branch: 1 branch
BranchTrueFalseTotal
unless $pass and $salt 0 1 1
Condition33%1/3
$pass and $salt
!ll&&!rl&&r
001
MC/DC0%0/2
$pass and $salt
$pass $salt
Truth table33%1/3
A and B
A= $passB= $salt
ABresult
0X0
100
111
293  
294 1 my $stride = int(($keylen + BCRYPT_HASHSIZE - 1) / BCRYPT_HASHSIZE);
295 1 my $amt = int(($keylen + $stride - 1) / $stride);
296  
297 1 my $sha2pass = sha512($pass);
298  
299 1 ▶ for (my $count = 1; $keylen > 1; $count++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keylen > 1) 2 1 3
300 2 my $countsalt = pack('N',$count & 0xffffffff);
301 # first round, salt is salt
302 2 my $sha2salt = sha512($salt . $countsalt);
303  
304 2 my $tmpout = $out = bcrypt_hash($sha2pass, $sha2salt);
305  
306 2 ▶ for (my $i=1; $i < $rounds; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rounds) 30 2 32
307 # subsequent rounds, salt is previous output
308 30 $sha2salt = sha512($tmpout);
309 30 $tmpout = bcrypt_hash($sha2pass,$sha2salt);
310 30 $out ^= $tmpout;
311 }
312  
313 # pbkdf2 deviation: output the key material non-linearly.
314 2 ▶ $amt = $amt<$keylen ? $amt : $keylen;
Branch: 1 branch
BranchTrueFalseTotal
$amt < $keylen ? : 1 1 2
315 2 my $i;
316 2 ▶ for ($i=0; $i<$amt; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $amt) 48 2 50
317 48 my $dest = $i * $stride + ($count - 1);
318 48 ▶ last if $dest >= $origkeylen;
Branch: 1 branch
BranchTrueFalseTotal
if $dest >= $origkeylen 0 48 48
319 48 substr($key,$dest,1,substr($out,$i,1));
320 }
321 2 $keylen -= $i;
322 }
323 1 return $key;
324 }
325  
326 1 1;
327 __END__
« blib/lib/Net/SSH/Perl/Auth/RSA.pm ↑ Summary blib/lib/Net/SSH/Perl/Subsystem/Server.pm »