File Coverage

blib/lib/Crypt/OpenPGP/CFB.pm
Criterion Covered Total %
statement 48 48 100.0
branch 4 4 100.0
condition 6 7 85.7
subroutine 6 6 100.0
pod 0 5 0.0
total 64 70 91.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 7     7   530 use strict;
  7         13  
  7         3569  
7              
8             sub new {
9 83     83 0 130 my $class = shift;
10 83         169 my $c = bless { }, $class;
11 83         249 $c->init(@_);
12             }
13              
14             sub init {
15 83     83 0 139 my $c = shift;
16 83         151 my($cipher, $iv) = @_;
17 83         177 $c->{cipher} = $cipher;
18 83         280 $c->{blocksize} = $cipher->blocksize;
19 83   66     698 $c->{iv} = $iv || "\0" x $c->{blocksize};
20 83         256 $c;
21             }
22              
23 44     44 0 102 sub sync { $_[0]->{unused} = '' }
24              
25             sub encrypt {
26 68     68 0 79 my $c = shift;
27 68         96 my($data) = @_;
28 68         81 my $ret = '';
29 68         116 my $iv = $c->{iv};
30 68   100     287 my $out = $c->{unused} || '';
31 68         83 my $size = length $out;
32 68         150 while ( $data ne '' ) {
33 582 100       793 unless ($size) {
34 576         2147 $out = $c->{cipher}->encrypt($iv);
35 576         9925 $size = $c->{blocksize};
36             }
37 582         724 my $in = substr $data, 0, $size, '';
38 582         505 $size -= (my $got = length $in);
39 582         800 $iv .= ($in ^= substr $out, 0, $got, '');
40 582         489 substr $iv, 0, $got, '';
41 582         1268 $ret .= $in;
42             }
43 68         102 $c->{unused} = $out;
44 68         86 $c->{iv} = $iv;
45 68         234 $ret;
46             }
47              
48             sub decrypt {
49 70     70 0 89 my $c = shift;
50 70         152 my($data) = @_;
51 70         99 my $ret = '';
52 70         106 my $iv = $c->{iv};
53 70   100     299 my $out = $c->{unused} || '';
54 70         93 my $size = length $out;
55 70         167 while ( $data ne '' ) {
56 590 100       837 unless ($size) {
57 585         2149 $out = $c->{cipher}->encrypt($iv);
58 585         8900 $size = $c->{blocksize};
59             }
60 590         760 my $in = substr $data, 0, $size, '';
61 590         557 $size -= (my $got = length $in);
62 590         635 substr $iv .= $in, 0, $got, '';
63 590         1405 $ret .= ($in ^= substr $out, 0, $got, '');
64             }
65 70         118 $c->{unused} = $out;
66 70         82 $c->{iv} = $iv;
67 70         220 $ret;
68             }
69              
70             1;
71             __END__
72              
73             =head1 NAME
74              
75             Crypt::OpenPGP::CFB - PGP Cipher Feedback Mode
76              
77             =head1 SYNOPSIS
78              
79             use Crypt::OpenPGP::CFB;
80              
81             my $key = 'foo bar';
82             my $cipher = Crypt::Blowfish->new( $key ); # for example
83             my $cfb = Crypt::OpenPGP::CFB->new( $cipher );
84              
85             my $plaintext = 'this is secret!';
86             my $ct = $cfb->encrypt( $plaintext );
87              
88             my $pt = $cfb->decrypt( $ct );
89              
90             =head1 DESCRIPTION
91              
92             I<Crypt::OpenPGP::CFB> implements the variant of Cipher Feedback mode
93             that PGP uses in its encryption and decryption. The key difference
94             with PGP CFB is that the CFB state is resynchronized at each
95             encryption/decryption. This applies both when encrypting secret key
96             data and in symmetric encryption of standard encrypted data. More
97             differences are described in the OpenPGP RFC, in section 13.9
98             (OpenPGP CFB mode).
99              
100             Typically you should never need to directly use I<Crypt::OpenPGP::CFB>;
101             I<Crypt::OpenPGP::Cipher> objects wrap around an instance of this
102             class and provide a uniform interface to symmetric ciphers. See
103             the documentation for that module for usage details.
104              
105             =head1 AUTHOR & COPYRIGHTS
106              
107             Please see the Crypt::OpenPGP manpage for author, copyright, and
108             license information.
109              
110             =cut