OpenPGP/Ciphertext.pm

filter: Statementstmt 78.3% 65 / 83 Branchbran 46.1% 12 / 26 Conditioncond 36.3% 4 / 11 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 92.8% 13 / 14 Podpod 57.1% 4 / 7 totaltotal 66.8% 99 / 148 CC 18 SCAR 21.0
CC
18
Cov
68%
decrypt
20.2
init
8.4
display
6.0
CRAP 8.2
« OpenPGP/Signature.pm ↑ Summary OpenPGP/KeyServer.pm »
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
BranchTrueFalseTotal
$_[0]{'is_mdc'} ? : 0 2 2
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
BranchTrueFalseTotal
if (my $key = $param{"SymKey"} and my $data = $param{"Data"}) 2 4 6
Condition66%2/3
my $key = $param{"SymKey"} and my $data = $param{"Data"}
!ll&&!rl&&r
402
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"}
ABresult
0X0
100
111
28 2 ▶ $enc->{is_mdc} = $param{MDC} || 0;
Condition50%1/2
$param{'MDC'} || 0
l!l
02
MC/DC0%0/1
$param{'MDC'} || 0
$param{'MDC'}
Truth table50%1/2
A
A= $param{'MDC'}
Aresult
11
00
29 2 $enc->{version} = 1;
30 2 ▶ my $alg = $param{Cipher} || DEFAULT_CIPHER;
Condition33%1/3
$param{'Cipher'} || &DEFAULT_CIPHER()
l!l&&r!l&&!r
200
MC/DC0%0/2
$param{'Cipher'} || &DEFAULT_CIPHER()
$param{'Cipher'} &DEFAULT_CIPHER()
Truth table33%1/3
A || B
A= $param{'Cipher'}B= &DEFAULT_CIPHER()
ABresult
1X1
011
000
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
BranchTrueFalseTotal
unless $enc->{"is_mdc"} 2 0 2
37 2 $enc->{ciphertext} .= $cipher->encrypt($data);
38  
39 2 ▶ if ($enc->{is_mdc}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enc->{"is_mdc"}) 0 2 2
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
BranchTrueFalseTotal
if ($is_mdc) 1 3 4
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
BranchTrueFalseTotal
if ($enc->{"is_mdc"}) 0 2 2
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
BranchTrueFalseTotal
if ($enc->{"is_mdc"}) 0 0 0
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
BranchTrueFalseTotal
my $cipher = "Crypt::OpenPGP::Cipher"->new($sym_alg, $key) or return $enc->error("Crypt::OpenPGP::Cipher"->errstr) 2 0 2
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
BranchTrueFalseTotal
unless substr($pt, -4, 2) eq substr($pt, -2, 2) 0 2 2
91 unless substr($pt, -4, 2) eq substr($pt, -2, 2);
92 2 ▶ $cipher->sync unless $enc->{is_mdc};
Branch: 1 branch
BranchTrueFalseTotal
unless $enc->{"is_mdc"} 2 0 2
93 2 $pt = $cipher->decrypt(substr $enc->{ciphertext}, $padlen);
94 2 ▶ if ($enc->{is_mdc}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enc->{"is_mdc"}) 0 2 2
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
BranchTrueFalseTotal
unless $mdc and ref $mdc eq "Crypt::OpenPGP::MDC" 0 0 0
Condition0%0/3
$mdc and ref $mdc eq "Crypt::OpenPGP::MDC"
!ll&&!rl&&r
000
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"
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless $mdc->digest eq $hash 0 0 0
104 unless $mdc->digest eq $hash;
105 }
106 2 $pt;
107 }
108  
109 3 1;
110 __END__
« OpenPGP/Signature.pm ↑ Summary OpenPGP/KeyServer.pm »