| 1 | package Crypt::CFB; | ||
| 2 | 1 | ▶ | use vars qw($VERSION) ; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use UNIVERSAL qw(can); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | require Exporter; | ||
| 5 | |||
| 6 | @ISA = qw(Exporter); | ||
| 7 | |||
| 8 | @EXPORT = qw(); | ||
| 9 | |||
| 10 | $VERSION = 0.02; | ||
| 11 | |||
| 12 | # | ||
| 13 | # The object returned by Crypt::CFB->new(<key>,<algorithm>,[iv]) contains: | ||
| 14 | # | ||
| 15 | # $self->{key}: the key | ||
| 16 | # $self->{algo}: instantiated object of the class <algorithm> | ||
| 17 | # $self->{register}: the internal state of the Cipher Feedback mode | ||
| 18 | # $self->{registerlength}: the length of the internal state in bytes | ||
| 19 | # $self->{bytes}: the number of bytes to xor per round | ||
| 20 | # $self->{iv}: a block of $self->{bytes} bytes, containing the | ||
| 21 | # Initialization Vector. | ||
| 22 | # $self->{cf}: anonymous subroutine without parameters. The | ||
| 23 | # subs in this implementation read $self->{key} | ||
| 24 | # and $self->{register}, apply the cryptographic | ||
| 25 | # one-way function and return its output. This is | ||
| 26 | # the stuff that is XORed to the cleartext in | ||
| 27 | # Crypt::CFB::encrypt. | ||
| 28 | # $self->{statef}: reference to a sub which updates the internal | ||
| 29 | # state after each $self->{cf} call. | ||
| 30 | # $self->{epattern}, $self->{rpattern}, $self->{spattern}: | ||
| 31 | # diverse patterns for pack/unpack, which are | ||
| 32 | # computed at instantiation. | ||
| 33 | # | ||
| 34 | # If you want to create another PurePerl Cipher Mode which is | ||
| 35 | # basically a stream cipher, then you can simply overload | ||
| 36 | # $self->{statef} and $self->{cf}. See for example Crypt::Ctr. | ||
| 37 | # | ||
| 38 | |||
| 39 | sub _statef; | ||
| 40 | sub _reginit; | ||
| 41 | |||
| 42 | sub new { | ||
| 43 | my ($proto, $key, $algo, $iv) = @_; | ||
| 44 | my $class = ref($proto) || $proto; | ||
| 45 | my $self = {}; | ||
| 46 | |||
| 47 | # The number of bytes that are extracted per round | ||
| 48 | # from the cipher/digest's output | ||
| 49 | |||
| 50 | $self->{bytes} = 1; # XXX unused | ||
| 51 | $self->{key} = $key; | ||
| 52 | $self->{cf} = sub {die "Don't forget to set the key stream function";}; | ||
| 53 | |||
| 54 | eval "require $algo;"; | ||
| 55 | if ($@) { | ||
| 56 | die "Could not instantiate $algo: $!"; | ||
| 57 | } | ||
| 58 | |||
| 59 | # We allow cryptographic one-way functions, | ||
| 60 | # i.e. ciphers and hashes. | ||
| 61 | |||
| 62 | if ($algo =~ m/^Crypt::/) { | ||
| 63 | _Crypt_new($self, $algo, $key); | ||
| 64 | } elsif ( $algo =~ m/^Digest::/) { | ||
| 65 | _Digest_new($self, $algo, $key); | ||
| 66 | } else { | ||
| 67 | die "Algorithm should belong to Crypt:: or Digest::"; | ||
| 68 | } | ||
| 69 | |||
| 70 | # These are patterns for pack/unpack which are | ||
| 71 | # subsequently used a lot. | ||
| 72 | |||
| 73 | $self->{epattern} = "x" . ($self->{registerlength} - $self->{bytes}) . | ||
| 74 | "a" . $self->{bytes}; | ||
| 75 | $self->{rpattern} = "x" . $self->{bytes} . | ||
| 76 | "a" . ($self->{registerlength} - $self->{bytes}); | ||
| 77 | $self->{spattern} = "a" . $self->{bytes} . "a*"; | ||
| 78 | |||
| 79 | # Store the Initialization Vector | ||
| 80 | if (defined($iv)) { | ||
| 81 | $self->{iv} = $iv; | ||
| 82 | } | ||
| 83 | |||
| 84 | # Initialize the internal state. | ||
| 85 | $self->{register} = _reginit($self); | ||
| 86 | |||
| 87 | # This is the function that does per-round manipulation | ||
| 88 | # of the internal state. | ||
| 89 | $self->{statef} = \&_statef; | ||
| 90 | |||
| 91 | bless ($self, $class); | ||
| 92 | return $self; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub _Digest_new { | ||
| 96 | my ($self, $algo, $key) = @_; | ||
| 97 | $self->{algo} = $algo->new(); | ||
| 98 | if ($@) { | ||
| 99 | die "Could not instantiate $algo: $!"; | ||
| 100 | } | ||
| 101 | |||
| 102 | # The Digest class has no "blocklength" method, | ||
| 103 | # but that's no problem | ||
| 104 | $self->{algo}->add(""); | ||
| 105 | $self->{registerlength} = length ($self->{algo}->digest); | ||
| 106 | if (not $self->{registerlength}) { | ||
| 107 | die "Could not set registerlength"; | ||
| 108 | } | ||
| 109 | |||
| 110 | # Anonymous function to produce the keystream | ||
| 111 | $self->{cf} = sub { | ||
| 112 | $self->{algo}->add($self->{key} . $self->{register}); | ||
| 113 | return $self->{algo}->digest; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | sub _Crypt_new { | ||
| 118 | my ($self, $algo, $key) = @_; | ||
| 119 | eval "require $algo;"; | ||
| 120 | |||
| 121 | if ($@) { | ||
| 122 | die "Could not instantiate $algo: $!"; | ||
| 123 | } | ||
| 124 | |||
| 125 | # Crypt::Blowfish returns keysize 0, so we take | ||
| 126 | # the maximum in that case | ||
| 127 | |||
| 128 | if (length $key > ( $algo->keysize || 56)) { | ||
| 129 | $key = substr $key, 0, $algo->keysize; | ||
| 130 | } | ||
| 131 | |||
| 132 | # We could check for correct keysizes, but the | ||
| 133 | # Crypt:: algorithms throw an error anyway. | ||
| 134 | |||
| 135 | $self->{algo} = $algo->new($key); | ||
| 136 | |||
| 137 | if ($@) { | ||
| 138 | die "Could not instantiate $algo: $!"; | ||
| 139 | } | ||
| 140 | |||
| 141 | if (not $self->{algo}->can('blocksize')) { | ||
| 142 | die "$algo does not implement blocksize"; | ||
| 143 | } | ||
| 144 | |||
| 145 | $self->{registerlength} = $self->{algo}->blocksize; | ||
| 146 | |||
| 147 | if (not $self->{registerlength}) { | ||
| 148 | die "Could not set registerlength"; | ||
| 149 | } | ||
| 150 | |||
| 151 | # Anonymous function to produce the keystream | ||
| 152 | $self->{cf} = sub { | ||
| 153 | return $self->{algo}->encrypt($self->{register}); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | sub _reginit { | ||
| 158 | my $self = shift; | ||
| 159 | my $iv = defined($self->{iv}) ? $self->{iv} : ""; | ||
| 160 | my $remainder = $self->{registerlength} - length($iv); | ||
| 161 | return $iv . ("\x0" x $remainder); | ||
| 162 | } | ||
| 163 | |||
| 164 | # Per $self->{bytes} encryption/decryption | ||
| 165 | sub bencrypt { | ||
| 166 | my ($self, $block, $d) = @_; | ||
| 167 | my $xor = &{$self->{cf}}(); | ||
| 168 | $xor = substr $xor, -($self->{bytes}), $self->{bytes}; | ||
| 169 | # $xor = unpack $self->{epattern}, $xor; | ||
| 170 | if ($self->{bytes} > 1) { | ||
| 171 | if (length $block < length $xor) { | ||
| 172 | $xor = substr $xor, 0, (length $block); | ||
| 173 | } | ||
| 174 | } | ||
| 175 | my $out = $block ^ $xor; | ||
| 176 | if ($d) { | ||
| 177 | &{$self->{statef}}($self,$block); | ||
| 178 | } else { | ||
| 179 | &{$self->{statef}}($self,$out); | ||
| 180 | } | ||
| 181 | return $out; | ||
| 182 | } | ||
| 183 | |||
| 184 | sub _encrypt { | ||
| 185 | my ($self, $string, $d) = @_; | ||
| 186 | my ($out, $i, $l); | ||
| 187 | $l = length ($string); | ||
| 188 | for ($i = 0; $i < $l; $i += $self->{bytes}) { | ||
| 189 | $out .= bencrypt ($self, (substr $string, $i, $self->{bytes}) , $d); | ||
| 190 | } | ||
| 191 | return $out; | ||
| 192 | } | ||
| 193 | |||
| 194 | sub encrypt { | ||
| 195 | my ($self, $string) = @_; | ||
| 196 | return _encrypt($self, $string, 0); | ||
| 197 | } | ||
| 198 | |||
| 199 | sub decrypt { | ||
| 200 | my ($self, $string) = @_; | ||
| 201 | return _encrypt($self, $string, 1); | ||
| 202 | } | ||
| 203 | |||
| 204 | # Reset the internal state | ||
| 205 | sub reset { | ||
| 206 | my ($self, $iv) = @_; | ||
| 207 | if (defined($iv)) { | ||
| 208 | $self->{iv} = $iv; | ||
| 209 | } | ||
| 210 | $self->{register} = _reginit($self); | ||
| 211 | } | ||
| 212 | |||
| 213 | # This manipulates the internal state | ||
| 214 | sub _statef { | ||
| 215 | my ($self, $c) = @_; | ||
| 216 | $self->{register} = (unpack $self->{rpattern}, $self->{register}) . $c; | ||
| 217 | } | ||
| 218 | |||
| 219 | 1; | ||
| 220 | |||
| 221 | __END__ | ||