1package Crypt::Rabbit;
2
3use strict;
4use warnings;
5require Exporter;
6
7our @EXPORT_OK = qw(new encrypt decrypt keysize rounds);
8our $VERSION = '1.0.0';
9our @ISA = qw(Exporter);
10
11require XSLoader;
12XSLoader::load('Crypt::Rabbit', $VERSION);
13
14# Preloaded methods go here.
15
16sub keysize { 16 }    # 16 bytes
17sub rounds { 1 }      # may be useful for some applications
18
19sub encrypt {
20    my ($class, $str) = @_;
21    my $len = length $str;
22    my $pad = pack "a" x ((16 - ($len % 16)) % 16), \000;
23    $str .= $pad;
24    my $ciphertext = rabbit_enc($class, $str);
25    return substr($ciphertext, 0, $len);
26}
27
28sub decrypt {
29    my ($class, $str) = @_;
30    my $len = length $str;
31    my $pad = pack "a" x ((16 - ($len % 16)) % 16), \000;
32    $str .= $pad;
33    my $ciphertext = rabbit_enc($class, $str);
34    return substr($ciphertext, 0, $len);
35}
36
371;
38
39__END__
40
41=head1 NAME
42
43Crypt::Rabbit - A new stream cipher based on the properties of counter
44assisted stream ciphers
45
46=head1 SYNOPSIS
47
48    use Crypt::Rabbit;
49
50    $cipher = new Crypt::Rabbit $key;
51    $ciphertext = $cipher->encrypt($plaintext);
52    $ks = $cipher->keysize();
53    $plaintext  = $cipher->decrypt($ciphertext);
54
55=head1 DESCRIPTION
56
57Rabbit is a new stream cipher based on the properties of counter
58assisted stream ciphers, invented by Martin Boesgaard, Mette Vesterager,
59Thomas Pedersen, Jesper Christiansen, and Ove Scavenius of Cryptico A/S.
60
61This module supports the following methods:
62
63=over
64
65=item B<new()>
66
67Initializes the internal states of Rabbit
68
69=item B<encrypt($data)>
70
71Encrypts the data stream B<$data>
72
73=item B<decrypt($data)>
74
75Decrypts the data stream B<$data>
76
77B<decrypt($data)> is the same as B<encrypt($data)>
78
79=item B<keysize()>
80
81Returns the size (in bytes) of the key used (16, in this case)
82
83=back
84
85=head1 CAVEAT
86
87The internal states of Rabbit are updated every time B<encrypt()> or
88B<decrypt()> are called. And since encryption/decryption depends on the
89internal states, a plaintext encrypted with a call to B<encrypt()> will
90not decrypt to the original message by just a call to B<decrypt()>. The
91proper way to decrypt a ciphertext is to re-initialize the internal
92states (by calling B<new()>) first before calling B<decrypt()>.
93
94=head1 BUG
95
96For the sake of simplicity, the C implementation encrypts and decrypts
97data in multiples of 16 bytes. If the last block of data is not a
98multiple of 16 bytes, it is padded with null characters before
99encryption. The resulting ciphertext is then truncated to the original
100message length before being output. An undesirable consequence of this
101is that encryption/decryption always starts at multiples of 16 bytes of
102the pseudorandom data stream produced by Rabbit. Improvements are most
103welcome. Please read contact.html for contact information.
104
105=head1 COPYRIGHT AND LICENSE
106
107Copyright (C) 2004 Julius C. Duque
108
109Copyright (C) 2003 Cryptico A/S
110
111This library is free software; you can redistribute it and/or modify it
112under the same terms as the GNU General Public License.
113
114This implementation of the Rabbit stream cipher is derived from the
115reference ANSI C code provided in the appendix of the paper, "Rabbit:
116A New High-Performance Stream Cipher", by Martin Boesgaard,
117Mette Vesterager, Thomas Pedersen, Jesper Christiansen, and
118Ove Scavenius of Cryptico A/S.
119
120For more information, please visit the Cryptico website at
121C<http://www.cryptico.com>.
122
123The Rabbit stream cipher is the copyrighted work of Cryptico A/S, and
124use of Rabbit may only be used for non-commercial purposes. Any
125reproduction or redistribution of Rabbit not in accordance with
126Cryptico's license agreement is expressly prohibited by law, and may
127result in severe civil and criminal penalties. Violators will be
128prosecuted to the maximum extent possible.
129
130This copyright does not prohibit distribution of any version of Perl
131containing this extension under the terms of the GNU or Artistic
132licenses.
133
134