File Coverage

blib/lib/Crypt/OpenPGP/CFB.pm
Criterion Covered Total %
statement 47 48 97.9
branch 2 4 50.0
condition 3 7 42.8
subroutine 5 6 83.3
pod 0 5 0.0
total 57 70 81.4


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 2     2   699 use strict;
  2         4  
  2         966  
7              
8             sub new {
9 4     4 0 9 my $class = shift;
10 4         10 my $c = bless { }, $class;
11 4         14 $c->init(@_);
12             }
13              
14             sub init {
15 4     4 0 6 my $c = shift;
16 4         8 my($cipher, $iv) = @_;
17 4         11 $c->{cipher} = $cipher;
18 4         14 $c->{blocksize} = $cipher->blocksize;
19 4   33     40 $c->{iv} = $iv || "\0" x $c->{blocksize};
20 4         16 $c;
21             }
22              
23 0     0 0 0 sub sync { $_[0]->{unused} = '' }
24              
25             sub encrypt {
26 4     4 0 8 my $c = shift;
27 4         7 my($data) = @_;
28 4         6 my $ret = '';
29 4         9 my $iv = $c->{iv};
30 4   50     21 my $out = $c->{unused} || '';
31 4         8 my $size = length $out;
32 4         11 while ($data) {
33 76 50       145 unless ($size) {
34 76         212 $out = $c->{cipher}->encrypt($iv);
35 76         987 $size = $c->{blocksize};
36             }
37 76         138 my $in = substr $data, 0, $size, '';
38 76         89 $size -= (my $got = length $in);
39 76         132 $iv .= ($in ^= substr $out, 0, $got, '');
40 76         86 substr $iv, 0, $got, '';
41 76         468 $ret .= $in;
42             }
43 4         10 $c->{unused} = $out;
44 4         7 $c->{iv} = $iv;
45 4         18 $ret;
46             }
47              
48             sub decrypt {
49 4     4 0 8 my $c = shift;
50 4         7 my($data) = @_;
51 4         12 my $ret = '';
52 4         9 my $iv = $c->{iv};
53 4   50     20 my $out = $c->{unused} || '';
54 4         8 my $size = length $out;
55 4         11 while ($data) {
56 76 50       134 unless ($size) {
57 76         231 $out = $c->{cipher}->encrypt($iv);
58 76         936 $size = $c->{blocksize};
59             }
60 76         125 my $in = substr $data, 0, $size, '';
61 76         91 $size -= (my $got = length $in);
62 76         119 substr $iv .= $in, 0, $got, '';
63 76         194 $ret .= ($in ^= substr $out, 0, $got, '');
64             }
65 4         10 $c->{unused} = $out;
66 4         7 $c->{iv} = $iv;
67 4         19 $ret;
68             }
69              
70             1;
71             __END__