OpenPGP/SessionKey.pm

filter: Statementstmt 49.4% 44 / 89 Branchbran 10.0% 2 / 20 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 68.7% 11 / 16 Podpod 71.4% 5 / 7 totaltotal 43.5% 64 / 147 CC 17 SCAR 23.2
CC
17
Cov
41%
init
27.7
decrypt
12.0
_decode
12.0
CRAP 10.2
« OpenPGP/KeyBlock.pm ↑ Summary OpenPGP/Signature.pm »
1 package Crypt::OpenPGP::SessionKey;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 our $VERSION = '1.21'; # VERSION
6  
7 2 ▶use Crypt::OpenPGP::Constants qw( DEFAULT_CIPHER );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Crypt::OpenPGP::Key::Public;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Crypt::OpenPGP::Util qw( mp2bin bin2mp bitsize );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Crypt::OpenPGP::Buffer;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use parent qw( Crypt::OpenPGP::ErrorHandler );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶sub key_id { $_[0]->{key_id} }
Subroutine key_id: called CC=1 SCAR=0.0
Pod documented
14  
15 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
16 1 my $class = shift;
17 1 my $key = bless { }, $class;
18 1 $key->init(@_);
19 }
20  
21 1 ▶sub init {
Subroutine init: called CC=7 SCAR=33.2
Pod undocumented
22 1 my $key = shift;
23 1 my %param = @_;
24 1 $key->{version} = 3;
25 1 ▶ if ((my $cert = $param{Key}) && (my $sym_key = $param{SymKey})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cert = $param{"Key"} and my $sym_key = $param{"SymKey"}) 0 1 1
Condition33%1/3
my $cert = $param{"Key"} and my $sym_key = $param{"SymKey"}
!ll&&!rl&&r
100
MC/DC0%0/2
my $cert = $param{"Key"} and my $sym_key = $param{"SymKey"}
my $cert = $param{"Key"} my $sym_key = $param{"SymKey"}
Truth table33%1/3
A and B
A= my $cert = $param{"Key"}B= my $sym_key = $param{"SymKey"}
ABresult
0X0
100
111
26 0 ▶ my $alg = $param{Cipher} || DEFAULT_CIPHER;
Condition0%0/3
$param{'Cipher'} || &DEFAULT_CIPHER()
l!l&&r!l&&!r
000
MC/DC0%0/2
$param{'Cipher'} || &DEFAULT_CIPHER()
$param{'Cipher'} &DEFAULT_CIPHER()
Truth table0%0/3
A || B
A= $param{'Cipher'}B= &DEFAULT_CIPHER()
ABresult
1X1
011
000
27 0 ▶ my $cipher = Crypt::OpenPGP::Cipher->new($alg) or
Branch: 1 branch
BranchTrueFalseTotal
my $cipher = "Crypt::OpenPGP::Cipher"->new($alg) or return ref($key)->error("Crypt::OpenPGP::Cipher"->errstr) 0 0 0
28 return (ref $key)->error( Crypt::OpenPGP::Cipher->errstr );
29 0 my $keysize = $cipher->keysize;
30 0 $sym_key = substr $sym_key, 0, $keysize;
31 0 my $pk = $cert->key->public_key;
32 0 ▶ my $enc = $key->_encode($sym_key, $alg, $pk->bytesize) or
Branch: 1 branch
BranchTrueFalseTotal
my $enc = $key->_encode($sym_key, $alg, $pk->bytesize) or return ref($key)->error('Encoding symkey failed: ' . $key->errstr) 0 0 0
33 return (ref $key)->error("Encoding symkey failed: " . $key->errstr);
34 0 $key->{key_id} = $cert->key_id;
35 0 ▶ $key->{C} = $pk->encrypt($enc) or
Branch: 1 branch
BranchTrueFalseTotal
$key->{"C"} = $pk->encrypt($enc) or return ref($key)->error('Encryption failed: ' . $pk->errstr) 0 0 0
36 return (ref $key)->error("Encryption failed: " . $pk->errstr);
37 0 $key->{pk_alg} = $pk->alg_id;
38 }
39 1 $key;
40 }
41  
42 1 ▶sub parse {
Subroutine parse: called CC=4 SCAR=14.1
Pod documented
43 1 my $class = shift;
44 1 my($buf) = @_;
45 1 my $key = $class->new;
46 1 $key->{version} = $buf->get_int8;
47 return $class->error("Unsupported version ($key->{version})")
48 1 ▶ unless $key->{version} == 2 || $key->{version} == 3;
Branch: 1 branch
BranchTrueFalseTotal
unless $key->{"version"} == 2 or $key->{"version"} == 3 0 1 1
Condition33%1/3
$key->{"version"} == 2 or $key->{"version"} == 3
l!l&&r!l&&!r
010
MC/DC0%0/2
$key->{"version"} == 2 or $key->{"version"} == 3
$key->{"version"} == 2 $key->{"version"} == 3
Truth table33%1/3
A or B
A= $key->{"version"} == 2B= $key->{"version"} == 3
ABresult
1X1
011
000
49 1 $key->{key_id} = $buf->get_bytes(8);
50 1 $key->{pk_alg} = $buf->get_int8;
51 1 my $pk = Crypt::OpenPGP::Key::Public->new($key->{pk_alg});
52 1 my @props = $pk->crypt_props;
53 1 for my $e (@props) {
54 2 $key->{C}{$e} = $buf->get_mp_int;
55 }
56 1 $key;
57 }
58  
59 0 ▶sub save {
Subroutine save: not called CC=2 SCAR=17.9
Pod documented
60 0 my $key = shift;
61 0 my $buf = Crypt::OpenPGP::Buffer->new;
62 0 $buf->put_int8($key->{version});
63 0 $buf->put_bytes($key->{key_id}, 8);
64 0 $buf->put_int8($key->{pk_alg});
65 0 my $c = $key->{C};
66 0 for my $prop (sort keys %$c) {
67 0 $buf->put_mp_int($c->{$prop});
68 }
69 0 $buf->bytes;
70 }
71  
72 0 ▶sub display {
Subroutine display: not called CC=2 SCAR=17.9
Pod undocumented
73 0 my $key = shift;
74 my $str = sprintf ":pubkey enc packet: version %d, algo %d, keyid %s\n",
75 0 $key->{version}, $key->{pk_alg}, uc unpack('H*', $key->{key_id});
76 0 my $c = $key->{C};
77 0 for my $prop (sort keys %$c) {
78 0 $str .= sprintf " data: [%d bits]\n", bitsize($c->{$prop});
79 }
80 0 $str;
81 }
82  
83 0 ▶sub decrypt {
Subroutine decrypt: not called CC=3 SCAR=24.8
Pod documented
84 0 my $key = shift;
85 0 my($sk) = @_;
86 0 ▶ return $key->error("Invalid secret key ID")
Branch: 1 branch
BranchTrueFalseTotal
unless $key->key_id eq $sk->key_id 0 0 0
87 unless $key->key_id eq $sk->key_id;
88 0 ▶ my($sym_key, $alg) = __PACKAGE__->_decode($sk->key->decrypt($key->{C}))
Branch: 1 branch
BranchTrueFalseTotal
() = __PACKAGE__->_decode($sk->key->decrypt($key->{"C"})) or return $key->error('Session key decryption failed: ' . __PACKAGE__->errstr) 0 0 0
89 or return $key->error("Session key decryption failed: " .
90 __PACKAGE__->errstr);
91 0 ($sym_key, $alg);
92 }
93  
94 0 ▶sub _encode {
Subroutine _encode: not called CC=2 SCAR=17.9
95 0 my $class = shift;
96 0 my($sym_key, $sym_alg, $size) = @_;
97 0 my $padlen = "$size" - length($sym_key) - 2 - 2 - 2;
98 0 my $pad = "\0";
99 0 ▶ while ($pad =~ tr/\0//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pad =~ tr/\000//) 0 0 0
100 0 $pad = Crypt::OpenPGP::Util::get_random_bytes($padlen);
101 }
102 0 bin2mp(pack 'na*na*n', 2, $pad, $sym_alg, $sym_key,
103 unpack('%16C*', $sym_key));
104 }
105  
106 0 ▶sub _decode {
Subroutine _decode: not called CC=3 SCAR=24.8
107 0 my $class = shift;
108 0 my($n) = @_;
109 0 my $ser = mp2bin($n);
110 0 ▶ return $class->error("Encoded data must start with 2")
Branch: 1 branch
BranchTrueFalseTotal
unless unpack("C", $ser) == 2 0 0 0
111 unless unpack('C', $ser) == 2;
112 0 my $csum = unpack 'n', substr $ser, -2, 2, '';
113 0 my($pad, $sym_key) = split /\0/, $ser, 2;
114 0 my $sym_alg = ord substr $sym_key, 0, 1, '';
115 0 ▶ return $class->error("Encoded data has bad checksum")
Branch: 1 branch
BranchTrueFalseTotal
unless unpack("%16C*", $sym_key) == $csum 0 0 0
116 unless unpack('%16C*', $sym_key) == $csum;
117 0 ($sym_key, $sym_alg);
118 }
119  
120 2 1;
121 __END__
« OpenPGP/KeyBlock.pm ↑ Summary OpenPGP/Signature.pm »