blib/lib/Vigil/Crypt.pm

filter: Statementstmt 80.0% 68 / 85 Branchbran 50.0% 8 / 16 Conditioncond 35.7% 10 / 28 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 85.7% 12 / 14 Podpod 85.7% 6 / 7 totaltotal 62.6% 104 / 166 CC 21 SCAR 17.9
CC
21
Cov
67%
decrypt
12.2
encrypt
8.1
last_error
6.0
CRAP 6.0
↑ Summary
1 package Vigil::Crypt;
2
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6 2 ▶use Bytes::Random::Secure qw(random_bytes);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Crypt::AuthEnc::ChaCha20Poly1305;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Digest::SHA qw(sha256);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Crypt::Argon2 qw(argon2id_pass argon2_verify);
Subroutine BEGIN: called CC=1 SCAR=0.0
10
11 2 our $VERSION = '2.1.2';
12
13 1 ▶sub new {
Subroutine new: called CC=3 SCAR=14.2
Pod documented
14 1 my ($class, $encryption_key) = @_;
15 1 ▶ unless(defined $encryption_key && length($encryption_key) == 64) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $encryption_key and length $encryption_key == 64) 0 1 1
Condition33%1/3
defined $encryption_key and length $encryption_key == 64
!ll&&!rl&&r
001
MC/DC0%0/2
defined $encryption_key and length $encryption_key == 64
defined $encryption_key length $encryption_key == 64
Truth table33%1/3
A and B
A= defined $encryption_keyB= length $encryption_key == 64
ABresult
0X0
100
111
16 0 warn 'Vigil::Crypt was given an invalid encryption key.';
17 0 return;
18 }
19 1 bless { _encryption_key => pack("H*", $encryption_key), _last_error => '' }, $class;
20 }
21
22 0 ▶sub last_error { return $_[0]->{_last_error} // ''; }
Subroutine last_error: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
$_[0]{"_last_error"} // ""
l!l
00
MC/DC0%0/1
$_[0]{"_last_error"} // ""
$_[0]{"_last_error"}
Truth table0%0/2
A
A= $_[0]{"_last_error"}
Aresult
11
00
23
24 # ----------------------
25 # Encrypt a plaintext string
26 # ----------------------
27 1 ▶sub encrypt {
Subroutine encrypt: called CC=6 SCAR=20.9
Pod documented
28 1 my ($self, $plaintext, $aad1, $aad2) = @_;
29
30 1 $self->{_last_error} = '';
31
32 # validate plaintext
33 1 ▶ unless (defined $plaintext && length $plaintext) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $plaintext and length $plaintext) 0 1 1
Condition33%1/3
defined $plaintext and length $plaintext
!ll&&!rl&&r
001
MC/DC0%0/2
defined $plaintext and length $plaintext
defined $plaintext length $plaintext
Truth table33%1/3
A and B
A= defined $plaintextB= length $plaintext
ABresult
0X0
100
111
34 0 $self->{_last_error} = "missing plaintext";
35 0 return;
36 }
37
38 1 my $aad = $self->_derive_key($aad1, $aad2);
39
40 # optional: if _derive_key returns undef, just use empty string
41 1 ▶ $aad //= '';
Condition50%1/2
$aad //= ""
l!l
10
MC/DC0%0/1
$aad //= ""
$aad
Truth table50%1/2
A
A= $aad
Aresult
11
00
42
43 1 my $nonce = random_bytes(12);
44
45 1 my $blob;
46 eval {
47 1 my $cipher = Crypt::AuthEnc::ChaCha20Poly1305->new($self->{_encryption_key}, $nonce);
48 1 $cipher->adata_add($aad);
49 1 my $ct = $cipher->encrypt_add($plaintext);
50 1 my $tag = $cipher->encrypt_done();
51 1 $blob = $nonce . $ct . $tag; # assign to outer $blob
52 1 1;
53 1 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $cipher = "Crypt::AuthEnc::ChaCha20Poly1305"->new($self->{"_encryption_key"}, $nonce); $cipher->adata_add($aad); my $ct = $cipher->encrypt_add($plaintext); my $tag = $cipher->encrypt_done; $blob = $nonce . $ct . $tag; 1 } } or do { my $err = $@ || "Unknown encryption error"; $self->{"_last_error"} = "encrypt failed: $err"; return } 1 0 1
54 0 ▶ my $err = $@ || 'Unknown encryption error';
Condition0%0/2
$@ || 'Unknown encryption error'
l!l
00
MC/DC0%0/1
$@ || 'Unknown encryption error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
55 0 $self->{_last_error} = "encrypt failed: $err";
56 0 return;
57 };
58
59 # return hex string for storage
60 1 return unpack("H*", $blob);
61 }
62
63 # ----------------------
64 # Decrypt a hex-encoded ciphertext string
65 # ----------------------
66 1 ▶sub decrypt {
Subroutine decrypt: called CC=8 SCAR=25.1
Pod documented
67 1 my ($self, $blob_hex, $aad1, $aad2) = @_;
68
69 1 $self->{_last_error} = '';
70
71 1 ▶ unless (defined $blob_hex && length $blob_hex) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $blob_hex and length $blob_hex) 0 1 1
Condition33%1/3
defined $blob_hex and length $blob_hex
!ll&&!rl&&r
001
MC/DC0%0/2
defined $blob_hex and length $blob_hex
defined $blob_hex length $blob_hex
Truth table33%1/3
A and B
A= defined $blob_hexB= length $blob_hex
ABresult
0X0
100
111
72 0 $self->{_last_error} = "missing blob";
73 0 return;
74 }
75
76 1 my $decoded = eval { pack("H*", $blob_hex) };
77 1 ▶ if ($@ || !defined $decoded) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not defined $decoded) 0 1 1
Condition33%1/3
$@ or not defined $decoded
l!l&&r!l&&!r
001
MC/DC0%0/2
$@ or not defined $decoded
$@ not defined $decoded
Truth table33%1/3
A or not(B)
A= $@B= not defined $decoded
ABresult
1X1
011
000
78 0 $self->{_last_error} = "invalid hex blob";
79 0 return;
80 }
81
82 1 ▶ if (length($decoded) < 28) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $decoded < 28) 0 1 1
83 0 $self->{_last_error} = "blob too short";
84 0 return;
85 }
86
87 1 my $aad = $self->_derive_key($aad1, $aad2);
88 1 ▶ $aad //= '';
Condition50%1/2
$aad //= ""
l!l
10
MC/DC0%0/1
$aad //= ""
$aad
Truth table50%1/2
A
A= $aad
Aresult
11
00
89
90 1 my $nonce = substr($decoded, 0, 12);
91 1 my $tag = substr($decoded, -16); # last 16 bytes is the Poly1305 tag
92 1 my $ciphertext = substr($decoded, 12, -16); # between nonce and tag
93
94 1 my $cipher = Crypt::AuthEnc::ChaCha20Poly1305->new($self->{_encryption_key}, $nonce);
95 1 $cipher->adata_add($aad);
96 1 my $plaintext = $cipher->decrypt_add($ciphertext);
97 1 my $ok = $cipher->decrypt_done($tag); # returns 1 if auth ok
98 1 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 1 1
99 0 $self->{_last_error} = "decrypt failed: authentication failed";
100 0 return;
101 }
102 1 return $plaintext;
103 }
104
105 # ----------------------
106 # Hashing functions
107 # ----------------------
108 1 ▶sub hash {
Subroutine hash: called CC=2 SCAR=7.1
Pod documented
109 1 my ($self, $password, $pepper) = @_;
110 1 ▶ $pepper //= '';
Condition50%1/2
$pepper //= ""
l!l
10
MC/DC0%0/1
$pepper //= ""
$pepper
Truth table50%1/2
A
A= $pepper
Aresult
11
00
111 1 return argon2id_pass(
112 sha256($password . $pepper),
113 random_bytes(16),
114 3, '32M', 1, 32
115 );
116 }
117
118 1 ▶sub verify_hash {
Subroutine verify_hash: called CC=2 SCAR=7.1
Pod undocumented
119 1 my ($self, $user_input, $stored_hash, $pepper) = @_;
120 1 ▶ $pepper //= '';
Condition50%1/2
$pepper //= ""
l!l
10
MC/DC0%0/1
$pepper //= ""
$pepper
Truth table50%1/2
A
A= $pepper
Aresult
11
00
121 1 return argon2_verify($stored_hash, sha256($user_input . $pepper));
122 }
123
124 0 ▶sub verify_password { return shift->verify_hash(@_); }
Subroutine verify_password: not called CC=1 SCAR=6.9
Pod documented
125
126 2 ▶sub _derive_key {
Subroutine _derive_key: called CC=4 SCAR=14.5
127 2 my ($self, $a, $b) = @_;
128 2 ▶ $a //= '';
Condition50%1/2
$a //= ""
l!l
20
MC/DC0%0/1
$a //= ""
$a
Truth table50%1/2
A
A= $a
Aresult
11
00
129 2 ▶ $b //= '';
Condition50%1/2
$b //= ""
l!l
02
MC/DC0%0/1
$b //= ""
$b
Truth table50%1/2
A
A= $b
Aresult
11
00
130 2 my $pepper = substr("1010" . $a, -4, 4) . substr($b . "101010101", 0, 9);
131 2 ▶ return undef unless length($pepper) == 13;
Branch: 1 branch
BranchTrueFalseTotal
unless length $pepper == 13 0 2 2
132 2 return $pepper;
133 }
134
135 2 1;
136
137
138 __END__
↑ Summary