| 1 | # | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # Copyright (C) 2015-2023 Joelle Maslak | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # All Rights Reserved - See License | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | package Crypt::EAMessage; | ||||||||||||||||||||||||||||||||||||||||||
| 7 | 9 | $Crypt::EAMessage::VERSION = '1.232011'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use v5.22; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | # ABSTRACT: Simple-to-use Abstraction of Encrypted Authenticated Messages | ||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 9 | ▶ | use autodie; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 9 | ▶ | use feature "signatures"; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 9 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 9 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 9 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 9 | ▶ | no warnings "experimental::signatures"; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 9 | ▶ | use Bytes::Random::Secure; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 9 | ▶ | use Crypt::AuthEnc::CCM qw(ccm_encrypt_authenticate ccm_decrypt_verify); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 9 | ▶ | use MIME::Base64 qw(encode_base64 decode_base64); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 9 | ▶ | use Storable qw(nfreeze thaw); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 9 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 26 | ▶ | around 'BUILDARGS', sub ( $orig, $class, %args ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 26 | my (@only_one) = qw(raw_key hex_key); | |||||||||||||||||||||||||||||||||||||||||
| 36 | 26 | my $cnt = 0; | |||||||||||||||||||||||||||||||||||||||||
| 37 | 26 | foreach my $a (@only_one) { | |||||||||||||||||||||||||||||||||||||||||
| 38 | 52 | ▶ | if ( exists( $args{$a} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 26 | $cnt++; | |||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | if ( $cnt > 1 ) { die("Must not have multiple *_key arguments"); } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 26 | ▶ | if ( exists( $args{hex_key} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 19 | my $hex = $args{hex_key}; | |||||||||||||||||||||||||||||||||||||||||
| 46 | 19 | delete( $args{hex_key} ); | |||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 19 | $args{raw_key} = _hex_to_raw($hex); | |||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 23 | $class->$orig(%args); | |||||||||||||||||||||||||||||||||||||||||
| 52 | 9 | }; | |||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 24 | ▶ | sub _hex_to_raw ($hex) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_hex_to_raw: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 24 | $hex =~ s/^0x//; # Remove 0x leader if it is present | |||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 24 | ▶ | if ( $hex =~ /[^0-9A-Fa-f]/s ) { die("Non-hex characters present in hex_key"); } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 23 | my $l = length($hex); | |||||||||||||||||||||||||||||||||||||||||
| 60 | 23 | ▶ | if ( ( $l != 32 ) && ( $l != 48 ) && ( $l != 64 ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$l != 32 and $l != 48 and $l != 64
$l != 32 and $l != 48
MC/DC100%3/3
$l != 32 and $l != 48 and $l != 64
$l != 32
$l != 48
$l != 64
Truth table100%4/4
A and B and C
A= $l != 32B= $l != 48C= $l != 64
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 2 | die("hex_key is the wrong length"); | |||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 21 | return pack( 'H*', $hex ); | |||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | subtype 'Crypt::EAMessage::Key', as 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 31 | ▶ | where { _valid_key($_) }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 2 | ▶ | message { "AES key lengths must be 16, 24, or 32 bytes long" }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 31 | ▶ | sub _valid_key ($key) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_valid_key: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 31 | my $l = length($_); | |||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 31 | ▶ | if ( ( $l != 16 ) && ( $l != 24 ) && ( $l != 32 ) ) { return; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$l != 16 and $l != 24 and $l != 32
$l != 16 and $l != 24
MC/DC100%3/3
$l != 16 and $l != 24 and $l != 32
$l != 16
$l != 24
$l != 32
Truth table100%4/4
A and B and C
A= $l != 16B= $l != 24C= $l != 32
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 29 | ▶ | if ( utf8::is_utf8($key) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 1 | die("Key must not be UTF-8 encoded"); | |||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 28 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 9 | has 'raw_key' => ( | |||||||||||||||||||||||||||||||||||||||||
| 84 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 85 | isa => 'Crypt::EAMessage::Key', | ||||||||||||||||||||||||||||||||||||||||||
| 86 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 87 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 18 | ▶ | sub hex_key { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
hex_key: called CC=4 SCAR=15.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 18 | ▶ | if ( ( scalar(@_) < 1 ) || ( scalar(@_) > 2 ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
scalar @_ < 1 or scalar @_ > 2
MC/DC0%0/2
scalar @_ < 1 or scalar @_ > 2
scalar @_ < 1
scalar @_ > 2
Truth table33%1/3
A or B
A= scalar @_ < 1B= scalar @_ > 2
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | confess("Invalid call"); | |||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 18 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 18 | ▶ | if ( scalar(@_) == 1 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | # Setter | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 5 | $self->raw_key( _hex_to_raw(shift) ); | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 18 | return unpack( 'H*', $self->raw_key() ); | |||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 4 | ▶ | sub encrypt_auth ( $self, $input ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encrypt_auth: called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 2 | my $ct = $self->_encrypt_auth_internal($input); | |||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | return "1$ct"; # Type 1 = Binary Format | |||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 3 | ▶ | sub encrypt_auth_ascii ( $self, $input, $eol = undef ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encrypt_auth_ascii: called CC=2 SCAR=9.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 3 | my $ct = $self->_encrypt_auth_internal($input); | |||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | my $base64 = encode_base64( $ct, $eol ); | |||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | return "2$base64"; # Type 2 = Base 64 | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 7 | ▶ | sub _encrypt_auth_internal ( $self, $input, $opts = {} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_encrypt_auth_internal: called CC=8 SCAR=25.7
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 7 | state $random = Bytes::Random::Secure->new( Bits => 1024, NonBlocking => 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 7 | ▶ | if ( defined Scalar::Util::reftype($input) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 6 | ▶ | if ( Scalar::Util::reftype($input) eq "OBJECT" ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 6 | die("Cannot encrypt a perl class (new style) object"); | |||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | 1 | for my $opt ( sort keys %$opts ) { | |||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | if ( $opt eq 'text' ) { next; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | die("Unknown option to encrypt: $opt"); | |||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | 1 | my $nonce = $random->bytes(16); | |||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 1 | my $data; | |||||||||||||||||||||||||||||||||||||||||
| 136 | 1 | ▶ | if ( ( !exists( $opts->{text} ) ) && ( !$opts->{text} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $opts->{'text'} || $opts->{'text'}
MC/DC0%0/2
exists $opts->{'text'} || $opts->{'text'}
exists $opts->{'text'}
$opts->{'text'}
Truth table33%1/3
A || B
A= exists $opts->{'text'}B= $opts->{'text'}
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | # Any type of input | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 1 | $data = nfreeze( \$input ); | |||||||||||||||||||||||||||||||||||||||||
| 139 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 140 | # Text only input | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | $data = $input; | |||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | 1 | my ( $enc, $tag ) = | |||||||||||||||||||||||||||||||||||||||||
| 145 | ccm_encrypt_authenticate( 'AES', $self->raw_key(), $nonce, '', 128, $data ); | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | my $ct = $nonce . $tag . $enc; | |||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | return $ct; | |||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 3 | ▶ | sub encrypt_auth_urlsafe ( $self, $input ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encrypt_auth_urlsafe: called CC=1 SCAR=3.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 1 | my $ct = $self->_encrypt_auth_internal($input); | |||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | my $urltext = encode_base64( $ct, "" ); | |||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | $urltext =~ tr|\+/|-_|; | |||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | return "3$urltext"; # Type 3 = Modified Base 64 | |||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | 3 | ▶ | sub encrypt_auth_portable ( $self, $input ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encrypt_auth_portable: called CC=1 SCAR=3.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | 1 | my $ct = $self->_encrypt_auth_internal( $input, { text => 1 } ); | |||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | my $urltext = encode_base64( $ct, "" ); | |||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | $urltext =~ tr|\+/|-_|; | |||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | return "4$urltext"; # Type 3 = Modified Base 64 | |||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | 7 | ▶ | sub decrypt_auth ( $self, $ct ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
decrypt_auth: called CC=6 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | ▶ | if ( length($ct) < 34 ) { die("Message too short to be valid") } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | 5 | my $type = substr( $ct, 0, 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 176 | 5 | my $enc = substr( $ct, 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | 5 | ▶ | if ( $type eq '1' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 1 | return $self->_decrypt_auth_internal($enc); | |||||||||||||||||||||||||||||||||||||||||
| 180 | } elsif ( $type eq '2' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | my $ascii = decode_base64($enc); # It's okay if this ignores bad base64, | |||||||||||||||||||||||||||||||||||||||||
| 182 | # since we'll fail decryption. | ||||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | return $self->_decrypt_auth_internal($ascii); | |||||||||||||||||||||||||||||||||||||||||
| 184 | } elsif ( $type eq '3' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | $enc =~ tr|-_|+/|; | |||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | my $ascii = decode_base64($enc); # It's okay if this ignores bad base64, | |||||||||||||||||||||||||||||||||||||||||
| 187 | # since we'll fail decryption. | ||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | return $self->_decrypt_auth_internal($ascii); | |||||||||||||||||||||||||||||||||||||||||
| 189 | } elsif ( $type eq '4' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | $enc =~ tr|-_|+/|; | |||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | my $ascii = decode_base64($enc); # It's okay if this ignores bad base64, | |||||||||||||||||||||||||||||||||||||||||
| 192 | # since we'll fail decryption. | ||||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | return $self->_decrypt_auth_internal( $ascii, { text => 1 } ); | |||||||||||||||||||||||||||||||||||||||||
| 194 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 195 | 4 | die("Unsupported encoding type"); | |||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | 1 | ▶ | sub _decrypt_auth_internal ( $self, $ct, $opts = {} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_decrypt_auth_internal: called CC=8 SCAR=31.1
| |||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | ▶ | if ( length($ct) < 32 ) { die("Message too short to be valid") } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | 1 | for my $opt ( sort keys %$opts ) { | |||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | ▶ | if ( $opt eq 'text' ) { next; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | die("Unknown option to decrypt: $opt"); | |||||||||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | 1 | my $nonce = substr( $ct, 0, 16 ); | |||||||||||||||||||||||||||||||||||||||||
| 209 | 1 | my $tag = substr( $ct, 16, 16 ); | |||||||||||||||||||||||||||||||||||||||||
| 210 | 1 | my $enc = substr( $ct, 32 ); | |||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | 1 | my $frozen = ccm_decrypt_verify( 'AES', $self->raw_key(), $nonce, '', $enc, $tag ); | |||||||||||||||||||||||||||||||||||||||||
| 213 | 1 | ▶ | if ( !defined($frozen) ) { die("Could not decrypt message") } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | 0 | ▶ | if ( ( !exists( $opts->{text} ) ) && ( !$opts->{text} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $opts->{'text'} || $opts->{'text'}
MC/DC0%0/2
exists $opts->{'text'} || $opts->{'text'}
exists $opts->{'text'}
$opts->{'text'}
Truth table0%0/3
A || B
A= exists $opts->{'text'}B= $opts->{'text'}
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | # Perl 5 data structure | ||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | my $plaintext = thaw($frozen); | |||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | return $$plaintext; | |||||||||||||||||||||||||||||||||||||||||
| 219 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 220 | # Plain text | ||||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | return $frozen; | |||||||||||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | 5 | ▶ | sub generate_key ($self) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
generate_key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | 4 | return Bytes::Random::Secure::random_bytes_hex(32); | |||||||||||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | 9 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | 9 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | __END__ | ||||||||||||||||||||||||||||||||||||||||||