Age.pm

filter: Statementstmt 100% 96 / 96 Branchbran 69.2% 36 / 52 Conditioncond 59.3% 19 / 32 MC/DCmcdc 18.7% 3 / 16 Subroutinesub 100% 15 / 15 Podpod 100% 7 / 7 totaltotal 80.7% 176 / 218 CC 43 SCAR 18.7
CC
43
Cov
84%
decrypt_file
10.6
encrypt_file
9.6
decrypt
7.1
CRAP 6.5
« Age/Header.pm ↑ Summary Age/Primitives.pm »
1 package Crypt::Age;
2 # ABSTRACT: Perl implementation of age encryption (age-encryption.org)
3  
4 7 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Crypt::Age::Keys;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Crypt::Age::Primitives;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Crypt::Age::Header;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 7 our $VERSION = '0.005';
13  
14 22 ▶sub generate_keypair {
Subroutine generate_keypair: called CC=1 SCAR=0.0
Pod documented
15 22 my ($class) = @_;
16 22 return Crypt::Age::Keys->generate_keypair;
17 }
18  
19  
20 28 ▶sub encrypt {
Subroutine encrypt: called CC=6 SCAR=18.0
Pod documented
21 28 my ($class, %args) = @_;
22  
23 28 ▶ my $plaintext = $args{plaintext} // croak "plaintext required";
Condition100%2/2
$args{"plaintext"} // croak("plaintext required")
l!l
271
MC/DC100%1/1
$args{"plaintext"} // croak("plaintext required")
$args{"plaintext"}
Truth table100%2/2
A
A= $args{"plaintext"}
Aresult
11
00
24 27 ▶ my $recipients = $args{recipients} // croak "recipients required";
Condition100%2/2
$args{"recipients"} // croak("recipients required")
l!l
261
MC/DC100%1/1
$args{"recipients"} // croak("recipients required")
$args{"recipients"}
Truth table100%2/2
A
A= $args{"recipients"}
Aresult
11
00
25  
26 # This is perl's own test for the in-memory open below, hoisted so it can
27 # say what is wrong. PerlIO::scalar downgrades the string in place
28 # (sv_utf8_downgrade) and, when that cannot be done, warns "Strings with
29 # code points over 0xFF may not be mapped into in-memory file handles" and
30 # returns EINVAL -- so the open croaked "open on input string: Invalid
31 # argument", which tells a caller who passed decoded characters nothing at
32 # all. Running the test first replaces that with a message naming the
33 # cause, and the warning never happens because the open is never reached.
34 #
35 # Not utf8::is_utf8: it reports the internal representation, so a pure
36 # ASCII string that happens to be stored upgraded answers true while
37 # holding nothing above 0xFF. What decides this is the content.
38 #
39 # Not a /[^\x00-\xff]/ scan either, though both are free on an unflagged
40 # string (perl short-circuits: downgrade on the flag, the regex on the
41 # optimizer knowing that class cannot match a non-UTF-8 target -- measured
42 # at 2000 passes over 16 MiB in under 0.01s CPU for both). They part on a
43 # flagged string: over 16 MiB, downgrade costs 32ms against the regex's
44 # 55ms, and it *is* the scan perl is about to do, so it leaves the string
45 # downgraded and perl's repeat of it is then a flag test. The regex pays
46 # for that scan twice. Worth it either way, but this way costs least.
47 #
48 # Mutating our own copy is safe and changes nothing a caller can see:
49 # downgrading converts the representation, never the value, %args and the
50 # lexical are both copies, and on success perl's open performs this very
51 # conversion anyway. A failure leaves the string untouched.
52 #
53 # One bit comes back, which is all that may be reported anyway: the offset
54 # a scan would yield is derived from the caller's plaintext, and plaintext
55 # does not go into error messages.
56 26 ▶ utf8::downgrade($plaintext, 1)
Branch: 1 branch
BranchTrueFalseTotal
utf8::downgrade($plaintext, 1) or croak("plaintext must be a byte string: it holds a code point above 0xFF, encode it before passing it in") 25 1 26
57 or croak 'plaintext must be a byte string: it holds a code point '
58 .'above 0xFF, encode it before passing it in';
59  
60 25 ▶ open my $ifh, '<:raw', \$plaintext or croak "open on input string: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $ifh, "<:raw", \$plaintext or croak("open on input string: $!") 25 0 25
61  
62 25 my $output = '';
63 25 ▶ open my $ofh, '>:raw', \$output or croak "open on output string: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $ofh, ">:raw", \$output or croak("open on output string: $!") 25 0 25
64  
65 25 $class->_encrypt_fh($ifh, $ofh, $recipients);
66  
67 16 return $output;
68 }
69  
70  
71 32 ▶sub decrypt {
Subroutine decrypt: called CC=7 SCAR=19.7
Pod documented
72 32 my ($class, %args) = @_;
73  
74 32 ▶ my $ciphertext = $args{ciphertext} // croak "ciphertext required";
Condition100%2/2
$args{"ciphertext"} // croak("ciphertext required")
l!l
311
MC/DC100%1/1
$args{"ciphertext"} // croak("ciphertext required")
$args{"ciphertext"}
Truth table100%2/2
A
A= $args{"ciphertext"}
Aresult
11
00
75 31 ▶ my $identities = $args{identities} // croak "identities required";
Condition50%1/2
$args{"identities"} // croak("identities required")
l!l
310
MC/DC0%0/1
$args{"identities"} // croak("identities required")
$args{"identities"}
Truth table50%1/2
A
A= $args{"identities"}
Aresult
11
00
76  
77 # Same test, same reasons as in encrypt above; the advice differs because
78 # age ciphertext is binary, so a wide character in it means the caller
79 # decoded bytes that were never text rather than forgot to encode text.
80 31 ▶ utf8::downgrade($ciphertext, 1)
Branch: 1 branch
BranchTrueFalseTotal
utf8::downgrade($ciphertext, 1) or croak("ciphertext must be a byte string: it holds a code point above 0xFF, read it with :raw rather than decoding it") 30 1 31
81 or croak 'ciphertext must be a byte string: it holds a code point '
82 .'above 0xFF, read it with :raw rather than decoding it';
83  
84 30 ▶ open my $ifh, '<:raw', \$ciphertext or croak "open on input string: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $ifh, "<:raw", \$ciphertext or croak("open on input string: $!") 30 0 30
85  
86 30 my $output = '';
87 30 ▶ open my $ofh, '>:raw', \$output or croak "open on output string: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $ofh, ">:raw", \$output or croak("open on output string: $!") 30 0 30
88  
89 # Forwarded only when the caller actually gave it, so that "not passed"
90 # stays distinguishable from "passed as undef" all the way down: the first
91 # takes Header's default, the second is an error there. Building the pair
92 # here rather than defaulting it means this method names no limit of its
93 # own -- see Crypt::Age::Header/THE STANZA LIMIT for the number and why it
94 # is ours rather than the format's.
95 $class->_decrypt_fh($ifh, $ofh, $identities,
96 30 ▶ exists $args{max_stanzas} ? ( max_stanzas => $args{max_stanzas} ) : ());
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'max_stanzas'} ? : 1 29 30
97  
98 19 return $output;
99 }
100  
101  
102 29 ▶sub _encrypt_fh {
Subroutine _encrypt_fh: called CC=5 SCAR=16.1
103 29 my ($class, $ifh, $ofh, $recipients) = @_;
104 29 ▶ binmode($ifh, ':raw') or croak "cannot binmode input filehandle: $!";
Branch: 1 branch
BranchTrueFalseTotal
binmode $ifh, ":raw" or croak("cannot binmode input filehandle: $!") 29 0 29
105 29 ▶ binmode($ofh, ':raw') or croak "cannot binmode output filehandle: $!";
Branch: 1 branch
BranchTrueFalseTotal
binmode $ofh, ":raw" or croak("cannot binmode output filehandle: $!") 29 0 29
106  
107 # Same skeleton as the shape checks in Crypt::Age::Header -- <param> must
108 # be a <Type>, then a clause carrying the requirement and its reason, then
109 # the fix -- because this guard and Header::create's fire on the identical
110 # caller mistake and only differ in which layer catches it first. This one
111 # always does: create is called below with the list this already accepted,
112 # so its message is unreachable from here and a caller who searched for
113 # one wording had to find the other.
114 #
115 # The reason is this layer's, not create's: create wraps the file key once
116 # per entry, while what a caller of encrypt sees is that every entry ends
117 # up able to decrypt. Nothing is interpolated, for the reason written out
118 # at create -- the mistake that puts a bare string here is the swap of
119 # recipient and identity, so the value is not reliably a public one.
120 29 ▶ croak 'recipients must be an ArrayRef: this method encrypts to every '
Branch: 1 branch
BranchTrueFalseTotal
if ref $recipients ne "ARRAY" 3 26 29
121 .'entry, pass [$recipient] rather than $recipient'
122 if ref($recipients) ne 'ARRAY';
123 # The emptiness case, moved onto the same skeleton in the same change that
124 # gave Header::create a guard of its own. Leaving it on its old wording
125 # would have rebuilt, for the empty list, exactly the divergence the shape
126 # checks above were just brought out of. The reason is this layer's again:
127 # what a caller of encrypt sees is not a header without stanzas but a
128 # result nobody can open.
129 26 ▶ croak 'recipients must not be empty: this method encrypts to every entry, '
Branch: 1 branch
BranchTrueFalseTotal
unless @$recipients 4 22 26
130 .'so with none the result can never be decrypted, pass at least one '
131 .'recipient'
132 unless @$recipients;
133  
134 # Generate random file key
135 22 my $file_key = Crypt::Age::Primitives->generate_file_key;
136  
137 # Create header with wrapped file key for each recipient
138 22 print {$ofh} Crypt::Age::Header->create($file_key, $recipients)->to_string;
139  
140 # Generate payload nonce and derive payload key
141 18 my $nonce = Crypt::Age::Primitives->generate_payload_nonce;
142 18 print {$ofh} $nonce;
143  
144 18 my $payload_key = Crypt::Age::Primitives->derive_payload_key($file_key, $nonce);
145 18 return Crypt::Age::Primitives->encrypt_payload_fh($payload_key, $ifh, $ofh);
146 }
147  
148 1 ▶sub encrypt_file {
Subroutine encrypt_file: called CC=8 SCAR=22.6
Pod documented
149 1 my ($class, %args) = @_;
150 1 ▶ my $input = $args{input} // croak "input required";
Condition50%1/2
$args{"input"} // croak("input required")
l!l
10
MC/DC0%0/1
$args{"input"} // croak("input required")
$args{"input"}
Truth table50%1/2
A
A= $args{"input"}
Aresult
11
00
151 1 ▶ my $output = $args{output} // croak "output required";
Condition50%1/2
$args{"output"} // croak("output required")
l!l
10
MC/DC0%0/1
$args{"output"} // croak("output required")
$args{"output"}
Truth table50%1/2
A
A= $args{"output"}
Aresult
11
00
152 1 ▶ my $recipients = $args{recipients} // croak "recipients required";
Condition50%1/2
$args{"recipients"} // croak("recipients required")
l!l
10
MC/DC0%0/1
$args{"recipients"} // croak("recipients required")
$args{"recipients"}
Truth table50%1/2
A
A= $args{"recipients"}
Aresult
11
00
153  
154 1 ▶ open my $in_fh, '<:raw', $input
Branch: 1 branch
BranchTrueFalseTotal
open my $in_fh, "<:raw", $input or croak("Cannot open input file '${input}': $!") 1 0 1
155 or croak "Cannot open input file '$input': $!";
156 1 ▶ open my $out_fh, '>:raw', $output
Branch: 1 branch
BranchTrueFalseTotal
open my $out_fh, ">:raw", $output or croak("Cannot open output file '${output}': $!") 1 0 1
157 or croak "Cannot open output file '$output': $!";
158  
159 1 $class->_encrypt_fh($in_fh, $out_fh, $recipients);
160  
161 1 ▶ close $out_fh or croak "Cannot close output file '$output': $!";
Branch: 1 branch
BranchTrueFalseTotal
close $out_fh or croak("Cannot close output file '${output}': $!") 1 0 1
162 1 ▶ close $in_fh or croak "Cannot close input file '$input': $!";
Branch: 1 branch
BranchTrueFalseTotal
close $in_fh or croak("Cannot close input file '${input}': $!") 1 0 1
163  
164 1 return 1;
165 }
166  
167  
168 3 ▶sub encrypt_filehandle {
Subroutine encrypt_filehandle: called CC=4 SCAR=14.5
Pod documented
169 3 my ($class, %args) = @_;
170 3 ▶ my $in_fh = $args{input} // croak "input required";
Condition50%1/2
$args{"input"} // croak("input required")
l!l
30
MC/DC0%0/1
$args{"input"} // croak("input required")
$args{"input"}
Truth table50%1/2
A
A= $args{"input"}
Aresult
11
00
171 3 ▶ my $out_fh = $args{output} // croak "output required";
Condition50%1/2
$args{"output"} // croak("output required")
l!l
30
MC/DC0%0/1
$args{"output"} // croak("output required")
$args{"output"}
Truth table50%1/2
A
A= $args{"output"}
Aresult
11
00
172 3 ▶ my $recipients = $args{recipients} // croak "recipients required";
Condition50%1/2
$args{"recipients"} // croak("recipients required")
l!l
30
MC/DC0%0/1
$args{"recipients"} // croak("recipients required")
$args{"recipients"}
Truth table50%1/2
A
A= $args{"recipients"}
Aresult
11
00
173  
174 3 $class->_encrypt_fh($in_fh, $out_fh, $recipients);
175  
176 1 return 1;
177  
178 }
179  
180  
181 115 ▶sub _decrypt_fh {
Subroutine _decrypt_fh: called CC=6 SCAR=18.0
182 115 my ($class, $ifh, $ofh, $identities, %opt) = @_;
183 115 ▶ binmode($ifh, ':raw') or croak "cannot binmode input filehandle: $!";
Branch: 1 branch
BranchTrueFalseTotal
binmode $ifh, ":raw" or croak("cannot binmode input filehandle: $!") 115 0 115
184 115 ▶ binmode($ofh, ':raw') or croak "cannot binmode output filehandle: $!";
Branch: 1 branch
BranchTrueFalseTotal
binmode $ofh, ":raw" or croak("cannot binmode output filehandle: $!") 115 0 115
185  
186 # The counterpart to the recipients check in _encrypt_fh, in the same form
187 # and ahead of unwrap_file_key's own check for the same reason. The clause
188 # is this layer's: unwrap_file_key tries each entry against each stanza,
189 # while what a caller of decrypt sees is that the file opens if any one
190 # entry fits.
191 #
192 # Never interpolate $identities. A bare string in this parameter is an
193 # identity, and this croak stands between it and the dereference in
194 # unwrap_file_key that would otherwise put 32 characters of secret key
195 # material into an exception; a message quoting it here would reintroduce
196 # exactly that leak one frame earlier.
197 115 ▶ croak 'identities must be an ArrayRef: this method decrypts with '
Branch: 1 branch
BranchTrueFalseTotal
if ref $identities ne "ARRAY" 3 112 115
198 .'whichever entry matches, pass [$identity] rather than $identity'
199 if ref($identities) ne 'ARRAY';
200 # The counterpart, in the same form and moved in the same change. Unlike
201 # the recipients side this one closes no defect: unwrap_file_key already
202 # croaks "No matching identity found" for an empty list -- measured, not
203 # assumed -- so nothing was ever silently accepted here. It moves so that
204 # the empty list reads one way on both sides of the API.
205 112 ▶ croak 'identities must not be empty: this method decrypts with whichever '
Branch: 1 branch
BranchTrueFalseTotal
unless @$identities 3 109 112
206 .'entry matches, so with none there is nothing that could match, '
207 .'pass at least one identity'
208 unless @$identities;
209  
210 # Parse header. %opt carries max_stanzas when the caller passed one, and is
211 # empty when they did not -- the default lives in Header, in one place, so
212 # this layer never has to name a number. Validation is Header's too: unlike
213 # identities above, a bad max_stanzas cannot leak anything and has one
214 # meaning on both layers, so a second check here would only be a second
215 # message to keep in step.
216 109 my $header = Crypt::Age::Header->parse_from_fh($ifh, %opt);
217  
218 # Unwrap file key using identities
219 77 my $file_key = $header->unwrap_file_key($identities);
220  
221 # Extract nonce (first 16 bytes after header) and encrypted payload
222 70 my $nonce = Crypt::Age::Primitives->paranoid_read($ifh, 16);
223 70 ▶ croak 'end of file reached before getting nonce' if length($nonce) != 16;
Branch: 1 branch
BranchTrueFalseTotal
if length $nonce != 16 3 67 70
224  
225 # Derive payload key using nonce
226 67 my $payload_key = Crypt::Age::Primitives->derive_payload_key($file_key, $nonce);
227  
228 67 return Crypt::Age::Primitives->decrypt_payload_fh($payload_key, $ifh, $ofh);
229 }
230  
231 8 ▶sub decrypt_file {
Subroutine decrypt_file: called CC=9 SCAR=23.6
Pod documented
232 8 my ($class, %args) = @_;
233 8 ▶ my $input = $args{input} // croak "input required";
Condition50%1/2
$args{"input"} // croak("input required")
l!l
80
MC/DC0%0/1
$args{"input"} // croak("input required")
$args{"input"}
Truth table50%1/2
A
A= $args{"input"}
Aresult
11
00
234 8 ▶ my $output = $args{output} // croak "output required";
Condition50%1/2
$args{"output"} // croak("output required")
l!l
80
MC/DC0%0/1
$args{"output"} // croak("output required")
$args{"output"}
Truth table50%1/2
A
A= $args{"output"}
Aresult
11
00
235 8 ▶ my $identities = $args{identities} // croak "identities required";
Condition50%1/2
$args{"identities"} // croak("identities required")
l!l
80
MC/DC0%0/1
$args{"identities"} // croak("identities required")
$args{"identities"}
Truth table50%1/2
A
A= $args{"identities"}
Aresult
11
00
236  
237 8 ▶ open my $in_fh, '<:raw', $input
Branch: 1 branch
BranchTrueFalseTotal
open my $in_fh, "<:raw", $input or croak("Cannot open input file '${input}': $!") 8 0 8
238 or croak "Cannot open input file '$input': $!";
239 8 ▶ open my $out_fh, '>:raw', $output
Branch: 1 branch
BranchTrueFalseTotal
open my $out_fh, ">:raw", $output or croak("Cannot open output file '${output}': $!") 8 0 8
240 or croak "Cannot open output file '$output': $!";
241  
242 # As in decrypt above: forwarded only when given.
243 $class->_decrypt_fh($in_fh, $out_fh, $identities,
244 8 ▶ exists $args{max_stanzas} ? ( max_stanzas => $args{max_stanzas} ) : ());
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'max_stanzas'} ? : 1 7 8
245  
246 7 ▶ close $out_fh or croak "Cannot close output file '$output': $!";
Branch: 1 branch
BranchTrueFalseTotal
close $out_fh or croak("Cannot close output file '${output}': $!") 7 0 7
247 7 ▶ close $in_fh or croak "Cannot close input file '$input': $!";
Branch: 1 branch
BranchTrueFalseTotal
close $in_fh or croak("Cannot close input file '${input}': $!") 7 0 7
248  
249 7 return 1;
250 }
251  
252  
253 77 ▶sub decrypt_filehandle {
Subroutine decrypt_filehandle: called CC=5 SCAR=16.6
Pod documented
254 77 my ($class, %args) = @_;
255 77 ▶ my $in_fh = $args{input} // croak "input required";
Condition50%1/2
$args{"input"} // croak("input required")
l!l
770
MC/DC0%0/1
$args{"input"} // croak("input required")
$args{"input"}
Truth table50%1/2
A
A= $args{"input"}
Aresult
11
00
256 77 ▶ my $out_fh = $args{output} // croak "output required";
Condition50%1/2
$args{"output"} // croak("output required")
l!l
770
MC/DC0%0/1
$args{"output"} // croak("output required")
$args{"output"}
Truth table50%1/2
A
A= $args{"output"}
Aresult
11
00
257 77 ▶ my $identities = $args{identities} // croak "identities required";
Condition50%1/2
$args{"identities"} // croak("identities required")
l!l
770
MC/DC0%0/1
$args{"identities"} // croak("identities required")
$args{"identities"}
Truth table50%1/2
A
A= $args{"identities"}
Aresult
11
00
258  
259 # As in decrypt above: forwarded only when given.
260 $class->_decrypt_fh($in_fh, $out_fh, $identities,
261 77 ▶ exists $args{max_stanzas} ? ( max_stanzas => $args{max_stanzas} ) : ());
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'max_stanzas'} ? : 1 76 77
262  
263 21 return 1;
264 }
265  
266  
267  
268 7 1;
269  
270 __END__
« Age/Header.pm ↑ Summary Age/Primitives.pm »