1package Crypt::Ctr;
2use Crypt::CFB;
3use vars qw($VERSION);
4@ISA = (Crypt::CFB);
5
6$VERSION = 0.01;
7
8sub _statef;
9
10#
11# Crypt::Ctr implements the Counter Mode for block ciphers.
12# almost everything is inherited from Crypt::CFB, just the
13# _statef method is overloaded.
14#
15# XXX  the counter is just a perl int. So after roughly two Gigabytes
16# of cleartext, the keystream will repeat itself.
17#
18
19sub new {
20	my ($proto, $key, $algo) = @_;
21	my $class = ref($proto) || $proto;
22	my $self = new Crypt::CFB ($key, $algo);
23	$self->{statef} = \&_statef;
24	$self->{fill} = "\x0" x ($self->{registerlength} - 4);
25	bless ($self, $class);
26}
27
28
29sub _statef {
30	my $self = shift;
31	my ($c, undef) = unpack "La*", $self->{register};
32	$c++;
33	$self->{register} = unpack "a*" , (pack "La*", ($c, $self->{fill}));
34}
35
361;
37__END__
38
39=pod
40
41=head1 NAME
42
43Crypt::Ctr - Encrypt Data in Counter Mode
44
45=head1 SYNOPSIS
46
47	use Crypt::Ctr;
48
49	my $cipher = new Crypt::Ctr $key, 'Crypt::Rijndael';
50
51	my $ciphertext = $cipher->encrypt($plaintext);
52	my $plaintext = $cipher->decrypt($ciphertext);
53
54	my $cipher2 = new Crypt::Ctr $key, 'Digest::MD5';
55
56	$ciphertext = $cipher->encrypt($plaintext);
57	$plaintext = $cipher->decrypt($ciphertext);
58
59=head1 DESCRIPTION
60
61Generic Counter Mode implementation in pure Perl.
62The Counter Mode module constructs a stream
63cipher from a block cipher or cryptographic hash funtion
64and returns it as an object. Any block cipher in the
65C<Crypt::> class can be used, as long as it supports the
66C<blocksize> and C<keysize> methods. Any hash function in
67the C<Digest::> class can be used, as long as it supports
68the C<add> method.
69
70=head2 Note
71
72Counter mode produces the keystream independent from the
73input. Be sure not to re-use keys in Counter mode. As
74with Cipher Feedback mode, one should use Counter mode
75inside authenticated channels, e.g. HMAC.
76
77=head1 METHODS
78
79=over 4
80
81=item C<$cipher = new Crypt::Ctr $key, $algorithm>
82
83Constructs a Crypt::Ctr object. If C<$algorithm> is a block cipher, then
84C<$key> should be of the correct size for that cipher. In most
85cases you can inquire the block cipher module by invoking the
86C<keysize> method. If C<$algorithm> is a hash function, then
87C<$key> can be of any size.
88
89=item C<$ciphertext = $cipher-E<gt>encrypt $plaintext>
90
91Encrypts C<$plaintext>. The input is XORed with the keystream
92generated from the internal state of the Ctr object and that
93state is updated with the output. C<$plaintext> can be of any length.
94
95=item C<$cipher-E<gt>reset>
96
97Resets the internal state. Remember to do that
98before decrypting, if you use the same object.
99
100=item C<$plaintext = $cipher-E<gt>decrypt $ciphertext>
101
102Decrypts C<$ciphertext>.
103
104=back
105
106=head1 BUGS
107
108This is awfully slow. Some classes in C<Digest::> do not provide
109the C<add> method, so they will fail.  The internal
110counter is a Perl integer. This could possibly lead to strange errors
111when encrypting more than C<POSIX::LONG_MAX> bytes and decrypting
112it on a different architecture.
113
114=head1 AUTHOR
115
116Matthias Bauer <matthiasb@acm.org>
117
118=cut
119
120
121