File Coverage

blib/lib/Crypt/OpenPGP/CFB.pm
Criterion Covered Total %
statement 51 51 100.0
branch 4 4 100.0
condition 6 7 85.7
subroutine 7 7 100.0
pod 0 5 0.0
total 68 74 91.8


line stmt bran cond sub pod time code
1             # This code based slightly on the Systemics Crypt::CFB.
2             # Parts Copyright (C) 1995, 1996 Systemics Ltd (http://www.systemics.com/)
3             # All rights reserved.
4              
5             package Crypt::OpenPGP::CFB;
6 12     12   268201 use strict;
  12         23  
  12         532  
7 12     12   63 use warnings;
  12         162  
  12         9783  
8              
9             our $VERSION = '1.19'; # VERSION
10              
11             sub new {
12 85     85 0 220 my $class = shift;
13 85         253 my $c = bless { }, $class;
14 85         397 $c->init(@_);
15             }
16              
17             sub init {
18 85     85 0 161 my $c = shift;
19 85         227 my($cipher, $iv) = @_;
20 85         243 $c->{cipher} = $cipher;
21 85         303 $c->{blocksize} = $cipher->blocksize;
22 85   66     859 $c->{iv} = $iv || "\0" x $c->{blocksize};
23 85         373 $c;
24             }
25              
26 44     44 0 119 sub sync { $_[0]->{unused} = '' }
27              
28             sub encrypt {
29 68     68 0 164 my $c = shift;
30 68         174 my($data) = @_;
31 68         122 my $ret = '';
32 68         143 my $iv = $c->{iv};
33 68   100     317 my $out = $c->{unused} || '';
34 68         133 my $size = length $out;
35 68         246 while ( $data ne '' ) {
36 582 100       1155 unless ($size) {
37 576         2720 $out = $c->{cipher}->encrypt($iv);
38 576         16792 $size = $c->{blocksize};
39             }
40 582         1044 my $in = substr $data, 0, $size, '';
41 582         961 $size -= (my $got = length $in);
42 582         1173 $iv .= ($in ^= substr $out, 0, $got, '');
43 582         884 substr $iv, 0, $got, '';
44 582         1452 $ret .= $in;
45             }
46 68         186 $c->{unused} = $out;
47 68         188 $c->{iv} = $iv;
48 68         370 $ret;
49             }
50              
51             sub decrypt {
52 73     73 0 169 my $c = shift;
53 73         236 my($data) = @_;
54 73         172 my $ret = '';
55 73         178 my $iv = $c->{iv};
56 73   100     402 my $out = $c->{unused} || '';
57 73         144 my $size = length $out;
58 73         228 while ( $data ne '' ) {
59 607 100       1178 unless ($size) {
60 601         2938 $out = $c->{cipher}->encrypt($iv);
61 601         16784 $size = $c->{blocksize};
62             }
63 607         1021 my $in = substr $data, 0, $size, '';
64 607         928 $size -= (my $got = length $in);
65 607         1097 substr $iv .= $in, 0, $got, '';
66 607         1693 $ret .= ($in ^= substr $out, 0, $got, '');
67             }
68 73         188 $c->{unused} = $out;
69 73         153 $c->{iv} = $iv;
70 73         308 $ret;
71             }
72              
73             1;
74             __END__