KeyEncrypt.pm

filter: Statementstmt 100% 155 / 155 Branchbran 84.2% 32 / 38 Conditioncond 96.1% 25 / 26 MC/DCmcdc 86.6% 13 / 15 Subroutinesub 100% 21 / 21 Podpod 100% 2 / 2 totaltotal 96.5% 248 / 257 CC 33 SCAR 18.9
CC
33
Cov
97%
encrypt_private_key
12.0
decrypt_private_key
10.0
_nostr_decode_bech32
8.5
CRAP 6.6
« Mention.pm ↑ Summary Bech32.pm »
1 package Net::Nostr::KeyEncrypt;
2  
3 1 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Net::Nostr::_ConstructorArgs ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Crypt::AuthEnc::ChaCha20Poly1305;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Crypt::PRNG qw(random_bytes);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Crypt::ScryptKDF qw(scrypt_raw);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Unicode::Normalize qw(NFKC);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Bitcoin::Crypto::Bech32 qw(encode_bech32 translate_5to8 translate_8to5);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 our @EXPORT_OK = qw(
17 encrypt_private_key
18 decrypt_private_key
19 );
20  
21 1 my $HEX64 = qr/\A[0-9a-f]{64}\z/;
22 1 my $VERSION_BYTE = 0x02;
23 1 my $MAX_BECH32_LENGTH = 5000;
24  
25 # Bech32 decode without BIP-173's 90-char limit, same as in Bech32.pm
26 {
27 1 my @ALPHABET = qw(
28 q p z r y 9 x 8 g f 2 t v d w 0
29 s 3 j n 5 4 k h c e 6 m u a 7 l
30 );
31 1 my %ALPHABET_MAP = map { $ALPHABET[$_] => $_ } 0 .. $#ALPHABET;
32 1 my $CHARS = join '', @ALPHABET;
33  
34 19 ▶ sub _polymod {
Subroutine _polymod: called CC=4 SCAR=13.9
35 19 my ($values) = @_;
36 19 my @C = (0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3);
37 19 my $chk = 1;
38 19 for my $val (@$values) {
39 3019 my $b = ($chk >> 25);
40 3019 $chk = ($chk & 0x1ffffff) << 5 ^ $val;
41 3019 ▶ for (0 .. 4) { $chk ^= (($b >> $_) & 1) ? $C[$_] : 0 }
Branch: 1 branch
BranchTrueFalseTotal
$b >> $_ & 1 ? : 7368 7727 15095
42 }
43 19 return $chk;
44 }
45  
46 19 ▶ sub _hrp_expand {
Subroutine _hrp_expand: called CC=1 SCAR=0.0
47 19 my @hrp = split //, shift;
48 19 return [map({ ord($_) >> 5 } @hrp), 0, map({ ord($_) & 31 } @hrp)];
49 }
50  
51 20 ▶ sub _nostr_decode_bech32 {
Subroutine _nostr_decode_bech32: called CC=8 SCAR=21.4
52 20 my ($str) = @_;
53 20 ▶ $str = lc $str if uc $str eq $str;
Branch: 1 branch
BranchTrueFalseTotal
if uc $str eq $str 0 20 20
54 20 ▶ croak "bech32 string exceeds 5000 character limit" if length($str) > $MAX_BECH32_LENGTH;
Branch: 1 branch
BranchTrueFalseTotal
if length $str > $MAX_BECH32_LENGTH 0 20 20
55 20 ▶ croak "bech32 string contains mixed case" if lc($str) ne $str;
Branch: 1 branch
BranchTrueFalseTotal
if lc $str ne $str 0 20 20
56  
57 20 my @parts = split /1/, $str;
58 20 ▶ croak "bech32 separator missing" if @parts < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @parts < 2 0 20 20
59 20 my $data_part = pop @parts;
60 20 my $hrp = join '1', @parts;
61  
62 20 ▶ croak "invalid bech32 data characters" if $data_part !~ /\A[$CHARS]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $data_part =~ /\A[$CHARS]+\z/ 1 19 20
63 19 ▶ croak "bech32 data part too short" if length($data_part) < 6;
Branch: 1 branch
BranchTrueFalseTotal
if length $data_part < 6 0 19 19
64  
65 19 my @data_values = map { $ALPHABET_MAP{$_} } split //, $data_part;
66 19 my $check_values = [@{_hrp_expand($hrp)}, @data_values];
67 19 ▶ croak "invalid bech32 checksum" unless _polymod($check_values) == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless _polymod($check_values) == 1 0 19 19
68  
69 19 my @payload = @data_values[0 .. $#data_values - 6];
70 19 return ($hrp, \@payload);
71 }
72 }
73  
74 1 ▶sub encrypt_private_key {
Subroutine encrypt_private_key: called CC=12 SCAR=24.8
Pod documented
75 24 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
76 24 ▶ my $privkey_hex = $args{privkey_hex} // croak "privkey_hex is required";
Condition100%2/2
$args{"privkey_hex"} // croak("privkey_hex is required")
l!l
231
MC/DC100%1/1
$args{"privkey_hex"} // croak("privkey_hex is required")
$args{"privkey_hex"}
Truth table100%2/2
A
A= $args{"privkey_hex"}
Aresult
11
00
77 23 ▶ my $password = $args{password} // croak "password is required";
Condition100%2/2
$args{"password"} // croak("password is required")
l!l
221
MC/DC100%1/1
$args{"password"} // croak("password is required")
$args{"password"}
Truth table100%2/2
A
A= $args{"password"}
Aresult
11
00
78 22 ▶ my $log_n = $args{log_n} // croak "log_n is required";
Condition100%2/2
$args{"log_n"} // croak("log_n is required")
l!l
211
MC/DC100%1/1
$args{"log_n"} // croak("log_n is required")
$args{"log_n"}
Truth table100%2/2
A
A= $args{"log_n"}
Aresult
11
00
79 21 ▶ my $key_security = $args{key_security} // 0x02;
Condition100%2/2
$args{"key_security"} // 2
l!l
516
MC/DC100%1/1
$args{"key_security"} // 2
$args{"key_security"}
Truth table100%2/2
A
A= $args{"key_security"}
Aresult
11
00
80  
81 21 ▶ croak "privkey_hex must be 64-char lowercase hex" unless $privkey_hex =~ $HEX64;
Branch: 1 branch
BranchTrueFalseTotal
unless $privkey_hex =~ /$HEX64/ 2 19 21
82 19 ▶ croak "password is required" unless length $password;
Branch: 1 branch
BranchTrueFalseTotal
unless length $password 1 18 19
83 18 ▶ croak "log_n must be between 1 and 22" unless $log_n >= 1 && $log_n <= 22;
Branch: 1 branch
BranchTrueFalseTotal
unless $log_n >= 1 and $log_n <= 22 2 16 18
Condition100%3/3
$log_n >= 1 and $log_n <= 22
!ll&&!rl&&r
1116
MC/DC100%2/2
$log_n >= 1 and $log_n <= 22
$log_n >= 1 $log_n <= 22
Truth table100%3/3
A and B
A= $log_n >= 1B= $log_n <= 22
ABresult
0X0
100
111
84 16 ▶ croak "key_security must be 0x00, 0x01, or 0x02"
Branch: 1 branch
BranchTrueFalseTotal
unless $key_security == 0 or $key_security == 1 or $key_security == 2 1 15 16
Condition100%6/6
$key_security == 0 or $key_security == 1 or $key_security == 2
l!l&&r!l&&!r
4111
$key_security == 0 or $key_security == 1
l!l&&r!l&&!r
2212
MC/DC100%3/3
$key_security == 0 or $key_security == 1 or $key_security == 2
$key_security == 0 $key_security == 1 $key_security == 2
Truth table100%4/4
A or B or C
A= $key_security == 0B= $key_security == 1C= $key_security == 2
ABCresult
1XX1
01X1
0011
0000
85 unless $key_security == 0x00 || $key_security == 0x01 || $key_security == 0x02;
86  
87 15 my $privkey_raw = pack('H*', $privkey_hex);
88 15 $password = encode('UTF-8', NFKC($password));
89  
90 15 my $salt = random_bytes(16);
91 15 my $nonce = random_bytes(24);
92  
93 15 my $sym_key = scrypt_raw($password, $salt, 2**$log_n, 8, 1, 32);
94  
95 15 my $aad = chr($key_security);
96 15 my $ciphertext_and_tag = _xchacha20poly1305_encrypt($sym_key, $nonce, $aad, $privkey_raw);
97  
98 15 my $raw = chr($VERSION_BYTE) . chr($log_n) . $salt . $nonce . $aad . $ciphertext_and_tag;
99  
100 15 my $data5 = translate_8to5($raw);
101 15 return encode_bech32('ncryptsec', $data5, 'bech32');
102 }
103  
104 21 ▶sub decrypt_private_key {
Subroutine decrypt_private_key: called CC=10 SCAR=23.0
Pod documented
105 21 my ($ncryptsec, $password, %opts) = @_;
106  
107 21 ▶ croak "ncryptsec string is required" unless defined $ncryptsec && length $ncryptsec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ncryptsec and length $ncryptsec 2 19 21
Condition100%3/3
defined $ncryptsec and length $ncryptsec
!ll&&!rl&&r
1119
MC/DC100%2/2
defined $ncryptsec and length $ncryptsec
defined $ncryptsec length $ncryptsec
Truth table100%3/3
A and B
A= defined $ncryptsecB= length $ncryptsec
ABresult
0X0
100
111
108 19 ▶ croak "password is required" unless defined $password && length $password;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $password and length $password 2 17 19
Condition100%3/3
defined $password and length $password
!ll&&!rl&&r
1117
MC/DC100%2/2
defined $password and length $password
defined $password length $password
Truth table100%3/3
A and B
A= defined $passwordB= length $password
ABresult
0X0
100
111
109  
110 17 my ($hrp, $data5) = _nostr_decode_bech32($ncryptsec);
111 16 ▶ croak "expected ncryptsec prefix, got $hrp" unless $hrp eq 'ncryptsec';
Branch: 1 branch
BranchTrueFalseTotal
unless $hrp eq "ncryptsec" 1 15 16
112  
113 15 my $raw = translate_5to8($data5);
114 15 ▶ croak "invalid payload size" unless length($raw) == 91;
Branch: 1 branch
BranchTrueFalseTotal
unless length $raw == 91 1 14 15
115  
116 14 my $version = ord(substr($raw, 0, 1));
117 14 ▶ croak "unknown version $version (expected $VERSION_BYTE)" unless $version == $VERSION_BYTE;
Branch: 1 branch
BranchTrueFalseTotal
unless $version == $VERSION_BYTE 1 13 14
118  
119 13 my $log_n = ord(substr($raw, 1, 1));
120 13 my $salt = substr($raw, 2, 16);
121 13 my $nonce = substr($raw, 18, 24);
122 13 my $aad = substr($raw, 42, 1);
123 13 my $ct_and_tag = substr($raw, 43);
124  
125 13 $password = encode('UTF-8', NFKC($password));
126  
127 # Use log_n from opts if provided, otherwise use embedded value
128 13 ▶ my $effective_log_n = $opts{log_n} // $log_n;
Condition66%2/3
$opts{"log_n"} // $log_n
l!l&&r!l&&!r
1120
MC/DC0%0/2
$opts{"log_n"} // $log_n
$opts{"log_n"} $log_n
Truth table66%2/3
A // B
A= $opts{"log_n"}B= $log_n
ABresult
1X1
011
000
129 13 my $sym_key = scrypt_raw($password, $salt, 2**$effective_log_n, 8, 1, 32);
130  
131 13 my $privkey_raw = eval { _xchacha20poly1305_decrypt($sym_key, $nonce, $aad, $ct_and_tag) };
132 13 ▶ croak "decryption failed: wrong password or corrupted data" unless defined $privkey_raw;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $privkey_raw 2 11 13
133  
134 11 return unpack('H*', $privkey_raw);
135 }
136  
137 # XChaCha20-Poly1305 built from HChaCha20 + IETF ChaCha20-Poly1305.
138 # HChaCha20 derives a 32-byte subkey from the first 16 bytes of the 24-byte
139 # nonce. The remaining 8 bytes (prepended with 4 zero bytes) become the
140 # 12-byte IETF nonce.
141  
142 15 ▶sub _xchacha20poly1305_encrypt {
Subroutine _xchacha20poly1305_encrypt: called CC=1 SCAR=0.0
143 15 my ($key, $nonce, $aad, $plaintext) = @_;
144 15 my ($subkey, $ietf_nonce) = _xchacha_derive($key, $nonce);
145  
146 15 my $ae = Crypt::AuthEnc::ChaCha20Poly1305->new($subkey, $ietf_nonce);
147 15 $ae->adata_add($aad);
148 15 my $ct = $ae->encrypt_add($plaintext);
149 15 my $tag = $ae->encrypt_done();
150 15 return $ct . $tag;
151 }
152  
153 13 ▶sub _xchacha20poly1305_decrypt {
Subroutine _xchacha20poly1305_decrypt: called CC=2 SCAR=6.9
154 13 my ($key, $nonce, $aad, $ct_and_tag) = @_;
155 13 my ($subkey, $ietf_nonce) = _xchacha_derive($key, $nonce);
156  
157 13 my $ct = substr($ct_and_tag, 0, length($ct_and_tag) - 16);
158 13 my $tag = substr($ct_and_tag, -16);
159  
160 13 my $ae = Crypt::AuthEnc::ChaCha20Poly1305->new($subkey, $ietf_nonce);
161 13 $ae->adata_add($aad);
162 13 my $pt = $ae->decrypt_add($ct);
163 13 my $result_tag = $ae->decrypt_done();
164 13 ▶ croak "MAC mismatch" unless $result_tag eq $tag;
Branch: 1 branch
BranchTrueFalseTotal
unless $result_tag eq $tag 2 11 13
165 11 return $pt;
166 }
167  
168 28 ▶sub _xchacha_derive {
Subroutine _xchacha_derive: called CC=1 SCAR=0.0
169 28 my ($key, $nonce) = @_;
170 28 my $subkey = _hchacha20($key, substr($nonce, 0, 16));
171 28 my $ietf_nonce = "\x00\x00\x00\x00" . substr($nonce, 16, 8);
172 28 return ($subkey, $ietf_nonce);
173 }
174  
175 # HChaCha20: 20-round ChaCha core producing a 32-byte subkey from a
176 # 32-byte key and 16-byte nonce.
177 28 ▶sub _hchacha20 {
Subroutine _hchacha20: called CC=2 SCAR=6.9
178 28 my ($key, $nonce) = @_;
179 28 my @s = unpack('V*', "expand 32-byte k" . $key . $nonce);
180  
181 28 for (1 .. 10) {
182 280 @s[0,4,8,12] = _quarter_round(@s[0,4,8,12]);
183 280 @s[1,5,9,13] = _quarter_round(@s[1,5,9,13]);
184 280 @s[2,6,10,14] = _quarter_round(@s[2,6,10,14]);
185 280 @s[3,7,11,15] = _quarter_round(@s[3,7,11,15]);
186 280 @s[0,5,10,15] = _quarter_round(@s[0,5,10,15]);
187 280 @s[1,6,11,12] = _quarter_round(@s[1,6,11,12]);
188 280 @s[2,7,8,13] = _quarter_round(@s[2,7,8,13]);
189 280 @s[3,4,9,14] = _quarter_round(@s[3,4,9,14]);
190 }
191  
192 28 return pack('V4V4', @s[0..3], @s[12..15]);
193 }
194  
195 2240 ▶sub _quarter_round {
Subroutine _quarter_round: called CC=1 SCAR=0.0
196 2240 my ($a, $b, $c, $d) = @_;
197 2240 $a = ($a + $b) & 0xffffffff; $d = _rotl32($d ^ $a, 16);
198 2240 $c = ($c + $d) & 0xffffffff; $b = _rotl32($b ^ $c, 12);
199 2240 $a = ($a + $b) & 0xffffffff; $d = _rotl32($d ^ $a, 8);
200 2240 $c = ($c + $d) & 0xffffffff; $b = _rotl32($b ^ $c, 7);
201 2240 return ($a, $b, $c, $d);
202 }
203  
204 8960 ▶sub _rotl32 {
Subroutine _rotl32: called CC=1 SCAR=0.0
205 8960 my ($v, $n) = @_;
206 8960 return (($v << $n) | (($v >> (32 - $n)) & ((1 << $n) - 1))) & 0xffffffff;
207 }
208  
209 1 1;
210  
211 __END__
« Mention.pm ↑ Summary Bech32.pm »