| 1 | package Crypt::Ctr::FullWidth; | ||
| 2 | 1 | ▶ | use base qw(Crypt::CFB); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | sub _statef; | ||
| 8 | |||
| 9 | sub new { | ||
| 10 | my ($proto, $key, $algo) = @_; | ||
| 11 | my $class = ref($proto) || $proto; | ||
| 12 | my $self = new Crypt::CFB ($key, $algo); | ||
| 13 | $self->{statef} = \&_statef; | ||
| 14 | bless ($self, $class); | ||
| 15 | } | ||
| 16 | |||
| 17 | |||
| 18 | sub _statef { | ||
| 19 | my $self = shift; | ||
| 20 | |||
| 21 | my @ints = reverse unpack "n*", $self->{register}; | ||
| 22 | |||
| 23 | use integer; # unsigned | ||
| 24 | foreach my $int ( @ints ) { | ||
| 25 | $int++; | ||
| 26 | last unless $int & ( 1 << 16 ); # we can stop if the integer didn't overflow | ||
| 27 | } | ||
| 28 | |||
| 29 | $self->{register} = pack("n*", reverse @ints); | ||
| 30 | } | ||
| 31 | |||
| 32 | sub bencrypt { | ||
| 33 | my ($self, $block, $d) = @_; | ||
| 34 | my $xor = &{$self->{cf}}(); | ||
| 35 | my $out = $block ^ substr($xor, 0, length($block) ); | ||
| 36 | if ($d) { | ||
| 37 | &{$self->{statef}}($self,$block); | ||
| 38 | } else { | ||
| 39 | &{$self->{statef}}($self,$out); | ||
| 40 | } | ||
| 41 | return $out; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub _encrypt { | ||
| 45 | my ($self, $string, $d) = @_; | ||
| 46 | my ($out, $i, $l); | ||
| 47 | my $blocksize = $self->{registerlength}; | ||
| 48 | join '', map { $self->bencrypt($_, $d) } unpack("(a$blocksize)*", $string); | ||
| 49 | } | ||
| 50 | |||
| 51 | # UGH! gotta duplicate these tow because they call _encrypt as a function, not a method | ||
| 52 | sub encrypt { | ||
| 53 | my ($self, $string) = @_; | ||
| 54 | return $self->_encrypt($string, 0); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub decrypt { | ||
| 58 | my ($self, $string) = @_; | ||
| 59 | return $self->_encrypt($string, 1); | ||
| 60 | } | ||
| 61 | |||
| 62 | sub set_nonce { | ||
| 63 | my ( $self, $nonce ) = @_; | ||
| 64 | $self->{register} = $nonce; | ||
| 65 | } | ||
| 66 | |||
| 67 | 1; | ||
| 68 | __END__ | ||