| 1 | package Crypt::OpenPGP::Ciphertext; | ||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 3 | our $VERSION = '1.21'; # VERSION | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Crypt::OpenPGP::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Crypt::OpenPGP::Cipher; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Crypt::OpenPGP::Constants qw( DEFAULT_CIPHER | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | PGP_PKT_ENCRYPTED | ||||||||||||||||||||||||||||
| 11 | 3 | PGP_PKT_ENCRYPTED_MDC ); | |||||||||||||||||||||||||||
| 12 | 3 | ▶ | use parent qw( Crypt::OpenPGP::ErrorHandler ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use constant MDC_TRAILER => chr(0xd3) . chr(0x14); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | sub pkt_type { $_[0]->{is_mdc} ? PGP_PKT_ENCRYPTED_MDC : PGP_PKT_ENCRYPTED } | ||||||||||||||||||||||||||
|
Subroutine
pkt_type: called CC=2 SCAR=7.6
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 6 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 6 | my $class = shift; | |||||||||||||||||||||||||||
| 20 | 6 | my $enc = bless { }, $class; | |||||||||||||||||||||||||||
| 21 | 6 | $enc->init(@_); | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 6 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=7 SCAR=21.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 25 | 6 | my $enc = shift; | |||||||||||||||||||||||||||
| 26 | 6 | my %param = @_; | |||||||||||||||||||||||||||
| 27 | 6 | ▶ | if ((my $key = $param{SymKey}) && (my $data = $param{Data})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
my $key = $param{"SymKey"} and my $data = $param{"Data"}
MC/DC50%1/2
my $key = $param{"SymKey"} and my $data = $param{"Data"}
my $key = $param{"SymKey"}
my $data = $param{"Data"}
Truth table66%2/3
A and B
A= my $key = $param{"SymKey"}B= my $data = $param{"Data"}
| |||||||||||||||||||||||||||||
| 28 | 2 | ▶ | $enc->{is_mdc} = $param{MDC} || 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$param{'MDC'} || 0
MC/DC0%0/1
$param{'MDC'} || 0
$param{'MDC'}
Truth table50%1/2
A
A= $param{'MDC'}
| |||||||||||||||||||||||||||||
| 29 | 2 | $enc->{version} = 1; | |||||||||||||||||||||||||||
| 30 | 2 | ▶ | my $alg = $param{Cipher} || DEFAULT_CIPHER; | ||||||||||||||||||||||||||
|
Condition33%1/3
$param{'Cipher'} || &DEFAULT_CIPHER()
MC/DC0%0/2
$param{'Cipher'} || &DEFAULT_CIPHER()
$param{'Cipher'}
&DEFAULT_CIPHER()
Truth table33%1/3
A || B
A= $param{'Cipher'}B= &DEFAULT_CIPHER()
| |||||||||||||||||||||||||||||
| 31 | 2 | my $cipher = Crypt::OpenPGP::Cipher->new($alg, $key); | |||||||||||||||||||||||||||
| 32 | 2 | my $bs = $cipher->blocksize; | |||||||||||||||||||||||||||
| 33 | 2 | my $pad = Crypt::OpenPGP::Util::get_random_bytes($bs); | |||||||||||||||||||||||||||
| 34 | 2 | $pad .= substr $pad, -2, 2; | |||||||||||||||||||||||||||
| 35 | 2 | $enc->{ciphertext} = $cipher->encrypt($pad); | |||||||||||||||||||||||||||
| 36 | 2 | ▶ | $cipher->sync unless $enc->{is_mdc}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 2 | $enc->{ciphertext} .= $cipher->encrypt($data); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 2 | ▶ | if ($enc->{is_mdc}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 0 | require Crypt::OpenPGP::MDC; | |||||||||||||||||||||||||||
| 41 | 0 | my $mdc = Crypt::OpenPGP::MDC->new( | |||||||||||||||||||||||||||
| 42 | Data => $pad . $data . MDC_TRAILER ); | ||||||||||||||||||||||||||||
| 43 | 0 | my $mdc_buf = Crypt::OpenPGP::PacketFactory->save($mdc); | |||||||||||||||||||||||||||
| 44 | 0 | $enc->{ciphertext} .= $cipher->encrypt($mdc_buf); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 6 | $enc; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 4 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 4 | my $class = shift; | |||||||||||||||||||||||||||
| 52 | 4 | my($buf, $is_mdc) = @_; | |||||||||||||||||||||||||||
| 53 | 4 | my $enc = $class->new; | |||||||||||||||||||||||||||
| 54 | 4 | $enc->{is_mdc} = $is_mdc; | |||||||||||||||||||||||||||
| 55 | 4 | ▶ | if ($is_mdc) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 1 | $enc->{version} = $buf->get_int8; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | 4 | $enc->{ciphertext} = $buf->get_bytes($buf->length - $buf->offset); | |||||||||||||||||||||||||||
| 59 | 4 | $enc; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 2 | ▶ | sub save { | ||||||||||||||||||||||||||
|
Subroutine
save: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 2 | my $enc = shift; | |||||||||||||||||||||||||||
| 64 | 2 | my $buf = Crypt::OpenPGP::Buffer->new; | |||||||||||||||||||||||||||
| 65 | 2 | ▶ | if ($enc->{is_mdc}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | $buf->put_int8($enc->{version}); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 2 | $buf->put_bytes($enc->{ciphertext}); | |||||||||||||||||||||||||||
| 69 | 2 | $buf->bytes; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | sub display { | ||||||||||||||||||||||||||
|
Subroutine
display: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 73 | 0 | my $enc = shift; | |||||||||||||||||||||||||||
| 74 | my $str = ":encrypted data packet:\n" . | ||||||||||||||||||||||||||||
| 75 | 0 | " length: " . length($enc->{ciphertext}) . "\n"; | |||||||||||||||||||||||||||
| 76 | 0 | ▶ | if ($enc->{is_mdc}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | $str .= " is_mdc: $enc->{version}\n"; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 0 | $str; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 2 | ▶ | sub decrypt { | ||||||||||||||||||||||||||
|
Subroutine
decrypt: called CC=8 SCAR=30.1
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 2 | my $enc = shift; | |||||||||||||||||||||||||||
| 84 | 2 | my($key, $sym_alg) = @_; | |||||||||||||||||||||||||||
| 85 | 2 | ▶ | my $cipher = Crypt::OpenPGP::Cipher->new($sym_alg, $key) or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | return $enc->error( Crypt::OpenPGP::Cipher->errstr ); | ||||||||||||||||||||||||||||
| 87 | 2 | my $padlen = $cipher->blocksize + 2; | |||||||||||||||||||||||||||
| 88 | my $pt = $enc->{prefix} = | ||||||||||||||||||||||||||||
| 89 | 2 | $cipher->decrypt(substr $enc->{ciphertext}, 0, $padlen); | |||||||||||||||||||||||||||
| 90 | 2 | ▶ | return $enc->error("Bad checksum") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | unless substr($pt, -4, 2) eq substr($pt, -2, 2); | ||||||||||||||||||||||||||||
| 92 | 2 | ▶ | $cipher->sync unless $enc->{is_mdc}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 2 | $pt = $cipher->decrypt(substr $enc->{ciphertext}, $padlen); | |||||||||||||||||||||||||||
| 94 | 2 | ▶ | if ($enc->{is_mdc}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | my $mdc_buf = Crypt::OpenPGP::Buffer->new_with_init(substr $pt,-22,22); | |||||||||||||||||||||||||||
| 96 | 0 | $pt = substr $pt, 0, -22; | |||||||||||||||||||||||||||
| 97 | 0 | my $mdc = Crypt::OpenPGP::PacketFactory->parse($mdc_buf); | |||||||||||||||||||||||||||
| 98 | 0 | ▶ | return $enc->error("Encrypted MDC packet without MDC") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$mdc and ref $mdc eq "Crypt::OpenPGP::MDC"
MC/DC0%0/2
$mdc and ref $mdc eq "Crypt::OpenPGP::MDC"
$mdc
ref $mdc eq "Crypt::OpenPGP::MDC"
Truth table0%0/3
A and B
A= $mdcB= ref $mdc eq "Crypt::OpenPGP::MDC"
| |||||||||||||||||||||||||||||
| 99 | unless $mdc && ref($mdc) eq 'Crypt::OpenPGP::MDC'; | ||||||||||||||||||||||||||||
| 100 | 0 | require Crypt::OpenPGP::Digest; | |||||||||||||||||||||||||||
| 101 | 0 | my $dgst = Crypt::OpenPGP::Digest->new('SHA1'); | |||||||||||||||||||||||||||
| 102 | 0 | my $hash = $dgst->hash($enc->{prefix} . $pt . chr(0xd3) . chr(0x14)); | |||||||||||||||||||||||||||
| 103 | 0 | ▶ | return $enc->error("SHA-1 hash of plaintext does not match MDC body") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | unless $mdc->digest eq $hash; | ||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | 2 | $pt; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 3 | 1; | |||||||||||||||||||||||||||
| 110 | __END__ | ||||||||||||||||||||||||||||