blib/lib/Crypt/Misc.pm

filter: Statementstmt 100% 189 / 189 Branchbran 78.8% 71 / 90 Conditioncond 67.5% 25 / 37 MC/DCmcdc 42.8% 9 / 21 Subroutinesub 100% 34 / 34 Podpod 100% 18 / 18 totaltotal 88.9% 346 / 389 CC 61 SCAR 21.3
CC
61
Cov
90%
pem_to_der
18.2
_name2mode
15.2
_decode_b58
10.1
CRAP 8.4
« blib/lib/Crypt/PK/DH.pm ↑ Summary blib/lib/Crypt/Mac.pm »
1 package Crypt::Misc;
2  
3 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 23 our $VERSION = '0.091';
6  
7 23 require Exporter; our @ISA = qw(Exporter); ### use Exporter 5.57 'import';
8 23 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 23 our %EXPORT_TAGS = ( all => [qw(encode_b64 decode_b64
10 encode_b64u decode_b64u
11 encode_b58b decode_b58b
12 encode_b58f decode_b58f
13 encode_b58r decode_b58r
14 encode_b58t decode_b58t
15 encode_b58s decode_b58s
16 encode_b32r decode_b32r
17 encode_b32b decode_b32b
18 encode_b32z decode_b32z
19 encode_b32c decode_b32c
20 pem_to_der der_to_pem
21 read_rawfile write_rawfile
22 slow_eq is_v4uuid random_v4uuid
23 is_uuid random_v7uuid
24 increment_octets_be increment_octets_le
25 )] );
26 23 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
27 23 our @EXPORT = qw();
28  
29 23 ▶use CryptX;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 23 ▶use Crypt::Digest 'digest_data';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 23 ▶use Crypt::Mode::CBC;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 23 ▶use Crypt::Mode::CFB;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 23 ▶use Crypt::Mode::ECB;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 23 ▶use Crypt::Mode::OFB;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 23 ▶use Crypt::Cipher;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 23 ▶use Crypt::PRNG 'random_bytes';
Subroutine BEGIN: called CC=1 SCAR=0.0
37 23 ▶use Time::HiRes (); # perl core module
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 292 ▶sub _encode_b58 {
Subroutine _encode_b58: called CC=6 SCAR=17.9
40 292 my ($bytes, $alphabet) = @_;
41  
42 292 ▶ return undef if !defined $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 1 291 292
43 291 ▶ return '' if length($bytes) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $bytes == 0 1 290 291
44  
45 # handle leading zero-bytes
46 290 my $base58 = '';
47 290 ▶ if ($bytes =~ /^(\x00+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes =~ /^(\x00+)/) 80 210 290
48 80 $base58 = ('0' x length($1));
49 }
50 290 $base58 .= _bin_to_radix($bytes, 58);
51  
52 290 ▶ if (defined $alphabet) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $alphabet) 290 0 290
53 290 my $default = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv";
54 290 ▶ return undef if $alphabet !~ /^[a-zA-Z0-9]{58}$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $alphabet =~ /^[a-zA-Z0-9]{58}$/ 0 290 290
55 290 my %map;
56 290 @map{split //, $default} = split //, $alphabet;
57 290 $base58 = join '', map { $map{$_} } split //, $base58;
58 }
59  
60 290 return $base58;
61 }
62  
63 294 ▶sub _decode_b58 {
Subroutine _decode_b58: called CC=10 SCAR=23.2
64 294 my ($base58, $alphabet) = @_;
65  
66 294 ▶ return undef if !defined $base58;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $base58 1 293 294
67 293 ▶ return '' if length($base58) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $base58 == 0 1 292 293
68  
69 292 my $default = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv";
70 292 ▶ if (defined $alphabet) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $alphabet) 292 0 292
71 292 ▶ return undef if $alphabet !~ /^[a-zA-Z0-9]{58}$/ || $base58 !~ /^[$alphabet]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $alphabet =~ /^[a-zA-Z0-9]{58}$/ and $base58 =~ /^[$alphabet]+$/ 2 290 292
Condition66%2/3
$alphabet =~ /^[a-zA-Z0-9]{58}$/ and $base58 =~ /^[$alphabet]+$/
!ll&&!rl&&r
02290
MC/DC50%1/2
$alphabet =~ /^[a-zA-Z0-9]{58}$/ and $base58 =~ /^[$alphabet]+$/
$alphabet =~ /^[a-zA-Z0-9]{58}$/ $base58 =~ /^[$alphabet]+$/
Truth table66%2/3
A and B
A= $alphabet =~ /^[a-zA-Z0-9]{58}$/B= $base58 =~ /^[$alphabet]+$/
ABresult
0X0
100
111
72 290 my %map;
73 290 @map{split //, $alphabet} = split //, $default;
74 290 $base58 = join '', map { $map{$_} } split //, $base58;
75 }
76 290 ▶ return undef if $base58 !~ /^[$default]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $base58 =~ /^[$default]+$/ 0 290 290
77  
78 # handle leading zeroes
79 290 my $bytes = '';
80 290 ▶ if ($base58 =~ /^(0+)(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base58 =~ /^(0+)(.*)$/) 80 210 290
81 80 $base58 = $2;
82 80 $bytes = ("\x00" x length($1));
83 }
84 290 ▶ $bytes .= _radix_to_bin($base58, 58) if defined $base58 && length($base58) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $base58 and length $base58 > 0 265 25 290
Condition66%2/3
defined $base58 and length $base58 > 0
!ll&&!rl&&r
025265
MC/DC50%1/2
defined $base58 and length $base58 > 0
defined $base58 length $base58 > 0
Truth table66%2/3
A and B
A= defined $base58B= length $base58 > 0
ABresult
0X0
100
111
85  
86 290 return $bytes;
87 }
88  
89 62 ▶sub decode_b58b { _decode_b58(shift, "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz") } # Bitcoin
Subroutine decode_b58b: called CC=1 SCAR=0.0
Pod documented
90 58 ▶sub decode_b58f { _decode_b58(shift, "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ") } # Flickr
Subroutine decode_b58f: called CC=1 SCAR=0.0
Pod documented
91 58 ▶sub decode_b58r { _decode_b58(shift, "rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz") } # Ripple
Subroutine decode_b58r: called CC=1 SCAR=0.0
Pod documented
92 58 ▶sub decode_b58t { _decode_b58(shift, "RPShNAF39wBUDnEGHJKLM4pQrsT7VWXYZ2bcdeCg65jkm8ofqi1tuvaxyz") } # Tipple
Subroutine decode_b58t: called CC=1 SCAR=0.0
Pod documented
93 58 ▶sub decode_b58s { _decode_b58(shift, "gsphnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCr65jkm8oFqi1tuvAxyz") } # Stellar
Subroutine decode_b58s: called CC=1 SCAR=0.0
Pod documented
94  
95 60 ▶sub encode_b58b { _encode_b58(shift, "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz") } # Bitcoin
Subroutine encode_b58b: called CC=1 SCAR=0.0
Pod documented
96 58 ▶sub encode_b58f { _encode_b58(shift, "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ") } # Flickr
Subroutine encode_b58f: called CC=1 SCAR=0.0
Pod documented
97 58 ▶sub encode_b58r { _encode_b58(shift, "rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz") } # Ripple
Subroutine encode_b58r: called CC=1 SCAR=0.0
Pod documented
98 58 ▶sub encode_b58t { _encode_b58(shift, "RPShNAF39wBUDnEGHJKLM4pQrsT7VWXYZ2bcdeCg65jkm8ofqi1tuvaxyz") } # Tipple
Subroutine encode_b58t: called CC=1 SCAR=0.0
Pod documented
99 58 ▶sub encode_b58s { _encode_b58(shift, "gsphnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCr65jkm8oFqi1tuvAxyz") } # Stellar
Subroutine encode_b58s: called CC=1 SCAR=0.0
Pod documented
100  
101 29 ▶sub pem_to_der {
Subroutine pem_to_der: called CC=17 SCAR=29.0
Pod documented
102 29 my ($data, $password) = @_;
103  
104 29 my ($begin, $obj1, $content, $end, $obj2);
105 # first try to load KEY (e.g. EC pem files might contain more parts)
106 29 ($begin, $obj1, $content, $end, $obj2) = $data =~ m/(----[- ]BEGIN ([^\r\n\-]+KEY)[ -]----)(.*?)(----[- ]END ([^\r\n\-]+)[ -]----)/s;
107 # if failed then try to load anything
108 29 ▶ ($begin, $obj1, $content, $end, $obj2) = $data =~ m/(----[- ]BEGIN ([^\r\n\-]+)[ -]----)(.*?)(----[- ]END ([^\r\n\-]+)[ -]----)/s unless defined $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 4 25 29
109 29 ▶ return undef unless $content;
Branch: 1 branch
BranchTrueFalseTotal
unless $content 0 29 29
110 29 ▶ return undef if !defined($obj1) || !defined($obj2) || $obj1 ne $obj2;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $obj1 && defined $obj2 or $obj1 ne $obj2 2 27 29
Condition50%3/6
not defined $obj1 && defined $obj2 or $obj1 ne $obj2
l!l&&r!l&&!r
0227
defined $obj1 && defined $obj2
!ll&&!rl&&r
0029
MC/DC33%1/3
not defined $obj1 && defined $obj2 or $obj1 ne $obj2
defined $obj1 defined $obj2 $obj1 ne $obj2
Truth table50%2/4
not(A && B) or C
A= defined $obj1B= defined $obj2C= $obj1 ne $obj2
ABCresult
0XX1
10X1
1111
1100
111  
112 27 $content =~ s/^\s+//sg;
113 27 $content =~ s/\s+$//sg;
114 27 $content =~ s/\r\n/\n/sg; # CR-LF >> LF
115 27 $content =~ s/\r/\n/sg; # CR >> LF
116 27 $content =~ s/\\\n//sg; # \ + LF
117  
118 27 my ($headers, undef, $b64) = $content =~ /^(([^:]+:.*?\n)*)(.*)$/s;
119 27 ▶ return undef unless $b64;
Branch: 1 branch
BranchTrueFalseTotal
unless $b64 0 27 27
120  
121 27 my $binary = decode_b64($b64);
122 27 ▶ return undef unless defined $binary;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $binary 0 27 27
123  
124 27 my ($ptype, $cipher_name, $iv_hex);
125 27 ▶ for my $h (split /\n/, ($headers||'')) {
Condition100%2/2
$headers || ''
l!l
1017
MC/DC100%1/1
$headers || ''
$headers
Truth table100%2/2
A
A= $headers
Aresult
11
00
126 20 my ($k, $v) = split /:\s*/, $h, 2;
127 20 ▶ $ptype = $v if $k eq 'Proc-Type';
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "Proc-Type" 10 10 20
128 20 ▶ ($cipher_name, $iv_hex) = $v =~ /^\s*(.*?)\s*,\s*([0-9a-fA-F]+)\s*$/ if $k eq 'DEK-Info';
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "DEK-Info" 10 10 20
129 }
130 27 ▶ if ($cipher_name && $iv_hex && $ptype && $ptype eq '4,ENCRYPTED') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cipher_name and $iv_hex and $ptype and $ptype eq "4,ENCRYPTED") 10 17 27
Condition66%6/9
$cipher_name and $iv_hex and $ptype and $ptype eq "4,ENCRYPTED"
!ll&&!rl&&r
17010
$cipher_name and $iv_hex and $ptype
!ll&&!rl&&r
17010
$cipher_name and $iv_hex
!ll&&!rl&&r
17010
MC/DC25%1/4
$cipher_name and $iv_hex and $ptype and $ptype eq "4,ENCRYPTED"
$cipher_name $iv_hex $ptype $ptype eq "4,ENCRYPTED"
Truth table40%2/5
A and B and C and D
A= $cipher_nameB= $iv_hexC= $ptypeD= $ptype eq "4,ENCRYPTED"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
131 10 ▶ croak "FATAL: encrypted PEM but no password provided" unless defined $password;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $password 0 10 10
132 10 my $iv = pack("H*", $iv_hex);
133 10 my ($mode, $klen) = _name2mode($cipher_name);
134 10 my $key = _password2key($password, $klen, $iv, 'MD5');
135 10 return $mode->decrypt($binary, $key, $iv);
136 }
137 17 return $binary;
138 }
139  
140 37 ▶sub der_to_pem {
Subroutine der_to_pem: called CC=7 SCAR=19.5
Pod documented
141 37 my ($data, $header_name, $password, $cipher_name) = @_;
142 37 ▶ croak "FATAL: der_to_pem invalid header name" unless defined $header_name && $header_name =~ /^[0-9A-Za-z ]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $header_name and $header_name =~ /^[0-9A-Za-z ]+$/ 4 33 37
Condition100%3/3
defined $header_name and $header_name =~ /^[0-9A-Za-z ]+$/
!ll&&!rl&&r
1333
MC/DC100%2/2
defined $header_name and $header_name =~ /^[0-9A-Za-z ]+$/
defined $header_name $header_name =~ /^[0-9A-Za-z ]+$/
Truth table100%3/3
A and B
A= defined $header_nameB= $header_name =~ /^[0-9A-Za-z ]+$/
ABresult
0X0
100
111
143 33 my $content = $data;
144 33 my @headers;
145  
146 33 ▶ if (defined $password) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $password) 9 24 33
147 9 ▶ $cipher_name ||= 'AES-256-CBC';
Condition100%2/2
$cipher_name ||= "AES-256-CBC"
l!l
72
MC/DC100%1/1
$cipher_name ||= "AES-256-CBC"
$cipher_name
Truth table100%2/2
A
A= $cipher_name
Aresult
11
00
148 9 my ($mode, $klen, $ilen) = _name2mode($cipher_name);
149 9 my $iv = random_bytes($ilen);
150 9 my $key = _password2key($password, $klen, $iv, 'MD5');
151 9 $content = $mode->encrypt($data, $key, $iv);
152 9 push @headers, 'Proc-Type: 4,ENCRYPTED', "DEK-Info: ".uc($cipher_name).",".unpack("H*", $iv);
153 }
154  
155 33 my $pem = "-----BEGIN $header_name-----\n";
156 33 ▶ if (@headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (@headers) 9 24 33
157 9 $pem .= "$_\n" for @headers;
158 9 $pem .= "\n";
159 }
160 33 my @l = encode_b64($content) =~ /.{1,64}/g;
161 33 $pem .= join("\n", @l) . "\n";
162 33 $pem .= "-----END $header_name-----\n";
163 33 return $pem;
164 }
165  
166 297 ▶sub read_rawfile {
Subroutine read_rawfile: called CC=3 SCAR=11.2
Pod documented
167 # $data = read_rawfile($filename);
168 297 my $f = shift;
169 297 ▶ croak "FATAL: read_rawfile() non-existing file '$f'" unless -f $f;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $f 0 297 297
170 297 ▶ open my $fh, "<", $f or croak "FATAL: read_rawfile() cannot open file '$f': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $f or croak("FATAL: read_rawfile() cannot open file '${f}': $!") 297 0 297
171 297 binmode $fh;
172 297 return do { local $/; <$fh> };
173 }
174  
175 3 ▶sub write_rawfile {
Subroutine write_rawfile: called CC=5 SCAR=17.2
Pod documented
176 # write_rawfile($filename, $data);
177 3 ▶ croak "FATAL: write_rawfile() no data" unless defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] 0 3 3
178 3 ▶ open my $fh, ">", $_[0] or croak "FATAL: write_rawfile() cannot open file '$_[0]': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $_[0] or croak("FATAL: write_rawfile() cannot open file '$_[0]': $!") 3 0 3
179 3 binmode $fh;
180 3 ▶ print $fh $_[1] or croak "FATAL: write_rawfile() cannot write to '$_[0]': $!";
Branch: 1 branch
BranchTrueFalseTotal
print $fh $_[1] or croak("FATAL: write_rawfile() cannot write to '$_[0]': $!") 3 0 3
181 3 ▶ close $fh or croak "FATAL: write_rawfile() cannot close '$_[0]': $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("FATAL: write_rawfile() cannot close '$_[0]': $!") 3 0 3
182 3 return;
183 }
184  
185 ### slow_eq() is implemented in XS (CryptX.xs) using libtomcrypt's mem_neq()
186  
187 1 ▶sub random_v4uuid() {
Subroutine random_v4uuid: called CC=1 SCAR=0.0
Pod documented
188 # Version 4 - random - UUID: xxxxxxxx-xxxx-4xxx-Yxxx-xxxxxxxxxxxx
189 # where x is any hexadecimal digit and Y is one of 8, 9, A, B (1000, 1001, 1010, 1011)
190 # e.g. f47ac10b-58cc-4372-a567-0e02b2c3d479
191 1 my $raw = random_bytes(16);
192 # xxxxxxxxxxxx4xxxYxxxxxxxxxxxxxxx
193 1 $raw &= pack("H*", "FFFFFFFFFFFF0FFFFFFFFFFFFFFFFFFF");
194 1 $raw |= pack("H*", "00000000000040000000000000000000");
195 1 $raw &= pack("H*", "FFFFFFFFFFFFFFFF3FFFFFFFFFFFFFFF"); # 0x3 == 0011b
196 1 $raw |= pack("H*", "00000000000000008000000000000000"); # 0x8 == 1000b
197 1 my $hex = unpack("H*", $raw);
198 1 $hex =~ s/^(.{8})(.{4})(.{4})(.{4})(.{12}).*$/$1-$2-$3-$4-$5/;
199 1 return $hex;
200 }
201  
202 3 ▶sub random_v7uuid() {
Subroutine random_v7uuid: called CC=1 SCAR=0.0
Pod documented
203 # RFC 9562 §5.7 - Version 7 UUID (time-ordered)
204 # Format: xxxxxxxx-xxxx-7xxx-[89ab]xxx-xxxxxxxxxxxx
205 # Structure: 48-bit ms timestamp | 4-bit ver=7 | 12-bit rand | 2-bit var=10 | 62-bit rand
206 3 my ($sec, $usec) = Time::HiRes::gettimeofday();
207 3 my $ms = $sec * 1000 + int($usec / 1000);
208 3 my $rand = random_bytes(10);
209 3 my $raw = pack("N", int($ms / 65536)) . # bytes 0-3: ms[47:16]
210 pack("n", $ms % 65536) . # bytes 4-5: ms[15:0]
211 pack("n", 0x7000 | (unpack("n", substr($rand, 0, 2)) & 0x0FFF)) . # bytes 6-7: ver=7 + rand_a
212 pack("C", 0x80 | (unpack("C", substr($rand, 2, 1)) & 0x3F)) . # byte 8: var=10 + rand_b
213 substr($rand, 3, 7); # bytes 9-15: rand_b (cont.)
214 3 my $hex = unpack("H*", $raw);
215 3 $hex =~ s/^(.{8})(.{4})(.{4})(.{4})(.{12})$/$1-$2-$3-$4-$5/;
216 3 return $hex;
217 }
218  
219 5 ▶sub is_v4uuid($) {
Subroutine is_v4uuid: called CC=3 SCAR=11.0
Pod documented
220 5 my $uuid = shift;
221 5 ▶ return 0 if !$uuid;
Branch: 1 branch
BranchTrueFalseTotal
unless $uuid 0 5 5
222 5 ▶ return 1 if $uuid =~ /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $uuid =~ /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i 2 3 5
223 3 return 0;
224 }
225  
226 11 ▶sub is_uuid($) {
Subroutine is_uuid: called CC=3 SCAR=11.0
Pod documented
227 11 my $uuid = shift;
228 11 ▶ return 0 if !$uuid;
Branch: 1 branch
BranchTrueFalseTotal
unless $uuid 1 10 11
229 10 ▶ return 1 if $uuid =~ /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $uuid =~ /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i 6 4 10
230 4 return 0;
231 }
232  
233 ### private functions
234  
235 20 ▶sub _name2mode {
Subroutine _name2mode: called CC=14 SCAR=27.2
236 20 my $cipher_name = uc(shift);
237 20 my %trans = ( 'DES-EDE3' => 'DES_EDE' );
238  
239 20 my ($cipher, undef, $klen, $mode) = $cipher_name =~ /^(AES|CAMELLIA|DES|DES-EDE3|SEED)(-(\d+))?-(CBC|CFB|ECB|OFB)$/i;
240 20 ▶ croak "FATAL: unsupported cipher '$cipher_name'" unless $cipher && $mode;
Branch: 1 branch
BranchTrueFalseTotal
unless $cipher and $mode 1 19 20
Condition66%2/3
$cipher and $mode
!ll&&!rl&&r
1019
MC/DC50%1/2
$cipher and $mode
$cipher $mode
Truth table66%2/3
A and B
A= $cipherB= $mode
ABresult
0X0
100
111
241 19 ▶ $cipher = $trans{$cipher} || $cipher;
Condition66%2/3
$trans{$cipher} || $cipher
l!l&&r!l&&!r
2170
MC/DC0%0/2
$trans{$cipher} || $cipher
$trans{$cipher} $cipher
Truth table66%2/3
A || B
A= $trans{$cipher}B= $cipher
ABresult
1X1
011
000
242 19 ▶ $klen = 192 if $cipher eq 'DES_EDE';
Branch: 1 branch
BranchTrueFalseTotal
if $cipher eq "DES_EDE" 2 17 19
243 19 ▶ $klen = 64 if $cipher eq 'DES';
Branch: 1 branch
BranchTrueFalseTotal
if $cipher eq "DES" 2 17 19
244 19 ▶ $klen = 128 if $cipher eq 'SEED';
Branch: 1 branch
BranchTrueFalseTotal
if $cipher eq "SEED" 2 17 19
245 19 ▶ $klen = $klen ? int($klen/8) : Crypt::Cipher::min_keysize($cipher);
Branch: 1 branch
BranchTrueFalseTotal
$klen ? : 19 0 19
246 19 my $ilen = Crypt::Cipher::blocksize($cipher);
247 19 ▶ croak "FATAL: unsupported cipher '$cipher_name'" unless $klen && $ilen;
Branch: 1 branch
BranchTrueFalseTotal
unless $klen and $ilen 0 19 19
Condition33%1/3
$klen and $ilen
!ll&&!rl&&r
0019
MC/DC0%0/2
$klen and $ilen
$klen $ilen
Truth table33%1/3
A and B
A= $klenB= $ilen
ABresult
0X0
100
111
248  
249 19 ▶ return (Crypt::Mode::CBC->new($cipher), $klen, $ilen) if $mode eq 'CBC';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "CBC" 15 4 19
250 4 ▶ return (Crypt::Mode::CFB->new($cipher), $klen, $ilen) if $mode eq 'CFB';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "CFB" 2 2 4
251 2 ▶ return (Crypt::Mode::ECB->new($cipher), $klen, $ilen) if $mode eq 'ECB';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "ECB" 0 2 2
252 2 ▶ return (Crypt::Mode::OFB->new($cipher), $klen, $ilen) if $mode eq 'OFB';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "OFB" 2 0 2
253 }
254  
255 19 ▶sub _password2key {
Subroutine _password2key: called CC=2 SCAR=6.9
256 19 my ($password, $klen, $iv, $hash) = @_;
257 19 my $salt = substr($iv, 0, 8);
258 19 my $key = '';
259 19 ▶ while (length($key) < $klen) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $key < $klen) 32 19 51
260 32 $key .= digest_data($hash, $key . $password . $salt);
261 }
262 19 return substr($key, 0, $klen);
263 }
264  
265 23 1;
266  
267 =pod
268  
269 =head1 NAME
270  
271 Crypt::Misc - miscellaneous functions related to (or used by) CryptX
272  
273 =head1 SYNOPSIS
274  
275 use Crypt::Misc ':all';
276  
277 my $rawbytes = 'hello world';
278 my $filename = 'sample.bin';
279 my $pem_data = "-----BEGIN PUBLIC KEY-----\n...\n-----END PUBLIC KEY-----\n";
280 my $str1 = 'same';
281 my $str2 = 'same';
282  
283 # Base64 and Base64 URL-safe functions
284 my $base64 = encode_b64($rawbytes);
285 my $rawbytes2 = decode_b64($base64);
286 my $base64url = encode_b64u($rawbytes);
287 my $rawbytes3 = decode_b64u($base64url);
288  
289 # read/write file
290 my $rawdata = read_rawfile($filename);
291 write_rawfile($filename, $rawdata);
292  
293 # convert PEM/DER
294 my $der_data = pem_to_der($pem_data);
295 my $pem_data2 = der_to_pem($der_data, "PUBLIC KEY");
296  
297 # others
298 die "mismatch" unless slow_eq($str1, $str2);
299  
300 =head1 DESCRIPTION
301  
302 This module contains a collection of helper functions related to CryptX, but it
303 does not implement cryptography itself.
304  
305 Most of them are also available in other Perl modules. If you already use
306 CryptX, these helpers can reduce extra dependencies.
307  
308 =head1 EXPORT
309  
310 Nothing is exported by default.
311  
312 You can export selected functions:
313  
314 use Crypt::Misc qw(read_rawfile write_rawfile slow_eq);
315  
316 Or all of them at once:
317  
318 use Crypt::Misc ':all';
319  
320 =head1 FUNCTIONS
321  
322 All encoding functions (C<encode_b64>, C<encode_b58b>, etc.) accept a binary
323 string and return an ASCII string. All decoding functions (C<decode_b64>,
324 C<decode_b58b>, etc.) accept an ASCII string and return a binary string, or
325 C<undef> if the input is malformed.
326  
327 An empty string is considered valid input and decodes to an empty string.
328 C<undef> is considered invalid input and results in C<undef>. Non-empty input
329 with no actual payload, such as whitespace-only or padding-only input, is also
330 considered malformed and results in C<undef>.
331  
332 The Base64 decoders C<decode_b64> and C<decode_b64u> also accept otherwise
333 valid payload with embedded whitespace. The other decoder families in this
334 module do not; for them, embedded whitespace is treated as malformed input.
335  
336 =head2 read_rawfile
337  
338 I<Since: CryptX-0.029>
339  
340 $rawdata = read_rawfile($filename);
341  
342 Reads file C<$filename> into a scalar as binary data, without decoding or
343 transformation.
344  
345 =head2 write_rawfile
346  
347 I<Since: CryptX-0.029>
348  
349 write_rawfile($filename, $rawdata);
350  
351 Writes C<$rawdata> to file C<$filename> as binary data.
352  
353 =head2 slow_eq
354  
355 I<Since: CryptX-0.029>
356  
357 if (slow_eq($data1, $data2)) { ... }
358  
359 Compares two strings in constant time to reduce timing side channels. Returns
360 C<1> if the strings are equal, C<0> if they differ, or C<undef> if either
361 argument is C<undef>.
362  
363 =head2 pem_to_der
364  
365 I<Since: CryptX-0.029>
366  
367 $der_data = pem_to_der($pem_data);
368 #or
369 $der_data = pem_to_der($pem_data, $password);
370  
371 Converts PEM to DER. Also supports password-protected PEM data.
372 Returns C<undef> if C<$pem_data> cannot be parsed (no valid PEM block found)
373 or if the C<BEGIN> / C<END> labels do not match. Croaks if the PEM is
374 encrypted but no C<$password> is provided. If an encrypted PEM is supplied
375 with the wrong password, decryption is expected to croak from the underlying
376 cipher/padding layer.
377  
378 =head2 der_to_pem
379  
380 I<Since: CryptX-0.029>
381  
382 $pem_data = der_to_pem($der_data, $header_name);
383 #or
384 $pem_data = der_to_pem($der_data, $header_name, $password);
385 #or
386 $pem_data = der_to_pem($der_data, $header_name, $password, $cipher_name);
387  
388 # $header_name e.g. "PUBLIC KEY", "RSA PRIVATE KEY" ...
389 # $cipher_name e.g. "DES-EDE3-CBC", "AES-256-CBC" (DEFAULT) ...
390  
391 Converts DER to PEM. Returns an ASCII PEM string. Also supports
392 password-protected PEM data. Any defined C<$password>, including
393 false-like values like C<''> or C<'0'>, enables PEM encryption.
394  
395 B<Security note>: do not use ECB-based ciphers (e.g. C<AES-256-ECB>) for PEM
396 encryption - ECB encrypts each block independently, leaking plaintext structure.
397 Use the default C<AES-256-CBC> or another chaining mode (CBC, CFB, OFB).
398  
399 B<Security note>: the traditional PEM encryption format uses a single-iteration
400 MD5-based key derivation which is weak against brute-force. For new applications,
401 prefer PKCS#8 encrypted keys (e.g. via L<Crypt::PK::RSA/export_key_pem>) or
402 an independent encryption layer.
403  
404 =head2 random_v4uuid
405  
406 I<Since: CryptX-0.031>
407  
408 my $uuid = random_v4uuid();
409  
410 Returns a cryptographically strong version 4 random UUID:
411 C<xxxxxxxx-xxxx-4xxx-Yxxx-xxxxxxxxxxxx>
412 where C<x> is any hexadecimal digit and C<Y> is one of 8, 9, A, B (1000, 1001, 1010, 1011)
413 e.g. C<f47ac10b-58cc-4372-a567-0e02b2c3d479>.
414  
415 =head2 is_v4uuid
416  
417 I<Since: CryptX-0.031>
418  
419 if (is_v4uuid($uuid)) {
420 ...
421 }
422  
423 Checks whether the given C<$uuid> string matches version 4 UUID format with a
424 relaxed variant policy. The variant nibble may be one of C<0>, C<8>, C<9>,
425 C<A>, or C<B>. Returns C<0> (mismatch) or C<1> (match).
426  
427 =head2 random_v7uuid
428  
429 I<Since: CryptX-0.088>
430  
431 my $uuid = random_v7uuid();
432  
433 Returns a cryptographically strong version 7 time-ordered UUID:
434 C<xxxxxxxx-xxxx-7xxx-Yxxx-xxxxxxxxxxxx>
435 where the first 48 bits encode the current Unix time in milliseconds (making UUIDs sortable by
436 generation time), followed by random bits. Ordering is therefore coarse at
437 millisecond granularity only; UUIDs generated within the same millisecond are
438 not guaranteed to be lexicographically monotonic. C<Y> is one of 8, 9, A, B
439 (RFC 9562 variant).
440  
441 =head2 is_uuid
442  
443 I<Since: CryptX-0.088>
444  
445 if (is_uuid($uuid)) {
446 ...
447 }
448  
449 Checks whether C<$uuid> is a validly formatted UUID (any version) in the standard
450 C<xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx> form with a relaxed variant policy.
451 The variant nibble may be one of C<0>, C<8>, C<9>, C<A>, or C<B>. Returns
452 C<1> (match) or C<0> (mismatch).
453 For a version-specific check see L</is_v4uuid>.
454  
455 =head2 increment_octets_le
456  
457 I<Since: CryptX-0.048>
458  
459 $octets = increment_octets_le($octets);
460  
461 Treats C<$octets> as a little-endian big number and returns the incremented
462 value.
463  
464 =head2 increment_octets_be
465  
466 I<Since: CryptX-0.048>
467  
468 $octets = increment_octets_be($octets);
469  
470 Treats C<$octets> as a big-endian big number and returns the incremented value.
471  
472 =head2 encode_b64
473  
474 I<Since: CryptX-0.029>
475  
476 $base64string = encode_b64($rawdata);
477  
478 Encodes C<$rawdata> as a Base64 string. No line endings are added.
479  
480 =head2 decode_b64
481  
482 I<Since: CryptX-0.029>
483  
484 $rawdata = decode_b64($base64string);
485  
486 Decodes a Base64 string.
487  
488 =head2 encode_b64u
489  
490 I<Since: CryptX-0.029>
491  
492 $base64url_string = encode_b64u($rawdata);
493  
494 Encodes C<$rawdata> as a Base64 URL-safe string. No line endings are added.
495  
496 =head2 decode_b64u
497  
498 I<Since: CryptX-0.029>
499  
500 $rawdata = decode_b64u($base64url_string);
501  
502 Decodes a Base64 URL-safe string.
503  
504 =head2 encode_b32r
505  
506 I<Since: CryptX-0.049>
507  
508 $string = encode_b32r($rawdata);
509  
510 Encode bytes into Base32 (rfc4648 alphabet) string, without "=" padding.
511  
512 =head2 decode_b32r
513  
514 I<Since: CryptX-0.049>
515  
516 $rawdata = decode_b32r($string);
517  
518 Decode a Base32 (rfc4648 alphabet) string into bytes.
519  
520 =head2 encode_b32b
521  
522 I<Since: CryptX-0.049>
523  
524 $string = encode_b32b($rawdata);
525  
526 Encode bytes into Base32 (base32hex alphabet) string, without "=" padding.
527  
528 =head2 decode_b32b
529  
530 I<Since: CryptX-0.049>
531  
532 $rawdata = decode_b32b($string);
533  
534 Decode a Base32 (base32hex alphabet) string into bytes.
535  
536 =head2 encode_b32z
537  
538 I<Since: CryptX-0.049>
539  
540 $string = encode_b32z($rawdata);
541  
542 Encode bytes into Base32 (zbase32 alphabet) string.
543  
544 =head2 decode_b32z
545  
546 I<Since: CryptX-0.049>
547  
548 $rawdata = decode_b32z($string);
549  
550 Decode a Base32 (zbase32 alphabet) string into bytes.
551  
552 =head2 encode_b32c
553  
554 I<Since: CryptX-0.049>
555  
556 $string = encode_b32c($rawdata);
557  
558 Encode bytes into Base32 (crockford alphabet) string.
559  
560 =head2 decode_b32c
561  
562 I<Since: CryptX-0.049>
563  
564 $rawdata = decode_b32c($string);
565  
566 Decode a Base32 (crockford alphabet) string into bytes.
567  
568 =head2 encode_b58b
569  
570 I<Since: CryptX-0.049>
571  
572 $string = encode_b58b($rawdata);
573  
574 Encode bytes into Base58 (Bitcoin alphabet) string.
575  
576 =head2 decode_b58b
577  
578 I<Since: CryptX-0.049>
579  
580 $rawdata = decode_b58b($string);
581  
582 Decode a Base58 (Bitcoin alphabet) string into bytes.
583  
584 =head2 encode_b58f
585  
586 I<Since: CryptX-0.049>
587  
588 $string = encode_b58f($rawdata);
589  
590 Encode bytes into Base58 (Flickr alphabet) string.
591  
592 =head2 decode_b58f
593  
594 I<Since: CryptX-0.049>
595  
596 $rawdata = decode_b58f($string);
597  
598 Decode a Base58 (Flickr alphabet) string into bytes.
599  
600 =head2 encode_b58r
601  
602 I<Since: CryptX-0.049>
603  
604 $string = encode_b58r($rawdata);
605  
606 Encode bytes into Base58 (Ripple alphabet) string.
607  
608 =head2 decode_b58r
609  
610 I<Since: CryptX-0.049>
611  
612 $rawdata = decode_b58r($string);
613  
614 Decode a Base58 (Ripple alphabet) string into bytes.
615  
616 =head2 encode_b58t
617  
618 I<Since: CryptX-0.049>
619  
620 $string = encode_b58t($rawdata);
621  
622 Encode bytes into Base58 (Tipple alphabet) string.
623  
624 =head2 decode_b58t
625  
626 I<Since: CryptX-0.049>
627  
628 $rawdata = decode_b58t($string);
629  
630 Decode a Base58 (Tipple alphabet) string into bytes.
631  
632 =head2 encode_b58s
633  
634 I<Since: CryptX-0.049>
635  
636 $string = encode_b58s($rawdata);
637  
638 Encode bytes into Base58 (Stellar alphabet) string.
639  
640 =head2 decode_b58s
641  
642 I<Since: CryptX-0.049>
643  
644 $rawdata = decode_b58s($string);
645  
646 Decode a Base58 (Stellar alphabet) string into bytes.
647  
648 =head1 SEE ALSO
649  
650 =over
651  
652 =item * L<CryptX>
653  
654 =back
655  
656 =cut
« blib/lib/Crypt/PK/DH.pm ↑ Summary blib/lib/Crypt/Mac.pm »