Crypto/Helpers.pm

filter: Statementstmt 89.4% 102 / 114 Branchbran 70.6% 41 / 58 Conditioncond 45.2% 19 / 42 MC/DCmcdc 14.2% 3 / 21 Subroutinesub 95.4% 21 / 22 Podpod 0.0% 0 / 12 totaltotal 69.1% 186 / 269 CC 45 SCAR 23.4
CC
45
Cov
76%
check_strict_der_signature
21.8
standard_push
17.2
check_strict_public_key
8.1
CRAP 10.3
« Crypto/Transaction.pm ↑ Summary Crypto/Transaction/Digest.pm »
1 package Bitcoin::Crypto::Helpers;
2 71 $Bitcoin::Crypto::Helpers::VERSION = '4.005';
3 71 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 71 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 71 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 71 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 71 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 71 ▶use Bitcoin::Secp256k1;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 71 ▶use Bitcoin::Crypto::Constants qw(USE_BIGINTS);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 71 ▶use Bitcoin::Crypto::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 71 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=8.5
14 71 require Math::BigInt;
15  
16 # Version 1.6003 of optional GMP is required for the from_bytes / to_bytes implementations
17 0 ▶ if (eval { require Math::BigInt::GMP; Math::BigInt::GMP->VERSION('1.6003'); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Math::BigInt::GMP; 'Math::BigInt::GMP'->VERSION('1.6003'); 1 } }) { } 0 71 71
18 0 Math::BigInt->import(try => 'GMP,LTM');
19 }
20 else {
21 71 Math::BigInt->import(try => 'LTM');
22 }
23 }
24  
25 71 our @EXPORT_OK = qw(
26 pad_hex
27 ensure_length
28 encode_64bit
29 decode_64bit
30 carp_once
31 parse_formatdesc
32 ecc
33 standard_push
34 check_strict_public_key
35 check_strict_der_signature
36 make_strict_der_signature
37 die_no_trace
38 );
39  
40 71 our @CARP_NOT;
41 71 my %warned;
42  
43 sub carp_once
44 0 ▶{
Subroutine carp_once: not called CC=2 SCAR=17.9
Pod undocumented
45 0 my ($msg) = @_;
46  
47 0 ▶ return if $warned{$msg};
Branch: 1 branch
BranchTrueFalseTotal
if $warned{$msg} 0 0 0
48 0 $warned{$msg} = 1;
49 0 local @CARP_NOT = ((caller)[0]);
50 0 carp($msg);
51 }
52  
53 sub pad_hex
54 412 ▶{
Subroutine pad_hex: called CC=1 SCAR=0.0
Pod undocumented
55 412 my ($hex) = @_;
56 412 $hex =~ s/\A0x//;
57 412 $hex =~ tr/0-9a-fA-F//cd;
58 412 return '0' x (length($hex) % 2) . $hex;
59 }
60  
61 sub ensure_length
62 2369 ▶{
Subroutine ensure_length: called CC=2 SCAR=6.9
Pod undocumented
63 2369 my ($packed, $bytelen) = @_;
64 2369 my $missing = $bytelen - length $packed;
65  
66 2369 ▶ Bitcoin::Crypto::Exception->raise(
Branch: 1 branch
BranchTrueFalseTotal
if $missing < 0 1 2368 2369
67 "packed string exceeds maximum number of bytes allowed ($bytelen)"
68 ) if $missing < 0;
69  
70 2368 return pack("x$missing") . $packed;
71 }
72  
73 sub encode_64bit
74 1111 ▶{
Subroutine encode_64bit: called CC=1 SCAR=0.0
Pod undocumented
75 1111 my $value = shift;
76  
77 1111 if (USE_BIGINTS) {
78 return scalar reverse ensure_length $value->as_bytes, 8;
79 }
80 else {
81 1111 return pack 'VV', $value & 0xffffffff, $value >> 32;
82 }
83 }
84  
85 sub decode_64bit
86 1016 ▶{
Subroutine decode_64bit: called CC=1 SCAR=0.0
Pod undocumented
87 1016 my $bytes = shift;
88  
89 1016 if (USE_BIGINTS) {
90 return Math::BigInt->from_bytes(scalar reverse $bytes);
91 }
92 else {
93 1016 my ($lower, $upper) = unpack 'VV', $bytes;
94 1016 return ($upper << 32) + $lower;
95 }
96 }
97  
98 # default operation is to decode based on formatdesc
99 # passing $reverse makes it encode instead
100 sub parse_formatdesc
101 1645 ▶{
Subroutine parse_formatdesc: called CC=8 SCAR=20.9
Pod undocumented
102 1645 my ($type, $data, $reverse) = @_;
103  
104 1645 ▶ if ($type eq 'hex') {
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq 'hex') { } 1362 283 1645
elsif ($type eq 'base58') { } 167 116 283
elsif ($type eq 'base64') { } 114 2 116
elsif ($type ne 'bytes') { } 0 2 2
105 1362 ▶ $data = $reverse
Branch: 1 branch
BranchTrueFalseTotal
$reverse ? : 953 409 1362
106 ? unpack 'H*', $data
107 : pack 'H*', pad_hex $data
108 ;
109 }
110 elsif ($type eq 'base58') {
111 167 require Bitcoin::Crypto::Base58;
112 167 ▶ $data = $reverse
Branch: 1 branch
BranchTrueFalseTotal
$reverse ? : 109 58 167
113 ? Bitcoin::Crypto::Base58::encode_base58check($data)
114 : Bitcoin::Crypto::Base58::decode_base58check($data)
115 ;
116 }
117 elsif ($type eq 'base64') {
118 114 ▶ $data = $reverse
Branch: 1 branch
BranchTrueFalseTotal
$reverse ? : 31 83 114
119 ? encode_base64($data, '')
120 : decode_base64($data)
121 ;
122 }
123 elsif ($type ne 'bytes') {
124 0 croak "bad format type: $type";
125 }
126  
127 1643 return $data;
128 }
129  
130 # define an arbitrary number of times a single secp256k1 context can be
131 # used. Create a new context after that. This gives an increased security
132 # according to libsecp256k1 documentation.
133 71 ▶use constant ECC_MAX_USES => 100;
Subroutine BEGIN: called CC=1 SCAR=0.0
134  
135 sub ecc
136 3511 ▶{
Subroutine ecc: called CC=2 SCAR=6.9
Pod undocumented
137 3511 state $secp;
138 3511 state $used_times = ECC_MAX_USES;
139  
140 3511 ▶ if (++$used_times > ECC_MAX_USES) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$used_times > 100) 59 3452 3511
141 59 $secp = Bitcoin::Secp256k1->new;
142 59 $used_times = 0;
143 }
144  
145 3511 return $secp;
146 }
147  
148 sub standard_push
149 856 ▶{
Subroutine standard_push: called CC=11 SCAR=28.4
Pod undocumented
150 856 my ($opcode_name, $bytes) = @_;
151  
152 # standard push is not checked for opcodes that push constant data
153 856 ▶ return !!1 if !$opcode_name || $opcode_name =~ /^OP_\d/;
Branch: 1 branch
BranchTrueFalseTotal
if not $opcode_name or $opcode_name =~ /^OP_\d/u 490 366 856
Condition66%2/3
not $opcode_name or $opcode_name =~ /^OP_\d/u
l!l&&r!l&&!r
0490366
MC/DC50%1/2
not $opcode_name or $opcode_name =~ /^OP_\d/u
not $opcode_name $opcode_name =~ /^OP_\d/u
Truth table66%2/3
not(A) or B
A= not $opcode_nameB= $opcode_name =~ /^OP_\d/u
ABresult
1X1
011
000
154  
155 366 ▶ if (length $bytes == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bytes == 0) 0 366 366
156  
157 # empty vectors are only pushed by OP_0
158 0 return !!0;
159 }
160  
161 366 ▶ if (length $bytes == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bytes == 1) 2 364 366
162 2 my $ord = ord $bytes;
163  
164 # anything up to 0x10 (excluding 0x00) and 0x81 has a special push
165 # opcode
166 2 ▶ return !!0
Branch: 1 branch
BranchTrueFalseTotal
unless $ord == 0 || $ord > 16 and $ord != 129 0 2 2
Condition33%2/6
$ord == 0 || $ord > 16 and $ord != 129
!ll&&!rl&&r
002
$ord == 0 or $ord > 16
l!l&&r!l&&!r
020
MC/DC0%0/3
$ord == 0 || $ord > 16 and $ord != 129
$ord == 0 $ord > 16 $ord != 129
Truth table20%1/5
A or B and C
A= $ord == 0B= $ord > 16C= $ord != 129
ABCresult
00X0
1X00
0100
1X11
0111
167 unless ($ord == 0x00 || $ord > 0x10)
168 && $ord != 0x81;
169 }
170  
171 366 ▶ if (length $bytes <= 75) {
Branch: 3 branches
BranchTrueFalseTotal
if (length $bytes <= 75) { } 364 2 366
elsif (length $bytes < 256) { } 2 0 2
elsif (length $bytes < 65536) { } 0 0 0
172  
173 # byte lengths from 1 to 75 use OP_PUSH
174 364 return $opcode_name eq 'OP_PUSH';
175 }
176 elsif (length $bytes < (1 << 8)) {
177  
178 # byte lengths fitting on 1 byte use OP_PUSHDATA1
179 2 return $opcode_name eq 'OP_PUSHDATA1';
180 }
181 elsif (length $bytes < (1 << 16)) {
182  
183 # byte lengths fitting on 2 bytes use OP_PUSHDATA2
184 0 return $opcode_name eq 'OP_PUSHDATA2';
185 }
186 else {
187  
188 # any other push uses OP_PUSHDATA4
189 0 return $opcode_name eq 'OP_PUSHDATA4';
190 }
191 }
192  
193 sub check_strict_public_key
194 2 ▶{
Subroutine check_strict_public_key: called CC=6 SCAR=20.9
Pod undocumented
195 2 my ($pubkey) = @_;
196  
197 2 my $byte = unpack('C', $pubkey);
198  
199 2 ▶ return !!1 if length($pubkey) == 65 && $byte == 0x04;
Branch: 1 branch
BranchTrueFalseTotal
if length $pubkey == 65 and $byte == 4 1 1 2
Condition66%2/3
length $pubkey == 65 and $byte == 4
!ll&&!rl&&r
011
MC/DC50%1/2
length $pubkey == 65 and $byte == 4
length $pubkey == 65 $byte == 4
Truth table66%2/3
A and B
A= length $pubkey == 65B= $byte == 4
ABresult
0X0
100
111
200 1 ▶ return !!1 if length($pubkey) == 33 && ($byte == 0x03 || $byte == 0x02);
Branch: 1 branch
BranchTrueFalseTotal
if length $pubkey == 33 and $byte == 3 || $byte == 2 0 1 1
Condition16%1/6
length $pubkey == 33 and $byte == 3 || $byte == 2
!ll&&!rl&&r
100
$byte == 3 or $byte == 2
l!l&&r!l&&!r
000
MC/DC0%0/3
length $pubkey == 33 and $byte == 3 || $byte == 2
length $pubkey == 33 $byte == 3 $byte == 2
Truth table25%1/4
A and B or C
A= length $pubkey == 33B= $byte == 3C= $byte == 2
ABCresult
0XX0
1000
11X1
1011
201  
202 1 return !!0;
203 }
204  
205 # translated to Perl from:
206 # https://github.com/bitcoin/bips/blob/master/bip-0066.mediawiki#der-encoding-reference
207 sub check_strict_der_signature
208 121 ▶{
Subroutine check_strict_der_signature: called CC=14 SCAR=30.8
Pod undocumented
209 121 my ($signature) = @_;
210  
211 # NOTE: increment by 1 to take (stripped earlier) sighash into account
212 # without changing the algorithm
213 121 my $len = length($signature) + 1;
214  
215 121 ▶ return !!0
Branch: 1 branch
BranchTrueFalseTotal
if $len < 9 or $len > 73 0 121 121
Condition33%1/3
$len < 9 or $len > 73
l!l&&r!l&&!r
00121
MC/DC0%0/2
$len < 9 or $len > 73
$len < 9 $len > 73
Truth table33%1/3
A or B
A= $len < 9B= $len > 73
ABresult
1X1
011
000
216 if $len < 9 || $len > 73;
217  
218 121 my ($t1, $t2, $t3, $r_len, $s_len);
219 121 ($t1, $t2, $r_len) = unpack 'CC @3C', $signature;
220  
221 # check start and r length
222 121 ▶ return !!0
Branch: 1 branch
BranchTrueFalseTotal
if $t1 != 48 or $t2 != $len - 3 or $r_len + 5 >= $len 4 117 121
Condition66%4/6
$t1 != 48 or $t2 != $len - 3 or $r_len + 5 >= $len
l!l&&r!l&&!r
40117
$t1 != 48 or $t2 != $len - 3
l!l&&r!l&&!r
04117
MC/DC33%1/3
$t1 != 48 or $t2 != $len - 3 or $r_len + 5 >= $len
$t1 != 48 $t2 != $len - 3 $r_len + 5 >= $len
Truth table50%2/4
A or B or C
A= $t1 != 48B= $t2 != $len - 3C= $r_len + 5 >= $len
ABCresult
1XX1
01X1
0011
0000
223 if $t1 != 0x30
224 || $t2 != $len - 3
225 || $r_len + 5 >= $len;
226  
227 117 $s_len = unpack '@' . (5 + $r_len) . 'C', $signature;
228  
229 # check s length
230 117 ▶ return !!0
Branch: 1 branch
BranchTrueFalseTotal
if $r_len + $s_len + 7 != $len 0 117 117
231 if $r_len + $s_len + 7 != $len;
232  
233 117 for my $item ([$r_len, 2], [$s_len, $r_len + 4]) {
234 234 ($t1, $t2, $t3) = unpack '@0C @2C @3C', substr $signature, $item->[1];
235  
236 # check parts of r or s
237 234 ▶ return !!0
Branch: 1 branch
BranchTrueFalseTotal
if $t1 != 2 or $item->[0] == 0 or $t2 & 128 or $item->[0] > 1 and $t2 == 0 and not $t3 & 128 0 234 234
Condition46%7/15
$t1 != 2 or $item->[0] == 0 or $t2 & 128 or $item->[0] > 1 and $t2 == 0 and not $t3 & 128
l!l&&r!l&&!r
00234
$t1 != 2 or $item->[0] == 0 or $t2 & 128
l!l&&r!l&&!r
00234
$t1 != 2 or $item->[0] == 0
l!l&&r!l&&!r
00234
$item->[0] > 1 and $t2 == 0 and not $t3 & 128
!ll&&!rl&&r
174600
$item->[0] > 1 and $t2 == 0
!ll&&!rl&&r
017460
MC/DC0%0/6
$t1 != 2 or $item->[0] == 0 or $t2 & 128 or $item->[0] > 1 and $t2 == 0 and not $t3 & 128
$t1 != 2 $item->[0] == 0 $t2 & 128 $item->[0] > 1 $t2 == 0 not $t3 & 128
Truth table28%2/7
A or B or C or D and E and not(F)
A= $t1 != 2B= $item->[0] == 0C= $t2 & 128D= $item->[0] > 1E= $t2 == 0F= not $t3 & 128
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001111
0000XX0
00010X0
0001100
238 if $t1 != 0x02
239 || $item->[0] == 0
240 || $t2 & 0x80
241 || ($item->[0] > 1 && $t2 == 0 && !($t3 & 0x80));
242 }
243  
244 117 return !!1;
245 }
246  
247 # this does not fix low s, just strict encoding
248 sub make_strict_der_signature
249 3 ▶{
Subroutine make_strict_der_signature: called CC=6 SCAR=18.3
Pod undocumented
250 3 my ($signature) = @_;
251 3 ▶ return $signature unless length $signature >= 8;
Branch: 1 branch
BranchTrueFalseTotal
unless length $signature >= 8 2 1 3
252  
253 # https://bitcoin.stackexchange.com/questions/92680/what-are-the-der-signature-and-sec-format
254 # also:
255 # - ignore any trailing data
256 # - fix negative r and s
257  
258 1 my ($compound, $total_len, $int1, $r_len) = unpack 'aCaC', $signature;
259 1 my $r = substr $signature, 4, $r_len;
260  
261 1 $signature = substr $signature, 4 + $r_len;
262 1 my ($int2, $s_len) = unpack 'aC', $signature;
263 1 my $s = substr $signature, 2, $s_len;
264  
265 # remove padding
266 1 ▶ $r = substr($r, 1)
Branch: 1 branch
BranchTrueFalseTotal
unpack("C", $r) == 0 and do { $r = substr($r, 1) } 0 1 1
267 while unpack('C', $r) == 0;
268 1 ▶ $s = substr($s, 1)
Branch: 1 branch
BranchTrueFalseTotal
unpack("C", $s) == 0 and do { $s = substr($s, 1) } 0 1 1
269 while unpack('C', $s) == 0;
270  
271 # top bit may be 1, so prepend with zero to avoid being interpreted as
272 # negative
273 1 ▶ $r = "\x00$r"
Branch: 1 branch
BranchTrueFalseTotal
if unpack("C", $r) & 128 0 1 1
274 if unpack('C', $r) & 0x80;
275 1 ▶ $s = "\x00$s"
Branch: 1 branch
BranchTrueFalseTotal
if unpack("C", $s) & 128 0 1 1
276 if unpack('C', $s) & 0x80;
277  
278 # return extracted strict signature
279 1 return pack "aCaCa*aCa*",
280 $compound,
281 4 + length($r) + length($s),
282 $int1,
283 length($r),
284 $r,
285 $int2,
286 length($s),
287 $s;
288 }
289  
290 sub die_no_trace
291 19 ▶{
Subroutine die_no_trace: called CC=1 SCAR=0.0
Pod undocumented
292 19 die $_[0] . "\n";
293 }
294  
295 71 1;
296  
297 # Internal use only
298  
« Crypto/Transaction.pm ↑ Summary Crypto/Transaction/Digest.pm »