Age/Keys.pm

filter: Statementstmt 100% 118 / 118 Branchbran 79.4% 27 / 34 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 17 / 17 Podpod 66.6% 8 / 12 totaltotal 94.0% 175 / 186 CC 27 SCAR 14.3
CC
27
Cov
95%
bech32_decode
8.0
_convert_bits
7.0
bech32_polymod
4.0
CRAP 4.2
« Age/Primitives.pm ↑ Summary Age/Stanza.pm »
1 package Crypt::Age::Keys;
2 # ABSTRACT: Key generation and Bech32 encoding for age encryption
3 8 our $VERSION = '0.005';
4 8 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Crypt::PK::X25519;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 # Bech32 character set
11 8 my $BECH32_CHARSET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l';
12 8 my %BECH32_CHAR_TO_VAL = map { substr($BECH32_CHARSET, $_, 1) => $_ } 0..31;
13  
14 # Human-readable parts
15 8 my $HRP_PUBLIC = 'age';
16 8 my $HRP_SECRET = 'age-secret-key-';
17  
18 54 ▶sub generate_keypair {
Subroutine generate_keypair: called CC=1 SCAR=0.0
Pod documented
19 54 my ($class) = @_;
20  
21 54 my $pk = Crypt::PK::X25519->new;
22 54 $pk->generate_key;
23  
24 54 my $secret_bytes = $pk->export_key_raw('private');
25 54 my $public_bytes = $pk->export_key_raw('public');
26  
27 54 my $public_key = $class->encode_public_key($public_bytes);
28 54 my $secret_key = $class->encode_secret_key($secret_bytes);
29  
30 54 return ($public_key, $secret_key);
31 }
32  
33  
34 57 ▶sub encode_public_key {
Subroutine encode_public_key: called CC=2 SCAR=6.9
Pod documented
35 57 my ($class, $bytes) = @_;
36 57 ▶ croak "Public key must be 32 bytes" unless length($bytes) == 32;
Branch: 1 branch
BranchTrueFalseTotal
unless length $bytes == 32 1 56 57
37 56 return $class->bech32_encode($HRP_PUBLIC, $bytes);
38 }
39  
40  
41 57 ▶sub decode_public_key {
Subroutine decode_public_key: called CC=3 SCAR=11.0
Pod documented
42 57 my ($class, $encoded) = @_;
43 57 my ($hrp, $bytes) = $class->bech32_decode($encoded);
44 # $hrp is caller material, not a value from this module: bech32_decode
45 # returns everything before the last "1" of the string it was handed. For
46 # a real key of the other kind that is only the other type prefix, but a
47 # string whose HRP carries key characters reaches here too, so nothing of
48 # it is quoted. The check is against a single constant, so naming the
49 # expected value says everything the caller needs. Written out in full at
50 # decode_secret_key, where the same value is a secret; both read the same
51 # way on purpose.
52 54 ▶ croak 'Invalid public key HRP: expected the literal '.$HRP_PUBLIC
Branch: 1 branch
BranchTrueFalseTotal
unless lc $hrp eq $HRP_PUBLIC 2 52 54
53 .' prefix, pass an age recipient rather than an identity or some '
54 .'other Bech32 string'
55 unless lc($hrp) eq $HRP_PUBLIC;
56 52 ▶ croak "Invalid public key length" unless length($bytes) == 32;
Branch: 1 branch
BranchTrueFalseTotal
unless length $bytes == 32 0 52 52
57 52 return $bytes;
58 }
59  
60  
61 55 ▶sub encode_secret_key {
Subroutine encode_secret_key: called CC=2 SCAR=7.1
Pod documented
62 55 my ($class, $bytes) = @_;
63 55 ▶ croak "Secret key must be 32 bytes" unless length($bytes) == 32;
Branch: 1 branch
BranchTrueFalseTotal
unless length $bytes == 32 0 55 55
64 55 return uc($class->bech32_encode($HRP_SECRET, $bytes));
65 }
66  
67  
68 95 ▶sub decode_secret_key {
Subroutine decode_secret_key: called CC=3 SCAR=11.0
Pod documented
69 95 my ($class, $encoded) = @_;
70 95 my ($hrp, $bytes) = $class->bech32_decode($encoded);
71 # The same croak as in decode_public_key, and the half where the quoted
72 # value could be a secret. $hrp is everything before the last "1" of the
73 # caller's string, so it is a prefix of what arrived.
74 #
75 # Reaching this croak takes a Bech32 string whose checksum verifies over
76 # the wrong HRP, which bounds it but does not make it safe. Measured on
77 # this module: a real key truncated anywhere dies earlier at "Invalid
78 # bech32: no separator", "Invalid bech32: empty data" or "Invalid bech32
79 # checksum", and one with trailing junk at "Invalid bech32 checksum" --
80 # none of those quote anything. So the string that gets here is
81 # constructed rather than mistyped; and a string constructed with the
82 # opening characters of a real identity as its HRP is precisely how those
83 # characters would be read back out of an exception, from inside this
84 # module, into a log the caller can no longer redact.
85 #
86 # As at the version-line croak in Crypt::Age::Header, the clause after the
87 # colon carries the requirement and the fix rather than a description of
88 # what arrived. The expected HRP is a constant of the format and is named.
89 94 ▶ croak 'Invalid secret key HRP: expected the literal '.$HRP_SECRET
Branch: 1 branch
BranchTrueFalseTotal
unless lc $hrp eq $HRP_SECRET 2 92 94
90 .' prefix, pass an age identity rather than a recipient or some '
91 .'other Bech32 string'
92 unless lc($hrp) eq $HRP_SECRET;
93 92 ▶ croak "Invalid secret key length" unless length($bytes) == 32;
Branch: 1 branch
BranchTrueFalseTotal
unless length $bytes == 32 0 92 92
94 92 return $bytes;
95 }
96  
97  
98 1 ▶sub public_key_from_secret {
Subroutine public_key_from_secret: called CC=1 SCAR=0.0
Pod documented
99 1 my ($class, $secret_key) = @_;
100 1 my $secret_bytes = $class->decode_secret_key($secret_key);
101 1 my $pk = Crypt::PK::X25519->new;
102 1 $pk->import_key_raw($secret_bytes, 'private');
103 1 my $public_bytes = $pk->export_key_raw('public');
104 1 return $class->encode_public_key($public_bytes);
105 }
106  
107  
108  
109 # Bech32 implementation (BIP-173)
110  
111 265 ▶sub bech32_polymod {
Subroutine bech32_polymod: called CC=4 SCAR=13.9
Pod undocumented
112 265 my ($values) = @_;
113 265 my @GEN = (0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3);
114 265 my $chk = 1;
115 265 for my $v (@$values) {
116 20777 my $b = $chk >> 25;
117 20777 $chk = (($chk & 0x1ffffff) << 5) ^ $v;
118 20777 for my $i (0..4) {
119 103885 ▶ $chk ^= (($b >> $i) & 1) ? $GEN[$i] : 0;
Branch: 1 branch
BranchTrueFalseTotal
$b >> $i & 1 ? : 47286 56599 103885
120 }
121 }
122 265 return $chk;
123 }
124  
125 265 ▶sub bech32_hrp_expand {
Subroutine bech32_hrp_expand: called CC=3 SCAR=11.0
Pod undocumented
126 265 my ($hrp) = @_;
127 265 my @result;
128 265 for my $c (split //, $hrp) {
129 2675 push @result, ord($c) >> 5;
130 }
131 265 push @result, 0;
132 265 for my $c (split //, $hrp) {
133 2675 push @result, ord($c) & 31;
134 }
135 265 return \@result;
136 }
137  
138 114 ▶sub bech32_create_checksum {
Subroutine bech32_create_checksum: called CC=2 SCAR=6.9
Pod undocumented
139 114 my ($hrp, $data) = @_;
140 114 my @values = (@{bech32_hrp_expand($hrp)}, @$data, 0, 0, 0, 0, 0, 0);
141 114 my $polymod = bech32_polymod(\@values) ^ 1;
142 114 my @checksum;
143 114 for my $i (0..5) {
144 684 push @checksum, ($polymod >> (5 * (5 - $i))) & 31;
145 }
146 114 return \@checksum;
147 }
148  
149 151 ▶sub bech32_verify_checksum {
Subroutine bech32_verify_checksum: called CC=1 SCAR=0.0
Pod undocumented
150 151 my ($hrp, $data) = @_;
151 151 return bech32_polymod([@{bech32_hrp_expand($hrp)}, @$data]) == 1;
152 }
153  
154 114 ▶sub bech32_encode {
Subroutine bech32_encode: called CC=2 SCAR=6.9
Pod documented
155 114 my ($class, $hrp, $bytes) = @_;
156  
157 # Convert 8-bit bytes to 5-bit groups
158 114 my $data = $class->_convert_bits([unpack('C*', $bytes)], 8, 5, 1);
159  
160 114 my $checksum = bech32_create_checksum($hrp, $data);
161 114 my @combined = (@$data, @$checksum);
162  
163 114 my $result = $hrp . '1';
164 114 for my $d (@combined) {
165 6560 $result .= substr($BECH32_CHARSET, $d, 1);
166 }
167  
168 114 return $result;
169 }
170  
171  
172 159 ▶sub bech32_decode {
Subroutine bech32_decode: called CC=8 SCAR=20.8
Pod documented
173 159 my ($class, $str) = @_;
174  
175 # BIP-173: "Decoders MUST NOT accept strings where some characters are
176 # uppercase and some are lowercase". The checksum is computed over the
177 # lowercase form either way, so a mixed-case string would otherwise verify
178 # and decode; both age 1.2.1 and rage 0.12.1 reject it.
179 159 ▶ croak "Invalid bech32: mixed case"
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /[a-z]/ and $str =~ /[A-Z]/ 5 154 159
Condition100%3/3
$str =~ /[a-z]/ and $str =~ /[A-Z]/
!ll&&!rl&&r
95595
MC/DC100%2/2
$str =~ /[a-z]/ and $str =~ /[A-Z]/
$str =~ /[a-z]/ $str =~ /[A-Z]/
Truth table100%3/3
A and B
A= $str =~ /[a-z]/B= $str =~ /[A-Z]/
ABresult
0X0
100
111
180 if $str =~ /[a-z]/ && $str =~ /[A-Z]/;
181  
182 # Find separator
183 154 my $sep_pos = rindex($str, '1');
184 154 ▶ croak "Invalid bech32: no separator" if $sep_pos < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $sep_pos < 1 1 153 154
185 153 ▶ croak "Invalid bech32: empty data" if $sep_pos + 1 >= length($str);
Branch: 1 branch
BranchTrueFalseTotal
if $sep_pos + 1 >= length $str 0 153 153
186  
187 153 my $hrp = substr($str, 0, $sep_pos);
188 153 my $data_part = substr($str, $sep_pos + 1);
189  
190 # Decode data part. The offending character is located, never quoted: no
191 # character of an encoded age key can reach this branch (every one of them
192 # is inside the charset by construction), so the only thing that could be
193 # copied into the exception is one byte of some other secret handed to this
194 # public method by mistake -- a passphrase, say. The offset counts from the
195 # start of $str rather than from the start of the data part, because $str
196 # is what the caller holds: substr($str, $N, 1) then lands on the character
197 # without the caller re-deriving where the separator was. Lowercasing one
198 # character at a time keeps that offset exact even where lc() of a single
199 # character is not a single character.
200 153 my @data;
201 153 for my $i (0 .. length($data_part) - 1) {
202 8611 my $c = lc(substr($data_part, $i, 1));
203 croak "Invalid bech32 character at offset ".($sep_pos + 1 + $i)
204 .": expected a character of the Bech32 charset"
205 8611 ▶ unless exists $BECH32_CHAR_TO_VAL{$c};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $BECH32_CHAR_TO_VAL{$c} 2 8609 8611
206 8609 push @data, $BECH32_CHAR_TO_VAL{$c};
207 }
208  
209 151 ▶ croak "Invalid bech32 checksum"
Branch: 1 branch
BranchTrueFalseTotal
unless bech32_verify_checksum(lc $hrp, \@data) 0 151 151
210 unless bech32_verify_checksum(lc($hrp), \@data);
211  
212 # Remove checksum (last 6 values)
213 151 splice(@data, -6);
214  
215 # Convert 5-bit groups back to 8-bit bytes
216 151 my $bytes = $class->_convert_bits(\@data, 5, 8, 0);
217  
218 151 return ($hrp, pack('C*', @$bytes));
219 }
220  
221  
222 265 ▶sub _convert_bits {
Subroutine _convert_bits: called CC=7 SCAR=19.5
223 265 my ($class, $data, $from_bits, $to_bits, $pad) = @_;
224  
225 265 my $acc = 0;
226 265 my $bits = 0;
227 265 my @result;
228 265 my $maxv = (1 << $to_bits) - 1;
229  
230 265 for my $v (@$data) {
231 11312 $acc = ($acc << $from_bits) | $v;
232 11312 $bits += $from_bits;
233 11312 ▶ while ($bits >= $to_bits) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits >= $to_bits) 10499 11312 21811
234 10499 $bits -= $to_bits;
235 10499 push @result, ($acc >> $bits) & $maxv;
236 }
237 }
238  
239 265 ▶ if ($pad) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pad) { } 114 151 265
240 114 ▶ if ($bits > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits > 0) 113 1 114
241 113 push @result, ($acc << ($to_bits - $bits)) & $maxv;
242 }
243 } else {
244 151 ▶ croak "Invalid padding" if $bits >= $from_bits;
Branch: 1 branch
BranchTrueFalseTotal
if $bits >= $from_bits 0 151 151
245 151 ▶ croak "Non-zero padding" if (($acc << ($to_bits - $bits)) & $maxv);
Branch: 1 branch
BranchTrueFalseTotal
if $acc << $to_bits - $bits & $maxv 0 151 151
246 }
247  
248 265 return \@result;
249 }
250  
251  
252 8 1;
253  
254 __END__
« Age/Primitives.pm ↑ Summary Age/Stanza.pm »