| 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 v5.20.0; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use experimental 'signatures'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Crypt::Sodium::XS; | ||||||||||||||||||||||||||||||||||||||||
|
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_flags_decrypt_default'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Crypt::Sodium::XS::Util qw(sodium_random_bytes); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Fcntl qw(O_CREAT O_EXCL O_TRUNC O_RDONLY O_RDWR O_WRONLY); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Getopt::Long qw(:config gnu_getopt); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use constant CSXS_MAGIC_FORMAT => 'A4CCA2CCS<'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use constant CSXS_MAGIC_LEN => 12; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use constant CSXS_MAGIC_FIELDS => [qw[ | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | magic_bytes | ||||||||||||||||||||||||||||||||||||||||||
| 19 | version | ||||||||||||||||||||||||||||||||||||||||||
| 20 | opts | ||||||||||||||||||||||||||||||||||||||||||
| 21 | reserved_1 | ||||||||||||||||||||||||||||||||||||||||||
| 22 | hash_primitive | ||||||||||||||||||||||||||||||||||||||||||
| 23 | sstream_primitive | ||||||||||||||||||||||||||||||||||||||||||
| 24 | bufksize | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | ]]; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 1 | ▶ | use constant CSXS_OPTFLAG_PAD_LAST_BLOCK => 1; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | # csxs magic | ||||||||||||||||||||||||||||||||||||||||||
| 29 | # 4 bytes "CSXS" | ||||||||||||||||||||||||||||||||||||||||||
| 30 | # 1 bytes version | ||||||||||||||||||||||||||||||||||||||||||
| 31 | # 1 bytes option flags | ||||||||||||||||||||||||||||||||||||||||||
| 32 | # 2 bytes RESERVED | ||||||||||||||||||||||||||||||||||||||||||
| 33 | # 1 bytes hash primitive | ||||||||||||||||||||||||||||||||||||||||||
| 34 | # 1 bytes secretstream primitive | ||||||||||||||||||||||||||||||||||||||||||
| 35 | # 2 bytes LE bufsize in kilobytes | ||||||||||||||||||||||||||||||||||||||||||
| 36 | # algorithm-specific; but for the only ones supported: | ||||||||||||||||||||||||||||||||||||||||||
| 37 | # 16 bytes pwhash salt | ||||||||||||||||||||||||||||||||||||||||||
| 38 | # 8 bytes opslimit | ||||||||||||||||||||||||||||||||||||||||||
| 39 | # 8 bytes memlimit | ||||||||||||||||||||||||||||||||||||||||||
| 40 | # 24 bytes secretstream header | ||||||||||||||||||||||||||||||||||||||||||
| 41 | # | ||||||||||||||||||||||||||||||||||||||||||
| 42 | # 68 total | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | # written blocks are 4096 bufsize + 17 abytes = 4113 | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | # opts flags | ||||||||||||||||||||||||||||||||||||||||||
| 47 | # 00000000 | ||||||||||||||||||||||||||||||||||||||||||
| 48 | # ||||||||--- 1 use padding | ||||||||||||||||||||||||||||||||||||||||||
| 49 | # |||||||---- 2 RESERVED | ||||||||||||||||||||||||||||||||||||||||||
| 50 | # ||||||----- 4 RESERVED | ||||||||||||||||||||||||||||||||||||||||||
| 51 | # |||||------ 8 RESERVED | ||||||||||||||||||||||||||||||||||||||||||
| 52 | # ||||------- 16 RESERVED | ||||||||||||||||||||||||||||||||||||||||||
| 53 | # |||-------- 32 RESERVED | ||||||||||||||||||||||||||||||||||||||||||
| 54 | # ||--------- 64 RESERVED | ||||||||||||||||||||||||||||||||||||||||||
| 55 | # |---------- 128 RESERVED | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | # hardcoded primitives, can be added to in future with header values. | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 1 | my $pwhash = Crypt::Sodium::XS->pwhash(primitive => 'argon2id'); | |||||||||||||||||||||||||||||||||||||||||
| 59 | 1 | my $sstream = Crypt::Sodium::XS->secretstream(primitive => 'xchacha20poly1305'); | |||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | sub usage { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
usage: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | print STDERR "@_\n" if @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | print STDERR <<'EOUSAGE'; | |||||||||||||||||||||||||||||||||||||||||
| 64 | usage: | ||||||||||||||||||||||||||||||||||||||||||
| 65 | csxs-ppcrypt -h | ||||||||||||||||||||||||||||||||||||||||||
| 66 | csxs-ppcrypt -D [-f] <infile> <outfile> | ||||||||||||||||||||||||||||||||||||||||||
| 67 | csxs-ppcrypt -E [-f] [-p] <infile> <outfile> | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | actions: | ||||||||||||||||||||||||||||||||||||||||||
| 70 | -h print this help message and exit | ||||||||||||||||||||||||||||||||||||||||||
| 71 | -D decrypt | ||||||||||||||||||||||||||||||||||||||||||
| 72 | -E encrypt | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | options: | ||||||||||||||||||||||||||||||||||||||||||
| 75 | -f force; overwrite an existing <outfile> | ||||||||||||||||||||||||||||||||||||||||||
| 76 | -p pad; ISO/IEC 7816-4 padding on final block | ||||||||||||||||||||||||||||||||||||||||||
| 77 | arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 78 | <infile> path to input file, or '-' for stdin | ||||||||||||||||||||||||||||||||||||||||||
| 79 | <outfile> path to output file, or '-' for stdout | ||||||||||||||||||||||||||||||||||||||||||
| 80 | EOUSAGE | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | exit 1 if @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | exit 0; | |||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | sub make_file_magic($opts) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
make_file_magic: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | return +{ | ||||||||||||||||||||||||||||||||||||||||||
| 87 | magic_bytes => 'CSXS', | ||||||||||||||||||||||||||||||||||||||||||
| 88 | version => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | opts => ($opts->{pad} ? CSXS_OPTFLAG_PAD_LAST_BLOCK : 0), | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | hash_primitive => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 91 | sstream_primitive => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 92 | bufksize => 4, | ||||||||||||||||||||||||||||||||||||||||||
| 93 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | sub parse_file_magic($str) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_file_magic: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | my %magic; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | @magic{@{;CSXS_MAGIC_FIELDS}} = unpack(CSXS_MAGIC_FORMAT, $str); | |||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | return \%magic; | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | sub stringify_file_magic($magic) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
stringify_file_magic: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | local $magic->{reserved_1} = "\0\0"; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | return pack(CSXS_MAGIC_FORMAT, @$magic{@{;CSXS_MAGIC_FIELDS}}); | |||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | sub verify_file_magic($magic) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
verify_file_magic: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | die "invalid magic: bad magic bytes" unless $magic->{magic_bytes} eq 'CSXS'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | die "invalid magic: incompatible version $magic->{version}" unless $magic->{version} == 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | # only padding supported currently | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | die "invalid magic: invalid opts" if $magic->{opts} != 0 and $magic->{opts} != 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$magic->{"opts"} != 0 and $magic->{"opts"} != 1
MC/DC0%0/2
$magic->{"opts"} != 0 and $magic->{"opts"} != 1
$magic->{"opts"} != 0
$magic->{"opts"} != 1
Truth table0%0/3
A and B
A= $magic->{"opts"} != 0B= $magic->{"opts"} != 1
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | die "invalid magic: invalid hash primitive" if $magic->{hash_primitive} != 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | die "invalid magic: invalid sstream primitive" if $magic->{sstream_primitive} != 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | # hardcoded | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | $magic->{bufksize} ||= 4; # REMOVEME: legacy format did not have bufsize. | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$magic->{"bufksize"} ||= 4
MC/DC0%0/1
$magic->{"bufksize"} ||= 4
$magic->{"bufksize"}
Truth table0%0/2
A
A= $magic->{"bufksize"}
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | die "invalid magic: invalid bufsize" if $magic->{bufksize} != 4; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | return $magic; | |||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | sub open_handles($opts) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
open_handles: not called CC=22 SCAR=62.3
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | my ($ifh, $ofh); | |||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | my ($infile, $outfile) = @{$opts}{qw{infile outfile}}; | |||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | ▶ | if ($infile eq '-' and !$outfile) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$infile eq "-" and not $outfile
MC/DC0%0/2
$infile eq "-" and not $outfile
$infile eq "-"
not $outfile
Truth table0%0/3
A and not(B)
A= $infile eq "-"B= not $outfile
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | die "reading from stdin requires -o <outfile>"; | |||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | unless ($outfile) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | if ($opts->{decrypt}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | $outfile = $infile; | |||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | ▶ | unless ($outfile =~ s/\.csxspp\z//) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | die "cannot determine output filename from input filename. use -o <outfile>."; | |||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | $outfile = "$infile.csxspp"; | |||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | if ($outfile ne '-' and -e $outfile and !$opts->{force}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$outfile ne "-" and -e $outfile and not $opts->{"force"}
$outfile ne "-" and -e $outfile
MC/DC0%0/3
$outfile ne "-" and -e $outfile and not $opts->{"force"}
$outfile ne "-"
-e $outfile
not $opts->{"force"}
Truth table0%0/4
A and B and not(C)
A= $outfile ne "-"B= -e $outfileC= not $opts->{"force"}
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | die "<outfile> '$outfile' exists. use -f to overwrite"; | |||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | if ($infile eq '-') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | $ifh = \*STDIN; | |||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||
| 145 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | ▶ | if ($opts->{encrypt}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | sysopen($ifh, $infile, O_RDONLY) or die "sysopen: $infile: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | ▶ | binmode($ifh, ":raw") or die "binmode: $infile: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||
| 150 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | open($ifh, "<:raw", $infile) or die "open: $infile: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | my $omode = 0644; | |||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | ▶ | $omode = 0600 if $opts->{decrypt}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | ▶ | if ($outfile eq '-') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | $ofh = \*STDOUT; | |||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||
| 160 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | my $flags = O_CREAT|O_TRUNC|O_WRONLY; | |||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | ▶ | $flags |= O_EXCL unless $opts->{force}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | ▶ | sysopen($ofh, $outfile, $flags, $omode) or die "open: $outfile: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | ▶ | binmode($ofh, ":raw") or die "binmode: $outfile: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | if ($opts->{encrypt} and !$opts->{force} and -t $ofh) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$opts->{"encrypt"} and not $opts->{"force"} and -t $ofh
$opts->{"encrypt"} and not $opts->{"force"}
MC/DC0%0/3
$opts->{"encrypt"} and not $opts->{"force"} and -t $ofh
$opts->{"encrypt"}
not $opts->{"force"}
-t $ofh
Truth table0%0/4
A and not(B) and C
A= $opts->{"encrypt"}B= not $opts->{"force"}C= -t $ofh
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | die "cowardly refusing to encrypt to a tty. use -f to override"; | |||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | return ($ifh, $ofh); | |||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | ▶ | sub decrypt($ifh, $ofh, $opts) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
decrypt: not called CC=16 SCAR=56.1
| |||||||||||||||||||||||||||||||||||||||||||
| 174 | # CSXS header | ||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | my $magic_len = CSXS_MAGIC_LEN; | |||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | my $salt_len = $pwhash->SALTBYTES; | |||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | my $header_len = $magic_len + $salt_len + 16; # opslimit 8 memlimit 8 | |||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | my $r = read($ifh, my $header, $header_len); | |||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | ▶ | die "read: $!" unless defined($r); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | 0 | ▶ | die "read: unexpected EOF reading header (expected: $header_len got: $r)" unless $r == $header_len; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | my ($magic, $salt, $opslimit, $memlimit) = unpack("a${magic_len}a${salt_len}Q<2", $header); | |||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | $magic = verify_file_magic(parse_file_magic($magic)); | |||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | ▶ | unless ($memlimit >= $pwhash->MEMLIMIT_MIN and $memlimit <= $pwhash->MEMLIMIT_MAX) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$memlimit >= $pwhash->MEMLIMIT_MIN and $memlimit <= $pwhash->MEMLIMIT_MAX
MC/DC0%0/2
$memlimit >= $pwhash->MEMLIMIT_MIN and $memlimit <= $pwhash->MEMLIMIT_MAX
$memlimit >= $pwhash->MEMLIMIT_MIN
$memlimit <= $pwhash->MEMLIMIT_MAX
Truth table0%0/3
A and B
A= $memlimit >= $pwhash->MEMLIMIT_MINB= $memlimit <= $pwhash->MEMLIMIT_MAX
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | die "read: bad header (invalid memlimit $memlimit)"; | |||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | ▶ | unless ($opslimit >= $pwhash->OPSLIMIT_MIN and $opslimit <= $pwhash->OPSLIMIT_MAX) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$opslimit >= $pwhash->OPSLIMIT_MIN and $opslimit <= $pwhash->OPSLIMIT_MAX
MC/DC0%0/2
$opslimit >= $pwhash->OPSLIMIT_MIN and $opslimit <= $pwhash->OPSLIMIT_MAX
$opslimit >= $pwhash->OPSLIMIT_MIN
$opslimit <= $pwhash->OPSLIMIT_MAX
Truth table0%0/3
A and B
A= $opslimit >= $pwhash->OPSLIMIT_MINB= $opslimit <= $pwhash->OPSLIMIT_MAX
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | die "read: bad header (invalid opslimit $memlimit)"; | |||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | my $key = $pwhash->pwhash($opts->{passphrase}, $salt, $sstream->KEYBYTES, $opslimit, $memlimit); | |||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | # secretstream header | ||||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | ▶ | $r = read($ifh, $header, $sstream->HEADERBYTES) or die "read: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | ▶ | die "read: $!" unless defined($r); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | 0 | ▶ | die "read: short stream header" unless $r == $sstream->HEADERBYTES; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | my $stream_dec = $sstream->init_decrypt($header, $key); | |||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | my $tag = -1; | |||||||||||||||||||||||||||||||||||||||||
| 198 | 0 | my $buf; | |||||||||||||||||||||||||||||||||||||||||
| 199 | 0 | my $bufsize = $magic->{bufksize} * 1024; | |||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | my $decrypt_bufsize = $bufsize + $sstream->ABYTES; | |||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | ▶ | while ($r = read($ifh, $buf, $decrypt_bufsize)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | ▶ | if ($tag == $sstream->TAG_FINAL) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | die "extra data after end-of-stream"; | |||||||||||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | (my $plaintext, $tag) = $stream_dec->decrypt($buf); | |||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | ▶ | if ($tag == $sstream->TAG_FINAL | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$tag == $sstream->TAG_FINAL and $magic->{"opts"} & 1
MC/DC0%0/2
$tag == $sstream->TAG_FINAL and $magic->{"opts"} & 1
$tag == $sstream->TAG_FINAL
$magic->{"opts"} & 1
Truth table0%0/3
A and B
A= $tag == $sstream->TAG_FINALB= $magic->{"opts"} & 1
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | && $magic->{opts} & CSXS_OPTFLAG_PAD_LAST_BLOCK) { | ||||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | $plaintext = $plaintext->unpad($bufsize); | |||||||||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||||||||
| 210 | 0 | $plaintext->to_fd(fileno($ofh)); | |||||||||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | ▶ | die "read: $!" unless defined($r); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | ▶ | die "missing end-of-stream" unless $tag == $sstream->TAG_FINAL; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | ▶ | sub encrypt($ifh, $ofh, $opts) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encrypt: not called CC=10 SCAR=47.0
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | my $salt_len = $pwhash->SALTBYTES; | |||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | my $salt = sodium_random_bytes($salt_len); | |||||||||||||||||||||||||||||||||||||||||
| 219 | # TODO: make memlimit/opslimit choosable. accept low, med*, high, or number | ||||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | my $opslimit = $pwhash->OPSLIMIT_MODERATE; | |||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | my $memlimit = $pwhash->MEMLIMIT_MODERATE; | |||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | my $key = $pwhash->pwhash($opts->{passphrase}, $salt, $sstream->KEYBYTES, $opslimit, $memlimit); | |||||||||||||||||||||||||||||||||||||||||
| 223 | # write as static string because only one version and primitive supported | ||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | my $magic = make_file_magic($opts); | |||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | my $file_magic = stringify_file_magic($magic); | |||||||||||||||||||||||||||||||||||||||||
| 226 | 0 | ▶ | syswrite($ofh, $file_magic . pack("a${salt_len}Q<2", $salt, $opslimit, $memlimit)) or die "write: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | my ($header, $stream_enc) = $sstream->init_encrypt($key); | |||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | ▶ | syswrite($ofh, $header) or die "write: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | my $fd = fileno($ifh); | |||||||||||||||||||||||||||||||||||||||||
| 231 | # what follows is tricky. ugly. sorry. | ||||||||||||||||||||||||||||||||||||||||||
| 232 | # writing to output is "one buffer behind" reading from input. this is so the | ||||||||||||||||||||||||||||||||||||||||||
| 233 | # final tag and padding can be put on the last block. | ||||||||||||||||||||||||||||||||||||||||||
| 234 | 0 | my $bufsize = $magic->{bufksize} * 1024; | |||||||||||||||||||||||||||||||||||||||||
| 235 | 0 | my $prev_buf = mv_from_fd($fd, $bufsize, protmem_flags_decrypt_default()); | |||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 237 | 0 | ▶ | last unless defined $prev_buf; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | my $next_buf; | |||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | ▶ | if ($prev_buf->size == $bufsize) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | $next_buf = mv_from_fd($fd, $bufsize, protmem_flags_decrypt_default()); | |||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | ▶ | undef $next_buf unless $next_buf->size; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | ▶ | my $tag = $next_buf ? $sstream->TAG_MESSAGE : $sstream->TAG_FINAL; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | my $this_buf = $next_buf ? $prev_buf | ||||||||||||||||||||||||||||||||||||||||||
| 245 | 0 | ▶ | : $opts->{pad} ? $prev_buf->pad($bufsize) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | : $prev_buf; | ||||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | undef $prev_buf; | |||||||||||||||||||||||||||||||||||||||||
| 248 | 0 | my $block = $stream_enc->encrypt($this_buf, $tag); | |||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | ▶ | syswrite($ofh, $block) or die "write: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | 0 | $prev_buf = $next_buf; | |||||||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | 0 | ▶ | sub main(@argv) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
main: not called CC=17 SCAR=57.2
| |||||||||||||||||||||||||||||||||||||||||||
| 255 | 0 | ▶ | Getopt::Long::GetOptionsFromArray( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | \@argv, | ||||||||||||||||||||||||||||||||||||||||||
| 257 | my $opts = {}, | ||||||||||||||||||||||||||||||||||||||||||
| 258 | qw( | ||||||||||||||||||||||||||||||||||||||||||
| 259 | decrypt|D | ||||||||||||||||||||||||||||||||||||||||||
| 260 | encrypt|E | ||||||||||||||||||||||||||||||||||||||||||
| 261 | force|f | ||||||||||||||||||||||||||||||||||||||||||
| 262 | help|h | ||||||||||||||||||||||||||||||||||||||||||
| 263 | pad|p | ||||||||||||||||||||||||||||||||||||||||||
| 264 | version|V | ||||||||||||||||||||||||||||||||||||||||||
| 265 | ), | ||||||||||||||||||||||||||||||||||||||||||
| 266 | ) or usage("Invalid options"); | ||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | ▶ | usage() if $opts->{help}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 269 | 0 | ▶ | print "csxs-ppcrypt $VERSION\n" and exit(0) if $opts->{version}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
$opts->{'version'} && (print("csxs-ppcrypt $main::VERSION\n") && exit 0)
print "csxs-ppcrypt $main::VERSION\n" and exit 0
MC/DC0%0/2
$opts->{'version'} && (print("csxs-ppcrypt $main::VERSION\n") && exit 0)
$opts->{'version'}
print "csxs-ppcrypt $main::VERSION\n"
Truth table0%0/3
A && B
A= $opts->{'version'}B= print "csxs-ppcrypt $main::VERSION\n"
| |||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | ▶ | if ((grep { $opts->{$_} } qw(decrypt encrypt)) != 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | 0 | usage("one (and only one) of -D or -E must be specified"); | |||||||||||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | $opts->{infile} = shift(@argv); | |||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | $opts->{outfile} = shift(@argv); | |||||||||||||||||||||||||||||||||||||||||
| 277 | 0 | ▶ | usage("missing <infile>") unless $opts->{infile}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 278 | 0 | ▶ | usage("missing <infile>") unless $opts->{outfile}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | ▶ | usage("too many arguments") if @argv; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | my ($ifh, $ofh) = open_handles($opts); | |||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | my $pp = mv_from_tty("Passphrase: "); | |||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | ▶ | if ($opts->{encrypt}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 285 | 0 | my $pp_confirm = mv_from_tty("Passphrase (again): "); | |||||||||||||||||||||||||||||||||||||||||
| 286 | 0 | ▶ | die "passphrases do not match" unless $pp->length == $pp_confirm->length and $pp eq $pp_confirm; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$pp->length == $pp_confirm->length and $pp eq $pp_confirm
MC/DC0%0/2
$pp->length == $pp_confirm->length and $pp eq $pp_confirm
$pp->length == $pp_confirm->length
$pp eq $pp_confirm
Truth table0%0/3
A and B
A= $pp->length == $pp_confirm->lengthB= $pp eq $pp_confirm
| |||||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | undef $pp_confirm; | |||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||
| 289 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 290 | 0 | ▶ | die "refusing to use empty passphrase" unless $pp->length; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||||||||
| 292 | 0 | $opts->{passphrase} = $pp; | |||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||
| 294 | 0 | ▶ | if ($opts->{decrypt}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 295 | 0 | decrypt($ifh, $ofh, $opts); | |||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||
| 297 | elsif ($opts->{encrypt}) { | ||||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | encrypt($ifh, $ofh, $opts); | |||||||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||||||
| 300 | 0 | ▶ | close($ifh) or die "<infile>: close: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 301 | 0 | ▶ | close($ofh) or die "<outfile>: close: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | 1 | ▶ | main(@ARGV) unless caller; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||
| 308 | __END__ | ||||||||||||||||||||||||||||||||||||||||||