| 1 | package Mail::DKIM2::Digest; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 211 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 211 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 211 | our $VERSION = '0.34'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 211 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 211 | ▶ | use Crypt::Digest::SHA256 (); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 211 | ▶ | use Crypt::Digest::SHA512 (); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # The hashes over message content -- every Message-Instance's header and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | # body hashes -- are almost all of a verifier's work on a big message. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | # CryptX's SHA-2 is portable C (about 460 MB/s on an Apple M-series, 150 on | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | # the dkim2.com box); OpenSSL's uses the CPU's SHA instructions where there | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | # are some (3.3 GB/s and 270 MB/s). So OpenSSL's, through Net::SSLeay, when | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | # that is installed (it usually is: IO::Socket::SSL needs it), and CryptX's | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | # otherwise. The digests are the same. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 211 | my %CRYPTX = ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | sha256 => 'Crypt::Digest::SHA256', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | sha512 => 'Crypt::Digest::SHA512', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 211 | my ($OPENSSL, %MD); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 10168 | ▶ | sub openssl_available { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
openssl_available: called CC=5 SCAR=22.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | $OPENSSL //= eval { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
openssl_available: not called CC=5 SCAR=22.8
Pod
documented
Branch: 2 branches
Condition50%2/4
$OPENSSL //= eval {
do {
require Net::SSLeay;
&Net::SSLeay::OpenSSL_add_all_digests();
foreach my $alg (keys %CRYPTX) {
$MD{$alg} = &Net::SSLeay::EVP_get_digestbyname($alg) or die "no $alg\n";
};
1
}
} ? 1 : 0
$OPENSSL //= eval {
do {
require Net::SSLeay;
&Net::SSLeay::OpenSSL_add_all_digests();
foreach my $alg (keys %CRYPTX) {
$MD{$alg} = &Net::SSLeay::EVP_get_digestbyname($alg) or die "no $alg\n";
};
1
}
} ? 1 : 0
MC/DC100%1/1
$OPENSSL //= eval {
do {
require Net::SSLeay;
&Net::SSLeay::OpenSSL_add_all_digests();
foreach my $alg (keys %CRYPTX) {
$MD{$alg} = &Net::SSLeay::EVP_get_digestbyname($alg) or die "no $alg\n";
};
1
}
} ? 1 : 0
$OPENSSL
MC/DC0%0/1
$OPENSSL //= eval {
do {
require Net::SSLeay;
&Net::SSLeay::OpenSSL_add_all_digests();
foreach my $alg (keys %CRYPTX) {
$MD{$alg} = &Net::SSLeay::EVP_get_digestbyname($alg) or die "no $alg\n";
};
1
}
} ? 1 : 0
$OPENSSL
Truth table100%2/2
A
A= $OPENSSL
Truth table0%0/2
A
A= $OPENSSL
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | require Net::SSLeay; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | Net::SSLeay::OpenSSL_add_all_digests(); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | for my $alg (keys %CRYPTX) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | $MD{$alg} = Net::SSLeay::EVP_get_digestbyname($alg) or die "no $alg\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | } ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | return $OPENSSL; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | # 'openssl' or 'cryptx'; set (local) to choose, as the tests do. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 211 | our $BACKEND; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 10088 | ▶ | sub backend { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
backend: called CC=3 SCAR=15.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | $BACKEND //= openssl_available() ? 'openssl' : 'cryptx'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
backend: not called CC=3 SCAR=15.7
Pod
documented
Branch: 2 branches
Condition50%2/4
$Mail::DKIM2::Digest::BACKEND //= openssl_available() ? "openssl" : "cryptx"
$Mail::DKIM2::Digest::BACKEND //= openssl_available() ? "openssl" : "cryptx"
MC/DC100%1/1
$Mail::DKIM2::Digest::BACKEND //= openssl_available() ? "openssl" : "cryptx"
$Mail::DKIM2::Digest::BACKEND
MC/DC0%0/1
$Mail::DKIM2::Digest::BACKEND //= openssl_available() ? "openssl" : "cryptx"
$Mail::DKIM2::Digest::BACKEND
Truth table100%2/2
A
A= $Mail::DKIM2::Digest::BACKEND
Truth table0%0/2
A
A= $Mail::DKIM2::Digest::BACKEND
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | return $BACKEND; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | # new($alg): an object with add($data, ...) and digest(), as CryptX's. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 10087 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=18.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | my ($class, $alg) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=4 SCAR=18.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | my $cx = $CRYPTX{$alg} or croak "unsupported hash algorithm: $alg"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | return $cx->new unless backend() eq 'openssl' && openssl_available(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%2/6
backend() eq "openssl" and openssl_available()
backend() eq "openssl" and openssl_available()
MC/DC50%1/2
backend() eq "openssl" and openssl_available()
backend() eq "openssl"
openssl_available()
MC/DC0%0/2
backend() eq "openssl" and openssl_available()
backend() eq "openssl"
openssl_available()
Truth table66%2/3
A and B
A= backend() eq "openssl"B= openssl_available()
Truth table0%0/3
A and B
A= backend() eq "openssl"B= openssl_available()
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | my $ctx = Net::SSLeay::EVP_MD_CTX_create(); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | Net::SSLeay::EVP_DigestInit($ctx, $MD{$alg}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | return bless \$ctx, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | # Net::SSLeay hashes a string's buffer as it finds it, which for a string | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | # Perl holds as characters is the UTF-8 of them: the octets 0x80 to 0xFF | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | # would be hashed as two each. Such a string is hashed as the octets it | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | # stands for, as CryptX (and Digest::SHA) hash it; the caller's copy is left | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | # alone. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 15225 | ▶ | sub add { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add: called CC=4 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add: not called CC=4 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | for (@_) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | if (utf8::is_utf8($_)) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | my $octets = $_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | utf8::downgrade($octets, 1) or croak 'Wide character in data to hash'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | Net::SSLeay::EVP_DigestUpdate($$self, $octets); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | Net::SSLeay::EVP_DigestUpdate($$self, $_); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 9879 | ▶ | sub digest { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
digest: called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
digest: not called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | my $d = Net::SSLeay::EVP_DigestFinal($$self); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | Net::SSLeay::EVP_MD_CTX_destroy($$self); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | $$self = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | return $d; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 9901 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=9.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=2 SCAR=9.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | Net::SSLeay::EVP_MD_CTX_destroy($$self) if $$self; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | # digest($alg, $data): the raw digest of $data. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 6489 | ▶ | sub digest_data { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
digest_data: called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | my $d = __PACKAGE__->new($_[0]); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
digest_data: not called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | $d->add($_[1]); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | return $d->digest; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 211 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||