Encryption.pm

filter: Statementstmt 26.3% 34 / 129 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 52.3% 11 / 21 Podpod 100% 5 / 5 totaltotal 22.2% 50 / 225 CC 32 SCAR 34.9
CC
32
Cov
18%
get_conversation_key
72.0
_unpad
72.0
_decode_payload
42.0
CRAP 32.7
« Badge.pm ↑ Summary MediaAttachment.pm »
1 package Net::Nostr::Encryption;
2  
3 2 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Crypt::PK::ECC;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Crypt::KeyDerivation qw(hkdf_extract);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Crypt::Stream::ChaCha;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Crypt::Mac::HMAC qw(hmac);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Crypt::PRNG qw(random_bytes);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Encode qw(encode decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use POSIX qw(floor);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 VERSION_2 => 2,
17 MIN_PLAINTEXT_LEN => 1,
18 MAX_PLAINTEXT_LEN => 4294967295,
19 EXTENDED_PREFIX_THRESHOLD => 65536,
20 MIN_PAYLOAD_LEN => 132,
21 MIN_RAW_LEN => 99,
22 2 };
23  
24 0 ▶sub get_conversation_key {
Subroutine get_conversation_key: not called CC=8 SCAR=42.8
Pod documented
25 0 my ($class, $privkey_hex, $pubkey_hex) = @_;
26  
27 0 ▶ croak "privkey_hex must be 64-char lowercase hex"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $privkey_hex and $privkey_hex =~ /\A[0-9a-f]{64}\z/ 0 0 0
Condition0%0/3
defined $privkey_hex and $privkey_hex =~ /\A[0-9a-f]{64}\z/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $privkey_hex and $privkey_hex =~ /\A[0-9a-f]{64}\z/
defined $privkey_hex $privkey_hex =~ /\A[0-9a-f]{64}\z/
Truth table0%0/3
A and B
A= defined $privkey_hexB= $privkey_hex =~ /\A[0-9a-f]{64}\z/
ABresult
0X0
100
111
28 unless defined $privkey_hex && $privkey_hex =~ /\A[0-9a-f]{64}\z/;
29 0 ▶ croak "pubkey_hex must be 64-char lowercase hex"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pubkey_hex and $pubkey_hex =~ /\A[0-9a-f]{64}\z/ 0 0 0
Condition0%0/3
defined $pubkey_hex and $pubkey_hex =~ /\A[0-9a-f]{64}\z/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $pubkey_hex and $pubkey_hex =~ /\A[0-9a-f]{64}\z/
defined $pubkey_hex $pubkey_hex =~ /\A[0-9a-f]{64}\z/
Truth table0%0/3
A and B
A= defined $pubkey_hexB= $pubkey_hex =~ /\A[0-9a-f]{64}\z/
ABresult
0X0
100
111
30 unless defined $pubkey_hex && $pubkey_hex =~ /\A[0-9a-f]{64}\z/;
31  
32 0 my $priv_raw = pack('H*', $privkey_hex);
33 0 my $pub_raw = pack('H*', $pubkey_hex);
34  
35 # Import private key
36 0 my $priv = Crypt::PK::ECC->new;
37 0 eval { $priv->import_key_raw($priv_raw, 'secp256k1') };
38 0 ▶ croak "invalid private key" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
39 0 ▶ croak "invalid private key" unless $priv->is_private;
Branch: 1 branch
BranchTrueFalseTotal
unless $priv->is_private 0 0 0
40  
41 # Import public key as compressed (02 prefix + x coordinate)
42 0 my $pub = Crypt::PK::ECC->new;
43 0 eval { $pub->import_key_raw("\x02" . $pub_raw, 'secp256k1') };
44 0 ▶ croak "invalid public key" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
45  
46 # ECDH - produces raw x coordinate of shared point
47 0 my $shared_x = $priv->shared_secret($pub);
48  
49 # HKDF-extract with salt='nip44-v2'
50 0 return hkdf_extract($shared_x, 'nip44-v2', 'SHA256');
51 }
52  
53 0 ▶sub get_message_keys {
Subroutine get_message_keys: not called CC=3 SCAR=24.8
Pod documented
54 0 my ($class, $conversation_key, $nonce) = @_;
55 0 ▶ croak "invalid conversation_key length" unless length($conversation_key) == 32;
Branch: 1 branch
BranchTrueFalseTotal
unless length $conversation_key == 32 0 0 0
56 0 ▶ croak "invalid nonce length" unless length($nonce) == 32;
Branch: 1 branch
BranchTrueFalseTotal
unless length $nonce == 32 0 0 0
57  
58 0 my $keys = _hkdf_expand($conversation_key, $nonce, 76);
59 return (
60 0 substr($keys, 0, 32), # chacha_key
61 substr($keys, 32, 12), # chacha_nonce
62 substr($keys, 44, 32), # hmac_key
63 );
64 }
65  
66 0 ▶sub calc_padded_len {
Subroutine calc_padded_len: not called CC=3 SCAR=24.8
Pod documented
67 0 my ($class, $unpadded_len) = @_;
68 0 ▶ return 32 if $unpadded_len <= 32;
Branch: 1 branch
BranchTrueFalseTotal
if $unpadded_len <= 32 0 0 0
69  
70 0 my $next_power = 1 << (floor(_log2($unpadded_len - 1)) + 1);
71 0 ▶ my $chunk = $next_power <= 256 ? 32 : $next_power / 8;
Branch: 1 branch
BranchTrueFalseTotal
$next_power <= 256 ? : 0 0 0
72 0 return $chunk * (floor(($unpadded_len - 1) / $chunk) + 1);
73 }
74  
75 0 ▶sub encrypt {
Subroutine encrypt: not called CC=5 SCAR=34.0
Pod documented
76 0 my ($class, $plaintext, $conversation_key, $nonce) = @_;
77  
78 0 my $unpadded = encode('UTF-8', $plaintext);
79 0 my $unpadded_len = length($unpadded);
80 0 ▶ croak "invalid plaintext length" if $unpadded_len < MIN_PLAINTEXT_LEN;
Branch: 1 branch
BranchTrueFalseTotal
if $unpadded_len < 1 0 0 0
81 0 ▶ croak "invalid plaintext length" if $unpadded_len > MAX_PLAINTEXT_LEN;
Branch: 1 branch
BranchTrueFalseTotal
if $unpadded_len > 4294967295 0 0 0
82  
83 0 ▶ $nonce //= random_bytes(32);
Condition0%0/3
$nonce //= &random_bytes(32)
l!l&&r!l&&!r
000
MC/DC0%0/2
$nonce //= &random_bytes(32)
$nonce &random_bytes(32)
Truth table0%0/3
A //= B
A= $nonceB= &random_bytes(32)
ABresult
1X1
011
000
84  
85 0 my ($chacha_key, $chacha_nonce, $hmac_key) =
86 $class->get_message_keys($conversation_key, $nonce);
87  
88 # Pad
89 0 my $padded_len = $class->calc_padded_len($unpadded_len);
90 0 ▶ my $prefix = $unpadded_len >= EXTENDED_PREFIX_THRESHOLD
Branch: 1 branch
BranchTrueFalseTotal
$unpadded_len >= 65536 ? : 0 0 0
91 ? pack('nN', 0, $unpadded_len)
92 : pack('n', $unpadded_len);
93 0 my $padded = $prefix . $unpadded . ("\x00" x ($padded_len - $unpadded_len));
94  
95 # Encrypt with ChaCha20
96 0 my $chacha = Crypt::Stream::ChaCha->new($chacha_key, $chacha_nonce);
97 0 my $ciphertext = $chacha->crypt($padded);
98  
99 # HMAC-SHA256 with AAD (nonce)
100 0 my $mac = hmac('SHA256', $hmac_key, $nonce . $ciphertext);
101  
102 # Base64 encode: version(1) + nonce(32) + ciphertext + mac(32)
103 0 return MIME::Base64::encode_base64(chr(VERSION_2) . $nonce . $ciphertext . $mac, '');
104 }
105  
106 0 ▶sub decrypt {
Subroutine decrypt: not called CC=2 SCAR=17.9
Pod documented
107 0 my ($class, $payload, $conversation_key) = @_;
108  
109 0 my ($nonce, $ciphertext, $mac) = $class->_decode_payload($payload);
110  
111 0 my ($chacha_key, $chacha_nonce, $hmac_key) =
112 $class->get_message_keys($conversation_key, $nonce);
113  
114 # Verify MAC
115 0 my $calculated_mac = hmac('SHA256', $hmac_key, $nonce . $ciphertext);
116 0 ▶ croak "invalid MAC" unless _ct_eq($calculated_mac, $mac);
Branch: 1 branch
BranchTrueFalseTotal
unless _ct_eq($calculated_mac, $mac) 0 0 0
117  
118 # Decrypt
119 0 my $chacha = Crypt::Stream::ChaCha->new($chacha_key, $chacha_nonce);
120 0 my $padded = $chacha->crypt($ciphertext);
121  
122 # Unpad
123 0 return $class->_unpad($padded);
124 }
125  
126 0 ▶sub _decode_payload {
Subroutine _decode_payload: not called CC=6 SCAR=37.4
127 0 my ($class, $payload) = @_;
128 0 my $plen = length($payload);
129  
130 0 ▶ croak "unknown version" if $plen == 0 || substr($payload, 0, 1) eq '#';
Branch: 1 branch
BranchTrueFalseTotal
if $plen == 0 or substr($payload, 0, 1) eq "#" 0 0 0
Condition0%0/3
$plen == 0 or substr($payload, 0, 1) eq "#"
l!l&&r!l&&!r
000
MC/DC0%0/2
$plen == 0 or substr($payload, 0, 1) eq "#"
$plen == 0 substr($payload, 0, 1) eq "#"
Truth table0%0/3
A or B
A= $plen == 0B= substr($payload, 0, 1) eq "#"
ABresult
1X1
011
000
131 0 ▶ croak "invalid payload size" if $plen < MIN_PAYLOAD_LEN;
Branch: 1 branch
BranchTrueFalseTotal
if $plen < 132 0 0 0
132  
133 0 my $data = MIME::Base64::decode_base64($payload);
134 0 my $dlen = length($data);
135 0 ▶ croak "invalid data size" if $dlen < MIN_RAW_LEN;
Branch: 1 branch
BranchTrueFalseTotal
if $dlen < 99 0 0 0
136  
137 0 my $version = ord(substr($data, 0, 1));
138 0 ▶ croak "unknown version $version" unless $version == VERSION_2;
Branch: 1 branch
BranchTrueFalseTotal
unless $version == 2 0 0 0
139  
140 0 my $nonce = substr($data, 1, 32);
141 0 my $ciphertext = substr($data, 33, $dlen - 65);
142 0 my $mac = substr($data, $dlen - 32);
143  
144 0 return ($nonce, $ciphertext, $mac);
145 }
146  
147 0 ▶sub _unpad {
Subroutine _unpad: not called CC=8 SCAR=42.8
148 0 my ($class, $padded) = @_;
149 0 ▶ croak "invalid padding" if length($padded) < 2;
Branch: 1 branch
BranchTrueFalseTotal
if length $padded < 2 0 0 0
150  
151 0 my $first_two = unpack('n', substr($padded, 0, 2));
152 0 my ($unpadded_len, $prefix_len);
153 0 ▶ if ($first_two == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_two == 0) { } 0 0 0
154 0 ▶ croak "invalid padding" if length($padded) < 6;
Branch: 1 branch
BranchTrueFalseTotal
if length $padded < 6 0 0 0
155 0 $unpadded_len = unpack('N', substr($padded, 2, 4));
156 0 ▶ croak "invalid padding" if $unpadded_len < EXTENDED_PREFIX_THRESHOLD;
Branch: 1 branch
BranchTrueFalseTotal
if $unpadded_len < 65536 0 0 0
157 0 $prefix_len = 6;
158 } else {
159 0 $unpadded_len = $first_two;
160 0 $prefix_len = 2;
161 }
162  
163 0 ▶ croak "invalid padding" if $unpadded_len == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $unpadded_len == 0 0 0 0
164  
165 0 my $unpadded = substr($padded, $prefix_len, $unpadded_len);
166 0 ▶ croak "invalid padding" unless length($unpadded) == $unpadded_len;
Branch: 1 branch
BranchTrueFalseTotal
unless length $unpadded == $unpadded_len 0 0 0
167  
168 0 my $expected_padded_len = $prefix_len + $class->calc_padded_len($unpadded_len);
169 0 ▶ croak "invalid padding" unless length($padded) == $expected_padded_len;
Branch: 1 branch
BranchTrueFalseTotal
unless length $padded == $expected_padded_len 0 0 0
170  
171 0 return decode('UTF-8', $unpadded);
172 }
173  
174 # Constant-time equality comparison
175 0 ▶sub _ct_eq {
Subroutine _ct_eq: not called CC=3 SCAR=24.8
176 0 my ($a, $b) = @_;
177 0 ▶ return 0 unless length($a) == length($b);
Branch: 1 branch
BranchTrueFalseTotal
unless length $a == length $b 0 0 0
178 0 my $result = 0;
179 0 for my $i (0 .. length($a) - 1) {
180 0 $result |= ord(substr($a, $i, 1)) ^ ord(substr($b, $i, 1));
181 }
182 0 return $result == 0;
183 }
184  
185 # HKDF-Expand with SHA256 (manual implementation since CryptX's hkdf_expand
186 # returns empty for our inputs)
187 0 ▶sub _hkdf_expand {
Subroutine _hkdf_expand: not called CC=2 SCAR=17.9
188 0 my ($prk, $info, $len) = @_;
189 0 my $hash_len = 32;
190 0 my $n = int(($len + $hash_len - 1) / $hash_len);
191 0 my $okm = '';
192 0 my $t = '';
193 0 for my $i (1 .. $n) {
194 0 $t = hmac('SHA256', $prk, $t . $info . chr($i));
195 0 $okm .= $t;
196 }
197 0 return substr($okm, 0, $len);
198 }
199  
200 0 ▶sub _log2 {
Subroutine _log2: not called CC=1 SCAR=6.9
201 0 my ($n) = @_;
202 0 return log($n) / log(2);
203 }
204  
205 2 1;
206  
207 __END__
« Badge.pm ↑ Summary MediaAttachment.pm »