File Coverage

blib/lib/Net/SSH/Perl/Cipher/CBC.pm
Criterion Covered Total %
statement 29 29 100.0
branch n/a
condition 2 3 66.6
subroutine 4 4 100.0
pod 0 3 0.0
total 35 39 89.7


line stmt bran cond sub pod time code
1             # This code is based in part on the Systemics Crypt::CBC.
2             # Parts copyright (C) 1995, 1996 Systemics Ltd (http://www.systemics.com/)
3             # All rights reserved.
4              
5             package Net::SSH::Perl::Cipher::CBC;
6 2     2   15 use strict;
  2         3  
  2         621  
7              
8             sub new {
9 37     37 0 111 my($class, $ciph, $iv) = @_;
10 37   66     372 bless {
11             cipher => $ciph,
12             iv => $iv || ("\0" x $ciph->blocksize),
13             }, $class;
14             }
15              
16             sub encrypt {
17 18     18 0 30 my $cbc = shift;
18 18         30 my $data = shift;
19              
20 18         27 my $retval = "";
21 18         30 my $iv = $cbc->{iv};
22 18         29 my $ciph = $cbc->{cipher};
23 18         51 my $size = $ciph->blocksize;
24              
25 18         48 while (length $data) {
26 18         47 my $in = substr($data, 0, $size, '') ^ $iv;
27 18         85 $iv = $ciph->encrypt($in);
28 18         47 $retval .= $iv;
29             }
30              
31 18         34 $cbc->{iv} = $iv;
32 18         55 $retval;
33             }
34              
35             sub decrypt {
36 19     19 0 29 my $cbc = shift;
37 19         28 my $data = shift;
38              
39 19         47 my $retval = "";
40 19         35 my $iv = $cbc->{iv};
41 19         34 my $ciph = $cbc->{cipher};
42 19         53 my $size = $ciph->blocksize;
43              
44 19         45 while (length $data) {
45 29         63 my $in = substr($data, 0, $size, '');
46 29         129 $retval .= $ciph->decrypt($in) ^ $iv;
47 29         75 $iv = $in;
48             }
49              
50 19         30 $cbc->{iv} = $iv;
51 19         73 $retval;
52             }
53              
54             1;
55             __END__