RNCryptor/V3.pm

filter: Statementstmt 28.5% 28 / 98 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 48 MC/DCmcdc 0.0% 0 / 31 Subroutinesub 47.3% 9 / 19 Podpod 30.0% 3 / 10 totaltotal 18.0% 40 / 222 CC 25 SCAR 29.0
CC
25
Cov
17%
encrypt_with_password
42.0
new
30.0
encrypt
20.0
CRAP 18.2
↑ Summary RNCryptor.pm »
1 package Crypt::RNCryptor::V3;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use parent 'Crypt::RNCryptor';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Crypt::PBKDF2;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Crypt::CBC;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Digest::SHA qw(hmac_sha256);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 VERSION => 3,
12 # option
13 OPTION_USE_PASSWORD => 1,
14 OPTION_NOT_USE_PASSWORD => 0,
15 # size
16 VERSION_SIZE => 1,
17 OPTIONS_SIZE => 1,
18 ENCRYPTION_SALT_SIZE => 8,
19 HMAC_SALT_SIZE => 8,
20 IV_SIZE => 16,
21 HMAC_SIZE => 32,
22 # PBKDF2
23 DEFAULT_PBKDF2_ITERATIONS => 10000,
24 PBKDF2_OUTPUT_SIZE => 32,
25 1 };
26  
27 1 ▶use Class::Accessor::Lite (
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ro => [qw(
29 password pbkdf2_iterations
30 encryption_key hmac_key
31 )],
32 1 );
33  
34 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
35 0 my ($class, %opts) = @_;
36 0 ▶ if ($opts{password} && ($opts{encryption_key} || $opts{hmac_key})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"password"} and $opts{"encryption_key"} || $opts{"hmac_key"}) 0 0 0
Condition0%0/6
$opts{"password"} and $opts{"encryption_key"} || $opts{"hmac_key"}
!ll&&!rl&&r
000
$opts{"encryption_key"} or $opts{"hmac_key"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$opts{"password"} and $opts{"encryption_key"} || $opts{"hmac_key"}
$opts{"password"} $opts{"encryption_key"} $opts{"hmac_key"}
Truth table0%0/4
A and B or C
A= $opts{"password"}B= $opts{"encryption_key"}C= $opts{"hmac_key"}
ABCresult
0XX0
1000
11X1
1011
37 0 confess 'Cannot set the "password" option with "encryption_key" or "hmac_key" option.';
38 }
39 0 ▶ if ($opts{pbkdf2_iterations}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"pbkdf2_iterations"}) 0 0 0
40 0 confess 'v3.1 is not supported still yet.';
41 }
42 bless {
43 password => $opts{password},
44 encryption_key => $opts{encryption_key},
45 hmac_key => $opts{hmac_key},
46 0 pbkdf2_iterations => DEFAULT_PBKDF2_ITERATIONS,
47 }, $class;
48 }
49  
50 0 ▶sub pbkdf2 {
Subroutine pbkdf2: not called CC=2 SCAR=17.9
Pod undocumented
51 0 my ($self, $password, $salt, $iterations) = @_;
52 0 ▶ $iterations ||= $self->pbkdf2_iterations;
Condition0%0/3
$iterations ||= $self->pbkdf2_iterations
l!l&&r!l&&!r
000
MC/DC0%0/2
$iterations ||= $self->pbkdf2_iterations
$iterations $self->pbkdf2_iterations
Truth table0%0/3
A ||= B
A= $iterationsB= $self->pbkdf2_iterations
ABresult
1X1
011
000
53 0 Crypt::PBKDF2->new(
54 hash_class => 'HMACSHA1',
55 iterations => $iterations,
56 output_len => PBKDF2_OUTPUT_SIZE,
57 )->PBKDF2($salt, $password);
58 }
59  
60 0 ▶sub aes256cbc {
Subroutine aes256cbc: not called CC=1 SCAR=6.9
Pod undocumented
61 0 my ($self, $encryption_key, $iv) = @_;
62 0 Crypt::CBC->new(
63 -literal_key => 1,
64 -key => $encryption_key,
65 -iv => $iv,
66 -header => 'none',
67 -cipher => 'Crypt::OpenSSL::AES',
68 );
69 }
70  
71 0 ▶sub make_options {
Subroutine make_options: not called CC=1 SCAR=6.9
Pod undocumented
72 0 my ($self, $use_password, $pbkdf2_iterations) = @_;
73 0 confess 'TODO';
74 }
75  
76 0 ▶sub encrypt {
Subroutine encrypt: not called CC=4 SCAR=30.0
Pod documented
77 0 my $self = shift;
78 0 ▶ return $self->encrypt_with_password(@_) if $self->password;
Branch: 1 branch
BranchTrueFalseTotal
if $self->password 0 0 0
79 0 ▶ return $self->encrypt_with_keys(@_) if $self->encryption_key && $self->hmac_key;
Branch: 1 branch
BranchTrueFalseTotal
if $self->encryption_key and $self->hmac_key 0 0 0
Condition0%0/3
$self->encryption_key and $self->hmac_key
!ll&&!rl&&r
000
MC/DC0%0/2
$self->encryption_key and $self->hmac_key
$self->encryption_key $self->hmac_key
Truth table0%0/3
A and B
A= $self->encryption_keyB= $self->hmac_key
ABresult
0X0
100
111
80 0 confess 'Cannot encrypt.';
81 }
82  
83 0 ▶sub encrypt_with_password {
Subroutine encrypt_with_password: not called CC=6 SCAR=37.4
Pod undocumented
84 0 my ($self, $plaintext, %opts) = @_;
85 0 ▶ my $iv = $opts{iv} || Crypt::CBC->random_bytes(IV_SIZE);
Condition0%0/3
$opts{'iv'} || 'Crypt::CBC'->random_bytes(16)
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'iv'} || 'Crypt::CBC'->random_bytes(16)
$opts{'iv'} 'Crypt::CBC'->random_bytes(16)
Truth table0%0/3
A || B
A= $opts{'iv'}B= 'Crypt::CBC'->random_bytes(16)
ABresult
1X1
011
000
86 0 ▶ my $encryption_salt = $opts{encryption_salt} || Crypt::CBC->random_bytes(ENCRYPTION_SALT_SIZE);
Condition0%0/3
$opts{'encryption_salt'} || 'Crypt::CBC'->random_bytes(8)
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'encryption_salt'} || 'Crypt::CBC'->random_bytes(8)
$opts{'encryption_salt'} 'Crypt::CBC'->random_bytes(8)
Truth table0%0/3
A || B
A= $opts{'encryption_salt'}B= 'Crypt::CBC'->random_bytes(8)
ABresult
1X1
011
000
87 0 ▶ my $hmac_salt = $opts{hmac_salt} || Crypt::CBC->random_bytes(HMAC_SALT_SIZE);
Condition0%0/3
$opts{'hmac_salt'} || 'Crypt::CBC'->random_bytes(8)
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'hmac_salt'} || 'Crypt::CBC'->random_bytes(8)
$opts{'hmac_salt'} 'Crypt::CBC'->random_bytes(8)
Truth table0%0/3
A || B
A= $opts{'hmac_salt'}B= 'Crypt::CBC'->random_bytes(8)
ABresult
1X1
011
000
88 0 ▶ my $password = $opts{password} || $self->password;
Condition0%0/3
$opts{'password'} || $self->password
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'password'} || $self->password
$opts{'password'} $self->password
Truth table0%0/3
A || B
A= $opts{'password'}B= $self->password
ABresult
1X1
011
000
89 0 ▶ my $pbkdf2_iterations = $opts{pbkdf2_iterations} || $self->pbkdf2_iterations;
Condition0%0/3
$opts{'pbkdf2_iterations'} || $self->pbkdf2_iterations
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'pbkdf2_iterations'} || $self->pbkdf2_iterations
$opts{'pbkdf2_iterations'} $self->pbkdf2_iterations
Truth table0%0/3
A || B
A= $opts{'pbkdf2_iterations'}B= $self->pbkdf2_iterations
ABresult
1X1
011
000
90  
91 0 my $encryption_key = $self->pbkdf2($password, $encryption_salt);
92 0 my $hmac_key = $self->pbkdf2($password, $hmac_salt);
93  
94 # Header = 3 || 1 || EncryptionSalt || HMACSalt || IV
95 0 my $header = pack('CCa*a*a*', VERSION, OPTION_USE_PASSWORD, $encryption_salt, $hmac_salt, $iv);
96 # Ciphertext = AES256(plaintext, ModeCBC, IV, EncryptionKey)
97 0 my $ciphertext = $self->aes256cbc($encryption_key, $iv)->encrypt($plaintext);
98 0 my $cipherdata = pack('a*a*', $header, $ciphertext);
99 # HMAC = HMAC(Header || Ciphertext, HMACKey, SHA-256)
100 0 my $hmac = hmac_sha256($cipherdata, $hmac_key);
101 # Message = Header || Ciphertext || HMAC
102 0 pack('a*a*', $cipherdata, $hmac);
103 }
104  
105 0 ▶sub encrypt_with_keys {
Subroutine encrypt_with_keys: not called CC=4 SCAR=30.0
Pod undocumented
106 0 my ($self, $plaintext, %opts) = @_;
107 0 ▶ my $iv = $opts{iv} || Crypt::CBC->random_bytes(IV_SIZE);
Condition0%0/3
$opts{'iv'} || 'Crypt::CBC'->random_bytes(16)
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'iv'} || 'Crypt::CBC'->random_bytes(16)
$opts{'iv'} 'Crypt::CBC'->random_bytes(16)
Truth table0%0/3
A || B
A= $opts{'iv'}B= 'Crypt::CBC'->random_bytes(16)
ABresult
1X1
011
000
108 0 ▶ my $encryption_key = $opts{encryption_key} || $self->encryption_key;
Condition0%0/3
$opts{'encryption_key'} || $self->encryption_key
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'encryption_key'} || $self->encryption_key
$opts{'encryption_key'} $self->encryption_key
Truth table0%0/3
A || B
A= $opts{'encryption_key'}B= $self->encryption_key
ABresult
1X1
011
000
109 0 ▶ my $hmac_key = $opts{hmac_key} || $self->hmac_key;
Condition0%0/3
$opts{'hmac_key'} || $self->hmac_key
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'hmac_key'} || $self->hmac_key
$opts{'hmac_key'} $self->hmac_key
Truth table0%0/3
A || B
A= $opts{'hmac_key'}B= $self->hmac_key
ABresult
1X1
011
000
110 # Header = 3 || 0 || IV
111 0 my $header = pack('CCa*', VERSION, OPTION_NOT_USE_PASSWORD, $iv);
112 # Ciphertext = AES256(plaintext, ModeCBC, IV, EncryptionKey)
113 0 my $ciphertext = $self->aes256cbc($encryption_key, $iv)->encrypt($plaintext);
114 0 my $cipherdata = pack('a*a*', $header, $ciphertext);
115 # HMAC = HMAC(Header || Ciphertext, HMACKey, SHA-256)
116 0 my $hmac = hmac_sha256($cipherdata, $hmac_key);
117 # Message = Header || Ciphertext || HMAC
118 0 pack('a*a*', $cipherdata, $hmac);
119 }
120  
121 0 ▶sub decrypt {
Subroutine decrypt: not called CC=4 SCAR=30.0
Pod documented
122 0 my $self = shift;
123 0 ▶ return $self->decrypt_with_password(@_) if $self->password;
Branch: 1 branch
BranchTrueFalseTotal
if $self->password 0 0 0
124 0 ▶ return $self->decrypt_with_keys(@_) if $self->encryption_key && $self->hmac_key;
Branch: 1 branch
BranchTrueFalseTotal
if $self->encryption_key and $self->hmac_key 0 0 0
Condition0%0/3
$self->encryption_key and $self->hmac_key
!ll&&!rl&&r
000
MC/DC0%0/2
$self->encryption_key and $self->hmac_key
$self->encryption_key $self->hmac_key
Truth table0%0/3
A and B
A= $self->encryption_keyB= $self->hmac_key
ABresult
0X0
100
111
125 0 confess 'Cannot decrypt.';
126 }
127  
128 0 ▶sub decrypt_with_password {
Subroutine decrypt_with_password: not called CC=3 SCAR=24.8
Pod undocumented
129 0 my ($self, $message, %opts) = @_;
130 0 ▶ my $password = $opts{password} || $self->password;
Condition0%0/3
$opts{'password'} || $self->password
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'password'} || $self->password
$opts{'password'} $self->password
Truth table0%0/3
A || B
A= $opts{'password'}B= $self->password
ABresult
1X1
011
000
131 0 my ($header, $ciphertext, $hmac) = do {
132 0 my $header_size = VERSION_SIZE + OPTIONS_SIZE + ENCRYPTION_SALT_SIZE + HMAC_SALT_SIZE + IV_SIZE;
133 0 my $fmt = sprintf('a%da%da%d',
134 $header_size,
135 length($message) - $header_size - HMAC_SIZE,
136 HMAC_SIZE);
137 0 unpack($fmt, $message);
138 };
139 0 my ($version, $options, $encryption_salt, $hmac_salt, $iv) = do {
140 0 my $fmt = sprintf('CCa%da%da%d', ENCRYPTION_SALT_SIZE, HMAC_SALT_SIZE, IV_SIZE);
141 0 unpack($fmt, $header);
142 };
143  
144 # compare HMAC
145 0 my $hmac_key = $self->pbkdf2($password, $hmac_salt);
146 0 my $computed_hmac = hmac_sha256(pack('a*a*', $header, $ciphertext), $hmac_key);
147 0 ▶ die "HMAC is not matched.\n" unless $computed_hmac eq $hmac;
Branch: 1 branch
BranchTrueFalseTotal
unless $computed_hmac eq $hmac 0 0 0
148  
149 # decrypt
150 0 my $encryption_key = $self->pbkdf2($password, $encryption_salt);
151 0 $self->aes256cbc($encryption_key, $iv)->decrypt($ciphertext);
152 }
153  
154 0 ▶sub decrypt_with_keys {
Subroutine decrypt_with_keys: not called CC=4 SCAR=30.0
Pod undocumented
155 0 my ($self, $message, %opts) = @_;
156 0 ▶ my $encryption_key = $opts{encryption_key} || $self->encryption_key;
Condition0%0/3
$opts{'encryption_key'} || $self->encryption_key
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'encryption_key'} || $self->encryption_key
$opts{'encryption_key'} $self->encryption_key
Truth table0%0/3
A || B
A= $opts{'encryption_key'}B= $self->encryption_key
ABresult
1X1
011
000
157 0 ▶ my $hmac_key = $opts{hmac_key} || $self->hmac_key;
Condition0%0/3
$opts{'hmac_key'} || $self->hmac_key
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'hmac_key'} || $self->hmac_key
$opts{'hmac_key'} $self->hmac_key
Truth table0%0/3
A || B
A= $opts{'hmac_key'}B= $self->hmac_key
ABresult
1X1
011
000
158  
159 0 my ($header, $ciphertext, $hmac) = do {
160 0 my $header_size = VERSION_SIZE + OPTIONS_SIZE + IV_SIZE;
161 0 my $fmt = sprintf('a%da%da%d',
162 $header_size,
163 length($message) - $header_size - HMAC_SIZE,
164 HMAC_SIZE);
165 0 unpack($fmt, $message);
166 };
167 0 my ($version, $options, $iv) = do {
168 0 my $fmt = sprintf('CCa%d', IV_SIZE);
169 0 unpack($fmt, $header);
170 };
171  
172 # compare HMAC
173 0 my $computed_hmac = hmac_sha256(pack('a*a*', $header, $ciphertext), $hmac_key);
174 0 ▶ die "HMAC is not matched.\n" unless $computed_hmac eq $hmac;
Branch: 1 branch
BranchTrueFalseTotal
unless $computed_hmac eq $hmac 0 0 0
175  
176 # Plaintext = AES256Decrypt(Ciphertext, ModeCBC, IV, EncryptionKey)
177 0 $self->aes256cbc($encryption_key, $iv)->decrypt($ciphertext);
178 }
179  
180 1 1;
181  
182 __END__
↑ Summary RNCryptor.pm »