bin/pminisign

filter: Statementstmt 16.8% 57 / 339 Branchbran 0.5% 1 / 174 Conditioncond 0.0% 0 / 78 MC/DCmcdc 0.0% 0 / 44 Subroutinesub 42.8% 15 / 35 Podpod n/a totaltotal 10.9% 73 / 670 CC 116 SCAR 55.6
CC
116
Cov
10%
main
812.0
verify
342.0
generate
306.0
CRAP 259.2
↑ Summary bin/csxs-ppcrypt »
1 #!/usr/bin/env perl
2 1 our $VERSION = '0.000001';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Getopt::Long qw/:config bundling/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Crypt::Sodium::XS::Base64 ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Crypt::Sodium::XS::MemVault ":constructors";
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Crypt::Sodium::XS::ProtMem "PROTMEM_ALL_DISABLED";
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Crypt::Sodium::XS::Util 'sodium_random_bytes';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Crypt::Sodium::XS::generichash ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Crypt::Sodium::XS::pwhash ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Crypt::Sodium::XS::sign ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use File::Basename "basename";
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # this code is structured to resemble minisign from which it derives, for ease
20 # of comparison.
21  
22 # sk format
23 # untrusted comment, arbitrary length
24 # "\n"
25 # base64url {
26 # sig_alg ------- a2
27 # kdf_alg ------- a2
28 # chk_alg ------- a2
29 # kdf_salt ------ a#saltbytes (depends on kdf_alg)
30 # kdf_opslimit -- Q< (8)
31 # kdf_memlimit -- Q< (8)
32 # key_num ------- a8 (LE integer)
33 # sk ------------ a#skbytes (depends on sig_alg)
34 # chk ----------- a#chkbytes (depends on chk_alg)
35 # }
36 # "\n"
37  
38 # pk format
39 # base64url {
40 # sig_alg -------- a2
41 # key_num -------- a8 (LE integer)
42 # pk ------------- a#pkbytes (depends on sig_alg)
43 # }
44 # "\n"
45  
46 # sig format
47 # untrusted comment, arbitrary length
48 # "\n"
49 # base64url {
50 # sig_alg -------- a2
51 # key_num -------- a8 (LE integer)
52 # sig ------------ a#signbytes (depends on sig_alg)
53 # }
54 # "\n"
55 # trusted comment, arbitrary length
56 # "\n"
57 # base64url {
58 # complete_sig, aribitrary length
59 # }
60 # "\n"
61  
62 # minisign clearly intended to support multiple algorithms but doesn't.
63 # 'Ed' is referred to as "legacy" (non-pre-hashed) and 'ED' is pre-hashed.
64 # now i'm confused. looking back, 'ED' is "legacy" (hashed) and 'Ed' is neither.
65 1 my %sig_algs = (
66 Ed => {
67 id => 'Ed',
68 name => 'ed25519',
69 bytes => sign_ed25519_BYTES,
70 pkbytes => sign_ed25519_PUBLICKEYBYTES,
71 skbytes => sign_ed25519_SECRETKEYBYTES,
72 keypair_gen => \&sign_ed25519_keypair,
73 sign => \&sign_ed25519_detached,
74 verify => \&sign_ed25519_verify,
75 },
76 ED => {
77 id => 'ED',
78 name => 'ed25519 pre-hashed',
79 bytes => sign_ed25519_BYTES,
80 pkbytes => sign_ed25519_PUBLICKEYBYTES,
81 skbytes => sign_ed25519_SECRETKEYBYTES,
82 hash_init => \&generichash_blake2b_init,
83 hashbytes => generichash_blake2b_BYTES_MAX,
84 keypair_gen => \&sign_ed25519_keypair,
85 sign => \&sign_ed25519_detached,
86 verify => \&sign_ed25519_verify,
87 },
88 );
89 1 my %kdf_algs = (
90 Sc => {
91 id => 'Sc',
92 name => 'scrypt',
93 saltbytes => pwhash_scryptsalsa208sha256_SALTBYTES,
94 opslimit_default => pwhash_scryptsalsa208sha256_OPSLIMIT_SENSITIVE,
95 memlimit_default => pwhash_scryptsalsa208sha256_MEMLIMIT_SENSITIVE,
96 salt_gen => \&pwhash_scryptsalsa208sha256_salt,
97 kdf_gen => \&pwhash_scryptsalsa208sha256,
98 },
99 # not supported upstream
100 # hopefully does not clash with later upstream choice
101 XA => {
102 id => 'XA',
103 name => 'argon2id',
104 saltbytes => pwhash_argon2id_SALTBYTES,
105 opslimit_default => pwhash_argon2id_OPSLIMIT_SENSITIVE,
106 memlimit_default => pwhash_argon2id_MEMLIMIT_SENSITIVE,
107 salt_gen => \&pwhash_argon2id_salt,
108 kdf_gen => \&pwhash_argon2id,
109 }
110 );
111 1 my %chk_algs = (
112 B2 => {
113 id => 'B2',
114 name => 'blake2b',
115 bytes => generichash_blake2b_BYTES,
116 init => \&generichash_blake2b_init,
117 },
118 );
119  
120 1 my $sig_alg_default = "Ed";
121 1 my $sig_alg_hashed_default = "ED";
122 1 my $kdf_alg_default = "XA";
123 1 my $chk_alg_default = "B2";
124 1 my $comment_prefix = "untrusted comment: ";
125 1 my $default_comment = "signature from minisign secret key";
126 1 my $sk_default_comment = "pminisign encrypted secret key";
127 1 my $trusted_comment_prefix = "trusted comment: ";
128 # other stuff is just hardcoded.
129  
130 0 ▶sub usage {
Subroutine usage: not called CC=3 SCAR=24.8
131 0 ▶ print STDERR "@_\n" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
132 0 print STDERR <<'EOUSAGE';
133 usage:
134 pminisign -G [-f] [-p pubkeyfile] [-s seckeyfile]
135 pminisign -S [-l] [-x sigfile] [-s seckeyfile] [-c untrusted_comment] \
136 [-t trusted_comment] -m file
137 pminisign -V [-H] [-x sigfile] [-p pubkeyfile | -P pubkey] [-o] [-q] -m file
138 pminisign -R -s seckeyfile -p pubkeyfile
139  
140 actions:
141 -G generate a new key pair
142 -S sign files
143 -V verify that a signature is valid for a given file
144 -R recreate a public key file from a secret key file
145 options:
146 -H require input to be prehashed
147 -l sign using the legacy format
148 -m <file> file to sign/verify
149 -o output the file content after verification
150 -p <pubkeyfile> public key file (default: ./minisign.pub)
151 -P <pubkey> public key, as a base64 string
152 -s <seckey> secret key file (default: ~/.minisign/minisign.key)
153 -x <sigfile> signature file (default: <file>.minisig)
154 -c <comment> add a one-line untrusted comment
155 -t <comment> add a one-line trusted comment
156 -q quiet mode, suppress output
157 -Q pretty quiet mode, only print the trusted comment
158 -f force. Combined with -G, overwrite a previous key pair
159 -v display version number
160 EOUSAGE
161 0 ▶ exit 1 if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
162 0 exit 0;
163 }
164  
165 0 ▶sub config_dir {
Subroutine config_dir: not called CC=4 SCAR=30.0
166 return ($ENV{MINISIGN_CONFIG_DIR}
167 0 ▶ or $ENV{HOME} && (File::Spec->catdir($ENV{HOME}, ".minisign"))
Condition0%0/8
$ENV{'MINISIGN_CONFIG_DIR'} || $ENV{'HOME'} && 'File::Spec'->catdir($ENV{'HOME'}, '.minisign') || undef
l!l
00
$ENV{"MINISIGN_CONFIG_DIR"} or $ENV{"HOME"} and "File::Spec"->catdir($ENV{"HOME"}, ".minisign")
l!l&&r!l&&!r
000
$ENV{"HOME"} and "File::Spec"->catdir($ENV{"HOME"}, ".minisign")
!ll&&!rl&&r
000
MC/DC0%0/3
$ENV{'MINISIGN_CONFIG_DIR'} || $ENV{'HOME'} && 'File::Spec'->catdir($ENV{'HOME'}, '.minisign') || undef
$ENV{"MINISIGN_CONFIG_DIR"} $ENV{"HOME"} "File::Spec"->catdir($ENV{"HOME"}, ".minisign")
Truth table0%0/4
A or B and C
A= $ENV{"MINISIGN_CONFIG_DIR"}B= $ENV{"HOME"}C= "File::Spec"->catdir($ENV{"HOME"}, ".minisign")
ABCresult
1XX1
0111
00X0
0100
168 or undef);
169 }
170  
171 0 ▶sub default_pk_path {
Subroutine default_pk_path: not called CC=2 SCAR=17.9
172 0 ▶ return File::Spec->catfile(config_dir() // (), "minisign.pub");
Condition0%0/3
config_dir() // ()
l!l&&r!l&&!r
000
MC/DC0%0/2
config_dir() // ()
config_dir() ()
Truth table0%0/3
A // B
A= config_dir()B= ()
ABresult
1X1
011
000
173 }
174  
175 0 ▶sub default_sk_path {
Subroutine default_sk_path: not called CC=2 SCAR=17.9
176 0 ▶ return File::Spec->catfile(config_dir() // (), "minisign.key");
Condition0%0/3
config_dir() // ()
l!l&&r!l&&!r
000
MC/DC0%0/2
config_dir() // ()
config_dir() ()
Truth table0%0/3
A // B
A= config_dir()B= ()
ABresult
1X1
011
000
177 }
178  
179 0 ▶sub pack_sig_data($sig_data) {
Subroutine pack_sig_data: not called CC=1 SCAR=6.9
180 0 my $sig_alg = $sig_algs{$sig_data->{sig_alg}};
181 0 my $sig_fmt = "a2a8a$sig_alg->{bytes}";
182 0 return pack($sig_fmt, @$sig_data{qw/sig_alg key_num sig/});
183 }
184  
185 0 ▶sub pack_pk_data($pk_data) {
Subroutine pack_pk_data: not called CC=1 SCAR=6.9
186 0 my $sig_alg = $sig_algs{$pk_data->{sig_alg}};
187 0 my $pk_fmt = "a2a8a$sig_alg->{pkbytes}";
188 0 return pack($pk_fmt, @$pk_data{qw/sig_alg key_num pk/});
189 }
190  
191 0 ▶sub pack_sk_data($sk_data) {
Subroutine pack_sk_data: not called CC=1 SCAR=6.9
192 0 my $sig_alg = $sig_algs{$sk_data->{sig_alg}};
193 0 my $kdf_alg = $kdf_algs{$sk_data->{kdf_alg}};
194 0 my $chk_alg = $chk_algs{$sk_data->{chk_alg}};
195 0 my $header_fmt = "a2a2a2a$kdf_alg->{saltbytes}Q<Q<a8";
196 0 my @header_keys = qw/sig_alg kdf_alg chk_alg salt opslimit memlimit key_num/;
197 0 my $header_str = pack($header_fmt, @$sk_data{@header_keys});
198 0 return $header_str . $sk_data->{sk} . pack("a$chk_alg->{bytes}", $sk_data->{chk});
199 }
200  
201 0 ▶sub unpack_sig_data($sig_blob) {
Subroutine unpack_sig_data: not called CC=2 SCAR=17.9
202 0 my $sig_data = {};
203 0 $sig_data->{sig_alg} = unpack("a2", $sig_blob);
204 my $sig_alg = $sig_algs{$sig_data->{sig_alg}}
205 0 ▶ // die "unsupported signature algorithm '$sig_data->{sig_alg}'";
Condition0%0/2
$sig_algs{$sig_data->{"sig_alg"}} // die(qq[unsupported signature algorithm '$sig_data->{"sig_alg"}'])
l!l
00
MC/DC0%0/1
$sig_algs{$sig_data->{"sig_alg"}} // die(qq[unsupported signature algorithm '$sig_data->{"sig_alg"}'])
$sig_algs{$sig_data->{"sig_alg"}}
Truth table0%0/2
A
A= $sig_algs{$sig_data->{"sig_alg"}}
Aresult
11
00
206 0 my $fmt = "a2a8a$sig_alg->{bytes}"; # a2 ignores algorithm, parsed above
207 0 (undef, @$sig_data{qw/key_num sig/}) = unpack($fmt, $sig_blob);
208 0 return $sig_data;
209 }
210  
211 0 ▶sub unpack_pk_data($pk_blob) {
Subroutine unpack_pk_data: not called CC=4 SCAR=30.0
212 0 my $pk_data = {};
213 0 ▶ die "invalid public key blob (too short)" if length($pk_blob) < 1;
Branch: 1 branch
BranchTrueFalseTotal
if length $pk_blob < 1 0 0 0
214 0 $pk_data->{sig_alg} = unpack("a2", $pk_blob);
215 0 my $sig_alg = $sig_algs{$pk_data->{sig_alg}};
216 0 ▶ die "unsupported signature algorithm '$pk_data->{sig_alg}'" unless $sig_alg;
Branch: 1 branch
BranchTrueFalseTotal
unless $sig_alg 0 0 0
217 # length matches unpack format below
218 0 ▶ die "invalid public key blob (too short)" if length($pk_blob) < 2 + 8 + $sig_alg->{pkbytes};
Branch: 1 branch
BranchTrueFalseTotal
if length $pk_blob < 10 + $sig_alg->{"pkbytes"} 0 0 0
219 # bleh, perl has to be 64-bit int here. :(
220 0 my $fmt = "a2a8a$sig_alg->{pkbytes}"; # a2 ignores algorithm, parsed above
221 0 (undef, @$pk_data{qw/key_num pk/}) = unpack($fmt, $pk_blob);
222 0 return $pk_data;
223 }
224  
225 # NB: $sk_blob must be a MemVault
226 0 ▶sub unpack_sk_data($sk_blob, $opts) {
Subroutine unpack_sk_data: not called CC=11 SCAR=48.8
227 0 my $sk_data = {};
228  
229 0 ▶ die "invalid secret key blob (too short: ".$sk_blob->size.")" if $sk_blob->size < 6;
Branch: 1 branch
BranchTrueFalseTotal
if $sk_blob->size < 6 0 0 0
230 0 @$sk_data{qw/sig_alg kdf_alg chk_alg/}
231 = unpack("a2a2a2", $sk_blob->extract(0, 6, PROTMEM_ALL_DISABLED));
232 my $sig_alg = $sig_algs{$sk_data->{sig_alg}}
233 0 ▶ // die "unsupported signature algorithm '$sk_data->{sig_alg}'";
Condition0%0/2
$sig_algs{$sk_data->{"sig_alg"}} // die(qq[unsupported signature algorithm '$sk_data->{"sig_alg"}'])
l!l
00
MC/DC0%0/1
$sig_algs{$sk_data->{"sig_alg"}} // die(qq[unsupported signature algorithm '$sk_data->{"sig_alg"}'])
$sig_algs{$sk_data->{"sig_alg"}}
Truth table0%0/2
A
A= $sig_algs{$sk_data->{"sig_alg"}}
Aresult
11
00
234 my $kdf_alg = $kdf_algs{$sk_data->{kdf_alg}}
235 0 ▶ // die "unsupported key derivation algorithm '$sk_data->{kdf_alg}'";
Condition0%0/2
$kdf_algs{$sk_data->{"kdf_alg"}} // die(qq[unsupported key derivation algorithm '$sk_data->{"kdf_alg"}'])
l!l
00
MC/DC0%0/1
$kdf_algs{$sk_data->{"kdf_alg"}} // die(qq[unsupported key derivation algorithm '$sk_data->{"kdf_alg"}'])
$kdf_algs{$sk_data->{"kdf_alg"}}
Truth table0%0/2
A
A= $kdf_algs{$sk_data->{"kdf_alg"}}
Aresult
11
00
236 my $chk_alg = $chk_algs{$sk_data->{chk_alg}}
237 0 ▶ // die "unsupported hash algorithm '$sk_data->{chk_alg}'";
Condition0%0/2
$chk_algs{$sk_data->{"chk_alg"}} // die(qq[unsupported hash algorithm '$sk_data->{"chk_alg"}'])
l!l
00
MC/DC0%0/1
$chk_algs{$sk_data->{"chk_alg"}} // die(qq[unsupported hash algorithm '$sk_data->{"chk_alg"}'])
$chk_algs{$sk_data->{"chk_alg"}}
Truth table0%0/2
A
A= $chk_algs{$sk_data->{"chk_alg"}}
Aresult
11
00
238  
239 0 my $salt_len = $kdf_alg->{saltbytes};
240 0 my $sk_len = $sig_alg->{skbytes};
241 0 my $chk_len = $chk_alg->{bytes};
242 # bleh, perl has to be 64-bit int here for 'Q'. :(
243 0 my $header_fmt = "a6a${salt_len}Q<Q<"; # a6 ignores algorithms, parsed above
244 0 my $header_chunk_len = 6 + $salt_len + 8 + 8;
245 0 my $key_fmt = "a8a${sk_len}a${chk_len}";
246 0 my $key_len = 8 + $sk_len + $chk_len;
247 0 my $total_len = $header_chunk_len + $key_len;
248 0 ▶ die "invalid secret key blob (too short)" if $sk_blob->size < $total_len;
Branch: 1 branch
BranchTrueFalseTotal
if $sk_blob->size < $total_len 0 0 0
249  
250 0 my $header_chunk = $sk_blob->extract(0, $header_chunk_len, PROTMEM_ALL_DISABLED);
251 0 my $key_chunk = $sk_blob->extract($header_chunk_len);
252 0 undef $sk_blob;
253 0 (undef, @$sk_data{qw/salt opslimit memlimit/})
254 = unpack($header_fmt, $header_chunk);
255 die "unsupported key derivation algorithm '$sk_data->{kdf_alg}'"
256 0 ▶ unless exists $kdf_algs{$sk_data->{kdf_alg}};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $kdf_algs{$sk_data->{"kdf_alg"}} 0 0 0
257 die "unsupported signature algorithm '$sk_data->{sig_alg}'"
258 0 ▶ unless exists $sig_algs{$sk_data->{sig_alg}};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $sig_algs{$sk_data->{"sig_alg"}} 0 0 0
259 die "unsupported checksum algorithm '$sk_data->{chk_alg}'"
260 0 ▶ unless exists $chk_algs{$sk_data->{chk_alg}};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $chk_algs{$sk_data->{"chk_alg"}} 0 0 0
261  
262 0 $sk_data->{key_num} = $key_chunk->extract(0, 8, PROTMEM_ALL_DISABLED);
263 0 $sk_data->{sk} = $key_chunk->extract(8, $sk_len);
264 0 print "deriving a key from the password and decrypting the secret key...\n";
265 0 STDOUT->flush;
266 0 my $passwd = mv_from_tty(undef, "Passphrase: ");
267 0 ▶ $passwd = $passwd->concat("\n") if $ENV{PMINISIGN_COMPATIBLE_SECRETKEY};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"PMINISIGN_COMPATIBLE_SECRETKEY"} 0 0 0
268 $passwd = $kdf_alg->{kdf_gen}->($passwd, $sk_data->{salt}, $sig_alg->{skbytes},
269 0 @$sk_data{qw/opslimit memlimit/});
270 0 $sk_data->{sk}->bitwise_xor_equals($passwd);
271 0 $sk_data->{chk} = $key_chunk->extract(8 + $sk_len, undef, PROTMEM_ALL_DISABLED);
272 0 ▶ die "Invalid secret key (wrong passphrase?)" if $sk_data->{chk} ne chk_sk($sk_data);
Branch: 1 branch
BranchTrueFalseTotal
if $sk_data->{"chk"} ne chk_sk($sk_data) 0 0 0
273 0 return $sk_data;
274 }
275  
276 0 ▶sub load_sig($opts) {
Subroutine load_sig: not called CC=8 SCAR=42.8
277 0 my $path = $opts->{sig_path};
278 0 ▶ open(my $fh, "<:raw", $path) or die "$path: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $path or die "${path}: $!" 0 0 0
279 0 my $comment = readline($fh) =~ s/[\r\n]+\z//r;
280 0 ▶ die "$path: invalid sig file (missing comment)\n" unless $comment;
Branch: 1 branch
BranchTrueFalseTotal
unless $comment 0 0 0
281 0 ▶ die "$path: invalid sig file (comment should start '$comment_prefix')\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $comment =~ s/\A\Q$comment_prefix\E//u 0 0 0
282 unless $comment =~ s/\A\Q$comment_prefix//;
283 0 my $sig_blob = readline($fh);
284 0 ▶ die "$path: invalid sig file (missing sig data)\n" unless $sig_blob;
Branch: 1 branch
BranchTrueFalseTotal
unless $sig_blob 0 0 0
285 0 my $trusted_comment = readline($fh);
286 0 my $full_sig = "";
287 0 ▶ if ($trusted_comment) {
Branch: 2 branches
BranchTrueFalseTotal
if ($trusted_comment) { } 0 0 0
elsif (not $ENV{'PMINISIGN_NO_TRUSTED_COMMENT'}) { } 0 0 0
288 0 $trusted_comment =~ s/[\r\n]+\z//;
289 0 ▶ die "$path: invalid sig file (trusted comment should start '$trusted_comment_prefix')\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $trusted_comment =~ s/\A\Q$trusted_comment_prefix\E//u 0 0 0
290 unless $trusted_comment =~ s/\A\Q$trusted_comment_prefix//;
291 0 $full_sig = readline($fh) =~ s/[\r\n]+\z//r;
292 }
293 elsif (!$ENV{PMINISIGN_NO_TRUSTED_COMMENT}) {
294 0 die "$path: invalid sig file (missing trusted comment)\n";
295 }
296 0 my $sig_data = unpack_sig_data(sodium_base642bin($sig_blob, BASE64_VARIANT_ORIGINAL));
297 0 $sig_data->{comment} = $comment;
298 0 $sig_data->{trusted_comment} = $trusted_comment;
299 0 $sig_data->{full_sig} = sodium_base642bin($full_sig, BASE64_VARIANT_ORIGINAL);
300 0 return $sig_data;
301 }
302  
303 0 ▶sub load_pk($path) {
Subroutine load_pk: not called CC=5 SCAR=34.0
304 0 ▶ open(my $fh, "<:raw", $path) or die "$path: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $path or die "${path}: $!" 0 0 0
305 0 my $comment = readline($fh) =~ s/[\r\n]+\z//r;
306 0 ▶ die "$path: invalid public key file (missing comment)\n" unless $comment;
Branch: 1 branch
BranchTrueFalseTotal
unless $comment 0 0 0
307 0 ▶ die "$path: invalid public key file (comment should start '$comment_prefix')\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $comment =~ s/\A\Q$comment_prefix\E//u 0 0 0
308 unless $comment =~ s/\A\Q$comment_prefix//;
309 0 my $pk_blob;
310 {
311 0 local $/ = undef;
312 0 ▶ defined($pk_blob = readline($fh)) or die "$path: $!";
Branch: 1 branch
BranchTrueFalseTotal
defined($pk_blob = readline $fh) or die "${path}: $!" 0 0 0
313 }
314 0 return unpack_pk_data(sodium_base642bin($pk_blob, BASE64_VARIANT_ORIGINAL));
315 }
316  
317 0 ▶sub load_sk($opts) {
Subroutine load_sk: not called CC=5 SCAR=34.0
318 0 my $path = $opts->{sk_path};
319 0 ▶ open(my $fh, "<:raw", $path) or die "$path: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $path or die "${path}: $!" 0 0 0
320 0 my $comment = readline($fh);
321 0 ▶ die "$path: invalid public key file (comment should start '$comment_prefix')\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $comment and $comment =~ s/\A\Q$comment_prefix\E//u 0 0 0
Condition0%0/3
$comment and $comment =~ s/\A\Q$comment_prefix\E//u
!ll&&!rl&&r
000
MC/DC0%0/2
$comment and $comment =~ s/\A\Q$comment_prefix\E//u
$comment $comment =~ s/\A\Q$comment_prefix\E//u
Truth table0%0/3
A and B
A= $commentB= $comment =~ s/\A\Q$comment_prefix\E//u
ABresult
0X0
100
111
322 unless $comment and $comment =~ s/\A\Q$comment_prefix//;
323 0 my $sk_blob = readline($fh);
324 0 ▶ die "$path: missing secret key line" unless $sk_blob;
Branch: 1 branch
BranchTrueFalseTotal
unless $sk_blob 0 0 0
325 0 close($fh);
326 0 return unpack_sk_data(mv_from_base64($sk_blob, BASE64_VARIANT_ORIGINAL), $opts);
327 }
328  
329 0 ▶sub load_msg { # no sub signature; uses alias to first arg
Subroutine load_msg: not called CC=4 SCAR=30.0
330 0 my (undef, $path, $hasher) = @_;
331 0 ▶ open(my $fh, "<:raw", $path) or die "$path: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $path or die "${path}: $!" 0 0 0
332 # eh, no read error handling...
333 0 ▶ if ($hasher) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hasher) { } 0 0 0
334 # 8k buf size chosen arbitrarily
335 0 ▶ while (read($fh, my $buf, 8192)) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $fh, my $buf, 8192) 0 0 0
336 0 $hasher->update($buf);
337 }
338 0 $_[0] = $hasher->final;
339 }
340 else {
341 # yikes. same as minisign, slurp it up.
342 0 local $/ = undef;
343 0 $_[0] = readline($fh);
344 }
345 }
346  
347 0 ▶sub chk_sk($sk_data) {
Subroutine chk_sk: not called CC=1 SCAR=6.9
348 0 my $chk_alg = $chk_algs{$sk_data->{chk_alg}};
349 0 my $hash = $chk_alg->{init}->($chk_alg->{bytes});
350 0 $hash->update($sk_data->{sig_alg});
351 0 $hash->update($sk_data->{key_num});
352 0 $hash->update($sk_data->{sk});
353 0 return $hash->final;
354 }
355  
356 0 ▶sub generate($opts) {
Subroutine generate: not called CC=17 SCAR=57.2
357 0 my $pk_path = $opts->{pk_path};
358 0 my $sk_path = $opts->{sk_path};
359 0 my $comment = 'untrusted comment: minisign public key';
360 0 ▶ if ((-e $sk_path or $pk_path && -e $pk_path) and !$opts->{force}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $sk_path || $pk_path && -e $pk_path and not $opts->{"force"}) 0 0 0
Condition0%0/9
-e $sk_path || $pk_path && -e $pk_path and not $opts->{"force"}
!ll&&!rl&&r
000
-e $sk_path or $pk_path and -e $pk_path
l!l&&r!l&&!r
000
$pk_path and -e $pk_path
!ll&&!rl&&r
000
MC/DC0%0/4
-e $sk_path || $pk_path && -e $pk_path and not $opts->{"force"}
-e $sk_path $pk_path -e $pk_path not $opts->{"force"}
Truth table0%0/6
A or B and C and not(D)
A= -e $sk_pathB= $pk_pathC= -e $pk_pathD= not $opts->{"force"}
ABCDresult
00XX0
010X0
1XX00
01100
1XX11
01111
361 0 ▶ my $path = -e $sk_path ? $sk_path : $pk_path;
Branch: 1 branch
BranchTrueFalseTotal
-e $sk_path ? : 0 0 0
362 0 die <<EODIE
363 Key generation aborted:
364 $path already exists.
365  
366 If you really want to overwrite the existing key pair, add the -f switch to
367 force this operation.
368 EODIE
369 }
370 0 $comment =~ tr/\r\n/ /s;
371 0 my $sig_alg = $sig_algs{$sig_alg_hashed_default};
372 0 ▶ my $kdf_alg = $kdf_algs{$ENV{PMINISIGN_COMPATIBLE_SECRETKEY} ? 'Sc' : $kdf_alg_default};
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'PMINISIGN_COMPATIBLE_SECRETKEY'} ? : 0 0 0
373 0 my $chk_alg = $chk_algs{$chk_alg_default};
374 0 my $key_num = sodium_random_bytes(8);
375 0 my ($pk, $sk) = $sig_alg->{keypair_gen}->();
376 0 my $pk_data = {
377 sig_alg => $sig_alg_default,
378 key_num => $key_num,
379 pk => $pk,
380 };
381 my $sk_data = {
382 sig_alg => $sig_alg_default,
383 kdf_alg => $kdf_alg->{id},
384 0 chk_alg => $chk_alg_default,
385 key_num => $key_num,
386 sk => $sk,
387 };
388 0 $sk_data->{chk} = chk_sk($sk_data);
389 0 print "Please enter a passphrase to protect the secret key.\n";
390 0 STDOUT->flush;
391 0 my $pw = mv_from_tty(undef, "Passphrase: ");
392 0 my $pw_chk = mv_from_tty(undef, "Passphrase (one more time): ");
393 0 ▶ unless ($pw->size == $pw_chk->size and $pw eq $pw_chk) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pw->size == $pw_chk->size and $pw eq $pw_chk) 0 0 0
Condition0%0/3
$pw->size == $pw_chk->size and $pw eq $pw_chk
!ll&&!rl&&r
000
MC/DC0%0/2
$pw->size == $pw_chk->size and $pw eq $pw_chk
$pw->size == $pw_chk->size $pw eq $pw_chk
Truth table0%0/3
A and B
A= $pw->size == $pw_chk->sizeB= $pw eq $pw_chk
ABresult
0X0
100
111
394 0 die "Passphrases don't match\n";
395 }
396 0 ▶ $pw = $pw->concat("\n") if $ENV{PMINISIGN_COMPATIBLE_SECRETKEY};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"PMINISIGN_COMPATIBLE_SECRETKEY"} 0 0 0
397 0 print "Deriving a key from the password in order to encrypt to secret key...\n";
398 0 print "(this may take a few moments)...\n";
399 0 my $pw_salt = $sk_data->{salt} = $kdf_alg->{salt_gen}->();
400 0 my $pw_hash;
401 0 my $opslimit = $kdf_alg->{opslimit_default};
402 0 my $memlimit = $kdf_alg->{memlimit_default};
403 0 $pw_hash = $kdf_alg->{kdf_gen}->($pw, $pw_salt, $sig_alg->{skbytes}, $opslimit, $memlimit);
404 0 $sk_data->{sk}->bitwise_xor_equals($pw_hash);
405 # supposed to /= 2 opslimit and memlimit down to minimum til it works.
406 0 $sk_data->{opslimit} = $opslimit;
407 0 $sk_data->{memlimit} = $memlimit;
408 0 my $sk_packed = pack_sk_data($sk_data);
409 0 ▶ sysopen(my $fh, $sk_path, O_WRONLY|O_NOCTTY|O_CREAT|O_TRUNC, 0600)
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $fh, $sk_path, 833, 384 or die "${sk_path}: $!" 0 0 0
410 or die "$sk_path: $!";
411 0 print $fh "$comment_prefix$comment\n";
412 0 $fh->flush;
413 0 $sk_packed->to_base64(BASE64_VARIANT_ORIGINAL)->to_fd(fileno($fh));
414 0 ▶ print $fh "\n" or die "$sk_path: $!";
Branch: 1 branch
BranchTrueFalseTotal
print $fh "\n" or die "${sk_path}: $!" 0 0 0
415 0 ▶ close($fh) or die "$sk_path: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "${sk_path}: $!" 0 0 0
416 0 ▶ open($fh, ">:raw", $pk_path) or die "$pk_path: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">:raw", $pk_path or die "${pk_path}: $!" 0 0 0
417 0 ▶ print $fh "$comment_prefix$comment\n" or die "$pk_path: $!";
Branch: 1 branch
BranchTrueFalseTotal
print $fh "$comment_prefix$comment\n" or die "${pk_path}: $!" 0 0 0
418 0 ▶ print $fh sodium_bin2base64(pack_pk_data($pk_data), BASE64_VARIANT_ORIGINAL)."\n" or die "$pk_path: $!";
Branch: 1 branch
BranchTrueFalseTotal
print $fh &sodium_bin2base64(scalar pack_pk_data($pk_data), 1) . "\n" or die "${pk_path}: $!" 0 0 0
419 0 ▶ close($fh) or die "$pk_path: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "${pk_path}: $!" 0 0 0
420 0 print "The secret key was saved as '$sk_path' - Keep it secret!\n";
421 0 print "The public key was saved as '$pk_path' - That one can be public.\n";
422 0 print "Files signed using this key pair can be verified with the following command:\n";
423 0 my $b64 = sodium_bin2base64(pack_pk_data($pk_data), BASE64_VARIANT_ORIGINAL);
424 0 print "pminisign -Vm <file> -P $b64\n";
425 }
426  
427 0 ▶sub recreate($opts) {
Subroutine recreate: not called CC=6 SCAR=37.4
428 0 my $pk_path = $opts->{pk_path};
429 0 my $sk_path = $opts->{sk_path};
430 0 ▶ if (-e $pk_path and !$opts->{force}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $pk_path and not $opts->{"force"}) 0 0 0
Condition0%0/3
-e $pk_path and not $opts->{"force"}
!ll&&!rl&&r
000
MC/DC0%0/2
-e $pk_path and not $opts->{"force"}
-e $pk_path not $opts->{"force"}
Truth table0%0/3
A and not(B)
A= -e $pk_pathB= not $opts->{"force"}
ABresult
0X0
100
111
431 0 die "Public key '$pk_path' already exists."
432 . "Use -f (force) if you wish to overwrite it.\n";
433 }
434 0 my $sk_data = load_sk($opts);
435 # depends on the fact secret key is really sk . pk
436 0 my $pk_len = $sig_algs{$sk_data->{sig_alg}}->{pkbytes};
437 my $pk_data = {
438 sig_alg => $sk_data->{sig_alg},
439 key_num => $sk_data->{key_num},
440 0 pk => $sk_data->{sk}->extract(-$pk_len, undef, PROTMEM_ALL_DISABLED),
441 };
442 0 ▶ open(my $fh, ">:raw", $pk_path) or die "$pk_path: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:raw", $pk_path or die "${pk_path}: $!" 0 0 0
443 0 ▶ print $fh sodium_bin2base64(pack_pk_data($pk_data), BASE64_VARIANT_ORIGINAL)."\n" or die "$pk_path: $!";
Branch: 1 branch
BranchTrueFalseTotal
print $fh &sodium_bin2base64(scalar pack_pk_data($pk_data), 1) . "\n" or die "${pk_path}: $!" 0 0 0
444 0 ▶ close($fh) or die "$pk_path: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "${pk_path}: $!" 0 0 0
445 }
446  
447 0 ▶sub signify($sk_data, $opts, $msg_path) {
Subroutine signify: not called CC=12 SCAR=50.5
448 0 ▶ my $sig_path = $opts->{sig_path} || "$msg_path.minisig";
Condition0%0/2
$opts->{'sig_path'} || "$msg_path.minisig"
l!l
00
MC/DC0%0/1
$opts->{'sig_path'} || "$msg_path.minisig"
$opts->{'sig_path'}
Truth table0%0/2
A
A= $opts->{'sig_path'}
Aresult
11
00
449 0 ▶ my $comment = $opts->{comment} // "signature from pminisign secret key";
Condition0%0/2
$opts->{"comment"} // "signature from pminisign secret key"
l!l
00
MC/DC0%0/1
$opts->{"comment"} // "signature from pminisign secret key"
$opts->{"comment"}
Truth table0%0/2
A
A= $opts->{"comment"}
Aresult
11
00
450 0 my $trusted_comment = $opts->{trusted_comment};
451 0 my $sig_alg;
452 0 ▶ if ($opts->{legacy_sign}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'legacy_sign'}) { } 0 0 0
453 0 $sig_alg = $sig_algs{'ED'};
454 }
455 else {
456 0 $sig_alg = $sig_algs{$sk_data->{sig_alg}};
457 }
458 0 $comment =~ tr/\r\n/ /s;
459 0 ▶ $comment ||= $default_comment;
Condition0%0/3
$comment ||= $default_comment
l!l&&r!l&&!r
000
MC/DC0%0/2
$comment ||= $default_comment
$comment $default_comment
Truth table0%0/3
A ||= B
A= $commentB= $default_comment
ABresult
1X1
011
000
460 0 $comment = "$comment_prefix$comment";
461 0 ▶ if (length($comment) > 1024) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $comment > 1024) 0 0 0
462 0 warn "Warning: comment too long. This breaks compatibility with signify.\n";
463 }
464 0 ▶ unless ($trusted_comment) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($trusted_comment) 0 0 0
465 $trusted_comment = "timestamp:" . time
466 . "\tfile:" . basename($msg_path)
467 0 ▶ . ($sig_alg->{hashbytes} ? "\thashed" : "");
Branch: 1 branch
BranchTrueFalseTotal
$sig_alg->{'hashbytes'} ? : 0 0 0
468 }
469 0 $trusted_comment =~ tr/\r\n/ /s;
470 0 ▶ if (length($trusted_comment) > 8192 - length($trusted_comment_prefix)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $trusted_comment > 8192 - length($trusted_comment_prefix)) 0 0 0
471 0 die "Trusted comment too long\n";
472 }
473 0 my $sig_input;
474 0 ▶ if (exists $sig_alg->{hash_init}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $sig_alg->{'hash_init'}) { } 0 0 0
475 0 load_msg($sig_input, $msg_path, $sig_alg->{hash_init}->($sig_alg->{hashbytes}));
476 }
477 else {
478 0 load_msg($sig_input, $msg_path);
479 }
480 my $sig_data = {
481 sig_alg => $sk_data->{sig_alg},
482 key_num => $sk_data->{key_num},
483 0 sig => $sig_alg->{sign}->($sig_input, $sk_data->{sk}),
484 };
485 0 ▶ open(my $fh, ">:raw", $sig_path) or die "$sig_path: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:raw", $sig_path or die "${sig_path}: $!" 0 0 0
486 0 print $fh $comment, "\n";
487 0 print $fh sodium_bin2base64(pack_sig_data($sig_data), BASE64_VARIANT_ORIGINAL), "\n";
488 0 ▶ unless ($ENV{PMINISIGN_NO_TRUSTED_COMMENT}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"PMINISIGN_NO_TRUSTED_COMMENT"}) 0 0 0
489 0 my $trusted_sig = $sig_alg->{sign}->($sig_data->{sig}."$trusted_comment", $sk_data->{sk});
490 0 print $fh "$trusted_comment_prefix$trusted_comment", "\n";
491 0 print $fh sodium_bin2base64($trusted_sig, BASE64_VARIANT_ORIGINAL), "\n";
492 }
493 }
494  
495 0 ▶sub verify($pk_data, $opts) {
Subroutine verify: not called CC=18 SCAR=58.3
496 0 my $sig_data = load_sig($opts);
497 0 my $msg_path = $opts->{msg_path};
498 0 ▶ my $quiet = $opts->{pretty_quiet} ? 2 : $opts->{quiet} ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
$opts->{'pretty_quiet'} ? : 0 0 0
$opts->{'quiet'} ? : 0 0 0
499 0 ▶ unless ($sig_data->{key_num} eq $pk_data->{key_num}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sig_data->{"key_num"} eq $pk_data->{"key_num"}) 0 0 0
500 # could just use 64 bit numbers everywhere but meh.
501 0 my $sig_num = unpack("a8", $sig_data->{key_num});
502 0 my $pk_num = unpack("a8", $pk_data->{key_num});
503 0 warn "Signature key id in '$opts->{sig_path}' is $sig_num\n";
504 0 warn "but the key id in the public key is $pk_num\n";
505 }
506 # pk_data sig_alg and sig_data sig_alg should match. Ed vs. ED just means
507 # pre-hashed. this format is janky.
508 0 my $sig_alg = $sig_algs{$sig_data->{sig_alg}};
509 0 ▶ if ($sig_data->{trusted_comment}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig_data->{"trusted_comment"}) 0 0 0
510 0 ▶ unless ($sig_alg->{verify}->($sig_data->{sig}.$sig_data->{trusted_comment},
Branch: 1 branch
BranchTrueFalseTotal
unless ($sig_alg->{"verify"}->($sig_data->{"sig"} . $sig_data->{"trusted_comment"}, $sig_data->{"full_sig"}, $pk_data->{"pk"})) 0 0 0
511 $sig_data->{full_sig}, $pk_data->{pk})) {
512 0 die "Comment signature verification failed\n";
513 }
514 }
515 0 my $sig_input;
516 0 ▶ if (exists $sig_alg->{hash_init}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $sig_alg->{'hash_init'}) { } 0 0 0
517 0 load_msg($sig_input, $msg_path, $sig_alg->{hash_init}->($sig_alg->{hashbytes}));
518 }
519 else {
520 0 ▶ die "Legacy (non-prehashed) signature found\n" if $opts->{prehashed};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"prehashed"} 0 0 0
521 0 load_msg($sig_input, $msg_path);
522 }
523 0 ▶ unless ($sig_alg->{verify}->($sig_input, $sig_data->{sig}, $pk_data->{pk})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sig_alg->{"verify"}->($sig_input, $sig_data->{"sig"}, $pk_data->{"pk"})) 0 0 0
524 0 die "Signature verification failed";
525 }
526 0 ▶ unless ($quiet) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($quiet) 0 0 0
527 0 ▶ my $handle = $opts->{do_print} ? \*STDERR : \*STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'do_print'} ? : 0 0 0
528 0 ▶ if ($quiet > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quiet > 1) { } 0 0 0
529 0 ▶ if ($sig_data->{trusted_comment}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig_data->{'trusted_comment'}) { } 0 0 0
530 0 print { $handle } $sig_data->{trusted_comment}, "\n";
531 }
532 else {
533 0 print { $handle } "No trusted comment\n";
534 }
535 }
536 else {
537 0 ▶ if ($sig_data->{trusted_comment}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig_data->{'trusted_comment'}) { } 0 0 0
538 0 print { $handle } "Signature and comment signature verified\n";
539 0 print { $handle } "Trusted comment: $sig_data->{trusted_comment}\n";
540 }
541 else {
542 0 print { $handle } "Signature verified\n";
543 0 print { $handle } "No trusted comment\n";
544 }
545 }
546 }
547 0 ▶ if ($opts->{do_print}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"do_print"}) 0 0 0
548 0 ▶ open(my $fh, "<", $msg_path) or die "$msg_path: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $msg_path or die "${msg_path}: $!" 0 0 0
549 0 seek($fh, 0, 0);
550 0 ▶ while (read($fh, my $buf, 40960)) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $fh, my $buf, 40960) 0 0 0
551 0 ▶ print $buf or die "stdout: $!";
Branch: 1 branch
BranchTrueFalseTotal
print $buf or die "stdout: $!" 0 0 0
552 }
553 }
554 }
555  
556 0 ▶sub main(@argv) {
Subroutine main: not called CC=28 SCAR=67.0
557 0 my $opts = {};
558 0 ▶ Getopt::Long::GetOptionsFromArray(\@argv, $opts, qw(
Branch: 1 branch
BranchTrueFalseTotal
&Getopt::Long::GetOptionsFromArray(\@argv, $opts, ("generate|G", "prehashed|H", "pk_b64|P:s", "pretty_quiet|Q", "recreate|R", "sign|S", "no_trusted_comment|T", "verify|V", "comment|c:s", "force|f", "help|h", "legacy_sign|l", "msg_path|m:s", "print|o", "pk_path|p:s", "sk_path|s:s", "quiet|q", "trusted_comment|t:s", "version|v", "sig_path|x:s")) or usage("Invalid options") 0 0 0
559 generate|G
560 prehashed|H
561 pk_b64|P:s
562 pretty_quiet|Q
563 recreate|R
564 sign|S
565 no_trusted_comment|T
566 verify|V
567 comment|c:s
568 force|f
569 help|h
570 legacy_sign|l
571 msg_path|m:s
572 print|o
573 pk_path|p:s
574 sk_path|s:s
575 quiet|q
576 trusted_comment|t:s
577 version|v
578 sig_path|x:s
579 )) or usage("Invalid options");
580  
581 0 ▶ usage() if $opts->{help};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"help"} 0 0 0
582 0 ▶ print "pminisign $VERSION\n" and exit(0) if $opts->{version};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"version"} 0 0 0
Condition0%0/5
$opts->{'version'} && (print("pminisign $main::VERSION\n") && exit 0)
!ll&&!rl&&r
000
print "pminisign $main::VERSION\n" and exit 0
!ll
00
MC/DC0%0/2
$opts->{'version'} && (print("pminisign $main::VERSION\n") && exit 0)
$opts->{'version'} print "pminisign $main::VERSION\n"
Truth table0%0/3
A && B
A= $opts->{'version'}B= print "pminisign $main::VERSION\n"
ABresult
0X0
100
111
583  
584 0 ▶ if ((grep { $opts->{$_} } qw(generate sign verify recreate)) != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep({$opts->{$_};} "generate", "sign", "verify", "recreate") != 1) 0 0 0
585 0 usage("one (and only one) of -G, -S, -V, or -R must be specified");
586 }
587 0 ▶ if ($opts->{pk_path} and $opts->{pk_b64}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"pk_path"} and $opts->{"pk_b64"}) 0 0 0
Condition0%0/3
$opts->{"pk_path"} and $opts->{"pk_b64"}
!ll&&!rl&&r
000
MC/DC0%0/2
$opts->{"pk_path"} and $opts->{"pk_b64"}
$opts->{"pk_path"} $opts->{"pk_b64"}
Truth table0%0/3
A and B
A= $opts->{"pk_path"}B= $opts->{"pk_b64"}
ABresult
0X0
100
111
588 0 die "a public key cannot be provided both inline and as a file";
589 }
590 0 ▶ if ($opts->{sig_path} and @argv > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"sig_path"} and @argv > 1) 0 0 0
Condition0%0/3
$opts->{"sig_path"} and @argv > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$opts->{"sig_path"} and @argv > 1
$opts->{"sig_path"} @argv > 1
Truth table0%0/3
A and B
A= $opts->{"sig_path"}B= @argv > 1
ABresult
0X0
100
111
591 0 die "a signature file can not be used when signing multiple files";
592 }
593  
594 0 ▶ if ($opts->{generate}) {
Branch: 4 branches
BranchTrueFalseTotal
if ($opts->{'generate'}) { } 0 0 0
elsif ($opts->{'sign'}) { } 0 0 0
elsif ($opts->{'recreate'}) { } 0 0 0
elsif ($opts->{'verify'}) { } 0 0 0
595 0 ▶ $opts->{pk_path} //= default_pk_path();
Condition0%0/3
$opts->{"pk_path"} //= default_pk_path()
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"pk_path"} //= default_pk_path()
$opts->{"pk_path"} default_pk_path()
Truth table0%0/3
A //= B
A= $opts->{"pk_path"}B= default_pk_path()
ABresult
1X1
011
000
596 0 ▶ $opts->{sk_path} //= default_sk_path();
Condition0%0/3
$opts->{"sk_path"} //= default_sk_path()
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"sk_path"} //= default_sk_path()
$opts->{"sk_path"} default_sk_path()
Truth table0%0/3
A //= B
A= $opts->{"sk_path"}B= default_sk_path()
ABresult
1X1
011
000
597 0 ▶ $opts->{comment} //= $sk_default_comment;
Condition0%0/3
$opts->{"comment"} //= $sk_default_comment
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"comment"} //= $sk_default_comment
$opts->{"comment"} $sk_default_comment
Truth table0%0/3
A //= B
A= $opts->{"comment"}B= $sk_default_comment
ABresult
1X1
011
000
598 0 generate($opts);
599 0 return 0;
600 }
601 elsif ($opts->{sign}) {
602 0 ▶ $opts->{sk_path} //= default_sk_path();
Condition0%0/3
$opts->{"sk_path"} //= default_sk_path()
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"sk_path"} //= default_sk_path()
$opts->{"sk_path"} default_sk_path()
Truth table0%0/3
A //= B
A= $opts->{"sk_path"}B= default_sk_path()
ABresult
1X1
011
000
603 0 ▶ usage("no file to sign provided") unless $opts->{msg_path};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"msg_path"} 0 0 0
604 0 my $sk_data = load_sk($opts);
605 0 for my $path ($opts->{msg_path}, @argv) {
606 0 signify($sk_data, $opts, $path);
607 }
608 0 return 0;
609 }
610 elsif ($opts->{recreate}) {
611 0 ▶ $opts->{pk_path} //= default_pk_path();
Condition0%0/3
$opts->{"pk_path"} //= default_pk_path()
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"pk_path"} //= default_pk_path()
$opts->{"pk_path"} default_pk_path()
Truth table0%0/3
A //= B
A= $opts->{"pk_path"}B= default_pk_path()
ABresult
1X1
011
000
612 0 ▶ $opts->{sk_path} //= default_sk_path();
Condition0%0/3
$opts->{"sk_path"} //= default_sk_path()
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"sk_path"} //= default_sk_path()
$opts->{"sk_path"} default_sk_path()
Truth table0%0/3
A //= B
A= $opts->{"sk_path"}B= default_sk_path()
ABresult
1X1
011
000
613 0 recreate($opts);
614 0 return 0;
615 }
616 elsif ($opts->{verify}) {
617 0 ▶ usage("No message file given") unless $opts->{msg_path};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"msg_path"} 0 0 0
618 0 my $pk_data;
619 0 ▶ if ($opts->{pk_path}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'pk_path'}) { } 0 0 0
elsif ($opts->{'pk_b64'}) { } 0 0 0
620 0 ▶ $pk_data = load_pk($opts->{pk_path}) if $opts->{pk_path};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"pk_path"} 0 0 0
621 }
622 elsif ($opts->{pk_b64}) {
623 0 ▶ $pk_data = unpack_pk_data(sodium_base642bin($opts->{pk_b64}, BASE64_VARIANT_ORIGINAL)) if $opts->{pk_b64};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"pk_b64"} 0 0 0
624 }
625 else {
626 0 $pk_data = load_pk(default_pk_path());
627 }
628 0 ▶ $opts->{sig_path} //= "$opts->{msg_path}.minisig";
Condition0%0/2
$opts->{"sig_path"} //= qq[$opts->{"msg_path"}.minisig]
l!l
00
MC/DC0%0/1
$opts->{"sig_path"} //= qq[$opts->{"msg_path"}.minisig]
$opts->{"sig_path"}
Truth table0%0/2
A
A= $opts->{"sig_path"}
Aresult
11
00
629 0 verify($pk_data, $opts);
630 0 return 0;
631 }
632 }
633  
634 1 ▶main(@ARGV) unless caller();
Branch: 1 branch
BranchTrueFalseTotal
unless caller() 0 1 1
635  
636 __END__
↑ Summary bin/csxs-ppcrypt »